VLCNetworkServerBrowserVLCMedia.m 4.4 KB

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