VLCMediaDiscoverer.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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:@"shoutcast"] autorelease],
  71. [[[VLCMediaDiscoverer alloc] initWithName:@"shoutcasttv"] autorelease], nil] retain];
  72. }
  73. return availableMediaDiscoverer;
  74. }
  75. - (id)initWithName:(NSString *)aServiceName
  76. {
  77. if (self = [super init])
  78. {
  79. libvlc_exception_t ex;
  80. libvlc_exception_init( &ex );
  81. localizedName = nil;
  82. discoveredMedia = nil;
  83. mdis = libvlc_media_discoverer_new_from_name( [VLCLibrary sharedInstance],
  84. [aServiceName UTF8String],
  85. &ex );
  86. catch_exception( &ex );
  87. libvlc_event_manager_t * p_em = libvlc_media_discoverer_event_manager(mdis);
  88. libvlc_event_attach(p_em, libvlc_MediaDiscovererStarted, HandleMediaDiscovererStarted, self, NULL);
  89. libvlc_event_attach(p_em, libvlc_MediaDiscovererEnded, HandleMediaDiscovererEnded, self, NULL);
  90. running = libvlc_media_discoverer_is_running(mdis);
  91. }
  92. return self;
  93. }
  94. - (void)release
  95. {
  96. @synchronized(self)
  97. {
  98. if([self retainCount] <= 1)
  99. {
  100. /* We must make sure we won't receive new event after an upcoming dealloc
  101. * We also may receive a -retain in some event callback that may occcur
  102. * Before libvlc_event_detach. So this can't happen in dealloc */
  103. libvlc_event_manager_t * p_em = libvlc_media_list_event_manager(mdis, NULL);
  104. libvlc_event_detach(p_em, libvlc_MediaDiscovererStarted, HandleMediaDiscovererStarted, self, NULL);
  105. libvlc_event_detach(p_em, libvlc_MediaDiscovererEnded, HandleMediaDiscovererEnded, self, NULL);
  106. }
  107. [super release];
  108. }
  109. }
  110. - (void)dealloc
  111. {
  112. [localizedName release];
  113. [discoveredMedia release];
  114. libvlc_media_discoverer_release( mdis );
  115. [super dealloc];
  116. }
  117. - (VLCMediaList *) discoveredMedia
  118. {
  119. if( discoveredMedia )
  120. return discoveredMedia;
  121. libvlc_media_list_t * p_mlist = libvlc_media_discoverer_media_list( mdis );
  122. VLCMediaList * ret = [VLCMediaList mediaListWithLibVLCMediaList:p_mlist];
  123. libvlc_media_list_release( p_mlist );
  124. discoveredMedia = [ret retain];
  125. return discoveredMedia;
  126. }
  127. - (NSString *)localizedName
  128. {
  129. if ( localizedName )
  130. return localizedName;
  131. char * name = libvlc_media_discoverer_localized_name( mdis );
  132. if (name)
  133. {
  134. localizedName = [[NSString stringWithUTF8String:name] retain];
  135. free( name );
  136. }
  137. return localizedName;
  138. }
  139. - (BOOL)isRunning
  140. {
  141. return running;
  142. }
  143. @end
  144. @implementation VLCMediaDiscoverer (Private)
  145. - (void)mediaDiscovererStarted
  146. {
  147. [self willChangeValueForKey:@"running"];
  148. running = YES;
  149. [self didChangeValueForKey:@"running"];
  150. }
  151. - (void)mediaDiscovererEnded
  152. {
  153. [self willChangeValueForKey:@"running"];
  154. running = NO;
  155. [self didChangeValueForKey:@"running"];
  156. }
  157. @end