VLCNetworkServerBrowserUPnP.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*****************************************************************************
  2. * VLCNetworkServerBrowserUPnP.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 "VLCNetworkServerBrowserUPnP.h"
  13. #import "MediaServerBasicObjectParser.h"
  14. #import "MediaServer1ItemObject.h"
  15. #import "MediaServer1ContainerObject.h"
  16. #import "MediaServer1Device.h"
  17. #import "BasicUPnPDevice+VLC.h"
  18. @interface VLCNetworkServerBrowserUPnP ()
  19. @property (nonatomic, readonly) MediaServer1Device *upnpDevice;
  20. @property (nonatomic, readonly) NSString *upnpRootID;
  21. @property (nonatomic, readonly) NSOperationQueue *upnpQueue;
  22. @property (nonatomic, readwrite) NSArray<id<VLCNetworkServerBrowserItem>> *items;
  23. @end
  24. @implementation VLCNetworkServerBrowserUPnP
  25. @synthesize title = _title, delegate = _delegate, items = _items, mediaList = _mediaList;
  26. - (instancetype)initWithUPNPDevice:(MediaServer1Device *)device header:(NSString *)header andRootID:(NSString *)upnpRootID
  27. {
  28. self = [super init];
  29. if (self) {
  30. _upnpDevice = device;
  31. _title = header;
  32. _upnpRootID = upnpRootID;
  33. _upnpQueue = [[NSOperationQueue alloc] init];
  34. _upnpQueue.maxConcurrentOperationCount = 1;
  35. _upnpQueue.name = @"org.videolan.vlc-ios.upnp.update";
  36. _items = [NSArray array];
  37. }
  38. return self;
  39. }
  40. - (void)update {
  41. [self.upnpQueue addOperationWithBlock:^{
  42. NSString *sortCriteria = @"";
  43. NSMutableString *outSortCaps = [[NSMutableString alloc] init];
  44. [[self.upnpDevice contentDirectory] GetSortCapabilitiesWithOutSortCaps:outSortCaps];
  45. if ([outSortCaps rangeOfString:@"dc:title"].location != NSNotFound)
  46. {
  47. sortCriteria = @"+dc:title";
  48. }
  49. NSMutableString *outResult = [[NSMutableString alloc] init];
  50. NSMutableString *outNumberReturned = [[NSMutableString alloc] init];
  51. NSMutableString *outTotalMatches = [[NSMutableString alloc] init];
  52. NSMutableString *outUpdateID = [[NSMutableString alloc] init];
  53. [[self.upnpDevice contentDirectory] BrowseWithObjectID:self.upnpRootID BrowseFlag:@"BrowseDirectChildren" Filter:@"*" StartingIndex:@"0" RequestedCount:@"0" SortCriteria:sortCriteria OutResult:outResult OutNumberReturned:outNumberReturned OutTotalMatches:outTotalMatches OutUpdateID:outUpdateID];
  54. NSData *didl = [outResult dataUsingEncoding:NSUTF8StringEncoding];
  55. MediaServerBasicObjectParser *parser;
  56. NSMutableArray *objectsArray = [[NSMutableArray alloc] init];
  57. parser = [[MediaServerBasicObjectParser alloc] initWithMediaObjectArray:objectsArray itemsOnly:NO];
  58. [parser parseFromData:didl];
  59. NSMutableArray *itemsArray = [[NSMutableArray alloc] init];
  60. for (MediaServer1BasicObject *object in objectsArray) {
  61. [itemsArray addObject:[[VLCNetworkServerBrowserItemUPnP alloc] initWithBasicObject:object device:self.upnpDevice]];
  62. }
  63. @synchronized(self->_items) {
  64. self->_items = [itemsArray copy];
  65. }
  66. self->_mediaList = [self buildMediaList];
  67. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  68. [self.delegate networkServerBrowserDidUpdate:self];
  69. }];
  70. }];
  71. }
  72. - (VLCMediaList *)buildMediaList
  73. {
  74. NSMutableArray *mediaArray;
  75. @synchronized(_items) {
  76. mediaArray = [NSMutableArray new];
  77. for (id<VLCNetworkServerBrowserItem> item in _items) {
  78. VLCMedia *media = [item media];
  79. if (media)
  80. [mediaArray addObject:media];
  81. }
  82. }
  83. VLCMediaList *mediaList = [[VLCMediaList alloc] initWithArray:mediaArray];
  84. return mediaList;
  85. }
  86. @end
  87. @interface MediaServer1ItemObject (VLC)
  88. @end
  89. @implementation MediaServer1ItemObject (VLC)
  90. - (id)vlc_ressourceItemForKey:(NSString *)key urlString:(NSString *)urlString device:(MediaServer1Device *)device {
  91. // Provide users with a descriptive action sheet for them to choose based on the multiple resources advertised by DLNA devices (HDHomeRun for example)
  92. NSRange position = [key rangeOfString:kVLCUPnPVideoProtocolKey];
  93. if (position.location == NSNotFound)
  94. return nil;
  95. NSString *orgPNValue;
  96. NSString *transcodeValue;
  97. // Attempt to parse DLNA.ORG_PN first
  98. NSArray *components = [key componentsSeparatedByString:@";"];
  99. NSArray *nonFlagsComponents = [components[0] componentsSeparatedByString:@":"];
  100. NSString *orgPN = [nonFlagsComponents lastObject];
  101. // Check to see if we are where we should be
  102. NSRange orgPNRange = [orgPN rangeOfString:@"DLNA.ORG_PN="];
  103. if (orgPNRange.location == 0) {
  104. orgPNValue = [orgPN substringFromIndex:orgPNRange.length];
  105. }
  106. // HDHomeRun: Get the transcode profile from the HTTP API if possible
  107. if ([device VLC_isHDHomeRunMediaServer]) {
  108. NSRange transcodeRange = [urlString rangeOfString:@"transcode="];
  109. if (transcodeRange.location != NSNotFound) {
  110. transcodeValue = [urlString substringFromIndex:transcodeRange.location + transcodeRange.length];
  111. // Check that there are no more parameters
  112. NSRange ampersandRange = [transcodeValue rangeOfString:@"&"];
  113. if (ampersandRange.location != NSNotFound) {
  114. transcodeValue = [transcodeValue substringToIndex:transcodeRange.location];
  115. }
  116. transcodeValue = [transcodeValue capitalizedString];
  117. }
  118. }
  119. // Fallbacks to get the most descriptive resource title
  120. NSString *profileTitle;
  121. if ([transcodeValue length] && [orgPNValue length]) {
  122. profileTitle = [NSString stringWithFormat:@"%@ (%@)", transcodeValue, orgPNValue];
  123. } else if ([transcodeValue length]) {
  124. profileTitle = transcodeValue;
  125. } else if ([orgPNValue length]) {
  126. profileTitle = orgPNValue;
  127. } else if ([key length]) {
  128. profileTitle = key;
  129. } else if ([urlString length]) {
  130. profileTitle = urlString;
  131. } else {
  132. profileTitle = NSLocalizedString(@"UNKNOWN", nil);
  133. }
  134. return [[VLCNetworkServerBrowserItemUPnPMultiRessource alloc] initWithTitle:profileTitle url:[NSURL URLWithString:urlString]];
  135. }
  136. - (NSArray *)vlc_ressourceItemsForDevice:(MediaServer1Device *)device {
  137. // Store it so we can act on the action sheet callback.
  138. NSMutableArray *array = [NSMutableArray array];
  139. [uriCollection enumerateKeysAndObjectsUsingBlock:^(NSString *_Nonnull key, NSString * _Nonnull urlString, BOOL * _Nonnull stop) {
  140. id item = [self vlc_ressourceItemForKey:key urlString:urlString device:device];
  141. if (item) {
  142. [array addObject:item];
  143. }
  144. }];
  145. return [array copy];
  146. }
  147. @end
  148. @interface VLCNetworkServerBrowserItemUPnP ()
  149. @property (nonatomic, readonly) MediaServer1BasicObject *mediaServerObject;
  150. @property (nonatomic, readonly) MediaServer1Device *upnpDevice;
  151. @end
  152. @implementation VLCNetworkServerBrowserItemUPnP
  153. @synthesize container = _container, name = _name, URL = _URL, fileSizeBytes = _fileSizeBytes;
  154. - (instancetype)initWithBasicObject:(MediaServer1BasicObject *)basicObject device:(nonnull MediaServer1Device *)device
  155. {
  156. self = [super init];
  157. if (self) {
  158. _mediaServerObject = basicObject;
  159. _upnpDevice = device;
  160. _name = basicObject.title;
  161. _thumbnailURL = [NSURL URLWithString:basicObject.albumArt];
  162. _fileSizeBytes = nil;
  163. _duration = nil;
  164. _URL = nil;
  165. _container = basicObject.isContainer;
  166. if (!_container && [basicObject isKindOfClass:[MediaServer1ItemObject class]]) {
  167. MediaServer1ItemObject *mediaItem = (MediaServer1ItemObject *)basicObject;
  168. long long mediaSize = 0;
  169. unsigned int durationInSeconds = 0;
  170. unsigned int bitrate = 0;
  171. for (MediaServer1ItemRes *resource in mediaItem.resources) {
  172. if (resource.bitrate > 0 && resource.durationInSeconds > 0) {
  173. mediaSize = resource.size;
  174. durationInSeconds = resource.durationInSeconds;
  175. bitrate = resource.bitrate;
  176. }
  177. }
  178. if (mediaSize < 1)
  179. mediaSize = [mediaItem.size integerValue];
  180. if (mediaSize < 1)
  181. mediaSize = (bitrate * durationInSeconds);
  182. // object.item.videoItem.videoBroadcast items (like the HDHomeRun) may not have this information. Center the title (this makes channel names look better for the HDHomeRun)
  183. if (mediaSize > 0) {
  184. _fileSizeBytes = @(mediaSize);
  185. }
  186. if (durationInSeconds > 0) {
  187. _duration = [VLCTime timeWithInt:durationInSeconds * 1000].stringValue;
  188. }
  189. NSArray<NSString *>* protocolStrings = [[mediaItem uriCollection] allKeys];
  190. protocolStrings = [protocolStrings filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString * _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
  191. if (evaluatedObject == nil || ![evaluatedObject isKindOfClass:[NSString class]])
  192. return NO;
  193. if ([evaluatedObject containsString:kVLCUPnPVideoProtocolKey])
  194. return YES;
  195. if ([evaluatedObject containsString:kVLCUPnPAudioProtocolKey])
  196. return YES;
  197. return NO;
  198. }]];
  199. // Check for multiple URIs.
  200. if ([mediaItem.uriCollection count] > 1) {
  201. for (NSString *key in mediaItem.uriCollection) {
  202. if ([key containsString:kVLCUPnPVideoProtocolKey] || [key containsString:kVLCUPnPAudioProtocolKey]) {
  203. mediaItem.uri = [mediaItem.uriCollection objectForKey:key];
  204. }
  205. }
  206. }
  207. _URL = [NSURL URLWithString:[mediaItem uri]];
  208. }
  209. }
  210. return self;
  211. }
  212. - (BOOL)isContainer {
  213. return self.mediaServerObject.isContainer;
  214. }
  215. - (BOOL)isDownloadable {
  216. // Disable downloading for the HDHomeRun for now to avoid infinite downloads (URI needs a duration parameter, otherwise you are just downloading a live stream). VLC also needs an extension in the file name for this to work.
  217. BOOL downloadable = ![self.upnpDevice VLC_isHDHomeRunMediaServer];
  218. return downloadable;
  219. }
  220. - (id<VLCNetworkServerBrowser>)containerBrowser {
  221. MediaServer1BasicObject *basicObject = self.mediaServerObject;
  222. if (basicObject.isContainer) {
  223. return [[VLCNetworkServerBrowserUPnP alloc] initWithUPNPDevice:self.upnpDevice header:self.mediaServerObject.title andRootID:self.mediaServerObject.objectID];
  224. } else if ([basicObject isKindOfClass:[MediaServer1ItemObject class]]) {
  225. return [[VLCNetworkServerBrowserUPnPMultiRessource alloc] initWithItem:(MediaServer1ItemObject *)self.mediaServerObject device:self.upnpDevice];
  226. } else {
  227. return nil;
  228. }
  229. }
  230. - (UIImage *)image {
  231. UIImage *broadcastImage = nil;
  232. // Custom TV icon for video broadcasts
  233. if ([[self.mediaServerObject objectClass] isEqualToString:@"object.item.videoItem.videoBroadcast"]) {
  234. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  235. broadcastImage = [UIImage imageNamed:@"TVBroadcastIcon"];
  236. } else {
  237. broadcastImage = [UIImage imageNamed:@"TVBroadcastIcon~ipad"];
  238. }
  239. }
  240. return broadcastImage;
  241. }
  242. - (VLCMedia *)media
  243. {
  244. if (!_URL)
  245. return [VLCMedia mediaAsNodeWithName:self.name];
  246. VLCMedia *media = [VLCMedia mediaWithURL:_URL];
  247. NSString *title = self.name;
  248. if (title.length) {
  249. [media setMetadata:self.name forKey:VLCMetaInformationTitle];
  250. }
  251. return media;
  252. }
  253. @end
  254. #pragma mark - Multi Ressource
  255. @implementation VLCNetworkServerBrowserUPnPMultiRessource
  256. @synthesize items = _items, title = _title, delegate = _delegate, mediaList = _mediaList;
  257. - (instancetype)initWithItem:(MediaServer1ItemObject *)itemObject device:(MediaServer1Device *)device
  258. {
  259. self = [super init];
  260. if (self) {
  261. _title = [itemObject title];
  262. _items = [itemObject vlc_ressourceItemsForDevice:device];
  263. _mediaList = [self buildMediaList];
  264. }
  265. return self;
  266. }
  267. - (void) update {
  268. [self.delegate networkServerBrowserDidUpdate:self];
  269. }
  270. - (VLCMediaList *)buildMediaList
  271. {
  272. VLCMediaList *mediaList = [[VLCMediaList alloc] init];
  273. @synchronized(_items) {
  274. for (id<VLCNetworkServerBrowserItem> browseritem in _items) {
  275. VLCMedia *media = [browseritem media];
  276. if (media)
  277. [mediaList addMedia:media];
  278. }
  279. }
  280. return mediaList;
  281. }
  282. @end
  283. @implementation VLCNetworkServerBrowserItemUPnPMultiRessource
  284. @synthesize URL = _URL, container = _container, fileSizeBytes = _fileSizeBytes, name =_name;
  285. - (instancetype)initWithTitle:(NSString *)title url:(NSURL *)url
  286. {
  287. self = [super init];
  288. if (self) {
  289. _name = title;
  290. _URL = url;
  291. _container = NO;
  292. _fileSizeBytes = nil;
  293. }
  294. return self;
  295. }
  296. - (id<VLCNetworkServerBrowser>)containerBrowser {
  297. return nil;
  298. }
  299. - (VLCMedia *)media
  300. {
  301. if (!_URL)
  302. return nil;
  303. VLCMedia *media = [VLCMedia mediaWithURL:_URL];
  304. NSString *title = self.name;
  305. if (title.length) {
  306. [media setMetadata:self.name forKey:VLCMetaInformationTitle];
  307. }
  308. return media;
  309. }
  310. @end