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 vlc_object_kill( p_vlm );
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 )
471 else if( psz_argument )
474 vlm_media_t *p_cfg = &p_media->cfg;
475 for ( j=0; j < p_cfg->i_input; j++)
477 if( !strcmp( p_cfg->ppsz_input[j], psz_argument ) )
485 if( p_media->cfg.b_vod )
486 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
488 i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, psz_instance, i_input_index );
490 else if( !strcmp( psz_control, "seek" ) )
494 vlc_bool_t b_relative;
495 if( psz_argument[0] == '+' || psz_argument[0] == '-' )
496 b_relative = VLC_TRUE;
498 b_relative = VLC_FALSE;
500 if( strstr( psz_argument, "ms" ) || strstr( psz_argument, "s" ) )
505 if( strstr( psz_argument, "ms" ) )
506 i_new_time = 1000 * (int64_t)atoi( psz_argument );
508 i_new_time = 1000000 * (int64_t)atoi( psz_argument );
513 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, &i_time );
514 i_new_time += i_time;
518 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, i_new_time );
523 double d_new_position = i18n_atof( psz_argument ) / 100.0;
527 double d_position = 0.0;
529 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
530 d_new_position += d_position;
532 if( d_new_position < 0.0 )
533 d_new_position = 0.0;
534 else if( d_new_position > 1.0 )
535 d_new_position = 1.0;
536 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_new_position );
541 i_result = VLC_EGENERIC;
544 else if( !strcmp( psz_control, "rewind" ) )
548 const double d_scale = i18n_atof( psz_argument );
551 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
552 d_position -= (d_scale / 1000.0);
553 if( d_position < 0.0 )
555 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
559 i_result = VLC_EGENERIC;
562 else if( !strcmp( psz_control, "forward" ) )
566 const double d_scale = i18n_atof( psz_argument );
569 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
570 d_position += (d_scale / 1000.0);
571 if( d_position > 1.0 )
573 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
578 i_result = VLC_EGENERIC;
581 else if( !strcmp( psz_control, "stop" ) )
583 i_result = vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
585 else if( !strcmp( psz_control, "pause" ) )
587 i_result = vlm_ControlInternal( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
591 i_result = VLC_EGENERIC;
596 *pp_status = vlm_MessageNew( "control", "unknown error" );
599 *pp_status = vlm_MessageNew( "control", NULL );
603 static int ExecuteExport( vlm_t *p_vlm, vlm_message_t **pp_status )
605 char *psz_export = Save( p_vlm );
607 *pp_status = vlm_MessageNew( "export", psz_export );
612 static int ExecuteSave( vlm_t *p_vlm, const char *psz_file, vlm_message_t **pp_status )
614 FILE *f = utf8_fopen( psz_file, "wt" );
620 psz_save = Save( p_vlm );
621 if( psz_save == NULL )
626 fwrite( psz_save, strlen( psz_save ), 1, f );
630 *pp_status = vlm_MessageNew( "save", NULL );
634 *pp_status = vlm_MessageNew( "save", "Unable to save to file" );
638 static int ExecuteLoad( vlm_t *p_vlm, const char *psz_url, vlm_message_t **pp_status )
640 stream_t *p_stream = stream_UrlNew( p_vlm, psz_url );
646 *pp_status = vlm_MessageNew( "load", "Unable to load from file" );
651 if( stream_Seek( p_stream, 0 ) != 0 )
653 stream_Delete( p_stream );
655 *pp_status = vlm_MessageNew( "load", "Read file error" );
659 i_size = stream_Size( p_stream );
661 psz_buffer = malloc( i_size + 1 );
664 stream_Delete( p_stream );
666 *pp_status = vlm_MessageNew( "load", "Read file error" );
670 stream_Read( p_stream, psz_buffer, i_size );
671 psz_buffer[i_size] = '\0';
673 stream_Delete( p_stream );
675 if( Load( p_vlm, psz_buffer ) )
679 *pp_status = vlm_MessageNew( "load", "Error while loading file" );
685 *pp_status = vlm_MessageNew( "load", NULL );
689 static int ExecuteScheduleProperty( vlm_t *p_vlm, vlm_schedule_sys_t *p_schedule, vlc_bool_t b_new,
690 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
692 const char *psz_cmd = b_new ? "new" : "setup";
695 for( i = 0; i < i_property; i++ )
697 if( !strcmp( ppsz_property[i], "enabled" ) ||
698 !strcmp( ppsz_property[i], "disabled" ) )
700 vlm_ScheduleSetup( p_schedule, ppsz_property[i], NULL );
702 else if( !strcmp( ppsz_property[i], "append" ) )
706 /* Beware: everything behind append is considered as
709 if( ++i >= i_property )
712 psz_line = strdup( ppsz_property[i] );
713 for( j = i+1; j < i_property; j++ )
715 psz_line = realloc( psz_line, strlen(psz_line) + strlen(ppsz_property[j]) + 1 + 1 );
716 strcat( psz_line, " " );
717 strcat( psz_line, ppsz_property[j] );
720 vlm_ScheduleSetup( p_schedule, "append", psz_line );
725 if( i + 1 >= i_property )
728 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
729 return ExecuteSyntaxError( psz_cmd, pp_status );
732 vlm_ScheduleSetup( p_schedule, ppsz_property[i], ppsz_property[i+1] );
736 *pp_status = vlm_MessageNew( psz_cmd, NULL );
740 static int ExecuteMediaProperty( vlm_t *p_vlm, int64_t id, vlc_bool_t b_new,
741 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
743 const char *psz_cmd = b_new ? "new" : "setup";
744 vlm_media_t *p_cfg = NULL;
748 #define ERROR( txt ) do { *pp_status = vlm_MessageNew( psz_cmd, txt); goto error; } while(0)
749 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA, id, &p_cfg ) )
750 ERROR( "unknown media" );
752 #define MISSING(cmd) do { if( !psz_value ) ERROR( "missing argument for " cmd ); } while(0)
753 for( i = 0; i < i_property; i++ )
755 const char *psz_option = ppsz_property[i];
756 const char *psz_value = i+1 < i_property ? ppsz_property[i+1] : NULL;
758 if( !strcmp( psz_option, "enabled" ) )
760 p_cfg->b_enabled = VLC_TRUE;
762 else if( !strcmp( psz_option, "disabled" ) )
764 p_cfg->b_enabled = VLC_FALSE;
766 else if( !strcmp( psz_option, "input" ) )
769 TAB_APPEND( p_cfg->i_input, p_cfg->ppsz_input, strdup(psz_value) );
772 else if( !strcmp( psz_option, "inputdel" ) && psz_value && !strcmp( psz_value, "all" ) )
774 while( p_cfg->i_input > 0 )
775 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[0] );
778 else if( !strcmp( psz_option, "inputdel" ) )
782 MISSING( "inputdel" );
784 for( j = 0; j < p_cfg->i_input; j++ )
786 if( !strcmp( p_cfg->ppsz_input[j], psz_value ) )
788 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[j] );
794 else if( !strcmp( psz_option, "inputdeln" ) )
798 MISSING( "inputdeln" );
800 i_index = atoi( psz_value );
801 if( i_index > 0 && i_index <= p_cfg->i_input )
802 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[i_index-1] );
805 else if( !strcmp( psz_option, "output" ) )
809 if( p_cfg->psz_output != NULL )
810 free( p_cfg->psz_output );
811 p_cfg->psz_output = *psz_value ? strdup( psz_value ) : NULL;
814 else if( !strcmp( psz_option, "option" ) )
818 TAB_APPEND( p_cfg->i_option, p_cfg->ppsz_option, strdup( psz_value ) );
821 else if( !strcmp( psz_option, "loop" ) )
824 ERROR( "invalid loop option for vod" );
825 p_cfg->broadcast.b_loop = VLC_TRUE;
827 else if( !strcmp( psz_option, "unloop" ) )
830 ERROR( "invalid unloop option for vod" );
831 p_cfg->broadcast.b_loop = VLC_FALSE;
833 else if( !strcmp( psz_option, "mux" ) )
837 ERROR( "invalid mux option for broadcast" );
839 if( p_cfg->vod.psz_mux )
840 free( p_cfg->vod.psz_mux );
841 p_cfg->vod.psz_mux = *psz_value ? strdup( psz_value ) : NULL;
846 fprintf( stderr, "PROP: name=%s unknown\n", psz_option );
847 ERROR( "Wrong command syntax" );
854 i_result = vlm_ControlInternal( p_vlm, VLM_CHANGE_MEDIA, p_cfg );
855 vlm_media_Delete( p_cfg );
857 *pp_status = vlm_MessageNew( psz_cmd, NULL );
864 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_cfg->id );
865 vlm_media_Delete( p_cfg );
869 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 )
872 if( !strcmp( psz_name, "all" ) || !strcmp( psz_name, "media" ) || !strcmp( psz_name, "schedule" ) )
874 *pp_status = vlm_MessageNew( "new", "\"all\", \"media\" and \"schedule\" are reserved names" );
877 if( ExecuteIsMedia( p_vlm, psz_name ) || ExecuteIsSchedule( p_vlm, psz_name ) )
879 *pp_status = vlm_MessageNew( "new", "%s: Name already in use", psz_name );
883 if( !strcmp( psz_type, "schedule" ) )
885 vlm_schedule_sys_t *p_schedule = vlm_ScheduleNew( p_vlm, psz_name );
888 *pp_status = vlm_MessageNew( "new", "could not create schedule" );
891 return ExecuteScheduleProperty( p_vlm, p_schedule, VLC_TRUE, i_property, ppsz_property, pp_status );
893 else if( !strcmp( psz_type, "vod" ) || !strcmp( psz_type, "broadcast" ) )
898 vlm_media_Init( &cfg );
899 cfg.psz_name = strdup( psz_name );
900 cfg.b_vod = !strcmp( psz_type, "vod" );
902 if( vlm_ControlInternal( p_vlm, VLM_ADD_MEDIA, &cfg, &id ) )
904 vlm_media_Clean( &cfg );
905 *pp_status = vlm_MessageNew( "new", "could not create media" );
908 vlm_media_Clean( &cfg );
909 return ExecuteMediaProperty( p_vlm, id, VLC_TRUE, i_property, ppsz_property, pp_status );
913 *pp_status = vlm_MessageNew( "new", "%s: Choose between vod, broadcast or schedule", psz_type );
918 static int ExecuteSetup( vlm_t *p_vlm, const char *psz_name, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
920 if( ExecuteIsSchedule( p_vlm, psz_name ) )
922 vlm_schedule_sys_t *p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
923 return ExecuteScheduleProperty( p_vlm, p_schedule, VLC_FALSE, i_property, ppsz_property, pp_status );
925 else if( ExecuteIsMedia( p_vlm, psz_name ) )
928 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
930 return ExecuteMediaProperty( p_vlm, id, VLC_FALSE, i_property, ppsz_property, pp_status );
934 *pp_status = vlm_MessageNew( "setup", "%s unknown", psz_name );
938 static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
939 vlm_message_t **pp_message )
941 size_t i_command = 0;
942 char buf[strlen (psz_command) + 1], *psz_buf = buf;
943 char *ppsz_command[3+sizeof (buf) / 2];
944 vlm_message_t *p_message = NULL;
946 /* First, parse the line and cut it */
947 while( *psz_command != '\0' )
949 const char *psz_temp;
951 if(isspace (*psz_command))
957 /* support for comments */
958 if( i_command == 0 && *psz_command == '#')
960 p_message = vlm_MessageNew( "", NULL );
964 psz_temp = FindCommandEnd( psz_command );
966 if( psz_temp == NULL )
968 p_message = vlm_MessageNew( "Incomplete command", psz_command );
972 assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
974 ppsz_command[i_command] = psz_buf;
975 memcpy (psz_buf, psz_command, psz_temp - psz_command);
976 psz_buf[psz_temp - psz_command] = '\0';
978 Unescape (psz_buf, psz_buf);
981 psz_buf += psz_temp - psz_command + 1;
982 psz_command = psz_temp;
984 assert (buf + sizeof (buf) >= psz_buf);
988 * And then Interpret it
991 #define IF_EXECUTE( name, check, cmd ) if( !strcmp(ppsz_command[0], name ) ) { if( (check) ) goto syntax_error; if( (cmd) ) goto error; goto success; }
994 p_message = vlm_MessageNew( "", NULL );
997 else IF_EXECUTE( "del", (i_command != 2), ExecuteDel(p_vlm, ppsz_command[1], &p_message) )
998 else IF_EXECUTE( "show", (i_command > 2), ExecuteShow(p_vlm, i_command > 1 ? ppsz_command[1] : NULL, &p_message) )
999 else IF_EXECUTE( "help", (i_command != 1), ExecuteHelp(p_vlm, &p_message) )
1000 else IF_EXECUTE( "control", (i_command < 3), ExecuteControl(p_vlm, ppsz_command[1], i_command - 2, &ppsz_command[2], &p_message) )
1001 else IF_EXECUTE( "save", (i_command != 2), ExecuteSave(p_vlm, ppsz_command[1], &p_message) )
1002 else IF_EXECUTE( "export", (i_command != 1), ExecuteExport(p_vlm, &p_message) )
1003 else IF_EXECUTE( "load", (i_command != 2), ExecuteLoad(p_vlm, ppsz_command[1], &p_message) )
1004 else IF_EXECUTE( "new", (i_command < 3), ExecuteNew(p_vlm, ppsz_command[1], ppsz_command[2], i_command-3, &ppsz_command[3], &p_message) )
1005 else IF_EXECUTE( "setup", (i_command < 2), ExecuteSetup(p_vlm, ppsz_command[1], i_command-2, &ppsz_command[2], &p_message) )
1008 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
1014 *pp_message = p_message;
1018 return ExecuteSyntaxError( ppsz_command[0], pp_message );
1021 *pp_message = p_message;
1022 return VLC_EGENERIC;
1025 /*****************************************************************************
1027 *****************************************************************************/
1028 vlm_media_sys_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
1032 for( i = 0; i < vlm->i_media; i++ )
1034 if( strcmp( psz_name, vlm->media[i]->cfg.psz_name ) == 0 )
1035 return vlm->media[i];
1041 /*****************************************************************************
1043 *****************************************************************************/
1044 static int64_t vlm_Date(void)
1049 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1055 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
1057 vlm_schedule_sys_t *p_sched = malloc( sizeof( vlm_schedule_sys_t ) );
1069 p_sched->psz_name = strdup( psz_name );
1070 p_sched->b_enabled = VLC_FALSE;
1071 p_sched->i_command = 0;
1072 p_sched->command = NULL;
1073 p_sched->i_date = 0;
1074 p_sched->i_period = 0;
1075 p_sched->i_repeat = -1;
1077 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1082 /* for now, simple delete. After, del with options (last arg) */
1083 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched,
1084 const char *psz_name )
1086 if( sched == NULL ) return;
1088 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1090 if( vlm->i_schedule == 0 && vlm->schedule ) free( vlm->schedule );
1091 free( sched->psz_name );
1092 while( sched->i_command )
1094 char *psz_cmd = sched->command[0];
1095 TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1101 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1105 for( i = 0; i < vlm->i_schedule; i++ )
1107 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1109 return vlm->schedule[i];
1116 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1117 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
1118 const char *psz_value )
1120 if( !strcmp( psz_cmd, "enabled" ) )
1122 schedule->b_enabled = VLC_TRUE;
1124 else if( !strcmp( psz_cmd, "disabled" ) )
1126 schedule->b_enabled = VLC_FALSE;
1128 #if !defined( UNDER_CE )
1129 else if( !strcmp( psz_cmd, "date" ) )
1135 time.tm_sec = 0; /* seconds */
1136 time.tm_min = 0; /* minutes */
1137 time.tm_hour = 0; /* hours */
1138 time.tm_mday = 0; /* day of the month */
1139 time.tm_mon = 0; /* month */
1140 time.tm_year = 0; /* year */
1141 time.tm_wday = 0; /* day of the week */
1142 time.tm_yday = 0; /* day in the year */
1143 time.tm_isdst = -1; /* daylight saving time */
1145 /* date should be year/month/day-hour:minutes:seconds */
1146 p = strchr( psz_value, '-' );
1148 if( !strcmp( psz_value, "now" ) )
1150 schedule->i_date = 0;
1152 else if( (p == NULL) && sscanf( psz_value, "%d:%d:%d", &time.tm_hour,
1153 &time.tm_min, &time.tm_sec ) != 3 )
1154 /* it must be a hour:minutes:seconds */
1162 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1180 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1186 time.tm_mon = i - 1;
1190 time.tm_year = i - 1900;
1191 time.tm_mon = j - 1;
1198 date = mktime( &time );
1199 schedule->i_date = ((mtime_t) date) * 1000000;
1202 else if( !strcmp( psz_cmd, "period" ) )
1206 const char *psz_time = NULL, *psz_date = NULL;
1210 /* First, if date or period are modified, repeat should be equal to -1 */
1211 schedule->i_repeat = -1;
1213 time.tm_sec = 0; /* seconds */
1214 time.tm_min = 0; /* minutes */
1215 time.tm_hour = 0; /* hours */
1216 time.tm_mday = 0; /* day of the month */
1217 time.tm_mon = 0; /* month */
1218 time.tm_year = 0; /* year */
1219 time.tm_wday = 0; /* day of the week */
1220 time.tm_yday = 0; /* day in the year */
1221 time.tm_isdst = -1; /* daylight saving time */
1223 /* date should be year/month/day-hour:minutes:seconds */
1224 p = strchr( psz_value, '-' );
1227 psz_date = psz_value;
1232 psz_time = psz_value;
1235 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1254 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1273 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1274 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1275 schedule->i_period = ((mtime_t) date) * 1000000;
1277 #endif /* UNDER_CE */
1278 else if( !strcmp( psz_cmd, "repeat" ) )
1282 if( sscanf( psz_value, "%d", &i ) == 1 )
1284 schedule->i_repeat = i;
1291 else if( !strcmp( psz_cmd, "append" ) )
1293 char *command = strdup( psz_value );
1295 TAB_APPEND( schedule->i_command, schedule->command, command );
1304 /*****************************************************************************
1305 * Message handling functions
1306 *****************************************************************************/
1307 vlm_message_t *vlm_MessageNew( const char *psz_name,
1308 const char *psz_format, ... )
1310 vlm_message_t *p_message;
1313 if( !psz_name ) return NULL;
1315 p_message = malloc( sizeof(vlm_message_t) );
1321 p_message->psz_value = 0;
1325 va_start( args, psz_format );
1326 if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1335 p_message->psz_name = strdup( psz_name );
1336 p_message->i_child = 0;
1337 p_message->child = NULL;
1342 void vlm_MessageDelete( vlm_message_t *p_message )
1344 if( p_message->psz_name ) free( p_message->psz_name );
1345 if( p_message->psz_value ) free( p_message->psz_value );
1346 while( p_message->i_child-- )
1347 vlm_MessageDelete( p_message->child[p_message->i_child] );
1348 if( p_message->child ) free( p_message->child );
1353 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1354 vlm_message_t *p_child )
1356 if( p_message == NULL ) return NULL;
1360 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1366 /*****************************************************************************
1367 * Misc utility functions
1368 *****************************************************************************/
1369 static vlm_message_t *vlm_ShowMedia( vlm_t *p_vlm, vlm_media_sys_t *p_media )
1371 vlm_media_t *p_cfg = &p_media->cfg;
1372 vlm_message_t *p_msg;
1373 vlm_message_t *p_msg_sub;
1376 p_msg = vlm_MessageNew( p_cfg->psz_name, 0 );
1377 vlm_MessageAdd( p_msg,
1378 vlm_MessageNew( "type", p_cfg->b_vod ? "vod" : "broadcast" ) );
1379 vlm_MessageAdd( p_msg,
1380 vlm_MessageNew( "enabled", p_cfg->b_enabled ? "yes" : "no" ) );
1383 vlm_MessageAdd( p_msg,
1384 vlm_MessageNew( "mux", p_cfg->vod.psz_mux ) );
1386 vlm_MessageAdd( p_msg,
1387 vlm_MessageNew( "loop", p_cfg->broadcast.b_loop ? "yes" : "no" ) );
1389 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "inputs", NULL ) );
1390 for( i = 0; i < p_cfg->i_input; i++ )
1393 asprintf( &psz_tmp, "%d", i+1 );
1394 vlm_MessageAdd( p_msg_sub,
1395 vlm_MessageNew( psz_tmp, p_cfg->ppsz_input[i] ) );
1399 vlm_MessageAdd( p_msg,
1400 vlm_MessageNew( "output", p_cfg->psz_output ? p_cfg->psz_output : "" ) );
1402 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "options", 0 ) );
1403 for( i = 0; i < p_cfg->i_option; i++ )
1404 vlm_MessageAdd( p_msg_sub, vlm_MessageNew( p_cfg->ppsz_option[i], 0 ) );
1406 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "instances", NULL ) );
1407 for( i = 0; i < p_media->i_instance; i++ )
1409 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1411 vlm_message_t *p_msg_instance;
1415 if( p_instance->p_input )
1416 var_Get( p_instance->p_input, "state", &val );
1418 p_msg_instance = vlm_MessageAdd( p_msg_sub, vlm_MessageNew( "instance" , NULL ) );
1420 vlm_MessageAdd( p_msg_instance,
1421 vlm_MessageNew( "name" , p_instance->psz_name ? p_instance->psz_name : "default" ) );
1422 vlm_MessageAdd( p_msg_instance,
1423 vlm_MessageNew( "state",
1424 val.i_int == PLAYING_S ? "playing" :
1425 val.i_int == PAUSE_S ? "paused" :
1428 /* FIXME should not do that this way */
1429 if( p_instance->p_input )
1431 #define APPEND_INPUT_INFO( a, format, type ) \
1432 asprintf( &psz_tmp, format, \
1433 var_Get ## type( p_instance->p_input, a ) ); \
1434 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( a, psz_tmp ) ); \
1436 APPEND_INPUT_INFO( "position", "%f", Float );
1437 APPEND_INPUT_INFO( "time", I64Fi, Time );
1438 APPEND_INPUT_INFO( "length", I64Fi, Time );
1439 APPEND_INPUT_INFO( "rate", "%d", Integer );
1440 APPEND_INPUT_INFO( "title", "%d", Integer );
1441 APPEND_INPUT_INFO( "chapter", "%d", Integer );
1442 APPEND_INPUT_INFO( "seekable", "%d", Bool );
1444 #undef APPEND_INPUT_INFO
1445 asprintf( &psz_tmp, "%d", p_instance->i_index + 1 );
1446 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( "playlistindex", psz_tmp ) );
1452 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
1453 vlm_schedule_sys_t *schedule,
1454 const char *psz_filter )
1458 vlm_message_t *p_msg = vlm_MessageNew( "show", NULL );
1460 vlm_MessageAdd( p_msg, vlm_ShowMedia( vlm, media ) );
1464 else if( schedule != NULL )
1468 vlm_message_t *msg_schedule;
1469 vlm_message_t *msg_child;
1472 msg = vlm_MessageNew( "show", NULL );
1474 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, 0 ) );
1476 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1478 vlm_MessageAdd( msg_schedule,
1479 vlm_MessageNew( "enabled", schedule->b_enabled ?
1482 #if !defined( UNDER_CE )
1483 if( schedule->i_date != 0 )
1486 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1489 #ifdef HAVE_LOCALTIME_R
1490 localtime_r( &i_time, &date);
1492 struct tm *p_date = localtime( &i_time );
1496 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1497 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1498 date.tm_hour, date.tm_min, date.tm_sec );
1500 vlm_MessageAdd( msg_schedule,
1501 vlm_MessageNew( "date", psz_date ) );
1506 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1509 if( schedule->i_period != 0 )
1511 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1514 date.tm_sec = (int)( i_time % 60 );
1515 i_time = i_time / 60;
1516 date.tm_min = (int)( i_time % 60 );
1517 i_time = i_time / 60;
1518 date.tm_hour = (int)( i_time % 24 );
1519 i_time = i_time / 24;
1520 date.tm_mday = (int)( i_time % 30 );
1521 i_time = i_time / 30;
1522 /* okay, okay, months are not always 30 days long */
1523 date.tm_mon = (int)( i_time % 12 );
1524 i_time = i_time / 12;
1525 date.tm_year = (int)i_time;
1527 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1528 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1530 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1534 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1536 #endif /* UNDER_CE */
1538 sprintf( buffer, "%d", schedule->i_repeat );
1539 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1542 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", 0) );
1544 for( i = 0; i < schedule->i_command; i++ )
1546 vlm_MessageAdd( msg_child,
1547 vlm_MessageNew( schedule->command[i], NULL ) );
1554 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1556 vlm_message_t *p_msg;
1557 vlm_message_t *p_msg_child;
1558 int i_vod = 0, i_broadcast = 0;
1562 for( i = 0; i < vlm->i_media; i++ )
1564 if( vlm->media[i]->cfg.b_vod )
1570 asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1572 p_msg = vlm_MessageNew( "show", NULL );
1573 p_msg_child = vlm_MessageAdd( p_msg, vlm_MessageNew( "media", psz_count ) );
1576 for( i = 0; i < vlm->i_media; i++ )
1577 vlm_MessageAdd( p_msg_child, vlm_ShowMedia( vlm, vlm->media[i] ) );
1582 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1586 vlm_message_t *msg_child;
1588 msg = vlm_MessageNew( "show", NULL );
1589 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", NULL ) );
1591 for( i = 0; i < vlm->i_schedule; i++ )
1593 vlm_schedule_sys_t *s = vlm->schedule[i];
1594 vlm_message_t *msg_schedule;
1595 mtime_t i_time, i_next_date;
1597 msg_schedule = vlm_MessageAdd( msg_child,
1598 vlm_MessageNew( s->psz_name, 0 ) );
1599 vlm_MessageAdd( msg_schedule,
1600 vlm_MessageNew( "enabled", s->b_enabled ?
1603 /* calculate next date */
1604 i_time = vlm_Date();
1605 i_next_date = s->i_date;
1607 if( s->i_period != 0 )
1610 while( s->i_date + j * s->i_period <= i_time &&
1616 i_next_date = s->i_date + j * s->i_period;
1619 if( i_next_date > i_time )
1621 time_t i_date = (time_t) (i_next_date / 1000000) ;
1623 #if !defined( UNDER_CE )
1626 ctime_r( &i_date, psz_date );
1628 char *psz_date = ctime( &i_date );
1631 vlm_MessageAdd( msg_schedule,
1632 vlm_MessageNew( "next launch", psz_date ) );
1640 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
1642 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1643 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1645 vlm_MessageAdd( show1, show2->child[0] );
1647 /* We must destroy the parent node "show" of show2
1648 * and not the children */
1649 free( show2->psz_name );
1657 return vlm_MessageNew( "show", NULL );
1661 static vlm_message_t *vlm_Help( vlm_t *vlm, char *psz_filter )
1663 vlm_message_t *message, *message_child;
1665 #define MessageAdd( a ) \
1666 vlm_MessageAdd( message, vlm_MessageNew( a, NULL ) );
1667 #define MessageAddChild( a ) \
1668 vlm_MessageAdd( message_child, vlm_MessageNew( a, NULL ) );
1670 if( psz_filter == NULL )
1672 message = vlm_MessageNew( "help", NULL );
1674 message_child = MessageAdd( "Commands Syntax:" );
1675 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
1676 MessageAddChild( "setup (name) (properties)" );
1677 MessageAddChild( "show [(name)|media|schedule]" );
1678 MessageAddChild( "del (name)|all|media|schedule" );
1679 MessageAddChild( "control (name) [instance_name] (command)" );
1680 MessageAddChild( "save (config_file)" );
1681 MessageAddChild( "export" );
1682 MessageAddChild( "load (config_file)" );
1684 message_child = MessageAdd( "Media Proprieties Syntax:" );
1685 MessageAddChild( "input (input_name)" );
1686 MessageAddChild( "inputdel (input_name)|all" );
1687 MessageAddChild( "inputdeln input_number" );
1688 MessageAddChild( "output (output_name)" );
1689 MessageAddChild( "option (option_name)[=value]" );
1690 MessageAddChild( "enabled|disabled" );
1691 MessageAddChild( "loop|unloop (broadcast only)" );
1692 MessageAddChild( "mux (mux_name)" );
1694 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
1695 MessageAddChild( "enabled|disabled" );
1696 MessageAddChild( "append (command_until_rest_of_the_line)" );
1697 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
1699 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
1700 "(days)-(hours):(minutes):(seconds)" );
1701 MessageAddChild( "repeat (number_of_repetitions)" );
1703 message_child = MessageAdd( "Control Commands Syntax:" );
1704 MessageAddChild( "play [input_number]" );
1705 MessageAddChild( "pause" );
1706 MessageAddChild( "stop" );
1707 MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](miliseconds)ms" );
1712 return vlm_MessageNew( "help", NULL );
1715 /*****************************************************************************
1716 * Config handling functions
1717 *****************************************************************************/
1718 static int Load( vlm_t *vlm, char *file )
1723 while( *pf != '\0' )
1725 vlm_message_t *message = NULL;
1728 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1733 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1737 if( pf[i_end] == '\n' ) i_end++;
1740 if( *pf && ExecuteCommand( vlm, pf, &message ) )
1744 if( message->psz_value )
1745 msg_Err( vlm, "Load error on line %d: %s: %s",
1746 i_line, message->psz_name, message->psz_value );
1747 vlm_MessageDelete( message );
1751 if( message ) vlm_MessageDelete( message );
1760 static char *Save( vlm_t *vlm )
1763 char psz_header[] = "\n"
1764 "# VLC media player VLM command batch\n"
1765 "# http://www.videolan.org/vlc/\n\n" ;
1768 int i_length = strlen( psz_header );
1770 for( i = 0; i < vlm->i_media; i++ )
1772 vlm_media_sys_t *media = vlm->media[i];
1773 vlm_media_t *p_cfg = &media->cfg;
1776 i_length += strlen( "new * vod " ) + strlen(p_cfg->psz_name);
1778 i_length += strlen( "new * broadcast " ) + strlen(p_cfg->psz_name);
1780 if( p_cfg->b_enabled == VLC_TRUE )
1781 i_length += strlen( "enabled" );
1783 i_length += strlen( "disabled" );
1785 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop == VLC_TRUE )
1786 i_length += strlen( " loop\n" );
1788 i_length += strlen( "\n" );
1790 for( j = 0; j < p_cfg->i_input; j++ )
1791 i_length += strlen( "setup * input \"\"\n" ) + strlen( p_cfg->psz_name ) + strlen( p_cfg->ppsz_input[j] );
1793 if( p_cfg->psz_output != NULL )
1794 i_length += strlen( "setup * output \n" ) + strlen(p_cfg->psz_name) + strlen(p_cfg->psz_output);
1796 for( j = 0; j < p_cfg->i_option; j++ )
1797 i_length += strlen("setup * option \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->ppsz_option[j]);
1799 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1800 i_length += strlen("setup * mux \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->vod.psz_mux);
1803 for( i = 0; i < vlm->i_schedule; i++ )
1805 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1807 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
1809 if( schedule->b_enabled == VLC_TRUE )
1811 i_length += strlen( "date //-:: enabled\n" ) + 14;
1815 i_length += strlen( "date //-:: disabled\n" ) + 14;
1819 if( schedule->i_period != 0 )
1821 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
1822 strlen( "period //-::\n" ) + 14;
1825 if( schedule->i_repeat >= 0 )
1829 sprintf( buffer, "%d", schedule->i_repeat );
1830 i_length += strlen( "setup repeat \n" ) +
1831 strlen( schedule->psz_name ) + strlen( buffer );
1838 for( j = 0; j < schedule->i_command; j++ )
1840 i_length += strlen( "setup append \n" ) +
1841 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
1846 /* Don't forget the '\0' */
1848 /* now we have the length of save */
1850 p = save = malloc( i_length );
1853 p += sprintf( p, "%s", psz_header );
1855 /* finally we can write in it */
1856 for( i = 0; i < vlm->i_media; i++ )
1858 vlm_media_sys_t *media = vlm->media[i];
1859 vlm_media_t *p_cfg = &media->cfg;
1862 p += sprintf( p, "new %s vod ", p_cfg->psz_name );
1864 p += sprintf( p, "new %s broadcast ", p_cfg->psz_name );
1866 if( p_cfg->b_enabled )
1867 p += sprintf( p, "enabled" );
1869 p += sprintf( p, "disabled" );
1871 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1872 p += sprintf( p, " loop\n" );
1874 p += sprintf( p, "\n" );
1876 for( j = 0; j < p_cfg->i_input; j++ )
1877 p += sprintf( p, "setup %s input \"%s\"\n", p_cfg->psz_name, p_cfg->ppsz_input[j] );
1879 if( p_cfg->psz_output )
1880 p += sprintf( p, "setup %s output %s\n", p_cfg->psz_name, p_cfg->psz_output );
1882 for( j = 0; j < p_cfg->i_option; j++ )
1883 p += sprintf( p, "setup %s option %s\n", p_cfg->psz_name, p_cfg->ppsz_option[j] );
1885 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1886 p += sprintf( p, "setup %s mux %s\n", p_cfg->psz_name, p_cfg->vod.psz_mux );
1889 /* and now, the schedule scripts */
1890 #if !defined( UNDER_CE )
1891 for( i = 0; i < vlm->i_schedule; i++ )
1893 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1895 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
1897 #ifdef HAVE_LOCALTIME_R
1898 localtime_r( &i_time, &date);
1900 struct tm *p_date = localtime( &i_time );
1904 p += sprintf( p, "new %s schedule ", schedule->psz_name);
1906 if( schedule->b_enabled == VLC_TRUE )
1908 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
1909 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1910 date.tm_hour, date.tm_min, date.tm_sec );
1914 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
1915 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1916 date.tm_hour, date.tm_min, date.tm_sec);
1919 if( schedule->i_period != 0 )
1921 p += sprintf( p, "setup %s ", schedule->psz_name );
1923 i_time = (time_t) ( schedule->i_period / 1000000 );
1925 date.tm_sec = (int)( i_time % 60 );
1926 i_time = i_time / 60;
1927 date.tm_min = (int)( i_time % 60 );
1928 i_time = i_time / 60;
1929 date.tm_hour = (int)( i_time % 24 );
1930 i_time = i_time / 24;
1931 date.tm_mday = (int)( i_time % 30 );
1932 i_time = i_time / 30;
1933 /* okay, okay, months are not always 30 days long */
1934 date.tm_mon = (int)( i_time % 12 );
1935 i_time = i_time / 12;
1936 date.tm_year = (int)i_time;
1938 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
1939 date.tm_year, date.tm_mon, date.tm_mday,
1940 date.tm_hour, date.tm_min, date.tm_sec);
1943 if( schedule->i_repeat >= 0 )
1945 p += sprintf( p, "setup %s repeat %d\n",
1946 schedule->psz_name, schedule->i_repeat );
1950 p += sprintf( p, "\n" );
1953 for( j = 0; j < schedule->i_command; j++ )
1955 p += sprintf( p, "setup %s append %s\n",
1956 schedule->psz_name, schedule->command[j] );
1960 #endif /* UNDER_CE */
1965 /*****************************************************************************
1967 *****************************************************************************/
1968 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
1969 const char *psz_id, int i_query, va_list args )
1971 vlm_t *vlm = (vlm_t *)p_private;
1976 if( !p_private || !p_vod_media )
1977 return VLC_EGENERIC;
1979 vlc_mutex_lock( &vlm->lock );
1982 for( i = 0, id = -1; i < vlm->i_media; i++ )
1984 if( p_vod_media == vlm->media[i]->vod.p_media )
1986 id = vlm->media[i]->cfg.id;
1992 vlc_mutex_unlock( &vlm->lock );
1993 return VLC_EGENERIC;
1998 case VOD_MEDIA_PLAY:
1999 psz = (const char *)va_arg( args, const char * );
2000 i_ret = vlm_ControlInternal( vlm, VLM_START_MEDIA_VOD_INSTANCE, id, psz_id, 0, psz );
2003 case VOD_MEDIA_PAUSE:
2004 i_ret = vlm_ControlInternal( vlm, VLM_PAUSE_MEDIA_INSTANCE, id, psz_id );
2007 case VOD_MEDIA_STOP:
2008 i_ret = vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, id, psz_id );
2011 case VOD_MEDIA_SEEK:
2013 double d_position = (double)va_arg( args, double );
2014 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position/100.0 );
2018 case VOD_MEDIA_REWIND:
2020 double d_scale = (double)va_arg( args, double );
2023 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2024 d_position -= (d_scale / 1000.0);
2025 if( d_position < 0.0 )
2027 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2031 case VOD_MEDIA_FORWARD:
2033 double d_scale = (double)va_arg( args, double );
2036 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2037 d_position += (d_scale / 1000.0);
2038 if( d_position > 1.0 )
2040 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2045 i_ret = VLC_EGENERIC;
2049 vlc_mutex_unlock( &vlm->lock );
2055 /*****************************************************************************
2057 *****************************************************************************/
2058 static int Manage( vlc_object_t* p_object )
2060 vlm_t *vlm = (vlm_t*)p_object;
2062 mtime_t i_lastcheck;
2065 i_lastcheck = vlm_Date();
2067 while( !vlm->b_die )
2069 char **ppsz_scheduled_commands = NULL;
2070 int i_scheduled_commands = 0;
2072 vlc_mutex_lock( &vlm->lock );
2074 /* destroy the inputs that wants to die, and launch the next input */
2075 for( i = 0; i < vlm->i_media; i++ )
2077 vlm_media_sys_t *p_media = vlm->media[i];
2079 for( j = 0; j < p_media->i_instance; )
2081 vlm_media_instance_sys_t *p_instance = p_media->instance[j];
2083 if( p_instance->p_input && ( p_instance->p_input->b_eof || p_instance->p_input->b_error ) )
2085 int i_new_input_index;
2088 i_new_input_index = p_instance->i_index + 1;
2089 if( !p_media->cfg.b_vod && p_media->cfg.broadcast.b_loop && i_new_input_index >= p_media->cfg.i_input )
2090 i_new_input_index = 0;
2092 /* FIXME implement multiple input with VOD */
2093 if( p_media->cfg.b_vod || i_new_input_index >= p_media->cfg.i_input )
2094 vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, p_instance->psz_name );
2096 vlm_ControlInternal( vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, p_instance->psz_name, i_new_input_index );
2108 i_time = vlm_Date();
2110 for( i = 0; i < vlm->i_schedule; i++ )
2112 mtime_t i_real_date = vlm->schedule[i]->i_date;
2114 if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2116 if( vlm->schedule[i]->i_date == 0 ) // now !
2118 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2119 i_real_date = i_time;
2121 else if( vlm->schedule[i]->i_period != 0 )
2124 while( vlm->schedule[i]->i_date + j *
2125 vlm->schedule[i]->i_period <= i_lastcheck &&
2126 ( vlm->schedule[i]->i_repeat > j ||
2127 vlm->schedule[i]->i_repeat == -1 ) )
2132 i_real_date = vlm->schedule[i]->i_date + j *
2133 vlm->schedule[i]->i_period;
2136 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2138 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2140 TAB_APPEND( i_scheduled_commands,
2141 ppsz_scheduled_commands,
2142 strdup(vlm->schedule[i]->command[j] ) );
2147 while( i_scheduled_commands )
2149 vlm_message_t *message = NULL;
2150 char *psz_command = ppsz_scheduled_commands[0];
2151 ExecuteCommand( vlm, psz_command,&message );
2153 /* for now, drop the message */
2154 vlm_MessageDelete( message );
2155 TAB_REMOVE( i_scheduled_commands,
2156 ppsz_scheduled_commands,
2158 free( psz_command );
2161 i_lastcheck = i_time;
2163 vlc_mutex_unlock( &vlm->lock );
2178 int i_connection_count;
2179 int i_connection_active;
2184 vlc_bool_t b_playing;
2185 int i_playing_index;
2188 } vlm_media_status_t;
2192 static vlm_media_sys_t *vlm_ControlMediaGetById( vlm_t *p_vlm, int64_t id )
2196 for( i = 0; i < p_vlm->i_media; i++ )
2198 if( p_vlm->media[i]->cfg.id == id )
2199 return p_vlm->media[i];
2203 static vlm_media_sys_t *vlm_ControlMediaGetByName( vlm_t *p_vlm, const char *psz_name )
2207 for( i = 0; i < p_vlm->i_media; i++ )
2209 if( !strcmp( p_vlm->media[i]->cfg.psz_name, psz_name ) )
2210 return p_vlm->media[i];
2214 static int vlm_MediaDescriptionCheck( vlm_t *p_vlm, vlm_media_t *p_cfg )
2218 if( !p_cfg || !p_cfg->psz_name ||
2219 !strcmp( p_cfg->psz_name, "all" ) || !strcmp( p_cfg->psz_name, "media" ) || !strcmp( p_cfg->psz_name, "schedule" ) )
2220 return VLC_EGENERIC;
2222 for( i = 0; i < p_vlm->i_media; i++ )
2224 if( p_vlm->media[i]->cfg.id == p_cfg->id )
2226 if( !strcmp( p_vlm->media[i]->cfg.psz_name, p_cfg->psz_name ) )
2227 return VLC_EGENERIC;
2233 /* Called after a media description is changed/added */
2234 static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
2236 vlm_media_t *p_cfg = &p_media->cfg;
2237 /* Check if we need to create/delete a vod media */
2240 if( !p_cfg->b_enabled && p_media->vod.p_media )
2242 p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
2243 p_media->vod.p_media = NULL;
2245 else if( p_cfg->b_enabled && !p_media->vod.p_media && p_cfg->i_input )
2247 /* Pre-parse the input */
2248 input_thread_t *p_input;
2254 input_ItemClean( &p_media->vod.item );
2255 input_ItemInit( VLC_OBJECT(p_vlm), &p_media->vod.item );
2257 if( p_cfg->psz_output )
2258 asprintf( &psz_output, "%s:description", p_cfg->psz_output );
2260 asprintf( &psz_output, "#description" );
2262 p_media->vod.item.psz_uri = strdup( p_cfg->ppsz_input[0] );
2264 TAB_INIT( p_media->vod.item.i_options, p_media->vod.item.ppsz_options );
2266 asprintf( &psz_dup, "sout=%s", psz_output);
2267 TAB_APPEND( p_media->vod.item.i_options, p_media->vod.item.ppsz_options, psz_dup );
2268 for( i = 0; i < p_cfg->i_option; i++ )
2270 psz_dup = strdup( p_cfg->ppsz_option[i] );
2271 TAB_APPEND( p_media->vod.item.i_options, p_media->vod.item.ppsz_options, psz_dup );
2273 psz_dup = strdup( "no-sout-keep" );
2274 TAB_APPEND( p_media->vod.item.i_options, p_media->vod.item.ppsz_options, psz_dup );
2276 asprintf( &psz_header, _("Media: %s"), p_cfg->psz_name );
2278 if( (p_input = input_CreateThreadExtended( p_vlm, &p_media->vod.item, psz_header, NULL ) ) )
2280 while( !p_input->b_eof && !p_input->b_error )
2283 input_StopThread( p_input );
2284 input_DestroyThreadExtended( p_input, NULL );
2289 if( p_cfg->vod.psz_mux )
2292 es_format_t es, *p_es = &es;
2295 sprintf( fourcc, "%4.4s", p_cfg->vod.psz_mux );
2296 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
2297 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
2299 item = p_media->vod.item;
2302 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
2304 p_media->vod.p_media =
2305 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
2309 p_media->vod.p_media =
2310 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &p_media->vod.item );
2316 /* TODO start media if needed */
2319 /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
2323 static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
2325 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
2328 if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
2329 return VLC_EGENERIC;
2330 if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
2331 return VLC_EGENERIC;
2335 /* TODO check what are the changes being done (stop instance if needed) */
2338 vlm_media_Clean( &p_media->cfg );
2339 vlm_media_Copy( &p_media->cfg, p_cfg );
2341 return vlm_OnMediaUpdate( p_vlm, p_media );
2343 static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id )
2345 vlm_media_sys_t *p_media;
2347 if( vlm_MediaDescriptionCheck( p_vlm, p_cfg ) || vlm_ControlMediaGetByName( p_vlm, p_cfg->psz_name ) )
2349 msg_Err( p_vlm, "invalid media description" );
2350 return VLC_EGENERIC;
2352 /* Check if we need to load the VOD server */
2353 if( p_cfg->b_vod && !p_vlm->i_vod )
2355 p_vlm->p_vod = vlc_object_create( p_vlm, VLC_OBJECT_VOD );
2356 vlc_object_attach( p_vlm->p_vod, p_vlm );
2357 p_vlm->p_vod->p_module = module_Need( p_vlm->p_vod, "vod server", 0, 0 );
2358 if( !p_vlm->p_vod->p_module )
2360 msg_Err( p_vlm, "cannot find vod server" );
2361 vlc_object_detach( p_vlm->p_vod );
2362 vlc_object_destroy( p_vlm->p_vod );
2364 return VLC_EGENERIC;
2367 p_vlm->p_vod->p_data = p_vlm;
2368 p_vlm->p_vod->pf_media_control = vlm_MediaVodControl;
2373 p_media = malloc( sizeof( vlm_media_sys_t ) );
2374 memset( p_media, 0, sizeof(vlm_media_sys_t) );
2376 vlm_media_Copy( &p_media->cfg, p_cfg );
2377 p_media->cfg.id = p_vlm->i_id++;
2378 /* FIXME do we do something here if enabled is true ? */
2380 input_ItemInit( VLC_OBJECT(p_vlm), &p_media->vod.item );
2382 p_media->vod.p_media = NULL;
2383 TAB_INIT( p_media->i_instance, p_media->instance );
2386 TAB_APPEND( p_vlm->i_media, p_vlm->media, p_media );
2389 *p_id = p_media->cfg.id;
2391 return vlm_OnMediaUpdate( p_vlm, p_media );
2394 static int vlm_ControlMediaDel( vlm_t *p_vlm, int64_t id )
2396 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2399 return VLC_EGENERIC;
2401 while( p_media->i_instance > 0 )
2402 vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, p_media->instance[0]->psz_name );
2404 if( p_media->cfg.b_vod )
2406 p_media->cfg.b_enabled = VLC_FALSE;
2407 vlm_OnMediaUpdate( p_vlm, p_media );
2411 vlm_media_Clean( &p_media->cfg );
2413 input_ItemClean( &p_media->vod.item );
2415 TAB_REMOVE( p_vlm->i_media, p_vlm->media, p_media );
2419 /* Check if we need to unload the VOD server */
2420 if( p_vlm->p_vod && p_vlm->i_vod <= 0 )
2422 module_Unneed( p_vlm->p_vod, p_vlm->p_vod->p_module );
2423 vlc_object_detach( p_vlm->p_vod );
2424 vlc_object_destroy( p_vlm->p_vod );
2425 p_vlm->p_vod = NULL;
2430 static int vlm_ControlMediaGets( vlm_t *p_vlm, vlm_media_t ***ppp_dsc, int *pi_dsc )
2432 vlm_media_t **pp_dsc;
2436 TAB_INIT( i_dsc, pp_dsc );
2437 for( i = 0; i < p_vlm->i_media; i++ )
2439 vlm_media_t *p_dsc = vlm_media_Duplicate( &p_vlm->media[i]->cfg );
2440 TAB_APPEND( i_dsc, pp_dsc, p_dsc );
2448 static int vlm_ControlMediaClear( vlm_t *p_vlm )
2450 while( p_vlm->i_media > 0 )
2451 vlm_ControlMediaDel( p_vlm, p_vlm->media[0]->cfg.id );
2455 static int vlm_ControlMediaGet( vlm_t *p_vlm, int64_t id, vlm_media_t **pp_dsc )
2457 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2459 return VLC_EGENERIC;
2461 *pp_dsc = vlm_media_Duplicate( &p_media->cfg );
2464 static int vlm_ControlMediaGetId( vlm_t *p_vlm, const char *psz_name, int64_t *p_id )
2466 vlm_media_sys_t *p_media = vlm_ControlMediaGetByName( p_vlm, psz_name );
2468 return VLC_EGENERIC;
2470 *p_id = p_media->cfg.id;
2474 static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
2478 for( i = 0; i < p_media->i_instance; i++ )
2480 const char *psz = p_media->instance[i]->psz_name;
2481 if( ( psz == NULL && psz_id == NULL ) ||
2482 ( psz && psz_id && !strcmp( psz, psz_id ) ) )
2483 return p_media->instance[i];
2487 static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
2489 vlm_media_instance_sys_t *p_instance = malloc( sizeof(vlm_media_instance_sys_t) );
2493 memset( p_instance, 0, sizeof(vlm_media_instance_sys_t) );
2495 p_instance->psz_name = NULL;
2497 p_instance->psz_name = strdup( psz_name );
2499 input_ItemInit( VLC_OBJECT(p_vlm), &p_instance->item );
2501 p_instance->i_index = 0;
2502 p_instance->b_sout_keep = VLC_FALSE;
2503 p_instance->p_input = NULL;
2504 p_instance->p_sout = NULL;
2508 static void vlm_MediaInstanceDelete( vlm_media_instance_sys_t *p_instance )
2510 if( p_instance->p_input )
2512 input_StopThread( p_instance->p_input );
2513 input_DestroyThreadExtended( p_instance->p_input, &p_instance->p_sout );
2515 if( p_instance->p_sout )
2516 sout_DeleteInstance( p_instance->p_sout );
2518 input_ItemClean( &p_instance->item );
2519 if( p_instance->psz_name )
2520 free( p_instance->psz_name );
2525 static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
2527 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2528 vlm_media_instance_sys_t *p_instance;
2531 if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
2532 return VLC_EGENERIC;
2534 /* TODO support multiple input for VOD with sout-keep ? */
2536 if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
2537 return VLC_EGENERIC;
2539 if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
2540 return VLC_EGENERIC;
2542 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2545 vlm_media_t *p_cfg = &p_media->cfg;
2549 p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
2553 TAB_INIT( p_instance->item.i_options, p_instance->item.ppsz_options );
2555 if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
2558 asprintf( &psz_buffer, "sout=%s%s%s",
2559 p_cfg->psz_output ? p_cfg->psz_output : "",
2560 (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
2561 psz_vod_output ? psz_vod_output : "" );
2562 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, psz_buffer );
2565 for( i = 0; i < p_cfg->i_option; i++ )
2567 if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
2568 p_instance->b_sout_keep = VLC_TRUE;
2569 else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
2570 p_instance->b_sout_keep = VLC_FALSE;
2572 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, strdup( p_cfg->ppsz_option[i] ) );
2574 /* We force the right sout-keep value (avoid using the sout-keep from the global configuration)
2575 * FIXME implement input list for VOD (need sout-keep)
2577 if( !p_cfg->b_vod && p_instance->b_sout_keep )
2578 psz_keep = strdup( "sout-keep" );
2580 psz_keep = strdup( "no-sout-keep" );
2581 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, psz_keep );
2583 TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
2586 /* Stop old instance */
2587 if( p_instance->p_input )
2589 if( p_instance->i_index == i_input_index &&
2590 !p_instance->p_input->b_eof && !p_instance->p_input->b_error )
2592 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2593 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2597 input_StopThread( p_instance->p_input );
2598 input_DestroyThreadExtended( p_instance->p_input, &p_instance->p_sout );
2599 if( !p_instance->b_sout_keep && p_instance->p_sout )
2601 sout_DeleteInstance( p_instance->p_sout );
2602 p_instance->p_sout = NULL;
2607 p_instance->i_index = i_input_index;
2608 if( p_instance->item.psz_uri )
2609 free( p_instance->item.psz_uri );
2610 p_instance->item.psz_uri = strdup( p_media->cfg.ppsz_input[p_instance->i_index] );
2612 asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name );
2613 p_instance->p_input = input_CreateThreadExtended( p_vlm, &p_instance->item, psz_log, p_instance->p_sout );
2614 if( !p_instance->p_input )
2616 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2617 vlm_MediaInstanceDelete( p_instance );
2624 static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
2626 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2627 vlm_media_instance_sys_t *p_instance;
2630 return VLC_EGENERIC;
2632 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2634 return VLC_EGENERIC;
2636 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2638 vlm_MediaInstanceDelete( p_instance );
2642 static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
2644 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2645 vlm_media_instance_sys_t *p_instance;
2649 return VLC_EGENERIC;
2651 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2652 if( !p_instance || !p_instance->p_input )
2653 return VLC_EGENERIC;
2655 /* Toggle pause state */
2656 i_state = var_GetInteger( p_instance->p_input, "state" );
2657 if( i_state == PAUSE_S )
2658 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2659 else if( i_state == PLAYING_S )
2660 var_SetInteger( p_instance->p_input, "state", PAUSE_S );
2663 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
2665 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2666 vlm_media_instance_sys_t *p_instance;
2669 return VLC_EGENERIC;
2671 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2672 if( !p_instance || !p_instance->p_input )
2673 return VLC_EGENERIC;
2676 *pi_time = var_GetTime( p_instance->p_input, "time" );
2678 *pd_position = var_GetFloat( p_instance->p_input, "position" );
2681 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t i_time, double d_position )
2683 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2684 vlm_media_instance_sys_t *p_instance;
2687 return VLC_EGENERIC;
2689 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2690 if( !p_instance || !p_instance->p_input )
2691 return VLC_EGENERIC;
2694 return var_SetTime( p_instance->p_input, "time", i_time );
2695 else if( d_position >= 0 && d_position <= 100 )
2696 return var_SetFloat( p_instance->p_input, "position", d_position );
2697 return VLC_EGENERIC;
2700 static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_instance_t ***ppp_idsc, int *pi_instance )
2702 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2703 vlm_media_instance_t **pp_idsc;
2708 return VLC_EGENERIC;
2710 TAB_INIT( i_idsc, pp_idsc );
2711 for( i = 0; i < p_media->i_instance; i++ )
2713 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
2714 vlm_media_instance_t *p_idsc = vlm_media_instance_New();
2716 if( p_instance->psz_name )
2717 p_idsc->psz_name = strdup( p_instance->psz_name );
2718 if( p_instance->p_input )
2720 p_idsc->i_time = var_GetTime( p_instance->p_input, "time" );
2721 p_idsc->i_length = var_GetTime( p_instance->p_input, "length" );
2722 p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
2723 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2724 p_idsc->b_paused = VLC_TRUE;
2725 p_idsc->i_rate = var_GetInteger( p_instance->p_input, "rate" );
2728 TAB_APPEND( i_idsc, pp_idsc, p_idsc );
2730 *ppp_idsc = pp_idsc;
2731 *pi_instance = i_idsc;
2734 static int vlm_ControlMediaInstanceClear( vlm_t *p_vlm, int64_t id )
2736 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2739 return VLC_EGENERIC;
2741 while( p_media->i_instance > 0 )
2742 vlm_ControlMediaInstanceStop( p_vlm, id, p_media->instance[0]->psz_name );
2747 static int vlm_ControlScheduleClear( vlm_t *p_vlm )
2749 while( p_vlm->i_schedule > 0 )
2750 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
2754 static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
2757 vlm_media_t **pp_dsc;
2758 vlm_media_t ***ppp_dsc;
2759 vlm_media_instance_t ***ppp_idsc;
2761 const char *psz_vod;
2775 case VLM_GET_MEDIAS:
2776 ppp_dsc = (vlm_media_t ***)va_arg( args, vlm_media_t *** );
2777 pi_int = (int *)va_arg( args, int * );
2778 return vlm_ControlMediaGets( p_vlm, ppp_dsc, pi_int );
2780 case VLM_CLEAR_MEDIAS:
2781 return vlm_ControlMediaClear( p_vlm );
2783 case VLM_CHANGE_MEDIA:
2784 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2785 return vlm_ControlMediaChange( p_vlm, p_dsc );
2788 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2789 p_id = (int64_t*)va_arg( args, int64_t * );
2790 return vlm_ControlMediaAdd( p_vlm, p_dsc, p_id );
2793 id = (int64_t)va_arg( args, int64_t );
2794 return vlm_ControlMediaDel( p_vlm, id );
2797 id = (int64_t)va_arg( args, int64_t );
2798 pp_dsc = (vlm_media_t **)va_arg( args, vlm_media_t ** );
2799 return vlm_ControlMediaGet( p_vlm, id, pp_dsc );
2801 case VLM_GET_MEDIA_ID:
2802 psz_id = (const char*)va_arg( args, const char * );
2803 p_id = (int64_t*)va_arg( args, int64_t * );
2804 return vlm_ControlMediaGetId( p_vlm, psz_id, p_id );
2807 /* Media instance control */
2808 case VLM_GET_MEDIA_INSTANCES:
2809 id = (int64_t)va_arg( args, int64_t );
2810 ppp_idsc = (vlm_media_instance_t ***)va_arg( args, vlm_media_instance_t *** );
2811 pi_int = (int *)va_arg( args, int *);
2812 return vlm_ControlMediaInstanceGets( p_vlm, id, ppp_idsc, pi_int );
2814 case VLM_CLEAR_MEDIA_INSTANCES:
2815 id = (int64_t)va_arg( args, int64_t );
2816 return vlm_ControlMediaInstanceClear( p_vlm, id );
2819 case VLM_START_MEDIA_BROADCAST_INSTANCE:
2820 id = (int64_t)va_arg( args, int64_t );
2821 psz_id = (const char*)va_arg( args, const char* );
2822 i_int = (int)va_arg( args, int );
2823 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, NULL );
2825 case VLM_START_MEDIA_VOD_INSTANCE:
2826 id = (int64_t)va_arg( args, int64_t );
2827 psz_id = (const char*)va_arg( args, const char* );
2828 i_int = (int)va_arg( args, int );
2829 psz_vod = (const char*)va_arg( args, const char* );
2831 return VLC_EGENERIC;
2832 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, psz_vod );
2834 case VLM_STOP_MEDIA_INSTANCE:
2835 id = (int64_t)va_arg( args, int64_t );
2836 psz_id = (const char*)va_arg( args, const char* );
2837 return vlm_ControlMediaInstanceStop( p_vlm, id, psz_id );
2839 case VLM_PAUSE_MEDIA_INSTANCE:
2840 id = (int64_t)va_arg( args, int64_t );
2841 psz_id = (const char*)va_arg( args, const char* );
2842 return vlm_ControlMediaInstancePause( p_vlm, id, psz_id );
2844 case VLM_GET_MEDIA_INSTANCE_TIME:
2845 id = (int64_t)va_arg( args, int64_t );
2846 psz_id = (const char*)va_arg( args, const char* );
2847 pi_i64 = (int64_t*)va_arg( args, int64_t * );
2848 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, pi_i64, NULL );
2849 case VLM_GET_MEDIA_INSTANCE_POSITION:
2850 id = (int64_t)va_arg( args, int64_t );
2851 psz_id = (const char*)va_arg( args, const char* );
2852 pd_double = (double*)va_arg( args, double* );
2853 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, NULL, pd_double );
2855 case VLM_SET_MEDIA_INSTANCE_TIME:
2856 id = (int64_t)va_arg( args, int64_t );
2857 psz_id = (const char*)va_arg( args, const char* );
2858 i_i64 = (int64_t)va_arg( args, int64_t);
2859 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, i_i64, -1 );
2860 case VLM_SET_MEDIA_INSTANCE_POSITION:
2861 id = (int64_t)va_arg( args, int64_t );
2862 psz_id = (const char*)va_arg( args, const char* );
2863 d_double = (double)va_arg( args, double );
2864 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, -1, d_double );
2866 case VLM_CLEAR_SCHEDULES:
2867 return vlm_ControlScheduleClear( p_vlm );
2870 msg_Err( p_vlm, "unknown VLM query" );
2871 return VLC_EGENERIC;
2874 static int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... )
2879 va_start( args, i_query );
2880 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2886 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2891 va_start( args, i_query );
2893 vlc_mutex_lock( &p_vlm->lock );
2894 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2895 vlc_mutex_unlock( &p_vlm->lock );
2902 #else /* ENABLE_VLM */
2904 /* We just define an empty wrapper */
2905 vlm_t *__vlm_New( vlc_object_t *a )
2907 msg_Err( a, "VideoLAN manager support is disabled" );
2910 void vlm_Delete( vlm_t *a )
2914 int vlm_ExecuteCommand( vlm_t *a, char *b, vlm_message_t **c )
2918 vlm_message_t *vlm_MessageNew( const char *psz_name,
2919 const char *psz_format, ... )
2923 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
2924 vlm_message_t *p_child )
2928 void vlm_MessageDelete( vlm_message_t *a )
2933 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2935 return VLC_EGENERIC;
2938 #endif /* ENABLE_VLM */