VLCNetworkServerBrowserVLCMedia.m 4.4 KB

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