VLCMediaDiscoverer.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*****************************************************************************
  2. * VLCMediaDiscoverer.m: VLCKit.framework VLCMediaDiscoverer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007 Pierre d'Herbemont
  5. * Copyright (C) 2007 the VideoLAN team
  6. * $Id$
  7. *
  8. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  23. *****************************************************************************/
  24. #import <Cocoa/Cocoa.h>
  25. #import "VLCMediaDiscoverer.h"
  26. #import "VLCLibrary.h"
  27. #import "VLCLibVLCBridging.h"
  28. #import "VLCEventManager.h"
  29. #include <vlc/libvlc.h>
  30. static NSMutableArray * availableMediaDiscoverer = nil; // Global list of media discoverers
  31. /**
  32. * Declares call back functions to be used with libvlc event callbacks.
  33. */
  34. @interface VLCMediaDiscoverer (Private)
  35. /**
  36. * TODO: Documention
  37. */
  38. - (void)mediaDiscovererStarted;
  39. /**
  40. * TODO: Documention
  41. */
  42. - (void)mediaDiscovererEnded;
  43. @end
  44. /* libvlc event callback */
  45. static void HandleMediaDiscovererStarted(const libvlc_event_t * event, void * user_data)
  46. {
  47. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  48. id self = user_data;
  49. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  50. withMethod:@selector(mediaDiscovererStarted)
  51. withArgumentAsObject:nil];
  52. [pool release];
  53. }
  54. static void HandleMediaDiscovererEnded( const libvlc_event_t * event, void * user_data)
  55. {
  56. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  57. id self = user_data;
  58. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  59. withMethod:@selector(mediaDiscovererEnded)
  60. withArgumentAsObject:nil];
  61. [pool release];
  62. }
  63. @implementation VLCMediaDiscoverer
  64. + (NSArray *)availableMediaDiscoverer
  65. {
  66. if( !availableMediaDiscoverer )
  67. {
  68. availableMediaDiscoverer = [[NSArray arrayWithObjects:
  69. [[[VLCMediaDiscoverer alloc] initWithName:@"sap"] autorelease],
  70. [[[VLCMediaDiscoverer alloc] initWithName:@"upnp_intel"] autorelease],
  71. [[[VLCMediaDiscoverer alloc] initWithName:@"freebox"] autorelease],
  72. [[[VLCMediaDiscoverer alloc] initWithName:@"video_dir"] autorelease],
  73. [[[VLCMediaDiscoverer alloc] initWithName:@"shoutcast"] autorelease],
  74. [[[VLCMediaDiscoverer alloc] initWithName:@"shoutcasttv"] autorelease], nil] retain];
  75. }
  76. return availableMediaDiscoverer;
  77. }
  78. - (id)initWithName:(NSString *)aServiceName
  79. {
  80. if (self = [super init])
  81. {
  82. libvlc_exception_t ex;
  83. libvlc_exception_init(&ex);
  84. localizedName = nil;
  85. discoveredMedia = nil;
  86. mdis = libvlc_media_discoverer_new_from_name([VLCLibrary sharedInstance],
  87. [aServiceName UTF8String],
  88. &ex);
  89. catch_exception(&ex);
  90. libvlc_event_manager_t * p_em = libvlc_media_discoverer_event_manager(mdis);
  91. libvlc_event_attach(p_em, libvlc_MediaDiscovererStarted, HandleMediaDiscovererStarted, self);
  92. libvlc_event_attach(p_em, libvlc_MediaDiscovererEnded, HandleMediaDiscovererEnded, self);
  93. running = libvlc_media_discoverer_is_running(mdis);
  94. }
  95. return self;
  96. }
  97. - (void)release
  98. {
  99. @synchronized(self)
  100. {
  101. if([self retainCount] <= 1)
  102. {
  103. /* We must make sure we won't receive new event after an upcoming dealloc
  104. * We also may receive a -retain in some event callback that may occcur
  105. * Before libvlc_event_detach. So this can't happen in dealloc */
  106. libvlc_event_manager_t * p_em = libvlc_media_list_event_manager(mdis);
  107. libvlc_event_detach(p_em, libvlc_MediaDiscovererStarted, HandleMediaDiscovererStarted, self);
  108. libvlc_event_detach(p_em, libvlc_MediaDiscovererEnded, HandleMediaDiscovererEnded, self);
  109. }
  110. [super release];
  111. }
  112. }
  113. - (void)dealloc
  114. {
  115. [localizedName release];
  116. [discoveredMedia release];
  117. libvlc_media_discoverer_release( mdis );
  118. [super dealloc];
  119. }
  120. - (VLCMediaList *) discoveredMedia
  121. {
  122. if( discoveredMedia )
  123. return discoveredMedia;
  124. libvlc_media_list_t * p_mlist = libvlc_media_discoverer_media_list( mdis );
  125. VLCMediaList * ret = [VLCMediaList mediaListWithLibVLCMediaList:p_mlist];
  126. libvlc_media_list_release( p_mlist );
  127. discoveredMedia = [ret retain];
  128. return discoveredMedia;
  129. }
  130. - (NSString *)localizedName
  131. {
  132. if ( localizedName )
  133. return localizedName;
  134. char * name = libvlc_media_discoverer_localized_name( mdis );
  135. if (name)
  136. {
  137. localizedName = [[NSString stringWithUTF8String:name] retain];
  138. free( name );
  139. }
  140. return localizedName;
  141. }
  142. - (BOOL)isRunning
  143. {
  144. return running;
  145. }
  146. @end
  147. @implementation VLCMediaDiscoverer (Private)
  148. - (void)mediaDiscovererStarted
  149. {
  150. [self willChangeValueForKey:@"running"];
  151. running = YES;
  152. [self didChangeValueForKey:@"running"];
  153. }
  154. - (void)mediaDiscovererEnded
  155. {
  156. [self willChangeValueForKey:@"running"];
  157. running = NO;
  158. [self didChangeValueForKey:@"running"];
  159. }
  160. @end