Skip to content

Commit

Permalink
Fix memory leak on iOS.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanheise committed Nov 7, 2020
1 parent 35e0577 commit edab9a2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
39 changes: 23 additions & 16 deletions just_audio/darwin/Classes/AudioPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,10 @@ - (void)onFailToComplete:(NSNotification *)notification {
- (void)onComplete:(NSNotification *)notification {
NSLog(@"onComplete");
if (_loopMode == loopOne) {
__weak __typeof__(self) weakSelf = self;
[self seek:kCMTimeZero index:@(_index) completionHandler:^(BOOL finished) {
// XXX: Not necessary?
[self play];
[weakSelf play];
}];
} else {
IndexedPlayerItem *endedPlayerItem = (IndexedPlayerItem *)notification.object;
Expand All @@ -645,16 +646,18 @@ - (void)onComplete:(NSNotification *)notification {
// sources.
// For now we just do a seek back to the start.
if ([_order count] == 1) {
__weak __typeof__(self) weakSelf = self;
[self seek:kCMTimeZero index:_order[0] completionHandler:^(BOOL finished) {
// XXX: Necessary?
[self play];
[weakSelf play];
}];
} else {
// When an item ends, seek back to its beginning.
[endedSource seek:kCMTimeZero];
__weak __typeof__(self) weakSelf = self;
[self seek:kCMTimeZero index:_order[0] completionHandler:^(BOOL finished) {
// XXX: Necessary?
[self play];
[weakSelf play];
}];
}
} else {
Expand Down Expand Up @@ -719,9 +722,10 @@ - (void)observeValueForKeyPath:(NSString *)keyPath
_loadResult = nil;
}
if (CMTIME_IS_VALID(_initialPos) && CMTIME_COMPARE_INLINE(_initialPos, >, kCMTimeZero)) {
__weak __typeof__(self) weakSelf = self;
[playerItem.audioSource seek:_initialPos completionHandler:^(BOOL finished) {
[self updatePosition];
[self broadcastPlaybackEvent];
[weakSelf updatePosition];
[weakSelf broadcastPlaybackEvent];
}];
_initialPos = kCMTimeZero;
}
Expand Down Expand Up @@ -841,10 +845,11 @@ - (void)observeValueForKeyPath:(NSString *)keyPath
[self enterBuffering:@"currentItem changed, seeking"];
[self updatePosition];
[self broadcastPlaybackEvent];
__weak __typeof__(self) weakSelf = self;
[source seek:kCMTimeZero completionHandler:^(BOOL finished) {
[self leaveBuffering:@"currentItem changed, finished seek"];
[self updatePosition];
[self broadcastPlaybackEvent];
[weakSelf leaveBuffering:@"currentItem changed, finished seek"];
[weakSelf updatePosition];
[weakSelf broadcastPlaybackEvent];
if (shouldResumePlayback) {
_player.actionAtItemEnd = originalEndAction;
// TODO: This logic is almost duplicated in seek. See if we can reuse this code.
Expand Down Expand Up @@ -1135,8 +1140,9 @@ - (void)seek:(CMTime)position index:(NSNumber *)newIndex completionHandler:(void
[self enterBuffering:@"seek"];
[self updatePosition];
[self broadcastPlaybackEvent];
__weak __typeof__(self) weakSelf = self;
[_indexedAudioSources[_index] seek:position completionHandler:^(BOOL finished) {
[self updatePosition];
[weakSelf updatePosition];
if (_playing) {
// If playing, buffering will be detected either by:
// 1. checkForDiscontinuity
Expand All @@ -1156,17 +1162,17 @@ - (void)seek:(CMTime)position index:(NSNumber *)newIndex completionHandler:(void
// !playbackBufferEmpty. Although this always seems to
// be full even right after a seek.
if (_player.currentItem.playbackBufferEmpty) {
[self enterBuffering:@"seek finished, playbackBufferEmpty"];
[weakSelf enterBuffering:@"seek finished, playbackBufferEmpty"];
} else {
[self leaveBuffering:@"seek finished, !playbackBufferEmpty"];
[weakSelf leaveBuffering:@"seek finished, !playbackBufferEmpty"];
}
[self updatePosition];
[weakSelf updatePosition];
if (_processingState != buffering) {
[self broadcastPlaybackEvent];
[weakSelf broadcastPlaybackEvent];
}
}
_seekPos = kCMTimeInvalid;
[self broadcastPlaybackEvent];
[weakSelf broadcastPlaybackEvent];
if (completionHandler) {
completionHandler(finished);
}
Expand All @@ -1191,6 +1197,7 @@ - (void)dispose {
for (int i = 0; i < [_indexedAudioSources count]; i++) {
[self removeItemObservers:_indexedAudioSources[i].playerItem];
}
_indexedAudioSources = nil;
}
_audioSource = nil;
if (_player) {
Expand All @@ -1201,8 +1208,8 @@ - (void)dispose {
_player = nil;
}
// Untested:
// [_eventChannel setStreamHandler:nil];
// [_methodChannel setMethodHandler:nil];
[_eventChannel setStreamHandler:nil];
[_methodChannel setMethodCallHandler:nil];
}

@end
14 changes: 2 additions & 12 deletions just_audio/darwin/Classes/IndexedPlayerItem.m
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
#import "IndexedPlayerItem.h"
#import "IndexedAudioSource.h"

@implementation IndexedPlayerItem {
IndexedAudioSource *_audioSource;
}

-(void)setAudioSource:(IndexedAudioSource *)audioSource {
_audioSource = audioSource;
}

-(IndexedAudioSource *)audioSource {
return _audioSource;
}

@implementation IndexedPlayerItem
@synthesize audioSource;
@end
2 changes: 1 addition & 1 deletion just_audio/ios/Classes/IndexedPlayerItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

@interface IndexedPlayerItem : AVPlayerItem

@property (readwrite, nonatomic) IndexedAudioSource *audioSource;
@property (readwrite, nonatomic, weak) IndexedAudioSource *audioSource;

@end
2 changes: 1 addition & 1 deletion just_audio/macos/Classes/IndexedPlayerItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

@interface IndexedPlayerItem : AVPlayerItem

@property (readwrite, nonatomic) IndexedAudioSource *audioSource;
@property (readwrite, nonatomic, weak) IndexedAudioSource *audioSource;

@end

0 comments on commit edab9a2

Please sign in to comment.