VLCNetworkServerBrowserVLCMedia.m 5.2 KB

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