1 /*****************************************************************************
2 * vlm.c: libvlc new API VLM handling functions
3 *****************************************************************************
4 * Copyright (C) 2005 VLC authors and VideoLAN
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * 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>
35 #include "libvlc_internal.h"
37 typedef struct libvlc_vlm_t
39 libvlc_event_manager_t event_manager
;
43 /* VLM events callback. Transmit to libvlc */
44 static int VlmEvent( vlc_object_t
*p_this
, const char * name
,
45 vlc_value_t old_val
, vlc_value_t newval
, void *param
)
50 vlm_event_t
*event
= (vlm_event_t
*)newval
.p_address
;
51 libvlc_event_manager_t
*p_event_manager
= (libvlc_event_manager_t
*) param
;
52 libvlc_event_t libvlc_event
;
54 libvlc_event
.u
.vlm_media_event
.psz_instance_name
= NULL
;
55 libvlc_event
.u
.vlm_media_event
.psz_media_name
= event
->psz_name
;
57 switch( event
->i_type
)
59 case VLM_EVENT_MEDIA_ADDED
:
60 libvlc_event
.type
= libvlc_VlmMediaAdded
;
62 case VLM_EVENT_MEDIA_REMOVED
:
63 libvlc_event
.type
= libvlc_VlmMediaRemoved
;
65 case VLM_EVENT_MEDIA_CHANGED
:
66 libvlc_event
.type
= libvlc_VlmMediaChanged
;
68 case VLM_EVENT_MEDIA_INSTANCE_STARTED
:
69 libvlc_event
.type
= libvlc_VlmMediaInstanceStarted
;
71 case VLM_EVENT_MEDIA_INSTANCE_STOPPED
:
72 libvlc_event
.type
= libvlc_VlmMediaInstanceStopped
;
74 case VLM_EVENT_MEDIA_INSTANCE_STATE
:
75 libvlc_event
.u
.vlm_media_event
.psz_instance_name
=
76 event
->psz_instance_name
;
77 switch( event
->input_state
)
80 libvlc_event
.type
= libvlc_VlmMediaInstanceStatusInit
;
84 libvlc_VlmMediaInstanceStatusOpening
;
88 libvlc_VlmMediaInstanceStatusPlaying
;
91 libvlc_event
.type
= libvlc_VlmMediaInstanceStatusPause
;
94 libvlc_event
.type
= libvlc_VlmMediaInstanceStatusEnd
;
97 libvlc_event
.type
= libvlc_VlmMediaInstanceStatusError
;
106 libvlc_event_send( p_event_manager
, &libvlc_event
);
110 void libvlc_vlm_release( libvlc_instance_t
*p_instance
)
112 vlm_t
*p_vlm
= p_instance
->vlm
->p_vlm
;
115 /* We need to remove medias in order to receive events */
116 vlm_Control( p_vlm
, VLM_CLEAR_MEDIAS
);
117 vlm_Control( p_vlm
, VLM_CLEAR_SCHEDULES
);
119 var_DelCallback( (vlc_object_t
*)p_vlm
, "intf-event", VlmEvent
,
120 &p_instance
->vlm
->event_manager
);
121 libvlc_event_manager_destroy( &p_instance
->vlm
->event_manager
);
123 free( p_instance
->vlm
);
124 p_instance
->vlm
= NULL
;
125 libvlc_release( p_instance
);
128 static int libvlc_vlm_init( libvlc_instance_t
*p_instance
)
130 if( !p_instance
->vlm
)
132 p_instance
->vlm
= malloc( sizeof(*p_instance
->vlm
) );
133 if( p_instance
->vlm
== NULL
)
135 p_instance
->vlm
->p_vlm
= NULL
;
136 libvlc_event_manager_init( &p_instance
->vlm
->event_manager
,
140 if( !p_instance
->vlm
->p_vlm
)
142 p_instance
->vlm
->p_vlm
= vlm_New( p_instance
->p_libvlc_int
, NULL
);
143 if( !p_instance
->vlm
->p_vlm
)
145 libvlc_printerr( "VLM not supported or out of memory" );
148 var_AddCallback( (vlc_object_t
*)p_instance
->vlm
->p_vlm
,
149 "intf-event", VlmEvent
,
150 &p_instance
->vlm
->event_manager
);
151 libvlc_retain( p_instance
);
157 #define VLM_RET(p,ret) do { \
158 if( libvlc_vlm_init( p_instance ) ) \
160 (p) = p_instance->vlm->p_vlm; \
163 static vlm_media_instance_t
*
164 libvlc_vlm_get_media_instance( libvlc_instance_t
*p_instance
,
165 const char *psz_name
, int i_minstance_idx
)
168 vlm_media_instance_t
**pp_minstance
;
169 vlm_media_instance_t
*p_minstance
;
173 VLM_RET(p_vlm
, NULL
);
175 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, psz_name
, &id
) ||
176 vlm_Control( p_vlm
, VLM_GET_MEDIA_INSTANCES
, id
, &pp_minstance
,
179 libvlc_printerr( "%s: media instances not found", psz_name
);
183 if( i_minstance_idx
>= 0 && i_minstance_idx
< i_minstance
)
185 p_minstance
= pp_minstance
[i_minstance_idx
];
186 TAB_REMOVE( i_minstance
, pp_minstance
, p_minstance
);
188 while( i_minstance
> 0 )
189 vlm_media_instance_Delete( pp_minstance
[--i_minstance
] );
190 TAB_CLEAN( i_minstance
, pp_minstance
);
194 /* local function to be used in libvlc_vlm_show_media only */
195 static char* recurse_answer( vlm_message_t
*p_answer
, const char* psz_delim
,
197 char* psz_childdelim
= NULL
;
198 char* psz_nametag
= NULL
;
199 char* psz_response
= strdup( "" );
202 vlm_message_t
*aw_child
, **paw_child
;
204 i_success
= asprintf( &psz_childdelim
, "%s\t", psz_delim
);
205 if( i_success
== -1 )
208 paw_child
= p_answer
->child
;
209 aw_child
= *( paw_child
);
210 /* Iterate over children */
211 for( int i
= 0; i
< p_answer
->i_child
; i
++ )
214 /* Spare comma if it is the last element */
216 if( i
== (p_answer
->i_child
- 1) )
219 /* Append name of child node, if not in a list */
222 i_success
= asprintf( &psz_tmp
, "%s\"%s\": ",
223 psz_response
, aw_child
->psz_name
);
224 if( i_success
== -1 ) break;
225 free( psz_response
);
226 psz_response
= psz_tmp
;
229 /* If child node has children, */
230 if( aw_child
->i_child
)
232 /* If the parent node is a list (hence the child node is
233 * inside a list), create a property of its name as if it
234 * had a name value node
239 i_success
= asprintf( &psz_nametag
, "\"name\": \"%s\",%s",
240 aw_child
->psz_name
, psz_childdelim
);
241 if( i_success
== -1 )
249 psz_nametag
= strdup( "" );
251 /* If the child is a list itself, format it accordingly and
252 * recurse through the child's children, telling them that
253 * they are inside a list.
255 if( strcmp( aw_child
->psz_name
, "media" ) == 0 ||
256 strcmp( aw_child
->psz_name
, "inputs" ) == 0 ||
257 strcmp( aw_child
->psz_name
, "options" ) == 0 )
259 char *psz_recurse
= recurse_answer( aw_child
, psz_childdelim
, 1 );
260 i_success
= asprintf( &psz_tmp
, "%s[%s%s%s]%c%s",
261 psz_response
, psz_childdelim
, psz_recurse
,
262 psz_delim
, c_comma
, psz_delim
);
264 if( i_success
== -1 ) break;
265 free( psz_response
);
266 psz_response
= psz_tmp
;
268 /* Not a list, so format the child as a JSON object and
269 * recurse through the child's children
273 char *psz_recurse
= recurse_answer( aw_child
, psz_childdelim
, 0 );
274 i_success
= asprintf( &psz_tmp
, "%s{%s%s%s%s}%c%s",
275 psz_response
, psz_childdelim
, psz_nametag
,
276 psz_recurse
, psz_delim
, c_comma
, psz_delim
);
278 if( i_success
== -1 ) break;
279 free( psz_response
);
280 psz_response
= psz_tmp
;
283 /* Otherwise - when no children are present - the node is a
284 * value node. So print the value string
288 /* If value is equivalent to NULL, print it as null */
289 if( aw_child
->psz_value
== NULL
290 || strcmp( aw_child
->psz_value
, "(null)" ) == 0 )
292 i_success
= asprintf( &psz_tmp
, "%snull%c%s",
293 psz_response
, c_comma
, psz_delim
);
294 if( i_success
== -1 ) break;
295 free( psz_response
);
296 psz_response
= psz_tmp
;
298 /* Otherwise print the value in quotation marks */
301 i_success
= asprintf( &psz_tmp
, "%s\"%s\"%c%s",
302 psz_response
, aw_child
->psz_value
,
303 c_comma
, psz_delim
);
304 if( i_success
== -1 ) break;
305 free( psz_response
);
306 psz_response
= psz_tmp
;
309 /* getting next child */
311 aw_child
= *( paw_child
);
314 free( psz_childdelim
);
315 if( i_success
== -1 )
317 free( psz_response
);
318 psz_response
= strdup( "" );
323 const char* libvlc_vlm_show_media( libvlc_instance_t
*p_instance
,
324 const char *psz_name
)
326 char *psz_message
= NULL
;
327 vlm_message_t
*answer
= NULL
;
328 char *psz_response
= NULL
;
329 const char *psz_fmt
= NULL
;
330 const char *psz_delimiter
= NULL
;
334 VLM_RET(p_vlm
, NULL
);
338 if( asprintf( &psz_message
, "show %s", psz_name
) == -1 )
341 vlm_ExecuteCommand( p_vlm
, psz_message
, &answer
);
342 if( answer
->psz_value
)
344 libvlc_printerr( "Unable to call show %s: %s",
345 psz_name
, answer
->psz_value
);
347 else if ( answer
->child
)
348 { /* in case everything was requested */
349 if ( strcmp( psz_name
, "" ) == 0 )
351 psz_fmt
= "{\n\t%s\n}\n";
352 psz_delimiter
= "\n\t";
358 psz_delimiter
= "\n";
361 char *psz_tmp
= recurse_answer( answer
, psz_delimiter
, i_list
);
362 if( asprintf( &psz_response
, psz_fmt
, psz_tmp
) == -1 )
364 libvlc_printerr( "Out of memory" );
369 vlm_MessageDelete( answer
);
371 return( psz_response
);
375 int libvlc_vlm_add_broadcast( libvlc_instance_t
*p_instance
,
376 const char *psz_name
,
377 const char *psz_input
,
378 const char *psz_output
, int i_options
,
379 const char * const *ppsz_options
,
380 int b_enabled
, int b_loop
)
388 vlm_media_Init( &m
);
389 m
.psz_name
= strdup( psz_name
);
390 m
.b_enabled
= b_enabled
;
392 m
.broadcast
.b_loop
= b_loop
;
394 TAB_APPEND( m
.i_input
, m
.ppsz_input
, strdup(psz_input
) );
396 m
.psz_output
= strdup( psz_output
);
397 for( n
= 0; n
< i_options
; n
++ )
398 TAB_APPEND( m
.i_option
, m
.ppsz_option
, strdup(ppsz_options
[n
]) );
400 n
= vlm_Control( p_vlm
, VLM_ADD_MEDIA
, &m
, NULL
);
401 vlm_media_Clean( &m
);
404 libvlc_printerr( "Media %s creation failed", psz_name
);
410 int libvlc_vlm_add_vod( libvlc_instance_t
*p_instance
, const char *psz_name
,
411 const char *psz_input
, int i_options
,
412 const char * const *ppsz_options
, int b_enabled
,
413 const char *psz_mux
)
421 vlm_media_Init( &m
);
422 m
.psz_name
= strdup( psz_name
);
423 m
.b_enabled
= b_enabled
;
425 m
.vod
.psz_mux
= psz_mux
? strdup( psz_mux
) : NULL
;
427 TAB_APPEND( m
.i_input
, m
.ppsz_input
, strdup(psz_input
) );
428 for( n
= 0; n
< i_options
; n
++ )
429 TAB_APPEND( m
.i_option
, m
.ppsz_option
, strdup(ppsz_options
[n
]) );
431 n
= vlm_Control( p_vlm
, VLM_ADD_MEDIA
, &m
, NULL
);
432 vlm_media_Clean( &m
);
435 libvlc_printerr( "Media %s creation failed", psz_name
);
441 int libvlc_vlm_del_media( libvlc_instance_t
*p_instance
, const char *psz_name
)
448 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, psz_name
, &id
) ||
449 vlm_Control( p_vlm
, VLM_DEL_MEDIA
, id
) )
451 libvlc_printerr( "Unable to delete %s", psz_name
);
457 static vlm_media_t
*get_media( libvlc_instance_t
*p_instance
,
458 vlm_t
**restrict pp_vlm
, const char *name
)
460 vlm_media_t
*p_media
;
464 VLM_RET(p_vlm
, NULL
);
465 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, name
, &id
) ||
466 vlm_Control( p_vlm
, VLM_GET_MEDIA
, id
, &p_media
) )
472 #define VLM_CHANGE(psz_error, code ) do { \
474 vlm_media_t *p_media = get_media( p_instance, &p_vlm, psz_name ); \
475 if( p_media != NULL ) { \
477 if( vlm_Control( p_vlm, VLM_CHANGE_MEDIA, p_media ) ) \
479 vlm_media_Delete( p_media ); \
480 if( p_vlm != NULL ) \
483 libvlc_printerr( psz_error, psz_name ); \
487 int libvlc_vlm_set_enabled( libvlc_instance_t
*p_instance
,
488 const char *psz_name
, int b_enabled
)
490 #define VLM_CHANGE_CODE { p_media->b_enabled = b_enabled; }
491 VLM_CHANGE( "Unable to delete %s", VLM_CHANGE_CODE
);
492 #undef VLM_CHANGE_CODE
495 int libvlc_vlm_set_loop( libvlc_instance_t
*p_instance
, const char *psz_name
,
498 #define VLM_CHANGE_CODE { p_media->broadcast.b_loop = b_loop; }
499 VLM_CHANGE( "Unable to change %s loop property", VLM_CHANGE_CODE
);
500 #undef VLM_CHANGE_CODE
503 int libvlc_vlm_set_mux( libvlc_instance_t
*p_instance
, const char *psz_name
,
504 const char *psz_mux
)
506 #define VLM_CHANGE_CODE { if( p_media->b_vod ) { \
507 free( p_media->vod.psz_mux ); \
508 p_media->vod.psz_mux = psz_mux \
509 ? strdup( psz_mux ) : NULL; \
511 VLM_CHANGE( "Unable to change %s mux property", VLM_CHANGE_CODE
);
512 #undef VLM_CHANGE_CODE
515 int libvlc_vlm_set_output( libvlc_instance_t
*p_instance
,
516 const char *psz_name
, const char *psz_output
)
518 #define VLM_CHANGE_CODE { free( p_media->psz_output ); \
519 p_media->psz_output = strdup( psz_output ); }
520 VLM_CHANGE( "Unable to change %s output property", VLM_CHANGE_CODE
);
521 #undef VLM_CHANGE_CODE
524 int libvlc_vlm_set_input( libvlc_instance_t
*p_instance
,
525 const char *psz_name
, const char *psz_input
)
527 #define VLM_CHANGE_CODE { while( p_media->i_input > 0 ) \
528 free( p_media->ppsz_input[--p_media->i_input] );\
529 TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
530 strdup(psz_input) ); }
531 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE
);
532 #undef VLM_CHANGE_CODE
535 int libvlc_vlm_add_input( libvlc_instance_t
*p_instance
,
536 const char *psz_name
, const char *psz_input
)
538 #define VLM_CHANGE_CODE { TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
539 strdup(psz_input) ); }
540 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE
);
541 #undef VLM_CHANGE_CODE
544 int libvlc_vlm_change_media( libvlc_instance_t
*p_instance
,
545 const char *psz_name
, const char *psz_input
,
546 const char *psz_output
, int i_options
,
547 const char * const *ppsz_options
, int b_enabled
,
550 #define VLM_CHANGE_CODE { int n; \
551 p_media->b_enabled = b_enabled; \
552 p_media->broadcast.b_loop = b_loop; \
553 while( p_media->i_input > 0 ) \
554 free( p_media->ppsz_input[--p_media->i_input] ); \
556 TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); \
557 free( p_media->psz_output ); \
558 p_media->psz_output = psz_output ? strdup( psz_output ) : NULL; \
559 while( p_media->i_option > 0 ) \
560 free( p_media->ppsz_option[--p_media->i_option] ); \
561 for( n = 0; n < i_options; n++ ) \
562 TAB_APPEND( p_media->i_option, p_media->ppsz_option, \
563 strdup(ppsz_options[n]) ); \
565 VLM_CHANGE( "Unable to change %s properties", VLM_CHANGE_CODE
);
566 #undef VLM_CHANGE_CODE
569 int libvlc_vlm_play_media( libvlc_instance_t
*p_instance
,
570 const char *psz_name
)
577 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, psz_name
, &id
) ||
578 vlm_Control( p_vlm
, VLM_START_MEDIA_BROADCAST_INSTANCE
, id
, NULL
, 0 ) )
580 libvlc_printerr( "Unable to play %s", psz_name
);
586 int libvlc_vlm_stop_media( libvlc_instance_t
*p_instance
,
587 const char *psz_name
)
594 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, psz_name
, &id
) ||
595 vlm_Control( p_vlm
, VLM_STOP_MEDIA_INSTANCE
, id
, NULL
) )
597 libvlc_printerr( "Unable to stop %s", psz_name
);
603 int libvlc_vlm_pause_media( libvlc_instance_t
*p_instance
,
604 const char *psz_name
)
611 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, psz_name
, &id
) ||
612 vlm_Control( p_vlm
, VLM_PAUSE_MEDIA_INSTANCE
, id
, NULL
) )
614 libvlc_printerr( "Unable to pause %s", psz_name
);
620 int libvlc_vlm_seek_media( libvlc_instance_t
*p_instance
,
621 const char *psz_name
, float f_percentage
)
628 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, psz_name
, &id
) ||
629 vlm_Control( p_vlm
, VLM_SET_MEDIA_INSTANCE_POSITION
, id
, NULL
,
632 libvlc_printerr( "Unable to seek %s to %f%%", psz_name
, f_percentage
);
638 float libvlc_vlm_get_media_instance_position( libvlc_instance_t
*p_instance
,
639 const char *psz_name
,
642 vlm_media_instance_t
*p_mi
;
645 p_mi
= libvlc_vlm_get_media_instance( p_instance
, psz_name
, i_instance
);
648 result
= p_mi
->d_position
;
649 vlm_media_instance_Delete( p_mi
);
654 int libvlc_vlm_get_media_instance_time( libvlc_instance_t
*p_instance
,
655 const char *psz_name
, int i_instance
)
657 vlm_media_instance_t
*p_mi
;
660 p_mi
= libvlc_vlm_get_media_instance( p_instance
, psz_name
, i_instance
);
663 result
= p_mi
->i_time
;
664 vlm_media_instance_Delete( p_mi
);
669 int libvlc_vlm_get_media_instance_length( libvlc_instance_t
*p_instance
,
670 const char *psz_name
,
673 vlm_media_instance_t
*p_mi
;
676 p_mi
= libvlc_vlm_get_media_instance( p_instance
, psz_name
, i_instance
);
679 result
= p_mi
->i_length
;
680 vlm_media_instance_Delete( p_mi
);
685 int libvlc_vlm_get_media_instance_rate( libvlc_instance_t
*p_instance
,
686 const char *psz_name
, int i_instance
)
688 vlm_media_instance_t
*p_mi
;
691 p_mi
= libvlc_vlm_get_media_instance( p_instance
, psz_name
, i_instance
);
694 result
= p_mi
->i_rate
;
695 vlm_media_instance_Delete( p_mi
);
701 int libvlc_vlm_get_media_instance_title( libvlc_instance_t
*p_instance
,
702 const char *psz_name
, int i_instance
)
704 vlm_media_instance_t
*p_mi
;
706 p_mi
= libvlc_vlm_get_media_instance( p_instance
, psz_name
, i_instance
);
708 vlm_media_instance_Delete( p_mi
);
709 return p_mi
? 0 : -1;
712 int libvlc_vlm_get_media_instance_chapter( libvlc_instance_t
*p_instance
,
713 const char *psz_name
,
716 vlm_media_instance_t
*p_mi
;
718 p_mi
= libvlc_vlm_get_media_instance( p_instance
, psz_name
,
721 vlm_media_instance_Delete( p_mi
);
722 return p_mi
? 0 : -1;
725 int libvlc_vlm_get_media_instance_seekable( libvlc_instance_t
*p_instance
,
726 const char *psz_name
,
729 vlm_media_instance_t
*p_mi
;
731 p_mi
= libvlc_vlm_get_media_instance( p_instance
, psz_name
, i_instance
);
733 vlm_media_instance_Delete( p_mi
);
734 return p_mi
? 0 : -1;
738 libvlc_event_manager_t
*
739 libvlc_vlm_get_event_manager( libvlc_instance_t
*p_instance
)
741 if( libvlc_vlm_init( p_instance
) )
743 return &p_instance
->vlm
->event_manager
;