VLCNetworkServerBrowserVLCMedia.m 4.8 KB

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