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 *****************************************************************************/
32 #include <stdlib.h> /* malloc(), free() */
33 #include <ctype.h> /* tolower() */
38 #include <vlc/input.h>
41 # include <time.h> /* ctime() */
42 # include <sys/timeb.h> /* ftime() */
49 /*****************************************************************************
51 *****************************************************************************/
52 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_t *, vlm_schedule_t *, const char * );
53 static vlm_message_t *vlm_Help( vlm_t *, char * );
55 static vlm_media_instance_t *vlm_MediaInstanceSearch( vlm_t *, vlm_media_t *, const char * );
57 static vlm_message_t *vlm_MessageNew( const char *, const char *, ... );
58 static vlm_message_t *vlm_MessageAdd( vlm_message_t *, vlm_message_t * );
60 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *, const char * );
62 static char *Save( vlm_t * );
63 static int Load( vlm_t *, char * );
64 static int ExecuteCommand( vlm_t *, const char *, vlm_message_t ** );
65 static int Manage( vlc_object_t * );
67 /*****************************************************************************
69 *****************************************************************************/
70 vlm_t *__vlm_New ( vlc_object_t *p_this )
76 /* to be sure to avoid multiple creation */
77 var_Create( p_this->p_libvlc_global, "vlm_mutex", VLC_VAR_MUTEX );
78 var_Get( p_this->p_libvlc_global, "vlm_mutex", &lockval );
79 vlc_mutex_lock( lockval.p_address );
81 if( !(p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE )) )
83 msg_Info( p_this, "creating VLM" );
84 if( ( p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM ) ) == NULL )
86 vlc_mutex_unlock( lockval.p_address );
90 vlc_mutex_init( p_this->p_libvlc, &p_vlm->lock );
94 p_vlm->i_schedule = 0;
95 p_vlm->schedule = NULL;
97 vlc_object_yield( p_vlm );
98 vlc_object_attach( p_vlm, p_this->p_libvlc );
100 vlc_mutex_unlock( lockval.p_address );
102 if( vlc_thread_create( p_vlm, "vlm thread",
103 Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
105 vlc_mutex_destroy( &p_vlm->lock );
106 vlc_object_destroy( p_vlm );
110 /* Try loading the vlm conf file given by --vlm-conf */
111 psz_vlmconf = config_GetPsz( p_vlm, "vlm-conf" );
113 if( psz_vlmconf && *psz_vlmconf )
115 vlm_message_t *p_message = NULL;
116 char *psz_buffer = NULL;
118 msg_Dbg( p_this, "loading VLM configuration" );
119 asprintf(&psz_buffer, "load %s", psz_vlmconf );
122 msg_Dbg( p_this, psz_buffer);
123 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) ){
124 msg_Warn( p_this, "error while loading the configuration file" );
126 vlm_MessageDelete(p_message);
135 /*****************************************************************************
137 *****************************************************************************/
138 void vlm_Delete( vlm_t *p_vlm )
142 var_Get( p_vlm->p_libvlc_global, "vlm_mutex", &lockval );
143 vlc_mutex_lock( lockval.p_address );
145 vlc_object_release( p_vlm );
147 if( p_vlm->i_refcount > 0 )
149 vlc_mutex_unlock( lockval.p_address );
153 p_vlm->b_die = VLC_TRUE;
154 vlc_thread_join( p_vlm );
156 vlc_mutex_destroy( &p_vlm->lock );
158 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0], NULL );
159 FREENULL( p_vlm->media );
161 while( p_vlm->i_schedule ) vlm_ScheduleDelete( p_vlm,
162 p_vlm->schedule[0], NULL );
163 FREENULL( p_vlm->schedule );
165 vlc_object_detach( p_vlm );
166 vlc_object_destroy( p_vlm );
167 vlc_mutex_unlock( lockval.p_address );
170 /*****************************************************************************
171 * vlm_ExecuteCommand:
172 *****************************************************************************/
173 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
174 vlm_message_t **pp_message)
178 vlc_mutex_lock( &p_vlm->lock );
179 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
180 vlc_mutex_unlock( &p_vlm->lock );
185 /*****************************************************************************
187 *****************************************************************************/
188 int vlm_Save( vlm_t *p_vlm, const char *psz_file )
193 if( !p_vlm || !psz_file ) return 1;
195 file = utf8_fopen( psz_file, "wt" );
196 if( file == NULL ) return 1;
198 psz_save = Save( p_vlm );
199 if( psz_save == NULL )
204 fwrite( psz_save, strlen( psz_save ), 1, file );
211 /*****************************************************************************
213 *****************************************************************************/
214 int vlm_Load( vlm_t *p_vlm, const char *psz_file )
220 if( !p_vlm || !psz_file ) return 1;
222 p_stream = stream_UrlNew( p_vlm, psz_file );
223 if( p_stream == NULL ) return 1;
225 if( stream_Seek( p_stream, 0 ) != 0 )
227 stream_Delete( p_stream );
231 i_size = stream_Size( p_stream );
233 psz_buffer = malloc( i_size + 1 );
236 stream_Delete( p_stream );
240 stream_Read( p_stream, psz_buffer, i_size );
241 psz_buffer[ i_size ] = '\0';
243 stream_Delete( p_stream );
245 if( Load( p_vlm, psz_buffer ) )
256 /*****************************************************************************
258 *****************************************************************************/
259 static const char *FindEndCommand( const char *psz_sent )
261 vlc_bool_t b_escape = VLC_FALSE;
267 while( ( *psz_sent != '\"' || b_escape == VLC_TRUE )
268 && ( *psz_sent != '\0' ) )
270 if( *psz_sent == '\'' && b_escape == VLC_FALSE )
272 psz_sent = FindEndCommand( psz_sent );
273 if( psz_sent == NULL ) return NULL;
275 else if( *psz_sent++ == '\\' && b_escape == VLC_FALSE )
278 b_escape = VLC_FALSE;
281 if( *psz_sent == '\"' )
287 /* *psz_sent == '\0' -> number of " is incorrect */
294 while( ( *psz_sent != '\'' || b_escape == VLC_TRUE )
295 && ( *psz_sent != '\0' ) )
297 if( *psz_sent == '\"' && b_escape == VLC_FALSE )
299 psz_sent = FindEndCommand( psz_sent );
300 if( psz_sent == NULL ) return NULL;
302 else if( *psz_sent++ == '\\' && b_escape == VLC_FALSE )
305 b_escape = VLC_FALSE;
308 if( *psz_sent == '\'' )
314 /* *psz_sent == '\0' -> number of " is incorrect */
319 default: /* now we can look for spaces */
320 while( ( *psz_sent != ' ' ) && ( *psz_sent != '\0' ) )
322 if( ( ( *psz_sent == '\'' ) || ( *psz_sent == '\"' ) )
323 && b_escape == VLC_FALSE )
325 psz_sent = FindEndCommand( psz_sent );
326 if( psz_sent == NULL ) return NULL;
328 else if( *psz_sent++ == '\\' && b_escape == VLC_FALSE )
331 b_escape = VLC_FALSE;
339 /*****************************************************************************
340 * ExecuteCommand: The main state machine
341 *****************************************************************************
342 * Execute a command which ends with '\0' (string)
343 *****************************************************************************/
344 static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
345 vlm_message_t **pp_message )
348 char **ppsz_command = NULL;
349 const char *psz_cmd = psz_command;
350 vlm_message_t *p_message = NULL;
353 /* First, parse the line and cut it */
354 while( *psz_cmd != '\0' )
357 if( *psz_cmd == ' ' || *psz_cmd == '\t' )
363 const char *psz_temp;
366 vlc_bool_t b_escape = VLC_FALSE;
369 /* support for comments */
370 if( i_command == 0 && *psz_cmd == '#')
372 p_message = vlm_MessageNew( "", NULL );
376 psz_temp = FindEndCommand( psz_cmd );
378 if( psz_temp == NULL )
380 p_message = vlm_MessageNew( "Incomplete command", psz_cmd );
384 i_temp = psz_temp - psz_cmd;
386 ppsz_command = realloc( ppsz_command, (i_command + 1) *
388 ppsz_command[ i_command ] = malloc( (i_temp + 1) * sizeof(char) );
390 /* unescape ", ' and \ ... and everything else */
392 psz_dst = ppsz_command[ i_command ];
395 if( *psz_buf == '\\' && b_escape == VLC_FALSE )
399 b_escape = VLC_FALSE;
414 * And then Interpret it
419 p_message = vlm_MessageNew( "", NULL );
423 if( !strcmp(ppsz_command[0], "new") )
427 /* Check the number of arguments */
428 if( i_command < 3 ) goto syntax_error;
431 if( !strcmp(ppsz_command[2], "vod") )
435 else if( !strcmp(ppsz_command[2], "broadcast") )
437 i_type = BROADCAST_TYPE;
439 else if( !strcmp(ppsz_command[2], "schedule") )
441 i_type = SCHEDULE_TYPE;
446 vlm_MessageNew( "new", "%s: Choose between vod, "
447 "broadcast or schedule", ppsz_command[1] );
451 /* Check for forbidden media names */
452 if( !strcmp(ppsz_command[1], "all") ||
453 !strcmp(ppsz_command[1], "media") ||
454 !strcmp(ppsz_command[1], "schedule") )
456 p_message = vlm_MessageNew( "new", "\"all\", \"media\" and "
457 "\"schedule\" are reserved names" );
461 /* Check the name is not already in use */
462 if( vlm_ScheduleSearch( p_vlm, ppsz_command[1] ) ||
463 vlm_MediaSearch( p_vlm, ppsz_command[1] ) )
465 p_message = vlm_MessageNew( "new", "%s: Name already in use",
471 if( i_type == SCHEDULE_TYPE )
473 vlm_schedule_t *p_schedule;
474 p_schedule = vlm_ScheduleNew( p_vlm, ppsz_command[1] );
477 p_message = vlm_MessageNew( "new", "could not create schedule" );
485 vlm_media_t *p_media;
486 p_media = vlm_MediaNew( p_vlm, ppsz_command[1], i_type );
489 p_message = vlm_MessageNew( "new", "could not create media" );
496 p_message = vlm_MessageNew( "new", NULL );
500 /* Properties will be dealt with later on */
503 else if( !strcmp(ppsz_command[0], "setup") )
505 if( i_command < 2 ) goto syntax_error;
507 /* Properties will be dealt with later on */
510 else if( !strcmp(ppsz_command[0], "del") )
512 vlm_media_t *p_media;
513 vlm_schedule_t *p_schedule;
515 if( i_command < 2 ) goto syntax_error;
517 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
518 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
520 if( p_schedule != NULL )
522 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
524 else if( p_media != NULL )
526 vlm_MediaDelete( p_vlm, p_media, NULL );
528 else if( !strcmp(ppsz_command[1], "media") )
530 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
533 else if( !strcmp(ppsz_command[1], "schedule") )
535 while( p_vlm->i_schedule )
536 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
538 else if( !strcmp(ppsz_command[1], "all") )
540 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
543 while( p_vlm->i_schedule )
544 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
548 p_message = vlm_MessageNew( "del", "%s: media unknown",
553 p_message = vlm_MessageNew( "del", NULL );
557 else if( !strcmp(ppsz_command[0], "show") )
559 vlm_media_t *p_media;
560 vlm_schedule_t *p_schedule;
564 p_message = vlm_Show( p_vlm, NULL, NULL, NULL );
567 else if( i_command > 2 ) goto syntax_error;
569 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
570 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
572 if( p_schedule != NULL )
574 p_message = vlm_Show( p_vlm, NULL, p_schedule, NULL );
576 else if( p_media != NULL )
578 p_message = vlm_Show( p_vlm, p_media, NULL, NULL );
582 p_message = vlm_Show( p_vlm, NULL, NULL, ppsz_command[1] );
588 else if( !strcmp(ppsz_command[0], "help") )
590 if( i_command != 1 ) goto syntax_error;
592 p_message = vlm_Help( p_vlm, NULL );
596 else if( !strcmp(ppsz_command[0], "control") )
598 vlm_media_t *p_media;
600 if( i_command < 3 ) goto syntax_error;
602 if( !(p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] ) ) )
604 p_message = vlm_MessageNew( "control", "%s: media unknown",
610 char *psz_command, *psz_arg = 0, *psz_instance = 0;
613 if( strcmp( ppsz_command[2], "play" ) &&
614 strcmp( ppsz_command[2], "stop" ) &&
615 strcmp( ppsz_command[2], "pause" ) &&
616 strcmp( ppsz_command[2], "seek" ) )
619 psz_instance = ppsz_command[2];
621 if( i_command < 4 ) goto syntax_error;
623 if( strcmp( ppsz_command[3], "play" ) &&
624 strcmp( ppsz_command[3], "stop" ) &&
625 strcmp( ppsz_command[3], "pause" ) &&
626 strcmp( ppsz_command[3], "seek" ) )
630 psz_command = ppsz_command[i_index];
632 if( i_command >= i_index + 2 ) psz_arg = ppsz_command[i_index + 1];
634 vlm_MediaControl( p_vlm, p_media, psz_instance, psz_command,
636 p_message = vlm_MessageNew( "control", NULL );
641 else if( !strcmp(ppsz_command[0], "save") )
643 if( i_command != 2 ) goto syntax_error;
645 if( vlm_Save( p_vlm, ppsz_command[1] ) )
647 p_message = vlm_MessageNew( "save", "Unable to save to file" );
652 p_message = vlm_MessageNew( "save", NULL );
657 else if( !strcmp(ppsz_command[0], "export" ) )
661 if( i_command != 1 ) goto syntax_error;
663 p_message = vlm_MessageNew( "export", psz_buf = Save( p_vlm ) );
669 else if( !strcmp(ppsz_command[0], "load") )
671 if( i_command != 2 ) goto syntax_error;
673 switch( vlm_Load( p_vlm, ppsz_command[1] ) )
676 p_message = vlm_MessageNew( "load", NULL );
679 p_message = vlm_MessageNew( "load", "Read file error" );
683 vlm_MessageNew( "load", "Error while loading file" );
687 vlm_MessageNew( "load", "Unable to load from file" );
694 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
698 /* Common code between "new" and "setup" */
699 if( !strcmp(ppsz_command[0], "new") ||
700 !strcmp(ppsz_command[0], "setup") )
702 int i_command_start = strcmp(ppsz_command[0], "new") ? 2 : 3;
703 vlm_media_t *p_media;
704 vlm_schedule_t *p_schedule;
706 if( i_command < i_command_start ) goto syntax_error;
708 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
709 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
711 if( !p_media && !p_schedule )
713 p_message = vlm_MessageNew( ppsz_command[0], "%s unknown",
718 if( p_schedule != NULL )
720 for( i = i_command_start ; i < i_command ; i++ )
722 if( !strcmp( ppsz_command[i], "enabled" ) ||
723 !strcmp( ppsz_command[i], "disabled" ) )
725 vlm_ScheduleSetup( p_schedule, ppsz_command[i], NULL );
728 /* Beware: everything behind append is considered as
730 else if( !strcmp( ppsz_command[i], "append" ) )
732 if( ++i >= i_command ) break;
734 for( j = i + 1; j < i_command; j++ )
737 realloc( ppsz_command[i], strlen(ppsz_command[i]) +
738 strlen(ppsz_command[j]) + 1 + 1 );
739 strcat( ppsz_command[i], " " );
740 strcat( ppsz_command[i], ppsz_command[j] );
743 vlm_ScheduleSetup( p_schedule, ppsz_command[i - 1],
749 if( i + 1 >= i_command && !strcmp(ppsz_command[0], "new") )
751 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
753 vlm_MessageNew( ppsz_command[0],
754 "Wrong properties syntax" );
757 else if( i + 1 >= i_command )
760 vlm_MessageNew( ppsz_command[0],
761 "Wrong properties syntax" );
765 vlm_ScheduleSetup( p_schedule, ppsz_command[i],
772 else if( p_media != NULL )
774 for( i = i_command_start ; i < i_command ; i++ )
776 if( !strcmp( ppsz_command[i], "enabled" ) ||
777 !strcmp( ppsz_command[i], "disabled" ) )
779 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
781 else if( i + 1 >= i_command &&
782 !strcmp( ppsz_command[i], "mux") )
784 if( p_media->i_type != VOD_TYPE )
786 p_message = vlm_MessageNew( ppsz_command[0],
787 "mux only available for broadcast" );
791 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
796 else if( !strcmp( ppsz_command[i], "loop" ) ||
797 !strcmp( ppsz_command[i], "unloop" ) )
799 if( p_media->i_type != BROADCAST_TYPE )
801 p_message = vlm_MessageNew( ppsz_command[0],
802 "loop only available for broadcast" );
806 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
811 if( ( (i + 1) >= i_command ) &&
812 !strcmp(ppsz_command[0], "new") )
814 vlm_MediaDelete( p_vlm, p_media, NULL );
816 vlm_MessageNew( ppsz_command[0],
817 "Wrong properties syntax" );
820 else if( (i + 1) >= i_command )
823 vlm_MessageNew( ppsz_command[0],
824 "Wrong properties syntax" );
828 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
835 p_message = vlm_MessageNew( ppsz_command[0], NULL );
840 for( i = 0 ; i < i_command ; i++ ) FREENULL( ppsz_command[i] );
841 FREENULL( ppsz_command );
842 *pp_message = p_message;
847 p_message = vlm_MessageNew( ppsz_command[0], "Wrong command syntax" );
850 for( i = 0 ; i < i_command ; i++ ) FREENULL( ppsz_command[i] );
851 FREENULL( ppsz_command );
852 *pp_message = p_message;
857 vlm_media_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
861 for( i = 0; i < vlm->i_media; i++ )
863 if( strcmp( psz_name, vlm->media[i]->psz_name ) == 0 )
865 return vlm->media[i];
872 /*****************************************************************************
874 *****************************************************************************/
875 static vlm_media_instance_t *
876 vlm_MediaInstanceSearch( vlm_t *vlm, vlm_media_t *media,
877 const char *psz_name )
881 for( i = 0; i < media->i_instance; i++ )
883 if( ( !psz_name && !media->instance[i]->psz_name ) ||
884 ( psz_name && media->instance[i]->psz_name &&
885 !strcmp( psz_name, media->instance[i]->psz_name ) ) )
887 return media->instance[i];
894 vlm_media_t *vlm_MediaNew( vlm_t *vlm, const char *psz_name, int i_type )
896 vlm_media_t *media = malloc( sizeof( vlm_media_t ) );
900 msg_Err( vlm, "out of memory" );
904 /* Check if we need to load the VOD server */
905 if( i_type == VOD_TYPE && !vlm->i_vod )
907 vlm->vod = vlc_object_create( vlm, VLC_OBJECT_VOD );
908 vlc_object_attach( vlm->vod, vlm );
909 vlm->vod->p_module = module_Need( vlm->vod, "vod server", 0, 0 );
910 if( !vlm->vod->p_module )
912 msg_Err( vlm, "cannot find vod server" );
913 vlc_object_detach( vlm->vod );
914 vlc_object_destroy( vlm->vod );
920 vlm->vod->p_data = vlm;
921 vlm->vod->pf_media_control = vlm_MediaVodControl;
924 if( i_type == VOD_TYPE ) vlm->i_vod++;
926 media->psz_name = strdup( psz_name );
927 media->b_enabled = VLC_FALSE;
928 media->b_loop = VLC_FALSE;
929 media->vod_media = NULL;
930 media->psz_vod_output = NULL;
931 media->psz_mux = NULL;
934 media->psz_output = NULL;
936 media->option = NULL;
937 media->i_type = i_type;
938 media->i_instance = 0;
939 media->instance = NULL;
941 input_ItemInit( VLC_OBJECT(vlm), &media->item );
943 TAB_APPEND( vlm->i_media, vlm->media, media );
948 /* for now, simple delete. After, del with options (last arg) */
949 void vlm_MediaDelete( vlm_t *vlm, vlm_media_t *media, const char *psz_name )
951 if( media == NULL ) return;
953 while( media->i_instance )
955 vlm_media_instance_t *p_instance = media->instance[0];
956 vlm_MediaControl( vlm, media, p_instance->psz_name, "stop", 0 );
959 TAB_REMOVE( vlm->i_media, vlm->media, media );
961 if( media->i_type == VOD_TYPE )
963 vlm_MediaSetup( vlm, media, "disabled", 0 );
967 /* Check if we need to unload the VOD server */
968 if( media->i_type == VOD_TYPE && !vlm->i_vod )
970 module_Unneed( vlm->vod, vlm->vod->p_module );
971 vlc_object_detach( vlm->vod );
972 vlc_object_destroy( vlm->vod );
976 if( vlm->i_media == 0 && vlm->media ) free( vlm->media );
978 free( media->psz_name );
980 while( media->i_input-- ) free( media->input[media->i_input] );
981 if( media->input ) free( media->input );
983 if( media->psz_output ) free( media->psz_output );
984 if( media->psz_mux ) free( media->psz_mux );
986 while( media->i_option-- ) free( media->option[media->i_option] );
987 if( media->option ) free( media->option );
989 input_ItemClean( &media->item );
994 int vlm_MediaSetup( vlm_t *vlm, vlm_media_t *media, const char *psz_cmd,
995 const char *psz_value )
997 if( !psz_cmd) return VLC_EGENERIC;
999 if( !strcmp( psz_cmd, "loop" ) )
1001 media->b_loop = VLC_TRUE;
1003 else if( !strcmp( psz_cmd, "unloop" ) )
1005 media->b_loop = VLC_FALSE;
1007 else if( !strcmp( psz_cmd, "enabled" ) )
1009 media->b_enabled = VLC_TRUE;
1011 else if( !strcmp( psz_cmd, "disabled" ) )
1013 media->b_enabled = VLC_FALSE;
1015 else if( !strcmp( psz_cmd, "mux" ) )
1017 if( media->psz_mux ) free( media->psz_mux );
1018 media->psz_mux = NULL;
1019 if( psz_value ) media->psz_mux = strdup( psz_value );
1021 else if( !strcmp( psz_cmd, "input" ) )
1025 if( psz_value != NULL && strlen(psz_value) > 1 &&
1026 ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
1027 ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
1028 psz_value[ strlen(psz_value) - 1 ] == '\"' ) )
1030 input = malloc( strlen(psz_value) - 1 );
1032 memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
1033 input[ strlen(psz_value) - 2 ] = '\0';
1037 input = strdup( psz_value );
1040 TAB_APPEND( media->i_input, media->input, input );
1042 else if( !strcmp( psz_cmd, "inputdel" ) && !strcmp( psz_value, "all" ) )
1044 while( media->i_input > 0 )
1046 TAB_REMOVE( media->i_input, media->input, media->input[0] );
1049 else if( !strcmp( psz_cmd, "inputdel" ) )
1054 if( psz_value != NULL && strlen(psz_value) > 1 &&
1055 ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
1056 ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
1057 psz_value[ strlen(psz_value) - 1 ] == '\"' ) )
1059 input = malloc( strlen(psz_value) - 1 );
1061 memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
1062 input[ strlen(psz_value) - 2 ] = '\0';
1066 input = strdup( psz_value );
1069 for( i = 0; i < media->i_input; i++ )
1071 if( !strcmp( input, media->input[i] ) )
1073 TAB_REMOVE( media->i_input, media->input, media->input[i] );
1078 else if( !strcmp( psz_cmd, "inputdeln" ) )
1080 int index = atoi( psz_value );
1081 if( index > 0 && index <= media->i_input )
1083 TAB_REMOVE( media->i_input, media->input, media->input[index-1] );
1086 else if( !strcmp( psz_cmd, "output" ) )
1088 if( media->psz_output != NULL )
1090 free( media->psz_output );
1092 media->psz_output = strdup( psz_value );
1094 else if( !strcmp( psz_cmd, "option" ) )
1097 psz_option = strdup( psz_value );
1099 TAB_APPEND( media->i_option, media->option, psz_option );
1103 return VLC_EGENERIC;
1106 /* Check if we need to create/delete a vod media */
1107 if( media->i_type == VOD_TYPE )
1109 if( !media->b_enabled && media->vod_media )
1111 vlm->vod->pf_media_del( vlm->vod, media->vod_media );
1112 media->vod_media = 0;
1114 else if( media->b_enabled && !media->vod_media && media->i_input )
1116 /* Pre-parse the input */
1117 input_thread_t *p_input;
1122 input_ItemClean( &media->item );
1123 input_ItemInit( VLC_OBJECT(vlm), &media->item );
1125 if( media->psz_output )
1126 asprintf( &psz_output, "%s:description", media->psz_output );
1128 asprintf( &psz_output, "#description" );
1130 media->item.psz_uri = strdup( media->input[0] );
1131 media->item.ppsz_options = malloc( sizeof( char* ) );
1132 asprintf( &media->item.ppsz_options[0], "sout=%s", psz_output);
1133 media->item.i_options = 1;
1134 for( i = 0; i < media->i_option; i++ )
1136 media->item.i_options++;
1137 media->item.ppsz_options =
1138 realloc( media->item.ppsz_options,
1139 media->item.i_options * sizeof( char* ) );
1140 media->item.ppsz_options[ media->item.i_options - 1 ] =
1141 strdup( media->option[i] );
1144 asprintf( &psz_header, _("Media: %s"), media->psz_name );
1146 if( (p_input = input_CreateThread2( vlm, &media->item, psz_header
1149 while( !p_input->b_eof && !p_input->b_error ) msleep( 100000 );
1151 input_StopThread( p_input );
1152 input_DestroyThread( p_input );
1153 vlc_object_detach( p_input );
1154 vlc_object_destroy( p_input );
1159 if( media->psz_mux )
1162 es_format_t es, *p_es = &es;
1165 sprintf( fourcc, "%4.4s", media->psz_mux );
1166 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
1167 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
1172 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
1175 vlm->vod->pf_media_new( vlm->vod, media->psz_name, &item );
1180 vlm->vod->pf_media_new( vlm->vod, media->psz_name,
1188 int vlm_MediaControl( vlm_t *vlm, vlm_media_t *media, const char *psz_id,
1189 const char *psz_command, const char *psz_args )
1191 vlm_media_instance_t *p_instance;
1195 p_instance = vlm_MediaInstanceSearch( vlm, media, psz_id );
1197 if( !strcmp( psz_command, "play" ) )
1199 if( !media->b_enabled || media->i_input == 0 ) return 0;
1203 p_instance = malloc( sizeof(vlm_media_instance_t) );
1204 if( !p_instance ) return VLC_EGENERIC;
1205 memset( p_instance, 0, sizeof(vlm_media_instance_t) );
1206 input_ItemInit( VLC_OBJECT(vlm), &p_instance->item );
1207 p_instance->p_input = NULL;
1209 if( ( media->psz_output != NULL ) || ( media->psz_vod_output != NULL ) )
1211 p_instance->item.ppsz_options = malloc( sizeof( char* ) );
1212 asprintf( &p_instance->item.ppsz_options[0], "sout=%s%s%s",
1213 media->psz_output ? media->psz_output : "",
1214 (media->psz_output && media->psz_vod_output) ?
1215 ":" : media->psz_vod_output ? "#" : "",
1216 media->psz_vod_output ? media->psz_vod_output : "" );
1217 p_instance->item.i_options = 1;
1220 for( i = 0; i < media->i_option; i++ )
1222 p_instance->item.i_options++;
1223 p_instance->item.ppsz_options =
1224 realloc( p_instance->item.ppsz_options,
1225 p_instance->item.i_options * sizeof( char* ) );
1226 p_instance->item.ppsz_options[p_instance->item.i_options - 1] =
1227 strdup( media->option[i] );
1230 p_instance->psz_name = psz_id ? strdup( psz_id ) : NULL;
1231 TAB_APPEND( media->i_instance, media->instance, p_instance );
1234 if( ( psz_args && sscanf(psz_args, "%d", &i) == 1 ) && ( i < media->i_input ) )
1236 p_instance->i_index = i;
1239 if( p_instance->item.psz_uri ) free( p_instance->item.psz_uri );
1240 p_instance->item.psz_uri =
1241 strdup( media->input[p_instance->i_index] );
1243 if( p_instance->p_input )
1245 input_StopThread( p_instance->p_input );
1246 input_DestroyThread( p_instance->p_input );
1247 vlc_object_detach( p_instance->p_input );
1248 vlc_object_destroy( p_instance->p_input );
1251 asprintf( &psz_header, _("Media: %s"), media->psz_name );
1252 p_instance->p_input = input_CreateThread2( vlm, &p_instance->item,
1254 if( !p_instance->p_input )
1256 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1257 input_ItemClean( &p_instance->item );
1258 if( p_instance->psz_name ) free( p_instance->psz_name );
1265 if( !p_instance ) return VLC_EGENERIC;
1267 if( !strcmp( psz_command, "seek" ) )
1274 f_percentage = i18n_atof( psz_args );
1275 if( f_percentage >= 0.0 && f_percentage <= 100.0 )
1277 val.f_float = f_percentage / 100.0 ;
1278 var_Set( p_instance->p_input, "position", val );
1283 else if( !strcmp( psz_command, "rewind" ) )
1291 f_scale = i18n_atof( psz_args );
1292 f_pos = var_GetFloat( p_instance->p_input, "position" );
1293 val.f_float = f_pos - (f_scale / 1000.0);
1294 if( val.f_float < 0.0 )
1296 var_Set( p_instance->p_input, "position", val );
1300 else if( !strcmp( psz_command, "forward" ) )
1308 f_scale = i18n_atof( psz_args );
1309 f_pos = var_GetFloat( p_instance->p_input, "position" );
1310 val.f_float = f_pos + (f_scale / 1000.0);
1311 if( val.f_float > 1.0 )
1313 var_Set( p_instance->p_input, "position", val );
1317 else if( !strcmp( psz_command, "stop" ) )
1319 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1321 if( p_instance->p_input )
1323 input_StopThread( p_instance->p_input );
1324 input_DestroyThread( p_instance->p_input );
1325 vlc_object_detach( p_instance->p_input );
1326 vlc_object_destroy( p_instance->p_input );
1329 input_ItemClean( &p_instance->item );
1330 if( p_instance->psz_name ) free( p_instance->psz_name );
1335 else if( !strcmp( psz_command, "pause" ) )
1339 if( !p_instance->p_input ) return VLC_SUCCESS;
1341 var_Get( p_instance->p_input, "state", &val );
1343 if( val.i_int == PAUSE_S ) val.i_int = PLAYING_S;
1344 else val.i_int = PAUSE_S;
1345 var_Set( p_instance->p_input, "state", val );
1350 return VLC_EGENERIC;
1353 /*****************************************************************************
1355 *****************************************************************************/
1356 static int64_t vlm_Date(void)
1361 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1367 vlm_schedule_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
1369 vlm_schedule_t *p_sched = malloc( sizeof( vlm_schedule_t ) );
1381 p_sched->psz_name = strdup( psz_name );
1382 p_sched->b_enabled = VLC_FALSE;
1383 p_sched->i_command = 0;
1384 p_sched->command = NULL;
1385 p_sched->i_date = 0;
1386 p_sched->i_period = 0;
1387 p_sched->i_repeat = -1;
1389 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1394 /* for now, simple delete. After, del with options (last arg) */
1395 void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_t *sched,
1396 const char *psz_name )
1398 if( sched == NULL ) return;
1400 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1402 if( vlm->i_schedule == 0 && vlm->schedule ) free( vlm->schedule );
1403 free( sched->psz_name );
1404 while( sched->i_command )
1406 char *psz_cmd = sched->command[0];
1407 TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1413 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1417 for( i = 0; i < vlm->i_schedule; i++ )
1419 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1421 return vlm->schedule[i];
1428 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1429 int vlm_ScheduleSetup( vlm_schedule_t *schedule, const char *psz_cmd,
1430 const char *psz_value )
1432 if( !strcmp( psz_cmd, "enabled" ) )
1434 schedule->b_enabled = VLC_TRUE;
1436 else if( !strcmp( psz_cmd, "disabled" ) )
1438 schedule->b_enabled = VLC_FALSE;
1440 #if !defined( UNDER_CE )
1441 else if( !strcmp( psz_cmd, "date" ) )
1447 time.tm_sec = 0; /* seconds */
1448 time.tm_min = 0; /* minutes */
1449 time.tm_hour = 0; /* hours */
1450 time.tm_mday = 0; /* day of the month */
1451 time.tm_mon = 0; /* month */
1452 time.tm_year = 0; /* year */
1453 time.tm_wday = 0; /* day of the week */
1454 time.tm_yday = 0; /* day in the year */
1455 time.tm_isdst = -1; /* daylight saving time */
1457 /* date should be year/month/day-hour:minutes:seconds */
1458 p = strchr( psz_value, '-' );
1460 if( !strcmp( psz_value, "now" ) )
1462 schedule->i_date = 0;
1464 else if( (p == NULL) && sscanf( psz_value, "%d:%d:%d", &time.tm_hour,
1465 &time.tm_min, &time.tm_sec ) != 3 )
1466 /* it must be a hour:minutes:seconds */
1474 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1492 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1498 time.tm_mon = i - 1;
1502 time.tm_year = i - 1900;
1503 time.tm_mon = j - 1;
1510 date = mktime( &time );
1511 schedule->i_date = ((mtime_t) date) * 1000000;
1514 else if( !strcmp( psz_cmd, "period" ) )
1518 const char *psz_time = NULL, *psz_date = NULL;
1522 /* First, if date or period are modified, repeat should be equal to -1 */
1523 schedule->i_repeat = -1;
1525 time.tm_sec = 0; /* seconds */
1526 time.tm_min = 0; /* minutes */
1527 time.tm_hour = 0; /* hours */
1528 time.tm_mday = 0; /* day of the month */
1529 time.tm_mon = 0; /* month */
1530 time.tm_year = 0; /* year */
1531 time.tm_wday = 0; /* day of the week */
1532 time.tm_yday = 0; /* day in the year */
1533 time.tm_isdst = -1; /* daylight saving time */
1535 /* date should be year/month/day-hour:minutes:seconds */
1536 p = strchr( psz_value, '-' );
1539 psz_date = psz_value;
1544 psz_time = psz_value;
1547 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1566 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1585 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1586 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1587 schedule->i_period = ((mtime_t) date) * 1000000;
1589 #endif /* UNDER_CE */
1590 else if( !strcmp( psz_cmd, "repeat" ) )
1594 if( sscanf( psz_value, "%d", &i ) == 1 )
1596 schedule->i_repeat = i;
1603 else if( !strcmp( psz_cmd, "append" ) )
1605 char *command = strdup( psz_value );
1607 TAB_APPEND( schedule->i_command, schedule->command, command );
1616 /*****************************************************************************
1617 * Message handling functions
1618 *****************************************************************************/
1619 static vlm_message_t *vlm_MessageNew( const char *psz_name,
1620 const char *psz_format, ... )
1622 vlm_message_t *p_message;
1625 if( !psz_name ) return 0;
1627 p_message = malloc( sizeof(vlm_message_t) );
1633 p_message->psz_value = 0;
1637 va_start( args, psz_format );
1638 if( vasprintf( &p_message->psz_value, psz_format, args ) < 0 )
1647 p_message->psz_name = strdup( psz_name );
1648 p_message->i_child = 0;
1649 p_message->child = NULL;
1654 void vlm_MessageDelete( vlm_message_t *p_message )
1656 if( p_message->psz_name ) free( p_message->psz_name );
1657 if( p_message->psz_value ) free( p_message->psz_value );
1658 while( p_message->i_child-- )
1659 vlm_MessageDelete( p_message->child[p_message->i_child] );
1660 if( p_message->child ) free( p_message->child );
1665 static vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1666 vlm_message_t *p_child )
1668 if( p_message == NULL ) return NULL;
1672 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1678 /*****************************************************************************
1679 * Misc utility functions
1680 *****************************************************************************/
1681 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_t *media,
1682 vlm_schedule_t *schedule,
1683 const char *psz_filter )
1689 vlm_message_t *msg_media;
1690 vlm_message_t *msg_child;
1692 msg = vlm_MessageNew( "show", NULL );
1693 msg_media = vlm_MessageAdd( msg, vlm_MessageNew( media->psz_name, 0 ));
1695 vlm_MessageAdd( msg_media,
1696 vlm_MessageNew( "type", media->i_type == VOD_TYPE ?
1697 "vod" : "broadcast" ) );
1698 vlm_MessageAdd( msg_media,
1699 vlm_MessageNew( "enabled", media->b_enabled ?
1702 vlm_MessageAdd( msg_media,
1703 vlm_MessageNew( "loop", media->b_loop ?
1706 if( media->i_type == VOD_TYPE && media->psz_mux )
1707 vlm_MessageAdd( msg_media,
1708 vlm_MessageNew( "mux", media->psz_mux ) );
1710 msg_child = vlm_MessageAdd( msg_media,
1711 vlm_MessageNew( "inputs", NULL ) );
1713 for( i = 0; i < media->i_input; i++ )
1715 vlm_MessageAdd( msg_child,
1716 vlm_MessageNew( media->input[i], NULL ) );
1719 vlm_MessageAdd( msg_media,
1720 vlm_MessageNew( "output", media->psz_output ?
1721 media->psz_output : "" ) );
1723 msg_child = vlm_MessageAdd( msg_media, vlm_MessageNew( "options", 0 ));
1725 for( i = 0; i < media->i_option; i++ )
1727 vlm_MessageAdd( msg_child, vlm_MessageNew( media->option[i], 0 ) );
1730 msg_child = vlm_MessageAdd( msg_media,
1731 vlm_MessageNew( "instances", NULL ) );
1733 for( i = 0; i < media->i_instance; i++ )
1735 vlm_media_instance_t *p_instance = media->instance[i];
1737 vlm_message_t *msg_instance;
1740 if( !p_instance->p_input ) val.i_int = END_S;
1741 else var_Get( p_instance->p_input, "state", &val );
1743 msg_instance = vlm_MessageNew( "instance" , NULL );
1744 vlm_MessageAdd( msg_instance, vlm_MessageNew( "name" , p_instance->psz_name ? p_instance->psz_name : "default" ) );
1745 vlm_MessageAdd( msg_instance, vlm_MessageNew( "state",
1746 val.i_int == PLAYING_S ? "playing" :
1747 val.i_int == PAUSE_S ? "paused" :
1749 #define APPEND_INPUT_INFO( a, format, type ) \
1750 asprintf( &psz_tmp, format, \
1751 var_Get ## type( p_instance->p_input, a ) ); \
1752 vlm_MessageAdd( msg_instance, vlm_MessageNew( a, psz_tmp ) ); \
1754 APPEND_INPUT_INFO( "position", "%f", Float );
1755 APPEND_INPUT_INFO( "time", I64Fi, Time );
1756 APPEND_INPUT_INFO( "length", I64Fi, Time );
1757 APPEND_INPUT_INFO( "rate", "%d", Integer );
1758 APPEND_INPUT_INFO( "title", "%d", Integer );
1759 APPEND_INPUT_INFO( "chapter", "%d", Integer );
1760 APPEND_INPUT_INFO( "seekable", "%d", Bool );
1761 #undef APPEND_INPUT_INFO
1762 asprintf( &psz_tmp, "%d", p_instance->i_index + 1 );
1763 vlm_MessageAdd( msg_instance, vlm_MessageNew( "playlistindex", psz_tmp ) );
1765 vlm_MessageAdd( msg_child, msg_instance );
1772 else if( schedule != NULL )
1776 vlm_message_t *msg_schedule;
1777 vlm_message_t *msg_child;
1780 msg = vlm_MessageNew( "show", NULL );
1782 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, 0 ) );
1784 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1786 vlm_MessageAdd( msg_schedule,
1787 vlm_MessageNew( "enabled", schedule->b_enabled ?
1790 #if !defined( UNDER_CE )
1791 if( schedule->i_date != 0 )
1794 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1797 #ifdef HAVE_LOCALTIME_R
1798 localtime_r( &i_time, &date);
1800 struct tm *p_date = localtime( &i_time );
1804 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1805 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1806 date.tm_hour, date.tm_min, date.tm_sec );
1808 vlm_MessageAdd( msg_schedule,
1809 vlm_MessageNew( "date", psz_date ) );
1814 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1817 if( schedule->i_period != 0 )
1819 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1822 date.tm_sec = (int)( i_time % 60 );
1823 i_time = i_time / 60;
1824 date.tm_min = (int)( i_time % 60 );
1825 i_time = i_time / 60;
1826 date.tm_hour = (int)( i_time % 24 );
1827 i_time = i_time / 24;
1828 date.tm_mday = (int)( i_time % 30 );
1829 i_time = i_time / 30;
1830 /* okay, okay, months are not always 30 days long */
1831 date.tm_mon = (int)( i_time % 12 );
1832 i_time = i_time / 12;
1833 date.tm_year = (int)i_time;
1835 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1836 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1838 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1842 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1844 #endif /* UNDER_CE */
1846 sprintf( buffer, "%d", schedule->i_repeat );
1847 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1850 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", 0) );
1852 for( i = 0; i < schedule->i_command; i++ )
1854 vlm_MessageAdd( msg_child,
1855 vlm_MessageNew( schedule->command[i], NULL ) );
1862 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1866 vlm_message_t *msg_child;
1867 int i_vod = 0, i_broadcast = 0;
1870 for( i = 0; i < vlm->i_media; i++ )
1872 if( vlm->media[i]->i_type == VOD_TYPE )
1878 asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1880 msg = vlm_MessageNew( "show", NULL );
1881 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "media", psz_count ) );
1884 for( i = 0; i < vlm->i_media; i++ )
1886 vlm_media_t *m = vlm->media[i];
1887 vlm_message_t *msg_media, *msg_instance;
1889 msg_media = vlm_MessageAdd( msg_child,
1890 vlm_MessageNew( m->psz_name, 0 ) );
1892 vlm_MessageAdd( msg_media,
1893 vlm_MessageNew( "type", m->i_type == VOD_TYPE ?
1894 "vod" : "broadcast" ) );
1896 vlm_MessageAdd( msg_media,
1897 vlm_MessageNew( "enabled", m->b_enabled ?
1900 if( m->i_type == VOD_TYPE && m->psz_mux )
1901 vlm_MessageAdd( msg_media,
1902 vlm_MessageNew( "mux", m->psz_mux ) );
1904 msg_instance = vlm_MessageAdd( msg_media,
1905 vlm_MessageNew( "instances", 0 ) );
1907 for( j = 0; j < m->i_instance; j++ )
1909 vlm_media_instance_t *p_instance = m->instance[j];
1912 if( !p_instance->p_input ) val.i_int = END_S;
1913 else var_Get( p_instance->p_input, "state", &val );
1915 vlm_MessageAdd( msg_instance,
1916 vlm_MessageNew( p_instance->psz_name ?
1917 p_instance->psz_name : "default",
1918 val.i_int == PLAYING_S ? "playing" :
1919 val.i_int == PAUSE_S ? "paused" :
1927 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1931 vlm_message_t *msg_child;
1933 msg = vlm_MessageNew( "show", NULL );
1934 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", NULL ) );
1936 for( i = 0; i < vlm->i_schedule; i++ )
1938 vlm_schedule_t *s = vlm->schedule[i];
1939 vlm_message_t *msg_schedule;
1940 mtime_t i_time, i_next_date;
1942 msg_schedule = vlm_MessageAdd( msg_child,
1943 vlm_MessageNew( s->psz_name, 0 ) );
1944 vlm_MessageAdd( msg_schedule,
1945 vlm_MessageNew( "enabled", s->b_enabled ?
1948 /* calculate next date */
1949 i_time = vlm_Date();
1950 i_next_date = s->i_date;
1952 if( s->i_period != 0 )
1955 while( s->i_date + j * s->i_period <= i_time &&
1961 i_next_date = s->i_date + j * s->i_period;
1964 if( i_next_date > i_time )
1966 time_t i_date = (time_t) (i_next_date / 1000000) ;
1968 #if !defined( UNDER_CE )
1971 ctime_r( &i_date, psz_date );
1973 char *psz_date = ctime( &i_date );
1976 vlm_MessageAdd( msg_schedule,
1977 vlm_MessageNew( "next launch", psz_date ) );
1985 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
1987 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1988 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1990 vlm_MessageAdd( show1, show2->child[0] );
1992 /* We must destroy the parent node "show" of show2
1993 * and not the children */
1994 free( show2->psz_name );
2002 return vlm_MessageNew( "show", NULL );
2006 static vlm_message_t *vlm_Help( vlm_t *vlm, char *psz_filter )
2008 vlm_message_t *message, *message_child;
2010 #define MessageAdd( a ) \
2011 vlm_MessageAdd( message, vlm_MessageNew( a, NULL ) );
2012 #define MessageAddChild( a ) \
2013 vlm_MessageAdd( message_child, vlm_MessageNew( a, NULL ) );
2015 if( psz_filter == NULL )
2017 message = vlm_MessageNew( "help", NULL );
2019 message_child = MessageAdd( "Commands Syntax:" );
2020 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
2021 MessageAddChild( "setup (name) (properties)" );
2022 MessageAddChild( "show [(name)|media|schedule]" );
2023 MessageAddChild( "del (name)|all|media|schedule" );
2024 MessageAddChild( "control (name) [instance_name] (command)" );
2025 MessageAddChild( "save (config_file)" );
2026 MessageAddChild( "export" );
2027 MessageAddChild( "load (config_file)" );
2029 message_child = MessageAdd( "Media Proprieties Syntax:" );
2030 MessageAddChild( "input (input_name)" );
2031 MessageAddChild( "inputdel (input_name)|all" );
2032 MessageAddChild( "inputdeln input_number" );
2033 MessageAddChild( "output (output_name)" );
2034 MessageAddChild( "option (option_name)[=value]" );
2035 MessageAddChild( "enabled|disabled" );
2036 MessageAddChild( "loop|unloop (broadcast only)" );
2037 MessageAddChild( "mux (mux_name)" );
2039 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
2040 MessageAddChild( "enabled|disabled" );
2041 MessageAddChild( "append (command_until_rest_of_the_line)" );
2042 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
2044 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
2045 "(days)-(hours):(minutes):(seconds)" );
2046 MessageAddChild( "repeat (number_of_repetitions)" );
2048 message_child = MessageAdd( "Control Commands Syntax:" );
2049 MessageAddChild( "play" );
2050 MessageAddChild( "pause" );
2051 MessageAddChild( "stop" );
2052 MessageAddChild( "seek (percentage)" );
2057 return vlm_MessageNew( "help", NULL );
2060 /*****************************************************************************
2061 * Config handling functions
2062 *****************************************************************************/
2063 static int Load( vlm_t *vlm, char *file )
2068 while( *pf != '\0' )
2070 vlm_message_t *message = NULL;
2073 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
2078 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
2082 if( pf[i_end] == '\n' ) i_end++;
2085 if( *pf && ExecuteCommand( vlm, pf, &message ) )
2089 if( message->psz_value )
2090 msg_Err( vlm, "Load error on line %d: %s: %s",
2091 i_line, message->psz_name, message->psz_value );
2092 vlm_MessageDelete( message );
2096 if( message ) vlm_MessageDelete( message );
2105 static char *Save( vlm_t *vlm )
2108 char psz_header[] = "\n"
2109 "# VLC media player VLM command batch\n"
2110 "# http://www.videolan.org/vlc/\n\n" ;
2113 int i_length = strlen( psz_header );
2115 for( i = 0; i < vlm->i_media; i++ )
2117 vlm_media_t *media = vlm->media[i];
2119 if( media->i_type == VOD_TYPE )
2121 i_length += strlen( "new vod " ) + strlen(media->psz_name);
2125 i_length += strlen( "new broadcast " ) + strlen(media->psz_name);
2128 if( media->b_enabled == VLC_TRUE )
2130 i_length += strlen( "enabled" );
2134 i_length += strlen( "disabled" );
2137 if( media->b_loop == VLC_TRUE )
2139 i_length += strlen( " loop\n" );
2143 i_length += strlen( "\n" );
2146 for( j = 0; j < media->i_input; j++ )
2148 i_length += strlen( "setup input \"\"\n" ) +
2149 strlen( media->psz_name ) + strlen( media->input[j] );
2152 if( media->psz_output != NULL )
2154 i_length += strlen(media->psz_name) + strlen(media->psz_output) +
2155 strlen( "setup output \n" );
2158 for( j=0 ; j < media->i_option ; j++ )
2160 i_length += strlen(media->psz_name) + strlen(media->option[j]) +
2161 strlen("setup option \n");
2165 for( i = 0; i < vlm->i_schedule; i++ )
2167 vlm_schedule_t *schedule = vlm->schedule[i];
2169 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
2171 if( schedule->b_enabled == VLC_TRUE )
2173 i_length += strlen( "date //-:: enabled\n" ) + 14;
2177 i_length += strlen( "date //-:: disabled\n" ) + 14;
2181 if( schedule->i_period != 0 )
2183 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
2184 strlen( "period //-::\n" ) + 14;
2187 if( schedule->i_repeat >= 0 )
2191 sprintf( buffer, "%d", schedule->i_repeat );
2192 i_length += strlen( "setup repeat \n" ) +
2193 strlen( schedule->psz_name ) + strlen( buffer );
2200 for( j = 0; j < schedule->i_command; j++ )
2202 i_length += strlen( "setup append \n" ) +
2203 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
2208 /* Don't forget the '\0' */
2210 /* now we have the length of save */
2212 p = save = malloc( i_length );
2215 p += sprintf( p, "%s", psz_header );
2217 /* finally we can write in it */
2218 for( i = 0; i < vlm->i_media; i++ )
2220 vlm_media_t *media = vlm->media[i];
2222 if( media->i_type == VOD_TYPE )
2224 p += sprintf( p, "new %s vod ", media->psz_name);
2228 p += sprintf( p, "new %s broadcast ", media->psz_name);
2231 if( media->b_enabled == VLC_TRUE )
2233 p += sprintf( p, "enabled" );
2237 p += sprintf( p, "disabled" );
2240 if( media->b_loop == VLC_TRUE )
2242 p += sprintf( p, " loop\n" );
2246 p += sprintf( p, "\n" );
2249 for( j = 0; j < media->i_input; j++ )
2251 p += sprintf( p, "setup %s input \"%s\"\n", media->psz_name,
2255 if( media->psz_output != NULL )
2257 p += sprintf( p, "setup %s output %s\n", media->psz_name,
2258 media->psz_output );
2261 for( j = 0; j < media->i_option; j++ )
2263 p += sprintf( p, "setup %s option %s\n", media->psz_name,
2268 /* and now, the schedule scripts */
2269 #if !defined( UNDER_CE )
2270 for( i = 0; i < vlm->i_schedule; i++ )
2272 vlm_schedule_t *schedule = vlm->schedule[i];
2274 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
2276 #ifdef HAVE_LOCALTIME_R
2277 localtime_r( &i_time, &date);
2279 struct tm *p_date = localtime( &i_time );
2283 p += sprintf( p, "new %s schedule ", schedule->psz_name);
2285 if( schedule->b_enabled == VLC_TRUE )
2287 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
2288 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2289 date.tm_hour, date.tm_min, date.tm_sec );
2293 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
2294 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2295 date.tm_hour, date.tm_min, date.tm_sec);
2298 if( schedule->i_period != 0 )
2300 p += sprintf( p, "setup %s ", schedule->psz_name );
2302 i_time = (time_t) ( schedule->i_period / 1000000 );
2304 date.tm_sec = (int)( i_time % 60 );
2305 i_time = i_time / 60;
2306 date.tm_min = (int)( i_time % 60 );
2307 i_time = i_time / 60;
2308 date.tm_hour = (int)( i_time % 24 );
2309 i_time = i_time / 24;
2310 date.tm_mday = (int)( i_time % 30 );
2311 i_time = i_time / 30;
2312 /* okay, okay, months are not always 30 days long */
2313 date.tm_mon = (int)( i_time % 12 );
2314 i_time = i_time / 12;
2315 date.tm_year = (int)i_time;
2317 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
2318 date.tm_year, date.tm_mon, date.tm_mday,
2319 date.tm_hour, date.tm_min, date.tm_sec);
2322 if( schedule->i_repeat >= 0 )
2324 p += sprintf( p, "setup %s repeat %d\n",
2325 schedule->psz_name, schedule->i_repeat );
2329 p += sprintf( p, "\n" );
2332 for( j = 0; j < schedule->i_command; j++ )
2334 p += sprintf( p, "setup %s append %s\n",
2335 schedule->psz_name, schedule->command[j] );
2339 #endif /* UNDER_CE */
2344 /*****************************************************************************
2346 *****************************************************************************/
2347 int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
2348 const char *psz_id, int i_query, va_list args )
2350 vlm_t *vlm = (vlm_t *)p_private;
2351 int i, i_ret = VLC_EGENERIC;
2353 if( !p_private || !p_vod_media ) return VLC_EGENERIC;
2355 vlc_mutex_lock( &vlm->lock );
2358 for( i = 0; i < vlm->i_media; i++ )
2360 if( p_vod_media == vlm->media[i]->vod_media ) break;
2363 if( i == vlm->i_media )
2365 vlc_mutex_unlock( &vlm->lock );
2366 return VLC_EGENERIC;
2371 case VOD_MEDIA_PLAY:
2372 vlm->media[i]->psz_vod_output = (char *)va_arg( args, char * );
2373 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "play", 0 );
2374 vlm->media[i]->psz_vod_output = 0;
2377 case VOD_MEDIA_PAUSE:
2378 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "pause", 0 );
2381 case VOD_MEDIA_STOP:
2382 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "stop", 0 );
2385 case VOD_MEDIA_SEEK:
2387 double f_pos = (double)va_arg( args, double );
2389 lldiv_t div = lldiv( f_pos * 10000000, 10000000 );
2390 sprintf( psz_pos, I64Fd".%07u", div.quot, (unsigned int) div.rem );
2391 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "seek", psz_pos);
2395 case VOD_MEDIA_REWIND:
2397 double f_scale = (double)va_arg( args, double );
2399 lldiv_t div = lldiv( f_scale * 10000000, 10000000 );
2400 sprintf( psz_scale, I64Fd".%07u", div.quot, (unsigned int) div.rem );
2401 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "rewind", psz_scale );
2405 case VOD_MEDIA_FORWARD:
2407 double f_scale = (double)va_arg( args, double );
2409 lldiv_t div = lldiv( f_scale * 10000000, 10000000 );
2410 sprintf( psz_scale, I64Fd".%07u", div.quot, (unsigned int) div.rem );
2411 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "forward", psz_scale );
2419 vlc_mutex_unlock( &vlm->lock );
2425 /*****************************************************************************
2427 *****************************************************************************/
2428 static int Manage( vlc_object_t* p_object )
2430 vlm_t *vlm = (vlm_t*)p_object;
2432 mtime_t i_lastcheck;
2435 i_lastcheck = vlm_Date();
2439 while( !vlm->b_die )
2441 char **ppsz_scheduled_commands = NULL;
2442 int i_scheduled_commands = 0;
2443 vlc_mutex_lock( &vlm->lock );
2445 /* destroy the inputs that wants to die, and launch the next input */
2446 for( i = 0; i < vlm->i_media; i++ )
2448 vlm_media_t *p_media = vlm->media[i];
2450 for( j = 0; j < p_media->i_instance; j++ )
2452 vlm_media_instance_t *p_instance = p_media->instance[j];
2454 if( !p_instance->p_input ||
2455 ( !p_instance->p_input->b_eof &&
2456 !p_instance->p_input->b_error ) ) continue;
2458 input_StopThread( p_instance->p_input );
2459 input_DestroyThread( p_instance->p_input );
2460 vlc_object_detach( p_instance->p_input );
2461 vlc_object_destroy( p_instance->p_input );
2463 p_instance->i_index++;
2464 if( p_instance->i_index == p_media->i_input &&
2465 p_media->b_loop ) p_instance->i_index = 0;
2467 if( p_instance->i_index < p_media->i_input )
2469 /* FIXME, find a way to select the right instance */
2471 sprintf( buffer, "%d", p_instance->i_index );
2472 vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2477 if( vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2478 "stop", 0 ) == VLC_SUCCESS )
2480 j--; /* the aray is one element smaller now. */
2487 i_time = vlm_Date();
2489 for( i = 0; i < vlm->i_schedule; i++ )
2491 mtime_t i_real_date = vlm->schedule[i]->i_date;
2493 if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2495 if( vlm->schedule[i]->i_date == 0 ) // now !
2497 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2498 i_real_date = i_time;
2500 else if( vlm->schedule[i]->i_period != 0 )
2503 while( vlm->schedule[i]->i_date + j *
2504 vlm->schedule[i]->i_period <= i_lastcheck &&
2505 ( vlm->schedule[i]->i_repeat > j ||
2506 vlm->schedule[i]->i_repeat == -1 ) )
2511 i_real_date = vlm->schedule[i]->i_date + j *
2512 vlm->schedule[i]->i_period;
2515 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2517 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2519 TAB_APPEND( i_scheduled_commands,
2520 ppsz_scheduled_commands,
2521 strdup(vlm->schedule[i]->command[j] ) );
2526 while( i_scheduled_commands )
2528 vlm_message_t *message = NULL;
2529 char *psz_command = ppsz_scheduled_commands[0];
2530 ExecuteCommand( vlm, psz_command,&message );
2532 /* for now, drop the message */
2533 vlm_MessageDelete( message );
2534 TAB_REMOVE( i_scheduled_commands,
2535 ppsz_scheduled_commands,
2537 free( psz_command );
2540 i_lastcheck = i_time;
2542 vlc_mutex_unlock( &vlm->lock );
2550 #else /* ENABLE_VLM */
2552 /* We just define an empty wrapper */
2553 vlm_t *__vlm_New( vlc_object_t *a )
2555 msg_Err( a, "VideoLAN manager support is disabled" );
2558 void vlm_Delete( vlm_t *a ){}
2559 int vlm_ExecuteCommand( vlm_t *a, char *b, vlm_message_t **c ){ return -1; }
2560 void vlm_MessageDelete( vlm_message_t *a ){}
2561 vlm_media_t *vlm_MediaNew( vlm_t *a, char *b, int c ){ return NULL; }
2562 vlm_media_t *vlm_MediaSearch (vlm_t *a, const char *b ) { return NULL; }
2563 void vlm_MediaDelete( vlm_t *a, vlm_media_t *b, char *c ){}
2564 int vlm_MediaSetup( vlm_t *a, vlm_media_t *b, char *c, char *d ){ return -1; }
2565 int vlm_MediaControl( vlm_t *a, vlm_media_t *b, char *c, char *d, char *e )
2567 vlm_schedule_t * vlm_ScheduleNew( vlm_t *a, char *b ){ return NULL; }
2568 void vlm_ScheduleDelete( vlm_t *a, vlm_schedule_t *b, char *c ){}
2569 int vlm_ScheduleSetup( vlm_schedule_t *a, char *b, char *c ){ return -1; }
2570 int vlm_MediaVodControl( void *a, vod_media_t *b, char *c, int d, va_list e )
2572 int vlm_Save( vlm_t *a, char *b ){ return -1; }
2573 int vlm_Load( vlm_t *a, char *b ){ return -1; }
2575 #endif /* ENABLE_VLM */