VLCServerBrowsingController.m 11 KB

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