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 );
108 /* VLM events callback. Transmit to libvlc */
109 static int VlmEvent( vlc_object_t *p_this, const char * name,
110 vlc_value_t old_val, vlc_value_t newval, void *param )
112 vlm_event_t *event = (vlm_event_t*)newval.p_address;
113 libvlc_event_manager_t *p_event_manager = (libvlc_event_manager_t *) param;
114 libvlc_event_t libvlc_event;
116 libvlc_event.u.vlm_media_event.psz_media_name = event->psz_name;
118 switch( event->i_type )
120 case VLM_EVENT_MEDIA_ADDED:
121 libvlc_event.type = libvlc_VlmMediaAdded;
123 case VLM_EVENT_MEDIA_REMOVED:
124 libvlc_event.type = libvlc_VlmMediaRemoved;
126 case VLM_EVENT_MEDIA_CHANGED:
127 libvlc_event.type = libvlc_VlmMediaChanged;
129 case VLM_EVENT_MEDIA_INSTANCE_STARTED:
130 libvlc_event.type = libvlc_VlmMediaInstanceStarted;
132 case VLM_EVENT_MEDIA_INSTANCE_STOPPED:
133 libvlc_event.type = libvlc_VlmMediaInstanceStopped;
136 libvlc_event_send( p_event_manager, &libvlc_event );
140 static int libvlc_vlm_init( libvlc_instance_t *p_instance,
141 libvlc_exception_t *p_exception )
143 if( !p_instance->p_event_manager )
145 p_instance->p_event_manager = libvlc_event_manager_new( p_instance->p_vlm,
146 p_instance, p_exception );
147 libvlc_event_manager_register_event_type( p_instance->p_event_manager,
148 libvlc_VlmMediaAdded, NULL );
149 libvlc_event_manager_register_event_type( p_instance->p_event_manager,
150 libvlc_VlmMediaRemoved, NULL );
151 libvlc_event_manager_register_event_type( p_instance->p_event_manager,
152 libvlc_VlmMediaChanged, NULL );
153 libvlc_event_manager_register_event_type( p_instance->p_event_manager,
154 libvlc_VlmMediaInstanceStarted, NULL );
155 libvlc_event_manager_register_event_type( p_instance->p_event_manager,
156 libvlc_VlmMediaInstanceStopped, NULL );
159 if( !p_instance->p_vlm )
161 p_instance->p_vlm = vlm_New( p_instance->p_libvlc_int );
162 var_AddCallback( (vlc_object_t *)p_instance->p_vlm, "intf-event", VlmEvent,
163 p_instance->p_event_manager );
165 if( !p_instance->p_vlm )
167 libvlc_exception_raise( p_exception,
168 "Unable to create VLM." );
173 #define VLM_RET(p,ret) do { \
174 if( libvlc_vlm_init( p_instance, p_exception ) ) return ret;\
175 (p) = p_instance->p_vlm; \
177 #define VLM(p) VLM_RET(p,)
179 static vlm_media_instance_t *
180 libvlc_vlm_get_media_instance( libvlc_instance_t *p_instance,
181 const char *psz_name, int i_minstance_idx,
182 libvlc_exception_t *p_exception )
185 vlm_media_instance_t **pp_minstance;
186 vlm_media_instance_t *p_minstance;
190 VLM_RET(p_vlm, NULL);
192 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
193 vlm_Control( p_vlm, VLM_GET_MEDIA_INSTANCES, id, &pp_minstance,
196 libvlc_exception_raise( p_exception, "Unable to get %s instances",
201 if( i_minstance_idx >= 0 && i_minstance_idx < i_minstance )
203 p_minstance = pp_minstance[i_minstance_idx];
204 TAB_REMOVE( i_minstance, pp_minstance, p_minstance );
206 while( i_minstance > 0 )
207 vlm_media_instance_Delete( pp_minstance[--i_minstance] );
208 TAB_CLEAN( i_minstance, pp_minstance );
213 void libvlc_vlm_release( libvlc_instance_t *p_instance,
214 libvlc_exception_t *p_exception)
223 void libvlc_vlm_add_broadcast( libvlc_instance_t *p_instance,
224 const char *psz_name,
225 const char *psz_input,
226 const char *psz_output, int i_options,
227 const char * const *ppsz_options,
228 int b_enabled, int b_loop,
229 libvlc_exception_t *p_exception )
237 vlm_media_Init( &m );
238 m.psz_name = strdup( psz_name );
239 m.b_enabled = b_enabled;
241 m.broadcast.b_loop = b_loop;
243 TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
245 m.psz_output = strdup( psz_output );
246 for( n = 0; n < i_options; n++ )
247 TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
249 n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
250 vlm_media_Clean( &m );
252 libvlc_exception_raise( p_exception, "Media %s creation failed",
256 void libvlc_vlm_add_vod( libvlc_instance_t *p_instance, const char *psz_name,
257 const char *psz_input, int i_options,
258 const char * const *ppsz_options, int b_enabled,
259 const char *psz_mux, libvlc_exception_t *p_exception )
267 vlm_media_Init( &m );
268 m.psz_name = strdup( psz_name );
269 m.b_enabled = b_enabled;
271 m.vod.psz_mux = psz_mux ? strdup( psz_mux ) : NULL;
273 TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
274 for( n = 0; n < i_options; n++ )
275 TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
277 n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
278 vlm_media_Clean( &m );
280 libvlc_exception_raise( p_exception, "Media %s creation failed",
284 void libvlc_vlm_del_media( libvlc_instance_t *p_instance, const char *psz_name,
285 libvlc_exception_t *p_exception )
292 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
293 vlm_Control( p_vlm, VLM_DEL_MEDIA, id ) )
295 libvlc_exception_raise( p_exception, "Unable to delete %s", psz_name );
299 #define VLM_CHANGE(psz_error, code ) do { \
300 vlm_media_t *p_media; \
304 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) || \
305 vlm_Control( p_vlm, VLM_GET_MEDIA, id, &p_media ) ) { \
306 libvlc_exception_raise( p_exception, psz_error, psz_name ); \
309 if( !p_media ) goto error; \
313 if( vlm_Control( p_vlm, VLM_CHANGE_MEDIA, p_media ) ) { \
314 vlm_media_Delete( p_media ); \
317 vlm_media_Delete( p_media ); \
320 libvlc_exception_raise( p_exception, psz_error, psz_name );\
323 void libvlc_vlm_set_enabled( libvlc_instance_t *p_instance,
324 const char *psz_name, int b_enabled,
325 libvlc_exception_t *p_exception )
327 #define VLM_CHANGE_CODE { p_media->b_enabled = b_enabled; }
328 VLM_CHANGE( "Unable to delete %s", VLM_CHANGE_CODE );
329 #undef VLM_CHANGE_CODE
332 void libvlc_vlm_set_loop( libvlc_instance_t *p_instance, const char *psz_name,
333 int b_loop, libvlc_exception_t *p_exception )
335 #define VLM_CHANGE_CODE { p_media->broadcast.b_loop = b_loop; }
336 VLM_CHANGE( "Unable to change %s loop property", VLM_CHANGE_CODE );
337 #undef VLM_CHANGE_CODE
340 void libvlc_vlm_set_mux( libvlc_instance_t *p_instance, const char *psz_name,
341 const char *psz_mux, libvlc_exception_t *p_exception )
343 #define VLM_CHANGE_CODE { if( p_media->b_vod ) { \
344 free( p_media->vod.psz_mux ); \
345 p_media->vod.psz_mux = psz_mux \
346 ? strdup( psz_mux ) : NULL; \
348 VLM_CHANGE( "Unable to change %s mux property", VLM_CHANGE_CODE );
349 #undef VLM_CHANGE_CODE
352 void libvlc_vlm_set_output( libvlc_instance_t *p_instance,
353 const char *psz_name, const char *psz_output,
354 libvlc_exception_t *p_exception )
356 #define VLM_CHANGE_CODE { free( p_media->psz_output ); \
357 p_media->psz_output = strdup( psz_output ); }
358 VLM_CHANGE( "Unable to change %s output property", VLM_CHANGE_CODE );
359 #undef VLM_CHANGE_CODE
362 void libvlc_vlm_set_input( libvlc_instance_t *p_instance,
363 const char *psz_name, const char *psz_input,
364 libvlc_exception_t *p_exception )
366 #define VLM_CHANGE_CODE { while( p_media->i_input > 0 ) \
367 free( p_media->ppsz_input[--p_media->i_input] );\
368 TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
369 strdup(psz_input) ); }
370 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
371 #undef VLM_CHANGE_CODE
374 void libvlc_vlm_add_input( libvlc_instance_t *p_instance,
375 const char *psz_name, const char *psz_input,
376 libvlc_exception_t *p_exception )
378 #define VLM_CHANGE_CODE { TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
379 strdup(psz_input) ); }
380 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
381 #undef VLM_CHANGE_CODE
384 void libvlc_vlm_change_media( libvlc_instance_t *p_instance,
385 const char *psz_name, const char *psz_input,
386 const char *psz_output, int i_options,
387 const char * const *ppsz_options, int b_enabled,
388 int b_loop, libvlc_exception_t *p_exception )
390 #define VLM_CHANGE_CODE { int n; \
391 p_media->b_enabled = b_enabled; \
392 p_media->broadcast.b_loop = b_loop; \
393 while( p_media->i_input > 0 ) \
394 free( p_media->ppsz_input[--p_media->i_input] ); \
396 TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); \
397 free( p_media->psz_output ); \
398 p_media->psz_output = psz_output ? strdup( psz_output ) : NULL; \
399 while( p_media->i_option > 0 ) \
400 free( p_media->ppsz_option[--p_media->i_option] ); \
401 for( n = 0; n < i_options; n++ ) \
402 TAB_APPEND( p_media->i_option, p_media->ppsz_option, \
403 strdup(ppsz_options[n]) ); \
405 VLM_CHANGE( "Unable to change %s properties", VLM_CHANGE_CODE );
406 #undef VLM_CHANGE_CODE
409 void libvlc_vlm_play_media( libvlc_instance_t *p_instance,
410 const char *psz_name,
411 libvlc_exception_t *p_exception )
418 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
419 vlm_Control( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, id, NULL, 0 ) )
421 libvlc_exception_raise( p_exception, "Unable to play %s", psz_name );
425 void libvlc_vlm_stop_media( libvlc_instance_t *p_instance,
426 const char *psz_name,
427 libvlc_exception_t *p_exception )
434 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
435 vlm_Control( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, NULL ) )
437 libvlc_exception_raise( p_exception, "Unable to stop %s", psz_name );
441 void libvlc_vlm_pause_media( libvlc_instance_t *p_instance,
442 const char *psz_name,
443 libvlc_exception_t *p_exception )
450 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
451 vlm_Control( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, id, NULL ) )
453 libvlc_exception_raise( p_exception, "Unable to pause %s", psz_name );
457 void libvlc_vlm_seek_media( libvlc_instance_t *p_instance,
458 const char *psz_name, float f_percentage,
459 libvlc_exception_t *p_exception )
466 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
467 vlm_Control( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, NULL,
469 libvlc_exception_raise( p_exception, "Unable to seek %s to %f",
470 psz_name, f_percentage );
473 float libvlc_vlm_get_media_instance_position( libvlc_instance_t *p_instance,
474 const char *psz_name,
476 libvlc_exception_t *p_exception )
478 vlm_media_instance_t *p_mi;
481 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
482 i_instance, p_exception );
485 result = p_mi->d_position;
486 vlm_media_instance_Delete( p_mi );
491 int libvlc_vlm_get_media_instance_time( libvlc_instance_t *p_instance,
492 const char *psz_name, int i_instance,
493 libvlc_exception_t *p_exception )
495 vlm_media_instance_t *p_mi;
498 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
499 i_instance, p_exception );
502 result = p_mi->i_time;
503 vlm_media_instance_Delete( p_mi );
508 int libvlc_vlm_get_media_instance_length( libvlc_instance_t *p_instance,
509 const char *psz_name,
511 libvlc_exception_t *p_exception )
513 vlm_media_instance_t *p_mi;
516 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
517 i_instance, p_exception );
520 result = p_mi->i_length;
521 vlm_media_instance_Delete( p_mi );
526 int libvlc_vlm_get_media_instance_rate( libvlc_instance_t *p_instance,
527 const char *psz_name, int i_instance,
528 libvlc_exception_t *p_exception )
530 vlm_media_instance_t *p_mi;
533 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
534 i_instance, p_exception );
537 result = p_mi->i_rate;
538 vlm_media_instance_Delete( p_mi );
543 int libvlc_vlm_get_media_instance_title( libvlc_instance_t *p_instance,
544 const char *psz_name, int i_instance,
545 libvlc_exception_t *p_exception )
547 vlm_media_instance_t *p_mi;
549 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
550 i_instance, p_exception );
552 vlm_media_instance_Delete( p_mi );
553 return p_mi ? 0 : -1;
556 int libvlc_vlm_get_media_instance_chapter( libvlc_instance_t *p_instance,
557 const char *psz_name,
559 libvlc_exception_t *p_exception )
561 vlm_media_instance_t *p_mi;
563 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
564 i_instance, p_exception );
566 vlm_media_instance_Delete( p_mi );
567 return p_mi ? 0 : -1;
570 int libvlc_vlm_get_media_instance_seekable( libvlc_instance_t *p_instance,
571 const char *psz_name,
573 libvlc_exception_t *p_exception )
575 vlm_media_instance_t *p_mi;
577 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
578 i_instance, p_exception );
580 vlm_media_instance_Delete( p_mi );
581 return p_mi ? 0 : -1;
584 libvlc_event_manager_t * libvlc_vlm_get_event_manager( libvlc_instance_t *p_instance,
585 libvlc_exception_t *p_exception )
588 VLM_RET( p_vlm, NULL);
589 return p_instance->p_event_manager;