VLCNetworkServerBrowserVLCMedia.m 5.0 KB

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