VLCMediaDiscoverer.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*****************************************************************************
  2. * VLCMediaDiscoverer.m: VLCKit.framework VLCMediaDiscoverer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007 Pierre d'Herbemont
  5. * Copyright (C) 2014-2015 Felix Paul Kühne
  6. * Copyright (C) 2007, 2015 VLC authors and VideoLAN
  7. * $Id$
  8. *
  9. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  10. * Felix Paul Kühne <fkuehne # videolan dot org>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU Lesser General Public License as published by
  14. * the Free Software Foundation; either version 2.1 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public License
  23. * along with this program; if not, write to the Free Software Foundation,
  24. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  25. *****************************************************************************/
  26. #import "VLCMediaDiscoverer.h"
  27. #import "VLCLibrary.h"
  28. #import "VLCLibVLCBridging.h"
  29. #import "VLCEventManager.h"
  30. #include <vlc/libvlc.h>
  31. #include <vlc/libvlc_media_discoverer.h>
  32. @interface VLCMediaDiscoverer ()
  33. {
  34. NSString *_localizedName;
  35. VLCMediaList *_discoveredMedia;
  36. void *_mdis;
  37. BOOL _running;
  38. VLCLibrary *_privateLibrary;
  39. }
  40. /**
  41. * libvlc told us that the discoverer is actually running
  42. */
  43. - (void)_mediaDiscovererStarted;
  44. /**
  45. * libvlc told us that the discoverer stopped running
  46. */
  47. - (void)_mediaDiscovererEnded;
  48. @end
  49. static NSArray *availableMediaDiscoverer = nil; // Global list of media discoverers
  50. /* libvlc event callback */
  51. static void HandleMediaDiscovererStarted(const libvlc_event_t *event, void *self)
  52. {
  53. @autoreleasepool {
  54. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  55. withMethod:@selector(_mediaDiscovererStarted)
  56. withArgumentAsObject:@(event->type)];
  57. }
  58. }
  59. static void HandleMediaDiscovererEnded(const libvlc_event_t *event, void *self)
  60. {
  61. @autoreleasepool {
  62. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  63. withMethod:@selector(_mediaDiscovererEnded)
  64. withArgumentAsObject:@(event->type)];
  65. }
  66. }
  67. @implementation VLCMediaDiscoverer
  68. @synthesize libraryInstance = _privateLibrary;
  69. + (NSArray *)availableMediaDiscoverer
  70. {
  71. if (!availableMediaDiscoverer) {
  72. availableMediaDiscoverer = @[[[VLCMediaDiscoverer alloc] initWithName:@"sap"],
  73. [[VLCMediaDiscoverer alloc] initWithName:@"upnp"],
  74. [[VLCMediaDiscoverer alloc] initWithName:@"freebox"],
  75. [[VLCMediaDiscoverer alloc] initWithName:@"video_dir"]];
  76. }
  77. return availableMediaDiscoverer;
  78. }
  79. - (instancetype)initWithName:(NSString *)aServiceName
  80. {
  81. if (self = [super init]) {
  82. _localizedName = nil;
  83. _discoveredMedia = nil;
  84. _privateLibrary = [VLCLibrary sharedLibrary];
  85. libvlc_retain([_privateLibrary instance]);
  86. _mdis = libvlc_media_discoverer_new([_privateLibrary instance],
  87. [aServiceName UTF8String]);
  88. if (_mdis == NULL) {
  89. VKLog(@"media discovery initialization failed, maybe no such module?");
  90. return NULL;
  91. }
  92. libvlc_event_manager_t *em = libvlc_media_discoverer_event_manager(_mdis);
  93. if (em) {
  94. libvlc_event_attach(em, libvlc_MediaDiscovererStarted, HandleMediaDiscovererStarted, (__bridge void *)(self));
  95. libvlc_event_attach(em, libvlc_MediaDiscovererEnded, HandleMediaDiscovererEnded, (__bridge void *)(self));
  96. }
  97. }
  98. return self;
  99. }
  100. - (void)dealloc
  101. {
  102. if (_running)
  103. [self stopDiscoverer];
  104. [[VLCEventManager sharedManager] cancelCallToObject:self];
  105. libvlc_event_manager_t *em = libvlc_media_discoverer_event_manager(_mdis);
  106. if (em) {
  107. libvlc_event_detach(em, libvlc_MediaDiscovererStarted, HandleMediaDiscovererStarted, (__bridge void *)(self));
  108. libvlc_event_detach(em, libvlc_MediaDiscovererEnded, HandleMediaDiscovererEnded, (__bridge void *)(self));
  109. }
  110. libvlc_media_discoverer_release(_mdis);
  111. libvlc_release(_privateLibrary.instance);
  112. }
  113. - (int)startDiscoverer
  114. {
  115. int returnValue = libvlc_media_discoverer_start(_mdis);
  116. if (returnValue == -1)
  117. VKLog(@"media discovery start failed");
  118. _running = libvlc_media_discoverer_is_running(_mdis);
  119. libvlc_media_list_t *p_mlist = libvlc_media_discoverer_media_list(_mdis);
  120. VLCMediaList *ret = [VLCMediaList mediaListWithLibVLCMediaList:p_mlist];
  121. libvlc_media_list_release(p_mlist);
  122. _discoveredMedia = ret;
  123. return returnValue;
  124. }
  125. - (void)stopDiscoverer
  126. {
  127. if (!_mdis) {
  128. _running = NO;
  129. return;
  130. }
  131. libvlc_media_discoverer_stop(_mdis);
  132. _running = libvlc_media_discoverer_is_running(_mdis);
  133. }
  134. - (VLCMediaList *)discoveredMedia
  135. {
  136. return _discoveredMedia;
  137. }
  138. - (NSString *)localizedName
  139. {
  140. if (_localizedName)
  141. return _localizedName;
  142. char *name = libvlc_media_discoverer_localized_name(_mdis);
  143. if (name) {
  144. _localizedName = @(name);
  145. free(name);
  146. }
  147. return _localizedName;
  148. }
  149. - (BOOL)isRunning
  150. {
  151. return _running;
  152. }
  153. - (void)_mediaDiscovererStarted
  154. {
  155. [self willChangeValueForKey:@"running"];
  156. _running = YES;
  157. [self didChangeValueForKey:@"running"];
  158. if (self.delegate) {
  159. if ([self.delegate respondsToSelector:@selector(discovererStarted:)])
  160. [self.delegate discovererStarted:self];
  161. }
  162. }
  163. - (void)_mediaDiscovererEnded
  164. {
  165. [self willChangeValueForKey:@"running"];
  166. _running = NO;
  167. [self didChangeValueForKey:@"running"];
  168. if (self.delegate) {
  169. if ([self.delegate respondsToSelector:@selector(discovererStopped:)])
  170. [self.delegate discovererStopped:self];
  171. }
  172. }
  173. @end