1 /*****************************************************************************
2 * vlm.c: VLM interface plugin
3 *****************************************************************************
4 * Copyright (C) 2000-2005 the VideoLAN team
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
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 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 General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
36 #include <ctype.h> /* tolower() */
42 # include <sys/time.h> /* gettimeofday() */
46 #include <sys/time.h> /* gettimeofday() */
49 #include <time.h> /* ctime() */
50 #if defined (WIN32) && !defined (UNDER_CE)
51 #include <sys/timeb.h> /* ftime() */
54 #include <vlc_input.h>
55 #include <vlc_stream.h>
56 #include "vlm_internal.h"
57 #include "vlm_event.h"
60 #include "../stream_output/stream_output.h"
61 #include "../libvlc.h"
63 /*****************************************************************************
65 *****************************************************************************/
67 static void vlm_Destructor( vlm_t *p_vlm );
68 static void* Manage( void * );
69 static int vlm_MediaVodControl( void *, vod_media_t *, const char *, int, va_list );
71 static int InputEventPreparse( vlc_object_t *p_this, char const *psz_cmd,
72 vlc_value_t oldval, vlc_value_t newval, void *p_data )
74 VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
75 vlc_sem_t *p_sem_preparse = p_data;
77 if( newval.i_int == INPUT_EVENT_DEAD ||
78 newval.i_int == INPUT_EVENT_ITEM_META )
79 vlc_sem_post( p_sem_preparse );
84 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
85 vlc_value_t oldval, vlc_value_t newval,
90 input_thread_t *p_input = (input_thread_t *)p_this;
91 vlm_t *p_vlm = libvlc_priv( p_input->p_libvlc )->p_vlm;
93 vlm_media_sys_t *p_media = p_data;
94 const char *psz_instance_name = NULL;
96 if( newval.i_int == INPUT_EVENT_STATE )
98 for( int i = 0; i < p_media->i_instance; i++ )
100 if( p_media->instance[i]->p_input == p_input )
102 psz_instance_name = p_media->instance[i]->psz_name;
106 vlm_SendEventMediaInstanceState( p_vlm, p_media->cfg.id, p_media->cfg.psz_name, psz_instance_name, var_GetInteger( p_input, "state" ) );
108 vlc_mutex_lock( &p_vlm->lock_manage );
109 p_vlm->input_state_changed = true;
110 vlc_cond_signal( &p_vlm->wait_manage );
111 vlc_mutex_unlock( &p_vlm->lock_manage );
116 static vlc_mutex_t vlm_mutex = VLC_STATIC_MUTEX;
119 /*****************************************************************************
121 *****************************************************************************/
122 vlm_t *vlm_New ( vlc_object_t *p_this )
124 vlm_t *p_vlm = NULL, **pp_vlm = &(libvlc_priv (p_this->p_libvlc)->p_vlm);
126 static const char vlm_object_name[] = "vlm daemon";
128 /* Avoid multiple creation */
129 vlc_mutex_lock( &vlm_mutex );
133 { /* VLM already exists */
134 vlc_object_hold( p_vlm );
135 vlc_mutex_unlock( &vlm_mutex );
139 msg_Dbg( p_this, "creating VLM" );
141 p_vlm = vlc_custom_create( p_this, sizeof( *p_vlm ), VLC_OBJECT_GENERIC,
145 vlc_mutex_unlock( &vlm_mutex );
149 vlc_mutex_init( &p_vlm->lock );
150 vlc_mutex_init( &p_vlm->lock_manage );
151 vlc_cond_init_daytime( &p_vlm->wait_manage );
152 p_vlm->input_state_changed = false;
154 TAB_INIT( p_vlm->i_media, p_vlm->media );
155 TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
157 var_Create( p_vlm, "intf-event", VLC_VAR_ADDRESS );
158 vlc_object_attach( p_vlm, p_this->p_libvlc );
160 if( vlc_clone( &p_vlm->thread, Manage, p_vlm, VLC_THREAD_PRIORITY_LOW ) )
162 vlc_cond_destroy( &p_vlm->wait_manage );
163 vlc_mutex_destroy( &p_vlm->lock );
164 vlc_mutex_destroy( &p_vlm->lock_manage );
165 vlc_object_release( p_vlm );
166 vlc_mutex_unlock( &vlm_mutex );
170 *pp_vlm = p_vlm; /* for future reference */
172 /* Load our configuration file */
173 psz_vlmconf = var_CreateGetString( p_vlm, "vlm-conf" );
174 if( psz_vlmconf && *psz_vlmconf )
176 vlm_message_t *p_message = NULL;
177 char *psz_buffer = NULL;
179 msg_Dbg( p_this, "loading VLM configuration" );
180 if( asprintf(&psz_buffer, "load %s", psz_vlmconf ) != -1 )
182 msg_Dbg( p_this, "%s", psz_buffer );
183 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
184 msg_Warn( p_this, "error while loading the configuration file" );
186 vlm_MessageDelete( p_message );
192 vlc_object_set_destructor( p_vlm, (vlc_destructor_t)vlm_Destructor );
193 vlc_mutex_unlock( &vlm_mutex );
198 /*****************************************************************************
200 *****************************************************************************/
201 void vlm_Delete( vlm_t *p_vlm )
203 /* vlm_Delete() is serialized against itself, and against vlm_New().
204 * This way, vlm_Destructor () (called from vlc_objet_release() above)
205 * is serialized against setting libvlc_priv->p_vlm from vlm_New(). */
206 vlc_mutex_lock( &vlm_mutex );
207 vlc_object_release( p_vlm );
208 vlc_mutex_unlock( &vlm_mutex );
211 /*****************************************************************************
213 *****************************************************************************/
214 static void vlm_Destructor( vlm_t *p_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->schedule, p_vlm->schedule );
222 vlc_mutex_unlock( &p_vlm->lock );
224 libvlc_priv(p_vlm->p_libvlc)->p_vlm = NULL;
225 vlc_object_kill( p_vlm );
229 module_unneed( p_vlm->p_vod, p_vlm->p_vod->p_module );
230 vlc_object_release( p_vlm->p_vod );
233 vlc_mutex_lock( &p_vlm->lock_manage );
234 p_vlm->input_state_changed = true;
235 vlc_cond_signal( &p_vlm->wait_manage );
236 vlc_mutex_unlock( &p_vlm->lock_manage );
238 /*vlc_cancel( p_vlm->thread ); */
239 vlc_join( p_vlm->thread, NULL );
241 vlc_cond_destroy( &p_vlm->wait_manage );
242 vlc_mutex_destroy( &p_vlm->lock );
243 vlc_mutex_destroy( &p_vlm->lock_manage );
246 /*****************************************************************************
247 * vlm_ExecuteCommand:
248 *****************************************************************************/
249 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
250 vlm_message_t **pp_message)
254 vlc_mutex_lock( &p_vlm->lock );
255 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
256 vlc_mutex_unlock( &p_vlm->lock );
262 int64_t vlm_Date(void)
264 #if defined (WIN32) && !defined (UNDER_CE)
267 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
269 struct timeval tv_date;
271 /* gettimeofday() cannot fail given &tv_date is a valid address */
272 (void)gettimeofday( &tv_date, NULL );
273 return (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec;
278 /*****************************************************************************
280 *****************************************************************************/
281 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
282 const char *psz_id, int i_query, va_list args )
284 vlm_t *vlm = (vlm_t *)p_private;
289 if( !p_private || !p_vod_media )
292 vlc_mutex_lock( &vlm->lock );
295 for( i = 0, id = -1; i < vlm->i_media; i++ )
297 if( p_vod_media == vlm->media[i]->vod.p_media )
299 id = vlm->media[i]->cfg.id;
305 vlc_mutex_unlock( &vlm->lock );
312 psz = (const char *)va_arg( args, const char * );
313 i_ret = vlm_ControlInternal( vlm, VLM_START_MEDIA_VOD_INSTANCE, id, psz_id, 0, psz );
316 case VOD_MEDIA_PAUSE:
317 i_ret = vlm_ControlInternal( vlm, VLM_PAUSE_MEDIA_INSTANCE, id, psz_id );
321 i_ret = vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, id, psz_id );
326 double d_position = (double)va_arg( args, double );
327 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position/100.0 );
331 case VOD_MEDIA_REWIND:
333 double d_scale = (double)va_arg( args, double );
336 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
337 d_position -= (d_scale / 1000.0);
338 if( d_position < 0.0 )
340 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
344 case VOD_MEDIA_FORWARD:
346 double d_scale = (double)va_arg( args, double );
349 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
350 d_position += (d_scale / 1000.0);
351 if( d_position > 1.0 )
353 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
358 i_ret = VLC_EGENERIC;
362 vlc_mutex_unlock( &vlm->lock );
368 /*****************************************************************************
370 *****************************************************************************/
371 static void* Manage( void* p_object )
373 vlm_t *vlm = (vlm_t*)p_object;
377 mtime_t i_nextschedule = 0;
379 int canc = vlc_savecancel ();
380 i_lastcheck = vlm_Date();
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 while( !vlm->input_state_changed && !scheduled_command )
392 scheduled_command = vlc_cond_timedwait( &vlm->wait_manage, &vlm->lock_manage, i_nextschedule ) != 0;
394 vlc_cond_wait( &vlm->wait_manage, &vlm->lock_manage );
396 vlm->input_state_changed = false;
397 vlc_mutex_unlock( &vlm->lock_manage );
398 /* destroy the inputs that wants to die, and launch the next input */
399 vlc_mutex_lock( &vlm->lock );
400 for( i = 0; i < vlm->i_media; i++ )
402 vlm_media_sys_t *p_media = vlm->media[i];
404 for( j = 0; j < p_media->i_instance; )
406 vlm_media_instance_sys_t *p_instance = p_media->instance[j];
408 if( p_instance->p_input && ( p_instance->p_input->b_eof || p_instance->p_input->b_error ) )
410 int i_new_input_index;
413 i_new_input_index = p_instance->i_index + 1;
414 if( !p_media->cfg.b_vod && p_media->cfg.broadcast.b_loop && i_new_input_index >= p_media->cfg.i_input )
415 i_new_input_index = 0;
417 /* FIXME implement multiple input with VOD */
418 if( p_media->cfg.b_vod || i_new_input_index >= p_media->cfg.i_input )
419 vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, p_instance->psz_name );
421 vlm_ControlInternal( vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, p_instance->psz_name, i_new_input_index );
436 for( i = 0; i < vlm->i_schedule; i++ )
438 mtime_t i_real_date = vlm->schedule[i]->i_date;
440 if( vlm->schedule[i]->b_enabled == true )
442 if( vlm->schedule[i]->i_date == 0 ) // now !
444 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
445 i_real_date = i_time;
447 else if( vlm->schedule[i]->i_period != 0 )
450 while( vlm->schedule[i]->i_date + j *
451 vlm->schedule[i]->i_period <= i_lastcheck &&
452 ( vlm->schedule[i]->i_repeat > j ||
453 vlm->schedule[i]->i_repeat == -1 ) )
458 i_real_date = vlm->schedule[i]->i_date + j *
459 vlm->schedule[i]->i_period;
462 if( i_real_date <= i_time )
464 if( i_real_date > i_lastcheck )
466 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
468 TAB_APPEND( i_scheduled_commands,
469 ppsz_scheduled_commands,
470 strdup(vlm->schedule[i]->command[j] ) );
476 i_nextschedule = i_real_date;
481 while( i_scheduled_commands )
483 vlm_message_t *message = NULL;
484 char *psz_command = ppsz_scheduled_commands[0];
485 ExecuteCommand( vlm, psz_command,&message );
487 /* for now, drop the message */
488 vlm_MessageDelete( message );
489 TAB_REMOVE( i_scheduled_commands,
490 ppsz_scheduled_commands,
495 i_lastcheck = i_time;
496 vlc_mutex_unlock( &vlm->lock );
500 vlc_restorecancel (canc);
511 int i_connection_count;
512 int i_connection_active;
521 } vlm_media_status_t;
525 static vlm_media_sys_t *vlm_ControlMediaGetById( vlm_t *p_vlm, int64_t id )
529 for( i = 0; i < p_vlm->i_media; i++ )
531 if( p_vlm->media[i]->cfg.id == id )
532 return p_vlm->media[i];
536 static vlm_media_sys_t *vlm_ControlMediaGetByName( vlm_t *p_vlm, const char *psz_name )
540 for( i = 0; i < p_vlm->i_media; i++ )
542 if( !strcmp( p_vlm->media[i]->cfg.psz_name, psz_name ) )
543 return p_vlm->media[i];
547 static int vlm_MediaDescriptionCheck( vlm_t *p_vlm, vlm_media_t *p_cfg )
551 if( !p_cfg || !p_cfg->psz_name ||
552 !strcmp( p_cfg->psz_name, "all" ) || !strcmp( p_cfg->psz_name, "media" ) || !strcmp( p_cfg->psz_name, "schedule" ) )
555 for( i = 0; i < p_vlm->i_media; i++ )
557 if( p_vlm->media[i]->cfg.id == p_cfg->id )
559 if( !strcmp( p_vlm->media[i]->cfg.psz_name, p_cfg->psz_name ) )
566 /* Called after a media description is changed/added */
567 static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
569 vlm_media_t *p_cfg = &p_media->cfg;
570 /* Check if we need to create/delete a vod media */
571 if( p_cfg->b_vod && p_vlm->p_vod )
573 if( !p_cfg->b_enabled && p_media->vod.p_media )
575 p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
576 p_media->vod.p_media = NULL;
578 else if( p_cfg->b_enabled && !p_media->vod.p_media && p_cfg->i_input )
580 /* Pre-parse the input */
581 input_thread_t *p_input;
587 vlc_gc_decref( p_media->vod.p_item );
588 p_media->vod.p_item = input_item_New( p_vlm, p_cfg->ppsz_input[0],
591 if( p_cfg->psz_output )
593 if( asprintf( &psz_output, "%s:description", p_cfg->psz_output ) == -1 )
597 psz_output = strdup( "#description" );
599 if( psz_output && asprintf( &psz_dup, "sout=%s", psz_output ) != -1 )
601 input_item_AddOption( p_media->vod.p_item, psz_dup, VLC_INPUT_OPTION_TRUSTED );
606 for( i = 0; i < p_cfg->i_option; i++ )
607 input_item_AddOption( p_media->vod.p_item,
608 p_cfg->ppsz_option[i], VLC_INPUT_OPTION_TRUSTED );
610 if( asprintf( &psz_header, _("Media: %s"), p_cfg->psz_name ) == -1 )
613 p_input = input_Create( p_vlm->p_vod, p_media->vod.p_item, psz_header, NULL );
616 vlc_sem_t sem_preparse;
617 vlc_sem_init( &sem_preparse, 0 );
618 var_AddCallback( p_input, "intf-event", InputEventPreparse, &sem_preparse );
620 if( !input_Start( p_input ) )
622 while( !p_input->b_dead && ( !p_cfg->vod.psz_mux || !input_item_IsPreparsed( p_media->vod.p_item ) ) )
623 vlc_sem_wait( &sem_preparse );
626 var_DelCallback( p_input, "intf-event", InputEventPreparse, &sem_preparse );
627 vlc_sem_destroy( &sem_preparse );
629 input_Stop( p_input, true );
630 vlc_thread_join( p_input );
631 vlc_object_release( p_input );
635 if( p_cfg->vod.psz_mux )
638 es_format_t es, *p_es = &es;
639 union { char text[5]; uint32_t value; } fourcc;
641 sprintf( fourcc.text, "%4.4s", p_cfg->vod.psz_mux );
642 fourcc.text[0] = tolower(fourcc.text[0]);
643 fourcc.text[1] = tolower(fourcc.text[1]);
644 fourcc.text[2] = tolower(fourcc.text[2]);
645 fourcc.text[3] = tolower(fourcc.text[3]);
647 /* XXX: Don't do it that way, but properly use a new input item ref. */
648 item = *p_media->vod.p_item;
651 es_format_Init( &es, VIDEO_ES, fourcc.value );
653 p_media->vod.p_media =
654 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
658 p_media->vod.p_media =
659 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, p_media->vod.p_item );
663 else if ( p_cfg->b_vod )
664 msg_Err( p_vlm, "vod server is not loaded" );
667 /* TODO start media if needed */
670 /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
672 vlm_SendEventMediaChanged( p_vlm, p_cfg->id, p_cfg->psz_name );
675 static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
677 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
680 if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
682 if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
687 /* TODO check what are the changes being done (stop instance if needed) */
690 vlm_media_Clean( &p_media->cfg );
691 vlm_media_Copy( &p_media->cfg, p_cfg );
693 return vlm_OnMediaUpdate( p_vlm, p_media );
696 static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id )
698 vlm_media_sys_t *p_media;
700 if( vlm_MediaDescriptionCheck( p_vlm, p_cfg ) || vlm_ControlMediaGetByName( p_vlm, p_cfg->psz_name ) )
702 msg_Err( p_vlm, "invalid media description" );
705 /* Check if we need to load the VOD server */
706 if( p_cfg->b_vod && !p_vlm->p_vod )
708 p_vlm->p_vod = vlc_custom_create( VLC_OBJECT(p_vlm), sizeof( vod_t ),
709 VLC_OBJECT_GENERIC, "vod server" );
710 vlc_object_attach( p_vlm->p_vod, p_vlm->p_libvlc );
711 p_vlm->p_vod->p_module = module_need( p_vlm->p_vod, "vod server", NULL, false );
712 if( !p_vlm->p_vod->p_module )
714 msg_Err( p_vlm, "cannot find vod server" );
715 vlc_object_release( p_vlm->p_vod );
720 p_vlm->p_vod->p_data = p_vlm;
721 p_vlm->p_vod->pf_media_control = vlm_MediaVodControl;
724 p_media = calloc( 1, sizeof( vlm_media_sys_t ) );
728 vlm_media_Copy( &p_media->cfg, p_cfg );
729 p_media->cfg.id = p_vlm->i_id++;
730 /* FIXME do we do something here if enabled is true ? */
732 p_media->vod.p_item = input_item_New( p_vlm, NULL, NULL );
734 p_media->vod.p_media = NULL;
735 TAB_INIT( p_media->i_instance, p_media->instance );
738 TAB_APPEND( p_vlm->i_media, p_vlm->media, p_media );
741 *p_id = p_media->cfg.id;
744 vlm_SendEventMediaAdded( p_vlm, p_media->cfg.id, p_media->cfg.psz_name );
745 return vlm_OnMediaUpdate( p_vlm, p_media );
748 static int vlm_ControlMediaDel( vlm_t *p_vlm, int64_t id )
750 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
755 while( p_media->i_instance > 0 )
756 vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, p_media->instance[0]->psz_name );
758 if( p_media->cfg.b_vod )
760 p_media->cfg.b_enabled = false;
761 vlm_OnMediaUpdate( p_vlm, p_media );
765 vlm_SendEventMediaRemoved( p_vlm, id, p_media->cfg.psz_name );
767 vlm_media_Clean( &p_media->cfg );
769 vlc_gc_decref( p_media->vod.p_item );
771 if( p_media->vod.p_media )
772 p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
774 TAB_REMOVE( p_vlm->i_media, p_vlm->media, p_media );
780 static int vlm_ControlMediaGets( vlm_t *p_vlm, vlm_media_t ***ppp_dsc, int *pi_dsc )
782 vlm_media_t **pp_dsc;
786 TAB_INIT( i_dsc, pp_dsc );
787 for( i = 0; i < p_vlm->i_media; i++ )
789 vlm_media_t *p_dsc = vlm_media_Duplicate( &p_vlm->media[i]->cfg );
790 TAB_APPEND( i_dsc, pp_dsc, p_dsc );
798 static int vlm_ControlMediaClear( vlm_t *p_vlm )
800 while( p_vlm->i_media > 0 )
801 vlm_ControlMediaDel( p_vlm, p_vlm->media[0]->cfg.id );
805 static int vlm_ControlMediaGet( vlm_t *p_vlm, int64_t id, vlm_media_t **pp_dsc )
807 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
811 *pp_dsc = vlm_media_Duplicate( &p_media->cfg );
814 static int vlm_ControlMediaGetId( vlm_t *p_vlm, const char *psz_name, int64_t *p_id )
816 vlm_media_sys_t *p_media = vlm_ControlMediaGetByName( p_vlm, psz_name );
820 *p_id = p_media->cfg.id;
824 static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
828 for( i = 0; i < p_media->i_instance; i++ )
830 const char *psz = p_media->instance[i]->psz_name;
831 if( ( psz == NULL && psz_id == NULL ) ||
832 ( psz && psz_id && !strcmp( psz, psz_id ) ) )
833 return p_media->instance[i];
837 static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
839 vlm_media_instance_sys_t *p_instance = calloc( 1, sizeof(vlm_media_instance_sys_t) );
843 p_instance->psz_name = NULL;
845 p_instance->psz_name = strdup( psz_name );
847 p_instance->p_item = input_item_New( p_vlm, NULL, NULL );
849 p_instance->i_index = 0;
850 p_instance->b_sout_keep = false;
851 p_instance->p_input = NULL;
852 p_instance->p_input_resource = NULL;
856 static void vlm_MediaInstanceDelete( vlm_t *p_vlm, int64_t id, vlm_media_instance_sys_t *p_instance, vlm_media_sys_t *p_media )
858 input_thread_t *p_input = p_instance->p_input;
861 input_Stop( p_input, true );
862 vlc_thread_join( p_input );
864 var_DelCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
865 vlc_object_release( p_input );
867 vlm_SendEventMediaInstanceStopped( p_vlm, id, p_media->cfg.psz_name );
869 if( p_instance->p_input_resource )
870 input_resource_Delete( p_instance->p_input_resource );
872 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
873 vlc_gc_decref( p_instance->p_item );
874 free( p_instance->psz_name );
879 static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
881 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
882 vlm_media_instance_sys_t *p_instance;
885 if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
888 /* TODO support multiple input for VOD with sout-keep ? */
890 if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
893 if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
896 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
899 vlm_media_t *p_cfg = &p_media->cfg;
902 p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
906 if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
909 if( asprintf( &psz_buffer, "sout=%s%s%s",
910 p_cfg->psz_output ? p_cfg->psz_output : "",
911 (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
912 psz_vod_output ? psz_vod_output : "" ) != -1 )
914 input_item_AddOption( p_instance->p_item, psz_buffer, VLC_INPUT_OPTION_TRUSTED );
919 for( i = 0; i < p_cfg->i_option; i++ )
921 if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
922 p_instance->b_sout_keep = true;
923 else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
924 p_instance->b_sout_keep = false;
926 input_item_AddOption( p_instance->p_item, p_cfg->ppsz_option[i], VLC_INPUT_OPTION_TRUSTED );
928 TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
931 /* Stop old instance */
932 input_thread_t *p_input = p_instance->p_input;
935 if( p_instance->i_index == i_input_index &&
936 !p_input->b_eof && !p_input->b_error )
938 if( var_GetInteger( p_input, "state" ) == PAUSE_S )
939 var_SetInteger( p_input, "state", PLAYING_S );
944 input_Stop( p_input, true );
945 vlc_thread_join( p_input );
947 var_DelCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
948 vlc_object_release( p_input );
950 if( !p_instance->b_sout_keep )
951 input_resource_TerminateSout( p_instance->p_input_resource );
952 input_resource_TerminateVout( p_instance->p_input_resource );
954 vlm_SendEventMediaInstanceStopped( p_vlm, id, p_media->cfg.psz_name );
958 p_instance->i_index = i_input_index;
959 input_item_SetURI( p_instance->p_item, p_media->cfg.ppsz_input[p_instance->i_index] ) ;
961 if( asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name ) != -1 )
963 vlc_object_t *p_parent = p_media->cfg.b_vod ?
964 VLC_OBJECT(p_vlm->p_vod) :
965 VLC_OBJECT(p_vlm->p_libvlc);
966 if( !p_instance->p_input_resource )
967 p_instance->p_input_resource = input_resource_New( VLC_OBJECT( p_vlm ) );
968 p_instance->p_input = input_Create( p_parent, p_instance->p_item,
969 psz_log, p_instance->p_input_resource );
970 if( p_instance->p_input )
972 var_AddCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
973 if( input_Start( p_instance->p_input ) != VLC_SUCCESS )
975 var_DelCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
976 vlc_object_release( p_instance->p_input );
977 p_instance->p_input = NULL;
981 if( !p_instance->p_input )
983 vlm_MediaInstanceDelete( p_vlm, id, p_instance, p_media );
987 vlm_SendEventMediaInstanceStarted( p_vlm, id, p_media->cfg.psz_name );
995 static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
997 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
998 vlm_media_instance_sys_t *p_instance;
1001 return VLC_EGENERIC;
1003 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1005 return VLC_EGENERIC;
1007 vlm_MediaInstanceDelete( p_vlm, id, p_instance, p_media );
1011 static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
1013 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1014 vlm_media_instance_sys_t *p_instance;
1018 return VLC_EGENERIC;
1020 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1021 if( !p_instance || !p_instance->p_input )
1022 return VLC_EGENERIC;
1024 /* Toggle pause state */
1025 i_state = var_GetInteger( p_instance->p_input, "state" );
1026 if( i_state == PAUSE_S )
1027 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
1028 else if( i_state == PLAYING_S )
1029 var_SetInteger( p_instance->p_input, "state", PAUSE_S );
1032 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
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 );
1041 if( !p_instance || !p_instance->p_input )
1042 return VLC_EGENERIC;
1045 *pi_time = var_GetTime( p_instance->p_input, "time" );
1047 *pd_position = var_GetFloat( p_instance->p_input, "position" );
1050 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t i_time, double d_position )
1052 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1053 vlm_media_instance_sys_t *p_instance;
1056 return VLC_EGENERIC;
1058 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1059 if( !p_instance || !p_instance->p_input )
1060 return VLC_EGENERIC;
1063 return var_SetTime( p_instance->p_input, "time", i_time );
1064 else if( d_position >= 0 && d_position <= 100 )
1065 return var_SetFloat( p_instance->p_input, "position", d_position );
1066 return VLC_EGENERIC;
1069 static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_instance_t ***ppp_idsc, int *pi_instance )
1071 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1072 vlm_media_instance_t **pp_idsc;
1077 return VLC_EGENERIC;
1079 TAB_INIT( i_idsc, pp_idsc );
1080 for( i = 0; i < p_media->i_instance; i++ )
1082 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1083 vlm_media_instance_t *p_idsc = vlm_media_instance_New();
1085 if( p_instance->psz_name )
1086 p_idsc->psz_name = strdup( p_instance->psz_name );
1087 if( p_instance->p_input )
1089 p_idsc->i_time = var_GetTime( p_instance->p_input, "time" );
1090 p_idsc->i_length = var_GetTime( p_instance->p_input, "length" );
1091 p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
1092 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
1093 p_idsc->b_paused = true;
1094 p_idsc->i_rate = INPUT_RATE_DEFAULT
1095 / var_GetFloat( p_instance->p_input, "rate" );
1098 TAB_APPEND( i_idsc, pp_idsc, p_idsc );
1100 *ppp_idsc = pp_idsc;
1101 *pi_instance = i_idsc;
1105 static int vlm_ControlMediaInstanceClear( vlm_t *p_vlm, int64_t id )
1107 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1110 return VLC_EGENERIC;
1112 while( p_media->i_instance > 0 )
1113 vlm_ControlMediaInstanceStop( p_vlm, id, p_media->instance[0]->psz_name );
1118 static int vlm_ControlScheduleClear( vlm_t *p_vlm )
1120 while( p_vlm->i_schedule > 0 )
1121 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0] );
1126 static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
1129 vlm_media_t **pp_dsc;
1130 vlm_media_t ***ppp_dsc;
1131 vlm_media_instance_t ***ppp_idsc;
1133 const char *psz_vod;
1147 case VLM_GET_MEDIAS:
1148 ppp_dsc = (vlm_media_t ***)va_arg( args, vlm_media_t *** );
1149 pi_int = (int *)va_arg( args, int * );
1150 return vlm_ControlMediaGets( p_vlm, ppp_dsc, pi_int );
1152 case VLM_CLEAR_MEDIAS:
1153 return vlm_ControlMediaClear( p_vlm );
1155 case VLM_CHANGE_MEDIA:
1156 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
1157 return vlm_ControlMediaChange( p_vlm, p_dsc );
1160 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
1161 p_id = (int64_t*)va_arg( args, int64_t * );
1162 return vlm_ControlMediaAdd( p_vlm, p_dsc, p_id );
1165 id = (int64_t)va_arg( args, int64_t );
1166 return vlm_ControlMediaDel( p_vlm, id );
1169 id = (int64_t)va_arg( args, int64_t );
1170 pp_dsc = (vlm_media_t **)va_arg( args, vlm_media_t ** );
1171 return vlm_ControlMediaGet( p_vlm, id, pp_dsc );
1173 case VLM_GET_MEDIA_ID:
1174 psz_id = (const char*)va_arg( args, const char * );
1175 p_id = (int64_t*)va_arg( args, int64_t * );
1176 return vlm_ControlMediaGetId( p_vlm, psz_id, p_id );
1179 /* Media instance control */
1180 case VLM_GET_MEDIA_INSTANCES:
1181 id = (int64_t)va_arg( args, int64_t );
1182 ppp_idsc = (vlm_media_instance_t ***)va_arg( args, vlm_media_instance_t *** );
1183 pi_int = (int *)va_arg( args, int *);
1184 return vlm_ControlMediaInstanceGets( p_vlm, id, ppp_idsc, pi_int );
1186 case VLM_CLEAR_MEDIA_INSTANCES:
1187 id = (int64_t)va_arg( args, int64_t );
1188 return vlm_ControlMediaInstanceClear( p_vlm, id );
1191 case VLM_START_MEDIA_BROADCAST_INSTANCE:
1192 id = (int64_t)va_arg( args, int64_t );
1193 psz_id = (const char*)va_arg( args, const char* );
1194 i_int = (int)va_arg( args, int );
1195 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, NULL );
1197 case VLM_START_MEDIA_VOD_INSTANCE:
1198 id = (int64_t)va_arg( args, int64_t );
1199 psz_id = (const char*)va_arg( args, const char* );
1200 i_int = (int)va_arg( args, int );
1201 psz_vod = (const char*)va_arg( args, const char* );
1203 return VLC_EGENERIC;
1204 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, psz_vod );
1206 case VLM_STOP_MEDIA_INSTANCE:
1207 id = (int64_t)va_arg( args, int64_t );
1208 psz_id = (const char*)va_arg( args, const char* );
1209 return vlm_ControlMediaInstanceStop( p_vlm, id, psz_id );
1211 case VLM_PAUSE_MEDIA_INSTANCE:
1212 id = (int64_t)va_arg( args, int64_t );
1213 psz_id = (const char*)va_arg( args, const char* );
1214 return vlm_ControlMediaInstancePause( p_vlm, id, psz_id );
1216 case VLM_GET_MEDIA_INSTANCE_TIME:
1217 id = (int64_t)va_arg( args, int64_t );
1218 psz_id = (const char*)va_arg( args, const char* );
1219 pi_i64 = (int64_t*)va_arg( args, int64_t * );
1220 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, pi_i64, NULL );
1221 case VLM_GET_MEDIA_INSTANCE_POSITION:
1222 id = (int64_t)va_arg( args, int64_t );
1223 psz_id = (const char*)va_arg( args, const char* );
1224 pd_double = (double*)va_arg( args, double* );
1225 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, NULL, pd_double );
1227 case VLM_SET_MEDIA_INSTANCE_TIME:
1228 id = (int64_t)va_arg( args, int64_t );
1229 psz_id = (const char*)va_arg( args, const char* );
1230 i_i64 = (int64_t)va_arg( args, int64_t);
1231 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, i_i64, -1 );
1232 case VLM_SET_MEDIA_INSTANCE_POSITION:
1233 id = (int64_t)va_arg( args, int64_t );
1234 psz_id = (const char*)va_arg( args, const char* );
1235 d_double = (double)va_arg( args, double );
1236 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, -1, d_double );
1238 case VLM_CLEAR_SCHEDULES:
1239 return vlm_ControlScheduleClear( p_vlm );
1242 msg_Err( p_vlm, "unknown VLM query" );
1243 return VLC_EGENERIC;
1247 int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... )
1252 va_start( args, i_query );
1253 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
1259 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
1264 va_start( args, i_query );
1266 vlc_mutex_lock( &p_vlm->lock );
1267 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
1268 vlc_mutex_unlock( &p_vlm->lock );