VLCMediaDiscoverer.m 5.9 KB

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