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 )
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 );
114 p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE );
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_VLM,
128 vlc_mutex_unlock( lockval.p_address );
132 vlc_mutex_init( p_this->p_libvlc, &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, VLC_FALSE ) )
143 vlc_mutex_destroy( &p_vlm->lock );
144 vlc_object_release( p_vlm );
148 /* Load our configuration file */
149 psz_vlmconf = var_CreateGetString( p_vlm, "vlm-conf" );
150 if( psz_vlmconf && *psz_vlmconf )
152 vlm_message_t *p_message = NULL;
153 char *psz_buffer = NULL;
155 msg_Dbg( p_this, "loading VLM configuration" );
156 asprintf(&psz_buffer, "load %s", psz_vlmconf );
159 msg_Dbg( p_this, psz_buffer );
160 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
161 msg_Warn( p_this, "error while loading the configuration file" );
163 vlm_MessageDelete(p_message);
169 vlc_object_set_destructor( p_vlm, (vlc_destructor_t)vlm_Destructor );
170 vlc_mutex_unlock( lockval.p_address );
175 /*****************************************************************************
177 *****************************************************************************/
178 void vlm_Delete( vlm_t *p_vlm )
180 vlc_object_release( p_vlm );
183 /*****************************************************************************
185 *****************************************************************************/
186 static void vlm_Destructor( vlm_t *p_vlm )
188 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
189 TAB_CLEAN( p_vlm->i_media, p_vlm->media );
191 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
192 TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
194 vlc_mutex_destroy( &p_vlm->lock );
197 /*****************************************************************************
198 * vlm_ExecuteCommand:
199 *****************************************************************************/
200 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
201 vlm_message_t **pp_message)
205 vlc_mutex_lock( &p_vlm->lock );
206 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
207 vlc_mutex_unlock( &p_vlm->lock );
213 static const char quotes[] = "\"'";
215 * FindCommandEnd: look for the end of a possibly quoted string
216 * @return NULL on mal-formatted string,
217 * pointer past the last character otherwise.
219 static const char *FindCommandEnd( const char *psz_sent )
223 while( (c = *psz_sent) != '\0' )
227 if( strchr(quotes,c) ) // opening quote
229 else if( isspace(c) ) // non-escaped space
233 psz_sent++; // skip escaped character
234 if( *psz_sent == '\0' )
240 if( c == quote ) // non-escaped matching quote
242 else if( (quote == '"') && (c == '\\') )
244 psz_sent++; // skip escaped character
245 if (*psz_sent == '\0')
246 return NULL; // error, closing quote missing
252 // error (NULL) if we could not find a matching quote
253 return quote ? NULL : psz_sent;
258 * Unescape a nul-terminated string.
259 * Note that in and out can be identical.
261 * @param out output buffer (at least <strlen (in) + 1> characters long)
262 * @param in nul-terminated string to be unescaped
264 * @return 0 on success, -1 on error.
266 static int Unescape( char *out, const char *in )
270 while( (c = *in++) != '\0' )
274 if (strchr(quotes,c)) // opening quote
298 /* None of the special cases - copy the backslash */
304 if( c == quote ) // non-escaped matching quote
309 if( (quote == '"') && (c == '\\') )
318 case '\0': // should never happen
322 /* None of the special cases - copy the backslash */
334 /*****************************************************************************
335 * ExecuteCommand: The main state machine
336 *****************************************************************************
337 * Execute a command which ends with '\0' (string)
338 *****************************************************************************/
339 static int ExecuteSyntaxError( const char *psz_cmd, vlm_message_t **pp_status )
341 *pp_status = vlm_MessageNew( psz_cmd, "Wrong command syntax" );
345 static vlc_bool_t ExecuteIsMedia( vlm_t *p_vlm, const char *psz_name )
349 if( !psz_name || vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
353 static vlc_bool_t ExecuteIsSchedule( vlm_t *p_vlm, const char *psz_name )
355 if( !psz_name || !vlm_ScheduleSearch( p_vlm, psz_name ) )
360 static int ExecuteDel( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
362 vlm_media_sys_t *p_media;
363 vlm_schedule_sys_t *p_schedule;
365 p_media = vlm_MediaSearch( p_vlm, psz_name );
366 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
368 if( p_schedule != NULL )
370 vlm_ScheduleDelete( p_vlm, p_schedule );
372 else if( p_media != NULL )
374 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_media->cfg.id );
376 else if( !strcmp(psz_name, "media") )
378 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
380 else if( !strcmp(psz_name, "schedule") )
382 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
384 else if( !strcmp(psz_name, "all") )
386 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
387 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
391 *pp_status = vlm_MessageNew( "del", "%s: media unknown", psz_name );
395 *pp_status = vlm_MessageNew( "del", vlm_NULL );
399 static int ExecuteShow( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
401 vlm_media_sys_t *p_media;
402 vlm_schedule_sys_t *p_schedule;
406 *pp_status = vlm_Show( p_vlm, NULL, NULL, NULL );
410 p_media = vlm_MediaSearch( p_vlm, psz_name );
411 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
413 if( p_schedule != NULL )
414 *pp_status = vlm_Show( p_vlm, NULL, p_schedule, NULL );
415 else if( p_media != NULL )
416 *pp_status = vlm_Show( p_vlm, p_media, NULL, NULL );
418 *pp_status = vlm_Show( p_vlm, NULL, NULL, psz_name );
423 static int ExecuteHelp( vlm_message_t *p_status )
425 vlm_message_t *message_child;
427 #define MessageAdd( a ) \
428 vlm_MessageAdd( p_status, vlm_MessageNew( a, vlm_NULL ) );
429 #define MessageAddChild( a ) \
430 vlm_MessageAdd( message_child, vlm_MessageNew( a, vlm_NULL ) );
432 p_status = vlm_MessageNew( "help", vlm_NULL );
434 message_child = MessageAdd( "Commands Syntax:" );
435 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
436 MessageAddChild( "setup (name) (properties)" );
437 MessageAddChild( "show [(name)|media|schedule]" );
438 MessageAddChild( "del (name)|all|media|schedule" );
439 MessageAddChild( "control (name) [instance_name] (command)" );
440 MessageAddChild( "save (config_file)" );
441 MessageAddChild( "export" );
442 MessageAddChild( "load (config_file)" );
444 message_child = MessageAdd( "Media Proprieties Syntax:" );
445 MessageAddChild( "input (input_name)" );
446 MessageAddChild( "inputdel (input_name)|all" );
447 MessageAddChild( "inputdeln input_number" );
448 MessageAddChild( "output (output_name)" );
449 MessageAddChild( "option (option_name)[=value]" );
450 MessageAddChild( "enabled|disabled" );
451 MessageAddChild( "loop|unloop (broadcast only)" );
452 MessageAddChild( "mux (mux_name)" );
454 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
455 MessageAddChild( "enabled|disabled" );
456 MessageAddChild( "append (command_until_rest_of_the_line)" );
457 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
459 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
460 "(days)-(hours):(minutes):(seconds)" );
461 MessageAddChild( "repeat (number_of_repetitions)" );
463 message_child = MessageAdd( "Control Commands Syntax:" );
464 MessageAddChild( "play [input_number]" );
465 MessageAddChild( "pause" );
466 MessageAddChild( "stop" );
467 MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](miliseconds)ms" );
472 static int ExecuteControl( vlm_t *p_vlm, const char *psz_name, const int i_arg, char ** ppsz_arg, vlm_message_t **pp_status )
474 vlm_media_sys_t *p_media;
475 const char *psz_control = NULL;
476 const char *psz_instance = NULL;
477 const char *psz_argument = NULL;
481 if( !ExecuteIsMedia( p_vlm, psz_name ) )
483 *pp_status = vlm_MessageNew( "control", "%s: media unknown", psz_name );
489 #define IS(txt) ( !strcmp( ppsz_arg[i_index], (txt) ) )
491 if( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") )
494 psz_instance = ppsz_arg[0];
496 if( i_index >= i_arg || ( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") ) )
497 return ExecuteSyntaxError( "control", pp_status );
500 psz_control = ppsz_arg[i_index];
502 if( i_index+1 < i_arg )
503 psz_argument = ppsz_arg[i_index+1];
505 p_media = vlm_MediaSearch( p_vlm, psz_name );
508 if( !strcmp( psz_control, "play" ) )
510 int i_input_index = 0;
513 if( ( psz_argument && sscanf(psz_argument, "%d", &i) == 1 ) && i > 0 && i-1 < p_media->cfg.i_input )
517 else if( psz_argument )
520 vlm_media_t *p_cfg = &p_media->cfg;
521 for ( j=0; j < p_cfg->i_input; j++)
523 if( !strcmp( p_cfg->ppsz_input[j], psz_argument ) )
531 if( p_media->cfg.b_vod )
532 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
534 i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, psz_instance, i_input_index );
536 else if( !strcmp( psz_control, "seek" ) )
540 vlc_bool_t b_relative;
541 if( psz_argument[0] == '+' || psz_argument[0] == '-' )
542 b_relative = VLC_TRUE;
544 b_relative = VLC_FALSE;
546 if( strstr( psz_argument, "ms" ) || strstr( psz_argument, "s" ) )
551 if( strstr( psz_argument, "ms" ) )
552 i_new_time = 1000 * (int64_t)atoi( psz_argument );
554 i_new_time = 1000000 * (int64_t)atoi( psz_argument );
559 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, &i_time );
560 i_new_time += i_time;
564 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, i_new_time );
569 double d_new_position = i18n_atof( psz_argument ) / 100.0;
573 double d_position = 0.0;
575 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
576 d_new_position += d_position;
578 if( d_new_position < 0.0 )
579 d_new_position = 0.0;
580 else if( d_new_position > 1.0 )
581 d_new_position = 1.0;
582 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_new_position );
587 i_result = VLC_EGENERIC;
590 else if( !strcmp( psz_control, "rewind" ) )
594 const double d_scale = i18n_atof( psz_argument );
597 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
598 d_position -= (d_scale / 1000.0);
599 if( d_position < 0.0 )
601 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
605 i_result = VLC_EGENERIC;
608 else if( !strcmp( psz_control, "forward" ) )
612 const double d_scale = i18n_atof( psz_argument );
615 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
616 d_position += (d_scale / 1000.0);
617 if( d_position > 1.0 )
619 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
624 i_result = VLC_EGENERIC;
627 else if( !strcmp( psz_control, "stop" ) )
629 i_result = vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
631 else if( !strcmp( psz_control, "pause" ) )
633 i_result = vlm_ControlInternal( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
637 i_result = VLC_EGENERIC;
642 *pp_status = vlm_MessageNew( "control", "unknown error" );
645 *pp_status = vlm_MessageNew( "control", vlm_NULL );
649 static int ExecuteExport( vlm_t *p_vlm, vlm_message_t **pp_status )
651 char *psz_export = Save( p_vlm );
653 *pp_status = vlm_MessageNew( "export", psz_export );
658 static int ExecuteSave( vlm_t *p_vlm, const char *psz_file, vlm_message_t **pp_status )
660 FILE *f = utf8_fopen( psz_file, "wt" );
666 psz_save = Save( p_vlm );
667 if( psz_save == NULL )
672 fwrite( psz_save, strlen( psz_save ), 1, f );
676 *pp_status = vlm_MessageNew( "save", vlm_NULL );
680 *pp_status = vlm_MessageNew( "save", "Unable to save to file" );
684 static int ExecuteLoad( vlm_t *p_vlm, const char *psz_url, vlm_message_t **pp_status )
686 stream_t *p_stream = stream_UrlNew( p_vlm, psz_url );
692 *pp_status = vlm_MessageNew( "load", "Unable to load from file" );
697 if( stream_Seek( p_stream, 0 ) != 0 )
699 stream_Delete( p_stream );
701 *pp_status = vlm_MessageNew( "load", "Read file error" );
705 i_size = stream_Size( p_stream );
707 psz_buffer = malloc( i_size + 1 );
710 stream_Delete( p_stream );
712 *pp_status = vlm_MessageNew( "load", "Read file error" );
716 stream_Read( p_stream, psz_buffer, i_size );
717 psz_buffer[i_size] = '\0';
719 stream_Delete( p_stream );
721 if( Load( p_vlm, psz_buffer ) )
725 *pp_status = vlm_MessageNew( "load", "Error while loading file" );
731 *pp_status = vlm_MessageNew( "load", vlm_NULL );
735 static int ExecuteScheduleProperty( vlm_t *p_vlm, vlm_schedule_sys_t *p_schedule, vlc_bool_t b_new,
736 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
738 const char *psz_cmd = b_new ? "new" : "setup";
741 for( i = 0; i < i_property; i++ )
743 if( !strcmp( ppsz_property[i], "enabled" ) ||
744 !strcmp( ppsz_property[i], "disabled" ) )
746 vlm_ScheduleSetup( p_schedule, ppsz_property[i], NULL );
748 else if( !strcmp( ppsz_property[i], "append" ) )
752 /* Beware: everything behind append is considered as
755 if( ++i >= i_property )
758 psz_line = strdup( ppsz_property[i] );
759 for( j = i+1; j < i_property; j++ )
761 psz_line = realloc( psz_line, strlen(psz_line) + strlen(ppsz_property[j]) + 1 + 1 );
762 strcat( psz_line, " " );
763 strcat( psz_line, ppsz_property[j] );
766 vlm_ScheduleSetup( p_schedule, "append", psz_line );
771 if( i + 1 >= i_property )
774 vlm_ScheduleDelete( p_vlm, p_schedule );
775 return ExecuteSyntaxError( psz_cmd, pp_status );
778 vlm_ScheduleSetup( p_schedule, ppsz_property[i], ppsz_property[i+1] );
782 *pp_status = vlm_MessageNew( psz_cmd, vlm_NULL );
786 static int ExecuteMediaProperty( vlm_t *p_vlm, int64_t id, vlc_bool_t b_new,
787 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
789 const char *psz_cmd = b_new ? "new" : "setup";
790 vlm_media_t *p_cfg = NULL;
796 #define ERROR( txt ) do { *pp_status = vlm_MessageNew( psz_cmd, txt); goto error; } while(0)
797 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA, id, &p_cfg ) )
798 ERROR( "unknown media" );
800 #define MISSING(cmd) do { if( !psz_value ) ERROR( "missing argument for " cmd ); } while(0)
801 for( i = 0; i < i_property; i++ )
803 const char *psz_option = ppsz_property[i];
804 const char *psz_value = i+1 < i_property ? ppsz_property[i+1] : NULL;
806 if( !strcmp( psz_option, "enabled" ) )
808 p_cfg->b_enabled = VLC_TRUE;
810 else if( !strcmp( psz_option, "disabled" ) )
812 p_cfg->b_enabled = VLC_FALSE;
814 else if( !strcmp( psz_option, "input" ) )
817 TAB_APPEND( p_cfg->i_input, p_cfg->ppsz_input, strdup(psz_value) );
820 else if( !strcmp( psz_option, "inputdel" ) && psz_value && !strcmp( psz_value, "all" ) )
822 while( p_cfg->i_input > 0 )
823 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[0] );
826 else if( !strcmp( psz_option, "inputdel" ) )
830 MISSING( "inputdel" );
832 for( j = 0; j < p_cfg->i_input; j++ )
834 if( !strcmp( p_cfg->ppsz_input[j], psz_value ) )
836 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[j] );
842 else if( !strcmp( psz_option, "inputdeln" ) )
846 MISSING( "inputdeln" );
848 i_index = atoi( psz_value );
849 if( i_index > 0 && i_index <= p_cfg->i_input )
850 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[i_index-1] );
853 else if( !strcmp( psz_option, "output" ) )
857 free( p_cfg->psz_output );
858 p_cfg->psz_output = *psz_value ? strdup( psz_value ) : NULL;
861 else if( !strcmp( psz_option, "option" ) )
865 TAB_APPEND( p_cfg->i_option, p_cfg->ppsz_option, strdup( psz_value ) );
868 else if( !strcmp( psz_option, "loop" ) )
871 ERROR( "invalid loop option for vod" );
872 p_cfg->broadcast.b_loop = VLC_TRUE;
874 else if( !strcmp( psz_option, "unloop" ) )
877 ERROR( "invalid unloop option for vod" );
878 p_cfg->broadcast.b_loop = VLC_FALSE;
880 else if( !strcmp( psz_option, "mux" ) )
884 ERROR( "invalid mux option for broadcast" );
886 free( p_cfg->vod.psz_mux );
887 p_cfg->vod.psz_mux = *psz_value ? strdup( psz_value ) : NULL;
892 fprintf( stderr, "PROP: name=%s unknown\n", psz_option );
893 ERROR( "Wrong command syntax" );
900 i_result = vlm_ControlInternal( p_vlm, VLM_CHANGE_MEDIA, p_cfg );
901 vlm_media_Delete( p_cfg );
903 *pp_status = vlm_MessageNew( psz_cmd, vlm_NULL );
910 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_cfg->id );
911 vlm_media_Delete( p_cfg );
916 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 )
919 if( !strcmp( psz_name, "all" ) || !strcmp( psz_name, "media" ) || !strcmp( psz_name, "schedule" ) )
921 *pp_status = vlm_MessageNew( "new", "\"all\", \"media\" and \"schedule\" are reserved names" );
924 if( ExecuteIsMedia( p_vlm, psz_name ) || ExecuteIsSchedule( p_vlm, psz_name ) )
926 *pp_status = vlm_MessageNew( "new", "%s: Name already in use", psz_name );
930 if( !strcmp( psz_type, "schedule" ) )
932 vlm_schedule_sys_t *p_schedule = vlm_ScheduleNew( p_vlm, psz_name );
935 *pp_status = vlm_MessageNew( "new", "could not create schedule" );
938 return ExecuteScheduleProperty( p_vlm, p_schedule, VLC_TRUE, i_property, ppsz_property, pp_status );
940 else if( !strcmp( psz_type, "vod" ) || !strcmp( psz_type, "broadcast" ) )
945 vlm_media_Init( &cfg );
946 cfg.psz_name = strdup( psz_name );
947 cfg.b_vod = !strcmp( psz_type, "vod" );
949 if( vlm_ControlInternal( p_vlm, VLM_ADD_MEDIA, &cfg, &id ) )
951 vlm_media_Clean( &cfg );
952 *pp_status = vlm_MessageNew( "new", "could not create media" );
955 vlm_media_Clean( &cfg );
956 return ExecuteMediaProperty( p_vlm, id, VLC_TRUE, i_property, ppsz_property, pp_status );
960 *pp_status = vlm_MessageNew( "new", "%s: Choose between vod, broadcast or schedule", psz_type );
965 static int ExecuteSetup( vlm_t *p_vlm, const char *psz_name, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
967 if( ExecuteIsSchedule( p_vlm, psz_name ) )
969 vlm_schedule_sys_t *p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
970 return ExecuteScheduleProperty( p_vlm, p_schedule, VLC_FALSE, i_property, ppsz_property, pp_status );
972 else if( ExecuteIsMedia( p_vlm, psz_name ) )
975 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
977 return ExecuteMediaProperty( p_vlm, id, VLC_FALSE, i_property, ppsz_property, pp_status );
981 *pp_status = vlm_MessageNew( "setup", "%s unknown", psz_name );
985 static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
986 vlm_message_t **pp_message )
988 size_t i_command = 0;
989 char buf[strlen (psz_command) + 1], *psz_buf = buf;
990 char *ppsz_command[3+sizeof (buf) / 2];
991 vlm_message_t *p_message = NULL;
993 /* First, parse the line and cut it */
994 while( *psz_command != '\0' )
996 const char *psz_temp;
998 if(isspace (*psz_command))
1004 /* support for comments */
1005 if( i_command == 0 && *psz_command == '#')
1007 p_message = vlm_MessageNew( "", vlm_NULL );
1011 psz_temp = FindCommandEnd( psz_command );
1013 if( psz_temp == NULL )
1015 p_message = vlm_MessageNew( "Incomplete command", psz_command );
1019 assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
1021 ppsz_command[i_command] = psz_buf;
1022 memcpy (psz_buf, psz_command, psz_temp - psz_command);
1023 psz_buf[psz_temp - psz_command] = '\0';
1025 Unescape (psz_buf, psz_buf);
1028 psz_buf += psz_temp - psz_command + 1;
1029 psz_command = psz_temp;
1031 assert (buf + sizeof (buf) >= psz_buf);
1035 * And then Interpret it
1038 #define IF_EXECUTE( name, check, cmd ) if( !strcmp(ppsz_command[0], name ) ) { if( (check) ) goto syntax_error; if( (cmd) ) goto error; goto success; }
1039 if( i_command == 0 )
1041 p_message = vlm_MessageNew( "", vlm_NULL );
1044 else IF_EXECUTE( "del", (i_command != 2), ExecuteDel(p_vlm, ppsz_command[1], &p_message) )
1045 else IF_EXECUTE( "show", (i_command > 2), ExecuteShow(p_vlm, i_command > 1 ? ppsz_command[1] : NULL, &p_message) )
1046 else IF_EXECUTE( "help", (i_command != 1), ExecuteHelp( p_message) )
1047 else IF_EXECUTE( "control", (i_command < 3), ExecuteControl(p_vlm, ppsz_command[1], i_command - 2, &ppsz_command[2], &p_message) )
1048 else IF_EXECUTE( "save", (i_command != 2), ExecuteSave(p_vlm, ppsz_command[1], &p_message) )
1049 else IF_EXECUTE( "export", (i_command != 1), ExecuteExport(p_vlm, &p_message) )
1050 else IF_EXECUTE( "load", (i_command != 2), ExecuteLoad(p_vlm, ppsz_command[1], &p_message) )
1051 else IF_EXECUTE( "new", (i_command < 3), ExecuteNew(p_vlm, ppsz_command[1], ppsz_command[2], i_command-3, &ppsz_command[3], &p_message) )
1052 else IF_EXECUTE( "setup", (i_command < 2), ExecuteSetup(p_vlm, ppsz_command[1], i_command-2, &ppsz_command[2], &p_message) )
1055 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
1061 *pp_message = p_message;
1065 return ExecuteSyntaxError( ppsz_command[0], pp_message );
1068 *pp_message = p_message;
1069 return VLC_EGENERIC;
1072 /*****************************************************************************
1074 *****************************************************************************/
1075 vlm_media_sys_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
1079 for( i = 0; i < vlm->i_media; i++ )
1081 if( strcmp( psz_name, vlm->media[i]->cfg.psz_name ) == 0 )
1082 return vlm->media[i];
1088 /*****************************************************************************
1090 *****************************************************************************/
1091 static int64_t vlm_Date(void)
1096 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1098 struct timeval tv_date;
1100 /* gettimeofday() cannot fail given &tv_date is a valid address */
1101 (void)gettimeofday( &tv_date, NULL );
1102 return (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec;
1106 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
1108 vlm_schedule_sys_t *p_sched = malloc( sizeof( vlm_schedule_sys_t ) );
1120 p_sched->psz_name = strdup( psz_name );
1121 p_sched->b_enabled = VLC_FALSE;
1122 p_sched->i_command = 0;
1123 p_sched->command = NULL;
1124 p_sched->i_date = 0;
1125 p_sched->i_period = 0;
1126 p_sched->i_repeat = -1;
1128 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1133 /* for now, simple delete. After, del with options (last arg) */
1134 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched )
1136 if( sched == NULL ) return;
1138 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1140 if( vlm->i_schedule == 0 ) free( vlm->schedule );
1141 free( sched->psz_name );
1142 while( sched->i_command )
1144 char *psz_cmd = sched->command[0];
1145 TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1151 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1155 for( i = 0; i < vlm->i_schedule; i++ )
1157 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1159 return vlm->schedule[i];
1166 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1167 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
1168 const char *psz_value )
1170 if( !strcmp( psz_cmd, "enabled" ) )
1172 schedule->b_enabled = VLC_TRUE;
1174 else if( !strcmp( psz_cmd, "disabled" ) )
1176 schedule->b_enabled = VLC_FALSE;
1178 #if !defined( UNDER_CE )
1179 else if( !strcmp( psz_cmd, "date" ) )
1185 time.tm_sec = 0; /* seconds */
1186 time.tm_min = 0; /* minutes */
1187 time.tm_hour = 0; /* hours */
1188 time.tm_mday = 0; /* day of the month */
1189 time.tm_mon = 0; /* month */
1190 time.tm_year = 0; /* year */
1191 time.tm_wday = 0; /* day of the week */
1192 time.tm_yday = 0; /* day in the year */
1193 time.tm_isdst = -1; /* daylight saving time */
1195 /* date should be year/month/day-hour:minutes:seconds */
1196 p = strchr( psz_value, '-' );
1198 if( !strcmp( psz_value, "now" ) )
1200 schedule->i_date = 0;
1202 else if( (p == NULL) && sscanf( psz_value, "%d:%d:%d", &time.tm_hour,
1203 &time.tm_min, &time.tm_sec ) != 3 )
1204 /* it must be a hour:minutes:seconds */
1212 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1230 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1236 time.tm_mon = i - 1;
1240 time.tm_year = i - 1900;
1241 time.tm_mon = j - 1;
1248 date = mktime( &time );
1249 schedule->i_date = ((mtime_t) date) * 1000000;
1252 else if( !strcmp( psz_cmd, "period" ) )
1256 const char *psz_time = NULL, *psz_date = NULL;
1260 /* First, if date or period are modified, repeat should be equal to -1 */
1261 schedule->i_repeat = -1;
1263 time.tm_sec = 0; /* seconds */
1264 time.tm_min = 0; /* minutes */
1265 time.tm_hour = 0; /* hours */
1266 time.tm_mday = 0; /* day of the month */
1267 time.tm_mon = 0; /* month */
1268 time.tm_year = 0; /* year */
1269 time.tm_wday = 0; /* day of the week */
1270 time.tm_yday = 0; /* day in the year */
1271 time.tm_isdst = -1; /* daylight saving time */
1273 /* date should be year/month/day-hour:minutes:seconds */
1274 p = strchr( psz_value, '-' );
1277 psz_date = psz_value;
1282 psz_time = psz_value;
1285 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1304 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1323 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1324 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1325 schedule->i_period = ((mtime_t) date) * 1000000;
1327 #endif /* UNDER_CE */
1328 else if( !strcmp( psz_cmd, "repeat" ) )
1332 if( sscanf( psz_value, "%d", &i ) == 1 )
1334 schedule->i_repeat = i;
1341 else if( !strcmp( psz_cmd, "append" ) )
1343 char *command = strdup( psz_value );
1345 TAB_APPEND( schedule->i_command, schedule->command, command );
1354 /*****************************************************************************
1355 * Message handling functions
1356 *****************************************************************************/
1357 vlm_message_t *vlm_MessageNew( const char *psz_name,
1358 const char *psz_format, ... )
1360 vlm_message_t *p_message;
1363 if( !psz_name ) return NULL;
1365 p_message = malloc( sizeof(vlm_message_t) );
1371 p_message->psz_value = 0;
1375 va_start( args, psz_format );
1376 if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1385 p_message->psz_name = strdup( psz_name );
1386 p_message->i_child = 0;
1387 p_message->child = NULL;
1392 void vlm_MessageDelete( vlm_message_t *p_message )
1394 free( p_message->psz_name );
1395 free( p_message->psz_value );
1396 while( p_message->i_child-- )
1397 vlm_MessageDelete( p_message->child[p_message->i_child] );
1398 free( p_message->child );
1403 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1404 vlm_message_t *p_child )
1406 if( p_message == NULL ) return NULL;
1410 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1416 /*****************************************************************************
1417 * Misc utility functions
1418 *****************************************************************************/
1419 static vlm_message_t *vlm_ShowMedia( vlm_media_sys_t *p_media )
1421 vlm_media_t *p_cfg = &p_media->cfg;
1422 vlm_message_t *p_msg;
1423 vlm_message_t *p_msg_sub;
1426 p_msg = vlm_MessageNew( p_cfg->psz_name, vlm_NULL );
1427 vlm_MessageAdd( p_msg,
1428 vlm_MessageNew( "type", p_cfg->b_vod ? "vod" : "broadcast" ) );
1429 vlm_MessageAdd( p_msg,
1430 vlm_MessageNew( "enabled", p_cfg->b_enabled ? "yes" : "no" ) );
1433 vlm_MessageAdd( p_msg,
1434 vlm_MessageNew( "mux", p_cfg->vod.psz_mux ) );
1436 vlm_MessageAdd( p_msg,
1437 vlm_MessageNew( "loop", p_cfg->broadcast.b_loop ? "yes" : "no" ) );
1439 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "inputs", vlm_NULL ) );
1440 for( i = 0; i < p_cfg->i_input; i++ )
1443 asprintf( &psz_tmp, "%d", i+1 );
1444 vlm_MessageAdd( p_msg_sub,
1445 vlm_MessageNew( psz_tmp, p_cfg->ppsz_input[i] ) );
1449 vlm_MessageAdd( p_msg,
1450 vlm_MessageNew( "output", p_cfg->psz_output ? p_cfg->psz_output : "" ) );
1452 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "options", vlm_NULL ) );
1453 for( i = 0; i < p_cfg->i_option; i++ )
1454 vlm_MessageAdd( p_msg_sub, vlm_MessageNew( p_cfg->ppsz_option[i], vlm_NULL ) );
1456 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "instances", vlm_NULL ) );
1457 for( i = 0; i < p_media->i_instance; i++ )
1459 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1461 vlm_message_t *p_msg_instance;
1465 if( p_instance->p_input )
1466 var_Get( p_instance->p_input, "state", &val );
1468 p_msg_instance = vlm_MessageAdd( p_msg_sub, vlm_MessageNew( "instance" , vlm_NULL ) );
1470 vlm_MessageAdd( p_msg_instance,
1471 vlm_MessageNew( "name" , p_instance->psz_name ? p_instance->psz_name : "default" ) );
1472 vlm_MessageAdd( p_msg_instance,
1473 vlm_MessageNew( "state",
1474 val.i_int == PLAYING_S ? "playing" :
1475 val.i_int == PAUSE_S ? "paused" :
1478 /* FIXME should not do that this way */
1479 if( p_instance->p_input )
1481 #define APPEND_INPUT_INFO( a, format, type ) \
1482 asprintf( &psz_tmp, format, \
1483 var_Get ## type( p_instance->p_input, a ) ); \
1484 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( a, psz_tmp ) ); \
1486 APPEND_INPUT_INFO( "position", "%f", Float );
1487 APPEND_INPUT_INFO( "time", I64Fi, Time );
1488 APPEND_INPUT_INFO( "length", I64Fi, Time );
1489 APPEND_INPUT_INFO( "rate", "%d", Integer );
1490 APPEND_INPUT_INFO( "title", "%d", Integer );
1491 APPEND_INPUT_INFO( "chapter", "%d", Integer );
1492 APPEND_INPUT_INFO( "seekable", "%d", Bool );
1494 #undef APPEND_INPUT_INFO
1495 asprintf( &psz_tmp, "%d", p_instance->i_index + 1 );
1496 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( "playlistindex", psz_tmp ) );
1502 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
1503 vlm_schedule_sys_t *schedule,
1504 const char *psz_filter )
1508 vlm_message_t *p_msg = vlm_MessageNew( "show", vlm_NULL );
1510 vlm_MessageAdd( p_msg, vlm_ShowMedia( media ) );
1514 else if( schedule != NULL )
1518 vlm_message_t *msg_schedule;
1519 vlm_message_t *msg_child;
1522 msg = vlm_MessageNew( "show", vlm_NULL );
1524 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, vlm_NULL ) );
1526 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1528 vlm_MessageAdd( msg_schedule,
1529 vlm_MessageNew( "enabled", schedule->b_enabled ?
1532 #if !defined( UNDER_CE )
1533 if( schedule->i_date != 0 )
1536 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1539 localtime_r( &i_time, &date);
1540 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1541 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1542 date.tm_hour, date.tm_min, date.tm_sec );
1544 vlm_MessageAdd( msg_schedule,
1545 vlm_MessageNew( "date", psz_date ) );
1550 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1553 if( schedule->i_period != 0 )
1555 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1558 date.tm_sec = (int)( i_time % 60 );
1559 i_time = i_time / 60;
1560 date.tm_min = (int)( i_time % 60 );
1561 i_time = i_time / 60;
1562 date.tm_hour = (int)( i_time % 24 );
1563 i_time = i_time / 24;
1564 date.tm_mday = (int)( i_time % 30 );
1565 i_time = i_time / 30;
1566 /* okay, okay, months are not always 30 days long */
1567 date.tm_mon = (int)( i_time % 12 );
1568 i_time = i_time / 12;
1569 date.tm_year = (int)i_time;
1571 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1572 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1574 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1578 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1580 #endif /* UNDER_CE */
1582 sprintf( buffer, "%d", schedule->i_repeat );
1583 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1586 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", vlm_NULL ) );
1588 for( i = 0; i < schedule->i_command; i++ )
1590 vlm_MessageAdd( msg_child,
1591 vlm_MessageNew( schedule->command[i], vlm_NULL ) );
1598 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1600 vlm_message_t *p_msg;
1601 vlm_message_t *p_msg_child;
1602 int i_vod = 0, i_broadcast = 0;
1606 for( i = 0; i < vlm->i_media; i++ )
1608 if( vlm->media[i]->cfg.b_vod )
1614 asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1616 p_msg = vlm_MessageNew( "show", vlm_NULL );
1617 p_msg_child = vlm_MessageAdd( p_msg, vlm_MessageNew( "media", psz_count ) );
1620 for( i = 0; i < vlm->i_media; i++ )
1621 vlm_MessageAdd( p_msg_child, vlm_ShowMedia( vlm->media[i] ) );
1626 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1630 vlm_message_t *msg_child;
1632 msg = vlm_MessageNew( "show", vlm_NULL );
1633 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", vlm_NULL ) );
1635 for( i = 0; i < vlm->i_schedule; i++ )
1637 vlm_schedule_sys_t *s = vlm->schedule[i];
1638 vlm_message_t *msg_schedule;
1639 mtime_t i_time, i_next_date;
1641 msg_schedule = vlm_MessageAdd( msg_child,
1642 vlm_MessageNew( s->psz_name, vlm_NULL ) );
1643 vlm_MessageAdd( msg_schedule,
1644 vlm_MessageNew( "enabled", s->b_enabled ?
1647 /* calculate next date */
1648 i_time = vlm_Date();
1649 i_next_date = s->i_date;
1651 if( s->i_period != 0 )
1654 while( s->i_date + j * s->i_period <= i_time &&
1660 i_next_date = s->i_date + j * s->i_period;
1663 if( i_next_date > i_time )
1665 time_t i_date = (time_t) (i_next_date / 1000000) ;
1667 #if !defined( UNDER_CE )
1670 ctime_r( &i_date, psz_date );
1672 char *psz_date = ctime( &i_date );
1675 vlm_MessageAdd( msg_schedule,
1676 vlm_MessageNew( "next launch", psz_date ) );
1684 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
1686 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1687 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1689 vlm_MessageAdd( show1, show2->child[0] );
1691 /* We must destroy the parent node "show" of show2
1692 * and not the children */
1693 free( show2->psz_name );
1701 return vlm_MessageNew( "show", vlm_NULL );
1705 /*****************************************************************************
1706 * Config handling functions
1707 *****************************************************************************/
1708 static int Load( vlm_t *vlm, char *file )
1713 while( *pf != '\0' )
1715 vlm_message_t *message = NULL;
1718 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1723 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1727 if( pf[i_end] == '\n' ) i_end++;
1730 if( *pf && ExecuteCommand( vlm, pf, &message ) )
1734 if( message->psz_value )
1735 msg_Err( vlm, "Load error on line %d: %s: %s",
1736 i_line, message->psz_name, message->psz_value );
1737 vlm_MessageDelete( message );
1741 if( message ) vlm_MessageDelete( message );
1750 static char *Save( vlm_t *vlm )
1753 char psz_header[] = "\n"
1754 "# VLC media player VLM command batch\n"
1755 "# http://www.videolan.org/vlc/\n\n" ;
1758 int i_length = strlen( psz_header );
1760 for( i = 0; i < vlm->i_media; i++ )
1762 vlm_media_sys_t *media = vlm->media[i];
1763 vlm_media_t *p_cfg = &media->cfg;
1766 i_length += strlen( "new * vod " ) + strlen(p_cfg->psz_name);
1768 i_length += strlen( "new * broadcast " ) + strlen(p_cfg->psz_name);
1770 if( p_cfg->b_enabled == VLC_TRUE )
1771 i_length += strlen( "enabled" );
1773 i_length += strlen( "disabled" );
1775 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop == VLC_TRUE )
1776 i_length += strlen( " loop\n" );
1778 i_length += strlen( "\n" );
1780 for( j = 0; j < p_cfg->i_input; j++ )
1781 i_length += strlen( "setup * input \"\"\n" ) + strlen( p_cfg->psz_name ) + strlen( p_cfg->ppsz_input[j] );
1783 if( p_cfg->psz_output != NULL )
1784 i_length += strlen( "setup * output \n" ) + strlen(p_cfg->psz_name) + strlen(p_cfg->psz_output);
1786 for( j = 0; j < p_cfg->i_option; j++ )
1787 i_length += strlen("setup * option \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->ppsz_option[j]);
1789 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1790 i_length += strlen("setup * mux \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->vod.psz_mux);
1793 for( i = 0; i < vlm->i_schedule; i++ )
1795 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1797 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
1799 if( schedule->b_enabled == VLC_TRUE )
1801 i_length += strlen( "date //-:: enabled\n" ) + 14;
1805 i_length += strlen( "date //-:: disabled\n" ) + 14;
1809 if( schedule->i_period != 0 )
1811 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
1812 strlen( "period //-::\n" ) + 14;
1815 if( schedule->i_repeat >= 0 )
1819 sprintf( buffer, "%d", schedule->i_repeat );
1820 i_length += strlen( "setup repeat \n" ) +
1821 strlen( schedule->psz_name ) + strlen( buffer );
1828 for( j = 0; j < schedule->i_command; j++ )
1830 i_length += strlen( "setup append \n" ) +
1831 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
1836 /* Don't forget the '\0' */
1838 /* now we have the length of save */
1840 p = save = malloc( i_length );
1841 if( !save ) return NULL;
1844 p += sprintf( p, "%s", psz_header );
1846 /* finally we can write in it */
1847 for( i = 0; i < vlm->i_media; i++ )
1849 vlm_media_sys_t *media = vlm->media[i];
1850 vlm_media_t *p_cfg = &media->cfg;
1853 p += sprintf( p, "new %s vod ", p_cfg->psz_name );
1855 p += sprintf( p, "new %s broadcast ", p_cfg->psz_name );
1857 if( p_cfg->b_enabled )
1858 p += sprintf( p, "enabled" );
1860 p += sprintf( p, "disabled" );
1862 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1863 p += sprintf( p, " loop\n" );
1865 p += sprintf( p, "\n" );
1867 for( j = 0; j < p_cfg->i_input; j++ )
1868 p += sprintf( p, "setup %s input \"%s\"\n", p_cfg->psz_name, p_cfg->ppsz_input[j] );
1870 if( p_cfg->psz_output )
1871 p += sprintf( p, "setup %s output %s\n", p_cfg->psz_name, p_cfg->psz_output );
1873 for( j = 0; j < p_cfg->i_option; j++ )
1874 p += sprintf( p, "setup %s option %s\n", p_cfg->psz_name, p_cfg->ppsz_option[j] );
1876 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1877 p += sprintf( p, "setup %s mux %s\n", p_cfg->psz_name, p_cfg->vod.psz_mux );
1880 /* and now, the schedule scripts */
1881 #if !defined( UNDER_CE )
1882 for( i = 0; i < vlm->i_schedule; i++ )
1884 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1886 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
1888 localtime_r( &i_time, &date);
1889 p += sprintf( p, "new %s schedule ", schedule->psz_name);
1891 if( schedule->b_enabled == VLC_TRUE )
1893 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
1894 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1895 date.tm_hour, date.tm_min, date.tm_sec );
1899 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
1900 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1901 date.tm_hour, date.tm_min, date.tm_sec);
1904 if( schedule->i_period != 0 )
1906 p += sprintf( p, "setup %s ", schedule->psz_name );
1908 i_time = (time_t) ( schedule->i_period / 1000000 );
1910 date.tm_sec = (int)( i_time % 60 );
1911 i_time = i_time / 60;
1912 date.tm_min = (int)( i_time % 60 );
1913 i_time = i_time / 60;
1914 date.tm_hour = (int)( i_time % 24 );
1915 i_time = i_time / 24;
1916 date.tm_mday = (int)( i_time % 30 );
1917 i_time = i_time / 30;
1918 /* okay, okay, months are not always 30 days long */
1919 date.tm_mon = (int)( i_time % 12 );
1920 i_time = i_time / 12;
1921 date.tm_year = (int)i_time;
1923 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
1924 date.tm_year, date.tm_mon, date.tm_mday,
1925 date.tm_hour, date.tm_min, date.tm_sec);
1928 if( schedule->i_repeat >= 0 )
1930 p += sprintf( p, "setup %s repeat %d\n",
1931 schedule->psz_name, schedule->i_repeat );
1935 p += sprintf( p, "\n" );
1938 for( j = 0; j < schedule->i_command; j++ )
1940 p += sprintf( p, "setup %s append %s\n",
1941 schedule->psz_name, schedule->command[j] );
1945 #endif /* UNDER_CE */
1950 /*****************************************************************************
1952 *****************************************************************************/
1953 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
1954 const char *psz_id, int i_query, va_list args )
1956 vlm_t *vlm = (vlm_t *)p_private;
1961 if( !p_private || !p_vod_media )
1962 return VLC_EGENERIC;
1964 vlc_mutex_lock( &vlm->lock );
1967 for( i = 0, id = -1; i < vlm->i_media; i++ )
1969 if( p_vod_media == vlm->media[i]->vod.p_media )
1971 id = vlm->media[i]->cfg.id;
1977 vlc_mutex_unlock( &vlm->lock );
1978 return VLC_EGENERIC;
1983 case VOD_MEDIA_PLAY:
1984 psz = (const char *)va_arg( args, const char * );
1985 i_ret = vlm_ControlInternal( vlm, VLM_START_MEDIA_VOD_INSTANCE, id, psz_id, 0, psz );
1988 case VOD_MEDIA_PAUSE:
1989 i_ret = vlm_ControlInternal( vlm, VLM_PAUSE_MEDIA_INSTANCE, id, psz_id );
1992 case VOD_MEDIA_STOP:
1993 i_ret = vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, id, psz_id );
1996 case VOD_MEDIA_SEEK:
1998 double d_position = (double)va_arg( args, double );
1999 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position/100.0 );
2003 case VOD_MEDIA_REWIND:
2005 double d_scale = (double)va_arg( args, double );
2008 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2009 d_position -= (d_scale / 1000.0);
2010 if( d_position < 0.0 )
2012 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2016 case VOD_MEDIA_FORWARD:
2018 double d_scale = (double)va_arg( args, double );
2021 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2022 d_position += (d_scale / 1000.0);
2023 if( d_position > 1.0 )
2025 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2030 i_ret = VLC_EGENERIC;
2034 vlc_mutex_unlock( &vlm->lock );
2040 /*****************************************************************************
2042 *****************************************************************************/
2043 static int Manage( vlc_object_t* p_object )
2045 vlm_t *vlm = (vlm_t*)p_object;
2047 mtime_t i_lastcheck;
2050 i_lastcheck = vlm_Date();
2052 while( !vlm->b_die )
2054 char **ppsz_scheduled_commands = NULL;
2055 int i_scheduled_commands = 0;
2057 vlc_mutex_lock( &vlm->lock );
2059 /* destroy the inputs that wants to die, and launch the next input */
2060 for( i = 0; i < vlm->i_media; i++ )
2062 vlm_media_sys_t *p_media = vlm->media[i];
2064 for( j = 0; j < p_media->i_instance; )
2066 vlm_media_instance_sys_t *p_instance = p_media->instance[j];
2068 if( p_instance->p_input && ( p_instance->p_input->b_eof || p_instance->p_input->b_error ) )
2070 int i_new_input_index;
2073 i_new_input_index = p_instance->i_index + 1;
2074 if( !p_media->cfg.b_vod && p_media->cfg.broadcast.b_loop && i_new_input_index >= p_media->cfg.i_input )
2075 i_new_input_index = 0;
2077 /* FIXME implement multiple input with VOD */
2078 if( p_media->cfg.b_vod || i_new_input_index >= p_media->cfg.i_input )
2079 vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, p_instance->psz_name );
2081 vlm_ControlInternal( vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, p_instance->psz_name, i_new_input_index );
2093 i_time = vlm_Date();
2095 for( i = 0; i < vlm->i_schedule; i++ )
2097 mtime_t i_real_date = vlm->schedule[i]->i_date;
2099 if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2101 if( vlm->schedule[i]->i_date == 0 ) // now !
2103 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2104 i_real_date = i_time;
2106 else if( vlm->schedule[i]->i_period != 0 )
2109 while( vlm->schedule[i]->i_date + j *
2110 vlm->schedule[i]->i_period <= i_lastcheck &&
2111 ( vlm->schedule[i]->i_repeat > j ||
2112 vlm->schedule[i]->i_repeat == -1 ) )
2117 i_real_date = vlm->schedule[i]->i_date + j *
2118 vlm->schedule[i]->i_period;
2121 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2123 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2125 TAB_APPEND( i_scheduled_commands,
2126 ppsz_scheduled_commands,
2127 strdup(vlm->schedule[i]->command[j] ) );
2132 while( i_scheduled_commands )
2134 vlm_message_t *message = NULL;
2135 char *psz_command = ppsz_scheduled_commands[0];
2136 ExecuteCommand( vlm, psz_command,&message );
2138 /* for now, drop the message */
2139 vlm_MessageDelete( message );
2140 TAB_REMOVE( i_scheduled_commands,
2141 ppsz_scheduled_commands,
2143 free( psz_command );
2146 i_lastcheck = i_time;
2148 vlc_mutex_unlock( &vlm->lock );
2163 int i_connection_count;
2164 int i_connection_active;
2169 vlc_bool_t b_playing;
2170 int i_playing_index;
2173 } vlm_media_status_t;
2177 static vlm_media_sys_t *vlm_ControlMediaGetById( vlm_t *p_vlm, int64_t id )
2181 for( i = 0; i < p_vlm->i_media; i++ )
2183 if( p_vlm->media[i]->cfg.id == id )
2184 return p_vlm->media[i];
2188 static vlm_media_sys_t *vlm_ControlMediaGetByName( vlm_t *p_vlm, const char *psz_name )
2192 for( i = 0; i < p_vlm->i_media; i++ )
2194 if( !strcmp( p_vlm->media[i]->cfg.psz_name, psz_name ) )
2195 return p_vlm->media[i];
2199 static int vlm_MediaDescriptionCheck( vlm_t *p_vlm, vlm_media_t *p_cfg )
2203 if( !p_cfg || !p_cfg->psz_name ||
2204 !strcmp( p_cfg->psz_name, "all" ) || !strcmp( p_cfg->psz_name, "media" ) || !strcmp( p_cfg->psz_name, "schedule" ) )
2205 return VLC_EGENERIC;
2207 for( i = 0; i < p_vlm->i_media; i++ )
2209 if( p_vlm->media[i]->cfg.id == p_cfg->id )
2211 if( !strcmp( p_vlm->media[i]->cfg.psz_name, p_cfg->psz_name ) )
2212 return VLC_EGENERIC;
2218 /* Called after a media description is changed/added */
2219 static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
2221 vlm_media_t *p_cfg = &p_media->cfg;
2222 /* Check if we need to create/delete a vod media */
2225 if( !p_cfg->b_enabled && p_media->vod.p_media )
2227 p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
2228 p_media->vod.p_media = NULL;
2230 else if( p_cfg->b_enabled && !p_media->vod.p_media && p_cfg->i_input )
2232 /* Pre-parse the input */
2233 input_thread_t *p_input;
2239 vlc_gc_decref( p_media->vod.p_item );
2240 p_media->vod.p_item = input_ItemNew( p_vlm, p_cfg->ppsz_input[0],
2243 if( p_cfg->psz_output )
2244 asprintf( &psz_output, "%s:description", p_cfg->psz_output );
2246 asprintf( &psz_output, "#description" );
2248 asprintf( &psz_dup, "sout=%s", psz_output);
2249 input_ItemAddOption( p_media->vod.p_item, psz_dup );
2251 for( i = 0; i < p_cfg->i_option; i++ )
2252 input_ItemAddOption( p_media->vod.p_item,
2253 p_cfg->ppsz_option[i] );
2254 input_ItemAddOption( p_media->vod.p_item, "no-sout-keep" );
2256 asprintf( &psz_header, _("Media: %s"), p_cfg->psz_name );
2258 if( (p_input = input_CreateThreadExtended( p_vlm, p_media->vod.p_item, psz_header, NULL ) ) )
2260 while( !p_input->b_eof && !p_input->b_error )
2263 input_StopThread( p_input );
2264 vlc_object_release( p_input );
2269 if( p_cfg->vod.psz_mux )
2272 es_format_t es, *p_es = &es;
2275 sprintf( fourcc, "%4.4s", p_cfg->vod.psz_mux );
2276 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
2277 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
2279 /* XXX: Don't do it that way, but properly use a new input item ref. */
2280 item = *p_media->vod.p_item;
2283 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
2285 p_media->vod.p_media =
2286 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
2290 p_media->vod.p_media =
2291 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, p_media->vod.p_item );
2297 /* TODO start media if needed */
2300 /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
2304 static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
2306 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
2309 if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
2310 return VLC_EGENERIC;
2311 if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
2312 return VLC_EGENERIC;
2316 /* TODO check what are the changes being done (stop instance if needed) */
2319 vlm_media_Clean( &p_media->cfg );
2320 vlm_media_Copy( &p_media->cfg, p_cfg );
2322 return vlm_OnMediaUpdate( p_vlm, p_media );
2325 static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id )
2327 vlm_media_sys_t *p_media;
2329 if( vlm_MediaDescriptionCheck( p_vlm, p_cfg ) || vlm_ControlMediaGetByName( p_vlm, p_cfg->psz_name ) )
2331 msg_Err( p_vlm, "invalid media description" );
2332 return VLC_EGENERIC;
2334 /* Check if we need to load the VOD server */
2335 if( p_cfg->b_vod && !p_vlm->i_vod )
2337 p_vlm->p_vod = vlc_custom_create( VLC_OBJECT(p_vlm), sizeof( vod_t ),
2338 VLC_OBJECT_GENERIC, "vod server" );
2339 vlc_object_attach( p_vlm->p_vod, p_vlm );
2340 p_vlm->p_vod->p_module = module_Need( p_vlm->p_vod, "vod server", 0, 0 );
2341 if( !p_vlm->p_vod->p_module )
2343 msg_Err( p_vlm, "cannot find vod server" );
2344 vlc_object_detach( p_vlm->p_vod );
2345 vlc_object_release( p_vlm->p_vod );
2347 return VLC_EGENERIC;
2350 p_vlm->p_vod->p_data = p_vlm;
2351 p_vlm->p_vod->pf_media_control = vlm_MediaVodControl;
2354 p_media = malloc( sizeof( vlm_media_sys_t ) );
2357 msg_Err( p_vlm, "out of memory" );
2360 memset( p_media, 0, sizeof(vlm_media_sys_t) );
2365 vlm_media_Copy( &p_media->cfg, p_cfg );
2366 p_media->cfg.id = p_vlm->i_id++;
2367 /* FIXME do we do something here if enabled is true ? */
2369 p_media->vod.p_item = input_ItemNew( p_vlm, NULL, NULL );
2371 p_media->vod.p_media = NULL;
2372 TAB_INIT( p_media->i_instance, p_media->instance );
2375 TAB_APPEND( p_vlm->i_media, p_vlm->media, p_media );
2378 *p_id = p_media->cfg.id;
2380 return vlm_OnMediaUpdate( p_vlm, p_media );
2383 static int vlm_ControlMediaDel( vlm_t *p_vlm, int64_t id )
2385 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2388 return VLC_EGENERIC;
2390 while( p_media->i_instance > 0 )
2391 vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, p_media->instance[0]->psz_name );
2393 if( p_media->cfg.b_vod )
2395 p_media->cfg.b_enabled = VLC_FALSE;
2396 vlm_OnMediaUpdate( p_vlm, p_media );
2400 vlm_media_Clean( &p_media->cfg );
2402 vlc_gc_decref( p_media->vod.p_item );
2404 TAB_REMOVE( p_vlm->i_media, p_vlm->media, p_media );
2408 /* Check if we need to unload the VOD server */
2409 if( p_vlm->p_vod && p_vlm->i_vod <= 0 )
2411 module_Unneed( p_vlm->p_vod, p_vlm->p_vod->p_module );
2412 vlc_object_detach( p_vlm->p_vod );
2413 vlc_object_release( p_vlm->p_vod );
2414 p_vlm->p_vod = NULL;
2419 static int vlm_ControlMediaGets( vlm_t *p_vlm, vlm_media_t ***ppp_dsc, int *pi_dsc )
2421 vlm_media_t **pp_dsc;
2425 TAB_INIT( i_dsc, pp_dsc );
2426 for( i = 0; i < p_vlm->i_media; i++ )
2428 vlm_media_t *p_dsc = vlm_media_Duplicate( &p_vlm->media[i]->cfg );
2429 TAB_APPEND( i_dsc, pp_dsc, p_dsc );
2437 static int vlm_ControlMediaClear( vlm_t *p_vlm )
2439 while( p_vlm->i_media > 0 )
2440 vlm_ControlMediaDel( p_vlm, p_vlm->media[0]->cfg.id );
2444 static int vlm_ControlMediaGet( vlm_t *p_vlm, int64_t id, vlm_media_t **pp_dsc )
2446 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2448 return VLC_EGENERIC;
2450 *pp_dsc = vlm_media_Duplicate( &p_media->cfg );
2453 static int vlm_ControlMediaGetId( vlm_t *p_vlm, const char *psz_name, int64_t *p_id )
2455 vlm_media_sys_t *p_media = vlm_ControlMediaGetByName( p_vlm, psz_name );
2457 return VLC_EGENERIC;
2459 *p_id = p_media->cfg.id;
2463 static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
2467 for( i = 0; i < p_media->i_instance; i++ )
2469 const char *psz = p_media->instance[i]->psz_name;
2470 if( ( psz == NULL && psz_id == NULL ) ||
2471 ( psz && psz_id && !strcmp( psz, psz_id ) ) )
2472 return p_media->instance[i];
2476 static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
2478 vlm_media_instance_sys_t *p_instance = malloc( sizeof(vlm_media_instance_sys_t) );
2482 memset( p_instance, 0, sizeof(vlm_media_instance_sys_t) );
2484 p_instance->psz_name = NULL;
2486 p_instance->psz_name = strdup( psz_name );
2488 p_instance->p_item = input_ItemNew( p_vlm, NULL, NULL );
2490 p_instance->i_index = 0;
2491 p_instance->b_sout_keep = VLC_FALSE;
2492 p_instance->p_input = NULL;
2493 p_instance->p_sout = NULL;
2497 static void vlm_MediaInstanceDelete( vlm_media_instance_sys_t *p_instance )
2499 if( p_instance->p_input )
2501 input_StopThread( p_instance->p_input );
2502 p_instance->p_sout = input_DetachSout( p_instance->p_input );
2503 vlc_object_release( p_instance->p_input );
2505 if( p_instance->p_sout )
2506 sout_DeleteInstance( p_instance->p_sout );
2508 vlc_gc_decref( p_instance->p_item );
2509 free( p_instance->psz_name );
2514 static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
2516 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2517 vlm_media_instance_sys_t *p_instance;
2520 if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
2521 return VLC_EGENERIC;
2523 /* TODO support multiple input for VOD with sout-keep ? */
2525 if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
2526 return VLC_EGENERIC;
2528 if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
2529 return VLC_EGENERIC;
2531 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2534 vlm_media_t *p_cfg = &p_media->cfg;
2535 const char *psz_keep;
2538 p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
2542 if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
2545 asprintf( &psz_buffer, "sout=%s%s%s",
2546 p_cfg->psz_output ? p_cfg->psz_output : "",
2547 (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
2548 psz_vod_output ? psz_vod_output : "" );
2549 input_ItemAddOption( p_instance->p_item, psz_buffer );
2553 for( i = 0; i < p_cfg->i_option; i++ )
2555 if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
2556 p_instance->b_sout_keep = VLC_TRUE;
2557 else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
2558 p_instance->b_sout_keep = VLC_FALSE;
2560 input_ItemAddOption( p_instance->p_item, p_cfg->ppsz_option[i] );
2562 /* We force the right sout-keep value (avoid using the sout-keep from the global configuration)
2563 * FIXME implement input list for VOD (need sout-keep)
2565 if( !p_cfg->b_vod && p_instance->b_sout_keep )
2566 psz_keep = "sout-keep";
2568 psz_keep = "no-sout-keep";
2569 input_ItemAddOption( p_instance->p_item, psz_keep );
2571 TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
2574 /* Stop old instance */
2575 if( p_instance->p_input )
2577 if( p_instance->i_index == i_input_index &&
2578 !p_instance->p_input->b_eof && !p_instance->p_input->b_error )
2580 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2581 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2585 input_StopThread( p_instance->p_input );
2586 p_instance->p_sout = input_DetachSout( p_instance->p_input );
2587 vlc_object_release( p_instance->p_input );
2588 if( !p_instance->b_sout_keep && p_instance->p_sout )
2590 sout_DeleteInstance( p_instance->p_sout );
2591 p_instance->p_sout = NULL;
2596 p_instance->i_index = i_input_index;
2597 input_item_SetURI( p_instance->p_item, p_media->cfg.ppsz_input[p_instance->i_index] ) ;
2599 asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name );
2600 p_instance->p_input = input_CreateThreadExtended( p_vlm, p_instance->p_item, psz_log, p_instance->p_sout );
2601 if( !p_instance->p_input )
2603 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2604 vlm_MediaInstanceDelete( p_instance );
2611 static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
2613 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2614 vlm_media_instance_sys_t *p_instance;
2617 return VLC_EGENERIC;
2619 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2621 return VLC_EGENERIC;
2623 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2625 vlm_MediaInstanceDelete( p_instance );
2629 static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
2631 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2632 vlm_media_instance_sys_t *p_instance;
2636 return VLC_EGENERIC;
2638 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2639 if( !p_instance || !p_instance->p_input )
2640 return VLC_EGENERIC;
2642 /* Toggle pause state */
2643 i_state = var_GetInteger( p_instance->p_input, "state" );
2644 if( i_state == PAUSE_S )
2645 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2646 else if( i_state == PLAYING_S )
2647 var_SetInteger( p_instance->p_input, "state", PAUSE_S );
2650 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
2652 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2653 vlm_media_instance_sys_t *p_instance;
2656 return VLC_EGENERIC;
2658 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2659 if( !p_instance || !p_instance->p_input )
2660 return VLC_EGENERIC;
2663 *pi_time = var_GetTime( p_instance->p_input, "time" );
2665 *pd_position = var_GetFloat( p_instance->p_input, "position" );
2668 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t i_time, double d_position )
2670 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2671 vlm_media_instance_sys_t *p_instance;
2674 return VLC_EGENERIC;
2676 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2677 if( !p_instance || !p_instance->p_input )
2678 return VLC_EGENERIC;
2681 return var_SetTime( p_instance->p_input, "time", i_time );
2682 else if( d_position >= 0 && d_position <= 100 )
2683 return var_SetFloat( p_instance->p_input, "position", d_position );
2684 return VLC_EGENERIC;
2687 static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_instance_t ***ppp_idsc, int *pi_instance )
2689 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2690 vlm_media_instance_t **pp_idsc;
2695 return VLC_EGENERIC;
2697 TAB_INIT( i_idsc, pp_idsc );
2698 for( i = 0; i < p_media->i_instance; i++ )
2700 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
2701 vlm_media_instance_t *p_idsc = vlm_media_instance_New();
2703 if( p_instance->psz_name )
2704 p_idsc->psz_name = strdup( p_instance->psz_name );
2705 if( p_instance->p_input )
2707 p_idsc->i_time = var_GetTime( p_instance->p_input, "time" );
2708 p_idsc->i_length = var_GetTime( p_instance->p_input, "length" );
2709 p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
2710 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2711 p_idsc->b_paused = VLC_TRUE;
2712 p_idsc->i_rate = var_GetInteger( p_instance->p_input, "rate" );
2715 TAB_APPEND( i_idsc, pp_idsc, p_idsc );
2717 *ppp_idsc = pp_idsc;
2718 *pi_instance = i_idsc;
2721 static int vlm_ControlMediaInstanceClear( vlm_t *p_vlm, int64_t id )
2723 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2726 return VLC_EGENERIC;
2728 while( p_media->i_instance > 0 )
2729 vlm_ControlMediaInstanceStop( p_vlm, id, p_media->instance[0]->psz_name );
2734 static int vlm_ControlScheduleClear( vlm_t *p_vlm )
2736 while( p_vlm->i_schedule > 0 )
2737 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0] );
2741 static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
2744 vlm_media_t **pp_dsc;
2745 vlm_media_t ***ppp_dsc;
2746 vlm_media_instance_t ***ppp_idsc;
2748 const char *psz_vod;
2762 case VLM_GET_MEDIAS:
2763 ppp_dsc = (vlm_media_t ***)va_arg( args, vlm_media_t *** );
2764 pi_int = (int *)va_arg( args, int * );
2765 return vlm_ControlMediaGets( p_vlm, ppp_dsc, pi_int );
2767 case VLM_CLEAR_MEDIAS:
2768 return vlm_ControlMediaClear( p_vlm );
2770 case VLM_CHANGE_MEDIA:
2771 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2772 return vlm_ControlMediaChange( p_vlm, p_dsc );
2775 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2776 p_id = (int64_t*)va_arg( args, int64_t * );
2777 return vlm_ControlMediaAdd( p_vlm, p_dsc, p_id );
2780 id = (int64_t)va_arg( args, int64_t );
2781 return vlm_ControlMediaDel( p_vlm, id );
2784 id = (int64_t)va_arg( args, int64_t );
2785 pp_dsc = (vlm_media_t **)va_arg( args, vlm_media_t ** );
2786 return vlm_ControlMediaGet( p_vlm, id, pp_dsc );
2788 case VLM_GET_MEDIA_ID:
2789 psz_id = (const char*)va_arg( args, const char * );
2790 p_id = (int64_t*)va_arg( args, int64_t * );
2791 return vlm_ControlMediaGetId( p_vlm, psz_id, p_id );
2794 /* Media instance control */
2795 case VLM_GET_MEDIA_INSTANCES:
2796 id = (int64_t)va_arg( args, int64_t );
2797 ppp_idsc = (vlm_media_instance_t ***)va_arg( args, vlm_media_instance_t *** );
2798 pi_int = (int *)va_arg( args, int *);
2799 return vlm_ControlMediaInstanceGets( p_vlm, id, ppp_idsc, pi_int );
2801 case VLM_CLEAR_MEDIA_INSTANCES:
2802 id = (int64_t)va_arg( args, int64_t );
2803 return vlm_ControlMediaInstanceClear( p_vlm, id );
2806 case VLM_START_MEDIA_BROADCAST_INSTANCE:
2807 id = (int64_t)va_arg( args, int64_t );
2808 psz_id = (const char*)va_arg( args, const char* );
2809 i_int = (int)va_arg( args, int );
2810 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, NULL );
2812 case VLM_START_MEDIA_VOD_INSTANCE:
2813 id = (int64_t)va_arg( args, int64_t );
2814 psz_id = (const char*)va_arg( args, const char* );
2815 i_int = (int)va_arg( args, int );
2816 psz_vod = (const char*)va_arg( args, const char* );
2818 return VLC_EGENERIC;
2819 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, psz_vod );
2821 case VLM_STOP_MEDIA_INSTANCE:
2822 id = (int64_t)va_arg( args, int64_t );
2823 psz_id = (const char*)va_arg( args, const char* );
2824 return vlm_ControlMediaInstanceStop( p_vlm, id, psz_id );
2826 case VLM_PAUSE_MEDIA_INSTANCE:
2827 id = (int64_t)va_arg( args, int64_t );
2828 psz_id = (const char*)va_arg( args, const char* );
2829 return vlm_ControlMediaInstancePause( p_vlm, id, psz_id );
2831 case VLM_GET_MEDIA_INSTANCE_TIME:
2832 id = (int64_t)va_arg( args, int64_t );
2833 psz_id = (const char*)va_arg( args, const char* );
2834 pi_i64 = (int64_t*)va_arg( args, int64_t * );
2835 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, pi_i64, NULL );
2836 case VLM_GET_MEDIA_INSTANCE_POSITION:
2837 id = (int64_t)va_arg( args, int64_t );
2838 psz_id = (const char*)va_arg( args, const char* );
2839 pd_double = (double*)va_arg( args, double* );
2840 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, NULL, pd_double );
2842 case VLM_SET_MEDIA_INSTANCE_TIME:
2843 id = (int64_t)va_arg( args, int64_t );
2844 psz_id = (const char*)va_arg( args, const char* );
2845 i_i64 = (int64_t)va_arg( args, int64_t);
2846 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, i_i64, -1 );
2847 case VLM_SET_MEDIA_INSTANCE_POSITION:
2848 id = (int64_t)va_arg( args, int64_t );
2849 psz_id = (const char*)va_arg( args, const char* );
2850 d_double = (double)va_arg( args, double );
2851 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, -1, d_double );
2853 case VLM_CLEAR_SCHEDULES:
2854 return vlm_ControlScheduleClear( p_vlm );
2857 msg_Err( p_vlm, "unknown VLM query" );
2858 return VLC_EGENERIC;
2861 static int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... )
2866 va_start( args, i_query );
2867 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2873 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2878 va_start( args, i_query );
2880 vlc_mutex_lock( &p_vlm->lock );
2881 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2882 vlc_mutex_unlock( &p_vlm->lock );
2889 #else /* ENABLE_VLM */
2891 /* We just define an empty wrapper */
2892 vlm_t *__vlm_New( vlc_object_t *a )
2894 msg_Err( a, "VideoLAN manager support is disabled" );
2898 void vlm_Delete( vlm_t *a )
2903 int vlm_ExecuteCommand( vlm_t *a, const char *b, vlm_message_t **c )
2908 vlm_message_t *vlm_MessageNew( const char *psz_name,
2909 const char *psz_format, ... )
2911 (void)psz_name; (void)psz_format;
2915 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
2916 vlm_message_t *p_child )
2921 void vlm_MessageDelete( vlm_message_t *a )
2926 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2928 (void)p_vlm; (void)i_query;
2929 return VLC_EGENERIC;
2932 #endif /* ENABLE_VLM */