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 *****************************************************************************/
32 #include <stdlib.h> /* malloc(), free() */
33 #include <ctype.h> /* tolower() */
40 # include <time.h> /* ctime() */
41 # include <sys/timeb.h> /* ftime() */
44 #include <vlc_input.h>
45 #include "input_internal.h"
46 #include <vlc_stream.h>
48 #include "vlm_internal.h"
50 #include <vlc_charset.h>
52 #include "../stream_output/stream_output.h"
54 /*****************************************************************************
56 *****************************************************************************/
58 static int vlm_ControlInternal( vlm_t *, int, ... );
61 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_sys_t *, vlm_schedule_sys_t *, const char * );
62 static vlm_message_t *vlm_Help( vlm_t *, char * );
64 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *, const char * );
66 static char *Save( vlm_t * );
67 static int Load( vlm_t *, char * );
69 static int ExecuteCommand( vlm_t *, const char *, vlm_message_t ** );
71 static int Manage( vlc_object_t * );
73 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name );
74 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched, const char *psz_name );
75 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
76 const char *psz_value );
78 static int vlm_MediaVodControl( void *, vod_media_t *, const char *, int, va_list );
81 static vlm_media_sys_t *vlm_MediaSearch( vlm_t *, const char *);
83 /*****************************************************************************
85 *****************************************************************************/
86 vlm_t *__vlm_New ( vlc_object_t *p_this )
92 /* Avoid multiple creation */
93 if( var_Create( p_this->p_libvlc_global, "vlm_mutex", VLC_VAR_MUTEX ) ||
94 var_Get( p_this->p_libvlc_global, "vlm_mutex", &lockval ) )
97 vlc_mutex_lock( lockval.p_address );
99 p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE );
102 vlc_object_yield( p_vlm );
103 vlc_mutex_unlock( lockval.p_address );
107 msg_Dbg( p_this, "creating VLM" );
109 p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM );
112 vlc_mutex_unlock( lockval.p_address );
116 vlc_mutex_init( p_this->p_libvlc, &p_vlm->lock );
118 TAB_INIT( p_vlm->i_media, p_vlm->media );
119 TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
122 vlc_object_yield( p_vlm );
123 vlc_object_attach( p_vlm, p_this->p_libvlc );
125 if( vlc_thread_create( p_vlm, "vlm thread",
126 Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
128 vlc_mutex_destroy( &p_vlm->lock );
129 vlc_object_destroy( p_vlm );
133 /* Load our configuration file */
134 psz_vlmconf = var_CreateGetString( p_vlm, "vlm-conf" );
135 if( psz_vlmconf && *psz_vlmconf )
137 vlm_message_t *p_message = NULL;
138 char *psz_buffer = NULL;
140 msg_Dbg( p_this, "loading VLM configuration" );
141 asprintf(&psz_buffer, "load %s", psz_vlmconf );
144 msg_Dbg( p_this, psz_buffer );
145 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
146 msg_Warn( p_this, "error while loading the configuration file" );
148 vlm_MessageDelete(p_message);
154 vlc_mutex_unlock( lockval.p_address );
159 /*****************************************************************************
161 *****************************************************************************/
162 void vlm_Delete( vlm_t *p_vlm )
166 var_Get( p_vlm->p_libvlc_global, "vlm_mutex", &lockval );
167 vlc_mutex_lock( lockval.p_address );
169 vlc_object_release( p_vlm );
171 if( p_vlm->i_refcount > 0 )
173 vlc_mutex_unlock( lockval.p_address );
177 p_vlm->b_die = VLC_TRUE;
178 vlc_thread_join( p_vlm );
180 vlc_object_detach( p_vlm );
182 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
183 TAB_CLEAN( p_vlm->i_media, p_vlm->media );
185 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
186 TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
188 vlc_mutex_destroy( &p_vlm->lock );
190 vlc_object_destroy( p_vlm );
191 vlc_mutex_unlock( lockval.p_address );
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, NULL );
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", 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_t *p_vlm, vlm_message_t **pp_status )
422 *pp_status = vlm_Help( p_vlm, NULL );
426 static int ExecuteControl( vlm_t *p_vlm, const char *psz_name, const int i_arg, char ** ppsz_arg, vlm_message_t **pp_status )
428 vlm_media_sys_t *p_media;
429 const char *psz_control = NULL;
430 const char *psz_instance = NULL;
431 const char *psz_argument = NULL;
435 if( !ExecuteIsMedia( p_vlm, psz_name ) )
437 *pp_status = vlm_MessageNew( "control", "%s: media unknown", psz_name );
443 #define IS(txt) ( !strcmp( ppsz_arg[i_index], (txt) ) )
445 if( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") )
448 psz_instance = ppsz_arg[0];
450 if( i_index >= i_arg || ( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") ) )
451 return ExecuteSyntaxError( "control", pp_status );
454 psz_control = ppsz_arg[i_index];
456 if( i_index+1 < i_arg )
457 psz_argument = ppsz_arg[i_index+1];
459 p_media = vlm_MediaSearch( p_vlm, psz_name );
462 if( !strcmp( psz_control, "play" ) )
464 int i_input_index = 0;
467 if( ( psz_argument && sscanf(psz_argument, "%d", &i) == 1 ) && i > 0 && i-1 < p_media->cfg.i_input )
470 if( p_media->cfg.b_vod )
471 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
473 i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, psz_instance, i_input_index );
475 else if( !strcmp( psz_control, "seek" ) )
479 vlc_bool_t b_relative;
480 if( psz_argument[0] == '+' || psz_argument[0] == '-' )
481 b_relative = VLC_TRUE;
483 b_relative = VLC_FALSE;
485 if( strstr( psz_argument, "ms" ) || strstr( psz_argument, "s" ) )
490 if( strstr( psz_argument, "ms" ) )
491 i_new_time = 1000 * (int64_t)atoi( psz_argument );
493 i_new_time = 1000000 * (int64_t)atoi( psz_argument );
498 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, &i_time );
499 i_new_time += i_time;
503 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, i_new_time );
508 double d_new_position = i18n_atof( psz_argument ) / 100.0;
512 double d_position = 0.0;
514 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
515 d_new_position += d_position;
517 if( d_new_position < 0.0 )
518 d_new_position = 0.0;
519 else if( d_new_position > 1.0 )
520 d_new_position = 1.0;
521 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_new_position );
526 i_result = VLC_EGENERIC;
529 else if( !strcmp( psz_control, "rewind" ) )
533 const double d_scale = i18n_atof( psz_argument );
536 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
537 d_position -= (d_scale / 1000.0);
538 if( d_position < 0.0 )
540 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
544 i_result = VLC_EGENERIC;
547 else if( !strcmp( psz_control, "forward" ) )
551 const double d_scale = i18n_atof( psz_argument );
554 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
555 d_position += (d_scale / 1000.0);
556 if( d_position > 1.0 )
558 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
563 i_result = VLC_EGENERIC;
566 else if( !strcmp( psz_control, "stop" ) )
568 i_result = vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
570 else if( !strcmp( psz_control, "pause" ) )
572 i_result = vlm_ControlInternal( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
576 i_result = VLC_EGENERIC;
581 *pp_status = vlm_MessageNew( "control", "unknown error" );
584 *pp_status = vlm_MessageNew( "control", NULL );
588 static int ExecuteExport( vlm_t *p_vlm, vlm_message_t **pp_status )
590 char *psz_export = Save( p_vlm );
592 *pp_status = vlm_MessageNew( "export", psz_export );
597 static int ExecuteSave( vlm_t *p_vlm, const char *psz_file, vlm_message_t **pp_status )
599 FILE *f = utf8_fopen( psz_file, "wt" );
605 psz_save = Save( p_vlm );
606 if( psz_save == NULL )
611 fwrite( psz_save, strlen( psz_save ), 1, f );
615 *pp_status = vlm_MessageNew( "save", NULL );
619 *pp_status = vlm_MessageNew( "save", "Unable to save to file" );
623 static int ExecuteLoad( vlm_t *p_vlm, const char *psz_url, vlm_message_t **pp_status )
625 stream_t *p_stream = stream_UrlNew( p_vlm, psz_url );
631 *pp_status = vlm_MessageNew( "load", "Unable to load from file" );
636 if( stream_Seek( p_stream, 0 ) != 0 )
638 stream_Delete( p_stream );
640 *pp_status = vlm_MessageNew( "load", "Read file error" );
644 i_size = stream_Size( p_stream );
646 psz_buffer = malloc( i_size + 1 );
649 stream_Delete( p_stream );
651 *pp_status = vlm_MessageNew( "load", "Read file error" );
655 stream_Read( p_stream, psz_buffer, i_size );
656 psz_buffer[i_size] = '\0';
658 stream_Delete( p_stream );
660 if( Load( p_vlm, psz_buffer ) )
664 *pp_status = vlm_MessageNew( "load", "Error while loading file" );
670 *pp_status = vlm_MessageNew( "load", NULL );
674 static int ExecuteScheduleProperty( vlm_t *p_vlm, vlm_schedule_sys_t *p_schedule, vlc_bool_t b_new,
675 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
677 const char *psz_cmd = b_new ? "new" : "setup";
680 for( i = 0; i < i_property; i++ )
682 if( !strcmp( ppsz_property[i], "enabled" ) ||
683 !strcmp( ppsz_property[i], "disabled" ) )
685 vlm_ScheduleSetup( p_schedule, ppsz_property[i], NULL );
687 else if( !strcmp( ppsz_property[i], "append" ) )
691 /* Beware: everything behind append is considered as
694 if( ++i >= i_property )
697 psz_line = strdup( ppsz_property[i] );
698 for( j = i+1; j < i_property; j++ )
700 psz_line = realloc( psz_line, strlen(psz_line) + strlen(ppsz_property[j]) + 1 + 1 );
701 strcat( psz_line, " " );
702 strcat( psz_line, ppsz_property[j] );
705 vlm_ScheduleSetup( p_schedule, "append", psz_line );
710 if( i + 1 >= i_property )
713 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
714 return ExecuteSyntaxError( psz_cmd, pp_status );
717 vlm_ScheduleSetup( p_schedule, ppsz_property[i], ppsz_property[i+1] );
721 *pp_status = vlm_MessageNew( psz_cmd, NULL );
725 static int ExecuteMediaProperty( vlm_t *p_vlm, int64_t id, vlc_bool_t b_new,
726 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
728 const char *psz_cmd = b_new ? "new" : "setup";
729 vlm_media_t *p_cfg = NULL;
733 #define ERROR( txt ) do { *pp_status = vlm_MessageNew( psz_cmd, txt); goto error; } while(0)
734 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA, id, &p_cfg ) )
735 ERROR( "unknown media" );
737 #define MISSING(cmd) do { if( !psz_value ) ERROR( "missing argument for " cmd ); } while(0)
738 for( i = 0; i < i_property; i++ )
740 const char *psz_option = ppsz_property[i];
741 const char *psz_value = i+1 < i_property ? ppsz_property[i+1] : NULL;
743 if( !strcmp( psz_option, "enabled" ) )
745 p_cfg->b_enabled = VLC_TRUE;
747 else if( !strcmp( psz_option, "disabled" ) )
749 p_cfg->b_enabled = VLC_FALSE;
751 else if( !strcmp( psz_option, "input" ) )
754 TAB_APPEND( p_cfg->i_input, p_cfg->ppsz_input, strdup(psz_value) );
757 else if( !strcmp( psz_option, "inputdel" ) && psz_value && !strcmp( psz_value, "all" ) )
759 while( p_cfg->i_input > 0 )
760 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[0] );
763 else if( !strcmp( psz_option, "inputdel" ) )
767 MISSING( "inputdel" );
769 for( j = 0; j < p_cfg->i_input; j++ )
771 if( !strcmp( p_cfg->ppsz_input[j], psz_value ) )
773 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[j] );
779 else if( !strcmp( psz_option, "inputdeln" ) )
783 MISSING( "inputdeln" );
785 i_index = atoi( psz_value );
786 if( i_index > 0 && i_index <= p_cfg->i_input )
787 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[i_index-1] );
790 else if( !strcmp( psz_option, "output" ) )
794 if( p_cfg->psz_output != NULL )
795 free( p_cfg->psz_output );
796 p_cfg->psz_output = *psz_value ? strdup( psz_value ) : NULL;
799 else if( !strcmp( psz_option, "option" ) )
803 TAB_APPEND( p_cfg->i_option, p_cfg->ppsz_option, strdup( psz_value ) );
806 else if( !strcmp( psz_option, "loop" ) )
809 ERROR( "invalid loop option for vod" );
810 p_cfg->broadcast.b_loop = VLC_TRUE;
812 else if( !strcmp( psz_option, "unloop" ) )
815 ERROR( "invalid unloop option for vod" );
816 p_cfg->broadcast.b_loop = VLC_FALSE;
818 else if( !strcmp( psz_option, "mux" ) )
822 ERROR( "invalid mux option for broadcast" );
824 if( p_cfg->vod.psz_mux )
825 free( p_cfg->vod.psz_mux );
826 p_cfg->vod.psz_mux = *psz_value ? strdup( psz_value ) : NULL;
831 fprintf( stderr, "PROP: name=%s unknown\n", psz_option );
832 ERROR( "Wrong command syntax" );
839 i_result = vlm_ControlInternal( p_vlm, VLM_CHANGE_MEDIA, p_cfg );
840 vlm_media_Delete( p_cfg );
842 *pp_status = vlm_MessageNew( psz_cmd, NULL );
849 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_cfg->id );
850 vlm_media_Delete( p_cfg );
854 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 )
857 if( !strcmp( psz_name, "all" ) || !strcmp( psz_name, "media" ) || !strcmp( psz_name, "schedule" ) )
859 *pp_status = vlm_MessageNew( "new", "\"all\", \"media\" and \"schedule\" are reserved names" );
862 if( ExecuteIsMedia( p_vlm, psz_name ) || ExecuteIsSchedule( p_vlm, psz_name ) )
864 *pp_status = vlm_MessageNew( "new", "%s: Name already in use", psz_name );
868 if( !strcmp( psz_type, "schedule" ) )
870 vlm_schedule_sys_t *p_schedule = vlm_ScheduleNew( p_vlm, psz_name );
873 *pp_status = vlm_MessageNew( "new", "could not create schedule" );
876 return ExecuteScheduleProperty( p_vlm, p_schedule, VLC_TRUE, i_property, ppsz_property, pp_status );
878 else if( !strcmp( psz_type, "vod" ) || !strcmp( psz_type, "broadcast" ) )
883 vlm_media_Init( &cfg );
884 cfg.psz_name = strdup( psz_name );
885 cfg.b_vod = !strcmp( psz_type, "vod" );
887 if( vlm_ControlInternal( p_vlm, VLM_ADD_MEDIA, &cfg, &id ) )
889 vlm_media_Clean( &cfg );
890 *pp_status = vlm_MessageNew( "new", "could not create media" );
893 vlm_media_Clean( &cfg );
894 return ExecuteMediaProperty( p_vlm, id, VLC_TRUE, i_property, ppsz_property, pp_status );
898 *pp_status = vlm_MessageNew( "new", "%s: Choose between vod, broadcast or schedule", psz_type );
903 static int ExecuteSetup( vlm_t *p_vlm, const char *psz_name, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
905 if( ExecuteIsSchedule( p_vlm, psz_name ) )
907 vlm_schedule_sys_t *p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
908 return ExecuteScheduleProperty( p_vlm, p_schedule, VLC_FALSE, i_property, ppsz_property, pp_status );
910 else if( ExecuteIsMedia( p_vlm, psz_name ) )
913 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
915 return ExecuteMediaProperty( p_vlm, id, VLC_FALSE, i_property, ppsz_property, pp_status );
919 *pp_status = vlm_MessageNew( "setup", "%s unknown", psz_name );
923 static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
924 vlm_message_t **pp_message )
926 size_t i_command = 0;
927 char buf[strlen (psz_command) + 1], *psz_buf = buf;
928 char *ppsz_command[3+sizeof (buf) / 2];
929 vlm_message_t *p_message = NULL;
931 /* First, parse the line and cut it */
932 while( *psz_command != '\0' )
934 const char *psz_temp;
936 if(isspace (*psz_command))
942 /* support for comments */
943 if( i_command == 0 && *psz_command == '#')
945 p_message = vlm_MessageNew( "", NULL );
949 psz_temp = FindCommandEnd( psz_command );
951 if( psz_temp == NULL )
953 p_message = vlm_MessageNew( "Incomplete command", psz_command );
957 assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
959 ppsz_command[i_command] = psz_buf;
960 memcpy (psz_buf, psz_command, psz_temp - psz_command);
961 psz_buf[psz_temp - psz_command] = '\0';
963 Unescape (psz_buf, psz_buf);
966 psz_buf += psz_temp - psz_command + 1;
967 psz_command = psz_temp;
969 assert (buf + sizeof (buf) >= psz_buf);
973 * And then Interpret it
976 #define IF_EXECUTE( name, check, cmd ) if( !strcmp(ppsz_command[0], name ) ) { if( (check) ) goto syntax_error; if( (cmd) ) goto error; goto success; }
979 p_message = vlm_MessageNew( "", NULL );
982 else IF_EXECUTE( "del", (i_command != 2), ExecuteDel(p_vlm, ppsz_command[1], &p_message) )
983 else IF_EXECUTE( "show", (i_command > 2), ExecuteShow(p_vlm, i_command > 1 ? ppsz_command[1] : NULL, &p_message) )
984 else IF_EXECUTE( "help", (i_command != 1), ExecuteHelp(p_vlm, &p_message) )
985 else IF_EXECUTE( "control", (i_command < 3), ExecuteControl(p_vlm, ppsz_command[1], i_command - 2, &ppsz_command[2], &p_message) )
986 else IF_EXECUTE( "save", (i_command != 2), ExecuteSave(p_vlm, ppsz_command[1], &p_message) )
987 else IF_EXECUTE( "export", (i_command != 1), ExecuteExport(p_vlm, &p_message) )
988 else IF_EXECUTE( "load", (i_command != 2), ExecuteLoad(p_vlm, ppsz_command[1], &p_message) )
989 else IF_EXECUTE( "new", (i_command < 3), ExecuteNew(p_vlm, ppsz_command[1], ppsz_command[2], i_command-3, &ppsz_command[3], &p_message) )
990 else IF_EXECUTE( "setup", (i_command < 2), ExecuteSetup(p_vlm, ppsz_command[1], i_command-2, &ppsz_command[2], &p_message) )
993 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
999 *pp_message = p_message;
1003 return ExecuteSyntaxError( ppsz_command[0], pp_message );
1006 *pp_message = p_message;
1007 return VLC_EGENERIC;
1010 /*****************************************************************************
1012 *****************************************************************************/
1013 vlm_media_sys_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
1017 for( i = 0; i < vlm->i_media; i++ )
1019 if( strcmp( psz_name, vlm->media[i]->cfg.psz_name ) == 0 )
1020 return vlm->media[i];
1026 /*****************************************************************************
1028 *****************************************************************************/
1029 static int64_t vlm_Date(void)
1034 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1040 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
1042 vlm_schedule_sys_t *p_sched = malloc( sizeof( vlm_schedule_sys_t ) );
1054 p_sched->psz_name = strdup( psz_name );
1055 p_sched->b_enabled = VLC_FALSE;
1056 p_sched->i_command = 0;
1057 p_sched->command = NULL;
1058 p_sched->i_date = 0;
1059 p_sched->i_period = 0;
1060 p_sched->i_repeat = -1;
1062 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1067 /* for now, simple delete. After, del with options (last arg) */
1068 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched,
1069 const char *psz_name )
1071 if( sched == NULL ) return;
1073 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1075 if( vlm->i_schedule == 0 && vlm->schedule ) free( vlm->schedule );
1076 free( sched->psz_name );
1077 while( sched->i_command )
1079 char *psz_cmd = sched->command[0];
1080 TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1086 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1090 for( i = 0; i < vlm->i_schedule; i++ )
1092 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1094 return vlm->schedule[i];
1101 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1102 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
1103 const char *psz_value )
1105 if( !strcmp( psz_cmd, "enabled" ) )
1107 schedule->b_enabled = VLC_TRUE;
1109 else if( !strcmp( psz_cmd, "disabled" ) )
1111 schedule->b_enabled = VLC_FALSE;
1113 #if !defined( UNDER_CE )
1114 else if( !strcmp( psz_cmd, "date" ) )
1120 time.tm_sec = 0; /* seconds */
1121 time.tm_min = 0; /* minutes */
1122 time.tm_hour = 0; /* hours */
1123 time.tm_mday = 0; /* day of the month */
1124 time.tm_mon = 0; /* month */
1125 time.tm_year = 0; /* year */
1126 time.tm_wday = 0; /* day of the week */
1127 time.tm_yday = 0; /* day in the year */
1128 time.tm_isdst = -1; /* daylight saving time */
1130 /* date should be year/month/day-hour:minutes:seconds */
1131 p = strchr( psz_value, '-' );
1133 if( !strcmp( psz_value, "now" ) )
1135 schedule->i_date = 0;
1137 else if( (p == NULL) && sscanf( psz_value, "%d:%d:%d", &time.tm_hour,
1138 &time.tm_min, &time.tm_sec ) != 3 )
1139 /* it must be a hour:minutes:seconds */
1147 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1165 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1171 time.tm_mon = i - 1;
1175 time.tm_year = i - 1900;
1176 time.tm_mon = j - 1;
1183 date = mktime( &time );
1184 schedule->i_date = ((mtime_t) date) * 1000000;
1187 else if( !strcmp( psz_cmd, "period" ) )
1191 const char *psz_time = NULL, *psz_date = NULL;
1195 /* First, if date or period are modified, repeat should be equal to -1 */
1196 schedule->i_repeat = -1;
1198 time.tm_sec = 0; /* seconds */
1199 time.tm_min = 0; /* minutes */
1200 time.tm_hour = 0; /* hours */
1201 time.tm_mday = 0; /* day of the month */
1202 time.tm_mon = 0; /* month */
1203 time.tm_year = 0; /* year */
1204 time.tm_wday = 0; /* day of the week */
1205 time.tm_yday = 0; /* day in the year */
1206 time.tm_isdst = -1; /* daylight saving time */
1208 /* date should be year/month/day-hour:minutes:seconds */
1209 p = strchr( psz_value, '-' );
1212 psz_date = psz_value;
1217 psz_time = psz_value;
1220 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1239 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1258 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1259 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1260 schedule->i_period = ((mtime_t) date) * 1000000;
1262 #endif /* UNDER_CE */
1263 else if( !strcmp( psz_cmd, "repeat" ) )
1267 if( sscanf( psz_value, "%d", &i ) == 1 )
1269 schedule->i_repeat = i;
1276 else if( !strcmp( psz_cmd, "append" ) )
1278 char *command = strdup( psz_value );
1280 TAB_APPEND( schedule->i_command, schedule->command, command );
1289 /*****************************************************************************
1290 * Message handling functions
1291 *****************************************************************************/
1292 vlm_message_t *vlm_MessageNew( const char *psz_name,
1293 const char *psz_format, ... )
1295 vlm_message_t *p_message;
1298 if( !psz_name ) return NULL;
1300 p_message = malloc( sizeof(vlm_message_t) );
1306 p_message->psz_value = 0;
1310 va_start( args, psz_format );
1311 if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1320 p_message->psz_name = strdup( psz_name );
1321 p_message->i_child = 0;
1322 p_message->child = NULL;
1327 void vlm_MessageDelete( vlm_message_t *p_message )
1329 if( p_message->psz_name ) free( p_message->psz_name );
1330 if( p_message->psz_value ) free( p_message->psz_value );
1331 while( p_message->i_child-- )
1332 vlm_MessageDelete( p_message->child[p_message->i_child] );
1333 if( p_message->child ) free( p_message->child );
1338 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1339 vlm_message_t *p_child )
1341 if( p_message == NULL ) return NULL;
1345 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1351 /*****************************************************************************
1352 * Misc utility functions
1353 *****************************************************************************/
1354 static vlm_message_t *vlm_ShowMedia( vlm_t *p_vlm, vlm_media_sys_t *p_media )
1356 vlm_media_t *p_cfg = &p_media->cfg;
1357 vlm_message_t *p_msg;
1358 vlm_message_t *p_msg_sub;
1361 p_msg = vlm_MessageNew( p_cfg->psz_name, 0 );
1362 vlm_MessageAdd( p_msg,
1363 vlm_MessageNew( "type", p_cfg->b_vod ? "vod" : "broadcast" ) );
1364 vlm_MessageAdd( p_msg,
1365 vlm_MessageNew( "enabled", p_cfg->b_enabled ? "yes" : "no" ) );
1368 vlm_MessageAdd( p_msg,
1369 vlm_MessageNew( "mux", p_cfg->vod.psz_mux ) );
1371 vlm_MessageAdd( p_msg,
1372 vlm_MessageNew( "loop", p_cfg->broadcast.b_loop ? "yes" : "no" ) );
1374 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "inputs", NULL ) );
1375 for( i = 0; i < p_cfg->i_input; i++ )
1376 vlm_MessageAdd( p_msg_sub, vlm_MessageNew( p_cfg->ppsz_input[i], NULL ) );
1378 vlm_MessageAdd( p_msg,
1379 vlm_MessageNew( "output", p_cfg->psz_output ? p_cfg->psz_output : "" ) );
1381 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "options", 0 ) );
1382 for( i = 0; i < p_cfg->i_option; i++ )
1383 vlm_MessageAdd( p_msg_sub, vlm_MessageNew( p_cfg->ppsz_option[i], 0 ) );
1385 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "instances", NULL ) );
1386 for( i = 0; i < p_media->i_instance; i++ )
1388 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1390 vlm_message_t *p_msg_instance;
1394 if( p_instance->p_input )
1395 var_Get( p_instance->p_input, "state", &val );
1397 p_msg_instance = vlm_MessageAdd( p_msg_sub, vlm_MessageNew( "instance" , NULL ) );
1399 vlm_MessageAdd( p_msg_instance,
1400 vlm_MessageNew( "name" , p_instance->psz_name ? p_instance->psz_name : "default" ) );
1401 vlm_MessageAdd( p_msg_instance,
1402 vlm_MessageNew( "state",
1403 val.i_int == PLAYING_S ? "playing" :
1404 val.i_int == PAUSE_S ? "paused" :
1407 /* FIXME should not do that this way */
1408 if( p_instance->p_input )
1410 #define APPEND_INPUT_INFO( a, format, type ) \
1411 asprintf( &psz_tmp, format, \
1412 var_Get ## type( p_instance->p_input, a ) ); \
1413 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( a, psz_tmp ) ); \
1415 APPEND_INPUT_INFO( "position", "%f", Float );
1416 APPEND_INPUT_INFO( "time", I64Fi, Time );
1417 APPEND_INPUT_INFO( "length", I64Fi, Time );
1418 APPEND_INPUT_INFO( "rate", "%d", Integer );
1419 APPEND_INPUT_INFO( "title", "%d", Integer );
1420 APPEND_INPUT_INFO( "chapter", "%d", Integer );
1421 APPEND_INPUT_INFO( "seekable", "%d", Bool );
1423 #undef APPEND_INPUT_INFO
1424 asprintf( &psz_tmp, "%d", p_instance->i_index + 1 );
1425 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( "playlistindex", psz_tmp ) );
1431 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
1432 vlm_schedule_sys_t *schedule,
1433 const char *psz_filter )
1437 vlm_message_t *p_msg = vlm_MessageNew( "show", NULL );
1439 vlm_MessageAdd( p_msg, vlm_ShowMedia( vlm, media ) );
1443 else if( schedule != NULL )
1447 vlm_message_t *msg_schedule;
1448 vlm_message_t *msg_child;
1451 msg = vlm_MessageNew( "show", NULL );
1453 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, 0 ) );
1455 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1457 vlm_MessageAdd( msg_schedule,
1458 vlm_MessageNew( "enabled", schedule->b_enabled ?
1461 #if !defined( UNDER_CE )
1462 if( schedule->i_date != 0 )
1465 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1468 #ifdef HAVE_LOCALTIME_R
1469 localtime_r( &i_time, &date);
1471 struct tm *p_date = localtime( &i_time );
1475 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1476 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1477 date.tm_hour, date.tm_min, date.tm_sec );
1479 vlm_MessageAdd( msg_schedule,
1480 vlm_MessageNew( "date", psz_date ) );
1485 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1488 if( schedule->i_period != 0 )
1490 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1493 date.tm_sec = (int)( i_time % 60 );
1494 i_time = i_time / 60;
1495 date.tm_min = (int)( i_time % 60 );
1496 i_time = i_time / 60;
1497 date.tm_hour = (int)( i_time % 24 );
1498 i_time = i_time / 24;
1499 date.tm_mday = (int)( i_time % 30 );
1500 i_time = i_time / 30;
1501 /* okay, okay, months are not always 30 days long */
1502 date.tm_mon = (int)( i_time % 12 );
1503 i_time = i_time / 12;
1504 date.tm_year = (int)i_time;
1506 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1507 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1509 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1513 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1515 #endif /* UNDER_CE */
1517 sprintf( buffer, "%d", schedule->i_repeat );
1518 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1521 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", 0) );
1523 for( i = 0; i < schedule->i_command; i++ )
1525 vlm_MessageAdd( msg_child,
1526 vlm_MessageNew( schedule->command[i], NULL ) );
1533 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1535 vlm_message_t *p_msg;
1536 vlm_message_t *p_msg_child;
1537 int i_vod = 0, i_broadcast = 0;
1541 for( i = 0; i < vlm->i_media; i++ )
1543 if( vlm->media[i]->cfg.b_vod )
1549 asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1551 p_msg = vlm_MessageNew( "show", NULL );
1552 p_msg_child = vlm_MessageAdd( p_msg, vlm_MessageNew( "media", psz_count ) );
1555 for( i = 0; i < vlm->i_media; i++ )
1556 vlm_MessageAdd( p_msg_child, vlm_ShowMedia( vlm, vlm->media[i] ) );
1561 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1565 vlm_message_t *msg_child;
1567 msg = vlm_MessageNew( "show", NULL );
1568 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", NULL ) );
1570 for( i = 0; i < vlm->i_schedule; i++ )
1572 vlm_schedule_sys_t *s = vlm->schedule[i];
1573 vlm_message_t *msg_schedule;
1574 mtime_t i_time, i_next_date;
1576 msg_schedule = vlm_MessageAdd( msg_child,
1577 vlm_MessageNew( s->psz_name, 0 ) );
1578 vlm_MessageAdd( msg_schedule,
1579 vlm_MessageNew( "enabled", s->b_enabled ?
1582 /* calculate next date */
1583 i_time = vlm_Date();
1584 i_next_date = s->i_date;
1586 if( s->i_period != 0 )
1589 while( s->i_date + j * s->i_period <= i_time &&
1595 i_next_date = s->i_date + j * s->i_period;
1598 if( i_next_date > i_time )
1600 time_t i_date = (time_t) (i_next_date / 1000000) ;
1602 #if !defined( UNDER_CE )
1605 ctime_r( &i_date, psz_date );
1607 char *psz_date = ctime( &i_date );
1610 vlm_MessageAdd( msg_schedule,
1611 vlm_MessageNew( "next launch", psz_date ) );
1619 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
1621 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1622 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1624 vlm_MessageAdd( show1, show2->child[0] );
1626 /* We must destroy the parent node "show" of show2
1627 * and not the children */
1628 free( show2->psz_name );
1636 return vlm_MessageNew( "show", NULL );
1640 static vlm_message_t *vlm_Help( vlm_t *vlm, char *psz_filter )
1642 vlm_message_t *message, *message_child;
1644 #define MessageAdd( a ) \
1645 vlm_MessageAdd( message, vlm_MessageNew( a, NULL ) );
1646 #define MessageAddChild( a ) \
1647 vlm_MessageAdd( message_child, vlm_MessageNew( a, NULL ) );
1649 if( psz_filter == NULL )
1651 message = vlm_MessageNew( "help", NULL );
1653 message_child = MessageAdd( "Commands Syntax:" );
1654 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
1655 MessageAddChild( "setup (name) (properties)" );
1656 MessageAddChild( "show [(name)|media|schedule]" );
1657 MessageAddChild( "del (name)|all|media|schedule" );
1658 MessageAddChild( "control (name) [instance_name] (command)" );
1659 MessageAddChild( "save (config_file)" );
1660 MessageAddChild( "export" );
1661 MessageAddChild( "load (config_file)" );
1663 message_child = MessageAdd( "Media Proprieties Syntax:" );
1664 MessageAddChild( "input (input_name)" );
1665 MessageAddChild( "inputdel (input_name)|all" );
1666 MessageAddChild( "inputdeln input_number" );
1667 MessageAddChild( "output (output_name)" );
1668 MessageAddChild( "option (option_name)[=value]" );
1669 MessageAddChild( "enabled|disabled" );
1670 MessageAddChild( "loop|unloop (broadcast only)" );
1671 MessageAddChild( "mux (mux_name)" );
1673 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
1674 MessageAddChild( "enabled|disabled" );
1675 MessageAddChild( "append (command_until_rest_of_the_line)" );
1676 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
1678 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
1679 "(days)-(hours):(minutes):(seconds)" );
1680 MessageAddChild( "repeat (number_of_repetitions)" );
1682 message_child = MessageAdd( "Control Commands Syntax:" );
1683 MessageAddChild( "play [input_number]" );
1684 MessageAddChild( "pause" );
1685 MessageAddChild( "stop" );
1686 MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](miliseconds)ms" );
1691 return vlm_MessageNew( "help", NULL );
1694 /*****************************************************************************
1695 * Config handling functions
1696 *****************************************************************************/
1697 static int Load( vlm_t *vlm, char *file )
1702 while( *pf != '\0' )
1704 vlm_message_t *message = NULL;
1707 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1712 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1716 if( pf[i_end] == '\n' ) i_end++;
1719 if( *pf && ExecuteCommand( vlm, pf, &message ) )
1723 if( message->psz_value )
1724 msg_Err( vlm, "Load error on line %d: %s: %s",
1725 i_line, message->psz_name, message->psz_value );
1726 vlm_MessageDelete( message );
1730 if( message ) vlm_MessageDelete( message );
1739 static char *Save( vlm_t *vlm )
1742 char psz_header[] = "\n"
1743 "# VLC media player VLM command batch\n"
1744 "# http://www.videolan.org/vlc/\n\n" ;
1747 int i_length = strlen( psz_header );
1749 for( i = 0; i < vlm->i_media; i++ )
1751 vlm_media_sys_t *media = vlm->media[i];
1752 vlm_media_t *p_cfg = &media->cfg;
1755 i_length += strlen( "new * vod " ) + strlen(p_cfg->psz_name);
1757 i_length += strlen( "new * broadcast " ) + strlen(p_cfg->psz_name);
1759 if( p_cfg->b_enabled == VLC_TRUE )
1760 i_length += strlen( "enabled" );
1762 i_length += strlen( "disabled" );
1764 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop == VLC_TRUE )
1765 i_length += strlen( " loop\n" );
1767 i_length += strlen( "\n" );
1769 for( j = 0; j < p_cfg->i_input; j++ )
1770 i_length += strlen( "setup * input \"\"\n" ) + strlen( p_cfg->psz_name ) + strlen( p_cfg->ppsz_input[j] );
1772 if( p_cfg->psz_output != NULL )
1773 i_length += strlen( "setup * output \n" ) + strlen(p_cfg->psz_name) + strlen(p_cfg->psz_output);
1775 for( j = 0; j < p_cfg->i_option; j++ )
1776 i_length += strlen("setup * option \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->ppsz_option[j]);
1778 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1779 i_length += strlen("setup * mux \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->vod.psz_mux);
1782 for( i = 0; i < vlm->i_schedule; i++ )
1784 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1786 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
1788 if( schedule->b_enabled == VLC_TRUE )
1790 i_length += strlen( "date //-:: enabled\n" ) + 14;
1794 i_length += strlen( "date //-:: disabled\n" ) + 14;
1798 if( schedule->i_period != 0 )
1800 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
1801 strlen( "period //-::\n" ) + 14;
1804 if( schedule->i_repeat >= 0 )
1808 sprintf( buffer, "%d", schedule->i_repeat );
1809 i_length += strlen( "setup repeat \n" ) +
1810 strlen( schedule->psz_name ) + strlen( buffer );
1817 for( j = 0; j < schedule->i_command; j++ )
1819 i_length += strlen( "setup append \n" ) +
1820 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
1825 /* Don't forget the '\0' */
1827 /* now we have the length of save */
1829 p = save = malloc( i_length );
1832 p += sprintf( p, "%s", psz_header );
1834 /* finally we can write in it */
1835 for( i = 0; i < vlm->i_media; i++ )
1837 vlm_media_sys_t *media = vlm->media[i];
1838 vlm_media_t *p_cfg = &media->cfg;
1841 p += sprintf( p, "new %s vod ", p_cfg->psz_name );
1843 p += sprintf( p, "new %s broadcast ", p_cfg->psz_name );
1845 if( p_cfg->b_enabled )
1846 p += sprintf( p, "enabled" );
1848 p += sprintf( p, "disabled" );
1850 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1851 p += sprintf( p, " loop\n" );
1853 p += sprintf( p, "\n" );
1855 for( j = 0; j < p_cfg->i_input; j++ )
1856 p += sprintf( p, "setup %s input \"%s\"\n", p_cfg->psz_name, p_cfg->ppsz_input[j] );
1858 if( p_cfg->psz_output )
1859 p += sprintf( p, "setup %s output %s\n", p_cfg->psz_name, p_cfg->psz_output );
1861 for( j = 0; j < p_cfg->i_option; j++ )
1862 p += sprintf( p, "setup %s option %s\n", p_cfg->psz_name, p_cfg->ppsz_option[j] );
1864 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1865 p += sprintf( p, "setup %s mux %s\n", p_cfg->psz_name, p_cfg->vod.psz_mux );
1868 /* and now, the schedule scripts */
1869 #if !defined( UNDER_CE )
1870 for( i = 0; i < vlm->i_schedule; i++ )
1872 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1874 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
1876 #ifdef HAVE_LOCALTIME_R
1877 localtime_r( &i_time, &date);
1879 struct tm *p_date = localtime( &i_time );
1883 p += sprintf( p, "new %s schedule ", schedule->psz_name);
1885 if( schedule->b_enabled == VLC_TRUE )
1887 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
1888 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1889 date.tm_hour, date.tm_min, date.tm_sec );
1893 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
1894 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1895 date.tm_hour, date.tm_min, date.tm_sec);
1898 if( schedule->i_period != 0 )
1900 p += sprintf( p, "setup %s ", schedule->psz_name );
1902 i_time = (time_t) ( schedule->i_period / 1000000 );
1904 date.tm_sec = (int)( i_time % 60 );
1905 i_time = i_time / 60;
1906 date.tm_min = (int)( i_time % 60 );
1907 i_time = i_time / 60;
1908 date.tm_hour = (int)( i_time % 24 );
1909 i_time = i_time / 24;
1910 date.tm_mday = (int)( i_time % 30 );
1911 i_time = i_time / 30;
1912 /* okay, okay, months are not always 30 days long */
1913 date.tm_mon = (int)( i_time % 12 );
1914 i_time = i_time / 12;
1915 date.tm_year = (int)i_time;
1917 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
1918 date.tm_year, date.tm_mon, date.tm_mday,
1919 date.tm_hour, date.tm_min, date.tm_sec);
1922 if( schedule->i_repeat >= 0 )
1924 p += sprintf( p, "setup %s repeat %d\n",
1925 schedule->psz_name, schedule->i_repeat );
1929 p += sprintf( p, "\n" );
1932 for( j = 0; j < schedule->i_command; j++ )
1934 p += sprintf( p, "setup %s append %s\n",
1935 schedule->psz_name, schedule->command[j] );
1939 #endif /* UNDER_CE */
1944 /*****************************************************************************
1946 *****************************************************************************/
1947 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
1948 const char *psz_id, int i_query, va_list args )
1950 vlm_t *vlm = (vlm_t *)p_private;
1955 if( !p_private || !p_vod_media )
1956 return VLC_EGENERIC;
1958 vlc_mutex_lock( &vlm->lock );
1961 for( i = 0, id = -1; i < vlm->i_media; i++ )
1963 if( p_vod_media == vlm->media[i]->vod.p_media )
1965 id = vlm->media[i]->cfg.id;
1971 vlc_mutex_unlock( &vlm->lock );
1972 return VLC_EGENERIC;
1977 case VOD_MEDIA_PLAY:
1978 psz = (const char *)va_arg( args, const char * );
1979 i_ret = vlm_ControlInternal( vlm, VLM_START_MEDIA_VOD_INSTANCE, id, psz_id, 0, psz );
1982 case VOD_MEDIA_PAUSE:
1983 i_ret = vlm_ControlInternal( vlm, VLM_PAUSE_MEDIA_INSTANCE, id, psz_id );
1986 case VOD_MEDIA_STOP:
1987 i_ret = vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, id, psz_id );
1990 case VOD_MEDIA_SEEK:
1992 double d_position = (double)va_arg( args, double );
1993 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position/100.0 );
1997 case VOD_MEDIA_REWIND:
1999 double d_scale = (double)va_arg( args, double );
2002 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2003 d_position -= (d_scale / 1000.0);
2004 if( d_position < 0.0 )
2006 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2010 case VOD_MEDIA_FORWARD:
2012 double d_scale = (double)va_arg( args, double );
2015 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2016 d_position += (d_scale / 1000.0);
2017 if( d_position > 1.0 )
2019 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2024 i_ret = VLC_EGENERIC;
2028 vlc_mutex_unlock( &vlm->lock );
2034 /*****************************************************************************
2036 *****************************************************************************/
2037 static int Manage( vlc_object_t* p_object )
2039 vlm_t *vlm = (vlm_t*)p_object;
2041 mtime_t i_lastcheck;
2044 i_lastcheck = vlm_Date();
2046 while( !vlm->b_die )
2048 char **ppsz_scheduled_commands = NULL;
2049 int i_scheduled_commands = 0;
2051 vlc_mutex_lock( &vlm->lock );
2053 /* destroy the inputs that wants to die, and launch the next input */
2054 for( i = 0; i < vlm->i_media; i++ )
2056 vlm_media_sys_t *p_media = vlm->media[i];
2058 for( j = 0; j < p_media->i_instance; )
2060 vlm_media_instance_sys_t *p_instance = p_media->instance[j];
2062 if( p_instance->p_input && ( p_instance->p_input->b_eof || p_instance->p_input->b_error ) )
2064 int i_new_input_index;
2067 i_new_input_index = p_instance->i_index + 1;
2068 if( !p_media->cfg.b_vod && p_media->cfg.broadcast.b_loop && i_new_input_index >= p_media->cfg.i_input )
2069 i_new_input_index = 0;
2071 /* FIXME implement multiple input with VOD */
2072 if( p_media->cfg.b_vod || i_new_input_index >= p_media->cfg.i_input )
2073 vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, p_instance->psz_name );
2075 vlm_ControlInternal( vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, p_instance->psz_name );
2087 i_time = vlm_Date();
2089 for( i = 0; i < vlm->i_schedule; i++ )
2091 mtime_t i_real_date = vlm->schedule[i]->i_date;
2093 if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2095 if( vlm->schedule[i]->i_date == 0 ) // now !
2097 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2098 i_real_date = i_time;
2100 else if( vlm->schedule[i]->i_period != 0 )
2103 while( vlm->schedule[i]->i_date + j *
2104 vlm->schedule[i]->i_period <= i_lastcheck &&
2105 ( vlm->schedule[i]->i_repeat > j ||
2106 vlm->schedule[i]->i_repeat == -1 ) )
2111 i_real_date = vlm->schedule[i]->i_date + j *
2112 vlm->schedule[i]->i_period;
2115 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2117 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2119 TAB_APPEND( i_scheduled_commands,
2120 ppsz_scheduled_commands,
2121 strdup(vlm->schedule[i]->command[j] ) );
2126 while( i_scheduled_commands )
2128 vlm_message_t *message = NULL;
2129 char *psz_command = ppsz_scheduled_commands[0];
2130 ExecuteCommand( vlm, psz_command,&message );
2132 /* for now, drop the message */
2133 vlm_MessageDelete( message );
2134 TAB_REMOVE( i_scheduled_commands,
2135 ppsz_scheduled_commands,
2137 free( psz_command );
2140 i_lastcheck = i_time;
2142 vlc_mutex_unlock( &vlm->lock );
2157 int i_connection_count;
2158 int i_connection_active;
2163 vlc_bool_t b_playing;
2164 int i_playing_index;
2167 } vlm_media_status_t;
2171 static vlm_media_sys_t *vlm_ControlMediaGetById( vlm_t *p_vlm, int64_t id )
2175 for( i = 0; i < p_vlm->i_media; i++ )
2177 if( p_vlm->media[i]->cfg.id == id )
2178 return p_vlm->media[i];
2182 static vlm_media_sys_t *vlm_ControlMediaGetByName( vlm_t *p_vlm, const char *psz_name )
2186 for( i = 0; i < p_vlm->i_media; i++ )
2188 if( !strcmp( p_vlm->media[i]->cfg.psz_name, psz_name ) )
2189 return p_vlm->media[i];
2193 static int vlm_MediaDescriptionCheck( vlm_t *p_vlm, vlm_media_t *p_cfg )
2197 if( !p_cfg || !p_cfg->psz_name ||
2198 !strcmp( p_cfg->psz_name, "all" ) || !strcmp( p_cfg->psz_name, "media" ) || !strcmp( p_cfg->psz_name, "schedule" ) )
2199 return VLC_EGENERIC;
2201 for( i = 0; i < p_vlm->i_media; i++ )
2203 if( p_vlm->media[i]->cfg.id == p_cfg->id )
2205 if( !strcmp( p_vlm->media[i]->cfg.psz_name, p_cfg->psz_name ) )
2206 return VLC_EGENERIC;
2212 /* Called after a media description is changed/added */
2213 static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
2215 vlm_media_t *p_cfg = &p_media->cfg;
2216 /* Check if we need to create/delete a vod media */
2219 if( !p_cfg->b_enabled && p_media->vod.p_media )
2221 p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
2222 p_media->vod.p_media = NULL;
2224 else if( p_cfg->b_enabled && !p_media->vod.p_media && p_cfg->i_input )
2226 /* Pre-parse the input */
2227 input_thread_t *p_input;
2233 input_ItemClean( &p_media->vod.item );
2234 input_ItemInit( VLC_OBJECT(p_vlm), &p_media->vod.item );
2236 if( p_cfg->psz_output )
2237 asprintf( &psz_output, "%s:description", p_cfg->psz_output );
2239 asprintf( &psz_output, "#description" );
2241 p_media->vod.item.psz_uri = strdup( p_cfg->ppsz_input[0] );
2243 TAB_INIT( p_media->vod.item.i_options, p_media->vod.item.ppsz_options );
2245 asprintf( &psz_dup, "sout=%s", psz_output);
2246 TAB_APPEND( p_media->vod.item.i_options, p_media->vod.item.ppsz_options, psz_dup );
2247 for( i = 0; i < p_cfg->i_option; i++ )
2249 psz_dup = strdup( p_cfg->ppsz_option[i] );
2250 TAB_APPEND( p_media->vod.item.i_options, p_media->vod.item.ppsz_options, psz_dup );
2252 psz_dup = strdup( "no-sout-keep" );
2253 TAB_APPEND( p_media->vod.item.i_options, p_media->vod.item.ppsz_options, psz_dup );
2255 asprintf( &psz_header, _("Media: %s"), p_cfg->psz_name );
2257 if( (p_input = input_CreateThreadExtended( p_vlm, &p_media->vod.item, psz_header, NULL ) ) )
2259 while( !p_input->b_eof && !p_input->b_error )
2262 input_StopThread( p_input );
2263 input_DestroyThreadExtended( p_input, NULL );
2268 if( p_cfg->vod.psz_mux )
2271 es_format_t es, *p_es = &es;
2274 sprintf( fourcc, "%4.4s", p_cfg->vod.psz_mux );
2275 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
2276 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
2278 item = p_media->vod.item;
2281 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
2283 p_media->vod.p_media =
2284 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
2288 p_media->vod.p_media =
2289 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &p_media->vod.item );
2295 /* TODO start media if needed */
2298 /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
2302 static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
2304 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
2307 if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
2308 return VLC_EGENERIC;
2309 if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
2310 return VLC_EGENERIC;
2314 /* TODO check what are the changes being done (stop instance if needed) */
2317 vlm_media_Clean( &p_media->cfg );
2318 vlm_media_Copy( &p_media->cfg, p_cfg );
2320 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_destroy( 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;
2352 p_media = malloc( sizeof( vlm_media_sys_t ) );
2353 memset( p_media, 0, sizeof(vlm_media_sys_t) );
2355 vlm_media_Copy( &p_media->cfg, p_cfg );
2356 p_media->cfg.id = p_vlm->i_id++;
2357 /* FIXME do we do something here if enabled is true ? */
2359 input_ItemInit( VLC_OBJECT(p_vlm), &p_media->vod.item );
2361 p_media->vod.p_media = NULL;
2362 TAB_INIT( p_media->i_instance, p_media->instance );
2365 TAB_APPEND( p_vlm->i_media, p_vlm->media, p_media );
2368 *p_id = p_media->cfg.id;
2370 return vlm_OnMediaUpdate( p_vlm, p_media );
2373 static int vlm_ControlMediaDel( vlm_t *p_vlm, int64_t id )
2375 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2378 return VLC_EGENERIC;
2380 while( p_media->i_instance > 0 )
2381 vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, p_media->instance[0]->psz_name );
2383 if( p_media->cfg.b_vod )
2385 p_media->cfg.b_enabled = VLC_FALSE;
2386 vlm_OnMediaUpdate( p_vlm, p_media );
2390 vlm_media_Clean( &p_media->cfg );
2392 input_ItemClean( &p_media->vod.item );
2394 TAB_REMOVE( p_vlm->i_media, p_vlm->media, p_media );
2398 /* Check if we need to unload the VOD server */
2399 if( p_vlm->p_vod && p_vlm->i_vod <= 0 )
2401 module_Unneed( p_vlm->p_vod, p_vlm->p_vod->p_module );
2402 vlc_object_detach( p_vlm->p_vod );
2403 vlc_object_destroy( p_vlm->p_vod );
2404 p_vlm->p_vod = NULL;
2409 static int vlm_ControlMediaGets( vlm_t *p_vlm, vlm_media_t ***ppp_dsc, int *pi_dsc )
2411 vlm_media_t **pp_dsc;
2415 TAB_INIT( i_dsc, pp_dsc );
2416 for( i = 0; i < p_vlm->i_media; i++ )
2418 vlm_media_t *p_dsc = vlm_media_Duplicate( &p_vlm->media[i]->cfg );
2419 TAB_APPEND( i_dsc, pp_dsc, p_dsc );
2427 static int vlm_ControlMediaClear( vlm_t *p_vlm )
2429 while( p_vlm->i_media > 0 )
2430 vlm_ControlMediaDel( p_vlm, p_vlm->media[0]->cfg.id );
2434 static int vlm_ControlMediaGet( vlm_t *p_vlm, int64_t id, vlm_media_t **pp_dsc )
2436 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2438 return VLC_EGENERIC;
2440 *pp_dsc = vlm_media_Duplicate( &p_media->cfg );
2443 static int vlm_ControlMediaGetId( vlm_t *p_vlm, const char *psz_name, int64_t *p_id )
2445 vlm_media_sys_t *p_media = vlm_ControlMediaGetByName( p_vlm, psz_name );
2447 return VLC_EGENERIC;
2449 *p_id = p_media->cfg.id;
2453 static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
2457 for( i = 0; i < p_media->i_instance; i++ )
2459 const char *psz = p_media->instance[i]->psz_name;
2460 if( ( psz == NULL && psz_id == NULL ) ||
2461 ( psz && psz_id && !strcmp( psz, psz_id ) ) )
2462 return p_media->instance[i];
2466 static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
2468 vlm_media_instance_sys_t *p_instance = malloc( sizeof(vlm_media_instance_sys_t) );
2472 memset( p_instance, 0, sizeof(vlm_media_instance_sys_t) );
2474 p_instance->psz_name = NULL;
2476 p_instance->psz_name = strdup( psz_name );
2478 input_ItemInit( VLC_OBJECT(p_vlm), &p_instance->item );
2480 p_instance->i_index = 0;
2481 p_instance->b_sout_keep = VLC_FALSE;
2482 p_instance->p_input = NULL;
2483 p_instance->p_sout = NULL;
2487 static void vlm_MediaInstanceDelete( vlm_media_instance_sys_t *p_instance )
2489 if( p_instance->p_input )
2491 input_StopThread( p_instance->p_input );
2492 input_DestroyThreadExtended( p_instance->p_input, &p_instance->p_sout );
2494 if( p_instance->p_sout )
2495 sout_DeleteInstance( p_instance->p_sout );
2497 input_ItemClean( &p_instance->item );
2498 if( p_instance->psz_name )
2499 free( p_instance->psz_name );
2504 static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
2506 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2507 vlm_media_instance_sys_t *p_instance;
2510 if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
2511 return VLC_EGENERIC;
2513 /* TODO support multiple input for VOD with sout-keep ? */
2515 if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
2516 return VLC_EGENERIC;
2518 if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
2519 return VLC_EGENERIC;
2521 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2524 vlm_media_t *p_cfg = &p_media->cfg;
2528 p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
2532 TAB_INIT( p_instance->item.i_options, p_instance->item.ppsz_options );
2534 if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
2537 asprintf( &psz_buffer, "sout=%s%s%s",
2538 p_cfg->psz_output ? p_cfg->psz_output : "",
2539 (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
2540 psz_vod_output ? psz_vod_output : "" );
2541 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, psz_buffer );
2544 for( i = 0; i < p_cfg->i_option; i++ )
2546 if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
2547 p_instance->b_sout_keep = VLC_TRUE;
2548 else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
2549 p_instance->b_sout_keep = VLC_FALSE;
2551 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, strdup( p_cfg->ppsz_option[i] ) );
2553 /* We force the right sout-keep value (avoid using the sout-keep from the global configuration)
2554 * FIXME implement input list for VOD (need sout-keep)
2556 if( !p_cfg->b_vod && p_instance->b_sout_keep )
2557 psz_keep = strdup( "sout-keep" );
2559 psz_keep = strdup( "no-sout-keep" );
2560 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, psz_keep );
2562 TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
2565 /* Stop old instance */
2566 if( p_instance->p_input )
2568 if( p_instance->i_index == i_input_index &&
2569 !p_instance->p_input->b_eof && !p_instance->p_input->b_error )
2571 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2572 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2576 input_StopThread( p_instance->p_input );
2577 input_DestroyThreadExtended( p_instance->p_input, &p_instance->p_sout );
2578 if( !p_instance->b_sout_keep && p_instance->p_sout )
2580 sout_DeleteInstance( p_instance->p_sout );
2581 p_instance->p_sout = NULL;
2586 p_instance->i_index = i_input_index;
2587 if( p_instance->item.psz_uri )
2588 free( p_instance->item.psz_uri );
2589 p_instance->item.psz_uri = strdup( p_media->cfg.ppsz_input[p_instance->i_index] );
2591 asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name );
2592 p_instance->p_input = input_CreateThreadExtended( p_vlm, &p_instance->item, psz_log, p_instance->p_sout );
2593 if( !p_instance->p_input )
2595 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2596 vlm_MediaInstanceDelete( p_instance );
2603 static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
2605 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2606 vlm_media_instance_sys_t *p_instance;
2609 return VLC_EGENERIC;
2611 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2613 return VLC_EGENERIC;
2615 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2617 vlm_MediaInstanceDelete( p_instance );
2621 static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
2623 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2624 vlm_media_instance_sys_t *p_instance;
2628 return VLC_EGENERIC;
2630 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2631 if( !p_instance || !p_instance->p_input )
2632 return VLC_EGENERIC;
2634 /* Toggle pause state */
2635 i_state = var_GetInteger( p_instance->p_input, "state" );
2636 if( i_state == PAUSE_S )
2637 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2638 else if( i_state == PLAYING_S )
2639 var_SetInteger( p_instance->p_input, "state", PAUSE_S );
2642 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
2644 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2645 vlm_media_instance_sys_t *p_instance;
2648 return VLC_EGENERIC;
2650 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2651 if( !p_instance || !p_instance->p_input )
2652 return VLC_EGENERIC;
2655 *pi_time = var_GetTime( p_instance->p_input, "time" );
2657 *pd_position = var_GetFloat( p_instance->p_input, "position" );
2660 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t i_time, double d_position )
2662 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2663 vlm_media_instance_sys_t *p_instance;
2666 return VLC_EGENERIC;
2668 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2669 if( !p_instance || !p_instance->p_input )
2670 return VLC_EGENERIC;
2673 return var_SetTime( p_instance->p_input, "time", i_time );
2674 else if( d_position >= 0 && d_position <= 100 )
2675 return var_SetFloat( p_instance->p_input, "position", d_position );
2676 return VLC_EGENERIC;
2679 static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_instance_t ***ppp_idsc, int *pi_instance )
2681 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2682 vlm_media_instance_t **pp_idsc;
2687 return VLC_EGENERIC;
2689 TAB_INIT( i_idsc, pp_idsc );
2690 for( i = 0; i < p_media->i_instance; i++ )
2692 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
2693 vlm_media_instance_t *p_idsc = vlm_media_instance_New();
2695 if( p_instance->psz_name )
2696 p_idsc->psz_name = strdup( p_instance->psz_name );
2697 if( p_instance->p_input )
2699 p_idsc->i_time = var_GetTime( p_instance->p_input, "time" );
2700 p_idsc->i_length = var_GetTime( p_instance->p_input, "length" );
2701 p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
2702 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2703 p_idsc->b_paused = VLC_TRUE;
2704 p_idsc->i_rate = var_GetInteger( p_instance->p_input, "rate" );
2707 TAB_APPEND( i_idsc, pp_idsc, p_idsc );
2709 *ppp_idsc = pp_idsc;
2710 *pi_instance = i_idsc;
2713 static int vlm_ControlMediaInstanceClear( vlm_t *p_vlm, int64_t id )
2715 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2718 return VLC_EGENERIC;
2720 while( p_media->i_instance > 0 )
2721 vlm_ControlMediaInstanceStop( p_vlm, id, p_media->instance[0]->psz_name );
2726 static int vlm_ControlScheduleClear( vlm_t *p_vlm )
2728 while( p_vlm->i_schedule > 0 )
2729 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
2733 static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
2736 vlm_media_t **pp_dsc;
2737 vlm_media_t ***ppp_dsc;
2738 vlm_media_instance_t ***ppp_idsc;
2740 const char *psz_vod;
2754 case VLM_GET_MEDIAS:
2755 ppp_dsc = (vlm_media_t ***)va_arg( args, vlm_media_t *** );
2756 pi_int = (int *)va_arg( args, int * );
2757 return vlm_ControlMediaGets( p_vlm, ppp_dsc, pi_int );
2759 case VLM_CLEAR_MEDIAS:
2760 return vlm_ControlMediaClear( p_vlm );
2762 case VLM_CHANGE_MEDIA:
2763 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2764 return vlm_ControlMediaChange( p_vlm, p_dsc );
2767 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2768 p_id = (int64_t*)va_arg( args, int64_t * );
2769 return vlm_ControlMediaAdd( p_vlm, p_dsc, p_id );
2772 id = (int64_t)va_arg( args, int64_t );
2773 return vlm_ControlMediaDel( p_vlm, id );
2776 id = (int64_t)va_arg( args, int64_t );
2777 pp_dsc = (vlm_media_t **)va_arg( args, vlm_media_t ** );
2778 return vlm_ControlMediaGet( p_vlm, id, pp_dsc );
2780 case VLM_GET_MEDIA_ID:
2781 psz_id = (const char*)va_arg( args, const char * );
2782 p_id = (int64_t*)va_arg( args, int64_t * );
2783 return vlm_ControlMediaGetId( p_vlm, psz_id, p_id );
2786 /* Media instance control */
2787 case VLM_GET_MEDIA_INSTANCES:
2788 id = (int64_t)va_arg( args, int64_t );
2789 ppp_idsc = (vlm_media_instance_t ***)va_arg( args, vlm_media_instance_t *** );
2790 pi_int = (int *)va_arg( args, int *);
2791 return vlm_ControlMediaInstanceGets( p_vlm, id, ppp_idsc, pi_int );
2793 case VLM_CLEAR_MEDIA_INSTANCES:
2794 id = (int64_t)va_arg( args, int64_t );
2795 return vlm_ControlMediaInstanceClear( p_vlm, id );
2798 case VLM_START_MEDIA_BROADCAST_INSTANCE:
2799 id = (int64_t)va_arg( args, int64_t );
2800 psz_id = (const char*)va_arg( args, const char* );
2801 i_int = (int)va_arg( args, int );
2802 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, NULL );
2804 case VLM_START_MEDIA_VOD_INSTANCE:
2805 id = (int64_t)va_arg( args, int64_t );
2806 psz_id = (const char*)va_arg( args, const char* );
2807 i_int = (int)va_arg( args, int );
2808 psz_vod = (const char*)va_arg( args, const char* );
2810 return VLC_EGENERIC;
2811 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, psz_vod );
2813 case VLM_STOP_MEDIA_INSTANCE:
2814 id = (int64_t)va_arg( args, int64_t );
2815 psz_id = (const char*)va_arg( args, const char* );
2816 return vlm_ControlMediaInstanceStop( p_vlm, id, psz_id );
2818 case VLM_PAUSE_MEDIA_INSTANCE:
2819 id = (int64_t)va_arg( args, int64_t );
2820 psz_id = (const char*)va_arg( args, const char* );
2821 return vlm_ControlMediaInstancePause( p_vlm, id, psz_id );
2823 case VLM_GET_MEDIA_INSTANCE_TIME:
2824 id = (int64_t)va_arg( args, int64_t );
2825 psz_id = (const char*)va_arg( args, const char* );
2826 pi_i64 = (int64_t*)va_arg( args, int64_t * );
2827 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, pi_i64, NULL );
2828 case VLM_GET_MEDIA_INSTANCE_POSITION:
2829 id = (int64_t)va_arg( args, int64_t );
2830 psz_id = (const char*)va_arg( args, const char* );
2831 pd_double = (double*)va_arg( args, double* );
2832 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, NULL, pd_double );
2834 case VLM_SET_MEDIA_INSTANCE_TIME:
2835 id = (int64_t)va_arg( args, int64_t );
2836 psz_id = (const char*)va_arg( args, const char* );
2837 i_i64 = (int64_t)va_arg( args, int64_t);
2838 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, i_i64, -1 );
2839 case VLM_SET_MEDIA_INSTANCE_POSITION:
2840 id = (int64_t)va_arg( args, int64_t );
2841 psz_id = (const char*)va_arg( args, const char* );
2842 d_double = (double)va_arg( args, double );
2843 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, -1, d_double );
2845 case VLM_CLEAR_SCHEDULES:
2846 return vlm_ControlScheduleClear( p_vlm );
2849 msg_Err( p_vlm, "unknown VLM query" );
2850 return VLC_EGENERIC;
2853 static int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... )
2858 va_start( args, i_query );
2859 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2865 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2870 va_start( args, i_query );
2872 vlc_mutex_lock( &p_vlm->lock );
2873 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2874 vlc_mutex_unlock( &p_vlm->lock );
2881 #else /* ENABLE_VLM */
2883 /* We just define an empty wrapper */
2884 vlm_t *__vlm_New( vlc_object_t *a )
2886 msg_Err( a, "VideoLAN manager support is disabled" );
2889 void vlm_Delete( vlm_t *a )
2893 int vlm_ExecuteCommand( vlm_t *a, char *b, vlm_message_t **c )
2897 vlm_message_t *vlm_MessageNew( const char *psz_name,
2898 const char *psz_format, ... )
2902 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
2903 vlm_message_t *p_child )
2907 void vlm_MessageDelete( vlm_message_t *a )
2912 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2914 return VLC_EGENERIC;
2917 #endif /* ENABLE_VLM */