1 /*****************************************************************************
2 * vlm.c: VLM interface plugin
3 *****************************************************************************
4 * Copyright (C) 2000-2005 the VideoLAN team
7 * Authors: Simon Latapie <garf@videolan.org>
8 * Laurent Aimar <fenrir@videolan.org>
9 * Gildas Bazin <gbazin@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
36 #include <ctype.h> /* tolower() */
44 # include <sys/time.h> /* gettimeofday() */
48 # include <time.h> /* ctime() */
49 # include <sys/timeb.h> /* ftime() */
52 #include <vlc_input.h>
53 #include "input_internal.h"
54 #include <vlc_stream.h>
55 #include "vlm_internal.h"
57 #include <vlc_charset.h>
59 #include "../stream_output/stream_output.h"
60 #include "../libvlc.h"
62 /*****************************************************************************
64 *****************************************************************************/
66 /* ugly kludge to avoid "null format string" warnings,
67 * even if we handle NULL format string in vlm_MessageNew() */
68 static const char *vlm_NULL = NULL;
70 static void vlm_Destructor( vlm_t *p_vlm );
73 static int vlm_ControlInternal( vlm_t *, int, ... );
76 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_sys_t *, vlm_schedule_sys_t *, const char * );
78 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *, const char * );
80 static char *Save( vlm_t * );
81 static int Load( vlm_t *, char * );
83 static int ExecuteCommand( vlm_t *, const char *, vlm_message_t ** );
85 static int Manage( vlc_object_t * );
87 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name );
88 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched );
89 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
90 const char *psz_value );
92 static int vlm_MediaVodControl( void *, vod_media_t *, const char *, int, va_list );
95 static vlm_media_sys_t *vlm_MediaSearch( vlm_t *, const char *);
97 /*****************************************************************************
99 *****************************************************************************/
100 vlm_t *__vlm_New ( vlc_object_t *p_this )
103 vlm_t *p_vlm = NULL, **pp_vlm = &(libvlc_priv (p_this->p_libvlc)->p_vlm);
105 static const char vlm_object_name[] = "vlm daemon";
107 /* Avoid multiple creation */
108 if( var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX ) ||
109 var_Get( p_this->p_libvlc, "vlm_mutex", &lockval ) )
112 vlc_mutex_lock( lockval.p_address );
116 { /* VLM already exists */
117 vlc_object_yield( p_vlm );
118 vlc_mutex_unlock( lockval.p_address );
122 msg_Dbg( p_this, "creating VLM" );
124 p_vlm = vlc_custom_create( p_this, sizeof( *p_vlm ), VLC_OBJECT_GENERIC,
128 vlc_mutex_unlock( lockval.p_address );
132 vlc_mutex_init( &p_vlm->lock );
134 TAB_INIT( p_vlm->i_media, p_vlm->media );
135 TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
138 vlc_object_attach( p_vlm, p_this->p_libvlc );
140 if( vlc_thread_create( p_vlm, "vlm thread",
141 Manage, VLC_THREAD_PRIORITY_LOW, false ) )
143 vlc_mutex_destroy( &p_vlm->lock );
144 vlc_object_release( p_vlm );
148 /* Load our configuration file */
149 psz_vlmconf = var_CreateGetString( p_vlm, "vlm-conf" );
150 if( psz_vlmconf && *psz_vlmconf )
152 vlm_message_t *p_message = NULL;
153 char *psz_buffer = NULL;
155 msg_Dbg( p_this, "loading VLM configuration" );
156 asprintf(&psz_buffer, "load %s", psz_vlmconf );
159 msg_Dbg( p_this, psz_buffer );
160 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
161 msg_Warn( p_this, "error while loading the configuration file" );
163 vlm_MessageDelete(p_message);
169 vlc_object_set_destructor( p_vlm, (vlc_destructor_t)vlm_Destructor );
170 *pp_vlm = p_vlm; /* for future reference */
171 vlc_mutex_unlock( lockval.p_address );
176 /*****************************************************************************
178 *****************************************************************************/
179 void vlm_Delete( vlm_t *p_vlm )
181 /* FIXME XXX: we need to set libvlc_priv->p_vlm bacl to NULL */
182 vlc_object_release( p_vlm );
185 /*****************************************************************************
187 *****************************************************************************/
188 static void vlm_Destructor( vlm_t *p_vlm )
190 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
191 TAB_CLEAN( p_vlm->i_media, p_vlm->media );
193 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
194 TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
196 vlc_mutex_destroy( &p_vlm->lock );
199 /*****************************************************************************
200 * vlm_ExecuteCommand:
201 *****************************************************************************/
202 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
203 vlm_message_t **pp_message)
207 vlc_mutex_lock( &p_vlm->lock );
208 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
209 vlc_mutex_unlock( &p_vlm->lock );
215 static const char quotes[] = "\"'";
217 * FindCommandEnd: look for the end of a possibly quoted string
218 * @return NULL on mal-formatted string,
219 * pointer past the last character otherwise.
221 static const char *FindCommandEnd( const char *psz_sent )
225 while( (c = *psz_sent) != '\0' )
229 if( strchr(quotes,c) ) // opening quote
231 else if( isspace(c) ) // non-escaped space
235 psz_sent++; // skip escaped character
236 if( *psz_sent == '\0' )
242 if( c == quote ) // non-escaped matching quote
244 else if( (quote == '"') && (c == '\\') )
246 psz_sent++; // skip escaped character
247 if (*psz_sent == '\0')
248 return NULL; // error, closing quote missing
254 // error (NULL) if we could not find a matching quote
255 return quote ? NULL : psz_sent;
260 * Unescape a nul-terminated string.
261 * Note that in and out can be identical.
263 * @param out output buffer (at least <strlen (in) + 1> characters long)
264 * @param in nul-terminated string to be unescaped
266 * @return 0 on success, -1 on error.
268 static int Unescape( char *out, const char *in )
272 while( (c = *in++) != '\0' )
276 if (strchr(quotes,c)) // opening quote
300 /* None of the special cases - copy the backslash */
306 if( c == quote ) // non-escaped matching quote
311 if( (quote == '"') && (c == '\\') )
320 case '\0': // should never happen
324 /* None of the special cases - copy the backslash */
336 /*****************************************************************************
337 * ExecuteCommand: The main state machine
338 *****************************************************************************
339 * Execute a command which ends with '\0' (string)
340 *****************************************************************************/
341 static int ExecuteSyntaxError( const char *psz_cmd, vlm_message_t **pp_status )
343 *pp_status = vlm_MessageNew( psz_cmd, "Wrong command syntax" );
347 static bool ExecuteIsMedia( vlm_t *p_vlm, const char *psz_name )
351 if( !psz_name || vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
355 static bool ExecuteIsSchedule( vlm_t *p_vlm, const char *psz_name )
357 if( !psz_name || !vlm_ScheduleSearch( p_vlm, psz_name ) )
362 static int ExecuteDel( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
364 vlm_media_sys_t *p_media;
365 vlm_schedule_sys_t *p_schedule;
367 p_media = vlm_MediaSearch( p_vlm, psz_name );
368 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
370 if( p_schedule != NULL )
372 vlm_ScheduleDelete( p_vlm, p_schedule );
374 else if( p_media != NULL )
376 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_media->cfg.id );
378 else if( !strcmp(psz_name, "media") )
380 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
382 else if( !strcmp(psz_name, "schedule") )
384 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
386 else if( !strcmp(psz_name, "all") )
388 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
389 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
393 *pp_status = vlm_MessageNew( "del", "%s: media unknown", psz_name );
397 *pp_status = vlm_MessageNew( "del", vlm_NULL );
401 static int ExecuteShow( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
403 vlm_media_sys_t *p_media;
404 vlm_schedule_sys_t *p_schedule;
408 *pp_status = vlm_Show( p_vlm, NULL, NULL, NULL );
412 p_media = vlm_MediaSearch( p_vlm, psz_name );
413 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
415 if( p_schedule != NULL )
416 *pp_status = vlm_Show( p_vlm, NULL, p_schedule, NULL );
417 else if( p_media != NULL )
418 *pp_status = vlm_Show( p_vlm, p_media, NULL, NULL );
420 *pp_status = vlm_Show( p_vlm, NULL, NULL, psz_name );
425 static int ExecuteHelp( vlm_message_t **pp_status )
427 vlm_message_t *message_child;
429 #define MessageAdd( a ) \
430 vlm_MessageAdd( *pp_status, vlm_MessageNew( a, vlm_NULL ) );
431 #define MessageAddChild( a ) \
432 vlm_MessageAdd( message_child, vlm_MessageNew( a, vlm_NULL ) );
434 *pp_status = vlm_MessageNew( "help", vlm_NULL );
436 message_child = MessageAdd( "Commands Syntax:" );
437 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
438 MessageAddChild( "setup (name) (properties)" );
439 MessageAddChild( "show [(name)|media|schedule]" );
440 MessageAddChild( "del (name)|all|media|schedule" );
441 MessageAddChild( "control (name) [instance_name] (command)" );
442 MessageAddChild( "save (config_file)" );
443 MessageAddChild( "export" );
444 MessageAddChild( "load (config_file)" );
446 message_child = MessageAdd( "Media Proprieties Syntax:" );
447 MessageAddChild( "input (input_name)" );
448 MessageAddChild( "inputdel (input_name)|all" );
449 MessageAddChild( "inputdeln input_number" );
450 MessageAddChild( "output (output_name)" );
451 MessageAddChild( "option (option_name)[=value]" );
452 MessageAddChild( "enabled|disabled" );
453 MessageAddChild( "loop|unloop (broadcast only)" );
454 MessageAddChild( "mux (mux_name)" );
456 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
457 MessageAddChild( "enabled|disabled" );
458 MessageAddChild( "append (command_until_rest_of_the_line)" );
459 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
461 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
462 "(days)-(hours):(minutes):(seconds)" );
463 MessageAddChild( "repeat (number_of_repetitions)" );
465 message_child = MessageAdd( "Control Commands Syntax:" );
466 MessageAddChild( "play [input_number]" );
467 MessageAddChild( "pause" );
468 MessageAddChild( "stop" );
469 MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](miliseconds)ms" );
474 static int ExecuteControl( vlm_t *p_vlm, const char *psz_name, const int i_arg, char ** ppsz_arg, vlm_message_t **pp_status )
476 vlm_media_sys_t *p_media;
477 const char *psz_control = NULL;
478 const char *psz_instance = NULL;
479 const char *psz_argument = NULL;
483 if( !ExecuteIsMedia( p_vlm, psz_name ) )
485 *pp_status = vlm_MessageNew( "control", "%s: media unknown", psz_name );
491 #define IS(txt) ( !strcmp( ppsz_arg[i_index], (txt) ) )
493 if( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") )
496 psz_instance = ppsz_arg[0];
498 if( i_index >= i_arg || ( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") ) )
499 return ExecuteSyntaxError( "control", pp_status );
502 psz_control = ppsz_arg[i_index];
504 if( i_index+1 < i_arg )
505 psz_argument = ppsz_arg[i_index+1];
507 p_media = vlm_MediaSearch( p_vlm, psz_name );
510 if( !strcmp( psz_control, "play" ) )
512 int i_input_index = 0;
515 if( ( psz_argument && sscanf(psz_argument, "%d", &i) == 1 ) && i > 0 && i-1 < p_media->cfg.i_input )
519 else if( psz_argument )
522 vlm_media_t *p_cfg = &p_media->cfg;
523 for ( j=0; j < p_cfg->i_input; j++)
525 if( !strcmp( p_cfg->ppsz_input[j], psz_argument ) )
533 if( p_media->cfg.b_vod )
534 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
536 i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, psz_instance, i_input_index );
538 else if( !strcmp( psz_control, "seek" ) )
543 if( psz_argument[0] == '+' || psz_argument[0] == '-' )
548 if( strstr( psz_argument, "ms" ) || strstr( psz_argument, "s" ) )
553 if( strstr( psz_argument, "ms" ) )
554 i_new_time = 1000 * (int64_t)atoi( psz_argument );
556 i_new_time = 1000000 * (int64_t)atoi( psz_argument );
561 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, &i_time );
562 i_new_time += i_time;
566 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, i_new_time );
571 double d_new_position = i18n_atof( psz_argument ) / 100.0;
575 double d_position = 0.0;
577 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
578 d_new_position += d_position;
580 if( d_new_position < 0.0 )
581 d_new_position = 0.0;
582 else if( d_new_position > 1.0 )
583 d_new_position = 1.0;
584 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_new_position );
589 i_result = VLC_EGENERIC;
592 else if( !strcmp( psz_control, "rewind" ) )
596 const double d_scale = i18n_atof( psz_argument );
599 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
600 d_position -= (d_scale / 1000.0);
601 if( d_position < 0.0 )
603 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
607 i_result = VLC_EGENERIC;
610 else if( !strcmp( psz_control, "forward" ) )
614 const double d_scale = i18n_atof( psz_argument );
617 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
618 d_position += (d_scale / 1000.0);
619 if( d_position > 1.0 )
621 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
626 i_result = VLC_EGENERIC;
629 else if( !strcmp( psz_control, "stop" ) )
631 i_result = vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
633 else if( !strcmp( psz_control, "pause" ) )
635 i_result = vlm_ControlInternal( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
639 i_result = VLC_EGENERIC;
644 *pp_status = vlm_MessageNew( "control", "unknown error" );
647 *pp_status = vlm_MessageNew( "control", vlm_NULL );
651 static int ExecuteExport( vlm_t *p_vlm, vlm_message_t **pp_status )
653 char *psz_export = Save( p_vlm );
655 *pp_status = vlm_MessageNew( "export", psz_export );
660 static int ExecuteSave( vlm_t *p_vlm, const char *psz_file, vlm_message_t **pp_status )
662 FILE *f = utf8_fopen( psz_file, "wt" );
668 psz_save = Save( p_vlm );
669 if( psz_save == NULL )
674 fwrite( psz_save, strlen( psz_save ), 1, f );
678 *pp_status = vlm_MessageNew( "save", vlm_NULL );
682 *pp_status = vlm_MessageNew( "save", "Unable to save to file" );
686 static int ExecuteLoad( vlm_t *p_vlm, const char *psz_url, vlm_message_t **pp_status )
688 stream_t *p_stream = stream_UrlNew( p_vlm, psz_url );
694 *pp_status = vlm_MessageNew( "load", "Unable to load from file" );
699 if( stream_Seek( p_stream, 0 ) != 0 )
701 stream_Delete( p_stream );
703 *pp_status = vlm_MessageNew( "load", "Read file error" );
707 i_size = stream_Size( p_stream );
709 psz_buffer = malloc( i_size + 1 );
712 stream_Delete( p_stream );
714 *pp_status = vlm_MessageNew( "load", "Read file error" );
718 stream_Read( p_stream, psz_buffer, i_size );
719 psz_buffer[i_size] = '\0';
721 stream_Delete( p_stream );
723 if( Load( p_vlm, psz_buffer ) )
727 *pp_status = vlm_MessageNew( "load", "Error while loading file" );
733 *pp_status = vlm_MessageNew( "load", vlm_NULL );
737 static int ExecuteScheduleProperty( vlm_t *p_vlm, vlm_schedule_sys_t *p_schedule, bool b_new,
738 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
740 const char *psz_cmd = b_new ? "new" : "setup";
743 for( i = 0; i < i_property; i++ )
745 if( !strcmp( ppsz_property[i], "enabled" ) ||
746 !strcmp( ppsz_property[i], "disabled" ) )
748 vlm_ScheduleSetup( p_schedule, ppsz_property[i], NULL );
750 else if( !strcmp( ppsz_property[i], "append" ) )
754 /* Beware: everything behind append is considered as
757 if( ++i >= i_property )
760 psz_line = strdup( ppsz_property[i] );
761 for( j = i+1; j < i_property; j++ )
763 psz_line = realloc( psz_line, strlen(psz_line) + strlen(ppsz_property[j]) + 1 + 1 );
764 strcat( psz_line, " " );
765 strcat( psz_line, ppsz_property[j] );
768 vlm_ScheduleSetup( p_schedule, "append", psz_line );
773 if( i + 1 >= i_property )
776 vlm_ScheduleDelete( p_vlm, p_schedule );
777 return ExecuteSyntaxError( psz_cmd, pp_status );
780 vlm_ScheduleSetup( p_schedule, ppsz_property[i], ppsz_property[i+1] );
784 *pp_status = vlm_MessageNew( psz_cmd, vlm_NULL );
788 static int ExecuteMediaProperty( vlm_t *p_vlm, int64_t id, bool b_new,
789 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
791 const char *psz_cmd = b_new ? "new" : "setup";
792 vlm_media_t *p_cfg = NULL;
798 #define ERROR( txt ) do { *pp_status = vlm_MessageNew( psz_cmd, txt); goto error; } while(0)
799 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA, id, &p_cfg ) )
800 ERROR( "unknown media" );
802 #define MISSING(cmd) do { if( !psz_value ) ERROR( "missing argument for " cmd ); } while(0)
803 for( i = 0; i < i_property; i++ )
805 const char *psz_option = ppsz_property[i];
806 const char *psz_value = i+1 < i_property ? ppsz_property[i+1] : NULL;
808 if( !strcmp( psz_option, "enabled" ) )
810 p_cfg->b_enabled = true;
812 else if( !strcmp( psz_option, "disabled" ) )
814 p_cfg->b_enabled = false;
816 else if( !strcmp( psz_option, "input" ) )
819 TAB_APPEND( p_cfg->i_input, p_cfg->ppsz_input, strdup(psz_value) );
822 else if( !strcmp( psz_option, "inputdel" ) && psz_value && !strcmp( psz_value, "all" ) )
824 while( p_cfg->i_input > 0 )
825 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[0] );
828 else if( !strcmp( psz_option, "inputdel" ) )
832 MISSING( "inputdel" );
834 for( j = 0; j < p_cfg->i_input; j++ )
836 if( !strcmp( p_cfg->ppsz_input[j], psz_value ) )
838 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[j] );
844 else if( !strcmp( psz_option, "inputdeln" ) )
848 MISSING( "inputdeln" );
850 i_index = atoi( psz_value );
851 if( i_index > 0 && i_index <= p_cfg->i_input )
852 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[i_index-1] );
855 else if( !strcmp( psz_option, "output" ) )
859 free( p_cfg->psz_output );
860 p_cfg->psz_output = *psz_value ? strdup( psz_value ) : NULL;
863 else if( !strcmp( psz_option, "option" ) )
867 TAB_APPEND( p_cfg->i_option, p_cfg->ppsz_option, strdup( psz_value ) );
870 else if( !strcmp( psz_option, "loop" ) )
873 ERROR( "invalid loop option for vod" );
874 p_cfg->broadcast.b_loop = true;
876 else if( !strcmp( psz_option, "unloop" ) )
879 ERROR( "invalid unloop option for vod" );
880 p_cfg->broadcast.b_loop = false;
882 else if( !strcmp( psz_option, "mux" ) )
886 ERROR( "invalid mux option for broadcast" );
888 free( p_cfg->vod.psz_mux );
889 p_cfg->vod.psz_mux = *psz_value ? strdup( psz_value ) : NULL;
894 fprintf( stderr, "PROP: name=%s unknown\n", psz_option );
895 ERROR( "Wrong command syntax" );
902 i_result = vlm_ControlInternal( p_vlm, VLM_CHANGE_MEDIA, p_cfg );
903 vlm_media_Delete( p_cfg );
905 *pp_status = vlm_MessageNew( psz_cmd, vlm_NULL );
912 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_cfg->id );
913 vlm_media_Delete( p_cfg );
918 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 )
921 if( !strcmp( psz_name, "all" ) || !strcmp( psz_name, "media" ) || !strcmp( psz_name, "schedule" ) )
923 *pp_status = vlm_MessageNew( "new", "\"all\", \"media\" and \"schedule\" are reserved names" );
926 if( ExecuteIsMedia( p_vlm, psz_name ) || ExecuteIsSchedule( p_vlm, psz_name ) )
928 *pp_status = vlm_MessageNew( "new", "%s: Name already in use", psz_name );
932 if( !strcmp( psz_type, "schedule" ) )
934 vlm_schedule_sys_t *p_schedule = vlm_ScheduleNew( p_vlm, psz_name );
937 *pp_status = vlm_MessageNew( "new", "could not create schedule" );
940 return ExecuteScheduleProperty( p_vlm, p_schedule, true, i_property, ppsz_property, pp_status );
942 else if( !strcmp( psz_type, "vod" ) || !strcmp( psz_type, "broadcast" ) )
947 vlm_media_Init( &cfg );
948 cfg.psz_name = strdup( psz_name );
949 cfg.b_vod = !strcmp( psz_type, "vod" );
951 if( vlm_ControlInternal( p_vlm, VLM_ADD_MEDIA, &cfg, &id ) )
953 vlm_media_Clean( &cfg );
954 *pp_status = vlm_MessageNew( "new", "could not create media" );
957 vlm_media_Clean( &cfg );
958 return ExecuteMediaProperty( p_vlm, id, true, i_property, ppsz_property, pp_status );
962 *pp_status = vlm_MessageNew( "new", "%s: Choose between vod, broadcast or schedule", psz_type );
967 static int ExecuteSetup( vlm_t *p_vlm, const char *psz_name, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
969 if( ExecuteIsSchedule( p_vlm, psz_name ) )
971 vlm_schedule_sys_t *p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
972 return ExecuteScheduleProperty( p_vlm, p_schedule, false, i_property, ppsz_property, pp_status );
974 else if( ExecuteIsMedia( p_vlm, psz_name ) )
977 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
979 return ExecuteMediaProperty( p_vlm, id, false, i_property, ppsz_property, pp_status );
983 *pp_status = vlm_MessageNew( "setup", "%s unknown", psz_name );
987 static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
988 vlm_message_t **pp_message )
990 size_t i_command = 0;
991 char buf[strlen (psz_command) + 1], *psz_buf = buf;
992 char *ppsz_command[3+sizeof (buf) / 2];
993 vlm_message_t *p_message = NULL;
995 /* First, parse the line and cut it */
996 while( *psz_command != '\0' )
998 const char *psz_temp;
1000 if(isspace (*psz_command))
1006 /* support for comments */
1007 if( i_command == 0 && *psz_command == '#')
1009 p_message = vlm_MessageNew( "", vlm_NULL );
1013 psz_temp = FindCommandEnd( psz_command );
1015 if( psz_temp == NULL )
1017 p_message = vlm_MessageNew( "Incomplete command", psz_command );
1021 assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
1023 ppsz_command[i_command] = psz_buf;
1024 memcpy (psz_buf, psz_command, psz_temp - psz_command);
1025 psz_buf[psz_temp - psz_command] = '\0';
1027 Unescape (psz_buf, psz_buf);
1030 psz_buf += psz_temp - psz_command + 1;
1031 psz_command = psz_temp;
1033 assert (buf + sizeof (buf) >= psz_buf);
1037 * And then Interpret it
1040 #define IF_EXECUTE( name, check, cmd ) if( !strcmp(ppsz_command[0], name ) ) { if( (check) ) goto syntax_error; if( (cmd) ) goto error; goto success; }
1041 if( i_command == 0 )
1043 p_message = vlm_MessageNew( "", vlm_NULL );
1046 else IF_EXECUTE( "del", (i_command != 2), ExecuteDel(p_vlm, ppsz_command[1], &p_message) )
1047 else IF_EXECUTE( "show", (i_command > 2), ExecuteShow(p_vlm, i_command > 1 ? ppsz_command[1] : NULL, &p_message) )
1048 else IF_EXECUTE( "help", (i_command != 1), ExecuteHelp( &p_message ) )
1049 else IF_EXECUTE( "control", (i_command < 3), ExecuteControl(p_vlm, ppsz_command[1], i_command - 2, &ppsz_command[2], &p_message) )
1050 else IF_EXECUTE( "save", (i_command != 2), ExecuteSave(p_vlm, ppsz_command[1], &p_message) )
1051 else IF_EXECUTE( "export", (i_command != 1), ExecuteExport(p_vlm, &p_message) )
1052 else IF_EXECUTE( "load", (i_command != 2), ExecuteLoad(p_vlm, ppsz_command[1], &p_message) )
1053 else IF_EXECUTE( "new", (i_command < 3), ExecuteNew(p_vlm, ppsz_command[1], ppsz_command[2], i_command-3, &ppsz_command[3], &p_message) )
1054 else IF_EXECUTE( "setup", (i_command < 2), ExecuteSetup(p_vlm, ppsz_command[1], i_command-2, &ppsz_command[2], &p_message) )
1057 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
1063 *pp_message = p_message;
1067 return ExecuteSyntaxError( ppsz_command[0], pp_message );
1070 *pp_message = p_message;
1071 return VLC_EGENERIC;
1074 /*****************************************************************************
1076 *****************************************************************************/
1077 vlm_media_sys_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
1081 for( i = 0; i < vlm->i_media; i++ )
1083 if( strcmp( psz_name, vlm->media[i]->cfg.psz_name ) == 0 )
1084 return vlm->media[i];
1090 /*****************************************************************************
1092 *****************************************************************************/
1093 static int64_t vlm_Date(void)
1098 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1100 struct timeval tv_date;
1102 /* gettimeofday() cannot fail given &tv_date is a valid address */
1103 (void)gettimeofday( &tv_date, NULL );
1104 return (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec;
1108 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
1110 vlm_schedule_sys_t *p_sched = malloc( sizeof( vlm_schedule_sys_t ) );
1122 p_sched->psz_name = strdup( psz_name );
1123 p_sched->b_enabled = false;
1124 p_sched->i_command = 0;
1125 p_sched->command = NULL;
1126 p_sched->i_date = 0;
1127 p_sched->i_period = 0;
1128 p_sched->i_repeat = -1;
1130 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1135 /* for now, simple delete. After, del with options (last arg) */
1136 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched )
1138 if( sched == NULL ) return;
1140 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1142 if( vlm->i_schedule == 0 ) free( vlm->schedule );
1143 free( sched->psz_name );
1144 while( sched->i_command )
1146 char *psz_cmd = sched->command[0];
1147 TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1153 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1157 for( i = 0; i < vlm->i_schedule; i++ )
1159 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1161 return vlm->schedule[i];
1168 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1169 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
1170 const char *psz_value )
1172 if( !strcmp( psz_cmd, "enabled" ) )
1174 schedule->b_enabled = true;
1176 else if( !strcmp( psz_cmd, "disabled" ) )
1178 schedule->b_enabled = false;
1180 #if !defined( UNDER_CE )
1181 else if( !strcmp( psz_cmd, "date" ) )
1187 time.tm_sec = 0; /* seconds */
1188 time.tm_min = 0; /* minutes */
1189 time.tm_hour = 0; /* hours */
1190 time.tm_mday = 0; /* day of the month */
1191 time.tm_mon = 0; /* month */
1192 time.tm_year = 0; /* year */
1193 time.tm_wday = 0; /* day of the week */
1194 time.tm_yday = 0; /* day in the year */
1195 time.tm_isdst = -1; /* daylight saving time */
1197 /* date should be year/month/day-hour:minutes:seconds */
1198 p = strchr( psz_value, '-' );
1200 if( !strcmp( psz_value, "now" ) )
1202 schedule->i_date = 0;
1204 else if( (p == NULL) && sscanf( psz_value, "%d:%d:%d", &time.tm_hour,
1205 &time.tm_min, &time.tm_sec ) != 3 )
1206 /* it must be a hour:minutes:seconds */
1214 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1232 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1238 time.tm_mon = i - 1;
1242 time.tm_year = i - 1900;
1243 time.tm_mon = j - 1;
1250 date = mktime( &time );
1251 schedule->i_date = ((mtime_t) date) * 1000000;
1254 else if( !strcmp( psz_cmd, "period" ) )
1258 const char *psz_time = NULL, *psz_date = NULL;
1262 /* First, if date or period are modified, repeat should be equal to -1 */
1263 schedule->i_repeat = -1;
1265 time.tm_sec = 0; /* seconds */
1266 time.tm_min = 0; /* minutes */
1267 time.tm_hour = 0; /* hours */
1268 time.tm_mday = 0; /* day of the month */
1269 time.tm_mon = 0; /* month */
1270 time.tm_year = 0; /* year */
1271 time.tm_wday = 0; /* day of the week */
1272 time.tm_yday = 0; /* day in the year */
1273 time.tm_isdst = -1; /* daylight saving time */
1275 /* date should be year/month/day-hour:minutes:seconds */
1276 p = strchr( psz_value, '-' );
1279 psz_date = psz_value;
1284 psz_time = psz_value;
1287 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1306 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1325 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1326 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1327 schedule->i_period = ((mtime_t) date) * 1000000;
1329 #endif /* UNDER_CE */
1330 else if( !strcmp( psz_cmd, "repeat" ) )
1334 if( sscanf( psz_value, "%d", &i ) == 1 )
1336 schedule->i_repeat = i;
1343 else if( !strcmp( psz_cmd, "append" ) )
1345 char *command = strdup( psz_value );
1347 TAB_APPEND( schedule->i_command, schedule->command, command );
1356 /*****************************************************************************
1357 * Message handling functions
1358 *****************************************************************************/
1359 vlm_message_t *vlm_MessageNew( const char *psz_name,
1360 const char *psz_format, ... )
1362 vlm_message_t *p_message;
1365 if( !psz_name ) return NULL;
1367 p_message = malloc( sizeof(vlm_message_t) );
1373 p_message->psz_value = 0;
1377 va_start( args, psz_format );
1378 if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1387 p_message->psz_name = strdup( psz_name );
1388 p_message->i_child = 0;
1389 p_message->child = NULL;
1394 void vlm_MessageDelete( vlm_message_t *p_message )
1396 free( p_message->psz_name );
1397 free( p_message->psz_value );
1398 while( p_message->i_child-- )
1399 vlm_MessageDelete( p_message->child[p_message->i_child] );
1400 free( p_message->child );
1405 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1406 vlm_message_t *p_child )
1408 if( p_message == NULL ) return NULL;
1412 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1418 /*****************************************************************************
1419 * Misc utility functions
1420 *****************************************************************************/
1421 static vlm_message_t *vlm_ShowMedia( vlm_media_sys_t *p_media )
1423 vlm_media_t *p_cfg = &p_media->cfg;
1424 vlm_message_t *p_msg;
1425 vlm_message_t *p_msg_sub;
1428 p_msg = vlm_MessageNew( p_cfg->psz_name, vlm_NULL );
1429 vlm_MessageAdd( p_msg,
1430 vlm_MessageNew( "type", p_cfg->b_vod ? "vod" : "broadcast" ) );
1431 vlm_MessageAdd( p_msg,
1432 vlm_MessageNew( "enabled", p_cfg->b_enabled ? "yes" : "no" ) );
1435 vlm_MessageAdd( p_msg,
1436 vlm_MessageNew( "mux", p_cfg->vod.psz_mux ) );
1438 vlm_MessageAdd( p_msg,
1439 vlm_MessageNew( "loop", p_cfg->broadcast.b_loop ? "yes" : "no" ) );
1441 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "inputs", vlm_NULL ) );
1442 for( i = 0; i < p_cfg->i_input; i++ )
1445 asprintf( &psz_tmp, "%d", i+1 );
1446 vlm_MessageAdd( p_msg_sub,
1447 vlm_MessageNew( psz_tmp, p_cfg->ppsz_input[i] ) );
1451 vlm_MessageAdd( p_msg,
1452 vlm_MessageNew( "output", p_cfg->psz_output ? p_cfg->psz_output : "" ) );
1454 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "options", vlm_NULL ) );
1455 for( i = 0; i < p_cfg->i_option; i++ )
1456 vlm_MessageAdd( p_msg_sub, vlm_MessageNew( p_cfg->ppsz_option[i], vlm_NULL ) );
1458 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "instances", vlm_NULL ) );
1459 for( i = 0; i < p_media->i_instance; i++ )
1461 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1463 vlm_message_t *p_msg_instance;
1467 if( p_instance->p_input )
1468 var_Get( p_instance->p_input, "state", &val );
1470 p_msg_instance = vlm_MessageAdd( p_msg_sub, vlm_MessageNew( "instance" , vlm_NULL ) );
1472 vlm_MessageAdd( p_msg_instance,
1473 vlm_MessageNew( "name" , p_instance->psz_name ? p_instance->psz_name : "default" ) );
1474 vlm_MessageAdd( p_msg_instance,
1475 vlm_MessageNew( "state",
1476 val.i_int == PLAYING_S ? "playing" :
1477 val.i_int == PAUSE_S ? "paused" :
1480 /* FIXME should not do that this way */
1481 if( p_instance->p_input )
1483 #define APPEND_INPUT_INFO( a, format, type ) \
1484 asprintf( &psz_tmp, format, \
1485 var_Get ## type( p_instance->p_input, a ) ); \
1486 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( a, psz_tmp ) ); \
1488 APPEND_INPUT_INFO( "position", "%f", Float );
1489 APPEND_INPUT_INFO( "time", "%"PRIi64, Time );
1490 APPEND_INPUT_INFO( "length", "%"PRIi64, Time );
1491 APPEND_INPUT_INFO( "rate", "%d", Integer );
1492 APPEND_INPUT_INFO( "title", "%d", Integer );
1493 APPEND_INPUT_INFO( "chapter", "%d", Integer );
1494 APPEND_INPUT_INFO( "seekable", "%d", Bool );
1496 #undef APPEND_INPUT_INFO
1497 asprintf( &psz_tmp, "%d", p_instance->i_index + 1 );
1498 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( "playlistindex", psz_tmp ) );
1504 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
1505 vlm_schedule_sys_t *schedule,
1506 const char *psz_filter )
1510 vlm_message_t *p_msg = vlm_MessageNew( "show", vlm_NULL );
1512 vlm_MessageAdd( p_msg, vlm_ShowMedia( media ) );
1516 else if( schedule != NULL )
1520 vlm_message_t *msg_schedule;
1521 vlm_message_t *msg_child;
1524 msg = vlm_MessageNew( "show", vlm_NULL );
1526 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, vlm_NULL ) );
1528 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1530 vlm_MessageAdd( msg_schedule,
1531 vlm_MessageNew( "enabled", schedule->b_enabled ?
1534 #if !defined( UNDER_CE )
1535 if( schedule->i_date != 0 )
1538 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1541 localtime_r( &i_time, &date);
1542 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1543 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1544 date.tm_hour, date.tm_min, date.tm_sec );
1546 vlm_MessageAdd( msg_schedule,
1547 vlm_MessageNew( "date", psz_date ) );
1552 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1555 if( schedule->i_period != 0 )
1557 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1560 date.tm_sec = (int)( i_time % 60 );
1561 i_time = i_time / 60;
1562 date.tm_min = (int)( i_time % 60 );
1563 i_time = i_time / 60;
1564 date.tm_hour = (int)( i_time % 24 );
1565 i_time = i_time / 24;
1566 date.tm_mday = (int)( i_time % 30 );
1567 i_time = i_time / 30;
1568 /* okay, okay, months are not always 30 days long */
1569 date.tm_mon = (int)( i_time % 12 );
1570 i_time = i_time / 12;
1571 date.tm_year = (int)i_time;
1573 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1574 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1576 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1580 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1582 #endif /* UNDER_CE */
1584 sprintf( buffer, "%d", schedule->i_repeat );
1585 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1588 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", vlm_NULL ) );
1590 for( i = 0; i < schedule->i_command; i++ )
1592 vlm_MessageAdd( msg_child,
1593 vlm_MessageNew( schedule->command[i], vlm_NULL ) );
1600 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1602 vlm_message_t *p_msg;
1603 vlm_message_t *p_msg_child;
1604 int i_vod = 0, i_broadcast = 0;
1608 for( i = 0; i < vlm->i_media; i++ )
1610 if( vlm->media[i]->cfg.b_vod )
1616 asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1618 p_msg = vlm_MessageNew( "show", vlm_NULL );
1619 p_msg_child = vlm_MessageAdd( p_msg, vlm_MessageNew( "media", psz_count ) );
1622 for( i = 0; i < vlm->i_media; i++ )
1623 vlm_MessageAdd( p_msg_child, vlm_ShowMedia( vlm->media[i] ) );
1628 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1632 vlm_message_t *msg_child;
1634 msg = vlm_MessageNew( "show", vlm_NULL );
1635 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", vlm_NULL ) );
1637 for( i = 0; i < vlm->i_schedule; i++ )
1639 vlm_schedule_sys_t *s = vlm->schedule[i];
1640 vlm_message_t *msg_schedule;
1641 mtime_t i_time, i_next_date;
1643 msg_schedule = vlm_MessageAdd( msg_child,
1644 vlm_MessageNew( s->psz_name, vlm_NULL ) );
1645 vlm_MessageAdd( msg_schedule,
1646 vlm_MessageNew( "enabled", s->b_enabled ?
1649 /* calculate next date */
1650 i_time = vlm_Date();
1651 i_next_date = s->i_date;
1653 if( s->i_period != 0 )
1656 while( s->i_date + j * s->i_period <= i_time &&
1662 i_next_date = s->i_date + j * s->i_period;
1665 if( i_next_date > i_time )
1667 time_t i_date = (time_t) (i_next_date / 1000000) ;
1669 #if !defined( UNDER_CE )
1672 ctime_r( &i_date, psz_date );
1674 char *psz_date = ctime( &i_date );
1677 vlm_MessageAdd( msg_schedule,
1678 vlm_MessageNew( "next launch", psz_date ) );
1686 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
1688 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1689 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1691 vlm_MessageAdd( show1, show2->child[0] );
1693 /* We must destroy the parent node "show" of show2
1694 * and not the children */
1695 free( show2->psz_name );
1703 return vlm_MessageNew( "show", vlm_NULL );
1707 /*****************************************************************************
1708 * Config handling functions
1709 *****************************************************************************/
1710 static int Load( vlm_t *vlm, char *file )
1715 while( *pf != '\0' )
1717 vlm_message_t *message = NULL;
1720 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1725 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1729 if( pf[i_end] == '\n' ) i_end++;
1732 if( *pf && ExecuteCommand( vlm, pf, &message ) )
1736 if( message->psz_value )
1737 msg_Err( vlm, "Load error on line %d: %s: %s",
1738 i_line, message->psz_name, message->psz_value );
1739 vlm_MessageDelete( message );
1743 if( message ) vlm_MessageDelete( message );
1752 static char *Save( vlm_t *vlm )
1755 char psz_header[] = "\n"
1756 "# VLC media player VLM command batch\n"
1757 "# http://www.videolan.org/vlc/\n\n" ;
1760 int i_length = strlen( psz_header );
1762 for( i = 0; i < vlm->i_media; i++ )
1764 vlm_media_sys_t *media = vlm->media[i];
1765 vlm_media_t *p_cfg = &media->cfg;
1768 i_length += strlen( "new * vod " ) + strlen(p_cfg->psz_name);
1770 i_length += strlen( "new * broadcast " ) + strlen(p_cfg->psz_name);
1772 if( p_cfg->b_enabled == true )
1773 i_length += strlen( "enabled" );
1775 i_length += strlen( "disabled" );
1777 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop == true )
1778 i_length += strlen( " loop\n" );
1780 i_length += strlen( "\n" );
1782 for( j = 0; j < p_cfg->i_input; j++ )
1783 i_length += strlen( "setup * input \"\"\n" ) + strlen( p_cfg->psz_name ) + strlen( p_cfg->ppsz_input[j] );
1785 if( p_cfg->psz_output != NULL )
1786 i_length += strlen( "setup * output \n" ) + strlen(p_cfg->psz_name) + strlen(p_cfg->psz_output);
1788 for( j = 0; j < p_cfg->i_option; j++ )
1789 i_length += strlen("setup * option \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->ppsz_option[j]);
1791 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1792 i_length += strlen("setup * mux \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->vod.psz_mux);
1795 for( i = 0; i < vlm->i_schedule; i++ )
1797 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1799 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
1801 if( schedule->b_enabled == true )
1803 i_length += strlen( "date //-:: enabled\n" ) + 14;
1807 i_length += strlen( "date //-:: disabled\n" ) + 14;
1811 if( schedule->i_period != 0 )
1813 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
1814 strlen( "period //-::\n" ) + 14;
1817 if( schedule->i_repeat >= 0 )
1821 sprintf( buffer, "%d", schedule->i_repeat );
1822 i_length += strlen( "setup repeat \n" ) +
1823 strlen( schedule->psz_name ) + strlen( buffer );
1830 for( j = 0; j < schedule->i_command; j++ )
1832 i_length += strlen( "setup append \n" ) +
1833 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
1838 /* Don't forget the '\0' */
1840 /* now we have the length of save */
1842 p = save = malloc( i_length );
1843 if( !save ) return NULL;
1846 p += sprintf( p, "%s", psz_header );
1848 /* finally we can write in it */
1849 for( i = 0; i < vlm->i_media; i++ )
1851 vlm_media_sys_t *media = vlm->media[i];
1852 vlm_media_t *p_cfg = &media->cfg;
1855 p += sprintf( p, "new %s vod ", p_cfg->psz_name );
1857 p += sprintf( p, "new %s broadcast ", p_cfg->psz_name );
1859 if( p_cfg->b_enabled )
1860 p += sprintf( p, "enabled" );
1862 p += sprintf( p, "disabled" );
1864 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1865 p += sprintf( p, " loop\n" );
1867 p += sprintf( p, "\n" );
1869 for( j = 0; j < p_cfg->i_input; j++ )
1870 p += sprintf( p, "setup %s input \"%s\"\n", p_cfg->psz_name, p_cfg->ppsz_input[j] );
1872 if( p_cfg->psz_output )
1873 p += sprintf( p, "setup %s output %s\n", p_cfg->psz_name, p_cfg->psz_output );
1875 for( j = 0; j < p_cfg->i_option; j++ )
1876 p += sprintf( p, "setup %s option %s\n", p_cfg->psz_name, p_cfg->ppsz_option[j] );
1878 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1879 p += sprintf( p, "setup %s mux %s\n", p_cfg->psz_name, p_cfg->vod.psz_mux );
1882 /* and now, the schedule scripts */
1883 #if !defined( UNDER_CE )
1884 for( i = 0; i < vlm->i_schedule; i++ )
1886 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1888 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
1890 localtime_r( &i_time, &date);
1891 p += sprintf( p, "new %s schedule ", schedule->psz_name);
1893 if( schedule->b_enabled == true )
1895 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
1896 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1897 date.tm_hour, date.tm_min, date.tm_sec );
1901 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
1902 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1903 date.tm_hour, date.tm_min, date.tm_sec);
1906 if( schedule->i_period != 0 )
1908 p += sprintf( p, "setup %s ", schedule->psz_name );
1910 i_time = (time_t) ( schedule->i_period / 1000000 );
1912 date.tm_sec = (int)( i_time % 60 );
1913 i_time = i_time / 60;
1914 date.tm_min = (int)( i_time % 60 );
1915 i_time = i_time / 60;
1916 date.tm_hour = (int)( i_time % 24 );
1917 i_time = i_time / 24;
1918 date.tm_mday = (int)( i_time % 30 );
1919 i_time = i_time / 30;
1920 /* okay, okay, months are not always 30 days long */
1921 date.tm_mon = (int)( i_time % 12 );
1922 i_time = i_time / 12;
1923 date.tm_year = (int)i_time;
1925 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
1926 date.tm_year, date.tm_mon, date.tm_mday,
1927 date.tm_hour, date.tm_min, date.tm_sec);
1930 if( schedule->i_repeat >= 0 )
1932 p += sprintf( p, "setup %s repeat %d\n",
1933 schedule->psz_name, schedule->i_repeat );
1937 p += sprintf( p, "\n" );
1940 for( j = 0; j < schedule->i_command; j++ )
1942 p += sprintf( p, "setup %s append %s\n",
1943 schedule->psz_name, schedule->command[j] );
1947 #endif /* UNDER_CE */
1952 /*****************************************************************************
1954 *****************************************************************************/
1955 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
1956 const char *psz_id, int i_query, va_list args )
1958 vlm_t *vlm = (vlm_t *)p_private;
1963 if( !p_private || !p_vod_media )
1964 return VLC_EGENERIC;
1966 vlc_mutex_lock( &vlm->lock );
1969 for( i = 0, id = -1; i < vlm->i_media; i++ )
1971 if( p_vod_media == vlm->media[i]->vod.p_media )
1973 id = vlm->media[i]->cfg.id;
1979 vlc_mutex_unlock( &vlm->lock );
1980 return VLC_EGENERIC;
1985 case VOD_MEDIA_PLAY:
1986 psz = (const char *)va_arg( args, const char * );
1987 i_ret = vlm_ControlInternal( vlm, VLM_START_MEDIA_VOD_INSTANCE, id, psz_id, 0, psz );
1990 case VOD_MEDIA_PAUSE:
1991 i_ret = vlm_ControlInternal( vlm, VLM_PAUSE_MEDIA_INSTANCE, id, psz_id );
1994 case VOD_MEDIA_STOP:
1995 i_ret = vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, id, psz_id );
1998 case VOD_MEDIA_SEEK:
2000 double d_position = (double)va_arg( args, double );
2001 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position/100.0 );
2005 case VOD_MEDIA_REWIND:
2007 double d_scale = (double)va_arg( args, double );
2010 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2011 d_position -= (d_scale / 1000.0);
2012 if( d_position < 0.0 )
2014 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2018 case VOD_MEDIA_FORWARD:
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 > 1.0 )
2027 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2032 i_ret = VLC_EGENERIC;
2036 vlc_mutex_unlock( &vlm->lock );
2042 /*****************************************************************************
2044 *****************************************************************************/
2045 static int Manage( vlc_object_t* p_object )
2047 vlm_t *vlm = (vlm_t*)p_object;
2049 mtime_t i_lastcheck;
2052 i_lastcheck = vlm_Date();
2054 while( !vlm->b_die )
2056 char **ppsz_scheduled_commands = NULL;
2057 int i_scheduled_commands = 0;
2059 vlc_mutex_lock( &vlm->lock );
2061 /* destroy the inputs that wants to die, and launch the next input */
2062 for( i = 0; i < vlm->i_media; i++ )
2064 vlm_media_sys_t *p_media = vlm->media[i];
2066 for( j = 0; j < p_media->i_instance; )
2068 vlm_media_instance_sys_t *p_instance = p_media->instance[j];
2070 if( p_instance->p_input && ( p_instance->p_input->b_eof || p_instance->p_input->b_error ) )
2072 int i_new_input_index;
2075 i_new_input_index = p_instance->i_index + 1;
2076 if( !p_media->cfg.b_vod && p_media->cfg.broadcast.b_loop && i_new_input_index >= p_media->cfg.i_input )
2077 i_new_input_index = 0;
2079 /* FIXME implement multiple input with VOD */
2080 if( p_media->cfg.b_vod || i_new_input_index >= p_media->cfg.i_input )
2081 vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, p_instance->psz_name );
2083 vlm_ControlInternal( vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, p_instance->psz_name, i_new_input_index );
2095 i_time = vlm_Date();
2097 for( i = 0; i < vlm->i_schedule; i++ )
2099 mtime_t i_real_date = vlm->schedule[i]->i_date;
2101 if( vlm->schedule[i]->b_enabled == true )
2103 if( vlm->schedule[i]->i_date == 0 ) // now !
2105 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2106 i_real_date = i_time;
2108 else if( vlm->schedule[i]->i_period != 0 )
2111 while( vlm->schedule[i]->i_date + j *
2112 vlm->schedule[i]->i_period <= i_lastcheck &&
2113 ( vlm->schedule[i]->i_repeat > j ||
2114 vlm->schedule[i]->i_repeat == -1 ) )
2119 i_real_date = vlm->schedule[i]->i_date + j *
2120 vlm->schedule[i]->i_period;
2123 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2125 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2127 TAB_APPEND( i_scheduled_commands,
2128 ppsz_scheduled_commands,
2129 strdup(vlm->schedule[i]->command[j] ) );
2134 while( i_scheduled_commands )
2136 vlm_message_t *message = NULL;
2137 char *psz_command = ppsz_scheduled_commands[0];
2138 ExecuteCommand( vlm, psz_command,&message );
2140 /* for now, drop the message */
2141 vlm_MessageDelete( message );
2142 TAB_REMOVE( i_scheduled_commands,
2143 ppsz_scheduled_commands,
2145 free( psz_command );
2148 i_lastcheck = i_time;
2150 vlc_mutex_unlock( &vlm->lock );
2165 int i_connection_count;
2166 int i_connection_active;
2172 int i_playing_index;
2175 } vlm_media_status_t;
2179 static vlm_media_sys_t *vlm_ControlMediaGetById( vlm_t *p_vlm, int64_t id )
2183 for( i = 0; i < p_vlm->i_media; i++ )
2185 if( p_vlm->media[i]->cfg.id == id )
2186 return p_vlm->media[i];
2190 static vlm_media_sys_t *vlm_ControlMediaGetByName( vlm_t *p_vlm, const char *psz_name )
2194 for( i = 0; i < p_vlm->i_media; i++ )
2196 if( !strcmp( p_vlm->media[i]->cfg.psz_name, psz_name ) )
2197 return p_vlm->media[i];
2201 static int vlm_MediaDescriptionCheck( vlm_t *p_vlm, vlm_media_t *p_cfg )
2205 if( !p_cfg || !p_cfg->psz_name ||
2206 !strcmp( p_cfg->psz_name, "all" ) || !strcmp( p_cfg->psz_name, "media" ) || !strcmp( p_cfg->psz_name, "schedule" ) )
2207 return VLC_EGENERIC;
2209 for( i = 0; i < p_vlm->i_media; i++ )
2211 if( p_vlm->media[i]->cfg.id == p_cfg->id )
2213 if( !strcmp( p_vlm->media[i]->cfg.psz_name, p_cfg->psz_name ) )
2214 return VLC_EGENERIC;
2220 /* Called after a media description is changed/added */
2221 static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
2223 vlm_media_t *p_cfg = &p_media->cfg;
2224 /* Check if we need to create/delete a vod media */
2227 if( !p_cfg->b_enabled && p_media->vod.p_media )
2229 p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
2230 p_media->vod.p_media = NULL;
2232 else if( p_cfg->b_enabled && !p_media->vod.p_media && p_cfg->i_input )
2234 /* Pre-parse the input */
2235 input_thread_t *p_input;
2241 vlc_gc_decref( p_media->vod.p_item );
2242 p_media->vod.p_item = input_ItemNew( p_vlm, p_cfg->ppsz_input[0],
2245 if( p_cfg->psz_output )
2246 asprintf( &psz_output, "%s:description", p_cfg->psz_output );
2248 asprintf( &psz_output, "#description" );
2250 asprintf( &psz_dup, "sout=%s", psz_output);
2251 input_ItemAddOption( p_media->vod.p_item, psz_dup );
2253 for( i = 0; i < p_cfg->i_option; i++ )
2254 input_ItemAddOption( p_media->vod.p_item,
2255 p_cfg->ppsz_option[i] );
2256 input_ItemAddOption( p_media->vod.p_item, "no-sout-keep" );
2258 asprintf( &psz_header, _("Media: %s"), p_cfg->psz_name );
2260 if( (p_input = input_CreateThreadExtended( p_vlm, p_media->vod.p_item, psz_header, NULL ) ) )
2262 while( !p_input->b_eof && !p_input->b_error )
2265 input_StopThread( p_input );
2266 vlc_object_release( p_input );
2271 if( p_cfg->vod.psz_mux )
2274 es_format_t es, *p_es = &es;
2277 sprintf( fourcc, "%4.4s", p_cfg->vod.psz_mux );
2278 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
2279 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
2281 /* XXX: Don't do it that way, but properly use a new input item ref. */
2282 item = *p_media->vod.p_item;
2285 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
2287 p_media->vod.p_media =
2288 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
2292 p_media->vod.p_media =
2293 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, p_media->vod.p_item );
2299 /* TODO start media if needed */
2302 /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
2306 static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
2308 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
2311 if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
2312 return VLC_EGENERIC;
2313 if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
2314 return VLC_EGENERIC;
2318 /* TODO check what are the changes being done (stop instance if needed) */
2321 vlm_media_Clean( &p_media->cfg );
2322 vlm_media_Copy( &p_media->cfg, p_cfg );
2324 return vlm_OnMediaUpdate( p_vlm, p_media );
2327 static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id )
2329 vlm_media_sys_t *p_media;
2331 if( vlm_MediaDescriptionCheck( p_vlm, p_cfg ) || vlm_ControlMediaGetByName( p_vlm, p_cfg->psz_name ) )
2333 msg_Err( p_vlm, "invalid media description" );
2334 return VLC_EGENERIC;
2336 /* Check if we need to load the VOD server */
2337 if( p_cfg->b_vod && !p_vlm->i_vod )
2339 p_vlm->p_vod = vlc_custom_create( VLC_OBJECT(p_vlm), sizeof( vod_t ),
2340 VLC_OBJECT_GENERIC, "vod server" );
2341 vlc_object_attach( p_vlm->p_vod, p_vlm );
2342 p_vlm->p_vod->p_module = module_Need( p_vlm->p_vod, "vod server", 0, 0 );
2343 if( !p_vlm->p_vod->p_module )
2345 msg_Err( p_vlm, "cannot find vod server" );
2346 vlc_object_detach( p_vlm->p_vod );
2347 vlc_object_release( p_vlm->p_vod );
2349 return VLC_EGENERIC;
2352 p_vlm->p_vod->p_data = p_vlm;
2353 p_vlm->p_vod->pf_media_control = vlm_MediaVodControl;
2356 p_media = malloc( sizeof( vlm_media_sys_t ) );
2359 msg_Err( p_vlm, "out of memory" );
2362 memset( p_media, 0, sizeof(vlm_media_sys_t) );
2367 vlm_media_Copy( &p_media->cfg, p_cfg );
2368 p_media->cfg.id = p_vlm->i_id++;
2369 /* FIXME do we do something here if enabled is true ? */
2371 p_media->vod.p_item = input_ItemNew( p_vlm, NULL, NULL );
2373 p_media->vod.p_media = NULL;
2374 TAB_INIT( p_media->i_instance, p_media->instance );
2377 TAB_APPEND( p_vlm->i_media, p_vlm->media, p_media );
2380 *p_id = p_media->cfg.id;
2382 return vlm_OnMediaUpdate( p_vlm, p_media );
2385 static int vlm_ControlMediaDel( vlm_t *p_vlm, int64_t id )
2387 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2390 return VLC_EGENERIC;
2392 while( p_media->i_instance > 0 )
2393 vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, p_media->instance[0]->psz_name );
2395 if( p_media->cfg.b_vod )
2397 p_media->cfg.b_enabled = false;
2398 vlm_OnMediaUpdate( p_vlm, p_media );
2402 vlm_media_Clean( &p_media->cfg );
2404 vlc_gc_decref( p_media->vod.p_item );
2406 TAB_REMOVE( p_vlm->i_media, p_vlm->media, p_media );
2410 /* Check if we need to unload the VOD server */
2411 if( p_vlm->p_vod && p_vlm->i_vod <= 0 )
2413 module_Unneed( p_vlm->p_vod, p_vlm->p_vod->p_module );
2414 vlc_object_detach( p_vlm->p_vod );
2415 vlc_object_release( p_vlm->p_vod );
2416 p_vlm->p_vod = NULL;
2421 static int vlm_ControlMediaGets( vlm_t *p_vlm, vlm_media_t ***ppp_dsc, int *pi_dsc )
2423 vlm_media_t **pp_dsc;
2427 TAB_INIT( i_dsc, pp_dsc );
2428 for( i = 0; i < p_vlm->i_media; i++ )
2430 vlm_media_t *p_dsc = vlm_media_Duplicate( &p_vlm->media[i]->cfg );
2431 TAB_APPEND( i_dsc, pp_dsc, p_dsc );
2439 static int vlm_ControlMediaClear( vlm_t *p_vlm )
2441 while( p_vlm->i_media > 0 )
2442 vlm_ControlMediaDel( p_vlm, p_vlm->media[0]->cfg.id );
2446 static int vlm_ControlMediaGet( vlm_t *p_vlm, int64_t id, vlm_media_t **pp_dsc )
2448 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2450 return VLC_EGENERIC;
2452 *pp_dsc = vlm_media_Duplicate( &p_media->cfg );
2455 static int vlm_ControlMediaGetId( vlm_t *p_vlm, const char *psz_name, int64_t *p_id )
2457 vlm_media_sys_t *p_media = vlm_ControlMediaGetByName( p_vlm, psz_name );
2459 return VLC_EGENERIC;
2461 *p_id = p_media->cfg.id;
2465 static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
2469 for( i = 0; i < p_media->i_instance; i++ )
2471 const char *psz = p_media->instance[i]->psz_name;
2472 if( ( psz == NULL && psz_id == NULL ) ||
2473 ( psz && psz_id && !strcmp( psz, psz_id ) ) )
2474 return p_media->instance[i];
2478 static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
2480 vlm_media_instance_sys_t *p_instance = malloc( sizeof(vlm_media_instance_sys_t) );
2484 memset( p_instance, 0, sizeof(vlm_media_instance_sys_t) );
2486 p_instance->psz_name = NULL;
2488 p_instance->psz_name = strdup( psz_name );
2490 p_instance->p_item = input_ItemNew( p_vlm, NULL, NULL );
2492 p_instance->i_index = 0;
2493 p_instance->b_sout_keep = false;
2494 p_instance->p_input = NULL;
2495 p_instance->p_sout = NULL;
2499 static void vlm_MediaInstanceDelete( vlm_media_instance_sys_t *p_instance )
2501 if( p_instance->p_input )
2503 input_StopThread( p_instance->p_input );
2504 p_instance->p_sout = input_DetachSout( p_instance->p_input );
2505 vlc_object_release( p_instance->p_input );
2507 if( p_instance->p_sout )
2508 sout_DeleteInstance( p_instance->p_sout );
2510 vlc_gc_decref( p_instance->p_item );
2511 free( p_instance->psz_name );
2516 static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
2518 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2519 vlm_media_instance_sys_t *p_instance;
2522 if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
2523 return VLC_EGENERIC;
2525 /* TODO support multiple input for VOD with sout-keep ? */
2527 if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
2528 return VLC_EGENERIC;
2530 if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
2531 return VLC_EGENERIC;
2533 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2536 vlm_media_t *p_cfg = &p_media->cfg;
2537 const char *psz_keep;
2540 p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
2544 if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
2547 asprintf( &psz_buffer, "sout=%s%s%s",
2548 p_cfg->psz_output ? p_cfg->psz_output : "",
2549 (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
2550 psz_vod_output ? psz_vod_output : "" );
2551 input_ItemAddOption( p_instance->p_item, psz_buffer );
2555 for( i = 0; i < p_cfg->i_option; i++ )
2557 if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
2558 p_instance->b_sout_keep = true;
2559 else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
2560 p_instance->b_sout_keep = false;
2562 input_ItemAddOption( p_instance->p_item, p_cfg->ppsz_option[i] );
2564 /* We force the right sout-keep value (avoid using the sout-keep from the global configuration)
2565 * FIXME implement input list for VOD (need sout-keep)
2567 if( !p_cfg->b_vod && p_instance->b_sout_keep )
2568 psz_keep = "sout-keep";
2570 psz_keep = "no-sout-keep";
2571 input_ItemAddOption( p_instance->p_item, psz_keep );
2573 TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
2576 /* Stop old instance */
2577 if( p_instance->p_input )
2579 if( p_instance->i_index == i_input_index &&
2580 !p_instance->p_input->b_eof && !p_instance->p_input->b_error )
2582 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2583 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2587 input_StopThread( p_instance->p_input );
2588 p_instance->p_sout = input_DetachSout( p_instance->p_input );
2589 vlc_object_release( p_instance->p_input );
2590 if( !p_instance->b_sout_keep && p_instance->p_sout )
2592 sout_DeleteInstance( p_instance->p_sout );
2593 p_instance->p_sout = NULL;
2598 p_instance->i_index = i_input_index;
2599 input_item_SetURI( p_instance->p_item, p_media->cfg.ppsz_input[p_instance->i_index] ) ;
2601 asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name );
2602 p_instance->p_input = input_CreateThreadExtended( p_vlm, p_instance->p_item, psz_log, p_instance->p_sout );
2603 if( !p_instance->p_input )
2605 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2606 vlm_MediaInstanceDelete( p_instance );
2613 static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
2615 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2616 vlm_media_instance_sys_t *p_instance;
2619 return VLC_EGENERIC;
2621 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2623 return VLC_EGENERIC;
2625 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2627 vlm_MediaInstanceDelete( p_instance );
2631 static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
2633 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2634 vlm_media_instance_sys_t *p_instance;
2638 return VLC_EGENERIC;
2640 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2641 if( !p_instance || !p_instance->p_input )
2642 return VLC_EGENERIC;
2644 /* Toggle pause state */
2645 i_state = var_GetInteger( p_instance->p_input, "state" );
2646 if( i_state == PAUSE_S )
2647 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2648 else if( i_state == PLAYING_S )
2649 var_SetInteger( p_instance->p_input, "state", PAUSE_S );
2652 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
2654 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2655 vlm_media_instance_sys_t *p_instance;
2658 return VLC_EGENERIC;
2660 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2661 if( !p_instance || !p_instance->p_input )
2662 return VLC_EGENERIC;
2665 *pi_time = var_GetTime( p_instance->p_input, "time" );
2667 *pd_position = var_GetFloat( p_instance->p_input, "position" );
2670 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t i_time, double d_position )
2672 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2673 vlm_media_instance_sys_t *p_instance;
2676 return VLC_EGENERIC;
2678 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2679 if( !p_instance || !p_instance->p_input )
2680 return VLC_EGENERIC;
2683 return var_SetTime( p_instance->p_input, "time", i_time );
2684 else if( d_position >= 0 && d_position <= 100 )
2685 return var_SetFloat( p_instance->p_input, "position", d_position );
2686 return VLC_EGENERIC;
2689 static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_instance_t ***ppp_idsc, int *pi_instance )
2691 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2692 vlm_media_instance_t **pp_idsc;
2697 return VLC_EGENERIC;
2699 TAB_INIT( i_idsc, pp_idsc );
2700 for( i = 0; i < p_media->i_instance; i++ )
2702 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
2703 vlm_media_instance_t *p_idsc = vlm_media_instance_New();
2705 if( p_instance->psz_name )
2706 p_idsc->psz_name = strdup( p_instance->psz_name );
2707 if( p_instance->p_input )
2709 p_idsc->i_time = var_GetTime( p_instance->p_input, "time" );
2710 p_idsc->i_length = var_GetTime( p_instance->p_input, "length" );
2711 p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
2712 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2713 p_idsc->b_paused = true;
2714 p_idsc->i_rate = var_GetInteger( p_instance->p_input, "rate" );
2717 TAB_APPEND( i_idsc, pp_idsc, p_idsc );
2719 *ppp_idsc = pp_idsc;
2720 *pi_instance = i_idsc;
2723 static int vlm_ControlMediaInstanceClear( vlm_t *p_vlm, int64_t id )
2725 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2728 return VLC_EGENERIC;
2730 while( p_media->i_instance > 0 )
2731 vlm_ControlMediaInstanceStop( p_vlm, id, p_media->instance[0]->psz_name );
2736 static int vlm_ControlScheduleClear( vlm_t *p_vlm )
2738 while( p_vlm->i_schedule > 0 )
2739 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0] );
2743 static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
2746 vlm_media_t **pp_dsc;
2747 vlm_media_t ***ppp_dsc;
2748 vlm_media_instance_t ***ppp_idsc;
2750 const char *psz_vod;
2764 case VLM_GET_MEDIAS:
2765 ppp_dsc = (vlm_media_t ***)va_arg( args, vlm_media_t *** );
2766 pi_int = (int *)va_arg( args, int * );
2767 return vlm_ControlMediaGets( p_vlm, ppp_dsc, pi_int );
2769 case VLM_CLEAR_MEDIAS:
2770 return vlm_ControlMediaClear( p_vlm );
2772 case VLM_CHANGE_MEDIA:
2773 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2774 return vlm_ControlMediaChange( p_vlm, p_dsc );
2777 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2778 p_id = (int64_t*)va_arg( args, int64_t * );
2779 return vlm_ControlMediaAdd( p_vlm, p_dsc, p_id );
2782 id = (int64_t)va_arg( args, int64_t );
2783 return vlm_ControlMediaDel( p_vlm, id );
2786 id = (int64_t)va_arg( args, int64_t );
2787 pp_dsc = (vlm_media_t **)va_arg( args, vlm_media_t ** );
2788 return vlm_ControlMediaGet( p_vlm, id, pp_dsc );
2790 case VLM_GET_MEDIA_ID:
2791 psz_id = (const char*)va_arg( args, const char * );
2792 p_id = (int64_t*)va_arg( args, int64_t * );
2793 return vlm_ControlMediaGetId( p_vlm, psz_id, p_id );
2796 /* Media instance control */
2797 case VLM_GET_MEDIA_INSTANCES:
2798 id = (int64_t)va_arg( args, int64_t );
2799 ppp_idsc = (vlm_media_instance_t ***)va_arg( args, vlm_media_instance_t *** );
2800 pi_int = (int *)va_arg( args, int *);
2801 return vlm_ControlMediaInstanceGets( p_vlm, id, ppp_idsc, pi_int );
2803 case VLM_CLEAR_MEDIA_INSTANCES:
2804 id = (int64_t)va_arg( args, int64_t );
2805 return vlm_ControlMediaInstanceClear( p_vlm, id );
2808 case VLM_START_MEDIA_BROADCAST_INSTANCE:
2809 id = (int64_t)va_arg( args, int64_t );
2810 psz_id = (const char*)va_arg( args, const char* );
2811 i_int = (int)va_arg( args, int );
2812 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, NULL );
2814 case VLM_START_MEDIA_VOD_INSTANCE:
2815 id = (int64_t)va_arg( args, int64_t );
2816 psz_id = (const char*)va_arg( args, const char* );
2817 i_int = (int)va_arg( args, int );
2818 psz_vod = (const char*)va_arg( args, const char* );
2820 return VLC_EGENERIC;
2821 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, psz_vod );
2823 case VLM_STOP_MEDIA_INSTANCE:
2824 id = (int64_t)va_arg( args, int64_t );
2825 psz_id = (const char*)va_arg( args, const char* );
2826 return vlm_ControlMediaInstanceStop( p_vlm, id, psz_id );
2828 case VLM_PAUSE_MEDIA_INSTANCE:
2829 id = (int64_t)va_arg( args, int64_t );
2830 psz_id = (const char*)va_arg( args, const char* );
2831 return vlm_ControlMediaInstancePause( p_vlm, id, psz_id );
2833 case VLM_GET_MEDIA_INSTANCE_TIME:
2834 id = (int64_t)va_arg( args, int64_t );
2835 psz_id = (const char*)va_arg( args, const char* );
2836 pi_i64 = (int64_t*)va_arg( args, int64_t * );
2837 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, pi_i64, NULL );
2838 case VLM_GET_MEDIA_INSTANCE_POSITION:
2839 id = (int64_t)va_arg( args, int64_t );
2840 psz_id = (const char*)va_arg( args, const char* );
2841 pd_double = (double*)va_arg( args, double* );
2842 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, NULL, pd_double );
2844 case VLM_SET_MEDIA_INSTANCE_TIME:
2845 id = (int64_t)va_arg( args, int64_t );
2846 psz_id = (const char*)va_arg( args, const char* );
2847 i_i64 = (int64_t)va_arg( args, int64_t);
2848 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, i_i64, -1 );
2849 case VLM_SET_MEDIA_INSTANCE_POSITION:
2850 id = (int64_t)va_arg( args, int64_t );
2851 psz_id = (const char*)va_arg( args, const char* );
2852 d_double = (double)va_arg( args, double );
2853 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, -1, d_double );
2855 case VLM_CLEAR_SCHEDULES:
2856 return vlm_ControlScheduleClear( p_vlm );
2859 msg_Err( p_vlm, "unknown VLM query" );
2860 return VLC_EGENERIC;
2863 static int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... )
2868 va_start( args, i_query );
2869 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2875 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2880 va_start( args, i_query );
2882 vlc_mutex_lock( &p_vlm->lock );
2883 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2884 vlc_mutex_unlock( &p_vlm->lock );
2891 #else /* ENABLE_VLM */
2893 /* We just define an empty wrapper */
2894 vlm_t *__vlm_New( vlc_object_t *a )
2896 msg_Err( a, "VideoLAN manager support is disabled" );
2900 void vlm_Delete( vlm_t *a )
2905 int vlm_ExecuteCommand( vlm_t *a, const char *b, vlm_message_t **c )
2910 vlm_message_t *vlm_MessageNew( const char *psz_name,
2911 const char *psz_format, ... )
2913 (void)psz_name; (void)psz_format;
2917 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
2918 vlm_message_t *p_child )
2923 void vlm_MessageDelete( vlm_message_t *a )
2928 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2930 (void)p_vlm; (void)i_query;
2931 return VLC_EGENERIC;
2934 #endif /* ENABLE_VLM */