VLCLocalServerDiscoveryController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*****************************************************************************
  2. * VLCLocalServerListViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Pierre SAGASPE <pierre.sagaspe # me.com>
  10. * Tobias Conradi <videolan # tobias-conradi.de>
  11. *
  12. * Refer to the COPYING file of the official project for license.
  13. *****************************************************************************/
  14. #import "VLCLocalServerDiscoveryController.h"
  15. #import "VLCServerListViewController.h"
  16. #import "VLCPlaybackController.h"
  17. #import "UPnPManager.h"
  18. #import "VLCNetworkListCell.h"
  19. #import "VLCLocalPlexFolderListViewController.h"
  20. #import "VLCFTPServerListViewController.h"
  21. #import "VLCUPnPServerListViewController.h"
  22. #import "VLCDiscoveryListViewController.h"
  23. #import "VLCSharedLibraryListViewController.h"
  24. #import "VLCSharedLibraryParser.h"
  25. #import "VLCHTTPUploaderController.h"
  26. #import "Reachability.h"
  27. #import "VLCLocalNetworkServiceBrowserNetService.h"
  28. typedef NS_ENUM(NSUInteger, VLCLocalServerSections) {
  29. VLCLocalServerSectionGeneric = 0,
  30. VLCLocalServerSectionUPnP,
  31. VLCLocalServerSectionPlex,
  32. VLCLocalServerSectionFTP,
  33. VLCLocalServerSectionVLCiOS,
  34. VLCLocalServerSectionSMB,
  35. VLCLocalServerSectionSAP
  36. };
  37. @interface VLCLocalServerDiscoveryController () <VLCLocalNetworkServiceBrowserDelegate, VLCMediaListDelegate, UPnPDBObserver>
  38. {
  39. id<VLCLocalNetworkServiceBrowser> _plexBrowser;
  40. id<VLCLocalNetworkServiceBrowser> _FTPBrowser;
  41. id<VLCLocalNetworkServiceBrowser> _HTTPBrowser;
  42. NSArray<VLCLocalNetworkServiceUPnP*> *_filteredUPNPDevices;
  43. NSArray *_UPNPdevices;
  44. VLCMediaDiscoverer *_sapDiscoverer;
  45. VLCMediaDiscoverer *_dsmDiscoverer;
  46. VLCSharedLibraryParser *_httpParser;
  47. Reachability *_reachability;
  48. NSString *_myHostName;
  49. BOOL _udnpDiscoveryRunning;
  50. NSTimer *_searchTimer;
  51. BOOL _setup;
  52. }
  53. @end
  54. @implementation VLCLocalServerDiscoveryController
  55. - (void)dealloc
  56. {
  57. [[NSNotificationCenter defaultCenter] removeObserver:self];
  58. [_reachability stopNotifier];
  59. [self stopDiscovery];
  60. }
  61. - (void)stopDiscovery
  62. {
  63. [self _stopUPNPDiscovery];
  64. [self _stopSAPDiscovery];
  65. [self _stopDSMDiscovery];
  66. [_FTPBrowser stopDiscovery];
  67. [_plexBrowser stopDiscovery];
  68. [_HTTPBrowser stopDiscovery];
  69. }
  70. - (void)startDiscovery
  71. {
  72. [_FTPBrowser startDiscovery];
  73. [_plexBrowser startDiscovery];
  74. [_HTTPBrowser startDiscovery];
  75. [self netReachabilityChanged];
  76. }
  77. - (NSArray *)sectionHeaderTexts
  78. {
  79. return @[@"Generic",
  80. @"Universal Plug'n'Play (UPnP)",
  81. _plexBrowser.name,
  82. _FTPBrowser.name,
  83. _HTTPBrowser.name,
  84. NSLocalizedString(@"SMB_CIFS_FILE_SERVERS", nil),
  85. @"SAP"];
  86. }
  87. - (instancetype)init
  88. {
  89. self = [super init];
  90. if (!self)
  91. return nil;
  92. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  93. [defaultCenter addObserver:self
  94. selector:@selector(stopDiscovery)
  95. name:UIApplicationWillResignActiveNotification
  96. object:[UIApplication sharedApplication]];
  97. [defaultCenter addObserver:self
  98. selector:@selector(startDiscovery:)
  99. name:UIApplicationDidBecomeActiveNotification
  100. object:[UIApplication sharedApplication]];
  101. _plexBrowser = [[VLCLocalNetworkServiceBrowserPlex alloc] init];
  102. _plexBrowser.delegate = self;
  103. _FTPBrowser = [[VLCLocalNetworkServiceBrowserFTP alloc] init];
  104. _FTPBrowser.delegate = self;
  105. _HTTPBrowser = [[VLCLocalNetworkServiceBrowserHTTP alloc] init];
  106. _HTTPBrowser.delegate = self;
  107. _reachability = [Reachability reachabilityForLocalWiFi];
  108. [_reachability startNotifier];
  109. [self netReachabilityChanged];
  110. _myHostName = [[VLCHTTPUploaderController sharedInstance] hostname];
  111. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netReachabilityChanged) name:kReachabilityChangedNotification object:nil];
  112. return self;
  113. }
  114. - (void)netReachabilityChanged
  115. {
  116. if (_reachability.currentReachabilityStatus == ReachableViaWiFi) {
  117. [self _startUPNPDiscovery];
  118. [self _startSAPDiscovery];
  119. [self _startDSMDiscovery];
  120. } else {
  121. [self _stopUPNPDiscovery];
  122. [self _stopSAPDiscovery];
  123. [self _stopDSMDiscovery];
  124. }
  125. }
  126. - (IBAction)goBack:(id)sender
  127. {
  128. [self _stopUPNPDiscovery];
  129. [self _stopSAPDiscovery];
  130. [self _stopDSMDiscovery];
  131. [[VLCSidebarController sharedInstance] toggleSidebar];
  132. }
  133. - (BOOL)shouldAutorotate
  134. {
  135. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  136. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  137. return NO;
  138. return YES;
  139. }
  140. #pragma mark - table view handling
  141. - (id<VLCLocalNetworkService>)networkServiceForIndexPath:(NSIndexPath *)indexPath
  142. {
  143. VLCLocalServerSections section = indexPath.section;
  144. NSUInteger row = indexPath.row;
  145. switch (section) {
  146. case VLCLocalServerSectionGeneric:
  147. {
  148. return [[VLCLocalNetworkServiceItemLogin alloc] init];
  149. }
  150. case VLCLocalServerSectionUPnP:
  151. {
  152. if (_filteredUPNPDevices.count > row) {
  153. return _filteredUPNPDevices[row];
  154. }
  155. }
  156. case VLCLocalServerSectionPlex:
  157. {
  158. return [_plexBrowser networkServiceForIndex:row];
  159. }
  160. case VLCLocalServerSectionFTP:
  161. {
  162. return [_FTPBrowser networkServiceForIndex:row];
  163. }
  164. case VLCLocalServerSectionVLCiOS:
  165. {
  166. return [_HTTPBrowser networkServiceForIndex:row];
  167. }
  168. case VLCLocalServerSectionSMB:
  169. {
  170. return [[VLCLocalNetworkServiceDSM alloc] initWithMediaItem:[_dsmDiscoverer.discoveredMedia mediaAtIndex:row]];
  171. }
  172. case VLCLocalServerSectionSAP:
  173. {
  174. return [[VLCLocalNetworkServiceSAP alloc] initWithMediaItem:[_sapDiscoverer.discoveredMedia mediaAtIndex:row]];
  175. }
  176. default:
  177. break;
  178. }
  179. return [[VLCLocalNetworkServiceItem alloc] initWithTile:@"FAIL"
  180. icon:nil];
  181. }
  182. - (NSInteger)numberOfItemsInSection:(NSInteger)section
  183. {
  184. switch (section) {
  185. case VLCLocalServerSectionGeneric:
  186. return 1;
  187. case VLCLocalServerSectionUPnP:
  188. return _filteredUPNPDevices.count;
  189. case VLCLocalServerSectionPlex:
  190. return _plexBrowser.numberOfItems;
  191. case VLCLocalServerSectionFTP:
  192. return _FTPBrowser.numberOfItems;
  193. case VLCLocalServerSectionVLCiOS:
  194. return _HTTPBrowser.numberOfItems;
  195. case VLCLocalServerSectionSMB:
  196. return _dsmDiscoverer.discoveredMedia.count;
  197. case VLCLocalServerSectionSAP:
  198. return _sapDiscoverer.discoveredMedia.count;
  199. default:
  200. return 0;
  201. }
  202. }
  203. - (BOOL)refreshDiscoveredData
  204. {
  205. if (_reachability.currentReachabilityStatus != ReachableViaWiFi)
  206. return NO;
  207. UPnPManager *managerInstance = [UPnPManager GetInstance];
  208. [[managerInstance DB] removeObserver:self];
  209. [[managerInstance SSDP] stopSSDP];
  210. [self _stopDSMDiscovery];
  211. [self _startUPNPDiscovery];
  212. [self _startSAPDiscovery];
  213. [self _startDSMDiscovery];
  214. return YES;
  215. }
  216. #pragma mark - VLCLocalNetworkServiceBrowserDelegate
  217. - (void)localNetworkServiceBrowserDidUpdateServices:(id<VLCLocalNetworkServiceBrowser>)serviceBrowser {
  218. if ([self.delegate respondsToSelector:@selector(discoveryFoundSomethingNew)]) {
  219. [self.delegate discoveryFoundSomethingNew];
  220. }
  221. }
  222. #pragma mark - UPNP discovery
  223. - (void)_startUPNPDiscovery
  224. {
  225. if (_reachability.currentReachabilityStatus != ReachableViaWiFi)
  226. return;
  227. UPnPManager *managerInstance = [UPnPManager GetInstance];
  228. _UPNPdevices = [[managerInstance DB] rootDevices];
  229. if (_UPNPdevices.count > 0)
  230. [self UPnPDBUpdated:nil];
  231. [[managerInstance DB] addObserver:self];
  232. //Optional; set User Agent
  233. if (!_setup) {
  234. [[managerInstance SSDP] setUserAgentProduct:[NSString stringWithFormat:@"VLCforiOS/%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]] andOS:[NSString stringWithFormat:@"iOS/%@", [[UIDevice currentDevice] systemVersion]]];
  235. _setup = YES;
  236. }
  237. //Search for UPnP Devices
  238. [[managerInstance SSDP] startSSDP];
  239. [[managerInstance SSDP] notifySSDPAlive];
  240. _searchTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:1.0] interval:10.0 target:self selector:@selector(_performSSDPSearch) userInfo:nil repeats:YES];
  241. [[NSRunLoop mainRunLoop] addTimer:_searchTimer forMode:NSRunLoopCommonModes];
  242. _udnpDiscoveryRunning = YES;
  243. }
  244. - (void)_performSSDPSearch
  245. {
  246. UPnPManager *managerInstance = [UPnPManager GetInstance];
  247. [[managerInstance SSDP] searchSSDP];
  248. [[managerInstance SSDP] searchForMediaServer];
  249. [[managerInstance SSDP] performSelectorInBackground:@selector(SSDPDBUpdate) withObject:nil];
  250. }
  251. - (void)_stopUPNPDiscovery
  252. {
  253. if (_udnpDiscoveryRunning) {
  254. UPnPManager *managerInstance = [UPnPManager GetInstance];
  255. [[managerInstance SSDP] notifySSDPByeBye];
  256. [_searchTimer invalidate];
  257. _searchTimer = nil;
  258. [[managerInstance DB] removeObserver:self];
  259. [[managerInstance SSDP] stopSSDP];
  260. _udnpDiscoveryRunning = NO;
  261. }
  262. }
  263. //protocol UPnPDBObserver
  264. - (void)UPnPDBWillUpdate:(UPnPDB*)sender
  265. {
  266. }
  267. - (void)UPnPDBUpdated:(UPnPDB*)sender
  268. {
  269. NSUInteger count = _UPNPdevices.count;
  270. BasicUPnPDevice *device;
  271. NSMutableArray<VLCLocalNetworkServiceUPnP*> *mutArray = [[NSMutableArray alloc] init];
  272. for (NSUInteger x = 0; x < count; x++) {
  273. device = _UPNPdevices[x];
  274. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"])
  275. [mutArray addObject:[[VLCLocalNetworkServiceUPnP alloc] initWithUPnPDevice:device]];
  276. else
  277. APLog(@"found device '%@' with unsupported urn '%@'", [device friendlyName], [device urn]);
  278. }
  279. _filteredUPNPDevices = nil;
  280. _filteredUPNPDevices = [NSArray arrayWithArray:mutArray];
  281. if (self.delegate) {
  282. if ([self.delegate respondsToSelector:@selector(discoveryFoundSomethingNew)]) {
  283. [self.delegate performSelectorOnMainThread:@selector(discoveryFoundSomethingNew) withObject:nil waitUntilDone:NO];
  284. }
  285. }
  286. }
  287. #pragma mark SAP discovery
  288. - (void)_startSAPDiscovery
  289. {
  290. if (_reachability.currentReachabilityStatus != ReachableViaWiFi)
  291. return;
  292. if (!_sapDiscoverer)
  293. _sapDiscoverer = [[VLCMediaDiscoverer alloc] initWithName:@"sap"];
  294. [_sapDiscoverer startDiscoverer];
  295. _sapDiscoverer.discoveredMedia.delegate = self;
  296. }
  297. - (void)_stopSAPDiscovery
  298. {
  299. [_sapDiscoverer stopDiscoverer];
  300. _sapDiscoverer = nil;
  301. }
  302. - (void)mediaList:(VLCMediaList *)aMediaList mediaAdded:(VLCMedia *)media atIndex:(NSInteger)index
  303. {
  304. [media parseWithOptions:VLCMediaParseNetwork];
  305. if (self.delegate) {
  306. if ([self.delegate respondsToSelector:@selector(discoveryFoundSomethingNew)]) {
  307. [self.delegate performSelectorOnMainThread:@selector(discoveryFoundSomethingNew) withObject:nil waitUntilDone:NO];
  308. }
  309. }
  310. }
  311. - (void)mediaList:(VLCMediaList *)aMediaList mediaRemovedAtIndex:(NSInteger)index
  312. {
  313. if (self.delegate) {
  314. if ([self.delegate respondsToSelector:@selector(discoveryFoundSomethingNew)]) {
  315. [self.delegate performSelectorOnMainThread:@selector(discoveryFoundSomethingNew) withObject:nil waitUntilDone:NO];
  316. }
  317. }
  318. }
  319. #pragma DSM discovery
  320. - (void)_startDSMDiscovery
  321. {
  322. if (_reachability.currentReachabilityStatus != ReachableViaWiFi)
  323. return;
  324. if (_dsmDiscoverer)
  325. return;
  326. _dsmDiscoverer = [[VLCMediaDiscoverer alloc] initWithName:@"dsm"];
  327. [_dsmDiscoverer startDiscoverer];
  328. _dsmDiscoverer.discoveredMedia.delegate = self;
  329. }
  330. - (void)_stopDSMDiscovery
  331. {
  332. [_dsmDiscoverer stopDiscoverer];
  333. _dsmDiscoverer = nil;
  334. }
  335. @end