Skip to content

Commit

Permalink
Revert "Use AVPlayer timer w/ notif"
Browse files Browse the repository at this point in the history
This reverts commit 7aad4ea.

Not actually any more efficient, it seems. Lazy sampling of CPU time sez:

0.1 avf: 16%
0.01 avf: 50-60%
0.1: ns: 16%
0.01 nst: 50-55%

nil: 10%

So, I think just decreasing the sample rate would be more beneficial.

Closes GH-77
  • Loading branch information
NattyNarwhal committed Oct 16, 2022
1 parent 7aad4ea commit d7bb154
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
2 changes: 2 additions & 0 deletions Submariner/SBDatabaseController.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@
SBLibrary *library;

CATransition *transition;

NSTimer *progressUpdateTimer;
}

@property (readwrite, strong) NSArray *resourceSortDescriptors;
Expand Down
33 changes: 25 additions & 8 deletions Submariner/SBDatabaseController.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
@interface SBDatabaseController (Private)
- (void)populatedDefaultSections;
- (void)displayViewControllerForResource:(SBResource *)resource;
- (void)updateProgress:(NSTimer *)updatedTimer;

// notifications
- (void)subsonicPlaylistsUpdatedNotification:(NSNotification *)notification;
Expand Down Expand Up @@ -247,12 +248,6 @@ - (void)windowDidLoad {
name:SBPlayerMovieToPlayNotification
object:nil];

// observe playback status
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateProgressNotification:)
name:SBPlayerProgressUpdatedNotification
object:nil];

// observe Subsonic connection
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(subsonicConnectionSucceeded:)
Expand Down Expand Up @@ -895,7 +890,27 @@ - (void)clearPlaybackProgress {
[progressSlider setDoubleValue:0];
}

- (void)updateProgressNotification:(NSNotification *)notification {
- (void)installProgressTimer {
if (progressUpdateTimer != nil) {
return;
}
progressUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:0.01
target:self
selector:@selector(updateProgress:)
userInfo:nil
repeats:YES];
}

- (void)uninstallProgressTimer {
if (progressUpdateTimer == nil) {
return;
}
[progressUpdateTimer invalidate];
progressUpdateTimer = nil;
[self clearPlaybackProgress];
}

- (void)updateProgress:(NSTimer *)updatedTimer {

if([[SBPlayer sharedInstance] isPlaying]) {
[progressSlider setEnabled:YES];
Expand Down Expand Up @@ -1368,14 +1383,16 @@ - (void)playerPlayStateNotification:(NSNotification *)notification {
SBTrack *currentTrack = [[SBPlayer sharedInstance] currentTrack];

if(currentTrack != nil) {
// XXX: Could it be safe to stop the timer when paused?
[self installProgressTimer];
if([[SBPlayer sharedInstance] isPaused]) {
[playPauseButton setState:NSControlStateValueOff];
}
else {
[playPauseButton setState:NSControlStateValueOn];
}
} else {
[self clearPlaybackProgress];
[self uninstallProgressTimer];
[playPauseButton setState:NSControlStateValueOn];
}
}
Expand Down
3 changes: 1 addition & 2 deletions Submariner/SBPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
extern NSString *SBPlayerPlaylistUpdatedNotification;
extern NSString *SBPlayerPlayStateNotification;
extern NSString *SBPlayerMovieToPlayNotification;
extern NSString *SBPlayerProgressUpdatedNotification;



// repeat modes
Expand All @@ -59,7 +59,6 @@ enum SBPlayerRepeatMode {
@interface SBPlayer : NSObject {
@private
AVPlayer *remotePlayer;
id playerObserverToken;

NSMutableArray *playlist;
SBTrack *currentTrack;
Expand Down
8 changes: 0 additions & 8 deletions Submariner/SBPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
NSString *SBPlayerPlaylistUpdatedNotification = @"SBPlayerPlaylistUpdatedNotification";
NSString *SBPlayerPlayStateNotification = @"SBPlayerPlayStateNotification";
NSString *SBPlayerMovieToPlayNotification = @"SBPlayerPlaylistUpdatedNotification";
NSString *SBPlayerProgressUpdatedNotification = @"SBPlayerProgressUpdatedNotification";



Expand Down Expand Up @@ -136,13 +135,7 @@ - (id)init {

// setup observers
[remotePlayer addObserver:self forKeyPath:@"status" options:0 context:nil];
// timed one
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object: nil];
CMTime interval = CMTimeMakeWithSeconds(0.1, NSEC_PER_SEC);
// playerObserverToken = [remotePlayer addPeriodicTimeObserverForInterval: interval queue: NULL usingBlock: ^(CMTime time) {
// // avoid retain cycle
// [[NSNotificationCenter defaultCenter] postNotificationName:SBPlayerProgressUpdatedNotification object: nil];
// }];

playlist = [[NSMutableArray alloc] init];
isShuffle = NO;
Expand All @@ -157,7 +150,6 @@ - (id)init {

- (void)dealloc {
// remove observers
[remotePlayer removeTimeObserver: playerObserverToken];
[remotePlayer removeObserver:self forKeyPath:@"status" context:nil];
[[NSNotificationCenter defaultCenter] removeObserver: self name:AVPlayerItemDidPlayToEndTimeNotification object: nil];
// remove remote player observers
Expand Down

0 comments on commit d7bb154

Please sign in to comment.