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() */
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"
61 /*****************************************************************************
63 *****************************************************************************/
65 /* ugly kludge to avoid "null format string" warnings,
66 * even if we handle NULL format string in vlm_MessageNew() */
67 static const char *vlm_NULL = NULL;
69 static void vlm_Destructor( vlm_t *p_vlm );
72 static int vlm_ControlInternal( vlm_t *, int, ... );
75 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_sys_t *, vlm_schedule_sys_t *, const char * );
77 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *, const char * );
79 static char *Save( vlm_t * );
80 static int Load( vlm_t *, char * );
82 static int ExecuteCommand( vlm_t *, const char *, vlm_message_t ** );
84 static int Manage( vlc_object_t * );
86 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name );
87 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched );
88 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
89 const char *psz_value );
91 static int vlm_MediaVodControl( void *, vod_media_t *, const char *, int, va_list );
94 static vlm_media_sys_t *vlm_MediaSearch( vlm_t *, const char *);
96 /*****************************************************************************
98 *****************************************************************************/
99 vlm_t *__vlm_New ( vlc_object_t *p_this )
105 /* Avoid multiple creation */
106 if( var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX ) ||
107 var_Get( p_this->p_libvlc, "vlm_mutex", &lockval ) )
110 vlc_mutex_lock( lockval.p_address );
112 p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE );
115 vlc_object_yield( p_vlm );
116 vlc_mutex_unlock( lockval.p_address );
120 msg_Dbg( p_this, "creating VLM" );
122 p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM );
125 vlc_mutex_unlock( lockval.p_address );
129 vlc_mutex_init( p_this->p_libvlc, &p_vlm->lock );
131 TAB_INIT( p_vlm->i_media, p_vlm->media );
132 TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
135 vlc_object_attach( p_vlm, p_this->p_libvlc );
137 if( vlc_thread_create( p_vlm, "vlm thread",
138 Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
140 vlc_mutex_destroy( &p_vlm->lock );
141 vlc_object_release( p_vlm );
145 /* Load our configuration file */
146 psz_vlmconf = var_CreateGetString( p_vlm, "vlm-conf" );
147 if( psz_vlmconf && *psz_vlmconf )
149 vlm_message_t *p_message = NULL;
150 char *psz_buffer = NULL;
152 msg_Dbg( p_this, "loading VLM configuration" );
153 asprintf(&psz_buffer, "load %s", psz_vlmconf );
156 msg_Dbg( p_this, psz_buffer );
157 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
158 msg_Warn( p_this, "error while loading the configuration file" );
160 vlm_MessageDelete(p_message);
166 vlc_object_set_destructor( p_vlm, (vlc_destructor_t)vlm_Destructor );
167 vlc_mutex_unlock( lockval.p_address );
172 /*****************************************************************************
174 *****************************************************************************/
175 void vlm_Delete( vlm_t *p_vlm )
177 vlc_object_release( p_vlm );
180 /*****************************************************************************
182 *****************************************************************************/
183 static void vlm_Destructor( vlm_t *p_vlm )
185 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
186 TAB_CLEAN( p_vlm->i_media, p_vlm->media );
188 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
189 TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
191 vlc_mutex_destroy( &p_vlm->lock );
194 /*****************************************************************************
195 * vlm_ExecuteCommand:
196 *****************************************************************************/
197 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
198 vlm_message_t **pp_message)
202 vlc_mutex_lock( &p_vlm->lock );
203 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
204 vlc_mutex_unlock( &p_vlm->lock );
210 static const char quotes[] = "\"'";
212 * FindCommandEnd: look for the end of a possibly quoted string
213 * @return NULL on mal-formatted string,
214 * pointer past the last character otherwise.
216 static const char *FindCommandEnd( const char *psz_sent )
220 while( (c = *psz_sent) != '\0' )
224 if( strchr(quotes,c) ) // opening quote
226 else if( isspace(c) ) // non-escaped space
230 psz_sent++; // skip escaped character
231 if( *psz_sent == '\0' )
237 if( c == quote ) // non-escaped matching quote
239 else if( (quote == '"') && (c == '\\') )
241 psz_sent++; // skip escaped character
242 if (*psz_sent == '\0')
243 return NULL; // error, closing quote missing
249 // error (NULL) if we could not find a matching quote
250 return quote ? NULL : psz_sent;
255 * Unescape a nul-terminated string.
256 * Note that in and out can be identical.
258 * @param out output buffer (at least <strlen (in) + 1> characters long)
259 * @param in nul-terminated string to be unescaped
261 * @return 0 on success, -1 on error.
263 static int Unescape( char *out, const char *in )
267 while( (c = *in++) != '\0' )
271 if (strchr(quotes,c)) // opening quote
295 /* None of the special cases - copy the backslash */
301 if( c == quote ) // non-escaped matching quote
306 if( (quote == '"') && (c == '\\') )
315 case '\0': // should never happen
319 /* None of the special cases - copy the backslash */
331 /*****************************************************************************
332 * ExecuteCommand: The main state machine
333 *****************************************************************************
334 * Execute a command which ends with '\0' (string)
335 *****************************************************************************/
336 static int ExecuteSyntaxError( const char *psz_cmd, vlm_message_t **pp_status )
338 *pp_status = vlm_MessageNew( psz_cmd, "Wrong command syntax" );
342 static vlc_bool_t ExecuteIsMedia( vlm_t *p_vlm, const char *psz_name )
346 if( !psz_name || vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
350 static vlc_bool_t ExecuteIsSchedule( vlm_t *p_vlm, const char *psz_name )
352 if( !psz_name || !vlm_ScheduleSearch( p_vlm, psz_name ) )
357 static int ExecuteDel( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
359 vlm_media_sys_t *p_media;
360 vlm_schedule_sys_t *p_schedule;
362 p_media = vlm_MediaSearch( p_vlm, psz_name );
363 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
365 if( p_schedule != NULL )
367 vlm_ScheduleDelete( p_vlm, p_schedule );
369 else if( p_media != NULL )
371 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_media->cfg.id );
373 else if( !strcmp(psz_name, "media") )
375 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
377 else if( !strcmp(psz_name, "schedule") )
379 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
381 else if( !strcmp(psz_name, "all") )
383 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
384 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
388 *pp_status = vlm_MessageNew( "del", "%s: media unknown", psz_name );
392 *pp_status = vlm_MessageNew( "del", vlm_NULL );
396 static int ExecuteShow( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
398 vlm_media_sys_t *p_media;
399 vlm_schedule_sys_t *p_schedule;
403 *pp_status = vlm_Show( p_vlm, NULL, NULL, NULL );
407 p_media = vlm_MediaSearch( p_vlm, psz_name );
408 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
410 if( p_schedule != NULL )
411 *pp_status = vlm_Show( p_vlm, NULL, p_schedule, NULL );
412 else if( p_media != NULL )
413 *pp_status = vlm_Show( p_vlm, p_media, NULL, NULL );
415 *pp_status = vlm_Show( p_vlm, NULL, NULL, psz_name );
420 static int ExecuteHelp( vlm_message_t *p_status )
422 vlm_message_t *message_child;
424 #define MessageAdd( a ) \
425 vlm_MessageAdd( p_status, vlm_MessageNew( a, vlm_NULL ) );
426 #define MessageAddChild( a ) \
427 vlm_MessageAdd( message_child, vlm_MessageNew( a, vlm_NULL ) );
429 p_status = vlm_MessageNew( "help", vlm_NULL );
431 message_child = MessageAdd( "Commands Syntax:" );
432 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
433 MessageAddChild( "setup (name) (properties)" );
434 MessageAddChild( "show [(name)|media|schedule]" );
435 MessageAddChild( "del (name)|all|media|schedule" );
436 MessageAddChild( "control (name) [instance_name] (command)" );
437 MessageAddChild( "save (config_file)" );
438 MessageAddChild( "export" );
439 MessageAddChild( "load (config_file)" );
441 message_child = MessageAdd( "Media Proprieties Syntax:" );
442 MessageAddChild( "input (input_name)" );
443 MessageAddChild( "inputdel (input_name)|all" );
444 MessageAddChild( "inputdeln input_number" );
445 MessageAddChild( "output (output_name)" );
446 MessageAddChild( "option (option_name)[=value]" );
447 MessageAddChild( "enabled|disabled" );
448 MessageAddChild( "loop|unloop (broadcast only)" );
449 MessageAddChild( "mux (mux_name)" );
451 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
452 MessageAddChild( "enabled|disabled" );
453 MessageAddChild( "append (command_until_rest_of_the_line)" );
454 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
456 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
457 "(days)-(hours):(minutes):(seconds)" );
458 MessageAddChild( "repeat (number_of_repetitions)" );
460 message_child = MessageAdd( "Control Commands Syntax:" );
461 MessageAddChild( "play [input_number]" );
462 MessageAddChild( "pause" );
463 MessageAddChild( "stop" );
464 MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](miliseconds)ms" );
469 static int ExecuteControl( vlm_t *p_vlm, const char *psz_name, const int i_arg, char ** ppsz_arg, vlm_message_t **pp_status )
471 vlm_media_sys_t *p_media;
472 const char *psz_control = NULL;
473 const char *psz_instance = NULL;
474 const char *psz_argument = NULL;
478 if( !ExecuteIsMedia( p_vlm, psz_name ) )
480 *pp_status = vlm_MessageNew( "control", "%s: media unknown", psz_name );
486 #define IS(txt) ( !strcmp( ppsz_arg[i_index], (txt) ) )
488 if( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") )
491 psz_instance = ppsz_arg[0];
493 if( i_index >= i_arg || ( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") ) )
494 return ExecuteSyntaxError( "control", pp_status );
497 psz_control = ppsz_arg[i_index];
499 if( i_index+1 < i_arg )
500 psz_argument = ppsz_arg[i_index+1];
502 p_media = vlm_MediaSearch( p_vlm, psz_name );
505 if( !strcmp( psz_control, "play" ) )
507 int i_input_index = 0;
510 if( ( psz_argument && sscanf(psz_argument, "%d", &i) == 1 ) && i > 0 && i-1 < p_media->cfg.i_input )
514 else if( psz_argument )
517 vlm_media_t *p_cfg = &p_media->cfg;
518 for ( j=0; j < p_cfg->i_input; j++)
520 if( !strcmp( p_cfg->ppsz_input[j], psz_argument ) )
528 if( p_media->cfg.b_vod )
529 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
531 i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, psz_instance, i_input_index );
533 else if( !strcmp( psz_control, "seek" ) )
537 vlc_bool_t b_relative;
538 if( psz_argument[0] == '+' || psz_argument[0] == '-' )
539 b_relative = VLC_TRUE;
541 b_relative = VLC_FALSE;
543 if( strstr( psz_argument, "ms" ) || strstr( psz_argument, "s" ) )
548 if( strstr( psz_argument, "ms" ) )
549 i_new_time = 1000 * (int64_t)atoi( psz_argument );
551 i_new_time = 1000000 * (int64_t)atoi( psz_argument );
556 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, &i_time );
557 i_new_time += i_time;
561 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, i_new_time );
566 double d_new_position = i18n_atof( psz_argument ) / 100.0;
570 double d_position = 0.0;
572 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
573 d_new_position += d_position;
575 if( d_new_position < 0.0 )
576 d_new_position = 0.0;
577 else if( d_new_position > 1.0 )
578 d_new_position = 1.0;
579 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_new_position );
584 i_result = VLC_EGENERIC;
587 else if( !strcmp( psz_control, "rewind" ) )
591 const double d_scale = i18n_atof( psz_argument );
594 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
595 d_position -= (d_scale / 1000.0);
596 if( d_position < 0.0 )
598 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
602 i_result = VLC_EGENERIC;
605 else if( !strcmp( psz_control, "forward" ) )
609 const double d_scale = i18n_atof( psz_argument );
612 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
613 d_position += (d_scale / 1000.0);
614 if( d_position > 1.0 )
616 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
621 i_result = VLC_EGENERIC;
624 else if( !strcmp( psz_control, "stop" ) )
626 i_result = vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
628 else if( !strcmp( psz_control, "pause" ) )
630 i_result = vlm_ControlInternal( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
634 i_result = VLC_EGENERIC;
639 *pp_status = vlm_MessageNew( "control", "unknown error" );
642 *pp_status = vlm_MessageNew( "control", vlm_NULL );
646 static int ExecuteExport( vlm_t *p_vlm, vlm_message_t **pp_status )
648 char *psz_export = Save( p_vlm );
650 *pp_status = vlm_MessageNew( "export", psz_export );
655 static int ExecuteSave( vlm_t *p_vlm, const char *psz_file, vlm_message_t **pp_status )
657 FILE *f = utf8_fopen( psz_file, "wt" );
663 psz_save = Save( p_vlm );
664 if( psz_save == NULL )
669 fwrite( psz_save, strlen( psz_save ), 1, f );
673 *pp_status = vlm_MessageNew( "save", vlm_NULL );
677 *pp_status = vlm_MessageNew( "save", "Unable to save to file" );
681 static int ExecuteLoad( vlm_t *p_vlm, const char *psz_url, vlm_message_t **pp_status )
683 stream_t *p_stream = stream_UrlNew( p_vlm, psz_url );
689 *pp_status = vlm_MessageNew( "load", "Unable to load from file" );
694 if( stream_Seek( p_stream, 0 ) != 0 )
696 stream_Delete( p_stream );
698 *pp_status = vlm_MessageNew( "load", "Read file error" );
702 i_size = stream_Size( p_stream );
704 psz_buffer = malloc( i_size + 1 );
707 stream_Delete( p_stream );
709 *pp_status = vlm_MessageNew( "load", "Read file error" );
713 stream_Read( p_stream, psz_buffer, i_size );
714 psz_buffer[i_size] = '\0';
716 stream_Delete( p_stream );
718 if( Load( p_vlm, psz_buffer ) )
722 *pp_status = vlm_MessageNew( "load", "Error while loading file" );
728 *pp_status = vlm_MessageNew( "load", vlm_NULL );
732 static int ExecuteScheduleProperty( vlm_t *p_vlm, vlm_schedule_sys_t *p_schedule, vlc_bool_t b_new,
733 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
735 const char *psz_cmd = b_new ? "new" : "setup";
738 for( i = 0; i < i_property; i++ )
740 if( !strcmp( ppsz_property[i], "enabled" ) ||
741 !strcmp( ppsz_property[i], "disabled" ) )
743 vlm_ScheduleSetup( p_schedule, ppsz_property[i], NULL );
745 else if( !strcmp( ppsz_property[i], "append" ) )
749 /* Beware: everything behind append is considered as
752 if( ++i >= i_property )
755 psz_line = strdup( ppsz_property[i] );
756 for( j = i+1; j < i_property; j++ )
758 psz_line = realloc( psz_line, strlen(psz_line) + strlen(ppsz_property[j]) + 1 + 1 );
759 strcat( psz_line, " " );
760 strcat( psz_line, ppsz_property[j] );
763 vlm_ScheduleSetup( p_schedule, "append", psz_line );
768 if( i + 1 >= i_property )
771 vlm_ScheduleDelete( p_vlm, p_schedule );
772 return ExecuteSyntaxError( psz_cmd, pp_status );
775 vlm_ScheduleSetup( p_schedule, ppsz_property[i], ppsz_property[i+1] );
779 *pp_status = vlm_MessageNew( psz_cmd, vlm_NULL );
783 static int ExecuteMediaProperty( vlm_t *p_vlm, int64_t id, vlc_bool_t b_new,
784 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
786 const char *psz_cmd = b_new ? "new" : "setup";
787 vlm_media_t *p_cfg = NULL;
793 #define ERROR( txt ) do { *pp_status = vlm_MessageNew( psz_cmd, txt); goto error; } while(0)
794 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA, id, &p_cfg ) )
795 ERROR( "unknown media" );
797 #define MISSING(cmd) do { if( !psz_value ) ERROR( "missing argument for " cmd ); } while(0)
798 for( i = 0; i < i_property; i++ )
800 const char *psz_option = ppsz_property[i];
801 const char *psz_value = i+1 < i_property ? ppsz_property[i+1] : NULL;
803 if( !strcmp( psz_option, "enabled" ) )
805 p_cfg->b_enabled = VLC_TRUE;
807 else if( !strcmp( psz_option, "disabled" ) )
809 p_cfg->b_enabled = VLC_FALSE;
811 else if( !strcmp( psz_option, "input" ) )
814 TAB_APPEND( p_cfg->i_input, p_cfg->ppsz_input, strdup(psz_value) );
817 else if( !strcmp( psz_option, "inputdel" ) && psz_value && !strcmp( psz_value, "all" ) )
819 while( p_cfg->i_input > 0 )
820 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[0] );
823 else if( !strcmp( psz_option, "inputdel" ) )
827 MISSING( "inputdel" );
829 for( j = 0; j < p_cfg->i_input; j++ )
831 if( !strcmp( p_cfg->ppsz_input[j], psz_value ) )
833 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[j] );
839 else if( !strcmp( psz_option, "inputdeln" ) )
843 MISSING( "inputdeln" );
845 i_index = atoi( psz_value );
846 if( i_index > 0 && i_index <= p_cfg->i_input )
847 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[i_index-1] );
850 else if( !strcmp( psz_option, "output" ) )
854 free( p_cfg->psz_output );
855 p_cfg->psz_output = *psz_value ? strdup( psz_value ) : NULL;
858 else if( !strcmp( psz_option, "option" ) )
862 TAB_APPEND( p_cfg->i_option, p_cfg->ppsz_option, strdup( psz_value ) );
865 else if( !strcmp( psz_option, "loop" ) )
868 ERROR( "invalid loop option for vod" );
869 p_cfg->broadcast.b_loop = VLC_TRUE;
871 else if( !strcmp( psz_option, "unloop" ) )
874 ERROR( "invalid unloop option for vod" );
875 p_cfg->broadcast.b_loop = VLC_FALSE;
877 else if( !strcmp( psz_option, "mux" ) )
881 ERROR( "invalid mux option for broadcast" );
883 free( p_cfg->vod.psz_mux );
884 p_cfg->vod.psz_mux = *psz_value ? strdup( psz_value ) : NULL;
889 fprintf( stderr, "PROP: name=%s unknown\n", psz_option );
890 ERROR( "Wrong command syntax" );
897 i_result = vlm_ControlInternal( p_vlm, VLM_CHANGE_MEDIA, p_cfg );
898 vlm_media_Delete( p_cfg );
900 *pp_status = vlm_MessageNew( psz_cmd, vlm_NULL );
907 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_cfg->id );
908 vlm_media_Delete( p_cfg );
913 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 )
916 if( !strcmp( psz_name, "all" ) || !strcmp( psz_name, "media" ) || !strcmp( psz_name, "schedule" ) )
918 *pp_status = vlm_MessageNew( "new", "\"all\", \"media\" and \"schedule\" are reserved names" );
921 if( ExecuteIsMedia( p_vlm, psz_name ) || ExecuteIsSchedule( p_vlm, psz_name ) )
923 *pp_status = vlm_MessageNew( "new", "%s: Name already in use", psz_name );
927 if( !strcmp( psz_type, "schedule" ) )
929 vlm_schedule_sys_t *p_schedule = vlm_ScheduleNew( p_vlm, psz_name );
932 *pp_status = vlm_MessageNew( "new", "could not create schedule" );
935 return ExecuteScheduleProperty( p_vlm, p_schedule, VLC_TRUE, i_property, ppsz_property, pp_status );
937 else if( !strcmp( psz_type, "vod" ) || !strcmp( psz_type, "broadcast" ) )
942 vlm_media_Init( &cfg );
943 cfg.psz_name = strdup( psz_name );
944 cfg.b_vod = !strcmp( psz_type, "vod" );
946 if( vlm_ControlInternal( p_vlm, VLM_ADD_MEDIA, &cfg, &id ) )
948 vlm_media_Clean( &cfg );
949 *pp_status = vlm_MessageNew( "new", "could not create media" );
952 vlm_media_Clean( &cfg );
953 return ExecuteMediaProperty( p_vlm, id, VLC_TRUE, i_property, ppsz_property, pp_status );
957 *pp_status = vlm_MessageNew( "new", "%s: Choose between vod, broadcast or schedule", psz_type );
962 static int ExecuteSetup( vlm_t *p_vlm, const char *psz_name, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
964 if( ExecuteIsSchedule( p_vlm, psz_name ) )
966 vlm_schedule_sys_t *p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
967 return ExecuteScheduleProperty( p_vlm, p_schedule, VLC_FALSE, i_property, ppsz_property, pp_status );
969 else if( ExecuteIsMedia( p_vlm, psz_name ) )
972 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
974 return ExecuteMediaProperty( p_vlm, id, VLC_FALSE, i_property, ppsz_property, pp_status );
978 *pp_status = vlm_MessageNew( "setup", "%s unknown", psz_name );
982 static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
983 vlm_message_t **pp_message )
985 size_t i_command = 0;
986 char buf[strlen (psz_command) + 1], *psz_buf = buf;
987 char *ppsz_command[3+sizeof (buf) / 2];
988 vlm_message_t *p_message = NULL;
990 /* First, parse the line and cut it */
991 while( *psz_command != '\0' )
993 const char *psz_temp;
995 if(isspace (*psz_command))
1001 /* support for comments */
1002 if( i_command == 0 && *psz_command == '#')
1004 p_message = vlm_MessageNew( "", vlm_NULL );
1008 psz_temp = FindCommandEnd( psz_command );
1010 if( psz_temp == NULL )
1012 p_message = vlm_MessageNew( "Incomplete command", psz_command );
1016 assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
1018 ppsz_command[i_command] = psz_buf;
1019 memcpy (psz_buf, psz_command, psz_temp - psz_command);
1020 psz_buf[psz_temp - psz_command] = '\0';
1022 Unescape (psz_buf, psz_buf);
1025 psz_buf += psz_temp - psz_command + 1;
1026 psz_command = psz_temp;
1028 assert (buf + sizeof (buf) >= psz_buf);
1032 * And then Interpret it
1035 #define IF_EXECUTE( name, check, cmd ) if( !strcmp(ppsz_command[0], name ) ) { if( (check) ) goto syntax_error; if( (cmd) ) goto error; goto success; }
1036 if( i_command == 0 )
1038 p_message = vlm_MessageNew( "", vlm_NULL );
1041 else IF_EXECUTE( "del", (i_command != 2), ExecuteDel(p_vlm, ppsz_command[1], &p_message) )
1042 else IF_EXECUTE( "show", (i_command > 2), ExecuteShow(p_vlm, i_command > 1 ? ppsz_command[1] : NULL, &p_message) )
1043 else IF_EXECUTE( "help", (i_command != 1), ExecuteHelp( p_message) )
1044 else IF_EXECUTE( "control", (i_command < 3), ExecuteControl(p_vlm, ppsz_command[1], i_command - 2, &ppsz_command[2], &p_message) )
1045 else IF_EXECUTE( "save", (i_command != 2), ExecuteSave(p_vlm, ppsz_command[1], &p_message) )
1046 else IF_EXECUTE( "export", (i_command != 1), ExecuteExport(p_vlm, &p_message) )
1047 else IF_EXECUTE( "load", (i_command != 2), ExecuteLoad(p_vlm, ppsz_command[1], &p_message) )
1048 else IF_EXECUTE( "new", (i_command < 3), ExecuteNew(p_vlm, ppsz_command[1], ppsz_command[2], i_command-3, &ppsz_command[3], &p_message) )
1049 else IF_EXECUTE( "setup", (i_command < 2), ExecuteSetup(p_vlm, ppsz_command[1], i_command-2, &ppsz_command[2], &p_message) )
1052 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
1058 *pp_message = p_message;
1062 return ExecuteSyntaxError( ppsz_command[0], pp_message );
1065 *pp_message = p_message;
1066 return VLC_EGENERIC;
1069 /*****************************************************************************
1071 *****************************************************************************/
1072 vlm_media_sys_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
1076 for( i = 0; i < vlm->i_media; i++ )
1078 if( strcmp( psz_name, vlm->media[i]->cfg.psz_name ) == 0 )
1079 return vlm->media[i];
1085 /*****************************************************************************
1087 *****************************************************************************/
1088 static int64_t vlm_Date(void)
1093 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1095 struct timeval tv_date;
1097 /* gettimeofday() cannot fail given &tv_date is a valid address */
1098 (void)gettimeofday( &tv_date, NULL );
1099 return (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec;
1103 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
1105 vlm_schedule_sys_t *p_sched = malloc( sizeof( vlm_schedule_sys_t ) );
1117 p_sched->psz_name = strdup( psz_name );
1118 p_sched->b_enabled = VLC_FALSE;
1119 p_sched->i_command = 0;
1120 p_sched->command = NULL;
1121 p_sched->i_date = 0;
1122 p_sched->i_period = 0;
1123 p_sched->i_repeat = -1;
1125 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1130 /* for now, simple delete. After, del with options (last arg) */
1131 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched )
1133 if( sched == NULL ) return;
1135 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1137 if( vlm->i_schedule == 0 ) free( vlm->schedule );
1138 free( sched->psz_name );
1139 while( sched->i_command )
1141 char *psz_cmd = sched->command[0];
1142 TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1148 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1152 for( i = 0; i < vlm->i_schedule; i++ )
1154 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1156 return vlm->schedule[i];
1163 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1164 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
1165 const char *psz_value )
1167 if( !strcmp( psz_cmd, "enabled" ) )
1169 schedule->b_enabled = VLC_TRUE;
1171 else if( !strcmp( psz_cmd, "disabled" ) )
1173 schedule->b_enabled = VLC_FALSE;
1175 #if !defined( UNDER_CE )
1176 else if( !strcmp( psz_cmd, "date" ) )
1182 time.tm_sec = 0; /* seconds */
1183 time.tm_min = 0; /* minutes */
1184 time.tm_hour = 0; /* hours */
1185 time.tm_mday = 0; /* day of the month */
1186 time.tm_mon = 0; /* month */
1187 time.tm_year = 0; /* year */
1188 time.tm_wday = 0; /* day of the week */
1189 time.tm_yday = 0; /* day in the year */
1190 time.tm_isdst = -1; /* daylight saving time */
1192 /* date should be year/month/day-hour:minutes:seconds */
1193 p = strchr( psz_value, '-' );
1195 if( !strcmp( psz_value, "now" ) )
1197 schedule->i_date = 0;
1199 else if( (p == NULL) && sscanf( psz_value, "%d:%d:%d", &time.tm_hour,
1200 &time.tm_min, &time.tm_sec ) != 3 )
1201 /* it must be a hour:minutes:seconds */
1209 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1227 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1233 time.tm_mon = i - 1;
1237 time.tm_year = i - 1900;
1238 time.tm_mon = j - 1;
1245 date = mktime( &time );
1246 schedule->i_date = ((mtime_t) date) * 1000000;
1249 else if( !strcmp( psz_cmd, "period" ) )
1253 const char *psz_time = NULL, *psz_date = NULL;
1257 /* First, if date or period are modified, repeat should be equal to -1 */
1258 schedule->i_repeat = -1;
1260 time.tm_sec = 0; /* seconds */
1261 time.tm_min = 0; /* minutes */
1262 time.tm_hour = 0; /* hours */
1263 time.tm_mday = 0; /* day of the month */
1264 time.tm_mon = 0; /* month */
1265 time.tm_year = 0; /* year */
1266 time.tm_wday = 0; /* day of the week */
1267 time.tm_yday = 0; /* day in the year */
1268 time.tm_isdst = -1; /* daylight saving time */
1270 /* date should be year/month/day-hour:minutes:seconds */
1271 p = strchr( psz_value, '-' );
1274 psz_date = psz_value;
1279 psz_time = psz_value;
1282 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1301 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1320 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1321 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1322 schedule->i_period = ((mtime_t) date) * 1000000;
1324 #endif /* UNDER_CE */
1325 else if( !strcmp( psz_cmd, "repeat" ) )
1329 if( sscanf( psz_value, "%d", &i ) == 1 )
1331 schedule->i_repeat = i;
1338 else if( !strcmp( psz_cmd, "append" ) )
1340 char *command = strdup( psz_value );
1342 TAB_APPEND( schedule->i_command, schedule->command, command );
1351 /*****************************************************************************
1352 * Message handling functions
1353 *****************************************************************************/
1354 vlm_message_t *vlm_MessageNew( const char *psz_name,
1355 const char *psz_format, ... )
1357 vlm_message_t *p_message;
1360 if( !psz_name ) return NULL;
1362 p_message = malloc( sizeof(vlm_message_t) );
1368 p_message->psz_value = 0;
1372 va_start( args, psz_format );
1373 if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1382 p_message->psz_name = strdup( psz_name );
1383 p_message->i_child = 0;
1384 p_message->child = NULL;
1389 void vlm_MessageDelete( vlm_message_t *p_message )
1391 free( p_message->psz_name );
1392 free( p_message->psz_value );
1393 while( p_message->i_child-- )
1394 vlm_MessageDelete( p_message->child[p_message->i_child] );
1395 free( p_message->child );
1400 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1401 vlm_message_t *p_child )
1403 if( p_message == NULL ) return NULL;
1407 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1413 /*****************************************************************************
1414 * Misc utility functions
1415 *****************************************************************************/
1416 static vlm_message_t *vlm_ShowMedia( vlm_media_sys_t *p_media )
1418 vlm_media_t *p_cfg = &p_media->cfg;
1419 vlm_message_t *p_msg;
1420 vlm_message_t *p_msg_sub;
1423 p_msg = vlm_MessageNew( p_cfg->psz_name, vlm_NULL );
1424 vlm_MessageAdd( p_msg,
1425 vlm_MessageNew( "type", p_cfg->b_vod ? "vod" : "broadcast" ) );
1426 vlm_MessageAdd( p_msg,
1427 vlm_MessageNew( "enabled", p_cfg->b_enabled ? "yes" : "no" ) );
1430 vlm_MessageAdd( p_msg,
1431 vlm_MessageNew( "mux", p_cfg->vod.psz_mux ) );
1433 vlm_MessageAdd( p_msg,
1434 vlm_MessageNew( "loop", p_cfg->broadcast.b_loop ? "yes" : "no" ) );
1436 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "inputs", vlm_NULL ) );
1437 for( i = 0; i < p_cfg->i_input; i++ )
1440 asprintf( &psz_tmp, "%d", i+1 );
1441 vlm_MessageAdd( p_msg_sub,
1442 vlm_MessageNew( psz_tmp, p_cfg->ppsz_input[i] ) );
1446 vlm_MessageAdd( p_msg,
1447 vlm_MessageNew( "output", p_cfg->psz_output ? p_cfg->psz_output : "" ) );
1449 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "options", vlm_NULL ) );
1450 for( i = 0; i < p_cfg->i_option; i++ )
1451 vlm_MessageAdd( p_msg_sub, vlm_MessageNew( p_cfg->ppsz_option[i], vlm_NULL ) );
1453 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "instances", vlm_NULL ) );
1454 for( i = 0; i < p_media->i_instance; i++ )
1456 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1458 vlm_message_t *p_msg_instance;
1462 if( p_instance->p_input )
1463 var_Get( p_instance->p_input, "state", &val );
1465 p_msg_instance = vlm_MessageAdd( p_msg_sub, vlm_MessageNew( "instance" , vlm_NULL ) );
1467 vlm_MessageAdd( p_msg_instance,
1468 vlm_MessageNew( "name" , p_instance->psz_name ? p_instance->psz_name : "default" ) );
1469 vlm_MessageAdd( p_msg_instance,
1470 vlm_MessageNew( "state",
1471 val.i_int == PLAYING_S ? "playing" :
1472 val.i_int == PAUSE_S ? "paused" :
1475 /* FIXME should not do that this way */
1476 if( p_instance->p_input )
1478 #define APPEND_INPUT_INFO( a, format, type ) \
1479 asprintf( &psz_tmp, format, \
1480 var_Get ## type( p_instance->p_input, a ) ); \
1481 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( a, psz_tmp ) ); \
1483 APPEND_INPUT_INFO( "position", "%f", Float );
1484 APPEND_INPUT_INFO( "time", I64Fi, Time );
1485 APPEND_INPUT_INFO( "length", I64Fi, Time );
1486 APPEND_INPUT_INFO( "rate", "%d", Integer );
1487 APPEND_INPUT_INFO( "title", "%d", Integer );
1488 APPEND_INPUT_INFO( "chapter", "%d", Integer );
1489 APPEND_INPUT_INFO( "seekable", "%d", Bool );
1491 #undef APPEND_INPUT_INFO
1492 asprintf( &psz_tmp, "%d", p_instance->i_index + 1 );
1493 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( "playlistindex", psz_tmp ) );
1499 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
1500 vlm_schedule_sys_t *schedule,
1501 const char *psz_filter )
1505 vlm_message_t *p_msg = vlm_MessageNew( "show", vlm_NULL );
1507 vlm_MessageAdd( p_msg, vlm_ShowMedia( media ) );
1511 else if( schedule != NULL )
1515 vlm_message_t *msg_schedule;
1516 vlm_message_t *msg_child;
1519 msg = vlm_MessageNew( "show", vlm_NULL );
1521 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, vlm_NULL ) );
1523 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1525 vlm_MessageAdd( msg_schedule,
1526 vlm_MessageNew( "enabled", schedule->b_enabled ?
1529 #if !defined( UNDER_CE )
1530 if( schedule->i_date != 0 )
1533 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1536 localtime_r( &i_time, &date);
1537 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1538 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1539 date.tm_hour, date.tm_min, date.tm_sec );
1541 vlm_MessageAdd( msg_schedule,
1542 vlm_MessageNew( "date", psz_date ) );
1547 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1550 if( schedule->i_period != 0 )
1552 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1555 date.tm_sec = (int)( i_time % 60 );
1556 i_time = i_time / 60;
1557 date.tm_min = (int)( i_time % 60 );
1558 i_time = i_time / 60;
1559 date.tm_hour = (int)( i_time % 24 );
1560 i_time = i_time / 24;
1561 date.tm_mday = (int)( i_time % 30 );
1562 i_time = i_time / 30;
1563 /* okay, okay, months are not always 30 days long */
1564 date.tm_mon = (int)( i_time % 12 );
1565 i_time = i_time / 12;
1566 date.tm_year = (int)i_time;
1568 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1569 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1571 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1575 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1577 #endif /* UNDER_CE */
1579 sprintf( buffer, "%d", schedule->i_repeat );
1580 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1583 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", vlm_NULL ) );
1585 for( i = 0; i < schedule->i_command; i++ )
1587 vlm_MessageAdd( msg_child,
1588 vlm_MessageNew( schedule->command[i], vlm_NULL ) );
1595 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1597 vlm_message_t *p_msg;
1598 vlm_message_t *p_msg_child;
1599 int i_vod = 0, i_broadcast = 0;
1603 for( i = 0; i < vlm->i_media; i++ )
1605 if( vlm->media[i]->cfg.b_vod )
1611 asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1613 p_msg = vlm_MessageNew( "show", vlm_NULL );
1614 p_msg_child = vlm_MessageAdd( p_msg, vlm_MessageNew( "media", psz_count ) );
1617 for( i = 0; i < vlm->i_media; i++ )
1618 vlm_MessageAdd( p_msg_child, vlm_ShowMedia( vlm->media[i] ) );
1623 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1627 vlm_message_t *msg_child;
1629 msg = vlm_MessageNew( "show", vlm_NULL );
1630 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", vlm_NULL ) );
1632 for( i = 0; i < vlm->i_schedule; i++ )
1634 vlm_schedule_sys_t *s = vlm->schedule[i];
1635 vlm_message_t *msg_schedule;
1636 mtime_t i_time, i_next_date;
1638 msg_schedule = vlm_MessageAdd( msg_child,
1639 vlm_MessageNew( s->psz_name, vlm_NULL ) );
1640 vlm_MessageAdd( msg_schedule,
1641 vlm_MessageNew( "enabled", s->b_enabled ?
1644 /* calculate next date */
1645 i_time = vlm_Date();
1646 i_next_date = s->i_date;
1648 if( s->i_period != 0 )
1651 while( s->i_date + j * s->i_period <= i_time &&
1657 i_next_date = s->i_date + j * s->i_period;
1660 if( i_next_date > i_time )
1662 time_t i_date = (time_t) (i_next_date / 1000000) ;
1664 #if !defined( UNDER_CE )
1667 ctime_r( &i_date, psz_date );
1669 char *psz_date = ctime( &i_date );
1672 vlm_MessageAdd( msg_schedule,
1673 vlm_MessageNew( "next launch", psz_date ) );
1681 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
1683 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1684 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1686 vlm_MessageAdd( show1, show2->child[0] );
1688 /* We must destroy the parent node "show" of show2
1689 * and not the children */
1690 free( show2->psz_name );
1698 return vlm_MessageNew( "show", vlm_NULL );
1702 /*****************************************************************************
1703 * Config handling functions
1704 *****************************************************************************/
1705 static int Load( vlm_t *vlm, char *file )
1710 while( *pf != '\0' )
1712 vlm_message_t *message = NULL;
1715 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1720 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1724 if( pf[i_end] == '\n' ) i_end++;
1727 if( *pf && ExecuteCommand( vlm, pf, &message ) )
1731 if( message->psz_value )
1732 msg_Err( vlm, "Load error on line %d: %s: %s",
1733 i_line, message->psz_name, message->psz_value );
1734 vlm_MessageDelete( message );
1738 if( message ) vlm_MessageDelete( message );
1747 static char *Save( vlm_t *vlm )
1750 char psz_header[] = "\n"
1751 "# VLC media player VLM command batch\n"
1752 "# http://www.videolan.org/vlc/\n\n" ;
1755 int i_length = strlen( psz_header );
1757 for( i = 0; i < vlm->i_media; i++ )
1759 vlm_media_sys_t *media = vlm->media[i];
1760 vlm_media_t *p_cfg = &media->cfg;
1763 i_length += strlen( "new * vod " ) + strlen(p_cfg->psz_name);
1765 i_length += strlen( "new * broadcast " ) + strlen(p_cfg->psz_name);
1767 if( p_cfg->b_enabled == VLC_TRUE )
1768 i_length += strlen( "enabled" );
1770 i_length += strlen( "disabled" );
1772 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop == VLC_TRUE )
1773 i_length += strlen( " loop\n" );
1775 i_length += strlen( "\n" );
1777 for( j = 0; j < p_cfg->i_input; j++ )
1778 i_length += strlen( "setup * input \"\"\n" ) + strlen( p_cfg->psz_name ) + strlen( p_cfg->ppsz_input[j] );
1780 if( p_cfg->psz_output != NULL )
1781 i_length += strlen( "setup * output \n" ) + strlen(p_cfg->psz_name) + strlen(p_cfg->psz_output);
1783 for( j = 0; j < p_cfg->i_option; j++ )
1784 i_length += strlen("setup * option \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->ppsz_option[j]);
1786 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1787 i_length += strlen("setup * mux \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->vod.psz_mux);
1790 for( i = 0; i < vlm->i_schedule; i++ )
1792 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1794 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
1796 if( schedule->b_enabled == VLC_TRUE )
1798 i_length += strlen( "date //-:: enabled\n" ) + 14;
1802 i_length += strlen( "date //-:: disabled\n" ) + 14;
1806 if( schedule->i_period != 0 )
1808 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
1809 strlen( "period //-::\n" ) + 14;
1812 if( schedule->i_repeat >= 0 )
1816 sprintf( buffer, "%d", schedule->i_repeat );
1817 i_length += strlen( "setup repeat \n" ) +
1818 strlen( schedule->psz_name ) + strlen( buffer );
1825 for( j = 0; j < schedule->i_command; j++ )
1827 i_length += strlen( "setup append \n" ) +
1828 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
1833 /* Don't forget the '\0' */
1835 /* now we have the length of save */
1837 p = save = malloc( i_length );
1838 if( !save ) return NULL;
1841 p += sprintf( p, "%s", psz_header );
1843 /* finally we can write in it */
1844 for( i = 0; i < vlm->i_media; i++ )
1846 vlm_media_sys_t *media = vlm->media[i];
1847 vlm_media_t *p_cfg = &media->cfg;
1850 p += sprintf( p, "new %s vod ", p_cfg->psz_name );
1852 p += sprintf( p, "new %s broadcast ", p_cfg->psz_name );
1854 if( p_cfg->b_enabled )
1855 p += sprintf( p, "enabled" );
1857 p += sprintf( p, "disabled" );
1859 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1860 p += sprintf( p, " loop\n" );
1862 p += sprintf( p, "\n" );
1864 for( j = 0; j < p_cfg->i_input; j++ )
1865 p += sprintf( p, "setup %s input \"%s\"\n", p_cfg->psz_name, p_cfg->ppsz_input[j] );
1867 if( p_cfg->psz_output )
1868 p += sprintf( p, "setup %s output %s\n", p_cfg->psz_name, p_cfg->psz_output );
1870 for( j = 0; j < p_cfg->i_option; j++ )
1871 p += sprintf( p, "setup %s option %s\n", p_cfg->psz_name, p_cfg->ppsz_option[j] );
1873 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1874 p += sprintf( p, "setup %s mux %s\n", p_cfg->psz_name, p_cfg->vod.psz_mux );
1877 /* and now, the schedule scripts */
1878 #if !defined( UNDER_CE )
1879 for( i = 0; i < vlm->i_schedule; i++ )
1881 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1883 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
1885 localtime_r( &i_time, &date);
1886 p += sprintf( p, "new %s schedule ", schedule->psz_name);
1888 if( schedule->b_enabled == VLC_TRUE )
1890 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
1891 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1892 date.tm_hour, date.tm_min, date.tm_sec );
1896 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
1897 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1898 date.tm_hour, date.tm_min, date.tm_sec);
1901 if( schedule->i_period != 0 )
1903 p += sprintf( p, "setup %s ", schedule->psz_name );
1905 i_time = (time_t) ( schedule->i_period / 1000000 );
1907 date.tm_sec = (int)( i_time % 60 );
1908 i_time = i_time / 60;
1909 date.tm_min = (int)( i_time % 60 );
1910 i_time = i_time / 60;
1911 date.tm_hour = (int)( i_time % 24 );
1912 i_time = i_time / 24;
1913 date.tm_mday = (int)( i_time % 30 );
1914 i_time = i_time / 30;
1915 /* okay, okay, months are not always 30 days long */
1916 date.tm_mon = (int)( i_time % 12 );
1917 i_time = i_time / 12;
1918 date.tm_year = (int)i_time;
1920 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
1921 date.tm_year, date.tm_mon, date.tm_mday,
1922 date.tm_hour, date.tm_min, date.tm_sec);
1925 if( schedule->i_repeat >= 0 )
1927 p += sprintf( p, "setup %s repeat %d\n",
1928 schedule->psz_name, schedule->i_repeat );
1932 p += sprintf( p, "\n" );
1935 for( j = 0; j < schedule->i_command; j++ )
1937 p += sprintf( p, "setup %s append %s\n",
1938 schedule->psz_name, schedule->command[j] );
1942 #endif /* UNDER_CE */
1947 /*****************************************************************************
1949 *****************************************************************************/
1950 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
1951 const char *psz_id, int i_query, va_list args )
1953 vlm_t *vlm = (vlm_t *)p_private;
1958 if( !p_private || !p_vod_media )
1959 return VLC_EGENERIC;
1961 vlc_mutex_lock( &vlm->lock );
1964 for( i = 0, id = -1; i < vlm->i_media; i++ )
1966 if( p_vod_media == vlm->media[i]->vod.p_media )
1968 id = vlm->media[i]->cfg.id;
1974 vlc_mutex_unlock( &vlm->lock );
1975 return VLC_EGENERIC;
1980 case VOD_MEDIA_PLAY:
1981 psz = (const char *)va_arg( args, const char * );
1982 i_ret = vlm_ControlInternal( vlm, VLM_START_MEDIA_VOD_INSTANCE, id, psz_id, 0, psz );
1985 case VOD_MEDIA_PAUSE:
1986 i_ret = vlm_ControlInternal( vlm, VLM_PAUSE_MEDIA_INSTANCE, id, psz_id );
1989 case VOD_MEDIA_STOP:
1990 i_ret = vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, id, psz_id );
1993 case VOD_MEDIA_SEEK:
1995 double d_position = (double)va_arg( args, double );
1996 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position/100.0 );
2000 case VOD_MEDIA_REWIND:
2002 double d_scale = (double)va_arg( args, double );
2005 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2006 d_position -= (d_scale / 1000.0);
2007 if( d_position < 0.0 )
2009 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2013 case VOD_MEDIA_FORWARD:
2015 double d_scale = (double)va_arg( args, double );
2018 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2019 d_position += (d_scale / 1000.0);
2020 if( d_position > 1.0 )
2022 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2027 i_ret = VLC_EGENERIC;
2031 vlc_mutex_unlock( &vlm->lock );
2037 /*****************************************************************************
2039 *****************************************************************************/
2040 static int Manage( vlc_object_t* p_object )
2042 vlm_t *vlm = (vlm_t*)p_object;
2044 mtime_t i_lastcheck;
2047 i_lastcheck = vlm_Date();
2049 while( !vlm->b_die )
2051 char **ppsz_scheduled_commands = NULL;
2052 int i_scheduled_commands = 0;
2054 vlc_mutex_lock( &vlm->lock );
2056 /* destroy the inputs that wants to die, and launch the next input */
2057 for( i = 0; i < vlm->i_media; i++ )
2059 vlm_media_sys_t *p_media = vlm->media[i];
2061 for( j = 0; j < p_media->i_instance; )
2063 vlm_media_instance_sys_t *p_instance = p_media->instance[j];
2065 if( p_instance->p_input && ( p_instance->p_input->b_eof || p_instance->p_input->b_error ) )
2067 int i_new_input_index;
2070 i_new_input_index = p_instance->i_index + 1;
2071 if( !p_media->cfg.b_vod && p_media->cfg.broadcast.b_loop && i_new_input_index >= p_media->cfg.i_input )
2072 i_new_input_index = 0;
2074 /* FIXME implement multiple input with VOD */
2075 if( p_media->cfg.b_vod || i_new_input_index >= p_media->cfg.i_input )
2076 vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, p_instance->psz_name );
2078 vlm_ControlInternal( vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, p_instance->psz_name, i_new_input_index );
2090 i_time = vlm_Date();
2092 for( i = 0; i < vlm->i_schedule; i++ )
2094 mtime_t i_real_date = vlm->schedule[i]->i_date;
2096 if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2098 if( vlm->schedule[i]->i_date == 0 ) // now !
2100 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2101 i_real_date = i_time;
2103 else if( vlm->schedule[i]->i_period != 0 )
2106 while( vlm->schedule[i]->i_date + j *
2107 vlm->schedule[i]->i_period <= i_lastcheck &&
2108 ( vlm->schedule[i]->i_repeat > j ||
2109 vlm->schedule[i]->i_repeat == -1 ) )
2114 i_real_date = vlm->schedule[i]->i_date + j *
2115 vlm->schedule[i]->i_period;
2118 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2120 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2122 TAB_APPEND( i_scheduled_commands,
2123 ppsz_scheduled_commands,
2124 strdup(vlm->schedule[i]->command[j] ) );
2129 while( i_scheduled_commands )
2131 vlm_message_t *message = NULL;
2132 char *psz_command = ppsz_scheduled_commands[0];
2133 ExecuteCommand( vlm, psz_command,&message );
2135 /* for now, drop the message */
2136 vlm_MessageDelete( message );
2137 TAB_REMOVE( i_scheduled_commands,
2138 ppsz_scheduled_commands,
2140 free( psz_command );
2143 i_lastcheck = i_time;
2145 vlc_mutex_unlock( &vlm->lock );
2160 int i_connection_count;
2161 int i_connection_active;
2166 vlc_bool_t b_playing;
2167 int i_playing_index;
2170 } vlm_media_status_t;
2174 static vlm_media_sys_t *vlm_ControlMediaGetById( vlm_t *p_vlm, int64_t id )
2178 for( i = 0; i < p_vlm->i_media; i++ )
2180 if( p_vlm->media[i]->cfg.id == id )
2181 return p_vlm->media[i];
2185 static vlm_media_sys_t *vlm_ControlMediaGetByName( vlm_t *p_vlm, const char *psz_name )
2189 for( i = 0; i < p_vlm->i_media; i++ )
2191 if( !strcmp( p_vlm->media[i]->cfg.psz_name, psz_name ) )
2192 return p_vlm->media[i];
2196 static int vlm_MediaDescriptionCheck( vlm_t *p_vlm, vlm_media_t *p_cfg )
2200 if( !p_cfg || !p_cfg->psz_name ||
2201 !strcmp( p_cfg->psz_name, "all" ) || !strcmp( p_cfg->psz_name, "media" ) || !strcmp( p_cfg->psz_name, "schedule" ) )
2202 return VLC_EGENERIC;
2204 for( i = 0; i < p_vlm->i_media; i++ )
2206 if( p_vlm->media[i]->cfg.id == p_cfg->id )
2208 if( !strcmp( p_vlm->media[i]->cfg.psz_name, p_cfg->psz_name ) )
2209 return VLC_EGENERIC;
2215 /* Called after a media description is changed/added */
2216 static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
2218 vlm_media_t *p_cfg = &p_media->cfg;
2219 /* Check if we need to create/delete a vod media */
2222 if( !p_cfg->b_enabled && p_media->vod.p_media )
2224 p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
2225 p_media->vod.p_media = NULL;
2227 else if( p_cfg->b_enabled && !p_media->vod.p_media && p_cfg->i_input )
2229 /* Pre-parse the input */
2230 input_thread_t *p_input;
2236 input_ItemClean( &p_media->vod.item );
2237 input_ItemInit( VLC_OBJECT(p_vlm), &p_media->vod.item );
2239 if( p_cfg->psz_output )
2240 asprintf( &psz_output, "%s:description", p_cfg->psz_output );
2242 asprintf( &psz_output, "#description" );
2244 p_media->vod.item.psz_uri = strdup( p_cfg->ppsz_input[0] );
2246 asprintf( &psz_dup, "sout=%s", psz_output);
2247 input_ItemAddOption( &p_media->vod.item, psz_dup );
2249 for( i = 0; i < p_cfg->i_option; i++ )
2250 input_ItemAddOption( &p_media->vod.item,
2251 p_cfg->ppsz_option[i] );
2252 input_ItemAddOption( &p_media->vod.item, "no-sout-keep" );
2254 asprintf( &psz_header, _("Media: %s"), p_cfg->psz_name );
2256 if( (p_input = input_CreateThreadExtended( p_vlm, &p_media->vod.item, psz_header, NULL ) ) )
2258 while( !p_input->b_eof && !p_input->b_error )
2261 input_StopThread( p_input );
2262 vlc_object_release( p_input );
2267 if( p_cfg->vod.psz_mux )
2270 es_format_t es, *p_es = &es;
2273 sprintf( fourcc, "%4.4s", p_cfg->vod.psz_mux );
2274 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
2275 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
2277 item = p_media->vod.item;
2280 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
2282 p_media->vod.p_media =
2283 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
2287 p_media->vod.p_media =
2288 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &p_media->vod.item );
2294 /* TODO start media if needed */
2297 /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
2301 static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
2303 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
2306 if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
2307 return VLC_EGENERIC;
2308 if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
2309 return VLC_EGENERIC;
2313 /* TODO check what are the changes being done (stop instance if needed) */
2316 vlm_media_Clean( &p_media->cfg );
2317 vlm_media_Copy( &p_media->cfg, p_cfg );
2319 return vlm_OnMediaUpdate( p_vlm, p_media );
2322 static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id )
2324 vlm_media_sys_t *p_media;
2326 if( vlm_MediaDescriptionCheck( p_vlm, p_cfg ) || vlm_ControlMediaGetByName( p_vlm, p_cfg->psz_name ) )
2328 msg_Err( p_vlm, "invalid media description" );
2329 return VLC_EGENERIC;
2331 /* Check if we need to load the VOD server */
2332 if( p_cfg->b_vod && !p_vlm->i_vod )
2334 p_vlm->p_vod = vlc_object_create( p_vlm, VLC_OBJECT_VOD );
2335 vlc_object_attach( p_vlm->p_vod, p_vlm );
2336 p_vlm->p_vod->p_module = module_Need( p_vlm->p_vod, "vod server", 0, 0 );
2337 if( !p_vlm->p_vod->p_module )
2339 msg_Err( p_vlm, "cannot find vod server" );
2340 vlc_object_detach( p_vlm->p_vod );
2341 vlc_object_release( p_vlm->p_vod );
2343 return VLC_EGENERIC;
2346 p_vlm->p_vod->p_data = p_vlm;
2347 p_vlm->p_vod->pf_media_control = vlm_MediaVodControl;
2350 p_media = malloc( sizeof( vlm_media_sys_t ) );
2353 msg_Err( p_vlm, "out of memory" );
2356 memset( p_media, 0, sizeof(vlm_media_sys_t) );
2361 vlm_media_Copy( &p_media->cfg, p_cfg );
2362 p_media->cfg.id = p_vlm->i_id++;
2363 /* FIXME do we do something here if enabled is true ? */
2365 input_ItemInit( VLC_OBJECT(p_vlm), &p_media->vod.item );
2367 p_media->vod.p_media = NULL;
2368 TAB_INIT( p_media->i_instance, p_media->instance );
2371 TAB_APPEND( p_vlm->i_media, p_vlm->media, p_media );
2374 *p_id = p_media->cfg.id;
2376 return vlm_OnMediaUpdate( p_vlm, p_media );
2379 static int vlm_ControlMediaDel( vlm_t *p_vlm, int64_t id )
2381 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2384 return VLC_EGENERIC;
2386 while( p_media->i_instance > 0 )
2387 vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, p_media->instance[0]->psz_name );
2389 if( p_media->cfg.b_vod )
2391 p_media->cfg.b_enabled = VLC_FALSE;
2392 vlm_OnMediaUpdate( p_vlm, p_media );
2396 vlm_media_Clean( &p_media->cfg );
2398 input_ItemClean( &p_media->vod.item );
2400 TAB_REMOVE( p_vlm->i_media, p_vlm->media, p_media );
2404 /* Check if we need to unload the VOD server */
2405 if( p_vlm->p_vod && p_vlm->i_vod <= 0 )
2407 module_Unneed( p_vlm->p_vod, p_vlm->p_vod->p_module );
2408 vlc_object_detach( p_vlm->p_vod );
2409 vlc_object_release( p_vlm->p_vod );
2410 p_vlm->p_vod = NULL;
2415 static int vlm_ControlMediaGets( vlm_t *p_vlm, vlm_media_t ***ppp_dsc, int *pi_dsc )
2417 vlm_media_t **pp_dsc;
2421 TAB_INIT( i_dsc, pp_dsc );
2422 for( i = 0; i < p_vlm->i_media; i++ )
2424 vlm_media_t *p_dsc = vlm_media_Duplicate( &p_vlm->media[i]->cfg );
2425 TAB_APPEND( i_dsc, pp_dsc, p_dsc );
2433 static int vlm_ControlMediaClear( vlm_t *p_vlm )
2435 while( p_vlm->i_media > 0 )
2436 vlm_ControlMediaDel( p_vlm, p_vlm->media[0]->cfg.id );
2440 static int vlm_ControlMediaGet( vlm_t *p_vlm, int64_t id, vlm_media_t **pp_dsc )
2442 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2444 return VLC_EGENERIC;
2446 *pp_dsc = vlm_media_Duplicate( &p_media->cfg );
2449 static int vlm_ControlMediaGetId( vlm_t *p_vlm, const char *psz_name, int64_t *p_id )
2451 vlm_media_sys_t *p_media = vlm_ControlMediaGetByName( p_vlm, psz_name );
2453 return VLC_EGENERIC;
2455 *p_id = p_media->cfg.id;
2459 static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
2463 for( i = 0; i < p_media->i_instance; i++ )
2465 const char *psz = p_media->instance[i]->psz_name;
2466 if( ( psz == NULL && psz_id == NULL ) ||
2467 ( psz && psz_id && !strcmp( psz, psz_id ) ) )
2468 return p_media->instance[i];
2472 static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
2474 vlm_media_instance_sys_t *p_instance = malloc( sizeof(vlm_media_instance_sys_t) );
2478 memset( p_instance, 0, sizeof(vlm_media_instance_sys_t) );
2480 p_instance->psz_name = NULL;
2482 p_instance->psz_name = strdup( psz_name );
2484 input_ItemInit( VLC_OBJECT(p_vlm), &p_instance->item );
2486 p_instance->i_index = 0;
2487 p_instance->b_sout_keep = VLC_FALSE;
2488 p_instance->p_input = NULL;
2489 p_instance->p_sout = NULL;
2493 static void vlm_MediaInstanceDelete( vlm_media_instance_sys_t *p_instance )
2495 if( p_instance->p_input )
2497 input_StopThread( p_instance->p_input );
2498 p_instance->p_sout = input_DetachSout( p_instance->p_input );
2499 vlc_object_release( p_instance->p_input );
2501 if( p_instance->p_sout )
2502 sout_DeleteInstance( p_instance->p_sout );
2504 input_ItemClean( &p_instance->item );
2505 free( p_instance->psz_name );
2510 static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
2512 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2513 vlm_media_instance_sys_t *p_instance;
2516 if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
2517 return VLC_EGENERIC;
2519 /* TODO support multiple input for VOD with sout-keep ? */
2521 if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
2522 return VLC_EGENERIC;
2524 if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
2525 return VLC_EGENERIC;
2527 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2530 vlm_media_t *p_cfg = &p_media->cfg;
2531 const char *psz_keep;
2534 p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
2538 if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
2541 asprintf( &psz_buffer, "sout=%s%s%s",
2542 p_cfg->psz_output ? p_cfg->psz_output : "",
2543 (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
2544 psz_vod_output ? psz_vod_output : "" );
2545 input_ItemAddOption( &p_instance->item, psz_buffer );
2549 for( i = 0; i < p_cfg->i_option; i++ )
2551 if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
2552 p_instance->b_sout_keep = VLC_TRUE;
2553 else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
2554 p_instance->b_sout_keep = VLC_FALSE;
2556 input_ItemAddOption( &p_instance->item, p_cfg->ppsz_option[i] );
2558 /* We force the right sout-keep value (avoid using the sout-keep from the global configuration)
2559 * FIXME implement input list for VOD (need sout-keep)
2561 if( !p_cfg->b_vod && p_instance->b_sout_keep )
2562 psz_keep = "sout-keep";
2564 psz_keep = "no-sout-keep";
2565 input_ItemAddOption( &p_instance->item, psz_keep );
2567 TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
2570 /* Stop old instance */
2571 if( p_instance->p_input )
2573 if( p_instance->i_index == i_input_index &&
2574 !p_instance->p_input->b_eof && !p_instance->p_input->b_error )
2576 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2577 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2581 input_StopThread( p_instance->p_input );
2582 p_instance->p_sout = input_DetachSout( p_instance->p_input );
2583 vlc_object_release( p_instance->p_input );
2584 if( !p_instance->b_sout_keep && p_instance->p_sout )
2586 sout_DeleteInstance( p_instance->p_sout );
2587 p_instance->p_sout = NULL;
2592 p_instance->i_index = i_input_index;
2593 input_item_SetURI( &p_instance->item, p_media->cfg.ppsz_input[p_instance->i_index] ) ;
2595 asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name );
2596 p_instance->p_input = input_CreateThreadExtended( p_vlm, &p_instance->item, psz_log, p_instance->p_sout );
2597 if( !p_instance->p_input )
2599 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2600 vlm_MediaInstanceDelete( p_instance );
2607 static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
2609 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2610 vlm_media_instance_sys_t *p_instance;
2613 return VLC_EGENERIC;
2615 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2617 return VLC_EGENERIC;
2619 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2621 vlm_MediaInstanceDelete( p_instance );
2625 static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
2627 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2628 vlm_media_instance_sys_t *p_instance;
2632 return VLC_EGENERIC;
2634 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2635 if( !p_instance || !p_instance->p_input )
2636 return VLC_EGENERIC;
2638 /* Toggle pause state */
2639 i_state = var_GetInteger( p_instance->p_input, "state" );
2640 if( i_state == PAUSE_S )
2641 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2642 else if( i_state == PLAYING_S )
2643 var_SetInteger( p_instance->p_input, "state", PAUSE_S );
2646 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
2648 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2649 vlm_media_instance_sys_t *p_instance;
2652 return VLC_EGENERIC;
2654 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2655 if( !p_instance || !p_instance->p_input )
2656 return VLC_EGENERIC;
2659 *pi_time = var_GetTime( p_instance->p_input, "time" );
2661 *pd_position = var_GetFloat( p_instance->p_input, "position" );
2664 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t i_time, double d_position )
2666 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2667 vlm_media_instance_sys_t *p_instance;
2670 return VLC_EGENERIC;
2672 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2673 if( !p_instance || !p_instance->p_input )
2674 return VLC_EGENERIC;
2677 return var_SetTime( p_instance->p_input, "time", i_time );
2678 else if( d_position >= 0 && d_position <= 100 )
2679 return var_SetFloat( p_instance->p_input, "position", d_position );
2680 return VLC_EGENERIC;
2683 static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_instance_t ***ppp_idsc, int *pi_instance )
2685 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2686 vlm_media_instance_t **pp_idsc;
2691 return VLC_EGENERIC;
2693 TAB_INIT( i_idsc, pp_idsc );
2694 for( i = 0; i < p_media->i_instance; i++ )
2696 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
2697 vlm_media_instance_t *p_idsc = vlm_media_instance_New();
2699 if( p_instance->psz_name )
2700 p_idsc->psz_name = strdup( p_instance->psz_name );
2701 if( p_instance->p_input )
2703 p_idsc->i_time = var_GetTime( p_instance->p_input, "time" );
2704 p_idsc->i_length = var_GetTime( p_instance->p_input, "length" );
2705 p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
2706 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2707 p_idsc->b_paused = VLC_TRUE;
2708 p_idsc->i_rate = var_GetInteger( p_instance->p_input, "rate" );
2711 TAB_APPEND( i_idsc, pp_idsc, p_idsc );
2713 *ppp_idsc = pp_idsc;
2714 *pi_instance = i_idsc;
2717 static int vlm_ControlMediaInstanceClear( vlm_t *p_vlm, int64_t id )
2719 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2722 return VLC_EGENERIC;
2724 while( p_media->i_instance > 0 )
2725 vlm_ControlMediaInstanceStop( p_vlm, id, p_media->instance[0]->psz_name );
2730 static int vlm_ControlScheduleClear( vlm_t *p_vlm )
2732 while( p_vlm->i_schedule > 0 )
2733 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0] );
2737 static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
2740 vlm_media_t **pp_dsc;
2741 vlm_media_t ***ppp_dsc;
2742 vlm_media_instance_t ***ppp_idsc;
2744 const char *psz_vod;
2758 case VLM_GET_MEDIAS:
2759 ppp_dsc = (vlm_media_t ***)va_arg( args, vlm_media_t *** );
2760 pi_int = (int *)va_arg( args, int * );
2761 return vlm_ControlMediaGets( p_vlm, ppp_dsc, pi_int );
2763 case VLM_CLEAR_MEDIAS:
2764 return vlm_ControlMediaClear( p_vlm );
2766 case VLM_CHANGE_MEDIA:
2767 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2768 return vlm_ControlMediaChange( p_vlm, p_dsc );
2771 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2772 p_id = (int64_t*)va_arg( args, int64_t * );
2773 return vlm_ControlMediaAdd( p_vlm, p_dsc, p_id );
2776 id = (int64_t)va_arg( args, int64_t );
2777 return vlm_ControlMediaDel( p_vlm, id );
2780 id = (int64_t)va_arg( args, int64_t );
2781 pp_dsc = (vlm_media_t **)va_arg( args, vlm_media_t ** );
2782 return vlm_ControlMediaGet( p_vlm, id, pp_dsc );
2784 case VLM_GET_MEDIA_ID:
2785 psz_id = (const char*)va_arg( args, const char * );
2786 p_id = (int64_t*)va_arg( args, int64_t * );
2787 return vlm_ControlMediaGetId( p_vlm, psz_id, p_id );
2790 /* Media instance control */
2791 case VLM_GET_MEDIA_INSTANCES:
2792 id = (int64_t)va_arg( args, int64_t );
2793 ppp_idsc = (vlm_media_instance_t ***)va_arg( args, vlm_media_instance_t *** );
2794 pi_int = (int *)va_arg( args, int *);
2795 return vlm_ControlMediaInstanceGets( p_vlm, id, ppp_idsc, pi_int );
2797 case VLM_CLEAR_MEDIA_INSTANCES:
2798 id = (int64_t)va_arg( args, int64_t );
2799 return vlm_ControlMediaInstanceClear( p_vlm, id );
2802 case VLM_START_MEDIA_BROADCAST_INSTANCE:
2803 id = (int64_t)va_arg( args, int64_t );
2804 psz_id = (const char*)va_arg( args, const char* );
2805 i_int = (int)va_arg( args, int );
2806 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, NULL );
2808 case VLM_START_MEDIA_VOD_INSTANCE:
2809 id = (int64_t)va_arg( args, int64_t );
2810 psz_id = (const char*)va_arg( args, const char* );
2811 i_int = (int)va_arg( args, int );
2812 psz_vod = (const char*)va_arg( args, const char* );
2814 return VLC_EGENERIC;
2815 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, psz_vod );
2817 case VLM_STOP_MEDIA_INSTANCE:
2818 id = (int64_t)va_arg( args, int64_t );
2819 psz_id = (const char*)va_arg( args, const char* );
2820 return vlm_ControlMediaInstanceStop( p_vlm, id, psz_id );
2822 case VLM_PAUSE_MEDIA_INSTANCE:
2823 id = (int64_t)va_arg( args, int64_t );
2824 psz_id = (const char*)va_arg( args, const char* );
2825 return vlm_ControlMediaInstancePause( p_vlm, id, psz_id );
2827 case VLM_GET_MEDIA_INSTANCE_TIME:
2828 id = (int64_t)va_arg( args, int64_t );
2829 psz_id = (const char*)va_arg( args, const char* );
2830 pi_i64 = (int64_t*)va_arg( args, int64_t * );
2831 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, pi_i64, NULL );
2832 case VLM_GET_MEDIA_INSTANCE_POSITION:
2833 id = (int64_t)va_arg( args, int64_t );
2834 psz_id = (const char*)va_arg( args, const char* );
2835 pd_double = (double*)va_arg( args, double* );
2836 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, NULL, pd_double );
2838 case VLM_SET_MEDIA_INSTANCE_TIME:
2839 id = (int64_t)va_arg( args, int64_t );
2840 psz_id = (const char*)va_arg( args, const char* );
2841 i_i64 = (int64_t)va_arg( args, int64_t);
2842 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, i_i64, -1 );
2843 case VLM_SET_MEDIA_INSTANCE_POSITION:
2844 id = (int64_t)va_arg( args, int64_t );
2845 psz_id = (const char*)va_arg( args, const char* );
2846 d_double = (double)va_arg( args, double );
2847 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, -1, d_double );
2849 case VLM_CLEAR_SCHEDULES:
2850 return vlm_ControlScheduleClear( p_vlm );
2853 msg_Err( p_vlm, "unknown VLM query" );
2854 return VLC_EGENERIC;
2857 static int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... )
2862 va_start( args, i_query );
2863 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2869 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2874 va_start( args, i_query );
2876 vlc_mutex_lock( &p_vlm->lock );
2877 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2878 vlc_mutex_unlock( &p_vlm->lock );
2885 #else /* ENABLE_VLM */
2887 /* We just define an empty wrapper */
2888 vlm_t *__vlm_New( vlc_object_t *a )
2890 msg_Err( a, "VideoLAN manager support is disabled" );
2894 void vlm_Delete( vlm_t *a )
2899 int vlm_ExecuteCommand( vlm_t *a, const char *b, vlm_message_t **c )
2904 vlm_message_t *vlm_MessageNew( const char *psz_name,
2905 const char *psz_format, ... )
2907 (void)psz_name; (void)psz_format;
2911 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
2912 vlm_message_t *p_child )
2917 void vlm_MessageDelete( vlm_message_t *a )
2922 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2924 (void)p_vlm; (void)i_query;
2925 return VLC_EGENERIC;
2928 #endif /* ENABLE_VLM */