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 *****************************************************************************/
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>
54 #include "vlm_internal.h"
56 #include <vlc_charset.h>
58 #include "../stream_output/stream_output.h"
60 /*****************************************************************************
62 *****************************************************************************/
64 static int vlm_ControlInternal( vlm_t *, int, ... );
67 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_sys_t *, vlm_schedule_sys_t *, const char * );
68 static vlm_message_t *vlm_Help( vlm_t *, char * );
70 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *, const char * );
72 static char *Save( vlm_t * );
73 static int Load( vlm_t *, char * );
75 static int ExecuteCommand( vlm_t *, const char *, vlm_message_t ** );
77 static int Manage( vlc_object_t * );
79 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name );
80 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched, const char *psz_name );
81 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
82 const char *psz_value );
84 static int vlm_MediaVodControl( void *, vod_media_t *, const char *, int, va_list );
87 static vlm_media_sys_t *vlm_MediaSearch( vlm_t *, const char *);
89 /*****************************************************************************
91 *****************************************************************************/
92 vlm_t *__vlm_New ( vlc_object_t *p_this )
98 /* Avoid multiple creation */
99 if( var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX ) ||
100 var_Get( p_this->p_libvlc, "vlm_mutex", &lockval ) )
103 vlc_mutex_lock( lockval.p_address );
105 p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE );
108 vlc_object_yield( p_vlm );
109 vlc_mutex_unlock( lockval.p_address );
113 msg_Dbg( p_this, "creating VLM" );
115 p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM );
118 vlc_mutex_unlock( lockval.p_address );
122 vlc_mutex_init( p_this->p_libvlc, &p_vlm->lock );
124 TAB_INIT( p_vlm->i_media, p_vlm->media );
125 TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
128 vlc_object_yield( p_vlm );
129 vlc_object_attach( p_vlm, p_this->p_libvlc );
131 if( vlc_thread_create( p_vlm, "vlm thread",
132 Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
134 vlc_mutex_destroy( &p_vlm->lock );
135 vlc_object_destroy( p_vlm );
139 /* Load our configuration file */
140 psz_vlmconf = var_CreateGetString( p_vlm, "vlm-conf" );
141 if( psz_vlmconf && *psz_vlmconf )
143 vlm_message_t *p_message = NULL;
144 char *psz_buffer = NULL;
146 msg_Dbg( p_this, "loading VLM configuration" );
147 asprintf(&psz_buffer, "load %s", psz_vlmconf );
150 msg_Dbg( p_this, psz_buffer );
151 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
152 msg_Warn( p_this, "error while loading the configuration file" );
154 vlm_MessageDelete(p_message);
160 vlc_mutex_unlock( lockval.p_address );
165 /*****************************************************************************
167 *****************************************************************************/
168 void vlm_Delete( vlm_t *p_vlm )
172 var_Get( p_vlm->p_libvlc, "vlm_mutex", &lockval );
173 vlc_mutex_lock( lockval.p_address );
175 vlc_object_release( p_vlm );
177 if( p_vlm->p_internals->i_refcount > 0 )
179 vlc_mutex_unlock( lockval.p_address );
183 vlc_object_kill( p_vlm );
184 vlc_thread_join( p_vlm );
186 vlc_object_detach( p_vlm );
188 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
189 TAB_CLEAN( p_vlm->i_media, p_vlm->media );
191 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
192 TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
194 vlc_mutex_destroy( &p_vlm->lock );
196 vlc_object_destroy( p_vlm );
197 vlc_mutex_unlock( lockval.p_address );
200 /*****************************************************************************
201 * vlm_ExecuteCommand:
202 *****************************************************************************/
203 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
204 vlm_message_t **pp_message)
208 vlc_mutex_lock( &p_vlm->lock );
209 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
210 vlc_mutex_unlock( &p_vlm->lock );
216 static const char quotes[] = "\"'";
218 * FindCommandEnd: look for the end of a possibly quoted string
219 * @return NULL on mal-formatted string,
220 * pointer past the last character otherwise.
222 static const char *FindCommandEnd( const char *psz_sent )
226 while( (c = *psz_sent) != '\0' )
230 if( strchr(quotes,c) ) // opening quote
232 else if( isspace(c) ) // non-escaped space
236 psz_sent++; // skip escaped character
237 if( *psz_sent == '\0' )
243 if( c == quote ) // non-escaped matching quote
245 else if( (quote == '"') && (c == '\\') )
247 psz_sent++; // skip escaped character
248 if (*psz_sent == '\0')
249 return NULL; // error, closing quote missing
255 // error (NULL) if we could not find a matching quote
256 return quote ? NULL : psz_sent;
261 * Unescape a nul-terminated string.
262 * Note that in and out can be identical.
264 * @param out output buffer (at least <strlen (in) + 1> characters long)
265 * @param in nul-terminated string to be unescaped
267 * @return 0 on success, -1 on error.
269 static int Unescape( char *out, const char *in )
273 while( (c = *in++) != '\0' )
277 if (strchr(quotes,c)) // opening quote
301 /* None of the special cases - copy the backslash */
307 if( c == quote ) // non-escaped matching quote
312 if( (quote == '"') && (c == '\\') )
321 case '\0': // should never happen
325 /* None of the special cases - copy the backslash */
337 /*****************************************************************************
338 * ExecuteCommand: The main state machine
339 *****************************************************************************
340 * Execute a command which ends with '\0' (string)
341 *****************************************************************************/
342 static int ExecuteSyntaxError( const char *psz_cmd, vlm_message_t **pp_status )
344 *pp_status = vlm_MessageNew( psz_cmd, "Wrong command syntax" );
348 static vlc_bool_t ExecuteIsMedia( vlm_t *p_vlm, const char *psz_name )
352 if( !psz_name || vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
356 static vlc_bool_t ExecuteIsSchedule( vlm_t *p_vlm, const char *psz_name )
358 if( !psz_name || !vlm_ScheduleSearch( p_vlm, psz_name ) )
363 static int ExecuteDel( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
365 vlm_media_sys_t *p_media;
366 vlm_schedule_sys_t *p_schedule;
368 p_media = vlm_MediaSearch( p_vlm, psz_name );
369 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
371 if( p_schedule != NULL )
373 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
375 else if( p_media != NULL )
377 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_media->cfg.id );
379 else if( !strcmp(psz_name, "media") )
381 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
383 else if( !strcmp(psz_name, "schedule") )
385 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
387 else if( !strcmp(psz_name, "all") )
389 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
390 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
394 *pp_status = vlm_MessageNew( "del", "%s: media unknown", psz_name );
398 *pp_status = vlm_MessageNew( "del", NULL );
402 static int ExecuteShow( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
404 vlm_media_sys_t *p_media;
405 vlm_schedule_sys_t *p_schedule;
409 *pp_status = vlm_Show( p_vlm, NULL, NULL, NULL );
413 p_media = vlm_MediaSearch( p_vlm, psz_name );
414 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
416 if( p_schedule != NULL )
417 *pp_status = vlm_Show( p_vlm, NULL, p_schedule, NULL );
418 else if( p_media != NULL )
419 *pp_status = vlm_Show( p_vlm, p_media, NULL, NULL );
421 *pp_status = vlm_Show( p_vlm, NULL, NULL, psz_name );
426 static int ExecuteHelp( vlm_t *p_vlm, vlm_message_t **pp_status )
428 *pp_status = vlm_Help( p_vlm, NULL );
432 static int ExecuteControl( vlm_t *p_vlm, const char *psz_name, const int i_arg, char ** ppsz_arg, vlm_message_t **pp_status )
434 vlm_media_sys_t *p_media;
435 const char *psz_control = NULL;
436 const char *psz_instance = NULL;
437 const char *psz_argument = NULL;
441 if( !ExecuteIsMedia( p_vlm, psz_name ) )
443 *pp_status = vlm_MessageNew( "control", "%s: media unknown", psz_name );
449 #define IS(txt) ( !strcmp( ppsz_arg[i_index], (txt) ) )
451 if( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") )
454 psz_instance = ppsz_arg[0];
456 if( i_index >= i_arg || ( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") ) )
457 return ExecuteSyntaxError( "control", pp_status );
460 psz_control = ppsz_arg[i_index];
462 if( i_index+1 < i_arg )
463 psz_argument = ppsz_arg[i_index+1];
465 p_media = vlm_MediaSearch( p_vlm, psz_name );
468 if( !strcmp( psz_control, "play" ) )
470 int i_input_index = 0;
473 if( ( psz_argument && sscanf(psz_argument, "%d", &i) == 1 ) && i > 0 && i-1 < p_media->cfg.i_input )
477 else if( psz_argument )
480 vlm_media_t *p_cfg = &p_media->cfg;
481 for ( j=0; j < p_cfg->i_input; j++)
483 if( !strcmp( p_cfg->ppsz_input[j], psz_argument ) )
491 if( p_media->cfg.b_vod )
492 i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_VOD_INSTANCE, p_media->cfg.id, psz_instance, i_input_index, NULL ); // we should get here now
494 i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, psz_instance, i_input_index );
496 else if( !strcmp( psz_control, "seek" ) )
500 vlc_bool_t b_relative;
501 if( psz_argument[0] == '+' || psz_argument[0] == '-' )
502 b_relative = VLC_TRUE;
504 b_relative = VLC_FALSE;
506 if( strstr( psz_argument, "ms" ) || strstr( psz_argument, "s" ) )
511 if( strstr( psz_argument, "ms" ) )
512 i_new_time = 1000 * (int64_t)atoi( psz_argument );
514 i_new_time = 1000000 * (int64_t)atoi( psz_argument );
519 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, &i_time );
520 i_new_time += i_time;
524 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, i_new_time );
529 double d_new_position = i18n_atof( psz_argument ) / 100.0;
533 double d_position = 0.0;
535 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
536 d_new_position += d_position;
538 if( d_new_position < 0.0 )
539 d_new_position = 0.0;
540 else if( d_new_position > 1.0 )
541 d_new_position = 1.0;
542 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_new_position );
547 i_result = VLC_EGENERIC;
550 else if( !strcmp( psz_control, "rewind" ) )
554 const double d_scale = i18n_atof( psz_argument );
557 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
558 d_position -= (d_scale / 1000.0);
559 if( d_position < 0.0 )
561 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
565 i_result = VLC_EGENERIC;
568 else if( !strcmp( psz_control, "forward" ) )
572 const double d_scale = i18n_atof( psz_argument );
575 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
576 d_position += (d_scale / 1000.0);
577 if( d_position > 1.0 )
579 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
584 i_result = VLC_EGENERIC;
587 else if( !strcmp( psz_control, "stop" ) )
589 i_result = vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
591 else if( !strcmp( psz_control, "pause" ) )
593 i_result = vlm_ControlInternal( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
597 i_result = VLC_EGENERIC;
602 *pp_status = vlm_MessageNew( "control", "unknown error" );
605 *pp_status = vlm_MessageNew( "control", NULL );
609 static int ExecuteExport( vlm_t *p_vlm, vlm_message_t **pp_status )
611 char *psz_export = Save( p_vlm );
613 *pp_status = vlm_MessageNew( "export", psz_export );
618 static int ExecuteSave( vlm_t *p_vlm, const char *psz_file, vlm_message_t **pp_status )
620 FILE *f = utf8_fopen( psz_file, "wt" );
626 psz_save = Save( p_vlm );
627 if( psz_save == NULL )
632 fwrite( psz_save, strlen( psz_save ), 1, f );
636 *pp_status = vlm_MessageNew( "save", NULL );
640 *pp_status = vlm_MessageNew( "save", "Unable to save to file" );
644 static int ExecuteLoad( vlm_t *p_vlm, const char *psz_url, vlm_message_t **pp_status )
646 stream_t *p_stream = stream_UrlNew( p_vlm, psz_url );
652 *pp_status = vlm_MessageNew( "load", "Unable to load from file" );
657 if( stream_Seek( p_stream, 0 ) != 0 )
659 stream_Delete( p_stream );
661 *pp_status = vlm_MessageNew( "load", "Read file error" );
665 i_size = stream_Size( p_stream );
667 psz_buffer = malloc( i_size + 1 );
670 stream_Delete( p_stream );
672 *pp_status = vlm_MessageNew( "load", "Read file error" );
676 stream_Read( p_stream, psz_buffer, i_size );
677 psz_buffer[i_size] = '\0';
679 stream_Delete( p_stream );
681 if( Load( p_vlm, psz_buffer ) )
685 *pp_status = vlm_MessageNew( "load", "Error while loading file" );
691 *pp_status = vlm_MessageNew( "load", NULL );
695 static int ExecuteScheduleProperty( vlm_t *p_vlm, vlm_schedule_sys_t *p_schedule, vlc_bool_t b_new,
696 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
698 const char *psz_cmd = b_new ? "new" : "setup";
701 for( i = 0; i < i_property; i++ )
703 if( !strcmp( ppsz_property[i], "enabled" ) ||
704 !strcmp( ppsz_property[i], "disabled" ) )
706 vlm_ScheduleSetup( p_schedule, ppsz_property[i], NULL );
708 else if( !strcmp( ppsz_property[i], "append" ) )
712 /* Beware: everything behind append is considered as
715 if( ++i >= i_property )
718 psz_line = strdup( ppsz_property[i] );
719 for( j = i+1; j < i_property; j++ )
721 psz_line = realloc( psz_line, strlen(psz_line) + strlen(ppsz_property[j]) + 1 + 1 );
722 strcat( psz_line, " " );
723 strcat( psz_line, ppsz_property[j] );
726 vlm_ScheduleSetup( p_schedule, "append", psz_line );
731 if( i + 1 >= i_property )
734 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
735 return ExecuteSyntaxError( psz_cmd, pp_status );
738 vlm_ScheduleSetup( p_schedule, ppsz_property[i], ppsz_property[i+1] );
742 *pp_status = vlm_MessageNew( psz_cmd, NULL );
746 static int ExecuteMediaProperty( vlm_t *p_vlm, int64_t id, vlc_bool_t b_new,
747 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
749 const char *psz_cmd = b_new ? "new" : "setup";
750 vlm_media_t *p_cfg = NULL;
756 #define ERROR( txt ) do { *pp_status = vlm_MessageNew( psz_cmd, txt); goto error; } while(0)
757 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA, id, &p_cfg ) )
758 ERROR( "unknown media" );
760 #define MISSING(cmd) do { if( !psz_value ) ERROR( "missing argument for " cmd ); } while(0)
761 for( i = 0; i < i_property; i++ )
763 const char *psz_option = ppsz_property[i];
764 const char *psz_value = i+1 < i_property ? ppsz_property[i+1] : NULL;
766 if( !strcmp( psz_option, "enabled" ) )
768 p_cfg->b_enabled = VLC_TRUE;
770 else if( !strcmp( psz_option, "disabled" ) )
772 p_cfg->b_enabled = VLC_FALSE;
774 else if( !strcmp( psz_option, "input" ) )
777 TAB_APPEND( p_cfg->i_input, p_cfg->ppsz_input, strdup(psz_value) );
780 else if( !strcmp( psz_option, "inputdel" ) && psz_value && !strcmp( psz_value, "all" ) )
782 while( p_cfg->i_input > 0 )
783 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[0] );
786 else if( !strcmp( psz_option, "inputdel" ) )
790 MISSING( "inputdel" );
792 for( j = 0; j < p_cfg->i_input; j++ )
794 if( !strcmp( p_cfg->ppsz_input[j], psz_value ) )
796 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[j] );
802 else if( !strcmp( psz_option, "inputdeln" ) )
806 MISSING( "inputdeln" );
808 i_index = atoi( psz_value );
809 if( i_index > 0 && i_index <= p_cfg->i_input )
810 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[i_index-1] );
813 else if( !strcmp( psz_option, "output" ) )
817 if( p_cfg->psz_output != NULL )
818 free( p_cfg->psz_output );
819 p_cfg->psz_output = *psz_value ? strdup( psz_value ) : NULL;
822 else if( !strcmp( psz_option, "option" ) )
826 TAB_APPEND( p_cfg->i_option, p_cfg->ppsz_option, strdup( psz_value ) );
829 else if( !strcmp( psz_option, "loop" ) )
832 ERROR( "invalid loop option for vod" );
833 p_cfg->broadcast.b_loop = VLC_TRUE;
835 else if( !strcmp( psz_option, "unloop" ) )
838 ERROR( "invalid unloop option for vod" );
839 p_cfg->broadcast.b_loop = VLC_FALSE;
841 else if( !strcmp( psz_option, "mux" ) )
845 ERROR( "invalid mux option for broadcast" );
847 if( p_cfg->vod.psz_mux )
848 free( p_cfg->vod.psz_mux );
849 p_cfg->vod.psz_mux = *psz_value ? strdup( psz_value ) : NULL;
854 fprintf( stderr, "PROP: name=%s unknown\n", psz_option );
855 ERROR( "Wrong command syntax" );
862 i_result = vlm_ControlInternal( p_vlm, VLM_CHANGE_MEDIA, p_cfg );
863 vlm_media_Delete( p_cfg );
865 *pp_status = vlm_MessageNew( psz_cmd, NULL );
872 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_cfg->id );
873 vlm_media_Delete( p_cfg );
878 static int ExecuteNew( vlm_t *p_vlm, const char *psz_name, const char *psz_type, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
881 if( !strcmp( psz_name, "all" ) || !strcmp( psz_name, "media" ) || !strcmp( psz_name, "schedule" ) )
883 *pp_status = vlm_MessageNew( "new", "\"all\", \"media\" and \"schedule\" are reserved names" );
886 if( ExecuteIsMedia( p_vlm, psz_name ) || ExecuteIsSchedule( p_vlm, psz_name ) )
888 *pp_status = vlm_MessageNew( "new", "%s: Name already in use", psz_name );
892 if( !strcmp( psz_type, "schedule" ) )
894 vlm_schedule_sys_t *p_schedule = vlm_ScheduleNew( p_vlm, psz_name );
897 *pp_status = vlm_MessageNew( "new", "could not create schedule" );
900 return ExecuteScheduleProperty( p_vlm, p_schedule, VLC_TRUE, i_property, ppsz_property, pp_status );
902 else if( !strcmp( psz_type, "vod" ) || !strcmp( psz_type, "broadcast" ) )
907 vlm_media_Init( &cfg );
908 cfg.psz_name = strdup( psz_name );
909 cfg.b_vod = !strcmp( psz_type, "vod" );
911 if( vlm_ControlInternal( p_vlm, VLM_ADD_MEDIA, &cfg, &id ) )
913 vlm_media_Clean( &cfg );
914 *pp_status = vlm_MessageNew( "new", "could not create media" );
917 vlm_media_Clean( &cfg );
918 return ExecuteMediaProperty( p_vlm, id, VLC_TRUE, i_property, ppsz_property, pp_status );
922 *pp_status = vlm_MessageNew( "new", "%s: Choose between vod, broadcast or schedule", psz_type );
927 static int ExecuteSetup( vlm_t *p_vlm, const char *psz_name, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
929 if( ExecuteIsSchedule( p_vlm, psz_name ) )
931 vlm_schedule_sys_t *p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
932 return ExecuteScheduleProperty( p_vlm, p_schedule, VLC_FALSE, i_property, ppsz_property, pp_status );
934 else if( ExecuteIsMedia( p_vlm, psz_name ) )
937 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
939 return ExecuteMediaProperty( p_vlm, id, VLC_FALSE, i_property, ppsz_property, pp_status );
943 *pp_status = vlm_MessageNew( "setup", "%s unknown", psz_name );
947 static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
948 vlm_message_t **pp_message )
950 size_t i_command = 0;
951 char buf[strlen (psz_command) + 1], *psz_buf = buf;
952 char *ppsz_command[3+sizeof (buf) / 2];
953 vlm_message_t *p_message = NULL;
955 /* First, parse the line and cut it */
956 while( *psz_command != '\0' )
958 const char *psz_temp;
960 if(isspace (*psz_command))
966 /* support for comments */
967 if( i_command == 0 && *psz_command == '#')
969 p_message = vlm_MessageNew( "", NULL );
973 psz_temp = FindCommandEnd( psz_command );
975 if( psz_temp == NULL )
977 p_message = vlm_MessageNew( "Incomplete command", psz_command );
981 assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
983 ppsz_command[i_command] = psz_buf;
984 memcpy (psz_buf, psz_command, psz_temp - psz_command);
985 psz_buf[psz_temp - psz_command] = '\0';
987 Unescape (psz_buf, psz_buf);
990 psz_buf += psz_temp - psz_command + 1;
991 psz_command = psz_temp;
993 assert (buf + sizeof (buf) >= psz_buf);
997 * And then Interpret it
1000 #define IF_EXECUTE( name, check, cmd ) if( !strcmp(ppsz_command[0], name ) ) { if( (check) ) goto syntax_error; if( (cmd) ) goto error; goto success; }
1001 if( i_command == 0 )
1003 p_message = vlm_MessageNew( "", NULL );
1006 else IF_EXECUTE( "del", (i_command != 2), ExecuteDel(p_vlm, ppsz_command[1], &p_message) )
1007 else IF_EXECUTE( "show", (i_command > 2), ExecuteShow(p_vlm, i_command > 1 ? ppsz_command[1] : NULL, &p_message) )
1008 else IF_EXECUTE( "help", (i_command != 1), ExecuteHelp(p_vlm, &p_message) )
1009 else IF_EXECUTE( "control", (i_command < 3), ExecuteControl(p_vlm, ppsz_command[1], i_command - 2, &ppsz_command[2], &p_message) )
1010 else IF_EXECUTE( "save", (i_command != 2), ExecuteSave(p_vlm, ppsz_command[1], &p_message) )
1011 else IF_EXECUTE( "export", (i_command != 1), ExecuteExport(p_vlm, &p_message) )
1012 else IF_EXECUTE( "load", (i_command != 2), ExecuteLoad(p_vlm, ppsz_command[1], &p_message) )
1013 else IF_EXECUTE( "new", (i_command < 3), ExecuteNew(p_vlm, ppsz_command[1], ppsz_command[2], i_command-3, &ppsz_command[3], &p_message) )
1014 else IF_EXECUTE( "setup", (i_command < 2), ExecuteSetup(p_vlm, ppsz_command[1], i_command-2, &ppsz_command[2], &p_message) )
1017 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
1023 *pp_message = p_message;
1027 return ExecuteSyntaxError( ppsz_command[0], pp_message );
1030 *pp_message = p_message;
1031 return VLC_EGENERIC;
1034 /*****************************************************************************
1036 *****************************************************************************/
1037 vlm_media_sys_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
1041 for( i = 0; i < vlm->i_media; i++ )
1043 if( strcmp( psz_name, vlm->media[i]->cfg.psz_name ) == 0 )
1044 return vlm->media[i];
1050 /*****************************************************************************
1052 *****************************************************************************/
1053 static int64_t vlm_Date(void)
1058 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1060 struct timeval tv_date;
1062 /* gettimeofday() cannot fail given &tv_date is a valid address */
1063 (void)gettimeofday( &tv_date, NULL );
1064 return (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec;
1068 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
1070 vlm_schedule_sys_t *p_sched = malloc( sizeof( vlm_schedule_sys_t ) );
1082 p_sched->psz_name = strdup( psz_name );
1083 p_sched->b_enabled = VLC_FALSE;
1084 p_sched->i_command = 0;
1085 p_sched->command = NULL;
1086 p_sched->i_date = 0;
1087 p_sched->i_period = 0;
1088 p_sched->i_repeat = -1;
1090 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1095 /* for now, simple delete. After, del with options (last arg) */
1096 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched,
1097 const char *psz_name )
1099 if( sched == NULL ) return;
1101 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1103 if( vlm->i_schedule == 0 && vlm->schedule ) free( vlm->schedule );
1104 free( sched->psz_name );
1105 while( sched->i_command )
1107 char *psz_cmd = sched->command[0];
1108 TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1114 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1118 for( i = 0; i < vlm->i_schedule; i++ )
1120 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1122 return vlm->schedule[i];
1129 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1130 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
1131 const char *psz_value )
1133 if( !strcmp( psz_cmd, "enabled" ) )
1135 schedule->b_enabled = VLC_TRUE;
1137 else if( !strcmp( psz_cmd, "disabled" ) )
1139 schedule->b_enabled = VLC_FALSE;
1141 #if !defined( UNDER_CE )
1142 else if( !strcmp( psz_cmd, "date" ) )
1148 time.tm_sec = 0; /* seconds */
1149 time.tm_min = 0; /* minutes */
1150 time.tm_hour = 0; /* hours */
1151 time.tm_mday = 0; /* day of the month */
1152 time.tm_mon = 0; /* month */
1153 time.tm_year = 0; /* year */
1154 time.tm_wday = 0; /* day of the week */
1155 time.tm_yday = 0; /* day in the year */
1156 time.tm_isdst = -1; /* daylight saving time */
1158 /* date should be year/month/day-hour:minutes:seconds */
1159 p = strchr( psz_value, '-' );
1161 if( !strcmp( psz_value, "now" ) )
1163 schedule->i_date = 0;
1165 else if( (p == NULL) && sscanf( psz_value, "%d:%d:%d", &time.tm_hour,
1166 &time.tm_min, &time.tm_sec ) != 3 )
1167 /* it must be a hour:minutes:seconds */
1175 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1193 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1199 time.tm_mon = i - 1;
1203 time.tm_year = i - 1900;
1204 time.tm_mon = j - 1;
1211 date = mktime( &time );
1212 schedule->i_date = ((mtime_t) date) * 1000000;
1215 else if( !strcmp( psz_cmd, "period" ) )
1219 const char *psz_time = NULL, *psz_date = NULL;
1223 /* First, if date or period are modified, repeat should be equal to -1 */
1224 schedule->i_repeat = -1;
1226 time.tm_sec = 0; /* seconds */
1227 time.tm_min = 0; /* minutes */
1228 time.tm_hour = 0; /* hours */
1229 time.tm_mday = 0; /* day of the month */
1230 time.tm_mon = 0; /* month */
1231 time.tm_year = 0; /* year */
1232 time.tm_wday = 0; /* day of the week */
1233 time.tm_yday = 0; /* day in the year */
1234 time.tm_isdst = -1; /* daylight saving time */
1236 /* date should be year/month/day-hour:minutes:seconds */
1237 p = strchr( psz_value, '-' );
1240 psz_date = psz_value;
1245 psz_time = psz_value;
1248 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1267 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1286 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1287 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1288 schedule->i_period = ((mtime_t) date) * 1000000;
1290 #endif /* UNDER_CE */
1291 else if( !strcmp( psz_cmd, "repeat" ) )
1295 if( sscanf( psz_value, "%d", &i ) == 1 )
1297 schedule->i_repeat = i;
1304 else if( !strcmp( psz_cmd, "append" ) )
1306 char *command = strdup( psz_value );
1308 TAB_APPEND( schedule->i_command, schedule->command, command );
1317 /*****************************************************************************
1318 * Message handling functions
1319 *****************************************************************************/
1320 vlm_message_t *vlm_MessageNew( const char *psz_name,
1321 const char *psz_format, ... )
1323 vlm_message_t *p_message;
1326 if( !psz_name ) return NULL;
1328 p_message = malloc( sizeof(vlm_message_t) );
1334 p_message->psz_value = 0;
1338 va_start( args, psz_format );
1339 if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1348 p_message->psz_name = strdup( psz_name );
1349 p_message->i_child = 0;
1350 p_message->child = NULL;
1355 void vlm_MessageDelete( vlm_message_t *p_message )
1357 if( p_message->psz_name ) free( p_message->psz_name );
1358 if( p_message->psz_value ) free( p_message->psz_value );
1359 while( p_message->i_child-- )
1360 vlm_MessageDelete( p_message->child[p_message->i_child] );
1361 if( p_message->child ) free( p_message->child );
1366 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1367 vlm_message_t *p_child )
1369 if( p_message == NULL ) return NULL;
1373 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1379 /*****************************************************************************
1380 * Misc utility functions
1381 *****************************************************************************/
1382 static vlm_message_t *vlm_ShowMedia( vlm_t *p_vlm, vlm_media_sys_t *p_media )
1384 vlm_media_t *p_cfg = &p_media->cfg;
1385 vlm_message_t *p_msg;
1386 vlm_message_t *p_msg_sub;
1389 p_msg = vlm_MessageNew( p_cfg->psz_name, 0 );
1390 vlm_MessageAdd( p_msg,
1391 vlm_MessageNew( "type", p_cfg->b_vod ? "vod" : "broadcast" ) );
1392 vlm_MessageAdd( p_msg,
1393 vlm_MessageNew( "enabled", p_cfg->b_enabled ? "yes" : "no" ) );
1396 vlm_MessageAdd( p_msg,
1397 vlm_MessageNew( "mux", p_cfg->vod.psz_mux ) );
1399 vlm_MessageAdd( p_msg,
1400 vlm_MessageNew( "loop", p_cfg->broadcast.b_loop ? "yes" : "no" ) );
1402 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "inputs", NULL ) );
1403 for( i = 0; i < p_cfg->i_input; i++ )
1406 asprintf( &psz_tmp, "%d", i+1 );
1407 vlm_MessageAdd( p_msg_sub,
1408 vlm_MessageNew( psz_tmp, p_cfg->ppsz_input[i] ) );
1412 vlm_MessageAdd( p_msg,
1413 vlm_MessageNew( "output", p_cfg->psz_output ? p_cfg->psz_output : "" ) );
1415 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "options", 0 ) );
1416 for( i = 0; i < p_cfg->i_option; i++ )
1417 vlm_MessageAdd( p_msg_sub, vlm_MessageNew( p_cfg->ppsz_option[i], 0 ) );
1419 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "instances", NULL ) );
1420 for( i = 0; i < p_media->i_instance; i++ )
1422 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1424 vlm_message_t *p_msg_instance;
1428 if( p_instance->p_input )
1429 var_Get( p_instance->p_input, "state", &val );
1431 p_msg_instance = vlm_MessageAdd( p_msg_sub, vlm_MessageNew( "instance" , NULL ) );
1433 vlm_MessageAdd( p_msg_instance,
1434 vlm_MessageNew( "name" , p_instance->psz_name ? p_instance->psz_name : "default" ) );
1435 vlm_MessageAdd( p_msg_instance,
1436 vlm_MessageNew( "state",
1437 val.i_int == PLAYING_S ? "playing" :
1438 val.i_int == PAUSE_S ? "paused" :
1441 /* FIXME should not do that this way */
1442 if( p_instance->p_input )
1444 #define APPEND_INPUT_INFO( a, format, type ) \
1445 asprintf( &psz_tmp, format, \
1446 var_Get ## type( p_instance->p_input, a ) ); \
1447 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( a, psz_tmp ) ); \
1449 APPEND_INPUT_INFO( "position", "%f", Float );
1450 APPEND_INPUT_INFO( "time", I64Fi, Time );
1451 APPEND_INPUT_INFO( "length", I64Fi, Time );
1452 APPEND_INPUT_INFO( "rate", "%d", Integer );
1453 APPEND_INPUT_INFO( "title", "%d", Integer );
1454 APPEND_INPUT_INFO( "chapter", "%d", Integer );
1455 APPEND_INPUT_INFO( "seekable", "%d", Bool );
1457 #undef APPEND_INPUT_INFO
1458 asprintf( &psz_tmp, "%d", p_instance->i_index + 1 );
1459 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( "playlistindex", psz_tmp ) );
1465 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
1466 vlm_schedule_sys_t *schedule,
1467 const char *psz_filter )
1471 vlm_message_t *p_msg = vlm_MessageNew( "show", NULL );
1473 vlm_MessageAdd( p_msg, vlm_ShowMedia( vlm, media ) );
1477 else if( schedule != NULL )
1481 vlm_message_t *msg_schedule;
1482 vlm_message_t *msg_child;
1485 msg = vlm_MessageNew( "show", NULL );
1487 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, 0 ) );
1489 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1491 vlm_MessageAdd( msg_schedule,
1492 vlm_MessageNew( "enabled", schedule->b_enabled ?
1495 #if !defined( UNDER_CE )
1496 if( schedule->i_date != 0 )
1499 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1502 #ifdef HAVE_LOCALTIME_R
1503 localtime_r( &i_time, &date);
1505 struct tm *p_date = localtime( &i_time );
1509 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1510 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1511 date.tm_hour, date.tm_min, date.tm_sec );
1513 vlm_MessageAdd( msg_schedule,
1514 vlm_MessageNew( "date", psz_date ) );
1519 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1522 if( schedule->i_period != 0 )
1524 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1527 date.tm_sec = (int)( i_time % 60 );
1528 i_time = i_time / 60;
1529 date.tm_min = (int)( i_time % 60 );
1530 i_time = i_time / 60;
1531 date.tm_hour = (int)( i_time % 24 );
1532 i_time = i_time / 24;
1533 date.tm_mday = (int)( i_time % 30 );
1534 i_time = i_time / 30;
1535 /* okay, okay, months are not always 30 days long */
1536 date.tm_mon = (int)( i_time % 12 );
1537 i_time = i_time / 12;
1538 date.tm_year = (int)i_time;
1540 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1541 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1543 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1547 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1549 #endif /* UNDER_CE */
1551 sprintf( buffer, "%d", schedule->i_repeat );
1552 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1555 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", 0) );
1557 for( i = 0; i < schedule->i_command; i++ )
1559 vlm_MessageAdd( msg_child,
1560 vlm_MessageNew( schedule->command[i], NULL ) );
1567 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1569 vlm_message_t *p_msg;
1570 vlm_message_t *p_msg_child;
1571 int i_vod = 0, i_broadcast = 0;
1575 for( i = 0; i < vlm->i_media; i++ )
1577 if( vlm->media[i]->cfg.b_vod )
1583 asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1585 p_msg = vlm_MessageNew( "show", NULL );
1586 p_msg_child = vlm_MessageAdd( p_msg, vlm_MessageNew( "media", psz_count ) );
1589 for( i = 0; i < vlm->i_media; i++ )
1590 vlm_MessageAdd( p_msg_child, vlm_ShowMedia( vlm, vlm->media[i] ) );
1595 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1599 vlm_message_t *msg_child;
1601 msg = vlm_MessageNew( "show", NULL );
1602 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", NULL ) );
1604 for( i = 0; i < vlm->i_schedule; i++ )
1606 vlm_schedule_sys_t *s = vlm->schedule[i];
1607 vlm_message_t *msg_schedule;
1608 mtime_t i_time, i_next_date;
1610 msg_schedule = vlm_MessageAdd( msg_child,
1611 vlm_MessageNew( s->psz_name, 0 ) );
1612 vlm_MessageAdd( msg_schedule,
1613 vlm_MessageNew( "enabled", s->b_enabled ?
1616 /* calculate next date */
1617 i_time = vlm_Date();
1618 i_next_date = s->i_date;
1620 if( s->i_period != 0 )
1623 while( s->i_date + j * s->i_period <= i_time &&
1629 i_next_date = s->i_date + j * s->i_period;
1632 if( i_next_date > i_time )
1634 time_t i_date = (time_t) (i_next_date / 1000000) ;
1636 #if !defined( UNDER_CE )
1639 ctime_r( &i_date, psz_date );
1641 char *psz_date = ctime( &i_date );
1644 vlm_MessageAdd( msg_schedule,
1645 vlm_MessageNew( "next launch", psz_date ) );
1653 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
1655 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1656 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1658 vlm_MessageAdd( show1, show2->child[0] );
1660 /* We must destroy the parent node "show" of show2
1661 * and not the children */
1662 free( show2->psz_name );
1670 return vlm_MessageNew( "show", NULL );
1674 static vlm_message_t *vlm_Help( vlm_t *vlm, char *psz_filter )
1676 vlm_message_t *message, *message_child;
1678 #define MessageAdd( a ) \
1679 vlm_MessageAdd( message, vlm_MessageNew( a, NULL ) );
1680 #define MessageAddChild( a ) \
1681 vlm_MessageAdd( message_child, vlm_MessageNew( a, NULL ) );
1683 if( psz_filter == NULL )
1685 message = vlm_MessageNew( "help", NULL );
1687 message_child = MessageAdd( "Commands Syntax:" );
1688 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
1689 MessageAddChild( "setup (name) (properties)" );
1690 MessageAddChild( "show [(name)|media|schedule]" );
1691 MessageAddChild( "del (name)|all|media|schedule" );
1692 MessageAddChild( "control (name) [instance_name] (command)" );
1693 MessageAddChild( "save (config_file)" );
1694 MessageAddChild( "export" );
1695 MessageAddChild( "load (config_file)" );
1697 message_child = MessageAdd( "Media Proprieties Syntax:" );
1698 MessageAddChild( "input (input_name)" );
1699 MessageAddChild( "inputdel (input_name)|all" );
1700 MessageAddChild( "inputdeln input_number" );
1701 MessageAddChild( "output (output_name)" );
1702 MessageAddChild( "option (option_name)[=value]" );
1703 MessageAddChild( "enabled|disabled" );
1704 MessageAddChild( "loop|unloop (broadcast only)" );
1705 MessageAddChild( "mux (mux_name)" );
1707 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
1708 MessageAddChild( "enabled|disabled" );
1709 MessageAddChild( "append (command_until_rest_of_the_line)" );
1710 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
1712 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
1713 "(days)-(hours):(minutes):(seconds)" );
1714 MessageAddChild( "repeat (number_of_repetitions)" );
1716 message_child = MessageAdd( "Control Commands Syntax:" );
1717 MessageAddChild( "play [input_number]" );
1718 MessageAddChild( "pause" );
1719 MessageAddChild( "stop" );
1720 MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](miliseconds)ms" );
1725 return vlm_MessageNew( "help", NULL );
1728 /*****************************************************************************
1729 * Config handling functions
1730 *****************************************************************************/
1731 static int Load( vlm_t *vlm, char *file )
1736 while( *pf != '\0' )
1738 vlm_message_t *message = NULL;
1741 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1746 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1750 if( pf[i_end] == '\n' ) i_end++;
1753 if( *pf && ExecuteCommand( vlm, pf, &message ) )
1757 if( message->psz_value )
1758 msg_Err( vlm, "Load error on line %d: %s: %s",
1759 i_line, message->psz_name, message->psz_value );
1760 vlm_MessageDelete( message );
1764 if( message ) vlm_MessageDelete( message );
1773 static char *Save( vlm_t *vlm )
1776 char psz_header[] = "\n"
1777 "# VLC media player VLM command batch\n"
1778 "# http://www.videolan.org/vlc/\n\n" ;
1781 int i_length = strlen( psz_header );
1783 for( i = 0; i < vlm->i_media; i++ )
1785 vlm_media_sys_t *media = vlm->media[i];
1786 vlm_media_t *p_cfg = &media->cfg;
1789 i_length += strlen( "new * vod " ) + strlen(p_cfg->psz_name);
1791 i_length += strlen( "new * broadcast " ) + strlen(p_cfg->psz_name);
1793 if( p_cfg->b_enabled == VLC_TRUE )
1794 i_length += strlen( "enabled" );
1796 i_length += strlen( "disabled" );
1798 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop == VLC_TRUE )
1799 i_length += strlen( " loop\n" );
1801 i_length += strlen( "\n" );
1803 for( j = 0; j < p_cfg->i_input; j++ )
1804 i_length += strlen( "setup * input \"\"\n" ) + strlen( p_cfg->psz_name ) + strlen( p_cfg->ppsz_input[j] );
1806 if( p_cfg->psz_output != NULL )
1807 i_length += strlen( "setup * output \n" ) + strlen(p_cfg->psz_name) + strlen(p_cfg->psz_output);
1809 for( j = 0; j < p_cfg->i_option; j++ )
1810 i_length += strlen("setup * option \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->ppsz_option[j]);
1812 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1813 i_length += strlen("setup * mux \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->vod.psz_mux);
1816 for( i = 0; i < vlm->i_schedule; i++ )
1818 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1820 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
1822 if( schedule->b_enabled == VLC_TRUE )
1824 i_length += strlen( "date //-:: enabled\n" ) + 14;
1828 i_length += strlen( "date //-:: disabled\n" ) + 14;
1832 if( schedule->i_period != 0 )
1834 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
1835 strlen( "period //-::\n" ) + 14;
1838 if( schedule->i_repeat >= 0 )
1842 sprintf( buffer, "%d", schedule->i_repeat );
1843 i_length += strlen( "setup repeat \n" ) +
1844 strlen( schedule->psz_name ) + strlen( buffer );
1851 for( j = 0; j < schedule->i_command; j++ )
1853 i_length += strlen( "setup append \n" ) +
1854 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
1859 /* Don't forget the '\0' */
1861 /* now we have the length of save */
1863 p = save = malloc( i_length );
1864 if( !save ) return NULL;
1867 p += sprintf( p, "%s", psz_header );
1869 /* finally we can write in it */
1870 for( i = 0; i < vlm->i_media; i++ )
1872 vlm_media_sys_t *media = vlm->media[i];
1873 vlm_media_t *p_cfg = &media->cfg;
1876 p += sprintf( p, "new %s vod ", p_cfg->psz_name );
1878 p += sprintf( p, "new %s broadcast ", p_cfg->psz_name );
1880 if( p_cfg->b_enabled )
1881 p += sprintf( p, "enabled" );
1883 p += sprintf( p, "disabled" );
1885 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1886 p += sprintf( p, " loop\n" );
1888 p += sprintf( p, "\n" );
1890 for( j = 0; j < p_cfg->i_input; j++ )
1891 p += sprintf( p, "setup %s input \"%s\"\n", p_cfg->psz_name, p_cfg->ppsz_input[j] );
1893 if( p_cfg->psz_output )
1894 p += sprintf( p, "setup %s output %s\n", p_cfg->psz_name, p_cfg->psz_output );
1896 for( j = 0; j < p_cfg->i_option; j++ )
1897 p += sprintf( p, "setup %s option %s\n", p_cfg->psz_name, p_cfg->ppsz_option[j] );
1899 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1900 p += sprintf( p, "setup %s mux %s\n", p_cfg->psz_name, p_cfg->vod.psz_mux );
1903 /* and now, the schedule scripts */
1904 #if !defined( UNDER_CE )
1905 for( i = 0; i < vlm->i_schedule; i++ )
1907 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1909 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
1911 #ifdef HAVE_LOCALTIME_R
1912 localtime_r( &i_time, &date);
1914 struct tm *p_date = localtime( &i_time );
1918 p += sprintf( p, "new %s schedule ", schedule->psz_name);
1920 if( schedule->b_enabled == VLC_TRUE )
1922 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
1923 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1924 date.tm_hour, date.tm_min, date.tm_sec );
1928 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
1929 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1930 date.tm_hour, date.tm_min, date.tm_sec);
1933 if( schedule->i_period != 0 )
1935 p += sprintf( p, "setup %s ", schedule->psz_name );
1937 i_time = (time_t) ( schedule->i_period / 1000000 );
1939 date.tm_sec = (int)( i_time % 60 );
1940 i_time = i_time / 60;
1941 date.tm_min = (int)( i_time % 60 );
1942 i_time = i_time / 60;
1943 date.tm_hour = (int)( i_time % 24 );
1944 i_time = i_time / 24;
1945 date.tm_mday = (int)( i_time % 30 );
1946 i_time = i_time / 30;
1947 /* okay, okay, months are not always 30 days long */
1948 date.tm_mon = (int)( i_time % 12 );
1949 i_time = i_time / 12;
1950 date.tm_year = (int)i_time;
1952 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
1953 date.tm_year, date.tm_mon, date.tm_mday,
1954 date.tm_hour, date.tm_min, date.tm_sec);
1957 if( schedule->i_repeat >= 0 )
1959 p += sprintf( p, "setup %s repeat %d\n",
1960 schedule->psz_name, schedule->i_repeat );
1964 p += sprintf( p, "\n" );
1967 for( j = 0; j < schedule->i_command; j++ )
1969 p += sprintf( p, "setup %s append %s\n",
1970 schedule->psz_name, schedule->command[j] );
1974 #endif /* UNDER_CE */
1979 /*****************************************************************************
1981 *****************************************************************************/
1982 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
1983 const char *psz_id, int i_query, va_list args )
1985 vlm_t *vlm = (vlm_t *)p_private;
1990 if( !p_private || !p_vod_media )
1991 return VLC_EGENERIC;
1993 vlc_mutex_lock( &vlm->lock );
1996 for( i = 0, id = -1; i < vlm->i_media; i++ )
1998 if( p_vod_media == vlm->media[i]->vod.p_media )
2000 id = vlm->media[i]->cfg.id;
2006 vlc_mutex_unlock( &vlm->lock );
2007 return VLC_EGENERIC;
2012 case VOD_MEDIA_PLAY:
2013 psz = (const char *)va_arg( args, const char * );
2014 i_ret = vlm_ControlInternal( vlm, VLM_START_MEDIA_VOD_INSTANCE, id, psz_id, 0, psz );
2017 case VOD_MEDIA_PAUSE:
2018 i_ret = vlm_ControlInternal( vlm, VLM_PAUSE_MEDIA_INSTANCE, id, psz_id );
2021 case VOD_MEDIA_STOP:
2022 i_ret = vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, id, psz_id );
2025 case VOD_MEDIA_SEEK:
2027 double d_position = (double)va_arg( args, double );
2028 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position/100.0 );
2032 case VOD_MEDIA_REWIND:
2034 double d_scale = (double)va_arg( args, double );
2037 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2038 d_position -= (d_scale / 1000.0);
2039 if( d_position < 0.0 )
2041 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2045 case VOD_MEDIA_FORWARD:
2047 double d_scale = (double)va_arg( args, double );
2050 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2051 d_position += (d_scale / 1000.0);
2052 if( d_position > 1.0 )
2054 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2059 i_ret = VLC_EGENERIC;
2063 vlc_mutex_unlock( &vlm->lock );
2069 /*****************************************************************************
2071 *****************************************************************************/
2072 static int Manage( vlc_object_t* p_object )
2074 vlm_t *vlm = (vlm_t*)p_object;
2076 mtime_t i_lastcheck;
2079 i_lastcheck = vlm_Date();
2081 while( !vlm->b_die )
2083 char **ppsz_scheduled_commands = NULL;
2084 int i_scheduled_commands = 0;
2086 vlc_mutex_lock( &vlm->lock );
2088 /* destroy the inputs that wants to die, and launch the next input */
2089 for( i = 0; i < vlm->i_media; i++ )
2091 vlm_media_sys_t *p_media = vlm->media[i];
2093 for( j = 0; j < p_media->i_instance; )
2095 vlm_media_instance_sys_t *p_instance = p_media->instance[j];
2097 if( p_instance->p_input && ( p_instance->p_input->b_eof || p_instance->p_input->b_error ) )
2099 int i_new_input_index;
2102 i_new_input_index = p_instance->i_index + 1;
2103 if( !p_media->cfg.b_vod && p_media->cfg.broadcast.b_loop && i_new_input_index >= p_media->cfg.i_input )
2104 i_new_input_index = 0;
2106 /* FIXME implement multiple input with VOD */
2107 if( p_media->cfg.b_vod || i_new_input_index >= p_media->cfg.i_input )
2108 vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, p_instance->psz_name );
2110 vlm_ControlInternal( vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, p_instance->psz_name, i_new_input_index );
2122 i_time = vlm_Date();
2124 for( i = 0; i < vlm->i_schedule; i++ )
2126 mtime_t i_real_date = vlm->schedule[i]->i_date;
2128 if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2130 if( vlm->schedule[i]->i_date == 0 ) // now !
2132 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2133 i_real_date = i_time;
2135 else if( vlm->schedule[i]->i_period != 0 )
2138 while( vlm->schedule[i]->i_date + j *
2139 vlm->schedule[i]->i_period <= i_lastcheck &&
2140 ( vlm->schedule[i]->i_repeat > j ||
2141 vlm->schedule[i]->i_repeat == -1 ) )
2146 i_real_date = vlm->schedule[i]->i_date + j *
2147 vlm->schedule[i]->i_period;
2150 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2152 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2154 TAB_APPEND( i_scheduled_commands,
2155 ppsz_scheduled_commands,
2156 strdup(vlm->schedule[i]->command[j] ) );
2161 while( i_scheduled_commands )
2163 vlm_message_t *message = NULL;
2164 char *psz_command = ppsz_scheduled_commands[0];
2165 ExecuteCommand( vlm, psz_command,&message );
2167 /* for now, drop the message */
2168 vlm_MessageDelete( message );
2169 TAB_REMOVE( i_scheduled_commands,
2170 ppsz_scheduled_commands,
2172 free( psz_command );
2175 i_lastcheck = i_time;
2177 vlc_mutex_unlock( &vlm->lock );
2192 int i_connection_count;
2193 int i_connection_active;
2198 vlc_bool_t b_playing;
2199 int i_playing_index;
2202 } vlm_media_status_t;
2206 static vlm_media_sys_t *vlm_ControlMediaGetById( vlm_t *p_vlm, int64_t id )
2210 for( i = 0; i < p_vlm->i_media; i++ )
2212 if( p_vlm->media[i]->cfg.id == id )
2213 return p_vlm->media[i];
2217 static vlm_media_sys_t *vlm_ControlMediaGetByName( vlm_t *p_vlm, const char *psz_name )
2221 for( i = 0; i < p_vlm->i_media; i++ )
2223 if( !strcmp( p_vlm->media[i]->cfg.psz_name, psz_name ) )
2224 return p_vlm->media[i];
2228 static int vlm_MediaDescriptionCheck( vlm_t *p_vlm, vlm_media_t *p_cfg )
2232 if( !p_cfg || !p_cfg->psz_name ||
2233 !strcmp( p_cfg->psz_name, "all" ) || !strcmp( p_cfg->psz_name, "media" ) || !strcmp( p_cfg->psz_name, "schedule" ) )
2234 return VLC_EGENERIC;
2236 for( i = 0; i < p_vlm->i_media; i++ )
2238 if( p_vlm->media[i]->cfg.id == p_cfg->id )
2240 if( !strcmp( p_vlm->media[i]->cfg.psz_name, p_cfg->psz_name ) )
2241 return VLC_EGENERIC;
2247 /* Called after a media description is changed/added */
2248 static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
2250 vlm_media_t *p_cfg = &p_media->cfg;
2251 /* Check if we need to create/delete a vod media */
2254 if( !p_cfg->b_enabled && p_media->vod.p_media )
2256 p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
2257 p_media->vod.p_media = NULL;
2259 else if( p_cfg->b_enabled && !p_media->vod.p_media && p_cfg->i_input )
2261 /* Pre-parse the input */
2262 input_thread_t *p_input;
2268 input_ItemClean( &p_media->vod.item );
2269 input_ItemInit( VLC_OBJECT(p_vlm), &p_media->vod.item );
2271 if( p_cfg->psz_output )
2272 asprintf( &psz_output, "%s:description", p_cfg->psz_output );
2274 asprintf( &psz_output, "#description" );
2276 p_media->vod.item.psz_uri = strdup( p_cfg->ppsz_input[0] );
2278 TAB_INIT( p_media->vod.item.i_options, p_media->vod.item.ppsz_options );
2280 asprintf( &psz_dup, "sout=%s", psz_output);
2281 TAB_APPEND( p_media->vod.item.i_options, p_media->vod.item.ppsz_options, psz_dup );
2282 for( i = 0; i < p_cfg->i_option; i++ )
2284 psz_dup = strdup( p_cfg->ppsz_option[i] );
2285 TAB_APPEND( p_media->vod.item.i_options, p_media->vod.item.ppsz_options, psz_dup );
2287 psz_dup = strdup( "no-sout-keep" );
2288 TAB_APPEND( p_media->vod.item.i_options, p_media->vod.item.ppsz_options, psz_dup );
2290 asprintf( &psz_header, _("Media: %s"), p_cfg->psz_name );
2292 if( (p_input = input_CreateThreadExtended( p_vlm, &p_media->vod.item, psz_header, NULL ) ) )
2294 while( !p_input->b_eof && !p_input->b_error )
2297 input_StopThread( p_input );
2298 input_DestroyThreadExtended( p_input, NULL );
2303 if( p_cfg->vod.psz_mux )
2306 es_format_t es, *p_es = &es;
2309 sprintf( fourcc, "%4.4s", p_cfg->vod.psz_mux );
2310 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
2311 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
2313 item = p_media->vod.item;
2316 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
2318 p_media->vod.p_media =
2319 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
2323 p_media->vod.p_media =
2324 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &p_media->vod.item );
2330 /* TODO start media if needed */
2333 /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
2337 static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
2339 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
2342 if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
2343 return VLC_EGENERIC;
2344 if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
2345 return VLC_EGENERIC;
2349 /* TODO check what are the changes being done (stop instance if needed) */
2352 vlm_media_Clean( &p_media->cfg );
2353 vlm_media_Copy( &p_media->cfg, p_cfg );
2355 return vlm_OnMediaUpdate( p_vlm, p_media );
2358 static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id )
2360 vlm_media_sys_t *p_media;
2362 if( vlm_MediaDescriptionCheck( p_vlm, p_cfg ) || vlm_ControlMediaGetByName( p_vlm, p_cfg->psz_name ) )
2364 msg_Err( p_vlm, "invalid media description" );
2365 return VLC_EGENERIC;
2367 /* Check if we need to load the VOD server */
2368 if( p_cfg->b_vod && !p_vlm->i_vod )
2370 p_vlm->p_vod = vlc_object_create( p_vlm, VLC_OBJECT_VOD );
2371 vlc_object_attach( p_vlm->p_vod, p_vlm );
2372 p_vlm->p_vod->p_module = module_Need( p_vlm->p_vod, "vod server", 0, 0 );
2373 if( !p_vlm->p_vod->p_module )
2375 msg_Err( p_vlm, "cannot find vod server" );
2376 vlc_object_detach( p_vlm->p_vod );
2377 vlc_object_destroy( p_vlm->p_vod );
2379 return VLC_EGENERIC;
2382 p_vlm->p_vod->p_data = p_vlm;
2383 p_vlm->p_vod->pf_media_control = vlm_MediaVodControl;
2386 p_media = malloc( sizeof( vlm_media_sys_t ) );
2389 msg_Err( p_vlm, "out of memory" );
2392 memset( p_media, 0, sizeof(vlm_media_sys_t) );
2397 vlm_media_Copy( &p_media->cfg, p_cfg );
2398 p_media->cfg.id = p_vlm->i_id++;
2399 /* FIXME do we do something here if enabled is true ? */
2401 input_ItemInit( VLC_OBJECT(p_vlm), &p_media->vod.item );
2403 p_media->vod.p_media = NULL;
2404 TAB_INIT( p_media->i_instance, p_media->instance );
2407 TAB_APPEND( p_vlm->i_media, p_vlm->media, p_media );
2410 *p_id = p_media->cfg.id;
2412 return vlm_OnMediaUpdate( p_vlm, p_media );
2415 static int vlm_ControlMediaDel( vlm_t *p_vlm, int64_t id )
2417 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2420 return VLC_EGENERIC;
2422 while( p_media->i_instance > 0 )
2423 vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, p_media->instance[0]->psz_name );
2425 if( p_media->cfg.b_vod )
2427 p_media->cfg.b_enabled = VLC_FALSE;
2428 vlm_OnMediaUpdate( p_vlm, p_media );
2432 vlm_media_Clean( &p_media->cfg );
2434 input_ItemClean( &p_media->vod.item );
2436 TAB_REMOVE( p_vlm->i_media, p_vlm->media, p_media );
2440 /* Check if we need to unload the VOD server */
2441 if( p_vlm->p_vod && p_vlm->i_vod <= 0 )
2443 module_Unneed( p_vlm->p_vod, p_vlm->p_vod->p_module );
2444 vlc_object_detach( p_vlm->p_vod );
2445 vlc_object_destroy( p_vlm->p_vod );
2446 p_vlm->p_vod = NULL;
2451 static int vlm_ControlMediaGets( vlm_t *p_vlm, vlm_media_t ***ppp_dsc, int *pi_dsc )
2453 vlm_media_t **pp_dsc;
2457 TAB_INIT( i_dsc, pp_dsc );
2458 for( i = 0; i < p_vlm->i_media; i++ )
2460 vlm_media_t *p_dsc = vlm_media_Duplicate( &p_vlm->media[i]->cfg );
2461 TAB_APPEND( i_dsc, pp_dsc, p_dsc );
2469 static int vlm_ControlMediaClear( vlm_t *p_vlm )
2471 while( p_vlm->i_media > 0 )
2472 vlm_ControlMediaDel( p_vlm, p_vlm->media[0]->cfg.id );
2476 static int vlm_ControlMediaGet( vlm_t *p_vlm, int64_t id, vlm_media_t **pp_dsc )
2478 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2480 return VLC_EGENERIC;
2482 *pp_dsc = vlm_media_Duplicate( &p_media->cfg );
2485 static int vlm_ControlMediaGetId( vlm_t *p_vlm, const char *psz_name, int64_t *p_id )
2487 vlm_media_sys_t *p_media = vlm_ControlMediaGetByName( p_vlm, psz_name );
2489 return VLC_EGENERIC;
2491 *p_id = p_media->cfg.id;
2495 static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
2499 for( i = 0; i < p_media->i_instance; i++ )
2501 const char *psz = p_media->instance[i]->psz_name;
2502 if( ( psz == NULL && psz_id == NULL ) ||
2503 ( psz && psz_id && !strcmp( psz, psz_id ) ) )
2504 return p_media->instance[i];
2508 static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
2510 vlm_media_instance_sys_t *p_instance = malloc( sizeof(vlm_media_instance_sys_t) );
2514 memset( p_instance, 0, sizeof(vlm_media_instance_sys_t) );
2516 p_instance->psz_name = NULL;
2518 p_instance->psz_name = strdup( psz_name );
2520 input_ItemInit( VLC_OBJECT(p_vlm), &p_instance->item );
2522 p_instance->i_index = 0;
2523 p_instance->b_sout_keep = VLC_FALSE;
2524 p_instance->p_input = NULL;
2525 p_instance->p_sout = NULL;
2529 static void vlm_MediaInstanceDelete( vlm_media_instance_sys_t *p_instance )
2531 if( p_instance->p_input )
2533 input_StopThread( p_instance->p_input );
2534 input_DestroyThreadExtended( p_instance->p_input, &p_instance->p_sout );
2536 if( p_instance->p_sout )
2537 sout_DeleteInstance( p_instance->p_sout );
2539 input_ItemClean( &p_instance->item );
2540 if( p_instance->psz_name )
2541 free( p_instance->psz_name );
2546 static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
2548 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2549 vlm_media_instance_sys_t *p_instance;
2552 if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
2553 return VLC_EGENERIC;
2555 /* TODO support multiple input for VOD with sout-keep ? */
2557 if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
2558 return VLC_EGENERIC;
2560 if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
2561 return VLC_EGENERIC;
2563 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2566 vlm_media_t *p_cfg = &p_media->cfg;
2570 p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
2574 TAB_INIT( p_instance->item.i_options, p_instance->item.ppsz_options );
2576 if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
2579 asprintf( &psz_buffer, "sout=%s%s%s",
2580 p_cfg->psz_output ? p_cfg->psz_output : "",
2581 (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
2582 psz_vod_output ? psz_vod_output : "" );
2583 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, psz_buffer );
2586 for( i = 0; i < p_cfg->i_option; i++ )
2588 if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
2589 p_instance->b_sout_keep = VLC_TRUE;
2590 else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
2591 p_instance->b_sout_keep = VLC_FALSE;
2593 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, strdup( p_cfg->ppsz_option[i] ) );
2595 /* We force the right sout-keep value (avoid using the sout-keep from the global configuration)
2596 * FIXME implement input list for VOD (need sout-keep)
2598 if( !p_cfg->b_vod && p_instance->b_sout_keep )
2599 psz_keep = strdup( "sout-keep" );
2601 psz_keep = strdup( "no-sout-keep" );
2602 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, psz_keep );
2604 TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
2607 /* Stop old instance */
2608 if( p_instance->p_input )
2610 if( p_instance->i_index == i_input_index &&
2611 !p_instance->p_input->b_eof && !p_instance->p_input->b_error )
2613 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2614 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2618 input_StopThread( p_instance->p_input );
2619 input_DestroyThreadExtended( p_instance->p_input, &p_instance->p_sout );
2620 if( !p_instance->b_sout_keep && p_instance->p_sout )
2622 sout_DeleteInstance( p_instance->p_sout );
2623 p_instance->p_sout = NULL;
2628 p_instance->i_index = i_input_index;
2629 input_item_SetURI( &p_instance->item, p_media->cfg.ppsz_input[p_instance->i_index] ) ;
2631 asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name );
2632 p_instance->p_input = input_CreateThreadExtended( p_vlm, &p_instance->item, psz_log, p_instance->p_sout );
2633 if( !p_instance->p_input )
2635 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2636 vlm_MediaInstanceDelete( p_instance );
2643 static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
2645 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2646 vlm_media_instance_sys_t *p_instance;
2649 return VLC_EGENERIC;
2651 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2653 return VLC_EGENERIC;
2655 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2657 vlm_MediaInstanceDelete( p_instance );
2661 static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
2663 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2664 vlm_media_instance_sys_t *p_instance;
2668 return VLC_EGENERIC;
2670 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2671 if( !p_instance || !p_instance->p_input )
2672 return VLC_EGENERIC;
2674 /* Toggle pause state */
2675 i_state = var_GetInteger( p_instance->p_input, "state" );
2676 if( i_state == PAUSE_S )
2677 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2678 else if( i_state == PLAYING_S )
2679 var_SetInteger( p_instance->p_input, "state", PAUSE_S );
2682 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
2684 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2685 vlm_media_instance_sys_t *p_instance;
2688 return VLC_EGENERIC;
2690 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2691 if( !p_instance || !p_instance->p_input )
2692 return VLC_EGENERIC;
2695 *pi_time = var_GetTime( p_instance->p_input, "time" );
2697 *pd_position = var_GetFloat( p_instance->p_input, "position" );
2700 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t i_time, double d_position )
2702 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2703 vlm_media_instance_sys_t *p_instance;
2706 return VLC_EGENERIC;
2708 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2709 if( !p_instance || !p_instance->p_input )
2710 return VLC_EGENERIC;
2713 return var_SetTime( p_instance->p_input, "time", i_time );
2714 else if( d_position >= 0 && d_position <= 100 )
2715 return var_SetFloat( p_instance->p_input, "position", d_position );
2716 return VLC_EGENERIC;
2719 static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_instance_t ***ppp_idsc, int *pi_instance )
2721 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2722 vlm_media_instance_t **pp_idsc;
2727 return VLC_EGENERIC;
2729 TAB_INIT( i_idsc, pp_idsc );
2730 for( i = 0; i < p_media->i_instance; i++ )
2732 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
2733 vlm_media_instance_t *p_idsc = vlm_media_instance_New();
2735 if( p_instance->psz_name )
2736 p_idsc->psz_name = strdup( p_instance->psz_name );
2737 if( p_instance->p_input )
2739 p_idsc->i_time = var_GetTime( p_instance->p_input, "time" );
2740 p_idsc->i_length = var_GetTime( p_instance->p_input, "length" );
2741 p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
2742 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2743 p_idsc->b_paused = VLC_TRUE;
2744 p_idsc->i_rate = var_GetInteger( p_instance->p_input, "rate" );
2747 TAB_APPEND( i_idsc, pp_idsc, p_idsc );
2749 *ppp_idsc = pp_idsc;
2750 *pi_instance = i_idsc;
2753 static int vlm_ControlMediaInstanceClear( vlm_t *p_vlm, int64_t id )
2755 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2758 return VLC_EGENERIC;
2760 while( p_media->i_instance > 0 )
2761 vlm_ControlMediaInstanceStop( p_vlm, id, p_media->instance[0]->psz_name );
2766 static int vlm_ControlScheduleClear( vlm_t *p_vlm )
2768 while( p_vlm->i_schedule > 0 )
2769 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
2773 static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
2776 vlm_media_t **pp_dsc;
2777 vlm_media_t ***ppp_dsc;
2778 vlm_media_instance_t ***ppp_idsc;
2780 const char *psz_vod;
2794 case VLM_GET_MEDIAS:
2795 ppp_dsc = (vlm_media_t ***)va_arg( args, vlm_media_t *** );
2796 pi_int = (int *)va_arg( args, int * );
2797 return vlm_ControlMediaGets( p_vlm, ppp_dsc, pi_int );
2799 case VLM_CLEAR_MEDIAS:
2800 return vlm_ControlMediaClear( p_vlm );
2802 case VLM_CHANGE_MEDIA:
2803 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2804 return vlm_ControlMediaChange( p_vlm, p_dsc );
2807 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2808 p_id = (int64_t*)va_arg( args, int64_t * );
2809 return vlm_ControlMediaAdd( p_vlm, p_dsc, p_id );
2812 id = (int64_t)va_arg( args, int64_t );
2813 return vlm_ControlMediaDel( p_vlm, id );
2816 id = (int64_t)va_arg( args, int64_t );
2817 pp_dsc = (vlm_media_t **)va_arg( args, vlm_media_t ** );
2818 return vlm_ControlMediaGet( p_vlm, id, pp_dsc );
2820 case VLM_GET_MEDIA_ID:
2821 psz_id = (const char*)va_arg( args, const char * );
2822 p_id = (int64_t*)va_arg( args, int64_t * );
2823 return vlm_ControlMediaGetId( p_vlm, psz_id, p_id );
2826 /* Media instance control */
2827 case VLM_GET_MEDIA_INSTANCES:
2828 id = (int64_t)va_arg( args, int64_t );
2829 ppp_idsc = (vlm_media_instance_t ***)va_arg( args, vlm_media_instance_t *** );
2830 pi_int = (int *)va_arg( args, int *);
2831 return vlm_ControlMediaInstanceGets( p_vlm, id, ppp_idsc, pi_int );
2833 case VLM_CLEAR_MEDIA_INSTANCES:
2834 id = (int64_t)va_arg( args, int64_t );
2835 return vlm_ControlMediaInstanceClear( p_vlm, id );
2838 case VLM_START_MEDIA_BROADCAST_INSTANCE:
2839 id = (int64_t)va_arg( args, int64_t );
2840 psz_id = (const char*)va_arg( args, const char* );
2841 i_int = (int)va_arg( args, int );
2842 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, NULL );
2844 case VLM_START_MEDIA_VOD_INSTANCE:
2845 id = (int64_t)va_arg( args, int64_t );
2846 psz_id = (const char*)va_arg( args, const char* );
2847 i_int = (int)va_arg( args, int );
2848 psz_vod = (const char*)va_arg( args, const char* );
2850 return VLC_EGENERIC;
2851 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, psz_vod );
2853 case VLM_STOP_MEDIA_INSTANCE:
2854 id = (int64_t)va_arg( args, int64_t );
2855 psz_id = (const char*)va_arg( args, const char* );
2856 return vlm_ControlMediaInstanceStop( p_vlm, id, psz_id );
2858 case VLM_PAUSE_MEDIA_INSTANCE:
2859 id = (int64_t)va_arg( args, int64_t );
2860 psz_id = (const char*)va_arg( args, const char* );
2861 return vlm_ControlMediaInstancePause( p_vlm, id, psz_id );
2863 case VLM_GET_MEDIA_INSTANCE_TIME:
2864 id = (int64_t)va_arg( args, int64_t );
2865 psz_id = (const char*)va_arg( args, const char* );
2866 pi_i64 = (int64_t*)va_arg( args, int64_t * );
2867 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, pi_i64, NULL );
2868 case VLM_GET_MEDIA_INSTANCE_POSITION:
2869 id = (int64_t)va_arg( args, int64_t );
2870 psz_id = (const char*)va_arg( args, const char* );
2871 pd_double = (double*)va_arg( args, double* );
2872 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, NULL, pd_double );
2874 case VLM_SET_MEDIA_INSTANCE_TIME:
2875 id = (int64_t)va_arg( args, int64_t );
2876 psz_id = (const char*)va_arg( args, const char* );
2877 i_i64 = (int64_t)va_arg( args, int64_t);
2878 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, i_i64, -1 );
2879 case VLM_SET_MEDIA_INSTANCE_POSITION:
2880 id = (int64_t)va_arg( args, int64_t );
2881 psz_id = (const char*)va_arg( args, const char* );
2882 d_double = (double)va_arg( args, double );
2883 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, -1, d_double );
2885 case VLM_CLEAR_SCHEDULES:
2886 return vlm_ControlScheduleClear( p_vlm );
2889 msg_Err( p_vlm, "unknown VLM query" );
2890 return VLC_EGENERIC;
2893 static int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... )
2898 va_start( args, i_query );
2899 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2905 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2910 va_start( args, i_query );
2912 vlc_mutex_lock( &p_vlm->lock );
2913 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2914 vlc_mutex_unlock( &p_vlm->lock );
2921 #else /* ENABLE_VLM */
2923 /* We just define an empty wrapper */
2924 vlm_t *__vlm_New( vlc_object_t *a )
2926 msg_Err( a, "VideoLAN manager support is disabled" );
2929 void vlm_Delete( vlm_t *a )
2933 int vlm_ExecuteCommand( vlm_t *a, char *b, vlm_message_t **c )
2937 vlm_message_t *vlm_MessageNew( const char *psz_name,
2938 const char *psz_format, ... )
2942 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
2943 vlm_message_t *p_child )
2947 void vlm_MessageDelete( vlm_message_t *a )
2952 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2954 return VLC_EGENERIC;
2957 #endif /* ENABLE_VLM */