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 *****************************************************************************/
28 #include <vlc/libvlc.h>
29 #include <vlc/libvlc_vlm.h>
31 #include <vlc_input.h>
34 #include "libvlc_internal.h"
36 /* VLM events callback. Transmit to libvlc */
37 static int VlmEvent( vlc_object_t *p_this, const char * name,
38 vlc_value_t old_val, vlc_value_t newval, void *param )
40 vlm_event_t *event = (vlm_event_t*)newval.p_address;
41 libvlc_event_manager_t *p_event_manager = (libvlc_event_manager_t *) param;
42 libvlc_event_t libvlc_event;
46 VLC_UNUSED( old_val );
48 libvlc_event.u.vlm_media_event.psz_instance_name = NULL;
49 libvlc_event.u.vlm_media_event.psz_media_name = event->psz_name;
51 switch( event->i_type )
53 case VLM_EVENT_MEDIA_ADDED:
54 libvlc_event.type = libvlc_VlmMediaAdded;
56 case VLM_EVENT_MEDIA_REMOVED:
57 libvlc_event.type = libvlc_VlmMediaRemoved;
59 case VLM_EVENT_MEDIA_CHANGED:
60 libvlc_event.type = libvlc_VlmMediaChanged;
62 case VLM_EVENT_MEDIA_INSTANCE_STARTED:
63 libvlc_event.type = libvlc_VlmMediaInstanceStarted;
65 case VLM_EVENT_MEDIA_INSTANCE_STOPPED:
66 libvlc_event.type = libvlc_VlmMediaInstanceStopped;
68 case VLM_EVENT_MEDIA_INSTANCE_STATE:
69 libvlc_event.u.vlm_media_event.psz_instance_name =
70 event->psz_instance_name;
71 switch( event->input_state )
74 libvlc_event.type = libvlc_VlmMediaInstanceStatusInit;
78 libvlc_VlmMediaInstanceStatusOpening;
82 libvlc_VlmMediaInstanceStatusPlaying;
85 libvlc_event.type = libvlc_VlmMediaInstanceStatusPause;
88 libvlc_event.type = libvlc_VlmMediaInstanceStatusEnd;
91 libvlc_event.type = libvlc_VlmMediaInstanceStatusError;
100 libvlc_event_send( p_event_manager, &libvlc_event );
104 static void libvlc_vlm_release_internal( libvlc_instance_t *p_instance )
106 vlm_t *p_vlm = p_instance->libvlc_vlm.p_vlm;
107 if( !p_instance->libvlc_vlm.p_vlm )
109 /* We need to remove medias in order to receive events */
110 vlm_Control( p_vlm, VLM_CLEAR_MEDIAS );
111 vlm_Control( p_vlm, VLM_CLEAR_SCHEDULES );
113 var_DelCallback( (vlc_object_t *)p_vlm, "intf-event", VlmEvent,
114 p_instance->libvlc_vlm.p_event_manager );
115 p_instance->libvlc_vlm.pf_release = NULL;
116 libvlc_event_manager_release( p_instance->libvlc_vlm.p_event_manager );
117 p_instance->libvlc_vlm.p_event_manager = NULL;
119 p_instance->libvlc_vlm.p_vlm = NULL;
122 static int libvlc_vlm_init( libvlc_instance_t *p_instance,
123 libvlc_exception_t *p_exception )
125 if( !p_instance->libvlc_vlm.p_event_manager )
127 p_instance->libvlc_vlm.p_event_manager =
128 libvlc_event_manager_new( p_instance->libvlc_vlm.p_vlm,
129 p_instance, p_exception );
130 libvlc_event_manager_register_event_type(
131 p_instance->libvlc_vlm.p_event_manager,
132 libvlc_VlmMediaAdded, NULL );
133 libvlc_event_manager_register_event_type(
134 p_instance->libvlc_vlm.p_event_manager,
135 libvlc_VlmMediaRemoved, NULL );
136 libvlc_event_manager_register_event_type(
137 p_instance->libvlc_vlm.p_event_manager,
138 libvlc_VlmMediaChanged, NULL );
139 libvlc_event_manager_register_event_type(
140 p_instance->libvlc_vlm.p_event_manager,
141 libvlc_VlmMediaInstanceStarted, NULL );
142 libvlc_event_manager_register_event_type(
143 p_instance->libvlc_vlm.p_event_manager,
144 libvlc_VlmMediaInstanceStopped, NULL );
145 libvlc_event_manager_register_event_type(
146 p_instance->libvlc_vlm.p_event_manager,
147 libvlc_VlmMediaInstanceStatusInit, NULL );
148 libvlc_event_manager_register_event_type(
149 p_instance->libvlc_vlm.p_event_manager,
150 libvlc_VlmMediaInstanceStatusOpening, NULL );
151 libvlc_event_manager_register_event_type(
152 p_instance->libvlc_vlm.p_event_manager,
153 libvlc_VlmMediaInstanceStatusPlaying, NULL );
154 libvlc_event_manager_register_event_type(
155 p_instance->libvlc_vlm.p_event_manager,
156 libvlc_VlmMediaInstanceStatusPause, NULL );
157 libvlc_event_manager_register_event_type(
158 p_instance->libvlc_vlm.p_event_manager,
159 libvlc_VlmMediaInstanceStatusEnd, NULL );
160 libvlc_event_manager_register_event_type(
161 p_instance->libvlc_vlm.p_event_manager,
162 libvlc_VlmMediaInstanceStatusError, NULL );
165 if( !p_instance->libvlc_vlm.p_vlm )
167 p_instance->libvlc_vlm.p_vlm = vlm_New( p_instance->p_libvlc_int );
168 if( !p_instance->libvlc_vlm.p_vlm )
170 libvlc_exception_raise( p_exception,
171 "Unable to create VLM." );
174 var_AddCallback( (vlc_object_t *)p_instance->libvlc_vlm.p_vlm,
175 "intf-event", VlmEvent,
176 p_instance->libvlc_vlm.p_event_manager );
177 p_instance->libvlc_vlm.pf_release = libvlc_vlm_release_internal;
183 void libvlc_vlm_release( libvlc_instance_t *p_instance,
184 libvlc_exception_t *p_exception)
186 libvlc_vlm_release_internal( p_instance );
189 #define VLM_RET(p,ret) do { \
190 if( libvlc_vlm_init( p_instance, p_exception ) ) return ret;\
191 (p) = p_instance->libvlc_vlm.p_vlm; \
193 #define VLM(p) VLM_RET(p,)
195 static vlm_media_instance_t *
196 libvlc_vlm_get_media_instance( libvlc_instance_t *p_instance,
197 const char *psz_name, int i_minstance_idx,
198 libvlc_exception_t *p_exception )
201 vlm_media_instance_t **pp_minstance;
202 vlm_media_instance_t *p_minstance;
206 VLM_RET(p_vlm, NULL);
208 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
209 vlm_Control( p_vlm, VLM_GET_MEDIA_INSTANCES, id, &pp_minstance,
212 libvlc_exception_raise( p_exception, "Unable to get %s instances",
217 if( i_minstance_idx >= 0 && i_minstance_idx < i_minstance )
219 p_minstance = pp_minstance[i_minstance_idx];
220 TAB_REMOVE( i_minstance, pp_minstance, p_minstance );
222 while( i_minstance > 0 )
223 vlm_media_instance_Delete( pp_minstance[--i_minstance] );
224 TAB_CLEAN( i_minstance, pp_minstance );
228 /* local function to be used in libvlc_vlm_show_media only */
229 static char* recurse_answer( vlm_message_t *p_answer, const char* psz_delim,
231 char* psz_childdelim = NULL;
232 char* psz_nametag = NULL;
233 char* psz_response = strdup( "" );
236 vlm_message_t *aw_child, **paw_child;
238 i_success = asprintf( &psz_childdelim, "%s\t", psz_delim);
240 /* starting with the children of root node */
241 if( i_success != -1 && p_answer->i_child )
243 paw_child = p_answer->child;
244 aw_child = *( paw_child );
245 /* Iterate over children */
246 for( i = 0; i < p_answer->i_child; i++ )
248 /* Spare comma if it is the last element */
250 if( i == (p_answer->i_child - 1) )
253 /* Append name of child node, if not in a list */
256 i_success = asprintf( &psz_response, "%s\"%s\": ",
257 psz_response, aw_child->psz_name );
258 if( i_success == -1 ) break;
261 /* If child node has children, */
262 if( aw_child->i_child )
264 /* If the parent node is a list (hence the child node is
265 * inside a list), create a property of its name as if it
266 * had a name value node
270 i_success = asprintf( &psz_nametag, "\"name\": \"%s\",%s",
271 aw_child->psz_name, psz_childdelim );
272 if( i_success == -1 ) break;
276 psz_nametag = strdup( "" );
278 /* If the child is a list itself, format it accordingly and
279 * recurse through the child's children, telling them that
280 * they are inside a list.
282 if( strcmp( aw_child->psz_name, "media" ) == 0 ||
283 strcmp( aw_child->psz_name, "inputs" ) == 0 ||
284 strcmp( aw_child->psz_name, "options" ) == 0 )
286 i_success = asprintf( &psz_response, "%s[%s%s%s]%c%s",
287 psz_response, psz_childdelim,
288 recurse_answer( aw_child,
290 psz_delim, c_comma, psz_delim );
291 if( i_success == -1 ) break;
293 /* Not a list, so format the child as a JSON object and
294 * recurse through the child's children
298 i_success = asprintf( &psz_response, "%s{%s%s%s%s}%c%s",
299 psz_response, psz_childdelim, psz_nametag,
300 recurse_answer( aw_child,
302 psz_delim, c_comma, psz_delim );
303 if( i_success == -1 ) break;
306 /* Otherwise - when no children are present - the node is a
307 * value node. So print the value string
311 /* If value is equivalent to NULL, print it as null */
312 if( aw_child->psz_value == NULL
313 || strcmp( aw_child->psz_value, "(null)" ) == 0 )
315 i_success = asprintf( &psz_response, "%snull%c%s",
316 psz_response, c_comma, psz_delim );
317 if( i_success == -1 )
320 /* Otherwise print the value in quotation marks */
323 i_success = asprintf( &psz_response, "%s\"%s\"%c%s",
324 psz_response, aw_child->psz_value,
325 c_comma, psz_delim );
326 if( i_success == -1 ) break;
329 /* getting next child */
331 aw_child = *( paw_child );
335 free( psz_childdelim );
336 if( i_success == -1 )
338 free( psz_response );
339 psz_response = strdup( "" );
344 const char* libvlc_vlm_show_media( libvlc_instance_t *p_instance,
345 const char *psz_name,
346 libvlc_exception_t *p_exception )
348 char *psz_message = NULL;
349 vlm_message_t *answer = NULL;
350 char *psz_response = NULL;
351 const char *psz_fmt = NULL;
352 const char *psz_delimiter = NULL;
356 VLM_RET(p_vlm, NULL);
358 if( psz_name == NULL )
360 libvlc_exception_raise( p_exception, "No media name supplied" );
362 else if( asprintf( &psz_message, "show %s", psz_name ) == -1 )
364 libvlc_exception_raise( p_exception, "Unable to call show %s",
369 vlm_ExecuteCommand( p_vlm, psz_message, &answer );
370 if( answer->psz_value )
372 libvlc_exception_raise( p_exception, "Unable to call show %s: %s",
373 psz_name, answer->psz_value );
375 else if ( answer->child ) {
376 /* in case everything was requested */
377 if ( strcmp( psz_name, "" ) == 0 )
379 psz_fmt = "{\n\t%s\n}\n";
380 psz_delimiter = "\n\t";
386 psz_delimiter = "\n";
389 if( asprintf( &psz_response, psz_fmt,
390 recurse_answer( answer, psz_delimiter, i_list ) )
393 libvlc_exception_raise( p_exception, "Error in show %s",
399 return( psz_response );
403 void libvlc_vlm_add_broadcast( libvlc_instance_t *p_instance,
404 const char *psz_name,
405 const char *psz_input,
406 const char *psz_output, int i_options,
407 const char * const *ppsz_options,
408 int b_enabled, int b_loop,
409 libvlc_exception_t *p_exception )
417 vlm_media_Init( &m );
418 m.psz_name = strdup( psz_name );
419 m.b_enabled = b_enabled;
421 m.broadcast.b_loop = b_loop;
423 TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
425 m.psz_output = strdup( psz_output );
426 for( n = 0; n < i_options; n++ )
427 TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
429 n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
430 vlm_media_Clean( &m );
432 libvlc_exception_raise( p_exception, "Media %s creation failed",
436 void libvlc_vlm_add_vod( libvlc_instance_t *p_instance, const char *psz_name,
437 const char *psz_input, int i_options,
438 const char * const *ppsz_options, int b_enabled,
439 const char *psz_mux, libvlc_exception_t *p_exception )
447 vlm_media_Init( &m );
448 m.psz_name = strdup( psz_name );
449 m.b_enabled = b_enabled;
451 m.vod.psz_mux = psz_mux ? strdup( psz_mux ) : NULL;
453 TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
454 for( n = 0; n < i_options; n++ )
455 TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
457 n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
458 vlm_media_Clean( &m );
460 libvlc_exception_raise( p_exception, "Media %s creation failed",
464 void libvlc_vlm_del_media( libvlc_instance_t *p_instance, const char *psz_name,
465 libvlc_exception_t *p_exception )
472 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
473 vlm_Control( p_vlm, VLM_DEL_MEDIA, id ) )
475 libvlc_exception_raise( p_exception, "Unable to delete %s", psz_name );
479 #define VLM_CHANGE(psz_error, code ) do { \
480 vlm_media_t *p_media; \
484 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) || \
485 vlm_Control( p_vlm, VLM_GET_MEDIA, id, &p_media ) ) { \
486 libvlc_exception_raise( p_exception, psz_error, psz_name ); \
489 if( !p_media ) goto error; \
493 if( vlm_Control( p_vlm, VLM_CHANGE_MEDIA, p_media ) ) { \
494 vlm_media_Delete( p_media ); \
497 vlm_media_Delete( p_media ); \
500 libvlc_exception_raise( p_exception, psz_error, psz_name );\
503 void libvlc_vlm_set_enabled( libvlc_instance_t *p_instance,
504 const char *psz_name, int b_enabled,
505 libvlc_exception_t *p_exception )
507 #define VLM_CHANGE_CODE { p_media->b_enabled = b_enabled; }
508 VLM_CHANGE( "Unable to delete %s", VLM_CHANGE_CODE );
509 #undef VLM_CHANGE_CODE
512 void libvlc_vlm_set_loop( libvlc_instance_t *p_instance, const char *psz_name,
513 int b_loop, libvlc_exception_t *p_exception )
515 #define VLM_CHANGE_CODE { p_media->broadcast.b_loop = b_loop; }
516 VLM_CHANGE( "Unable to change %s loop property", VLM_CHANGE_CODE );
517 #undef VLM_CHANGE_CODE
520 void libvlc_vlm_set_mux( libvlc_instance_t *p_instance, const char *psz_name,
521 const char *psz_mux, libvlc_exception_t *p_exception )
523 #define VLM_CHANGE_CODE { if( p_media->b_vod ) { \
524 free( p_media->vod.psz_mux ); \
525 p_media->vod.psz_mux = psz_mux \
526 ? strdup( psz_mux ) : NULL; \
528 VLM_CHANGE( "Unable to change %s mux property", VLM_CHANGE_CODE );
529 #undef VLM_CHANGE_CODE
532 void libvlc_vlm_set_output( libvlc_instance_t *p_instance,
533 const char *psz_name, const char *psz_output,
534 libvlc_exception_t *p_exception )
536 #define VLM_CHANGE_CODE { free( p_media->psz_output ); \
537 p_media->psz_output = strdup( psz_output ); }
538 VLM_CHANGE( "Unable to change %s output property", VLM_CHANGE_CODE );
539 #undef VLM_CHANGE_CODE
542 void libvlc_vlm_set_input( libvlc_instance_t *p_instance,
543 const char *psz_name, const char *psz_input,
544 libvlc_exception_t *p_exception )
546 #define VLM_CHANGE_CODE { while( p_media->i_input > 0 ) \
547 free( p_media->ppsz_input[--p_media->i_input] );\
548 TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
549 strdup(psz_input) ); }
550 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
551 #undef VLM_CHANGE_CODE
554 void libvlc_vlm_add_input( libvlc_instance_t *p_instance,
555 const char *psz_name, const char *psz_input,
556 libvlc_exception_t *p_exception )
558 #define VLM_CHANGE_CODE { TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
559 strdup(psz_input) ); }
560 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
561 #undef VLM_CHANGE_CODE
564 void libvlc_vlm_change_media( libvlc_instance_t *p_instance,
565 const char *psz_name, const char *psz_input,
566 const char *psz_output, int i_options,
567 const char * const *ppsz_options, int b_enabled,
568 int b_loop, libvlc_exception_t *p_exception )
570 #define VLM_CHANGE_CODE { int n; \
571 p_media->b_enabled = b_enabled; \
572 p_media->broadcast.b_loop = b_loop; \
573 while( p_media->i_input > 0 ) \
574 free( p_media->ppsz_input[--p_media->i_input] ); \
576 TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); \
577 free( p_media->psz_output ); \
578 p_media->psz_output = psz_output ? strdup( psz_output ) : NULL; \
579 while( p_media->i_option > 0 ) \
580 free( p_media->ppsz_option[--p_media->i_option] ); \
581 for( n = 0; n < i_options; n++ ) \
582 TAB_APPEND( p_media->i_option, p_media->ppsz_option, \
583 strdup(ppsz_options[n]) ); \
585 VLM_CHANGE( "Unable to change %s properties", VLM_CHANGE_CODE );
586 #undef VLM_CHANGE_CODE
589 void libvlc_vlm_play_media( libvlc_instance_t *p_instance,
590 const char *psz_name,
591 libvlc_exception_t *p_exception )
598 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
599 vlm_Control( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, id, NULL, 0 ) )
601 libvlc_exception_raise( p_exception, "Unable to play %s", psz_name );
605 void libvlc_vlm_stop_media( libvlc_instance_t *p_instance,
606 const char *psz_name,
607 libvlc_exception_t *p_exception )
614 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
615 vlm_Control( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, NULL ) )
617 libvlc_exception_raise( p_exception, "Unable to stop %s", psz_name );
621 void libvlc_vlm_pause_media( libvlc_instance_t *p_instance,
622 const char *psz_name,
623 libvlc_exception_t *p_exception )
630 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
631 vlm_Control( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, id, NULL ) )
633 libvlc_exception_raise( p_exception, "Unable to pause %s", psz_name );
637 void libvlc_vlm_seek_media( libvlc_instance_t *p_instance,
638 const char *psz_name, float f_percentage,
639 libvlc_exception_t *p_exception )
646 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
647 vlm_Control( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, NULL,
649 libvlc_exception_raise( p_exception, "Unable to seek %s to %f",
650 psz_name, f_percentage );
653 float libvlc_vlm_get_media_instance_position( libvlc_instance_t *p_instance,
654 const char *psz_name,
656 libvlc_exception_t *p_exception )
658 vlm_media_instance_t *p_mi;
661 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
662 i_instance, p_exception );
665 result = p_mi->d_position;
666 vlm_media_instance_Delete( p_mi );
671 int libvlc_vlm_get_media_instance_time( libvlc_instance_t *p_instance,
672 const char *psz_name, int i_instance,
673 libvlc_exception_t *p_exception )
675 vlm_media_instance_t *p_mi;
678 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
679 i_instance, p_exception );
682 result = p_mi->i_time;
683 vlm_media_instance_Delete( p_mi );
688 int libvlc_vlm_get_media_instance_length( libvlc_instance_t *p_instance,
689 const char *psz_name,
691 libvlc_exception_t *p_exception )
693 vlm_media_instance_t *p_mi;
696 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
697 i_instance, p_exception );
700 result = p_mi->i_length;
701 vlm_media_instance_Delete( p_mi );
706 int libvlc_vlm_get_media_instance_rate( libvlc_instance_t *p_instance,
707 const char *psz_name, int i_instance,
708 libvlc_exception_t *p_exception )
710 vlm_media_instance_t *p_mi;
713 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
714 i_instance, p_exception );
717 result = p_mi->i_rate;
718 vlm_media_instance_Delete( p_mi );
723 int libvlc_vlm_get_media_instance_title( libvlc_instance_t *p_instance,
724 const char *psz_name, int i_instance,
725 libvlc_exception_t *p_exception )
727 vlm_media_instance_t *p_mi;
729 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
730 i_instance, p_exception );
732 vlm_media_instance_Delete( p_mi );
733 return p_mi ? 0 : -1;
736 int libvlc_vlm_get_media_instance_chapter( libvlc_instance_t *p_instance,
737 const char *psz_name,
739 libvlc_exception_t *p_exception )
741 vlm_media_instance_t *p_mi;
743 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
744 i_instance, p_exception );
746 vlm_media_instance_Delete( p_mi );
747 return p_mi ? 0 : -1;
750 int libvlc_vlm_get_media_instance_seekable( libvlc_instance_t *p_instance,
751 const char *psz_name,
753 libvlc_exception_t *p_exception )
755 vlm_media_instance_t *p_mi;
757 p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
758 i_instance, p_exception );
760 vlm_media_instance_Delete( p_mi );
761 return p_mi ? 0 : -1;
764 libvlc_event_manager_t * libvlc_vlm_get_event_manager( libvlc_instance_t *p_instance,
765 libvlc_exception_t *p_exception )
768 VLM_RET( p_vlm, NULL);
769 return p_instance->libvlc_vlm.p_event_manager;