VLCMediaDiscoverer.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*****************************************************************************
  2. * VLCMediaDiscoverer.m: VLCKit.framework VLCMediaDiscoverer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007 Pierre d'Herbemont
  5. * Copyright (C) 2007 VLC authors and VideoLAN
  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 it
  11. * under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this program; if not, write to the Free Software Foundation,
  22. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  23. *****************************************************************************/
  24. #import "VLCMediaDiscoverer.h"
  25. #import "VLCLibrary.h"
  26. #import "VLCLibVLCBridging.h"
  27. #import "VLCEventManager.h"
  28. #include <vlc/libvlc.h>
  29. static NSArray * availableMediaDiscoverer = nil; // Global list of media discoverers
  30. /**
  31. * Declares call back functions to be used with libvlc event callbacks.
  32. */
  33. @interface VLCMediaDiscoverer (Private)
  34. /**
  35. * TODO: Documention
  36. */
  37. - (void)mediaDiscovererStarted;
  38. /**
  39. * TODO: Documention
  40. */
  41. - (void)mediaDiscovererEnded;
  42. @end
  43. /* libvlc event callback */
  44. static void HandleMediaDiscovererStarted(const libvlc_event_t * event, void * user_data)
  45. {
  46. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  47. id self = user_data;
  48. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  49. withMethod:@selector(mediaDiscovererStarted)
  50. withArgumentAsObject:nil];
  51. [pool release];
  52. }
  53. static void HandleMediaDiscovererEnded( const libvlc_event_t * event, void * user_data)
  54. {
  55. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  56. id self = user_data;
  57. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  58. withMethod:@selector(mediaDiscovererEnded)
  59. withArgumentAsObject:nil];
  60. [pool release];
  61. }
  62. @implementation VLCMediaDiscoverer
  63. + (NSArray *)availableMediaDiscoverer
  64. {
  65. if (!availableMediaDiscoverer) {
  66. availableMediaDiscoverer = [@[[[[VLCMediaDiscoverer alloc] initWithName:@"sap"] autorelease],
  67. [[[VLCMediaDiscoverer alloc] initWithName:@"upnp"] autorelease],
  68. [[[VLCMediaDiscoverer alloc] initWithName:@"freebox"] autorelease],
  69. [[[VLCMediaDiscoverer alloc] initWithName:@"video_dir"] autorelease]] retain];
  70. }
  71. return availableMediaDiscoverer;
  72. }
  73. - (id)initWithName:(NSString *)aServiceName
  74. {
  75. if (self = [super init]) {
  76. localizedName = nil;
  77. discoveredMedia = nil;
  78. mdis = libvlc_media_discoverer_new_from_name([VLCLibrary sharedInstance],
  79. [aServiceName UTF8String]);
  80. NSAssert(mdis, @"No such media discoverer");
  81. libvlc_event_manager_t * p_em = libvlc_media_discoverer_event_manager(mdis);
  82. libvlc_event_attach(p_em, libvlc_MediaDiscovererStarted, HandleMediaDiscovererStarted, self);
  83. libvlc_event_attach(p_em, libvlc_MediaDiscovererEnded, HandleMediaDiscovererEnded, self);
  84. running = libvlc_media_discoverer_is_running(mdis);
  85. }
  86. return self;
  87. }
  88. - (void)dealloc
  89. {
  90. libvlc_event_manager_t *em = libvlc_media_list_event_manager(mdis);
  91. libvlc_event_detach(em, libvlc_MediaDiscovererStarted, HandleMediaDiscovererStarted, self);
  92. libvlc_event_detach(em, libvlc_MediaDiscovererEnded, HandleMediaDiscovererEnded, self);
  93. [[VLCEventManager sharedManager] cancelCallToObject:self];
  94. [localizedName release];
  95. [discoveredMedia release];
  96. libvlc_media_discoverer_release( mdis );
  97. [super dealloc];
  98. }
  99. - (VLCMediaList *) discoveredMedia
  100. {
  101. if (discoveredMedia)
  102. return discoveredMedia;
  103. libvlc_media_list_t * p_mlist = libvlc_media_discoverer_media_list( mdis );
  104. VLCMediaList * ret = [VLCMediaList mediaListWithLibVLCMediaList:p_mlist];
  105. libvlc_media_list_release( p_mlist );
  106. discoveredMedia = [ret retain];
  107. return discoveredMedia;
  108. }
  109. - (NSString *)localizedName
  110. {
  111. if (localizedName)
  112. return localizedName;
  113. char * name = libvlc_media_discoverer_localized_name( mdis );
  114. if (name) {
  115. localizedName = [@(name) retain];
  116. free( name );
  117. }
  118. return localizedName;
  119. }
  120. - (BOOL)isRunning
  121. {
  122. return running;
  123. }
  124. @end
  125. @implementation VLCMediaDiscoverer (Private)
  126. - (void)mediaDiscovererStarted
  127. {
  128. [self willChangeValueForKey:@"running"];
  129. running = YES;
  130. [self didChangeValueForKey:@"running"];
  131. }
  132. - (void)mediaDiscovererEnded
  133. {
  134. [self willChangeValueForKey:@"running"];
  135. running = NO;
  136. [self didChangeValueForKey:@"running"];
  137. }
  138. @end