Browse Source

network stream: mostly re-write the SPU via http detector (fixes #132)

This adds support for https, works-around an issue in libvlc when using private API to set a subtitle and uses libvlc 3.0's feature to play subtitles via http so they no longer need to be downloaded first

(cherry picked from commit b8d720409a334c6399901b773244a2e44055303f)
Felix Paul Kühne 6 years ago
parent
commit
892dc98052
2 changed files with 22 additions and 49 deletions
  1. 10 37
      Sources/VLCOpenNetworkStreamViewController.m
  2. 12 12
      Sources/VLCPlaybackController.m

+ 10 - 37
Sources/VLCOpenNetworkStreamViewController.m

@@ -367,30 +367,29 @@ forRowAtIndexPath:(NSIndexPath *)indexPath
 #pragma mark - internals
 - (void)_openURLStringAndDismiss:(NSString *)url
 {
-    NSURL *URLscheme = [NSURL URLWithString:url];
-    NSString *URLofSubtitle = nil;
+    NSURL *playbackURL = [NSURL URLWithString:url];
+    NSURL *subtitlesURL = nil;
 
-    if ([URLscheme.scheme isEqualToString:@"http"] && self.ScanSubToggleSwitch.on) {
-        URLofSubtitle = [self _checkURLofSubtitle:url];
+    if (([playbackURL.scheme isEqualToString:@"http"] || [playbackURL.scheme isEqualToString:@"https"]) && self.ScanSubToggleSwitch.on) {
+        subtitlesURL = [self _checkURLofSubtitle:playbackURL];
     }
 
     VLCMedia *media = [VLCMedia mediaWithURL:[NSURL URLWithString:url]];
     VLCMediaList *medialist = [[VLCMediaList alloc] init];
     [medialist addMedia:media];
-    [[VLCPlaybackController sharedInstance] playMediaList:medialist firstIndex:0 subtitlesFilePath:URLofSubtitle];
+    [[VLCPlaybackController sharedInstance] playMediaList:medialist firstIndex:0 subtitlesFilePath:subtitlesURL.absoluteString];
 }
 
-- (NSString *)_checkURLofSubtitle:(NSString *)url
+- (NSURL *)_checkURLofSubtitle:(NSURL *)url
 {
     NSCharacterSet *characterFilter = [NSCharacterSet characterSetWithCharactersInString:@"\\.():$"];
     NSString *subtitleFileExtensions = [[kSupportedSubtitleFileExtensions componentsSeparatedByCharactersInSet:characterFilter] componentsJoinedByString:@""];
     NSArray *arraySubtitleFileExtensions = [subtitleFileExtensions componentsSeparatedByString:@"|"];
-    NSString *urlTemp = [[url stringByDeletingPathExtension] stringByAppendingString:@"."];
+    NSURL *urlWithoutExtension = [url URLByDeletingPathExtension];
     NSUInteger count = arraySubtitleFileExtensions.count;
 
     for (int i = 0; i < count; i++) {
-        NSString *checkAddress = [urlTemp stringByAppendingString:arraySubtitleFileExtensions[i]];
-        NSURL *checkURL = [NSURL URLWithString:checkAddress];
+        NSURL *checkURL = [urlWithoutExtension URLByAppendingPathExtension:arraySubtitleFileExtensions[i]];
         NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:checkURL];
         request.HTTPMethod = @"HEAD";
 
@@ -400,39 +399,13 @@ forRowAtIndexPath:(NSIndexPath *)indexPath
         NSInteger httpStatus = [(NSHTTPURLResponse *)response statusCode];
 
         if (httpStatus == 200) {
-            NSString *fileSubtitlePath = [self _getFileSubtitleFromServer:checkURL];
-            return fileSubtitlePath;
+            APLog(@"%s:found matching spu file: %@", __PRETTY_FUNCTION__, checkURL);
+            return checkURL;
         }
     }
     return nil;
 }
 
-- (NSString *)_getFileSubtitleFromServer:(NSURL *)url
-{
-    NSString *fileSubtitlePath = nil;
-    NSString *fileName = [[url path] lastPathComponent];
-    NSData *receivedSub = [NSData dataWithContentsOfURL:url];
-
-    if (receivedSub.length < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
-        NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
-        NSString *directoryPath = [searchPaths objectAtIndex:0];
-        fileSubtitlePath = [directoryPath stringByAppendingPathComponent:fileName];
-        NSFileManager *fileManager = [NSFileManager defaultManager];
-        if (![fileManager fileExistsAtPath:fileSubtitlePath]) {
-            [fileManager createFileAtPath:fileSubtitlePath contents:nil attributes:nil];
-            if (![fileManager fileExistsAtPath:fileSubtitlePath])
-                APLog(@"file creation failed, no data was saved");
-        }
-        [receivedSub writeToFile:fileSubtitlePath atomically:YES];
-    } else {
-        [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"DISK_FULL", nil)
-                                             errorMessage:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), fileName, [[UIDevice currentDevice] model]]
-                                           viewController:self];
-    }
-
-    return fileSubtitlePath;
-}
-
 - (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error
 {
     NSError __block *erreur = NULL;

+ 12 - 12
Sources/VLCPlaybackController.m

@@ -194,10 +194,7 @@ typedef NS_ENUM(NSUInteger, VLCAspectRatio) {
     _actualVideoOutputView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
     _actualVideoOutputView.autoresizesSubviews = YES;
 
-    if (_pathToExternalSubtitlesFile)
-        _listPlayer = [[VLCMediaListPlayer alloc] initWithOptions:@[[NSString stringWithFormat:@"--%@=%@", kVLCSettingSubtitlesFilePath, _pathToExternalSubtitlesFile]] andDrawable:_actualVideoOutputView];
-    else
-        _listPlayer = [[VLCMediaListPlayer alloc] initWithDrawable:_actualVideoOutputView];
+    _listPlayer = [[VLCMediaListPlayer alloc] initWithDrawable:_actualVideoOutputView];
 
     /* to enable debug logging for the playback library instance, switch the boolean below
      * note that the library instance used for playback may not necessarily match the instance
@@ -212,8 +209,6 @@ typedef NS_ENUM(NSUInteger, VLCAspectRatio) {
         [_mediaPlayer setDeinterlaceFilter:@"blend"];
     else
         [_mediaPlayer setDeinterlaceFilter:nil];
-    if (_pathToExternalSubtitlesFile)
-        [_mediaPlayer addPlaybackSlave:[NSURL fileURLWithPath:_pathToExternalSubtitlesFile] type:VLCMediaPlaybackSlaveTypeSubtitle enforce:YES];
 
     VLCMedia *media = [_mediaList mediaAtIndex:_itemInMediaListToBePlayedFirst];
     [media parseWithOptions:VLCMediaParseLocal];
@@ -260,6 +255,17 @@ typedef NS_ENUM(NSUInteger, VLCAspectRatio) {
 
     [[self remoteControlService] subscribeToRemoteCommands];
 
+    if (_pathToExternalSubtitlesFile) {
+        /* this could be a path or an absolute string - let's see */
+        NSURL *subtitleURL = [NSURL URLWithString:_pathToExternalSubtitlesFile];
+        if (!subtitleURL) {
+            subtitleURL = [NSURL fileURLWithPath:_pathToExternalSubtitlesFile];
+        }
+        if (subtitleURL) {
+            [_mediaPlayer addPlaybackSlave:subtitleURL type:VLCMediaPlaybackSlaveTypeSubtitle enforce:YES];
+        }
+    }
+
     _playerIsSetup = YES;
 
     [[NSNotificationCenter defaultCenter] postNotificationName:VLCPlaybackControllerPlaybackDidStart object:self];
@@ -295,12 +301,6 @@ typedef NS_ENUM(NSUInteger, VLCAspectRatio) {
     }
     if (!_sessionWillRestart) {
         _mediaList = nil;
-        if (_pathToExternalSubtitlesFile) {
-            NSFileManager *fileManager = [NSFileManager defaultManager];
-            if ([fileManager fileExistsAtPath:_pathToExternalSubtitlesFile])
-                [fileManager removeItemAtPath:_pathToExternalSubtitlesFile error:nil];
-            _pathToExternalSubtitlesFile = nil;
-        }
     }
     _playerIsSetup = NO;
     [_shuffleStack removeAllObjects];