VLCMediaDiscoverer.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. @interface VLCMediaDiscoverer ()
  32. {
  33. NSString *_localizedName;
  34. VLCMediaList *_discoveredMedia;
  35. void *_mdis;
  36. BOOL _running;
  37. VLCLibrary *_privateLibrary;
  38. }
  39. /**
  40. * libvlc told us that the discoverer is actually running
  41. */
  42. - (void)_mediaDiscovererStarted;
  43. /**
  44. * libvlc told us that the discoverer stopped running
  45. */
  46. - (void)_mediaDiscovererEnded;
  47. @end
  48. static NSArray *availableMediaDiscoverer = nil; // Global list of media discoverers
  49. /* libvlc event callback */
  50. static void HandleMediaDiscovererStarted(const libvlc_event_t *event, void *user_data)
  51. {
  52. @autoreleasepool {
  53. NSLog(@"HandleMediaDiscovererStarted");
  54. id self = (__bridge id)(user_data);
  55. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  56. withMethod:@selector(_mediaDiscovererStarted)
  57. withArgumentAsObject:nil];
  58. }
  59. }
  60. static void HandleMediaDiscovererEnded( const libvlc_event_t *event, void *user_data)
  61. {
  62. @autoreleasepool {
  63. NSLog(@"HandleMediaDiscovererEnded");
  64. id self = (__bridge id)(user_data);
  65. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  66. withMethod:@selector(_mediaDiscovererEnded)
  67. withArgumentAsObject:nil];
  68. }
  69. }
  70. @implementation VLCMediaDiscoverer
  71. + (NSArray *)availableMediaDiscoverer
  72. {
  73. if (!availableMediaDiscoverer) {
  74. availableMediaDiscoverer = @[[[VLCMediaDiscoverer alloc] initWithName:@"sap"],
  75. [[VLCMediaDiscoverer alloc] initWithName:@"upnp"],
  76. [[VLCMediaDiscoverer alloc] initWithName:@"freebox"],
  77. [[VLCMediaDiscoverer alloc] initWithName:@"video_dir"]];
  78. }
  79. return availableMediaDiscoverer;
  80. }
  81. - (instancetype)initWithName:(NSString *)aServiceName
  82. {
  83. if (self = [super init]) {
  84. _localizedName = nil;
  85. _discoveredMedia = nil;
  86. _privateLibrary = [VLCLibrary sharedLibrary];
  87. libvlc_retain(_privateLibrary.instance);
  88. _mdis = libvlc_media_discoverer_new(_privateLibrary.instance,
  89. [aServiceName UTF8String]);
  90. if (_mdis == NULL) {
  91. VKLog(@"media discovery initialization failed, maybe no such module?");
  92. return NULL;
  93. }
  94. libvlc_event_manager_t * p_em = libvlc_media_discoverer_event_manager(_mdis);
  95. libvlc_event_attach(p_em, libvlc_MediaDiscovererStarted, HandleMediaDiscovererStarted, (__bridge void *)(self));
  96. libvlc_event_attach(p_em, libvlc_MediaDiscovererEnded, HandleMediaDiscovererEnded, (__bridge void *)(self));
  97. //FIXME: add more events
  98. int returnValue = libvlc_media_discoverer_start(_mdis);
  99. if (returnValue == -1) {
  100. VKLog(@"media discovery start failed");
  101. return NULL;
  102. }
  103. _running = libvlc_media_discoverer_is_running(_mdis);
  104. }
  105. return self;
  106. }
  107. - (void)dealloc
  108. {
  109. libvlc_event_manager_t *em = libvlc_media_list_event_manager(_mdis);
  110. libvlc_event_detach(em, libvlc_MediaDiscovererStarted, HandleMediaDiscovererStarted, (__bridge void *)(self));
  111. libvlc_event_detach(em, libvlc_MediaDiscovererEnded, HandleMediaDiscovererEnded, (__bridge void *)(self));
  112. [[VLCEventManager sharedManager] cancelCallToObject:self];
  113. libvlc_media_discoverer_release(_mdis);
  114. libvlc_release(_privateLibrary.instance);
  115. }
  116. - (VLCMediaList *)discoveredMedia
  117. {
  118. if (_discoveredMedia)
  119. return _discoveredMedia;
  120. libvlc_media_list_t *p_mlist = libvlc_media_discoverer_media_list( _mdis );
  121. VLCMediaList *ret = [VLCMediaList mediaListWithLibVLCMediaList:p_mlist];
  122. libvlc_media_list_release( p_mlist );
  123. _discoveredMedia = ret;
  124. return _discoveredMedia;
  125. }
  126. - (NSString *)localizedName
  127. {
  128. if (_localizedName)
  129. return _localizedName;
  130. char *name = libvlc_media_discoverer_localized_name(_mdis);
  131. if (name) {
  132. _localizedName = @(name);
  133. free(name);
  134. }
  135. return _localizedName;
  136. }
  137. - (BOOL)isRunning
  138. {
  139. return _running;
  140. }
  141. - (void)_mediaDiscovererStarted
  142. {
  143. [self willChangeValueForKey:@"running"];
  144. _running = YES;
  145. [self didChangeValueForKey:@"running"];
  146. if (self.delegate) {
  147. if ([self.delegate respondsToSelector:@selector(discovererStarted:)])
  148. [self.delegate discovererStarted:self];
  149. }
  150. }
  151. - (void)_mediaDiscovererEnded
  152. {
  153. [self willChangeValueForKey:@"running"];
  154. _running = NO;
  155. [self didChangeValueForKey:@"running"];
  156. if (self.delegate) {
  157. if ([self.delegate respondsToSelector:@selector(discovererStopped:)])
  158. [self.delegate discovererStopped:self];
  159. }
  160. }
  161. @end