]> git.sesse.net Git - vlc/blob - projects/macosx/framework/Sources/VLCMediaDiscoverer.m
00ac08b87523c2c4933c5a9689170cbabe673e6f
[vlc] / projects / macosx / framework / Sources / VLCMediaDiscoverer.m
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
25 #import <Cocoa/Cocoa.h>
26 #import "VLCMediaDiscoverer.h"
27 #import "VLCLibrary.h"
28 #import "VLCLibVLCBridging.h"
29 #import "VLCEventManager.h"
30
31 #include <vlc/libvlc.h>
32
33 static NSMutableArray * availableMediaDiscoverer = nil;     // Global list of media discoverers
34
35 /**
36  * Declares call back functions to be used with libvlc event callbacks.
37  */
38 @interface VLCMediaDiscoverer (Private)
39 /**
40  * TODO: Documention
41  */
42 - (void)mediaDiscovererStarted;
43
44 /**
45  * TODO: Documention
46  */
47 - (void)mediaDiscovererEnded;
48 @end
49
50 /* libvlc event callback */
51 static void HandleMediaDiscovererStarted(const libvlc_event_t * event, void * user_data)
52 {
53     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
54     id self = user_data;
55     [[VLCEventManager sharedManager] callOnMainThreadObject:self
56                                                  withMethod:@selector(mediaDiscovererStarted)
57                                        withArgumentAsObject:nil];
58     [pool release];
59 }
60
61 static void HandleMediaDiscovererEnded( const libvlc_event_t * event, void * user_data)
62 {
63     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
64     id self = user_data;
65     [[VLCEventManager sharedManager] callOnMainThreadObject:self
66                                                  withMethod:@selector(mediaDiscovererEnded)
67                                        withArgumentAsObject:nil];
68     [pool release];
69 }
70
71
72 @implementation VLCMediaDiscoverer
73 + (NSArray *)availableMediaDiscoverer
74 {
75     if( !availableMediaDiscoverer )
76     {
77         availableMediaDiscoverer = [[NSArray arrayWithObjects:
78                                 [[[VLCMediaDiscoverer alloc] initWithName:@"sap"] autorelease],
79                                 [[[VLCMediaDiscoverer alloc] initWithName:@"upnp_intel"] autorelease],
80                                 [[[VLCMediaDiscoverer alloc] initWithName:@"freebox"] autorelease],
81                                 [[[VLCMediaDiscoverer alloc] initWithName:@"video_dir"] autorelease],
82                                 [[[VLCMediaDiscoverer alloc] initWithName:@"shoutcast"] autorelease],
83                                 [[[VLCMediaDiscoverer alloc] initWithName:@"shoutcasttv"] autorelease], nil] retain];
84     }
85     return availableMediaDiscoverer;
86 }
87
88 - (id)initWithName:(NSString *)aServiceName
89 {
90     if (self = [super init])
91     {
92         libvlc_exception_t ex;
93         libvlc_exception_init(&ex);
94         localizedName = nil;
95         discoveredMedia = nil;
96         mdis = libvlc_media_discoverer_new_from_name([VLCLibrary sharedInstance],
97                                                      [aServiceName UTF8String],
98                                                      &ex);
99         catch_exception(&ex);
100
101         libvlc_event_manager_t * p_em = libvlc_media_discoverer_event_manager(mdis);
102         libvlc_event_attach(p_em, libvlc_MediaDiscovererStarted, HandleMediaDiscovererStarted, self);
103         libvlc_event_attach(p_em, libvlc_MediaDiscovererEnded,   HandleMediaDiscovererEnded,   self);
104
105         running = libvlc_media_discoverer_is_running(mdis);
106     }
107     return self;
108 }
109
110 - (void)release
111 {
112     @synchronized(self)
113     {
114         if([self retainCount] <= 1)
115         {
116             /* We must make sure we won't receive new event after an upcoming dealloc
117              * We also may receive a -retain in some event callback that may occcur
118              * Before libvlc_event_detach. So this can't happen in dealloc */
119             libvlc_event_manager_t * p_em = libvlc_media_list_event_manager(mdis);
120             libvlc_event_detach(p_em, libvlc_MediaDiscovererStarted, HandleMediaDiscovererStarted, self);
121             libvlc_event_detach(p_em, libvlc_MediaDiscovererEnded,   HandleMediaDiscovererEnded,   self);
122         }
123         [super release];
124     }
125 }
126
127 - (void)dealloc
128 {
129     [localizedName release];
130     [discoveredMedia release];
131     libvlc_media_discoverer_release( mdis );
132     [super dealloc];
133 }
134
135 - (VLCMediaList *) discoveredMedia
136 {
137     if( discoveredMedia )
138         return discoveredMedia;
139
140     libvlc_media_list_t * p_mlist = libvlc_media_discoverer_media_list( mdis );
141     VLCMediaList * ret = [VLCMediaList mediaListWithLibVLCMediaList:p_mlist];
142     libvlc_media_list_release( p_mlist );
143
144     discoveredMedia = [ret retain];
145     return discoveredMedia;
146 }
147
148 - (NSString *)localizedName
149 {
150     if ( localizedName )
151         return localizedName;
152
153     char * name = libvlc_media_discoverer_localized_name( mdis );
154     if (name)
155     {
156         localizedName = [[NSString stringWithUTF8String:name] retain];
157         free( name );
158     }
159     return localizedName;
160 }
161
162 - (BOOL)isRunning
163 {
164     return running;
165 }
166 @end
167
168 @implementation VLCMediaDiscoverer (Private)
169 - (void)mediaDiscovererStarted
170 {
171     [self willChangeValueForKey:@"running"];
172     running = YES;
173     [self didChangeValueForKey:@"running"];
174 }
175
176 - (void)mediaDiscovererEnded
177 {
178     [self willChangeValueForKey:@"running"];
179     running = NO;
180     [self didChangeValueForKey:@"running"];
181 }
182 @end