VLCNetworkServerBrowserVLCMedia.m 5.9 KB

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