VLCNetworkServerBrowserVLCMedia.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*****************************************************************************
  2. * VLCNetworkServerBrowserVLCMedia.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCNetworkServerBrowserVLCMedia.h"
  13. #import "NSString+SupportedMedia.h"
  14. @interface VLCNetworkServerBrowserVLCMedia () <VLCMediaListDelegate, VLCMediaDelegate>
  15. {
  16. BOOL _needsNotifyDelegate;
  17. }
  18. @property (nonatomic) VLCMedia *rootMedia;
  19. @property (nonatomic) VLCMediaList *mediaList;
  20. @property (nonatomic) VLCMediaList *mediaListUnfiltered;
  21. @property (nonatomic) NSMutableArray<id<VLCNetworkServerBrowserItem>> *mutableItems;
  22. @property (nonatomic, readonly) NSDictionary *mediaOptions;
  23. @end
  24. @implementation VLCNetworkServerBrowserVLCMedia
  25. @synthesize delegate = _delegate;
  26. - (instancetype)initWithMedia:(VLCMedia *)media options:(nonnull NSDictionary *)mediaOptions
  27. {
  28. self = [super init];
  29. if (self) {
  30. _mutableItems = [[NSMutableArray alloc] init];
  31. _mediaList = [[VLCMediaList alloc] init];
  32. _rootMedia = media;
  33. _rootMedia.delegate = self;
  34. [media parseWithOptions:VLCMediaParseNetwork];
  35. _mediaListUnfiltered = [_rootMedia subitems];
  36. _mediaListUnfiltered.delegate = self;
  37. NSMutableDictionary *mediaOptionsNoFilter = [mediaOptions mutableCopy];
  38. [mediaOptionsNoFilter setObject:@" " forKey:@":ignore-filetypes"];
  39. _mediaOptions = [mediaOptionsNoFilter copy];
  40. [self _addMediaListRootItemsToList];
  41. }
  42. return self;
  43. }
  44. - (BOOL)shouldFilterMedia:(VLCMedia *)media
  45. {
  46. return ![media.url.absoluteString isSupportedAudioMediaFormat] && ![media.url.absoluteString isSupportedMediaFormat] && media.mediaType != VLCMediaTypeDirectory;
  47. }
  48. - (void)_addMediaListRootItemsToList
  49. {
  50. VLCMediaList *rootItems = _rootMedia.subitems;
  51. [rootItems lock];
  52. NSUInteger count = rootItems.count;
  53. for (NSUInteger i = 0; i < count; i++) {
  54. VLCMedia *media = [rootItems mediaAtIndex:i];
  55. if (![self shouldFilterMedia:media]) {
  56. NSInteger mediaIndex = self.mutableItems.count;
  57. [self.mediaList insertMedia:media atIndex:mediaIndex];
  58. [self.mutableItems insertObject:[[VLCNetworkServerBrowserItemVLCMedia alloc] initWithMedia:media options:self.mediaOptions] atIndex:mediaIndex];
  59. }
  60. }
  61. [rootItems unlock];
  62. }
  63. - (void)update {
  64. int ret = [self.rootMedia parseWithOptions:VLCMediaParseNetwork];
  65. if (ret == -1) {
  66. [self.delegate networkServerBrowserDidUpdate:self];
  67. }
  68. }
  69. - (NSString *)title {
  70. if (self.rootMedia.isParsed)
  71. return [self.rootMedia metadataForKey:VLCMetaInformationTitle];
  72. return @"";
  73. }
  74. - (NSArray<id<VLCNetworkServerBrowserItem>> *)items {
  75. return self.mutableItems.copy;
  76. }
  77. #pragma mark - media list delegate
  78. - (void)mediaList:(VLCMediaList *)aMediaList mediaAdded:(VLCMedia *)media atIndex:(NSInteger)index
  79. {
  80. [media addOptions:self.mediaOptions];
  81. if (![self shouldFilterMedia:media]) {
  82. NSInteger mediaIndex = self.mutableItems.count;
  83. [self.mediaList insertMedia:media atIndex:mediaIndex];
  84. [self.mutableItems insertObject:[[VLCNetworkServerBrowserItemVLCMedia alloc] initWithMedia:media options:self.mediaOptions] atIndex:mediaIndex];
  85. }
  86. [self.delegate networkServerBrowserDidUpdate:self];
  87. }
  88. - (void)mediaList:(VLCMediaList *)aMediaList mediaRemovedAtIndex:(NSInteger)index
  89. {
  90. VLCMedia *media = [self.mediaListUnfiltered mediaAtIndex:index];
  91. NSInteger mediaIndex = [self.mediaList indexOfMedia:media];
  92. [self.mediaList removeMediaAtIndex:mediaIndex];
  93. [self.mutableItems removeObjectAtIndex:mediaIndex];
  94. [self.delegate networkServerBrowserDidUpdate:self];
  95. }
  96. #pragma mark - media delegate
  97. - (void)mediaDidFinishParsing:(VLCMedia *)aMedia
  98. {
  99. [self setNeedsNotifyDelegateForDidUpdate];
  100. }
  101. - (void)mediaMetaDataDidChange:(VLCMedia *)aMedia
  102. {
  103. [self setNeedsNotifyDelegateForDidUpdate];
  104. }
  105. - (void)setNeedsNotifyDelegateForDidUpdate
  106. {
  107. if (_needsNotifyDelegate) {
  108. return;
  109. }
  110. _needsNotifyDelegate = YES;
  111. double amountOfSeconds = 0.1;
  112. dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(amountOfSeconds * NSEC_PER_SEC));
  113. dispatch_after(delayTime, dispatch_get_main_queue(), ^{
  114. _needsNotifyDelegate = NO;
  115. [self.delegate networkServerBrowserDidUpdate:self];
  116. });
  117. }
  118. @end
  119. @interface VLCNetworkServerBrowserItemVLCMedia () <VLCMediaDelegate>
  120. @property (nonatomic, readonly) NSDictionary *mediaOptions;
  121. @end
  122. @implementation VLCNetworkServerBrowserItemVLCMedia
  123. @synthesize name = _name, container = _container, fileSizeBytes = _fileSizeBytes, URL = _URL, media = _media, downloadable = _downloadable;
  124. - (instancetype)initWithMedia:(VLCMedia *)media options:(NSDictionary *)mediaOptions;
  125. {
  126. self = [super init];
  127. if (self) {
  128. _media = media;
  129. _container = media.mediaType == VLCMediaTypeDirectory;
  130. NSString *title;
  131. if (media.isParsed) {
  132. title = [media metadataForKey:VLCMetaInformationTitle];
  133. }
  134. if (!title) {
  135. title = [media.url.lastPathComponent stringByRemovingPercentEncoding];
  136. }
  137. if (!title) {
  138. title = [media.url.absoluteString stringByRemovingPercentEncoding];
  139. }
  140. _name = title;
  141. _URL = media.url;
  142. _mediaOptions = [mediaOptions copy];
  143. _downloadable = NO;
  144. }
  145. return self;
  146. }
  147. - (id<VLCNetworkServerBrowser>)containerBrowser {
  148. return [[VLCNetworkServerBrowserVLCMedia alloc] initWithMedia:self.media options:self.mediaOptions];
  149. }
  150. @end