VLCNetworkServerBrowserVLCMedia.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #import "VLC-Swift.h"
  15. @interface VLCNetworkServerBrowserVLCMedia () <VLCMediaListDelegate, VLCMediaDelegate>
  16. {
  17. VLCDialogProvider *_dialogProvider;
  18. VLCCustomDialogRendererHandler *_customDialogHandler;
  19. }
  20. @property (nonatomic) VLCMedia *rootMedia;
  21. @property (nonatomic) VLCMediaList *mediaList;
  22. @property (nonatomic) VLCMediaList *mediaListUnfiltered;
  23. @property (nonatomic) NSMutableArray<id<VLCNetworkServerBrowserItem>> *mutableItems;
  24. @property (nonatomic, readonly) NSDictionary *mediaOptions;
  25. @end
  26. @implementation VLCNetworkServerBrowserVLCMedia
  27. @synthesize delegate = _delegate;
  28. - (instancetype)initWithMedia:(VLCMedia *)media options:(nonnull NSDictionary *)mediaOptions
  29. {
  30. self = [super init];
  31. if (self) {
  32. _mutableItems = [[NSMutableArray alloc] init];
  33. _mediaList = [[VLCMediaList alloc] init];
  34. _rootMedia = media;
  35. _rootMedia.delegate = self;
  36. [media addOption:kVLCForceSMBV1];
  37. // Set timeout to 0 in order to avoid getting interrupted in dialogs for timeout reasons
  38. [media parseWithOptions:VLCMediaParseNetwork|VLCMediaDoInteract timeout:0];
  39. _mediaListUnfiltered = [_rootMedia subitems];
  40. _mediaListUnfiltered.delegate = self;
  41. NSMutableDictionary *mediaOptionsNoFilter = [mediaOptions mutableCopy];
  42. [mediaOptionsNoFilter setObject:@" " forKey:@":ignore-filetypes"];
  43. _mediaOptions = [mediaOptionsNoFilter copy];
  44. [self _addMediaListRootItemsToList];
  45. _dialogProvider = [[VLCDialogProvider alloc] initWithLibrary:[VLCLibrary sharedLibrary] customUI:YES];
  46. _customDialogHandler = [[VLCCustomDialogRendererHandler alloc]
  47. initWithDialogProvider:_dialogProvider];
  48. __weak typeof(self) weakSelf = self;
  49. _customDialogHandler.completionHandler = ^(VLCCustomDialogRendererHandlerCompletionType status)
  50. {
  51. [weakSelf customDialogCompletionHandlerWithStatus:status];
  52. };
  53. _dialogProvider.customRenderer = _customDialogHandler;
  54. }
  55. return self;
  56. }
  57. - (void)customDialogCompletionHandlerWithStatus:(VLCCustomDialogRendererHandlerCompletionType)status
  58. {
  59. if (status == VLCCustomDialogRendererHandlerCompletionTypeStop) {
  60. [_rootMedia parseStop];
  61. }
  62. }
  63. - (BOOL)shouldFilterMedia:(VLCMedia *)media
  64. {
  65. NSString *absoluteString = media.url.absoluteString;
  66. return ![absoluteString isSupportedAudioMediaFormat] && ![absoluteString isSupportedMediaFormat] && ![absoluteString isSupportedPlaylistFormat] && media.mediaType != VLCMediaTypeDirectory;
  67. }
  68. - (void)_addMediaListRootItemsToList
  69. {
  70. VLCMediaList *rootItems = _rootMedia.subitems;
  71. [rootItems lock];
  72. NSUInteger count = rootItems.count;
  73. for (NSUInteger i = 0; i < count; i++) {
  74. VLCMedia *media = [rootItems mediaAtIndex:i];
  75. if (![self shouldFilterMedia:media]) {
  76. NSInteger mediaIndex = self.mutableItems.count;
  77. [self.mediaList insertMedia:media atIndex:mediaIndex];
  78. [self.mutableItems insertObject:[[VLCNetworkServerBrowserItemVLCMedia alloc] initWithMedia:media options:self.mediaOptions] atIndex:mediaIndex];
  79. }
  80. }
  81. [rootItems unlock];
  82. }
  83. - (void)update {
  84. int ret = [self.rootMedia parseWithOptions:VLCMediaParseNetwork];
  85. if (ret == -1) {
  86. [self.delegate networkServerBrowserDidUpdate:self];
  87. }
  88. }
  89. - (NSString *)title {
  90. return [self.rootMedia metadataForKey:VLCMetaInformationTitle];
  91. }
  92. - (NSArray<id<VLCNetworkServerBrowserItem>> *)items {
  93. return self.mutableItems.copy;
  94. }
  95. #pragma mark - media list delegate
  96. - (void)mediaList:(VLCMediaList *)aMediaList mediaAdded:(VLCMedia *)media atIndex:(NSUInteger)index
  97. {
  98. [media addOptions:self.mediaOptions];
  99. if (![self shouldFilterMedia:media]) {
  100. NSInteger mediaIndex = self.mutableItems.count;
  101. [self.mediaList insertMedia:media atIndex:mediaIndex];
  102. [self.mutableItems insertObject:[[VLCNetworkServerBrowserItemVLCMedia alloc] initWithMedia:media options:self.mediaOptions] atIndex:mediaIndex];
  103. }
  104. [self.delegate networkServerBrowserDidUpdate:self];
  105. }
  106. - (void)mediaList:(VLCMediaList *)aMediaList mediaRemovedAtIndex:(NSUInteger)index
  107. {
  108. VLCMedia *media = [self.mediaListUnfiltered mediaAtIndex:index];
  109. NSInteger mediaIndex = [self.mediaList indexOfMedia:media];
  110. [self.mediaList removeMediaAtIndex:mediaIndex];
  111. [self.mutableItems removeObjectAtIndex:mediaIndex];
  112. [self.delegate networkServerBrowserDidUpdate:self];
  113. }
  114. #pragma mark - media delegate
  115. - (void)mediaDidFinishParsing:(VLCMedia *)aMedia
  116. {
  117. if ([aMedia parsedStatus] != VLCMediaParsedStatusDone) {
  118. [self.delegate networkServerBrowserShouldPopView:self];
  119. } else if (self.mediaList.count != 0) {
  120. [self.delegate networkServerBrowserDidUpdate:self];
  121. } else {
  122. [self.delegate networkServerBrowserEndParsing:self];
  123. }
  124. }
  125. @end
  126. @interface VLCNetworkServerBrowserItemVLCMedia () <VLCMediaDelegate>
  127. @property (nonatomic, readonly) NSDictionary *mediaOptions;
  128. @end
  129. @implementation VLCNetworkServerBrowserItemVLCMedia
  130. @synthesize name = _name, container = _container, fileSizeBytes = _fileSizeBytes, URL = _URL, media = _media, downloadable = _downloadable;
  131. - (instancetype)initWithMedia:(VLCMedia *)media options:(NSDictionary *)mediaOptions;
  132. {
  133. self = [super init];
  134. if (self) {
  135. _media = media;
  136. _container = media.mediaType == VLCMediaTypeDirectory;
  137. NSString *title = [media metadataForKey:VLCMetaInformationTitle];
  138. if (!title) {
  139. title = [media.url.lastPathComponent stringByRemovingPercentEncoding];
  140. }
  141. if (!title) {
  142. title = [media.url.absoluteString stringByRemovingPercentEncoding];
  143. }
  144. _name = title;
  145. _URL = media.url;
  146. _mediaOptions = [mediaOptions copy];
  147. _downloadable = NO;
  148. }
  149. return self;
  150. }
  151. - (id<VLCNetworkServerBrowser>)containerBrowser {
  152. return [[VLCNetworkServerBrowserVLCMedia alloc] initWithMedia:self.media options:self.mediaOptions];
  153. }
  154. @end