VLCMediaDiscoverer.m 5.8 KB

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