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
;
381 time_t lastcheck
, now
, nextschedule
= 0;
387 char **ppsz_scheduled_commands
= NULL
;
388 int i_scheduled_commands
= 0;
389 bool scheduled_command
= false;
391 vlc_mutex_lock( &vlm
->lock_manage
);
392 mutex_cleanup_push( &vlm
->lock_manage
);
393 while( !vlm
->input_state_changed
&& !scheduled_command
)
395 if( nextschedule
!= 0 )
396 scheduled_command
= vlc_cond_timedwait_daytime( &vlm
->wait_manage
, &vlm
->lock_manage
, nextschedule
) != 0;
398 vlc_cond_wait( &vlm
->wait_manage
, &vlm
->lock_manage
);
400 vlm
->input_state_changed
= false;
402 vlc_mutex_unlock( &vlm
->lock_manage
);
404 int canc
= vlc_savecancel ();
405 /* destroy the inputs that wants to die, and launch the next input */
406 vlc_mutex_lock( &vlm
->lock
);
407 for( int i
= 0; i
< vlm
->i_media
; i
++ )
409 vlm_media_sys_t
*p_media
= vlm
->media
[i
];
411 for( int j
= 0; j
< p_media
->i_instance
; )
413 vlm_media_instance_sys_t
*p_instance
= p_media
->instance
[j
];
416 if( p_instance
->p_input
!= NULL
)
417 state
= var_GetInteger( p_instance
->p_input
, "state" );
418 if( state
== END_S
|| state
== ERROR_S
)
420 int i_new_input_index
;
423 i_new_input_index
= p_instance
->i_index
+ 1;
424 if( !p_media
->cfg
.b_vod
&& p_media
->cfg
.broadcast
.b_loop
&& i_new_input_index
>= p_media
->cfg
.i_input
)
425 i_new_input_index
= 0;
427 /* FIXME implement multiple input with VOD */
428 if( p_media
->cfg
.b_vod
|| i_new_input_index
>= p_media
->cfg
.i_input
)
429 vlm_ControlInternal( vlm
, VLM_STOP_MEDIA_INSTANCE
, p_media
->cfg
.id
, p_instance
->psz_name
);
431 vlm_ControlInternal( vlm
, VLM_START_MEDIA_BROADCAST_INSTANCE
, p_media
->cfg
.id
, p_instance
->psz_name
, i_new_input_index
);
446 for( int i
= 0; i
< vlm
->i_schedule
; i
++ )
448 time_t real_date
= vlm
->schedule
[i
]->date
;
450 if( vlm
->schedule
[i
]->b_enabled
)
452 if( vlm
->schedule
[i
]->date
== 0 ) // now !
454 vlm
->schedule
[i
]->date
= now
;
457 else if( vlm
->schedule
[i
]->period
!= 0 )
460 while( ((vlm
->schedule
[i
]->date
+ j
*
461 vlm
->schedule
[i
]->period
) <= lastcheck
) &&
462 ( vlm
->schedule
[i
]->i_repeat
> j
||
463 vlm
->schedule
[i
]->i_repeat
< 0 ) )
468 real_date
= vlm
->schedule
[i
]->date
+ j
*
469 vlm
->schedule
[i
]->period
;
472 if( real_date
<= now
)
474 if( real_date
> lastcheck
)
476 for( int j
= 0; j
< vlm
->schedule
[i
]->i_command
; j
++ )
478 TAB_APPEND( i_scheduled_commands
,
479 ppsz_scheduled_commands
,
480 strdup(vlm
->schedule
[i
]->command
[j
] ) );
484 else if( nextschedule
== 0 || real_date
< nextschedule
)
486 nextschedule
= real_date
;
491 while( i_scheduled_commands
)
493 vlm_message_t
*message
= NULL
;
494 char *psz_command
= ppsz_scheduled_commands
[0];
495 ExecuteCommand( vlm
, psz_command
,&message
);
497 /* for now, drop the message */
498 vlm_MessageDelete( message
);
499 TAB_REMOVE( i_scheduled_commands
,
500 ppsz_scheduled_commands
,
506 vlc_mutex_unlock( &vlm
->lock
);
507 vlc_restorecancel (canc
);
520 int i_connection_count;
521 int i_connection_active;
530 } vlm_media_status_t;
534 static vlm_media_sys_t
*vlm_ControlMediaGetById( vlm_t
*p_vlm
, int64_t id
)
536 for( int i
= 0; i
< p_vlm
->i_media
; i
++ )
538 if( p_vlm
->media
[i
]->cfg
.id
== id
)
539 return p_vlm
->media
[i
];
543 static vlm_media_sys_t
*vlm_ControlMediaGetByName( vlm_t
*p_vlm
, const char *psz_name
)
545 for( int i
= 0; i
< p_vlm
->i_media
; i
++ )
547 if( !strcmp( p_vlm
->media
[i
]->cfg
.psz_name
, psz_name
) )
548 return p_vlm
->media
[i
];
552 static int vlm_MediaDescriptionCheck( vlm_t
*p_vlm
, vlm_media_t
*p_cfg
)
554 if( !p_cfg
|| !p_cfg
->psz_name
||
555 !strcmp( p_cfg
->psz_name
, "all" ) || !strcmp( p_cfg
->psz_name
, "media" ) || !strcmp( p_cfg
->psz_name
, "schedule" ) )
558 for( int i
= 0; i
< p_vlm
->i_media
; i
++ )
560 if( p_vlm
->media
[i
]->cfg
.id
== p_cfg
->id
)
562 if( !strcmp( p_vlm
->media
[i
]->cfg
.psz_name
, p_cfg
->psz_name
) )
569 /* Called after a media description is changed/added */
570 static int vlm_OnMediaUpdate( vlm_t
*p_vlm
, vlm_media_sys_t
*p_media
)
572 vlm_media_t
*p_cfg
= &p_media
->cfg
;
573 /* Check if we need to create/delete a vod media */
574 if( p_cfg
->b_vod
&& p_vlm
->p_vod
)
576 if( !p_cfg
->b_enabled
&& p_media
->vod
.p_media
)
578 p_vlm
->p_vod
->pf_media_del( p_vlm
->p_vod
, p_media
->vod
.p_media
);
579 p_media
->vod
.p_media
= NULL
;
581 else if( p_cfg
->b_enabled
&& !p_media
->vod
.p_media
&& p_cfg
->i_input
)
583 /* Pre-parse the input */
584 input_thread_t
*p_input
;
589 input_item_Release( p_media
->vod
.p_item
);
591 if( strstr( p_cfg
->ppsz_input
[0], "://" ) == NULL
)
593 char *psz_uri
= vlc_path2uri( p_cfg
->ppsz_input
[0], NULL
);
594 p_media
->vod
.p_item
= input_item_New( psz_uri
,
599 p_media
->vod
.p_item
= input_item_New( p_cfg
->ppsz_input
[0],
602 if( p_cfg
->psz_output
)
604 if( asprintf( &psz_output
, "%s:description", p_cfg
->psz_output
) == -1 )
608 psz_output
= strdup( "#description" );
610 if( psz_output
&& asprintf( &psz_dup
, "sout=%s", psz_output
) != -1 )
612 input_item_AddOption( p_media
->vod
.p_item
, psz_dup
, VLC_INPUT_OPTION_TRUSTED
);
617 for( int i
= 0; i
< p_cfg
->i_option
; i
++ )
618 input_item_AddOption( p_media
->vod
.p_item
,
619 p_cfg
->ppsz_option
[i
], VLC_INPUT_OPTION_TRUSTED
);
621 if( asprintf( &psz_header
, _("Media: %s"), p_cfg
->psz_name
) == -1 )
624 sout_description_data_t data
;
625 TAB_INIT(data
.i_es
, data
.es
);
627 p_input
= input_Create( p_vlm
->p_vod
, 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 var_AddCallback( p_input
, "intf-event", InputEventPreparse
,
638 data
.sem
= &sem_preparse
;
639 var_Create( p_input
, "sout-description-data", VLC_VAR_ADDRESS
);
640 var_SetAddress( p_input
, "sout-description-data", &data
);
642 if( !input_Start( p_input
) )
643 vlc_sem_wait( &sem_preparse
);
645 var_DelCallback( p_input
, "intf-event", InputEventPreparse
,
648 input_Stop( p_input
);
649 input_Close( p_input
);
650 vlc_sem_destroy( &sem_preparse
);
654 /* XXX: Don't do it that way, but properly use a new input item ref. */
655 input_item_t item
= *p_media
->vod
.p_item
;;
656 es_format_t es
, *p_es
= &es
;
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
;
669 unsigned char utext
[5];
673 sprintf( fourcc
.text
, "%4.4s", psz_mux
);
674 for( int i
= 0; i
< 4; i
++ )
675 fourcc
.utext
[i
] = tolower(fourcc
.utext
[i
]);
679 es_format_Init( &es
, VIDEO_ES
, fourcc
.value
);
683 item
.i_es
= data
.i_es
;
686 p_media
->vod
.p_media
= p_vlm
->p_vod
->pf_media_new( p_vlm
->p_vod
,
687 p_cfg
->psz_name
, &item
);
689 TAB_CLEAN(data
.i_es
, data
.es
);
692 else if ( p_cfg
->b_vod
)
693 msg_Err( p_vlm
, "vod server is not loaded" );
696 /* TODO start media if needed */
699 /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
701 vlm_SendEventMediaChanged( p_vlm
, p_cfg
->id
, p_cfg
->psz_name
);
704 static int vlm_ControlMediaChange( vlm_t
*p_vlm
, vlm_media_t
*p_cfg
)
706 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, p_cfg
->id
);
709 if( !p_media
|| vlm_MediaDescriptionCheck( p_vlm
, p_cfg
) )
711 if( ( p_media
->cfg
.b_vod
&& !p_cfg
->b_vod
) || ( !p_media
->cfg
.b_vod
&& p_cfg
->b_vod
) )
716 /* TODO check what are the changes being done (stop instance if needed) */
719 vlm_media_Clean( &p_media
->cfg
);
720 vlm_media_Copy( &p_media
->cfg
, p_cfg
);
722 return vlm_OnMediaUpdate( p_vlm
, p_media
);
725 static int vlm_ControlMediaAdd( vlm_t
*p_vlm
, vlm_media_t
*p_cfg
, int64_t *p_id
)
727 vlm_media_sys_t
*p_media
;
729 if( vlm_MediaDescriptionCheck( p_vlm
, p_cfg
) || vlm_ControlMediaGetByName( p_vlm
, p_cfg
->psz_name
) )
731 msg_Err( p_vlm
, "invalid media description" );
734 /* Check if we need to load the VOD server */
735 if( p_cfg
->b_vod
&& !p_vlm
->p_vod
)
737 p_vlm
->p_vod
= vlc_custom_create( VLC_OBJECT(p_vlm
), sizeof( vod_t
),
739 p_vlm
->p_vod
->p_module
= module_need( p_vlm
->p_vod
, "vod server", "$vod-server", false );
740 if( !p_vlm
->p_vod
->p_module
)
742 msg_Err( p_vlm
, "cannot find vod server" );
743 vlc_object_release( p_vlm
->p_vod
);
748 p_vlm
->p_vod
->p_data
= p_vlm
;
749 p_vlm
->p_vod
->pf_media_control
= vlm_MediaVodControl
;
752 p_media
= calloc( 1, sizeof( vlm_media_sys_t
) );
756 vlm_media_Copy( &p_media
->cfg
, p_cfg
);
757 p_media
->cfg
.id
= p_vlm
->i_id
++;
758 /* FIXME do we do something here if enabled is true ? */
760 p_media
->vod
.p_item
= input_item_New( NULL
, NULL
);
762 p_media
->vod
.p_media
= NULL
;
763 TAB_INIT( p_media
->i_instance
, p_media
->instance
);
766 TAB_APPEND( p_vlm
->i_media
, p_vlm
->media
, p_media
);
769 *p_id
= p_media
->cfg
.id
;
772 vlm_SendEventMediaAdded( p_vlm
, p_media
->cfg
.id
, p_media
->cfg
.psz_name
);
773 return vlm_OnMediaUpdate( p_vlm
, p_media
);
776 static int vlm_ControlMediaDel( vlm_t
*p_vlm
, int64_t id
)
778 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
783 while( p_media
->i_instance
> 0 )
784 vlm_ControlInternal( p_vlm
, VLM_STOP_MEDIA_INSTANCE
, id
, p_media
->instance
[0]->psz_name
);
786 if( p_media
->cfg
.b_vod
)
788 p_media
->cfg
.b_enabled
= false;
789 vlm_OnMediaUpdate( p_vlm
, p_media
);
793 vlm_SendEventMediaRemoved( p_vlm
, id
, p_media
->cfg
.psz_name
);
795 vlm_media_Clean( &p_media
->cfg
);
797 input_item_Release( p_media
->vod
.p_item
);
799 if( p_media
->vod
.p_media
)
800 p_vlm
->p_vod
->pf_media_del( p_vlm
->p_vod
, p_media
->vod
.p_media
);
802 TAB_REMOVE( p_vlm
->i_media
, p_vlm
->media
, p_media
);
808 static int vlm_ControlMediaGets( vlm_t
*p_vlm
, vlm_media_t
***ppp_dsc
, int *pi_dsc
)
810 vlm_media_t
**pp_dsc
;
813 TAB_INIT( i_dsc
, pp_dsc
);
814 for( int i
= 0; i
< p_vlm
->i_media
; i
++ )
816 vlm_media_t
*p_dsc
= vlm_media_Duplicate( &p_vlm
->media
[i
]->cfg
);
817 TAB_APPEND( i_dsc
, pp_dsc
, p_dsc
);
825 static int vlm_ControlMediaClear( vlm_t
*p_vlm
)
827 while( p_vlm
->i_media
> 0 )
828 vlm_ControlMediaDel( p_vlm
, p_vlm
->media
[0]->cfg
.id
);
832 static int vlm_ControlMediaGet( vlm_t
*p_vlm
, int64_t id
, vlm_media_t
**pp_dsc
)
834 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
838 *pp_dsc
= vlm_media_Duplicate( &p_media
->cfg
);
841 static int vlm_ControlMediaGetId( vlm_t
*p_vlm
, const char *psz_name
, int64_t *p_id
)
843 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetByName( p_vlm
, psz_name
);
847 *p_id
= p_media
->cfg
.id
;
851 static vlm_media_instance_sys_t
*vlm_ControlMediaInstanceGetByName( vlm_media_sys_t
*p_media
, const char *psz_id
)
853 for( int i
= 0; i
< p_media
->i_instance
; i
++ )
855 const char *psz
= p_media
->instance
[i
]->psz_name
;
856 if( ( psz
== NULL
&& psz_id
== NULL
) ||
857 ( psz
&& psz_id
&& !strcmp( psz
, psz_id
) ) )
858 return p_media
->instance
[i
];
862 static vlm_media_instance_sys_t
*vlm_MediaInstanceNew( vlm_t
*p_vlm
, const char *psz_name
)
864 vlm_media_instance_sys_t
*p_instance
= calloc( 1, sizeof(vlm_media_instance_sys_t
) );
868 p_instance
->psz_name
= NULL
;
870 p_instance
->psz_name
= strdup( psz_name
);
872 p_instance
->p_item
= input_item_New( NULL
, NULL
);
874 p_instance
->i_index
= 0;
875 p_instance
->b_sout_keep
= false;
876 p_instance
->p_parent
= vlc_object_create( p_vlm
, sizeof (vlc_object_t
) );
877 p_instance
->p_input
= NULL
;
878 p_instance
->p_input_resource
= input_resource_New( p_instance
->p_parent
);
882 static void vlm_MediaInstanceDelete( vlm_t
*p_vlm
, int64_t id
, vlm_media_instance_sys_t
*p_instance
, vlm_media_sys_t
*p_media
)
884 input_thread_t
*p_input
= p_instance
->p_input
;
887 input_Stop( p_input
);
888 input_Close( p_input
);
890 vlm_SendEventMediaInstanceStopped( p_vlm
, id
, p_media
->cfg
.psz_name
);
892 input_resource_Terminate( p_instance
->p_input_resource
);
893 input_resource_Release( p_instance
->p_input_resource
);
894 vlc_object_release( p_instance
->p_parent
);
896 TAB_REMOVE( p_media
->i_instance
, p_media
->instance
, p_instance
);
897 input_item_Release( p_instance
->p_item
);
898 free( p_instance
->psz_name
);
903 static int vlm_ControlMediaInstanceStart( vlm_t
*p_vlm
, int64_t id
, const char *psz_id
, int i_input_index
, const char *psz_vod_output
)
905 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
906 vlm_media_instance_sys_t
*p_instance
;
909 if( !p_media
|| !p_media
->cfg
.b_enabled
|| p_media
->cfg
.i_input
<= 0 )
912 /* TODO support multiple input for VOD with sout-keep ? */
914 if( ( p_media
->cfg
.b_vod
&& !psz_vod_output
) || ( !p_media
->cfg
.b_vod
&& psz_vod_output
) )
917 if( i_input_index
< 0 || i_input_index
>= p_media
->cfg
.i_input
)
920 p_instance
= vlm_ControlMediaInstanceGetByName( p_media
, psz_id
);
923 vlm_media_t
*p_cfg
= &p_media
->cfg
;
925 p_instance
= vlm_MediaInstanceNew( p_vlm
, psz_id
);
931 var_Create( p_instance
->p_parent
, "vod-media", VLC_VAR_ADDRESS
);
932 var_SetAddress( p_instance
->p_parent
, "vod-media",
933 p_media
->vod
.p_media
);
934 var_Create( p_instance
->p_parent
, "vod-session", VLC_VAR_STRING
);
935 var_SetString( p_instance
->p_parent
, "vod-session", psz_id
);
938 if( p_cfg
->psz_output
!= NULL
|| psz_vod_output
!= NULL
)
941 if( asprintf( &psz_buffer
, "sout=%s%s%s",
942 p_cfg
->psz_output
? p_cfg
->psz_output
: "",
943 (p_cfg
->psz_output
&& psz_vod_output
) ? ":" : psz_vod_output
? "#" : "",
944 psz_vod_output
? psz_vod_output
: "" ) != -1 )
946 input_item_AddOption( p_instance
->p_item
, psz_buffer
, VLC_INPUT_OPTION_TRUSTED
);
951 for( int i
= 0; i
< p_cfg
->i_option
; i
++ )
953 if( !strcmp( p_cfg
->ppsz_option
[i
], "sout-keep" ) )
954 p_instance
->b_sout_keep
= true;
955 else if( !strcmp( p_cfg
->ppsz_option
[i
], "nosout-keep" ) || !strcmp( p_cfg
->ppsz_option
[i
], "no-sout-keep" ) )
956 p_instance
->b_sout_keep
= false;
958 input_item_AddOption( p_instance
->p_item
, p_cfg
->ppsz_option
[i
], VLC_INPUT_OPTION_TRUSTED
);
960 TAB_APPEND( p_media
->i_instance
, p_media
->instance
, p_instance
);
963 /* Stop old instance */
964 input_thread_t
*p_input
= p_instance
->p_input
;
967 if( p_instance
->i_index
== i_input_index
)
969 int state
= var_GetInteger( p_input
, "state" );
970 if( state
== PAUSE_S
)
971 var_SetInteger( p_input
, "state", PLAYING_S
);
975 input_Stop( p_input
);
976 input_Close( p_input
);
978 if( !p_instance
->b_sout_keep
)
979 input_resource_TerminateSout( p_instance
->p_input_resource
);
980 input_resource_TerminateVout( p_instance
->p_input_resource
);
982 vlm_SendEventMediaInstanceStopped( p_vlm
, id
, p_media
->cfg
.psz_name
);
986 p_instance
->i_index
= i_input_index
;
987 if( strstr( p_media
->cfg
.ppsz_input
[p_instance
->i_index
], "://" ) == NULL
)
989 char *psz_uri
= vlc_path2uri(
990 p_media
->cfg
.ppsz_input
[p_instance
->i_index
], NULL
);
991 input_item_SetURI( p_instance
->p_item
, psz_uri
) ;
995 input_item_SetURI( p_instance
->p_item
, p_media
->cfg
.ppsz_input
[p_instance
->i_index
] ) ;
997 if( asprintf( &psz_log
, _("Media: %s"), p_media
->cfg
.psz_name
) != -1 )
999 p_instance
->p_input
= input_Create( p_instance
->p_parent
,
1000 p_instance
->p_item
, psz_log
,
1001 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
);