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 // We have ourselves been triggering the stop event.
109 // No need to wake up our manage thread, because
110 // we'll join it just after the Stop().
111 vlc_mutex_lock( &p_input->p->lock_control );
112 bool abort = p_input->p->b_abort;
113 vlc_mutex_unlock( &p_input->p->lock_control );
118 vlc_mutex_lock( &p_vlm->lock );
119 vlc_cond_signal( &p_vlm->wait );
120 vlc_mutex_unlock( &p_vlm->lock );
125 static vlc_mutex_t vlm_mutex = VLC_STATIC_MUTEX;
128 /*****************************************************************************
130 *****************************************************************************/
131 vlm_t *vlm_New ( vlc_object_t *p_this )
133 vlm_t *p_vlm = NULL, **pp_vlm = &(libvlc_priv (p_this->p_libvlc)->p_vlm);
135 static const char vlm_object_name[] = "vlm daemon";
137 /* Avoid multiple creation */
138 vlc_mutex_lock( &vlm_mutex );
142 { /* VLM already exists */
143 vlc_object_hold( p_vlm );
144 vlc_mutex_unlock( &vlm_mutex );
148 msg_Dbg( p_this, "creating VLM" );
150 p_vlm = vlc_custom_create( p_this, sizeof( *p_vlm ), VLC_OBJECT_GENERIC,
154 vlc_mutex_unlock( &vlm_mutex );
158 vlc_mutex_init( &p_vlm->lock );
159 vlc_mutex_init( &p_vlm->lock );
160 vlc_cond_init_daytime( &p_vlm->wait );
162 TAB_INIT( p_vlm->i_media, p_vlm->media );
163 TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
166 var_Create( p_vlm, "intf-event", VLC_VAR_ADDRESS );
167 vlc_object_attach( p_vlm, p_this->p_libvlc );
169 if( vlc_clone( &p_vlm->thread, Manage, p_vlm, VLC_THREAD_PRIORITY_LOW ) )
171 vlc_cond_destroy( &p_vlm->wait );
172 vlc_mutex_destroy( &p_vlm->lock );
173 vlc_mutex_destroy( &p_vlm->lock );
174 vlc_object_release( p_vlm );
175 vlc_mutex_unlock( &vlm_mutex );
179 *pp_vlm = p_vlm; /* for future reference */
181 /* Load our configuration file */
182 psz_vlmconf = var_CreateGetString( p_vlm, "vlm-conf" );
183 if( psz_vlmconf && *psz_vlmconf )
185 vlm_message_t *p_message = NULL;
186 char *psz_buffer = NULL;
188 msg_Dbg( p_this, "loading VLM configuration" );
189 if( asprintf(&psz_buffer, "load %s", psz_vlmconf ) != -1 )
191 msg_Dbg( p_this, "%s", psz_buffer );
192 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
193 msg_Warn( p_this, "error while loading the configuration file" );
195 vlm_MessageDelete( p_message );
201 vlc_object_set_destructor( p_vlm, (vlc_destructor_t)vlm_Destructor );
202 vlc_mutex_unlock( &vlm_mutex );
207 /*****************************************************************************
209 *****************************************************************************/
210 void vlm_Delete( vlm_t *p_vlm )
212 /* vlm_Delete() is serialized against itself, and against vlm_New().
213 * This way, vlm_Destructor () (called from vlc_objet_release() above)
214 * is serialized against setting libvlc_priv->p_vlm from vlm_New(). */
215 vlc_mutex_lock( &vlm_mutex );
216 vlc_object_release( p_vlm );
217 vlc_mutex_unlock( &vlm_mutex );
220 /*****************************************************************************
222 *****************************************************************************/
223 static void vlm_Destructor( vlm_t *p_vlm )
225 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
226 TAB_CLEAN( p_vlm->i_media, p_vlm->media );
228 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
229 TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
231 vlc_mutex_lock( &p_vlm->lock );
232 vlc_cond_signal( &p_vlm->wait );
233 vlc_mutex_unlock( &p_vlm->lock );
235 libvlc_priv(p_vlm->p_libvlc)->p_vlm = NULL;
236 vlc_object_kill( p_vlm );
237 /*vlc_cancel( p_vlm->thread ); */
238 vlc_join( p_vlm->thread, NULL );
240 vlc_cond_destroy( &p_vlm->wait );
241 vlc_mutex_destroy( &p_vlm->lock );
242 vlc_mutex_destroy( &p_vlm->lock );
245 /*****************************************************************************
246 * vlm_ExecuteCommand:
247 *****************************************************************************/
248 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
249 vlm_message_t **pp_message)
253 vlc_mutex_lock( &p_vlm->lock );
254 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
255 vlc_mutex_unlock( &p_vlm->lock );
261 int64_t vlm_Date(void)
263 #if defined (WIN32) && !defined (UNDER_CE)
266 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
268 struct timeval tv_date;
270 /* gettimeofday() cannot fail given &tv_date is a valid address */
271 (void)gettimeofday( &tv_date, NULL );
272 return (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec;
277 /*****************************************************************************
279 *****************************************************************************/
280 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
281 const char *psz_id, int i_query, va_list args )
283 vlm_t *vlm = (vlm_t *)p_private;
288 if( !p_private || !p_vod_media )
291 vlc_mutex_lock( &vlm->lock );
294 for( i = 0, id = -1; i < vlm->i_media; i++ )
296 if( p_vod_media == vlm->media[i]->vod.p_media )
298 id = vlm->media[i]->cfg.id;
304 vlc_mutex_unlock( &vlm->lock );
311 psz = (const char *)va_arg( args, const char * );
312 i_ret = vlm_ControlInternal( vlm, VLM_START_MEDIA_VOD_INSTANCE, id, psz_id, 0, psz );
315 case VOD_MEDIA_PAUSE:
316 i_ret = vlm_ControlInternal( vlm, VLM_PAUSE_MEDIA_INSTANCE, id, psz_id );
320 i_ret = vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, id, psz_id );
325 double d_position = (double)va_arg( args, double );
326 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position/100.0 );
330 case VOD_MEDIA_REWIND:
332 double d_scale = (double)va_arg( args, double );
335 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
336 d_position -= (d_scale / 1000.0);
337 if( d_position < 0.0 )
339 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
343 case VOD_MEDIA_FORWARD:
345 double d_scale = (double)va_arg( args, double );
348 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
349 d_position += (d_scale / 1000.0);
350 if( d_position > 1.0 )
352 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
357 i_ret = VLC_EGENERIC;
361 vlc_mutex_unlock( &vlm->lock );
367 /*****************************************************************************
369 *****************************************************************************/
370 static void* Manage( void* p_object )
372 vlm_t *vlm = (vlm_t*)p_object;
376 mtime_t i_nextschedule = 0;
378 int canc = vlc_savecancel ();
379 i_lastcheck = vlm_Date();
383 char **ppsz_scheduled_commands = NULL;
384 int i_scheduled_commands = 0;
386 vlc_mutex_lock( &vlm->lock );
388 vlc_cond_timedwait( &vlm->wait, &vlm->lock, i_nextschedule );
390 vlc_cond_wait( &vlm->wait, &vlm->lock );
392 /* destroy the inputs that wants to die, and launch the next input */
393 for( i = 0; i < vlm->i_media; i++ )
395 vlm_media_sys_t *p_media = vlm->media[i];
397 for( j = 0; j < p_media->i_instance; )
399 vlm_media_instance_sys_t *p_instance = p_media->instance[j];
401 if( p_instance->p_input && ( p_instance->p_input->b_eof || p_instance->p_input->b_error ) )
403 int i_new_input_index;
406 i_new_input_index = p_instance->i_index + 1;
407 if( !p_media->cfg.b_vod && p_media->cfg.broadcast.b_loop && i_new_input_index >= p_media->cfg.i_input )
408 i_new_input_index = 0;
410 /* FIXME implement multiple input with VOD */
411 if( p_media->cfg.b_vod || i_new_input_index >= p_media->cfg.i_input )
412 vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, p_instance->psz_name );
414 vlm_ControlInternal( vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, p_instance->psz_name, i_new_input_index );
429 for( i = 0; i < vlm->i_schedule; i++ )
431 mtime_t i_real_date = vlm->schedule[i]->i_date;
433 if( vlm->schedule[i]->b_enabled == true )
435 if( vlm->schedule[i]->i_date == 0 ) // now !
437 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
438 i_real_date = i_time;
440 else if( vlm->schedule[i]->i_period != 0 )
443 while( vlm->schedule[i]->i_date + j *
444 vlm->schedule[i]->i_period <= i_lastcheck &&
445 ( vlm->schedule[i]->i_repeat > j ||
446 vlm->schedule[i]->i_repeat == -1 ) )
451 i_real_date = vlm->schedule[i]->i_date + j *
452 vlm->schedule[i]->i_period;
455 if( i_real_date <= i_time )
457 if( i_real_date > i_lastcheck )
459 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
461 TAB_APPEND( i_scheduled_commands,
462 ppsz_scheduled_commands,
463 strdup(vlm->schedule[i]->command[j] ) );
469 i_nextschedule = i_real_date;
474 while( i_scheduled_commands )
476 vlm_message_t *message = NULL;
477 char *psz_command = ppsz_scheduled_commands[0];
478 ExecuteCommand( vlm, psz_command,&message );
480 /* for now, drop the message */
481 vlm_MessageDelete( message );
482 TAB_REMOVE( i_scheduled_commands,
483 ppsz_scheduled_commands,
488 i_lastcheck = i_time;
490 vlc_mutex_unlock( &vlm->lock );
493 vlc_restorecancel (canc);
504 int i_connection_count;
505 int i_connection_active;
514 } vlm_media_status_t;
518 static vlm_media_sys_t *vlm_ControlMediaGetById( vlm_t *p_vlm, int64_t id )
522 for( i = 0; i < p_vlm->i_media; i++ )
524 if( p_vlm->media[i]->cfg.id == id )
525 return p_vlm->media[i];
529 static vlm_media_sys_t *vlm_ControlMediaGetByName( vlm_t *p_vlm, const char *psz_name )
533 for( i = 0; i < p_vlm->i_media; i++ )
535 if( !strcmp( p_vlm->media[i]->cfg.psz_name, psz_name ) )
536 return p_vlm->media[i];
540 static int vlm_MediaDescriptionCheck( vlm_t *p_vlm, vlm_media_t *p_cfg )
544 if( !p_cfg || !p_cfg->psz_name ||
545 !strcmp( p_cfg->psz_name, "all" ) || !strcmp( p_cfg->psz_name, "media" ) || !strcmp( p_cfg->psz_name, "schedule" ) )
548 for( i = 0; i < p_vlm->i_media; i++ )
550 if( p_vlm->media[i]->cfg.id == p_cfg->id )
552 if( !strcmp( p_vlm->media[i]->cfg.psz_name, p_cfg->psz_name ) )
559 /* Called after a media description is changed/added */
560 static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
562 vlm_media_t *p_cfg = &p_media->cfg;
563 /* Check if we need to create/delete a vod media */
566 if( !p_cfg->b_enabled && p_media->vod.p_media )
568 p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
569 p_media->vod.p_media = NULL;
571 else if( p_cfg->b_enabled && !p_media->vod.p_media && p_cfg->i_input )
573 /* Pre-parse the input */
574 input_thread_t *p_input;
580 vlc_gc_decref( p_media->vod.p_item );
581 p_media->vod.p_item = input_item_New( p_vlm, p_cfg->ppsz_input[0],
584 if( p_cfg->psz_output )
586 if( asprintf( &psz_output, "%s:description", p_cfg->psz_output ) == -1 )
590 psz_output = strdup( "#description" );
592 if( psz_output && asprintf( &psz_dup, "sout=%s", psz_output ) != -1 )
594 input_item_AddOption( p_media->vod.p_item, psz_dup, VLC_INPUT_OPTION_TRUSTED );
599 for( i = 0; i < p_cfg->i_option; i++ )
600 input_item_AddOption( p_media->vod.p_item,
601 p_cfg->ppsz_option[i], VLC_INPUT_OPTION_TRUSTED );
603 if( asprintf( &psz_header, _("Media: %s"), p_cfg->psz_name ) == -1 )
606 p_input = input_Create( p_vlm->p_vod, p_media->vod.p_item, psz_header, NULL );
609 vlc_sem_t sem_preparse;
610 vlc_sem_init( &sem_preparse, 0 );
611 var_AddCallback( p_input, "intf-event", InputEventPreparse, &sem_preparse );
613 if( !input_Start( p_input ) )
615 while( !p_input->b_dead && ( !p_cfg->vod.psz_mux || !input_item_IsPreparsed( p_media->vod.p_item ) ) )
616 vlc_sem_wait( &sem_preparse );
619 var_DelCallback( p_input, "intf-event", InputEventPreparse, &sem_preparse );
620 vlc_sem_destroy( &sem_preparse );
622 input_Stop( p_input, true );
623 vlc_thread_join( p_input );
624 vlc_object_release( p_input );
628 if( p_cfg->vod.psz_mux )
631 es_format_t es, *p_es = &es;
632 union { char text[5]; uint32_t value; } fourcc;
634 sprintf( fourcc.text, "%4.4s", p_cfg->vod.psz_mux );
635 fourcc.text[0] = tolower(fourcc.text[0]);
636 fourcc.text[1] = tolower(fourcc.text[1]);
637 fourcc.text[2] = tolower(fourcc.text[2]);
638 fourcc.text[3] = tolower(fourcc.text[3]);
640 /* XXX: Don't do it that way, but properly use a new input item ref. */
641 item = *p_media->vod.p_item;
644 es_format_Init( &es, VIDEO_ES, fourcc.value );
646 p_media->vod.p_media =
647 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
651 p_media->vod.p_media =
652 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, p_media->vod.p_item );
658 /* TODO start media if needed */
661 /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
663 vlm_SendEventMediaChanged( p_vlm, p_cfg->id, p_cfg->psz_name );
666 static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
668 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
671 if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
673 if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
678 /* TODO check what are the changes being done (stop instance if needed) */
681 vlm_media_Clean( &p_media->cfg );
682 vlm_media_Copy( &p_media->cfg, p_cfg );
684 return vlm_OnMediaUpdate( p_vlm, p_media );
687 static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id )
689 vlm_media_sys_t *p_media;
691 if( vlm_MediaDescriptionCheck( p_vlm, p_cfg ) || vlm_ControlMediaGetByName( p_vlm, p_cfg->psz_name ) )
693 msg_Err( p_vlm, "invalid media description" );
696 /* Check if we need to load the VOD server */
697 if( p_cfg->b_vod && !p_vlm->i_vod )
699 p_vlm->p_vod = vlc_custom_create( VLC_OBJECT(p_vlm), sizeof( vod_t ),
700 VLC_OBJECT_GENERIC, "vod server" );
701 vlc_object_attach( p_vlm->p_vod, p_vlm->p_libvlc );
702 p_vlm->p_vod->p_module = module_need( p_vlm->p_vod, "vod server", NULL, false );
703 if( !p_vlm->p_vod->p_module )
705 msg_Err( p_vlm, "cannot find vod server" );
706 vlc_object_release( p_vlm->p_vod );
711 p_vlm->p_vod->p_data = p_vlm;
712 p_vlm->p_vod->pf_media_control = vlm_MediaVodControl;
715 p_media = calloc( 1, sizeof( vlm_media_sys_t ) );
722 vlm_media_Copy( &p_media->cfg, p_cfg );
723 p_media->cfg.id = p_vlm->i_id++;
724 /* FIXME do we do something here if enabled is true ? */
726 p_media->vod.p_item = input_item_New( p_vlm, NULL, NULL );
728 p_media->vod.p_media = NULL;
729 TAB_INIT( p_media->i_instance, p_media->instance );
732 TAB_APPEND( p_vlm->i_media, p_vlm->media, p_media );
735 *p_id = p_media->cfg.id;
738 vlm_SendEventMediaAdded( p_vlm, p_media->cfg.id, p_media->cfg.psz_name );
739 return vlm_OnMediaUpdate( p_vlm, p_media );
742 static int vlm_ControlMediaDel( vlm_t *p_vlm, int64_t id )
744 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
749 while( p_media->i_instance > 0 )
750 vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, p_media->instance[0]->psz_name );
752 if( p_media->cfg.b_vod )
754 p_media->cfg.b_enabled = false;
755 vlm_OnMediaUpdate( p_vlm, p_media );
760 vlm_SendEventMediaRemoved( p_vlm, id, p_media->cfg.psz_name );
762 vlm_media_Clean( &p_media->cfg );
764 vlc_gc_decref( p_media->vod.p_item );
766 if( p_media->vod.p_media )
767 p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
769 TAB_REMOVE( p_vlm->i_media, p_vlm->media, p_media );
772 /* Check if we need to unload the VOD server */
773 if( p_vlm->p_vod && p_vlm->i_vod <= 0 )
775 module_unneed( p_vlm->p_vod, p_vlm->p_vod->p_module );
776 vlc_object_release( p_vlm->p_vod );
783 static int vlm_ControlMediaGets( vlm_t *p_vlm, vlm_media_t ***ppp_dsc, int *pi_dsc )
785 vlm_media_t **pp_dsc;
789 TAB_INIT( i_dsc, pp_dsc );
790 for( i = 0; i < p_vlm->i_media; i++ )
792 vlm_media_t *p_dsc = vlm_media_Duplicate( &p_vlm->media[i]->cfg );
793 TAB_APPEND( i_dsc, pp_dsc, p_dsc );
801 static int vlm_ControlMediaClear( vlm_t *p_vlm )
803 while( p_vlm->i_media > 0 )
804 vlm_ControlMediaDel( p_vlm, p_vlm->media[0]->cfg.id );
808 static int vlm_ControlMediaGet( vlm_t *p_vlm, int64_t id, vlm_media_t **pp_dsc )
810 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
814 *pp_dsc = vlm_media_Duplicate( &p_media->cfg );
817 static int vlm_ControlMediaGetId( vlm_t *p_vlm, const char *psz_name, int64_t *p_id )
819 vlm_media_sys_t *p_media = vlm_ControlMediaGetByName( p_vlm, psz_name );
823 *p_id = p_media->cfg.id;
827 static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
831 for( i = 0; i < p_media->i_instance; i++ )
833 const char *psz = p_media->instance[i]->psz_name;
834 if( ( psz == NULL && psz_id == NULL ) ||
835 ( psz && psz_id && !strcmp( psz, psz_id ) ) )
836 return p_media->instance[i];
840 static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
842 vlm_media_instance_sys_t *p_instance = calloc( 1, sizeof(vlm_media_instance_sys_t) );
846 p_instance->psz_name = NULL;
848 p_instance->psz_name = strdup( psz_name );
850 p_instance->p_item = input_item_New( p_vlm, NULL, NULL );
852 p_instance->i_index = 0;
853 p_instance->b_sout_keep = false;
854 p_instance->p_input = NULL;
855 p_instance->p_input_resource = NULL;
859 static void vlm_MediaInstanceDelete( vlm_t *p_vlm, int64_t id, vlm_media_instance_sys_t *p_instance, vlm_media_sys_t *p_media )
861 input_thread_t *p_input = p_instance->p_input;
864 input_resource_t *p_resource;
866 input_Stop( p_input, true );
867 vlc_thread_join( p_input );
869 p_resource = input_DetachResource( p_input );
870 input_resource_Delete( p_resource );
872 var_DelCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
873 vlc_object_release( p_input );
875 vlm_SendEventMediaInstanceStopped( p_vlm, id, p_media->cfg.psz_name );
877 if( p_instance->p_input_resource )
878 input_resource_Delete( p_instance->p_input_resource );
880 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
881 vlc_gc_decref( p_instance->p_item );
882 free( p_instance->psz_name );
887 static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
889 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
890 vlm_media_instance_sys_t *p_instance;
893 if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
896 /* TODO support multiple input for VOD with sout-keep ? */
898 if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
901 if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
904 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
907 vlm_media_t *p_cfg = &p_media->cfg;
910 p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
914 if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
917 if( asprintf( &psz_buffer, "sout=%s%s%s",
918 p_cfg->psz_output ? p_cfg->psz_output : "",
919 (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
920 psz_vod_output ? psz_vod_output : "" ) != -1 )
922 input_item_AddOption( p_instance->p_item, psz_buffer, VLC_INPUT_OPTION_TRUSTED );
927 for( i = 0; i < p_cfg->i_option; i++ )
929 if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
930 p_instance->b_sout_keep = true;
931 else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
932 p_instance->b_sout_keep = false;
934 input_item_AddOption( p_instance->p_item, p_cfg->ppsz_option[i], VLC_INPUT_OPTION_TRUSTED );
936 TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
939 /* Stop old instance */
940 input_thread_t *p_input = p_instance->p_input;
943 if( p_instance->i_index == i_input_index &&
944 !p_input->b_eof && !p_input->b_error )
946 if( var_GetInteger( p_input, "state" ) == PAUSE_S )
947 var_SetInteger( p_input, "state", PLAYING_S );
951 input_Stop( p_input, true );
952 vlc_thread_join( p_input );
954 p_instance->p_input_resource = input_DetachResource( p_input );
956 var_DelCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
957 vlc_object_release( p_input );
959 if( !p_instance->b_sout_keep )
960 input_resource_TerminateSout( p_instance->p_input_resource );
961 input_resource_TerminateVout( p_instance->p_input_resource );
963 vlm_SendEventMediaInstanceStopped( p_vlm, id, p_media->cfg.psz_name );
967 p_instance->i_index = i_input_index;
968 input_item_SetURI( p_instance->p_item, p_media->cfg.ppsz_input[p_instance->i_index] ) ;
970 if( asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name ) != -1 )
972 vlc_object_t *p_parent = p_media->cfg.b_vod ?
973 VLC_OBJECT(p_vlm->p_vod) :
974 VLC_OBJECT(p_vlm->p_libvlc);
975 p_instance->p_input = input_Create( p_parent, p_instance->p_item,
976 psz_log, p_instance->p_input_resource );
977 if( p_instance->p_input )
979 var_AddCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
980 if( input_Start( p_instance->p_input ) != VLC_SUCCESS )
982 var_DelCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
983 vlc_object_release( p_instance->p_input );
984 p_instance->p_input = NULL;
987 p_instance->p_input_resource = NULL;
989 if( !p_instance->p_input )
991 vlm_MediaInstanceDelete( p_vlm, id, p_instance, p_media );
995 vlm_SendEventMediaInstanceStarted( p_vlm, id, p_media->cfg.psz_name );
1003 static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
1005 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1006 vlm_media_instance_sys_t *p_instance;
1009 return VLC_EGENERIC;
1011 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1013 return VLC_EGENERIC;
1015 vlm_MediaInstanceDelete( p_vlm, id, p_instance, p_media );
1019 static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
1021 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1022 vlm_media_instance_sys_t *p_instance;
1026 return VLC_EGENERIC;
1028 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1029 if( !p_instance || !p_instance->p_input )
1030 return VLC_EGENERIC;
1032 /* Toggle pause state */
1033 i_state = var_GetInteger( p_instance->p_input, "state" );
1034 if( i_state == PAUSE_S )
1035 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
1036 else if( i_state == PLAYING_S )
1037 var_SetInteger( p_instance->p_input, "state", PAUSE_S );
1040 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
1042 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1043 vlm_media_instance_sys_t *p_instance;
1046 return VLC_EGENERIC;
1048 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1049 if( !p_instance || !p_instance->p_input )
1050 return VLC_EGENERIC;
1053 *pi_time = var_GetTime( p_instance->p_input, "time" );
1055 *pd_position = var_GetFloat( p_instance->p_input, "position" );
1058 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t i_time, double d_position )
1060 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1061 vlm_media_instance_sys_t *p_instance;
1064 return VLC_EGENERIC;
1066 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1067 if( !p_instance || !p_instance->p_input )
1068 return VLC_EGENERIC;
1071 return var_SetTime( p_instance->p_input, "time", i_time );
1072 else if( d_position >= 0 && d_position <= 100 )
1073 return var_SetFloat( p_instance->p_input, "position", d_position );
1074 return VLC_EGENERIC;
1077 static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_instance_t ***ppp_idsc, int *pi_instance )
1079 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1080 vlm_media_instance_t **pp_idsc;
1085 return VLC_EGENERIC;
1087 TAB_INIT( i_idsc, pp_idsc );
1088 for( i = 0; i < p_media->i_instance; i++ )
1090 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1091 vlm_media_instance_t *p_idsc = vlm_media_instance_New();
1093 if( p_instance->psz_name )
1094 p_idsc->psz_name = strdup( p_instance->psz_name );
1095 if( p_instance->p_input )
1097 p_idsc->i_time = var_GetTime( p_instance->p_input, "time" );
1098 p_idsc->i_length = var_GetTime( p_instance->p_input, "length" );
1099 p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
1100 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
1101 p_idsc->b_paused = true;
1102 p_idsc->i_rate = INPUT_RATE_DEFAULT
1103 / var_GetFloat( p_instance->p_input, "rate" );
1106 TAB_APPEND( i_idsc, pp_idsc, p_idsc );
1108 *ppp_idsc = pp_idsc;
1109 *pi_instance = i_idsc;
1113 static int vlm_ControlMediaInstanceClear( vlm_t *p_vlm, int64_t id )
1115 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1118 return VLC_EGENERIC;
1120 while( p_media->i_instance > 0 )
1121 vlm_ControlMediaInstanceStop( p_vlm, id, p_media->instance[0]->psz_name );
1126 static int vlm_ControlScheduleClear( vlm_t *p_vlm )
1128 while( p_vlm->i_schedule > 0 )
1129 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0] );
1134 static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
1137 vlm_media_t **pp_dsc;
1138 vlm_media_t ***ppp_dsc;
1139 vlm_media_instance_t ***ppp_idsc;
1141 const char *psz_vod;
1155 case VLM_GET_MEDIAS:
1156 ppp_dsc = (vlm_media_t ***)va_arg( args, vlm_media_t *** );
1157 pi_int = (int *)va_arg( args, int * );
1158 return vlm_ControlMediaGets( p_vlm, ppp_dsc, pi_int );
1160 case VLM_CLEAR_MEDIAS:
1161 return vlm_ControlMediaClear( p_vlm );
1163 case VLM_CHANGE_MEDIA:
1164 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
1165 return vlm_ControlMediaChange( p_vlm, p_dsc );
1168 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
1169 p_id = (int64_t*)va_arg( args, int64_t * );
1170 return vlm_ControlMediaAdd( p_vlm, p_dsc, p_id );
1173 id = (int64_t)va_arg( args, int64_t );
1174 return vlm_ControlMediaDel( p_vlm, id );
1177 id = (int64_t)va_arg( args, int64_t );
1178 pp_dsc = (vlm_media_t **)va_arg( args, vlm_media_t ** );
1179 return vlm_ControlMediaGet( p_vlm, id, pp_dsc );
1181 case VLM_GET_MEDIA_ID:
1182 psz_id = (const char*)va_arg( args, const char * );
1183 p_id = (int64_t*)va_arg( args, int64_t * );
1184 return vlm_ControlMediaGetId( p_vlm, psz_id, p_id );
1187 /* Media instance control */
1188 case VLM_GET_MEDIA_INSTANCES:
1189 id = (int64_t)va_arg( args, int64_t );
1190 ppp_idsc = (vlm_media_instance_t ***)va_arg( args, vlm_media_instance_t *** );
1191 pi_int = (int *)va_arg( args, int *);
1192 return vlm_ControlMediaInstanceGets( p_vlm, id, ppp_idsc, pi_int );
1194 case VLM_CLEAR_MEDIA_INSTANCES:
1195 id = (int64_t)va_arg( args, int64_t );
1196 return vlm_ControlMediaInstanceClear( p_vlm, id );
1199 case VLM_START_MEDIA_BROADCAST_INSTANCE:
1200 id = (int64_t)va_arg( args, int64_t );
1201 psz_id = (const char*)va_arg( args, const char* );
1202 i_int = (int)va_arg( args, int );
1203 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, NULL );
1205 case VLM_START_MEDIA_VOD_INSTANCE:
1206 id = (int64_t)va_arg( args, int64_t );
1207 psz_id = (const char*)va_arg( args, const char* );
1208 i_int = (int)va_arg( args, int );
1209 psz_vod = (const char*)va_arg( args, const char* );
1211 return VLC_EGENERIC;
1212 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, psz_vod );
1214 case VLM_STOP_MEDIA_INSTANCE:
1215 id = (int64_t)va_arg( args, int64_t );
1216 psz_id = (const char*)va_arg( args, const char* );
1217 return vlm_ControlMediaInstanceStop( p_vlm, id, psz_id );
1219 case VLM_PAUSE_MEDIA_INSTANCE:
1220 id = (int64_t)va_arg( args, int64_t );
1221 psz_id = (const char*)va_arg( args, const char* );
1222 return vlm_ControlMediaInstancePause( p_vlm, id, psz_id );
1224 case VLM_GET_MEDIA_INSTANCE_TIME:
1225 id = (int64_t)va_arg( args, int64_t );
1226 psz_id = (const char*)va_arg( args, const char* );
1227 pi_i64 = (int64_t*)va_arg( args, int64_t * );
1228 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, pi_i64, NULL );
1229 case VLM_GET_MEDIA_INSTANCE_POSITION:
1230 id = (int64_t)va_arg( args, int64_t );
1231 psz_id = (const char*)va_arg( args, const char* );
1232 pd_double = (double*)va_arg( args, double* );
1233 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, NULL, pd_double );
1235 case VLM_SET_MEDIA_INSTANCE_TIME:
1236 id = (int64_t)va_arg( args, int64_t );
1237 psz_id = (const char*)va_arg( args, const char* );
1238 i_i64 = (int64_t)va_arg( args, int64_t);
1239 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, i_i64, -1 );
1240 case VLM_SET_MEDIA_INSTANCE_POSITION:
1241 id = (int64_t)va_arg( args, int64_t );
1242 psz_id = (const char*)va_arg( args, const char* );
1243 d_double = (double)va_arg( args, double );
1244 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, -1, d_double );
1246 case VLM_CLEAR_SCHEDULES:
1247 return vlm_ControlScheduleClear( p_vlm );
1250 msg_Err( p_vlm, "unknown VLM query" );
1251 return VLC_EGENERIC;
1255 int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... )
1260 va_start( args, i_query );
1261 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
1267 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
1272 va_start( args, i_query );
1274 vlc_mutex_lock( &p_vlm->lock );
1275 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
1276 vlc_mutex_unlock( &p_vlm->lock );