1 /*****************************************************************************
2 * vlm.c: VLM interface plugin
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
30 #include <ctype.h> /* tolower() */
37 #include <vlc/input.h>
40 # include <time.h> /* ctime() */
41 # include <sys/timeb.h> /* ftime() */
48 if( p ) { free( p ); (p) = NULL; }
50 /*****************************************************************************
52 *****************************************************************************/
53 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_t *, vlm_schedule_t *, char * );
54 static vlm_message_t *vlm_Help( vlm_t *, char * );
56 static vlm_media_t *vlm_MediaSearch ( vlm_t *, char * );
57 static vlm_media_instance_t *vlm_MediaInstanceSearch( vlm_t *, vlm_media_t *, char * );
59 static vlm_message_t *vlm_MessageNew( char *, const char *, ... );
60 static vlm_message_t *vlm_MessageAdd( vlm_message_t *, vlm_message_t * );
62 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *, char * );
64 static char *Save( vlm_t * );
65 static int Load( vlm_t *, char * );
66 static int ExecuteCommand( vlm_t *, char *, vlm_message_t ** );
67 static int Manage( vlc_object_t * );
69 /*****************************************************************************
71 *****************************************************************************/
72 vlm_t *__vlm_New ( vlc_object_t *p_this )
78 /* to be sure to avoid multiple creation */
79 var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX );
80 var_Get( p_this->p_libvlc, "vlm_mutex", &lockval );
81 vlc_mutex_lock( lockval.p_address );
83 if( !(p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE )) )
85 msg_Info( p_this, "creating vlm" );
86 if( ( p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM ) ) == NULL )
88 vlc_mutex_unlock( lockval.p_address );
92 vlc_mutex_init( p_this->p_vlc, &p_vlm->lock );
96 p_vlm->i_schedule = 0;
97 p_vlm->schedule = NULL;
99 vlc_object_yield( p_vlm );
100 vlc_object_attach( p_vlm, p_this->p_vlc );
102 vlc_mutex_unlock( lockval.p_address );
104 if( vlc_thread_create( p_vlm, "vlm thread",
105 Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
107 vlc_mutex_destroy( &p_vlm->lock );
108 vlc_object_destroy( p_vlm );
112 /* Try loading the vlm conf file given by --vlm-conf */
113 psz_vlmconf = config_GetPsz( p_vlm, "vlm-conf" );
115 if( psz_vlmconf && *psz_vlmconf )
117 vlm_message_t *p_message = NULL;
118 char *psz_buffer = NULL;
120 msg_Dbg( p_this, "loading vlm conf ..." );
121 asprintf(&psz_buffer, "load %s", psz_vlmconf );
124 msg_Dbg( p_this, psz_buffer);
125 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) ){
126 msg_Warn( p_this, "error while loading the vlm conf file" );
137 /*****************************************************************************
139 *****************************************************************************/
140 void vlm_Delete( vlm_t *p_vlm )
144 var_Get( p_vlm->p_libvlc, "vlm_mutex", &lockval );
145 vlc_mutex_lock( lockval.p_address );
147 vlc_object_release( p_vlm );
149 if( p_vlm->i_refcount > 0 )
151 vlc_mutex_unlock( lockval.p_address );
155 p_vlm->b_die = VLC_TRUE;
156 vlc_thread_join( p_vlm );
158 vlc_mutex_destroy( &p_vlm->lock );
160 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0], NULL );
161 FREE( p_vlm->media );
163 while( p_vlm->i_schedule ) vlm_ScheduleDelete( p_vlm,
164 p_vlm->schedule[0], NULL );
165 FREE( p_vlm->schedule );
167 vlc_object_detach( p_vlm );
168 vlc_object_destroy( p_vlm );
169 vlc_mutex_unlock( lockval.p_address );
172 /*****************************************************************************
173 * vlm_ExecuteCommand:
174 *****************************************************************************/
175 int vlm_ExecuteCommand( vlm_t *p_vlm, char *psz_command,
176 vlm_message_t **pp_message)
180 vlc_mutex_lock( &p_vlm->lock );
181 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
182 vlc_mutex_unlock( &p_vlm->lock );
187 /*****************************************************************************
189 *****************************************************************************/
190 int vlm_Save( vlm_t *p_vlm, char *psz_file )
195 if( !p_vlm || !psz_file ) return 1;
197 file = fopen( psz_file, "wt" );
198 if( file == NULL ) return 1;
200 psz_save = Save( p_vlm );
201 if( psz_save == NULL )
206 fwrite( psz_save, strlen( psz_save ), 1, file );
213 /*****************************************************************************
215 *****************************************************************************/
216 int vlm_Load( vlm_t *p_vlm, char *psz_file )
222 if( !p_vlm || !psz_file ) return 1;
224 file = fopen( psz_file, "r" );
225 if( file == NULL ) return 1;
227 if( fseek( file, 0, SEEK_END) != 0 ) return 2;
229 i_size = ftell( file );
230 fseek( file, 0, SEEK_SET);
231 psz_buffer = malloc( i_size + 1 );
237 fread( psz_buffer, 1, i_size, file);
238 psz_buffer[ i_size ] = '\0';
239 if( Load( p_vlm, psz_buffer ) )
251 /*****************************************************************************
253 *****************************************************************************/
254 static char *FindEndCommand( char *psz )
256 char *psz_sent = psz;
263 while( ( *psz_sent != '\"' ) && ( *psz_sent != '\0' ) )
265 if( *psz_sent == '\'' )
267 psz_sent = FindEndCommand( psz_sent );
268 if( psz_sent == NULL ) return NULL;
273 if( *psz_sent == '\"' )
279 /* *psz_sent == '\0' -> number of " is incorrect */
287 while( ( *psz_sent != '\'' ) && ( *psz_sent != '\0' ) )
289 if( *psz_sent == '\"' )
291 psz_sent = FindEndCommand( psz_sent );
292 if( psz_sent == NULL ) return NULL;
297 if( *psz_sent == '\'' )
303 /* *psz_sent == '\0' -> number of " is incorrect */
308 default: /* now we can look for spaces */
309 while( ( *psz_sent != ' ' ) && ( *psz_sent != '\0' ) )
311 if( ( *psz_sent == '\'' ) || ( *psz_sent == '\"' ) )
313 psz_sent = FindEndCommand( psz_sent );
322 /*****************************************************************************
323 * ExecuteCommand: The main state machine
324 *****************************************************************************
325 * Execute a command which ends with '\0' (string)
326 *****************************************************************************/
327 static int ExecuteCommand( vlm_t *p_vlm, char *psz_command,
328 vlm_message_t **pp_message )
331 char **ppsz_command = NULL;
332 char *psz_cmd = psz_command;
333 vlm_message_t *p_message = NULL;
336 /* First, parse the line and cut it */
337 while( *psz_cmd != '\0' )
340 if( *psz_cmd == ' ' || *psz_cmd == '\t' )
349 /* support for comments */
350 if( i_command == 0 && *psz_cmd == '#')
352 p_message = vlm_MessageNew( "", NULL );
356 psz_temp = FindEndCommand( psz_cmd );
358 if( psz_temp == NULL ) goto error;
360 i_temp = psz_temp - psz_cmd;
362 ppsz_command = realloc( ppsz_command, (i_command + 1) *
364 ppsz_command[ i_command ] = malloc( (i_temp + 1) * sizeof(char) );
365 strncpy( ppsz_command[ i_command ], psz_cmd, i_temp );
366 ppsz_command[ i_command ][ i_temp ] = '\0';
375 * And then Interpret it
380 p_message = vlm_MessageNew( "", NULL );
384 if( !strcmp(ppsz_command[0], "new") )
388 /* Check the number of arguments */
389 if( i_command < 3 ) goto syntax_error;
392 if( !strcmp(ppsz_command[2], "vod") )
396 else if( !strcmp(ppsz_command[2], "broadcast") )
398 i_type = BROADCAST_TYPE;
400 else if( !strcmp(ppsz_command[2], "schedule") )
402 i_type = SCHEDULE_TYPE;
407 vlm_MessageNew( "new", "%s: Choose between vod, "
408 "broadcast or schedule", ppsz_command[1] );
412 /* Check for forbidden media names */
413 if( !strcmp(ppsz_command[1], "all") ||
414 !strcmp(ppsz_command[1], "media") ||
415 !strcmp(ppsz_command[1], "schedule") )
417 p_message = vlm_MessageNew( "new", "\"all\", \"media\" and "
418 "\"schedule\" are reserved names" );
422 /* Check the name is not already in use */
423 if( vlm_ScheduleSearch( p_vlm, ppsz_command[1] ) ||
424 vlm_MediaSearch( p_vlm, ppsz_command[1] ) )
426 p_message = vlm_MessageNew( "new", "%s: Name already in use",
432 if( i_type == SCHEDULE_TYPE )
434 vlm_schedule_t *p_schedule;
435 p_schedule = vlm_ScheduleNew( p_vlm, ppsz_command[1] );
438 p_message = vlm_MessageNew( "new", "could not create schedule" );
446 vlm_media_t *p_media;
447 p_media = vlm_MediaNew( p_vlm, ppsz_command[1], i_type );
450 p_message = vlm_MessageNew( "new", "could not create media" );
457 p_message = vlm_MessageNew( "new", NULL );
461 /* Properties will be dealt with later on */
464 else if( !strcmp(ppsz_command[0], "setup") )
466 if( i_command < 2 ) goto syntax_error;
468 /* Properties will be dealt with later on */
471 else if( !strcmp(ppsz_command[0], "del") )
473 vlm_media_t *p_media;
474 vlm_schedule_t *p_schedule;
476 if( i_command < 2 ) goto syntax_error;
478 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
479 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
481 if( p_schedule != NULL )
483 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
485 else if( p_media != NULL )
487 vlm_MediaDelete( p_vlm, p_media, NULL );
489 else if( !strcmp(ppsz_command[1], "media") )
491 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
494 else if( !strcmp(ppsz_command[1], "schedule") )
496 while( p_vlm->i_schedule )
497 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
499 else if( !strcmp(ppsz_command[1], "all") )
501 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
504 while( p_vlm->i_schedule )
505 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
509 p_message = vlm_MessageNew( "del", "%s: media unknown",
514 p_message = vlm_MessageNew( "del", NULL );
518 else if( !strcmp(ppsz_command[0], "show") )
520 vlm_media_t *p_media;
521 vlm_schedule_t *p_schedule;
525 p_message = vlm_Show( p_vlm, NULL, NULL, NULL );
528 else if( i_command > 2 ) goto syntax_error;
530 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
531 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
533 if( p_schedule != NULL )
535 p_message = vlm_Show( p_vlm, NULL, p_schedule, NULL );
537 else if( p_media != NULL )
539 p_message = vlm_Show( p_vlm, p_media, NULL, NULL );
543 p_message = vlm_Show( p_vlm, NULL, NULL, ppsz_command[1] );
549 else if( !strcmp(ppsz_command[0], "help") )
551 if( i_command != 1 ) goto syntax_error;
553 p_message = vlm_Help( p_vlm, NULL );
557 else if( !strcmp(ppsz_command[0], "control") )
559 vlm_media_t *p_media;
561 if( i_command < 3 ) goto syntax_error;
563 if( !(p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] ) ) )
565 p_message = vlm_MessageNew( "control", "%s: media unknown",
571 char *psz_command, *psz_arg = 0, *psz_instance = 0;
574 if( strcmp( ppsz_command[2], "play" ) &&
575 strcmp( ppsz_command[2], "stop" ) &&
576 strcmp( ppsz_command[2], "pause" ) &&
577 strcmp( ppsz_command[2], "seek" ) )
580 psz_instance = ppsz_command[2];
582 if( i_command < 4 ) goto syntax_error;
585 psz_command = ppsz_command[i_index];
587 if( i_command >= i_index + 2 ) psz_arg = ppsz_command[i_index + 1];
589 vlm_MediaControl( p_vlm, p_media, psz_instance, psz_command,
591 p_message = vlm_MessageNew( "control", NULL );
596 else if( !strcmp(ppsz_command[0], "save") )
598 if( i_command != 2 ) goto syntax_error;
600 if( vlm_Save( p_vlm, ppsz_command[1] ) )
602 p_message = vlm_MessageNew( "save", "Unable to save to file" );
607 p_message = vlm_MessageNew( "save", NULL );
612 else if( !strcmp(ppsz_command[0], "load") )
614 if( i_command != 2 ) goto syntax_error;
616 switch( vlm_Load( p_vlm, ppsz_command[1] ) )
619 p_message = vlm_MessageNew( "load", NULL );
622 p_message = vlm_MessageNew( "load", "read file error" );
626 vlm_MessageNew( "load", "error while loading file" );
630 vlm_MessageNew( "load", "Unable to load from file" );
637 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
641 /* Common code between "new" and "setup" */
642 if( !strcmp(ppsz_command[0], "new") ||
643 !strcmp(ppsz_command[0], "setup") )
645 int i_command_start = strcmp(ppsz_command[0], "new") ? 2 : 3;
646 vlm_media_t *p_media;
647 vlm_schedule_t *p_schedule;
649 if( i_command < i_command_start ) goto syntax_error;
651 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
652 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
654 if( !p_media && !p_schedule )
656 p_message = vlm_MessageNew( ppsz_command[0], "%s unknown",
661 if( p_schedule != NULL )
663 for( i = i_command_start ; i < i_command ; i++ )
665 if( !strcmp( ppsz_command[i], "enabled" ) ||
666 !strcmp( ppsz_command[i], "disabled" ) )
668 vlm_ScheduleSetup( p_schedule, ppsz_command[i], NULL );
671 /* Beware: everything behind append is considered as
673 else if( !strcmp( ppsz_command[i], "append" ) )
675 if( ++i >= i_command ) break;
677 for( j = i + 1; j < i_command; j++ )
680 realloc( ppsz_command[i], strlen(ppsz_command[i]) +
681 strlen(ppsz_command[j]) + 1 + 1 );
682 strcat( ppsz_command[i], " " );
683 strcat( ppsz_command[i], ppsz_command[j] );
686 vlm_ScheduleSetup( p_schedule, ppsz_command[i - 1],
692 if( i + 1 >= i_command && !strcmp(ppsz_command[0], "new") )
694 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
696 vlm_MessageNew( ppsz_command[0],
697 "Wrong properties syntax" );
700 else if( i + 1 >= i_command )
703 vlm_MessageNew( ppsz_command[0],
704 "Wrong properties syntax" );
708 vlm_ScheduleSetup( p_schedule, ppsz_command[i],
715 else if( p_media != NULL )
717 for( i = i_command_start ; i < i_command ; i++ )
719 if( !strcmp( ppsz_command[i], "enabled" ) ||
720 !strcmp( ppsz_command[i], "disabled" ) )
722 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
724 else if( i + 1 >= i_command &&
725 !strcmp( ppsz_command[i], "mux") )
727 if( p_media->i_type != VOD_TYPE )
729 p_message = vlm_MessageNew( ppsz_command[0],
730 "mux only available for broadcast" );
734 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
739 else if( !strcmp( ppsz_command[i], "loop" ) ||
740 !strcmp( ppsz_command[i], "unloop" ) )
742 if( p_media->i_type != BROADCAST_TYPE )
744 p_message = vlm_MessageNew( ppsz_command[0],
745 "loop only available for broadcast" );
749 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
754 if( i + 1 >= i_command &&
755 !strcmp(ppsz_command[0], "new") )
757 vlm_MediaDelete( p_vlm, p_media, NULL );
759 vlm_MessageNew( ppsz_command[0],
760 "Wrong properties syntax" );
763 else if( i + 1 >= i_command )
766 vlm_MessageNew( ppsz_command[0],
767 "Wrong properties syntax" );
771 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
778 p_message = vlm_MessageNew( ppsz_command[0], NULL );
783 for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
784 FREE( ppsz_command );
785 *pp_message = p_message;
790 p_message = vlm_MessageNew( ppsz_command[0], "Wrong command syntax" );
793 for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
794 FREE( ppsz_command );
795 *pp_message = p_message;
800 static vlm_media_t *vlm_MediaSearch( vlm_t *vlm, char *psz_name )
804 for( i = 0; i < vlm->i_media; i++ )
806 if( strcmp( psz_name, vlm->media[i]->psz_name ) == 0 )
808 return vlm->media[i];
815 /*****************************************************************************
817 *****************************************************************************/
818 static vlm_media_instance_t *
819 vlm_MediaInstanceSearch( vlm_t *vlm, vlm_media_t *media, char *psz_name )
823 for( i = 0; i < media->i_instance; i++ )
825 if( ( !psz_name && !media->instance[i]->psz_name ) ||
826 ( psz_name && media->instance[i]->psz_name &&
827 !strcmp( psz_name, media->instance[i]->psz_name ) ) )
829 return media->instance[i];
836 vlm_media_t *vlm_MediaNew( vlm_t *vlm, char *psz_name, int i_type )
838 vlm_media_t *media = malloc( sizeof( vlm_media_t ) );
842 msg_Err( vlm, "out of memory" );
846 /* Check if we need to load the VOD server */
847 if( i_type == VOD_TYPE && !vlm->i_vod )
849 vlm->vod = vlc_object_create( vlm, VLC_OBJECT_VOD );
850 vlc_object_attach( vlm->vod, vlm );
851 vlm->vod->p_module = module_Need( vlm->vod, "vod server", 0, 0 );
852 if( !vlm->vod->p_module )
854 msg_Err( vlm, "cannot find vod server" );
855 vlc_object_detach( vlm->vod );
856 vlc_object_destroy( vlm->vod );
862 vlm->vod->p_data = vlm;
863 vlm->vod->pf_media_control = vlm_MediaVodControl;
866 if( i_type == VOD_TYPE ) vlm->i_vod++;
868 media->psz_name = strdup( psz_name );
869 media->b_enabled = VLC_FALSE;
870 media->b_loop = VLC_FALSE;
871 media->vod_media = NULL;
872 media->psz_vod_output = NULL;
873 media->psz_mux = NULL;
876 media->psz_output = NULL;
878 media->option = NULL;
879 media->i_type = i_type;
880 media->i_instance = 0;
881 media->instance = NULL;
883 media->item.psz_uri = strdup( psz_name );
884 vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
886 TAB_APPEND( vlm->i_media, vlm->media, media );
891 /* for now, simple delete. After, del with options (last arg) */
892 void vlm_MediaDelete( vlm_t *vlm, vlm_media_t *media, char *psz_name )
894 if( media == NULL ) return;
896 while( media->i_instance )
898 vlm_media_instance_t *p_instance = media->instance[0];
899 vlm_MediaControl( vlm, media, p_instance->psz_name, "stop", 0 );
902 TAB_REMOVE( vlm->i_media, vlm->media, media );
904 if( media->i_type == VOD_TYPE )
906 vlm_MediaSetup( vlm, media, "disabled", 0 );
910 /* Check if we need to unload the VOD server */
911 if( media->i_type == VOD_TYPE && !vlm->i_vod )
913 module_Unneed( vlm->vod, vlm->vod->p_module );
914 vlc_object_detach( vlm->vod );
915 vlc_object_destroy( vlm->vod );
919 if( vlm->i_media == 0 && vlm->media ) free( vlm->media );
921 free( media->psz_name );
923 while( media->i_input-- ) free( media->input[media->i_input] );
924 if( media->input ) free( media->input );
926 if( media->psz_output ) free( media->psz_output );
927 if( media->psz_mux ) free( media->psz_mux );
929 while( media->i_option-- ) free( media->option[media->i_option] );
930 if( media->option ) free( media->option );
932 vlc_input_item_Clean( &media->item );
937 int vlm_MediaSetup( vlm_t *vlm, vlm_media_t *media, char *psz_cmd,
940 if( !psz_cmd) return VLC_EGENERIC;
942 if( !strcmp( psz_cmd, "loop" ) )
944 media->b_loop = VLC_TRUE;
946 else if( !strcmp( psz_cmd, "unloop" ) )
948 media->b_loop = VLC_FALSE;
950 else if( !strcmp( psz_cmd, "enabled" ) )
952 media->b_enabled = VLC_TRUE;
954 else if( !strcmp( psz_cmd, "disabled" ) )
956 media->b_enabled = VLC_FALSE;
958 else if( !strcmp( psz_cmd, "mux" ) )
960 if( media->psz_mux ) free( media->psz_mux );
961 media->psz_mux = NULL;
962 if( psz_value ) media->psz_mux = strdup( psz_value );
964 else if( !strcmp( psz_cmd, "input" ) )
968 if( psz_value != NULL && strlen(psz_value) > 1 &&
969 ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
970 ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
971 psz_value[ strlen(psz_value) - 1 ] == '\"' ) )
973 input = malloc( strlen(psz_value) - 1 );
975 memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
976 input[ strlen(psz_value) - 2 ] = '\0';
980 input = strdup( psz_value );
983 TAB_APPEND( media->i_input, media->input, input );
985 else if( !strcmp( psz_cmd, "inputdel" ) && !strcmp( psz_value, "all" ) )
987 while( media->i_input > 0 )
989 TAB_REMOVE( media->i_input, media->input, media->input[0] );
992 else if( !strcmp( psz_cmd, "inputdel" ) )
997 if( psz_value != NULL && strlen(psz_value) > 1 &&
998 ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
999 ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
1000 psz_value[ strlen(psz_value) - 1 ] == '\"' ) )
1002 input = malloc( strlen(psz_value) - 1 );
1004 memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
1005 input[ strlen(psz_value) - 2 ] = '\0';
1009 input = strdup( psz_value );
1012 for( i = 0; i < media->i_input; i++ )
1014 if( !strcmp( input, media->input[i] ) )
1016 TAB_REMOVE( media->i_input, media->input, media->input[i] );
1021 else if( !strcmp( psz_cmd, "inputdeln" ) )
1023 int index = atoi( psz_value );
1024 if( index > 0 && index <= media->i_input )
1026 TAB_REMOVE( media->i_input, media->input, media->input[index-1] );
1029 else if( !strcmp( psz_cmd, "output" ) )
1031 if( media->psz_output != NULL )
1033 free( media->psz_output );
1035 media->psz_output = strdup( psz_value );
1037 else if( !strcmp( psz_cmd, "option" ) )
1040 psz_option = strdup( psz_value );
1042 TAB_APPEND( media->i_option, media->option, psz_option );
1046 return VLC_EGENERIC;
1049 /* Check if we need to create/delete a vod media */
1050 if( media->i_type == VOD_TYPE )
1052 if( !media->b_enabled && media->vod_media )
1054 vlm->vod->pf_media_del( vlm->vod, media->vod_media );
1055 media->vod_media = 0;
1057 else if( media->b_enabled && !media->vod_media && media->i_input )
1059 /* Pre-parse the input */
1060 input_thread_t *p_input;
1064 vlc_input_item_Clean( &media->item );
1065 vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
1067 if( media->psz_output )
1068 asprintf( &psz_output, "%s:description", media->psz_output );
1070 asprintf( &psz_output, "#description" );
1072 media->item.psz_uri = strdup( media->input[0] );
1073 media->item.ppsz_options = malloc( sizeof( char* ) );
1074 asprintf( &media->item.ppsz_options[0], "sout=%s", psz_output);
1075 media->item.i_options = 1;
1076 for( i = 0; i < media->i_option; i++ )
1078 media->item.i_options++;
1079 media->item.ppsz_options =
1080 realloc( media->item.ppsz_options,
1081 media->item.i_options * sizeof( char* ) );
1082 media->item.ppsz_options[ media->item.i_options - 1 ] =
1083 strdup( media->option[i] );
1086 if( (p_input = input_CreateThread( vlm, &media->item ) ) )
1088 while( !p_input->b_eof && !p_input->b_error ) msleep( 100000 );
1090 input_StopThread( p_input );
1091 input_DestroyThread( p_input );
1092 vlc_object_detach( p_input );
1093 vlc_object_destroy( p_input );
1097 if( media->psz_mux )
1100 es_format_t es, *p_es = &es;
1103 sprintf( fourcc, "%4.4s", media->psz_mux );
1104 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
1105 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
1110 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
1113 vlm->vod->pf_media_new( vlm->vod, media->psz_name, &item );
1118 vlm->vod->pf_media_new( vlm->vod, media->psz_name,
1126 int vlm_MediaControl( vlm_t *vlm, vlm_media_t *media, char *psz_id,
1127 char *psz_command, char *psz_args )
1129 vlm_media_instance_t *p_instance;
1132 p_instance = vlm_MediaInstanceSearch( vlm, media, psz_id );
1134 if( !strcmp( psz_command, "play" ) )
1136 if( !media->b_enabled || media->i_input == 0 ) return 0;
1140 p_instance = malloc( sizeof(vlm_media_instance_t) );
1141 if( !p_instance ) return VLC_EGENERIC;
1142 memset( p_instance, 0, sizeof(vlm_media_instance_t) );
1143 vlc_input_item_Init( VLC_OBJECT(vlm), &p_instance->item );
1144 p_instance->p_input = NULL;
1146 if( media->psz_output != NULL || media->psz_vod_output != NULL )
1148 p_instance->item.ppsz_options = malloc( sizeof( char* ) );
1149 asprintf( &p_instance->item.ppsz_options[0], "sout=%s%s%s",
1150 media->psz_output ? media->psz_output : "",
1151 (media->psz_output && media->psz_vod_output) ?
1152 ":" : media->psz_vod_output ? "#" : "",
1153 media->psz_vod_output ? media->psz_vod_output : "" );
1154 p_instance->item.i_options = 1;
1157 for( i = 0; i < media->i_option; i++ )
1159 p_instance->item.i_options++;
1160 p_instance->item.ppsz_options =
1161 realloc( p_instance->item.ppsz_options,
1162 p_instance->item.i_options * sizeof( char* ) );
1163 p_instance->item.ppsz_options[p_instance->item.i_options - 1] =
1164 strdup( media->option[i] );
1167 p_instance->psz_name = psz_id ? strdup( psz_id ) : NULL;
1168 TAB_APPEND( media->i_instance, media->instance, p_instance );
1171 if( psz_args && sscanf(psz_args, "%d", &i) == 1 && i < media->i_input )
1173 p_instance->i_index = i;
1176 if( p_instance->item.psz_uri ) free( p_instance->item.psz_uri );
1177 p_instance->item.psz_uri =
1178 strdup( media->input[p_instance->i_index] );
1180 if( p_instance->p_input )
1182 input_StopThread( p_instance->p_input );
1183 input_DestroyThread( p_instance->p_input );
1184 vlc_object_detach( p_instance->p_input );
1185 vlc_object_destroy( p_instance->p_input );
1188 p_instance->p_input = input_CreateThread( vlm, &p_instance->item );
1189 if( !p_instance->p_input )
1191 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1192 vlc_input_item_Clean( &p_instance->item );
1193 if( p_instance->psz_name ) free( p_instance->psz_name );
1199 if( !p_instance ) return VLC_EGENERIC;
1201 if( !strcmp( psz_command, "seek" ) )
1206 if( psz_args && sscanf( psz_args, "%f", &f_percentage ) == 1 )
1208 val.f_float = f_percentage / 100.0 ;
1209 var_Set( p_instance->p_input, "position", val );
1213 else if( !strcmp( psz_command, "stop" ) )
1215 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1217 if( p_instance->p_input )
1219 input_StopThread( p_instance->p_input );
1220 input_DestroyThread( p_instance->p_input );
1221 vlc_object_detach( p_instance->p_input );
1222 vlc_object_destroy( p_instance->p_input );
1225 vlc_input_item_Clean( &p_instance->item );
1226 if( p_instance->psz_name ) free( p_instance->psz_name );
1231 else if( !strcmp( psz_command, "pause" ) )
1235 if( !p_instance->p_input ) return VLC_SUCCESS;
1237 var_Get( p_instance->p_input, "state", &val );
1239 if( val.i_int == PAUSE_S ) val.i_int = PLAYING_S;
1240 else val.i_int = PAUSE_S;
1241 var_Set( p_instance->p_input, "state", val );
1246 return VLC_EGENERIC;
1249 /*****************************************************************************
1251 *****************************************************************************/
1252 static int64_t vlm_Date()
1257 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1263 vlm_schedule_t *vlm_ScheduleNew( vlm_t *vlm, char *psz_name )
1265 vlm_schedule_t *p_sched = malloc( sizeof( vlm_schedule_t ) );
1277 p_sched->psz_name = strdup( psz_name );
1278 p_sched->b_enabled = VLC_FALSE;
1279 p_sched->i_command = 0;
1280 p_sched->command = NULL;
1281 p_sched->i_date = 0;
1282 p_sched->i_period = 0;
1283 p_sched->i_repeat = -1;
1285 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1290 /* for now, simple delete. After, del with options (last arg) */
1291 void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_t *sched,
1294 if( sched == NULL ) return;
1296 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1298 if( vlm->i_schedule == 0 && vlm->schedule ) free( vlm->schedule );
1299 free( sched->psz_name );
1300 while( sched->i_command-- ) free( sched->command[sched->i_command] );
1304 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *vlm, char *psz_name )
1308 for( i = 0; i < vlm->i_schedule; i++ )
1310 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1312 return vlm->schedule[i];
1319 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1320 int vlm_ScheduleSetup( vlm_schedule_t *schedule, char *psz_cmd,
1323 if( !strcmp( psz_cmd, "enabled" ) )
1325 schedule->b_enabled = VLC_TRUE;
1327 else if( !strcmp( psz_cmd, "disabled" ) )
1329 schedule->b_enabled = VLC_FALSE;
1331 #if !defined( UNDER_CE )
1332 else if( !strcmp( psz_cmd, "date" ) )
1338 time.tm_sec = 0; /* seconds */
1339 time.tm_min = 0; /* minutes */
1340 time.tm_hour = 0; /* hours */
1341 time.tm_mday = 0; /* day of the month */
1342 time.tm_mon = 0; /* month */
1343 time.tm_year = 0; /* year */
1344 time.tm_wday = 0; /* day of the week */
1345 time.tm_yday = 0; /* day in the year */
1346 time.tm_isdst = 0; /* daylight saving time */
1348 /* date should be year/month/day-hour:minutes:seconds */
1349 p = strchr( psz_value, '-' );
1351 if( !strcmp( psz_value, "now" ) )
1353 schedule->i_date = 0;
1355 else if( p == NULL && sscanf( psz_value, "%d:%d:%d", &time.tm_hour, &time.tm_min, &time.tm_sec ) != 3 ) /* it must be a hour:minutes:seconds */
1363 switch( sscanf( p + 1, "%d:%d:%d", &i, &j, &k ) )
1383 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1389 time.tm_mon = i - 1;
1393 time.tm_year = i - 1900;
1394 time.tm_mon = j - 1;
1401 date = mktime( &time );
1402 schedule->i_date = ((mtime_t) date) * 1000000;
1405 else if( !strcmp( psz_cmd, "period" ) )
1409 char *psz_time = NULL, *psz_date = NULL;
1413 /* First, if date or period are modified, repeat should be equal to -1 */
1414 schedule->i_repeat = -1;
1416 time.tm_sec = 0; /* seconds */
1417 time.tm_min = 0; /* minutes */
1418 time.tm_hour = 0; /* hours */
1419 time.tm_mday = 0; /* day of the month */
1420 time.tm_mon = 0; /* month */
1421 time.tm_year = 0; /* year */
1422 time.tm_wday = 0; /* day of the week */
1423 time.tm_yday = 0; /* day in the year */
1424 time.tm_isdst = 0; /* daylight saving time */
1426 /* date should be year/month/day-hour:minutes:seconds */
1427 p = strchr( psz_value, '-' );
1430 psz_date = psz_value;
1437 psz_time = psz_value;
1441 switch( sscanf( psz_time, "%d:%d:%d", &i, &j, &k ) )
1460 switch( sscanf( psz_date, "%d/%d/%d", &i, &j, &k ) )
1479 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1480 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1481 schedule->i_period = ((mtime_t) date) * 1000000;
1483 #endif /* UNDER_CE */
1484 else if( !strcmp( psz_cmd, "repeat" ) )
1488 if( sscanf( psz_value, "%d", &i ) == 1 )
1490 schedule->i_repeat = i;
1497 else if( !strcmp( psz_cmd, "append" ) )
1499 char *command = strdup( psz_value );
1501 TAB_APPEND( schedule->i_command, schedule->command, command );
1510 /*****************************************************************************
1511 * Message handling functions
1512 *****************************************************************************/
1513 static vlm_message_t *vlm_MessageNew( char *psz_name,
1514 const char *psz_format, ... )
1516 vlm_message_t *p_message;
1519 if( !psz_name ) return 0;
1521 p_message = malloc( sizeof(vlm_message_t) );
1527 p_message->psz_value = 0;
1531 va_start( args, psz_format );
1532 if( vasprintf( &p_message->psz_value, psz_format, args ) < 0 )
1541 p_message->psz_name = strdup( psz_name );
1542 p_message->i_child = 0;
1543 p_message->child = NULL;
1548 void vlm_MessageDelete( vlm_message_t *p_message )
1550 if( p_message->psz_name ) free( p_message->psz_name );
1551 if( p_message->psz_value ) free( p_message->psz_value );
1552 while( p_message->i_child-- )
1553 vlm_MessageDelete( p_message->child[p_message->i_child] );
1554 if( p_message->child ) free( p_message->child );
1559 static vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1560 vlm_message_t *p_child )
1562 if( p_message == NULL ) return NULL;
1566 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1572 /*****************************************************************************
1573 * Misc utility functions
1574 *****************************************************************************/
1575 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_t *media,
1576 vlm_schedule_t *schedule, char *psz_filter )
1582 vlm_message_t *msg_media;
1583 vlm_message_t *msg_child;
1585 msg = vlm_MessageNew( "show", NULL );
1586 msg_media = vlm_MessageAdd( msg, vlm_MessageNew( media->psz_name, 0 ));
1588 vlm_MessageAdd( msg_media,
1589 vlm_MessageNew( "type", media->i_type == VOD_TYPE ?
1590 "vod" : "broadcast" ) );
1591 vlm_MessageAdd( msg_media,
1592 vlm_MessageNew( "enabled", media->b_enabled ?
1595 vlm_MessageAdd( msg_media,
1596 vlm_MessageNew( "loop", media->b_loop ?
1599 if( media->i_type == VOD_TYPE && media->psz_mux )
1600 vlm_MessageAdd( msg_media,
1601 vlm_MessageNew( "mux", media->psz_mux ) );
1603 msg_child = vlm_MessageAdd( msg_media,
1604 vlm_MessageNew( "inputs", NULL ) );
1606 for( i = 0; i < media->i_input; i++ )
1608 vlm_MessageAdd( msg_child,
1609 vlm_MessageNew( media->input[i], NULL ) );
1612 vlm_MessageAdd( msg_media,
1613 vlm_MessageNew( "output", media->psz_output ?
1614 media->psz_output : "" ) );
1616 msg_child = vlm_MessageAdd( msg_media, vlm_MessageNew( "options", 0 ));
1618 for( i = 0; i < media->i_option; i++ )
1620 vlm_MessageAdd( msg_child, vlm_MessageNew( media->option[i], 0 ) );
1623 msg_child = vlm_MessageAdd( msg_media,
1624 vlm_MessageNew( "instances", NULL ) );
1626 for( i = 0; i < media->i_instance; i++ )
1628 vlm_media_instance_t *p_instance = media->instance[i];
1631 if( !p_instance->p_input ) val.i_int = END_S;
1632 else var_Get( p_instance->p_input, "state", &val );
1634 vlm_MessageAdd( msg_child,
1635 vlm_MessageNew( p_instance->psz_name ?
1636 p_instance->psz_name : "default",
1637 val.i_int == PLAYING_S ? "playing" :
1638 val.i_int == PAUSE_S ? "paused" :
1646 else if( schedule != NULL )
1650 vlm_message_t *msg_schedule;
1651 vlm_message_t *msg_child;
1654 msg = vlm_MessageNew( "show", NULL );
1656 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, 0 ) );
1658 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1660 vlm_MessageAdd( msg_schedule,
1661 vlm_MessageNew( "enabled", schedule->b_enabled ?
1664 #if !defined( UNDER_CE )
1665 if( schedule->i_date != 0 )
1668 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1671 #ifdef HAVE_LOCALTIME_R
1672 localtime_r( &i_time, &date);
1674 struct tm *p_date = localtime( &i_time );
1678 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1679 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1680 date.tm_hour, date.tm_min, date.tm_sec );
1682 vlm_MessageAdd( msg_schedule,
1683 vlm_MessageNew( "date", psz_date ) );
1688 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1691 if( schedule->i_period != 0 )
1693 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1696 date.tm_sec = (int)( i_time % 60 );
1697 i_time = i_time / 60;
1698 date.tm_min = (int)( i_time % 60 );
1699 i_time = i_time / 60;
1700 date.tm_hour = (int)( i_time % 24 );
1701 i_time = i_time / 24;
1702 date.tm_mday = (int)( i_time % 30 );
1703 i_time = i_time / 30;
1704 /* okay, okay, months are not always 30 days long */
1705 date.tm_mon = (int)( i_time % 12 );
1706 i_time = i_time / 12;
1707 date.tm_year = (int)i_time;
1709 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1710 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1712 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1716 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1718 #endif /* UNDER_CE */
1720 sprintf( buffer, "%d", schedule->i_repeat );
1721 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1724 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", 0) );
1726 for( i = 0; i < schedule->i_command; i++ )
1728 vlm_MessageAdd( msg_child,
1729 vlm_MessageNew( schedule->command[i], NULL ) );
1736 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1740 vlm_message_t *msg_child;
1742 msg = vlm_MessageNew( "show", NULL );
1743 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "media", NULL ) );
1745 for( i = 0; i < vlm->i_media; i++ )
1747 vlm_media_t *m = vlm->media[i];
1748 vlm_message_t *msg_media, *msg_instance;
1750 msg_media = vlm_MessageAdd( msg_child,
1751 vlm_MessageNew( m->psz_name, 0 ) );
1753 vlm_MessageAdd( msg_media,
1754 vlm_MessageNew( "type", m->i_type == VOD_TYPE ?
1755 "vod" : "broadcast" ) );
1757 vlm_MessageAdd( msg_media,
1758 vlm_MessageNew( "enabled", m->b_enabled ?
1761 if( m->i_type == VOD_TYPE && m->psz_mux )
1762 vlm_MessageAdd( msg_media,
1763 vlm_MessageNew( "mux", m->psz_mux ) );
1765 msg_instance = vlm_MessageAdd( msg_media,
1766 vlm_MessageNew( "instances", 0 ) );
1768 for( j = 0; j < m->i_instance; j++ )
1770 vlm_media_instance_t *p_instance = m->instance[j];
1773 if( !p_instance->p_input ) val.i_int = END_S;
1774 else var_Get( p_instance->p_input, "state", &val );
1776 vlm_MessageAdd( msg_instance,
1777 vlm_MessageNew( p_instance->psz_name ?
1778 p_instance->psz_name : "default",
1779 val.i_int == PLAYING_S ? "playing" :
1780 val.i_int == PAUSE_S ? "paused" :
1788 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1792 vlm_message_t *msg_child;
1794 msg = vlm_MessageNew( "show", NULL );
1795 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", NULL ) );
1797 for( i = 0; i < vlm->i_schedule; i++ )
1799 vlm_schedule_t *s = vlm->schedule[i];
1800 vlm_message_t *msg_schedule;
1801 mtime_t i_time, i_next_date;
1803 msg_schedule = vlm_MessageAdd( msg_child,
1804 vlm_MessageNew( s->psz_name, 0 ) );
1805 vlm_MessageAdd( msg_schedule,
1806 vlm_MessageNew( "enabled", s->b_enabled ?
1809 if( !s->b_enabled ) return msg;
1812 vlm_MessageAdd( msg_schedule,
1813 vlm_MessageNew( "enabled", "yes" ) );
1815 /* calculate next date */
1816 i_time = vlm_Date();
1817 i_next_date = s->i_date;
1819 if( s->i_period != 0 )
1822 while( s->i_date + j * s->i_period <= i_time &&
1828 i_next_date = s->i_date + j * s->i_period;
1831 if( i_next_date > i_time )
1833 time_t i_date = (time_t) (i_next_date / 1000000) ;
1835 #if !defined( UNDER_CE )
1838 ctime_r( &i_date, psz_date );
1840 char *psz_date = ctime( &i_date );
1843 vlm_MessageAdd( msg_schedule,
1844 vlm_MessageNew( "next launch", psz_date ) );
1852 else if( psz_filter == NULL && media == NULL && schedule == NULL )
1854 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1855 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1857 vlm_MessageAdd( show1, show2->child[0] );
1859 /* We must destroy the parent node "show" of show2
1860 * and not the children */
1861 free( show2->psz_name );
1869 return vlm_MessageNew( "show", NULL );
1873 static vlm_message_t *vlm_Help( vlm_t *vlm, char *psz_filter )
1875 vlm_message_t *message, *message_child;
1877 #define MessageAdd( a ) \
1878 vlm_MessageAdd( message, vlm_MessageNew( a, NULL ) );
1879 #define MessageAddChild( a ) \
1880 vlm_MessageAdd( message_child, vlm_MessageNew( a, NULL ) );
1882 if( psz_filter == NULL )
1884 message = vlm_MessageNew( "help", NULL );
1886 message_child = MessageAdd( "Commands Syntax:" );
1887 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
1888 MessageAddChild( "setup (name) (properties)" );
1889 MessageAddChild( "show [(name)|media|schedule]" );
1890 MessageAddChild( "del (name)|all|media|schedule" );
1891 MessageAddChild( "control (name) [instance_name] (command)" );
1892 MessageAddChild( "save (config_file)" );
1893 MessageAddChild( "load (config_file)" );
1895 message_child = MessageAdd( "Media Proprieties Syntax:" );
1896 MessageAddChild( "input (input_name)" );
1897 MessageAddChild( "inputdel (input_name)|all" );
1898 MessageAddChild( "inputdeln input_number" );
1899 MessageAddChild( "output (output_name)" );
1900 MessageAddChild( "enabled|disabled" );
1901 MessageAddChild( "loop|unloop (broadcast only)" );
1902 MessageAddChild( "mux (mux_name)" );
1904 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
1905 MessageAddChild( "enabled|disabled" );
1906 MessageAddChild( "append (command_until_rest_of_the_line)" );
1907 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
1909 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
1910 "(days)-(hours):(minutes):(seconds)" );
1911 MessageAddChild( "repeat (number_of_repetitions)" );
1913 message_child = MessageAdd( "Control Commands Syntax:" );
1914 MessageAddChild( "play" );
1915 MessageAddChild( "pause" );
1916 MessageAddChild( "stop" );
1917 MessageAddChild( "seek (percentage)" );
1922 return vlm_MessageNew( "help", NULL );
1925 /*****************************************************************************
1926 * Config handling functions
1927 *****************************************************************************/
1928 static int Load( vlm_t *vlm, char *file )
1932 while( *pf != '\0' )
1934 vlm_message_t *message = NULL;
1938 while( pf[i_temp] != '\n' && pf[i_temp] != '\0' && pf[i_temp] != '\r' )
1943 if( pf[i_temp] == '\r' || pf[i_temp] == '\n' )
1946 i_next = i_temp + 1;
1953 if( ExecuteCommand( vlm, pf, &message ) )
1966 static char *Save( vlm_t *vlm )
1973 for( i = 0; i < vlm->i_media; i++ )
1975 vlm_media_t *media = vlm->media[i];
1977 if( media->i_type == VOD_TYPE )
1979 i_length += strlen( "new vod " ) + strlen(media->psz_name);
1983 i_length += strlen( "new broadcast " ) + strlen(media->psz_name);
1986 if( media->b_enabled == VLC_TRUE )
1988 i_length += strlen( "enabled" );
1992 i_length += strlen( "disabled" );
1995 if( media->b_loop == VLC_TRUE )
1997 i_length += strlen( " loop\n" );
2001 i_length += strlen( "\n" );
2004 for( j = 0; j < media->i_input; j++ )
2006 i_length += strlen( "setup input \"\"\n" ) +
2007 strlen( media->psz_name ) + strlen( media->input[j] );
2010 if( media->psz_output != NULL )
2012 i_length += strlen(media->psz_name) + strlen(media->psz_output) +
2013 strlen( "setup output \n" );
2016 for( j=0 ; j < media->i_option ; j++ )
2018 i_length += strlen(media->psz_name) + strlen(media->option[j]) +
2019 strlen("setup option \n");
2023 for( i = 0; i < vlm->i_schedule; i++ )
2025 vlm_schedule_t *schedule = vlm->schedule[i];
2027 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
2029 if( schedule->b_enabled == VLC_TRUE )
2031 i_length += strlen( "date //-:: enabled\n" ) + 14;
2035 i_length += strlen( "date //-:: disabled\n" ) + 14;
2039 if( schedule->i_period != 0 )
2041 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
2042 strlen( "period //-::\n" ) + 14;
2045 if( schedule->i_repeat >= 0 )
2049 sprintf( buffer, "%d", schedule->i_repeat );
2050 i_length += strlen( "setup repeat \n" ) +
2051 strlen( schedule->psz_name ) + strlen( buffer );
2058 for( j = 0; j < schedule->i_command; j++ )
2060 i_length += strlen( "setup append \n" ) +
2061 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
2066 /* Don't forget the '\0' */
2068 /* now we have the length of save */
2070 p = save = malloc( i_length );
2073 /* finally we can write in it */
2074 for( i = 0; i < vlm->i_media; i++ )
2076 vlm_media_t *media = vlm->media[i];
2078 if( media->i_type == VOD_TYPE )
2080 p += sprintf( p, "new %s vod ", media->psz_name);
2084 p += sprintf( p, "new %s broadcast ", media->psz_name);
2087 if( media->b_enabled == VLC_TRUE )
2089 p += sprintf( p, "enabled" );
2093 p += sprintf( p, "disabled" );
2096 if( media->b_loop == VLC_TRUE )
2098 p += sprintf( p, " loop\n" );
2102 p += sprintf( p, "\n" );
2105 for( j = 0; j < media->i_input; j++ )
2107 p += sprintf( p, "setup %s input \"%s\"\n", media->psz_name,
2111 if( media->psz_output != NULL )
2113 p += sprintf( p, "setup %s output %s\n", media->psz_name,
2114 media->psz_output );
2117 for( j = 0; j < media->i_option; j++ )
2119 p += sprintf( p, "setup %s option %s\n", media->psz_name,
2124 /* and now, the schedule scripts */
2125 #if !defined( UNDER_CE )
2126 for( i = 0; i < vlm->i_schedule; i++ )
2128 vlm_schedule_t *schedule = vlm->schedule[i];
2130 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
2132 #ifdef HAVE_LOCALTIME_R
2133 localtime_r( &i_time, &date);
2135 struct tm *p_date = localtime( &i_time );
2139 p += sprintf( p, "new %s schedule ", schedule->psz_name);
2141 if( schedule->b_enabled == VLC_TRUE )
2143 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
2144 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2145 date.tm_hour, date.tm_min, date.tm_sec );
2149 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
2150 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2151 date.tm_hour, date.tm_min, date.tm_sec);
2154 if( schedule->i_period != 0 )
2156 p += sprintf( p, "setup %s ", schedule->psz_name );
2158 i_time = (time_t) ( schedule->i_period / 1000000 );
2160 date.tm_sec = (int)( i_time % 60 );
2161 i_time = i_time / 60;
2162 date.tm_min = (int)( i_time % 60 );
2163 i_time = i_time / 60;
2164 date.tm_hour = (int)( i_time % 24 );
2165 i_time = i_time / 24;
2166 date.tm_mday = (int)( i_time % 30 );
2167 i_time = i_time / 30;
2168 /* okay, okay, months are not always 30 days long */
2169 date.tm_mon = (int)( i_time % 12 );
2170 i_time = i_time / 12;
2171 date.tm_year = (int)i_time;
2173 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
2174 date.tm_year, date.tm_mon, date.tm_mday,
2175 date.tm_hour, date.tm_min, date.tm_sec);
2178 if( schedule->i_repeat >= 0 )
2180 p += sprintf( p, "setup %s repeat %d\n",
2181 schedule->psz_name, schedule->i_repeat );
2185 p += sprintf( p, "\n" );
2188 for( j = 0; j < schedule->i_command; j++ )
2190 p += sprintf( p, "setup %s append %s\n",
2191 schedule->psz_name, schedule->command[j] );
2195 #endif /* UNDER_CE */
2200 /*****************************************************************************
2202 *****************************************************************************/
2203 int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
2204 char *psz_id, int i_query, va_list args )
2206 vlm_t *vlm = (vlm_t *)p_private;
2207 int i, i_ret = VLC_EGENERIC;
2209 if( !p_private || !p_vod_media ) return VLC_EGENERIC;
2211 vlc_mutex_lock( &vlm->lock );
2214 for( i = 0; i < vlm->i_media; i++ )
2216 if( p_vod_media == vlm->media[i]->vod_media ) break;
2219 if( i == vlm->i_media )
2221 vlc_mutex_unlock( &vlm->lock );
2222 return VLC_EGENERIC;
2227 case VOD_MEDIA_PLAY:
2228 vlm->media[i]->psz_vod_output = (char *)va_arg( args, char * );
2229 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "play", 0 );
2230 vlm->media[i]->psz_vod_output = 0;
2233 case VOD_MEDIA_PAUSE:
2234 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "pause", 0 );
2237 case VOD_MEDIA_STOP:
2238 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "stop", 0 );
2241 case VOD_MEDIA_SEEK:
2243 double f_pos = (double)va_arg( args, double );
2246 sprintf( psz_pos, "%f", f_pos );
2247 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "seek", psz_pos);
2255 vlc_mutex_unlock( &vlm->lock );
2260 /*****************************************************************************
2262 *****************************************************************************/
2263 static int Manage( vlc_object_t* p_object )
2265 vlm_t *vlm = (vlm_t*)p_object;
2267 mtime_t i_lastcheck;
2270 i_lastcheck = vlm_Date();
2274 while( !vlm->b_die )
2276 vlc_mutex_lock( &vlm->lock );
2278 /* destroy the inputs that wants to die, and launch the next input */
2279 for( i = 0; i < vlm->i_media; i++ )
2281 vlm_media_t *p_media = vlm->media[i];
2283 for( j = 0; j < p_media->i_instance; j++ )
2285 vlm_media_instance_t *p_instance = p_media->instance[j];
2287 if( !p_instance->p_input ||
2288 ( !p_instance->p_input->b_eof &&
2289 !p_instance->p_input->b_error ) ) continue;
2291 input_StopThread( p_instance->p_input );
2292 input_DestroyThread( p_instance->p_input );
2293 vlc_object_detach( p_instance->p_input );
2294 vlc_object_destroy( p_instance->p_input );
2296 p_instance->i_index++;
2297 if( p_instance->i_index == p_media->i_input &&
2298 p_media->b_loop ) p_instance->i_index = 0;
2300 if( p_instance->i_index < p_media->i_input )
2302 /* FIXME, find a way to select the right instance */
2304 sprintf( buffer, "%d", p_instance->i_index );
2305 vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2310 if( vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2311 "stop", 0 ) == VLC_SUCCESS ) i--;
2317 i_time = vlm_Date();
2319 for( i = 0; i < vlm->i_schedule; i++ )
2321 mtime_t i_real_date = vlm->schedule[i]->i_date;
2323 if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2325 if( vlm->schedule[i]->i_date == 0 ) // now !
2327 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2328 i_real_date = i_time;
2330 else if( vlm->schedule[i]->i_period != 0 )
2333 while( vlm->schedule[i]->i_date + j *
2334 vlm->schedule[i]->i_period <= i_lastcheck &&
2335 ( vlm->schedule[i]->i_repeat > j ||
2336 vlm->schedule[i]->i_repeat == -1 ) )
2341 i_real_date = vlm->schedule[i]->i_date + j *
2342 vlm->schedule[i]->i_period;
2345 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2347 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2349 vlm_message_t *message = NULL;
2351 ExecuteCommand( vlm, vlm->schedule[i]->command[j],
2354 /* for now, drop the message */
2361 i_lastcheck = i_time;
2363 vlc_mutex_unlock( &vlm->lock );
2371 #else /* ENABLE_VLM */
2373 /* We just define an empty wrapper */
2374 vlm_t *__vlm_New( vlc_object_t *a )
2376 msg_Err( a, "VideoLAN manager support is disabled" );
2379 void vlm_Delete( vlm_t *a ){}
2380 int vlm_ExecuteCommand( vlm_t *a, char *b, vlm_message_t **c ){ return -1; }
2381 void vlm_MessageDelete( vlm_message_t *a ){}
2382 vlm_media_t *vlm_MediaNew( vlm_t *a, char *b, int c ){ return NULL; }
2383 void vlm_MediaDelete( vlm_t *a, vlm_media_t *b, char *c ){}
2384 int vlm_MediaSetup( vlm_t *a, vlm_media_t *b, char *c, char *d ){ return -1; }
2385 int vlm_MediaControl( vlm_t *a, vlm_media_t *b, char *c, char *d, char *e )
2387 vlm_schedule_t * vlm_ScheduleNew( vlm_t *a, char *b ){ return NULL; }
2388 void vlm_ScheduleDelete( vlm_t *a, vlm_schedule_t *b, char *c ){}
2389 int vlm_ScheduleSetup( vlm_schedule_t *a, char *b, char *c ){ return -1; }
2390 int vlm_MediaVodControl( void *a, vod_media_t *b, char *c, int d, va_list e )
2392 int vlm_Save( vlm_t *a, char *b ){ return -1; }
2393 int vlm_Load( vlm_t *a, char *b ){ return -1; }
2395 #endif /* ENABLE_VLM */