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 /* VLM events callback. Transmit to libvlc */
38 static int VlmEvent( vlc_object_t
*p_this
, const char * name
,
39 vlc_value_t old_val
, vlc_value_t newval
, void *param
)
44 vlm_event_t
*event
= (vlm_event_t
*)newval
.p_address
;
45 libvlc_event_manager_t
*p_event_manager
= (libvlc_event_manager_t
*) param
;
46 libvlc_event_t libvlc_event
;
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
;
120 libvlc_release( p_instance
);
123 static int libvlc_vlm_init( libvlc_instance_t
*p_instance
)
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 if( unlikely(p_instance
->libvlc_vlm
.p_event_manager
== NULL
) )
133 if( !p_instance
->libvlc_vlm
.p_vlm
)
135 p_instance
->libvlc_vlm
.p_vlm
= vlm_New( p_instance
->p_libvlc_int
);
136 if( !p_instance
->libvlc_vlm
.p_vlm
)
138 libvlc_printerr( "VLM not supported or out of memory" );
141 var_AddCallback( (vlc_object_t
*)p_instance
->libvlc_vlm
.p_vlm
,
142 "intf-event", VlmEvent
,
143 p_instance
->libvlc_vlm
.p_event_manager
);
144 p_instance
->libvlc_vlm
.pf_release
= libvlc_vlm_release_internal
;
145 libvlc_retain( p_instance
);
151 void libvlc_vlm_release( libvlc_instance_t
*p_instance
)
153 libvlc_vlm_release_internal( p_instance
);
156 #define VLM_RET(p,ret) do { \
157 if( libvlc_vlm_init( p_instance ) ) \
159 (p) = p_instance->libvlc_vlm.p_vlm; \
162 static vlm_media_instance_t
*
163 libvlc_vlm_get_media_instance( libvlc_instance_t
*p_instance
,
164 const char *psz_name
, int i_minstance_idx
)
167 vlm_media_instance_t
**pp_minstance
;
168 vlm_media_instance_t
*p_minstance
;
172 VLM_RET(p_vlm
, NULL
);
174 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, psz_name
, &id
) ||
175 vlm_Control( p_vlm
, VLM_GET_MEDIA_INSTANCES
, id
, &pp_minstance
,
178 libvlc_printerr( "%s: media instances not found", psz_name
);
182 if( i_minstance_idx
>= 0 && i_minstance_idx
< i_minstance
)
184 p_minstance
= pp_minstance
[i_minstance_idx
];
185 TAB_REMOVE( i_minstance
, pp_minstance
, p_minstance
);
187 while( i_minstance
> 0 )
188 vlm_media_instance_Delete( pp_minstance
[--i_minstance
] );
189 TAB_CLEAN( i_minstance
, pp_minstance
);
193 /* local function to be used in libvlc_vlm_show_media only */
194 static char* recurse_answer( vlm_message_t
*p_answer
, const char* psz_delim
,
196 char* psz_childdelim
= NULL
;
197 char* psz_nametag
= NULL
;
198 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( i
= 0; i
< p_answer
->i_child
; i
++ )
213 /* Spare comma if it is the last element */
215 if( i
== (p_answer
->i_child
- 1) )
218 /* Append name of child node, if not in a list */
221 i_success
= asprintf( &psz_tmp
, "%s\"%s\": ",
222 psz_response
, aw_child
->psz_name
);
223 if( i_success
== -1 ) break;
224 free( psz_response
);
225 psz_response
= psz_tmp
;
228 /* If child node has children, */
229 if( aw_child
->i_child
)
231 /* If the parent node is a list (hence the child node is
232 * inside a list), create a property of its name as if it
233 * had a name value node
238 i_success
= asprintf( &psz_nametag
, "\"name\": \"%s\",%s",
239 aw_child
->psz_name
, psz_childdelim
);
240 if( i_success
== -1 )
248 psz_nametag
= strdup( "" );
250 /* If the child is a list itself, format it accordingly and
251 * recurse through the child's children, telling them that
252 * they are inside a list.
254 if( strcmp( aw_child
->psz_name
, "media" ) == 0 ||
255 strcmp( aw_child
->psz_name
, "inputs" ) == 0 ||
256 strcmp( aw_child
->psz_name
, "options" ) == 0 )
258 char *psz_recurse
= recurse_answer( aw_child
, psz_childdelim
, 1 );
259 i_success
= asprintf( &psz_tmp
, "%s[%s%s%s]%c%s",
260 psz_response
, psz_childdelim
, psz_recurse
,
261 psz_delim
, c_comma
, psz_delim
);
263 if( i_success
== -1 ) break;
264 free( psz_response
);
265 psz_response
= psz_tmp
;
267 /* Not a list, so format the child as a JSON object and
268 * recurse through the child's children
272 char *psz_recurse
= recurse_answer( aw_child
, psz_childdelim
, 0 );
273 i_success
= asprintf( &psz_tmp
, "%s{%s%s%s%s}%c%s",
274 psz_response
, psz_childdelim
, psz_nametag
,
275 psz_recurse
, psz_delim
, c_comma
, psz_delim
);
277 if( i_success
== -1 ) break;
278 free( psz_response
);
279 psz_response
= psz_tmp
;
282 /* Otherwise - when no children are present - the node is a
283 * value node. So print the value string
287 /* If value is equivalent to NULL, print it as null */
288 if( aw_child
->psz_value
== NULL
289 || strcmp( aw_child
->psz_value
, "(null)" ) == 0 )
291 i_success
= asprintf( &psz_tmp
, "%snull%c%s",
292 psz_response
, c_comma
, psz_delim
);
293 if( i_success
== -1 ) break;
294 free( psz_response
);
295 psz_response
= psz_tmp
;
297 /* Otherwise print the value in quotation marks */
300 i_success
= asprintf( &psz_tmp
, "%s\"%s\"%c%s",
301 psz_response
, aw_child
->psz_value
,
302 c_comma
, psz_delim
);
303 if( i_success
== -1 ) break;
304 free( psz_response
);
305 psz_response
= psz_tmp
;
308 /* getting next child */
310 aw_child
= *( paw_child
);
313 free( psz_childdelim
);
314 if( i_success
== -1 )
316 free( psz_response
);
317 psz_response
= strdup( "" );
322 const char* libvlc_vlm_show_media( libvlc_instance_t
*p_instance
,
323 const char *psz_name
)
325 char *psz_message
= NULL
;
326 vlm_message_t
*answer
= NULL
;
327 char *psz_response
= NULL
;
328 const char *psz_fmt
= NULL
;
329 const char *psz_delimiter
= NULL
;
333 VLM_RET(p_vlm
, NULL
);
337 if( asprintf( &psz_message
, "show %s", psz_name
) == -1 )
340 vlm_ExecuteCommand( p_vlm
, psz_message
, &answer
);
341 if( answer
->psz_value
)
343 libvlc_printerr( "Unable to call show %s: %s",
344 psz_name
, answer
->psz_value
);
346 else if ( answer
->child
)
347 { /* in case everything was requested */
348 if ( strcmp( psz_name
, "" ) == 0 )
350 psz_fmt
= "{\n\t%s\n}\n";
351 psz_delimiter
= "\n\t";
357 psz_delimiter
= "\n";
360 char *psz_tmp
= recurse_answer( answer
, psz_delimiter
, i_list
);
361 if( asprintf( &psz_response
, psz_fmt
, psz_tmp
) == -1 )
363 libvlc_printerr( "Out of memory" );
368 vlm_MessageDelete( answer
);
370 return( psz_response
);
374 int libvlc_vlm_add_broadcast( libvlc_instance_t
*p_instance
,
375 const char *psz_name
,
376 const char *psz_input
,
377 const char *psz_output
, int i_options
,
378 const char * const *ppsz_options
,
379 int b_enabled
, int b_loop
)
387 vlm_media_Init( &m
);
388 m
.psz_name
= strdup( psz_name
);
389 m
.b_enabled
= b_enabled
;
391 m
.broadcast
.b_loop
= b_loop
;
393 TAB_APPEND( m
.i_input
, m
.ppsz_input
, strdup(psz_input
) );
395 m
.psz_output
= strdup( psz_output
);
396 for( n
= 0; n
< i_options
; n
++ )
397 TAB_APPEND( m
.i_option
, m
.ppsz_option
, strdup(ppsz_options
[n
]) );
399 n
= vlm_Control( p_vlm
, VLM_ADD_MEDIA
, &m
, NULL
);
400 vlm_media_Clean( &m
);
403 libvlc_printerr( "Media %s creation failed", psz_name
);
409 int libvlc_vlm_add_vod( libvlc_instance_t
*p_instance
, const char *psz_name
,
410 const char *psz_input
, int i_options
,
411 const char * const *ppsz_options
, int b_enabled
,
412 const char *psz_mux
)
420 vlm_media_Init( &m
);
421 m
.psz_name
= strdup( psz_name
);
422 m
.b_enabled
= b_enabled
;
424 m
.vod
.psz_mux
= psz_mux
? strdup( psz_mux
) : NULL
;
426 TAB_APPEND( m
.i_input
, m
.ppsz_input
, strdup(psz_input
) );
427 for( n
= 0; n
< i_options
; n
++ )
428 TAB_APPEND( m
.i_option
, m
.ppsz_option
, strdup(ppsz_options
[n
]) );
430 n
= vlm_Control( p_vlm
, VLM_ADD_MEDIA
, &m
, NULL
);
431 vlm_media_Clean( &m
);
434 libvlc_printerr( "Media %s creation failed", psz_name
);
440 int libvlc_vlm_del_media( libvlc_instance_t
*p_instance
, const char *psz_name
)
447 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, psz_name
, &id
) ||
448 vlm_Control( p_vlm
, VLM_DEL_MEDIA
, id
) )
450 libvlc_printerr( "Unable to delete %s", psz_name
);
456 static vlm_media_t
*get_media( libvlc_instance_t
*p_instance
,
457 vlm_t
**restrict pp_vlm
, const char *name
)
459 vlm_media_t
*p_media
;
463 VLM_RET(p_vlm
, NULL
);
464 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, name
, &id
) ||
465 vlm_Control( p_vlm
, VLM_GET_MEDIA
, id
, &p_media
) )
471 #define VLM_CHANGE(psz_error, code ) do { \
473 vlm_media_t *p_media = get_media( p_instance, &p_vlm, psz_name ); \
474 if( p_media != NULL ) { \
476 if( vlm_Control( p_vlm, VLM_CHANGE_MEDIA, p_media ) ) \
478 vlm_media_Delete( p_media ); \
479 if( p_vlm != NULL ) \
482 libvlc_printerr( psz_error, psz_name ); \
486 int libvlc_vlm_set_enabled( libvlc_instance_t
*p_instance
,
487 const char *psz_name
, int b_enabled
)
489 #define VLM_CHANGE_CODE { p_media->b_enabled = b_enabled; }
490 VLM_CHANGE( "Unable to delete %s", VLM_CHANGE_CODE
);
491 #undef VLM_CHANGE_CODE
494 int libvlc_vlm_set_loop( libvlc_instance_t
*p_instance
, const char *psz_name
,
497 #define VLM_CHANGE_CODE { p_media->broadcast.b_loop = b_loop; }
498 VLM_CHANGE( "Unable to change %s loop property", VLM_CHANGE_CODE
);
499 #undef VLM_CHANGE_CODE
502 int libvlc_vlm_set_mux( libvlc_instance_t
*p_instance
, const char *psz_name
,
503 const char *psz_mux
)
505 #define VLM_CHANGE_CODE { if( p_media->b_vod ) { \
506 free( p_media->vod.psz_mux ); \
507 p_media->vod.psz_mux = psz_mux \
508 ? strdup( psz_mux ) : NULL; \
510 VLM_CHANGE( "Unable to change %s mux property", VLM_CHANGE_CODE
);
511 #undef VLM_CHANGE_CODE
514 int libvlc_vlm_set_output( libvlc_instance_t
*p_instance
,
515 const char *psz_name
, const char *psz_output
)
517 #define VLM_CHANGE_CODE { free( p_media->psz_output ); \
518 p_media->psz_output = strdup( psz_output ); }
519 VLM_CHANGE( "Unable to change %s output property", VLM_CHANGE_CODE
);
520 #undef VLM_CHANGE_CODE
523 int libvlc_vlm_set_input( libvlc_instance_t
*p_instance
,
524 const char *psz_name
, const char *psz_input
)
526 #define VLM_CHANGE_CODE { while( p_media->i_input > 0 ) \
527 free( p_media->ppsz_input[--p_media->i_input] );\
528 TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
529 strdup(psz_input) ); }
530 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE
);
531 #undef VLM_CHANGE_CODE
534 int libvlc_vlm_add_input( libvlc_instance_t
*p_instance
,
535 const char *psz_name
, const char *psz_input
)
537 #define VLM_CHANGE_CODE { TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
538 strdup(psz_input) ); }
539 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE
);
540 #undef VLM_CHANGE_CODE
543 int libvlc_vlm_change_media( libvlc_instance_t
*p_instance
,
544 const char *psz_name
, const char *psz_input
,
545 const char *psz_output
, int i_options
,
546 const char * const *ppsz_options
, int b_enabled
,
549 #define VLM_CHANGE_CODE { int n; \
550 p_media->b_enabled = b_enabled; \
551 p_media->broadcast.b_loop = b_loop; \
552 while( p_media->i_input > 0 ) \
553 free( p_media->ppsz_input[--p_media->i_input] ); \
555 TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); \
556 free( p_media->psz_output ); \
557 p_media->psz_output = psz_output ? strdup( psz_output ) : NULL; \
558 while( p_media->i_option > 0 ) \
559 free( p_media->ppsz_option[--p_media->i_option] ); \
560 for( n = 0; n < i_options; n++ ) \
561 TAB_APPEND( p_media->i_option, p_media->ppsz_option, \
562 strdup(ppsz_options[n]) ); \
564 VLM_CHANGE( "Unable to change %s properties", VLM_CHANGE_CODE
);
565 #undef VLM_CHANGE_CODE
568 int libvlc_vlm_play_media( libvlc_instance_t
*p_instance
,
569 const char *psz_name
)
576 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, psz_name
, &id
) ||
577 vlm_Control( p_vlm
, VLM_START_MEDIA_BROADCAST_INSTANCE
, id
, NULL
, 0 ) )
579 libvlc_printerr( "Unable to play %s", psz_name
);
585 int libvlc_vlm_stop_media( libvlc_instance_t
*p_instance
,
586 const char *psz_name
)
593 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, psz_name
, &id
) ||
594 vlm_Control( p_vlm
, VLM_STOP_MEDIA_INSTANCE
, id
, NULL
) )
596 libvlc_printerr( "Unable to stop %s", psz_name
);
602 int libvlc_vlm_pause_media( libvlc_instance_t
*p_instance
,
603 const char *psz_name
)
610 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, psz_name
, &id
) ||
611 vlm_Control( p_vlm
, VLM_PAUSE_MEDIA_INSTANCE
, id
, NULL
) )
613 libvlc_printerr( "Unable to pause %s", psz_name
);
619 int libvlc_vlm_seek_media( libvlc_instance_t
*p_instance
,
620 const char *psz_name
, float f_percentage
)
627 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, psz_name
, &id
) ||
628 vlm_Control( p_vlm
, VLM_SET_MEDIA_INSTANCE_POSITION
, id
, NULL
,
631 libvlc_printerr( "Unable to seek %s to %f%%", psz_name
, f_percentage
);
637 float libvlc_vlm_get_media_instance_position( libvlc_instance_t
*p_instance
,
638 const char *psz_name
,
641 vlm_media_instance_t
*p_mi
;
644 p_mi
= libvlc_vlm_get_media_instance( p_instance
, psz_name
, i_instance
);
647 result
= p_mi
->d_position
;
648 vlm_media_instance_Delete( p_mi
);
653 int libvlc_vlm_get_media_instance_time( libvlc_instance_t
*p_instance
,
654 const char *psz_name
, int i_instance
)
656 vlm_media_instance_t
*p_mi
;
659 p_mi
= libvlc_vlm_get_media_instance( p_instance
, psz_name
, i_instance
);
662 result
= p_mi
->i_time
;
663 vlm_media_instance_Delete( p_mi
);
668 int libvlc_vlm_get_media_instance_length( libvlc_instance_t
*p_instance
,
669 const char *psz_name
,
672 vlm_media_instance_t
*p_mi
;
675 p_mi
= libvlc_vlm_get_media_instance( p_instance
, psz_name
, i_instance
);
678 result
= p_mi
->i_length
;
679 vlm_media_instance_Delete( p_mi
);
684 int libvlc_vlm_get_media_instance_rate( libvlc_instance_t
*p_instance
,
685 const char *psz_name
, int i_instance
)
687 vlm_media_instance_t
*p_mi
;
690 p_mi
= libvlc_vlm_get_media_instance( p_instance
, psz_name
, i_instance
);
693 result
= p_mi
->i_rate
;
694 vlm_media_instance_Delete( p_mi
);
700 int libvlc_vlm_get_media_instance_title( libvlc_instance_t
*p_instance
,
701 const char *psz_name
, int i_instance
)
703 vlm_media_instance_t
*p_mi
;
705 p_mi
= libvlc_vlm_get_media_instance( p_instance
, psz_name
, i_instance
);
707 vlm_media_instance_Delete( p_mi
);
708 return p_mi
? 0 : -1;
711 int libvlc_vlm_get_media_instance_chapter( libvlc_instance_t
*p_instance
,
712 const char *psz_name
,
715 vlm_media_instance_t
*p_mi
;
717 p_mi
= libvlc_vlm_get_media_instance( p_instance
, psz_name
,
720 vlm_media_instance_Delete( p_mi
);
721 return p_mi
? 0 : -1;
724 int libvlc_vlm_get_media_instance_seekable( libvlc_instance_t
*p_instance
,
725 const char *psz_name
,
728 vlm_media_instance_t
*p_mi
;
730 p_mi
= libvlc_vlm_get_media_instance( p_instance
, psz_name
, i_instance
);
732 vlm_media_instance_Delete( p_mi
);
733 return p_mi
? 0 : -1;
737 libvlc_event_manager_t
*
738 libvlc_vlm_get_event_manager( libvlc_instance_t
*p_instance
)
740 if( libvlc_vlm_init( p_instance
) )
742 return p_instance
->libvlc_vlm
.p_event_manager
;