demux: adaptive: add missing override specifier
[vlc.git] / lib / renderer_discoverer.c
blob67b81f4bf165754454a87ae2b511682985c29ce2
1 /*****************************************************************************
2 * renderer_discoverer.c: libvlc renderer API
3 *****************************************************************************
4 * Copyright © 2016 VLC authors and VideoLAN
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.
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.
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 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <assert.h>
27 #include <vlc/libvlc.h>
28 #include <vlc/libvlc_renderer_discoverer.h>
30 #include <vlc_common.h>
32 #include "libvlc_internal.h"
33 #include "renderer_discoverer_internal.h"
35 struct libvlc_renderer_discoverer_t
37 libvlc_event_manager_t event_manager;
38 vlc_object_t * p_object;
39 vlc_renderer_discovery_t *p_rd;
41 int i_items;
42 vlc_renderer_item_t ** pp_items;
43 char name[];
46 static_assert( VLC_RENDERER_CAN_AUDIO == LIBVLC_RENDERER_CAN_AUDIO &&
47 VLC_RENDERER_CAN_VIDEO == LIBVLC_RENDERER_CAN_VIDEO,
48 "core/libvlc renderer flags mismatch" );
50 vlc_renderer_item_t *
51 libvlc_renderer_item_to_vlc( libvlc_renderer_item_t *p_item )
53 return (vlc_renderer_item_t*) p_item;
56 static void renderer_discovery_item_added( vlc_renderer_discovery_t *rd,
57 vlc_renderer_item_t *p_item )
59 libvlc_renderer_discoverer_t *p_lrd = rd->owner.sys;
61 vlc_renderer_item_hold( p_item );
63 TAB_APPEND( p_lrd->i_items, p_lrd->pp_items, p_item );
65 libvlc_event_t event = {
66 .type = libvlc_RendererDiscovererItemAdded,
67 .u.renderer_discoverer_item_added.item =
68 (libvlc_renderer_item_t*) p_item,
70 libvlc_event_send( &p_lrd->event_manager, &event );
73 static void renderer_discovery_item_removed( vlc_renderer_discovery_t *rd,
74 vlc_renderer_item_t *p_item )
76 libvlc_renderer_discoverer_t *p_lrd = rd->owner.sys;
78 int i_idx;
79 TAB_FIND( p_lrd->i_items, p_lrd->pp_items, p_item, i_idx );
80 assert( i_idx != -1 );
81 TAB_ERASE( p_lrd->i_items, p_lrd->pp_items, i_idx );
83 libvlc_event_t event = {
84 .type = libvlc_RendererDiscovererItemDeleted,
85 .u.renderer_discoverer_item_deleted.item =
86 (libvlc_renderer_item_t*) p_item,
88 libvlc_event_send( &p_lrd->event_manager, &event );
90 vlc_renderer_item_release( p_item );
93 libvlc_renderer_item_t *
94 libvlc_renderer_item_hold(libvlc_renderer_item_t *p_item)
96 vlc_renderer_item_hold( (vlc_renderer_item_t *) p_item );
97 return p_item;
100 void
101 libvlc_renderer_item_release(libvlc_renderer_item_t *p_item)
103 vlc_renderer_item_release( (vlc_renderer_item_t *) p_item );
106 const char *
107 libvlc_renderer_item_name( const libvlc_renderer_item_t *p_item )
109 return vlc_renderer_item_name( (const vlc_renderer_item_t *) p_item );
112 const char *
113 libvlc_renderer_item_type( const libvlc_renderer_item_t *p_item )
115 return vlc_renderer_item_type( (const vlc_renderer_item_t *) p_item );
118 const char *
119 libvlc_renderer_item_icon_uri( const libvlc_renderer_item_t *p_item )
121 return vlc_renderer_item_icon_uri( (const vlc_renderer_item_t *) p_item );
125 libvlc_renderer_item_flags( const libvlc_renderer_item_t *p_item )
127 return vlc_renderer_item_flags( (const vlc_renderer_item_t *) p_item );
130 libvlc_renderer_discoverer_t *
131 libvlc_renderer_discoverer_new( libvlc_instance_t *p_inst,
132 const char *psz_name )
134 size_t len = strlen( psz_name ) + 1;
135 libvlc_renderer_discoverer_t *p_lrd = malloc( sizeof(*p_lrd) + len );
137 if( unlikely(p_lrd == NULL) )
138 return NULL;
140 p_lrd->p_object = VLC_OBJECT(p_inst->p_libvlc_int);
141 memcpy( p_lrd->name, psz_name, len );
142 TAB_INIT( p_lrd->i_items, p_lrd->pp_items );
143 p_lrd->p_rd = NULL;
144 libvlc_event_manager_init( &p_lrd->event_manager, p_lrd );
146 return p_lrd;
149 void
150 libvlc_renderer_discoverer_release( libvlc_renderer_discoverer_t *p_lrd )
152 libvlc_renderer_discoverer_stop( p_lrd );
153 libvlc_event_manager_destroy( &p_lrd->event_manager );
154 free( p_lrd );
158 libvlc_renderer_discoverer_start( libvlc_renderer_discoverer_t *p_lrd )
160 assert( p_lrd->p_rd == NULL );
162 struct vlc_renderer_discovery_owner owner =
164 p_lrd,
165 renderer_discovery_item_added,
166 renderer_discovery_item_removed,
169 p_lrd->p_rd = vlc_rd_new( p_lrd->p_object, p_lrd->name, &owner );
170 return p_lrd->p_rd != NULL ? 0 : -1;
173 void
174 libvlc_renderer_discoverer_stop( libvlc_renderer_discoverer_t *p_lrd )
176 if( p_lrd->p_rd != NULL )
178 vlc_rd_release( p_lrd->p_rd );
179 p_lrd->p_rd = NULL;
182 for( int i = 0; i < p_lrd->i_items; ++i )
183 vlc_renderer_item_release( p_lrd->pp_items[i] );
184 TAB_CLEAN( p_lrd->i_items, p_lrd->pp_items );
187 libvlc_event_manager_t *
188 libvlc_renderer_discoverer_event_manager( libvlc_renderer_discoverer_t *p_lrd )
190 return &p_lrd->event_manager;
193 void
194 libvlc_renderer_discoverer_list_release( libvlc_rd_description_t **pp_services,
195 size_t i_count )
197 if( i_count > 0 )
199 for( size_t i = 0; i < i_count; ++i )
201 free( pp_services[i]->psz_name );
202 free( pp_services[i]->psz_longname );
204 free( *pp_services );
205 free( pp_services );
209 size_t
210 libvlc_renderer_discoverer_list_get( libvlc_instance_t *p_inst,
211 libvlc_rd_description_t ***ppp_services )
213 assert( p_inst != NULL && ppp_services != NULL );
215 /* Fetch all rd names, and longnames */
216 char **ppsz_names, **ppsz_longnames;
217 int i_ret = vlc_rd_get_names( p_inst->p_libvlc_int, &ppsz_names,
218 &ppsz_longnames );
220 if( i_ret != VLC_SUCCESS )
222 *ppp_services = NULL;
223 return 0;
226 /* Count the number of sd matching our category (i_cat/i_core_cat) */
227 size_t i_nb_services = 0;
228 char **ppsz_name = ppsz_names;
229 for( ; *ppsz_name != NULL; ppsz_name++ )
230 i_nb_services++;
232 libvlc_rd_description_t **pp_services = NULL,
233 *p_services = NULL;
234 if( i_nb_services > 0 )
236 /* Double alloc here, so that the caller iterates through pointers of
237 * struct instead of structs. This allows us to modify the struct
238 * without breaking the API. */
240 pp_services =
241 malloc( i_nb_services
242 * sizeof(libvlc_rd_description_t *) );
243 p_services =
244 malloc( i_nb_services
245 * sizeof(libvlc_rd_description_t) );
246 if( pp_services == NULL || p_services == NULL )
248 free( pp_services );
249 free( p_services );
250 pp_services = NULL;
251 p_services = NULL;
252 i_nb_services = 0;
253 /* Even if alloc fails, the next loop must be run in order to free
254 * names returned by vlc_sd_GetNames */
258 /* Fill output pp_services or free unused name, longname */
259 char **ppsz_longname = ppsz_longnames;
260 unsigned int i_service_idx = 0;
261 libvlc_rd_description_t *p_service = p_services;
262 for( ppsz_name = ppsz_names; *ppsz_name != NULL; ppsz_name++, ppsz_longname++ )
264 if( pp_services != NULL )
266 p_service->psz_name = *ppsz_name;
267 p_service->psz_longname = *ppsz_longname;
268 pp_services[i_service_idx++] = p_service++;
270 else
272 free( *ppsz_name );
273 free( *ppsz_longname );
276 free( ppsz_names );
277 free( ppsz_longnames );
279 *ppp_services = pp_services;
280 return i_nb_services;