VLCNetworkServerBrowserUPnP.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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;
  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. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  67. [self.delegate networkServerBrowserDidUpdate:self];
  68. }];
  69. }];
  70. }
  71. - (VLCMediaList *)mediaList
  72. {
  73. NSMutableArray *mediaArray;
  74. @synchronized(_items) {
  75. NSUInteger count = _items.count;
  76. mediaArray = [NSMutableArray arrayWithCapacity:count];
  77. for (NSInteger i = count - 1; i >= 0; i--) {
  78. VLCMedia *media = [_items[i] 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:@"http-get:*:video/"];
  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. // The extra whitespace is to get UIActionSheet to render the text better (this bug has been fixed in iOS 8)
  124. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  125. if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
  126. profileTitle = [NSString stringWithFormat:@" %@ ", profileTitle];
  127. }
  128. }
  129. } else if ([transcodeValue length]) {
  130. profileTitle = transcodeValue;
  131. } else if ([orgPNValue length]) {
  132. profileTitle = orgPNValue;
  133. } else if ([key length]) {
  134. profileTitle = key;
  135. } else if ([urlString length]) {
  136. profileTitle = urlString;
  137. } else {
  138. profileTitle = NSLocalizedString(@"UNKNOWN", nil);
  139. }
  140. return [[VLCNetworkServerBrowserItemUPnPMultiRessource alloc] initWithTitle:profileTitle url:[NSURL URLWithString:urlString]];
  141. }
  142. - (NSArray *)vlc_ressourceItemsForDevice:(MediaServer1Device *)device {
  143. // Store it so we can act on the action sheet callback.
  144. NSMutableArray *array = [NSMutableArray array];
  145. [uriCollection enumerateKeysAndObjectsUsingBlock:^(NSString *_Nonnull key, NSString * _Nonnull urlString, BOOL * _Nonnull stop) {
  146. id item = [self vlc_ressourceItemForKey:key urlString:urlString device:device];
  147. if (item) {
  148. [array addObject:item];
  149. }
  150. }];
  151. return [array copy];
  152. }
  153. @end
  154. @interface VLCNetworkServerBrowserItemUPnP ()
  155. @property (nonatomic, readonly) MediaServer1BasicObject *mediaServerObject;
  156. @property (nonatomic, readonly) MediaServer1Device *upnpDevice;
  157. @end
  158. @implementation VLCNetworkServerBrowserItemUPnP
  159. @synthesize container = _container, name = _name, URL = _URL, fileSizeBytes = _fileSizeBytes;
  160. - (instancetype)initWithBasicObject:(MediaServer1BasicObject *)basicObject device:(nonnull MediaServer1Device *)device
  161. {
  162. self = [super init];
  163. if (self) {
  164. _mediaServerObject = basicObject;
  165. _upnpDevice = device;
  166. _name = basicObject.title;
  167. _thumbnailURL = [NSURL URLWithString:basicObject.albumArt];
  168. _fileSizeBytes = nil;
  169. _duration = nil;
  170. _URL = nil;
  171. _container = basicObject.isContainer;
  172. if (!_container && [basicObject isKindOfClass:[MediaServer1ItemObject class]]) {
  173. MediaServer1ItemObject *mediaItem = (MediaServer1ItemObject *)basicObject;
  174. long long mediaSize = 0;
  175. unsigned int durationInSeconds = 0;
  176. unsigned int bitrate = 0;
  177. for (MediaServer1ItemRes *resource in mediaItem.resources) {
  178. if (resource.bitrate > 0 && resource.durationInSeconds > 0) {
  179. mediaSize = resource.size;
  180. durationInSeconds = resource.durationInSeconds;
  181. bitrate = resource.bitrate;
  182. }
  183. }
  184. if (mediaSize < 1)
  185. mediaSize = [mediaItem.size integerValue];
  186. if (mediaSize < 1)
  187. mediaSize = (bitrate * durationInSeconds);
  188. // 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)
  189. if (mediaSize > 0) {
  190. _fileSizeBytes = @(mediaSize);
  191. }
  192. if (durationInSeconds > 0) {
  193. _duration = [VLCTime timeWithInt:durationInSeconds * 1000].stringValue;
  194. }
  195. NSArray<NSString *>* protocolStrings = [[mediaItem uriCollection] allKeys];
  196. protocolStrings = [protocolStrings filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString * _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
  197. if (evaluatedObject == nil || ![evaluatedObject isKindOfClass:[NSString class]])
  198. return NO;
  199. if ([evaluatedObject respondsToSelector:@selector(containsString:)]) {
  200. if ([evaluatedObject containsString:@"http-get:*:video/"])
  201. return YES;
  202. if ([evaluatedObject containsString:@"http-get:*:audio/"])
  203. return YES;
  204. } else {
  205. NSRange foundRange = [evaluatedObject rangeOfString:@"http-get:*:video/"];
  206. if (foundRange.location != NSNotFound)
  207. return YES;
  208. foundRange = [evaluatedObject rangeOfString:@"http-get:*:audio/"];
  209. if (foundRange.location != NSNotFound)
  210. return YES;
  211. }
  212. return NO;
  213. }]];
  214. /* FIXME: on some servers, we can have more than 1 protocol string as different transcoding schemes are offered
  215. * in previous versions, we offered a selector - maybe re-add? */
  216. _URL = [NSURL URLWithString:[mediaItem uri]];
  217. }
  218. }
  219. return self;
  220. }
  221. - (BOOL)isContainer {
  222. return self.mediaServerObject.isContainer;
  223. }
  224. - (BOOL)isDownloadable {
  225. // 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.
  226. BOOL downloadable = ![self.upnpDevice VLC_isHDHomeRunMediaServer];
  227. return downloadable;
  228. }
  229. - (id<VLCNetworkServerBrowser>)containerBrowser {
  230. MediaServer1BasicObject *basicObject = self.mediaServerObject;
  231. if (basicObject.isContainer) {
  232. return [[VLCNetworkServerBrowserUPnP alloc] initWithUPNPDevice:self.upnpDevice header:self.mediaServerObject.title andRootID:self.mediaServerObject.objectID];
  233. } else if ([basicObject isKindOfClass:[MediaServer1ItemObject class]]) {
  234. return [[VLCNetworkServerBrowserUPnPMultiRessource alloc] initWithItem:(MediaServer1ItemObject *)self.mediaServerObject device:self.upnpDevice];
  235. } else {
  236. return nil;
  237. }
  238. }
  239. - (UIImage *)image {
  240. UIImage *broadcastImage = nil;
  241. // Custom TV icon for video broadcasts
  242. if ([[self.mediaServerObject objectClass] isEqualToString:@"object.item.videoItem.videoBroadcast"]) {
  243. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  244. broadcastImage = [UIImage imageNamed:@"TVBroadcastIcon"];
  245. } else {
  246. broadcastImage = [UIImage imageNamed:@"TVBroadcastIcon~ipad"];
  247. }
  248. }
  249. return broadcastImage;
  250. }
  251. - (VLCMedia *)media
  252. {
  253. if (!_URL)
  254. return nil;
  255. VLCMedia *media = [VLCMedia mediaWithURL:_URL];
  256. NSString *title = self.name;
  257. if (title.length) {
  258. [media setMetadata:self.name forKey:VLCMetaInformationTitle];
  259. }
  260. return media;
  261. }
  262. @end
  263. #pragma mark - Multi Ressource
  264. @implementation VLCNetworkServerBrowserUPnPMultiRessource
  265. @synthesize items = _items, title = _title, delegate = _delegate;
  266. - (instancetype)initWithItem:(MediaServer1ItemObject *)itemObject device:(MediaServer1Device *)device
  267. {
  268. self = [super init];
  269. if (self) {
  270. _title = [itemObject title];
  271. _items = [itemObject vlc_ressourceItemsForDevice:device];
  272. }
  273. return self;
  274. }
  275. - (void) update {
  276. [self.delegate networkServerBrowserDidUpdate:self];
  277. }
  278. - (VLCMediaList *)mediaList
  279. {
  280. VLCMediaList *mediaList = [[VLCMediaList alloc] init];
  281. @synchronized(_items) {
  282. NSUInteger count = _items.count;
  283. for (NSInteger i = count - 1; i >= 0; i--) {
  284. VLCMedia *media = [_items[i] media];
  285. if (media)
  286. [mediaList addMedia:media];
  287. }
  288. }
  289. return mediaList;
  290. }
  291. @end
  292. @implementation VLCNetworkServerBrowserItemUPnPMultiRessource
  293. @synthesize URL = _URL, container = _container, fileSizeBytes = _fileSizeBytes, name =_name;
  294. - (instancetype)initWithTitle:(NSString *)title url:(NSURL *)url
  295. {
  296. self = [super init];
  297. if (self) {
  298. _name = title;
  299. _URL = url;
  300. _container = NO;
  301. _fileSizeBytes = nil;
  302. }
  303. return self;
  304. }
  305. - (id<VLCNetworkServerBrowser>)containerBrowser {
  306. return nil;
  307. }
  308. - (VLCMedia *)media
  309. {
  310. if (!_URL)
  311. return nil;
  312. VLCMedia *media = [VLCMedia mediaWithURL:_URL];
  313. NSString *title = self.name;
  314. if (title.length) {
  315. [media setMetadata:self.name forKey:VLCMetaInformationTitle];
  316. }
  317. return media;
  318. }
  319. @end