VLCNetworkServerBrowserUPnP.m 12 KB

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