VLCNetworkServerBrowserVLCMedia.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. NSString *absoluteString = media.url.absoluteString;
  47. return ![absoluteString isSupportedAudioMediaFormat] && ![absoluteString isSupportedMediaFormat] && ![absoluteString isSupportedPlaylistFormat] && media.mediaType != VLCMediaTypeDirectory;
  48. }
  49. - (void)_addMediaListRootItemsToList
  50. {
  51. VLCMediaList *rootItems = _rootMedia.subitems;
  52. [rootItems lock];
  53. NSUInteger count = rootItems.count;
  54. for (NSUInteger i = 0; i < count; i++) {
  55. VLCMedia *media = [rootItems mediaAtIndex:i];
  56. if (![self shouldFilterMedia:media]) {
  57. NSInteger mediaIndex = self.mutableItems.count;
  58. [self.mediaList insertMedia:media atIndex:mediaIndex];
  59. [self.mutableItems insertObject:[[VLCNetworkServerBrowserItemVLCMedia alloc] initWithMedia:media options:self.mediaOptions] atIndex:mediaIndex];
  60. }
  61. }
  62. [rootItems unlock];
  63. }
  64. - (void)update {
  65. int ret = [self.rootMedia parseWithOptions:VLCMediaParseNetwork];
  66. if (ret == -1) {
  67. [self.delegate networkServerBrowserDidUpdate:self];
  68. }
  69. }
  70. - (NSString *)title {
  71. return [self.rootMedia metadataForKey:VLCMetaInformationTitle];
  72. }
  73. - (NSArray<id<VLCNetworkServerBrowserItem>> *)items {
  74. return self.mutableItems.copy;
  75. }
  76. #pragma mark - media list delegate
  77. - (void)mediaList:(VLCMediaList *)aMediaList mediaAdded:(VLCMedia *)media atIndex:(NSUInteger)index
  78. {
  79. [media addOptions:self.mediaOptions];
  80. if (![self shouldFilterMedia:media]) {
  81. NSInteger mediaIndex = self.mutableItems.count;
  82. [self.mediaList insertMedia:media atIndex:mediaIndex];
  83. [self.mutableItems insertObject:[[VLCNetworkServerBrowserItemVLCMedia alloc] initWithMedia:media options:self.mediaOptions] atIndex:mediaIndex];
  84. }
  85. [self.delegate networkServerBrowserDidUpdate:self];
  86. }
  87. - (void)mediaList:(VLCMediaList *)aMediaList mediaRemovedAtIndex:(NSUInteger)index
  88. {
  89. VLCMedia *media = [self.mediaListUnfiltered mediaAtIndex:index];
  90. NSInteger mediaIndex = [self.mediaList indexOfMedia:media];
  91. [self.mediaList removeMediaAtIndex:mediaIndex];
  92. [self.mutableItems removeObjectAtIndex:mediaIndex];
  93. [self.delegate networkServerBrowserDidUpdate:self];
  94. }
  95. #pragma mark - media delegate
  96. - (void)mediaDidFinishParsing:(VLCMedia *)aMedia
  97. {
  98. [self setNeedsNotifyDelegateForDidUpdate];
  99. }
  100. - (void)mediaMetaDataDidChange:(VLCMedia *)aMedia
  101. {
  102. [self setNeedsNotifyDelegateForDidUpdate];
  103. }
  104. - (void)setNeedsNotifyDelegateForDidUpdate
  105. {
  106. if (_needsNotifyDelegate) {
  107. return;
  108. }
  109. _needsNotifyDelegate = YES;
  110. double amountOfSeconds = 0.1;
  111. dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(amountOfSeconds * NSEC_PER_SEC));
  112. dispatch_after(delayTime, dispatch_get_main_queue(), ^{
  113. self->_needsNotifyDelegate = NO;
  114. [self.delegate networkServerBrowserDidUpdate:self];
  115. });
  116. }
  117. @end
  118. @interface VLCNetworkServerBrowserItemVLCMedia () <VLCMediaDelegate>
  119. @property (nonatomic, readonly) NSDictionary *mediaOptions;
  120. @end
  121. @implementation VLCNetworkServerBrowserItemVLCMedia
  122. @synthesize name = _name, container = _container, fileSizeBytes = _fileSizeBytes, URL = _URL, media = _media, downloadable = _downloadable;
  123. - (instancetype)initWithMedia:(VLCMedia *)media options:(NSDictionary *)mediaOptions;
  124. {
  125. self = [super init];
  126. if (self) {
  127. _media = media;
  128. _container = media.mediaType == VLCMediaTypeDirectory;
  129. NSString *title = [media metadataForKey:VLCMetaInformationTitle];
  130. if (!title) {
  131. title = [media.url.lastPathComponent stringByRemovingPercentEncoding];
  132. }
  133. if (!title) {
  134. title = [media.url.absoluteString stringByRemovingPercentEncoding];
  135. }
  136. _name = title;
  137. _URL = media.url;
  138. _mediaOptions = [mediaOptions copy];
  139. _downloadable = NO;
  140. }
  141. return self;
  142. }
  143. - (id<VLCNetworkServerBrowser>)containerBrowser {
  144. return [[VLCNetworkServerBrowserVLCMedia alloc] initWithMedia:self.media options:self.mediaOptions];
  145. }
  146. @end