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
;
115 /*****************************************************************************
117 *****************************************************************************/
118 vlm_t
*vlm_New ( vlc_object_t
*p_this
)
120 vlm_t
*p_vlm
= NULL
, **pp_vlm
= &(libvlc_priv (p_this
->obj
.libvlc
)->p_vlm
);
123 /* Avoid multiple creation */
124 vlc_mutex_lock( &vlm_mutex
);
128 { /* VLM already exists */
129 if( likely( p_vlm
->users
< UINT_MAX
) )
133 vlc_mutex_unlock( &vlm_mutex
);
137 msg_Dbg( p_this
, "creating VLM" );
139 p_vlm
= vlc_custom_create( p_this
->obj
.libvlc
, sizeof( *p_vlm
),
143 vlc_mutex_unlock( &vlm_mutex
);
147 vlc_mutex_init( &p_vlm
->lock
);
148 vlc_mutex_init( &p_vlm
->lock_manage
);
149 vlc_cond_init_daytime( &p_vlm
->wait_manage
);
151 p_vlm
->input_state_changed
= false;
153 TAB_INIT( p_vlm
->i_media
, p_vlm
->media
);
154 TAB_INIT( p_vlm
->i_schedule
, p_vlm
->schedule
);
156 var_Create( p_vlm
, "intf-event", VLC_VAR_ADDRESS
);
158 if( vlc_clone( &p_vlm
->thread
, Manage
, p_vlm
, VLC_THREAD_PRIORITY_LOW
) )
160 vlc_cond_destroy( &p_vlm
->wait_manage
);
161 vlc_mutex_destroy( &p_vlm
->lock
);
162 vlc_mutex_destroy( &p_vlm
->lock_manage
);
163 vlc_object_release( p_vlm
);
164 vlc_mutex_unlock( &vlm_mutex
);
168 *pp_vlm
= p_vlm
; /* for future reference */
170 /* Load our configuration file */
171 psz_vlmconf
= var_CreateGetString( p_vlm
, "vlm-conf" );
172 if( psz_vlmconf
&& *psz_vlmconf
)
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
);
190 vlc_mutex_unlock( &vlm_mutex
);
195 /*****************************************************************************
197 *****************************************************************************/
198 void vlm_Delete( vlm_t
*p_vlm
)
200 /* vlm_Delete() is serialized against itself, and against vlm_New().
201 * This mutex protects libvlc_priv->p_vlm and p_vlm->users. */
202 vlc_mutex_lock( &vlm_mutex
);
203 assert( p_vlm
->users
> 0 );
204 if( --p_vlm
->users
== 0 )
205 assert( libvlc_priv(p_vlm
->obj
.libvlc
)->p_vlm
== p_vlm
);
211 vlc_mutex_unlock( &vlm_mutex
);
215 /* Destroy and release VLM */
216 vlc_mutex_lock( &p_vlm
->lock
);
217 vlm_ControlInternal( p_vlm
, VLM_CLEAR_MEDIAS
);
218 TAB_CLEAN( p_vlm
->i_media
, p_vlm
->media
);
220 vlm_ControlInternal( p_vlm
, VLM_CLEAR_SCHEDULES
);
221 TAB_CLEAN( p_vlm
->i_schedule
, p_vlm
->schedule
);
222 vlc_mutex_unlock( &p_vlm
->lock
);
224 vlc_cancel( p_vlm
->thread
);
228 module_unneed( p_vlm
->p_vod
, p_vlm
->p_vod
->p_module
);
229 vlc_object_release( p_vlm
->p_vod
);
232 libvlc_priv(p_vlm
->obj
.libvlc
)->p_vlm
= NULL
;
233 vlc_mutex_unlock( &vlm_mutex
);
235 vlc_join( p_vlm
->thread
, NULL
);
237 vlc_cond_destroy( &p_vlm
->wait_manage
);
238 vlc_mutex_destroy( &p_vlm
->lock
);
239 vlc_mutex_destroy( &p_vlm
->lock_manage
);
240 vlc_object_release( p_vlm
);
243 /*****************************************************************************
244 * vlm_ExecuteCommand:
245 *****************************************************************************/
246 int vlm_ExecuteCommand( vlm_t
*p_vlm
, const char *psz_command
,
247 vlm_message_t
**pp_message
)
251 vlc_mutex_lock( &p_vlm
->lock
);
252 i_result
= ExecuteCommand( p_vlm
, psz_command
, pp_message
);
253 vlc_mutex_unlock( &p_vlm
->lock
);
258 /*****************************************************************************
260 *****************************************************************************/
261 static int vlm_MediaVodControl( void *p_private
, vod_media_t
*p_vod_media
,
262 const char *psz_id
, int i_query
, va_list args
)
264 vlm_t
*vlm
= (vlm_t
*)p_private
;
269 if( !p_private
|| !p_vod_media
)
272 vlc_mutex_lock( &vlm
->lock
);
275 for( i
= 0, id
= -1; i
< vlm
->i_media
; i
++ )
277 if( p_vod_media
== vlm
->media
[i
]->vod
.p_media
)
279 id
= vlm
->media
[i
]->cfg
.id
;
285 vlc_mutex_unlock( &vlm
->lock
);
293 psz
= (const char *)va_arg( args
, const char * );
294 int64_t *i_time
= (int64_t *)va_arg( args
, int64_t *);
295 bool b_retry
= false;
298 /* No start time requested: return the current NPT */
299 i_ret
= vlm_ControlInternal( vlm
, VLM_GET_MEDIA_INSTANCE_TIME
, id
, psz_id
, i_time
);
300 /* The instance is not running yet, it will start at 0 */
306 /* We want to seek before unpausing, but it won't
307 * work if the instance is not running yet. */
308 b_retry
= vlm_ControlInternal( vlm
, VLM_SET_MEDIA_INSTANCE_TIME
, id
, psz_id
, *i_time
);
311 i_ret
= vlm_ControlInternal( vlm
, VLM_START_MEDIA_VOD_INSTANCE
, id
, psz_id
, 0, psz
);
313 if (!i_ret
&& b_retry
)
314 i_ret
= vlm_ControlInternal( vlm
, VLM_SET_MEDIA_INSTANCE_TIME
, id
, psz_id
, *i_time
);
318 case VOD_MEDIA_PAUSE
:
320 int64_t *i_time
= (int64_t *)va_arg( args
, int64_t *);
321 i_ret
= vlm_ControlInternal( vlm
, VLM_PAUSE_MEDIA_INSTANCE
, id
, psz_id
);
323 i_ret
= vlm_ControlInternal( vlm
, VLM_GET_MEDIA_INSTANCE_TIME
, id
, psz_id
, i_time
);
328 i_ret
= vlm_ControlInternal( vlm
, VLM_STOP_MEDIA_INSTANCE
, id
, psz_id
);
333 int64_t i_time
= (int64_t)va_arg( args
, int64_t );
334 i_ret
= vlm_ControlInternal( vlm
, VLM_SET_MEDIA_INSTANCE_TIME
, id
, psz_id
, i_time
);
338 case VOD_MEDIA_REWIND
:
340 double d_scale
= (double)va_arg( args
, double );
343 vlm_ControlInternal( vlm
, VLM_GET_MEDIA_INSTANCE_POSITION
, id
, psz_id
, &d_position
);
344 d_position
-= (d_scale
/ 1000.0);
345 if( d_position
< 0.0 )
347 i_ret
= vlm_ControlInternal( vlm
, VLM_SET_MEDIA_INSTANCE_POSITION
, id
, psz_id
, d_position
);
351 case VOD_MEDIA_FORWARD
:
353 double d_scale
= (double)va_arg( args
, double );
356 vlm_ControlInternal( vlm
, VLM_GET_MEDIA_INSTANCE_POSITION
, id
, psz_id
, &d_position
);
357 d_position
+= (d_scale
/ 1000.0);
358 if( d_position
> 1.0 )
360 i_ret
= vlm_ControlInternal( vlm
, VLM_SET_MEDIA_INSTANCE_POSITION
, id
, psz_id
, d_position
);
365 i_ret
= VLC_EGENERIC
;
369 vlc_mutex_unlock( &vlm
->lock
);
375 /*****************************************************************************
377 *****************************************************************************/
378 static void* Manage( void* p_object
)
380 vlm_t
*vlm
= (vlm_t
*)p_object
;
382 time_t lastcheck
, now
, nextschedule
= 0;
388 char **ppsz_scheduled_commands
= NULL
;
389 int i_scheduled_commands
= 0;
390 bool scheduled_command
= false;
392 vlc_mutex_lock( &vlm
->lock_manage
);
393 mutex_cleanup_push( &vlm
->lock_manage
);
394 while( !vlm
->input_state_changed
&& !scheduled_command
)
396 if( nextschedule
!= 0 )
397 scheduled_command
= vlc_cond_timedwait_daytime( &vlm
->wait_manage
, &vlm
->lock_manage
, nextschedule
) != 0;
399 vlc_cond_wait( &vlm
->wait_manage
, &vlm
->lock_manage
);
401 vlm
->input_state_changed
= false;
403 vlc_mutex_unlock( &vlm
->lock_manage
);
405 int canc
= vlc_savecancel ();
406 /* destroy the inputs that wants to die, and launch the next input */
407 vlc_mutex_lock( &vlm
->lock
);
408 for( int i
= 0; i
< vlm
->i_media
; i
++ )
410 vlm_media_sys_t
*p_media
= vlm
->media
[i
];
412 for( j
= 0; j
< p_media
->i_instance
; )
414 vlm_media_instance_sys_t
*p_instance
= p_media
->instance
[j
];
417 if( p_instance
->p_input
!= NULL
)
418 state
= var_GetInteger( p_instance
->p_input
, "state" );
419 if( state
== END_S
|| state
== ERROR_S
)
421 int i_new_input_index
;
424 i_new_input_index
= p_instance
->i_index
+ 1;
425 if( !p_media
->cfg
.b_vod
&& p_media
->cfg
.broadcast
.b_loop
&& i_new_input_index
>= p_media
->cfg
.i_input
)
426 i_new_input_index
= 0;
428 /* FIXME implement multiple input with VOD */
429 if( p_media
->cfg
.b_vod
|| i_new_input_index
>= p_media
->cfg
.i_input
)
430 vlm_ControlInternal( vlm
, VLM_STOP_MEDIA_INSTANCE
, p_media
->cfg
.id
, p_instance
->psz_name
);
432 vlm_ControlInternal( vlm
, VLM_START_MEDIA_BROADCAST_INSTANCE
, p_media
->cfg
.id
, p_instance
->psz_name
, i_new_input_index
);
447 for( int i
= 0; i
< vlm
->i_schedule
; i
++ )
449 time_t real_date
= vlm
->schedule
[i
]->date
;
451 if( vlm
->schedule
[i
]->b_enabled
)
453 if( vlm
->schedule
[i
]->date
== 0 ) // now !
455 vlm
->schedule
[i
]->date
= now
;
458 else if( vlm
->schedule
[i
]->period
!= 0 )
461 while( ((vlm
->schedule
[i
]->date
+ j
*
462 vlm
->schedule
[i
]->period
) <= lastcheck
) &&
463 ( vlm
->schedule
[i
]->i_repeat
> j
||
464 vlm
->schedule
[i
]->i_repeat
< 0 ) )
469 real_date
= vlm
->schedule
[i
]->date
+ j
*
470 vlm
->schedule
[i
]->period
;
473 if( real_date
<= now
)
475 if( real_date
> lastcheck
)
477 for( j
= 0; j
< vlm
->schedule
[i
]->i_command
; j
++ )
479 TAB_APPEND( i_scheduled_commands
,
480 ppsz_scheduled_commands
,
481 strdup(vlm
->schedule
[i
]->command
[j
] ) );
485 else if( nextschedule
== 0 || real_date
< nextschedule
)
487 nextschedule
= real_date
;
492 while( i_scheduled_commands
)
494 vlm_message_t
*message
= NULL
;
495 char *psz_command
= ppsz_scheduled_commands
[0];
496 ExecuteCommand( vlm
, psz_command
,&message
);
498 /* for now, drop the message */
499 vlm_MessageDelete( message
);
500 TAB_REMOVE( i_scheduled_commands
,
501 ppsz_scheduled_commands
,
507 vlc_mutex_unlock( &vlm
->lock
);
508 vlc_restorecancel (canc
);
521 int i_connection_count;
522 int i_connection_active;
531 } vlm_media_status_t;
535 static vlm_media_sys_t
*vlm_ControlMediaGetById( vlm_t
*p_vlm
, int64_t id
)
537 for( int i
= 0; i
< p_vlm
->i_media
; i
++ )
539 if( p_vlm
->media
[i
]->cfg
.id
== id
)
540 return p_vlm
->media
[i
];
544 static vlm_media_sys_t
*vlm_ControlMediaGetByName( vlm_t
*p_vlm
, const char *psz_name
)
546 for( int i
= 0; i
< p_vlm
->i_media
; i
++ )
548 if( !strcmp( p_vlm
->media
[i
]->cfg
.psz_name
, psz_name
) )
549 return p_vlm
->media
[i
];
553 static int vlm_MediaDescriptionCheck( vlm_t
*p_vlm
, vlm_media_t
*p_cfg
)
555 if( !p_cfg
|| !p_cfg
->psz_name
||
556 !strcmp( p_cfg
->psz_name
, "all" ) || !strcmp( p_cfg
->psz_name
, "media" ) || !strcmp( p_cfg
->psz_name
, "schedule" ) )
559 for( int i
= 0; i
< p_vlm
->i_media
; i
++ )
561 if( p_vlm
->media
[i
]->cfg
.id
== p_cfg
->id
)
563 if( !strcmp( p_vlm
->media
[i
]->cfg
.psz_name
, p_cfg
->psz_name
) )
570 /* Called after a media description is changed/added */
571 static int vlm_OnMediaUpdate( vlm_t
*p_vlm
, vlm_media_sys_t
*p_media
)
573 vlm_media_t
*p_cfg
= &p_media
->cfg
;
574 /* Check if we need to create/delete a vod media */
575 if( p_cfg
->b_vod
&& p_vlm
->p_vod
)
577 if( !p_cfg
->b_enabled
&& p_media
->vod
.p_media
)
579 p_vlm
->p_vod
->pf_media_del( p_vlm
->p_vod
, p_media
->vod
.p_media
);
580 p_media
->vod
.p_media
= NULL
;
582 else if( p_cfg
->b_enabled
&& !p_media
->vod
.p_media
&& p_cfg
->i_input
)
584 /* Pre-parse the input */
585 input_thread_t
*p_input
;
590 input_item_Release( p_media
->vod
.p_item
);
592 if( strstr( p_cfg
->ppsz_input
[0], "://" ) == NULL
)
594 char *psz_uri
= vlc_path2uri( p_cfg
->ppsz_input
[0], NULL
);
595 p_media
->vod
.p_item
= input_item_New( psz_uri
,
600 p_media
->vod
.p_item
= input_item_New( p_cfg
->ppsz_input
[0],
603 if( p_cfg
->psz_output
)
605 if( asprintf( &psz_output
, "%s:description", p_cfg
->psz_output
) == -1 )
609 psz_output
= strdup( "#description" );
611 if( psz_output
&& asprintf( &psz_dup
, "sout=%s", psz_output
) != -1 )
613 input_item_AddOption( p_media
->vod
.p_item
, psz_dup
, VLC_INPUT_OPTION_TRUSTED
);
618 for( int i
= 0; i
< p_cfg
->i_option
; i
++ )
619 input_item_AddOption( p_media
->vod
.p_item
,
620 p_cfg
->ppsz_option
[i
], VLC_INPUT_OPTION_TRUSTED
);
622 if( asprintf( &psz_header
, _("Media: %s"), p_cfg
->psz_name
) == -1 )
625 sout_description_data_t data
;
626 TAB_INIT(data
.i_es
, data
.es
);
628 p_input
= input_Create( p_vlm
->p_vod
, p_media
->vod
.p_item
, psz_header
, NULL
);
631 vlc_sem_t sem_preparse
;
632 vlc_sem_init( &sem_preparse
, 0 );
634 preparse_data_t preparse
= { .p_sem
= &sem_preparse
,
635 .b_mux
= (p_cfg
->vod
.psz_mux
!= NULL
) };
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 if( p_cfg
->vod
.psz_mux
)
660 if (!strcmp(p_cfg
->vod
.psz_mux
, "ps"))
662 else if (!strcmp(p_cfg
->vod
.psz_mux
, "ts"))
665 psz_mux
= p_cfg
->vod
.psz_mux
;
667 es_format_t es
, *p_es
= &es
;
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( p_vlm
->p_vod
, "vod server", "$vod-server", false );
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 p_instance
->p_item
, psz_log
,
1002 p_instance
->p_input_resource
);
1003 if( p_instance
->p_input
)
1005 var_AddCallback( p_instance
->p_input
, "intf-event", InputEvent
, p_media
);
1007 if( input_Start( p_instance
->p_input
) != VLC_SUCCESS
)
1009 var_DelCallback( p_instance
->p_input
, "intf-event", InputEvent
, p_media
);
1010 input_Close( p_instance
->p_input
);
1011 p_instance
->p_input
= NULL
;
1015 if( !p_instance
->p_input
)
1017 vlm_MediaInstanceDelete( p_vlm
, id
, p_instance
, p_media
);
1021 vlm_SendEventMediaInstanceStarted( p_vlm
, id
, p_media
->cfg
.psz_name
);
1029 static int vlm_ControlMediaInstanceStop( vlm_t
*p_vlm
, int64_t id
, const char *psz_id
)
1031 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
1032 vlm_media_instance_sys_t
*p_instance
;
1035 return VLC_EGENERIC
;
1037 p_instance
= vlm_ControlMediaInstanceGetByName( p_media
, psz_id
);
1039 return VLC_EGENERIC
;
1041 vlm_MediaInstanceDelete( p_vlm
, id
, p_instance
, p_media
);
1045 static int vlm_ControlMediaInstancePause( vlm_t
*p_vlm
, int64_t id
, const char *psz_id
)
1047 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
1048 vlm_media_instance_sys_t
*p_instance
;
1052 return VLC_EGENERIC
;
1054 p_instance
= vlm_ControlMediaInstanceGetByName( p_media
, psz_id
);
1055 if( !p_instance
|| !p_instance
->p_input
)
1056 return VLC_EGENERIC
;
1058 /* Toggle pause state */
1059 i_state
= var_GetInteger( p_instance
->p_input
, "state" );
1060 if( i_state
== PAUSE_S
&& !p_media
->cfg
.b_vod
)
1061 var_SetInteger( p_instance
->p_input
, "state", PLAYING_S
);
1062 else if( i_state
== PLAYING_S
)
1063 var_SetInteger( p_instance
->p_input
, "state", PAUSE_S
);
1066 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t
*p_vlm
, int64_t id
, const char *psz_id
, int64_t *pi_time
, double *pd_position
)
1068 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
1069 vlm_media_instance_sys_t
*p_instance
;
1072 return VLC_EGENERIC
;
1074 p_instance
= vlm_ControlMediaInstanceGetByName( p_media
, psz_id
);
1075 if( !p_instance
|| !p_instance
->p_input
)
1076 return VLC_EGENERIC
;
1079 *pi_time
= var_GetInteger( p_instance
->p_input
, "time" );
1081 *pd_position
= var_GetFloat( p_instance
->p_input
, "position" );
1084 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t
*p_vlm
, int64_t id
, const char *psz_id
, int64_t i_time
, double d_position
)
1086 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
1087 vlm_media_instance_sys_t
*p_instance
;
1090 return VLC_EGENERIC
;
1092 p_instance
= vlm_ControlMediaInstanceGetByName( p_media
, psz_id
);
1093 if( !p_instance
|| !p_instance
->p_input
)
1094 return VLC_EGENERIC
;
1097 return var_SetInteger( p_instance
->p_input
, "time", i_time
);
1098 else if( d_position
>= 0 && d_position
<= 100 )
1099 return var_SetFloat( p_instance
->p_input
, "position", d_position
);
1100 return VLC_EGENERIC
;
1103 static int vlm_ControlMediaInstanceGets( vlm_t
*p_vlm
, int64_t id
, vlm_media_instance_t
***ppp_idsc
, int *pi_instance
)
1105 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
1106 vlm_media_instance_t
**pp_idsc
;
1110 return VLC_EGENERIC
;
1112 TAB_INIT( i_idsc
, pp_idsc
);
1113 for( int i
= 0; i
< p_media
->i_instance
; i
++ )
1115 vlm_media_instance_sys_t
*p_instance
= p_media
->instance
[i
];
1116 vlm_media_instance_t
*p_idsc
= vlm_media_instance_New();
1118 if( p_instance
->psz_name
)
1119 p_idsc
->psz_name
= strdup( p_instance
->psz_name
);
1120 if( p_instance
->p_input
)
1122 p_idsc
->i_time
= var_GetInteger( p_instance
->p_input
, "time" );
1123 p_idsc
->i_length
= var_GetInteger( p_instance
->p_input
, "length" );
1124 p_idsc
->d_position
= var_GetFloat( p_instance
->p_input
, "position" );
1125 if( var_GetInteger( p_instance
->p_input
, "state" ) == PAUSE_S
)
1126 p_idsc
->b_paused
= true;
1127 p_idsc
->i_rate
= INPUT_RATE_DEFAULT
1128 / var_GetFloat( p_instance
->p_input
, "rate" );
1131 TAB_APPEND( i_idsc
, pp_idsc
, p_idsc
);
1133 *ppp_idsc
= pp_idsc
;
1134 *pi_instance
= i_idsc
;
1138 static int vlm_ControlMediaInstanceClear( vlm_t
*p_vlm
, int64_t id
)
1140 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
1143 return VLC_EGENERIC
;
1145 while( p_media
->i_instance
> 0 )
1146 vlm_ControlMediaInstanceStop( p_vlm
, id
, p_media
->instance
[0]->psz_name
);
1151 static int vlm_ControlScheduleClear( vlm_t
*p_vlm
)
1153 while( p_vlm
->i_schedule
> 0 )
1154 vlm_ScheduleDelete( p_vlm
, p_vlm
->schedule
[0] );
1159 static int vlm_vaControlInternal( vlm_t
*p_vlm
, int i_query
, va_list args
)
1162 vlm_media_t
**pp_dsc
;
1163 vlm_media_t
***ppp_dsc
;
1164 vlm_media_instance_t
***ppp_idsc
;
1166 const char *psz_vod
;
1180 case VLM_GET_MEDIAS
:
1181 ppp_dsc
= (vlm_media_t
***)va_arg( args
, vlm_media_t
*** );
1182 pi_int
= (int *)va_arg( args
, int * );
1183 return vlm_ControlMediaGets( p_vlm
, ppp_dsc
, pi_int
);
1185 case VLM_CLEAR_MEDIAS
:
1186 return vlm_ControlMediaClear( p_vlm
);
1188 case VLM_CHANGE_MEDIA
:
1189 p_dsc
= (vlm_media_t
*)va_arg( args
, vlm_media_t
* );
1190 return vlm_ControlMediaChange( p_vlm
, p_dsc
);
1193 p_dsc
= (vlm_media_t
*)va_arg( args
, vlm_media_t
* );
1194 p_id
= (int64_t*)va_arg( args
, int64_t * );
1195 return vlm_ControlMediaAdd( p_vlm
, p_dsc
, p_id
);
1198 id
= (int64_t)va_arg( args
, int64_t );
1199 return vlm_ControlMediaDel( p_vlm
, id
);
1202 id
= (int64_t)va_arg( args
, int64_t );
1203 pp_dsc
= (vlm_media_t
**)va_arg( args
, vlm_media_t
** );
1204 return vlm_ControlMediaGet( p_vlm
, id
, pp_dsc
);
1206 case VLM_GET_MEDIA_ID
:
1207 psz_id
= (const char*)va_arg( args
, const char * );
1208 p_id
= (int64_t*)va_arg( args
, int64_t * );
1209 return vlm_ControlMediaGetId( p_vlm
, psz_id
, p_id
);
1212 /* Media instance control */
1213 case VLM_GET_MEDIA_INSTANCES
:
1214 id
= (int64_t)va_arg( args
, int64_t );
1215 ppp_idsc
= (vlm_media_instance_t
***)va_arg( args
, vlm_media_instance_t
*** );
1216 pi_int
= (int *)va_arg( args
, int *);
1217 return vlm_ControlMediaInstanceGets( p_vlm
, id
, ppp_idsc
, pi_int
);
1219 case VLM_CLEAR_MEDIA_INSTANCES
:
1220 id
= (int64_t)va_arg( args
, int64_t );
1221 return vlm_ControlMediaInstanceClear( p_vlm
, id
);
1224 case VLM_START_MEDIA_BROADCAST_INSTANCE
:
1225 id
= (int64_t)va_arg( args
, int64_t );
1226 psz_id
= (const char*)va_arg( args
, const char* );
1227 i_int
= (int)va_arg( args
, int );
1228 return vlm_ControlMediaInstanceStart( p_vlm
, id
, psz_id
, i_int
, NULL
);
1230 case VLM_START_MEDIA_VOD_INSTANCE
:
1231 id
= (int64_t)va_arg( args
, int64_t );
1232 psz_id
= (const char*)va_arg( args
, const char* );
1233 i_int
= (int)va_arg( args
, int );
1234 psz_vod
= (const char*)va_arg( args
, const char* );
1236 return VLC_EGENERIC
;
1237 return vlm_ControlMediaInstanceStart( p_vlm
, id
, psz_id
, i_int
, psz_vod
);
1239 case VLM_STOP_MEDIA_INSTANCE
:
1240 id
= (int64_t)va_arg( args
, int64_t );
1241 psz_id
= (const char*)va_arg( args
, const char* );
1242 return vlm_ControlMediaInstanceStop( p_vlm
, id
, psz_id
);
1244 case VLM_PAUSE_MEDIA_INSTANCE
:
1245 id
= (int64_t)va_arg( args
, int64_t );
1246 psz_id
= (const char*)va_arg( args
, const char* );
1247 return vlm_ControlMediaInstancePause( p_vlm
, id
, psz_id
);
1249 case VLM_GET_MEDIA_INSTANCE_TIME
:
1250 id
= (int64_t)va_arg( args
, int64_t );
1251 psz_id
= (const char*)va_arg( args
, const char* );
1252 pi_i64
= (int64_t*)va_arg( args
, int64_t * );
1253 return vlm_ControlMediaInstanceGetTimePosition( p_vlm
, id
, psz_id
, pi_i64
, NULL
);
1254 case VLM_GET_MEDIA_INSTANCE_POSITION
:
1255 id
= (int64_t)va_arg( args
, int64_t );
1256 psz_id
= (const char*)va_arg( args
, const char* );
1257 pd_double
= (double*)va_arg( args
, double* );
1258 return vlm_ControlMediaInstanceGetTimePosition( p_vlm
, id
, psz_id
, NULL
, pd_double
);
1260 case VLM_SET_MEDIA_INSTANCE_TIME
:
1261 id
= (int64_t)va_arg( args
, int64_t );
1262 psz_id
= (const char*)va_arg( args
, const char* );
1263 i_i64
= (int64_t)va_arg( args
, int64_t);
1264 return vlm_ControlMediaInstanceSetTimePosition( p_vlm
, id
, psz_id
, i_i64
, -1 );
1265 case VLM_SET_MEDIA_INSTANCE_POSITION
:
1266 id
= (int64_t)va_arg( args
, int64_t );
1267 psz_id
= (const char*)va_arg( args
, const char* );
1268 d_double
= (double)va_arg( args
, double );
1269 return vlm_ControlMediaInstanceSetTimePosition( p_vlm
, id
, psz_id
, -1, d_double
);
1271 case VLM_CLEAR_SCHEDULES
:
1272 return vlm_ControlScheduleClear( p_vlm
);
1275 msg_Err( p_vlm
, "unknown VLM query" );
1276 return VLC_EGENERIC
;
1280 int vlm_ControlInternal( vlm_t
*p_vlm
, int i_query
, ... )
1285 va_start( args
, i_query
);
1286 i_result
= vlm_vaControlInternal( p_vlm
, i_query
, args
);
1292 int vlm_Control( vlm_t
*p_vlm
, int i_query
, ... )
1297 va_start( args
, i_query
);
1299 vlc_mutex_lock( &p_vlm
->lock
);
1300 i_result
= vlm_vaControlInternal( p_vlm
, i_query
, args
);
1301 vlc_mutex_unlock( &p_vlm
->lock
);