1 /*****************************************************************************
2 * vlm.c: VLM interface plugin
3 *****************************************************************************
4 * Copyright (C) 2000-2005 VLC authors and VideoLAN
7 * Authors: Simon Latapie <garf@videolan.org>
8 * Laurent Aimar <fenrir@videolan.org>
9 * Gildas Bazin <gbazin@videolan.org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
36 #include <ctype.h> /* tolower() */
37 #include <time.h> /* ctime() */
42 #include <vlc_modules.h>
44 #include <vlc_input.h>
45 #include <vlc_stream.h>
46 #include "vlm_internal.h"
47 #include "vlm_event.h"
51 #include "../stream_output/stream_output.h"
52 #include "../libvlc.h"
54 /*****************************************************************************
56 *****************************************************************************/
58 static void* Manage( void * );
59 static int vlm_MediaVodControl( void *, vod_media_t
*, const char *, int, va_list );
61 typedef struct preparse_data_t
67 static int InputEventPreparse( vlc_object_t
*p_this
, char const *psz_cmd
,
68 vlc_value_t oldval
, vlc_value_t newval
, void *p_data
)
70 VLC_UNUSED(p_this
); VLC_UNUSED(psz_cmd
); VLC_UNUSED(oldval
);
71 preparse_data_t
*p_pre
= p_data
;
73 if( newval
.i_int
== INPUT_EVENT_DEAD
||
74 ( p_pre
->b_mux
&& newval
.i_int
== INPUT_EVENT_ITEM_META
) )
75 vlc_sem_post( p_pre
->p_sem
);
80 static int InputEvent( vlc_object_t
*p_this
, char const *psz_cmd
,
81 vlc_value_t oldval
, vlc_value_t newval
,
86 input_thread_t
*p_input
= (input_thread_t
*)p_this
;
87 vlm_t
*p_vlm
= libvlc_priv( p_input
->obj
.libvlc
)->p_vlm
;
89 vlm_media_sys_t
*p_media
= p_data
;
90 const char *psz_instance_name
= NULL
;
92 if( newval
.i_int
== INPUT_EVENT_STATE
)
94 for( int i
= 0; i
< p_media
->i_instance
; i
++ )
96 if( p_media
->instance
[i
]->p_input
== p_input
)
98 psz_instance_name
= p_media
->instance
[i
]->psz_name
;
102 vlm_SendEventMediaInstanceState( p_vlm
, p_media
->cfg
.id
, p_media
->cfg
.psz_name
, psz_instance_name
, var_GetInteger( p_input
, "state" ) );
104 vlc_mutex_lock( &p_vlm
->lock_manage
);
105 p_vlm
->input_state_changed
= true;
106 vlc_cond_signal( &p_vlm
->wait_manage
);
107 vlc_mutex_unlock( &p_vlm
->lock_manage
);
112 static vlc_mutex_t vlm_mutex
= VLC_STATIC_MUTEX
;
114 /*****************************************************************************
116 *****************************************************************************/
117 vlm_t
*vlm_New( libvlc_int_t
*libvlc
, const char *psz_vlmconf
)
119 vlm_t
*p_vlm
= NULL
, **pp_vlm
= &(libvlc_priv(libvlc
)->p_vlm
);
120 vlc_object_t
*p_this
= VLC_OBJECT(libvlc
);
122 /* Avoid multiple creation */
123 vlc_mutex_lock( &vlm_mutex
);
127 { /* VLM already exists */
128 if( likely( p_vlm
->users
< UINT_MAX
) )
132 vlc_mutex_unlock( &vlm_mutex
);
136 msg_Dbg( p_this
, "creating VLM" );
138 p_vlm
= vlc_custom_create( p_this
->obj
.libvlc
, sizeof( *p_vlm
),
142 vlc_mutex_unlock( &vlm_mutex
);
146 vlc_mutex_init( &p_vlm
->lock
);
147 vlc_mutex_init( &p_vlm
->lock_manage
);
148 vlc_cond_init_daytime( &p_vlm
->wait_manage
);
150 p_vlm
->input_state_changed
= false;
152 TAB_INIT( p_vlm
->i_media
, p_vlm
->media
);
153 TAB_INIT( p_vlm
->i_schedule
, p_vlm
->schedule
);
155 var_Create( p_vlm
, "intf-event", VLC_VAR_ADDRESS
);
157 if( vlc_clone( &p_vlm
->thread
, Manage
, p_vlm
, VLC_THREAD_PRIORITY_LOW
) )
159 vlc_cond_destroy( &p_vlm
->wait_manage
);
160 vlc_mutex_destroy( &p_vlm
->lock
);
161 vlc_mutex_destroy( &p_vlm
->lock_manage
);
162 vlc_object_release( p_vlm
);
163 vlc_mutex_unlock( &vlm_mutex
);
167 *pp_vlm
= p_vlm
; /* for future reference */
169 vlc_mutex_unlock( &vlm_mutex
);
171 /* Load our configuration file */
172 if( psz_vlmconf
!= NULL
)
174 vlm_message_t
*p_message
= NULL
;
175 char *psz_buffer
= NULL
;
177 msg_Dbg( p_this
, "loading VLM configuration" );
178 if( asprintf(&psz_buffer
, "load %s", psz_vlmconf
) != -1 )
180 msg_Dbg( p_this
, "%s", psz_buffer
);
181 if( vlm_ExecuteCommand( p_vlm
, psz_buffer
, &p_message
) )
182 msg_Warn( p_this
, "error while loading the configuration file" );
184 vlm_MessageDelete( p_message
);
192 /*****************************************************************************
194 *****************************************************************************/
195 void vlm_Delete( vlm_t
*p_vlm
)
197 /* vlm_Delete() is serialized against itself, and against vlm_New().
198 * This mutex protects libvlc_priv->p_vlm and p_vlm->users. */
199 vlc_mutex_lock( &vlm_mutex
);
200 assert( p_vlm
->users
> 0 );
201 if( --p_vlm
->users
== 0 )
202 assert( libvlc_priv(p_vlm
->obj
.libvlc
)->p_vlm
== p_vlm
);
208 vlc_mutex_unlock( &vlm_mutex
);
212 /* Destroy and release VLM */
213 vlc_mutex_lock( &p_vlm
->lock
);
214 vlm_ControlInternal( p_vlm
, VLM_CLEAR_MEDIAS
);
215 TAB_CLEAN( p_vlm
->i_media
, p_vlm
->media
);
217 vlm_ControlInternal( p_vlm
, VLM_CLEAR_SCHEDULES
);
218 TAB_CLEAN( p_vlm
->i_schedule
, p_vlm
->schedule
);
219 vlc_mutex_unlock( &p_vlm
->lock
);
221 vlc_cancel( p_vlm
->thread
);
225 module_unneed( p_vlm
->p_vod
, p_vlm
->p_vod
->p_module
);
226 vlc_object_release( p_vlm
->p_vod
);
229 libvlc_priv(p_vlm
->obj
.libvlc
)->p_vlm
= NULL
;
230 vlc_mutex_unlock( &vlm_mutex
);
232 vlc_join( p_vlm
->thread
, NULL
);
234 vlc_cond_destroy( &p_vlm
->wait_manage
);
235 vlc_mutex_destroy( &p_vlm
->lock
);
236 vlc_mutex_destroy( &p_vlm
->lock_manage
);
237 vlc_object_release( p_vlm
);
240 /*****************************************************************************
241 * vlm_ExecuteCommand:
242 *****************************************************************************/
243 int vlm_ExecuteCommand( vlm_t
*p_vlm
, const char *psz_command
,
244 vlm_message_t
**pp_message
)
248 vlc_mutex_lock( &p_vlm
->lock
);
249 i_result
= ExecuteCommand( p_vlm
, psz_command
, pp_message
);
250 vlc_mutex_unlock( &p_vlm
->lock
);
255 /*****************************************************************************
257 *****************************************************************************/
258 static int vlm_MediaVodControl( void *p_private
, vod_media_t
*p_vod_media
,
259 const char *psz_id
, int i_query
, va_list args
)
261 vlm_t
*vlm
= (vlm_t
*)p_private
;
266 if( !p_private
|| !p_vod_media
)
269 vlc_mutex_lock( &vlm
->lock
);
272 for( i
= 0, id
= -1; i
< vlm
->i_media
; i
++ )
274 if( p_vod_media
== vlm
->media
[i
]->vod
.p_media
)
276 id
= vlm
->media
[i
]->cfg
.id
;
282 vlc_mutex_unlock( &vlm
->lock
);
290 psz
= (const char *)va_arg( args
, const char * );
291 int64_t *i_time
= (int64_t *)va_arg( args
, int64_t *);
292 bool b_retry
= false;
295 /* No start time requested: return the current NPT */
296 i_ret
= vlm_ControlInternal( vlm
, VLM_GET_MEDIA_INSTANCE_TIME
, id
, psz_id
, i_time
);
297 /* The instance is not running yet, it will start at 0 */
303 /* We want to seek before unpausing, but it won't
304 * work if the instance is not running yet. */
305 b_retry
= vlm_ControlInternal( vlm
, VLM_SET_MEDIA_INSTANCE_TIME
, id
, psz_id
, *i_time
);
308 i_ret
= vlm_ControlInternal( vlm
, VLM_START_MEDIA_VOD_INSTANCE
, id
, psz_id
, 0, psz
);
310 if (!i_ret
&& b_retry
)
311 i_ret
= vlm_ControlInternal( vlm
, VLM_SET_MEDIA_INSTANCE_TIME
, id
, psz_id
, *i_time
);
315 case VOD_MEDIA_PAUSE
:
317 int64_t *i_time
= (int64_t *)va_arg( args
, int64_t *);
318 i_ret
= vlm_ControlInternal( vlm
, VLM_PAUSE_MEDIA_INSTANCE
, id
, psz_id
);
320 i_ret
= vlm_ControlInternal( vlm
, VLM_GET_MEDIA_INSTANCE_TIME
, id
, psz_id
, i_time
);
325 i_ret
= vlm_ControlInternal( vlm
, VLM_STOP_MEDIA_INSTANCE
, id
, psz_id
);
330 int64_t i_time
= (int64_t)va_arg( args
, int64_t );
331 i_ret
= vlm_ControlInternal( vlm
, VLM_SET_MEDIA_INSTANCE_TIME
, id
, psz_id
, i_time
);
335 case VOD_MEDIA_REWIND
:
337 double d_scale
= (double)va_arg( args
, double );
340 vlm_ControlInternal( vlm
, VLM_GET_MEDIA_INSTANCE_POSITION
, id
, psz_id
, &d_position
);
341 d_position
-= (d_scale
/ 1000.0);
342 if( d_position
< 0.0 )
344 i_ret
= vlm_ControlInternal( vlm
, VLM_SET_MEDIA_INSTANCE_POSITION
, id
, psz_id
, d_position
);
348 case VOD_MEDIA_FORWARD
:
350 double d_scale
= (double)va_arg( args
, double );
353 vlm_ControlInternal( vlm
, VLM_GET_MEDIA_INSTANCE_POSITION
, id
, psz_id
, &d_position
);
354 d_position
+= (d_scale
/ 1000.0);
355 if( d_position
> 1.0 )
357 i_ret
= vlm_ControlInternal( vlm
, VLM_SET_MEDIA_INSTANCE_POSITION
, id
, psz_id
, d_position
);
362 i_ret
= VLC_EGENERIC
;
366 vlc_mutex_unlock( &vlm
->lock
);
372 /*****************************************************************************
374 *****************************************************************************/
375 static void* Manage( void* p_object
)
377 vlm_t
*vlm
= (vlm_t
*)p_object
;
378 time_t lastcheck
, now
, nextschedule
= 0;
384 char **ppsz_scheduled_commands
= NULL
;
385 int i_scheduled_commands
= 0;
386 bool scheduled_command
= false;
388 vlc_mutex_lock( &vlm
->lock_manage
);
389 mutex_cleanup_push( &vlm
->lock_manage
);
390 while( !vlm
->input_state_changed
&& !scheduled_command
)
392 if( nextschedule
!= 0 )
393 scheduled_command
= vlc_cond_timedwait_daytime( &vlm
->wait_manage
, &vlm
->lock_manage
, nextschedule
) != 0;
395 vlc_cond_wait( &vlm
->wait_manage
, &vlm
->lock_manage
);
397 vlm
->input_state_changed
= false;
399 vlc_mutex_unlock( &vlm
->lock_manage
);
401 int canc
= vlc_savecancel ();
402 /* destroy the inputs that wants to die, and launch the next input */
403 vlc_mutex_lock( &vlm
->lock
);
404 for( int i
= 0; i
< vlm
->i_media
; i
++ )
406 vlm_media_sys_t
*p_media
= vlm
->media
[i
];
408 for( int j
= 0; j
< p_media
->i_instance
; )
410 vlm_media_instance_sys_t
*p_instance
= p_media
->instance
[j
];
413 if( p_instance
->p_input
!= NULL
)
414 state
= var_GetInteger( p_instance
->p_input
, "state" );
415 if( state
== END_S
|| state
== ERROR_S
)
417 int i_new_input_index
;
420 i_new_input_index
= p_instance
->i_index
+ 1;
421 if( !p_media
->cfg
.b_vod
&& p_media
->cfg
.broadcast
.b_loop
&& i_new_input_index
>= p_media
->cfg
.i_input
)
422 i_new_input_index
= 0;
424 /* FIXME implement multiple input with VOD */
425 if( p_media
->cfg
.b_vod
|| i_new_input_index
>= p_media
->cfg
.i_input
)
426 vlm_ControlInternal( vlm
, VLM_STOP_MEDIA_INSTANCE
, p_media
->cfg
.id
, p_instance
->psz_name
);
428 vlm_ControlInternal( vlm
, VLM_START_MEDIA_BROADCAST_INSTANCE
, p_media
->cfg
.id
, p_instance
->psz_name
, i_new_input_index
);
443 for( int i
= 0; i
< vlm
->i_schedule
; i
++ )
445 time_t real_date
= vlm
->schedule
[i
]->date
;
447 if( vlm
->schedule
[i
]->b_enabled
)
450 if( vlm
->schedule
[i
]->date
== 0 ) // now !
452 vlm
->schedule
[i
]->date
= now
;
456 else if( vlm
->schedule
[i
]->period
!= 0 )
459 while( ((vlm
->schedule
[i
]->date
+ j
*
460 vlm
->schedule
[i
]->period
) <= lastcheck
) &&
461 ( vlm
->schedule
[i
]->i_repeat
> j
||
462 vlm
->schedule
[i
]->i_repeat
< 0 ) )
467 real_date
= vlm
->schedule
[i
]->date
+ j
*
468 vlm
->schedule
[i
]->period
;
471 if( real_date
<= now
)
473 if( real_date
> lastcheck
|| b_now
)
475 for( int j
= 0; j
< vlm
->schedule
[i
]->i_command
; j
++ )
477 TAB_APPEND( i_scheduled_commands
,
478 ppsz_scheduled_commands
,
479 strdup(vlm
->schedule
[i
]->command
[j
] ) );
483 else if( nextschedule
== 0 || real_date
< nextschedule
)
485 nextschedule
= real_date
;
490 while( i_scheduled_commands
)
492 vlm_message_t
*message
= NULL
;
493 char *psz_command
= ppsz_scheduled_commands
[0];
494 ExecuteCommand( vlm
, psz_command
,&message
);
496 /* for now, drop the message */
497 vlm_MessageDelete( message
);
498 TAB_REMOVE( i_scheduled_commands
,
499 ppsz_scheduled_commands
,
505 vlc_mutex_unlock( &vlm
->lock
);
506 vlc_restorecancel (canc
);
519 int i_connection_count;
520 int i_connection_active;
529 } vlm_media_status_t;
533 static vlm_media_sys_t
*vlm_ControlMediaGetById( vlm_t
*p_vlm
, int64_t id
)
535 for( int i
= 0; i
< p_vlm
->i_media
; i
++ )
537 if( p_vlm
->media
[i
]->cfg
.id
== id
)
538 return p_vlm
->media
[i
];
542 static vlm_media_sys_t
*vlm_ControlMediaGetByName( vlm_t
*p_vlm
, const char *psz_name
)
544 for( int i
= 0; i
< p_vlm
->i_media
; i
++ )
546 if( !strcmp( p_vlm
->media
[i
]->cfg
.psz_name
, psz_name
) )
547 return p_vlm
->media
[i
];
551 static int vlm_MediaDescriptionCheck( vlm_t
*p_vlm
, vlm_media_t
*p_cfg
)
553 if( !p_cfg
|| !p_cfg
->psz_name
||
554 !strcmp( p_cfg
->psz_name
, "all" ) || !strcmp( p_cfg
->psz_name
, "media" ) || !strcmp( p_cfg
->psz_name
, "schedule" ) )
557 for( int i
= 0; i
< p_vlm
->i_media
; i
++ )
559 if( p_vlm
->media
[i
]->cfg
.id
== p_cfg
->id
)
561 if( !strcmp( p_vlm
->media
[i
]->cfg
.psz_name
, p_cfg
->psz_name
) )
568 /* Called after a media description is changed/added */
569 static int vlm_OnMediaUpdate( vlm_t
*p_vlm
, vlm_media_sys_t
*p_media
)
571 vlm_media_t
*p_cfg
= &p_media
->cfg
;
572 /* Check if we need to create/delete a vod media */
573 if( p_cfg
->b_vod
&& p_vlm
->p_vod
)
575 if( !p_cfg
->b_enabled
&& p_media
->vod
.p_media
)
577 p_vlm
->p_vod
->pf_media_del( p_vlm
->p_vod
, p_media
->vod
.p_media
);
578 p_media
->vod
.p_media
= NULL
;
580 else if( p_cfg
->b_enabled
&& !p_media
->vod
.p_media
&& p_cfg
->i_input
)
582 /* Pre-parse the input */
583 input_thread_t
*p_input
;
588 input_item_Release( p_media
->vod
.p_item
);
590 if( strstr( p_cfg
->ppsz_input
[0], "://" ) == NULL
)
592 char *psz_uri
= vlc_path2uri( p_cfg
->ppsz_input
[0], NULL
);
593 p_media
->vod
.p_item
= input_item_New( psz_uri
,
598 p_media
->vod
.p_item
= input_item_New( p_cfg
->ppsz_input
[0],
601 if( p_cfg
->psz_output
)
603 if( asprintf( &psz_output
, "%s:description", p_cfg
->psz_output
) == -1 )
607 psz_output
= strdup( "#description" );
609 if( psz_output
&& asprintf( &psz_dup
, "sout=%s", psz_output
) != -1 )
611 input_item_AddOption( p_media
->vod
.p_item
, psz_dup
, VLC_INPUT_OPTION_TRUSTED
);
616 for( int i
= 0; i
< p_cfg
->i_option
; i
++ )
617 input_item_AddOption( p_media
->vod
.p_item
,
618 p_cfg
->ppsz_option
[i
], VLC_INPUT_OPTION_TRUSTED
);
620 if( asprintf( &psz_header
, _("Media: %s"), p_cfg
->psz_name
) == -1 )
623 sout_description_data_t data
;
624 TAB_INIT(data
.i_es
, data
.es
);
626 p_input
= input_Create( p_vlm
->p_vod
, input_LegacyEvents
, NULL
,
627 p_media
->vod
.p_item
, psz_header
, NULL
, NULL
);
630 vlc_sem_t sem_preparse
;
631 vlc_sem_init( &sem_preparse
, 0 );
633 preparse_data_t preparse
= { .p_sem
= &sem_preparse
,
634 .b_mux
= (p_cfg
->vod
.psz_mux
!= NULL
) };
635 input_LegacyVarInit( p_input
);
636 var_AddCallback( p_input
, "intf-event", InputEventPreparse
,
639 data
.sem
= &sem_preparse
;
640 var_Create( p_input
, "sout-description-data", VLC_VAR_ADDRESS
);
641 var_SetAddress( p_input
, "sout-description-data", &data
);
643 if( !input_Start( p_input
) )
644 vlc_sem_wait( &sem_preparse
);
646 var_DelCallback( p_input
, "intf-event", InputEventPreparse
,
649 input_Stop( p_input
);
650 input_Close( p_input
);
651 vlc_sem_destroy( &sem_preparse
);
655 /* XXX: Don't do it that way, but properly use a new input item ref. */
656 input_item_t item
= *p_media
->vod
.p_item
;;
657 es_format_t es
, *p_es
= &es
;
658 if( p_cfg
->vod
.psz_mux
)
661 if (!strcmp(p_cfg
->vod
.psz_mux
, "ps"))
663 else if (!strcmp(p_cfg
->vod
.psz_mux
, "ts"))
666 psz_mux
= p_cfg
->vod
.psz_mux
;
670 unsigned char utext
[5];
674 sprintf( fourcc
.text
, "%4.4s", psz_mux
);
675 for( int i
= 0; i
< 4; i
++ )
676 fourcc
.utext
[i
] = tolower(fourcc
.utext
[i
]);
680 es_format_Init( &es
, VIDEO_ES
, fourcc
.value
);
684 item
.i_es
= data
.i_es
;
687 p_media
->vod
.p_media
= p_vlm
->p_vod
->pf_media_new( p_vlm
->p_vod
,
688 p_cfg
->psz_name
, &item
);
690 TAB_CLEAN(data
.i_es
, data
.es
);
693 else if ( p_cfg
->b_vod
)
694 msg_Err( p_vlm
, "vod server is not loaded" );
697 /* TODO start media if needed */
700 /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
702 vlm_SendEventMediaChanged( p_vlm
, p_cfg
->id
, p_cfg
->psz_name
);
705 static int vlm_ControlMediaChange( vlm_t
*p_vlm
, vlm_media_t
*p_cfg
)
707 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, p_cfg
->id
);
710 if( !p_media
|| vlm_MediaDescriptionCheck( p_vlm
, p_cfg
) )
712 if( ( p_media
->cfg
.b_vod
&& !p_cfg
->b_vod
) || ( !p_media
->cfg
.b_vod
&& p_cfg
->b_vod
) )
717 /* TODO check what are the changes being done (stop instance if needed) */
720 vlm_media_Clean( &p_media
->cfg
);
721 vlm_media_Copy( &p_media
->cfg
, p_cfg
);
723 return vlm_OnMediaUpdate( p_vlm
, p_media
);
726 static int vlm_ControlMediaAdd( vlm_t
*p_vlm
, vlm_media_t
*p_cfg
, int64_t *p_id
)
728 vlm_media_sys_t
*p_media
;
730 if( vlm_MediaDescriptionCheck( p_vlm
, p_cfg
) || vlm_ControlMediaGetByName( p_vlm
, p_cfg
->psz_name
) )
732 msg_Err( p_vlm
, "invalid media description" );
735 /* Check if we need to load the VOD server */
736 if( p_cfg
->b_vod
&& !p_vlm
->p_vod
)
738 p_vlm
->p_vod
= vlc_custom_create( VLC_OBJECT(p_vlm
), sizeof( vod_t
),
740 p_vlm
->p_vod
->p_module
= module_need_var( p_vlm
->p_vod
, "vod server", "vod-server" );
741 if( !p_vlm
->p_vod
->p_module
)
743 msg_Err( p_vlm
, "cannot find vod server" );
744 vlc_object_release( p_vlm
->p_vod
);
749 p_vlm
->p_vod
->p_data
= p_vlm
;
750 p_vlm
->p_vod
->pf_media_control
= vlm_MediaVodControl
;
753 p_media
= calloc( 1, sizeof( vlm_media_sys_t
) );
757 vlm_media_Copy( &p_media
->cfg
, p_cfg
);
758 p_media
->cfg
.id
= p_vlm
->i_id
++;
759 /* FIXME do we do something here if enabled is true ? */
761 p_media
->vod
.p_item
= input_item_New( NULL
, NULL
);
763 p_media
->vod
.p_media
= NULL
;
764 TAB_INIT( p_media
->i_instance
, p_media
->instance
);
767 TAB_APPEND( p_vlm
->i_media
, p_vlm
->media
, p_media
);
770 *p_id
= p_media
->cfg
.id
;
773 vlm_SendEventMediaAdded( p_vlm
, p_media
->cfg
.id
, p_media
->cfg
.psz_name
);
774 return vlm_OnMediaUpdate( p_vlm
, p_media
);
777 static int vlm_ControlMediaDel( vlm_t
*p_vlm
, int64_t id
)
779 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
784 while( p_media
->i_instance
> 0 )
785 vlm_ControlInternal( p_vlm
, VLM_STOP_MEDIA_INSTANCE
, id
, p_media
->instance
[0]->psz_name
);
787 if( p_media
->cfg
.b_vod
)
789 p_media
->cfg
.b_enabled
= false;
790 vlm_OnMediaUpdate( p_vlm
, p_media
);
794 vlm_SendEventMediaRemoved( p_vlm
, id
, p_media
->cfg
.psz_name
);
796 vlm_media_Clean( &p_media
->cfg
);
798 input_item_Release( p_media
->vod
.p_item
);
800 if( p_media
->vod
.p_media
)
801 p_vlm
->p_vod
->pf_media_del( p_vlm
->p_vod
, p_media
->vod
.p_media
);
803 TAB_REMOVE( p_vlm
->i_media
, p_vlm
->media
, p_media
);
809 static int vlm_ControlMediaGets( vlm_t
*p_vlm
, vlm_media_t
***ppp_dsc
, int *pi_dsc
)
811 vlm_media_t
**pp_dsc
;
814 TAB_INIT( i_dsc
, pp_dsc
);
815 for( int i
= 0; i
< p_vlm
->i_media
; i
++ )
817 vlm_media_t
*p_dsc
= vlm_media_Duplicate( &p_vlm
->media
[i
]->cfg
);
818 TAB_APPEND( i_dsc
, pp_dsc
, p_dsc
);
826 static int vlm_ControlMediaClear( vlm_t
*p_vlm
)
828 while( p_vlm
->i_media
> 0 )
829 vlm_ControlMediaDel( p_vlm
, p_vlm
->media
[0]->cfg
.id
);
833 static int vlm_ControlMediaGet( vlm_t
*p_vlm
, int64_t id
, vlm_media_t
**pp_dsc
)
835 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
839 *pp_dsc
= vlm_media_Duplicate( &p_media
->cfg
);
842 static int vlm_ControlMediaGetId( vlm_t
*p_vlm
, const char *psz_name
, int64_t *p_id
)
844 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetByName( p_vlm
, psz_name
);
848 *p_id
= p_media
->cfg
.id
;
852 static vlm_media_instance_sys_t
*vlm_ControlMediaInstanceGetByName( vlm_media_sys_t
*p_media
, const char *psz_id
)
854 for( int i
= 0; i
< p_media
->i_instance
; i
++ )
856 const char *psz
= p_media
->instance
[i
]->psz_name
;
857 if( ( psz
== NULL
&& psz_id
== NULL
) ||
858 ( psz
&& psz_id
&& !strcmp( psz
, psz_id
) ) )
859 return p_media
->instance
[i
];
863 static vlm_media_instance_sys_t
*vlm_MediaInstanceNew( vlm_t
*p_vlm
, const char *psz_name
)
865 vlm_media_instance_sys_t
*p_instance
= calloc( 1, sizeof(vlm_media_instance_sys_t
) );
869 p_instance
->psz_name
= NULL
;
871 p_instance
->psz_name
= strdup( psz_name
);
873 p_instance
->p_item
= input_item_New( NULL
, NULL
);
875 p_instance
->i_index
= 0;
876 p_instance
->b_sout_keep
= false;
877 p_instance
->p_parent
= vlc_object_create( p_vlm
, sizeof (vlc_object_t
) );
878 p_instance
->p_input
= NULL
;
879 p_instance
->p_input_resource
= input_resource_New( p_instance
->p_parent
);
883 static void vlm_MediaInstanceDelete( vlm_t
*p_vlm
, int64_t id
, vlm_media_instance_sys_t
*p_instance
, vlm_media_sys_t
*p_media
)
885 input_thread_t
*p_input
= p_instance
->p_input
;
888 input_Stop( p_input
);
889 input_Close( p_input
);
891 vlm_SendEventMediaInstanceStopped( p_vlm
, id
, p_media
->cfg
.psz_name
);
893 input_resource_Terminate( p_instance
->p_input_resource
);
894 input_resource_Release( p_instance
->p_input_resource
);
895 vlc_object_release( p_instance
->p_parent
);
897 TAB_REMOVE( p_media
->i_instance
, p_media
->instance
, p_instance
);
898 input_item_Release( p_instance
->p_item
);
899 free( p_instance
->psz_name
);
904 static int vlm_ControlMediaInstanceStart( vlm_t
*p_vlm
, int64_t id
, const char *psz_id
, int i_input_index
, const char *psz_vod_output
)
906 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
907 vlm_media_instance_sys_t
*p_instance
;
910 if( !p_media
|| !p_media
->cfg
.b_enabled
|| p_media
->cfg
.i_input
<= 0 )
913 /* TODO support multiple input for VOD with sout-keep ? */
915 if( ( p_media
->cfg
.b_vod
&& !psz_vod_output
) || ( !p_media
->cfg
.b_vod
&& psz_vod_output
) )
918 if( i_input_index
< 0 || i_input_index
>= p_media
->cfg
.i_input
)
921 p_instance
= vlm_ControlMediaInstanceGetByName( p_media
, psz_id
);
924 vlm_media_t
*p_cfg
= &p_media
->cfg
;
926 p_instance
= vlm_MediaInstanceNew( p_vlm
, psz_id
);
932 var_Create( p_instance
->p_parent
, "vod-media", VLC_VAR_ADDRESS
);
933 var_SetAddress( p_instance
->p_parent
, "vod-media",
934 p_media
->vod
.p_media
);
935 var_Create( p_instance
->p_parent
, "vod-session", VLC_VAR_STRING
);
936 var_SetString( p_instance
->p_parent
, "vod-session", psz_id
);
939 if( p_cfg
->psz_output
!= NULL
|| psz_vod_output
!= NULL
)
942 if( asprintf( &psz_buffer
, "sout=%s%s%s",
943 p_cfg
->psz_output
? p_cfg
->psz_output
: "",
944 (p_cfg
->psz_output
&& psz_vod_output
) ? ":" : psz_vod_output
? "#" : "",
945 psz_vod_output
? psz_vod_output
: "" ) != -1 )
947 input_item_AddOption( p_instance
->p_item
, psz_buffer
, VLC_INPUT_OPTION_TRUSTED
);
952 for( int i
= 0; i
< p_cfg
->i_option
; i
++ )
954 if( !strcmp( p_cfg
->ppsz_option
[i
], "sout-keep" ) )
955 p_instance
->b_sout_keep
= true;
956 else if( !strcmp( p_cfg
->ppsz_option
[i
], "nosout-keep" ) || !strcmp( p_cfg
->ppsz_option
[i
], "no-sout-keep" ) )
957 p_instance
->b_sout_keep
= false;
959 input_item_AddOption( p_instance
->p_item
, p_cfg
->ppsz_option
[i
], VLC_INPUT_OPTION_TRUSTED
);
961 TAB_APPEND( p_media
->i_instance
, p_media
->instance
, p_instance
);
964 /* Stop old instance */
965 input_thread_t
*p_input
= p_instance
->p_input
;
968 if( p_instance
->i_index
== i_input_index
)
970 int state
= var_GetInteger( p_input
, "state" );
971 if( state
== PAUSE_S
)
972 var_SetInteger( p_input
, "state", PLAYING_S
);
976 input_Stop( p_input
);
977 input_Close( p_input
);
979 if( !p_instance
->b_sout_keep
)
980 input_resource_TerminateSout( p_instance
->p_input_resource
);
981 input_resource_TerminateVout( p_instance
->p_input_resource
);
983 vlm_SendEventMediaInstanceStopped( p_vlm
, id
, p_media
->cfg
.psz_name
);
987 p_instance
->i_index
= i_input_index
;
988 if( strstr( p_media
->cfg
.ppsz_input
[p_instance
->i_index
], "://" ) == NULL
)
990 char *psz_uri
= vlc_path2uri(
991 p_media
->cfg
.ppsz_input
[p_instance
->i_index
], NULL
);
992 input_item_SetURI( p_instance
->p_item
, psz_uri
) ;
996 input_item_SetURI( p_instance
->p_item
, p_media
->cfg
.ppsz_input
[p_instance
->i_index
] ) ;
998 if( asprintf( &psz_log
, _("Media: %s"), p_media
->cfg
.psz_name
) != -1 )
1000 p_instance
->p_input
= input_Create( p_instance
->p_parent
,
1001 input_LegacyEvents
, NULL
,
1002 p_instance
->p_item
, psz_log
,
1003 p_instance
->p_input_resource
,
1005 if( p_instance
->p_input
)
1007 input_LegacyVarInit( p_instance
->p_input
);
1008 var_AddCallback( p_instance
->p_input
, "intf-event", InputEvent
, p_media
);
1010 if( input_Start( p_instance
->p_input
) != VLC_SUCCESS
)
1012 var_DelCallback( p_instance
->p_input
, "intf-event", InputEvent
, p_media
);
1013 input_Close( p_instance
->p_input
);
1014 p_instance
->p_input
= NULL
;
1018 if( !p_instance
->p_input
)
1020 vlm_MediaInstanceDelete( p_vlm
, id
, p_instance
, p_media
);
1024 vlm_SendEventMediaInstanceStarted( p_vlm
, id
, p_media
->cfg
.psz_name
);
1032 static int vlm_ControlMediaInstanceStop( vlm_t
*p_vlm
, int64_t id
, const char *psz_id
)
1034 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
1035 vlm_media_instance_sys_t
*p_instance
;
1038 return VLC_EGENERIC
;
1040 p_instance
= vlm_ControlMediaInstanceGetByName( p_media
, psz_id
);
1042 return VLC_EGENERIC
;
1044 vlm_MediaInstanceDelete( p_vlm
, id
, p_instance
, p_media
);
1048 static int vlm_ControlMediaInstancePause( vlm_t
*p_vlm
, int64_t id
, const char *psz_id
)
1050 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
1051 vlm_media_instance_sys_t
*p_instance
;
1055 return VLC_EGENERIC
;
1057 p_instance
= vlm_ControlMediaInstanceGetByName( p_media
, psz_id
);
1058 if( !p_instance
|| !p_instance
->p_input
)
1059 return VLC_EGENERIC
;
1061 /* Toggle pause state */
1062 i_state
= var_GetInteger( p_instance
->p_input
, "state" );
1063 if( i_state
== PAUSE_S
&& !p_media
->cfg
.b_vod
)
1064 var_SetInteger( p_instance
->p_input
, "state", PLAYING_S
);
1065 else if( i_state
== PLAYING_S
)
1066 var_SetInteger( p_instance
->p_input
, "state", PAUSE_S
);
1069 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t
*p_vlm
, int64_t id
, const char *psz_id
, int64_t *pi_time
, double *pd_position
)
1071 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
1072 vlm_media_instance_sys_t
*p_instance
;
1075 return VLC_EGENERIC
;
1077 p_instance
= vlm_ControlMediaInstanceGetByName( p_media
, psz_id
);
1078 if( !p_instance
|| !p_instance
->p_input
)
1079 return VLC_EGENERIC
;
1082 *pi_time
= US_FROM_VLC_TICK(var_GetInteger( p_instance
->p_input
, "time" ));
1084 *pd_position
= var_GetFloat( p_instance
->p_input
, "position" );
1087 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t
*p_vlm
, int64_t id
, const char *psz_id
, int64_t i_time
, double d_position
)
1089 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
1090 vlm_media_instance_sys_t
*p_instance
;
1093 return VLC_EGENERIC
;
1095 p_instance
= vlm_ControlMediaInstanceGetByName( p_media
, psz_id
);
1096 if( !p_instance
|| !p_instance
->p_input
)
1097 return VLC_EGENERIC
;
1100 return var_SetInteger( p_instance
->p_input
, "time", VLC_TICK_FROM_US(i_time
) );
1101 else if( d_position
>= 0 && d_position
<= 100 )
1102 return var_SetFloat( p_instance
->p_input
, "position", d_position
);
1103 return VLC_EGENERIC
;
1106 static int vlm_ControlMediaInstanceGets( vlm_t
*p_vlm
, int64_t id
, vlm_media_instance_t
***ppp_idsc
, int *pi_instance
)
1108 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
1109 vlm_media_instance_t
**pp_idsc
;
1113 return VLC_EGENERIC
;
1115 TAB_INIT( i_idsc
, pp_idsc
);
1116 for( int i
= 0; i
< p_media
->i_instance
; i
++ )
1118 vlm_media_instance_sys_t
*p_instance
= p_media
->instance
[i
];
1119 vlm_media_instance_t
*p_idsc
= vlm_media_instance_New();
1121 if( p_instance
->psz_name
)
1122 p_idsc
->psz_name
= strdup( p_instance
->psz_name
);
1123 if( p_instance
->p_input
)
1125 p_idsc
->i_time
= US_FROM_VLC_TICK(var_GetInteger( p_instance
->p_input
, "time" ));
1126 p_idsc
->i_length
= US_FROM_VLC_TICK(var_GetInteger( p_instance
->p_input
, "length" ));
1127 p_idsc
->d_position
= var_GetFloat( p_instance
->p_input
, "position" );
1128 if( var_GetInteger( p_instance
->p_input
, "state" ) == PAUSE_S
)
1129 p_idsc
->b_paused
= true;
1130 p_idsc
->i_rate
= INPUT_RATE_DEFAULT
1131 / var_GetFloat( p_instance
->p_input
, "rate" );
1134 TAB_APPEND( i_idsc
, pp_idsc
, p_idsc
);
1136 *ppp_idsc
= pp_idsc
;
1137 *pi_instance
= i_idsc
;
1141 static int vlm_ControlMediaInstanceClear( vlm_t
*p_vlm
, int64_t id
)
1143 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
1146 return VLC_EGENERIC
;
1148 while( p_media
->i_instance
> 0 )
1149 vlm_ControlMediaInstanceStop( p_vlm
, id
, p_media
->instance
[0]->psz_name
);
1154 static int vlm_ControlScheduleClear( vlm_t
*p_vlm
)
1156 while( p_vlm
->i_schedule
> 0 )
1157 vlm_ScheduleDelete( p_vlm
, p_vlm
->schedule
[0] );
1162 static int vlm_vaControlInternal( vlm_t
*p_vlm
, int i_query
, va_list args
)
1165 vlm_media_t
**pp_dsc
;
1166 vlm_media_t
***ppp_dsc
;
1167 vlm_media_instance_t
***ppp_idsc
;
1169 const char *psz_vod
;
1183 case VLM_GET_MEDIAS
:
1184 ppp_dsc
= (vlm_media_t
***)va_arg( args
, vlm_media_t
*** );
1185 pi_int
= (int *)va_arg( args
, int * );
1186 return vlm_ControlMediaGets( p_vlm
, ppp_dsc
, pi_int
);
1188 case VLM_CLEAR_MEDIAS
:
1189 return vlm_ControlMediaClear( p_vlm
);
1191 case VLM_CHANGE_MEDIA
:
1192 p_dsc
= (vlm_media_t
*)va_arg( args
, vlm_media_t
* );
1193 return vlm_ControlMediaChange( p_vlm
, p_dsc
);
1196 p_dsc
= (vlm_media_t
*)va_arg( args
, vlm_media_t
* );
1197 p_id
= (int64_t*)va_arg( args
, int64_t * );
1198 return vlm_ControlMediaAdd( p_vlm
, p_dsc
, p_id
);
1201 id
= (int64_t)va_arg( args
, int64_t );
1202 return vlm_ControlMediaDel( p_vlm
, id
);
1205 id
= (int64_t)va_arg( args
, int64_t );
1206 pp_dsc
= (vlm_media_t
**)va_arg( args
, vlm_media_t
** );
1207 return vlm_ControlMediaGet( p_vlm
, id
, pp_dsc
);
1209 case VLM_GET_MEDIA_ID
:
1210 psz_id
= (const char*)va_arg( args
, const char * );
1211 p_id
= (int64_t*)va_arg( args
, int64_t * );
1212 return vlm_ControlMediaGetId( p_vlm
, psz_id
, p_id
);
1215 /* Media instance control */
1216 case VLM_GET_MEDIA_INSTANCES
:
1217 id
= (int64_t)va_arg( args
, int64_t );
1218 ppp_idsc
= (vlm_media_instance_t
***)va_arg( args
, vlm_media_instance_t
*** );
1219 pi_int
= (int *)va_arg( args
, int *);
1220 return vlm_ControlMediaInstanceGets( p_vlm
, id
, ppp_idsc
, pi_int
);
1222 case VLM_CLEAR_MEDIA_INSTANCES
:
1223 id
= (int64_t)va_arg( args
, int64_t );
1224 return vlm_ControlMediaInstanceClear( p_vlm
, id
);
1227 case VLM_START_MEDIA_BROADCAST_INSTANCE
:
1228 id
= (int64_t)va_arg( args
, int64_t );
1229 psz_id
= (const char*)va_arg( args
, const char* );
1230 i_int
= (int)va_arg( args
, int );
1231 return vlm_ControlMediaInstanceStart( p_vlm
, id
, psz_id
, i_int
, NULL
);
1233 case VLM_START_MEDIA_VOD_INSTANCE
:
1234 id
= (int64_t)va_arg( args
, int64_t );
1235 psz_id
= (const char*)va_arg( args
, const char* );
1236 i_int
= (int)va_arg( args
, int );
1237 psz_vod
= (const char*)va_arg( args
, const char* );
1239 return VLC_EGENERIC
;
1240 return vlm_ControlMediaInstanceStart( p_vlm
, id
, psz_id
, i_int
, psz_vod
);
1242 case VLM_STOP_MEDIA_INSTANCE
:
1243 id
= (int64_t)va_arg( args
, int64_t );
1244 psz_id
= (const char*)va_arg( args
, const char* );
1245 return vlm_ControlMediaInstanceStop( p_vlm
, id
, psz_id
);
1247 case VLM_PAUSE_MEDIA_INSTANCE
:
1248 id
= (int64_t)va_arg( args
, int64_t );
1249 psz_id
= (const char*)va_arg( args
, const char* );
1250 return vlm_ControlMediaInstancePause( p_vlm
, id
, psz_id
);
1252 case VLM_GET_MEDIA_INSTANCE_TIME
:
1253 id
= (int64_t)va_arg( args
, int64_t );
1254 psz_id
= (const char*)va_arg( args
, const char* );
1255 pi_i64
= (int64_t*)va_arg( args
, int64_t * );
1256 return vlm_ControlMediaInstanceGetTimePosition( p_vlm
, id
, psz_id
, pi_i64
, NULL
);
1257 case VLM_GET_MEDIA_INSTANCE_POSITION
:
1258 id
= (int64_t)va_arg( args
, int64_t );
1259 psz_id
= (const char*)va_arg( args
, const char* );
1260 pd_double
= (double*)va_arg( args
, double* );
1261 return vlm_ControlMediaInstanceGetTimePosition( p_vlm
, id
, psz_id
, NULL
, pd_double
);
1263 case VLM_SET_MEDIA_INSTANCE_TIME
:
1264 id
= (int64_t)va_arg( args
, int64_t );
1265 psz_id
= (const char*)va_arg( args
, const char* );
1266 i_i64
= (int64_t)va_arg( args
, int64_t);
1267 return vlm_ControlMediaInstanceSetTimePosition( p_vlm
, id
, psz_id
, i_i64
, -1 );
1268 case VLM_SET_MEDIA_INSTANCE_POSITION
:
1269 id
= (int64_t)va_arg( args
, int64_t );
1270 psz_id
= (const char*)va_arg( args
, const char* );
1271 d_double
= (double)va_arg( args
, double );
1272 return vlm_ControlMediaInstanceSetTimePosition( p_vlm
, id
, psz_id
, -1, d_double
);
1274 case VLM_CLEAR_SCHEDULES
:
1275 return vlm_ControlScheduleClear( p_vlm
);
1278 msg_Err( p_vlm
, "unknown VLM query" );
1279 return VLC_EGENERIC
;
1283 int vlm_ControlInternal( vlm_t
*p_vlm
, int i_query
, ... )
1288 va_start( args
, i_query
);
1289 i_result
= vlm_vaControlInternal( p_vlm
, i_query
, args
);
1295 int vlm_Control( vlm_t
*p_vlm
, int i_query
, ... )
1300 va_start( args
, i_query
);
1302 vlc_mutex_lock( &p_vlm
->lock
);
1303 i_result
= vlm_vaControlInternal( p_vlm
, i_query
, args
);
1304 vlc_mutex_unlock( &p_vlm
->lock
);