VLCRemotePlaybackViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*****************************************************************************
  2. * VLC for iOS
  3. *****************************************************************************
  4. * Copyright (c) 2015-2019 VideoLAN. All rights reserved.
  5. * $Id$
  6. *
  7. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  8. * Tobias Conradi <videolan # tobias-conradi.de>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCRemotePlaybackViewController.h"
  13. #import "Reachability.h"
  14. #import "VLCHTTPUploaderController.h"
  15. #import "VLCMediaFileDiscoverer.h"
  16. #import "VLCRemoteBrowsingTVCell.h"
  17. #import "VLCMaskView.h"
  18. #import "CAAnimation+VLCWiggle.h"
  19. #import "NSString+SupportedMedia.h"
  20. #import "VLC-Swift.h"
  21. #define remotePlaybackReuseIdentifer @"remotePlaybackReuseIdentifer"
  22. @interface VLCRemotePlaybackViewController () <UICollectionViewDataSource, UICollectionViewDelegate, VLCMediaFileDiscovererDelegate>
  23. @property (strong, nonatomic) Reachability *reachability;
  24. @property (strong, nonatomic) NSMutableArray<NSString *> *discoveredFiles;
  25. @property (strong, nonatomic) VLCMediaThumbnailerCache *thumbnailerCache;
  26. @property (nonatomic) NSIndexPath *currentlyFocusedIndexPath;
  27. @end
  28. @implementation VLCRemotePlaybackViewController
  29. - (NSString *)title
  30. {
  31. return NSLocalizedString(@"WEBINTF_TITLE_ATV", nil);
  32. }
  33. - (void)viewDidLoad
  34. {
  35. [super viewDidLoad];
  36. UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.cachedMediaCollectionView.collectionViewLayout;
  37. const CGFloat inset = 50.;
  38. flowLayout.sectionInset = UIEdgeInsetsMake(inset, inset, inset, inset);
  39. flowLayout.itemSize = CGSizeMake(250.0, 300.0);
  40. flowLayout.minimumInteritemSpacing = 48.0;
  41. flowLayout.minimumLineSpacing = 100.0;
  42. [self.cachedMediaCollectionView registerNib:[UINib nibWithNibName:@"VLCRemoteBrowsingTVCell" bundle:nil]
  43. forCellWithReuseIdentifier:VLCRemoteBrowsingTVCellIdentifier];
  44. self.reachability = [Reachability reachabilityForLocalWiFi];
  45. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  46. [notificationCenter addObserver:self
  47. selector:@selector(reachabilityChanged)
  48. name:kReachabilityChangedNotification
  49. object:nil];
  50. VLCMediaFileDiscoverer *discoverer = [VLCMediaFileDiscoverer sharedInstance];
  51. discoverer.filterResultsForPlayability = NO;
  52. self.discoveredFiles = [NSMutableArray array];
  53. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  54. discoverer.directoryPath = [[searchPaths firstObject] stringByAppendingPathComponent:kVLCHTTPUploadDirectory];
  55. [discoverer addObserver:self];
  56. [discoverer startDiscovering];
  57. self.cachedMediaLabel.text = NSLocalizedString(@"CACHED_MEDIA", nil);
  58. self.cachedMediaLongLabel.text = NSLocalizedString(@"CACHED_MEDIA_LONG", nil);
  59. /* After day 354 of the year, the usual VLC cone is replaced by another cone
  60. * wearing a Father Xmas hat.
  61. * Note: this icon doesn't represent an endorsement of The Coca-Cola Company
  62. * and should not be confused with the idea of religious statements or propagation there off
  63. */
  64. NSCalendar *gregorian =
  65. [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
  66. NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitYear forDate:[NSDate date]];
  67. if (dayOfYear >= 354)
  68. self.cachedMediaConeImageView.image = [UIImage imageNamed:@"xmas-cone"];
  69. self.thumbnailerCache = [VLCMediaThumbnailerCache alloc];
  70. [[NSNotificationCenter defaultCenter] addObserver:self
  71. selector:@selector(realoadMediaCollectionView:)
  72. name:@"thumbnailIComplete" object:nil];
  73. }
  74. - (void)viewDidLayoutSubviews
  75. {
  76. [super viewDidLayoutSubviews];
  77. UICollectionView *collectionView = self.cachedMediaCollectionView;
  78. VLCMaskView *maskView = (VLCMaskView *)collectionView.maskView;
  79. maskView.maskEnd = self.topLayoutGuide.length * 0.8;
  80. /*
  81. Update the position from where the collection view's content should
  82. start to fade out. The size of the fade increases as the collection
  83. view scrolls to a maximum of half the navigation bar's height.
  84. */
  85. CGFloat maximumMaskStart = maskView.maskEnd + (self.topLayoutGuide.length * 0.5);
  86. CGFloat verticalScrollPosition = MAX(0, collectionView.contentOffset.y + collectionView.contentInset.top);
  87. maskView.maskStart = MIN(maximumMaskStart, maskView.maskEnd + verticalScrollPosition);
  88. /*
  89. Position the mask view so that it is always fills the visible area of
  90. the collection view.
  91. */
  92. CGSize collectionViewSize = collectionView.bounds.size;
  93. maskView.frame = CGRectMake(0, collectionView.contentOffset.y, collectionViewSize.width, collectionViewSize.height);
  94. }
  95. - (void)viewWillAppear:(BOOL)animated
  96. {
  97. [super viewWillAppear:animated];
  98. [[VLCMediaFileDiscoverer sharedInstance] updateMediaList];
  99. [self.reachability startNotifier];
  100. [self updateHTTPServerAddress];
  101. }
  102. - (void)viewWillDisappear:(BOOL)animated
  103. {
  104. [super viewWillDisappear:animated];
  105. [self.reachability stopNotifier];
  106. }
  107. - (void)reachabilityChanged
  108. {
  109. [self updateHTTPServerAddress];
  110. }
  111. - (void)updateHTTPServerAddress
  112. {
  113. BOOL connectedViaWifi = self.reachability.currentReachabilityStatus == ReachableViaWiFi;
  114. self.toggleHTTPServerButton.enabled = connectedViaWifi;
  115. NSString *uploadText = connectedViaWifi ? [[VLCHTTPUploaderController sharedInstance] httpStatus] : NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", nil);
  116. self.httpServerLabel.text = uploadText;
  117. if (connectedViaWifi && [VLCHTTPUploaderController sharedInstance].isServerRunning)
  118. [self.toggleHTTPServerButton setTitle:NSLocalizedString(@"HTTP_SERVER_ON", nil) forState:UIControlStateNormal];
  119. else
  120. [self.toggleHTTPServerButton setTitle:NSLocalizedString(@"HTTP_SERVER_OFF", nil) forState:UIControlStateNormal];
  121. }
  122. - (void)toggleHTTPServer:(id)sender
  123. {
  124. BOOL futureHTTPServerState = ![VLCHTTPUploaderController sharedInstance].isServerRunning ;
  125. [[NSUserDefaults standardUserDefaults] setBool:futureHTTPServerState forKey:kVLCSettingSaveHTTPUploadServerStatus];
  126. [[VLCHTTPUploaderController sharedInstance] changeHTTPServerState:futureHTTPServerState];
  127. [self updateHTTPServerAddress];
  128. [[NSUserDefaults standardUserDefaults] synchronize];
  129. }
  130. #pragma mark - collection view data source
  131. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  132. {
  133. VLCRemoteBrowsingTVCell *cell = (VLCRemoteBrowsingTVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:VLCRemoteBrowsingTVCellIdentifier forIndexPath:indexPath];
  134. return cell;
  135. }
  136. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  137. {
  138. return 1;
  139. }
  140. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(VLCRemoteBrowsingTVCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  141. {
  142. NSString *cellTitle;
  143. NSUInteger row = indexPath.row;
  144. NSURL *thumbnailURL = nil;
  145. @synchronized(self.discoveredFiles) {
  146. if (self.discoveredFiles.count > row) {
  147. cellTitle = [self.discoveredFiles[row] lastPathComponent];
  148. if (cellTitle.isSupportedMediaFormat) {
  149. thumbnailURL = [self.thumbnailerCache getThumbnailURL:self.discoveredFiles[row]];
  150. }
  151. }
  152. }
  153. [cell prepareForReuse];
  154. [cell setIsDirectory:NO];
  155. if (thumbnailURL) {
  156. [cell setThumbnailURL:thumbnailURL];
  157. } else if (cellTitle.isSupportedMediaFormat) {
  158. [cell setThumbnailImage:[UIImage imageNamed:@"movie"]];
  159. } else if (cellTitle.isSupportedAudioMediaFormat) {
  160. [cell setThumbnailImage:[UIImage imageNamed:@"audio"]];
  161. } else {
  162. [cell setThumbnailImage:[UIImage imageNamed:@"blank"]];
  163. }
  164. [cell setTitle:cellTitle];
  165. }
  166. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  167. {
  168. NSUInteger ret;
  169. @synchronized(self.discoveredFiles) {
  170. ret = self.discoveredFiles.count;
  171. }
  172. self.cachedMediaConeImageView.hidden = ret > 0;
  173. return ret;
  174. }
  175. -(BOOL)collectionView:(UICollectionView *)collectionView shouldUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context
  176. {
  177. if (self.editing) {
  178. return context.nextFocusedIndexPath == nil;
  179. }
  180. return YES;
  181. }
  182. - (void)collectionView:(UICollectionView *)collectionView didUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
  183. {
  184. NSIndexPath *nextPath = context.nextFocusedIndexPath;
  185. if (!nextPath) {
  186. self.editing = NO;
  187. }
  188. self.currentlyFocusedIndexPath = nextPath;
  189. }
  190. #pragma mark - editing
  191. - (NSIndexPath *)indexPathToDelete
  192. {
  193. NSIndexPath *indexPathToDelete = self.currentlyFocusedIndexPath;
  194. return indexPathToDelete;
  195. }
  196. - (NSString *)itemToDelete
  197. {
  198. NSIndexPath *indexPathToDelete = self.indexPathToDelete;
  199. if (!indexPathToDelete) {
  200. return nil;
  201. }
  202. NSString *ret = nil;
  203. @synchronized(self.discoveredFiles) {
  204. NSInteger index = indexPathToDelete.item;
  205. if (index < self.discoveredFiles.count) {
  206. ret = self.discoveredFiles[index];
  207. }
  208. }
  209. return ret;
  210. }
  211. - (void)setEditing:(BOOL)editing
  212. {
  213. [super setEditing:editing];
  214. UICollectionViewCell *focusedCell = [self.cachedMediaCollectionView cellForItemAtIndexPath:self.currentlyFocusedIndexPath];
  215. if (editing) {
  216. [focusedCell.layer addAnimation:[CAAnimation vlc_wiggleAnimationwithSoftMode:NO]
  217. forKey:VLCWiggleAnimationKey];
  218. } else {
  219. [focusedCell.layer removeAnimationForKey:VLCWiggleAnimationKey];
  220. }
  221. }
  222. - (void)deleteFileAtIndex:(NSIndexPath *)indexPathToDelete
  223. {
  224. [super deleteFileAtIndex:indexPathToDelete];
  225. if (!indexPathToDelete) {
  226. return;
  227. }
  228. __block NSString *fileToDelete = nil;
  229. [self.cachedMediaCollectionView performBatchUpdates:^{
  230. @synchronized(self.discoveredFiles) {
  231. fileToDelete = self.discoveredFiles[indexPathToDelete.item];
  232. [self.discoveredFiles removeObject:fileToDelete];
  233. }
  234. [self.cachedMediaCollectionView deleteItemsAtIndexPaths:@[indexPathToDelete]];
  235. } completion:^(BOOL finished) {
  236. [[NSFileManager defaultManager] removeItemAtPath:fileToDelete error:nil];
  237. self.editing = NO;
  238. }];
  239. }
  240. #pragma mark - collection view delegate
  241. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  242. {
  243. NSURL *url;
  244. @synchronized(self.discoveredFiles) {
  245. url = [NSURL fileURLWithPath:self.discoveredFiles[indexPath.row]];
  246. }
  247. VLCMediaList *medialist = [[VLCMediaList alloc] init];
  248. [medialist addMedia:[VLCMedia mediaWithURL:url]];
  249. [[VLCPlaybackService sharedInstance] playMediaList:medialist firstIndex:0 subtitlesFilePath:nil];
  250. [self presentViewController:[VLCFullscreenMovieTVViewController fullscreenMovieTVViewController]
  251. animated:YES
  252. completion:nil];
  253. }
  254. #pragma mark - media file discovery
  255. - (void)mediaFilesFoundRequiringAdditionToStorageBackend:(NSArray<NSString *> *)foundFiles
  256. {
  257. @synchronized(self.discoveredFiles) {
  258. self.discoveredFiles = [NSMutableArray arrayWithArray:foundFiles];
  259. for (int cnt = 0; cnt < [self.discoveredFiles count]; cnt++) {
  260. if (self.discoveredFiles[cnt].isSupportedMediaFormat) {
  261. [self.thumbnailerCache getVideoThumbnail:self.discoveredFiles[cnt]];
  262. }
  263. }
  264. }
  265. [self.cachedMediaCollectionView reloadData];
  266. }
  267. - (void)mediaFileAdded:(NSString *)filePath loading:(BOOL)isLoading
  268. {
  269. @synchronized(self.discoveredFiles) {
  270. if (![self.discoveredFiles containsObject:filePath]) {
  271. [self.discoveredFiles addObject:filePath];
  272. }
  273. }
  274. [self.cachedMediaCollectionView reloadData];
  275. }
  276. - (void)mediaFileDeleted:(NSString *)filePath
  277. {
  278. @synchronized(self.discoveredFiles) {
  279. [self.discoveredFiles removeObject:filePath];
  280. [self.thumbnailerCache removeThumbnail:filePath];
  281. }
  282. [self.cachedMediaCollectionView reloadData];
  283. }
  284. - (void)realoadMediaCollectionView:(NSNotification *)notification
  285. {
  286. [self.cachedMediaCollectionView reloadData];
  287. }
  288. @end