VLCNetworkServerBrowserVLCMedia.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. }
  33. return self;
  34. }
  35. - (void)setDelegate:(id<VLCNetworkServerBrowserDelegate>)delegate
  36. {
  37. _delegate = delegate;
  38. [self _addMediaListRootItemsToList];
  39. }
  40. - (void)_addMediaListRootItemsToList
  41. {
  42. VLCMediaList *rootItems = _rootMedia.subitems;
  43. [rootItems lock];
  44. NSUInteger count = rootItems.count;
  45. for (NSUInteger i = 0; i < count; i++) {
  46. VLCMedia *media = [rootItems mediaAtIndex:i];
  47. VLCMedia *newMedia = [VLCMedia mediaWithURL:media.url];
  48. [self.mutableItems addObject:[[VLCNetworkServerBrowserItemVLCMedia alloc] initWithMedia:newMedia options:self.mediaOptions]];
  49. newMedia.delegate = self;
  50. [newMedia addOptions:self.mediaOptions];
  51. [newMedia parseWithOptions:VLCMediaParseNetwork];
  52. }
  53. [rootItems unlock];
  54. [self.delegate networkServerBrowserDidUpdate:self];
  55. }
  56. - (void)update {
  57. int ret = [self.rootMedia parseWithOptions:VLCMediaParseNetwork];
  58. APLog(@"%s: %i", __PRETTY_FUNCTION__, ret);
  59. if (ret == -1) {
  60. [self.delegate networkServerBrowserDidUpdate:self];
  61. }
  62. }
  63. - (NSString *)title {
  64. if (self.rootMedia.isParsed)
  65. return [self.rootMedia metadataForKey:VLCMetaInformationTitle];
  66. return @"";
  67. }
  68. - (NSArray<id<VLCNetworkServerBrowserItem>> *)items {
  69. return self.mutableItems.copy;
  70. }
  71. #pragma mark - media list delegate
  72. - (void)mediaList:(VLCMediaList *)aMediaList mediaAdded:(VLCMedia *)media atIndex:(NSInteger)index
  73. {
  74. APLog(@"%s: %@", __PRETTY_FUNCTION__, media);
  75. [media addOptions:self.mediaOptions];
  76. [self.mutableItems addObject:[[VLCNetworkServerBrowserItemVLCMedia alloc] initWithMedia:media options:self.mediaOptions]];
  77. [self.delegate networkServerBrowserDidUpdate:self];
  78. }
  79. - (void)mediaList:(VLCMediaList *)aMediaList mediaRemovedAtIndex:(NSInteger)index
  80. {
  81. APLog(@"%s", __PRETTY_FUNCTION__);
  82. [self.mutableItems removeObjectAtIndex:index];
  83. [self.delegate networkServerBrowserDidUpdate:self];
  84. }
  85. #pragma mark - media delegate
  86. - (void)mediaDidFinishParsing:(VLCMedia *)aMedia
  87. {
  88. [self.delegate networkServerBrowserDidUpdate:self];
  89. }
  90. - (void)mediaMetaDataDidChange:(VLCMedia *)aMedia
  91. {
  92. [self.delegate networkServerBrowserDidUpdate:self];
  93. }
  94. @end
  95. @interface VLCNetworkServerBrowserItemVLCMedia () <VLCMediaDelegate>
  96. @property (nonatomic, readonly) VLCMedia *media;
  97. @property (nonatomic, readonly) NSDictionary *mediaOptions;
  98. @end
  99. @implementation VLCNetworkServerBrowserItemVLCMedia
  100. @synthesize name = _name, container = _container, fileSizeBytes = _fileSizeBytes, URL = _URL;
  101. - (instancetype)initWithMedia:(VLCMedia *)media options:(NSDictionary *)mediaOptions;
  102. {
  103. self = [super init];
  104. if (self) {
  105. _media = media;
  106. _container = media.mediaType == VLCMediaTypeDirectory;
  107. NSString *title;
  108. if (media.isParsed) {
  109. title = [media metadataForKey:VLCMetaInformationTitle];
  110. }
  111. if (!title) {
  112. title = media.url.lastPathComponent;
  113. }
  114. if (!title) {
  115. title = media.url.absoluteString;
  116. }
  117. _name = title;
  118. _URL = media.url;
  119. _mediaOptions = [mediaOptions copy];
  120. // _downloadable = NO; //TODO: add property for downloadable?
  121. }
  122. return self;
  123. }
  124. - (id<VLCNetworkServerBrowser>)containerBrowser {
  125. return [[VLCNetworkServerBrowserVLCMedia alloc] initWithMedia:self.media options:self.mediaOptions];
  126. }
  127. @end
  128. @implementation VLCNetworkServerBrowserVLCMedia (SMB)
  129. + (instancetype)SMBNetworkServerBrowserWithURL:(NSURL *)url username:(NSString *)username password:(NSString *)password workgroup:(NSString *)workgroup {
  130. VLCMedia *media = [VLCMedia mediaWithURL:url];
  131. NSDictionary *mediaOptions = @{@"smb-user" : username ?: @"",
  132. @"smb-pwd" : password ?: @"",
  133. @"smb-domain" : workgroup?: @"WORKGROUP"};
  134. [media addOptions:mediaOptions];
  135. return [[self alloc] initWithMedia:media options:mediaOptions];
  136. }
  137. @end