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 )
106 /* Avoid multiple creation */
107 if( var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX ) ||
108 var_Get( p_this->p_libvlc, "vlm_mutex", &lockval ) )
111 vlc_mutex_lock( lockval.p_address );
113 p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE );
116 vlc_object_yield( p_vlm );
117 vlc_mutex_unlock( lockval.p_address );
121 msg_Dbg( p_this, "creating VLM" );
123 p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM );
126 vlc_mutex_unlock( lockval.p_address );
130 vlc_mutex_init( p_this->p_libvlc, &p_vlm->lock );
132 TAB_INIT( p_vlm->i_media, p_vlm->media );
133 TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
136 vlc_object_attach( p_vlm, p_this->p_libvlc );
138 if( vlc_thread_create( p_vlm, "vlm thread",
139 Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
141 vlc_mutex_destroy( &p_vlm->lock );
142 vlc_object_release( p_vlm );
146 /* Load our configuration file */
147 psz_vlmconf = var_CreateGetString( p_vlm, "vlm-conf" );
148 if( psz_vlmconf && *psz_vlmconf )
150 vlm_message_t *p_message = NULL;
151 char *psz_buffer = NULL;
153 msg_Dbg( p_this, "loading VLM configuration" );
154 asprintf(&psz_buffer, "load %s", psz_vlmconf );
157 msg_Dbg( p_this, psz_buffer );
158 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
159 msg_Warn( p_this, "error while loading the configuration file" );
161 vlm_MessageDelete(p_message);
167 vlc_object_set_destructor( p_vlm, (vlc_destructor_t)vlm_Destructor );
168 vlc_mutex_unlock( lockval.p_address );
173 /*****************************************************************************
175 *****************************************************************************/
176 void vlm_Delete( vlm_t *p_vlm )
178 vlc_object_release( p_vlm );
181 /*****************************************************************************
183 *****************************************************************************/
184 static void vlm_Destructor( vlm_t *p_vlm )
186 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
187 TAB_CLEAN( p_vlm->i_media, p_vlm->media );
189 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
190 TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
192 vlc_mutex_destroy( &p_vlm->lock );
195 /*****************************************************************************
196 * vlm_ExecuteCommand:
197 *****************************************************************************/
198 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
199 vlm_message_t **pp_message)
203 vlc_mutex_lock( &p_vlm->lock );
204 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
205 vlc_mutex_unlock( &p_vlm->lock );
211 static const char quotes[] = "\"'";
213 * FindCommandEnd: look for the end of a possibly quoted string
214 * @return NULL on mal-formatted string,
215 * pointer past the last character otherwise.
217 static const char *FindCommandEnd( const char *psz_sent )
221 while( (c = *psz_sent) != '\0' )
225 if( strchr(quotes,c) ) // opening quote
227 else if( isspace(c) ) // non-escaped space
231 psz_sent++; // skip escaped character
232 if( *psz_sent == '\0' )
238 if( c == quote ) // non-escaped matching quote
240 else if( (quote == '"') && (c == '\\') )
242 psz_sent++; // skip escaped character
243 if (*psz_sent == '\0')
244 return NULL; // error, closing quote missing
250 // error (NULL) if we could not find a matching quote
251 return quote ? NULL : psz_sent;
256 * Unescape a nul-terminated string.
257 * Note that in and out can be identical.
259 * @param out output buffer (at least <strlen (in) + 1> characters long)
260 * @param in nul-terminated string to be unescaped
262 * @return 0 on success, -1 on error.
264 static int Unescape( char *out, const char *in )
268 while( (c = *in++) != '\0' )
272 if (strchr(quotes,c)) // opening quote
296 /* None of the special cases - copy the backslash */
302 if( c == quote ) // non-escaped matching quote
307 if( (quote == '"') && (c == '\\') )
316 case '\0': // should never happen
320 /* None of the special cases - copy the backslash */
332 /*****************************************************************************
333 * ExecuteCommand: The main state machine
334 *****************************************************************************
335 * Execute a command which ends with '\0' (string)
336 *****************************************************************************/
337 static int ExecuteSyntaxError( const char *psz_cmd, vlm_message_t **pp_status )
339 *pp_status = vlm_MessageNew( psz_cmd, "Wrong command syntax" );
343 static vlc_bool_t ExecuteIsMedia( vlm_t *p_vlm, const char *psz_name )
347 if( !psz_name || vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
351 static vlc_bool_t ExecuteIsSchedule( vlm_t *p_vlm, const char *psz_name )
353 if( !psz_name || !vlm_ScheduleSearch( p_vlm, psz_name ) )
358 static int ExecuteDel( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
360 vlm_media_sys_t *p_media;
361 vlm_schedule_sys_t *p_schedule;
363 p_media = vlm_MediaSearch( p_vlm, psz_name );
364 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
366 if( p_schedule != NULL )
368 vlm_ScheduleDelete( p_vlm, p_schedule );
370 else if( p_media != NULL )
372 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_media->cfg.id );
374 else if( !strcmp(psz_name, "media") )
376 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
378 else if( !strcmp(psz_name, "schedule") )
380 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
382 else if( !strcmp(psz_name, "all") )
384 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
385 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
389 *pp_status = vlm_MessageNew( "del", "%s: media unknown", psz_name );
393 *pp_status = vlm_MessageNew( "del", vlm_NULL );
397 static int ExecuteShow( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
399 vlm_media_sys_t *p_media;
400 vlm_schedule_sys_t *p_schedule;
404 *pp_status = vlm_Show( p_vlm, NULL, NULL, NULL );
408 p_media = vlm_MediaSearch( p_vlm, psz_name );
409 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
411 if( p_schedule != NULL )
412 *pp_status = vlm_Show( p_vlm, NULL, p_schedule, NULL );
413 else if( p_media != NULL )
414 *pp_status = vlm_Show( p_vlm, p_media, NULL, NULL );
416 *pp_status = vlm_Show( p_vlm, NULL, NULL, psz_name );
421 static int ExecuteHelp( vlm_message_t *p_status )
423 vlm_message_t *message_child;
425 #define MessageAdd( a ) \
426 vlm_MessageAdd( p_status, vlm_MessageNew( a, vlm_NULL ) );
427 #define MessageAddChild( a ) \
428 vlm_MessageAdd( message_child, vlm_MessageNew( a, vlm_NULL ) );
430 p_status = vlm_MessageNew( "help", vlm_NULL );
432 message_child = MessageAdd( "Commands Syntax:" );
433 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
434 MessageAddChild( "setup (name) (properties)" );
435 MessageAddChild( "show [(name)|media|schedule]" );
436 MessageAddChild( "del (name)|all|media|schedule" );
437 MessageAddChild( "control (name) [instance_name] (command)" );
438 MessageAddChild( "save (config_file)" );
439 MessageAddChild( "export" );
440 MessageAddChild( "load (config_file)" );
442 message_child = MessageAdd( "Media Proprieties Syntax:" );
443 MessageAddChild( "input (input_name)" );
444 MessageAddChild( "inputdel (input_name)|all" );
445 MessageAddChild( "inputdeln input_number" );
446 MessageAddChild( "output (output_name)" );
447 MessageAddChild( "option (option_name)[=value]" );
448 MessageAddChild( "enabled|disabled" );
449 MessageAddChild( "loop|unloop (broadcast only)" );
450 MessageAddChild( "mux (mux_name)" );
452 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
453 MessageAddChild( "enabled|disabled" );
454 MessageAddChild( "append (command_until_rest_of_the_line)" );
455 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
457 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
458 "(days)-(hours):(minutes):(seconds)" );
459 MessageAddChild( "repeat (number_of_repetitions)" );
461 message_child = MessageAdd( "Control Commands Syntax:" );
462 MessageAddChild( "play [input_number]" );
463 MessageAddChild( "pause" );
464 MessageAddChild( "stop" );
465 MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](miliseconds)ms" );
470 static int ExecuteControl( vlm_t *p_vlm, const char *psz_name, const int i_arg, char ** ppsz_arg, vlm_message_t **pp_status )
472 vlm_media_sys_t *p_media;
473 const char *psz_control = NULL;
474 const char *psz_instance = NULL;
475 const char *psz_argument = NULL;
479 if( !ExecuteIsMedia( p_vlm, psz_name ) )
481 *pp_status = vlm_MessageNew( "control", "%s: media unknown", psz_name );
487 #define IS(txt) ( !strcmp( ppsz_arg[i_index], (txt) ) )
489 if( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") )
492 psz_instance = ppsz_arg[0];
494 if( i_index >= i_arg || ( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") ) )
495 return ExecuteSyntaxError( "control", pp_status );
498 psz_control = ppsz_arg[i_index];
500 if( i_index+1 < i_arg )
501 psz_argument = ppsz_arg[i_index+1];
503 p_media = vlm_MediaSearch( p_vlm, psz_name );
506 if( !strcmp( psz_control, "play" ) )
508 int i_input_index = 0;
511 if( ( psz_argument && sscanf(psz_argument, "%d", &i) == 1 ) && i > 0 && i-1 < p_media->cfg.i_input )
515 else if( psz_argument )
518 vlm_media_t *p_cfg = &p_media->cfg;
519 for ( j=0; j < p_cfg->i_input; j++)
521 if( !strcmp( p_cfg->ppsz_input[j], psz_argument ) )
529 if( p_media->cfg.b_vod )
530 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
532 i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, psz_instance, i_input_index );
534 else if( !strcmp( psz_control, "seek" ) )
538 vlc_bool_t b_relative;
539 if( psz_argument[0] == '+' || psz_argument[0] == '-' )
540 b_relative = VLC_TRUE;
542 b_relative = VLC_FALSE;
544 if( strstr( psz_argument, "ms" ) || strstr( psz_argument, "s" ) )
549 if( strstr( psz_argument, "ms" ) )
550 i_new_time = 1000 * (int64_t)atoi( psz_argument );
552 i_new_time = 1000000 * (int64_t)atoi( psz_argument );
557 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, &i_time );
558 i_new_time += i_time;
562 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, i_new_time );
567 double d_new_position = i18n_atof( psz_argument ) / 100.0;
571 double d_position = 0.0;
573 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
574 d_new_position += d_position;
576 if( d_new_position < 0.0 )
577 d_new_position = 0.0;
578 else if( d_new_position > 1.0 )
579 d_new_position = 1.0;
580 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_new_position );
585 i_result = VLC_EGENERIC;
588 else if( !strcmp( psz_control, "rewind" ) )
592 const double d_scale = i18n_atof( psz_argument );
595 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
596 d_position -= (d_scale / 1000.0);
597 if( d_position < 0.0 )
599 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
603 i_result = VLC_EGENERIC;
606 else if( !strcmp( psz_control, "forward" ) )
610 const double d_scale = i18n_atof( psz_argument );
613 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
614 d_position += (d_scale / 1000.0);
615 if( d_position > 1.0 )
617 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
622 i_result = VLC_EGENERIC;
625 else if( !strcmp( psz_control, "stop" ) )
627 i_result = vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
629 else if( !strcmp( psz_control, "pause" ) )
631 i_result = vlm_ControlInternal( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
635 i_result = VLC_EGENERIC;
640 *pp_status = vlm_MessageNew( "control", "unknown error" );
643 *pp_status = vlm_MessageNew( "control", vlm_NULL );
647 static int ExecuteExport( vlm_t *p_vlm, vlm_message_t **pp_status )
649 char *psz_export = Save( p_vlm );
651 *pp_status = vlm_MessageNew( "export", psz_export );
656 static int ExecuteSave( vlm_t *p_vlm, const char *psz_file, vlm_message_t **pp_status )
658 FILE *f = utf8_fopen( psz_file, "wt" );
664 psz_save = Save( p_vlm );
665 if( psz_save == NULL )
670 fwrite( psz_save, strlen( psz_save ), 1, f );
674 *pp_status = vlm_MessageNew( "save", vlm_NULL );
678 *pp_status = vlm_MessageNew( "save", "Unable to save to file" );
682 static int ExecuteLoad( vlm_t *p_vlm, const char *psz_url, vlm_message_t **pp_status )
684 stream_t *p_stream = stream_UrlNew( p_vlm, psz_url );
690 *pp_status = vlm_MessageNew( "load", "Unable to load from file" );
695 if( stream_Seek( p_stream, 0 ) != 0 )
697 stream_Delete( p_stream );
699 *pp_status = vlm_MessageNew( "load", "Read file error" );
703 i_size = stream_Size( p_stream );
705 psz_buffer = malloc( i_size + 1 );
708 stream_Delete( p_stream );
710 *pp_status = vlm_MessageNew( "load", "Read file error" );
714 stream_Read( p_stream, psz_buffer, i_size );
715 psz_buffer[i_size] = '\0';
717 stream_Delete( p_stream );
719 if( Load( p_vlm, psz_buffer ) )
723 *pp_status = vlm_MessageNew( "load", "Error while loading file" );
729 *pp_status = vlm_MessageNew( "load", vlm_NULL );
733 static int ExecuteScheduleProperty( vlm_t *p_vlm, vlm_schedule_sys_t *p_schedule, vlc_bool_t b_new,
734 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
736 const char *psz_cmd = b_new ? "new" : "setup";
739 for( i = 0; i < i_property; i++ )
741 if( !strcmp( ppsz_property[i], "enabled" ) ||
742 !strcmp( ppsz_property[i], "disabled" ) )
744 vlm_ScheduleSetup( p_schedule, ppsz_property[i], NULL );
746 else if( !strcmp( ppsz_property[i], "append" ) )
750 /* Beware: everything behind append is considered as
753 if( ++i >= i_property )
756 psz_line = strdup( ppsz_property[i] );
757 for( j = i+1; j < i_property; j++ )
759 psz_line = realloc( psz_line, strlen(psz_line) + strlen(ppsz_property[j]) + 1 + 1 );
760 strcat( psz_line, " " );
761 strcat( psz_line, ppsz_property[j] );
764 vlm_ScheduleSetup( p_schedule, "append", psz_line );
769 if( i + 1 >= i_property )
772 vlm_ScheduleDelete( p_vlm, p_schedule );
773 return ExecuteSyntaxError( psz_cmd, pp_status );
776 vlm_ScheduleSetup( p_schedule, ppsz_property[i], ppsz_property[i+1] );
780 *pp_status = vlm_MessageNew( psz_cmd, vlm_NULL );
784 static int ExecuteMediaProperty( vlm_t *p_vlm, int64_t id, vlc_bool_t b_new,
785 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
787 const char *psz_cmd = b_new ? "new" : "setup";
788 vlm_media_t *p_cfg = NULL;
794 #define ERROR( txt ) do { *pp_status = vlm_MessageNew( psz_cmd, txt); goto error; } while(0)
795 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA, id, &p_cfg ) )
796 ERROR( "unknown media" );
798 #define MISSING(cmd) do { if( !psz_value ) ERROR( "missing argument for " cmd ); } while(0)
799 for( i = 0; i < i_property; i++ )
801 const char *psz_option = ppsz_property[i];
802 const char *psz_value = i+1 < i_property ? ppsz_property[i+1] : NULL;
804 if( !strcmp( psz_option, "enabled" ) )
806 p_cfg->b_enabled = VLC_TRUE;
808 else if( !strcmp( psz_option, "disabled" ) )
810 p_cfg->b_enabled = VLC_FALSE;
812 else if( !strcmp( psz_option, "input" ) )
815 TAB_APPEND( p_cfg->i_input, p_cfg->ppsz_input, strdup(psz_value) );
818 else if( !strcmp( psz_option, "inputdel" ) && psz_value && !strcmp( psz_value, "all" ) )
820 while( p_cfg->i_input > 0 )
821 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[0] );
824 else if( !strcmp( psz_option, "inputdel" ) )
828 MISSING( "inputdel" );
830 for( j = 0; j < p_cfg->i_input; j++ )
832 if( !strcmp( p_cfg->ppsz_input[j], psz_value ) )
834 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[j] );
840 else if( !strcmp( psz_option, "inputdeln" ) )
844 MISSING( "inputdeln" );
846 i_index = atoi( psz_value );
847 if( i_index > 0 && i_index <= p_cfg->i_input )
848 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[i_index-1] );
851 else if( !strcmp( psz_option, "output" ) )
855 free( p_cfg->psz_output );
856 p_cfg->psz_output = *psz_value ? strdup( psz_value ) : NULL;
859 else if( !strcmp( psz_option, "option" ) )
863 TAB_APPEND( p_cfg->i_option, p_cfg->ppsz_option, strdup( psz_value ) );
866 else if( !strcmp( psz_option, "loop" ) )
869 ERROR( "invalid loop option for vod" );
870 p_cfg->broadcast.b_loop = VLC_TRUE;
872 else if( !strcmp( psz_option, "unloop" ) )
875 ERROR( "invalid unloop option for vod" );
876 p_cfg->broadcast.b_loop = VLC_FALSE;
878 else if( !strcmp( psz_option, "mux" ) )
882 ERROR( "invalid mux option for broadcast" );
884 free( p_cfg->vod.psz_mux );
885 p_cfg->vod.psz_mux = *psz_value ? strdup( psz_value ) : NULL;
890 fprintf( stderr, "PROP: name=%s unknown\n", psz_option );
891 ERROR( "Wrong command syntax" );
898 i_result = vlm_ControlInternal( p_vlm, VLM_CHANGE_MEDIA, p_cfg );
899 vlm_media_Delete( p_cfg );
901 *pp_status = vlm_MessageNew( psz_cmd, vlm_NULL );
908 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_cfg->id );
909 vlm_media_Delete( p_cfg );
914 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 )
917 if( !strcmp( psz_name, "all" ) || !strcmp( psz_name, "media" ) || !strcmp( psz_name, "schedule" ) )
919 *pp_status = vlm_MessageNew( "new", "\"all\", \"media\" and \"schedule\" are reserved names" );
922 if( ExecuteIsMedia( p_vlm, psz_name ) || ExecuteIsSchedule( p_vlm, psz_name ) )
924 *pp_status = vlm_MessageNew( "new", "%s: Name already in use", psz_name );
928 if( !strcmp( psz_type, "schedule" ) )
930 vlm_schedule_sys_t *p_schedule = vlm_ScheduleNew( p_vlm, psz_name );
933 *pp_status = vlm_MessageNew( "new", "could not create schedule" );
936 return ExecuteScheduleProperty( p_vlm, p_schedule, VLC_TRUE, i_property, ppsz_property, pp_status );
938 else if( !strcmp( psz_type, "vod" ) || !strcmp( psz_type, "broadcast" ) )
943 vlm_media_Init( &cfg );
944 cfg.psz_name = strdup( psz_name );
945 cfg.b_vod = !strcmp( psz_type, "vod" );
947 if( vlm_ControlInternal( p_vlm, VLM_ADD_MEDIA, &cfg, &id ) )
949 vlm_media_Clean( &cfg );
950 *pp_status = vlm_MessageNew( "new", "could not create media" );
953 vlm_media_Clean( &cfg );
954 return ExecuteMediaProperty( p_vlm, id, VLC_TRUE, i_property, ppsz_property, pp_status );
958 *pp_status = vlm_MessageNew( "new", "%s: Choose between vod, broadcast or schedule", psz_type );
963 static int ExecuteSetup( vlm_t *p_vlm, const char *psz_name, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
965 if( ExecuteIsSchedule( p_vlm, psz_name ) )
967 vlm_schedule_sys_t *p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
968 return ExecuteScheduleProperty( p_vlm, p_schedule, VLC_FALSE, i_property, ppsz_property, pp_status );
970 else if( ExecuteIsMedia( p_vlm, psz_name ) )
973 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
975 return ExecuteMediaProperty( p_vlm, id, VLC_FALSE, i_property, ppsz_property, pp_status );
979 *pp_status = vlm_MessageNew( "setup", "%s unknown", psz_name );
983 static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
984 vlm_message_t **pp_message )
986 size_t i_command = 0;
987 char buf[strlen (psz_command) + 1], *psz_buf = buf;
988 char *ppsz_command[3+sizeof (buf) / 2];
989 vlm_message_t *p_message = NULL;
991 /* First, parse the line and cut it */
992 while( *psz_command != '\0' )
994 const char *psz_temp;
996 if(isspace (*psz_command))
1002 /* support for comments */
1003 if( i_command == 0 && *psz_command == '#')
1005 p_message = vlm_MessageNew( "", vlm_NULL );
1009 psz_temp = FindCommandEnd( psz_command );
1011 if( psz_temp == NULL )
1013 p_message = vlm_MessageNew( "Incomplete command", psz_command );
1017 assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
1019 ppsz_command[i_command] = psz_buf;
1020 memcpy (psz_buf, psz_command, psz_temp - psz_command);
1021 psz_buf[psz_temp - psz_command] = '\0';
1023 Unescape (psz_buf, psz_buf);
1026 psz_buf += psz_temp - psz_command + 1;
1027 psz_command = psz_temp;
1029 assert (buf + sizeof (buf) >= psz_buf);
1033 * And then Interpret it
1036 #define IF_EXECUTE( name, check, cmd ) if( !strcmp(ppsz_command[0], name ) ) { if( (check) ) goto syntax_error; if( (cmd) ) goto error; goto success; }
1037 if( i_command == 0 )
1039 p_message = vlm_MessageNew( "", vlm_NULL );
1042 else IF_EXECUTE( "del", (i_command != 2), ExecuteDel(p_vlm, ppsz_command[1], &p_message) )
1043 else IF_EXECUTE( "show", (i_command > 2), ExecuteShow(p_vlm, i_command > 1 ? ppsz_command[1] : NULL, &p_message) )
1044 else IF_EXECUTE( "help", (i_command != 1), ExecuteHelp( p_message) )
1045 else IF_EXECUTE( "control", (i_command < 3), ExecuteControl(p_vlm, ppsz_command[1], i_command - 2, &ppsz_command[2], &p_message) )
1046 else IF_EXECUTE( "save", (i_command != 2), ExecuteSave(p_vlm, ppsz_command[1], &p_message) )
1047 else IF_EXECUTE( "export", (i_command != 1), ExecuteExport(p_vlm, &p_message) )
1048 else IF_EXECUTE( "load", (i_command != 2), ExecuteLoad(p_vlm, ppsz_command[1], &p_message) )
1049 else IF_EXECUTE( "new", (i_command < 3), ExecuteNew(p_vlm, ppsz_command[1], ppsz_command[2], i_command-3, &ppsz_command[3], &p_message) )
1050 else IF_EXECUTE( "setup", (i_command < 2), ExecuteSetup(p_vlm, ppsz_command[1], i_command-2, &ppsz_command[2], &p_message) )
1053 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
1059 *pp_message = p_message;
1063 return ExecuteSyntaxError( ppsz_command[0], pp_message );
1066 *pp_message = p_message;
1067 return VLC_EGENERIC;
1070 /*****************************************************************************
1072 *****************************************************************************/
1073 vlm_media_sys_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
1077 for( i = 0; i < vlm->i_media; i++ )
1079 if( strcmp( psz_name, vlm->media[i]->cfg.psz_name ) == 0 )
1080 return vlm->media[i];
1086 /*****************************************************************************
1088 *****************************************************************************/
1089 static int64_t vlm_Date(void)
1094 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1096 struct timeval tv_date;
1098 /* gettimeofday() cannot fail given &tv_date is a valid address */
1099 (void)gettimeofday( &tv_date, NULL );
1100 return (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec;
1104 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
1106 vlm_schedule_sys_t *p_sched = malloc( sizeof( vlm_schedule_sys_t ) );
1118 p_sched->psz_name = strdup( psz_name );
1119 p_sched->b_enabled = VLC_FALSE;
1120 p_sched->i_command = 0;
1121 p_sched->command = NULL;
1122 p_sched->i_date = 0;
1123 p_sched->i_period = 0;
1124 p_sched->i_repeat = -1;
1126 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1131 /* for now, simple delete. After, del with options (last arg) */
1132 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched )
1134 if( sched == NULL ) return;
1136 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1138 if( vlm->i_schedule == 0 ) free( vlm->schedule );
1139 free( sched->psz_name );
1140 while( sched->i_command )
1142 char *psz_cmd = sched->command[0];
1143 TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1149 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1153 for( i = 0; i < vlm->i_schedule; i++ )
1155 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1157 return vlm->schedule[i];
1164 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1165 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
1166 const char *psz_value )
1168 if( !strcmp( psz_cmd, "enabled" ) )
1170 schedule->b_enabled = VLC_TRUE;
1172 else if( !strcmp( psz_cmd, "disabled" ) )
1174 schedule->b_enabled = VLC_FALSE;
1176 #if !defined( UNDER_CE )
1177 else if( !strcmp( psz_cmd, "date" ) )
1183 time.tm_sec = 0; /* seconds */
1184 time.tm_min = 0; /* minutes */
1185 time.tm_hour = 0; /* hours */
1186 time.tm_mday = 0; /* day of the month */
1187 time.tm_mon = 0; /* month */
1188 time.tm_year = 0; /* year */
1189 time.tm_wday = 0; /* day of the week */
1190 time.tm_yday = 0; /* day in the year */
1191 time.tm_isdst = -1; /* daylight saving time */
1193 /* date should be year/month/day-hour:minutes:seconds */
1194 p = strchr( psz_value, '-' );
1196 if( !strcmp( psz_value, "now" ) )
1198 schedule->i_date = 0;
1200 else if( (p == NULL) && sscanf( psz_value, "%d:%d:%d", &time.tm_hour,
1201 &time.tm_min, &time.tm_sec ) != 3 )
1202 /* it must be a hour:minutes:seconds */
1210 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1228 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1234 time.tm_mon = i - 1;
1238 time.tm_year = i - 1900;
1239 time.tm_mon = j - 1;
1246 date = mktime( &time );
1247 schedule->i_date = ((mtime_t) date) * 1000000;
1250 else if( !strcmp( psz_cmd, "period" ) )
1254 const char *psz_time = NULL, *psz_date = NULL;
1258 /* First, if date or period are modified, repeat should be equal to -1 */
1259 schedule->i_repeat = -1;
1261 time.tm_sec = 0; /* seconds */
1262 time.tm_min = 0; /* minutes */
1263 time.tm_hour = 0; /* hours */
1264 time.tm_mday = 0; /* day of the month */
1265 time.tm_mon = 0; /* month */
1266 time.tm_year = 0; /* year */
1267 time.tm_wday = 0; /* day of the week */
1268 time.tm_yday = 0; /* day in the year */
1269 time.tm_isdst = -1; /* daylight saving time */
1271 /* date should be year/month/day-hour:minutes:seconds */
1272 p = strchr( psz_value, '-' );
1275 psz_date = psz_value;
1280 psz_time = psz_value;
1283 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1302 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1321 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1322 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1323 schedule->i_period = ((mtime_t) date) * 1000000;
1325 #endif /* UNDER_CE */
1326 else if( !strcmp( psz_cmd, "repeat" ) )
1330 if( sscanf( psz_value, "%d", &i ) == 1 )
1332 schedule->i_repeat = i;
1339 else if( !strcmp( psz_cmd, "append" ) )
1341 char *command = strdup( psz_value );
1343 TAB_APPEND( schedule->i_command, schedule->command, command );
1352 /*****************************************************************************
1353 * Message handling functions
1354 *****************************************************************************/
1355 vlm_message_t *vlm_MessageNew( const char *psz_name,
1356 const char *psz_format, ... )
1358 vlm_message_t *p_message;
1361 if( !psz_name ) return NULL;
1363 p_message = malloc( sizeof(vlm_message_t) );
1369 p_message->psz_value = 0;
1373 va_start( args, psz_format );
1374 if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1383 p_message->psz_name = strdup( psz_name );
1384 p_message->i_child = 0;
1385 p_message->child = NULL;
1390 void vlm_MessageDelete( vlm_message_t *p_message )
1392 free( p_message->psz_name );
1393 free( p_message->psz_value );
1394 while( p_message->i_child-- )
1395 vlm_MessageDelete( p_message->child[p_message->i_child] );
1396 free( p_message->child );
1401 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1402 vlm_message_t *p_child )
1404 if( p_message == NULL ) return NULL;
1408 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1414 /*****************************************************************************
1415 * Misc utility functions
1416 *****************************************************************************/
1417 static vlm_message_t *vlm_ShowMedia( vlm_media_sys_t *p_media )
1419 vlm_media_t *p_cfg = &p_media->cfg;
1420 vlm_message_t *p_msg;
1421 vlm_message_t *p_msg_sub;
1424 p_msg = vlm_MessageNew( p_cfg->psz_name, vlm_NULL );
1425 vlm_MessageAdd( p_msg,
1426 vlm_MessageNew( "type", p_cfg->b_vod ? "vod" : "broadcast" ) );
1427 vlm_MessageAdd( p_msg,
1428 vlm_MessageNew( "enabled", p_cfg->b_enabled ? "yes" : "no" ) );
1431 vlm_MessageAdd( p_msg,
1432 vlm_MessageNew( "mux", p_cfg->vod.psz_mux ) );
1434 vlm_MessageAdd( p_msg,
1435 vlm_MessageNew( "loop", p_cfg->broadcast.b_loop ? "yes" : "no" ) );
1437 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "inputs", vlm_NULL ) );
1438 for( i = 0; i < p_cfg->i_input; i++ )
1441 asprintf( &psz_tmp, "%d", i+1 );
1442 vlm_MessageAdd( p_msg_sub,
1443 vlm_MessageNew( psz_tmp, p_cfg->ppsz_input[i] ) );
1447 vlm_MessageAdd( p_msg,
1448 vlm_MessageNew( "output", p_cfg->psz_output ? p_cfg->psz_output : "" ) );
1450 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "options", vlm_NULL ) );
1451 for( i = 0; i < p_cfg->i_option; i++ )
1452 vlm_MessageAdd( p_msg_sub, vlm_MessageNew( p_cfg->ppsz_option[i], vlm_NULL ) );
1454 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "instances", vlm_NULL ) );
1455 for( i = 0; i < p_media->i_instance; i++ )
1457 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1459 vlm_message_t *p_msg_instance;
1463 if( p_instance->p_input )
1464 var_Get( p_instance->p_input, "state", &val );
1466 p_msg_instance = vlm_MessageAdd( p_msg_sub, vlm_MessageNew( "instance" , vlm_NULL ) );
1468 vlm_MessageAdd( p_msg_instance,
1469 vlm_MessageNew( "name" , p_instance->psz_name ? p_instance->psz_name : "default" ) );
1470 vlm_MessageAdd( p_msg_instance,
1471 vlm_MessageNew( "state",
1472 val.i_int == PLAYING_S ? "playing" :
1473 val.i_int == PAUSE_S ? "paused" :
1476 /* FIXME should not do that this way */
1477 if( p_instance->p_input )
1479 #define APPEND_INPUT_INFO( a, format, type ) \
1480 asprintf( &psz_tmp, format, \
1481 var_Get ## type( p_instance->p_input, a ) ); \
1482 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( a, psz_tmp ) ); \
1484 APPEND_INPUT_INFO( "position", "%f", Float );
1485 APPEND_INPUT_INFO( "time", I64Fi, Time );
1486 APPEND_INPUT_INFO( "length", I64Fi, Time );
1487 APPEND_INPUT_INFO( "rate", "%d", Integer );
1488 APPEND_INPUT_INFO( "title", "%d", Integer );
1489 APPEND_INPUT_INFO( "chapter", "%d", Integer );
1490 APPEND_INPUT_INFO( "seekable", "%d", Bool );
1492 #undef APPEND_INPUT_INFO
1493 asprintf( &psz_tmp, "%d", p_instance->i_index + 1 );
1494 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( "playlistindex", psz_tmp ) );
1500 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
1501 vlm_schedule_sys_t *schedule,
1502 const char *psz_filter )
1506 vlm_message_t *p_msg = vlm_MessageNew( "show", vlm_NULL );
1508 vlm_MessageAdd( p_msg, vlm_ShowMedia( media ) );
1512 else if( schedule != NULL )
1516 vlm_message_t *msg_schedule;
1517 vlm_message_t *msg_child;
1520 msg = vlm_MessageNew( "show", vlm_NULL );
1522 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, vlm_NULL ) );
1524 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1526 vlm_MessageAdd( msg_schedule,
1527 vlm_MessageNew( "enabled", schedule->b_enabled ?
1530 #if !defined( UNDER_CE )
1531 if( schedule->i_date != 0 )
1534 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1537 localtime_r( &i_time, &date);
1538 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1539 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1540 date.tm_hour, date.tm_min, date.tm_sec );
1542 vlm_MessageAdd( msg_schedule,
1543 vlm_MessageNew( "date", psz_date ) );
1548 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1551 if( schedule->i_period != 0 )
1553 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1556 date.tm_sec = (int)( i_time % 60 );
1557 i_time = i_time / 60;
1558 date.tm_min = (int)( i_time % 60 );
1559 i_time = i_time / 60;
1560 date.tm_hour = (int)( i_time % 24 );
1561 i_time = i_time / 24;
1562 date.tm_mday = (int)( i_time % 30 );
1563 i_time = i_time / 30;
1564 /* okay, okay, months are not always 30 days long */
1565 date.tm_mon = (int)( i_time % 12 );
1566 i_time = i_time / 12;
1567 date.tm_year = (int)i_time;
1569 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1570 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1572 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1576 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1578 #endif /* UNDER_CE */
1580 sprintf( buffer, "%d", schedule->i_repeat );
1581 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1584 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", vlm_NULL ) );
1586 for( i = 0; i < schedule->i_command; i++ )
1588 vlm_MessageAdd( msg_child,
1589 vlm_MessageNew( schedule->command[i], vlm_NULL ) );
1596 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1598 vlm_message_t *p_msg;
1599 vlm_message_t *p_msg_child;
1600 int i_vod = 0, i_broadcast = 0;
1604 for( i = 0; i < vlm->i_media; i++ )
1606 if( vlm->media[i]->cfg.b_vod )
1612 asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1614 p_msg = vlm_MessageNew( "show", vlm_NULL );
1615 p_msg_child = vlm_MessageAdd( p_msg, vlm_MessageNew( "media", psz_count ) );
1618 for( i = 0; i < vlm->i_media; i++ )
1619 vlm_MessageAdd( p_msg_child, vlm_ShowMedia( vlm->media[i] ) );
1624 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1628 vlm_message_t *msg_child;
1630 msg = vlm_MessageNew( "show", vlm_NULL );
1631 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", vlm_NULL ) );
1633 for( i = 0; i < vlm->i_schedule; i++ )
1635 vlm_schedule_sys_t *s = vlm->schedule[i];
1636 vlm_message_t *msg_schedule;
1637 mtime_t i_time, i_next_date;
1639 msg_schedule = vlm_MessageAdd( msg_child,
1640 vlm_MessageNew( s->psz_name, vlm_NULL ) );
1641 vlm_MessageAdd( msg_schedule,
1642 vlm_MessageNew( "enabled", s->b_enabled ?
1645 /* calculate next date */
1646 i_time = vlm_Date();
1647 i_next_date = s->i_date;
1649 if( s->i_period != 0 )
1652 while( s->i_date + j * s->i_period <= i_time &&
1658 i_next_date = s->i_date + j * s->i_period;
1661 if( i_next_date > i_time )
1663 time_t i_date = (time_t) (i_next_date / 1000000) ;
1665 #if !defined( UNDER_CE )
1668 ctime_r( &i_date, psz_date );
1670 char *psz_date = ctime( &i_date );
1673 vlm_MessageAdd( msg_schedule,
1674 vlm_MessageNew( "next launch", psz_date ) );
1682 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
1684 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1685 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1687 vlm_MessageAdd( show1, show2->child[0] );
1689 /* We must destroy the parent node "show" of show2
1690 * and not the children */
1691 free( show2->psz_name );
1699 return vlm_MessageNew( "show", vlm_NULL );
1703 /*****************************************************************************
1704 * Config handling functions
1705 *****************************************************************************/
1706 static int Load( vlm_t *vlm, char *file )
1711 while( *pf != '\0' )
1713 vlm_message_t *message = NULL;
1716 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1721 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1725 if( pf[i_end] == '\n' ) i_end++;
1728 if( *pf && ExecuteCommand( vlm, pf, &message ) )
1732 if( message->psz_value )
1733 msg_Err( vlm, "Load error on line %d: %s: %s",
1734 i_line, message->psz_name, message->psz_value );
1735 vlm_MessageDelete( message );
1739 if( message ) vlm_MessageDelete( message );
1748 static char *Save( vlm_t *vlm )
1751 char psz_header[] = "\n"
1752 "# VLC media player VLM command batch\n"
1753 "# http://www.videolan.org/vlc/\n\n" ;
1756 int i_length = strlen( psz_header );
1758 for( i = 0; i < vlm->i_media; i++ )
1760 vlm_media_sys_t *media = vlm->media[i];
1761 vlm_media_t *p_cfg = &media->cfg;
1764 i_length += strlen( "new * vod " ) + strlen(p_cfg->psz_name);
1766 i_length += strlen( "new * broadcast " ) + strlen(p_cfg->psz_name);
1768 if( p_cfg->b_enabled == VLC_TRUE )
1769 i_length += strlen( "enabled" );
1771 i_length += strlen( "disabled" );
1773 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop == VLC_TRUE )
1774 i_length += strlen( " loop\n" );
1776 i_length += strlen( "\n" );
1778 for( j = 0; j < p_cfg->i_input; j++ )
1779 i_length += strlen( "setup * input \"\"\n" ) + strlen( p_cfg->psz_name ) + strlen( p_cfg->ppsz_input[j] );
1781 if( p_cfg->psz_output != NULL )
1782 i_length += strlen( "setup * output \n" ) + strlen(p_cfg->psz_name) + strlen(p_cfg->psz_output);
1784 for( j = 0; j < p_cfg->i_option; j++ )
1785 i_length += strlen("setup * option \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->ppsz_option[j]);
1787 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1788 i_length += strlen("setup * mux \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->vod.psz_mux);
1791 for( i = 0; i < vlm->i_schedule; i++ )
1793 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1795 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
1797 if( schedule->b_enabled == VLC_TRUE )
1799 i_length += strlen( "date //-:: enabled\n" ) + 14;
1803 i_length += strlen( "date //-:: disabled\n" ) + 14;
1807 if( schedule->i_period != 0 )
1809 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
1810 strlen( "period //-::\n" ) + 14;
1813 if( schedule->i_repeat >= 0 )
1817 sprintf( buffer, "%d", schedule->i_repeat );
1818 i_length += strlen( "setup repeat \n" ) +
1819 strlen( schedule->psz_name ) + strlen( buffer );
1826 for( j = 0; j < schedule->i_command; j++ )
1828 i_length += strlen( "setup append \n" ) +
1829 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
1834 /* Don't forget the '\0' */
1836 /* now we have the length of save */
1838 p = save = malloc( i_length );
1839 if( !save ) return NULL;
1842 p += sprintf( p, "%s", psz_header );
1844 /* finally we can write in it */
1845 for( i = 0; i < vlm->i_media; i++ )
1847 vlm_media_sys_t *media = vlm->media[i];
1848 vlm_media_t *p_cfg = &media->cfg;
1851 p += sprintf( p, "new %s vod ", p_cfg->psz_name );
1853 p += sprintf( p, "new %s broadcast ", p_cfg->psz_name );
1855 if( p_cfg->b_enabled )
1856 p += sprintf( p, "enabled" );
1858 p += sprintf( p, "disabled" );
1860 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1861 p += sprintf( p, " loop\n" );
1863 p += sprintf( p, "\n" );
1865 for( j = 0; j < p_cfg->i_input; j++ )
1866 p += sprintf( p, "setup %s input \"%s\"\n", p_cfg->psz_name, p_cfg->ppsz_input[j] );
1868 if( p_cfg->psz_output )
1869 p += sprintf( p, "setup %s output %s\n", p_cfg->psz_name, p_cfg->psz_output );
1871 for( j = 0; j < p_cfg->i_option; j++ )
1872 p += sprintf( p, "setup %s option %s\n", p_cfg->psz_name, p_cfg->ppsz_option[j] );
1874 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1875 p += sprintf( p, "setup %s mux %s\n", p_cfg->psz_name, p_cfg->vod.psz_mux );
1878 /* and now, the schedule scripts */
1879 #if !defined( UNDER_CE )
1880 for( i = 0; i < vlm->i_schedule; i++ )
1882 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1884 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
1886 localtime_r( &i_time, &date);
1887 p += sprintf( p, "new %s schedule ", schedule->psz_name);
1889 if( schedule->b_enabled == VLC_TRUE )
1891 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
1892 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1893 date.tm_hour, date.tm_min, date.tm_sec );
1897 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
1898 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1899 date.tm_hour, date.tm_min, date.tm_sec);
1902 if( schedule->i_period != 0 )
1904 p += sprintf( p, "setup %s ", schedule->psz_name );
1906 i_time = (time_t) ( schedule->i_period / 1000000 );
1908 date.tm_sec = (int)( i_time % 60 );
1909 i_time = i_time / 60;
1910 date.tm_min = (int)( i_time % 60 );
1911 i_time = i_time / 60;
1912 date.tm_hour = (int)( i_time % 24 );
1913 i_time = i_time / 24;
1914 date.tm_mday = (int)( i_time % 30 );
1915 i_time = i_time / 30;
1916 /* okay, okay, months are not always 30 days long */
1917 date.tm_mon = (int)( i_time % 12 );
1918 i_time = i_time / 12;
1919 date.tm_year = (int)i_time;
1921 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
1922 date.tm_year, date.tm_mon, date.tm_mday,
1923 date.tm_hour, date.tm_min, date.tm_sec);
1926 if( schedule->i_repeat >= 0 )
1928 p += sprintf( p, "setup %s repeat %d\n",
1929 schedule->psz_name, schedule->i_repeat );
1933 p += sprintf( p, "\n" );
1936 for( j = 0; j < schedule->i_command; j++ )
1938 p += sprintf( p, "setup %s append %s\n",
1939 schedule->psz_name, schedule->command[j] );
1943 #endif /* UNDER_CE */
1948 /*****************************************************************************
1950 *****************************************************************************/
1951 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
1952 const char *psz_id, int i_query, va_list args )
1954 vlm_t *vlm = (vlm_t *)p_private;
1959 if( !p_private || !p_vod_media )
1960 return VLC_EGENERIC;
1962 vlc_mutex_lock( &vlm->lock );
1965 for( i = 0, id = -1; i < vlm->i_media; i++ )
1967 if( p_vod_media == vlm->media[i]->vod.p_media )
1969 id = vlm->media[i]->cfg.id;
1975 vlc_mutex_unlock( &vlm->lock );
1976 return VLC_EGENERIC;
1981 case VOD_MEDIA_PLAY:
1982 psz = (const char *)va_arg( args, const char * );
1983 i_ret = vlm_ControlInternal( vlm, VLM_START_MEDIA_VOD_INSTANCE, id, psz_id, 0, psz );
1986 case VOD_MEDIA_PAUSE:
1987 i_ret = vlm_ControlInternal( vlm, VLM_PAUSE_MEDIA_INSTANCE, id, psz_id );
1990 case VOD_MEDIA_STOP:
1991 i_ret = vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, id, psz_id );
1994 case VOD_MEDIA_SEEK:
1996 double d_position = (double)va_arg( args, double );
1997 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position/100.0 );
2001 case VOD_MEDIA_REWIND:
2003 double d_scale = (double)va_arg( args, double );
2006 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2007 d_position -= (d_scale / 1000.0);
2008 if( d_position < 0.0 )
2010 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2014 case VOD_MEDIA_FORWARD:
2016 double d_scale = (double)va_arg( args, double );
2019 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2020 d_position += (d_scale / 1000.0);
2021 if( d_position > 1.0 )
2023 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2028 i_ret = VLC_EGENERIC;
2032 vlc_mutex_unlock( &vlm->lock );
2038 /*****************************************************************************
2040 *****************************************************************************/
2041 static int Manage( vlc_object_t* p_object )
2043 vlm_t *vlm = (vlm_t*)p_object;
2045 mtime_t i_lastcheck;
2048 i_lastcheck = vlm_Date();
2050 while( !vlm->b_die )
2052 char **ppsz_scheduled_commands = NULL;
2053 int i_scheduled_commands = 0;
2055 vlc_mutex_lock( &vlm->lock );
2057 /* destroy the inputs that wants to die, and launch the next input */
2058 for( i = 0; i < vlm->i_media; i++ )
2060 vlm_media_sys_t *p_media = vlm->media[i];
2062 for( j = 0; j < p_media->i_instance; )
2064 vlm_media_instance_sys_t *p_instance = p_media->instance[j];
2066 if( p_instance->p_input && ( p_instance->p_input->b_eof || p_instance->p_input->b_error ) )
2068 int i_new_input_index;
2071 i_new_input_index = p_instance->i_index + 1;
2072 if( !p_media->cfg.b_vod && p_media->cfg.broadcast.b_loop && i_new_input_index >= p_media->cfg.i_input )
2073 i_new_input_index = 0;
2075 /* FIXME implement multiple input with VOD */
2076 if( p_media->cfg.b_vod || i_new_input_index >= p_media->cfg.i_input )
2077 vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, p_instance->psz_name );
2079 vlm_ControlInternal( vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, p_instance->psz_name, i_new_input_index );
2091 i_time = vlm_Date();
2093 for( i = 0; i < vlm->i_schedule; i++ )
2095 mtime_t i_real_date = vlm->schedule[i]->i_date;
2097 if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2099 if( vlm->schedule[i]->i_date == 0 ) // now !
2101 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2102 i_real_date = i_time;
2104 else if( vlm->schedule[i]->i_period != 0 )
2107 while( vlm->schedule[i]->i_date + j *
2108 vlm->schedule[i]->i_period <= i_lastcheck &&
2109 ( vlm->schedule[i]->i_repeat > j ||
2110 vlm->schedule[i]->i_repeat == -1 ) )
2115 i_real_date = vlm->schedule[i]->i_date + j *
2116 vlm->schedule[i]->i_period;
2119 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2121 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2123 TAB_APPEND( i_scheduled_commands,
2124 ppsz_scheduled_commands,
2125 strdup(vlm->schedule[i]->command[j] ) );
2130 while( i_scheduled_commands )
2132 vlm_message_t *message = NULL;
2133 char *psz_command = ppsz_scheduled_commands[0];
2134 ExecuteCommand( vlm, psz_command,&message );
2136 /* for now, drop the message */
2137 vlm_MessageDelete( message );
2138 TAB_REMOVE( i_scheduled_commands,
2139 ppsz_scheduled_commands,
2141 free( psz_command );
2144 i_lastcheck = i_time;
2146 vlc_mutex_unlock( &vlm->lock );
2161 int i_connection_count;
2162 int i_connection_active;
2167 vlc_bool_t b_playing;
2168 int i_playing_index;
2171 } vlm_media_status_t;
2175 static vlm_media_sys_t *vlm_ControlMediaGetById( vlm_t *p_vlm, int64_t id )
2179 for( i = 0; i < p_vlm->i_media; i++ )
2181 if( p_vlm->media[i]->cfg.id == id )
2182 return p_vlm->media[i];
2186 static vlm_media_sys_t *vlm_ControlMediaGetByName( vlm_t *p_vlm, const char *psz_name )
2190 for( i = 0; i < p_vlm->i_media; i++ )
2192 if( !strcmp( p_vlm->media[i]->cfg.psz_name, psz_name ) )
2193 return p_vlm->media[i];
2197 static int vlm_MediaDescriptionCheck( vlm_t *p_vlm, vlm_media_t *p_cfg )
2201 if( !p_cfg || !p_cfg->psz_name ||
2202 !strcmp( p_cfg->psz_name, "all" ) || !strcmp( p_cfg->psz_name, "media" ) || !strcmp( p_cfg->psz_name, "schedule" ) )
2203 return VLC_EGENERIC;
2205 for( i = 0; i < p_vlm->i_media; i++ )
2207 if( p_vlm->media[i]->cfg.id == p_cfg->id )
2209 if( !strcmp( p_vlm->media[i]->cfg.psz_name, p_cfg->psz_name ) )
2210 return VLC_EGENERIC;
2216 /* Called after a media description is changed/added */
2217 static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
2219 vlm_media_t *p_cfg = &p_media->cfg;
2220 /* Check if we need to create/delete a vod media */
2223 if( !p_cfg->b_enabled && p_media->vod.p_media )
2225 p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
2226 p_media->vod.p_media = NULL;
2228 else if( p_cfg->b_enabled && !p_media->vod.p_media && p_cfg->i_input )
2230 /* Pre-parse the input */
2231 input_thread_t *p_input;
2237 input_ItemClean( &p_media->vod.item );
2238 input_ItemInit( VLC_OBJECT(p_vlm), &p_media->vod.item );
2240 if( p_cfg->psz_output )
2241 asprintf( &psz_output, "%s:description", p_cfg->psz_output );
2243 asprintf( &psz_output, "#description" );
2245 p_media->vod.item.psz_uri = strdup( p_cfg->ppsz_input[0] );
2247 asprintf( &psz_dup, "sout=%s", psz_output);
2248 input_ItemAddOption( &p_media->vod.item, psz_dup );
2250 for( i = 0; i < p_cfg->i_option; i++ )
2251 input_ItemAddOption( &p_media->vod.item,
2252 p_cfg->ppsz_option[i] );
2253 input_ItemAddOption( &p_media->vod.item, "no-sout-keep" );
2255 asprintf( &psz_header, _("Media: %s"), p_cfg->psz_name );
2257 if( (p_input = input_CreateThreadExtended( p_vlm, &p_media->vod.item, psz_header, NULL ) ) )
2259 while( !p_input->b_eof && !p_input->b_error )
2262 input_StopThread( p_input );
2263 vlc_object_release( p_input );
2268 if( p_cfg->vod.psz_mux )
2271 es_format_t es, *p_es = &es;
2274 sprintf( fourcc, "%4.4s", p_cfg->vod.psz_mux );
2275 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
2276 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
2278 item = p_media->vod.item;
2281 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
2283 p_media->vod.p_media =
2284 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
2288 p_media->vod.p_media =
2289 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &p_media->vod.item );
2295 /* TODO start media if needed */
2298 /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
2302 static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
2304 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
2307 if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
2308 return VLC_EGENERIC;
2309 if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
2310 return VLC_EGENERIC;
2314 /* TODO check what are the changes being done (stop instance if needed) */
2317 vlm_media_Clean( &p_media->cfg );
2318 vlm_media_Copy( &p_media->cfg, p_cfg );
2320 return vlm_OnMediaUpdate( p_vlm, p_media );
2323 static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id )
2325 vlm_media_sys_t *p_media;
2327 if( vlm_MediaDescriptionCheck( p_vlm, p_cfg ) || vlm_ControlMediaGetByName( p_vlm, p_cfg->psz_name ) )
2329 msg_Err( p_vlm, "invalid media description" );
2330 return VLC_EGENERIC;
2332 /* Check if we need to load the VOD server */
2333 if( p_cfg->b_vod && !p_vlm->i_vod )
2335 p_vlm->p_vod = vlc_custom_create( VLC_OBJECT(p_vlm),
2336 sizeof( p_vlm->p_vod ),
2337 VLC_OBJECT_GENERIC, "vod server" );
2338 vlc_object_attach( p_vlm->p_vod, p_vlm );
2339 p_vlm->p_vod->p_module = module_Need( p_vlm->p_vod, "vod server", 0, 0 );
2340 if( !p_vlm->p_vod->p_module )
2342 msg_Err( p_vlm, "cannot find vod server" );
2343 vlc_object_detach( p_vlm->p_vod );
2344 vlc_object_release( p_vlm->p_vod );
2346 return VLC_EGENERIC;
2349 p_vlm->p_vod->p_data = p_vlm;
2350 p_vlm->p_vod->pf_media_control = vlm_MediaVodControl;
2353 p_media = malloc( sizeof( vlm_media_sys_t ) );
2356 msg_Err( p_vlm, "out of memory" );
2359 memset( p_media, 0, sizeof(vlm_media_sys_t) );
2364 vlm_media_Copy( &p_media->cfg, p_cfg );
2365 p_media->cfg.id = p_vlm->i_id++;
2366 /* FIXME do we do something here if enabled is true ? */
2368 input_ItemInit( VLC_OBJECT(p_vlm), &p_media->vod.item );
2370 p_media->vod.p_media = NULL;
2371 TAB_INIT( p_media->i_instance, p_media->instance );
2374 TAB_APPEND( p_vlm->i_media, p_vlm->media, p_media );
2377 *p_id = p_media->cfg.id;
2379 return vlm_OnMediaUpdate( p_vlm, p_media );
2382 static int vlm_ControlMediaDel( vlm_t *p_vlm, int64_t id )
2384 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2387 return VLC_EGENERIC;
2389 while( p_media->i_instance > 0 )
2390 vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, p_media->instance[0]->psz_name );
2392 if( p_media->cfg.b_vod )
2394 p_media->cfg.b_enabled = VLC_FALSE;
2395 vlm_OnMediaUpdate( p_vlm, p_media );
2399 vlm_media_Clean( &p_media->cfg );
2401 input_ItemClean( &p_media->vod.item );
2403 TAB_REMOVE( p_vlm->i_media, p_vlm->media, p_media );
2407 /* Check if we need to unload the VOD server */
2408 if( p_vlm->p_vod && p_vlm->i_vod <= 0 )
2410 module_Unneed( p_vlm->p_vod, p_vlm->p_vod->p_module );
2411 vlc_object_detach( p_vlm->p_vod );
2412 vlc_object_release( p_vlm->p_vod );
2413 p_vlm->p_vod = NULL;
2418 static int vlm_ControlMediaGets( vlm_t *p_vlm, vlm_media_t ***ppp_dsc, int *pi_dsc )
2420 vlm_media_t **pp_dsc;
2424 TAB_INIT( i_dsc, pp_dsc );
2425 for( i = 0; i < p_vlm->i_media; i++ )
2427 vlm_media_t *p_dsc = vlm_media_Duplicate( &p_vlm->media[i]->cfg );
2428 TAB_APPEND( i_dsc, pp_dsc, p_dsc );
2436 static int vlm_ControlMediaClear( vlm_t *p_vlm )
2438 while( p_vlm->i_media > 0 )
2439 vlm_ControlMediaDel( p_vlm, p_vlm->media[0]->cfg.id );
2443 static int vlm_ControlMediaGet( vlm_t *p_vlm, int64_t id, vlm_media_t **pp_dsc )
2445 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2447 return VLC_EGENERIC;
2449 *pp_dsc = vlm_media_Duplicate( &p_media->cfg );
2452 static int vlm_ControlMediaGetId( vlm_t *p_vlm, const char *psz_name, int64_t *p_id )
2454 vlm_media_sys_t *p_media = vlm_ControlMediaGetByName( p_vlm, psz_name );
2456 return VLC_EGENERIC;
2458 *p_id = p_media->cfg.id;
2462 static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
2466 for( i = 0; i < p_media->i_instance; i++ )
2468 const char *psz = p_media->instance[i]->psz_name;
2469 if( ( psz == NULL && psz_id == NULL ) ||
2470 ( psz && psz_id && !strcmp( psz, psz_id ) ) )
2471 return p_media->instance[i];
2475 static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
2477 vlm_media_instance_sys_t *p_instance = malloc( sizeof(vlm_media_instance_sys_t) );
2481 memset( p_instance, 0, sizeof(vlm_media_instance_sys_t) );
2483 p_instance->psz_name = NULL;
2485 p_instance->psz_name = strdup( psz_name );
2487 input_ItemInit( VLC_OBJECT(p_vlm), &p_instance->item );
2489 p_instance->i_index = 0;
2490 p_instance->b_sout_keep = VLC_FALSE;
2491 p_instance->p_input = NULL;
2492 p_instance->p_sout = NULL;
2496 static void vlm_MediaInstanceDelete( vlm_media_instance_sys_t *p_instance )
2498 if( p_instance->p_input )
2500 input_StopThread( p_instance->p_input );
2501 p_instance->p_sout = input_DetachSout( p_instance->p_input );
2502 vlc_object_release( p_instance->p_input );
2504 if( p_instance->p_sout )
2505 sout_DeleteInstance( p_instance->p_sout );
2507 input_ItemClean( &p_instance->item );
2508 free( p_instance->psz_name );
2513 static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
2515 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2516 vlm_media_instance_sys_t *p_instance;
2519 if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
2520 return VLC_EGENERIC;
2522 /* TODO support multiple input for VOD with sout-keep ? */
2524 if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
2525 return VLC_EGENERIC;
2527 if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
2528 return VLC_EGENERIC;
2530 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2533 vlm_media_t *p_cfg = &p_media->cfg;
2534 const char *psz_keep;
2537 p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
2541 if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
2544 asprintf( &psz_buffer, "sout=%s%s%s",
2545 p_cfg->psz_output ? p_cfg->psz_output : "",
2546 (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
2547 psz_vod_output ? psz_vod_output : "" );
2548 input_ItemAddOption( &p_instance->item, psz_buffer );
2552 for( i = 0; i < p_cfg->i_option; i++ )
2554 if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
2555 p_instance->b_sout_keep = VLC_TRUE;
2556 else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
2557 p_instance->b_sout_keep = VLC_FALSE;
2559 input_ItemAddOption( &p_instance->item, p_cfg->ppsz_option[i] );
2561 /* We force the right sout-keep value (avoid using the sout-keep from the global configuration)
2562 * FIXME implement input list for VOD (need sout-keep)
2564 if( !p_cfg->b_vod && p_instance->b_sout_keep )
2565 psz_keep = "sout-keep";
2567 psz_keep = "no-sout-keep";
2568 input_ItemAddOption( &p_instance->item, psz_keep );
2570 TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
2573 /* Stop old instance */
2574 if( p_instance->p_input )
2576 if( p_instance->i_index == i_input_index &&
2577 !p_instance->p_input->b_eof && !p_instance->p_input->b_error )
2579 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2580 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2584 input_StopThread( p_instance->p_input );
2585 p_instance->p_sout = input_DetachSout( p_instance->p_input );
2586 vlc_object_release( p_instance->p_input );
2587 if( !p_instance->b_sout_keep && p_instance->p_sout )
2589 sout_DeleteInstance( p_instance->p_sout );
2590 p_instance->p_sout = NULL;
2595 p_instance->i_index = i_input_index;
2596 input_item_SetURI( &p_instance->item, p_media->cfg.ppsz_input[p_instance->i_index] ) ;
2598 asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name );
2599 p_instance->p_input = input_CreateThreadExtended( p_vlm, &p_instance->item, psz_log, p_instance->p_sout );
2600 if( !p_instance->p_input )
2602 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2603 vlm_MediaInstanceDelete( p_instance );
2610 static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
2612 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2613 vlm_media_instance_sys_t *p_instance;
2616 return VLC_EGENERIC;
2618 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2620 return VLC_EGENERIC;
2622 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2624 vlm_MediaInstanceDelete( p_instance );
2628 static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
2630 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2631 vlm_media_instance_sys_t *p_instance;
2635 return VLC_EGENERIC;
2637 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2638 if( !p_instance || !p_instance->p_input )
2639 return VLC_EGENERIC;
2641 /* Toggle pause state */
2642 i_state = var_GetInteger( p_instance->p_input, "state" );
2643 if( i_state == PAUSE_S )
2644 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2645 else if( i_state == PLAYING_S )
2646 var_SetInteger( p_instance->p_input, "state", PAUSE_S );
2649 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
2651 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2652 vlm_media_instance_sys_t *p_instance;
2655 return VLC_EGENERIC;
2657 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2658 if( !p_instance || !p_instance->p_input )
2659 return VLC_EGENERIC;
2662 *pi_time = var_GetTime( p_instance->p_input, "time" );
2664 *pd_position = var_GetFloat( p_instance->p_input, "position" );
2667 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t i_time, double d_position )
2669 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2670 vlm_media_instance_sys_t *p_instance;
2673 return VLC_EGENERIC;
2675 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2676 if( !p_instance || !p_instance->p_input )
2677 return VLC_EGENERIC;
2680 return var_SetTime( p_instance->p_input, "time", i_time );
2681 else if( d_position >= 0 && d_position <= 100 )
2682 return var_SetFloat( p_instance->p_input, "position", d_position );
2683 return VLC_EGENERIC;
2686 static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_instance_t ***ppp_idsc, int *pi_instance )
2688 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2689 vlm_media_instance_t **pp_idsc;
2694 return VLC_EGENERIC;
2696 TAB_INIT( i_idsc, pp_idsc );
2697 for( i = 0; i < p_media->i_instance; i++ )
2699 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
2700 vlm_media_instance_t *p_idsc = vlm_media_instance_New();
2702 if( p_instance->psz_name )
2703 p_idsc->psz_name = strdup( p_instance->psz_name );
2704 if( p_instance->p_input )
2706 p_idsc->i_time = var_GetTime( p_instance->p_input, "time" );
2707 p_idsc->i_length = var_GetTime( p_instance->p_input, "length" );
2708 p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
2709 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2710 p_idsc->b_paused = VLC_TRUE;
2711 p_idsc->i_rate = var_GetInteger( p_instance->p_input, "rate" );
2714 TAB_APPEND( i_idsc, pp_idsc, p_idsc );
2716 *ppp_idsc = pp_idsc;
2717 *pi_instance = i_idsc;
2720 static int vlm_ControlMediaInstanceClear( vlm_t *p_vlm, int64_t id )
2722 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2725 return VLC_EGENERIC;
2727 while( p_media->i_instance > 0 )
2728 vlm_ControlMediaInstanceStop( p_vlm, id, p_media->instance[0]->psz_name );
2733 static int vlm_ControlScheduleClear( vlm_t *p_vlm )
2735 while( p_vlm->i_schedule > 0 )
2736 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0] );
2740 static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
2743 vlm_media_t **pp_dsc;
2744 vlm_media_t ***ppp_dsc;
2745 vlm_media_instance_t ***ppp_idsc;
2747 const char *psz_vod;
2761 case VLM_GET_MEDIAS:
2762 ppp_dsc = (vlm_media_t ***)va_arg( args, vlm_media_t *** );
2763 pi_int = (int *)va_arg( args, int * );
2764 return vlm_ControlMediaGets( p_vlm, ppp_dsc, pi_int );
2766 case VLM_CLEAR_MEDIAS:
2767 return vlm_ControlMediaClear( p_vlm );
2769 case VLM_CHANGE_MEDIA:
2770 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2771 return vlm_ControlMediaChange( p_vlm, p_dsc );
2774 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2775 p_id = (int64_t*)va_arg( args, int64_t * );
2776 return vlm_ControlMediaAdd( p_vlm, p_dsc, p_id );
2779 id = (int64_t)va_arg( args, int64_t );
2780 return vlm_ControlMediaDel( p_vlm, id );
2783 id = (int64_t)va_arg( args, int64_t );
2784 pp_dsc = (vlm_media_t **)va_arg( args, vlm_media_t ** );
2785 return vlm_ControlMediaGet( p_vlm, id, pp_dsc );
2787 case VLM_GET_MEDIA_ID:
2788 psz_id = (const char*)va_arg( args, const char * );
2789 p_id = (int64_t*)va_arg( args, int64_t * );
2790 return vlm_ControlMediaGetId( p_vlm, psz_id, p_id );
2793 /* Media instance control */
2794 case VLM_GET_MEDIA_INSTANCES:
2795 id = (int64_t)va_arg( args, int64_t );
2796 ppp_idsc = (vlm_media_instance_t ***)va_arg( args, vlm_media_instance_t *** );
2797 pi_int = (int *)va_arg( args, int *);
2798 return vlm_ControlMediaInstanceGets( p_vlm, id, ppp_idsc, pi_int );
2800 case VLM_CLEAR_MEDIA_INSTANCES:
2801 id = (int64_t)va_arg( args, int64_t );
2802 return vlm_ControlMediaInstanceClear( p_vlm, id );
2805 case VLM_START_MEDIA_BROADCAST_INSTANCE:
2806 id = (int64_t)va_arg( args, int64_t );
2807 psz_id = (const char*)va_arg( args, const char* );
2808 i_int = (int)va_arg( args, int );
2809 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, NULL );
2811 case VLM_START_MEDIA_VOD_INSTANCE:
2812 id = (int64_t)va_arg( args, int64_t );
2813 psz_id = (const char*)va_arg( args, const char* );
2814 i_int = (int)va_arg( args, int );
2815 psz_vod = (const char*)va_arg( args, const char* );
2817 return VLC_EGENERIC;
2818 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, psz_vod );
2820 case VLM_STOP_MEDIA_INSTANCE:
2821 id = (int64_t)va_arg( args, int64_t );
2822 psz_id = (const char*)va_arg( args, const char* );
2823 return vlm_ControlMediaInstanceStop( p_vlm, id, psz_id );
2825 case VLM_PAUSE_MEDIA_INSTANCE:
2826 id = (int64_t)va_arg( args, int64_t );
2827 psz_id = (const char*)va_arg( args, const char* );
2828 return vlm_ControlMediaInstancePause( p_vlm, id, psz_id );
2830 case VLM_GET_MEDIA_INSTANCE_TIME:
2831 id = (int64_t)va_arg( args, int64_t );
2832 psz_id = (const char*)va_arg( args, const char* );
2833 pi_i64 = (int64_t*)va_arg( args, int64_t * );
2834 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, pi_i64, NULL );
2835 case VLM_GET_MEDIA_INSTANCE_POSITION:
2836 id = (int64_t)va_arg( args, int64_t );
2837 psz_id = (const char*)va_arg( args, const char* );
2838 pd_double = (double*)va_arg( args, double* );
2839 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, NULL, pd_double );
2841 case VLM_SET_MEDIA_INSTANCE_TIME:
2842 id = (int64_t)va_arg( args, int64_t );
2843 psz_id = (const char*)va_arg( args, const char* );
2844 i_i64 = (int64_t)va_arg( args, int64_t);
2845 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, i_i64, -1 );
2846 case VLM_SET_MEDIA_INSTANCE_POSITION:
2847 id = (int64_t)va_arg( args, int64_t );
2848 psz_id = (const char*)va_arg( args, const char* );
2849 d_double = (double)va_arg( args, double );
2850 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, -1, d_double );
2852 case VLM_CLEAR_SCHEDULES:
2853 return vlm_ControlScheduleClear( p_vlm );
2856 msg_Err( p_vlm, "unknown VLM query" );
2857 return VLC_EGENERIC;
2860 static int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... )
2865 va_start( args, i_query );
2866 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2872 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2877 va_start( args, i_query );
2879 vlc_mutex_lock( &p_vlm->lock );
2880 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2881 vlc_mutex_unlock( &p_vlm->lock );
2888 #else /* ENABLE_VLM */
2890 /* We just define an empty wrapper */
2891 vlm_t *__vlm_New( vlc_object_t *a )
2893 msg_Err( a, "VideoLAN manager support is disabled" );
2897 void vlm_Delete( vlm_t *a )
2902 int vlm_ExecuteCommand( vlm_t *a, const char *b, vlm_message_t **c )
2907 vlm_message_t *vlm_MessageNew( const char *psz_name,
2908 const char *psz_format, ... )
2910 (void)psz_name; (void)psz_format;
2914 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
2915 vlm_message_t *p_child )
2920 void vlm_MessageDelete( vlm_message_t *a )
2925 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2927 (void)p_vlm; (void)i_query;
2928 return VLC_EGENERIC;
2931 #endif /* ENABLE_VLM */