VLCLocalNetworkServiceBrowserUPnP.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*****************************************************************************
  2. * VLCLocalNetworkServiceBrowserUPnP.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 "VLCLocalNetworkServiceBrowserUPnP.h"
  13. #import "VLCLocalNetworkServiceUPnP.h"
  14. #import "UPnPManager.h"
  15. @interface VLCLocalNetworkServiceBrowserUPnP () <UPnPDBObserver>{
  16. BOOL _udnpDiscoveryRunning;
  17. NSTimer *_searchTimer;
  18. BOOL _setup;
  19. }
  20. @property (nonatomic) NSArray<VLCLocalNetworkServiceUPnP*> *filteredUPNPDevices;
  21. @property (nonatomic) NSArray *UPNPdevices;
  22. @end
  23. @implementation VLCLocalNetworkServiceBrowserUPnP
  24. @synthesize name = _name, delegate = _delegate;
  25. - (instancetype)init
  26. {
  27. self = [super init];
  28. if (self) {
  29. #if TARGET_OS_TV
  30. _name = NSLocalizedString(@"UPNP_SHORT", nil);
  31. #else
  32. _name = NSLocalizedString(@"UPNP_LONG", nil);
  33. #endif
  34. }
  35. return self;
  36. }
  37. #pragma mark - VLCLocalNetworkServiceBrowser Protocol
  38. - (NSUInteger)numberOfItems {
  39. return _filteredUPNPDevices.count;
  40. }
  41. - (id<VLCLocalNetworkService>)networkServiceForIndex:(NSUInteger)index {
  42. return _filteredUPNPDevices[index];
  43. }
  44. - (void)startDiscovery {
  45. [self _startUPNPDiscovery];
  46. }
  47. - (void)stopDiscovery {
  48. [self _stopUPNPDiscovery];
  49. }
  50. #pragma mark -
  51. #pragma mark - UPNP discovery
  52. - (void)_startUPNPDiscovery
  53. {
  54. UPnPManager *managerInstance = [UPnPManager GetInstance];
  55. _UPNPdevices = [[managerInstance DB] rootDevices];
  56. if (_UPNPdevices.count > 0)
  57. [self UPnPDBUpdated:nil];
  58. [[managerInstance DB] addObserver:self];
  59. //Optional; set User Agent
  60. if (!_setup) {
  61. [[managerInstance SSDP] setUserAgentProduct:[NSString stringWithFormat:@"VLCforiOS/%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]] andOS:[NSString stringWithFormat:@"iOS/%@", [[UIDevice currentDevice] systemVersion]]];
  62. _setup = YES;
  63. }
  64. //Search for UPnP Devices
  65. [[managerInstance SSDP] startSSDP];
  66. [[managerInstance SSDP] notifySSDPAlive];
  67. _searchTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:1.0] interval:10.0 target:self selector:@selector(_performSSDPSearch) userInfo:nil repeats:YES];
  68. [[NSRunLoop mainRunLoop] addTimer:_searchTimer forMode:NSRunLoopCommonModes];
  69. _udnpDiscoveryRunning = YES;
  70. }
  71. - (void)_performSSDPSearch
  72. {
  73. UPnPManager *managerInstance = [UPnPManager GetInstance];
  74. [[managerInstance SSDP] searchSSDP];
  75. [[managerInstance SSDP] searchForMediaServer];
  76. [[managerInstance SSDP] performSelectorInBackground:@selector(SSDPDBUpdate) withObject:nil];
  77. }
  78. - (void)_stopUPNPDiscovery
  79. {
  80. if (_udnpDiscoveryRunning) {
  81. UPnPManager *managerInstance = [UPnPManager GetInstance];
  82. [[managerInstance SSDP] notifySSDPByeBye];
  83. [_searchTimer invalidate];
  84. _searchTimer = nil;
  85. [[managerInstance DB] removeObserver:self];
  86. [[managerInstance SSDP] stopSSDP];
  87. _udnpDiscoveryRunning = NO;
  88. }
  89. }
  90. #pragma mark - UPnPDBObserver protocol
  91. - (void)UPnPDBWillUpdate:(UPnPDB*)sender
  92. {
  93. }
  94. - (void)UPnPDBUpdated:(UPnPDB*)sender
  95. {
  96. NSUInteger count = _UPNPdevices.count;
  97. BasicUPnPDevice *device;
  98. NSMutableArray<VLCLocalNetworkServiceUPnP*> *mutArray = [[NSMutableArray alloc] init];
  99. for (NSUInteger x = 0; x < count; x++) {
  100. device = _UPNPdevices[x];
  101. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"])
  102. [mutArray addObject:[[VLCLocalNetworkServiceUPnP alloc] initWithUPnPDevice:device]];
  103. else
  104. APLog(@"found device '%@' with unsupported urn '%@'", [device friendlyName], [device urn]);
  105. }
  106. _filteredUPNPDevices = nil;
  107. _filteredUPNPDevices = [NSArray arrayWithArray:mutArray];
  108. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  109. [self.delegate localNetworkServiceBrowserDidUpdateServices:self];
  110. }];
  111. }
  112. @end