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 <time.h> /* ctime() */
47 # include <sys/timeb.h> /* ftime() */
50 #include <vlc_input.h>
51 #include "input_internal.h"
52 #include <vlc_stream.h>
53 #include "vlm_internal.h"
55 #include <vlc_charset.h>
57 #include "../stream_output/stream_output.h"
58 #include "../libvlc.h"
60 /*****************************************************************************
62 *****************************************************************************/
64 static void vlm_Destructor( vlm_t *p_vlm );
65 static void* Manage( void * );
66 static int vlm_MediaVodControl( void *, vod_media_t *, const char *, int, va_list );
68 /*****************************************************************************
70 *****************************************************************************/
71 vlm_t *__vlm_New ( vlc_object_t *p_this )
74 vlm_t *p_vlm = NULL, **pp_vlm = &(libvlc_priv (p_this->p_libvlc)->p_vlm);
76 static const char vlm_object_name[] = "vlm daemon";
78 /* Avoid multiple creation */
79 if( var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX ) ||
80 var_Get( p_this->p_libvlc, "vlm_mutex", &lockval ) )
83 vlc_mutex_lock( lockval.p_address );
87 { /* VLM already exists */
88 vlc_object_yield( p_vlm );
89 vlc_mutex_unlock( lockval.p_address );
93 msg_Dbg( p_this, "creating VLM" );
95 p_vlm = vlc_custom_create( p_this, sizeof( *p_vlm ), VLC_OBJECT_GENERIC,
99 vlc_mutex_unlock( lockval.p_address );
103 vlc_mutex_init( &p_vlm->lock );
105 TAB_INIT( p_vlm->i_media, p_vlm->media );
106 TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
109 vlc_object_attach( p_vlm, p_this->p_libvlc );
111 if( vlc_clone( &p_vlm->thread, Manage, p_vlm, VLC_THREAD_PRIORITY_LOW ) )
113 vlc_mutex_destroy( &p_vlm->lock );
114 vlc_object_release( p_vlm );
118 /* Load our configuration file */
119 psz_vlmconf = var_CreateGetString( p_vlm, "vlm-conf" );
120 if( psz_vlmconf && *psz_vlmconf )
122 vlm_message_t *p_message = NULL;
123 char *psz_buffer = NULL;
125 msg_Dbg( p_this, "loading VLM configuration" );
126 if( asprintf(&psz_buffer, "load %s", psz_vlmconf ) != -1 )
128 msg_Dbg( p_this, "%s", psz_buffer );
129 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
130 msg_Warn( p_this, "error while loading the configuration file" );
132 vlm_MessageDelete( p_message );
138 vlc_object_set_destructor( p_vlm, (vlc_destructor_t)vlm_Destructor );
139 *pp_vlm = p_vlm; /* for future reference */
140 vlc_mutex_unlock( lockval.p_address );
145 /*****************************************************************************
147 *****************************************************************************/
148 void vlm_Delete( vlm_t *p_vlm )
152 /* vlm_Delete() is serialized against itself, and against vlm_New().
153 * This way, vlm_Destructor () (called from vlc_objet_release() above)
154 * is serialized against setting libvlc_priv->p_vlm from vlm_New(). */
155 var_Get( p_vlm->p_libvlc, "vlm_mutex", &lockval );
156 vlc_mutex_lock( lockval.p_address );
157 vlc_object_release( p_vlm );
158 vlc_mutex_unlock( lockval.p_address );
161 /*****************************************************************************
163 *****************************************************************************/
164 static void vlm_Destructor( vlm_t *p_vlm )
166 libvlc_priv (p_vlm->p_libvlc)->p_vlm = NULL;
168 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
169 TAB_CLEAN( p_vlm->i_media, p_vlm->media );
171 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
172 TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
174 vlc_object_kill( p_vlm );
175 /*vlc_cancel( p_vlm->thread ); */
176 vlc_join( p_vlm->thread, NULL );
177 vlc_mutex_destroy( &p_vlm->lock );
180 /*****************************************************************************
181 * vlm_ExecuteCommand:
182 *****************************************************************************/
183 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
184 vlm_message_t **pp_message)
188 vlc_mutex_lock( &p_vlm->lock );
189 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
190 vlc_mutex_unlock( &p_vlm->lock );
196 int64_t vlm_Date(void)
201 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
203 struct timeval tv_date;
205 /* gettimeofday() cannot fail given &tv_date is a valid address */
206 (void)gettimeofday( &tv_date, NULL );
207 return (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec;
212 /*****************************************************************************
214 *****************************************************************************/
215 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
216 const char *psz_id, int i_query, va_list args )
218 vlm_t *vlm = (vlm_t *)p_private;
223 if( !p_private || !p_vod_media )
226 vlc_mutex_lock( &vlm->lock );
229 for( i = 0, id = -1; i < vlm->i_media; i++ )
231 if( p_vod_media == vlm->media[i]->vod.p_media )
233 id = vlm->media[i]->cfg.id;
239 vlc_mutex_unlock( &vlm->lock );
246 psz = (const char *)va_arg( args, const char * );
247 i_ret = vlm_ControlInternal( vlm, VLM_START_MEDIA_VOD_INSTANCE, id, psz_id, 0, psz );
250 case VOD_MEDIA_PAUSE:
251 i_ret = vlm_ControlInternal( vlm, VLM_PAUSE_MEDIA_INSTANCE, id, psz_id );
255 i_ret = vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, id, psz_id );
260 double d_position = (double)va_arg( args, double );
261 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position/100.0 );
265 case VOD_MEDIA_REWIND:
267 double d_scale = (double)va_arg( args, double );
270 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
271 d_position -= (d_scale / 1000.0);
272 if( d_position < 0.0 )
274 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
278 case VOD_MEDIA_FORWARD:
280 double d_scale = (double)va_arg( args, double );
283 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
284 d_position += (d_scale / 1000.0);
285 if( d_position > 1.0 )
287 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
292 i_ret = VLC_EGENERIC;
296 vlc_mutex_unlock( &vlm->lock );
302 /*****************************************************************************
304 *****************************************************************************/
305 static void* Manage( void* p_object )
307 vlm_t *vlm = (vlm_t*)p_object;
312 int canc = vlc_savecancel ();
313 i_lastcheck = vlm_Date();
317 char **ppsz_scheduled_commands = NULL;
318 int i_scheduled_commands = 0;
320 vlc_mutex_lock( &vlm->lock );
322 /* destroy the inputs that wants to die, and launch the next input */
323 for( i = 0; i < vlm->i_media; i++ )
325 vlm_media_sys_t *p_media = vlm->media[i];
327 for( j = 0; j < p_media->i_instance; )
329 vlm_media_instance_sys_t *p_instance = p_media->instance[j];
331 if( p_instance->p_input && ( p_instance->p_input->b_eof || p_instance->p_input->b_error ) )
333 int i_new_input_index;
336 i_new_input_index = p_instance->i_index + 1;
337 if( !p_media->cfg.b_vod && p_media->cfg.broadcast.b_loop && i_new_input_index >= p_media->cfg.i_input )
338 i_new_input_index = 0;
340 /* FIXME implement multiple input with VOD */
341 if( p_media->cfg.b_vod || i_new_input_index >= p_media->cfg.i_input )
342 vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, p_instance->psz_name );
344 vlm_ControlInternal( vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, p_instance->psz_name, i_new_input_index );
358 for( i = 0; i < vlm->i_schedule; i++ )
360 mtime_t i_real_date = vlm->schedule[i]->i_date;
362 if( vlm->schedule[i]->b_enabled == true )
364 if( vlm->schedule[i]->i_date == 0 ) // now !
366 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
367 i_real_date = i_time;
369 else if( vlm->schedule[i]->i_period != 0 )
372 while( vlm->schedule[i]->i_date + j *
373 vlm->schedule[i]->i_period <= i_lastcheck &&
374 ( vlm->schedule[i]->i_repeat > j ||
375 vlm->schedule[i]->i_repeat == -1 ) )
380 i_real_date = vlm->schedule[i]->i_date + j *
381 vlm->schedule[i]->i_period;
384 if( i_real_date <= i_time && i_real_date > i_lastcheck )
386 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
388 TAB_APPEND( i_scheduled_commands,
389 ppsz_scheduled_commands,
390 strdup(vlm->schedule[i]->command[j] ) );
395 while( i_scheduled_commands )
397 vlm_message_t *message = NULL;
398 char *psz_command = ppsz_scheduled_commands[0];
399 ExecuteCommand( vlm, psz_command,&message );
401 /* for now, drop the message */
402 vlm_MessageDelete( message );
403 TAB_REMOVE( i_scheduled_commands,
404 ppsz_scheduled_commands,
409 i_lastcheck = i_time;
411 vlc_mutex_unlock( &vlm->lock );
416 vlc_restorecancel (canc);
427 int i_connection_count;
428 int i_connection_active;
437 } vlm_media_status_t;
441 static vlm_media_sys_t *vlm_ControlMediaGetById( vlm_t *p_vlm, int64_t id )
445 for( i = 0; i < p_vlm->i_media; i++ )
447 if( p_vlm->media[i]->cfg.id == id )
448 return p_vlm->media[i];
452 static vlm_media_sys_t *vlm_ControlMediaGetByName( vlm_t *p_vlm, const char *psz_name )
456 for( i = 0; i < p_vlm->i_media; i++ )
458 if( !strcmp( p_vlm->media[i]->cfg.psz_name, psz_name ) )
459 return p_vlm->media[i];
463 static int vlm_MediaDescriptionCheck( vlm_t *p_vlm, vlm_media_t *p_cfg )
467 if( !p_cfg || !p_cfg->psz_name ||
468 !strcmp( p_cfg->psz_name, "all" ) || !strcmp( p_cfg->psz_name, "media" ) || !strcmp( p_cfg->psz_name, "schedule" ) )
471 for( i = 0; i < p_vlm->i_media; i++ )
473 if( p_vlm->media[i]->cfg.id == p_cfg->id )
475 if( !strcmp( p_vlm->media[i]->cfg.psz_name, p_cfg->psz_name ) )
482 /* Called after a media description is changed/added */
483 static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
485 vlm_media_t *p_cfg = &p_media->cfg;
486 /* Check if we need to create/delete a vod media */
489 if( !p_cfg->b_enabled && p_media->vod.p_media )
491 p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
492 p_media->vod.p_media = NULL;
494 else if( p_cfg->b_enabled && !p_media->vod.p_media && p_cfg->i_input )
496 /* Pre-parse the input */
497 input_thread_t *p_input;
503 vlc_gc_decref( p_media->vod.p_item );
504 p_media->vod.p_item = input_item_New( p_vlm, p_cfg->ppsz_input[0],
507 if( p_cfg->psz_output )
509 if( asprintf( &psz_output, "%s:description", p_cfg->psz_output ) == -1 )
513 psz_output = strdup( "#description" );
515 if( psz_output && asprintf( &psz_dup, "sout=%s", psz_output ) != -1 )
517 input_item_AddOption( p_media->vod.p_item, psz_dup );
522 for( i = 0; i < p_cfg->i_option; i++ )
523 input_item_AddOption( p_media->vod.p_item,
524 p_cfg->ppsz_option[i] );
526 if( asprintf( &psz_header, _("Media: %s"), p_cfg->psz_name ) == -1 )
529 if( (p_input = input_CreateThreadExtended( p_vlm, p_media->vod.p_item, psz_header, NULL ) ) )
531 while( !p_input->b_eof && !p_input->b_error )
534 input_StopThread( p_input );
535 vlc_object_release( p_input );
539 if( p_cfg->vod.psz_mux )
542 es_format_t es, *p_es = &es;
545 sprintf( fourcc, "%4.4s", p_cfg->vod.psz_mux );
546 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
547 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
549 /* XXX: Don't do it that way, but properly use a new input item ref. */
550 item = *p_media->vod.p_item;
553 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
555 p_media->vod.p_media =
556 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
560 p_media->vod.p_media =
561 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, p_media->vod.p_item );
567 /* TODO start media if needed */
570 /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
574 static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
576 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
579 if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
581 if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
586 /* TODO check what are the changes being done (stop instance if needed) */
589 vlm_media_Clean( &p_media->cfg );
590 vlm_media_Copy( &p_media->cfg, p_cfg );
592 return vlm_OnMediaUpdate( p_vlm, p_media );
595 static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id )
597 vlm_media_sys_t *p_media;
599 if( vlm_MediaDescriptionCheck( p_vlm, p_cfg ) || vlm_ControlMediaGetByName( p_vlm, p_cfg->psz_name ) )
601 msg_Err( p_vlm, "invalid media description" );
604 /* Check if we need to load the VOD server */
605 if( p_cfg->b_vod && !p_vlm->i_vod )
607 p_vlm->p_vod = vlc_custom_create( VLC_OBJECT(p_vlm), sizeof( vod_t ),
608 VLC_OBJECT_GENERIC, "vod server" );
609 vlc_object_attach( p_vlm->p_vod, p_vlm );
610 p_vlm->p_vod->p_module = module_Need( p_vlm->p_vod, "vod server", 0, 0 );
611 if( !p_vlm->p_vod->p_module )
613 msg_Err( p_vlm, "cannot find vod server" );
614 vlc_object_detach( p_vlm->p_vod );
615 vlc_object_release( p_vlm->p_vod );
620 p_vlm->p_vod->p_data = p_vlm;
621 p_vlm->p_vod->pf_media_control = vlm_MediaVodControl;
624 p_media = malloc( sizeof( vlm_media_sys_t ) );
627 memset( p_media, 0, sizeof(vlm_media_sys_t) );
632 vlm_media_Copy( &p_media->cfg, p_cfg );
633 p_media->cfg.id = p_vlm->i_id++;
634 /* FIXME do we do something here if enabled is true ? */
636 p_media->vod.p_item = input_item_New( p_vlm, NULL, NULL );
638 p_media->vod.p_media = NULL;
639 TAB_INIT( p_media->i_instance, p_media->instance );
642 TAB_APPEND( p_vlm->i_media, p_vlm->media, p_media );
645 *p_id = p_media->cfg.id;
647 return vlm_OnMediaUpdate( p_vlm, p_media );
650 static int vlm_ControlMediaDel( vlm_t *p_vlm, int64_t id )
652 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
657 while( p_media->i_instance > 0 )
658 vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, p_media->instance[0]->psz_name );
660 if( p_media->cfg.b_vod )
662 p_media->cfg.b_enabled = false;
663 vlm_OnMediaUpdate( p_vlm, p_media );
667 vlm_media_Clean( &p_media->cfg );
669 vlc_gc_decref( p_media->vod.p_item );
671 TAB_REMOVE( p_vlm->i_media, p_vlm->media, p_media );
675 /* Check if we need to unload the VOD server */
676 if( p_vlm->p_vod && p_vlm->i_vod <= 0 )
678 module_Unneed( p_vlm->p_vod, p_vlm->p_vod->p_module );
679 vlc_object_detach( p_vlm->p_vod );
680 vlc_object_release( p_vlm->p_vod );
686 static int vlm_ControlMediaGets( vlm_t *p_vlm, vlm_media_t ***ppp_dsc, int *pi_dsc )
688 vlm_media_t **pp_dsc;
692 TAB_INIT( i_dsc, pp_dsc );
693 for( i = 0; i < p_vlm->i_media; i++ )
695 vlm_media_t *p_dsc = vlm_media_Duplicate( &p_vlm->media[i]->cfg );
696 TAB_APPEND( i_dsc, pp_dsc, p_dsc );
704 static int vlm_ControlMediaClear( vlm_t *p_vlm )
706 while( p_vlm->i_media > 0 )
707 vlm_ControlMediaDel( p_vlm, p_vlm->media[0]->cfg.id );
711 static int vlm_ControlMediaGet( vlm_t *p_vlm, int64_t id, vlm_media_t **pp_dsc )
713 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
717 *pp_dsc = vlm_media_Duplicate( &p_media->cfg );
720 static int vlm_ControlMediaGetId( vlm_t *p_vlm, const char *psz_name, int64_t *p_id )
722 vlm_media_sys_t *p_media = vlm_ControlMediaGetByName( p_vlm, psz_name );
726 *p_id = p_media->cfg.id;
730 static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
734 for( i = 0; i < p_media->i_instance; i++ )
736 const char *psz = p_media->instance[i]->psz_name;
737 if( ( psz == NULL && psz_id == NULL ) ||
738 ( psz && psz_id && !strcmp( psz, psz_id ) ) )
739 return p_media->instance[i];
743 static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
745 vlm_media_instance_sys_t *p_instance = malloc( sizeof(vlm_media_instance_sys_t) );
749 memset( p_instance, 0, sizeof(vlm_media_instance_sys_t) );
751 p_instance->psz_name = NULL;
753 p_instance->psz_name = strdup( psz_name );
755 p_instance->p_item = input_item_New( p_vlm, NULL, NULL );
757 p_instance->i_index = 0;
758 p_instance->b_sout_keep = false;
759 p_instance->p_input = NULL;
760 p_instance->p_sout = NULL;
764 static void vlm_MediaInstanceDelete( vlm_media_instance_sys_t *p_instance )
766 input_thread_t *p_input = p_instance->p_input;
769 input_StopThread( p_input );
770 p_instance->p_sout = input_DetachSout( p_input );
771 vlc_thread_join( p_input );
772 vlc_object_release( p_input );
774 if( p_instance->p_sout )
775 sout_DeleteInstance( p_instance->p_sout );
777 vlc_gc_decref( p_instance->p_item );
778 free( p_instance->psz_name );
783 static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
785 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
786 vlm_media_instance_sys_t *p_instance;
789 if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
792 /* TODO support multiple input for VOD with sout-keep ? */
794 if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
797 if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
800 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
803 vlm_media_t *p_cfg = &p_media->cfg;
806 p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
810 if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
813 if( asprintf( &psz_buffer, "sout=%s%s%s",
814 p_cfg->psz_output ? p_cfg->psz_output : "",
815 (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
816 psz_vod_output ? psz_vod_output : "" ) != -1 )
818 input_item_AddOption( p_instance->p_item, psz_buffer );
823 for( i = 0; i < p_cfg->i_option; i++ )
825 if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
826 p_instance->b_sout_keep = true;
827 else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
828 p_instance->b_sout_keep = false;
830 input_item_AddOption( p_instance->p_item, p_cfg->ppsz_option[i] );
832 TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
835 /* Stop old instance */
836 input_thread_t *p_input = p_instance->p_input;
839 if( p_instance->i_index == i_input_index &&
840 !p_input->b_eof && !p_input->b_error )
842 if( var_GetInteger( p_input, "state" ) == PAUSE_S )
843 var_SetInteger( p_input, "state", PLAYING_S );
847 input_StopThread( p_input );
848 p_instance->p_sout = input_DetachSout( p_input );
849 vlc_thread_join( p_input );
850 vlc_object_release( p_input );
851 if( !p_instance->b_sout_keep && p_instance->p_sout )
853 sout_DeleteInstance( p_instance->p_sout );
854 p_instance->p_sout = NULL;
859 p_instance->i_index = i_input_index;
860 input_item_SetURI( p_instance->p_item, p_media->cfg.ppsz_input[p_instance->i_index] ) ;
862 if( asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name ) != -1 )
864 p_instance->p_input = input_CreateThreadExtended( p_vlm, p_instance->p_item, psz_log, p_instance->p_sout );
865 if( !p_instance->p_input )
867 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
868 vlm_MediaInstanceDelete( p_instance );
876 static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
878 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
879 vlm_media_instance_sys_t *p_instance;
884 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
888 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
890 vlm_MediaInstanceDelete( p_instance );
894 static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
896 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
897 vlm_media_instance_sys_t *p_instance;
903 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
904 if( !p_instance || !p_instance->p_input )
907 /* Toggle pause state */
908 i_state = var_GetInteger( p_instance->p_input, "state" );
909 if( i_state == PAUSE_S )
910 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
911 else if( i_state == PLAYING_S )
912 var_SetInteger( p_instance->p_input, "state", PAUSE_S );
915 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
917 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
918 vlm_media_instance_sys_t *p_instance;
923 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
924 if( !p_instance || !p_instance->p_input )
928 *pi_time = var_GetTime( p_instance->p_input, "time" );
930 *pd_position = var_GetFloat( p_instance->p_input, "position" );
933 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t i_time, double d_position )
935 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
936 vlm_media_instance_sys_t *p_instance;
941 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
942 if( !p_instance || !p_instance->p_input )
946 return var_SetTime( p_instance->p_input, "time", i_time );
947 else if( d_position >= 0 && d_position <= 100 )
948 return var_SetFloat( p_instance->p_input, "position", d_position );
952 static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_instance_t ***ppp_idsc, int *pi_instance )
954 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
955 vlm_media_instance_t **pp_idsc;
962 TAB_INIT( i_idsc, pp_idsc );
963 for( i = 0; i < p_media->i_instance; i++ )
965 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
966 vlm_media_instance_t *p_idsc = vlm_media_instance_New();
968 if( p_instance->psz_name )
969 p_idsc->psz_name = strdup( p_instance->psz_name );
970 if( p_instance->p_input )
972 p_idsc->i_time = var_GetTime( p_instance->p_input, "time" );
973 p_idsc->i_length = var_GetTime( p_instance->p_input, "length" );
974 p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
975 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
976 p_idsc->b_paused = true;
977 p_idsc->i_rate = var_GetInteger( p_instance->p_input, "rate" );
980 TAB_APPEND( i_idsc, pp_idsc, p_idsc );
983 *pi_instance = i_idsc;
987 static int vlm_ControlMediaInstanceClear( vlm_t *p_vlm, int64_t id )
989 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
994 while( p_media->i_instance > 0 )
995 vlm_ControlMediaInstanceStop( p_vlm, id, p_media->instance[0]->psz_name );
1000 static int vlm_ControlScheduleClear( vlm_t *p_vlm )
1002 while( p_vlm->i_schedule > 0 )
1003 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0] );
1008 static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
1011 vlm_media_t **pp_dsc;
1012 vlm_media_t ***ppp_dsc;
1013 vlm_media_instance_t ***ppp_idsc;
1015 const char *psz_vod;
1029 case VLM_GET_MEDIAS:
1030 ppp_dsc = (vlm_media_t ***)va_arg( args, vlm_media_t *** );
1031 pi_int = (int *)va_arg( args, int * );
1032 return vlm_ControlMediaGets( p_vlm, ppp_dsc, pi_int );
1034 case VLM_CLEAR_MEDIAS:
1035 return vlm_ControlMediaClear( p_vlm );
1037 case VLM_CHANGE_MEDIA:
1038 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
1039 return vlm_ControlMediaChange( p_vlm, p_dsc );
1042 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
1043 p_id = (int64_t*)va_arg( args, int64_t * );
1044 return vlm_ControlMediaAdd( p_vlm, p_dsc, p_id );
1047 id = (int64_t)va_arg( args, int64_t );
1048 return vlm_ControlMediaDel( p_vlm, id );
1051 id = (int64_t)va_arg( args, int64_t );
1052 pp_dsc = (vlm_media_t **)va_arg( args, vlm_media_t ** );
1053 return vlm_ControlMediaGet( p_vlm, id, pp_dsc );
1055 case VLM_GET_MEDIA_ID:
1056 psz_id = (const char*)va_arg( args, const char * );
1057 p_id = (int64_t*)va_arg( args, int64_t * );
1058 return vlm_ControlMediaGetId( p_vlm, psz_id, p_id );
1061 /* Media instance control */
1062 case VLM_GET_MEDIA_INSTANCES:
1063 id = (int64_t)va_arg( args, int64_t );
1064 ppp_idsc = (vlm_media_instance_t ***)va_arg( args, vlm_media_instance_t *** );
1065 pi_int = (int *)va_arg( args, int *);
1066 return vlm_ControlMediaInstanceGets( p_vlm, id, ppp_idsc, pi_int );
1068 case VLM_CLEAR_MEDIA_INSTANCES:
1069 id = (int64_t)va_arg( args, int64_t );
1070 return vlm_ControlMediaInstanceClear( p_vlm, id );
1073 case VLM_START_MEDIA_BROADCAST_INSTANCE:
1074 id = (int64_t)va_arg( args, int64_t );
1075 psz_id = (const char*)va_arg( args, const char* );
1076 i_int = (int)va_arg( args, int );
1077 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, NULL );
1079 case VLM_START_MEDIA_VOD_INSTANCE:
1080 id = (int64_t)va_arg( args, int64_t );
1081 psz_id = (const char*)va_arg( args, const char* );
1082 i_int = (int)va_arg( args, int );
1083 psz_vod = (const char*)va_arg( args, const char* );
1085 return VLC_EGENERIC;
1086 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, psz_vod );
1088 case VLM_STOP_MEDIA_INSTANCE:
1089 id = (int64_t)va_arg( args, int64_t );
1090 psz_id = (const char*)va_arg( args, const char* );
1091 return vlm_ControlMediaInstanceStop( p_vlm, id, psz_id );
1093 case VLM_PAUSE_MEDIA_INSTANCE:
1094 id = (int64_t)va_arg( args, int64_t );
1095 psz_id = (const char*)va_arg( args, const char* );
1096 return vlm_ControlMediaInstancePause( p_vlm, id, psz_id );
1098 case VLM_GET_MEDIA_INSTANCE_TIME:
1099 id = (int64_t)va_arg( args, int64_t );
1100 psz_id = (const char*)va_arg( args, const char* );
1101 pi_i64 = (int64_t*)va_arg( args, int64_t * );
1102 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, pi_i64, NULL );
1103 case VLM_GET_MEDIA_INSTANCE_POSITION:
1104 id = (int64_t)va_arg( args, int64_t );
1105 psz_id = (const char*)va_arg( args, const char* );
1106 pd_double = (double*)va_arg( args, double* );
1107 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, NULL, pd_double );
1109 case VLM_SET_MEDIA_INSTANCE_TIME:
1110 id = (int64_t)va_arg( args, int64_t );
1111 psz_id = (const char*)va_arg( args, const char* );
1112 i_i64 = (int64_t)va_arg( args, int64_t);
1113 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, i_i64, -1 );
1114 case VLM_SET_MEDIA_INSTANCE_POSITION:
1115 id = (int64_t)va_arg( args, int64_t );
1116 psz_id = (const char*)va_arg( args, const char* );
1117 d_double = (double)va_arg( args, double );
1118 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, -1, d_double );
1120 case VLM_CLEAR_SCHEDULES:
1121 return vlm_ControlScheduleClear( p_vlm );
1124 msg_Err( p_vlm, "unknown VLM query" );
1125 return VLC_EGENERIC;
1129 int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... )
1134 va_start( args, i_query );
1135 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
1141 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
1146 va_start( args, i_query );
1148 vlc_mutex_lock( &p_vlm->lock );
1149 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
1150 vlc_mutex_unlock( &p_vlm->lock );