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 if( !p_instance->p_vlm )
164 libvlc_exception_raise( p_exception,
165 "Unable to create VLM." );
168 var_AddCallback( (vlc_object_t *)p_instance->p_vlm, "intf-event", VlmEvent,
169 p_instance->p_event_manager );
174 #define VLM_RET(p,ret) do { \
175 if( libvlc_vlm_init( p_instance, p_exception ) ) return ret;\
176 (p) = p_instance->p_vlm; \
178 #define VLM(p) VLM_RET(p,)
180 static vlm_media_instance_t *
181 libvlc_vlm_get_media_instance( libvlc_instance_t *p_instance,
182 const char *psz_name, int i_minstance_idx,
183 libvlc_exception_t *p_exception )
186 vlm_media_instance_t **pp_minstance;
187 vlm_media_instance_t *p_minstance;
191 VLM_RET(p_vlm, NULL);
193 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
194 vlm_Control( p_vlm, VLM_GET_MEDIA_INSTANCES, id, &pp_minstance,
197 libvlc_exception_raise( p_exception, "Unable to get %s instances",
202 if( i_minstance_idx >= 0 && i_minstance_idx < i_minstance )
204 p_minstance = pp_minstance[i_minstance_idx];
205 TAB_REMOVE( i_minstance, pp_minstance, p_minstance );
207 while( i_minstance > 0 )
208 vlm_media_instance_Delete( pp_minstance[--i_minstance] );
209 TAB_CLEAN( i_minstance, pp_minstance );
214 void libvlc_vlm_release( libvlc_instance_t *p_instance,
215 libvlc_exception_t *p_exception)
222 p_instance->p_vlm = NULL;
225 void libvlc_vlm_add_broadcast( libvlc_instance_t *p_instance,
226 const char *psz_name,
227 const char *psz_input,
228 const char *psz_output, int i_options,
229 const char * const *ppsz_options,
230 int b_enabled, int b_loop,
231 libvlc_exception_t *p_exception )
239 vlm_media_Init( &m );
240 m.psz_name = strdup( psz_name );
241 m.b_enabled = b_enabled;
243 m.broadcast.b_loop = b_loop;
245 TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
247 m.psz_output = strdup( psz_output );
248 for( n = 0; n < i_options; n++ )
249 TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
251 n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
252 vlm_media_Clean( &m );
254 libvlc_exception_raise( p_exception, "Media %s creation failed",
258 void libvlc_vlm_add_vod( libvlc_instance_t *p_instance, const char *psz_name,
259 const char *psz_input, int i_options,
260 const char * const *ppsz_options, int b_enabled,
261 const char *psz_mux, libvlc_exception_t *p_exception )
269 vlm_media_Init( &m );
270 m.psz_name = strdup( psz_name );
271 m.b_enabled = b_enabled;
273 m.vod.psz_mux = psz_mux ? strdup( psz_mux ) : NULL;
275 TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
276 for( n = 0; n < i_options; n++ )
277 TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
279 n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
280 vlm_media_Clean( &m );
282 libvlc_exception_raise( p_exception, "Media %s creation failed",
286 void libvlc_vlm_del_media( libvlc_instance_t *p_instance, const char *psz_name,
287 libvlc_exception_t *p_exception )
294 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
295 vlm_Control( p_vlm, VLM_DEL_MEDIA, id ) )
297 libvlc_exception_raise( p_exception, "Unable to delete %s", psz_name );
301 #define VLM_CHANGE(psz_error, code ) do { \
302 vlm_media_t *p_media; \
306 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) || \
307 vlm_Control( p_vlm, VLM_GET_MEDIA, id, &p_media ) ) { \
308 libvlc_exception_raise( p_exception, psz_error, psz_name ); \
311 if( !p_media ) goto error; \
315 if( vlm_Control( p_vlm, VLM_CHANGE_MEDIA, p_media ) ) { \
316 vlm_media_Delete( p_media ); \
319 vlm_media_Delete( p_media ); \
322 libvlc_exception_raise( p_exception, psz_error, psz_name );\
325 void libvlc_vlm_set_enabled( libvlc_instance_t *p_instance,
326 const char *psz_name, int b_enabled,
327 libvlc_exception_t *p_exception )
329 #define VLM_CHANGE_CODE { p_media->b_enabled = b_enabled; }
330 VLM_CHANGE( "Unable to delete %s", VLM_CHANGE_CODE );
331 #undef VLM_CHANGE_CODE
334 void libvlc_vlm_set_loop( libvlc_instance_t *p_instance, const char *psz_name,
335 int b_loop, libvlc_exception_t *p_exception )
337 #define VLM_CHANGE_CODE { p_media->broadcast.b_loop = b_loop; }
338 VLM_CHANGE( "Unable to change %s loop property", VLM_CHANGE_CODE );
339 #undef VLM_CHANGE_CODE
342 void libvlc_vlm_set_mux( libvlc_instance_t *p_instance, const char *psz_name,
343 const char *psz_mux, libvlc_exception_t *p_exception )
345 #define VLM_CHANGE_CODE { if( p_media->b_vod ) { \
346 free( p_media->vod.psz_mux ); \
347 p_media->vod.psz_mux = psz_mux \
348 ? strdup( psz_mux ) : NULL; \
350 VLM_CHANGE( "Unable to change %s mux property", VLM_CHANGE_CODE );
351 #undef VLM_CHANGE_CODE
354 void libvlc_vlm_set_output( libvlc_instance_t *p_instance,
355 const char *psz_name, const char *psz_output,
356 libvlc_exception_t *p_exception )
358 #define VLM_CHANGE_CODE { free( p_media->psz_output ); \
359 p_media->psz_output = strdup( psz_output ); }
360 VLM_CHANGE( "Unable to change %s output property", VLM_CHANGE_CODE );
361 #undef VLM_CHANGE_CODE
364 void libvlc_vlm_set_input( libvlc_instance_t *p_instance,
365 const char *psz_name, const char *psz_input,
366 libvlc_exception_t *p_exception )
368 #define VLM_CHANGE_CODE { while( p_media->i_input > 0 ) \
369 free( p_media->ppsz_input[--p_media->i_input] );\
370 TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
371 strdup(psz_input) ); }
372 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
373 #undef VLM_CHANGE_CODE
376 void libvlc_vlm_add_input( libvlc_instance_t *p_instance,
377 const char *psz_name, const char *psz_input,
378 libvlc_exception_t *p_exception )
380 #define VLM_CHANGE_CODE { TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
381 strdup(psz_input) ); }
382 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
383 #undef VLM_CHANGE_CODE
386 void libvlc_vlm_change_media( libvlc_instance_t *p_instance,
387 const char *psz_name, const char *psz_input,
388 const char *psz_output, int i_options,
389 const char * const *ppsz_options, int b_enabled,
390 int b_loop, libvlc_exception_t *p_exception )
392 #define VLM_CHANGE_CODE { int n; \
393 p_media->b_enabled = b_enabled; \
394 p_media->broadcast.b_loop = b_loop; \
395 while( p_media->i_input > 0 ) \
396 free( p_media->ppsz_input[--p_media->i_input] ); \
398 TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); \
399 free( p_media->psz_output ); \
400 p_media->psz_output = psz_output ? strdup( psz_output ) : NULL; \
401 while( p_media->i_option > 0 ) \
402 free( p_media->ppsz_option[--p_media->i_option] ); \
403 for( n = 0; n < i_options; n++ ) \
404 TAB_APPEND( p_media->i_option, p_media->ppsz_option, \
405 strdup(ppsz_options[n]) ); \
407 VLM_CHANGE( "Unable to change %s properties", VLM_CHANGE_CODE );
408 #undef VLM_CHANGE_CODE
411 void libvlc_vlm_play_media( libvlc_instance_t *p_instance,
412 const char *psz_name,
413 libvlc_exception_t *p_exception )
420 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
421 vlm_Control( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, id, NULL, 0 ) )
423 libvlc_exception_raise( p_exception, "Unable to play %s", psz_name );
427 void libvlc_vlm_stop_media( libvlc_instance_t *p_instance,
428 const char *psz_name,
429 libvlc_exception_t *p_exception )
436 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
437 vlm_Control( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, NULL ) )
439 libvlc_exception_raise( p_exception, "Unable to stop %s", psz_name );
443 void libvlc_vlm_pause_media( libvlc_instance_t *p_instance,
444 const char *psz_name,
445 libvlc_exception_t *p_exception )
452 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
453 vlm_Control( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, id, NULL ) )
455 libvlc_exception_raise( p_exception, "Unable to pause %s", psz_name );
459 void libvlc_vlm_seek_media( libvlc_instance_t *p_instance,
460 const char *psz_name, float f_percentage,
461 libvlc_exception_t *p_exception )
468 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
469 vlm_Control( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, NULL,
471 libvlc_exception_raise( p_exception, "Unable to seek %s to %f",
472 psz_name, f_percentage );
475 float libvlc_vlm_get_media_instance_position( libvlc_instance_t *p_instance,
476 const char *psz_name,
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->d_position;
488 vlm_media_instance_Delete( p_mi );
493 int libvlc_vlm_get_media_instance_time( 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;
500 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
501 i_instance, p_exception );
504 result = p_mi->i_time;
505 vlm_media_instance_Delete( p_mi );
510 int libvlc_vlm_get_media_instance_length( libvlc_instance_t *p_instance,
511 const char *psz_name,
513 libvlc_exception_t *p_exception )
515 vlm_media_instance_t *p_mi;
518 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
519 i_instance, p_exception );
522 result = p_mi->i_length;
523 vlm_media_instance_Delete( p_mi );
528 int libvlc_vlm_get_media_instance_rate( libvlc_instance_t *p_instance,
529 const char *psz_name, int i_instance,
530 libvlc_exception_t *p_exception )
532 vlm_media_instance_t *p_mi;
535 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
536 i_instance, p_exception );
539 result = p_mi->i_rate;
540 vlm_media_instance_Delete( p_mi );
545 int libvlc_vlm_get_media_instance_title( libvlc_instance_t *p_instance,
546 const char *psz_name, int i_instance,
547 libvlc_exception_t *p_exception )
549 vlm_media_instance_t *p_mi;
551 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
552 i_instance, p_exception );
554 vlm_media_instance_Delete( p_mi );
555 return p_mi ? 0 : -1;
558 int libvlc_vlm_get_media_instance_chapter( libvlc_instance_t *p_instance,
559 const char *psz_name,
561 libvlc_exception_t *p_exception )
563 vlm_media_instance_t *p_mi;
565 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
566 i_instance, p_exception );
568 vlm_media_instance_Delete( p_mi );
569 return p_mi ? 0 : -1;
572 int libvlc_vlm_get_media_instance_seekable( libvlc_instance_t *p_instance,
573 const char *psz_name,
575 libvlc_exception_t *p_exception )
577 vlm_media_instance_t *p_mi;
579 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
580 i_instance, p_exception );
582 vlm_media_instance_Delete( p_mi );
583 return p_mi ? 0 : -1;
586 libvlc_event_manager_t * libvlc_vlm_get_event_manager( libvlc_instance_t *p_instance,
587 libvlc_exception_t *p_exception )
590 VLM_RET( p_vlm, NULL);
591 return p_instance->p_event_manager;