libvlc: add renderer_discoverer external API
[vlc.git] / lib / renderer_discoverer.c
1 /*****************************************************************************
2  * renderer_discoverer.c: libvlc renderer API
3  *****************************************************************************
4  * Copyright © 2016 VLC authors and VideoLAN
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <assert.h>
26
27 #include <vlc/libvlc.h>
28 #include <vlc/libvlc_renderer_discoverer.h>
29
30 #include <vlc_common.h>
31
32 #include "libvlc_internal.h"
33 #include "renderer_discoverer_internal.h"
34
35 struct libvlc_renderer_discoverer_t
36 {
37     vlc_renderer_discovery_t *p_rd;
38     libvlc_event_manager_t *p_event_manager;
39
40     int                     i_items;
41     vlc_renderer_item_t **  pp_items;
42 };
43
44 static_assert( VLC_RENDERER_CAN_AUDIO == LIBVLC_RENDERER_CAN_AUDIO &&
45                VLC_RENDERER_CAN_VIDEO == LIBVLC_RENDERER_CAN_VIDEO,
46               "core/libvlc renderer flags mismatch" );
47
48 const vlc_renderer_item_t *
49 libvlc_renderer_item_to_vlc( const libvlc_renderer_item_t *p_item )
50 {
51     return (const vlc_renderer_item_t*) p_item;
52 }
53
54 static void
55 renderer_discovery_item_added( const vlc_event_t *p_event, void *p_user_data )
56 {
57     libvlc_renderer_discoverer_t *p_lrd = p_user_data;
58     vlc_renderer_item_t *p_item =
59         p_event->u.renderer_discovery_item_added.p_new_item;
60
61     vlc_renderer_item_hold( p_item );
62
63     TAB_APPEND( p_lrd->i_items, p_lrd->pp_items, p_item );
64
65     libvlc_event_t event = {
66         .type = libvlc_RendererDiscovererItemAdded,
67         .u.renderer_discoverer_item_added.item =
68             (libvlc_renderer_item_t*) p_item,
69     };
70     libvlc_event_send( p_lrd->p_event_manager, &event );
71 }
72
73 static void
74 renderer_discovery_item_removed( const vlc_event_t *p_event, void *p_user_data )
75 {
76     libvlc_renderer_discoverer_t *p_lrd = p_user_data;
77     vlc_renderer_item_t *p_item =
78         p_event->u.renderer_discovery_item_removed.p_item;
79
80     int i_idx;
81     TAB_FIND( p_lrd->i_items, p_lrd->pp_items, p_item, i_idx );
82     assert( i_idx != -1 );
83     TAB_ERASE( p_lrd->i_items, p_lrd->pp_items, i_idx );
84
85     libvlc_event_t event = {
86         .type = libvlc_RendererDiscovererItemDeleted,
87         .u.renderer_discoverer_item_deleted.item =
88             (libvlc_renderer_item_t*) p_item,
89     };
90     libvlc_event_send( p_lrd->p_event_manager, &event );
91
92     vlc_renderer_item_release( p_item );
93 }
94
95 const char *
96 libvlc_renderer_item_name( const libvlc_renderer_item_t *p_item )
97 {
98     return vlc_renderer_item_name( (vlc_renderer_item_t *) p_item );
99 }
100
101 const char *
102 libvlc_renderer_item_type( const libvlc_renderer_item_t *p_item )
103 {
104     return vlc_renderer_item_type( (vlc_renderer_item_t *) p_item );
105 }
106
107 const char *
108 libvlc_renderer_item_icon_uri( const libvlc_renderer_item_t *p_item )
109 {
110     return vlc_renderer_item_icon_uri( (vlc_renderer_item_t *) p_item );
111 }
112
113 int
114 libvlc_renderer_item_flags( const libvlc_renderer_item_t *p_item )
115 {
116     return vlc_renderer_item_flags( (vlc_renderer_item_t *) p_item );
117 }
118
119 libvlc_renderer_discoverer_t *
120 libvlc_renderer_discoverer_new( libvlc_instance_t *p_inst,
121                                 const char *psz_name )
122 {
123     libvlc_renderer_discoverer_t *p_lrd =
124         calloc( 1, sizeof(libvlc_renderer_discoverer_t) );
125
126     if( unlikely(p_lrd == NULL) )
127         return NULL;
128
129     p_lrd->p_rd = vlc_rd_new( VLC_OBJECT( p_inst->p_libvlc_int ), psz_name );
130     if( unlikely(p_lrd->p_rd == NULL) )
131         goto error;
132
133     TAB_INIT( p_lrd->i_items, p_lrd->pp_items );
134
135     p_lrd->p_event_manager = libvlc_event_manager_new( p_lrd );
136     if( unlikely(p_lrd->p_event_manager == NULL) )
137         goto error;
138
139     vlc_event_manager_t *p_rd_ev = vlc_rd_event_manager( p_lrd->p_rd );
140
141     if( vlc_event_attach( p_rd_ev, vlc_RendererDiscoveryItemAdded,
142                           renderer_discovery_item_added, p_lrd )
143                           != VLC_SUCCESS )
144         goto error;
145     if( vlc_event_attach( p_rd_ev, vlc_RendererDiscoveryItemRemoved,
146                           renderer_discovery_item_removed, p_lrd )
147                           != VLC_SUCCESS )
148         goto error;
149
150     return p_lrd;
151
152 error:
153     libvlc_renderer_discoverer_release( p_lrd );
154     return NULL;
155 }
156
157 void
158 libvlc_renderer_discoverer_release( libvlc_renderer_discoverer_t *p_lrd )
159 {
160     if( p_lrd->p_rd != NULL )
161         vlc_rd_release( p_lrd->p_rd );
162
163     if( p_lrd->p_event_manager != NULL )
164         libvlc_event_manager_release( p_lrd->p_event_manager );
165
166     free( p_lrd );
167 }
168
169 int
170 libvlc_renderer_discoverer_start( libvlc_renderer_discoverer_t *p_lrd )
171 {
172     return vlc_rd_start( p_lrd->p_rd );
173 }
174
175 void
176 libvlc_renderer_discoverer_stop( libvlc_renderer_discoverer_t *p_lrd )
177 {
178     vlc_rd_stop( p_lrd->p_rd );
179
180     for( int i = 0; i < p_lrd->i_items; ++i )
181         vlc_renderer_item_release( p_lrd->pp_items[i] );
182     TAB_CLEAN( p_lrd->i_items, p_lrd->pp_items );
183 }
184
185 libvlc_event_manager_t *
186 libvlc_renderer_discoverer_event_manager( libvlc_renderer_discoverer_t *p_lrd )
187 {
188     return p_lrd->p_event_manager;
189 }
190
191 void
192 libvlc_renderer_discoverer_list_release( libvlc_rd_description_t **pp_services,
193                                          size_t i_count )
194 {
195     if( i_count > 0 )
196     {
197         for( size_t i = 0; i < i_count; ++i )
198         {
199             free( pp_services[i]->psz_name );
200             free( pp_services[i]->psz_longname );
201         }
202         free( *pp_services );
203         free( pp_services );
204     }
205 }
206
207 ssize_t
208 libvlc_renderer_discoverer_list_get( libvlc_instance_t *p_inst,
209                                      libvlc_rd_description_t ***ppp_services )
210 {
211     assert( p_inst != NULL && ppp_services != NULL );
212
213     /* Fetch all rd names, and longnames */
214     char **ppsz_names, **ppsz_longnames;
215     int i_ret = vlc_rd_get_names( p_inst->p_libvlc_int, &ppsz_names,
216                                   &ppsz_longnames );
217
218     if( i_ret != VLC_SUCCESS )
219     {
220         *ppp_services = NULL;
221         return -1;
222     }
223
224     /* Count the number of sd matching our category (i_cat/i_core_cat) */
225     size_t i_nb_services = 0;
226     char **ppsz_name = ppsz_names;
227     for( ; *ppsz_name != NULL; ppsz_name++ )
228         i_nb_services++;
229
230     libvlc_rd_description_t **pp_services = NULL,
231                                               *p_services = NULL;
232     if( i_nb_services > 0 )
233     {
234         /* Double alloc here, so that the caller iterates through pointers of
235          * struct instead of structs. This allows us to modify the struct
236          * without breaking the API. */
237
238         pp_services =
239             malloc( i_nb_services
240                     * sizeof(libvlc_rd_description_t *) );
241         p_services =
242             malloc( i_nb_services
243                     * sizeof(libvlc_rd_description_t) );
244         if( pp_services == NULL || p_services == NULL )
245         {
246             free( pp_services );
247             free( p_services );
248             pp_services = NULL;
249             p_services = NULL;
250             i_nb_services = -1;
251             /* Even if alloc fails, the next loop must be run in order to free
252              * names returned by vlc_sd_GetNames */
253         }
254     }
255
256     /* Fill output pp_services or free unused name, longname */
257     char **ppsz_longname = ppsz_longnames;
258     unsigned int i_service_idx = 0;
259     libvlc_rd_description_t *p_service = p_services;
260     for( ppsz_name = ppsz_names; *ppsz_name != NULL; ppsz_name++, ppsz_longname++ )
261     {
262         if( pp_services != NULL )
263         {
264             p_service->psz_name = *ppsz_name;
265             p_service->psz_longname = *ppsz_longname;
266             pp_services[i_service_idx++] = p_service++;
267         }
268         else
269         {
270             free( *ppsz_name );
271             free( *ppsz_longname );
272         }
273     }
274     free( ppsz_names );
275     free( ppsz_longnames );
276
277     *ppp_services = pp_services;
278     return i_nb_services;
279 }