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