VLCLocalNetworkServiceBrowserUPnP.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. if (index < _filteredUPNPDevices.count)
  43. return _filteredUPNPDevices[index];
  44. return nil;
  45. }
  46. - (void)startDiscovery {
  47. [self _startUPNPDiscovery];
  48. }
  49. - (void)stopDiscovery {
  50. [self _stopUPNPDiscovery];
  51. }
  52. #pragma mark -
  53. #pragma mark - UPNP discovery
  54. - (void)_startUPNPDiscovery
  55. {
  56. UPnPManager *managerInstance = [UPnPManager GetInstance];
  57. _UPNPdevices = [[managerInstance DB] rootDevices];
  58. if (_UPNPdevices.count > 0)
  59. [self UPnPDBUpdated:nil];
  60. [[managerInstance DB] addObserver:self];
  61. //Optional; set User Agent
  62. if (!_setup) {
  63. [[managerInstance SSDP] setUserAgentProduct:[NSString stringWithFormat:@"VLCforiOS/%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]] andOS:[NSString stringWithFormat:@"iOS/%@", [[UIDevice currentDevice] systemVersion]]];
  64. _setup = YES;
  65. }
  66. //Search for UPnP Devices
  67. [[managerInstance SSDP] startSSDP];
  68. [[managerInstance SSDP] notifySSDPAlive];
  69. _searchTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:1.0] interval:10.0 target:self selector:@selector(_performSSDPSearch) userInfo:nil repeats:YES];
  70. [[NSRunLoop mainRunLoop] addTimer:_searchTimer forMode:NSRunLoopCommonModes];
  71. _udnpDiscoveryRunning = YES;
  72. }
  73. - (void)_performSSDPSearch
  74. {
  75. UPnPManager *managerInstance = [UPnPManager GetInstance];
  76. [[managerInstance SSDP] searchSSDP];
  77. [[managerInstance SSDP] searchForMediaServer];
  78. [[managerInstance SSDP] performSelectorInBackground:@selector(SSDPDBUpdate) withObject:nil];
  79. }
  80. - (void)_stopUPNPDiscovery
  81. {
  82. if (_udnpDiscoveryRunning) {
  83. UPnPManager *managerInstance = [UPnPManager GetInstance];
  84. [[managerInstance SSDP] notifySSDPByeBye];
  85. [_searchTimer invalidate];
  86. _searchTimer = nil;
  87. [[managerInstance DB] removeObserver:self];
  88. [[managerInstance SSDP] stopSSDP];
  89. _udnpDiscoveryRunning = NO;
  90. }
  91. }
  92. #pragma mark - UPnPDBObserver protocol
  93. - (void)UPnPDBWillUpdate:(UPnPDB*)sender
  94. {
  95. }
  96. - (void)UPnPDBUpdated:(UPnPDB*)sender
  97. {
  98. NSUInteger count = _UPNPdevices.count;
  99. BasicUPnPDevice *device;
  100. NSMutableArray<VLCLocalNetworkServiceUPnP*> *mutArray = [[NSMutableArray alloc] init];
  101. for (NSUInteger x = 0; x < count; x++) {
  102. device = _UPNPdevices[x];
  103. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"])
  104. [mutArray addObject:[[VLCLocalNetworkServiceUPnP alloc] initWithUPnPDevice:device serviceName:self.name]];
  105. else
  106. APLog(@"found device '%@' with unsupported urn '%@'", [device friendlyName], [device urn]);
  107. }
  108. _filteredUPNPDevices = nil;
  109. _filteredUPNPDevices = [NSArray arrayWithArray:mutArray];
  110. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  111. [self.delegate localNetworkServiceBrowserDidUpdateServices:self];
  112. }];
  113. }
  114. @end