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 if( asprintf(&psz_buffer, "load %s", psz_vlmconf ) == -1 )
160 msg_Dbg( p_this, psz_buffer );
161 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
162 msg_Warn( p_this, "error while loading the configuration file" );
164 vlm_MessageDelete(p_message);
170 vlc_object_set_destructor( p_vlm, (vlc_destructor_t)vlm_Destructor );
171 *pp_vlm = p_vlm; /* for future reference */
172 vlc_mutex_unlock( lockval.p_address );
177 /*****************************************************************************
179 *****************************************************************************/
180 void vlm_Delete( vlm_t *p_vlm )
184 /* vlm_Delete() is serialized against itself, and against vlm_New().
185 * This way, vlm_Destructor () (called from vlc_objet_release() above)
186 * is serialized against setting libvlc_priv->p_vlm from vlm_New(). */
187 var_Get( p_vlm->p_libvlc, "vlm_mutex", &lockval );
188 vlc_mutex_lock( lockval.p_address );
189 vlc_object_release( p_vlm );
190 vlc_mutex_unlock( lockval.p_address );
193 /*****************************************************************************
195 *****************************************************************************/
196 static void vlm_Destructor( vlm_t *p_vlm )
198 libvlc_priv (p_vlm->p_libvlc)->p_vlm = NULL;
200 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
201 TAB_CLEAN( p_vlm->i_media, p_vlm->media );
203 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
204 TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
206 vlc_mutex_destroy( &p_vlm->lock );
209 /*****************************************************************************
210 * vlm_ExecuteCommand:
211 *****************************************************************************/
212 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
213 vlm_message_t **pp_message)
217 vlc_mutex_lock( &p_vlm->lock );
218 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
219 vlc_mutex_unlock( &p_vlm->lock );
225 static const char quotes[] = "\"'";
227 * FindCommandEnd: look for the end of a possibly quoted string
228 * @return NULL on mal-formatted string,
229 * pointer past the last character otherwise.
231 static const char *FindCommandEnd( const char *psz_sent )
235 while( (c = *psz_sent) != '\0' )
239 if( strchr(quotes,c) ) // opening quote
241 else if( isspace(c) ) // non-escaped space
245 psz_sent++; // skip escaped character
246 if( *psz_sent == '\0' )
252 if( c == quote ) // non-escaped matching quote
254 else if( (quote == '"') && (c == '\\') )
256 psz_sent++; // skip escaped character
257 if (*psz_sent == '\0')
258 return NULL; // error, closing quote missing
264 // error (NULL) if we could not find a matching quote
265 return quote ? NULL : psz_sent;
270 * Unescape a nul-terminated string.
271 * Note that in and out can be identical.
273 * @param out output buffer (at least <strlen (in) + 1> characters long)
274 * @param in nul-terminated string to be unescaped
276 * @return 0 on success, -1 on error.
278 static int Unescape( char *out, const char *in )
282 while( (c = *in++) != '\0' )
286 if (strchr(quotes,c)) // opening quote
310 /* None of the special cases - copy the backslash */
316 if( c == quote ) // non-escaped matching quote
321 if( (quote == '"') && (c == '\\') )
330 case '\0': // should never happen
334 /* None of the special cases - copy the backslash */
346 /*****************************************************************************
347 * ExecuteCommand: The main state machine
348 *****************************************************************************
349 * Execute a command which ends with '\0' (string)
350 *****************************************************************************/
351 static int ExecuteSyntaxError( const char *psz_cmd, vlm_message_t **pp_status )
353 *pp_status = vlm_MessageNew( psz_cmd, "Wrong command syntax" );
357 static bool ExecuteIsMedia( vlm_t *p_vlm, const char *psz_name )
361 if( !psz_name || vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
365 static bool ExecuteIsSchedule( vlm_t *p_vlm, const char *psz_name )
367 if( !psz_name || !vlm_ScheduleSearch( p_vlm, psz_name ) )
372 static int ExecuteDel( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
374 vlm_media_sys_t *p_media;
375 vlm_schedule_sys_t *p_schedule;
377 p_media = vlm_MediaSearch( p_vlm, psz_name );
378 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
380 if( p_schedule != NULL )
382 vlm_ScheduleDelete( p_vlm, p_schedule );
384 else if( p_media != NULL )
386 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_media->cfg.id );
388 else if( !strcmp(psz_name, "media") )
390 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
392 else if( !strcmp(psz_name, "schedule") )
394 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
396 else if( !strcmp(psz_name, "all") )
398 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
399 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
403 *pp_status = vlm_MessageNew( "del", "%s: media unknown", psz_name );
407 *pp_status = vlm_MessageNew( "del", vlm_NULL );
411 static int ExecuteShow( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
413 vlm_media_sys_t *p_media;
414 vlm_schedule_sys_t *p_schedule;
418 *pp_status = vlm_Show( p_vlm, NULL, NULL, NULL );
422 p_media = vlm_MediaSearch( p_vlm, psz_name );
423 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
425 if( p_schedule != NULL )
426 *pp_status = vlm_Show( p_vlm, NULL, p_schedule, NULL );
427 else if( p_media != NULL )
428 *pp_status = vlm_Show( p_vlm, p_media, NULL, NULL );
430 *pp_status = vlm_Show( p_vlm, NULL, NULL, psz_name );
435 static int ExecuteHelp( vlm_message_t **pp_status )
437 vlm_message_t *message_child;
439 #define MessageAdd( a ) \
440 vlm_MessageAdd( *pp_status, vlm_MessageNew( a, vlm_NULL ) );
441 #define MessageAddChild( a ) \
442 vlm_MessageAdd( message_child, vlm_MessageNew( a, vlm_NULL ) );
444 *pp_status = vlm_MessageNew( "help", vlm_NULL );
446 message_child = MessageAdd( "Commands Syntax:" );
447 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
448 MessageAddChild( "setup (name) (properties)" );
449 MessageAddChild( "show [(name)|media|schedule]" );
450 MessageAddChild( "del (name)|all|media|schedule" );
451 MessageAddChild( "control (name) [instance_name] (command)" );
452 MessageAddChild( "save (config_file)" );
453 MessageAddChild( "export" );
454 MessageAddChild( "load (config_file)" );
456 message_child = MessageAdd( "Media Proprieties Syntax:" );
457 MessageAddChild( "input (input_name)" );
458 MessageAddChild( "inputdel (input_name)|all" );
459 MessageAddChild( "inputdeln input_number" );
460 MessageAddChild( "output (output_name)" );
461 MessageAddChild( "option (option_name)[=value]" );
462 MessageAddChild( "enabled|disabled" );
463 MessageAddChild( "loop|unloop (broadcast only)" );
464 MessageAddChild( "mux (mux_name)" );
466 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
467 MessageAddChild( "enabled|disabled" );
468 MessageAddChild( "append (command_until_rest_of_the_line)" );
469 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
471 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
472 "(days)-(hours):(minutes):(seconds)" );
473 MessageAddChild( "repeat (number_of_repetitions)" );
475 message_child = MessageAdd( "Control Commands Syntax:" );
476 MessageAddChild( "play [input_number]" );
477 MessageAddChild( "pause" );
478 MessageAddChild( "stop" );
479 MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](miliseconds)ms" );
484 static int ExecuteControl( vlm_t *p_vlm, const char *psz_name, const int i_arg, char ** ppsz_arg, vlm_message_t **pp_status )
486 vlm_media_sys_t *p_media;
487 const char *psz_control = NULL;
488 const char *psz_instance = NULL;
489 const char *psz_argument = NULL;
493 if( !ExecuteIsMedia( p_vlm, psz_name ) )
495 *pp_status = vlm_MessageNew( "control", "%s: media unknown", psz_name );
501 #define IS(txt) ( !strcmp( ppsz_arg[i_index], (txt) ) )
503 if( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") )
506 psz_instance = ppsz_arg[0];
508 if( i_index >= i_arg || ( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") ) )
509 return ExecuteSyntaxError( "control", pp_status );
512 psz_control = ppsz_arg[i_index];
514 if( i_index+1 < i_arg )
515 psz_argument = ppsz_arg[i_index+1];
517 p_media = vlm_MediaSearch( p_vlm, psz_name );
520 if( !strcmp( psz_control, "play" ) )
522 int i_input_index = 0;
525 if( ( psz_argument && sscanf(psz_argument, "%d", &i) == 1 ) && i > 0 && i-1 < p_media->cfg.i_input )
529 else if( psz_argument )
532 vlm_media_t *p_cfg = &p_media->cfg;
533 for ( j=0; j < p_cfg->i_input; j++)
535 if( !strcmp( p_cfg->ppsz_input[j], psz_argument ) )
543 if( p_media->cfg.b_vod )
544 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
546 i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, psz_instance, i_input_index );
548 else if( !strcmp( psz_control, "seek" ) )
553 if( psz_argument[0] == '+' || psz_argument[0] == '-' )
558 if( strstr( psz_argument, "ms" ) || strstr( psz_argument, "s" ) )
563 if( strstr( psz_argument, "ms" ) )
564 i_new_time = 1000 * (int64_t)atoi( psz_argument );
566 i_new_time = 1000000 * (int64_t)atoi( psz_argument );
571 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, &i_time );
572 i_new_time += i_time;
576 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, i_new_time );
581 double d_new_position = i18n_atof( psz_argument ) / 100.0;
585 double d_position = 0.0;
587 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
588 d_new_position += d_position;
590 if( d_new_position < 0.0 )
591 d_new_position = 0.0;
592 else if( d_new_position > 1.0 )
593 d_new_position = 1.0;
594 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_new_position );
599 i_result = VLC_EGENERIC;
602 else if( !strcmp( psz_control, "rewind" ) )
606 const double d_scale = i18n_atof( psz_argument );
609 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
610 d_position -= (d_scale / 1000.0);
611 if( d_position < 0.0 )
613 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
617 i_result = VLC_EGENERIC;
620 else if( !strcmp( psz_control, "forward" ) )
624 const double d_scale = i18n_atof( psz_argument );
627 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
628 d_position += (d_scale / 1000.0);
629 if( d_position > 1.0 )
631 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
636 i_result = VLC_EGENERIC;
639 else if( !strcmp( psz_control, "stop" ) )
641 i_result = vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
643 else if( !strcmp( psz_control, "pause" ) )
645 i_result = vlm_ControlInternal( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
649 i_result = VLC_EGENERIC;
654 *pp_status = vlm_MessageNew( "control", "unknown error" );
657 *pp_status = vlm_MessageNew( "control", vlm_NULL );
661 static int ExecuteExport( vlm_t *p_vlm, vlm_message_t **pp_status )
663 char *psz_export = Save( p_vlm );
665 *pp_status = vlm_MessageNew( "export", psz_export );
670 static int ExecuteSave( vlm_t *p_vlm, const char *psz_file, vlm_message_t **pp_status )
672 FILE *f = utf8_fopen( psz_file, "wt" );
673 char *psz_save = NULL;
678 psz_save = Save( p_vlm );
679 if( psz_save == NULL )
681 if( fputs( psz_save, f ) == EOF )
691 *pp_status = vlm_MessageNew( "save", vlm_NULL );
698 *pp_status = vlm_MessageNew( "save", "Unable to save to file");
702 static int ExecuteLoad( vlm_t *p_vlm, const char *psz_url, vlm_message_t **pp_status )
704 stream_t *p_stream = stream_UrlNew( p_vlm, psz_url );
710 *pp_status = vlm_MessageNew( "load", "Unable to load from file" );
715 if( stream_Seek( p_stream, 0 ) != 0 )
717 stream_Delete( p_stream );
719 *pp_status = vlm_MessageNew( "load", "Read file error" );
723 i_size = stream_Size( p_stream );
725 psz_buffer = malloc( i_size + 1 );
728 stream_Delete( p_stream );
730 *pp_status = vlm_MessageNew( "load", "Read file error" );
734 stream_Read( p_stream, psz_buffer, i_size );
735 psz_buffer[i_size] = '\0';
737 stream_Delete( p_stream );
739 if( Load( p_vlm, psz_buffer ) )
743 *pp_status = vlm_MessageNew( "load", "Error while loading file" );
749 *pp_status = vlm_MessageNew( "load", vlm_NULL );
753 static int ExecuteScheduleProperty( vlm_t *p_vlm, vlm_schedule_sys_t *p_schedule, bool b_new,
754 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
756 const char *psz_cmd = b_new ? "new" : "setup";
759 for( i = 0; i < i_property; i++ )
761 if( !strcmp( ppsz_property[i], "enabled" ) ||
762 !strcmp( ppsz_property[i], "disabled" ) )
764 vlm_ScheduleSetup( p_schedule, ppsz_property[i], NULL );
766 else if( !strcmp( ppsz_property[i], "append" ) )
770 /* Beware: everything behind append is considered as
773 if( ++i >= i_property )
776 psz_line = strdup( ppsz_property[i] );
777 for( j = i+1; j < i_property; j++ )
779 psz_line = realloc( psz_line, strlen(psz_line) + strlen(ppsz_property[j]) + 1 + 1 );
780 strcat( psz_line, " " );
781 strcat( psz_line, ppsz_property[j] );
784 vlm_ScheduleSetup( p_schedule, "append", psz_line );
789 if( i + 1 >= i_property )
792 vlm_ScheduleDelete( p_vlm, p_schedule );
793 return ExecuteSyntaxError( psz_cmd, pp_status );
796 vlm_ScheduleSetup( p_schedule, ppsz_property[i], ppsz_property[i+1] );
800 *pp_status = vlm_MessageNew( psz_cmd, vlm_NULL );
804 static int ExecuteMediaProperty( vlm_t *p_vlm, int64_t id, bool b_new,
805 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
807 const char *psz_cmd = b_new ? "new" : "setup";
808 vlm_media_t *p_cfg = NULL;
814 #define ERROR( txt ) do { *pp_status = vlm_MessageNew( psz_cmd, txt); goto error; } while(0)
815 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA, id, &p_cfg ) )
816 ERROR( "unknown media" );
818 #define MISSING(cmd) do { if( !psz_value ) ERROR( "missing argument for " cmd ); } while(0)
819 for( i = 0; i < i_property; i++ )
821 const char *psz_option = ppsz_property[i];
822 const char *psz_value = i+1 < i_property ? ppsz_property[i+1] : NULL;
824 if( !strcmp( psz_option, "enabled" ) )
826 p_cfg->b_enabled = true;
828 else if( !strcmp( psz_option, "disabled" ) )
830 p_cfg->b_enabled = false;
832 else if( !strcmp( psz_option, "input" ) )
835 TAB_APPEND( p_cfg->i_input, p_cfg->ppsz_input, strdup(psz_value) );
838 else if( !strcmp( psz_option, "inputdel" ) && psz_value && !strcmp( psz_value, "all" ) )
840 while( p_cfg->i_input > 0 )
841 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[0] );
844 else if( !strcmp( psz_option, "inputdel" ) )
848 MISSING( "inputdel" );
850 for( j = 0; j < p_cfg->i_input; j++ )
852 if( !strcmp( p_cfg->ppsz_input[j], psz_value ) )
854 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[j] );
860 else if( !strcmp( psz_option, "inputdeln" ) )
864 MISSING( "inputdeln" );
866 i_index = atoi( psz_value );
867 if( i_index > 0 && i_index <= p_cfg->i_input )
868 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[i_index-1] );
871 else if( !strcmp( psz_option, "output" ) )
875 free( p_cfg->psz_output );
876 p_cfg->psz_output = *psz_value ? strdup( psz_value ) : NULL;
879 else if( !strcmp( psz_option, "option" ) )
883 TAB_APPEND( p_cfg->i_option, p_cfg->ppsz_option, strdup( psz_value ) );
886 else if( !strcmp( psz_option, "loop" ) )
889 ERROR( "invalid loop option for vod" );
890 p_cfg->broadcast.b_loop = true;
892 else if( !strcmp( psz_option, "unloop" ) )
895 ERROR( "invalid unloop option for vod" );
896 p_cfg->broadcast.b_loop = false;
898 else if( !strcmp( psz_option, "mux" ) )
902 ERROR( "invalid mux option for broadcast" );
904 free( p_cfg->vod.psz_mux );
905 p_cfg->vod.psz_mux = *psz_value ? strdup( psz_value ) : NULL;
910 fprintf( stderr, "PROP: name=%s unknown\n", psz_option );
911 ERROR( "Wrong command syntax" );
918 i_result = vlm_ControlInternal( p_vlm, VLM_CHANGE_MEDIA, p_cfg );
919 vlm_media_Delete( p_cfg );
921 *pp_status = vlm_MessageNew( psz_cmd, vlm_NULL );
928 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_cfg->id );
929 vlm_media_Delete( p_cfg );
934 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 )
937 if( !strcmp( psz_name, "all" ) || !strcmp( psz_name, "media" ) || !strcmp( psz_name, "schedule" ) )
939 *pp_status = vlm_MessageNew( "new", "\"all\", \"media\" and \"schedule\" are reserved names" );
942 if( ExecuteIsMedia( p_vlm, psz_name ) || ExecuteIsSchedule( p_vlm, psz_name ) )
944 *pp_status = vlm_MessageNew( "new", "%s: Name already in use", psz_name );
948 if( !strcmp( psz_type, "schedule" ) )
950 vlm_schedule_sys_t *p_schedule = vlm_ScheduleNew( p_vlm, psz_name );
953 *pp_status = vlm_MessageNew( "new", "could not create schedule" );
956 return ExecuteScheduleProperty( p_vlm, p_schedule, true, i_property, ppsz_property, pp_status );
958 else if( !strcmp( psz_type, "vod" ) || !strcmp( psz_type, "broadcast" ) )
963 vlm_media_Init( &cfg );
964 cfg.psz_name = strdup( psz_name );
965 cfg.b_vod = !strcmp( psz_type, "vod" );
967 if( vlm_ControlInternal( p_vlm, VLM_ADD_MEDIA, &cfg, &id ) )
969 vlm_media_Clean( &cfg );
970 *pp_status = vlm_MessageNew( "new", "could not create media" );
973 vlm_media_Clean( &cfg );
974 return ExecuteMediaProperty( p_vlm, id, true, i_property, ppsz_property, pp_status );
978 *pp_status = vlm_MessageNew( "new", "%s: Choose between vod, broadcast or schedule", psz_type );
983 static int ExecuteSetup( vlm_t *p_vlm, const char *psz_name, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
985 if( ExecuteIsSchedule( p_vlm, psz_name ) )
987 vlm_schedule_sys_t *p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
988 return ExecuteScheduleProperty( p_vlm, p_schedule, false, i_property, ppsz_property, pp_status );
990 else if( ExecuteIsMedia( p_vlm, psz_name ) )
993 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
995 return ExecuteMediaProperty( p_vlm, id, false, i_property, ppsz_property, pp_status );
999 *pp_status = vlm_MessageNew( "setup", "%s unknown", psz_name );
1000 return VLC_EGENERIC;
1003 static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
1004 vlm_message_t **pp_message )
1006 size_t i_command = 0;
1007 char buf[strlen (psz_command) + 1], *psz_buf = buf;
1008 char *ppsz_command[3+sizeof (buf) / 2];
1009 vlm_message_t *p_message = NULL;
1011 /* First, parse the line and cut it */
1012 while( *psz_command != '\0' )
1014 const char *psz_temp;
1016 if(isspace (*psz_command))
1022 /* support for comments */
1023 if( i_command == 0 && *psz_command == '#')
1025 p_message = vlm_MessageNew( "", vlm_NULL );
1029 psz_temp = FindCommandEnd( psz_command );
1031 if( psz_temp == NULL )
1033 p_message = vlm_MessageNew( "Incomplete command", psz_command );
1037 assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
1039 ppsz_command[i_command] = psz_buf;
1040 memcpy (psz_buf, psz_command, psz_temp - psz_command);
1041 psz_buf[psz_temp - psz_command] = '\0';
1043 Unescape (psz_buf, psz_buf);
1046 psz_buf += psz_temp - psz_command + 1;
1047 psz_command = psz_temp;
1049 assert (buf + sizeof (buf) >= psz_buf);
1053 * And then Interpret it
1056 #define IF_EXECUTE( name, check, cmd ) if( !strcmp(ppsz_command[0], name ) ) { if( (check) ) goto syntax_error; if( (cmd) ) goto error; goto success; }
1057 if( i_command == 0 )
1059 p_message = vlm_MessageNew( "", vlm_NULL );
1062 else IF_EXECUTE( "del", (i_command != 2), ExecuteDel(p_vlm, ppsz_command[1], &p_message) )
1063 else IF_EXECUTE( "show", (i_command > 2), ExecuteShow(p_vlm, i_command > 1 ? ppsz_command[1] : NULL, &p_message) )
1064 else IF_EXECUTE( "help", (i_command != 1), ExecuteHelp( &p_message ) )
1065 else IF_EXECUTE( "control", (i_command < 3), ExecuteControl(p_vlm, ppsz_command[1], i_command - 2, &ppsz_command[2], &p_message) )
1066 else IF_EXECUTE( "save", (i_command != 2), ExecuteSave(p_vlm, ppsz_command[1], &p_message) )
1067 else IF_EXECUTE( "export", (i_command != 1), ExecuteExport(p_vlm, &p_message) )
1068 else IF_EXECUTE( "load", (i_command != 2), ExecuteLoad(p_vlm, ppsz_command[1], &p_message) )
1069 else IF_EXECUTE( "new", (i_command < 3), ExecuteNew(p_vlm, ppsz_command[1], ppsz_command[2], i_command-3, &ppsz_command[3], &p_message) )
1070 else IF_EXECUTE( "setup", (i_command < 2), ExecuteSetup(p_vlm, ppsz_command[1], i_command-2, &ppsz_command[2], &p_message) )
1073 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
1079 *pp_message = p_message;
1083 return ExecuteSyntaxError( ppsz_command[0], pp_message );
1086 *pp_message = p_message;
1087 return VLC_EGENERIC;
1090 /*****************************************************************************
1092 *****************************************************************************/
1093 vlm_media_sys_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
1097 for( i = 0; i < vlm->i_media; i++ )
1099 if( strcmp( psz_name, vlm->media[i]->cfg.psz_name ) == 0 )
1100 return vlm->media[i];
1106 /*****************************************************************************
1108 *****************************************************************************/
1109 static int64_t vlm_Date(void)
1114 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1116 struct timeval tv_date;
1118 /* gettimeofday() cannot fail given &tv_date is a valid address */
1119 (void)gettimeofday( &tv_date, NULL );
1120 return (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec;
1124 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
1126 vlm_schedule_sys_t *p_sched = malloc( sizeof( vlm_schedule_sys_t ) );
1138 p_sched->psz_name = strdup( psz_name );
1139 p_sched->b_enabled = false;
1140 p_sched->i_command = 0;
1141 p_sched->command = NULL;
1142 p_sched->i_date = 0;
1143 p_sched->i_period = 0;
1144 p_sched->i_repeat = -1;
1146 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1151 /* for now, simple delete. After, del with options (last arg) */
1152 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched )
1154 if( sched == NULL ) return;
1156 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1158 if( vlm->i_schedule == 0 ) free( vlm->schedule );
1159 free( sched->psz_name );
1160 while( sched->i_command )
1162 char *psz_cmd = sched->command[0];
1163 TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1169 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1173 for( i = 0; i < vlm->i_schedule; i++ )
1175 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1177 return vlm->schedule[i];
1184 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1185 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
1186 const char *psz_value )
1188 if( !strcmp( psz_cmd, "enabled" ) )
1190 schedule->b_enabled = true;
1192 else if( !strcmp( psz_cmd, "disabled" ) )
1194 schedule->b_enabled = false;
1196 #if !defined( UNDER_CE )
1197 else if( !strcmp( psz_cmd, "date" ) )
1203 time.tm_sec = 0; /* seconds */
1204 time.tm_min = 0; /* minutes */
1205 time.tm_hour = 0; /* hours */
1206 time.tm_mday = 0; /* day of the month */
1207 time.tm_mon = 0; /* month */
1208 time.tm_year = 0; /* year */
1209 time.tm_wday = 0; /* day of the week */
1210 time.tm_yday = 0; /* day in the year */
1211 time.tm_isdst = -1; /* daylight saving time */
1213 /* date should be year/month/day-hour:minutes:seconds */
1214 p = strchr( psz_value, '-' );
1216 if( !strcmp( psz_value, "now" ) )
1218 schedule->i_date = 0;
1220 else if( (p == NULL) && sscanf( psz_value, "%d:%d:%d", &time.tm_hour,
1221 &time.tm_min, &time.tm_sec ) != 3 )
1222 /* it must be a hour:minutes:seconds */
1230 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1248 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1254 time.tm_mon = i - 1;
1258 time.tm_year = i - 1900;
1259 time.tm_mon = j - 1;
1266 date = mktime( &time );
1267 schedule->i_date = ((mtime_t) date) * 1000000;
1270 else if( !strcmp( psz_cmd, "period" ) )
1274 const char *psz_time = NULL, *psz_date = NULL;
1278 /* First, if date or period are modified, repeat should be equal to -1 */
1279 schedule->i_repeat = -1;
1281 time.tm_sec = 0; /* seconds */
1282 time.tm_min = 0; /* minutes */
1283 time.tm_hour = 0; /* hours */
1284 time.tm_mday = 0; /* day of the month */
1285 time.tm_mon = 0; /* month */
1286 time.tm_year = 0; /* year */
1287 time.tm_wday = 0; /* day of the week */
1288 time.tm_yday = 0; /* day in the year */
1289 time.tm_isdst = -1; /* daylight saving time */
1291 /* date should be year/month/day-hour:minutes:seconds */
1292 p = strchr( psz_value, '-' );
1295 psz_date = psz_value;
1300 psz_time = psz_value;
1303 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1322 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1341 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1342 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1343 schedule->i_period = ((mtime_t) date) * 1000000;
1345 #endif /* UNDER_CE */
1346 else if( !strcmp( psz_cmd, "repeat" ) )
1350 if( sscanf( psz_value, "%d", &i ) == 1 )
1352 schedule->i_repeat = i;
1359 else if( !strcmp( psz_cmd, "append" ) )
1361 char *command = strdup( psz_value );
1363 TAB_APPEND( schedule->i_command, schedule->command, command );
1372 /*****************************************************************************
1373 * Message handling functions
1374 *****************************************************************************/
1375 vlm_message_t *vlm_MessageNew( const char *psz_name,
1376 const char *psz_format, ... )
1378 vlm_message_t *p_message;
1381 if( !psz_name ) return NULL;
1383 p_message = malloc( sizeof(vlm_message_t) );
1389 p_message->psz_value = 0;
1393 va_start( args, psz_format );
1394 if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1403 p_message->psz_name = strdup( psz_name );
1404 p_message->i_child = 0;
1405 p_message->child = NULL;
1410 void vlm_MessageDelete( vlm_message_t *p_message )
1412 free( p_message->psz_name );
1413 free( p_message->psz_value );
1414 while( p_message->i_child-- )
1415 vlm_MessageDelete( p_message->child[p_message->i_child] );
1416 free( p_message->child );
1421 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1422 vlm_message_t *p_child )
1424 if( p_message == NULL ) return NULL;
1428 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1434 /*****************************************************************************
1435 * Misc utility functions
1436 *****************************************************************************/
1437 static vlm_message_t *vlm_ShowMedia( vlm_media_sys_t *p_media )
1439 vlm_media_t *p_cfg = &p_media->cfg;
1440 vlm_message_t *p_msg;
1441 vlm_message_t *p_msg_sub;
1444 p_msg = vlm_MessageNew( p_cfg->psz_name, vlm_NULL );
1445 vlm_MessageAdd( p_msg,
1446 vlm_MessageNew( "type", p_cfg->b_vod ? "vod" : "broadcast" ) );
1447 vlm_MessageAdd( p_msg,
1448 vlm_MessageNew( "enabled", p_cfg->b_enabled ? "yes" : "no" ) );
1451 vlm_MessageAdd( p_msg,
1452 vlm_MessageNew( "mux", p_cfg->vod.psz_mux ) );
1454 vlm_MessageAdd( p_msg,
1455 vlm_MessageNew( "loop", p_cfg->broadcast.b_loop ? "yes" : "no" ) );
1457 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "inputs", vlm_NULL ) );
1458 for( i = 0; i < p_cfg->i_input; i++ )
1461 if( asprintf( &psz_tmp, "%d", i+1 ) != -1 )
1463 vlm_MessageAdd( p_msg_sub,
1464 vlm_MessageNew( psz_tmp, p_cfg->ppsz_input[i] ) );
1469 vlm_MessageAdd( p_msg,
1470 vlm_MessageNew( "output", p_cfg->psz_output ? p_cfg->psz_output : "" ) );
1472 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "options", vlm_NULL ) );
1473 for( i = 0; i < p_cfg->i_option; i++ )
1474 vlm_MessageAdd( p_msg_sub, vlm_MessageNew( p_cfg->ppsz_option[i], vlm_NULL ) );
1476 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "instances", vlm_NULL ) );
1477 for( i = 0; i < p_media->i_instance; i++ )
1479 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1481 vlm_message_t *p_msg_instance;
1485 if( p_instance->p_input )
1486 var_Get( p_instance->p_input, "state", &val );
1488 p_msg_instance = vlm_MessageAdd( p_msg_sub, vlm_MessageNew( "instance" , vlm_NULL ) );
1490 vlm_MessageAdd( p_msg_instance,
1491 vlm_MessageNew( "name" , p_instance->psz_name ? p_instance->psz_name : "default" ) );
1492 vlm_MessageAdd( p_msg_instance,
1493 vlm_MessageNew( "state",
1494 val.i_int == PLAYING_S ? "playing" :
1495 val.i_int == PAUSE_S ? "paused" :
1498 /* FIXME should not do that this way */
1499 if( p_instance->p_input )
1501 #define APPEND_INPUT_INFO( a, format, type ) \
1502 if( asprintf( &psz_tmp, format, \
1503 var_Get ## type( p_instance->p_input, a ) ) != -1 ) \
1505 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( a, \
1509 APPEND_INPUT_INFO( "position", "%f", Float );
1510 APPEND_INPUT_INFO( "time", "%"PRIi64, Time );
1511 APPEND_INPUT_INFO( "length", "%"PRIi64, Time );
1512 APPEND_INPUT_INFO( "rate", "%d", Integer );
1513 APPEND_INPUT_INFO( "title", "%d", Integer );
1514 APPEND_INPUT_INFO( "chapter", "%d", Integer );
1515 APPEND_INPUT_INFO( "seekable", "%d", Bool );
1517 #undef APPEND_INPUT_INFO
1518 if( asprintf( &psz_tmp, "%d", p_instance->i_index + 1 ) != -1 )
1520 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( "playlistindex",
1528 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
1529 vlm_schedule_sys_t *schedule,
1530 const char *psz_filter )
1534 vlm_message_t *p_msg = vlm_MessageNew( "show", vlm_NULL );
1536 vlm_MessageAdd( p_msg, vlm_ShowMedia( media ) );
1540 else if( schedule != NULL )
1544 vlm_message_t *msg_schedule;
1545 vlm_message_t *msg_child;
1548 msg = vlm_MessageNew( "show", vlm_NULL );
1550 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, vlm_NULL ) );
1552 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1554 vlm_MessageAdd( msg_schedule,
1555 vlm_MessageNew( "enabled", schedule->b_enabled ?
1558 #if !defined( UNDER_CE )
1559 if( schedule->i_date != 0 )
1562 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1565 localtime_r( &i_time, &date);
1566 if( asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1567 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1568 date.tm_hour, date.tm_min, date.tm_sec ) != -1 )
1570 vlm_MessageAdd( msg_schedule,
1571 vlm_MessageNew( "date", psz_date ) );
1577 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1580 if( schedule->i_period != 0 )
1582 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1585 date.tm_sec = (int)( i_time % 60 );
1586 i_time = i_time / 60;
1587 date.tm_min = (int)( i_time % 60 );
1588 i_time = i_time / 60;
1589 date.tm_hour = (int)( i_time % 24 );
1590 i_time = i_time / 24;
1591 date.tm_mday = (int)( i_time % 30 );
1592 i_time = i_time / 30;
1593 /* okay, okay, months are not always 30 days long */
1594 date.tm_mon = (int)( i_time % 12 );
1595 i_time = i_time / 12;
1596 date.tm_year = (int)i_time;
1598 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1599 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1601 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1605 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1607 #endif /* UNDER_CE */
1609 sprintf( buffer, "%d", schedule->i_repeat );
1610 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1613 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", vlm_NULL ) );
1615 for( i = 0; i < schedule->i_command; i++ )
1617 vlm_MessageAdd( msg_child,
1618 vlm_MessageNew( schedule->command[i], vlm_NULL ) );
1625 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1627 vlm_message_t *p_msg;
1628 vlm_message_t *p_msg_child;
1629 int i_vod = 0, i_broadcast = 0;
1633 for( i = 0; i < vlm->i_media; i++ )
1635 if( vlm->media[i]->cfg.b_vod )
1641 if( asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast,
1644 p_msg = vlm_MessageNew( "show", vlm_NULL );
1645 p_msg_child = vlm_MessageAdd( p_msg, vlm_MessageNew( "media", psz_count ) );
1648 for( i = 0; i < vlm->i_media; i++ )
1649 vlm_MessageAdd( p_msg_child, vlm_ShowMedia( vlm->media[i] ) );
1654 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1658 vlm_message_t *msg_child;
1660 msg = vlm_MessageNew( "show", vlm_NULL );
1661 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", vlm_NULL ) );
1663 for( i = 0; i < vlm->i_schedule; i++ )
1665 vlm_schedule_sys_t *s = vlm->schedule[i];
1666 vlm_message_t *msg_schedule;
1667 mtime_t i_time, i_next_date;
1669 msg_schedule = vlm_MessageAdd( msg_child,
1670 vlm_MessageNew( s->psz_name, vlm_NULL ) );
1671 vlm_MessageAdd( msg_schedule,
1672 vlm_MessageNew( "enabled", s->b_enabled ?
1675 /* calculate next date */
1676 i_time = vlm_Date();
1677 i_next_date = s->i_date;
1679 if( s->i_period != 0 )
1682 while( s->i_date + j * s->i_period <= i_time &&
1688 i_next_date = s->i_date + j * s->i_period;
1691 if( i_next_date > i_time )
1693 time_t i_date = (time_t) (i_next_date / 1000000) ;
1695 #if !defined( UNDER_CE )
1698 ctime_r( &i_date, psz_date );
1700 char *psz_date = ctime( &i_date );
1703 vlm_MessageAdd( msg_schedule,
1704 vlm_MessageNew( "next launch", psz_date ) );
1712 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
1714 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1715 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1717 vlm_MessageAdd( show1, show2->child[0] );
1719 /* We must destroy the parent node "show" of show2
1720 * and not the children */
1721 free( show2->psz_name );
1729 return vlm_MessageNew( "show", vlm_NULL );
1733 /*****************************************************************************
1734 * Config handling functions
1735 *****************************************************************************/
1736 static int Load( vlm_t *vlm, char *file )
1741 while( *pf != '\0' )
1743 vlm_message_t *message = NULL;
1746 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1751 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1755 if( pf[i_end] == '\n' ) i_end++;
1758 if( *pf && ExecuteCommand( vlm, pf, &message ) )
1762 if( message->psz_value )
1763 msg_Err( vlm, "Load error on line %d: %s: %s",
1764 i_line, message->psz_name, message->psz_value );
1765 vlm_MessageDelete( message );
1769 if( message ) vlm_MessageDelete( message );
1778 static char *Save( vlm_t *vlm )
1781 char psz_header[] = "\n"
1782 "# VLC media player VLM command batch\n"
1783 "# http://www.videolan.org/vlc/\n\n" ;
1786 int i_length = strlen( psz_header );
1788 for( i = 0; i < vlm->i_media; i++ )
1790 vlm_media_sys_t *media = vlm->media[i];
1791 vlm_media_t *p_cfg = &media->cfg;
1794 i_length += strlen( "new * vod " ) + strlen(p_cfg->psz_name);
1796 i_length += strlen( "new * broadcast " ) + strlen(p_cfg->psz_name);
1798 if( p_cfg->b_enabled == true )
1799 i_length += strlen( "enabled" );
1801 i_length += strlen( "disabled" );
1803 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop == true )
1804 i_length += strlen( " loop\n" );
1806 i_length += strlen( "\n" );
1808 for( j = 0; j < p_cfg->i_input; j++ )
1809 i_length += strlen( "setup * input \"\"\n" ) + strlen( p_cfg->psz_name ) + strlen( p_cfg->ppsz_input[j] );
1811 if( p_cfg->psz_output != NULL )
1812 i_length += strlen( "setup * output \n" ) + strlen(p_cfg->psz_name) + strlen(p_cfg->psz_output);
1814 for( j = 0; j < p_cfg->i_option; j++ )
1815 i_length += strlen("setup * option \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->ppsz_option[j]);
1817 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1818 i_length += strlen("setup * mux \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->vod.psz_mux);
1821 for( i = 0; i < vlm->i_schedule; i++ )
1823 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1825 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
1827 if( schedule->b_enabled == true )
1829 i_length += strlen( "date //-:: enabled\n" ) + 14;
1833 i_length += strlen( "date //-:: disabled\n" ) + 14;
1837 if( schedule->i_period != 0 )
1839 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
1840 strlen( "period //-::\n" ) + 14;
1843 if( schedule->i_repeat >= 0 )
1847 sprintf( buffer, "%d", schedule->i_repeat );
1848 i_length += strlen( "setup repeat \n" ) +
1849 strlen( schedule->psz_name ) + strlen( buffer );
1856 for( j = 0; j < schedule->i_command; j++ )
1858 i_length += strlen( "setup append \n" ) +
1859 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
1864 /* Don't forget the '\0' */
1866 /* now we have the length of save */
1868 p = save = malloc( i_length );
1869 if( !save ) return NULL;
1872 p += sprintf( p, "%s", psz_header );
1874 /* finally we can write in it */
1875 for( i = 0; i < vlm->i_media; i++ )
1877 vlm_media_sys_t *media = vlm->media[i];
1878 vlm_media_t *p_cfg = &media->cfg;
1881 p += sprintf( p, "new %s vod ", p_cfg->psz_name );
1883 p += sprintf( p, "new %s broadcast ", p_cfg->psz_name );
1885 if( p_cfg->b_enabled )
1886 p += sprintf( p, "enabled" );
1888 p += sprintf( p, "disabled" );
1890 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1891 p += sprintf( p, " loop\n" );
1893 p += sprintf( p, "\n" );
1895 for( j = 0; j < p_cfg->i_input; j++ )
1896 p += sprintf( p, "setup %s input \"%s\"\n", p_cfg->psz_name, p_cfg->ppsz_input[j] );
1898 if( p_cfg->psz_output )
1899 p += sprintf( p, "setup %s output %s\n", p_cfg->psz_name, p_cfg->psz_output );
1901 for( j = 0; j < p_cfg->i_option; j++ )
1902 p += sprintf( p, "setup %s option %s\n", p_cfg->psz_name, p_cfg->ppsz_option[j] );
1904 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1905 p += sprintf( p, "setup %s mux %s\n", p_cfg->psz_name, p_cfg->vod.psz_mux );
1908 /* and now, the schedule scripts */
1909 #if !defined( UNDER_CE )
1910 for( i = 0; i < vlm->i_schedule; i++ )
1912 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1914 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
1916 localtime_r( &i_time, &date);
1917 p += sprintf( p, "new %s schedule ", schedule->psz_name);
1919 if( schedule->b_enabled == true )
1921 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
1922 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1923 date.tm_hour, date.tm_min, date.tm_sec );
1927 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
1928 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1929 date.tm_hour, date.tm_min, date.tm_sec);
1932 if( schedule->i_period != 0 )
1934 p += sprintf( p, "setup %s ", schedule->psz_name );
1936 i_time = (time_t) ( schedule->i_period / 1000000 );
1938 date.tm_sec = (int)( i_time % 60 );
1939 i_time = i_time / 60;
1940 date.tm_min = (int)( i_time % 60 );
1941 i_time = i_time / 60;
1942 date.tm_hour = (int)( i_time % 24 );
1943 i_time = i_time / 24;
1944 date.tm_mday = (int)( i_time % 30 );
1945 i_time = i_time / 30;
1946 /* okay, okay, months are not always 30 days long */
1947 date.tm_mon = (int)( i_time % 12 );
1948 i_time = i_time / 12;
1949 date.tm_year = (int)i_time;
1951 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
1952 date.tm_year, date.tm_mon, date.tm_mday,
1953 date.tm_hour, date.tm_min, date.tm_sec);
1956 if( schedule->i_repeat >= 0 )
1958 p += sprintf( p, "setup %s repeat %d\n",
1959 schedule->psz_name, schedule->i_repeat );
1963 p += sprintf( p, "\n" );
1966 for( j = 0; j < schedule->i_command; j++ )
1968 p += sprintf( p, "setup %s append %s\n",
1969 schedule->psz_name, schedule->command[j] );
1973 #endif /* UNDER_CE */
1978 /*****************************************************************************
1980 *****************************************************************************/
1981 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
1982 const char *psz_id, int i_query, va_list args )
1984 vlm_t *vlm = (vlm_t *)p_private;
1989 if( !p_private || !p_vod_media )
1990 return VLC_EGENERIC;
1992 vlc_mutex_lock( &vlm->lock );
1995 for( i = 0, id = -1; i < vlm->i_media; i++ )
1997 if( p_vod_media == vlm->media[i]->vod.p_media )
1999 id = vlm->media[i]->cfg.id;
2005 vlc_mutex_unlock( &vlm->lock );
2006 return VLC_EGENERIC;
2011 case VOD_MEDIA_PLAY:
2012 psz = (const char *)va_arg( args, const char * );
2013 i_ret = vlm_ControlInternal( vlm, VLM_START_MEDIA_VOD_INSTANCE, id, psz_id, 0, psz );
2016 case VOD_MEDIA_PAUSE:
2017 i_ret = vlm_ControlInternal( vlm, VLM_PAUSE_MEDIA_INSTANCE, id, psz_id );
2020 case VOD_MEDIA_STOP:
2021 i_ret = vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, id, psz_id );
2024 case VOD_MEDIA_SEEK:
2026 double d_position = (double)va_arg( args, double );
2027 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position/100.0 );
2031 case VOD_MEDIA_REWIND:
2033 double d_scale = (double)va_arg( args, double );
2036 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2037 d_position -= (d_scale / 1000.0);
2038 if( d_position < 0.0 )
2040 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2044 case VOD_MEDIA_FORWARD:
2046 double d_scale = (double)va_arg( args, double );
2049 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2050 d_position += (d_scale / 1000.0);
2051 if( d_position > 1.0 )
2053 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2058 i_ret = VLC_EGENERIC;
2062 vlc_mutex_unlock( &vlm->lock );
2068 /*****************************************************************************
2070 *****************************************************************************/
2071 static int Manage( vlc_object_t* p_object )
2073 vlm_t *vlm = (vlm_t*)p_object;
2075 mtime_t i_lastcheck;
2078 i_lastcheck = vlm_Date();
2080 while( !vlm->b_die )
2082 char **ppsz_scheduled_commands = NULL;
2083 int i_scheduled_commands = 0;
2085 vlc_mutex_lock( &vlm->lock );
2087 /* destroy the inputs that wants to die, and launch the next input */
2088 for( i = 0; i < vlm->i_media; i++ )
2090 vlm_media_sys_t *p_media = vlm->media[i];
2092 for( j = 0; j < p_media->i_instance; )
2094 vlm_media_instance_sys_t *p_instance = p_media->instance[j];
2096 if( p_instance->p_input && ( p_instance->p_input->b_eof || p_instance->p_input->b_error ) )
2098 int i_new_input_index;
2101 i_new_input_index = p_instance->i_index + 1;
2102 if( !p_media->cfg.b_vod && p_media->cfg.broadcast.b_loop && i_new_input_index >= p_media->cfg.i_input )
2103 i_new_input_index = 0;
2105 /* FIXME implement multiple input with VOD */
2106 if( p_media->cfg.b_vod || i_new_input_index >= p_media->cfg.i_input )
2107 vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, p_instance->psz_name );
2109 vlm_ControlInternal( vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, p_instance->psz_name, i_new_input_index );
2121 i_time = vlm_Date();
2123 for( i = 0; i < vlm->i_schedule; i++ )
2125 mtime_t i_real_date = vlm->schedule[i]->i_date;
2127 if( vlm->schedule[i]->b_enabled == true )
2129 if( vlm->schedule[i]->i_date == 0 ) // now !
2131 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2132 i_real_date = i_time;
2134 else if( vlm->schedule[i]->i_period != 0 )
2137 while( vlm->schedule[i]->i_date + j *
2138 vlm->schedule[i]->i_period <= i_lastcheck &&
2139 ( vlm->schedule[i]->i_repeat > j ||
2140 vlm->schedule[i]->i_repeat == -1 ) )
2145 i_real_date = vlm->schedule[i]->i_date + j *
2146 vlm->schedule[i]->i_period;
2149 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2151 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2153 TAB_APPEND( i_scheduled_commands,
2154 ppsz_scheduled_commands,
2155 strdup(vlm->schedule[i]->command[j] ) );
2160 while( i_scheduled_commands )
2162 vlm_message_t *message = NULL;
2163 char *psz_command = ppsz_scheduled_commands[0];
2164 ExecuteCommand( vlm, psz_command,&message );
2166 /* for now, drop the message */
2167 vlm_MessageDelete( message );
2168 TAB_REMOVE( i_scheduled_commands,
2169 ppsz_scheduled_commands,
2171 free( psz_command );
2174 i_lastcheck = i_time;
2176 vlc_mutex_unlock( &vlm->lock );
2191 int i_connection_count;
2192 int i_connection_active;
2198 int i_playing_index;
2201 } vlm_media_status_t;
2205 static vlm_media_sys_t *vlm_ControlMediaGetById( vlm_t *p_vlm, int64_t id )
2209 for( i = 0; i < p_vlm->i_media; i++ )
2211 if( p_vlm->media[i]->cfg.id == id )
2212 return p_vlm->media[i];
2216 static vlm_media_sys_t *vlm_ControlMediaGetByName( vlm_t *p_vlm, const char *psz_name )
2220 for( i = 0; i < p_vlm->i_media; i++ )
2222 if( !strcmp( p_vlm->media[i]->cfg.psz_name, psz_name ) )
2223 return p_vlm->media[i];
2227 static int vlm_MediaDescriptionCheck( vlm_t *p_vlm, vlm_media_t *p_cfg )
2231 if( !p_cfg || !p_cfg->psz_name ||
2232 !strcmp( p_cfg->psz_name, "all" ) || !strcmp( p_cfg->psz_name, "media" ) || !strcmp( p_cfg->psz_name, "schedule" ) )
2233 return VLC_EGENERIC;
2235 for( i = 0; i < p_vlm->i_media; i++ )
2237 if( p_vlm->media[i]->cfg.id == p_cfg->id )
2239 if( !strcmp( p_vlm->media[i]->cfg.psz_name, p_cfg->psz_name ) )
2240 return VLC_EGENERIC;
2246 /* Called after a media description is changed/added */
2247 static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
2249 vlm_media_t *p_cfg = &p_media->cfg;
2250 /* Check if we need to create/delete a vod media */
2253 if( !p_cfg->b_enabled && p_media->vod.p_media )
2255 p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
2256 p_media->vod.p_media = NULL;
2258 else if( p_cfg->b_enabled && !p_media->vod.p_media && p_cfg->i_input )
2260 /* Pre-parse the input */
2261 input_thread_t *p_input;
2267 vlc_gc_decref( p_media->vod.p_item );
2268 p_media->vod.p_item = input_ItemNew( p_vlm, p_cfg->ppsz_input[0],
2271 if( p_cfg->psz_output )
2272 asprintf( &psz_output, "%s:description", p_cfg->psz_output );
2274 asprintf( &psz_output, "#description" );
2276 asprintf( &psz_dup, "sout=%s", psz_output);
2277 input_ItemAddOption( p_media->vod.p_item, psz_dup );
2279 for( i = 0; i < p_cfg->i_option; i++ )
2280 input_ItemAddOption( p_media->vod.p_item,
2281 p_cfg->ppsz_option[i] );
2282 input_ItemAddOption( p_media->vod.p_item, "no-sout-keep" );
2284 asprintf( &psz_header, _("Media: %s"), p_cfg->psz_name );
2286 if( (p_input = input_CreateThreadExtended( p_vlm, p_media->vod.p_item, psz_header, NULL ) ) )
2288 while( !p_input->b_eof && !p_input->b_error )
2291 input_StopThread( p_input );
2292 vlc_object_release( p_input );
2297 if( p_cfg->vod.psz_mux )
2300 es_format_t es, *p_es = &es;
2303 sprintf( fourcc, "%4.4s", p_cfg->vod.psz_mux );
2304 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
2305 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
2307 /* XXX: Don't do it that way, but properly use a new input item ref. */
2308 item = *p_media->vod.p_item;
2311 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
2313 p_media->vod.p_media =
2314 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
2318 p_media->vod.p_media =
2319 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, p_media->vod.p_item );
2325 /* TODO start media if needed */
2328 /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
2332 static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
2334 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
2337 if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
2338 return VLC_EGENERIC;
2339 if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
2340 return VLC_EGENERIC;
2344 /* TODO check what are the changes being done (stop instance if needed) */
2347 vlm_media_Clean( &p_media->cfg );
2348 vlm_media_Copy( &p_media->cfg, p_cfg );
2350 return vlm_OnMediaUpdate( p_vlm, p_media );
2353 static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id )
2355 vlm_media_sys_t *p_media;
2357 if( vlm_MediaDescriptionCheck( p_vlm, p_cfg ) || vlm_ControlMediaGetByName( p_vlm, p_cfg->psz_name ) )
2359 msg_Err( p_vlm, "invalid media description" );
2360 return VLC_EGENERIC;
2362 /* Check if we need to load the VOD server */
2363 if( p_cfg->b_vod && !p_vlm->i_vod )
2365 p_vlm->p_vod = vlc_custom_create( VLC_OBJECT(p_vlm), sizeof( vod_t ),
2366 VLC_OBJECT_GENERIC, "vod server" );
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_release( 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 p_media->vod.p_item = input_ItemNew( p_vlm, NULL, NULL );
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 = false;
2424 vlm_OnMediaUpdate( p_vlm, p_media );
2428 vlm_media_Clean( &p_media->cfg );
2430 vlc_gc_decref( p_media->vod.p_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_release( 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 p_instance->p_item = input_ItemNew( p_vlm, NULL, NULL );
2518 p_instance->i_index = 0;
2519 p_instance->b_sout_keep = 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 p_instance->p_sout = input_DetachSout( p_instance->p_input );
2531 vlc_object_release( p_instance->p_input );
2533 if( p_instance->p_sout )
2534 sout_DeleteInstance( p_instance->p_sout );
2536 vlc_gc_decref( p_instance->p_item );
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;
2563 const char *psz_keep;
2566 p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
2570 if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
2573 asprintf( &psz_buffer, "sout=%s%s%s",
2574 p_cfg->psz_output ? p_cfg->psz_output : "",
2575 (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
2576 psz_vod_output ? psz_vod_output : "" );
2577 input_ItemAddOption( p_instance->p_item, psz_buffer );
2581 for( i = 0; i < p_cfg->i_option; i++ )
2583 if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
2584 p_instance->b_sout_keep = true;
2585 else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
2586 p_instance->b_sout_keep = false;
2588 input_ItemAddOption( p_instance->p_item, p_cfg->ppsz_option[i] );
2590 /* We force the right sout-keep value (avoid using the sout-keep from the global configuration)
2591 * FIXME implement input list for VOD (need sout-keep)
2593 if( !p_cfg->b_vod && p_instance->b_sout_keep )
2594 psz_keep = "sout-keep";
2596 psz_keep = "no-sout-keep";
2597 input_ItemAddOption( p_instance->p_item, psz_keep );
2599 TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
2602 /* Stop old instance */
2603 if( p_instance->p_input )
2605 if( p_instance->i_index == i_input_index &&
2606 !p_instance->p_input->b_eof && !p_instance->p_input->b_error )
2608 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2609 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2613 input_StopThread( p_instance->p_input );
2614 p_instance->p_sout = input_DetachSout( p_instance->p_input );
2615 vlc_object_release( p_instance->p_input );
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->p_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->p_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 = 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] );
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" );
2926 void vlm_Delete( vlm_t *a )
2931 int vlm_ExecuteCommand( vlm_t *a, const char *b, vlm_message_t **c )
2936 vlm_message_t *vlm_MessageNew( const char *psz_name,
2937 const char *psz_format, ... )
2939 (void)psz_name; (void)psz_format;
2943 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
2944 vlm_message_t *p_child )
2949 void vlm_MessageDelete( vlm_message_t *a )
2954 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2956 (void)p_vlm; (void)i_query;
2957 return VLC_EGENERIC;
2960 #endif /* ENABLE_VLM */