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() */
44 # include <sys/time.h> /* gettimeofday() */
48 # include <time.h> /* ctime() */
49 # include <sys/timeb.h> /* ftime() */
52 #include <vlc_input.h>
53 #include "input_internal.h"
54 #include <vlc_stream.h>
55 #include "vlm_internal.h"
57 #include <vlc_charset.h>
59 #include "../stream_output/stream_output.h"
60 #include "../libvlc.h"
62 /*****************************************************************************
64 *****************************************************************************/
66 static void vlm_Destructor( vlm_t *p_vlm );
67 static int Manage( vlc_object_t * );
68 static int vlm_MediaVodControl( void *, vod_media_t *, const char *, int, va_list );
70 /*****************************************************************************
72 *****************************************************************************/
73 vlm_t *__vlm_New ( vlc_object_t *p_this )
76 vlm_t *p_vlm = NULL, **pp_vlm = &(libvlc_priv (p_this->p_libvlc)->p_vlm);
78 static const char vlm_object_name[] = "vlm daemon";
80 /* Avoid multiple creation */
81 if( var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX ) ||
82 var_Get( p_this->p_libvlc, "vlm_mutex", &lockval ) )
85 vlc_mutex_lock( lockval.p_address );
89 { /* VLM already exists */
90 vlc_object_yield( p_vlm );
91 vlc_mutex_unlock( lockval.p_address );
95 msg_Dbg( p_this, "creating VLM" );
97 p_vlm = vlc_custom_create( p_this, sizeof( *p_vlm ), VLC_OBJECT_GENERIC,
101 vlc_mutex_unlock( lockval.p_address );
105 vlc_mutex_init( &p_vlm->lock );
107 TAB_INIT( p_vlm->i_media, p_vlm->media );
108 TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
111 vlc_object_attach( p_vlm, p_this->p_libvlc );
113 if( vlc_thread_create( p_vlm, "vlm thread",
114 Manage, VLC_THREAD_PRIORITY_LOW, false ) )
116 vlc_mutex_destroy( &p_vlm->lock );
117 vlc_object_release( p_vlm );
121 /* Load our configuration file */
122 psz_vlmconf = var_CreateGetString( p_vlm, "vlm-conf" );
123 if( psz_vlmconf && *psz_vlmconf )
125 vlm_message_t *p_message = NULL;
126 char *psz_buffer = NULL;
128 msg_Dbg( p_this, "loading VLM configuration" );
129 if( asprintf(&psz_buffer, "load %s", psz_vlmconf ) == -1 )
133 msg_Dbg( p_this, psz_buffer );
134 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
135 msg_Warn( p_this, "error while loading the configuration file" );
137 vlm_MessageDelete(p_message);
143 vlc_object_set_destructor( p_vlm, (vlc_destructor_t)vlm_Destructor );
144 *pp_vlm = p_vlm; /* for future reference */
145 vlc_mutex_unlock( lockval.p_address );
150 /*****************************************************************************
152 *****************************************************************************/
153 void vlm_Delete( vlm_t *p_vlm )
157 /* vlm_Delete() is serialized against itself, and against vlm_New().
158 * This way, vlm_Destructor () (called from vlc_objet_release() above)
159 * is serialized against setting libvlc_priv->p_vlm from vlm_New(). */
160 var_Get( p_vlm->p_libvlc, "vlm_mutex", &lockval );
161 vlc_mutex_lock( lockval.p_address );
162 vlc_object_release( p_vlm );
163 vlc_mutex_unlock( lockval.p_address );
166 /*****************************************************************************
168 *****************************************************************************/
169 static void vlm_Destructor( vlm_t *p_vlm )
171 libvlc_priv (p_vlm->p_libvlc)->p_vlm = NULL;
173 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
174 TAB_CLEAN( p_vlm->i_media, p_vlm->media );
176 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
177 TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
179 vlc_mutex_destroy( &p_vlm->lock );
182 /*****************************************************************************
183 * vlm_ExecuteCommand:
184 *****************************************************************************/
185 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
186 vlm_message_t **pp_message)
190 vlc_mutex_lock( &p_vlm->lock );
191 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
192 vlc_mutex_unlock( &p_vlm->lock );
198 int64_t vlm_Date(void)
203 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
205 struct timeval tv_date;
207 /* gettimeofday() cannot fail given &tv_date is a valid address */
208 (void)gettimeofday( &tv_date, NULL );
209 return (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec;
214 /*****************************************************************************
216 *****************************************************************************/
217 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
218 const char *psz_id, int i_query, va_list args )
220 vlm_t *vlm = (vlm_t *)p_private;
225 if( !p_private || !p_vod_media )
228 vlc_mutex_lock( &vlm->lock );
231 for( i = 0, id = -1; i < vlm->i_media; i++ )
233 if( p_vod_media == vlm->media[i]->vod.p_media )
235 id = vlm->media[i]->cfg.id;
241 vlc_mutex_unlock( &vlm->lock );
248 psz = (const char *)va_arg( args, const char * );
249 i_ret = vlm_ControlInternal( vlm, VLM_START_MEDIA_VOD_INSTANCE, id, psz_id, 0, psz );
252 case VOD_MEDIA_PAUSE:
253 i_ret = vlm_ControlInternal( vlm, VLM_PAUSE_MEDIA_INSTANCE, id, psz_id );
257 i_ret = vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, id, psz_id );
262 double d_position = (double)va_arg( args, double );
263 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position/100.0 );
267 case VOD_MEDIA_REWIND:
269 double d_scale = (double)va_arg( args, double );
272 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
273 d_position -= (d_scale / 1000.0);
274 if( d_position < 0.0 )
276 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
280 case VOD_MEDIA_FORWARD:
282 double d_scale = (double)va_arg( args, double );
285 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
286 d_position += (d_scale / 1000.0);
287 if( d_position > 1.0 )
289 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
294 i_ret = VLC_EGENERIC;
298 vlc_mutex_unlock( &vlm->lock );
304 /*****************************************************************************
306 *****************************************************************************/
307 static int Manage( vlc_object_t* p_object )
309 vlm_t *vlm = (vlm_t*)p_object;
314 i_lastcheck = vlm_Date();
318 char **ppsz_scheduled_commands = NULL;
319 int i_scheduled_commands = 0;
321 vlc_mutex_lock( &vlm->lock );
323 /* destroy the inputs that wants to die, and launch the next input */
324 for( i = 0; i < vlm->i_media; i++ )
326 vlm_media_sys_t *p_media = vlm->media[i];
328 for( j = 0; j < p_media->i_instance; )
330 vlm_media_instance_sys_t *p_instance = p_media->instance[j];
332 if( p_instance->p_input && ( p_instance->p_input->b_eof || p_instance->p_input->b_error ) )
334 int i_new_input_index;
337 i_new_input_index = p_instance->i_index + 1;
338 if( !p_media->cfg.b_vod && p_media->cfg.broadcast.b_loop && i_new_input_index >= p_media->cfg.i_input )
339 i_new_input_index = 0;
341 /* FIXME implement multiple input with VOD */
342 if( p_media->cfg.b_vod || i_new_input_index >= p_media->cfg.i_input )
343 vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, p_instance->psz_name );
345 vlm_ControlInternal( vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, p_instance->psz_name, i_new_input_index );
359 for( i = 0; i < vlm->i_schedule; i++ )
361 mtime_t i_real_date = vlm->schedule[i]->i_date;
363 if( vlm->schedule[i]->b_enabled == true )
365 if( vlm->schedule[i]->i_date == 0 ) // now !
367 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
368 i_real_date = i_time;
370 else if( vlm->schedule[i]->i_period != 0 )
373 while( vlm->schedule[i]->i_date + j *
374 vlm->schedule[i]->i_period <= i_lastcheck &&
375 ( vlm->schedule[i]->i_repeat > j ||
376 vlm->schedule[i]->i_repeat == -1 ) )
381 i_real_date = vlm->schedule[i]->i_date + j *
382 vlm->schedule[i]->i_period;
385 if( i_real_date <= i_time && i_real_date > i_lastcheck )
387 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
389 TAB_APPEND( i_scheduled_commands,
390 ppsz_scheduled_commands,
391 strdup(vlm->schedule[i]->command[j] ) );
396 while( i_scheduled_commands )
398 vlm_message_t *message = NULL;
399 char *psz_command = ppsz_scheduled_commands[0];
400 ExecuteCommand( vlm, psz_command,&message );
402 /* for now, drop the message */
403 vlm_MessageDelete( message );
404 TAB_REMOVE( i_scheduled_commands,
405 ppsz_scheduled_commands,
410 i_lastcheck = i_time;
412 vlc_mutex_unlock( &vlm->lock );
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_ItemNew( p_vlm, p_cfg->ppsz_input[0],
507 if( p_cfg->psz_output )
508 asprintf( &psz_output, "%s:description", p_cfg->psz_output );
510 asprintf( &psz_output, "#description" );
512 asprintf( &psz_dup, "sout=%s", psz_output);
513 input_ItemAddOption( p_media->vod.p_item, psz_dup );
515 for( i = 0; i < p_cfg->i_option; i++ )
516 input_ItemAddOption( p_media->vod.p_item,
517 p_cfg->ppsz_option[i] );
519 asprintf( &psz_header, _("Media: %s"), p_cfg->psz_name );
521 if( (p_input = input_CreateThreadExtended( p_vlm, p_media->vod.p_item, psz_header, NULL ) ) )
523 while( !p_input->b_eof && !p_input->b_error )
526 input_StopThread( p_input );
527 vlc_object_release( p_input );
532 if( p_cfg->vod.psz_mux )
535 es_format_t es, *p_es = &es;
538 sprintf( fourcc, "%4.4s", p_cfg->vod.psz_mux );
539 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
540 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
542 /* XXX: Don't do it that way, but properly use a new input item ref. */
543 item = *p_media->vod.p_item;
546 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
548 p_media->vod.p_media =
549 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
553 p_media->vod.p_media =
554 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, p_media->vod.p_item );
560 /* TODO start media if needed */
563 /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
567 static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
569 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
572 if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
574 if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
579 /* TODO check what are the changes being done (stop instance if needed) */
582 vlm_media_Clean( &p_media->cfg );
583 vlm_media_Copy( &p_media->cfg, p_cfg );
585 return vlm_OnMediaUpdate( p_vlm, p_media );
588 static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id )
590 vlm_media_sys_t *p_media;
592 if( vlm_MediaDescriptionCheck( p_vlm, p_cfg ) || vlm_ControlMediaGetByName( p_vlm, p_cfg->psz_name ) )
594 msg_Err( p_vlm, "invalid media description" );
597 /* Check if we need to load the VOD server */
598 if( p_cfg->b_vod && !p_vlm->i_vod )
600 p_vlm->p_vod = vlc_custom_create( VLC_OBJECT(p_vlm), sizeof( vod_t ),
601 VLC_OBJECT_GENERIC, "vod server" );
602 vlc_object_attach( p_vlm->p_vod, p_vlm );
603 p_vlm->p_vod->p_module = module_Need( p_vlm->p_vod, "vod server", 0, 0 );
604 if( !p_vlm->p_vod->p_module )
606 msg_Err( p_vlm, "cannot find vod server" );
607 vlc_object_detach( p_vlm->p_vod );
608 vlc_object_release( p_vlm->p_vod );
613 p_vlm->p_vod->p_data = p_vlm;
614 p_vlm->p_vod->pf_media_control = vlm_MediaVodControl;
617 p_media = malloc( sizeof( vlm_media_sys_t ) );
620 memset( p_media, 0, sizeof(vlm_media_sys_t) );
625 vlm_media_Copy( &p_media->cfg, p_cfg );
626 p_media->cfg.id = p_vlm->i_id++;
627 /* FIXME do we do something here if enabled is true ? */
629 p_media->vod.p_item = input_ItemNew( p_vlm, NULL, NULL );
631 p_media->vod.p_media = NULL;
632 TAB_INIT( p_media->i_instance, p_media->instance );
635 TAB_APPEND( p_vlm->i_media, p_vlm->media, p_media );
638 *p_id = p_media->cfg.id;
640 return vlm_OnMediaUpdate( p_vlm, p_media );
643 static int vlm_ControlMediaDel( vlm_t *p_vlm, int64_t id )
645 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
650 while( p_media->i_instance > 0 )
651 vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, p_media->instance[0]->psz_name );
653 if( p_media->cfg.b_vod )
655 p_media->cfg.b_enabled = false;
656 vlm_OnMediaUpdate( p_vlm, p_media );
660 vlm_media_Clean( &p_media->cfg );
662 vlc_gc_decref( p_media->vod.p_item );
664 TAB_REMOVE( p_vlm->i_media, p_vlm->media, p_media );
668 /* Check if we need to unload the VOD server */
669 if( p_vlm->p_vod && p_vlm->i_vod <= 0 )
671 module_Unneed( p_vlm->p_vod, p_vlm->p_vod->p_module );
672 vlc_object_detach( p_vlm->p_vod );
673 vlc_object_release( p_vlm->p_vod );
679 static int vlm_ControlMediaGets( vlm_t *p_vlm, vlm_media_t ***ppp_dsc, int *pi_dsc )
681 vlm_media_t **pp_dsc;
685 TAB_INIT( i_dsc, pp_dsc );
686 for( i = 0; i < p_vlm->i_media; i++ )
688 vlm_media_t *p_dsc = vlm_media_Duplicate( &p_vlm->media[i]->cfg );
689 TAB_APPEND( i_dsc, pp_dsc, p_dsc );
697 static int vlm_ControlMediaClear( vlm_t *p_vlm )
699 while( p_vlm->i_media > 0 )
700 vlm_ControlMediaDel( p_vlm, p_vlm->media[0]->cfg.id );
704 static int vlm_ControlMediaGet( vlm_t *p_vlm, int64_t id, vlm_media_t **pp_dsc )
706 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
710 *pp_dsc = vlm_media_Duplicate( &p_media->cfg );
713 static int vlm_ControlMediaGetId( vlm_t *p_vlm, const char *psz_name, int64_t *p_id )
715 vlm_media_sys_t *p_media = vlm_ControlMediaGetByName( p_vlm, psz_name );
719 *p_id = p_media->cfg.id;
723 static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
727 for( i = 0; i < p_media->i_instance; i++ )
729 const char *psz = p_media->instance[i]->psz_name;
730 if( ( psz == NULL && psz_id == NULL ) ||
731 ( psz && psz_id && !strcmp( psz, psz_id ) ) )
732 return p_media->instance[i];
736 static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
738 vlm_media_instance_sys_t *p_instance = malloc( sizeof(vlm_media_instance_sys_t) );
742 memset( p_instance, 0, sizeof(vlm_media_instance_sys_t) );
744 p_instance->psz_name = NULL;
746 p_instance->psz_name = strdup( psz_name );
748 p_instance->p_item = input_ItemNew( p_vlm, NULL, NULL );
750 p_instance->i_index = 0;
751 p_instance->b_sout_keep = false;
752 p_instance->p_input = NULL;
753 p_instance->p_sout = NULL;
757 static void vlm_MediaInstanceDelete( vlm_media_instance_sys_t *p_instance )
759 if( p_instance->p_input )
761 input_StopThread( p_instance->p_input );
762 p_instance->p_sout = input_DetachSout( p_instance->p_input );
763 vlc_object_release( p_instance->p_input );
765 if( p_instance->p_sout )
766 sout_DeleteInstance( p_instance->p_sout );
768 vlc_gc_decref( p_instance->p_item );
769 free( p_instance->psz_name );
774 static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
776 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
777 vlm_media_instance_sys_t *p_instance;
780 if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
783 /* TODO support multiple input for VOD with sout-keep ? */
785 if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
788 if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
791 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
794 vlm_media_t *p_cfg = &p_media->cfg;
797 p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
801 if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
804 asprintf( &psz_buffer, "sout=%s%s%s",
805 p_cfg->psz_output ? p_cfg->psz_output : "",
806 (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
807 psz_vod_output ? psz_vod_output : "" );
808 input_ItemAddOption( p_instance->p_item, psz_buffer );
812 for( i = 0; i < p_cfg->i_option; i++ )
814 if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
815 p_instance->b_sout_keep = true;
816 else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
817 p_instance->b_sout_keep = false;
819 input_ItemAddOption( p_instance->p_item, p_cfg->ppsz_option[i] );
821 TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
824 /* Stop old instance */
825 if( p_instance->p_input )
827 if( p_instance->i_index == i_input_index &&
828 !p_instance->p_input->b_eof && !p_instance->p_input->b_error )
830 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
831 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
835 input_StopThread( p_instance->p_input );
836 p_instance->p_sout = input_DetachSout( p_instance->p_input );
837 vlc_object_release( p_instance->p_input );
838 if( !p_instance->b_sout_keep && p_instance->p_sout )
840 sout_DeleteInstance( p_instance->p_sout );
841 p_instance->p_sout = NULL;
846 p_instance->i_index = i_input_index;
847 input_item_SetURI( p_instance->p_item, p_media->cfg.ppsz_input[p_instance->i_index] ) ;
849 asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name );
850 p_instance->p_input = input_CreateThreadExtended( p_vlm, p_instance->p_item, psz_log, p_instance->p_sout );
851 if( !p_instance->p_input )
853 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
854 vlm_MediaInstanceDelete( p_instance );
861 static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
863 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
864 vlm_media_instance_sys_t *p_instance;
869 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
873 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
875 vlm_MediaInstanceDelete( p_instance );
879 static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
881 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
882 vlm_media_instance_sys_t *p_instance;
888 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
889 if( !p_instance || !p_instance->p_input )
892 /* Toggle pause state */
893 i_state = var_GetInteger( p_instance->p_input, "state" );
894 if( i_state == PAUSE_S )
895 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
896 else if( i_state == PLAYING_S )
897 var_SetInteger( p_instance->p_input, "state", PAUSE_S );
900 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
902 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
903 vlm_media_instance_sys_t *p_instance;
908 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
909 if( !p_instance || !p_instance->p_input )
913 *pi_time = var_GetTime( p_instance->p_input, "time" );
915 *pd_position = var_GetFloat( p_instance->p_input, "position" );
918 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t i_time, double d_position )
920 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
921 vlm_media_instance_sys_t *p_instance;
926 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
927 if( !p_instance || !p_instance->p_input )
931 return var_SetTime( p_instance->p_input, "time", i_time );
932 else if( d_position >= 0 && d_position <= 100 )
933 return var_SetFloat( p_instance->p_input, "position", d_position );
937 static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_instance_t ***ppp_idsc, int *pi_instance )
939 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
940 vlm_media_instance_t **pp_idsc;
947 TAB_INIT( i_idsc, pp_idsc );
948 for( i = 0; i < p_media->i_instance; i++ )
950 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
951 vlm_media_instance_t *p_idsc = vlm_media_instance_New();
953 if( p_instance->psz_name )
954 p_idsc->psz_name = strdup( p_instance->psz_name );
955 if( p_instance->p_input )
957 p_idsc->i_time = var_GetTime( p_instance->p_input, "time" );
958 p_idsc->i_length = var_GetTime( p_instance->p_input, "length" );
959 p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
960 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
961 p_idsc->b_paused = true;
962 p_idsc->i_rate = var_GetInteger( p_instance->p_input, "rate" );
965 TAB_APPEND( i_idsc, pp_idsc, p_idsc );
968 *pi_instance = i_idsc;
972 static int vlm_ControlMediaInstanceClear( vlm_t *p_vlm, int64_t id )
974 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
979 while( p_media->i_instance > 0 )
980 vlm_ControlMediaInstanceStop( p_vlm, id, p_media->instance[0]->psz_name );
985 static int vlm_ControlScheduleClear( vlm_t *p_vlm )
987 while( p_vlm->i_schedule > 0 )
988 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0] );
993 static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
996 vlm_media_t **pp_dsc;
997 vlm_media_t ***ppp_dsc;
998 vlm_media_instance_t ***ppp_idsc;
1000 const char *psz_vod;
1014 case VLM_GET_MEDIAS:
1015 ppp_dsc = (vlm_media_t ***)va_arg( args, vlm_media_t *** );
1016 pi_int = (int *)va_arg( args, int * );
1017 return vlm_ControlMediaGets( p_vlm, ppp_dsc, pi_int );
1019 case VLM_CLEAR_MEDIAS:
1020 return vlm_ControlMediaClear( p_vlm );
1022 case VLM_CHANGE_MEDIA:
1023 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
1024 return vlm_ControlMediaChange( p_vlm, p_dsc );
1027 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
1028 p_id = (int64_t*)va_arg( args, int64_t * );
1029 return vlm_ControlMediaAdd( p_vlm, p_dsc, p_id );
1032 id = (int64_t)va_arg( args, int64_t );
1033 return vlm_ControlMediaDel( p_vlm, id );
1036 id = (int64_t)va_arg( args, int64_t );
1037 pp_dsc = (vlm_media_t **)va_arg( args, vlm_media_t ** );
1038 return vlm_ControlMediaGet( p_vlm, id, pp_dsc );
1040 case VLM_GET_MEDIA_ID:
1041 psz_id = (const char*)va_arg( args, const char * );
1042 p_id = (int64_t*)va_arg( args, int64_t * );
1043 return vlm_ControlMediaGetId( p_vlm, psz_id, p_id );
1046 /* Media instance control */
1047 case VLM_GET_MEDIA_INSTANCES:
1048 id = (int64_t)va_arg( args, int64_t );
1049 ppp_idsc = (vlm_media_instance_t ***)va_arg( args, vlm_media_instance_t *** );
1050 pi_int = (int *)va_arg( args, int *);
1051 return vlm_ControlMediaInstanceGets( p_vlm, id, ppp_idsc, pi_int );
1053 case VLM_CLEAR_MEDIA_INSTANCES:
1054 id = (int64_t)va_arg( args, int64_t );
1055 return vlm_ControlMediaInstanceClear( p_vlm, id );
1058 case VLM_START_MEDIA_BROADCAST_INSTANCE:
1059 id = (int64_t)va_arg( args, int64_t );
1060 psz_id = (const char*)va_arg( args, const char* );
1061 i_int = (int)va_arg( args, int );
1062 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, NULL );
1064 case VLM_START_MEDIA_VOD_INSTANCE:
1065 id = (int64_t)va_arg( args, int64_t );
1066 psz_id = (const char*)va_arg( args, const char* );
1067 i_int = (int)va_arg( args, int );
1068 psz_vod = (const char*)va_arg( args, const char* );
1070 return VLC_EGENERIC;
1071 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, psz_vod );
1073 case VLM_STOP_MEDIA_INSTANCE:
1074 id = (int64_t)va_arg( args, int64_t );
1075 psz_id = (const char*)va_arg( args, const char* );
1076 return vlm_ControlMediaInstanceStop( p_vlm, id, psz_id );
1078 case VLM_PAUSE_MEDIA_INSTANCE:
1079 id = (int64_t)va_arg( args, int64_t );
1080 psz_id = (const char*)va_arg( args, const char* );
1081 return vlm_ControlMediaInstancePause( p_vlm, id, psz_id );
1083 case VLM_GET_MEDIA_INSTANCE_TIME:
1084 id = (int64_t)va_arg( args, int64_t );
1085 psz_id = (const char*)va_arg( args, const char* );
1086 pi_i64 = (int64_t*)va_arg( args, int64_t * );
1087 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, pi_i64, NULL );
1088 case VLM_GET_MEDIA_INSTANCE_POSITION:
1089 id = (int64_t)va_arg( args, int64_t );
1090 psz_id = (const char*)va_arg( args, const char* );
1091 pd_double = (double*)va_arg( args, double* );
1092 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, NULL, pd_double );
1094 case VLM_SET_MEDIA_INSTANCE_TIME:
1095 id = (int64_t)va_arg( args, int64_t );
1096 psz_id = (const char*)va_arg( args, const char* );
1097 i_i64 = (int64_t)va_arg( args, int64_t);
1098 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, i_i64, -1 );
1099 case VLM_SET_MEDIA_INSTANCE_POSITION:
1100 id = (int64_t)va_arg( args, int64_t );
1101 psz_id = (const char*)va_arg( args, const char* );
1102 d_double = (double)va_arg( args, double );
1103 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, -1, d_double );
1105 case VLM_CLEAR_SCHEDULES:
1106 return vlm_ControlScheduleClear( p_vlm );
1109 msg_Err( p_vlm, "unknown VLM query" );
1110 return VLC_EGENERIC;
1114 int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... )
1119 va_start( args, i_query );
1120 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
1126 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
1131 va_start( args, i_query );
1133 vlc_mutex_lock( &p_vlm->lock );
1134 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
1135 vlc_mutex_unlock( &p_vlm->lock );
1142 #else /* ENABLE_VLM */
1144 /* We just define an empty wrapper */
1145 vlm_t *__vlm_New( vlc_object_t *a )
1147 msg_Err( a, "VideoLAN manager support is disabled" );
1151 void vlm_Delete( vlm_t *a )
1156 int vlm_ExecuteCommand( vlm_t *a, const char *b, vlm_message_t **c )
1161 vlm_message_t *vlm_MessageNew( const char *psz_name,
1162 const char *psz_format, ... )
1164 (void)psz_name; (void)psz_format;
1168 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1169 vlm_message_t *p_child )
1174 void vlm_MessageDelete( vlm_message_t *a )
1179 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
1181 (void)p_vlm; (void)i_query;
1182 return VLC_EGENERIC;
1185 #endif /* ENABLE_VLM */