VLCNetworkServerBrowserUPnP.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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(_items) {
  64. _items = [itemsArray copy];
  65. }
  66. _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. NSUInteger count = _items.count;
  77. mediaArray = [NSMutableArray arrayWithCapacity:count];
  78. for (NSInteger i = count - 1; i >= 0; i--) {
  79. VLCMedia *media = [_items[i] media];
  80. if (media)
  81. [mediaArray addObject:media];
  82. }
  83. }
  84. VLCMediaList *mediaList = [[VLCMediaList alloc] initWithArray:mediaArray];
  85. return mediaList;
  86. }
  87. @end
  88. @interface MediaServer1ItemObject (VLC)
  89. @end
  90. @implementation MediaServer1ItemObject (VLC)
  91. - (id)vlc_ressourceItemForKey:(NSString *)key urlString:(NSString *)urlString device:(MediaServer1Device *)device {
  92. // Provide users with a descriptive action sheet for them to choose based on the multiple resources advertised by DLNA devices (HDHomeRun for example)
  93. NSRange position = [key rangeOfString:kVLCUPnPVideoProtocolKey];
  94. if (position.location == NSNotFound)
  95. return nil;
  96. NSString *orgPNValue;
  97. NSString *transcodeValue;
  98. // Attempt to parse DLNA.ORG_PN first
  99. NSArray *components = [key componentsSeparatedByString:@";"];
  100. NSArray *nonFlagsComponents = [components[0] componentsSeparatedByString:@":"];
  101. NSString *orgPN = [nonFlagsComponents lastObject];
  102. // Check to see if we are where we should be
  103. NSRange orgPNRange = [orgPN rangeOfString:@"DLNA.ORG_PN="];
  104. if (orgPNRange.location == 0) {
  105. orgPNValue = [orgPN substringFromIndex:orgPNRange.length];
  106. }
  107. // HDHomeRun: Get the transcode profile from the HTTP API if possible
  108. if ([device VLC_isHDHomeRunMediaServer]) {
  109. NSRange transcodeRange = [urlString rangeOfString:@"transcode="];
  110. if (transcodeRange.location != NSNotFound) {
  111. transcodeValue = [urlString substringFromIndex:transcodeRange.location + transcodeRange.length];
  112. // Check that there are no more parameters
  113. NSRange ampersandRange = [transcodeValue rangeOfString:@"&"];
  114. if (ampersandRange.location != NSNotFound) {
  115. transcodeValue = [transcodeValue substringToIndex:transcodeRange.location];
  116. }
  117. transcodeValue = [transcodeValue capitalizedString];
  118. }
  119. }
  120. // Fallbacks to get the most descriptive resource title
  121. NSString *profileTitle;
  122. if ([transcodeValue length] && [orgPNValue length]) {
  123. profileTitle = [NSString stringWithFormat:@"%@ (%@)", transcodeValue, orgPNValue];
  124. // The extra whitespace is to get UIActionSheet to render the text better (this bug has been fixed in iOS 8)
  125. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  126. if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
  127. profileTitle = [NSString stringWithFormat:@" %@ ", profileTitle];
  128. }
  129. }
  130. } else if ([transcodeValue length]) {
  131. profileTitle = transcodeValue;
  132. } else if ([orgPNValue length]) {
  133. profileTitle = orgPNValue;
  134. } else if ([key length]) {
  135. profileTitle = key;
  136. } else if ([urlString length]) {
  137. profileTitle = urlString;
  138. } else {
  139. profileTitle = NSLocalizedString(@"UNKNOWN", nil);
  140. }
  141. return [[VLCNetworkServerBrowserItemUPnPMultiRessource alloc] initWithTitle:profileTitle url:[NSURL URLWithString:urlString]];
  142. }
  143. - (NSArray *)vlc_ressourceItemsForDevice:(MediaServer1Device *)device {
  144. // Store it so we can act on the action sheet callback.
  145. NSMutableArray *array = [NSMutableArray array];
  146. [uriCollection enumerateKeysAndObjectsUsingBlock:^(NSString *_Nonnull key, NSString * _Nonnull urlString, BOOL * _Nonnull stop) {
  147. id item = [self vlc_ressourceItemForKey:key urlString:urlString device:device];
  148. if (item) {
  149. [array addObject:item];
  150. }
  151. }];
  152. return [array copy];
  153. }
  154. @end
  155. @interface VLCNetworkServerBrowserItemUPnP ()
  156. @property (nonatomic, readonly) MediaServer1BasicObject *mediaServerObject;
  157. @property (nonatomic, readonly) MediaServer1Device *upnpDevice;
  158. @end
  159. @implementation VLCNetworkServerBrowserItemUPnP
  160. @synthesize container = _container, name = _name, URL = _URL, fileSizeBytes = _fileSizeBytes;
  161. - (instancetype)initWithBasicObject:(MediaServer1BasicObject *)basicObject device:(nonnull MediaServer1Device *)device
  162. {
  163. self = [super init];
  164. if (self) {
  165. _mediaServerObject = basicObject;
  166. _upnpDevice = device;
  167. _name = basicObject.title;
  168. _thumbnailURL = [NSURL URLWithString:basicObject.albumArt];
  169. _fileSizeBytes = nil;
  170. _duration = nil;
  171. _URL = nil;
  172. _container = basicObject.isContainer;
  173. if (!_container && [basicObject isKindOfClass:[MediaServer1ItemObject class]]) {
  174. MediaServer1ItemObject *mediaItem = (MediaServer1ItemObject *)basicObject;
  175. long long mediaSize = 0;
  176. unsigned int durationInSeconds = 0;
  177. unsigned int bitrate = 0;
  178. for (MediaServer1ItemRes *resource in mediaItem.resources) {
  179. if (resource.bitrate > 0 && resource.durationInSeconds > 0) {
  180. mediaSize = resource.size;
  181. durationInSeconds = resource.durationInSeconds;
  182. bitrate = resource.bitrate;
  183. }
  184. }
  185. if (mediaSize < 1)
  186. mediaSize = [mediaItem.size integerValue];
  187. if (mediaSize < 1)
  188. mediaSize = (bitrate * durationInSeconds);
  189. // 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)
  190. if (mediaSize > 0) {
  191. _fileSizeBytes = @(mediaSize);
  192. }
  193. if (durationInSeconds > 0) {
  194. _duration = [VLCTime timeWithInt:durationInSeconds * 1000].stringValue;
  195. }
  196. NSArray<NSString *>* protocolStrings = [[mediaItem uriCollection] allKeys];
  197. protocolStrings = [protocolStrings filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString * _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
  198. if (evaluatedObject == nil || ![evaluatedObject isKindOfClass:[NSString class]])
  199. return NO;
  200. if ([evaluatedObject respondsToSelector:@selector(containsString:)]) {
  201. if ([evaluatedObject containsString:kVLCUPnPVideoProtocolKey])
  202. return YES;
  203. if ([evaluatedObject containsString:kVLCUPnPAudioProtocolKey])
  204. return YES;
  205. } else {
  206. NSRange foundRange = [evaluatedObject rangeOfString:kVLCUPnPVideoProtocolKey];
  207. if (foundRange.location != NSNotFound)
  208. return YES;
  209. foundRange = [evaluatedObject rangeOfString:kVLCUPnPAudioProtocolKey];
  210. if (foundRange.location != NSNotFound)
  211. return YES;
  212. }
  213. return NO;
  214. }]];
  215. // Check for multiple URIs.
  216. if ([mediaItem.uriCollection count] > 1) {
  217. for (NSString *key in mediaItem.uriCollection) {
  218. if ([key respondsToSelector:@selector(containsString:)]) {
  219. if ([key containsString:kVLCUPnPVideoProtocolKey] || [key containsString:kVLCUPnPAudioProtocolKey]) {
  220. mediaItem.uri = [mediaItem.uriCollection objectForKey:key];
  221. }
  222. } else {
  223. NSRange foundRage = [key rangeOfString:kVLCUPnPVideoProtocolKey];
  224. if (foundRage.location != NSNotFound) {
  225. mediaItem.uri = [mediaItem.uriCollection objectForKey:key];
  226. } else {
  227. foundRage = [key rangeOfString:kVLCUPnPAudioProtocolKey];
  228. if (foundRage.location != NSNotFound) {
  229. mediaItem.uri = [mediaItem.uriCollection objectForKey:key];
  230. }
  231. }
  232. }
  233. }
  234. }
  235. _URL = [NSURL URLWithString:[mediaItem uri]];
  236. }
  237. }
  238. return self;
  239. }
  240. - (BOOL)isContainer {
  241. return self.mediaServerObject.isContainer;
  242. }
  243. - (BOOL)isDownloadable {
  244. // 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.
  245. BOOL downloadable = ![self.upnpDevice VLC_isHDHomeRunMediaServer];
  246. return downloadable;
  247. }
  248. - (id<VLCNetworkServerBrowser>)containerBrowser {
  249. MediaServer1BasicObject *basicObject = self.mediaServerObject;
  250. if (basicObject.isContainer) {
  251. return [[VLCNetworkServerBrowserUPnP alloc] initWithUPNPDevice:self.upnpDevice header:self.mediaServerObject.title andRootID:self.mediaServerObject.objectID];
  252. } else if ([basicObject isKindOfClass:[MediaServer1ItemObject class]]) {
  253. return [[VLCNetworkServerBrowserUPnPMultiRessource alloc] initWithItem:(MediaServer1ItemObject *)self.mediaServerObject device:self.upnpDevice];
  254. } else {
  255. return nil;
  256. }
  257. }
  258. - (UIImage *)image {
  259. UIImage *broadcastImage = nil;
  260. // Custom TV icon for video broadcasts
  261. if ([[self.mediaServerObject objectClass] isEqualToString:@"object.item.videoItem.videoBroadcast"]) {
  262. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  263. broadcastImage = [UIImage imageNamed:@"TVBroadcastIcon"];
  264. } else {
  265. broadcastImage = [UIImage imageNamed:@"TVBroadcastIcon~ipad"];
  266. }
  267. }
  268. return broadcastImage;
  269. }
  270. - (VLCMedia *)media
  271. {
  272. if (!_URL)
  273. return [VLCMedia mediaAsNodeWithName:self.name];
  274. VLCMedia *media = [VLCMedia mediaWithURL:_URL];
  275. NSString *title = self.name;
  276. if (title.length) {
  277. [media setMetadata:self.name forKey:VLCMetaInformationTitle];
  278. }
  279. return media;
  280. }
  281. @end
  282. #pragma mark - Multi Ressource
  283. @implementation VLCNetworkServerBrowserUPnPMultiRessource
  284. @synthesize items = _items, title = _title, delegate = _delegate, mediaList = _mediaList;
  285. - (instancetype)initWithItem:(MediaServer1ItemObject *)itemObject device:(MediaServer1Device *)device
  286. {
  287. self = [super init];
  288. if (self) {
  289. _title = [itemObject title];
  290. _items = [itemObject vlc_ressourceItemsForDevice:device];
  291. _mediaList = [self buildMediaList];
  292. }
  293. return self;
  294. }
  295. - (void) update {
  296. [self.delegate networkServerBrowserDidUpdate:self];
  297. }
  298. - (VLCMediaList *)buildMediaList
  299. {
  300. VLCMediaList *mediaList = [[VLCMediaList alloc] init];
  301. @synchronized(_items) {
  302. NSUInteger count = _items.count;
  303. for (NSInteger i = count - 1; i >= 0; i--) {
  304. VLCMedia *media = [_items[i] media];
  305. if (media)
  306. [mediaList addMedia:media];
  307. }
  308. }
  309. return mediaList;
  310. }
  311. @end
  312. @implementation VLCNetworkServerBrowserItemUPnPMultiRessource
  313. @synthesize URL = _URL, container = _container, fileSizeBytes = _fileSizeBytes, name =_name;
  314. - (instancetype)initWithTitle:(NSString *)title url:(NSURL *)url
  315. {
  316. self = [super init];
  317. if (self) {
  318. _name = title;
  319. _URL = url;
  320. _container = NO;
  321. _fileSizeBytes = nil;
  322. }
  323. return self;
  324. }
  325. - (id<VLCNetworkServerBrowser>)containerBrowser {
  326. return nil;
  327. }
  328. - (VLCMedia *)media
  329. {
  330. if (!_URL)
  331. return nil;
  332. VLCMedia *media = [VLCMedia mediaWithURL:_URL];
  333. NSString *title = self.name;
  334. if (title.length) {
  335. [media setMetadata:self.name forKey:VLCMetaInformationTitle];
  336. }
  337. return media;
  338. }
  339. @end