1 /*****************************************************************************
2 * vlm.c: VLM interface plugin
3 *****************************************************************************
4 * Copyright (C) 2000-2005 the VideoLAN team
7 * Authors: Simon Latapie <garf@videolan.org>
8 * Laurent Aimar <fenrir@videolan.org>
9 * Gildas Bazin <gbazin@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
36 #include <ctype.h> /* tolower() */
42 # include <time.h> /* ctime() */
43 # include <sys/timeb.h> /* ftime() */
46 #include <vlc_input.h>
47 #include "input_internal.h"
48 #include <vlc_stream.h>
50 #include "vlm_internal.h"
52 #include <vlc_charset.h>
54 #include "../stream_output/stream_output.h"
56 /*****************************************************************************
58 *****************************************************************************/
60 static int vlm_ControlInternal( vlm_t *, int, ... );
63 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_sys_t *, vlm_schedule_sys_t *, const char * );
64 static vlm_message_t *vlm_Help( vlm_t *, char * );
66 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *, const char * );
68 static char *Save( vlm_t * );
69 static int Load( vlm_t *, char * );
71 static int ExecuteCommand( vlm_t *, const char *, vlm_message_t ** );
73 static int Manage( vlc_object_t * );
75 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name );
76 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched, const char *psz_name );
77 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
78 const char *psz_value );
80 static int vlm_MediaVodControl( void *, vod_media_t *, const char *, int, va_list );
83 static vlm_media_sys_t *vlm_MediaSearch( vlm_t *, const char *);
85 /*****************************************************************************
87 *****************************************************************************/
88 vlm_t *__vlm_New ( vlc_object_t *p_this )
94 /* Avoid multiple creation */
95 if( var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX ) ||
96 var_Get( p_this->p_libvlc, "vlm_mutex", &lockval ) )
99 vlc_mutex_lock( lockval.p_address );
101 p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE );
104 vlc_object_yield( p_vlm );
105 vlc_mutex_unlock( lockval.p_address );
109 msg_Dbg( p_this, "creating VLM" );
111 p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM );
114 vlc_mutex_unlock( lockval.p_address );
118 vlc_mutex_init( p_this->p_libvlc, &p_vlm->lock );
120 TAB_INIT( p_vlm->i_media, p_vlm->media );
121 TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
124 vlc_object_yield( p_vlm );
125 vlc_object_attach( p_vlm, p_this->p_libvlc );
127 if( vlc_thread_create( p_vlm, "vlm thread",
128 Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
130 vlc_mutex_destroy( &p_vlm->lock );
131 vlc_object_destroy( p_vlm );
135 /* Load our configuration file */
136 psz_vlmconf = var_CreateGetString( p_vlm, "vlm-conf" );
137 if( psz_vlmconf && *psz_vlmconf )
139 vlm_message_t *p_message = NULL;
140 char *psz_buffer = NULL;
142 msg_Dbg( p_this, "loading VLM configuration" );
143 asprintf(&psz_buffer, "load %s", psz_vlmconf );
146 msg_Dbg( p_this, psz_buffer );
147 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
148 msg_Warn( p_this, "error while loading the configuration file" );
150 vlm_MessageDelete(p_message);
156 vlc_mutex_unlock( lockval.p_address );
161 /*****************************************************************************
163 *****************************************************************************/
164 void vlm_Delete( vlm_t *p_vlm )
168 var_Get( p_vlm->p_libvlc, "vlm_mutex", &lockval );
169 vlc_mutex_lock( lockval.p_address );
171 vlc_object_release( p_vlm );
173 if( p_vlm->p_internals->i_refcount > 0 )
175 vlc_mutex_unlock( lockval.p_address );
179 vlc_object_kill( p_vlm );
180 vlc_thread_join( p_vlm );
182 vlc_object_detach( p_vlm );
184 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
185 TAB_CLEAN( p_vlm->i_media, p_vlm->media );
187 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
188 TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
190 vlc_mutex_destroy( &p_vlm->lock );
192 vlc_object_destroy( p_vlm );
193 vlc_mutex_unlock( lockval.p_address );
196 /*****************************************************************************
197 * vlm_ExecuteCommand:
198 *****************************************************************************/
199 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
200 vlm_message_t **pp_message)
204 vlc_mutex_lock( &p_vlm->lock );
205 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
206 vlc_mutex_unlock( &p_vlm->lock );
212 static const char quotes[] = "\"'";
214 * FindCommandEnd: look for the end of a possibly quoted string
215 * @return NULL on mal-formatted string,
216 * pointer past the last character otherwise.
218 static const char *FindCommandEnd( const char *psz_sent )
222 while( (c = *psz_sent) != '\0' )
226 if( strchr(quotes,c) ) // opening quote
228 else if( isspace(c) ) // non-escaped space
232 psz_sent++; // skip escaped character
233 if( *psz_sent == '\0' )
239 if( c == quote ) // non-escaped matching quote
241 else if( (quote == '"') && (c == '\\') )
243 psz_sent++; // skip escaped character
244 if (*psz_sent == '\0')
245 return NULL; // error, closing quote missing
251 // error (NULL) if we could not find a matching quote
252 return quote ? NULL : psz_sent;
257 * Unescape a nul-terminated string.
258 * Note that in and out can be identical.
260 * @param out output buffer (at least <strlen (in) + 1> characters long)
261 * @param in nul-terminated string to be unescaped
263 * @return 0 on success, -1 on error.
265 static int Unescape( char *out, const char *in )
269 while( (c = *in++) != '\0' )
273 if (strchr(quotes,c)) // opening quote
297 /* None of the special cases - copy the backslash */
303 if( c == quote ) // non-escaped matching quote
308 if( (quote == '"') && (c == '\\') )
317 case '\0': // should never happen
321 /* None of the special cases - copy the backslash */
333 /*****************************************************************************
334 * ExecuteCommand: The main state machine
335 *****************************************************************************
336 * Execute a command which ends with '\0' (string)
337 *****************************************************************************/
338 static int ExecuteSyntaxError( const char *psz_cmd, vlm_message_t **pp_status )
340 *pp_status = vlm_MessageNew( psz_cmd, "Wrong command syntax" );
344 static vlc_bool_t ExecuteIsMedia( vlm_t *p_vlm, const char *psz_name )
348 if( !psz_name || vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
352 static vlc_bool_t ExecuteIsSchedule( vlm_t *p_vlm, const char *psz_name )
354 if( !psz_name || !vlm_ScheduleSearch( p_vlm, psz_name ) )
359 static int ExecuteDel( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
361 vlm_media_sys_t *p_media;
362 vlm_schedule_sys_t *p_schedule;
364 p_media = vlm_MediaSearch( p_vlm, psz_name );
365 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
367 if( p_schedule != NULL )
369 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
371 else if( p_media != NULL )
373 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_media->cfg.id );
375 else if( !strcmp(psz_name, "media") )
377 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
379 else if( !strcmp(psz_name, "schedule") )
381 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
383 else if( !strcmp(psz_name, "all") )
385 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
386 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
390 *pp_status = vlm_MessageNew( "del", "%s: media unknown", psz_name );
394 *pp_status = vlm_MessageNew( "del", NULL );
398 static int ExecuteShow( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
400 vlm_media_sys_t *p_media;
401 vlm_schedule_sys_t *p_schedule;
405 *pp_status = vlm_Show( p_vlm, NULL, NULL, NULL );
409 p_media = vlm_MediaSearch( p_vlm, psz_name );
410 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
412 if( p_schedule != NULL )
413 *pp_status = vlm_Show( p_vlm, NULL, p_schedule, NULL );
414 else if( p_media != NULL )
415 *pp_status = vlm_Show( p_vlm, p_media, NULL, NULL );
417 *pp_status = vlm_Show( p_vlm, NULL, NULL, psz_name );
422 static int ExecuteHelp( vlm_t *p_vlm, vlm_message_t **pp_status )
424 *pp_status = vlm_Help( p_vlm, NULL );
428 static int ExecuteControl( vlm_t *p_vlm, const char *psz_name, const int i_arg, char ** ppsz_arg, vlm_message_t **pp_status )
430 vlm_media_sys_t *p_media;
431 const char *psz_control = NULL;
432 const char *psz_instance = NULL;
433 const char *psz_argument = NULL;
437 if( !ExecuteIsMedia( p_vlm, psz_name ) )
439 *pp_status = vlm_MessageNew( "control", "%s: media unknown", psz_name );
445 #define IS(txt) ( !strcmp( ppsz_arg[i_index], (txt) ) )
447 if( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") )
450 psz_instance = ppsz_arg[0];
452 if( i_index >= i_arg || ( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") ) )
453 return ExecuteSyntaxError( "control", pp_status );
456 psz_control = ppsz_arg[i_index];
458 if( i_index+1 < i_arg )
459 psz_argument = ppsz_arg[i_index+1];
461 p_media = vlm_MediaSearch( p_vlm, psz_name );
464 if( !strcmp( psz_control, "play" ) )
466 int i_input_index = 0;
469 if( ( psz_argument && sscanf(psz_argument, "%d", &i) == 1 ) && i > 0 && i-1 < p_media->cfg.i_input )
473 else if( psz_argument )
476 vlm_media_t *p_cfg = &p_media->cfg;
477 for ( j=0; j < p_cfg->i_input; j++)
479 if( !strcmp( p_cfg->ppsz_input[j], psz_argument ) )
487 if( p_media->cfg.b_vod )
488 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
490 i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, psz_instance, i_input_index );
492 else if( !strcmp( psz_control, "seek" ) )
496 vlc_bool_t b_relative;
497 if( psz_argument[0] == '+' || psz_argument[0] == '-' )
498 b_relative = VLC_TRUE;
500 b_relative = VLC_FALSE;
502 if( strstr( psz_argument, "ms" ) || strstr( psz_argument, "s" ) )
507 if( strstr( psz_argument, "ms" ) )
508 i_new_time = 1000 * (int64_t)atoi( psz_argument );
510 i_new_time = 1000000 * (int64_t)atoi( psz_argument );
515 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, &i_time );
516 i_new_time += i_time;
520 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, i_new_time );
525 double d_new_position = i18n_atof( psz_argument ) / 100.0;
529 double d_position = 0.0;
531 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
532 d_new_position += d_position;
534 if( d_new_position < 0.0 )
535 d_new_position = 0.0;
536 else if( d_new_position > 1.0 )
537 d_new_position = 1.0;
538 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_new_position );
543 i_result = VLC_EGENERIC;
546 else if( !strcmp( psz_control, "rewind" ) )
550 const double d_scale = i18n_atof( psz_argument );
553 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
554 d_position -= (d_scale / 1000.0);
555 if( d_position < 0.0 )
557 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
561 i_result = VLC_EGENERIC;
564 else if( !strcmp( psz_control, "forward" ) )
568 const double d_scale = i18n_atof( psz_argument );
571 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
572 d_position += (d_scale / 1000.0);
573 if( d_position > 1.0 )
575 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
580 i_result = VLC_EGENERIC;
583 else if( !strcmp( psz_control, "stop" ) )
585 i_result = vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
587 else if( !strcmp( psz_control, "pause" ) )
589 i_result = vlm_ControlInternal( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
593 i_result = VLC_EGENERIC;
598 *pp_status = vlm_MessageNew( "control", "unknown error" );
601 *pp_status = vlm_MessageNew( "control", NULL );
605 static int ExecuteExport( vlm_t *p_vlm, vlm_message_t **pp_status )
607 char *psz_export = Save( p_vlm );
609 *pp_status = vlm_MessageNew( "export", psz_export );
614 static int ExecuteSave( vlm_t *p_vlm, const char *psz_file, vlm_message_t **pp_status )
616 FILE *f = utf8_fopen( psz_file, "wt" );
622 psz_save = Save( p_vlm );
623 if( psz_save == NULL )
628 fwrite( psz_save, strlen( psz_save ), 1, f );
632 *pp_status = vlm_MessageNew( "save", NULL );
636 *pp_status = vlm_MessageNew( "save", "Unable to save to file" );
640 static int ExecuteLoad( vlm_t *p_vlm, const char *psz_url, vlm_message_t **pp_status )
642 stream_t *p_stream = stream_UrlNew( p_vlm, psz_url );
648 *pp_status = vlm_MessageNew( "load", "Unable to load from file" );
653 if( stream_Seek( p_stream, 0 ) != 0 )
655 stream_Delete( p_stream );
657 *pp_status = vlm_MessageNew( "load", "Read file error" );
661 i_size = stream_Size( p_stream );
663 psz_buffer = malloc( i_size + 1 );
666 stream_Delete( p_stream );
668 *pp_status = vlm_MessageNew( "load", "Read file error" );
672 stream_Read( p_stream, psz_buffer, i_size );
673 psz_buffer[i_size] = '\0';
675 stream_Delete( p_stream );
677 if( Load( p_vlm, psz_buffer ) )
681 *pp_status = vlm_MessageNew( "load", "Error while loading file" );
687 *pp_status = vlm_MessageNew( "load", NULL );
691 static int ExecuteScheduleProperty( vlm_t *p_vlm, vlm_schedule_sys_t *p_schedule, vlc_bool_t b_new,
692 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
694 const char *psz_cmd = b_new ? "new" : "setup";
697 for( i = 0; i < i_property; i++ )
699 if( !strcmp( ppsz_property[i], "enabled" ) ||
700 !strcmp( ppsz_property[i], "disabled" ) )
702 vlm_ScheduleSetup( p_schedule, ppsz_property[i], NULL );
704 else if( !strcmp( ppsz_property[i], "append" ) )
708 /* Beware: everything behind append is considered as
711 if( ++i >= i_property )
714 psz_line = strdup( ppsz_property[i] );
715 for( j = i+1; j < i_property; j++ )
717 psz_line = realloc( psz_line, strlen(psz_line) + strlen(ppsz_property[j]) + 1 + 1 );
718 strcat( psz_line, " " );
719 strcat( psz_line, ppsz_property[j] );
722 vlm_ScheduleSetup( p_schedule, "append", psz_line );
727 if( i + 1 >= i_property )
730 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
731 return ExecuteSyntaxError( psz_cmd, pp_status );
734 vlm_ScheduleSetup( p_schedule, ppsz_property[i], ppsz_property[i+1] );
738 *pp_status = vlm_MessageNew( psz_cmd, NULL );
742 static int ExecuteMediaProperty( vlm_t *p_vlm, int64_t id, vlc_bool_t b_new,
743 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
745 const char *psz_cmd = b_new ? "new" : "setup";
746 vlm_media_t *p_cfg = NULL;
752 #define ERROR( txt ) do { *pp_status = vlm_MessageNew( psz_cmd, txt); goto error; } while(0)
753 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA, id, &p_cfg ) )
754 ERROR( "unknown media" );
756 #define MISSING(cmd) do { if( !psz_value ) ERROR( "missing argument for " cmd ); } while(0)
757 for( i = 0; i < i_property; i++ )
759 const char *psz_option = ppsz_property[i];
760 const char *psz_value = i+1 < i_property ? ppsz_property[i+1] : NULL;
762 if( !strcmp( psz_option, "enabled" ) )
764 p_cfg->b_enabled = VLC_TRUE;
766 else if( !strcmp( psz_option, "disabled" ) )
768 p_cfg->b_enabled = VLC_FALSE;
770 else if( !strcmp( psz_option, "input" ) )
773 TAB_APPEND( p_cfg->i_input, p_cfg->ppsz_input, strdup(psz_value) );
776 else if( !strcmp( psz_option, "inputdel" ) && psz_value && !strcmp( psz_value, "all" ) )
778 while( p_cfg->i_input > 0 )
779 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[0] );
782 else if( !strcmp( psz_option, "inputdel" ) )
786 MISSING( "inputdel" );
788 for( j = 0; j < p_cfg->i_input; j++ )
790 if( !strcmp( p_cfg->ppsz_input[j], psz_value ) )
792 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[j] );
798 else if( !strcmp( psz_option, "inputdeln" ) )
802 MISSING( "inputdeln" );
804 i_index = atoi( psz_value );
805 if( i_index > 0 && i_index <= p_cfg->i_input )
806 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[i_index-1] );
809 else if( !strcmp( psz_option, "output" ) )
813 if( p_cfg->psz_output != NULL )
814 free( p_cfg->psz_output );
815 p_cfg->psz_output = *psz_value ? strdup( psz_value ) : NULL;
818 else if( !strcmp( psz_option, "option" ) )
822 TAB_APPEND( p_cfg->i_option, p_cfg->ppsz_option, strdup( psz_value ) );
825 else if( !strcmp( psz_option, "loop" ) )
828 ERROR( "invalid loop option for vod" );
829 p_cfg->broadcast.b_loop = VLC_TRUE;
831 else if( !strcmp( psz_option, "unloop" ) )
834 ERROR( "invalid unloop option for vod" );
835 p_cfg->broadcast.b_loop = VLC_FALSE;
837 else if( !strcmp( psz_option, "mux" ) )
841 ERROR( "invalid mux option for broadcast" );
843 if( p_cfg->vod.psz_mux )
844 free( p_cfg->vod.psz_mux );
845 p_cfg->vod.psz_mux = *psz_value ? strdup( psz_value ) : NULL;
850 fprintf( stderr, "PROP: name=%s unknown\n", psz_option );
851 ERROR( "Wrong command syntax" );
858 i_result = vlm_ControlInternal( p_vlm, VLM_CHANGE_MEDIA, p_cfg );
859 vlm_media_Delete( p_cfg );
861 *pp_status = vlm_MessageNew( psz_cmd, NULL );
868 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_cfg->id );
869 vlm_media_Delete( p_cfg );
874 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 )
877 if( !strcmp( psz_name, "all" ) || !strcmp( psz_name, "media" ) || !strcmp( psz_name, "schedule" ) )
879 *pp_status = vlm_MessageNew( "new", "\"all\", \"media\" and \"schedule\" are reserved names" );
882 if( ExecuteIsMedia( p_vlm, psz_name ) || ExecuteIsSchedule( p_vlm, psz_name ) )
884 *pp_status = vlm_MessageNew( "new", "%s: Name already in use", psz_name );
888 if( !strcmp( psz_type, "schedule" ) )
890 vlm_schedule_sys_t *p_schedule = vlm_ScheduleNew( p_vlm, psz_name );
893 *pp_status = vlm_MessageNew( "new", "could not create schedule" );
896 return ExecuteScheduleProperty( p_vlm, p_schedule, VLC_TRUE, i_property, ppsz_property, pp_status );
898 else if( !strcmp( psz_type, "vod" ) || !strcmp( psz_type, "broadcast" ) )
903 vlm_media_Init( &cfg );
904 cfg.psz_name = strdup( psz_name );
905 cfg.b_vod = !strcmp( psz_type, "vod" );
907 if( vlm_ControlInternal( p_vlm, VLM_ADD_MEDIA, &cfg, &id ) )
909 vlm_media_Clean( &cfg );
910 *pp_status = vlm_MessageNew( "new", "could not create media" );
913 vlm_media_Clean( &cfg );
914 return ExecuteMediaProperty( p_vlm, id, VLC_TRUE, i_property, ppsz_property, pp_status );
918 *pp_status = vlm_MessageNew( "new", "%s: Choose between vod, broadcast or schedule", psz_type );
923 static int ExecuteSetup( vlm_t *p_vlm, const char *psz_name, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
925 if( ExecuteIsSchedule( p_vlm, psz_name ) )
927 vlm_schedule_sys_t *p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
928 return ExecuteScheduleProperty( p_vlm, p_schedule, VLC_FALSE, i_property, ppsz_property, pp_status );
930 else if( ExecuteIsMedia( p_vlm, psz_name ) )
933 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
935 return ExecuteMediaProperty( p_vlm, id, VLC_FALSE, i_property, ppsz_property, pp_status );
939 *pp_status = vlm_MessageNew( "setup", "%s unknown", psz_name );
943 static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
944 vlm_message_t **pp_message )
946 size_t i_command = 0;
947 char buf[strlen (psz_command) + 1], *psz_buf = buf;
948 char *ppsz_command[3+sizeof (buf) / 2];
949 vlm_message_t *p_message = NULL;
951 /* First, parse the line and cut it */
952 while( *psz_command != '\0' )
954 const char *psz_temp;
956 if(isspace (*psz_command))
962 /* support for comments */
963 if( i_command == 0 && *psz_command == '#')
965 p_message = vlm_MessageNew( "", NULL );
969 psz_temp = FindCommandEnd( psz_command );
971 if( psz_temp == NULL )
973 p_message = vlm_MessageNew( "Incomplete command", psz_command );
977 assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
979 ppsz_command[i_command] = psz_buf;
980 memcpy (psz_buf, psz_command, psz_temp - psz_command);
981 psz_buf[psz_temp - psz_command] = '\0';
983 Unescape (psz_buf, psz_buf);
986 psz_buf += psz_temp - psz_command + 1;
987 psz_command = psz_temp;
989 assert (buf + sizeof (buf) >= psz_buf);
993 * And then Interpret it
996 #define IF_EXECUTE( name, check, cmd ) if( !strcmp(ppsz_command[0], name ) ) { if( (check) ) goto syntax_error; if( (cmd) ) goto error; goto success; }
999 p_message = vlm_MessageNew( "", NULL );
1002 else IF_EXECUTE( "del", (i_command != 2), ExecuteDel(p_vlm, ppsz_command[1], &p_message) )
1003 else IF_EXECUTE( "show", (i_command > 2), ExecuteShow(p_vlm, i_command > 1 ? ppsz_command[1] : NULL, &p_message) )
1004 else IF_EXECUTE( "help", (i_command != 1), ExecuteHelp(p_vlm, &p_message) )
1005 else IF_EXECUTE( "control", (i_command < 3), ExecuteControl(p_vlm, ppsz_command[1], i_command - 2, &ppsz_command[2], &p_message) )
1006 else IF_EXECUTE( "save", (i_command != 2), ExecuteSave(p_vlm, ppsz_command[1], &p_message) )
1007 else IF_EXECUTE( "export", (i_command != 1), ExecuteExport(p_vlm, &p_message) )
1008 else IF_EXECUTE( "load", (i_command != 2), ExecuteLoad(p_vlm, ppsz_command[1], &p_message) )
1009 else IF_EXECUTE( "new", (i_command < 3), ExecuteNew(p_vlm, ppsz_command[1], ppsz_command[2], i_command-3, &ppsz_command[3], &p_message) )
1010 else IF_EXECUTE( "setup", (i_command < 2), ExecuteSetup(p_vlm, ppsz_command[1], i_command-2, &ppsz_command[2], &p_message) )
1013 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
1019 *pp_message = p_message;
1023 return ExecuteSyntaxError( ppsz_command[0], pp_message );
1026 *pp_message = p_message;
1027 return VLC_EGENERIC;
1030 /*****************************************************************************
1032 *****************************************************************************/
1033 vlm_media_sys_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
1037 for( i = 0; i < vlm->i_media; i++ )
1039 if( strcmp( psz_name, vlm->media[i]->cfg.psz_name ) == 0 )
1040 return vlm->media[i];
1046 /*****************************************************************************
1048 *****************************************************************************/
1049 static int64_t vlm_Date(void)
1054 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1056 struct timeval tv_date;
1058 /* gettimeofday() cannot fail given &tv_date is a valid address */
1059 (void)gettimeofday( &tv_date, NULL );
1060 return (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec;
1064 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
1066 vlm_schedule_sys_t *p_sched = malloc( sizeof( vlm_schedule_sys_t ) );
1078 p_sched->psz_name = strdup( psz_name );
1079 p_sched->b_enabled = VLC_FALSE;
1080 p_sched->i_command = 0;
1081 p_sched->command = NULL;
1082 p_sched->i_date = 0;
1083 p_sched->i_period = 0;
1084 p_sched->i_repeat = -1;
1086 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1091 /* for now, simple delete. After, del with options (last arg) */
1092 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched,
1093 const char *psz_name )
1095 if( sched == NULL ) return;
1097 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1099 if( vlm->i_schedule == 0 && vlm->schedule ) free( vlm->schedule );
1100 free( sched->psz_name );
1101 while( sched->i_command )
1103 char *psz_cmd = sched->command[0];
1104 TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1110 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1114 for( i = 0; i < vlm->i_schedule; i++ )
1116 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1118 return vlm->schedule[i];
1125 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1126 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
1127 const char *psz_value )
1129 if( !strcmp( psz_cmd, "enabled" ) )
1131 schedule->b_enabled = VLC_TRUE;
1133 else if( !strcmp( psz_cmd, "disabled" ) )
1135 schedule->b_enabled = VLC_FALSE;
1137 #if !defined( UNDER_CE )
1138 else if( !strcmp( psz_cmd, "date" ) )
1144 time.tm_sec = 0; /* seconds */
1145 time.tm_min = 0; /* minutes */
1146 time.tm_hour = 0; /* hours */
1147 time.tm_mday = 0; /* day of the month */
1148 time.tm_mon = 0; /* month */
1149 time.tm_year = 0; /* year */
1150 time.tm_wday = 0; /* day of the week */
1151 time.tm_yday = 0; /* day in the year */
1152 time.tm_isdst = -1; /* daylight saving time */
1154 /* date should be year/month/day-hour:minutes:seconds */
1155 p = strchr( psz_value, '-' );
1157 if( !strcmp( psz_value, "now" ) )
1159 schedule->i_date = 0;
1161 else if( (p == NULL) && sscanf( psz_value, "%d:%d:%d", &time.tm_hour,
1162 &time.tm_min, &time.tm_sec ) != 3 )
1163 /* it must be a hour:minutes:seconds */
1171 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1189 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1195 time.tm_mon = i - 1;
1199 time.tm_year = i - 1900;
1200 time.tm_mon = j - 1;
1207 date = mktime( &time );
1208 schedule->i_date = ((mtime_t) date) * 1000000;
1211 else if( !strcmp( psz_cmd, "period" ) )
1215 const char *psz_time = NULL, *psz_date = NULL;
1219 /* First, if date or period are modified, repeat should be equal to -1 */
1220 schedule->i_repeat = -1;
1222 time.tm_sec = 0; /* seconds */
1223 time.tm_min = 0; /* minutes */
1224 time.tm_hour = 0; /* hours */
1225 time.tm_mday = 0; /* day of the month */
1226 time.tm_mon = 0; /* month */
1227 time.tm_year = 0; /* year */
1228 time.tm_wday = 0; /* day of the week */
1229 time.tm_yday = 0; /* day in the year */
1230 time.tm_isdst = -1; /* daylight saving time */
1232 /* date should be year/month/day-hour:minutes:seconds */
1233 p = strchr( psz_value, '-' );
1236 psz_date = psz_value;
1241 psz_time = psz_value;
1244 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1263 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1282 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1283 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1284 schedule->i_period = ((mtime_t) date) * 1000000;
1286 #endif /* UNDER_CE */
1287 else if( !strcmp( psz_cmd, "repeat" ) )
1291 if( sscanf( psz_value, "%d", &i ) == 1 )
1293 schedule->i_repeat = i;
1300 else if( !strcmp( psz_cmd, "append" ) )
1302 char *command = strdup( psz_value );
1304 TAB_APPEND( schedule->i_command, schedule->command, command );
1313 /*****************************************************************************
1314 * Message handling functions
1315 *****************************************************************************/
1316 vlm_message_t *vlm_MessageNew( const char *psz_name,
1317 const char *psz_format, ... )
1319 vlm_message_t *p_message;
1322 if( !psz_name ) return NULL;
1324 p_message = malloc( sizeof(vlm_message_t) );
1330 p_message->psz_value = 0;
1334 va_start( args, psz_format );
1335 if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1344 p_message->psz_name = strdup( psz_name );
1345 p_message->i_child = 0;
1346 p_message->child = NULL;
1351 void vlm_MessageDelete( vlm_message_t *p_message )
1353 if( p_message->psz_name ) free( p_message->psz_name );
1354 if( p_message->psz_value ) free( p_message->psz_value );
1355 while( p_message->i_child-- )
1356 vlm_MessageDelete( p_message->child[p_message->i_child] );
1357 if( p_message->child ) free( p_message->child );
1362 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1363 vlm_message_t *p_child )
1365 if( p_message == NULL ) return NULL;
1369 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1375 /*****************************************************************************
1376 * Misc utility functions
1377 *****************************************************************************/
1378 static vlm_message_t *vlm_ShowMedia( vlm_t *p_vlm, vlm_media_sys_t *p_media )
1380 vlm_media_t *p_cfg = &p_media->cfg;
1381 vlm_message_t *p_msg;
1382 vlm_message_t *p_msg_sub;
1385 p_msg = vlm_MessageNew( p_cfg->psz_name, 0 );
1386 vlm_MessageAdd( p_msg,
1387 vlm_MessageNew( "type", p_cfg->b_vod ? "vod" : "broadcast" ) );
1388 vlm_MessageAdd( p_msg,
1389 vlm_MessageNew( "enabled", p_cfg->b_enabled ? "yes" : "no" ) );
1392 vlm_MessageAdd( p_msg,
1393 vlm_MessageNew( "mux", p_cfg->vod.psz_mux ) );
1395 vlm_MessageAdd( p_msg,
1396 vlm_MessageNew( "loop", p_cfg->broadcast.b_loop ? "yes" : "no" ) );
1398 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "inputs", NULL ) );
1399 for( i = 0; i < p_cfg->i_input; i++ )
1402 asprintf( &psz_tmp, "%d", i+1 );
1403 vlm_MessageAdd( p_msg_sub,
1404 vlm_MessageNew( psz_tmp, p_cfg->ppsz_input[i] ) );
1408 vlm_MessageAdd( p_msg,
1409 vlm_MessageNew( "output", p_cfg->psz_output ? p_cfg->psz_output : "" ) );
1411 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "options", 0 ) );
1412 for( i = 0; i < p_cfg->i_option; i++ )
1413 vlm_MessageAdd( p_msg_sub, vlm_MessageNew( p_cfg->ppsz_option[i], 0 ) );
1415 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "instances", NULL ) );
1416 for( i = 0; i < p_media->i_instance; i++ )
1418 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1420 vlm_message_t *p_msg_instance;
1424 if( p_instance->p_input )
1425 var_Get( p_instance->p_input, "state", &val );
1427 p_msg_instance = vlm_MessageAdd( p_msg_sub, vlm_MessageNew( "instance" , NULL ) );
1429 vlm_MessageAdd( p_msg_instance,
1430 vlm_MessageNew( "name" , p_instance->psz_name ? p_instance->psz_name : "default" ) );
1431 vlm_MessageAdd( p_msg_instance,
1432 vlm_MessageNew( "state",
1433 val.i_int == PLAYING_S ? "playing" :
1434 val.i_int == PAUSE_S ? "paused" :
1437 /* FIXME should not do that this way */
1438 if( p_instance->p_input )
1440 #define APPEND_INPUT_INFO( a, format, type ) \
1441 asprintf( &psz_tmp, format, \
1442 var_Get ## type( p_instance->p_input, a ) ); \
1443 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( a, psz_tmp ) ); \
1445 APPEND_INPUT_INFO( "position", "%f", Float );
1446 APPEND_INPUT_INFO( "time", I64Fi, Time );
1447 APPEND_INPUT_INFO( "length", I64Fi, Time );
1448 APPEND_INPUT_INFO( "rate", "%d", Integer );
1449 APPEND_INPUT_INFO( "title", "%d", Integer );
1450 APPEND_INPUT_INFO( "chapter", "%d", Integer );
1451 APPEND_INPUT_INFO( "seekable", "%d", Bool );
1453 #undef APPEND_INPUT_INFO
1454 asprintf( &psz_tmp, "%d", p_instance->i_index + 1 );
1455 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( "playlistindex", psz_tmp ) );
1461 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
1462 vlm_schedule_sys_t *schedule,
1463 const char *psz_filter )
1467 vlm_message_t *p_msg = vlm_MessageNew( "show", NULL );
1469 vlm_MessageAdd( p_msg, vlm_ShowMedia( vlm, media ) );
1473 else if( schedule != NULL )
1477 vlm_message_t *msg_schedule;
1478 vlm_message_t *msg_child;
1481 msg = vlm_MessageNew( "show", NULL );
1483 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, 0 ) );
1485 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1487 vlm_MessageAdd( msg_schedule,
1488 vlm_MessageNew( "enabled", schedule->b_enabled ?
1491 #if !defined( UNDER_CE )
1492 if( schedule->i_date != 0 )
1495 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1498 #ifdef HAVE_LOCALTIME_R
1499 localtime_r( &i_time, &date);
1501 struct tm *p_date = localtime( &i_time );
1505 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1506 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1507 date.tm_hour, date.tm_min, date.tm_sec );
1509 vlm_MessageAdd( msg_schedule,
1510 vlm_MessageNew( "date", psz_date ) );
1515 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1518 if( schedule->i_period != 0 )
1520 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1523 date.tm_sec = (int)( i_time % 60 );
1524 i_time = i_time / 60;
1525 date.tm_min = (int)( i_time % 60 );
1526 i_time = i_time / 60;
1527 date.tm_hour = (int)( i_time % 24 );
1528 i_time = i_time / 24;
1529 date.tm_mday = (int)( i_time % 30 );
1530 i_time = i_time / 30;
1531 /* okay, okay, months are not always 30 days long */
1532 date.tm_mon = (int)( i_time % 12 );
1533 i_time = i_time / 12;
1534 date.tm_year = (int)i_time;
1536 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1537 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1539 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1543 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1545 #endif /* UNDER_CE */
1547 sprintf( buffer, "%d", schedule->i_repeat );
1548 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1551 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", 0) );
1553 for( i = 0; i < schedule->i_command; i++ )
1555 vlm_MessageAdd( msg_child,
1556 vlm_MessageNew( schedule->command[i], NULL ) );
1563 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1565 vlm_message_t *p_msg;
1566 vlm_message_t *p_msg_child;
1567 int i_vod = 0, i_broadcast = 0;
1571 for( i = 0; i < vlm->i_media; i++ )
1573 if( vlm->media[i]->cfg.b_vod )
1579 asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1581 p_msg = vlm_MessageNew( "show", NULL );
1582 p_msg_child = vlm_MessageAdd( p_msg, vlm_MessageNew( "media", psz_count ) );
1585 for( i = 0; i < vlm->i_media; i++ )
1586 vlm_MessageAdd( p_msg_child, vlm_ShowMedia( vlm, vlm->media[i] ) );
1591 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1595 vlm_message_t *msg_child;
1597 msg = vlm_MessageNew( "show", NULL );
1598 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", NULL ) );
1600 for( i = 0; i < vlm->i_schedule; i++ )
1602 vlm_schedule_sys_t *s = vlm->schedule[i];
1603 vlm_message_t *msg_schedule;
1604 mtime_t i_time, i_next_date;
1606 msg_schedule = vlm_MessageAdd( msg_child,
1607 vlm_MessageNew( s->psz_name, 0 ) );
1608 vlm_MessageAdd( msg_schedule,
1609 vlm_MessageNew( "enabled", s->b_enabled ?
1612 /* calculate next date */
1613 i_time = vlm_Date();
1614 i_next_date = s->i_date;
1616 if( s->i_period != 0 )
1619 while( s->i_date + j * s->i_period <= i_time &&
1625 i_next_date = s->i_date + j * s->i_period;
1628 if( i_next_date > i_time )
1630 time_t i_date = (time_t) (i_next_date / 1000000) ;
1632 #if !defined( UNDER_CE )
1635 ctime_r( &i_date, psz_date );
1637 char *psz_date = ctime( &i_date );
1640 vlm_MessageAdd( msg_schedule,
1641 vlm_MessageNew( "next launch", psz_date ) );
1649 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
1651 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1652 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1654 vlm_MessageAdd( show1, show2->child[0] );
1656 /* We must destroy the parent node "show" of show2
1657 * and not the children */
1658 free( show2->psz_name );
1666 return vlm_MessageNew( "show", NULL );
1670 static vlm_message_t *vlm_Help( vlm_t *vlm, char *psz_filter )
1672 vlm_message_t *message, *message_child;
1674 #define MessageAdd( a ) \
1675 vlm_MessageAdd( message, vlm_MessageNew( a, NULL ) );
1676 #define MessageAddChild( a ) \
1677 vlm_MessageAdd( message_child, vlm_MessageNew( a, NULL ) );
1679 if( psz_filter == NULL )
1681 message = vlm_MessageNew( "help", NULL );
1683 message_child = MessageAdd( "Commands Syntax:" );
1684 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
1685 MessageAddChild( "setup (name) (properties)" );
1686 MessageAddChild( "show [(name)|media|schedule]" );
1687 MessageAddChild( "del (name)|all|media|schedule" );
1688 MessageAddChild( "control (name) [instance_name] (command)" );
1689 MessageAddChild( "save (config_file)" );
1690 MessageAddChild( "export" );
1691 MessageAddChild( "load (config_file)" );
1693 message_child = MessageAdd( "Media Proprieties Syntax:" );
1694 MessageAddChild( "input (input_name)" );
1695 MessageAddChild( "inputdel (input_name)|all" );
1696 MessageAddChild( "inputdeln input_number" );
1697 MessageAddChild( "output (output_name)" );
1698 MessageAddChild( "option (option_name)[=value]" );
1699 MessageAddChild( "enabled|disabled" );
1700 MessageAddChild( "loop|unloop (broadcast only)" );
1701 MessageAddChild( "mux (mux_name)" );
1703 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
1704 MessageAddChild( "enabled|disabled" );
1705 MessageAddChild( "append (command_until_rest_of_the_line)" );
1706 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
1708 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
1709 "(days)-(hours):(minutes):(seconds)" );
1710 MessageAddChild( "repeat (number_of_repetitions)" );
1712 message_child = MessageAdd( "Control Commands Syntax:" );
1713 MessageAddChild( "play [input_number]" );
1714 MessageAddChild( "pause" );
1715 MessageAddChild( "stop" );
1716 MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](miliseconds)ms" );
1721 return vlm_MessageNew( "help", NULL );
1724 /*****************************************************************************
1725 * Config handling functions
1726 *****************************************************************************/
1727 static int Load( vlm_t *vlm, char *file )
1732 while( *pf != '\0' )
1734 vlm_message_t *message = NULL;
1737 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1742 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1746 if( pf[i_end] == '\n' ) i_end++;
1749 if( *pf && ExecuteCommand( vlm, pf, &message ) )
1753 if( message->psz_value )
1754 msg_Err( vlm, "Load error on line %d: %s: %s",
1755 i_line, message->psz_name, message->psz_value );
1756 vlm_MessageDelete( message );
1760 if( message ) vlm_MessageDelete( message );
1769 static char *Save( vlm_t *vlm )
1772 char psz_header[] = "\n"
1773 "# VLC media player VLM command batch\n"
1774 "# http://www.videolan.org/vlc/\n\n" ;
1777 int i_length = strlen( psz_header );
1779 for( i = 0; i < vlm->i_media; i++ )
1781 vlm_media_sys_t *media = vlm->media[i];
1782 vlm_media_t *p_cfg = &media->cfg;
1785 i_length += strlen( "new * vod " ) + strlen(p_cfg->psz_name);
1787 i_length += strlen( "new * broadcast " ) + strlen(p_cfg->psz_name);
1789 if( p_cfg->b_enabled == VLC_TRUE )
1790 i_length += strlen( "enabled" );
1792 i_length += strlen( "disabled" );
1794 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop == VLC_TRUE )
1795 i_length += strlen( " loop\n" );
1797 i_length += strlen( "\n" );
1799 for( j = 0; j < p_cfg->i_input; j++ )
1800 i_length += strlen( "setup * input \"\"\n" ) + strlen( p_cfg->psz_name ) + strlen( p_cfg->ppsz_input[j] );
1802 if( p_cfg->psz_output != NULL )
1803 i_length += strlen( "setup * output \n" ) + strlen(p_cfg->psz_name) + strlen(p_cfg->psz_output);
1805 for( j = 0; j < p_cfg->i_option; j++ )
1806 i_length += strlen("setup * option \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->ppsz_option[j]);
1808 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1809 i_length += strlen("setup * mux \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->vod.psz_mux);
1812 for( i = 0; i < vlm->i_schedule; i++ )
1814 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1816 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
1818 if( schedule->b_enabled == VLC_TRUE )
1820 i_length += strlen( "date //-:: enabled\n" ) + 14;
1824 i_length += strlen( "date //-:: disabled\n" ) + 14;
1828 if( schedule->i_period != 0 )
1830 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
1831 strlen( "period //-::\n" ) + 14;
1834 if( schedule->i_repeat >= 0 )
1838 sprintf( buffer, "%d", schedule->i_repeat );
1839 i_length += strlen( "setup repeat \n" ) +
1840 strlen( schedule->psz_name ) + strlen( buffer );
1847 for( j = 0; j < schedule->i_command; j++ )
1849 i_length += strlen( "setup append \n" ) +
1850 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
1855 /* Don't forget the '\0' */
1857 /* now we have the length of save */
1859 p = save = malloc( i_length );
1860 if( !save ) return NULL;
1863 p += sprintf( p, "%s", psz_header );
1865 /* finally we can write in it */
1866 for( i = 0; i < vlm->i_media; i++ )
1868 vlm_media_sys_t *media = vlm->media[i];
1869 vlm_media_t *p_cfg = &media->cfg;
1872 p += sprintf( p, "new %s vod ", p_cfg->psz_name );
1874 p += sprintf( p, "new %s broadcast ", p_cfg->psz_name );
1876 if( p_cfg->b_enabled )
1877 p += sprintf( p, "enabled" );
1879 p += sprintf( p, "disabled" );
1881 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1882 p += sprintf( p, " loop\n" );
1884 p += sprintf( p, "\n" );
1886 for( j = 0; j < p_cfg->i_input; j++ )
1887 p += sprintf( p, "setup %s input \"%s\"\n", p_cfg->psz_name, p_cfg->ppsz_input[j] );
1889 if( p_cfg->psz_output )
1890 p += sprintf( p, "setup %s output %s\n", p_cfg->psz_name, p_cfg->psz_output );
1892 for( j = 0; j < p_cfg->i_option; j++ )
1893 p += sprintf( p, "setup %s option %s\n", p_cfg->psz_name, p_cfg->ppsz_option[j] );
1895 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1896 p += sprintf( p, "setup %s mux %s\n", p_cfg->psz_name, p_cfg->vod.psz_mux );
1899 /* and now, the schedule scripts */
1900 #if !defined( UNDER_CE )
1901 for( i = 0; i < vlm->i_schedule; i++ )
1903 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1905 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
1907 #ifdef HAVE_LOCALTIME_R
1908 localtime_r( &i_time, &date);
1910 struct tm *p_date = localtime( &i_time );
1914 p += sprintf( p, "new %s schedule ", schedule->psz_name);
1916 if( schedule->b_enabled == VLC_TRUE )
1918 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
1919 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1920 date.tm_hour, date.tm_min, date.tm_sec );
1924 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
1925 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1926 date.tm_hour, date.tm_min, date.tm_sec);
1929 if( schedule->i_period != 0 )
1931 p += sprintf( p, "setup %s ", schedule->psz_name );
1933 i_time = (time_t) ( schedule->i_period / 1000000 );
1935 date.tm_sec = (int)( i_time % 60 );
1936 i_time = i_time / 60;
1937 date.tm_min = (int)( i_time % 60 );
1938 i_time = i_time / 60;
1939 date.tm_hour = (int)( i_time % 24 );
1940 i_time = i_time / 24;
1941 date.tm_mday = (int)( i_time % 30 );
1942 i_time = i_time / 30;
1943 /* okay, okay, months are not always 30 days long */
1944 date.tm_mon = (int)( i_time % 12 );
1945 i_time = i_time / 12;
1946 date.tm_year = (int)i_time;
1948 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
1949 date.tm_year, date.tm_mon, date.tm_mday,
1950 date.tm_hour, date.tm_min, date.tm_sec);
1953 if( schedule->i_repeat >= 0 )
1955 p += sprintf( p, "setup %s repeat %d\n",
1956 schedule->psz_name, schedule->i_repeat );
1960 p += sprintf( p, "\n" );
1963 for( j = 0; j < schedule->i_command; j++ )
1965 p += sprintf( p, "setup %s append %s\n",
1966 schedule->psz_name, schedule->command[j] );
1970 #endif /* UNDER_CE */
1975 /*****************************************************************************
1977 *****************************************************************************/
1978 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
1979 const char *psz_id, int i_query, va_list args )
1981 vlm_t *vlm = (vlm_t *)p_private;
1986 if( !p_private || !p_vod_media )
1987 return VLC_EGENERIC;
1989 vlc_mutex_lock( &vlm->lock );
1992 for( i = 0, id = -1; i < vlm->i_media; i++ )
1994 if( p_vod_media == vlm->media[i]->vod.p_media )
1996 id = vlm->media[i]->cfg.id;
2002 vlc_mutex_unlock( &vlm->lock );
2003 return VLC_EGENERIC;
2008 case VOD_MEDIA_PLAY:
2009 psz = (const char *)va_arg( args, const char * );
2010 i_ret = vlm_ControlInternal( vlm, VLM_START_MEDIA_VOD_INSTANCE, id, psz_id, 0, psz );
2013 case VOD_MEDIA_PAUSE:
2014 i_ret = vlm_ControlInternal( vlm, VLM_PAUSE_MEDIA_INSTANCE, id, psz_id );
2017 case VOD_MEDIA_STOP:
2018 i_ret = vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, id, psz_id );
2021 case VOD_MEDIA_SEEK:
2023 double d_position = (double)va_arg( args, double );
2024 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position/100.0 );
2028 case VOD_MEDIA_REWIND:
2030 double d_scale = (double)va_arg( args, double );
2033 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2034 d_position -= (d_scale / 1000.0);
2035 if( d_position < 0.0 )
2037 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2041 case VOD_MEDIA_FORWARD:
2043 double d_scale = (double)va_arg( args, double );
2046 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2047 d_position += (d_scale / 1000.0);
2048 if( d_position > 1.0 )
2050 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2055 i_ret = VLC_EGENERIC;
2059 vlc_mutex_unlock( &vlm->lock );
2065 /*****************************************************************************
2067 *****************************************************************************/
2068 static int Manage( vlc_object_t* p_object )
2070 vlm_t *vlm = (vlm_t*)p_object;
2072 mtime_t i_lastcheck;
2075 i_lastcheck = vlm_Date();
2077 while( !vlm->b_die )
2079 char **ppsz_scheduled_commands = NULL;
2080 int i_scheduled_commands = 0;
2082 vlc_mutex_lock( &vlm->lock );
2084 /* destroy the inputs that wants to die, and launch the next input */
2085 for( i = 0; i < vlm->i_media; i++ )
2087 vlm_media_sys_t *p_media = vlm->media[i];
2089 for( j = 0; j < p_media->i_instance; )
2091 vlm_media_instance_sys_t *p_instance = p_media->instance[j];
2093 if( p_instance->p_input && ( p_instance->p_input->b_eof || p_instance->p_input->b_error ) )
2095 int i_new_input_index;
2098 i_new_input_index = p_instance->i_index + 1;
2099 if( !p_media->cfg.b_vod && p_media->cfg.broadcast.b_loop && i_new_input_index >= p_media->cfg.i_input )
2100 i_new_input_index = 0;
2102 /* FIXME implement multiple input with VOD */
2103 if( p_media->cfg.b_vod || i_new_input_index >= p_media->cfg.i_input )
2104 vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, p_instance->psz_name );
2106 vlm_ControlInternal( vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, p_instance->psz_name, i_new_input_index );
2118 i_time = vlm_Date();
2120 for( i = 0; i < vlm->i_schedule; i++ )
2122 mtime_t i_real_date = vlm->schedule[i]->i_date;
2124 if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2126 if( vlm->schedule[i]->i_date == 0 ) // now !
2128 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2129 i_real_date = i_time;
2131 else if( vlm->schedule[i]->i_period != 0 )
2134 while( vlm->schedule[i]->i_date + j *
2135 vlm->schedule[i]->i_period <= i_lastcheck &&
2136 ( vlm->schedule[i]->i_repeat > j ||
2137 vlm->schedule[i]->i_repeat == -1 ) )
2142 i_real_date = vlm->schedule[i]->i_date + j *
2143 vlm->schedule[i]->i_period;
2146 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2148 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2150 TAB_APPEND( i_scheduled_commands,
2151 ppsz_scheduled_commands,
2152 strdup(vlm->schedule[i]->command[j] ) );
2157 while( i_scheduled_commands )
2159 vlm_message_t *message = NULL;
2160 char *psz_command = ppsz_scheduled_commands[0];
2161 ExecuteCommand( vlm, psz_command,&message );
2163 /* for now, drop the message */
2164 vlm_MessageDelete( message );
2165 TAB_REMOVE( i_scheduled_commands,
2166 ppsz_scheduled_commands,
2168 free( psz_command );
2171 i_lastcheck = i_time;
2173 vlc_mutex_unlock( &vlm->lock );
2188 int i_connection_count;
2189 int i_connection_active;
2194 vlc_bool_t b_playing;
2195 int i_playing_index;
2198 } vlm_media_status_t;
2202 static vlm_media_sys_t *vlm_ControlMediaGetById( vlm_t *p_vlm, int64_t id )
2206 for( i = 0; i < p_vlm->i_media; i++ )
2208 if( p_vlm->media[i]->cfg.id == id )
2209 return p_vlm->media[i];
2213 static vlm_media_sys_t *vlm_ControlMediaGetByName( vlm_t *p_vlm, const char *psz_name )
2217 for( i = 0; i < p_vlm->i_media; i++ )
2219 if( !strcmp( p_vlm->media[i]->cfg.psz_name, psz_name ) )
2220 return p_vlm->media[i];
2224 static int vlm_MediaDescriptionCheck( vlm_t *p_vlm, vlm_media_t *p_cfg )
2228 if( !p_cfg || !p_cfg->psz_name ||
2229 !strcmp( p_cfg->psz_name, "all" ) || !strcmp( p_cfg->psz_name, "media" ) || !strcmp( p_cfg->psz_name, "schedule" ) )
2230 return VLC_EGENERIC;
2232 for( i = 0; i < p_vlm->i_media; i++ )
2234 if( p_vlm->media[i]->cfg.id == p_cfg->id )
2236 if( !strcmp( p_vlm->media[i]->cfg.psz_name, p_cfg->psz_name ) )
2237 return VLC_EGENERIC;
2243 /* Called after a media description is changed/added */
2244 static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
2246 vlm_media_t *p_cfg = &p_media->cfg;
2247 /* Check if we need to create/delete a vod media */
2250 if( !p_cfg->b_enabled && p_media->vod.p_media )
2252 p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
2253 p_media->vod.p_media = NULL;
2255 else if( p_cfg->b_enabled && !p_media->vod.p_media && p_cfg->i_input )
2257 /* Pre-parse the input */
2258 input_thread_t *p_input;
2264 input_ItemClean( &p_media->vod.item );
2265 input_ItemInit( VLC_OBJECT(p_vlm), &p_media->vod.item );
2267 if( p_cfg->psz_output )
2268 asprintf( &psz_output, "%s:description", p_cfg->psz_output );
2270 asprintf( &psz_output, "#description" );
2272 p_media->vod.item.psz_uri = strdup( p_cfg->ppsz_input[0] );
2274 TAB_INIT( p_media->vod.item.i_options, p_media->vod.item.ppsz_options );
2276 asprintf( &psz_dup, "sout=%s", psz_output);
2277 TAB_APPEND( p_media->vod.item.i_options, p_media->vod.item.ppsz_options, psz_dup );
2278 for( i = 0; i < p_cfg->i_option; i++ )
2280 psz_dup = strdup( p_cfg->ppsz_option[i] );
2281 TAB_APPEND( p_media->vod.item.i_options, p_media->vod.item.ppsz_options, psz_dup );
2283 psz_dup = strdup( "no-sout-keep" );
2284 TAB_APPEND( p_media->vod.item.i_options, p_media->vod.item.ppsz_options, psz_dup );
2286 asprintf( &psz_header, _("Media: %s"), p_cfg->psz_name );
2288 if( (p_input = input_CreateThreadExtended( p_vlm, &p_media->vod.item, psz_header, NULL ) ) )
2290 while( !p_input->b_eof && !p_input->b_error )
2293 input_StopThread( p_input );
2294 input_DestroyThreadExtended( p_input, NULL );
2299 if( p_cfg->vod.psz_mux )
2302 es_format_t es, *p_es = &es;
2305 sprintf( fourcc, "%4.4s", p_cfg->vod.psz_mux );
2306 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
2307 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
2309 item = p_media->vod.item;
2312 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
2314 p_media->vod.p_media =
2315 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
2319 p_media->vod.p_media =
2320 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &p_media->vod.item );
2326 /* TODO start media if needed */
2329 /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
2333 static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
2335 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
2338 if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
2339 return VLC_EGENERIC;
2340 if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
2341 return VLC_EGENERIC;
2345 /* TODO check what are the changes being done (stop instance if needed) */
2348 vlm_media_Clean( &p_media->cfg );
2349 vlm_media_Copy( &p_media->cfg, p_cfg );
2351 return vlm_OnMediaUpdate( p_vlm, p_media );
2354 static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id )
2356 vlm_media_sys_t *p_media;
2358 if( vlm_MediaDescriptionCheck( p_vlm, p_cfg ) || vlm_ControlMediaGetByName( p_vlm, p_cfg->psz_name ) )
2360 msg_Err( p_vlm, "invalid media description" );
2361 return VLC_EGENERIC;
2363 /* Check if we need to load the VOD server */
2364 if( p_cfg->b_vod && !p_vlm->i_vod )
2366 p_vlm->p_vod = vlc_object_create( p_vlm, VLC_OBJECT_VOD );
2367 vlc_object_attach( p_vlm->p_vod, p_vlm );
2368 p_vlm->p_vod->p_module = module_Need( p_vlm->p_vod, "vod server", 0, 0 );
2369 if( !p_vlm->p_vod->p_module )
2371 msg_Err( p_vlm, "cannot find vod server" );
2372 vlc_object_detach( p_vlm->p_vod );
2373 vlc_object_destroy( p_vlm->p_vod );
2375 return VLC_EGENERIC;
2378 p_vlm->p_vod->p_data = p_vlm;
2379 p_vlm->p_vod->pf_media_control = vlm_MediaVodControl;
2382 p_media = malloc( sizeof( vlm_media_sys_t ) );
2385 msg_Err( p_vlm, "out of memory" );
2388 memset( p_media, 0, sizeof(vlm_media_sys_t) );
2393 vlm_media_Copy( &p_media->cfg, p_cfg );
2394 p_media->cfg.id = p_vlm->i_id++;
2395 /* FIXME do we do something here if enabled is true ? */
2397 input_ItemInit( VLC_OBJECT(p_vlm), &p_media->vod.item );
2399 p_media->vod.p_media = NULL;
2400 TAB_INIT( p_media->i_instance, p_media->instance );
2403 TAB_APPEND( p_vlm->i_media, p_vlm->media, p_media );
2406 *p_id = p_media->cfg.id;
2408 return vlm_OnMediaUpdate( p_vlm, p_media );
2411 static int vlm_ControlMediaDel( vlm_t *p_vlm, int64_t id )
2413 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2416 return VLC_EGENERIC;
2418 while( p_media->i_instance > 0 )
2419 vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, p_media->instance[0]->psz_name );
2421 if( p_media->cfg.b_vod )
2423 p_media->cfg.b_enabled = VLC_FALSE;
2424 vlm_OnMediaUpdate( p_vlm, p_media );
2428 vlm_media_Clean( &p_media->cfg );
2430 input_ItemClean( &p_media->vod.item );
2432 TAB_REMOVE( p_vlm->i_media, p_vlm->media, p_media );
2436 /* Check if we need to unload the VOD server */
2437 if( p_vlm->p_vod && p_vlm->i_vod <= 0 )
2439 module_Unneed( p_vlm->p_vod, p_vlm->p_vod->p_module );
2440 vlc_object_detach( p_vlm->p_vod );
2441 vlc_object_destroy( p_vlm->p_vod );
2442 p_vlm->p_vod = NULL;
2447 static int vlm_ControlMediaGets( vlm_t *p_vlm, vlm_media_t ***ppp_dsc, int *pi_dsc )
2449 vlm_media_t **pp_dsc;
2453 TAB_INIT( i_dsc, pp_dsc );
2454 for( i = 0; i < p_vlm->i_media; i++ )
2456 vlm_media_t *p_dsc = vlm_media_Duplicate( &p_vlm->media[i]->cfg );
2457 TAB_APPEND( i_dsc, pp_dsc, p_dsc );
2465 static int vlm_ControlMediaClear( vlm_t *p_vlm )
2467 while( p_vlm->i_media > 0 )
2468 vlm_ControlMediaDel( p_vlm, p_vlm->media[0]->cfg.id );
2472 static int vlm_ControlMediaGet( vlm_t *p_vlm, int64_t id, vlm_media_t **pp_dsc )
2474 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2476 return VLC_EGENERIC;
2478 *pp_dsc = vlm_media_Duplicate( &p_media->cfg );
2481 static int vlm_ControlMediaGetId( vlm_t *p_vlm, const char *psz_name, int64_t *p_id )
2483 vlm_media_sys_t *p_media = vlm_ControlMediaGetByName( p_vlm, psz_name );
2485 return VLC_EGENERIC;
2487 *p_id = p_media->cfg.id;
2491 static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
2495 for( i = 0; i < p_media->i_instance; i++ )
2497 const char *psz = p_media->instance[i]->psz_name;
2498 if( ( psz == NULL && psz_id == NULL ) ||
2499 ( psz && psz_id && !strcmp( psz, psz_id ) ) )
2500 return p_media->instance[i];
2504 static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
2506 vlm_media_instance_sys_t *p_instance = malloc( sizeof(vlm_media_instance_sys_t) );
2510 memset( p_instance, 0, sizeof(vlm_media_instance_sys_t) );
2512 p_instance->psz_name = NULL;
2514 p_instance->psz_name = strdup( psz_name );
2516 input_ItemInit( VLC_OBJECT(p_vlm), &p_instance->item );
2518 p_instance->i_index = 0;
2519 p_instance->b_sout_keep = VLC_FALSE;
2520 p_instance->p_input = NULL;
2521 p_instance->p_sout = NULL;
2525 static void vlm_MediaInstanceDelete( vlm_media_instance_sys_t *p_instance )
2527 if( p_instance->p_input )
2529 input_StopThread( p_instance->p_input );
2530 input_DestroyThreadExtended( p_instance->p_input, &p_instance->p_sout );
2532 if( p_instance->p_sout )
2533 sout_DeleteInstance( p_instance->p_sout );
2535 input_ItemClean( &p_instance->item );
2536 if( p_instance->psz_name )
2537 free( p_instance->psz_name );
2542 static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
2544 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2545 vlm_media_instance_sys_t *p_instance;
2548 if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
2549 return VLC_EGENERIC;
2551 /* TODO support multiple input for VOD with sout-keep ? */
2553 if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
2554 return VLC_EGENERIC;
2556 if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
2557 return VLC_EGENERIC;
2559 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2562 vlm_media_t *p_cfg = &p_media->cfg;
2566 p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
2570 TAB_INIT( p_instance->item.i_options, p_instance->item.ppsz_options );
2572 if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
2575 asprintf( &psz_buffer, "sout=%s%s%s",
2576 p_cfg->psz_output ? p_cfg->psz_output : "",
2577 (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
2578 psz_vod_output ? psz_vod_output : "" );
2579 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, psz_buffer );
2582 for( i = 0; i < p_cfg->i_option; i++ )
2584 if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
2585 p_instance->b_sout_keep = VLC_TRUE;
2586 else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
2587 p_instance->b_sout_keep = VLC_FALSE;
2589 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, strdup( p_cfg->ppsz_option[i] ) );
2591 /* We force the right sout-keep value (avoid using the sout-keep from the global configuration)
2592 * FIXME implement input list for VOD (need sout-keep)
2594 if( !p_cfg->b_vod && p_instance->b_sout_keep )
2595 psz_keep = strdup( "sout-keep" );
2597 psz_keep = strdup( "no-sout-keep" );
2598 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, psz_keep );
2600 TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
2603 /* Stop old instance */
2604 if( p_instance->p_input )
2606 if( p_instance->i_index == i_input_index &&
2607 !p_instance->p_input->b_eof && !p_instance->p_input->b_error )
2609 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2610 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2614 input_StopThread( p_instance->p_input );
2615 input_DestroyThreadExtended( p_instance->p_input, &p_instance->p_sout );
2616 if( !p_instance->b_sout_keep && p_instance->p_sout )
2618 sout_DeleteInstance( p_instance->p_sout );
2619 p_instance->p_sout = NULL;
2624 p_instance->i_index = i_input_index;
2625 input_item_SetURI( &p_instance->item, p_media->cfg.ppsz_input[p_instance->i_index] ) ;
2627 asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name );
2628 p_instance->p_input = input_CreateThreadExtended( p_vlm, &p_instance->item, psz_log, p_instance->p_sout );
2629 if( !p_instance->p_input )
2631 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2632 vlm_MediaInstanceDelete( p_instance );
2639 static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
2641 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2642 vlm_media_instance_sys_t *p_instance;
2645 return VLC_EGENERIC;
2647 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2649 return VLC_EGENERIC;
2651 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2653 vlm_MediaInstanceDelete( p_instance );
2657 static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
2659 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2660 vlm_media_instance_sys_t *p_instance;
2664 return VLC_EGENERIC;
2666 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2667 if( !p_instance || !p_instance->p_input )
2668 return VLC_EGENERIC;
2670 /* Toggle pause state */
2671 i_state = var_GetInteger( p_instance->p_input, "state" );
2672 if( i_state == PAUSE_S )
2673 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2674 else if( i_state == PLAYING_S )
2675 var_SetInteger( p_instance->p_input, "state", PAUSE_S );
2678 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
2680 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2681 vlm_media_instance_sys_t *p_instance;
2684 return VLC_EGENERIC;
2686 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2687 if( !p_instance || !p_instance->p_input )
2688 return VLC_EGENERIC;
2691 *pi_time = var_GetTime( p_instance->p_input, "time" );
2693 *pd_position = var_GetFloat( p_instance->p_input, "position" );
2696 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t i_time, double d_position )
2698 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2699 vlm_media_instance_sys_t *p_instance;
2702 return VLC_EGENERIC;
2704 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2705 if( !p_instance || !p_instance->p_input )
2706 return VLC_EGENERIC;
2709 return var_SetTime( p_instance->p_input, "time", i_time );
2710 else if( d_position >= 0 && d_position <= 100 )
2711 return var_SetFloat( p_instance->p_input, "position", d_position );
2712 return VLC_EGENERIC;
2715 static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_instance_t ***ppp_idsc, int *pi_instance )
2717 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2718 vlm_media_instance_t **pp_idsc;
2723 return VLC_EGENERIC;
2725 TAB_INIT( i_idsc, pp_idsc );
2726 for( i = 0; i < p_media->i_instance; i++ )
2728 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
2729 vlm_media_instance_t *p_idsc = vlm_media_instance_New();
2731 if( p_instance->psz_name )
2732 p_idsc->psz_name = strdup( p_instance->psz_name );
2733 if( p_instance->p_input )
2735 p_idsc->i_time = var_GetTime( p_instance->p_input, "time" );
2736 p_idsc->i_length = var_GetTime( p_instance->p_input, "length" );
2737 p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
2738 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2739 p_idsc->b_paused = VLC_TRUE;
2740 p_idsc->i_rate = var_GetInteger( p_instance->p_input, "rate" );
2743 TAB_APPEND( i_idsc, pp_idsc, p_idsc );
2745 *ppp_idsc = pp_idsc;
2746 *pi_instance = i_idsc;
2749 static int vlm_ControlMediaInstanceClear( vlm_t *p_vlm, int64_t id )
2751 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2754 return VLC_EGENERIC;
2756 while( p_media->i_instance > 0 )
2757 vlm_ControlMediaInstanceStop( p_vlm, id, p_media->instance[0]->psz_name );
2762 static int vlm_ControlScheduleClear( vlm_t *p_vlm )
2764 while( p_vlm->i_schedule > 0 )
2765 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
2769 static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
2772 vlm_media_t **pp_dsc;
2773 vlm_media_t ***ppp_dsc;
2774 vlm_media_instance_t ***ppp_idsc;
2776 const char *psz_vod;
2790 case VLM_GET_MEDIAS:
2791 ppp_dsc = (vlm_media_t ***)va_arg( args, vlm_media_t *** );
2792 pi_int = (int *)va_arg( args, int * );
2793 return vlm_ControlMediaGets( p_vlm, ppp_dsc, pi_int );
2795 case VLM_CLEAR_MEDIAS:
2796 return vlm_ControlMediaClear( p_vlm );
2798 case VLM_CHANGE_MEDIA:
2799 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2800 return vlm_ControlMediaChange( p_vlm, p_dsc );
2803 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2804 p_id = (int64_t*)va_arg( args, int64_t * );
2805 return vlm_ControlMediaAdd( p_vlm, p_dsc, p_id );
2808 id = (int64_t)va_arg( args, int64_t );
2809 return vlm_ControlMediaDel( p_vlm, id );
2812 id = (int64_t)va_arg( args, int64_t );
2813 pp_dsc = (vlm_media_t **)va_arg( args, vlm_media_t ** );
2814 return vlm_ControlMediaGet( p_vlm, id, pp_dsc );
2816 case VLM_GET_MEDIA_ID:
2817 psz_id = (const char*)va_arg( args, const char * );
2818 p_id = (int64_t*)va_arg( args, int64_t * );
2819 return vlm_ControlMediaGetId( p_vlm, psz_id, p_id );
2822 /* Media instance control */
2823 case VLM_GET_MEDIA_INSTANCES:
2824 id = (int64_t)va_arg( args, int64_t );
2825 ppp_idsc = (vlm_media_instance_t ***)va_arg( args, vlm_media_instance_t *** );
2826 pi_int = (int *)va_arg( args, int *);
2827 return vlm_ControlMediaInstanceGets( p_vlm, id, ppp_idsc, pi_int );
2829 case VLM_CLEAR_MEDIA_INSTANCES:
2830 id = (int64_t)va_arg( args, int64_t );
2831 return vlm_ControlMediaInstanceClear( p_vlm, id );
2834 case VLM_START_MEDIA_BROADCAST_INSTANCE:
2835 id = (int64_t)va_arg( args, int64_t );
2836 psz_id = (const char*)va_arg( args, const char* );
2837 i_int = (int)va_arg( args, int );
2838 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, NULL );
2840 case VLM_START_MEDIA_VOD_INSTANCE:
2841 id = (int64_t)va_arg( args, int64_t );
2842 psz_id = (const char*)va_arg( args, const char* );
2843 i_int = (int)va_arg( args, int );
2844 psz_vod = (const char*)va_arg( args, const char* );
2846 return VLC_EGENERIC;
2847 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, psz_vod );
2849 case VLM_STOP_MEDIA_INSTANCE:
2850 id = (int64_t)va_arg( args, int64_t );
2851 psz_id = (const char*)va_arg( args, const char* );
2852 return vlm_ControlMediaInstanceStop( p_vlm, id, psz_id );
2854 case VLM_PAUSE_MEDIA_INSTANCE:
2855 id = (int64_t)va_arg( args, int64_t );
2856 psz_id = (const char*)va_arg( args, const char* );
2857 return vlm_ControlMediaInstancePause( p_vlm, id, psz_id );
2859 case VLM_GET_MEDIA_INSTANCE_TIME:
2860 id = (int64_t)va_arg( args, int64_t );
2861 psz_id = (const char*)va_arg( args, const char* );
2862 pi_i64 = (int64_t*)va_arg( args, int64_t * );
2863 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, pi_i64, NULL );
2864 case VLM_GET_MEDIA_INSTANCE_POSITION:
2865 id = (int64_t)va_arg( args, int64_t );
2866 psz_id = (const char*)va_arg( args, const char* );
2867 pd_double = (double*)va_arg( args, double* );
2868 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, NULL, pd_double );
2870 case VLM_SET_MEDIA_INSTANCE_TIME:
2871 id = (int64_t)va_arg( args, int64_t );
2872 psz_id = (const char*)va_arg( args, const char* );
2873 i_i64 = (int64_t)va_arg( args, int64_t);
2874 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, i_i64, -1 );
2875 case VLM_SET_MEDIA_INSTANCE_POSITION:
2876 id = (int64_t)va_arg( args, int64_t );
2877 psz_id = (const char*)va_arg( args, const char* );
2878 d_double = (double)va_arg( args, double );
2879 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, -1, d_double );
2881 case VLM_CLEAR_SCHEDULES:
2882 return vlm_ControlScheduleClear( p_vlm );
2885 msg_Err( p_vlm, "unknown VLM query" );
2886 return VLC_EGENERIC;
2889 static int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... )
2894 va_start( args, i_query );
2895 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2901 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2906 va_start( args, i_query );
2908 vlc_mutex_lock( &p_vlm->lock );
2909 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2910 vlc_mutex_unlock( &p_vlm->lock );
2917 #else /* ENABLE_VLM */
2919 /* We just define an empty wrapper */
2920 vlm_t *__vlm_New( vlc_object_t *a )
2922 msg_Err( a, "VideoLAN manager support is disabled" );
2925 void vlm_Delete( vlm_t *a )
2929 int vlm_ExecuteCommand( vlm_t *a, char *b, vlm_message_t **c )
2933 vlm_message_t *vlm_MessageNew( const char *psz_name,
2934 const char *psz_format, ... )
2938 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
2939 vlm_message_t *p_child )
2943 void vlm_MessageDelete( vlm_message_t *a )
2948 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2950 return VLC_EGENERIC;
2953 #endif /* ENABLE_VLM */