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 )
871 input_resource_Terminate( p_instance->p_input_resource );
872 input_resource_Release( p_instance->p_input_resource );
875 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
876 vlc_gc_decref( p_instance->p_item );
877 free( p_instance->psz_name );
882 static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
884 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
885 vlm_media_instance_sys_t *p_instance;
888 if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
891 /* TODO support multiple input for VOD with sout-keep ? */
893 if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
896 if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
899 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
902 vlm_media_t *p_cfg = &p_media->cfg;
905 p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
909 if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
912 if( asprintf( &psz_buffer, "sout=%s%s%s",
913 p_cfg->psz_output ? p_cfg->psz_output : "",
914 (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
915 psz_vod_output ? psz_vod_output : "" ) != -1 )
917 input_item_AddOption( p_instance->p_item, psz_buffer, VLC_INPUT_OPTION_TRUSTED );
922 for( i = 0; i < p_cfg->i_option; i++ )
924 if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
925 p_instance->b_sout_keep = true;
926 else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
927 p_instance->b_sout_keep = false;
929 input_item_AddOption( p_instance->p_item, p_cfg->ppsz_option[i], VLC_INPUT_OPTION_TRUSTED );
931 TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
934 /* Stop old instance */
935 input_thread_t *p_input = p_instance->p_input;
938 if( p_instance->i_index == i_input_index &&
939 !p_input->b_eof && !p_input->b_error )
941 if( var_GetInteger( p_input, "state" ) == PAUSE_S )
942 var_SetInteger( p_input, "state", PLAYING_S );
947 input_Stop( p_input, true );
948 vlc_thread_join( p_input );
950 var_DelCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
951 vlc_object_release( p_input );
953 if( !p_instance->b_sout_keep )
954 input_resource_TerminateSout( p_instance->p_input_resource );
955 input_resource_TerminateVout( p_instance->p_input_resource );
957 vlm_SendEventMediaInstanceStopped( p_vlm, id, p_media->cfg.psz_name );
961 p_instance->i_index = i_input_index;
962 input_item_SetURI( p_instance->p_item, p_media->cfg.ppsz_input[p_instance->i_index] ) ;
964 if( asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name ) != -1 )
966 vlc_object_t *p_parent = p_media->cfg.b_vod ?
967 VLC_OBJECT(p_vlm->p_vod) :
968 VLC_OBJECT(p_vlm->p_libvlc);
969 if( !p_instance->p_input_resource )
970 p_instance->p_input_resource = input_resource_New( VLC_OBJECT( p_vlm ) );
971 p_instance->p_input = input_Create( p_parent, p_instance->p_item,
972 psz_log, p_instance->p_input_resource );
973 if( p_instance->p_input )
975 var_AddCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
976 if( input_Start( p_instance->p_input ) != VLC_SUCCESS )
978 var_DelCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
979 vlc_object_release( p_instance->p_input );
980 p_instance->p_input = NULL;
984 if( !p_instance->p_input )
986 vlm_MediaInstanceDelete( p_vlm, id, p_instance, p_media );
990 vlm_SendEventMediaInstanceStarted( p_vlm, id, p_media->cfg.psz_name );
998 static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
1000 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1001 vlm_media_instance_sys_t *p_instance;
1004 return VLC_EGENERIC;
1006 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1008 return VLC_EGENERIC;
1010 vlm_MediaInstanceDelete( p_vlm, id, p_instance, p_media );
1014 static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
1016 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1017 vlm_media_instance_sys_t *p_instance;
1021 return VLC_EGENERIC;
1023 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1024 if( !p_instance || !p_instance->p_input )
1025 return VLC_EGENERIC;
1027 /* Toggle pause state */
1028 i_state = var_GetInteger( p_instance->p_input, "state" );
1029 if( i_state == PAUSE_S )
1030 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
1031 else if( i_state == PLAYING_S )
1032 var_SetInteger( p_instance->p_input, "state", PAUSE_S );
1035 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
1037 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1038 vlm_media_instance_sys_t *p_instance;
1041 return VLC_EGENERIC;
1043 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1044 if( !p_instance || !p_instance->p_input )
1045 return VLC_EGENERIC;
1048 *pi_time = var_GetTime( p_instance->p_input, "time" );
1050 *pd_position = var_GetFloat( p_instance->p_input, "position" );
1053 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t i_time, double d_position )
1055 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1056 vlm_media_instance_sys_t *p_instance;
1059 return VLC_EGENERIC;
1061 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1062 if( !p_instance || !p_instance->p_input )
1063 return VLC_EGENERIC;
1066 return var_SetTime( p_instance->p_input, "time", i_time );
1067 else if( d_position >= 0 && d_position <= 100 )
1068 return var_SetFloat( p_instance->p_input, "position", d_position );
1069 return VLC_EGENERIC;
1072 static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_instance_t ***ppp_idsc, int *pi_instance )
1074 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1075 vlm_media_instance_t **pp_idsc;
1080 return VLC_EGENERIC;
1082 TAB_INIT( i_idsc, pp_idsc );
1083 for( i = 0; i < p_media->i_instance; i++ )
1085 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1086 vlm_media_instance_t *p_idsc = vlm_media_instance_New();
1088 if( p_instance->psz_name )
1089 p_idsc->psz_name = strdup( p_instance->psz_name );
1090 if( p_instance->p_input )
1092 p_idsc->i_time = var_GetTime( p_instance->p_input, "time" );
1093 p_idsc->i_length = var_GetTime( p_instance->p_input, "length" );
1094 p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
1095 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
1096 p_idsc->b_paused = true;
1097 p_idsc->i_rate = INPUT_RATE_DEFAULT
1098 / var_GetFloat( p_instance->p_input, "rate" );
1101 TAB_APPEND( i_idsc, pp_idsc, p_idsc );
1103 *ppp_idsc = pp_idsc;
1104 *pi_instance = i_idsc;
1108 static int vlm_ControlMediaInstanceClear( vlm_t *p_vlm, int64_t id )
1110 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1113 return VLC_EGENERIC;
1115 while( p_media->i_instance > 0 )
1116 vlm_ControlMediaInstanceStop( p_vlm, id, p_media->instance[0]->psz_name );
1121 static int vlm_ControlScheduleClear( vlm_t *p_vlm )
1123 while( p_vlm->i_schedule > 0 )
1124 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0] );
1129 static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
1132 vlm_media_t **pp_dsc;
1133 vlm_media_t ***ppp_dsc;
1134 vlm_media_instance_t ***ppp_idsc;
1136 const char *psz_vod;
1150 case VLM_GET_MEDIAS:
1151 ppp_dsc = (vlm_media_t ***)va_arg( args, vlm_media_t *** );
1152 pi_int = (int *)va_arg( args, int * );
1153 return vlm_ControlMediaGets( p_vlm, ppp_dsc, pi_int );
1155 case VLM_CLEAR_MEDIAS:
1156 return vlm_ControlMediaClear( p_vlm );
1158 case VLM_CHANGE_MEDIA:
1159 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
1160 return vlm_ControlMediaChange( p_vlm, p_dsc );
1163 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
1164 p_id = (int64_t*)va_arg( args, int64_t * );
1165 return vlm_ControlMediaAdd( p_vlm, p_dsc, p_id );
1168 id = (int64_t)va_arg( args, int64_t );
1169 return vlm_ControlMediaDel( p_vlm, id );
1172 id = (int64_t)va_arg( args, int64_t );
1173 pp_dsc = (vlm_media_t **)va_arg( args, vlm_media_t ** );
1174 return vlm_ControlMediaGet( p_vlm, id, pp_dsc );
1176 case VLM_GET_MEDIA_ID:
1177 psz_id = (const char*)va_arg( args, const char * );
1178 p_id = (int64_t*)va_arg( args, int64_t * );
1179 return vlm_ControlMediaGetId( p_vlm, psz_id, p_id );
1182 /* Media instance control */
1183 case VLM_GET_MEDIA_INSTANCES:
1184 id = (int64_t)va_arg( args, int64_t );
1185 ppp_idsc = (vlm_media_instance_t ***)va_arg( args, vlm_media_instance_t *** );
1186 pi_int = (int *)va_arg( args, int *);
1187 return vlm_ControlMediaInstanceGets( p_vlm, id, ppp_idsc, pi_int );
1189 case VLM_CLEAR_MEDIA_INSTANCES:
1190 id = (int64_t)va_arg( args, int64_t );
1191 return vlm_ControlMediaInstanceClear( p_vlm, id );
1194 case VLM_START_MEDIA_BROADCAST_INSTANCE:
1195 id = (int64_t)va_arg( args, int64_t );
1196 psz_id = (const char*)va_arg( args, const char* );
1197 i_int = (int)va_arg( args, int );
1198 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, NULL );
1200 case VLM_START_MEDIA_VOD_INSTANCE:
1201 id = (int64_t)va_arg( args, int64_t );
1202 psz_id = (const char*)va_arg( args, const char* );
1203 i_int = (int)va_arg( args, int );
1204 psz_vod = (const char*)va_arg( args, const char* );
1206 return VLC_EGENERIC;
1207 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, psz_vod );
1209 case VLM_STOP_MEDIA_INSTANCE:
1210 id = (int64_t)va_arg( args, int64_t );
1211 psz_id = (const char*)va_arg( args, const char* );
1212 return vlm_ControlMediaInstanceStop( p_vlm, id, psz_id );
1214 case VLM_PAUSE_MEDIA_INSTANCE:
1215 id = (int64_t)va_arg( args, int64_t );
1216 psz_id = (const char*)va_arg( args, const char* );
1217 return vlm_ControlMediaInstancePause( p_vlm, id, psz_id );
1219 case VLM_GET_MEDIA_INSTANCE_TIME:
1220 id = (int64_t)va_arg( args, int64_t );
1221 psz_id = (const char*)va_arg( args, const char* );
1222 pi_i64 = (int64_t*)va_arg( args, int64_t * );
1223 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, pi_i64, NULL );
1224 case VLM_GET_MEDIA_INSTANCE_POSITION:
1225 id = (int64_t)va_arg( args, int64_t );
1226 psz_id = (const char*)va_arg( args, const char* );
1227 pd_double = (double*)va_arg( args, double* );
1228 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, NULL, pd_double );
1230 case VLM_SET_MEDIA_INSTANCE_TIME:
1231 id = (int64_t)va_arg( args, int64_t );
1232 psz_id = (const char*)va_arg( args, const char* );
1233 i_i64 = (int64_t)va_arg( args, int64_t);
1234 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, i_i64, -1 );
1235 case VLM_SET_MEDIA_INSTANCE_POSITION:
1236 id = (int64_t)va_arg( args, int64_t );
1237 psz_id = (const char*)va_arg( args, const char* );
1238 d_double = (double)va_arg( args, double );
1239 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, -1, d_double );
1241 case VLM_CLEAR_SCHEDULES:
1242 return vlm_ControlScheduleClear( p_vlm );
1245 msg_Err( p_vlm, "unknown VLM query" );
1246 return VLC_EGENERIC;
1250 int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... )
1255 va_start( args, i_query );
1256 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
1262 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
1267 va_start( args, i_query );
1269 vlc_mutex_lock( &p_vlm->lock );
1270 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
1271 vlc_mutex_unlock( &p_vlm->lock );