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 "libvlc_internal.h"
26 #include <vlc/libvlc.h>
28 #include <vlc_input.h>
32 /* local function to be used in libvlc_vlm_show_media only */
33 static char* recurse_answer( char* psz_prefix, vlm_message_t *p_answer ) {
34 char* psz_childprefix;
35 char* psz_response="";
38 vlm_message_t *aw_child, **paw_child;
40 asprintf( &psz_childprefix, "%s%s.", psz_prefix, p_answer->psz_name );
42 if ( p_answer->i_child )
44 paw_child = p_answer->child;
45 aw_child = *( paw_child );
46 for( i = 0; i < p_answer->i_child; i++ )
48 asprintf( &response_tmp, "%s%s%s:%s\n",
49 psz_response, psz_prefix, aw_child->psz_name,
50 aw_child->psz_value );
52 psz_response = response_tmp;
53 if ( aw_child->i_child )
55 asprintf(&response_tmp, "%s%s", psz_response,
56 recurse_answer(psz_childprefix, aw_child));
58 psz_response = response_tmp;
61 aw_child = *( paw_child );
64 free( psz_childprefix );
68 char* libvlc_vlm_show_media( libvlc_instance_t *p_instance, char *psz_name,
69 libvlc_exception_t *p_exception )
72 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 );
95 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
101 char* libvlc_vlm_show_media( libvlc_instance_t *p_instance,
102 const char *psz_name,
103 libvlc_exception_t *p_exception )
107 /* FIXME is it needed ? */
108 libvlc_exception_raise( p_exception, "Unable to call show %s", psz_name );
111 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
118 static int libvlc_vlm_init( libvlc_instance_t *p_instance,
119 libvlc_exception_t *p_exception )
122 if( !p_instance->p_vlm )
123 p_instance->p_vlm = vlm_New( p_instance->p_libvlc_int );
125 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
129 if( !p_instance->p_vlm )
131 libvlc_exception_raise( p_exception,
132 "Unable to create VLM." );
137 #define VLM_RET(p,ret) do { \
138 if( libvlc_vlm_init( p_instance, p_exception ) ) return ret;\
139 (p) = p_instance->p_vlm; \
141 #define VLM(p) VLM_RET(p,)
143 static vlm_media_instance_t *libvlc_vlm_get_media_instance( libvlc_instance_t *p_instance,
144 const char *psz_name,
146 libvlc_exception_t *p_exception )
150 vlm_media_instance_t **pp_minstance;
151 vlm_media_instance_t *p_minstance;
155 VLM_RET(p_vlm, NULL);
157 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
158 vlm_Control( p_vlm, VLM_GET_MEDIA_INSTANCES, id, &pp_minstance, &i_minstance ) )
160 libvlc_exception_raise( p_exception, "Unable to get %s instances", psz_name );
164 if( i_minstance_idx >= 0 && i_minstance_idx < i_minstance )
166 p_minstance = pp_minstance[i_minstance_idx];
167 TAB_REMOVE( i_minstance, pp_minstance, p_minstance );
169 while( i_minstance > 0 )
170 vlm_media_instance_Delete( pp_minstance[--i_minstance] );
171 TAB_CLEAN( i_minstance, pp_minstance );
174 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
180 void libvlc_vlm_release( libvlc_instance_t *p_instance, libvlc_exception_t *p_exception)
189 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
194 void libvlc_vlm_add_broadcast( libvlc_instance_t *p_instance,
195 const char *psz_name,
196 const char *psz_input,
197 const char *psz_output, int i_options,
198 const char * const *ppsz_options,
199 int b_enabled, int b_loop,
200 libvlc_exception_t *p_exception )
209 vlm_media_Init( &m );
210 m.psz_name = strdup( psz_name );
211 m.b_enabled = b_enabled;
213 m.broadcast.b_loop = b_loop;
215 TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
217 m.psz_output = strdup( psz_output );
218 for( n = 0; n < i_options; n++ )
219 TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
221 n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
222 vlm_media_Clean( &m );
224 libvlc_exception_raise( p_exception, "Media %s creation failed", psz_name );
226 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
231 void libvlc_vlm_add_vod( libvlc_instance_t *p_instance, const char *psz_name,
232 const char *psz_input, int i_options,
233 const char * const *ppsz_options, int b_enabled,
234 const char *psz_mux, libvlc_exception_t *p_exception )
243 vlm_media_Init( &m );
244 m.psz_name = strdup( psz_name );
245 m.b_enabled = b_enabled;
247 m.vod.psz_mux = psz_mux ? strdup( psz_mux ) : NULL;
249 TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
250 for( n = 0; n < i_options; n++ )
251 TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
253 n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
254 vlm_media_Clean( &m );
256 libvlc_exception_raise( p_exception, "Media %s creation failed", psz_name );
258 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
263 void libvlc_vlm_del_media( libvlc_instance_t *p_instance, const char *psz_name,
264 libvlc_exception_t *p_exception )
272 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
273 vlm_Control( p_vlm, VLM_DEL_MEDIA, id ) )
275 libvlc_exception_raise( p_exception, "Unable to delete %s", psz_name );
278 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
283 #define VLM_CHANGE(psz_error, code ) do { \
284 vlm_media_t *p_media; \
288 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) || \
289 vlm_Control( p_vlm, VLM_GET_MEDIA, id, &p_media ) ) { \
290 libvlc_exception_raise( p_exception, psz_error, psz_name ); \
293 if( !p_media ) goto error; \
297 if( vlm_Control( p_vlm, VLM_CHANGE_MEDIA, p_media ) ) { \
298 vlm_media_Delete( p_media ); \
301 vlm_media_Delete( p_media ); \
304 libvlc_exception_raise( p_exception, psz_error, psz_name );\
307 void libvlc_vlm_set_enabled( libvlc_instance_t *p_instance,
308 const char *psz_name, int b_enabled,
309 libvlc_exception_t *p_exception )
312 #define VLM_CHANGE_CODE { p_media->b_enabled = b_enabled; }
313 VLM_CHANGE( "Unable to delete %s", VLM_CHANGE_CODE );
314 #undef VLM_CHANGE_CODE
316 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
321 void libvlc_vlm_set_loop( libvlc_instance_t *p_instance, const char *psz_name,
322 int b_loop, libvlc_exception_t *p_exception )
325 #define VLM_CHANGE_CODE { p_media->broadcast.b_loop = b_loop; }
326 VLM_CHANGE( "Unable to change %s loop property", VLM_CHANGE_CODE );
327 #undef VLM_CHANGE_CODE
329 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
334 void libvlc_vlm_set_mux( libvlc_instance_t *p_instance, const char *psz_name,
335 const char *psz_mux, libvlc_exception_t *p_exception )
338 #define VLM_CHANGE_CODE { if( p_media->b_vod ) { \
339 free( p_media->vod.psz_mux ); \
340 p_media->vod.psz_mux = psz_mux ? strdup( psz_mux ) : NULL; \
342 VLM_CHANGE( "Unable to change %s mux property", VLM_CHANGE_CODE );
343 #undef VLM_CHANGE_CODE
345 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
350 void libvlc_vlm_set_output( libvlc_instance_t *p_instance,
351 const char *psz_name, const char *psz_output,
352 libvlc_exception_t *p_exception )
355 #define VLM_CHANGE_CODE { free( p_media->psz_output ); \
356 p_media->psz_output = strdup( psz_output ); }
357 VLM_CHANGE( "Unable to change %s output property", VLM_CHANGE_CODE );
358 #undef VLM_CHANGE_CODE
360 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
365 void libvlc_vlm_set_input( libvlc_instance_t *p_instance,
366 const char *psz_name, const char *psz_input,
367 libvlc_exception_t *p_exception )
370 #define VLM_CHANGE_CODE { while( p_media->i_input > 0 ) \
371 free( p_media->ppsz_input[--p_media->i_input] );\
372 TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); }
373 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
374 #undef VLM_CHANGE_CODE
376 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
381 void libvlc_vlm_add_input( libvlc_instance_t *p_instance,
382 const char *psz_name, const char *psz_input,
383 libvlc_exception_t *p_exception )
386 #define VLM_CHANGE_CODE { TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); }
387 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
388 #undef VLM_CHANGE_CODE
390 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
395 void libvlc_vlm_change_media( libvlc_instance_t *p_instance,
396 const char *psz_name, const char *psz_input,
397 const char *psz_output, int i_options,
398 const char * const *ppsz_options, int b_enabled,
399 int b_loop, libvlc_exception_t *p_exception )
402 #define VLM_CHANGE_CODE { int n; \
403 p_media->b_enabled = b_enabled; \
404 p_media->broadcast.b_loop = b_loop; \
405 while( p_media->i_input > 0 ) \
406 free( p_media->ppsz_input[--p_media->i_input] ); \
408 TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); \
409 free( p_media->psz_output ); \
410 p_media->psz_output = psz_output ? strdup( psz_output ) : NULL; \
411 while( p_media->i_option > 0 ) \
412 free( p_media->ppsz_option[--p_media->i_option] ); \
413 for( n = 0; n < i_options; n++ ) \
414 TAB_APPEND( p_media->i_option, p_media->ppsz_option, strdup(ppsz_options[n]) ); \
416 VLM_CHANGE( "Unable to change %s properties", VLM_CHANGE_CODE );
417 #undef VLM_CHANGE_CODE
419 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
424 void libvlc_vlm_play_media( libvlc_instance_t *p_instance,
425 const char *psz_name,
426 libvlc_exception_t *p_exception )
434 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
435 vlm_Control( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, id, NULL, 0 ) )
437 libvlc_exception_raise( p_exception, "Unable to play %s", psz_name );
440 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
445 void libvlc_vlm_stop_media( libvlc_instance_t *p_instance,
446 const char *psz_name,
447 libvlc_exception_t *p_exception )
455 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
456 vlm_Control( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, NULL ) )
458 libvlc_exception_raise( p_exception, "Unable to stop %s", psz_name );
461 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
466 void libvlc_vlm_pause_media( libvlc_instance_t *p_instance,
467 const char *psz_name,
468 libvlc_exception_t *p_exception )
476 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
477 vlm_Control( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, id, NULL ) )
479 libvlc_exception_raise( p_exception, "Unable to pause %s", psz_name );
482 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
487 void libvlc_vlm_seek_media( libvlc_instance_t *p_instance,
488 const char *psz_name, float f_percentage,
489 libvlc_exception_t *p_exception )
497 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
498 vlm_Control( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, NULL, f_percentage ) )
500 libvlc_exception_raise( p_exception, "Unable to seek %s to %f", psz_name, f_percentage );
503 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
508 float libvlc_vlm_get_media_instance_position( libvlc_instance_t *p_instance,
509 const char *psz_name,
511 libvlc_exception_t *p_exception )
515 vlm_media_instance_t *p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
516 i_instance, p_exception );
519 result = p_mi->d_position;
520 vlm_media_instance_Delete( p_mi );
523 libvlc_exception_raise( p_exception, "Unable to get position attribute" );
526 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
531 int libvlc_vlm_get_media_instance_time( libvlc_instance_t *p_instance,
532 const char *psz_name, int i_instance,
533 libvlc_exception_t *p_exception )
537 vlm_media_instance_t *p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
538 i_instance, p_exception );
541 result = p_mi->i_time;
542 vlm_media_instance_Delete( p_mi );
545 libvlc_exception_raise( p_exception, "Unable to get time attribute" );
548 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
553 int libvlc_vlm_get_media_instance_length( libvlc_instance_t *p_instance,
554 const char *psz_name,
556 libvlc_exception_t *p_exception )
560 vlm_media_instance_t *p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
561 i_instance, p_exception );
564 result = p_mi->i_length;
565 vlm_media_instance_Delete( p_mi );
568 libvlc_exception_raise( p_exception, "Unable to get length attribute" );
571 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
576 int libvlc_vlm_get_media_instance_rate( libvlc_instance_t *p_instance,
577 const char *psz_name, int i_instance,
578 libvlc_exception_t *p_exception )
582 vlm_media_instance_t *p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
583 i_instance, p_exception );
586 result = p_mi->i_rate;
587 vlm_media_instance_Delete( p_mi );
590 libvlc_exception_raise( p_exception, "Unable to get rate attribute" );
593 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
598 int libvlc_vlm_get_media_instance_title( libvlc_instance_t *p_instance,
599 const char *psz_name, int i_instance,
600 libvlc_exception_t *p_exception )
604 vlm_media_instance_t *p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
605 i_instance, p_exception );
608 vlm_media_instance_Delete( p_mi );
611 libvlc_exception_raise( p_exception, "Unable to get title attribute" );
614 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
619 int libvlc_vlm_get_media_instance_chapter( libvlc_instance_t *p_instance,
620 const char *psz_name,
622 libvlc_exception_t *p_exception )
626 vlm_media_instance_t *p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
627 i_instance, p_exception );
630 vlm_media_instance_Delete( p_mi );
633 libvlc_exception_raise( p_exception, "Unable to get chapter attribute" );
636 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
641 int libvlc_vlm_get_media_instance_seekable( libvlc_instance_t *p_instance,
642 const char *psz_name,
644 libvlc_exception_t *p_exception )
648 vlm_media_instance_t *p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
649 i_instance, p_exception );
652 vlm_media_instance_Delete( p_mi );
655 libvlc_exception_raise( p_exception, "Unable to get seekable attribute" );
658 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );