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->libvlc_vlm.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 VLC_UNUSED( p_this );
118 VLC_UNUSED( old_val );
120 libvlc_event.u.vlm_media_event.psz_instance_name = NULL;
121 libvlc_event.u.vlm_media_event.psz_media_name = event->psz_name;
123 switch( event->i_type )
125 case VLM_EVENT_MEDIA_ADDED:
126 libvlc_event.type = libvlc_VlmMediaAdded;
128 case VLM_EVENT_MEDIA_REMOVED:
129 libvlc_event.type = libvlc_VlmMediaRemoved;
131 case VLM_EVENT_MEDIA_CHANGED:
132 libvlc_event.type = libvlc_VlmMediaChanged;
134 case VLM_EVENT_MEDIA_INSTANCE_STARTED:
135 libvlc_event.type = libvlc_VlmMediaInstanceStarted;
137 case VLM_EVENT_MEDIA_INSTANCE_STOPPED:
138 libvlc_event.type = libvlc_VlmMediaInstanceStopped;
140 case VLM_EVENT_MEDIA_INSTANCE_STATE:
141 libvlc_event.u.vlm_media_event.psz_instance_name =
142 event->psz_instance_name;
143 switch( event->input_state )
146 libvlc_event.type = libvlc_VlmMediaInstanceStatusInit;
150 libvlc_VlmMediaInstanceStatusOpening;
154 libvlc_VlmMediaInstanceStatusPlaying;
157 libvlc_event.type = libvlc_VlmMediaInstanceStatusPause;
160 libvlc_event.type = libvlc_VlmMediaInstanceStatusEnd;
163 libvlc_event.type = libvlc_VlmMediaInstanceStatusError;
172 libvlc_event_send( p_event_manager, &libvlc_event );
176 static void libvlc_vlm_release_internal( libvlc_instance_t *p_instance )
178 vlm_t *p_vlm = p_instance->libvlc_vlm.p_vlm;
179 if( !p_instance->libvlc_vlm.p_vlm )
181 /* We need to remove medias in order to receive events */
182 vlm_Control( p_vlm, VLM_CLEAR_MEDIAS );
183 vlm_Control( p_vlm, VLM_CLEAR_SCHEDULES );
185 var_DelCallback( (vlc_object_t *)p_vlm, "intf-event", VlmEvent,
186 p_instance->libvlc_vlm.p_event_manager );
187 p_instance->libvlc_vlm.pf_release = NULL;
188 libvlc_event_manager_release( p_instance->libvlc_vlm.p_event_manager );
189 p_instance->libvlc_vlm.p_event_manager = NULL;
191 p_instance->libvlc_vlm.p_vlm = NULL;
194 static int libvlc_vlm_init( libvlc_instance_t *p_instance,
195 libvlc_exception_t *p_exception )
197 if( !p_instance->libvlc_vlm.p_event_manager )
199 p_instance->libvlc_vlm.p_event_manager =
200 libvlc_event_manager_new( p_instance->libvlc_vlm.p_vlm,
201 p_instance, p_exception );
202 libvlc_event_manager_register_event_type(
203 p_instance->libvlc_vlm.p_event_manager,
204 libvlc_VlmMediaAdded, NULL );
205 libvlc_event_manager_register_event_type(
206 p_instance->libvlc_vlm.p_event_manager,
207 libvlc_VlmMediaRemoved, NULL );
208 libvlc_event_manager_register_event_type(
209 p_instance->libvlc_vlm.p_event_manager,
210 libvlc_VlmMediaChanged, NULL );
211 libvlc_event_manager_register_event_type(
212 p_instance->libvlc_vlm.p_event_manager,
213 libvlc_VlmMediaInstanceStarted, NULL );
214 libvlc_event_manager_register_event_type(
215 p_instance->libvlc_vlm.p_event_manager,
216 libvlc_VlmMediaInstanceStopped, NULL );
217 libvlc_event_manager_register_event_type(
218 p_instance->libvlc_vlm.p_event_manager,
219 libvlc_VlmMediaInstanceStatusInit, NULL );
220 libvlc_event_manager_register_event_type(
221 p_instance->libvlc_vlm.p_event_manager,
222 libvlc_VlmMediaInstanceStatusOpening, NULL );
223 libvlc_event_manager_register_event_type(
224 p_instance->libvlc_vlm.p_event_manager,
225 libvlc_VlmMediaInstanceStatusPlaying, NULL );
226 libvlc_event_manager_register_event_type(
227 p_instance->libvlc_vlm.p_event_manager,
228 libvlc_VlmMediaInstanceStatusPause, NULL );
229 libvlc_event_manager_register_event_type(
230 p_instance->libvlc_vlm.p_event_manager,
231 libvlc_VlmMediaInstanceStatusEnd, NULL );
232 libvlc_event_manager_register_event_type(
233 p_instance->libvlc_vlm.p_event_manager,
234 libvlc_VlmMediaInstanceStatusError, NULL );
237 if( !p_instance->libvlc_vlm.p_vlm )
239 p_instance->libvlc_vlm.p_vlm = vlm_New( p_instance->p_libvlc_int );
240 if( !p_instance->libvlc_vlm.p_vlm )
242 libvlc_exception_raise( p_exception,
243 "Unable to create VLM." );
246 var_AddCallback( (vlc_object_t *)p_instance->libvlc_vlm.p_vlm,
247 "intf-event", VlmEvent,
248 p_instance->libvlc_vlm.p_event_manager );
249 p_instance->libvlc_vlm.pf_release = libvlc_vlm_release_internal;
255 void libvlc_vlm_release( libvlc_instance_t *p_instance,
256 libvlc_exception_t *p_exception)
258 libvlc_vlm_release_internal( p_instance );
261 #define VLM_RET(p,ret) do { \
262 if( libvlc_vlm_init( p_instance, p_exception ) ) return ret;\
263 (p) = p_instance->libvlc_vlm.p_vlm; \
265 #define VLM(p) VLM_RET(p,)
267 static vlm_media_instance_t *
268 libvlc_vlm_get_media_instance( libvlc_instance_t *p_instance,
269 const char *psz_name, int i_minstance_idx,
270 libvlc_exception_t *p_exception )
273 vlm_media_instance_t **pp_minstance;
274 vlm_media_instance_t *p_minstance;
278 VLM_RET(p_vlm, NULL);
280 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
281 vlm_Control( p_vlm, VLM_GET_MEDIA_INSTANCES, id, &pp_minstance,
284 libvlc_exception_raise( p_exception, "Unable to get %s instances",
289 if( i_minstance_idx >= 0 && i_minstance_idx < i_minstance )
291 p_minstance = pp_minstance[i_minstance_idx];
292 TAB_REMOVE( i_minstance, pp_minstance, p_minstance );
294 while( i_minstance > 0 )
295 vlm_media_instance_Delete( pp_minstance[--i_minstance] );
296 TAB_CLEAN( i_minstance, pp_minstance );
300 void libvlc_vlm_add_broadcast( libvlc_instance_t *p_instance,
301 const char *psz_name,
302 const char *psz_input,
303 const char *psz_output, int i_options,
304 const char * const *ppsz_options,
305 int b_enabled, int b_loop,
306 libvlc_exception_t *p_exception )
314 vlm_media_Init( &m );
315 m.psz_name = strdup( psz_name );
316 m.b_enabled = b_enabled;
318 m.broadcast.b_loop = b_loop;
320 TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
322 m.psz_output = strdup( psz_output );
323 for( n = 0; n < i_options; n++ )
324 TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
326 n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
327 vlm_media_Clean( &m );
329 libvlc_exception_raise( p_exception, "Media %s creation failed",
333 void libvlc_vlm_add_vod( libvlc_instance_t *p_instance, const char *psz_name,
334 const char *psz_input, int i_options,
335 const char * const *ppsz_options, int b_enabled,
336 const char *psz_mux, libvlc_exception_t *p_exception )
344 vlm_media_Init( &m );
345 m.psz_name = strdup( psz_name );
346 m.b_enabled = b_enabled;
348 m.vod.psz_mux = psz_mux ? strdup( psz_mux ) : NULL;
350 TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
351 for( n = 0; n < i_options; n++ )
352 TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
354 n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
355 vlm_media_Clean( &m );
357 libvlc_exception_raise( p_exception, "Media %s creation failed",
361 void libvlc_vlm_del_media( libvlc_instance_t *p_instance, const char *psz_name,
362 libvlc_exception_t *p_exception )
369 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
370 vlm_Control( p_vlm, VLM_DEL_MEDIA, id ) )
372 libvlc_exception_raise( p_exception, "Unable to delete %s", psz_name );
376 #define VLM_CHANGE(psz_error, code ) do { \
377 vlm_media_t *p_media; \
381 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) || \
382 vlm_Control( p_vlm, VLM_GET_MEDIA, id, &p_media ) ) { \
383 libvlc_exception_raise( p_exception, psz_error, psz_name ); \
386 if( !p_media ) goto error; \
390 if( vlm_Control( p_vlm, VLM_CHANGE_MEDIA, p_media ) ) { \
391 vlm_media_Delete( p_media ); \
394 vlm_media_Delete( p_media ); \
397 libvlc_exception_raise( p_exception, psz_error, psz_name );\
400 void libvlc_vlm_set_enabled( libvlc_instance_t *p_instance,
401 const char *psz_name, int b_enabled,
402 libvlc_exception_t *p_exception )
404 #define VLM_CHANGE_CODE { p_media->b_enabled = b_enabled; }
405 VLM_CHANGE( "Unable to delete %s", VLM_CHANGE_CODE );
406 #undef VLM_CHANGE_CODE
409 void libvlc_vlm_set_loop( libvlc_instance_t *p_instance, const char *psz_name,
410 int b_loop, libvlc_exception_t *p_exception )
412 #define VLM_CHANGE_CODE { p_media->broadcast.b_loop = b_loop; }
413 VLM_CHANGE( "Unable to change %s loop property", VLM_CHANGE_CODE );
414 #undef VLM_CHANGE_CODE
417 void libvlc_vlm_set_mux( libvlc_instance_t *p_instance, const char *psz_name,
418 const char *psz_mux, libvlc_exception_t *p_exception )
420 #define VLM_CHANGE_CODE { if( p_media->b_vod ) { \
421 free( p_media->vod.psz_mux ); \
422 p_media->vod.psz_mux = psz_mux \
423 ? strdup( psz_mux ) : NULL; \
425 VLM_CHANGE( "Unable to change %s mux property", VLM_CHANGE_CODE );
426 #undef VLM_CHANGE_CODE
429 void libvlc_vlm_set_output( libvlc_instance_t *p_instance,
430 const char *psz_name, const char *psz_output,
431 libvlc_exception_t *p_exception )
433 #define VLM_CHANGE_CODE { free( p_media->psz_output ); \
434 p_media->psz_output = strdup( psz_output ); }
435 VLM_CHANGE( "Unable to change %s output property", VLM_CHANGE_CODE );
436 #undef VLM_CHANGE_CODE
439 void libvlc_vlm_set_input( libvlc_instance_t *p_instance,
440 const char *psz_name, const char *psz_input,
441 libvlc_exception_t *p_exception )
443 #define VLM_CHANGE_CODE { while( p_media->i_input > 0 ) \
444 free( p_media->ppsz_input[--p_media->i_input] );\
445 TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
446 strdup(psz_input) ); }
447 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
448 #undef VLM_CHANGE_CODE
451 void libvlc_vlm_add_input( libvlc_instance_t *p_instance,
452 const char *psz_name, const char *psz_input,
453 libvlc_exception_t *p_exception )
455 #define VLM_CHANGE_CODE { TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
456 strdup(psz_input) ); }
457 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
458 #undef VLM_CHANGE_CODE
461 void libvlc_vlm_change_media( libvlc_instance_t *p_instance,
462 const char *psz_name, const char *psz_input,
463 const char *psz_output, int i_options,
464 const char * const *ppsz_options, int b_enabled,
465 int b_loop, libvlc_exception_t *p_exception )
467 #define VLM_CHANGE_CODE { int n; \
468 p_media->b_enabled = b_enabled; \
469 p_media->broadcast.b_loop = b_loop; \
470 while( p_media->i_input > 0 ) \
471 free( p_media->ppsz_input[--p_media->i_input] ); \
473 TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); \
474 free( p_media->psz_output ); \
475 p_media->psz_output = psz_output ? strdup( psz_output ) : NULL; \
476 while( p_media->i_option > 0 ) \
477 free( p_media->ppsz_option[--p_media->i_option] ); \
478 for( n = 0; n < i_options; n++ ) \
479 TAB_APPEND( p_media->i_option, p_media->ppsz_option, \
480 strdup(ppsz_options[n]) ); \
482 VLM_CHANGE( "Unable to change %s properties", VLM_CHANGE_CODE );
483 #undef VLM_CHANGE_CODE
486 void libvlc_vlm_play_media( libvlc_instance_t *p_instance,
487 const char *psz_name,
488 libvlc_exception_t *p_exception )
495 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
496 vlm_Control( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, id, NULL, 0 ) )
498 libvlc_exception_raise( p_exception, "Unable to play %s", psz_name );
502 void libvlc_vlm_stop_media( libvlc_instance_t *p_instance,
503 const char *psz_name,
504 libvlc_exception_t *p_exception )
511 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
512 vlm_Control( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, NULL ) )
514 libvlc_exception_raise( p_exception, "Unable to stop %s", psz_name );
518 void libvlc_vlm_pause_media( libvlc_instance_t *p_instance,
519 const char *psz_name,
520 libvlc_exception_t *p_exception )
527 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
528 vlm_Control( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, id, NULL ) )
530 libvlc_exception_raise( p_exception, "Unable to pause %s", psz_name );
534 void libvlc_vlm_seek_media( libvlc_instance_t *p_instance,
535 const char *psz_name, float f_percentage,
536 libvlc_exception_t *p_exception )
543 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
544 vlm_Control( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, NULL,
546 libvlc_exception_raise( p_exception, "Unable to seek %s to %f",
547 psz_name, f_percentage );
550 float libvlc_vlm_get_media_instance_position( libvlc_instance_t *p_instance,
551 const char *psz_name,
553 libvlc_exception_t *p_exception )
555 vlm_media_instance_t *p_mi;
558 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
559 i_instance, p_exception );
562 result = p_mi->d_position;
563 vlm_media_instance_Delete( p_mi );
568 int libvlc_vlm_get_media_instance_time( libvlc_instance_t *p_instance,
569 const char *psz_name, int i_instance,
570 libvlc_exception_t *p_exception )
572 vlm_media_instance_t *p_mi;
575 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
576 i_instance, p_exception );
579 result = p_mi->i_time;
580 vlm_media_instance_Delete( p_mi );
585 int libvlc_vlm_get_media_instance_length( libvlc_instance_t *p_instance,
586 const char *psz_name,
588 libvlc_exception_t *p_exception )
590 vlm_media_instance_t *p_mi;
593 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
594 i_instance, p_exception );
597 result = p_mi->i_length;
598 vlm_media_instance_Delete( p_mi );
603 int libvlc_vlm_get_media_instance_rate( libvlc_instance_t *p_instance,
604 const char *psz_name, int i_instance,
605 libvlc_exception_t *p_exception )
607 vlm_media_instance_t *p_mi;
610 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
611 i_instance, p_exception );
614 result = p_mi->i_rate;
615 vlm_media_instance_Delete( p_mi );
620 int libvlc_vlm_get_media_instance_title( libvlc_instance_t *p_instance,
621 const char *psz_name, int i_instance,
622 libvlc_exception_t *p_exception )
624 vlm_media_instance_t *p_mi;
626 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
627 i_instance, p_exception );
629 vlm_media_instance_Delete( p_mi );
630 return p_mi ? 0 : -1;
633 int libvlc_vlm_get_media_instance_chapter( libvlc_instance_t *p_instance,
634 const char *psz_name,
636 libvlc_exception_t *p_exception )
638 vlm_media_instance_t *p_mi;
640 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
641 i_instance, p_exception );
643 vlm_media_instance_Delete( p_mi );
644 return p_mi ? 0 : -1;
647 int libvlc_vlm_get_media_instance_seekable( libvlc_instance_t *p_instance,
648 const char *psz_name,
650 libvlc_exception_t *p_exception )
652 vlm_media_instance_t *p_mi;
654 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
655 i_instance, p_exception );
657 vlm_media_instance_Delete( p_mi );
658 return p_mi ? 0 : -1;
661 libvlc_event_manager_t * libvlc_vlm_get_event_manager( libvlc_instance_t *p_instance,
662 libvlc_exception_t *p_exception )
665 VLM_RET( p_vlm, NULL);
666 return p_instance->libvlc_vlm.p_event_manager;