1 /*****************************************************************************
2 * VLCMediaDiscoverer.m: VLCKit.framework VLCMediaDiscoverer implementation
3 *****************************************************************************
4 * Copyright (C) 2007 Pierre d'Herbemont
5 * Copyright (C) 2007 the VideoLAN team
8 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
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.
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.
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 *****************************************************************************/
25 #import <Cocoa/Cocoa.h>
26 #import "VLCMediaDiscoverer.h"
27 #import "VLCLibrary.h"
28 #import "VLCLibVLCBridging.h"
29 #import "VLCEventManager.h"
31 #include <vlc/libvlc.h>
33 static NSMutableArray * availableMediaDiscoverer = nil; // Global list of media discoverers
36 * Declares call back functions to be used with libvlc event callbacks.
38 @interface VLCMediaDiscoverer (Private)
42 - (void)mediaDiscovererStarted;
47 - (void)mediaDiscovererEnded;
50 /* libvlc event callback */
51 static void HandleMediaDiscovererStarted(const libvlc_event_t * event, void * user_data)
53 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
55 [[VLCEventManager sharedManager] callOnMainThreadObject:self
56 withMethod:@selector(mediaDiscovererStarted)
57 withArgumentAsObject:nil];
61 static void HandleMediaDiscovererEnded( const libvlc_event_t * event, void * user_data)
63 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
65 [[VLCEventManager sharedManager] callOnMainThreadObject:self
66 withMethod:@selector(mediaDiscovererEnded)
67 withArgumentAsObject:nil];
72 @implementation VLCMediaDiscoverer
73 + (NSArray *)availableMediaDiscoverer
75 if( !availableMediaDiscoverer )
77 availableMediaDiscoverer = [[NSArray arrayWithObjects:
78 [[[VLCMediaDiscoverer alloc] initWithName:@"sap"] autorelease],
79 [[[VLCMediaDiscoverer alloc] initWithName:@"freebox"] autorelease],
80 [[[VLCMediaDiscoverer alloc] initWithName:@"shoutcast"] autorelease],
81 [[[VLCMediaDiscoverer alloc] initWithName:@"shoutcasttv"] autorelease], nil] retain];
83 return availableMediaDiscoverer;
86 - (id)initWithName:(NSString *)aServiceName
88 if (self = [super init])
90 libvlc_exception_t ex;
91 libvlc_exception_init( &ex );
93 discoveredMedia = nil;
94 mdis = libvlc_media_discoverer_new_from_name( [VLCLibrary sharedInstance],
95 [aServiceName UTF8String],
97 catch_exception( &ex );
99 libvlc_event_manager_t * p_em = libvlc_media_discoverer_event_manager(mdis);
100 libvlc_event_attach(p_em, libvlc_MediaDiscovererStarted, HandleMediaDiscovererStarted, self, NULL);
101 libvlc_event_attach(p_em, libvlc_MediaDiscovererEnded, HandleMediaDiscovererEnded, self, NULL);
102 running = libvlc_media_discoverer_is_running(mdis);
111 if([self retainCount] <= 1)
113 /* We must make sure we won't receive new event after an upcoming dealloc
114 * We also may receive a -retain in some event callback that may occcur
115 * Before libvlc_event_detach. So this can't happen in dealloc */
116 libvlc_event_manager_t * p_em = libvlc_media_list_event_manager(mdis, NULL);
117 libvlc_event_detach(p_em, libvlc_MediaDiscovererStarted, HandleMediaDiscovererStarted, self, NULL);
118 libvlc_event_detach(p_em, libvlc_MediaDiscovererEnded, HandleMediaDiscovererEnded, self, NULL);
126 [localizedName release];
127 [discoveredMedia release];
128 libvlc_media_discoverer_release( mdis );
132 - (VLCMediaList *) discoveredMedia
134 if( discoveredMedia )
135 return discoveredMedia;
137 libvlc_media_list_t * p_mlist = libvlc_media_discoverer_media_list( mdis );
138 VLCMediaList * ret = [VLCMediaList mediaListWithLibVLCMediaList:p_mlist];
139 libvlc_media_list_release( p_mlist );
141 discoveredMedia = [ret retain];
142 return discoveredMedia;
145 - (NSString *)localizedName
148 return localizedName;
150 char * name = libvlc_media_discoverer_localized_name( mdis );
153 localizedName = [[NSString stringWithUTF8String:name] retain];
156 return localizedName;
165 @implementation VLCMediaDiscoverer (Private)
166 - (void)mediaDiscovererStarted
168 [self willChangeValueForKey:@"running"];
170 [self didChangeValueForKey:@"running"];
173 - (void)mediaDiscovererEnded
175 [self willChangeValueForKey:@"running"];
177 [self didChangeValueForKey:@"running"];