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)
221 p_instance->p_vlm = NULL;
224 void libvlc_vlm_add_broadcast( libvlc_instance_t *p_instance,
225 const char *psz_name,
226 const char *psz_input,
227 const char *psz_output, int i_options,
228 const char * const *ppsz_options,
229 int b_enabled, int b_loop,
230 libvlc_exception_t *p_exception )
238 vlm_media_Init( &m );
239 m.psz_name = strdup( psz_name );
240 m.b_enabled = b_enabled;
242 m.broadcast.b_loop = b_loop;
244 TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
246 m.psz_output = strdup( psz_output );
247 for( n = 0; n < i_options; n++ )
248 TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
250 n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
251 vlm_media_Clean( &m );
253 libvlc_exception_raise( p_exception, "Media %s creation failed",
257 void libvlc_vlm_add_vod( libvlc_instance_t *p_instance, const char *psz_name,
258 const char *psz_input, int i_options,
259 const char * const *ppsz_options, int b_enabled,
260 const char *psz_mux, libvlc_exception_t *p_exception )
268 vlm_media_Init( &m );
269 m.psz_name = strdup( psz_name );
270 m.b_enabled = b_enabled;
272 m.vod.psz_mux = psz_mux ? strdup( psz_mux ) : NULL;
274 TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
275 for( n = 0; n < i_options; n++ )
276 TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
278 n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
279 vlm_media_Clean( &m );
281 libvlc_exception_raise( p_exception, "Media %s creation failed",
285 void libvlc_vlm_del_media( libvlc_instance_t *p_instance, const char *psz_name,
286 libvlc_exception_t *p_exception )
293 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
294 vlm_Control( p_vlm, VLM_DEL_MEDIA, id ) )
296 libvlc_exception_raise( p_exception, "Unable to delete %s", psz_name );
300 #define VLM_CHANGE(psz_error, code ) do { \
301 vlm_media_t *p_media; \
305 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) || \
306 vlm_Control( p_vlm, VLM_GET_MEDIA, id, &p_media ) ) { \
307 libvlc_exception_raise( p_exception, psz_error, psz_name ); \
310 if( !p_media ) goto error; \
314 if( vlm_Control( p_vlm, VLM_CHANGE_MEDIA, p_media ) ) { \
315 vlm_media_Delete( p_media ); \
318 vlm_media_Delete( p_media ); \
321 libvlc_exception_raise( p_exception, psz_error, psz_name );\
324 void libvlc_vlm_set_enabled( libvlc_instance_t *p_instance,
325 const char *psz_name, int b_enabled,
326 libvlc_exception_t *p_exception )
328 #define VLM_CHANGE_CODE { p_media->b_enabled = b_enabled; }
329 VLM_CHANGE( "Unable to delete %s", VLM_CHANGE_CODE );
330 #undef VLM_CHANGE_CODE
333 void libvlc_vlm_set_loop( libvlc_instance_t *p_instance, const char *psz_name,
334 int b_loop, libvlc_exception_t *p_exception )
336 #define VLM_CHANGE_CODE { p_media->broadcast.b_loop = b_loop; }
337 VLM_CHANGE( "Unable to change %s loop property", VLM_CHANGE_CODE );
338 #undef VLM_CHANGE_CODE
341 void libvlc_vlm_set_mux( libvlc_instance_t *p_instance, const char *psz_name,
342 const char *psz_mux, libvlc_exception_t *p_exception )
344 #define VLM_CHANGE_CODE { if( p_media->b_vod ) { \
345 free( p_media->vod.psz_mux ); \
346 p_media->vod.psz_mux = psz_mux \
347 ? strdup( psz_mux ) : NULL; \
349 VLM_CHANGE( "Unable to change %s mux property", VLM_CHANGE_CODE );
350 #undef VLM_CHANGE_CODE
353 void libvlc_vlm_set_output( libvlc_instance_t *p_instance,
354 const char *psz_name, const char *psz_output,
355 libvlc_exception_t *p_exception )
357 #define VLM_CHANGE_CODE { free( p_media->psz_output ); \
358 p_media->psz_output = strdup( psz_output ); }
359 VLM_CHANGE( "Unable to change %s output property", VLM_CHANGE_CODE );
360 #undef VLM_CHANGE_CODE
363 void libvlc_vlm_set_input( libvlc_instance_t *p_instance,
364 const char *psz_name, const char *psz_input,
365 libvlc_exception_t *p_exception )
367 #define VLM_CHANGE_CODE { while( p_media->i_input > 0 ) \
368 free( p_media->ppsz_input[--p_media->i_input] );\
369 TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
370 strdup(psz_input) ); }
371 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
372 #undef VLM_CHANGE_CODE
375 void libvlc_vlm_add_input( libvlc_instance_t *p_instance,
376 const char *psz_name, const char *psz_input,
377 libvlc_exception_t *p_exception )
379 #define VLM_CHANGE_CODE { TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
380 strdup(psz_input) ); }
381 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
382 #undef VLM_CHANGE_CODE
385 void libvlc_vlm_change_media( libvlc_instance_t *p_instance,
386 const char *psz_name, const char *psz_input,
387 const char *psz_output, int i_options,
388 const char * const *ppsz_options, int b_enabled,
389 int b_loop, libvlc_exception_t *p_exception )
391 #define VLM_CHANGE_CODE { int n; \
392 p_media->b_enabled = b_enabled; \
393 p_media->broadcast.b_loop = b_loop; \
394 while( p_media->i_input > 0 ) \
395 free( p_media->ppsz_input[--p_media->i_input] ); \
397 TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); \
398 free( p_media->psz_output ); \
399 p_media->psz_output = psz_output ? strdup( psz_output ) : NULL; \
400 while( p_media->i_option > 0 ) \
401 free( p_media->ppsz_option[--p_media->i_option] ); \
402 for( n = 0; n < i_options; n++ ) \
403 TAB_APPEND( p_media->i_option, p_media->ppsz_option, \
404 strdup(ppsz_options[n]) ); \
406 VLM_CHANGE( "Unable to change %s properties", VLM_CHANGE_CODE );
407 #undef VLM_CHANGE_CODE
410 void libvlc_vlm_play_media( libvlc_instance_t *p_instance,
411 const char *psz_name,
412 libvlc_exception_t *p_exception )
419 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
420 vlm_Control( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, id, NULL, 0 ) )
422 libvlc_exception_raise( p_exception, "Unable to play %s", psz_name );
426 void libvlc_vlm_stop_media( libvlc_instance_t *p_instance,
427 const char *psz_name,
428 libvlc_exception_t *p_exception )
435 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
436 vlm_Control( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, NULL ) )
438 libvlc_exception_raise( p_exception, "Unable to stop %s", psz_name );
442 void libvlc_vlm_pause_media( libvlc_instance_t *p_instance,
443 const char *psz_name,
444 libvlc_exception_t *p_exception )
451 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
452 vlm_Control( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, id, NULL ) )
454 libvlc_exception_raise( p_exception, "Unable to pause %s", psz_name );
458 void libvlc_vlm_seek_media( libvlc_instance_t *p_instance,
459 const char *psz_name, float f_percentage,
460 libvlc_exception_t *p_exception )
467 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
468 vlm_Control( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, NULL,
470 libvlc_exception_raise( p_exception, "Unable to seek %s to %f",
471 psz_name, f_percentage );
474 float libvlc_vlm_get_media_instance_position( libvlc_instance_t *p_instance,
475 const char *psz_name,
477 libvlc_exception_t *p_exception )
479 vlm_media_instance_t *p_mi;
482 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
483 i_instance, p_exception );
486 result = p_mi->d_position;
487 vlm_media_instance_Delete( p_mi );
492 int libvlc_vlm_get_media_instance_time( libvlc_instance_t *p_instance,
493 const char *psz_name, int i_instance,
494 libvlc_exception_t *p_exception )
496 vlm_media_instance_t *p_mi;
499 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
500 i_instance, p_exception );
503 result = p_mi->i_time;
504 vlm_media_instance_Delete( p_mi );
509 int libvlc_vlm_get_media_instance_length( libvlc_instance_t *p_instance,
510 const char *psz_name,
512 libvlc_exception_t *p_exception )
514 vlm_media_instance_t *p_mi;
517 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
518 i_instance, p_exception );
521 result = p_mi->i_length;
522 vlm_media_instance_Delete( p_mi );
527 int libvlc_vlm_get_media_instance_rate( libvlc_instance_t *p_instance,
528 const char *psz_name, int i_instance,
529 libvlc_exception_t *p_exception )
531 vlm_media_instance_t *p_mi;
534 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
535 i_instance, p_exception );
538 result = p_mi->i_rate;
539 vlm_media_instance_Delete( p_mi );
544 int libvlc_vlm_get_media_instance_title( libvlc_instance_t *p_instance,
545 const char *psz_name, int i_instance,
546 libvlc_exception_t *p_exception )
548 vlm_media_instance_t *p_mi;
550 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
551 i_instance, p_exception );
553 vlm_media_instance_Delete( p_mi );
554 return p_mi ? 0 : -1;
557 int libvlc_vlm_get_media_instance_chapter( libvlc_instance_t *p_instance,
558 const char *psz_name,
560 libvlc_exception_t *p_exception )
562 vlm_media_instance_t *p_mi;
564 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
565 i_instance, p_exception );
567 vlm_media_instance_Delete( p_mi );
568 return p_mi ? 0 : -1;
571 int libvlc_vlm_get_media_instance_seekable( libvlc_instance_t *p_instance,
572 const char *psz_name,
574 libvlc_exception_t *p_exception )
576 vlm_media_instance_t *p_mi;
578 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
579 i_instance, p_exception );
581 vlm_media_instance_Delete( p_mi );
582 return p_mi ? 0 : -1;
585 libvlc_event_manager_t * libvlc_vlm_get_event_manager( libvlc_instance_t *p_instance,
586 libvlc_exception_t *p_exception )
589 VLM_RET( p_vlm, NULL);
590 return p_instance->p_event_manager;