VLCMediaDiscoverer.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*****************************************************************************
  2. * VLCMediaDiscoverer.m: VLC.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 NSArray * availableMediaDiscoverer = nil;
  31. @interface VLCMediaDiscoverer (Private)
  32. - (void)mediaDiscovererStarted;
  33. - (void)mediaDiscovererEnded;
  34. @end
  35. /* libvlc event callback */
  36. static void HandleMediaDiscovererStarted(const libvlc_event_t *event, void *user_data)
  37. {
  38. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  39. id self = user_data;
  40. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  41. withMethod:@selector(mediaDiscovererStarted)
  42. withArgumentAsObject:nil];
  43. [pool release];
  44. }
  45. static void HandleMediaDiscovererEnded( 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(mediaDiscovererEnded)
  51. withArgumentAsObject:nil];
  52. [pool release];
  53. }
  54. @implementation VLCMediaDiscoverer
  55. + (NSArray *)availableMediaDiscoverer
  56. {
  57. if( !availableMediaDiscoverer )
  58. {
  59. availableMediaDiscoverer = [[NSArray arrayWithObjects:
  60. [[[VLCMediaDiscoverer alloc] initWithName:@"sap"] autorelease],
  61. [[[VLCMediaDiscoverer alloc] initWithName:@"shoutcast"] autorelease],
  62. [[[VLCMediaDiscoverer alloc] initWithName:@"shoutcasttv"] autorelease], nil] retain];
  63. }
  64. return availableMediaDiscoverer;
  65. }
  66. - (id)initWithName:(NSString *)aServiceName
  67. {
  68. if (self = [super init])
  69. {
  70. libvlc_exception_t ex;
  71. libvlc_exception_init( &ex );
  72. localizedName = nil;
  73. discoveredMedia = nil;
  74. mdis = libvlc_media_discoverer_new_from_name( [VLCLibrary sharedInstance],
  75. [aServiceName UTF8String],
  76. &ex );
  77. libvlc_event_manager_t * p_em = libvlc_media_discoverer_event_manager(mdis);
  78. libvlc_event_attach(p_em, libvlc_MediaDiscovererStarted, HandleMediaDiscovererStarted, self, NULL);
  79. libvlc_event_attach(p_em, libvlc_MediaDiscovererEnded, HandleMediaDiscovererEnded, self, NULL);
  80. running = libvlc_media_discoverer_is_running(mdis);
  81. quit_on_exception( &ex );
  82. }
  83. return self;
  84. }
  85. - (void)release
  86. {
  87. @synchronized(self)
  88. {
  89. if([self retainCount] <= 1)
  90. {
  91. /* We must make sure we won't receive new event after an upcoming dealloc
  92. * We also may receive a -retain in some event callback that may occcur
  93. * Before libvlc_event_detach. So this can't happen in dealloc */
  94. libvlc_event_manager_t * p_em = libvlc_media_list_event_manager(mdis, NULL);
  95. libvlc_event_detach(p_em, libvlc_MediaDiscovererStarted, HandleMediaDiscovererStarted, self, NULL);
  96. libvlc_event_detach(p_em, libvlc_MediaDiscovererEnded, HandleMediaDiscovererEnded, self, NULL);
  97. }
  98. [super release];
  99. }
  100. }
  101. - (void)dealloc
  102. {
  103. if( localizedName )
  104. [localizedName release];
  105. if( discoveredMedia )
  106. [discoveredMedia release];
  107. libvlc_media_discoverer_release( mdis );
  108. [super dealloc];
  109. }
  110. - (VLCMediaList *) discoveredMedia
  111. {
  112. if( discoveredMedia )
  113. return discoveredMedia;
  114. libvlc_media_list_t * p_mlist = libvlc_media_discoverer_media_list( mdis );
  115. VLCMediaList * ret = [VLCMediaList mediaListWithLibVLCMediaList: p_mlist];
  116. libvlc_media_list_release( p_mlist );
  117. if( ret )
  118. {
  119. discoveredMedia = [ret retain];
  120. }
  121. return discoveredMedia;
  122. }
  123. - (NSString *)localizedName
  124. {
  125. NSString * aString = nil;
  126. char * name = libvlc_media_discoverer_localized_name( mdis );
  127. if( localizedName )
  128. return localizedName;
  129. if (name)
  130. {
  131. aString = [NSString stringWithUTF8String:name];
  132. free( name );
  133. }
  134. if( aString )
  135. {
  136. localizedName = [aString retain];
  137. }
  138. return localizedName;
  139. }
  140. - (BOOL)isRunning
  141. {
  142. return running;
  143. }
  144. @end
  145. @implementation VLCMediaDiscoverer (Private)
  146. - (void)mediaDiscovererStarted
  147. {
  148. [self willChangeValueForKey:@"running"];
  149. running = YES;
  150. [self didChangeValueForKey:@"running"];
  151. }
  152. - (void)mediaDiscovererEnded
  153. {
  154. [self willChangeValueForKey:@"running"];
  155. running = NO;
  156. [self didChangeValueForKey:@"running"];
  157. }
  158. @end