vlc/libvlc.h: Set the return value of the libvlc_*_is_*() call to vlc_bool_t.
[vlc.git] / src / control / media_discoverer.c
1 /*****************************************************************************
2  * media_discoverer.c: libvlc new API media discoverer functions
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include "libvlc_internal.h"
25 #include <vlc/libvlc.h>
26 #include <assert.h>
27 #include "vlc_services_discovery.h"
28
29 /*
30  * Private functions
31  */
32
33 /**************************************************************************
34  *       services_discovery_item_added (Private) (VLC event callback)
35  **************************************************************************/
36
37 static void services_discovery_item_added( const vlc_event_t * p_event,
38                                            void * user_data )
39 {
40     input_item_t * p_item = p_event->u.services_discovery_item_added.p_new_item;
41     libvlc_media_descriptor_t * p_md;
42     libvlc_media_discoverer_t * p_mdis = user_data;
43
44     p_md = libvlc_media_descriptor_new_from_input_item(
45             p_mdis->p_libvlc_instance,
46             p_item, NULL );
47
48     libvlc_media_list_lock( p_mdis->p_mlist );
49     _libvlc_media_list_add_media_descriptor( p_mdis->p_mlist, p_md, NULL );
50     libvlc_media_list_unlock( p_mdis->p_mlist );
51 }
52
53 /**************************************************************************
54  *       services_discovery_item_removed (Private) (VLC event callback)
55  **************************************************************************/
56
57 static void services_discovery_item_removed( const vlc_event_t * p_event,
58                                              void * user_data )
59 {
60     input_item_t * p_item = p_event->u.services_discovery_item_added.p_new_item;
61     libvlc_media_descriptor_t * p_md;
62     libvlc_media_discoverer_t * p_mdis = user_data;
63
64     int i, count = libvlc_media_list_count( p_mdis->p_mlist, NULL );
65     libvlc_media_list_lock( p_mdis->p_mlist );
66     for( i = 0; i < count; i++ )
67     {
68         p_md = libvlc_media_list_item_at_index( p_mdis->p_mlist, i, NULL );
69         if( p_md->p_input_item == p_item )
70         {
71             _libvlc_media_list_remove_index( p_mdis->p_mlist, i, NULL );
72             break;
73         }
74     }
75     libvlc_media_list_unlock( p_mdis->p_mlist );
76 }
77
78 /**************************************************************************
79  *       services_discovery_started (Private) (VLC event callback)
80  **************************************************************************/
81
82 static void services_discovery_started( const vlc_event_t * p_event,
83                                         void * user_data )
84 {
85     libvlc_media_discoverer_t * p_mdis = user_data;
86     libvlc_event_t event;
87     p_mdis->running = VLC_TRUE;
88     event.type = libvlc_MediaDiscovererStarted;
89     libvlc_event_send( p_mdis->p_event_manager, &event );
90 }
91
92 /**************************************************************************
93  *       services_discovery_ended (Private) (VLC event callback)
94  **************************************************************************/
95
96 static void services_discovery_ended( const vlc_event_t * p_event,
97                                       void * user_data )
98 {
99     libvlc_media_discoverer_t * p_mdis = user_data;
100     libvlc_event_t event;
101     p_mdis->running = VLC_FALSE;
102     event.type = libvlc_MediaDiscovererEnded;
103     libvlc_event_send( p_mdis->p_event_manager, &event );
104 }
105
106 /*
107  * Public libvlc functions
108  */
109
110 /**************************************************************************
111  *       new (Public)
112  *
113  * Init an object.
114  **************************************************************************/
115 libvlc_media_discoverer_t *
116 libvlc_media_discoverer_new_from_name( libvlc_instance_t * p_inst,
117                                        const char * psz_name,
118                                        libvlc_exception_t * p_e )
119 {
120     libvlc_media_discoverer_t * p_mdis;
121  
122     p_mdis = malloc(sizeof(libvlc_media_discoverer_t));
123     if( !p_mdis )
124     {
125         libvlc_exception_raise( p_e, "Not enough memory" );
126         return NULL;
127     }
128
129     p_mdis->p_libvlc_instance = p_inst;
130     p_mdis->p_mlist = libvlc_media_list_new( p_inst, NULL );
131     p_mdis->p_mlist->b_read_only = VLC_TRUE;
132     p_mdis->running = VLC_FALSE;
133
134     p_mdis->p_event_manager = libvlc_event_manager_new( p_mdis,
135             p_inst, NULL );
136
137     libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
138             libvlc_MediaDiscovererStarted, NULL );
139     libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
140             libvlc_MediaDiscovererEnded, NULL );
141
142     p_mdis->p_sd = services_discovery_Create( (vlc_object_t*)p_inst->p_libvlc_int, psz_name );
143
144     if( !p_mdis->p_sd )
145     {
146         free( p_mdis );
147         libvlc_exception_raise( p_e, "Can't find the services_discovery module named '%s'", psz_name );
148         return NULL;
149     }
150
151     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
152                       vlc_ServicesDiscoveryItemAdded,
153                       services_discovery_item_added,
154                       p_mdis );
155     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
156                       vlc_ServicesDiscoveryItemRemoved,
157                       services_discovery_item_removed,
158                       p_mdis );
159     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
160                       vlc_ServicesDiscoveryStarted,
161                       services_discovery_started,
162                       p_mdis );
163     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
164                       vlc_ServicesDiscoveryEnded,
165                       services_discovery_ended,
166                       p_mdis );
167  
168     services_discovery_Start( p_mdis->p_sd );
169
170     /* Here we go */
171
172     return p_mdis;
173 }
174
175 /**************************************************************************
176  * release (Public)
177  **************************************************************************/
178 void
179 libvlc_media_discoverer_release( libvlc_media_discoverer_t * p_mdis )
180 {
181     libvlc_media_list_release( p_mdis->p_mlist );
182     services_discovery_Destroy( p_mdis->p_sd );
183     free( p_mdis );
184 }
185
186 /**************************************************************************
187  * localized_name (Public)
188  **************************************************************************/
189 char *
190 libvlc_media_discoverer_localized_name( libvlc_media_discoverer_t * p_mdis )
191 {
192     return services_discovery_GetLocalizedName( p_mdis->p_sd );
193 }
194
195 /**************************************************************************
196  * media_list (Public)
197  **************************************************************************/
198 libvlc_media_list_t *
199 libvlc_media_discoverer_media_list( libvlc_media_discoverer_t * p_mdis )
200 {
201     libvlc_media_list_retain( p_mdis->p_mlist );
202     return p_mdis->p_mlist;
203 }
204
205 /**************************************************************************
206  * event_manager (Public)
207  **************************************************************************/
208 libvlc_event_manager_t *
209 libvlc_media_discoverer_event_manager( libvlc_media_discoverer_t * p_mdis )
210 {
211     return p_mdis->p_event_manager;
212 }
213
214
215 /**************************************************************************
216  * running (Public)
217  **************************************************************************/
218 vlc_bool_t
219 libvlc_media_discoverer_is_running( libvlc_media_discoverer_t * p_mdis )
220 {
221     return p_mdis->running;
222 }
223