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>
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
;
122 static int libvlc_vlm_init( libvlc_instance_t
*p_instance
)
124 if( !p_instance
->libvlc_vlm
.p_event_manager
)
126 p_instance
->libvlc_vlm
.p_event_manager
=
127 libvlc_event_manager_new( p_instance
->libvlc_vlm
.p_vlm
, p_instance
);
128 if( unlikely(p_instance
->libvlc_vlm
.p_event_manager
== NULL
) )
130 libvlc_event_manager_register_event_type(
131 p_instance
->libvlc_vlm
.p_event_manager
,
132 libvlc_VlmMediaAdded
);
133 libvlc_event_manager_register_event_type(
134 p_instance
->libvlc_vlm
.p_event_manager
,
135 libvlc_VlmMediaRemoved
);
136 libvlc_event_manager_register_event_type(
137 p_instance
->libvlc_vlm
.p_event_manager
,
138 libvlc_VlmMediaChanged
);
139 libvlc_event_manager_register_event_type(
140 p_instance
->libvlc_vlm
.p_event_manager
,
141 libvlc_VlmMediaInstanceStarted
);
142 libvlc_event_manager_register_event_type(
143 p_instance
->libvlc_vlm
.p_event_manager
,
144 libvlc_VlmMediaInstanceStopped
);
145 libvlc_event_manager_register_event_type(
146 p_instance
->libvlc_vlm
.p_event_manager
,
147 libvlc_VlmMediaInstanceStatusInit
);
148 libvlc_event_manager_register_event_type(
149 p_instance
->libvlc_vlm
.p_event_manager
,
150 libvlc_VlmMediaInstanceStatusOpening
);
151 libvlc_event_manager_register_event_type(
152 p_instance
->libvlc_vlm
.p_event_manager
,
153 libvlc_VlmMediaInstanceStatusPlaying
);
154 libvlc_event_manager_register_event_type(
155 p_instance
->libvlc_vlm
.p_event_manager
,
156 libvlc_VlmMediaInstanceStatusPause
);
157 libvlc_event_manager_register_event_type(
158 p_instance
->libvlc_vlm
.p_event_manager
,
159 libvlc_VlmMediaInstanceStatusEnd
);
160 libvlc_event_manager_register_event_type(
161 p_instance
->libvlc_vlm
.p_event_manager
,
162 libvlc_VlmMediaInstanceStatusError
);
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_printerr( "VLM not supported or out of memory" );
173 var_AddCallback( (vlc_object_t
*)p_instance
->libvlc_vlm
.p_vlm
,
174 "intf-event", VlmEvent
,
175 p_instance
->libvlc_vlm
.p_event_manager
);
176 p_instance
->libvlc_vlm
.pf_release
= libvlc_vlm_release_internal
;
182 void libvlc_vlm_release( libvlc_instance_t
*p_instance
)
184 libvlc_vlm_release_internal( p_instance
);
187 #define VLM_RET(p,ret) do { \
188 if( libvlc_vlm_init( p_instance ) ) \
190 (p) = p_instance->libvlc_vlm.p_vlm; \
193 static vlm_media_instance_t
*
194 libvlc_vlm_get_media_instance( libvlc_instance_t
*p_instance
,
195 const char *psz_name
, int i_minstance_idx
)
198 vlm_media_instance_t
**pp_minstance
;
199 vlm_media_instance_t
*p_minstance
;
203 VLM_RET(p_vlm
, NULL
);
205 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, psz_name
, &id
) ||
206 vlm_Control( p_vlm
, VLM_GET_MEDIA_INSTANCES
, id
, &pp_minstance
,
209 libvlc_printerr( "%s: media instances not found", psz_name
);
213 if( i_minstance_idx
>= 0 && i_minstance_idx
< i_minstance
)
215 p_minstance
= pp_minstance
[i_minstance_idx
];
216 TAB_REMOVE( i_minstance
, pp_minstance
, p_minstance
);
218 while( i_minstance
> 0 )
219 vlm_media_instance_Delete( pp_minstance
[--i_minstance
] );
220 TAB_CLEAN( i_minstance
, pp_minstance
);
224 /* local function to be used in libvlc_vlm_show_media only */
225 static char* recurse_answer( vlm_message_t
*p_answer
, const char* psz_delim
,
227 char* psz_childdelim
= NULL
;
228 char* psz_nametag
= NULL
;
229 char* psz_response
= strdup( "" );
232 vlm_message_t
*aw_child
, **paw_child
;
234 i_success
= asprintf( &psz_childdelim
, "%s\t", psz_delim
);
236 /* starting with the children of root node */
237 if( i_success
!= -1 && p_answer
->i_child
)
239 paw_child
= p_answer
->child
;
240 aw_child
= *( paw_child
);
241 /* Iterate over children */
242 for( i
= 0; i
< p_answer
->i_child
; i
++ )
244 /* Spare comma if it is the last element */
246 if( i
== (p_answer
->i_child
- 1) )
249 /* Append name of child node, if not in a list */
252 i_success
= asprintf( &psz_response
, "%s\"%s\": ",
253 psz_response
, aw_child
->psz_name
);
254 if( i_success
== -1 ) break;
257 /* If child node has children, */
258 if( aw_child
->i_child
)
260 /* If the parent node is a list (hence the child node is
261 * inside a list), create a property of its name as if it
262 * had a name value node
266 i_success
= asprintf( &psz_nametag
, "\"name\": \"%s\",%s",
267 aw_child
->psz_name
, psz_childdelim
);
268 if( i_success
== -1 ) break;
272 psz_nametag
= strdup( "" );
274 /* If the child is a list itself, format it accordingly and
275 * recurse through the child's children, telling them that
276 * they are inside a list.
278 if( strcmp( aw_child
->psz_name
, "media" ) == 0 ||
279 strcmp( aw_child
->psz_name
, "inputs" ) == 0 ||
280 strcmp( aw_child
->psz_name
, "options" ) == 0 )
282 i_success
= asprintf( &psz_response
, "%s[%s%s%s]%c%s",
283 psz_response
, psz_childdelim
,
284 recurse_answer( aw_child
,
286 psz_delim
, c_comma
, psz_delim
);
287 if( i_success
== -1 ) break;
289 /* Not a list, so format the child as a JSON object and
290 * recurse through the child's children
294 i_success
= asprintf( &psz_response
, "%s{%s%s%s%s}%c%s",
295 psz_response
, psz_childdelim
, psz_nametag
,
296 recurse_answer( aw_child
,
298 psz_delim
, c_comma
, psz_delim
);
299 if( i_success
== -1 ) break;
302 /* Otherwise - when no children are present - the node is a
303 * value node. So print the value string
307 /* If value is equivalent to NULL, print it as null */
308 if( aw_child
->psz_value
== NULL
309 || strcmp( aw_child
->psz_value
, "(null)" ) == 0 )
311 i_success
= asprintf( &psz_response
, "%snull%c%s",
312 psz_response
, c_comma
, psz_delim
);
313 if( i_success
== -1 )
316 /* Otherwise print the value in quotation marks */
319 i_success
= asprintf( &psz_response
, "%s\"%s\"%c%s",
320 psz_response
, aw_child
->psz_value
,
321 c_comma
, psz_delim
);
322 if( i_success
== -1 ) break;
325 /* getting next child */
327 aw_child
= *( paw_child
);
331 free( psz_childdelim
);
332 if( i_success
== -1 )
334 free( psz_response
);
335 psz_response
= strdup( "" );
340 const char* libvlc_vlm_show_media( libvlc_instance_t
*p_instance
,
341 const char *psz_name
)
343 char *psz_message
= NULL
;
344 vlm_message_t
*answer
= NULL
;
345 char *psz_response
= NULL
;
346 const char *psz_fmt
= NULL
;
347 const char *psz_delimiter
= NULL
;
351 VLM_RET(p_vlm
, NULL
);
355 if( asprintf( &psz_message
, "show %s", psz_name
) == -1 )
358 vlm_ExecuteCommand( p_vlm
, psz_message
, &answer
);
359 if( answer
->psz_value
)
361 libvlc_printerr( "Unable to call show %s: %s",
362 psz_name
, answer
->psz_value
);
364 else if ( answer
->child
)
365 { /* in case everything was requested */
366 if ( strcmp( psz_name
, "" ) == 0 )
368 psz_fmt
= "{\n\t%s\n}\n";
369 psz_delimiter
= "\n\t";
375 psz_delimiter
= "\n";
378 if( asprintf( &psz_response
, psz_fmt
,
379 recurse_answer( answer
, psz_delimiter
, i_list
) ) == -1 )
381 libvlc_printerr( "Out of memory" );
386 return( psz_response
);
390 int libvlc_vlm_add_broadcast( libvlc_instance_t
*p_instance
,
391 const char *psz_name
,
392 const char *psz_input
,
393 const char *psz_output
, int i_options
,
394 const char * const *ppsz_options
,
395 int b_enabled
, int b_loop
)
403 vlm_media_Init( &m
);
404 m
.psz_name
= strdup( psz_name
);
405 m
.b_enabled
= b_enabled
;
407 m
.broadcast
.b_loop
= b_loop
;
409 TAB_APPEND( m
.i_input
, m
.ppsz_input
, strdup(psz_input
) );
411 m
.psz_output
= strdup( psz_output
);
412 for( n
= 0; n
< i_options
; n
++ )
413 TAB_APPEND( m
.i_option
, m
.ppsz_option
, strdup(ppsz_options
[n
]) );
415 n
= vlm_Control( p_vlm
, VLM_ADD_MEDIA
, &m
, NULL
);
416 vlm_media_Clean( &m
);
419 libvlc_printerr( "Media %s creation failed", psz_name
);
425 int libvlc_vlm_add_vod( libvlc_instance_t
*p_instance
, const char *psz_name
,
426 const char *psz_input
, int i_options
,
427 const char * const *ppsz_options
, int b_enabled
,
428 const char *psz_mux
)
436 vlm_media_Init( &m
);
437 m
.psz_name
= strdup( psz_name
);
438 m
.b_enabled
= b_enabled
;
440 m
.vod
.psz_mux
= psz_mux
? strdup( psz_mux
) : NULL
;
442 TAB_APPEND( m
.i_input
, m
.ppsz_input
, strdup(psz_input
) );
443 for( n
= 0; n
< i_options
; n
++ )
444 TAB_APPEND( m
.i_option
, m
.ppsz_option
, strdup(ppsz_options
[n
]) );
446 n
= vlm_Control( p_vlm
, VLM_ADD_MEDIA
, &m
, NULL
);
447 vlm_media_Clean( &m
);
450 libvlc_printerr( "Media %s creation failed", psz_name
);
456 int libvlc_vlm_del_media( libvlc_instance_t
*p_instance
, const char *psz_name
)
463 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, psz_name
, &id
) ||
464 vlm_Control( p_vlm
, VLM_DEL_MEDIA
, id
) )
466 libvlc_printerr( "Unable to delete %s", psz_name
);
472 static vlm_media_t
*get_media( libvlc_instance_t
*p_instance
,
473 vlm_t
**restrict pp_vlm
, const char *name
)
475 vlm_media_t
*p_media
;
479 VLM_RET(p_vlm
, NULL
);
480 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, name
, &id
) ||
481 vlm_Control( p_vlm
, VLM_GET_MEDIA
, id
, &p_media
) )
487 #define VLM_CHANGE(psz_error, code ) do { \
489 vlm_media_t *p_media = get_media( p_instance, &p_vlm, psz_name ); \
490 if( p_media != NULL ) { \
492 if( vlm_Control( p_vlm, VLM_CHANGE_MEDIA, p_media ) ) \
494 vlm_media_Delete( p_media ); \
495 if( p_vlm != NULL ) \
498 libvlc_printerr( psz_error, psz_name ); \
502 int libvlc_vlm_set_enabled( libvlc_instance_t
*p_instance
,
503 const char *psz_name
, int b_enabled
)
505 #define VLM_CHANGE_CODE { p_media->b_enabled = b_enabled; }
506 VLM_CHANGE( "Unable to delete %s", VLM_CHANGE_CODE
);
507 #undef VLM_CHANGE_CODE
510 int libvlc_vlm_set_loop( libvlc_instance_t
*p_instance
, const char *psz_name
,
513 #define VLM_CHANGE_CODE { p_media->broadcast.b_loop = b_loop; }
514 VLM_CHANGE( "Unable to change %s loop property", VLM_CHANGE_CODE
);
515 #undef VLM_CHANGE_CODE
518 int libvlc_vlm_set_mux( libvlc_instance_t
*p_instance
, const char *psz_name
,
519 const char *psz_mux
)
521 #define VLM_CHANGE_CODE { if( p_media->b_vod ) { \
522 free( p_media->vod.psz_mux ); \
523 p_media->vod.psz_mux = psz_mux \
524 ? strdup( psz_mux ) : NULL; \
526 VLM_CHANGE( "Unable to change %s mux property", VLM_CHANGE_CODE
);
527 #undef VLM_CHANGE_CODE
530 int libvlc_vlm_set_output( libvlc_instance_t
*p_instance
,
531 const char *psz_name
, const char *psz_output
)
533 #define VLM_CHANGE_CODE { free( p_media->psz_output ); \
534 p_media->psz_output = strdup( psz_output ); }
535 VLM_CHANGE( "Unable to change %s output property", VLM_CHANGE_CODE
);
536 #undef VLM_CHANGE_CODE
539 int libvlc_vlm_set_input( libvlc_instance_t
*p_instance
,
540 const char *psz_name
, const char *psz_input
)
542 #define VLM_CHANGE_CODE { while( p_media->i_input > 0 ) \
543 free( p_media->ppsz_input[--p_media->i_input] );\
544 TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
545 strdup(psz_input) ); }
546 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE
);
547 #undef VLM_CHANGE_CODE
550 int libvlc_vlm_add_input( libvlc_instance_t
*p_instance
,
551 const char *psz_name
, const char *psz_input
)
553 #define VLM_CHANGE_CODE { TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
554 strdup(psz_input) ); }
555 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE
);
556 #undef VLM_CHANGE_CODE
559 int libvlc_vlm_change_media( libvlc_instance_t
*p_instance
,
560 const char *psz_name
, const char *psz_input
,
561 const char *psz_output
, int i_options
,
562 const char * const *ppsz_options
, int b_enabled
,
565 #define VLM_CHANGE_CODE { int n; \
566 p_media->b_enabled = b_enabled; \
567 p_media->broadcast.b_loop = b_loop; \
568 while( p_media->i_input > 0 ) \
569 free( p_media->ppsz_input[--p_media->i_input] ); \
571 TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); \
572 free( p_media->psz_output ); \
573 p_media->psz_output = psz_output ? strdup( psz_output ) : NULL; \
574 while( p_media->i_option > 0 ) \
575 free( p_media->ppsz_option[--p_media->i_option] ); \
576 for( n = 0; n < i_options; n++ ) \
577 TAB_APPEND( p_media->i_option, p_media->ppsz_option, \
578 strdup(ppsz_options[n]) ); \
580 VLM_CHANGE( "Unable to change %s properties", VLM_CHANGE_CODE
);
581 #undef VLM_CHANGE_CODE
584 int libvlc_vlm_play_media( libvlc_instance_t
*p_instance
,
585 const char *psz_name
)
592 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, psz_name
, &id
) ||
593 vlm_Control( p_vlm
, VLM_START_MEDIA_BROADCAST_INSTANCE
, id
, NULL
, 0 ) )
595 libvlc_printerr( "Unable to play %s", psz_name
);
601 int libvlc_vlm_stop_media( libvlc_instance_t
*p_instance
,
602 const char *psz_name
)
609 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, psz_name
, &id
) ||
610 vlm_Control( p_vlm
, VLM_STOP_MEDIA_INSTANCE
, id
, NULL
) )
612 libvlc_printerr( "Unable to stop %s", psz_name
);
618 int libvlc_vlm_pause_media( libvlc_instance_t
*p_instance
,
619 const char *psz_name
)
626 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, psz_name
, &id
) ||
627 vlm_Control( p_vlm
, VLM_PAUSE_MEDIA_INSTANCE
, id
, NULL
) )
629 libvlc_printerr( "Unable to pause %s", psz_name
);
635 int libvlc_vlm_seek_media( libvlc_instance_t
*p_instance
,
636 const char *psz_name
, float f_percentage
)
643 if( vlm_Control( p_vlm
, VLM_GET_MEDIA_ID
, psz_name
, &id
) ||
644 vlm_Control( p_vlm
, VLM_SET_MEDIA_INSTANCE_POSITION
, id
, NULL
,
647 libvlc_printerr( "Unable to seek %s to %f%%", psz_name
, f_percentage
);
653 float libvlc_vlm_get_media_instance_position( libvlc_instance_t
*p_instance
,
654 const char *psz_name
,
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
->d_position
;
664 vlm_media_instance_Delete( p_mi
);
669 int libvlc_vlm_get_media_instance_time( libvlc_instance_t
*p_instance
,
670 const char *psz_name
, int i_instance
)
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_time
;
679 vlm_media_instance_Delete( p_mi
);
684 int libvlc_vlm_get_media_instance_length( libvlc_instance_t
*p_instance
,
685 const char *psz_name
,
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_length
;
695 vlm_media_instance_Delete( p_mi
);
700 int libvlc_vlm_get_media_instance_rate( libvlc_instance_t
*p_instance
,
701 const char *psz_name
, int i_instance
)
703 vlm_media_instance_t
*p_mi
;
706 p_mi
= libvlc_vlm_get_media_instance( p_instance
, psz_name
, i_instance
);
709 result
= p_mi
->i_rate
;
710 vlm_media_instance_Delete( p_mi
);
716 int libvlc_vlm_get_media_instance_title( libvlc_instance_t
*p_instance
,
717 const char *psz_name
, int i_instance
)
719 vlm_media_instance_t
*p_mi
;
721 p_mi
= libvlc_vlm_get_media_instance( p_instance
, psz_name
, i_instance
);
723 vlm_media_instance_Delete( p_mi
);
724 return p_mi
? 0 : -1;
727 int libvlc_vlm_get_media_instance_chapter( libvlc_instance_t
*p_instance
,
728 const char *psz_name
,
731 vlm_media_instance_t
*p_mi
;
733 p_mi
= libvlc_vlm_get_media_instance( p_instance
, psz_name
,
736 vlm_media_instance_Delete( p_mi
);
737 return p_mi
? 0 : -1;
740 int libvlc_vlm_get_media_instance_seekable( libvlc_instance_t
*p_instance
,
741 const char *psz_name
,
744 vlm_media_instance_t
*p_mi
;
746 p_mi
= libvlc_vlm_get_media_instance( p_instance
, psz_name
, i_instance
);
748 vlm_media_instance_Delete( p_mi
);
749 return p_mi
? 0 : -1;
753 libvlc_event_manager_t
*
754 libvlc_vlm_get_event_manager( libvlc_instance_t
*p_instance
)
757 VLM_RET( p_vlm
, NULL
);
758 return p_instance
->libvlc_vlm
.p_event_manager
;