VLCMediaFileDiscoverer.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*****************************************************************************
  2. * VLCMediaFileDiscoverer.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Gleb Pinigin <gpinigin # gmail.com>
  9. * Felix Paul Kühne <fkuehne # videolan.org>
  10. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "VLCMediaFileDiscoverer.h"
  14. #import "NSString+SupportedMedia.h"
  15. const float MediaTimerInterval = 2.f;
  16. @interface VLCMediaFileDiscoverer () {
  17. NSMutableArray *_observers;
  18. dispatch_source_t _directorySource;
  19. NSArray *_directoryFiles;
  20. NSMutableDictionary *_addedFilesMapping;
  21. NSTimer *_addMediaTimer;
  22. NSArray *_discoveredFilePath;
  23. }
  24. @end
  25. @implementation VLCMediaFileDiscoverer
  26. - (id)init
  27. {
  28. self = [super init];
  29. if (self) {
  30. _observers = [NSMutableArray array];
  31. _addedFilesMapping = [NSMutableDictionary dictionary];
  32. }
  33. return self;
  34. }
  35. + (instancetype)sharedInstance
  36. {
  37. static dispatch_once_t onceToken;
  38. static VLCMediaFileDiscoverer *instance;
  39. dispatch_once(&onceToken, ^{
  40. instance = [VLCMediaFileDiscoverer new];
  41. instance.filterResultsForPlayability = YES;
  42. });
  43. return instance;
  44. }
  45. #pragma mark - observation
  46. - (void)addObserver:(id<VLCMediaFileDiscovererDelegate>)delegate
  47. {
  48. [_observers addObject:delegate];
  49. }
  50. - (void)removeObserver:(id<VLCMediaFileDiscovererDelegate>)delegate
  51. {
  52. [_observers removeObject:delegate];
  53. }
  54. - (void)notifyFileDeleted:(NSString *)fileName
  55. {
  56. for (id<VLCMediaFileDiscovererDelegate> delegate in _observers) {
  57. if ([delegate respondsToSelector:@selector(mediaFileDeleted:)]) {
  58. [delegate mediaFileDeleted:[self filePath:fileName]];
  59. }
  60. }
  61. }
  62. - (void)notifyFileAdded:(NSString *)fileName loading:(BOOL)isLoading
  63. {
  64. for (id<VLCMediaFileDiscovererDelegate> delegate in _observers) {
  65. if ([delegate respondsToSelector:@selector(mediaFileAdded:loading:)]) {
  66. [delegate mediaFileAdded:[self filePath:fileName] loading:isLoading];
  67. }
  68. }
  69. }
  70. - (void)notifySizeChanged:(NSString *)fileName size:(unsigned long long)size
  71. {
  72. for (id<VLCMediaFileDiscovererDelegate> delegate in _observers) {
  73. if ([delegate respondsToSelector:@selector(mediaFileChanged:size:)]) {
  74. [delegate mediaFileChanged:[self filePath:fileName] size:size];
  75. }
  76. }
  77. }
  78. #pragma mark - discovering
  79. - (void)startDiscovering
  80. {
  81. if (!_directoryPath) {
  82. APLog(@"file discovery failed, no path was set");
  83. return;
  84. } else
  85. APLog(@"will discover files in path: '%@'", _directoryPath);
  86. _directoryFiles = [self directoryFiles];
  87. int const folderDescriptor = open([_directoryPath fileSystemRepresentation], O_EVTONLY);
  88. _directorySource = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, folderDescriptor,
  89. DISPATCH_VNODE_WRITE, DISPATCH_TARGET_QUEUE_DEFAULT);
  90. dispatch_source_set_event_handler(_directorySource, ^(){
  91. unsigned long const data = dispatch_source_get_data(_directorySource);
  92. if (data & DISPATCH_VNODE_WRITE) {
  93. // Do all the work on the main thread,
  94. // including timer scheduling, notifications delivering
  95. dispatch_async(dispatch_get_main_queue(), ^{
  96. [self directoryDidChange];
  97. });
  98. }
  99. });
  100. dispatch_source_set_cancel_handler(_directorySource, ^(){
  101. close(folderDescriptor);
  102. });
  103. dispatch_resume(_directorySource);
  104. }
  105. - (void)stopDiscovering
  106. {
  107. dispatch_source_cancel(_directorySource);
  108. [self invalidateTimer];
  109. }
  110. #pragma mark -
  111. - (NSArray *)directoryFiles
  112. {
  113. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:_directoryPath
  114. error:nil];
  115. return foundFiles;
  116. }
  117. - (NSString *)filePath:(NSString *)fileName
  118. {
  119. return [_directoryPath stringByAppendingPathComponent:fileName];
  120. }
  121. #pragma mark - directory watcher delegate
  122. - (void)directoryDidChange
  123. {
  124. NSArray *foundFiles = [self directoryFiles];
  125. if (_directoryFiles.count > foundFiles.count) { // File was deleted
  126. NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"not (self in %@)", foundFiles];
  127. NSArray *deletedFiles = [_directoryFiles filteredArrayUsingPredicate:filterPredicate];
  128. for (NSString *fileName in deletedFiles)
  129. [self notifyFileDeleted:fileName];
  130. } else if (_directoryFiles.count < foundFiles.count) { // File was added
  131. NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"not (self in %@)", _directoryFiles];
  132. NSMutableArray *addedFiles = [NSMutableArray arrayWithArray:[foundFiles filteredArrayUsingPredicate:filterPredicate]];
  133. for (NSString *fileName in addedFiles) {
  134. BOOL isDirectory = NO;
  135. NSString *directoryPath = [self directoryPath];
  136. NSString *filePath = [directoryPath stringByAppendingPathComponent:fileName];
  137. BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
  138. if (exists && !isDirectory) {
  139. if (self.filterResultsForPlayability) {
  140. if ([fileName isSupportedMediaFormat] || [fileName isSupportedAudioMediaFormat] || [fileName isSupportedPlaylistFormat]) {
  141. [_addedFilesMapping setObject:@(0) forKey:fileName];
  142. [self notifyFileAdded:fileName loading:YES];
  143. }
  144. } else {
  145. [_addedFilesMapping setObject:@(0) forKey:fileName];
  146. [self notifyFileAdded:fileName loading:YES];
  147. }
  148. } else if (exists && isDirectory) {
  149. // add folders
  150. NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:filePath error:nil];
  151. for (NSString* file in files) {
  152. NSString *fullFilePath = [directoryPath stringByAppendingPathComponent:file];
  153. isDirectory = NO;
  154. exists = [[NSFileManager defaultManager] fileExistsAtPath:fullFilePath isDirectory:&isDirectory];
  155. //only add folders or files in folders
  156. if ((exists && isDirectory) || ![filePath.lastPathComponent isEqualToString:@"Documents"]) {
  157. NSString *folderpath = [filePath stringByReplacingOccurrencesOfString:directoryPath withString:@""];
  158. if (![folderpath isEqualToString:@""]) {
  159. folderpath = [folderpath stringByAppendingString:@"/"];
  160. }
  161. NSString *path = [folderpath stringByAppendingString:file];
  162. [_addedFilesMapping setObject:@(0) forKey:path];
  163. [self notifyFileAdded:path loading:YES];
  164. }
  165. }
  166. }
  167. }
  168. if (![_addMediaTimer isValid]) {
  169. _addMediaTimer = [NSTimer scheduledTimerWithTimeInterval:MediaTimerInterval
  170. target:self selector:@selector(addFileTimerFired)
  171. userInfo:nil repeats:YES];
  172. }
  173. }
  174. _directoryFiles = foundFiles;
  175. }
  176. #pragma mark - media timer
  177. - (void)addFileTimerFired
  178. {
  179. NSArray *allKeys = [_addedFilesMapping allKeys];
  180. NSFileManager *fileManager = [NSFileManager defaultManager];
  181. for (NSString *fileName in allKeys) {
  182. NSString *filePath = [self filePath:fileName];
  183. if (![fileManager fileExistsAtPath:filePath]) {
  184. [_addedFilesMapping removeObjectForKey:fileName];
  185. continue;
  186. }
  187. NSNumber *prevFetchedSize = [_addedFilesMapping objectForKey:fileName];
  188. NSDictionary *attribs = [fileManager attributesOfItemAtPath:filePath error:nil];
  189. NSNumber *updatedSize = [attribs objectForKey:NSFileSize];
  190. if (!updatedSize)
  191. continue;
  192. [self notifySizeChanged:fileName size:[updatedSize unsignedLongLongValue]];
  193. if ([prevFetchedSize compare:updatedSize] == NSOrderedSame) {
  194. [_addedFilesMapping removeObjectForKey:fileName];
  195. [self notifyFileAdded:fileName loading:NO];
  196. } else
  197. [_addedFilesMapping setObject:updatedSize forKey:fileName];
  198. }
  199. if (_addedFilesMapping.count == 0)
  200. [self invalidateTimer];
  201. }
  202. - (void)invalidateTimer
  203. {
  204. [_addMediaTimer invalidate];
  205. _addMediaTimer = nil;
  206. }
  207. #pragma mark - media list management
  208. - (void)updateMediaList
  209. {
  210. if (![NSThread isMainThread]) {
  211. [self performSelectorOnMainThread:@selector(updateMediaList) withObject:nil waitUntilDone:NO];
  212. return;
  213. }
  214. NSString *directoryPath = [self directoryPath];
  215. NSMutableArray *foundFiles = [NSMutableArray arrayWithArray:[[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil]];
  216. NSMutableArray *filePaths = [NSMutableArray array];
  217. NSURL *fileURL;
  218. while (foundFiles.count) {
  219. NSString *fileName = foundFiles.firstObject;
  220. NSString *filePath = [directoryPath stringByAppendingPathComponent:fileName];
  221. [foundFiles removeObject:fileName];
  222. BOOL isDirectory = NO;
  223. BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
  224. if (exists && !isDirectory) {
  225. if (self.filterResultsForPlayability) {
  226. if ([fileName isSupportedMediaFormat] || [fileName isSupportedAudioMediaFormat]) {
  227. [filePaths addObject:filePath];
  228. /* exclude media files from backup (QA1719) */
  229. fileURL = [NSURL fileURLWithPath:filePath];
  230. [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  231. }
  232. } else {
  233. [filePaths addObject:filePath];
  234. /* exclude media files from backup (QA1719) */
  235. fileURL = [NSURL fileURLWithPath:filePath];
  236. [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  237. }
  238. } else if (exists && isDirectory) {
  239. // add folders
  240. NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:filePath error:nil];
  241. for (NSString* file in files) {
  242. NSString *fullFilePath = [directoryPath stringByAppendingPathComponent:file];
  243. isDirectory = NO;
  244. exists = [[NSFileManager defaultManager] fileExistsAtPath:fullFilePath isDirectory:&isDirectory];
  245. //only add folders or files in folders
  246. if ((exists && isDirectory) || ![filePath.lastPathComponent isEqualToString:@"Documents"]) {
  247. NSString *folderpath = [filePath stringByReplacingOccurrencesOfString:directoryPath withString:@""];
  248. if (![folderpath isEqualToString:@""]) {
  249. folderpath = [folderpath stringByAppendingString:@"/"];
  250. }
  251. NSString *path = [folderpath stringByAppendingString:file];
  252. [foundFiles addObject:path];
  253. }
  254. }
  255. }
  256. }
  257. if (![_discoveredFilePath isEqualToArray:filePaths]) {
  258. _discoveredFilePath = filePaths;
  259. for (id<VLCMediaFileDiscovererDelegate> delegate in _observers) {
  260. if ([delegate respondsToSelector:@selector(mediaFilesFoundRequiringAdditionToStorageBackend:)]) {
  261. [delegate mediaFilesFoundRequiringAdditionToStorageBackend:[filePaths copy]];
  262. }
  263. }
  264. }
  265. }
  266. @end