浏览代码

vpc: implement basic locking to protect start and stop

Felix Paul Kühne 9 年之前
父节点
当前提交
4690a88755
共有 2 个文件被更改,包括 28 次插入2 次删除
  1. 0 2
      Sources/VLCLibraryViewController.m
  2. 28 0
      Sources/VLCPlaybackController.m

+ 0 - 2
Sources/VLCLibraryViewController.m

@@ -845,8 +845,6 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
     NSArray *visibleCells = [collectionView visibleCells];
     NSUInteger cellCount = visibleCells.count;
 
-    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
-
     if (self.editing) {
         if (_libraryMode == VLCLibraryModeCreateFolder) {
             _folderObject = _foundMedia[indexPath.item];

+ 28 - 0
Sources/VLCPlaybackController.m

@@ -65,6 +65,8 @@ NSString *const VLCPlaybackControllerPlaybackPositionUpdated = @"VLCPlaybackCont
     BOOL _mediaWasJustStarted;
     BOOL _recheckForExistingThumbnail;
     BOOL _activeSession;
+
+    NSLock *_playbackSessionManagementLock;
 }
 
 @end
@@ -103,6 +105,8 @@ NSString *const VLCPlaybackControllerPlaybackPositionUpdated = @"VLCPlaybackCont
                               name:UIApplicationDidBecomeActiveNotification object:nil];
         [defaultCenter addObserver:self selector:@selector(applicationDidEnterBackground:)
                               name:UIApplicationDidEnterBackgroundNotification object:nil];
+
+        _playbackSessionManagementLock = [[NSLock alloc] init];
     }
     return self;
 }
@@ -193,6 +197,13 @@ NSString *const VLCPlaybackControllerPlaybackPositionUpdated = @"VLCPlaybackCont
         APLog(@"%s: player is already setup, bailing out", __PRETTY_FUNCTION__);
         return;
     }
+
+    BOOL ret = [_playbackSessionManagementLock tryLock];
+    if (!ret) {
+        APLog(@"%s: locking failed", __PRETTY_FUNCTION__);
+        return;
+    }
+
     _activeSession = YES;
 
 #if TARGET_OS_IOS
@@ -205,6 +216,7 @@ NSString *const VLCPlaybackControllerPlaybackPositionUpdated = @"VLCPlaybackCont
 
     if (!self.url && !self.mediaList) {
         APLog(@"%s: no URL and no media list set, stopping playback", __PRETTY_FUNCTION__);
+        [_playbackSessionManagementLock unlock];
         [self stopPlayback];
         return;
     }
@@ -261,6 +273,8 @@ NSString *const VLCPlaybackControllerPlaybackPositionUpdated = @"VLCPlaybackCont
     }
     [_listPlayer setRepeatMode:VLCDoNotRepeat];
 
+    [_playbackSessionManagementLock unlock];
+
     if (![self _isMediaSuitableForDevice:media]) {
 #if TARGET_OS_IOS
         VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DEVICE_TOOSLOW_TITLE", nil)
@@ -295,6 +309,12 @@ NSString *const VLCPlaybackControllerPlaybackPositionUpdated = @"VLCPlaybackCont
 
 - (void)_playNewMedia
 {
+    BOOL ret = [_playbackSessionManagementLock tryLock];
+    if (!ret) {
+        ASLog(@"%s: locking failed", __PRETTY_FUNCTION__);
+        return;
+    }
+
     // Set last selected equalizer profile
     unsigned int profile = (unsigned int)[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingEqualizerProfile] integerValue];
     [_mediaPlayer resetEqualizerFromProfile:profile];
@@ -321,6 +341,7 @@ NSString *const VLCPlaybackControllerPlaybackPositionUpdated = @"VLCPlaybackCont
     _playerIsSetup = YES;
 
     [[NSNotificationCenter defaultCenter] postNotificationName:VLCPlaybackControllerPlaybackDidStart object:self];
+    [_playbackSessionManagementLock unlock];
 }
 
 #if TARGET_OS_IOS
@@ -335,6 +356,12 @@ NSString *const VLCPlaybackControllerPlaybackPositionUpdated = @"VLCPlaybackCont
 
 - (void)stopPlayback
 {
+    BOOL ret = [_playbackSessionManagementLock tryLock];
+    if (!ret) {
+        APLog(@"%s: locking failed", __PRETTY_FUNCTION__);
+        return;
+    }
+
     if (_mediaPlayer) {
         @try {
             [_mediaPlayer removeObserver:self forKeyPath:@"time"];
@@ -379,6 +406,7 @@ NSString *const VLCPlaybackControllerPlaybackPositionUpdated = @"VLCPlaybackCont
     [self unsubscribeFromRemoteCommand];
     _activeSession = NO;
 
+    [_playbackSessionManagementLock unlock];
     if (_playbackFailed) {
         [[NSNotificationCenter defaultCenter] postNotificationName:VLCPlaybackControllerPlaybackDidFail object:self];
     } else if (!_sessionWillRestart) {