VLCNetworkServerBrowserUPnP.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. self.items = [itemsArray copy];
  64. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  65. [self.delegate networkServerBrowserDidUpdate:self];
  66. }];
  67. }];
  68. }
  69. @end
  70. @interface MediaServer1ItemObject (VLC)
  71. @end
  72. @implementation MediaServer1ItemObject (VLC)
  73. - (id)vlc_ressourceItemForKey:(NSString *)key urlString:(NSString *)urlString device:(MediaServer1Device *)device {
  74. // Provide users with a descriptive action sheet for them to choose based on the multiple resources advertised by DLNA devices (HDHomeRun for example)
  75. NSRange position = [key rangeOfString:@"http-get:*:video/"];
  76. if (position.location == NSNotFound)
  77. return nil;
  78. NSString *orgPNValue;
  79. NSString *transcodeValue;
  80. // Attempt to parse DLNA.ORG_PN first
  81. NSArray *components = [key componentsSeparatedByString:@";"];
  82. NSArray *nonFlagsComponents = [components[0] componentsSeparatedByString:@":"];
  83. NSString *orgPN = [nonFlagsComponents lastObject];
  84. // Check to see if we are where we should be
  85. NSRange orgPNRange = [orgPN rangeOfString:@"DLNA.ORG_PN="];
  86. if (orgPNRange.location == 0) {
  87. orgPNValue = [orgPN substringFromIndex:orgPNRange.length];
  88. }
  89. // HDHomeRun: Get the transcode profile from the HTTP API if possible
  90. if ([device VLC_isHDHomeRunMediaServer]) {
  91. NSRange transcodeRange = [urlString rangeOfString:@"transcode="];
  92. if (transcodeRange.location != NSNotFound) {
  93. transcodeValue = [urlString substringFromIndex:transcodeRange.location + transcodeRange.length];
  94. // Check that there are no more parameters
  95. NSRange ampersandRange = [transcodeValue rangeOfString:@"&"];
  96. if (ampersandRange.location != NSNotFound) {
  97. transcodeValue = [transcodeValue substringToIndex:transcodeRange.location];
  98. }
  99. transcodeValue = [transcodeValue capitalizedString];
  100. }
  101. }
  102. // Fallbacks to get the most descriptive resource title
  103. NSString *profileTitle;
  104. if ([transcodeValue length] && [orgPNValue length]) {
  105. profileTitle = [NSString stringWithFormat:@"%@ (%@)", transcodeValue, orgPNValue];
  106. // The extra whitespace is to get UIActionSheet to render the text better (this bug has been fixed in iOS 8)
  107. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  108. if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
  109. profileTitle = [NSString stringWithFormat:@" %@ ", profileTitle];
  110. }
  111. }
  112. } else if ([transcodeValue length]) {
  113. profileTitle = transcodeValue;
  114. } else if ([orgPNValue length]) {
  115. profileTitle = orgPNValue;
  116. } else if ([key length]) {
  117. profileTitle = key;
  118. } else if ([urlString length]) {
  119. profileTitle = urlString;
  120. } else {
  121. profileTitle = NSLocalizedString(@"UNKNOWN", nil);
  122. }
  123. return [[VLCNetworkServerBrowserItemUPnPMultiRessource alloc] initWithTitle:profileTitle url:[NSURL URLWithString:urlString]];
  124. }
  125. - (NSArray *)vlc_ressourceItemsForDevice:(MediaServer1Device *)device {
  126. // Store it so we can act on the action sheet callback.
  127. NSMutableArray *array = [NSMutableArray array];
  128. [uriCollection enumerateKeysAndObjectsUsingBlock:^(NSString *_Nonnull key, NSString * _Nonnull urlString, BOOL * _Nonnull stop) {
  129. id item = [self vlc_ressourceItemForKey:key urlString:urlString device:device];
  130. if (item) {
  131. [array addObject:item];
  132. }
  133. }];
  134. return [array copy];
  135. }
  136. @end
  137. @interface VLCNetworkServerBrowserItemUPnP ()
  138. @property (nonatomic, readonly) MediaServer1BasicObject *mediaServerObject;
  139. @property (nonatomic, readonly) MediaServer1Device *upnpDevice;
  140. @end
  141. @implementation VLCNetworkServerBrowserItemUPnP
  142. @synthesize container = _container, name = _name, URL = _URL, fileSizeBytes = _fileSizeBytes;
  143. - (instancetype)initWithBasicObject:(MediaServer1BasicObject *)basicObject device:(nonnull MediaServer1Device *)device
  144. {
  145. self = [super init];
  146. if (self) {
  147. _mediaServerObject = basicObject;
  148. _upnpDevice = device;
  149. _name = basicObject.title;
  150. _thumbnailURL = [NSURL URLWithString:basicObject.albumArt];
  151. _fileSizeBytes = nil;
  152. _duration = nil;
  153. _URL = nil;
  154. _container = basicObject.isContainer;
  155. if (!_container && [basicObject isKindOfClass:[MediaServer1ItemObject class]]) {
  156. MediaServer1ItemObject *mediaItem = (MediaServer1ItemObject *)basicObject;
  157. long long mediaSize = 0;
  158. unsigned int durationInSeconds = 0;
  159. unsigned int bitrate = 0;
  160. for (MediaServer1ItemRes *resource in mediaItem.resources) {
  161. if (resource.bitrate > 0 && resource.durationInSeconds > 0) {
  162. mediaSize = resource.size;
  163. durationInSeconds = resource.durationInSeconds;
  164. bitrate = resource.bitrate;
  165. }
  166. }
  167. if (mediaSize < 1)
  168. mediaSize = [mediaItem.size integerValue];
  169. if (mediaSize < 1)
  170. mediaSize = (bitrate * durationInSeconds);
  171. // 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)
  172. if (mediaSize > 0) {
  173. _fileSizeBytes = @(mediaSize);
  174. }
  175. if (durationInSeconds > 0) {
  176. _duration = [VLCTime timeWithInt:durationInSeconds * 1000].stringValue;
  177. }
  178. NSArray<NSString *>* protocolStrings = [[mediaItem uriCollection] allKeys];
  179. protocolStrings = [protocolStrings filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString * _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
  180. return [evaluatedObject containsString:@"http-get:*:video/"];
  181. }]];
  182. if (protocolStrings.count == 1) {
  183. _URL = [NSURL URLWithString:[mediaItem uri]];
  184. } else if (protocolStrings.count > 1) {
  185. // whith mutlple playable ressources we simulate to be a container
  186. _container = YES;
  187. }
  188. }
  189. }
  190. return self;
  191. }
  192. - (BOOL)isContainer {
  193. return self.mediaServerObject.isContainer;
  194. }
  195. - (BOOL)isDownloadable {
  196. // 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.
  197. BOOL downloadable = ![self.upnpDevice VLC_isHDHomeRunMediaServer];
  198. return downloadable;
  199. }
  200. - (id<VLCNetworkServerBrowser>)containerBrowser {
  201. MediaServer1BasicObject *basicObject = self.mediaServerObject;
  202. if (basicObject.isContainer) {
  203. return [[VLCNetworkServerBrowserUPnP alloc] initWithUPNPDevice:self.upnpDevice header:self.mediaServerObject.title andRootID:self.mediaServerObject.objectID];
  204. } else if ([basicObject isKindOfClass:[MediaServer1ItemObject class]]) {
  205. return [[VLCNetworkServerBrowserUPnPMultiRessource alloc] initWithItem:(MediaServer1ItemObject *)self.mediaServerObject device:self.upnpDevice];
  206. } else {
  207. return nil;
  208. }
  209. }
  210. - (UIImage *)image {
  211. UIImage *broadcastImage = nil;
  212. // Custom TV icon for video broadcasts
  213. if ([[self.mediaServerObject objectClass] isEqualToString:@"object.item.videoItem.videoBroadcast"]) {
  214. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  215. broadcastImage = [UIImage imageNamed:@"TVBroadcastIcon"];
  216. } else {
  217. broadcastImage = [UIImage imageNamed:@"TVBroadcastIcon~ipad"];
  218. }
  219. }
  220. return broadcastImage;
  221. }
  222. @end
  223. #pragma mark - Multi Ressource
  224. @implementation VLCNetworkServerBrowserUPnPMultiRessource
  225. @synthesize items = _items, title = _title, delegate = _delegate;
  226. - (instancetype)initWithItem:(MediaServer1ItemObject *)itemObject device:(MediaServer1Device *)device
  227. {
  228. self = [super init];
  229. if (self) {
  230. _title = [itemObject title];
  231. _items = [itemObject vlc_ressourceItemsForDevice:device];
  232. }
  233. return self;
  234. }
  235. - (void) update {
  236. [self.delegate networkServerBrowserDidUpdate:self];
  237. }
  238. @end
  239. @implementation VLCNetworkServerBrowserItemUPnPMultiRessource
  240. @synthesize URL = _URL, container = _container, fileSizeBytes = _fileSizeBytes, name =_name;
  241. - (instancetype)initWithTitle:(NSString *)title url:(NSURL *)url
  242. {
  243. self = [super init];
  244. if (self) {
  245. _name = title;
  246. _URL = url;
  247. _container = NO;
  248. _fileSizeBytes = nil;
  249. }
  250. return self;
  251. }
  252. - (id<VLCNetworkServerBrowser>)containerBrowser {
  253. return nil;
  254. }
  255. @end