VLCNetworkServerBrowserUPnP.m 13 KB

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