VLCNetworkServerBrowserVLCMedia.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. @property (nonatomic) VLCMedia *rootMedia;
  16. @property (nonatomic) VLCMediaList *mediaList;
  17. @property (nonatomic) VLCMediaList *mediaListUnfiltered;
  18. @property (nonatomic) NSMutableArray<id<VLCNetworkServerBrowserItem>> *mutableItems;
  19. @property (nonatomic, readonly) NSDictionary *mediaOptions;
  20. @end
  21. @implementation VLCNetworkServerBrowserVLCMedia
  22. @synthesize delegate = _delegate;
  23. - (instancetype)initWithMedia:(VLCMedia *)media options:(nonnull NSDictionary *)mediaOptions
  24. {
  25. self = [super init];
  26. if (self) {
  27. _mutableItems = [[NSMutableArray alloc] init];
  28. _mediaList = [[VLCMediaList alloc] init];
  29. _rootMedia = media;
  30. _rootMedia.delegate = self;
  31. [media parseWithOptions:VLCMediaParseNetwork];
  32. _mediaListUnfiltered = [_rootMedia subitems];
  33. _mediaListUnfiltered.delegate = self;
  34. NSMutableDictionary *mediaOptionsNoFilter = [mediaOptions mutableCopy];
  35. [mediaOptionsNoFilter setObject:@" " forKey:@":ignore-filetypes"];
  36. _mediaOptions = [mediaOptionsNoFilter copy];
  37. [self _addMediaListRootItemsToList];
  38. }
  39. return self;
  40. }
  41. - (BOOL)shouldFilterMedia:(VLCMedia *)media
  42. {
  43. NSString *absoluteString = media.url.absoluteString;
  44. return ![absoluteString isSupportedAudioMediaFormat] && ![absoluteString isSupportedMediaFormat] && ![absoluteString isSupportedPlaylistFormat] && media.mediaType != VLCMediaTypeDirectory;
  45. }
  46. - (void)_addMediaListRootItemsToList
  47. {
  48. VLCMediaList *rootItems = _rootMedia.subitems;
  49. [rootItems lock];
  50. NSUInteger count = rootItems.count;
  51. for (NSUInteger i = 0; i < count; i++) {
  52. VLCMedia *media = [rootItems mediaAtIndex:i];
  53. if (![self shouldFilterMedia:media]) {
  54. NSInteger mediaIndex = self.mutableItems.count;
  55. [self.mediaList insertMedia:media atIndex:mediaIndex];
  56. [self.mutableItems insertObject:[[VLCNetworkServerBrowserItemVLCMedia alloc] initWithMedia:media options:self.mediaOptions] atIndex:mediaIndex];
  57. }
  58. }
  59. [rootItems unlock];
  60. }
  61. - (void)update {
  62. int ret = [self.rootMedia parseWithOptions:VLCMediaParseNetwork];
  63. if (ret == -1) {
  64. [self.delegate networkServerBrowserDidUpdate:self];
  65. }
  66. }
  67. - (NSString *)title {
  68. return [self.rootMedia metadataForKey:VLCMetaInformationTitle];
  69. }
  70. - (NSArray<id<VLCNetworkServerBrowserItem>> *)items {
  71. return self.mutableItems.copy;
  72. }
  73. #pragma mark - media list delegate
  74. - (void)mediaList:(VLCMediaList *)aMediaList mediaAdded:(VLCMedia *)media atIndex:(NSUInteger)index
  75. {
  76. [media addOptions:self.mediaOptions];
  77. if (![self shouldFilterMedia:media]) {
  78. NSInteger mediaIndex = self.mutableItems.count;
  79. [self.mediaList insertMedia:media atIndex:mediaIndex];
  80. [self.mutableItems insertObject:[[VLCNetworkServerBrowserItemVLCMedia alloc] initWithMedia:media options:self.mediaOptions] atIndex:mediaIndex];
  81. }
  82. [self.delegate networkServerBrowserDidUpdate:self];
  83. }
  84. - (void)mediaList:(VLCMediaList *)aMediaList mediaRemovedAtIndex:(NSUInteger)index
  85. {
  86. VLCMedia *media = [self.mediaListUnfiltered mediaAtIndex:index];
  87. NSInteger mediaIndex = [self.mediaList indexOfMedia:media];
  88. [self.mediaList removeMediaAtIndex:mediaIndex];
  89. [self.mutableItems removeObjectAtIndex:mediaIndex];
  90. [self.delegate networkServerBrowserDidUpdate:self];
  91. }
  92. #pragma mark - media delegate
  93. - (void)mediaDidFinishParsing:(VLCMedia *)aMedia
  94. {
  95. [self.delegate networkServerBrowserDidUpdate:self];
  96. }
  97. @end
  98. @interface VLCNetworkServerBrowserItemVLCMedia () <VLCMediaDelegate>
  99. @property (nonatomic, readonly) NSDictionary *mediaOptions;
  100. @end
  101. @implementation VLCNetworkServerBrowserItemVLCMedia
  102. @synthesize name = _name, container = _container, fileSizeBytes = _fileSizeBytes, URL = _URL, media = _media, downloadable = _downloadable;
  103. - (instancetype)initWithMedia:(VLCMedia *)media options:(NSDictionary *)mediaOptions;
  104. {
  105. self = [super init];
  106. if (self) {
  107. _media = media;
  108. _container = media.mediaType == VLCMediaTypeDirectory;
  109. NSString *title = [media metadataForKey:VLCMetaInformationTitle];
  110. if (!title) {
  111. title = [media.url.lastPathComponent stringByRemovingPercentEncoding];
  112. }
  113. if (!title) {
  114. title = [media.url.absoluteString stringByRemovingPercentEncoding];
  115. }
  116. _name = title;
  117. _URL = media.url;
  118. _mediaOptions = [mediaOptions copy];
  119. _downloadable = NO;
  120. }
  121. return self;
  122. }
  123. - (id<VLCNetworkServerBrowser>)containerBrowser {
  124. return [[VLCNetworkServerBrowserVLCMedia alloc] initWithMedia:self.media options:self.mediaOptions];
  125. }
  126. @end