1 /*****************************************************************************
2 * vlm.c: VLM interface plugin
3 *****************************************************************************
4 * Copyright (C) 2000-2005 the VideoLAN team
7 * Authors: Simon Latapie <garf@videolan.org>
8 * Laurent Aimar <fenrir@videolan.org>
9 * Gildas Bazin <gbazin@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
36 #include <ctype.h> /* tolower() */
44 # include <sys/time.h> /* gettimeofday() */
48 # include <time.h> /* ctime() */
49 # include <sys/timeb.h> /* ftime() */
52 #include <vlc_input.h>
53 #include "input_internal.h"
54 #include <vlc_stream.h>
55 #include "vlm_internal.h"
57 #include <vlc_charset.h>
59 #include "../stream_output/stream_output.h"
60 #include "../libvlc.h"
62 /*****************************************************************************
64 *****************************************************************************/
66 /* ugly kludge to avoid "null format string" warnings,
67 * even if we handle NULL format string in vlm_MessageNew() */
68 static const char *vlm_NULL = NULL;
70 static void vlm_Destructor( vlm_t *p_vlm );
73 static int vlm_ControlInternal( vlm_t *, int, ... );
76 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_sys_t *, vlm_schedule_sys_t *, const char * );
78 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *, const char * );
80 static char *Save( vlm_t * );
81 static int Load( vlm_t *, char * );
83 static int ExecuteCommand( vlm_t *, const char *, vlm_message_t ** );
85 static int Manage( vlc_object_t * );
87 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name );
88 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched );
89 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
90 const char *psz_value );
92 static int vlm_MediaVodControl( void *, vod_media_t *, const char *, int, va_list );
95 static vlm_media_sys_t *vlm_MediaSearch( vlm_t *, const char *);
97 /*****************************************************************************
99 *****************************************************************************/
100 vlm_t *__vlm_New ( vlc_object_t *p_this )
103 vlm_t *p_vlm = NULL, **pp_vlm = &(libvlc_priv (p_this->p_libvlc)->p_vlm);
105 static const char vlm_object_name[] = "vlm daemon";
107 /* Avoid multiple creation */
108 if( var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX ) ||
109 var_Get( p_this->p_libvlc, "vlm_mutex", &lockval ) )
112 vlc_mutex_lock( lockval.p_address );
116 { /* VLM already exists */
117 vlc_object_yield( p_vlm );
118 vlc_mutex_unlock( lockval.p_address );
122 msg_Dbg( p_this, "creating VLM" );
124 p_vlm = vlc_custom_create( p_this, sizeof( *p_vlm ), VLC_OBJECT_GENERIC,
128 vlc_mutex_unlock( lockval.p_address );
132 vlc_mutex_init( &p_vlm->lock );
134 TAB_INIT( p_vlm->i_media, p_vlm->media );
135 TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
138 vlc_object_attach( p_vlm, p_this->p_libvlc );
140 if( vlc_thread_create( p_vlm, "vlm thread",
141 Manage, VLC_THREAD_PRIORITY_LOW, false ) )
143 vlc_mutex_destroy( &p_vlm->lock );
144 vlc_object_release( p_vlm );
148 /* Load our configuration file */
149 psz_vlmconf = var_CreateGetString( p_vlm, "vlm-conf" );
150 if( psz_vlmconf && *psz_vlmconf )
152 vlm_message_t *p_message = NULL;
153 char *psz_buffer = NULL;
155 msg_Dbg( p_this, "loading VLM configuration" );
156 asprintf(&psz_buffer, "load %s", psz_vlmconf );
159 msg_Dbg( p_this, psz_buffer );
160 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
161 msg_Warn( p_this, "error while loading the configuration file" );
163 vlm_MessageDelete(p_message);
169 vlc_object_set_destructor( p_vlm, (vlc_destructor_t)vlm_Destructor );
170 *pp_vlm = p_vlm; /* for future reference */
171 vlc_mutex_unlock( lockval.p_address );
176 /*****************************************************************************
178 *****************************************************************************/
179 void vlm_Delete( vlm_t *p_vlm )
181 vlc_object_release( p_vlm );
184 /*****************************************************************************
186 *****************************************************************************/
187 static void vlm_Destructor( vlm_t *p_vlm )
189 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
190 TAB_CLEAN( p_vlm->i_media, p_vlm->media );
192 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
193 TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
195 vlc_mutex_destroy( &p_vlm->lock );
198 /*****************************************************************************
199 * vlm_ExecuteCommand:
200 *****************************************************************************/
201 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
202 vlm_message_t **pp_message)
206 vlc_mutex_lock( &p_vlm->lock );
207 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
208 vlc_mutex_unlock( &p_vlm->lock );
214 static const char quotes[] = "\"'";
216 * FindCommandEnd: look for the end of a possibly quoted string
217 * @return NULL on mal-formatted string,
218 * pointer past the last character otherwise.
220 static const char *FindCommandEnd( const char *psz_sent )
224 while( (c = *psz_sent) != '\0' )
228 if( strchr(quotes,c) ) // opening quote
230 else if( isspace(c) ) // non-escaped space
234 psz_sent++; // skip escaped character
235 if( *psz_sent == '\0' )
241 if( c == quote ) // non-escaped matching quote
243 else if( (quote == '"') && (c == '\\') )
245 psz_sent++; // skip escaped character
246 if (*psz_sent == '\0')
247 return NULL; // error, closing quote missing
253 // error (NULL) if we could not find a matching quote
254 return quote ? NULL : psz_sent;
259 * Unescape a nul-terminated string.
260 * Note that in and out can be identical.
262 * @param out output buffer (at least <strlen (in) + 1> characters long)
263 * @param in nul-terminated string to be unescaped
265 * @return 0 on success, -1 on error.
267 static int Unescape( char *out, const char *in )
271 while( (c = *in++) != '\0' )
275 if (strchr(quotes,c)) // opening quote
299 /* None of the special cases - copy the backslash */
305 if( c == quote ) // non-escaped matching quote
310 if( (quote == '"') && (c == '\\') )
319 case '\0': // should never happen
323 /* None of the special cases - copy the backslash */
335 /*****************************************************************************
336 * ExecuteCommand: The main state machine
337 *****************************************************************************
338 * Execute a command which ends with '\0' (string)
339 *****************************************************************************/
340 static int ExecuteSyntaxError( const char *psz_cmd, vlm_message_t **pp_status )
342 *pp_status = vlm_MessageNew( psz_cmd, "Wrong command syntax" );
346 static bool ExecuteIsMedia( vlm_t *p_vlm, const char *psz_name )
350 if( !psz_name || vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
354 static bool ExecuteIsSchedule( vlm_t *p_vlm, const char *psz_name )
356 if( !psz_name || !vlm_ScheduleSearch( p_vlm, psz_name ) )
361 static int ExecuteDel( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
363 vlm_media_sys_t *p_media;
364 vlm_schedule_sys_t *p_schedule;
366 p_media = vlm_MediaSearch( p_vlm, psz_name );
367 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
369 if( p_schedule != NULL )
371 vlm_ScheduleDelete( p_vlm, p_schedule );
373 else if( p_media != NULL )
375 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_media->cfg.id );
377 else if( !strcmp(psz_name, "media") )
379 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
381 else if( !strcmp(psz_name, "schedule") )
383 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
385 else if( !strcmp(psz_name, "all") )
387 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
388 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
392 *pp_status = vlm_MessageNew( "del", "%s: media unknown", psz_name );
396 *pp_status = vlm_MessageNew( "del", vlm_NULL );
400 static int ExecuteShow( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
402 vlm_media_sys_t *p_media;
403 vlm_schedule_sys_t *p_schedule;
407 *pp_status = vlm_Show( p_vlm, NULL, NULL, NULL );
411 p_media = vlm_MediaSearch( p_vlm, psz_name );
412 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
414 if( p_schedule != NULL )
415 *pp_status = vlm_Show( p_vlm, NULL, p_schedule, NULL );
416 else if( p_media != NULL )
417 *pp_status = vlm_Show( p_vlm, p_media, NULL, NULL );
419 *pp_status = vlm_Show( p_vlm, NULL, NULL, psz_name );
424 static int ExecuteHelp( vlm_message_t **pp_status )
426 vlm_message_t *message_child;
428 #define MessageAdd( a ) \
429 vlm_MessageAdd( *pp_status, vlm_MessageNew( a, vlm_NULL ) );
430 #define MessageAddChild( a ) \
431 vlm_MessageAdd( message_child, vlm_MessageNew( a, vlm_NULL ) );
433 *pp_status = vlm_MessageNew( "help", vlm_NULL );
435 message_child = MessageAdd( "Commands Syntax:" );
436 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
437 MessageAddChild( "setup (name) (properties)" );
438 MessageAddChild( "show [(name)|media|schedule]" );
439 MessageAddChild( "del (name)|all|media|schedule" );
440 MessageAddChild( "control (name) [instance_name] (command)" );
441 MessageAddChild( "save (config_file)" );
442 MessageAddChild( "export" );
443 MessageAddChild( "load (config_file)" );
445 message_child = MessageAdd( "Media Proprieties Syntax:" );
446 MessageAddChild( "input (input_name)" );
447 MessageAddChild( "inputdel (input_name)|all" );
448 MessageAddChild( "inputdeln input_number" );
449 MessageAddChild( "output (output_name)" );
450 MessageAddChild( "option (option_name)[=value]" );
451 MessageAddChild( "enabled|disabled" );
452 MessageAddChild( "loop|unloop (broadcast only)" );
453 MessageAddChild( "mux (mux_name)" );
455 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
456 MessageAddChild( "enabled|disabled" );
457 MessageAddChild( "append (command_until_rest_of_the_line)" );
458 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
460 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
461 "(days)-(hours):(minutes):(seconds)" );
462 MessageAddChild( "repeat (number_of_repetitions)" );
464 message_child = MessageAdd( "Control Commands Syntax:" );
465 MessageAddChild( "play [input_number]" );
466 MessageAddChild( "pause" );
467 MessageAddChild( "stop" );
468 MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](miliseconds)ms" );
473 static int ExecuteControl( vlm_t *p_vlm, const char *psz_name, const int i_arg, char ** ppsz_arg, vlm_message_t **pp_status )
475 vlm_media_sys_t *p_media;
476 const char *psz_control = NULL;
477 const char *psz_instance = NULL;
478 const char *psz_argument = NULL;
482 if( !ExecuteIsMedia( p_vlm, psz_name ) )
484 *pp_status = vlm_MessageNew( "control", "%s: media unknown", psz_name );
490 #define IS(txt) ( !strcmp( ppsz_arg[i_index], (txt) ) )
492 if( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") )
495 psz_instance = ppsz_arg[0];
497 if( i_index >= i_arg || ( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") ) )
498 return ExecuteSyntaxError( "control", pp_status );
501 psz_control = ppsz_arg[i_index];
503 if( i_index+1 < i_arg )
504 psz_argument = ppsz_arg[i_index+1];
506 p_media = vlm_MediaSearch( p_vlm, psz_name );
509 if( !strcmp( psz_control, "play" ) )
511 int i_input_index = 0;
514 if( ( psz_argument && sscanf(psz_argument, "%d", &i) == 1 ) && i > 0 && i-1 < p_media->cfg.i_input )
518 else if( psz_argument )
521 vlm_media_t *p_cfg = &p_media->cfg;
522 for ( j=0; j < p_cfg->i_input; j++)
524 if( !strcmp( p_cfg->ppsz_input[j], psz_argument ) )
532 if( p_media->cfg.b_vod )
533 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
535 i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, psz_instance, i_input_index );
537 else if( !strcmp( psz_control, "seek" ) )
542 if( psz_argument[0] == '+' || psz_argument[0] == '-' )
547 if( strstr( psz_argument, "ms" ) || strstr( psz_argument, "s" ) )
552 if( strstr( psz_argument, "ms" ) )
553 i_new_time = 1000 * (int64_t)atoi( psz_argument );
555 i_new_time = 1000000 * (int64_t)atoi( psz_argument );
560 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, &i_time );
561 i_new_time += i_time;
565 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, i_new_time );
570 double d_new_position = i18n_atof( psz_argument ) / 100.0;
574 double d_position = 0.0;
576 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
577 d_new_position += d_position;
579 if( d_new_position < 0.0 )
580 d_new_position = 0.0;
581 else if( d_new_position > 1.0 )
582 d_new_position = 1.0;
583 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_new_position );
588 i_result = VLC_EGENERIC;
591 else if( !strcmp( psz_control, "rewind" ) )
595 const double d_scale = i18n_atof( psz_argument );
598 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
599 d_position -= (d_scale / 1000.0);
600 if( d_position < 0.0 )
602 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
606 i_result = VLC_EGENERIC;
609 else if( !strcmp( psz_control, "forward" ) )
613 const double d_scale = i18n_atof( psz_argument );
616 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
617 d_position += (d_scale / 1000.0);
618 if( d_position > 1.0 )
620 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_position );
625 i_result = VLC_EGENERIC;
628 else if( !strcmp( psz_control, "stop" ) )
630 i_result = vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
632 else if( !strcmp( psz_control, "pause" ) )
634 i_result = vlm_ControlInternal( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
638 i_result = VLC_EGENERIC;
643 *pp_status = vlm_MessageNew( "control", "unknown error" );
646 *pp_status = vlm_MessageNew( "control", vlm_NULL );
650 static int ExecuteExport( vlm_t *p_vlm, vlm_message_t **pp_status )
652 char *psz_export = Save( p_vlm );
654 *pp_status = vlm_MessageNew( "export", psz_export );
659 static int ExecuteSave( vlm_t *p_vlm, const char *psz_file, vlm_message_t **pp_status )
661 FILE *f = utf8_fopen( psz_file, "wt" );
667 psz_save = Save( p_vlm );
668 if( psz_save == NULL )
673 fwrite( psz_save, strlen( psz_save ), 1, f );
677 *pp_status = vlm_MessageNew( "save", vlm_NULL );
681 *pp_status = vlm_MessageNew( "save", "Unable to save to file" );
685 static int ExecuteLoad( vlm_t *p_vlm, const char *psz_url, vlm_message_t **pp_status )
687 stream_t *p_stream = stream_UrlNew( p_vlm, psz_url );
693 *pp_status = vlm_MessageNew( "load", "Unable to load from file" );
698 if( stream_Seek( p_stream, 0 ) != 0 )
700 stream_Delete( p_stream );
702 *pp_status = vlm_MessageNew( "load", "Read file error" );
706 i_size = stream_Size( p_stream );
708 psz_buffer = malloc( i_size + 1 );
711 stream_Delete( p_stream );
713 *pp_status = vlm_MessageNew( "load", "Read file error" );
717 stream_Read( p_stream, psz_buffer, i_size );
718 psz_buffer[i_size] = '\0';
720 stream_Delete( p_stream );
722 if( Load( p_vlm, psz_buffer ) )
726 *pp_status = vlm_MessageNew( "load", "Error while loading file" );
732 *pp_status = vlm_MessageNew( "load", vlm_NULL );
736 static int ExecuteScheduleProperty( vlm_t *p_vlm, vlm_schedule_sys_t *p_schedule, bool b_new,
737 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
739 const char *psz_cmd = b_new ? "new" : "setup";
742 for( i = 0; i < i_property; i++ )
744 if( !strcmp( ppsz_property[i], "enabled" ) ||
745 !strcmp( ppsz_property[i], "disabled" ) )
747 vlm_ScheduleSetup( p_schedule, ppsz_property[i], NULL );
749 else if( !strcmp( ppsz_property[i], "append" ) )
753 /* Beware: everything behind append is considered as
756 if( ++i >= i_property )
759 psz_line = strdup( ppsz_property[i] );
760 for( j = i+1; j < i_property; j++ )
762 psz_line = realloc( psz_line, strlen(psz_line) + strlen(ppsz_property[j]) + 1 + 1 );
763 strcat( psz_line, " " );
764 strcat( psz_line, ppsz_property[j] );
767 vlm_ScheduleSetup( p_schedule, "append", psz_line );
772 if( i + 1 >= i_property )
775 vlm_ScheduleDelete( p_vlm, p_schedule );
776 return ExecuteSyntaxError( psz_cmd, pp_status );
779 vlm_ScheduleSetup( p_schedule, ppsz_property[i], ppsz_property[i+1] );
783 *pp_status = vlm_MessageNew( psz_cmd, vlm_NULL );
787 static int ExecuteMediaProperty( vlm_t *p_vlm, int64_t id, bool b_new,
788 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
790 const char *psz_cmd = b_new ? "new" : "setup";
791 vlm_media_t *p_cfg = NULL;
797 #define ERROR( txt ) do { *pp_status = vlm_MessageNew( psz_cmd, txt); goto error; } while(0)
798 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA, id, &p_cfg ) )
799 ERROR( "unknown media" );
801 #define MISSING(cmd) do { if( !psz_value ) ERROR( "missing argument for " cmd ); } while(0)
802 for( i = 0; i < i_property; i++ )
804 const char *psz_option = ppsz_property[i];
805 const char *psz_value = i+1 < i_property ? ppsz_property[i+1] : NULL;
807 if( !strcmp( psz_option, "enabled" ) )
809 p_cfg->b_enabled = true;
811 else if( !strcmp( psz_option, "disabled" ) )
813 p_cfg->b_enabled = false;
815 else if( !strcmp( psz_option, "input" ) )
818 TAB_APPEND( p_cfg->i_input, p_cfg->ppsz_input, strdup(psz_value) );
821 else if( !strcmp( psz_option, "inputdel" ) && psz_value && !strcmp( psz_value, "all" ) )
823 while( p_cfg->i_input > 0 )
824 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[0] );
827 else if( !strcmp( psz_option, "inputdel" ) )
831 MISSING( "inputdel" );
833 for( j = 0; j < p_cfg->i_input; j++ )
835 if( !strcmp( p_cfg->ppsz_input[j], psz_value ) )
837 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[j] );
843 else if( !strcmp( psz_option, "inputdeln" ) )
847 MISSING( "inputdeln" );
849 i_index = atoi( psz_value );
850 if( i_index > 0 && i_index <= p_cfg->i_input )
851 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[i_index-1] );
854 else if( !strcmp( psz_option, "output" ) )
858 free( p_cfg->psz_output );
859 p_cfg->psz_output = *psz_value ? strdup( psz_value ) : NULL;
862 else if( !strcmp( psz_option, "option" ) )
866 TAB_APPEND( p_cfg->i_option, p_cfg->ppsz_option, strdup( psz_value ) );
869 else if( !strcmp( psz_option, "loop" ) )
872 ERROR( "invalid loop option for vod" );
873 p_cfg->broadcast.b_loop = true;
875 else if( !strcmp( psz_option, "unloop" ) )
878 ERROR( "invalid unloop option for vod" );
879 p_cfg->broadcast.b_loop = false;
881 else if( !strcmp( psz_option, "mux" ) )
885 ERROR( "invalid mux option for broadcast" );
887 free( p_cfg->vod.psz_mux );
888 p_cfg->vod.psz_mux = *psz_value ? strdup( psz_value ) : NULL;
893 fprintf( stderr, "PROP: name=%s unknown\n", psz_option );
894 ERROR( "Wrong command syntax" );
901 i_result = vlm_ControlInternal( p_vlm, VLM_CHANGE_MEDIA, p_cfg );
902 vlm_media_Delete( p_cfg );
904 *pp_status = vlm_MessageNew( psz_cmd, vlm_NULL );
911 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_cfg->id );
912 vlm_media_Delete( p_cfg );
917 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 )
920 if( !strcmp( psz_name, "all" ) || !strcmp( psz_name, "media" ) || !strcmp( psz_name, "schedule" ) )
922 *pp_status = vlm_MessageNew( "new", "\"all\", \"media\" and \"schedule\" are reserved names" );
925 if( ExecuteIsMedia( p_vlm, psz_name ) || ExecuteIsSchedule( p_vlm, psz_name ) )
927 *pp_status = vlm_MessageNew( "new", "%s: Name already in use", psz_name );
931 if( !strcmp( psz_type, "schedule" ) )
933 vlm_schedule_sys_t *p_schedule = vlm_ScheduleNew( p_vlm, psz_name );
936 *pp_status = vlm_MessageNew( "new", "could not create schedule" );
939 return ExecuteScheduleProperty( p_vlm, p_schedule, true, i_property, ppsz_property, pp_status );
941 else if( !strcmp( psz_type, "vod" ) || !strcmp( psz_type, "broadcast" ) )
946 vlm_media_Init( &cfg );
947 cfg.psz_name = strdup( psz_name );
948 cfg.b_vod = !strcmp( psz_type, "vod" );
950 if( vlm_ControlInternal( p_vlm, VLM_ADD_MEDIA, &cfg, &id ) )
952 vlm_media_Clean( &cfg );
953 *pp_status = vlm_MessageNew( "new", "could not create media" );
956 vlm_media_Clean( &cfg );
957 return ExecuteMediaProperty( p_vlm, id, true, i_property, ppsz_property, pp_status );
961 *pp_status = vlm_MessageNew( "new", "%s: Choose between vod, broadcast or schedule", psz_type );
966 static int ExecuteSetup( vlm_t *p_vlm, const char *psz_name, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
968 if( ExecuteIsSchedule( p_vlm, psz_name ) )
970 vlm_schedule_sys_t *p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
971 return ExecuteScheduleProperty( p_vlm, p_schedule, false, i_property, ppsz_property, pp_status );
973 else if( ExecuteIsMedia( p_vlm, psz_name ) )
976 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
978 return ExecuteMediaProperty( p_vlm, id, false, i_property, ppsz_property, pp_status );
982 *pp_status = vlm_MessageNew( "setup", "%s unknown", psz_name );
986 static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
987 vlm_message_t **pp_message )
989 size_t i_command = 0;
990 char buf[strlen (psz_command) + 1], *psz_buf = buf;
991 char *ppsz_command[3+sizeof (buf) / 2];
992 vlm_message_t *p_message = NULL;
994 /* First, parse the line and cut it */
995 while( *psz_command != '\0' )
997 const char *psz_temp;
999 if(isspace (*psz_command))
1005 /* support for comments */
1006 if( i_command == 0 && *psz_command == '#')
1008 p_message = vlm_MessageNew( "", vlm_NULL );
1012 psz_temp = FindCommandEnd( psz_command );
1014 if( psz_temp == NULL )
1016 p_message = vlm_MessageNew( "Incomplete command", psz_command );
1020 assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
1022 ppsz_command[i_command] = psz_buf;
1023 memcpy (psz_buf, psz_command, psz_temp - psz_command);
1024 psz_buf[psz_temp - psz_command] = '\0';
1026 Unescape (psz_buf, psz_buf);
1029 psz_buf += psz_temp - psz_command + 1;
1030 psz_command = psz_temp;
1032 assert (buf + sizeof (buf) >= psz_buf);
1036 * And then Interpret it
1039 #define IF_EXECUTE( name, check, cmd ) if( !strcmp(ppsz_command[0], name ) ) { if( (check) ) goto syntax_error; if( (cmd) ) goto error; goto success; }
1040 if( i_command == 0 )
1042 p_message = vlm_MessageNew( "", vlm_NULL );
1045 else IF_EXECUTE( "del", (i_command != 2), ExecuteDel(p_vlm, ppsz_command[1], &p_message) )
1046 else IF_EXECUTE( "show", (i_command > 2), ExecuteShow(p_vlm, i_command > 1 ? ppsz_command[1] : NULL, &p_message) )
1047 else IF_EXECUTE( "help", (i_command != 1), ExecuteHelp( &p_message ) )
1048 else IF_EXECUTE( "control", (i_command < 3), ExecuteControl(p_vlm, ppsz_command[1], i_command - 2, &ppsz_command[2], &p_message) )
1049 else IF_EXECUTE( "save", (i_command != 2), ExecuteSave(p_vlm, ppsz_command[1], &p_message) )
1050 else IF_EXECUTE( "export", (i_command != 1), ExecuteExport(p_vlm, &p_message) )
1051 else IF_EXECUTE( "load", (i_command != 2), ExecuteLoad(p_vlm, ppsz_command[1], &p_message) )
1052 else IF_EXECUTE( "new", (i_command < 3), ExecuteNew(p_vlm, ppsz_command[1], ppsz_command[2], i_command-3, &ppsz_command[3], &p_message) )
1053 else IF_EXECUTE( "setup", (i_command < 2), ExecuteSetup(p_vlm, ppsz_command[1], i_command-2, &ppsz_command[2], &p_message) )
1056 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
1062 *pp_message = p_message;
1066 return ExecuteSyntaxError( ppsz_command[0], pp_message );
1069 *pp_message = p_message;
1070 return VLC_EGENERIC;
1073 /*****************************************************************************
1075 *****************************************************************************/
1076 vlm_media_sys_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
1080 for( i = 0; i < vlm->i_media; i++ )
1082 if( strcmp( psz_name, vlm->media[i]->cfg.psz_name ) == 0 )
1083 return vlm->media[i];
1089 /*****************************************************************************
1091 *****************************************************************************/
1092 static int64_t vlm_Date(void)
1097 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1099 struct timeval tv_date;
1101 /* gettimeofday() cannot fail given &tv_date is a valid address */
1102 (void)gettimeofday( &tv_date, NULL );
1103 return (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec;
1107 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
1109 vlm_schedule_sys_t *p_sched = malloc( sizeof( vlm_schedule_sys_t ) );
1121 p_sched->psz_name = strdup( psz_name );
1122 p_sched->b_enabled = false;
1123 p_sched->i_command = 0;
1124 p_sched->command = NULL;
1125 p_sched->i_date = 0;
1126 p_sched->i_period = 0;
1127 p_sched->i_repeat = -1;
1129 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1134 /* for now, simple delete. After, del with options (last arg) */
1135 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched )
1137 if( sched == NULL ) return;
1139 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1141 if( vlm->i_schedule == 0 ) free( vlm->schedule );
1142 free( sched->psz_name );
1143 while( sched->i_command )
1145 char *psz_cmd = sched->command[0];
1146 TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1152 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1156 for( i = 0; i < vlm->i_schedule; i++ )
1158 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1160 return vlm->schedule[i];
1167 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1168 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
1169 const char *psz_value )
1171 if( !strcmp( psz_cmd, "enabled" ) )
1173 schedule->b_enabled = true;
1175 else if( !strcmp( psz_cmd, "disabled" ) )
1177 schedule->b_enabled = false;
1179 #if !defined( UNDER_CE )
1180 else if( !strcmp( psz_cmd, "date" ) )
1186 time.tm_sec = 0; /* seconds */
1187 time.tm_min = 0; /* minutes */
1188 time.tm_hour = 0; /* hours */
1189 time.tm_mday = 0; /* day of the month */
1190 time.tm_mon = 0; /* month */
1191 time.tm_year = 0; /* year */
1192 time.tm_wday = 0; /* day of the week */
1193 time.tm_yday = 0; /* day in the year */
1194 time.tm_isdst = -1; /* daylight saving time */
1196 /* date should be year/month/day-hour:minutes:seconds */
1197 p = strchr( psz_value, '-' );
1199 if( !strcmp( psz_value, "now" ) )
1201 schedule->i_date = 0;
1203 else if( (p == NULL) && sscanf( psz_value, "%d:%d:%d", &time.tm_hour,
1204 &time.tm_min, &time.tm_sec ) != 3 )
1205 /* it must be a hour:minutes:seconds */
1213 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1231 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1237 time.tm_mon = i - 1;
1241 time.tm_year = i - 1900;
1242 time.tm_mon = j - 1;
1249 date = mktime( &time );
1250 schedule->i_date = ((mtime_t) date) * 1000000;
1253 else if( !strcmp( psz_cmd, "period" ) )
1257 const char *psz_time = NULL, *psz_date = NULL;
1261 /* First, if date or period are modified, repeat should be equal to -1 */
1262 schedule->i_repeat = -1;
1264 time.tm_sec = 0; /* seconds */
1265 time.tm_min = 0; /* minutes */
1266 time.tm_hour = 0; /* hours */
1267 time.tm_mday = 0; /* day of the month */
1268 time.tm_mon = 0; /* month */
1269 time.tm_year = 0; /* year */
1270 time.tm_wday = 0; /* day of the week */
1271 time.tm_yday = 0; /* day in the year */
1272 time.tm_isdst = -1; /* daylight saving time */
1274 /* date should be year/month/day-hour:minutes:seconds */
1275 p = strchr( psz_value, '-' );
1278 psz_date = psz_value;
1283 psz_time = psz_value;
1286 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1305 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1324 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1325 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1326 schedule->i_period = ((mtime_t) date) * 1000000;
1328 #endif /* UNDER_CE */
1329 else if( !strcmp( psz_cmd, "repeat" ) )
1333 if( sscanf( psz_value, "%d", &i ) == 1 )
1335 schedule->i_repeat = i;
1342 else if( !strcmp( psz_cmd, "append" ) )
1344 char *command = strdup( psz_value );
1346 TAB_APPEND( schedule->i_command, schedule->command, command );
1355 /*****************************************************************************
1356 * Message handling functions
1357 *****************************************************************************/
1358 vlm_message_t *vlm_MessageNew( const char *psz_name,
1359 const char *psz_format, ... )
1361 vlm_message_t *p_message;
1364 if( !psz_name ) return NULL;
1366 p_message = malloc( sizeof(vlm_message_t) );
1372 p_message->psz_value = 0;
1376 va_start( args, psz_format );
1377 if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1386 p_message->psz_name = strdup( psz_name );
1387 p_message->i_child = 0;
1388 p_message->child = NULL;
1393 void vlm_MessageDelete( vlm_message_t *p_message )
1395 free( p_message->psz_name );
1396 free( p_message->psz_value );
1397 while( p_message->i_child-- )
1398 vlm_MessageDelete( p_message->child[p_message->i_child] );
1399 free( p_message->child );
1404 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1405 vlm_message_t *p_child )
1407 if( p_message == NULL ) return NULL;
1411 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1417 /*****************************************************************************
1418 * Misc utility functions
1419 *****************************************************************************/
1420 static vlm_message_t *vlm_ShowMedia( vlm_media_sys_t *p_media )
1422 vlm_media_t *p_cfg = &p_media->cfg;
1423 vlm_message_t *p_msg;
1424 vlm_message_t *p_msg_sub;
1427 p_msg = vlm_MessageNew( p_cfg->psz_name, vlm_NULL );
1428 vlm_MessageAdd( p_msg,
1429 vlm_MessageNew( "type", p_cfg->b_vod ? "vod" : "broadcast" ) );
1430 vlm_MessageAdd( p_msg,
1431 vlm_MessageNew( "enabled", p_cfg->b_enabled ? "yes" : "no" ) );
1434 vlm_MessageAdd( p_msg,
1435 vlm_MessageNew( "mux", p_cfg->vod.psz_mux ) );
1437 vlm_MessageAdd( p_msg,
1438 vlm_MessageNew( "loop", p_cfg->broadcast.b_loop ? "yes" : "no" ) );
1440 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "inputs", vlm_NULL ) );
1441 for( i = 0; i < p_cfg->i_input; i++ )
1444 asprintf( &psz_tmp, "%d", i+1 );
1445 vlm_MessageAdd( p_msg_sub,
1446 vlm_MessageNew( psz_tmp, p_cfg->ppsz_input[i] ) );
1450 vlm_MessageAdd( p_msg,
1451 vlm_MessageNew( "output", p_cfg->psz_output ? p_cfg->psz_output : "" ) );
1453 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "options", vlm_NULL ) );
1454 for( i = 0; i < p_cfg->i_option; i++ )
1455 vlm_MessageAdd( p_msg_sub, vlm_MessageNew( p_cfg->ppsz_option[i], vlm_NULL ) );
1457 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageNew( "instances", vlm_NULL ) );
1458 for( i = 0; i < p_media->i_instance; i++ )
1460 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1462 vlm_message_t *p_msg_instance;
1466 if( p_instance->p_input )
1467 var_Get( p_instance->p_input, "state", &val );
1469 p_msg_instance = vlm_MessageAdd( p_msg_sub, vlm_MessageNew( "instance" , vlm_NULL ) );
1471 vlm_MessageAdd( p_msg_instance,
1472 vlm_MessageNew( "name" , p_instance->psz_name ? p_instance->psz_name : "default" ) );
1473 vlm_MessageAdd( p_msg_instance,
1474 vlm_MessageNew( "state",
1475 val.i_int == PLAYING_S ? "playing" :
1476 val.i_int == PAUSE_S ? "paused" :
1479 /* FIXME should not do that this way */
1480 if( p_instance->p_input )
1482 #define APPEND_INPUT_INFO( a, format, type ) \
1483 asprintf( &psz_tmp, format, \
1484 var_Get ## type( p_instance->p_input, a ) ); \
1485 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( a, psz_tmp ) ); \
1487 APPEND_INPUT_INFO( "position", "%f", Float );
1488 APPEND_INPUT_INFO( "time", "%"PRIi64, Time );
1489 APPEND_INPUT_INFO( "length", "%"PRIi64, Time );
1490 APPEND_INPUT_INFO( "rate", "%d", Integer );
1491 APPEND_INPUT_INFO( "title", "%d", Integer );
1492 APPEND_INPUT_INFO( "chapter", "%d", Integer );
1493 APPEND_INPUT_INFO( "seekable", "%d", Bool );
1495 #undef APPEND_INPUT_INFO
1496 asprintf( &psz_tmp, "%d", p_instance->i_index + 1 );
1497 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( "playlistindex", psz_tmp ) );
1503 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
1504 vlm_schedule_sys_t *schedule,
1505 const char *psz_filter )
1509 vlm_message_t *p_msg = vlm_MessageNew( "show", vlm_NULL );
1511 vlm_MessageAdd( p_msg, vlm_ShowMedia( media ) );
1515 else if( schedule != NULL )
1519 vlm_message_t *msg_schedule;
1520 vlm_message_t *msg_child;
1523 msg = vlm_MessageNew( "show", vlm_NULL );
1525 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, vlm_NULL ) );
1527 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1529 vlm_MessageAdd( msg_schedule,
1530 vlm_MessageNew( "enabled", schedule->b_enabled ?
1533 #if !defined( UNDER_CE )
1534 if( schedule->i_date != 0 )
1537 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1540 localtime_r( &i_time, &date);
1541 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1542 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1543 date.tm_hour, date.tm_min, date.tm_sec );
1545 vlm_MessageAdd( msg_schedule,
1546 vlm_MessageNew( "date", psz_date ) );
1551 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1554 if( schedule->i_period != 0 )
1556 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1559 date.tm_sec = (int)( i_time % 60 );
1560 i_time = i_time / 60;
1561 date.tm_min = (int)( i_time % 60 );
1562 i_time = i_time / 60;
1563 date.tm_hour = (int)( i_time % 24 );
1564 i_time = i_time / 24;
1565 date.tm_mday = (int)( i_time % 30 );
1566 i_time = i_time / 30;
1567 /* okay, okay, months are not always 30 days long */
1568 date.tm_mon = (int)( i_time % 12 );
1569 i_time = i_time / 12;
1570 date.tm_year = (int)i_time;
1572 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1573 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1575 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1579 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1581 #endif /* UNDER_CE */
1583 sprintf( buffer, "%d", schedule->i_repeat );
1584 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1587 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", vlm_NULL ) );
1589 for( i = 0; i < schedule->i_command; i++ )
1591 vlm_MessageAdd( msg_child,
1592 vlm_MessageNew( schedule->command[i], vlm_NULL ) );
1599 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1601 vlm_message_t *p_msg;
1602 vlm_message_t *p_msg_child;
1603 int i_vod = 0, i_broadcast = 0;
1607 for( i = 0; i < vlm->i_media; i++ )
1609 if( vlm->media[i]->cfg.b_vod )
1615 asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1617 p_msg = vlm_MessageNew( "show", vlm_NULL );
1618 p_msg_child = vlm_MessageAdd( p_msg, vlm_MessageNew( "media", psz_count ) );
1621 for( i = 0; i < vlm->i_media; i++ )
1622 vlm_MessageAdd( p_msg_child, vlm_ShowMedia( vlm->media[i] ) );
1627 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1631 vlm_message_t *msg_child;
1633 msg = vlm_MessageNew( "show", vlm_NULL );
1634 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", vlm_NULL ) );
1636 for( i = 0; i < vlm->i_schedule; i++ )
1638 vlm_schedule_sys_t *s = vlm->schedule[i];
1639 vlm_message_t *msg_schedule;
1640 mtime_t i_time, i_next_date;
1642 msg_schedule = vlm_MessageAdd( msg_child,
1643 vlm_MessageNew( s->psz_name, vlm_NULL ) );
1644 vlm_MessageAdd( msg_schedule,
1645 vlm_MessageNew( "enabled", s->b_enabled ?
1648 /* calculate next date */
1649 i_time = vlm_Date();
1650 i_next_date = s->i_date;
1652 if( s->i_period != 0 )
1655 while( s->i_date + j * s->i_period <= i_time &&
1661 i_next_date = s->i_date + j * s->i_period;
1664 if( i_next_date > i_time )
1666 time_t i_date = (time_t) (i_next_date / 1000000) ;
1668 #if !defined( UNDER_CE )
1671 ctime_r( &i_date, psz_date );
1673 char *psz_date = ctime( &i_date );
1676 vlm_MessageAdd( msg_schedule,
1677 vlm_MessageNew( "next launch", psz_date ) );
1685 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
1687 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1688 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1690 vlm_MessageAdd( show1, show2->child[0] );
1692 /* We must destroy the parent node "show" of show2
1693 * and not the children */
1694 free( show2->psz_name );
1702 return vlm_MessageNew( "show", vlm_NULL );
1706 /*****************************************************************************
1707 * Config handling functions
1708 *****************************************************************************/
1709 static int Load( vlm_t *vlm, char *file )
1714 while( *pf != '\0' )
1716 vlm_message_t *message = NULL;
1719 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1724 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1728 if( pf[i_end] == '\n' ) i_end++;
1731 if( *pf && ExecuteCommand( vlm, pf, &message ) )
1735 if( message->psz_value )
1736 msg_Err( vlm, "Load error on line %d: %s: %s",
1737 i_line, message->psz_name, message->psz_value );
1738 vlm_MessageDelete( message );
1742 if( message ) vlm_MessageDelete( message );
1751 static char *Save( vlm_t *vlm )
1754 char psz_header[] = "\n"
1755 "# VLC media player VLM command batch\n"
1756 "# http://www.videolan.org/vlc/\n\n" ;
1759 int i_length = strlen( psz_header );
1761 for( i = 0; i < vlm->i_media; i++ )
1763 vlm_media_sys_t *media = vlm->media[i];
1764 vlm_media_t *p_cfg = &media->cfg;
1767 i_length += strlen( "new * vod " ) + strlen(p_cfg->psz_name);
1769 i_length += strlen( "new * broadcast " ) + strlen(p_cfg->psz_name);
1771 if( p_cfg->b_enabled == true )
1772 i_length += strlen( "enabled" );
1774 i_length += strlen( "disabled" );
1776 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop == true )
1777 i_length += strlen( " loop\n" );
1779 i_length += strlen( "\n" );
1781 for( j = 0; j < p_cfg->i_input; j++ )
1782 i_length += strlen( "setup * input \"\"\n" ) + strlen( p_cfg->psz_name ) + strlen( p_cfg->ppsz_input[j] );
1784 if( p_cfg->psz_output != NULL )
1785 i_length += strlen( "setup * output \n" ) + strlen(p_cfg->psz_name) + strlen(p_cfg->psz_output);
1787 for( j = 0; j < p_cfg->i_option; j++ )
1788 i_length += strlen("setup * option \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->ppsz_option[j]);
1790 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1791 i_length += strlen("setup * mux \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->vod.psz_mux);
1794 for( i = 0; i < vlm->i_schedule; i++ )
1796 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1798 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
1800 if( schedule->b_enabled == true )
1802 i_length += strlen( "date //-:: enabled\n" ) + 14;
1806 i_length += strlen( "date //-:: disabled\n" ) + 14;
1810 if( schedule->i_period != 0 )
1812 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
1813 strlen( "period //-::\n" ) + 14;
1816 if( schedule->i_repeat >= 0 )
1820 sprintf( buffer, "%d", schedule->i_repeat );
1821 i_length += strlen( "setup repeat \n" ) +
1822 strlen( schedule->psz_name ) + strlen( buffer );
1829 for( j = 0; j < schedule->i_command; j++ )
1831 i_length += strlen( "setup append \n" ) +
1832 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
1837 /* Don't forget the '\0' */
1839 /* now we have the length of save */
1841 p = save = malloc( i_length );
1842 if( !save ) return NULL;
1845 p += sprintf( p, "%s", psz_header );
1847 /* finally we can write in it */
1848 for( i = 0; i < vlm->i_media; i++ )
1850 vlm_media_sys_t *media = vlm->media[i];
1851 vlm_media_t *p_cfg = &media->cfg;
1854 p += sprintf( p, "new %s vod ", p_cfg->psz_name );
1856 p += sprintf( p, "new %s broadcast ", p_cfg->psz_name );
1858 if( p_cfg->b_enabled )
1859 p += sprintf( p, "enabled" );
1861 p += sprintf( p, "disabled" );
1863 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1864 p += sprintf( p, " loop\n" );
1866 p += sprintf( p, "\n" );
1868 for( j = 0; j < p_cfg->i_input; j++ )
1869 p += sprintf( p, "setup %s input \"%s\"\n", p_cfg->psz_name, p_cfg->ppsz_input[j] );
1871 if( p_cfg->psz_output )
1872 p += sprintf( p, "setup %s output %s\n", p_cfg->psz_name, p_cfg->psz_output );
1874 for( j = 0; j < p_cfg->i_option; j++ )
1875 p += sprintf( p, "setup %s option %s\n", p_cfg->psz_name, p_cfg->ppsz_option[j] );
1877 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1878 p += sprintf( p, "setup %s mux %s\n", p_cfg->psz_name, p_cfg->vod.psz_mux );
1881 /* and now, the schedule scripts */
1882 #if !defined( UNDER_CE )
1883 for( i = 0; i < vlm->i_schedule; i++ )
1885 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1887 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
1889 localtime_r( &i_time, &date);
1890 p += sprintf( p, "new %s schedule ", schedule->psz_name);
1892 if( schedule->b_enabled == true )
1894 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
1895 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1896 date.tm_hour, date.tm_min, date.tm_sec );
1900 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
1901 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1902 date.tm_hour, date.tm_min, date.tm_sec);
1905 if( schedule->i_period != 0 )
1907 p += sprintf( p, "setup %s ", schedule->psz_name );
1909 i_time = (time_t) ( schedule->i_period / 1000000 );
1911 date.tm_sec = (int)( i_time % 60 );
1912 i_time = i_time / 60;
1913 date.tm_min = (int)( i_time % 60 );
1914 i_time = i_time / 60;
1915 date.tm_hour = (int)( i_time % 24 );
1916 i_time = i_time / 24;
1917 date.tm_mday = (int)( i_time % 30 );
1918 i_time = i_time / 30;
1919 /* okay, okay, months are not always 30 days long */
1920 date.tm_mon = (int)( i_time % 12 );
1921 i_time = i_time / 12;
1922 date.tm_year = (int)i_time;
1924 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
1925 date.tm_year, date.tm_mon, date.tm_mday,
1926 date.tm_hour, date.tm_min, date.tm_sec);
1929 if( schedule->i_repeat >= 0 )
1931 p += sprintf( p, "setup %s repeat %d\n",
1932 schedule->psz_name, schedule->i_repeat );
1936 p += sprintf( p, "\n" );
1939 for( j = 0; j < schedule->i_command; j++ )
1941 p += sprintf( p, "setup %s append %s\n",
1942 schedule->psz_name, schedule->command[j] );
1946 #endif /* UNDER_CE */
1951 /*****************************************************************************
1953 *****************************************************************************/
1954 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
1955 const char *psz_id, int i_query, va_list args )
1957 vlm_t *vlm = (vlm_t *)p_private;
1962 if( !p_private || !p_vod_media )
1963 return VLC_EGENERIC;
1965 vlc_mutex_lock( &vlm->lock );
1968 for( i = 0, id = -1; i < vlm->i_media; i++ )
1970 if( p_vod_media == vlm->media[i]->vod.p_media )
1972 id = vlm->media[i]->cfg.id;
1978 vlc_mutex_unlock( &vlm->lock );
1979 return VLC_EGENERIC;
1984 case VOD_MEDIA_PLAY:
1985 psz = (const char *)va_arg( args, const char * );
1986 i_ret = vlm_ControlInternal( vlm, VLM_START_MEDIA_VOD_INSTANCE, id, psz_id, 0, psz );
1989 case VOD_MEDIA_PAUSE:
1990 i_ret = vlm_ControlInternal( vlm, VLM_PAUSE_MEDIA_INSTANCE, id, psz_id );
1993 case VOD_MEDIA_STOP:
1994 i_ret = vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, id, psz_id );
1997 case VOD_MEDIA_SEEK:
1999 double d_position = (double)va_arg( args, double );
2000 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position/100.0 );
2004 case VOD_MEDIA_REWIND:
2006 double d_scale = (double)va_arg( args, double );
2009 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2010 d_position -= (d_scale / 1000.0);
2011 if( d_position < 0.0 )
2013 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2017 case VOD_MEDIA_FORWARD:
2019 double d_scale = (double)va_arg( args, double );
2022 vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
2023 d_position += (d_scale / 1000.0);
2024 if( d_position > 1.0 )
2026 i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
2031 i_ret = VLC_EGENERIC;
2035 vlc_mutex_unlock( &vlm->lock );
2041 /*****************************************************************************
2043 *****************************************************************************/
2044 static int Manage( vlc_object_t* p_object )
2046 vlm_t *vlm = (vlm_t*)p_object;
2048 mtime_t i_lastcheck;
2051 i_lastcheck = vlm_Date();
2053 while( !vlm->b_die )
2055 char **ppsz_scheduled_commands = NULL;
2056 int i_scheduled_commands = 0;
2058 vlc_mutex_lock( &vlm->lock );
2060 /* destroy the inputs that wants to die, and launch the next input */
2061 for( i = 0; i < vlm->i_media; i++ )
2063 vlm_media_sys_t *p_media = vlm->media[i];
2065 for( j = 0; j < p_media->i_instance; )
2067 vlm_media_instance_sys_t *p_instance = p_media->instance[j];
2069 if( p_instance->p_input && ( p_instance->p_input->b_eof || p_instance->p_input->b_error ) )
2071 int i_new_input_index;
2074 i_new_input_index = p_instance->i_index + 1;
2075 if( !p_media->cfg.b_vod && p_media->cfg.broadcast.b_loop && i_new_input_index >= p_media->cfg.i_input )
2076 i_new_input_index = 0;
2078 /* FIXME implement multiple input with VOD */
2079 if( p_media->cfg.b_vod || i_new_input_index >= p_media->cfg.i_input )
2080 vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, p_instance->psz_name );
2082 vlm_ControlInternal( vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, p_instance->psz_name, i_new_input_index );
2094 i_time = vlm_Date();
2096 for( i = 0; i < vlm->i_schedule; i++ )
2098 mtime_t i_real_date = vlm->schedule[i]->i_date;
2100 if( vlm->schedule[i]->b_enabled == true )
2102 if( vlm->schedule[i]->i_date == 0 ) // now !
2104 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2105 i_real_date = i_time;
2107 else if( vlm->schedule[i]->i_period != 0 )
2110 while( vlm->schedule[i]->i_date + j *
2111 vlm->schedule[i]->i_period <= i_lastcheck &&
2112 ( vlm->schedule[i]->i_repeat > j ||
2113 vlm->schedule[i]->i_repeat == -1 ) )
2118 i_real_date = vlm->schedule[i]->i_date + j *
2119 vlm->schedule[i]->i_period;
2122 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2124 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2126 TAB_APPEND( i_scheduled_commands,
2127 ppsz_scheduled_commands,
2128 strdup(vlm->schedule[i]->command[j] ) );
2133 while( i_scheduled_commands )
2135 vlm_message_t *message = NULL;
2136 char *psz_command = ppsz_scheduled_commands[0];
2137 ExecuteCommand( vlm, psz_command,&message );
2139 /* for now, drop the message */
2140 vlm_MessageDelete( message );
2141 TAB_REMOVE( i_scheduled_commands,
2142 ppsz_scheduled_commands,
2144 free( psz_command );
2147 i_lastcheck = i_time;
2149 vlc_mutex_unlock( &vlm->lock );
2164 int i_connection_count;
2165 int i_connection_active;
2171 int i_playing_index;
2174 } vlm_media_status_t;
2178 static vlm_media_sys_t *vlm_ControlMediaGetById( vlm_t *p_vlm, int64_t id )
2182 for( i = 0; i < p_vlm->i_media; i++ )
2184 if( p_vlm->media[i]->cfg.id == id )
2185 return p_vlm->media[i];
2189 static vlm_media_sys_t *vlm_ControlMediaGetByName( vlm_t *p_vlm, const char *psz_name )
2193 for( i = 0; i < p_vlm->i_media; i++ )
2195 if( !strcmp( p_vlm->media[i]->cfg.psz_name, psz_name ) )
2196 return p_vlm->media[i];
2200 static int vlm_MediaDescriptionCheck( vlm_t *p_vlm, vlm_media_t *p_cfg )
2204 if( !p_cfg || !p_cfg->psz_name ||
2205 !strcmp( p_cfg->psz_name, "all" ) || !strcmp( p_cfg->psz_name, "media" ) || !strcmp( p_cfg->psz_name, "schedule" ) )
2206 return VLC_EGENERIC;
2208 for( i = 0; i < p_vlm->i_media; i++ )
2210 if( p_vlm->media[i]->cfg.id == p_cfg->id )
2212 if( !strcmp( p_vlm->media[i]->cfg.psz_name, p_cfg->psz_name ) )
2213 return VLC_EGENERIC;
2219 /* Called after a media description is changed/added */
2220 static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
2222 vlm_media_t *p_cfg = &p_media->cfg;
2223 /* Check if we need to create/delete a vod media */
2226 if( !p_cfg->b_enabled && p_media->vod.p_media )
2228 p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
2229 p_media->vod.p_media = NULL;
2231 else if( p_cfg->b_enabled && !p_media->vod.p_media && p_cfg->i_input )
2233 /* Pre-parse the input */
2234 input_thread_t *p_input;
2240 vlc_gc_decref( p_media->vod.p_item );
2241 p_media->vod.p_item = input_ItemNew( p_vlm, p_cfg->ppsz_input[0],
2244 if( p_cfg->psz_output )
2245 asprintf( &psz_output, "%s:description", p_cfg->psz_output );
2247 asprintf( &psz_output, "#description" );
2249 asprintf( &psz_dup, "sout=%s", psz_output);
2250 input_ItemAddOption( p_media->vod.p_item, psz_dup );
2252 for( i = 0; i < p_cfg->i_option; i++ )
2253 input_ItemAddOption( p_media->vod.p_item,
2254 p_cfg->ppsz_option[i] );
2255 input_ItemAddOption( p_media->vod.p_item, "no-sout-keep" );
2257 asprintf( &psz_header, _("Media: %s"), p_cfg->psz_name );
2259 if( (p_input = input_CreateThreadExtended( p_vlm, p_media->vod.p_item, psz_header, NULL ) ) )
2261 while( !p_input->b_eof && !p_input->b_error )
2264 input_StopThread( p_input );
2265 vlc_object_release( p_input );
2270 if( p_cfg->vod.psz_mux )
2273 es_format_t es, *p_es = &es;
2276 sprintf( fourcc, "%4.4s", p_cfg->vod.psz_mux );
2277 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
2278 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
2280 /* XXX: Don't do it that way, but properly use a new input item ref. */
2281 item = *p_media->vod.p_item;
2284 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
2286 p_media->vod.p_media =
2287 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
2291 p_media->vod.p_media =
2292 p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, p_media->vod.p_item );
2298 /* TODO start media if needed */
2301 /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
2305 static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
2307 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
2310 if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
2311 return VLC_EGENERIC;
2312 if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
2313 return VLC_EGENERIC;
2317 /* TODO check what are the changes being done (stop instance if needed) */
2320 vlm_media_Clean( &p_media->cfg );
2321 vlm_media_Copy( &p_media->cfg, p_cfg );
2323 return vlm_OnMediaUpdate( p_vlm, p_media );
2326 static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id )
2328 vlm_media_sys_t *p_media;
2330 if( vlm_MediaDescriptionCheck( p_vlm, p_cfg ) || vlm_ControlMediaGetByName( p_vlm, p_cfg->psz_name ) )
2332 msg_Err( p_vlm, "invalid media description" );
2333 return VLC_EGENERIC;
2335 /* Check if we need to load the VOD server */
2336 if( p_cfg->b_vod && !p_vlm->i_vod )
2338 p_vlm->p_vod = vlc_custom_create( VLC_OBJECT(p_vlm), sizeof( vod_t ),
2339 VLC_OBJECT_GENERIC, "vod server" );
2340 vlc_object_attach( p_vlm->p_vod, p_vlm );
2341 p_vlm->p_vod->p_module = module_Need( p_vlm->p_vod, "vod server", 0, 0 );
2342 if( !p_vlm->p_vod->p_module )
2344 msg_Err( p_vlm, "cannot find vod server" );
2345 vlc_object_detach( p_vlm->p_vod );
2346 vlc_object_release( p_vlm->p_vod );
2348 return VLC_EGENERIC;
2351 p_vlm->p_vod->p_data = p_vlm;
2352 p_vlm->p_vod->pf_media_control = vlm_MediaVodControl;
2355 p_media = malloc( sizeof( vlm_media_sys_t ) );
2358 msg_Err( p_vlm, "out of memory" );
2361 memset( p_media, 0, sizeof(vlm_media_sys_t) );
2366 vlm_media_Copy( &p_media->cfg, p_cfg );
2367 p_media->cfg.id = p_vlm->i_id++;
2368 /* FIXME do we do something here if enabled is true ? */
2370 p_media->vod.p_item = input_ItemNew( p_vlm, NULL, NULL );
2372 p_media->vod.p_media = NULL;
2373 TAB_INIT( p_media->i_instance, p_media->instance );
2376 TAB_APPEND( p_vlm->i_media, p_vlm->media, p_media );
2379 *p_id = p_media->cfg.id;
2381 return vlm_OnMediaUpdate( p_vlm, p_media );
2384 static int vlm_ControlMediaDel( vlm_t *p_vlm, int64_t id )
2386 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2389 return VLC_EGENERIC;
2391 while( p_media->i_instance > 0 )
2392 vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, p_media->instance[0]->psz_name );
2394 if( p_media->cfg.b_vod )
2396 p_media->cfg.b_enabled = false;
2397 vlm_OnMediaUpdate( p_vlm, p_media );
2401 vlm_media_Clean( &p_media->cfg );
2403 vlc_gc_decref( p_media->vod.p_item );
2405 TAB_REMOVE( p_vlm->i_media, p_vlm->media, p_media );
2409 /* Check if we need to unload the VOD server */
2410 if( p_vlm->p_vod && p_vlm->i_vod <= 0 )
2412 module_Unneed( p_vlm->p_vod, p_vlm->p_vod->p_module );
2413 vlc_object_detach( p_vlm->p_vod );
2414 vlc_object_release( p_vlm->p_vod );
2415 p_vlm->p_vod = NULL;
2420 static int vlm_ControlMediaGets( vlm_t *p_vlm, vlm_media_t ***ppp_dsc, int *pi_dsc )
2422 vlm_media_t **pp_dsc;
2426 TAB_INIT( i_dsc, pp_dsc );
2427 for( i = 0; i < p_vlm->i_media; i++ )
2429 vlm_media_t *p_dsc = vlm_media_Duplicate( &p_vlm->media[i]->cfg );
2430 TAB_APPEND( i_dsc, pp_dsc, p_dsc );
2438 static int vlm_ControlMediaClear( vlm_t *p_vlm )
2440 while( p_vlm->i_media > 0 )
2441 vlm_ControlMediaDel( p_vlm, p_vlm->media[0]->cfg.id );
2445 static int vlm_ControlMediaGet( vlm_t *p_vlm, int64_t id, vlm_media_t **pp_dsc )
2447 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2449 return VLC_EGENERIC;
2451 *pp_dsc = vlm_media_Duplicate( &p_media->cfg );
2454 static int vlm_ControlMediaGetId( vlm_t *p_vlm, const char *psz_name, int64_t *p_id )
2456 vlm_media_sys_t *p_media = vlm_ControlMediaGetByName( p_vlm, psz_name );
2458 return VLC_EGENERIC;
2460 *p_id = p_media->cfg.id;
2464 static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
2468 for( i = 0; i < p_media->i_instance; i++ )
2470 const char *psz = p_media->instance[i]->psz_name;
2471 if( ( psz == NULL && psz_id == NULL ) ||
2472 ( psz && psz_id && !strcmp( psz, psz_id ) ) )
2473 return p_media->instance[i];
2477 static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
2479 vlm_media_instance_sys_t *p_instance = malloc( sizeof(vlm_media_instance_sys_t) );
2483 memset( p_instance, 0, sizeof(vlm_media_instance_sys_t) );
2485 p_instance->psz_name = NULL;
2487 p_instance->psz_name = strdup( psz_name );
2489 p_instance->p_item = input_ItemNew( p_vlm, NULL, NULL );
2491 p_instance->i_index = 0;
2492 p_instance->b_sout_keep = false;
2493 p_instance->p_input = NULL;
2494 p_instance->p_sout = NULL;
2498 static void vlm_MediaInstanceDelete( vlm_media_instance_sys_t *p_instance )
2500 if( p_instance->p_input )
2502 input_StopThread( p_instance->p_input );
2503 p_instance->p_sout = input_DetachSout( p_instance->p_input );
2504 vlc_object_release( p_instance->p_input );
2506 if( p_instance->p_sout )
2507 sout_DeleteInstance( p_instance->p_sout );
2509 vlc_gc_decref( p_instance->p_item );
2510 free( p_instance->psz_name );
2515 static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
2517 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2518 vlm_media_instance_sys_t *p_instance;
2521 if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
2522 return VLC_EGENERIC;
2524 /* TODO support multiple input for VOD with sout-keep ? */
2526 if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
2527 return VLC_EGENERIC;
2529 if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
2530 return VLC_EGENERIC;
2532 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2535 vlm_media_t *p_cfg = &p_media->cfg;
2536 const char *psz_keep;
2539 p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
2543 if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
2546 asprintf( &psz_buffer, "sout=%s%s%s",
2547 p_cfg->psz_output ? p_cfg->psz_output : "",
2548 (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
2549 psz_vod_output ? psz_vod_output : "" );
2550 input_ItemAddOption( p_instance->p_item, psz_buffer );
2554 for( i = 0; i < p_cfg->i_option; i++ )
2556 if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
2557 p_instance->b_sout_keep = true;
2558 else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
2559 p_instance->b_sout_keep = false;
2561 input_ItemAddOption( p_instance->p_item, p_cfg->ppsz_option[i] );
2563 /* We force the right sout-keep value (avoid using the sout-keep from the global configuration)
2564 * FIXME implement input list for VOD (need sout-keep)
2566 if( !p_cfg->b_vod && p_instance->b_sout_keep )
2567 psz_keep = "sout-keep";
2569 psz_keep = "no-sout-keep";
2570 input_ItemAddOption( p_instance->p_item, psz_keep );
2572 TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
2575 /* Stop old instance */
2576 if( p_instance->p_input )
2578 if( p_instance->i_index == i_input_index &&
2579 !p_instance->p_input->b_eof && !p_instance->p_input->b_error )
2581 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2582 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2586 input_StopThread( p_instance->p_input );
2587 p_instance->p_sout = input_DetachSout( p_instance->p_input );
2588 vlc_object_release( p_instance->p_input );
2589 if( !p_instance->b_sout_keep && p_instance->p_sout )
2591 sout_DeleteInstance( p_instance->p_sout );
2592 p_instance->p_sout = NULL;
2597 p_instance->i_index = i_input_index;
2598 input_item_SetURI( p_instance->p_item, p_media->cfg.ppsz_input[p_instance->i_index] ) ;
2600 asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name );
2601 p_instance->p_input = input_CreateThreadExtended( p_vlm, p_instance->p_item, psz_log, p_instance->p_sout );
2602 if( !p_instance->p_input )
2604 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2605 vlm_MediaInstanceDelete( p_instance );
2612 static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
2614 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2615 vlm_media_instance_sys_t *p_instance;
2618 return VLC_EGENERIC;
2620 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2622 return VLC_EGENERIC;
2624 TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
2626 vlm_MediaInstanceDelete( p_instance );
2630 static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
2632 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2633 vlm_media_instance_sys_t *p_instance;
2637 return VLC_EGENERIC;
2639 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2640 if( !p_instance || !p_instance->p_input )
2641 return VLC_EGENERIC;
2643 /* Toggle pause state */
2644 i_state = var_GetInteger( p_instance->p_input, "state" );
2645 if( i_state == PAUSE_S )
2646 var_SetInteger( p_instance->p_input, "state", PLAYING_S );
2647 else if( i_state == PLAYING_S )
2648 var_SetInteger( p_instance->p_input, "state", PAUSE_S );
2651 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
2653 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2654 vlm_media_instance_sys_t *p_instance;
2657 return VLC_EGENERIC;
2659 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2660 if( !p_instance || !p_instance->p_input )
2661 return VLC_EGENERIC;
2664 *pi_time = var_GetTime( p_instance->p_input, "time" );
2666 *pd_position = var_GetFloat( p_instance->p_input, "position" );
2669 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t i_time, double d_position )
2671 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2672 vlm_media_instance_sys_t *p_instance;
2675 return VLC_EGENERIC;
2677 p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
2678 if( !p_instance || !p_instance->p_input )
2679 return VLC_EGENERIC;
2682 return var_SetTime( p_instance->p_input, "time", i_time );
2683 else if( d_position >= 0 && d_position <= 100 )
2684 return var_SetFloat( p_instance->p_input, "position", d_position );
2685 return VLC_EGENERIC;
2688 static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_instance_t ***ppp_idsc, int *pi_instance )
2690 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2691 vlm_media_instance_t **pp_idsc;
2696 return VLC_EGENERIC;
2698 TAB_INIT( i_idsc, pp_idsc );
2699 for( i = 0; i < p_media->i_instance; i++ )
2701 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
2702 vlm_media_instance_t *p_idsc = vlm_media_instance_New();
2704 if( p_instance->psz_name )
2705 p_idsc->psz_name = strdup( p_instance->psz_name );
2706 if( p_instance->p_input )
2708 p_idsc->i_time = var_GetTime( p_instance->p_input, "time" );
2709 p_idsc->i_length = var_GetTime( p_instance->p_input, "length" );
2710 p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
2711 if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
2712 p_idsc->b_paused = true;
2713 p_idsc->i_rate = var_GetInteger( p_instance->p_input, "rate" );
2716 TAB_APPEND( i_idsc, pp_idsc, p_idsc );
2718 *ppp_idsc = pp_idsc;
2719 *pi_instance = i_idsc;
2722 static int vlm_ControlMediaInstanceClear( vlm_t *p_vlm, int64_t id )
2724 vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
2727 return VLC_EGENERIC;
2729 while( p_media->i_instance > 0 )
2730 vlm_ControlMediaInstanceStop( p_vlm, id, p_media->instance[0]->psz_name );
2735 static int vlm_ControlScheduleClear( vlm_t *p_vlm )
2737 while( p_vlm->i_schedule > 0 )
2738 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0] );
2742 static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
2745 vlm_media_t **pp_dsc;
2746 vlm_media_t ***ppp_dsc;
2747 vlm_media_instance_t ***ppp_idsc;
2749 const char *psz_vod;
2763 case VLM_GET_MEDIAS:
2764 ppp_dsc = (vlm_media_t ***)va_arg( args, vlm_media_t *** );
2765 pi_int = (int *)va_arg( args, int * );
2766 return vlm_ControlMediaGets( p_vlm, ppp_dsc, pi_int );
2768 case VLM_CLEAR_MEDIAS:
2769 return vlm_ControlMediaClear( p_vlm );
2771 case VLM_CHANGE_MEDIA:
2772 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2773 return vlm_ControlMediaChange( p_vlm, p_dsc );
2776 p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
2777 p_id = (int64_t*)va_arg( args, int64_t * );
2778 return vlm_ControlMediaAdd( p_vlm, p_dsc, p_id );
2781 id = (int64_t)va_arg( args, int64_t );
2782 return vlm_ControlMediaDel( p_vlm, id );
2785 id = (int64_t)va_arg( args, int64_t );
2786 pp_dsc = (vlm_media_t **)va_arg( args, vlm_media_t ** );
2787 return vlm_ControlMediaGet( p_vlm, id, pp_dsc );
2789 case VLM_GET_MEDIA_ID:
2790 psz_id = (const char*)va_arg( args, const char * );
2791 p_id = (int64_t*)va_arg( args, int64_t * );
2792 return vlm_ControlMediaGetId( p_vlm, psz_id, p_id );
2795 /* Media instance control */
2796 case VLM_GET_MEDIA_INSTANCES:
2797 id = (int64_t)va_arg( args, int64_t );
2798 ppp_idsc = (vlm_media_instance_t ***)va_arg( args, vlm_media_instance_t *** );
2799 pi_int = (int *)va_arg( args, int *);
2800 return vlm_ControlMediaInstanceGets( p_vlm, id, ppp_idsc, pi_int );
2802 case VLM_CLEAR_MEDIA_INSTANCES:
2803 id = (int64_t)va_arg( args, int64_t );
2804 return vlm_ControlMediaInstanceClear( p_vlm, id );
2807 case VLM_START_MEDIA_BROADCAST_INSTANCE:
2808 id = (int64_t)va_arg( args, int64_t );
2809 psz_id = (const char*)va_arg( args, const char* );
2810 i_int = (int)va_arg( args, int );
2811 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, NULL );
2813 case VLM_START_MEDIA_VOD_INSTANCE:
2814 id = (int64_t)va_arg( args, int64_t );
2815 psz_id = (const char*)va_arg( args, const char* );
2816 i_int = (int)va_arg( args, int );
2817 psz_vod = (const char*)va_arg( args, const char* );
2819 return VLC_EGENERIC;
2820 return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, psz_vod );
2822 case VLM_STOP_MEDIA_INSTANCE:
2823 id = (int64_t)va_arg( args, int64_t );
2824 psz_id = (const char*)va_arg( args, const char* );
2825 return vlm_ControlMediaInstanceStop( p_vlm, id, psz_id );
2827 case VLM_PAUSE_MEDIA_INSTANCE:
2828 id = (int64_t)va_arg( args, int64_t );
2829 psz_id = (const char*)va_arg( args, const char* );
2830 return vlm_ControlMediaInstancePause( p_vlm, id, psz_id );
2832 case VLM_GET_MEDIA_INSTANCE_TIME:
2833 id = (int64_t)va_arg( args, int64_t );
2834 psz_id = (const char*)va_arg( args, const char* );
2835 pi_i64 = (int64_t*)va_arg( args, int64_t * );
2836 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, pi_i64, NULL );
2837 case VLM_GET_MEDIA_INSTANCE_POSITION:
2838 id = (int64_t)va_arg( args, int64_t );
2839 psz_id = (const char*)va_arg( args, const char* );
2840 pd_double = (double*)va_arg( args, double* );
2841 return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, NULL, pd_double );
2843 case VLM_SET_MEDIA_INSTANCE_TIME:
2844 id = (int64_t)va_arg( args, int64_t );
2845 psz_id = (const char*)va_arg( args, const char* );
2846 i_i64 = (int64_t)va_arg( args, int64_t);
2847 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, i_i64, -1 );
2848 case VLM_SET_MEDIA_INSTANCE_POSITION:
2849 id = (int64_t)va_arg( args, int64_t );
2850 psz_id = (const char*)va_arg( args, const char* );
2851 d_double = (double)va_arg( args, double );
2852 return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, -1, d_double );
2854 case VLM_CLEAR_SCHEDULES:
2855 return vlm_ControlScheduleClear( p_vlm );
2858 msg_Err( p_vlm, "unknown VLM query" );
2859 return VLC_EGENERIC;
2862 static int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... )
2867 va_start( args, i_query );
2868 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2874 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2879 va_start( args, i_query );
2881 vlc_mutex_lock( &p_vlm->lock );
2882 i_result = vlm_vaControlInternal( p_vlm, i_query, args );
2883 vlc_mutex_unlock( &p_vlm->lock );
2890 #else /* ENABLE_VLM */
2892 /* We just define an empty wrapper */
2893 vlm_t *__vlm_New( vlc_object_t *a )
2895 msg_Err( a, "VideoLAN manager support is disabled" );
2899 void vlm_Delete( vlm_t *a )
2904 int vlm_ExecuteCommand( vlm_t *a, const char *b, vlm_message_t **c )
2909 vlm_message_t *vlm_MessageNew( const char *psz_name,
2910 const char *psz_format, ... )
2912 (void)psz_name; (void)psz_format;
2916 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
2917 vlm_message_t *p_child )
2922 void vlm_MessageDelete( vlm_message_t *a )
2927 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
2929 (void)p_vlm; (void)i_query;
2930 return VLC_EGENERIC;
2933 #endif /* ENABLE_VLM */