1 /*****************************************************************************
2 * vlm.c: libvlc new API VLM handling functions
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
24 #include <vlc/libvlc.h>
25 #include <vlc/libvlc_vlm.h>
27 #include <vlc_input.h>
30 #include "libvlc_internal.h"
33 /* local function to be used in libvlc_vlm_show_media only */
34 static char* recurse_answer( char* psz_prefix, vlm_message_t *p_answer ) {
35 char* psz_childprefix;
36 char* psz_response="";
39 vlm_message_t *aw_child, **paw_child;
41 asprintf( &psz_childprefix, "%s%s.", psz_prefix, p_answer->psz_name );
43 if ( p_answer->i_child )
45 paw_child = p_answer->child;
46 aw_child = *( paw_child );
47 for( i = 0; i < p_answer->i_child; i++ )
49 asprintf( &response_tmp, "%s%s%s:%s\n",
50 psz_response, psz_prefix, aw_child->psz_name,
51 aw_child->psz_value );
53 psz_response = response_tmp;
54 if ( aw_child->i_child )
56 asprintf(&response_tmp, "%s%s", psz_response,
57 recurse_answer(psz_childprefix, aw_child));
59 psz_response = response_tmp;
62 aw_child = *( paw_child );
65 free( psz_childprefix );
69 char* libvlc_vlm_show_media( libvlc_instance_t *p_instance, char *psz_name,
70 libvlc_exception_t *p_exception )
73 vlm_message_t *answer;
77 asprintf( &psz_message, "show %s", psz_name );
78 asprintf( &psz_response, "", psz_name );
79 vlm_ExecuteCommand( p_instance->p_vlm, psz_message, &answer );
80 if( answer->psz_value )
82 libvlc_exception_raise( p_exception, "Unable to call show %s: %s",
83 psz_name, answer->psz_value );
89 psz_response = recurse_answer( "", answer );
93 return(psz_response );
97 char* libvlc_vlm_show_media( libvlc_instance_t *p_instance,
99 libvlc_exception_t *p_exception )
102 /* FIXME is it needed ? */
103 libvlc_exception_raise( p_exception, "Unable to call show %s", psz_name );
109 static int libvlc_vlm_init( libvlc_instance_t *p_instance,
110 libvlc_exception_t *p_exception )
112 if( !p_instance->p_vlm )
113 p_instance->p_vlm = vlm_New( p_instance->p_libvlc_int );
115 if( !p_instance->p_vlm )
117 libvlc_exception_raise( p_exception,
118 "Unable to create VLM." );
123 #define VLM_RET(p,ret) do { \
124 if( libvlc_vlm_init( p_instance, p_exception ) ) return ret;\
125 (p) = p_instance->p_vlm; \
127 #define VLM(p) VLM_RET(p,)
129 static vlm_media_instance_t *
130 libvlc_vlm_get_media_instance( libvlc_instance_t *p_instance,
131 const char *psz_name, int i_minstance_idx,
132 libvlc_exception_t *p_exception )
135 vlm_media_instance_t **pp_minstance;
136 vlm_media_instance_t *p_minstance;
140 VLM_RET(p_vlm, NULL);
142 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
143 vlm_Control( p_vlm, VLM_GET_MEDIA_INSTANCES, id, &pp_minstance,
146 libvlc_exception_raise( p_exception, "Unable to get %s instances",
151 if( i_minstance_idx >= 0 && i_minstance_idx < i_minstance )
153 p_minstance = pp_minstance[i_minstance_idx];
154 TAB_REMOVE( i_minstance, pp_minstance, p_minstance );
156 while( i_minstance > 0 )
157 vlm_media_instance_Delete( pp_minstance[--i_minstance] );
158 TAB_CLEAN( i_minstance, pp_minstance );
163 void libvlc_vlm_release( libvlc_instance_t *p_instance,
164 libvlc_exception_t *p_exception)
173 void libvlc_vlm_add_broadcast( libvlc_instance_t *p_instance,
174 const char *psz_name,
175 const char *psz_input,
176 const char *psz_output, int i_options,
177 const char * const *ppsz_options,
178 int b_enabled, int b_loop,
179 libvlc_exception_t *p_exception )
187 vlm_media_Init( &m );
188 m.psz_name = strdup( psz_name );
189 m.b_enabled = b_enabled;
191 m.broadcast.b_loop = b_loop;
193 TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
195 m.psz_output = strdup( psz_output );
196 for( n = 0; n < i_options; n++ )
197 TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
199 n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
200 vlm_media_Clean( &m );
202 libvlc_exception_raise( p_exception, "Media %s creation failed",
206 void libvlc_vlm_add_vod( libvlc_instance_t *p_instance, const char *psz_name,
207 const char *psz_input, int i_options,
208 const char * const *ppsz_options, int b_enabled,
209 const char *psz_mux, libvlc_exception_t *p_exception )
217 vlm_media_Init( &m );
218 m.psz_name = strdup( psz_name );
219 m.b_enabled = b_enabled;
221 m.vod.psz_mux = psz_mux ? strdup( psz_mux ) : NULL;
223 TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
224 for( n = 0; n < i_options; n++ )
225 TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
227 n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
228 vlm_media_Clean( &m );
230 libvlc_exception_raise( p_exception, "Media %s creation failed",
234 void libvlc_vlm_del_media( libvlc_instance_t *p_instance, const char *psz_name,
235 libvlc_exception_t *p_exception )
242 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
243 vlm_Control( p_vlm, VLM_DEL_MEDIA, id ) )
245 libvlc_exception_raise( p_exception, "Unable to delete %s", psz_name );
249 #define VLM_CHANGE(psz_error, code ) do { \
250 vlm_media_t *p_media; \
254 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) || \
255 vlm_Control( p_vlm, VLM_GET_MEDIA, id, &p_media ) ) { \
256 libvlc_exception_raise( p_exception, psz_error, psz_name ); \
259 if( !p_media ) goto error; \
263 if( vlm_Control( p_vlm, VLM_CHANGE_MEDIA, p_media ) ) { \
264 vlm_media_Delete( p_media ); \
267 vlm_media_Delete( p_media ); \
270 libvlc_exception_raise( p_exception, psz_error, psz_name );\
273 void libvlc_vlm_set_enabled( libvlc_instance_t *p_instance,
274 const char *psz_name, int b_enabled,
275 libvlc_exception_t *p_exception )
277 #define VLM_CHANGE_CODE { p_media->b_enabled = b_enabled; }
278 VLM_CHANGE( "Unable to delete %s", VLM_CHANGE_CODE );
279 #undef VLM_CHANGE_CODE
282 void libvlc_vlm_set_loop( libvlc_instance_t *p_instance, const char *psz_name,
283 int b_loop, libvlc_exception_t *p_exception )
285 #define VLM_CHANGE_CODE { p_media->broadcast.b_loop = b_loop; }
286 VLM_CHANGE( "Unable to change %s loop property", VLM_CHANGE_CODE );
287 #undef VLM_CHANGE_CODE
290 void libvlc_vlm_set_mux( libvlc_instance_t *p_instance, const char *psz_name,
291 const char *psz_mux, libvlc_exception_t *p_exception )
293 #define VLM_CHANGE_CODE { if( p_media->b_vod ) { \
294 free( p_media->vod.psz_mux ); \
295 p_media->vod.psz_mux = psz_mux \
296 ? strdup( psz_mux ) : NULL; \
298 VLM_CHANGE( "Unable to change %s mux property", VLM_CHANGE_CODE );
299 #undef VLM_CHANGE_CODE
302 void libvlc_vlm_set_output( libvlc_instance_t *p_instance,
303 const char *psz_name, const char *psz_output,
304 libvlc_exception_t *p_exception )
306 #define VLM_CHANGE_CODE { free( p_media->psz_output ); \
307 p_media->psz_output = strdup( psz_output ); }
308 VLM_CHANGE( "Unable to change %s output property", VLM_CHANGE_CODE );
309 #undef VLM_CHANGE_CODE
312 void libvlc_vlm_set_input( libvlc_instance_t *p_instance,
313 const char *psz_name, const char *psz_input,
314 libvlc_exception_t *p_exception )
316 #define VLM_CHANGE_CODE { while( p_media->i_input > 0 ) \
317 free( p_media->ppsz_input[--p_media->i_input] );\
318 TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
319 strdup(psz_input) ); }
320 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
321 #undef VLM_CHANGE_CODE
324 void libvlc_vlm_add_input( libvlc_instance_t *p_instance,
325 const char *psz_name, const char *psz_input,
326 libvlc_exception_t *p_exception )
328 #define VLM_CHANGE_CODE { TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
329 strdup(psz_input) ); }
330 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
331 #undef VLM_CHANGE_CODE
334 void libvlc_vlm_change_media( libvlc_instance_t *p_instance,
335 const char *psz_name, const char *psz_input,
336 const char *psz_output, int i_options,
337 const char * const *ppsz_options, int b_enabled,
338 int b_loop, libvlc_exception_t *p_exception )
340 #define VLM_CHANGE_CODE { int n; \
341 p_media->b_enabled = b_enabled; \
342 p_media->broadcast.b_loop = b_loop; \
343 while( p_media->i_input > 0 ) \
344 free( p_media->ppsz_input[--p_media->i_input] ); \
346 TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); \
347 free( p_media->psz_output ); \
348 p_media->psz_output = psz_output ? strdup( psz_output ) : NULL; \
349 while( p_media->i_option > 0 ) \
350 free( p_media->ppsz_option[--p_media->i_option] ); \
351 for( n = 0; n < i_options; n++ ) \
352 TAB_APPEND( p_media->i_option, p_media->ppsz_option, \
353 strdup(ppsz_options[n]) ); \
355 VLM_CHANGE( "Unable to change %s properties", VLM_CHANGE_CODE );
356 #undef VLM_CHANGE_CODE
359 void libvlc_vlm_play_media( libvlc_instance_t *p_instance,
360 const char *psz_name,
361 libvlc_exception_t *p_exception )
368 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
369 vlm_Control( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, id, NULL, 0 ) )
371 libvlc_exception_raise( p_exception, "Unable to play %s", psz_name );
375 void libvlc_vlm_stop_media( libvlc_instance_t *p_instance,
376 const char *psz_name,
377 libvlc_exception_t *p_exception )
384 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
385 vlm_Control( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, NULL ) )
387 libvlc_exception_raise( p_exception, "Unable to stop %s", psz_name );
391 void libvlc_vlm_pause_media( libvlc_instance_t *p_instance,
392 const char *psz_name,
393 libvlc_exception_t *p_exception )
400 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
401 vlm_Control( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, id, NULL ) )
403 libvlc_exception_raise( p_exception, "Unable to pause %s", psz_name );
407 void libvlc_vlm_seek_media( libvlc_instance_t *p_instance,
408 const char *psz_name, float f_percentage,
409 libvlc_exception_t *p_exception )
416 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
417 vlm_Control( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, NULL,
419 libvlc_exception_raise( p_exception, "Unable to seek %s to %f",
420 psz_name, f_percentage );
423 float libvlc_vlm_get_media_instance_position( libvlc_instance_t *p_instance,
424 const char *psz_name,
426 libvlc_exception_t *p_exception )
428 vlm_media_instance_t *p_mi;
431 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
432 i_instance, p_exception );
435 result = p_mi->d_position;
436 vlm_media_instance_Delete( p_mi );
441 int libvlc_vlm_get_media_instance_time( libvlc_instance_t *p_instance,
442 const char *psz_name, int i_instance,
443 libvlc_exception_t *p_exception )
445 vlm_media_instance_t *p_mi;
448 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
449 i_instance, p_exception );
452 result = p_mi->i_time;
453 vlm_media_instance_Delete( p_mi );
458 int libvlc_vlm_get_media_instance_length( libvlc_instance_t *p_instance,
459 const char *psz_name,
461 libvlc_exception_t *p_exception )
463 vlm_media_instance_t *p_mi;
466 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
467 i_instance, p_exception );
470 result = p_mi->i_length;
471 vlm_media_instance_Delete( p_mi );
476 int libvlc_vlm_get_media_instance_rate( libvlc_instance_t *p_instance,
477 const char *psz_name, int i_instance,
478 libvlc_exception_t *p_exception )
480 vlm_media_instance_t *p_mi;
483 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
484 i_instance, p_exception );
487 result = p_mi->i_rate;
488 vlm_media_instance_Delete( p_mi );
493 int libvlc_vlm_get_media_instance_title( libvlc_instance_t *p_instance,
494 const char *psz_name, int i_instance,
495 libvlc_exception_t *p_exception )
497 vlm_media_instance_t *p_mi;
499 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
500 i_instance, p_exception );
502 vlm_media_instance_Delete( p_mi );
503 return p_mi ? 0 : -1;
506 int libvlc_vlm_get_media_instance_chapter( libvlc_instance_t *p_instance,
507 const char *psz_name,
509 libvlc_exception_t *p_exception )
511 vlm_media_instance_t *p_mi;
513 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
514 i_instance, p_exception );
516 vlm_media_instance_Delete( p_mi );
517 return p_mi ? 0 : -1;
520 int libvlc_vlm_get_media_instance_seekable( libvlc_instance_t *p_instance,
521 const char *psz_name,
523 libvlc_exception_t *p_exception )
525 vlm_media_instance_t *p_mi;
527 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
528 i_instance, p_exception );
530 vlm_media_instance_Delete( p_mi );
531 return p_mi ? 0 : -1;