VLCServerBrowsingController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*****************************************************************************
  2. * VLC for iOS
  3. *****************************************************************************
  4. * Copyright (c) 2015-2018 VideoLAN. All rights reserved.
  5. * $Id$
  6. *
  7. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  8. * Felix Paul Kühne <fkuehne # videolan.org>
  9. * Pierre SAGASPE <pierre.sagaspe # me.com>
  10. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "VLCServerBrowsingController.h"
  14. #import "NSString+SupportedMedia.h"
  15. #import "UIDevice+VLC.h"
  16. #import "VLCPlaybackService.h"
  17. #if TARGET_OS_TV
  18. #import "VLCFullscreenMovieTVViewController.h"
  19. #import "MetaDataFetcherKit.h"
  20. #endif
  21. #if DOWNLOAD_SUPPORTED
  22. #import "VLCDownloadViewController.h"
  23. #endif
  24. @implementation VLCServerBrowsingController
  25. - (instancetype)initWithViewController:(UIViewController *)viewController serverBrowser:(id<VLCNetworkServerBrowser>)browser
  26. {
  27. self = [super init];
  28. if (self) {
  29. _viewController = viewController;
  30. _serverBrowser = browser;
  31. #if TARGET_OS_TV
  32. if (![kVLCfortvOSMovieDBKey isEqualToString:@""]) {
  33. MDFMovieDBSessionManager *movieDBSessionManager = [MDFMovieDBSessionManager sharedInstance];
  34. movieDBSessionManager.apiKey = kVLCfortvOSMovieDBKey;
  35. [movieDBSessionManager fetchProperties];
  36. }
  37. #endif
  38. }
  39. return self;
  40. }
  41. #pragma mark -
  42. - (NSByteCountFormatter *)byteCounterFormatter
  43. {
  44. if (!_byteCountFormatter)
  45. _byteCountFormatter = [[NSByteCountFormatter alloc] init];
  46. return _byteCountFormatter;
  47. }
  48. - (UIImage *)genericFileImage
  49. {
  50. if (!_genericFileImage)
  51. _genericFileImage = [UIImage imageNamed:@"blank"];
  52. return _genericFileImage;
  53. }
  54. - (UIImage *)folderImage
  55. {
  56. if (!_folderImage)
  57. _folderImage = [UIImage imageNamed:@"folder"];
  58. return _folderImage;
  59. }
  60. #pragma mark - cell configuration
  61. - (void)configureCell:(id<VLCRemoteBrowsingCell>)cell withItem:(id<VLCNetworkServerBrowserItem>)item
  62. {
  63. if (item.isContainer) {
  64. cell.isDirectory = YES;
  65. cell.thumbnailImage = self.folderImage;
  66. } else {
  67. cell.isDirectory = NO;
  68. cell.thumbnailImage = self.genericFileImage;
  69. NSString *sizeString = item.fileSizeBytes ? [self.byteCounterFormatter stringFromByteCount:item.fileSizeBytes.longLongValue] : nil;
  70. NSString *duration = nil;
  71. if ([item respondsToSelector:@selector(duration)])
  72. duration = item.duration;
  73. NSString *subtitle = nil;
  74. if (sizeString && duration) {
  75. subtitle = [NSString stringWithFormat:@"%@ (%@)",duration, sizeString];
  76. } else if (sizeString) {
  77. subtitle = sizeString;
  78. } else if (duration) {
  79. subtitle = duration;
  80. }
  81. cell.subtitle = subtitle;
  82. #if DOWNLOAD_SUPPORTED
  83. if ([item respondsToSelector:@selector(isDownloadable)])
  84. cell.isDownloadable = item.isDownloadable;
  85. else
  86. cell.isDownloadable = NO;
  87. #endif
  88. }
  89. cell.title = item.name;
  90. NSURL *thumbnailURL = nil;
  91. if ([item respondsToSelector:@selector(thumbnailURL)])
  92. thumbnailURL = item.thumbnailURL;
  93. [cell setThumbnailURL:thumbnailURL];
  94. }
  95. #pragma mark - subtitles
  96. - (NSArray<NSURL*> *)_searchSubtitle:(NSString *)url
  97. {
  98. NSString *urlTemp = [[url lastPathComponent] stringByDeletingPathExtension];
  99. NSMutableArray<NSURL*> *urls = [NSMutableArray arrayWithArray:[self.serverBrowser.items valueForKey:@"URL"]];
  100. NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"SELF.path contains[c] %@", urlTemp];
  101. [urls filterUsingPredicate:namePredicate];
  102. NSPredicate *formatPrediate = [NSPredicate predicateWithBlock:^BOOL(NSURL *_Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
  103. return [evaluatedObject.path isSupportedSubtitleFormat];
  104. }];
  105. [urls filterUsingPredicate:formatPrediate];
  106. return [NSArray arrayWithArray:urls];
  107. }
  108. - (NSString *)_getFileSubtitleFromServer:(NSURL *)subtitleURL
  109. {
  110. NSString *FileSubtitlePath = nil;
  111. NSData *receivedSub = [NSData dataWithContentsOfURL:subtitleURL]; // TODO: fix synchronous load
  112. if (receivedSub.length < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
  113. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  114. NSString *directoryPath = searchPaths[0];
  115. FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[subtitleURL lastPathComponent]];
  116. NSFileManager *fileManager = [NSFileManager defaultManager];
  117. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  118. //create local subtitle file
  119. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  120. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  121. APLog(@"file creation failed, no data was saved");
  122. return nil;
  123. }
  124. }
  125. [receivedSub writeToFile:FileSubtitlePath atomically:YES];
  126. } else {
  127. [self.viewController vlc_showAlertWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  128. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), [subtitleURL lastPathComponent], [[UIDevice currentDevice] model]]
  129. buttonTitle:NSLocalizedString(@"BUTTON_OK", nil)];
  130. }
  131. return FileSubtitlePath;
  132. }
  133. - (void)configureSubtitlesInMediaList:(VLCMediaList *)mediaList
  134. {
  135. NSArray *items = self.serverBrowser.items;
  136. id<VLCNetworkServerBrowserItem> loopItem;
  137. [mediaList lock];
  138. NSUInteger count = mediaList.count;
  139. for (NSUInteger i = 0; i < count; i++) {
  140. loopItem = items[i];
  141. NSString *URLofSubtitle = nil;
  142. NSURL *remoteSubtitleURL = nil;
  143. if ([loopItem respondsToSelector:@selector(subtitleURL)]) {
  144. remoteSubtitleURL = [loopItem subtitleURL];
  145. }
  146. if (remoteSubtitleURL == nil) {
  147. NSArray *subtitlesList = [self _searchSubtitle:loopItem.URL.lastPathComponent];
  148. remoteSubtitleURL = subtitlesList.firstObject;
  149. }
  150. if(remoteSubtitleURL != nil) {
  151. URLofSubtitle = [self _getFileSubtitleFromServer:remoteSubtitleURL];
  152. if (URLofSubtitle != nil)
  153. [[mediaList mediaAtIndex:i] addOptions:@{ kVLCSettingSubtitlesFilePath : URLofSubtitle }];
  154. }
  155. }
  156. [mediaList unlock];
  157. }
  158. #pragma mark - File Streaming
  159. - (void)showMovieViewController
  160. {
  161. #if TARGET_OS_TV
  162. VLCFullscreenMovieTVViewController *moviewVC = [VLCFullscreenMovieTVViewController fullscreenMovieTVViewController];
  163. [self.viewController presentViewController:moviewVC
  164. animated:YES
  165. completion:nil];
  166. #endif
  167. }
  168. - (void)streamMediaList:(VLCMediaList *)mediaList startingAtIndex:(NSInteger)startIndex
  169. {
  170. VLCPlaybackService *vpc = [VLCPlaybackService sharedInstance];
  171. vpc.fullscreenSessionRequested = YES;
  172. [vpc playMediaList:mediaList firstIndex:startIndex subtitlesFilePath:nil];
  173. [self showMovieViewController];
  174. }
  175. - (void)streamFileForItem:(id<VLCNetworkServerBrowserItem>)item
  176. {
  177. NSString *URLofSubtitle = nil;
  178. NSURL *remoteSubtitleURL = nil;
  179. if ([item respondsToSelector:@selector(subtitleURL)])
  180. remoteSubtitleURL = [item subtitleURL];
  181. if (!remoteSubtitleURL) {
  182. NSArray *SubtitlesList = [self _searchSubtitle:item.URL.lastPathComponent];
  183. remoteSubtitleURL = SubtitlesList.firstObject;
  184. }
  185. if(remoteSubtitleURL)
  186. URLofSubtitle = [self _getFileSubtitleFromServer:remoteSubtitleURL];
  187. VLCPlaybackService *vpc = [VLCPlaybackService sharedInstance];
  188. vpc.fullscreenSessionRequested = YES;
  189. VLCMediaList *medialist = [[VLCMediaList alloc] init];
  190. [medialist addMedia:[VLCMedia mediaWithURL:item.URL]];
  191. [vpc playMediaList:medialist firstIndex:0 subtitlesFilePath:URLofSubtitle];
  192. [self showMovieViewController];
  193. }
  194. #pragma mark - Downloads
  195. #if DOWNLOAD_SUPPORTED
  196. - (BOOL)triggerDownloadForItem:(id<VLCNetworkServerBrowserItem>)item
  197. {
  198. // is item supposed to be not downloadable?
  199. if ([item respondsToSelector:@selector(isDownloadable)] && ![item isDownloadable]) {
  200. return NO;
  201. }
  202. // if the item has no URL we can't download it
  203. if (!item.URL) {
  204. return NO;
  205. }
  206. if (item.fileSizeBytes.longLongValue < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
  207. [self _downloadItem:item];
  208. return YES;
  209. } else {
  210. NSString *title = NSLocalizedString(@"DISK_FULL", nil);
  211. NSString *messsage = [NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), item.name, [[UIDevice currentDevice] model]];
  212. NSString *button = NSLocalizedString(@"BUTTON_OK", nil);
  213. [self.viewController vlc_showAlertWithTitle:title message:messsage buttonTitle:button];
  214. return NO;
  215. }
  216. }
  217. - (void)_downloadItem:(id<VLCNetworkServerBrowserItem>)item
  218. {
  219. NSString *filename;
  220. if ([item respondsToSelector:@selector(filename)])
  221. filename = item.filename;
  222. else
  223. filename = item.name;
  224. if (filename.pathExtension.length == 0) {
  225. /* there are few crappy UPnP servers who don't reveal the correct file extension, so we use a generic fake (#11123) */
  226. NSString *urlExtension = item.URL.pathExtension;
  227. NSString *extension = urlExtension.length != 0 ? urlExtension : @"vlc";
  228. filename = [filename stringByAppendingPathExtension:extension];
  229. }
  230. [[VLCDownloadViewController sharedInstance] addURLToDownloadList:item.URL fileNameOfMedia:filename];
  231. if (item.subtitleURL)
  232. [self getFileSubtitleFromServer:item];
  233. }
  234. - (void)getFileSubtitleFromServer:(id<VLCNetworkServerBrowserItem>)item
  235. {
  236. NSString *filename = nil;
  237. if ([item respondsToSelector:@selector(filename)])
  238. filename = item.filename;
  239. else
  240. filename = item.name;
  241. NSString *FileSubtitlePath = nil;
  242. NSURL *subtitleURL = item.subtitleURL;
  243. NSString *extension = [subtitleURL pathExtension];
  244. if ([extension isEqualToString:@""]) {
  245. if ([item respondsToSelector:@selector(subtitleType)]) {
  246. extension = item.subtitleType;
  247. } else {
  248. /* insert a generic subtitle file extension here because otherwise the file would be lost */
  249. extension = @"sub";
  250. }
  251. }
  252. filename = [NSString stringWithFormat:@"%@.%@", [filename stringByDeletingPathExtension], extension];
  253. NSData *receivedSub = [NSData dataWithContentsOfURL:subtitleURL]; // TODO: fix synchronous load
  254. if (receivedSub.length < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
  255. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  256. NSString *directoryPath = searchPaths[0];
  257. FileSubtitlePath = [directoryPath stringByAppendingPathComponent:filename];
  258. NSFileManager *fileManager = [NSFileManager defaultManager];
  259. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  260. //create local subtitle file
  261. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  262. if (![fileManager fileExistsAtPath:FileSubtitlePath])
  263. APLog(@"file creation failed, no data was saved");
  264. }
  265. [receivedSub writeToFile:FileSubtitlePath atomically:YES];
  266. } else
  267. APLog(@"Disk full");
  268. }
  269. #endif
  270. @end