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 )
233 i_size = ftell( file );
234 fseek( file, 0, SEEK_SET );
235 psz_buffer = malloc( i_size + 1 );
241 fread( psz_buffer, 1, i_size, file );
242 psz_buffer[ i_size ] = '\0';
243 if( Load( p_vlm, psz_buffer ) )
256 /*****************************************************************************
258 *****************************************************************************/
259 static char *FindEndCommand( char *psz )
261 char *psz_sent = psz;
268 while( ( *psz_sent != '\"' ) && ( *psz_sent != '\0' ) )
270 if( *psz_sent == '\'' )
272 psz_sent = FindEndCommand( psz_sent );
273 if( psz_sent == NULL ) return NULL;
278 if( *psz_sent == '\"' )
284 /* *psz_sent == '\0' -> number of " is incorrect */
292 while( ( *psz_sent != '\'' ) && ( *psz_sent != '\0' ) )
294 if( *psz_sent == '\"' )
296 psz_sent = FindEndCommand( psz_sent );
297 if( psz_sent == NULL ) return NULL;
302 if( *psz_sent == '\'' )
308 /* *psz_sent == '\0' -> number of " is incorrect */
313 default: /* now we can look for spaces */
314 while( ( *psz_sent != ' ' ) && ( *psz_sent != '\0' ) )
316 if( ( *psz_sent == '\'' ) || ( *psz_sent == '\"' ) )
318 psz_sent = FindEndCommand( psz_sent );
327 /*****************************************************************************
328 * ExecuteCommand: The main state machine
329 *****************************************************************************
330 * Execute a command which ends with '\0' (string)
331 *****************************************************************************/
332 static int ExecuteCommand( vlm_t *p_vlm, char *psz_command,
333 vlm_message_t **pp_message )
336 char **ppsz_command = NULL;
337 char *psz_cmd = psz_command;
338 vlm_message_t *p_message = NULL;
341 /* First, parse the line and cut it */
342 while( *psz_cmd != '\0' )
345 if( *psz_cmd == ' ' || *psz_cmd == '\t' )
354 /* support for comments */
355 if( i_command == 0 && *psz_cmd == '#')
357 p_message = vlm_MessageNew( "", NULL );
361 psz_temp = FindEndCommand( psz_cmd );
363 if( psz_temp == NULL ) goto error;
365 i_temp = psz_temp - psz_cmd;
367 ppsz_command = realloc( ppsz_command, (i_command + 1) *
369 ppsz_command[ i_command ] = malloc( (i_temp + 1) * sizeof(char) );
370 strncpy( ppsz_command[ i_command ], psz_cmd, i_temp );
371 ppsz_command[ i_command ][ i_temp ] = '\0';
380 * And then Interpret it
385 p_message = vlm_MessageNew( "", NULL );
389 if( !strcmp(ppsz_command[0], "new") )
393 /* Check the number of arguments */
394 if( i_command < 3 ) goto syntax_error;
397 if( !strcmp(ppsz_command[2], "vod") )
401 else if( !strcmp(ppsz_command[2], "broadcast") )
403 i_type = BROADCAST_TYPE;
405 else if( !strcmp(ppsz_command[2], "schedule") )
407 i_type = SCHEDULE_TYPE;
412 vlm_MessageNew( "new", "%s: Choose between vod, "
413 "broadcast or schedule", ppsz_command[1] );
417 /* Check for forbidden media names */
418 if( !strcmp(ppsz_command[1], "all") ||
419 !strcmp(ppsz_command[1], "media") ||
420 !strcmp(ppsz_command[1], "schedule") )
422 p_message = vlm_MessageNew( "new", "\"all\", \"media\" and "
423 "\"schedule\" are reserved names" );
427 /* Check the name is not already in use */
428 if( vlm_ScheduleSearch( p_vlm, ppsz_command[1] ) ||
429 vlm_MediaSearch( p_vlm, ppsz_command[1] ) )
431 p_message = vlm_MessageNew( "new", "%s: Name already in use",
437 if( i_type == SCHEDULE_TYPE )
439 vlm_schedule_t *p_schedule;
440 p_schedule = vlm_ScheduleNew( p_vlm, ppsz_command[1] );
443 p_message = vlm_MessageNew( "new", "could not create schedule" );
451 vlm_media_t *p_media;
452 p_media = vlm_MediaNew( p_vlm, ppsz_command[1], i_type );
455 p_message = vlm_MessageNew( "new", "could not create media" );
462 p_message = vlm_MessageNew( "new", NULL );
466 /* Properties will be dealt with later on */
469 else if( !strcmp(ppsz_command[0], "setup") )
471 if( i_command < 2 ) goto syntax_error;
473 /* Properties will be dealt with later on */
476 else if( !strcmp(ppsz_command[0], "del") )
478 vlm_media_t *p_media;
479 vlm_schedule_t *p_schedule;
481 if( i_command < 2 ) goto syntax_error;
483 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
484 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
486 if( p_schedule != NULL )
488 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
490 else if( p_media != NULL )
492 vlm_MediaDelete( p_vlm, p_media, NULL );
494 else if( !strcmp(ppsz_command[1], "media") )
496 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
499 else if( !strcmp(ppsz_command[1], "schedule") )
501 while( p_vlm->i_schedule )
502 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
504 else if( !strcmp(ppsz_command[1], "all") )
506 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
509 while( p_vlm->i_schedule )
510 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
514 p_message = vlm_MessageNew( "del", "%s: media unknown",
519 p_message = vlm_MessageNew( "del", NULL );
523 else if( !strcmp(ppsz_command[0], "show") )
525 vlm_media_t *p_media;
526 vlm_schedule_t *p_schedule;
530 p_message = vlm_Show( p_vlm, NULL, NULL, NULL );
533 else if( i_command > 2 ) goto syntax_error;
535 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
536 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
538 if( p_schedule != NULL )
540 p_message = vlm_Show( p_vlm, NULL, p_schedule, NULL );
542 else if( p_media != NULL )
544 p_message = vlm_Show( p_vlm, p_media, NULL, NULL );
548 p_message = vlm_Show( p_vlm, NULL, NULL, ppsz_command[1] );
554 else if( !strcmp(ppsz_command[0], "help") )
556 if( i_command != 1 ) goto syntax_error;
558 p_message = vlm_Help( p_vlm, NULL );
562 else if( !strcmp(ppsz_command[0], "control") )
564 vlm_media_t *p_media;
566 if( i_command < 3 ) goto syntax_error;
568 if( !(p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] ) ) )
570 p_message = vlm_MessageNew( "control", "%s: media unknown",
576 char *psz_command, *psz_arg = 0, *psz_instance = 0;
579 if( strcmp( ppsz_command[2], "play" ) &&
580 strcmp( ppsz_command[2], "stop" ) &&
581 strcmp( ppsz_command[2], "pause" ) &&
582 strcmp( ppsz_command[2], "seek" ) )
585 psz_instance = ppsz_command[2];
587 if( i_command < 4 ) goto syntax_error;
590 psz_command = ppsz_command[i_index];
592 if( i_command >= i_index + 2 ) psz_arg = ppsz_command[i_index + 1];
594 vlm_MediaControl( p_vlm, p_media, psz_instance, psz_command,
596 p_message = vlm_MessageNew( "control", NULL );
601 else if( !strcmp(ppsz_command[0], "save") )
603 if( i_command != 2 ) goto syntax_error;
605 if( vlm_Save( p_vlm, ppsz_command[1] ) )
607 p_message = vlm_MessageNew( "save", "Unable to save to file" );
612 p_message = vlm_MessageNew( "save", NULL );
617 else if( !strcmp(ppsz_command[0], "load") )
619 if( i_command != 2 ) goto syntax_error;
621 switch( vlm_Load( p_vlm, ppsz_command[1] ) )
624 p_message = vlm_MessageNew( "load", NULL );
627 p_message = vlm_MessageNew( "load", "read file error" );
631 vlm_MessageNew( "load", "error while loading file" );
635 vlm_MessageNew( "load", "Unable to load from file" );
642 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
646 /* Common code between "new" and "setup" */
647 if( !strcmp(ppsz_command[0], "new") ||
648 !strcmp(ppsz_command[0], "setup") )
650 int i_command_start = strcmp(ppsz_command[0], "new") ? 2 : 3;
651 vlm_media_t *p_media;
652 vlm_schedule_t *p_schedule;
654 if( i_command < i_command_start ) goto syntax_error;
656 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
657 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
659 if( !p_media && !p_schedule )
661 p_message = vlm_MessageNew( ppsz_command[0], "%s unknown",
666 if( p_schedule != NULL )
668 for( i = i_command_start ; i < i_command ; i++ )
670 if( !strcmp( ppsz_command[i], "enabled" ) ||
671 !strcmp( ppsz_command[i], "disabled" ) )
673 vlm_ScheduleSetup( p_schedule, ppsz_command[i], NULL );
676 /* Beware: everything behind append is considered as
678 else if( !strcmp( ppsz_command[i], "append" ) )
680 if( ++i >= i_command ) break;
682 for( j = i + 1; j < i_command; j++ )
685 realloc( ppsz_command[i], strlen(ppsz_command[i]) +
686 strlen(ppsz_command[j]) + 1 + 1 );
687 strcat( ppsz_command[i], " " );
688 strcat( ppsz_command[i], ppsz_command[j] );
691 vlm_ScheduleSetup( p_schedule, ppsz_command[i - 1],
697 if( i + 1 >= i_command && !strcmp(ppsz_command[0], "new") )
699 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
701 vlm_MessageNew( ppsz_command[0],
702 "Wrong properties syntax" );
705 else if( i + 1 >= i_command )
708 vlm_MessageNew( ppsz_command[0],
709 "Wrong properties syntax" );
713 vlm_ScheduleSetup( p_schedule, ppsz_command[i],
720 else if( p_media != NULL )
722 for( i = i_command_start ; i < i_command ; i++ )
724 if( !strcmp( ppsz_command[i], "enabled" ) ||
725 !strcmp( ppsz_command[i], "disabled" ) )
727 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
729 else if( i + 1 >= i_command &&
730 !strcmp( ppsz_command[i], "mux") )
732 if( p_media->i_type != VOD_TYPE )
734 p_message = vlm_MessageNew( ppsz_command[0],
735 "mux only available for broadcast" );
739 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
744 else if( !strcmp( ppsz_command[i], "loop" ) ||
745 !strcmp( ppsz_command[i], "unloop" ) )
747 if( p_media->i_type != BROADCAST_TYPE )
749 p_message = vlm_MessageNew( ppsz_command[0],
750 "loop only available for broadcast" );
754 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
759 if( i + 1 >= i_command &&
760 !strcmp(ppsz_command[0], "new") )
762 vlm_MediaDelete( p_vlm, p_media, NULL );
764 vlm_MessageNew( ppsz_command[0],
765 "Wrong properties syntax" );
768 else if( i + 1 >= i_command )
771 vlm_MessageNew( ppsz_command[0],
772 "Wrong properties syntax" );
776 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
783 p_message = vlm_MessageNew( ppsz_command[0], NULL );
788 for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
789 FREE( ppsz_command );
790 *pp_message = p_message;
795 p_message = vlm_MessageNew( ppsz_command[0], "Wrong command syntax" );
798 for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
799 FREE( ppsz_command );
800 *pp_message = p_message;
805 static vlm_media_t *vlm_MediaSearch( vlm_t *vlm, char *psz_name )
809 for( i = 0; i < vlm->i_media; i++ )
811 if( strcmp( psz_name, vlm->media[i]->psz_name ) == 0 )
813 return vlm->media[i];
820 /*****************************************************************************
822 *****************************************************************************/
823 static vlm_media_instance_t *
824 vlm_MediaInstanceSearch( vlm_t *vlm, vlm_media_t *media, char *psz_name )
828 for( i = 0; i < media->i_instance; i++ )
830 if( ( !psz_name && !media->instance[i]->psz_name ) ||
831 ( psz_name && media->instance[i]->psz_name &&
832 !strcmp( psz_name, media->instance[i]->psz_name ) ) )
834 return media->instance[i];
841 vlm_media_t *vlm_MediaNew( vlm_t *vlm, char *psz_name, int i_type )
843 vlm_media_t *media = malloc( sizeof( vlm_media_t ) );
847 msg_Err( vlm, "out of memory" );
851 /* Check if we need to load the VOD server */
852 if( i_type == VOD_TYPE && !vlm->i_vod )
854 vlm->vod = vlc_object_create( vlm, VLC_OBJECT_VOD );
855 vlc_object_attach( vlm->vod, vlm );
856 vlm->vod->p_module = module_Need( vlm->vod, "vod server", 0, 0 );
857 if( !vlm->vod->p_module )
859 msg_Err( vlm, "cannot find vod server" );
860 vlc_object_detach( vlm->vod );
861 vlc_object_destroy( vlm->vod );
867 vlm->vod->p_data = vlm;
868 vlm->vod->pf_media_control = vlm_MediaVodControl;
871 if( i_type == VOD_TYPE ) vlm->i_vod++;
873 media->psz_name = strdup( psz_name );
874 media->b_enabled = VLC_FALSE;
875 media->b_loop = VLC_FALSE;
876 media->vod_media = NULL;
877 media->psz_vod_output = NULL;
878 media->psz_mux = NULL;
881 media->psz_output = NULL;
883 media->option = NULL;
884 media->i_type = i_type;
885 media->i_instance = 0;
886 media->instance = NULL;
888 media->item.psz_uri = strdup( psz_name );
889 vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
891 TAB_APPEND( vlm->i_media, vlm->media, media );
896 /* for now, simple delete. After, del with options (last arg) */
897 void vlm_MediaDelete( vlm_t *vlm, vlm_media_t *media, char *psz_name )
899 if( media == NULL ) return;
901 while( media->i_instance )
903 vlm_media_instance_t *p_instance = media->instance[0];
904 vlm_MediaControl( vlm, media, p_instance->psz_name, "stop", 0 );
907 TAB_REMOVE( vlm->i_media, vlm->media, media );
909 if( media->i_type == VOD_TYPE )
911 vlm_MediaSetup( vlm, media, "disabled", 0 );
915 /* Check if we need to unload the VOD server */
916 if( media->i_type == VOD_TYPE && !vlm->i_vod )
918 module_Unneed( vlm->vod, vlm->vod->p_module );
919 vlc_object_detach( vlm->vod );
920 vlc_object_destroy( vlm->vod );
924 if( vlm->i_media == 0 && vlm->media ) free( vlm->media );
926 free( media->psz_name );
928 while( media->i_input-- ) free( media->input[media->i_input] );
929 if( media->input ) free( media->input );
931 if( media->psz_output ) free( media->psz_output );
932 if( media->psz_mux ) free( media->psz_mux );
934 while( media->i_option-- ) free( media->option[media->i_option] );
935 if( media->option ) free( media->option );
937 vlc_input_item_Clean( &media->item );
942 int vlm_MediaSetup( vlm_t *vlm, vlm_media_t *media, char *psz_cmd,
945 if( !psz_cmd) return VLC_EGENERIC;
947 if( !strcmp( psz_cmd, "loop" ) )
949 media->b_loop = VLC_TRUE;
951 else if( !strcmp( psz_cmd, "unloop" ) )
953 media->b_loop = VLC_FALSE;
955 else if( !strcmp( psz_cmd, "enabled" ) )
957 media->b_enabled = VLC_TRUE;
959 else if( !strcmp( psz_cmd, "disabled" ) )
961 media->b_enabled = VLC_FALSE;
963 else if( !strcmp( psz_cmd, "mux" ) )
965 if( media->psz_mux ) free( media->psz_mux );
966 media->psz_mux = NULL;
967 if( psz_value ) media->psz_mux = strdup( psz_value );
969 else if( !strcmp( psz_cmd, "input" ) )
973 if( psz_value != NULL && strlen(psz_value) > 1 &&
974 ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
975 ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
976 psz_value[ strlen(psz_value) - 1 ] == '\"' ) )
978 input = malloc( strlen(psz_value) - 1 );
980 memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
981 input[ strlen(psz_value) - 2 ] = '\0';
985 input = strdup( psz_value );
988 TAB_APPEND( media->i_input, media->input, input );
990 else if( !strcmp( psz_cmd, "inputdel" ) && !strcmp( psz_value, "all" ) )
992 while( media->i_input > 0 )
994 TAB_REMOVE( media->i_input, media->input, media->input[0] );
997 else if( !strcmp( psz_cmd, "inputdel" ) )
1002 if( psz_value != NULL && strlen(psz_value) > 1 &&
1003 ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
1004 ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
1005 psz_value[ strlen(psz_value) - 1 ] == '\"' ) )
1007 input = malloc( strlen(psz_value) - 1 );
1009 memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
1010 input[ strlen(psz_value) - 2 ] = '\0';
1014 input = strdup( psz_value );
1017 for( i = 0; i < media->i_input; i++ )
1019 if( !strcmp( input, media->input[i] ) )
1021 TAB_REMOVE( media->i_input, media->input, media->input[i] );
1026 else if( !strcmp( psz_cmd, "inputdeln" ) )
1028 int index = atoi( psz_value );
1029 if( index > 0 && index <= media->i_input )
1031 TAB_REMOVE( media->i_input, media->input, media->input[index-1] );
1034 else if( !strcmp( psz_cmd, "output" ) )
1036 if( media->psz_output != NULL )
1038 free( media->psz_output );
1040 media->psz_output = strdup( psz_value );
1042 else if( !strcmp( psz_cmd, "option" ) )
1045 psz_option = strdup( psz_value );
1047 TAB_APPEND( media->i_option, media->option, psz_option );
1051 return VLC_EGENERIC;
1054 /* Check if we need to create/delete a vod media */
1055 if( media->i_type == VOD_TYPE )
1057 if( !media->b_enabled && media->vod_media )
1059 vlm->vod->pf_media_del( vlm->vod, media->vod_media );
1060 media->vod_media = 0;
1062 else if( media->b_enabled && !media->vod_media && media->i_input )
1064 /* Pre-parse the input */
1065 input_thread_t *p_input;
1069 vlc_input_item_Clean( &media->item );
1070 vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
1072 if( media->psz_output )
1073 asprintf( &psz_output, "%s:description", media->psz_output );
1075 asprintf( &psz_output, "#description" );
1077 media->item.psz_uri = strdup( media->input[0] );
1078 media->item.ppsz_options = malloc( sizeof( char* ) );
1079 asprintf( &media->item.ppsz_options[0], "sout=%s", psz_output);
1080 media->item.i_options = 1;
1081 for( i = 0; i < media->i_option; i++ )
1083 media->item.i_options++;
1084 media->item.ppsz_options =
1085 realloc( media->item.ppsz_options,
1086 media->item.i_options * sizeof( char* ) );
1087 media->item.ppsz_options[ media->item.i_options - 1 ] =
1088 strdup( media->option[i] );
1091 if( (p_input = input_CreateThread( vlm, &media->item ) ) )
1093 while( !p_input->b_eof && !p_input->b_error ) msleep( 100000 );
1095 input_StopThread( p_input );
1096 input_DestroyThread( p_input );
1097 vlc_object_detach( p_input );
1098 vlc_object_destroy( p_input );
1102 if( media->psz_mux )
1105 es_format_t es, *p_es = &es;
1108 sprintf( fourcc, "%4.4s", media->psz_mux );
1109 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
1110 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
1115 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
1118 vlm->vod->pf_media_new( vlm->vod, media->psz_name, &item );
1123 vlm->vod->pf_media_new( vlm->vod, media->psz_name,
1131 int vlm_MediaControl( vlm_t *vlm, vlm_media_t *media, char *psz_id,
1132 char *psz_command, char *psz_args )
1134 vlm_media_instance_t *p_instance;
1137 p_instance = vlm_MediaInstanceSearch( vlm, media, psz_id );
1139 if( !strcmp( psz_command, "play" ) )
1141 if( !media->b_enabled || media->i_input == 0 ) return 0;
1145 p_instance = malloc( sizeof(vlm_media_instance_t) );
1146 if( !p_instance ) return VLC_EGENERIC;
1147 memset( p_instance, 0, sizeof(vlm_media_instance_t) );
1148 vlc_input_item_Init( VLC_OBJECT(vlm), &p_instance->item );
1149 p_instance->p_input = NULL;
1151 if( media->psz_output != NULL || media->psz_vod_output != NULL )
1153 p_instance->item.ppsz_options = malloc( sizeof( char* ) );
1154 asprintf( &p_instance->item.ppsz_options[0], "sout=%s%s%s",
1155 media->psz_output ? media->psz_output : "",
1156 (media->psz_output && media->psz_vod_output) ?
1157 ":" : media->psz_vod_output ? "#" : "",
1158 media->psz_vod_output ? media->psz_vod_output : "" );
1159 p_instance->item.i_options = 1;
1162 for( i = 0; i < media->i_option; i++ )
1164 p_instance->item.i_options++;
1165 p_instance->item.ppsz_options =
1166 realloc( p_instance->item.ppsz_options,
1167 p_instance->item.i_options * sizeof( char* ) );
1168 p_instance->item.ppsz_options[p_instance->item.i_options - 1] =
1169 strdup( media->option[i] );
1172 p_instance->psz_name = psz_id ? strdup( psz_id ) : NULL;
1173 TAB_APPEND( media->i_instance, media->instance, p_instance );
1176 if( psz_args && sscanf(psz_args, "%d", &i) == 1 && i < media->i_input )
1178 p_instance->i_index = i;
1181 if( p_instance->item.psz_uri ) free( p_instance->item.psz_uri );
1182 p_instance->item.psz_uri =
1183 strdup( media->input[p_instance->i_index] );
1185 if( p_instance->p_input )
1187 input_StopThread( p_instance->p_input );
1188 input_DestroyThread( p_instance->p_input );
1189 vlc_object_detach( p_instance->p_input );
1190 vlc_object_destroy( p_instance->p_input );
1193 p_instance->p_input = input_CreateThread( vlm, &p_instance->item );
1194 if( !p_instance->p_input )
1196 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1197 vlc_input_item_Clean( &p_instance->item );
1198 if( p_instance->psz_name ) free( p_instance->psz_name );
1204 if( !p_instance ) return VLC_EGENERIC;
1206 if( !strcmp( psz_command, "seek" ) )
1211 if( psz_args && sscanf( psz_args, "%f", &f_percentage ) == 1 )
1213 val.f_float = f_percentage / 100.0 ;
1214 var_Set( p_instance->p_input, "position", val );
1218 else if( !strcmp( psz_command, "stop" ) )
1220 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1222 if( p_instance->p_input )
1224 input_StopThread( p_instance->p_input );
1225 input_DestroyThread( p_instance->p_input );
1226 vlc_object_detach( p_instance->p_input );
1227 vlc_object_destroy( p_instance->p_input );
1230 vlc_input_item_Clean( &p_instance->item );
1231 if( p_instance->psz_name ) free( p_instance->psz_name );
1236 else if( !strcmp( psz_command, "pause" ) )
1240 if( !p_instance->p_input ) return VLC_SUCCESS;
1242 var_Get( p_instance->p_input, "state", &val );
1244 if( val.i_int == PAUSE_S ) val.i_int = PLAYING_S;
1245 else val.i_int = PAUSE_S;
1246 var_Set( p_instance->p_input, "state", val );
1251 return VLC_EGENERIC;
1254 /*****************************************************************************
1256 *****************************************************************************/
1257 static int64_t vlm_Date()
1262 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1268 vlm_schedule_t *vlm_ScheduleNew( vlm_t *vlm, char *psz_name )
1270 vlm_schedule_t *p_sched = malloc( sizeof( vlm_schedule_t ) );
1282 p_sched->psz_name = strdup( psz_name );
1283 p_sched->b_enabled = VLC_FALSE;
1284 p_sched->i_command = 0;
1285 p_sched->command = NULL;
1286 p_sched->i_date = 0;
1287 p_sched->i_period = 0;
1288 p_sched->i_repeat = -1;
1290 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1295 /* for now, simple delete. After, del with options (last arg) */
1296 void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_t *sched,
1299 if( sched == NULL ) return;
1301 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1303 if( vlm->i_schedule == 0 && vlm->schedule ) free( vlm->schedule );
1304 free( sched->psz_name );
1305 while( sched->i_command-- ) free( sched->command[sched->i_command] );
1309 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *vlm, char *psz_name )
1313 for( i = 0; i < vlm->i_schedule; i++ )
1315 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1317 return vlm->schedule[i];
1324 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1325 int vlm_ScheduleSetup( vlm_schedule_t *schedule, char *psz_cmd,
1328 if( !strcmp( psz_cmd, "enabled" ) )
1330 schedule->b_enabled = VLC_TRUE;
1332 else if( !strcmp( psz_cmd, "disabled" ) )
1334 schedule->b_enabled = VLC_FALSE;
1336 #if !defined( UNDER_CE )
1337 else if( !strcmp( psz_cmd, "date" ) )
1343 time.tm_sec = 0; /* seconds */
1344 time.tm_min = 0; /* minutes */
1345 time.tm_hour = 0; /* hours */
1346 time.tm_mday = 0; /* day of the month */
1347 time.tm_mon = 0; /* month */
1348 time.tm_year = 0; /* year */
1349 time.tm_wday = 0; /* day of the week */
1350 time.tm_yday = 0; /* day in the year */
1351 time.tm_isdst = -1; /* daylight saving time */
1353 /* date should be year/month/day-hour:minutes:seconds */
1354 p = strchr( psz_value, '-' );
1356 if( !strcmp( psz_value, "now" ) )
1358 schedule->i_date = 0;
1360 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 */
1368 switch( sscanf( p + 1, "%d:%d:%d", &i, &j, &k ) )
1388 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1394 time.tm_mon = i - 1;
1398 time.tm_year = i - 1900;
1399 time.tm_mon = j - 1;
1406 date = mktime( &time );
1407 schedule->i_date = ((mtime_t) date) * 1000000;
1410 else if( !strcmp( psz_cmd, "period" ) )
1414 char *psz_time = NULL, *psz_date = NULL;
1418 /* First, if date or period are modified, repeat should be equal to -1 */
1419 schedule->i_repeat = -1;
1421 time.tm_sec = 0; /* seconds */
1422 time.tm_min = 0; /* minutes */
1423 time.tm_hour = 0; /* hours */
1424 time.tm_mday = 0; /* day of the month */
1425 time.tm_mon = 0; /* month */
1426 time.tm_year = 0; /* year */
1427 time.tm_wday = 0; /* day of the week */
1428 time.tm_yday = 0; /* day in the year */
1429 time.tm_isdst = -1; /* daylight saving time */
1431 /* date should be year/month/day-hour:minutes:seconds */
1432 p = strchr( psz_value, '-' );
1435 psz_date = psz_value;
1442 psz_time = psz_value;
1446 switch( sscanf( psz_time, "%d:%d:%d", &i, &j, &k ) )
1465 switch( sscanf( psz_date, "%d/%d/%d", &i, &j, &k ) )
1484 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1485 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1486 schedule->i_period = ((mtime_t) date) * 1000000;
1488 #endif /* UNDER_CE */
1489 else if( !strcmp( psz_cmd, "repeat" ) )
1493 if( sscanf( psz_value, "%d", &i ) == 1 )
1495 schedule->i_repeat = i;
1502 else if( !strcmp( psz_cmd, "append" ) )
1504 char *command = strdup( psz_value );
1506 TAB_APPEND( schedule->i_command, schedule->command, command );
1515 /*****************************************************************************
1516 * Message handling functions
1517 *****************************************************************************/
1518 static vlm_message_t *vlm_MessageNew( char *psz_name,
1519 const char *psz_format, ... )
1521 vlm_message_t *p_message;
1524 if( !psz_name ) return 0;
1526 p_message = malloc( sizeof(vlm_message_t) );
1532 p_message->psz_value = 0;
1536 va_start( args, psz_format );
1537 if( vasprintf( &p_message->psz_value, psz_format, args ) < 0 )
1546 p_message->psz_name = strdup( psz_name );
1547 p_message->i_child = 0;
1548 p_message->child = NULL;
1553 void vlm_MessageDelete( vlm_message_t *p_message )
1555 if( p_message->psz_name ) free( p_message->psz_name );
1556 if( p_message->psz_value ) free( p_message->psz_value );
1557 while( p_message->i_child-- )
1558 vlm_MessageDelete( p_message->child[p_message->i_child] );
1559 if( p_message->child ) free( p_message->child );
1564 static vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1565 vlm_message_t *p_child )
1567 if( p_message == NULL ) return NULL;
1571 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1577 /*****************************************************************************
1578 * Misc utility functions
1579 *****************************************************************************/
1580 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_t *media,
1581 vlm_schedule_t *schedule, char *psz_filter )
1587 vlm_message_t *msg_media;
1588 vlm_message_t *msg_child;
1590 msg = vlm_MessageNew( "show", NULL );
1591 msg_media = vlm_MessageAdd( msg, vlm_MessageNew( media->psz_name, 0 ));
1593 vlm_MessageAdd( msg_media,
1594 vlm_MessageNew( "type", media->i_type == VOD_TYPE ?
1595 "vod" : "broadcast" ) );
1596 vlm_MessageAdd( msg_media,
1597 vlm_MessageNew( "enabled", media->b_enabled ?
1600 vlm_MessageAdd( msg_media,
1601 vlm_MessageNew( "loop", media->b_loop ?
1604 if( media->i_type == VOD_TYPE && media->psz_mux )
1605 vlm_MessageAdd( msg_media,
1606 vlm_MessageNew( "mux", media->psz_mux ) );
1608 msg_child = vlm_MessageAdd( msg_media,
1609 vlm_MessageNew( "inputs", NULL ) );
1611 for( i = 0; i < media->i_input; i++ )
1613 vlm_MessageAdd( msg_child,
1614 vlm_MessageNew( media->input[i], NULL ) );
1617 vlm_MessageAdd( msg_media,
1618 vlm_MessageNew( "output", media->psz_output ?
1619 media->psz_output : "" ) );
1621 msg_child = vlm_MessageAdd( msg_media, vlm_MessageNew( "options", 0 ));
1623 for( i = 0; i < media->i_option; i++ )
1625 vlm_MessageAdd( msg_child, vlm_MessageNew( media->option[i], 0 ) );
1628 msg_child = vlm_MessageAdd( msg_media,
1629 vlm_MessageNew( "instances", NULL ) );
1631 for( i = 0; i < media->i_instance; i++ )
1633 vlm_media_instance_t *p_instance = media->instance[i];
1636 if( !p_instance->p_input ) val.i_int = END_S;
1637 else var_Get( p_instance->p_input, "state", &val );
1639 vlm_MessageAdd( msg_child,
1640 vlm_MessageNew( p_instance->psz_name ?
1641 p_instance->psz_name : "default",
1642 val.i_int == PLAYING_S ? "playing" :
1643 val.i_int == PAUSE_S ? "paused" :
1651 else if( schedule != NULL )
1655 vlm_message_t *msg_schedule;
1656 vlm_message_t *msg_child;
1659 msg = vlm_MessageNew( "show", NULL );
1661 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, 0 ) );
1663 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1665 vlm_MessageAdd( msg_schedule,
1666 vlm_MessageNew( "enabled", schedule->b_enabled ?
1669 #if !defined( UNDER_CE )
1670 if( schedule->i_date != 0 )
1673 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1676 #ifdef HAVE_LOCALTIME_R
1677 localtime_r( &i_time, &date);
1679 struct tm *p_date = localtime( &i_time );
1683 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1684 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1685 date.tm_hour, date.tm_min, date.tm_sec );
1687 vlm_MessageAdd( msg_schedule,
1688 vlm_MessageNew( "date", psz_date ) );
1693 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1696 if( schedule->i_period != 0 )
1698 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1701 date.tm_sec = (int)( i_time % 60 );
1702 i_time = i_time / 60;
1703 date.tm_min = (int)( i_time % 60 );
1704 i_time = i_time / 60;
1705 date.tm_hour = (int)( i_time % 24 );
1706 i_time = i_time / 24;
1707 date.tm_mday = (int)( i_time % 30 );
1708 i_time = i_time / 30;
1709 /* okay, okay, months are not always 30 days long */
1710 date.tm_mon = (int)( i_time % 12 );
1711 i_time = i_time / 12;
1712 date.tm_year = (int)i_time;
1714 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1715 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1717 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1721 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1723 #endif /* UNDER_CE */
1725 sprintf( buffer, "%d", schedule->i_repeat );
1726 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1729 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", 0) );
1731 for( i = 0; i < schedule->i_command; i++ )
1733 vlm_MessageAdd( msg_child,
1734 vlm_MessageNew( schedule->command[i], NULL ) );
1741 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1745 vlm_message_t *msg_child;
1746 int i_vod = 0, i_broadcast = 0;
1749 for( i = 0; i < vlm->i_media; i++ )
1751 if( vlm->media[i]->i_type == VOD_TYPE )
1757 asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1759 msg = vlm_MessageNew( "show", NULL );
1760 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "media", psz_count ) );
1763 for( i = 0; i < vlm->i_media; i++ )
1765 vlm_media_t *m = vlm->media[i];
1766 vlm_message_t *msg_media, *msg_instance;
1768 msg_media = vlm_MessageAdd( msg_child,
1769 vlm_MessageNew( m->psz_name, 0 ) );
1771 vlm_MessageAdd( msg_media,
1772 vlm_MessageNew( "type", m->i_type == VOD_TYPE ?
1773 "vod" : "broadcast" ) );
1775 vlm_MessageAdd( msg_media,
1776 vlm_MessageNew( "enabled", m->b_enabled ?
1779 if( m->i_type == VOD_TYPE && m->psz_mux )
1780 vlm_MessageAdd( msg_media,
1781 vlm_MessageNew( "mux", m->psz_mux ) );
1783 msg_instance = vlm_MessageAdd( msg_media,
1784 vlm_MessageNew( "instances", 0 ) );
1786 for( j = 0; j < m->i_instance; j++ )
1788 vlm_media_instance_t *p_instance = m->instance[j];
1791 if( !p_instance->p_input ) val.i_int = END_S;
1792 else var_Get( p_instance->p_input, "state", &val );
1794 vlm_MessageAdd( msg_instance,
1795 vlm_MessageNew( p_instance->psz_name ?
1796 p_instance->psz_name : "default",
1797 val.i_int == PLAYING_S ? "playing" :
1798 val.i_int == PAUSE_S ? "paused" :
1806 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1810 vlm_message_t *msg_child;
1812 msg = vlm_MessageNew( "show", NULL );
1813 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", NULL ) );
1815 for( i = 0; i < vlm->i_schedule; i++ )
1817 vlm_schedule_t *s = vlm->schedule[i];
1818 vlm_message_t *msg_schedule;
1819 mtime_t i_time, i_next_date;
1821 msg_schedule = vlm_MessageAdd( msg_child,
1822 vlm_MessageNew( s->psz_name, 0 ) );
1823 vlm_MessageAdd( msg_schedule,
1824 vlm_MessageNew( "enabled", s->b_enabled ?
1827 if( !s->b_enabled ) return msg;
1830 vlm_MessageAdd( msg_schedule,
1831 vlm_MessageNew( "enabled", "yes" ) );
1833 /* calculate next date */
1834 i_time = vlm_Date();
1835 i_next_date = s->i_date;
1837 if( s->i_period != 0 )
1840 while( s->i_date + j * s->i_period <= i_time &&
1846 i_next_date = s->i_date + j * s->i_period;
1849 if( i_next_date > i_time )
1851 time_t i_date = (time_t) (i_next_date / 1000000) ;
1853 #if !defined( UNDER_CE )
1856 ctime_r( &i_date, psz_date );
1858 char *psz_date = ctime( &i_date );
1861 vlm_MessageAdd( msg_schedule,
1862 vlm_MessageNew( "next launch", psz_date ) );
1870 else if( psz_filter == NULL && media == NULL && schedule == NULL )
1872 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1873 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1875 vlm_MessageAdd( show1, show2->child[0] );
1877 /* We must destroy the parent node "show" of show2
1878 * and not the children */
1879 free( show2->psz_name );
1887 return vlm_MessageNew( "show", NULL );
1891 static vlm_message_t *vlm_Help( vlm_t *vlm, char *psz_filter )
1893 vlm_message_t *message, *message_child;
1895 #define MessageAdd( a ) \
1896 vlm_MessageAdd( message, vlm_MessageNew( a, NULL ) );
1897 #define MessageAddChild( a ) \
1898 vlm_MessageAdd( message_child, vlm_MessageNew( a, NULL ) );
1900 if( psz_filter == NULL )
1902 message = vlm_MessageNew( "help", NULL );
1904 message_child = MessageAdd( "Commands Syntax:" );
1905 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
1906 MessageAddChild( "setup (name) (properties)" );
1907 MessageAddChild( "show [(name)|media|schedule]" );
1908 MessageAddChild( "del (name)|all|media|schedule" );
1909 MessageAddChild( "control (name) [instance_name] (command)" );
1910 MessageAddChild( "save (config_file)" );
1911 MessageAddChild( "load (config_file)" );
1913 message_child = MessageAdd( "Media Proprieties Syntax:" );
1914 MessageAddChild( "input (input_name)" );
1915 MessageAddChild( "inputdel (input_name)|all" );
1916 MessageAddChild( "inputdeln input_number" );
1917 MessageAddChild( "output (output_name)" );
1918 MessageAddChild( "option (option_name)[=value]" );
1919 MessageAddChild( "enabled|disabled" );
1920 MessageAddChild( "loop|unloop (broadcast only)" );
1921 MessageAddChild( "mux (mux_name)" );
1923 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
1924 MessageAddChild( "enabled|disabled" );
1925 MessageAddChild( "append (command_until_rest_of_the_line)" );
1926 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
1928 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
1929 "(days)-(hours):(minutes):(seconds)" );
1930 MessageAddChild( "repeat (number_of_repetitions)" );
1932 message_child = MessageAdd( "Control Commands Syntax:" );
1933 MessageAddChild( "play" );
1934 MessageAddChild( "pause" );
1935 MessageAddChild( "stop" );
1936 MessageAddChild( "seek (percentage)" );
1941 return vlm_MessageNew( "help", NULL );
1944 /*****************************************************************************
1945 * Config handling functions
1946 *****************************************************************************/
1947 static int Load( vlm_t *vlm, char *file )
1951 while( *pf != '\0' )
1953 vlm_message_t *message = NULL;
1956 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1961 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1965 if( pf[i_end] == '\n' ) i_end++;
1968 if( *pf && ExecuteCommand( vlm, pf, &message ) )
1970 if( message ) free( message );
1973 if( message ) free( message );
1981 static char *Save( vlm_t *vlm )
1988 for( i = 0; i < vlm->i_media; i++ )
1990 vlm_media_t *media = vlm->media[i];
1992 if( media->i_type == VOD_TYPE )
1994 i_length += strlen( "new vod " ) + strlen(media->psz_name);
1998 i_length += strlen( "new broadcast " ) + strlen(media->psz_name);
2001 if( media->b_enabled == VLC_TRUE )
2003 i_length += strlen( "enabled" );
2007 i_length += strlen( "disabled" );
2010 if( media->b_loop == VLC_TRUE )
2012 i_length += strlen( " loop\n" );
2016 i_length += strlen( "\n" );
2019 for( j = 0; j < media->i_input; j++ )
2021 i_length += strlen( "setup input \"\"\n" ) +
2022 strlen( media->psz_name ) + strlen( media->input[j] );
2025 if( media->psz_output != NULL )
2027 i_length += strlen(media->psz_name) + strlen(media->psz_output) +
2028 strlen( "setup output \n" );
2031 for( j=0 ; j < media->i_option ; j++ )
2033 i_length += strlen(media->psz_name) + strlen(media->option[j]) +
2034 strlen("setup option \n");
2038 for( i = 0; i < vlm->i_schedule; i++ )
2040 vlm_schedule_t *schedule = vlm->schedule[i];
2042 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
2044 if( schedule->b_enabled == VLC_TRUE )
2046 i_length += strlen( "date //-:: enabled\n" ) + 14;
2050 i_length += strlen( "date //-:: disabled\n" ) + 14;
2054 if( schedule->i_period != 0 )
2056 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
2057 strlen( "period //-::\n" ) + 14;
2060 if( schedule->i_repeat >= 0 )
2064 sprintf( buffer, "%d", schedule->i_repeat );
2065 i_length += strlen( "setup repeat \n" ) +
2066 strlen( schedule->psz_name ) + strlen( buffer );
2073 for( j = 0; j < schedule->i_command; j++ )
2075 i_length += strlen( "setup append \n" ) +
2076 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
2081 /* Don't forget the '\0' */
2083 /* now we have the length of save */
2085 p = save = malloc( i_length );
2088 /* finally we can write in it */
2089 for( i = 0; i < vlm->i_media; i++ )
2091 vlm_media_t *media = vlm->media[i];
2093 if( media->i_type == VOD_TYPE )
2095 p += sprintf( p, "new %s vod ", media->psz_name);
2099 p += sprintf( p, "new %s broadcast ", media->psz_name);
2102 if( media->b_enabled == VLC_TRUE )
2104 p += sprintf( p, "enabled" );
2108 p += sprintf( p, "disabled" );
2111 if( media->b_loop == VLC_TRUE )
2113 p += sprintf( p, " loop\n" );
2117 p += sprintf( p, "\n" );
2120 for( j = 0; j < media->i_input; j++ )
2122 p += sprintf( p, "setup %s input \"%s\"\n", media->psz_name,
2126 if( media->psz_output != NULL )
2128 p += sprintf( p, "setup %s output %s\n", media->psz_name,
2129 media->psz_output );
2132 for( j = 0; j < media->i_option; j++ )
2134 p += sprintf( p, "setup %s option %s\n", media->psz_name,
2139 /* and now, the schedule scripts */
2140 #if !defined( UNDER_CE )
2141 for( i = 0; i < vlm->i_schedule; i++ )
2143 vlm_schedule_t *schedule = vlm->schedule[i];
2145 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
2147 #ifdef HAVE_LOCALTIME_R
2148 localtime_r( &i_time, &date);
2150 struct tm *p_date = localtime( &i_time );
2154 p += sprintf( p, "new %s schedule ", schedule->psz_name);
2156 if( schedule->b_enabled == VLC_TRUE )
2158 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
2159 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2160 date.tm_hour, date.tm_min, date.tm_sec );
2164 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
2165 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2166 date.tm_hour, date.tm_min, date.tm_sec);
2169 if( schedule->i_period != 0 )
2171 p += sprintf( p, "setup %s ", schedule->psz_name );
2173 i_time = (time_t) ( schedule->i_period / 1000000 );
2175 date.tm_sec = (int)( i_time % 60 );
2176 i_time = i_time / 60;
2177 date.tm_min = (int)( i_time % 60 );
2178 i_time = i_time / 60;
2179 date.tm_hour = (int)( i_time % 24 );
2180 i_time = i_time / 24;
2181 date.tm_mday = (int)( i_time % 30 );
2182 i_time = i_time / 30;
2183 /* okay, okay, months are not always 30 days long */
2184 date.tm_mon = (int)( i_time % 12 );
2185 i_time = i_time / 12;
2186 date.tm_year = (int)i_time;
2188 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
2189 date.tm_year, date.tm_mon, date.tm_mday,
2190 date.tm_hour, date.tm_min, date.tm_sec);
2193 if( schedule->i_repeat >= 0 )
2195 p += sprintf( p, "setup %s repeat %d\n",
2196 schedule->psz_name, schedule->i_repeat );
2200 p += sprintf( p, "\n" );
2203 for( j = 0; j < schedule->i_command; j++ )
2205 p += sprintf( p, "setup %s append %s\n",
2206 schedule->psz_name, schedule->command[j] );
2210 #endif /* UNDER_CE */
2215 /*****************************************************************************
2217 *****************************************************************************/
2218 int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
2219 char *psz_id, int i_query, va_list args )
2221 vlm_t *vlm = (vlm_t *)p_private;
2222 int i, i_ret = VLC_EGENERIC;
2224 if( !p_private || !p_vod_media ) return VLC_EGENERIC;
2226 vlc_mutex_lock( &vlm->lock );
2229 for( i = 0; i < vlm->i_media; i++ )
2231 if( p_vod_media == vlm->media[i]->vod_media ) break;
2234 if( i == vlm->i_media )
2236 vlc_mutex_unlock( &vlm->lock );
2237 return VLC_EGENERIC;
2242 case VOD_MEDIA_PLAY:
2243 vlm->media[i]->psz_vod_output = (char *)va_arg( args, char * );
2244 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "play", 0 );
2245 vlm->media[i]->psz_vod_output = 0;
2248 case VOD_MEDIA_PAUSE:
2249 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "pause", 0 );
2252 case VOD_MEDIA_STOP:
2253 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "stop", 0 );
2256 case VOD_MEDIA_SEEK:
2258 double f_pos = (double)va_arg( args, double );
2261 sprintf( psz_pos, "%f", f_pos );
2262 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "seek", psz_pos);
2270 vlc_mutex_unlock( &vlm->lock );
2275 /*****************************************************************************
2277 *****************************************************************************/
2278 static int Manage( vlc_object_t* p_object )
2280 vlm_t *vlm = (vlm_t*)p_object;
2282 mtime_t i_lastcheck;
2285 i_lastcheck = vlm_Date();
2289 while( !vlm->b_die )
2291 vlc_mutex_lock( &vlm->lock );
2293 /* destroy the inputs that wants to die, and launch the next input */
2294 for( i = 0; i < vlm->i_media; i++ )
2296 vlm_media_t *p_media = vlm->media[i];
2298 for( j = 0; j < p_media->i_instance; j++ )
2300 vlm_media_instance_t *p_instance = p_media->instance[j];
2302 if( !p_instance->p_input ||
2303 ( !p_instance->p_input->b_eof &&
2304 !p_instance->p_input->b_error ) ) continue;
2306 input_StopThread( p_instance->p_input );
2307 input_DestroyThread( p_instance->p_input );
2308 vlc_object_detach( p_instance->p_input );
2309 vlc_object_destroy( p_instance->p_input );
2311 p_instance->i_index++;
2312 if( p_instance->i_index == p_media->i_input &&
2313 p_media->b_loop ) p_instance->i_index = 0;
2315 if( p_instance->i_index < p_media->i_input )
2317 /* FIXME, find a way to select the right instance */
2319 sprintf( buffer, "%d", p_instance->i_index );
2320 vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2325 if( vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2326 "stop", 0 ) == VLC_SUCCESS ) i--;
2332 i_time = vlm_Date();
2334 for( i = 0; i < vlm->i_schedule; i++ )
2336 mtime_t i_real_date = vlm->schedule[i]->i_date;
2338 if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2340 if( vlm->schedule[i]->i_date == 0 ) // now !
2342 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2343 i_real_date = i_time;
2345 else if( vlm->schedule[i]->i_period != 0 )
2348 while( vlm->schedule[i]->i_date + j *
2349 vlm->schedule[i]->i_period <= i_lastcheck &&
2350 ( vlm->schedule[i]->i_repeat > j ||
2351 vlm->schedule[i]->i_repeat == -1 ) )
2356 i_real_date = vlm->schedule[i]->i_date + j *
2357 vlm->schedule[i]->i_period;
2360 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2362 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2364 vlm_message_t *message = NULL;
2366 ExecuteCommand( vlm, vlm->schedule[i]->command[j],
2369 /* for now, drop the message */
2376 i_lastcheck = i_time;
2378 vlc_mutex_unlock( &vlm->lock );
2386 #else /* ENABLE_VLM */
2388 /* We just define an empty wrapper */
2389 vlm_t *__vlm_New( vlc_object_t *a )
2391 msg_Err( a, "VideoLAN manager support is disabled" );
2394 void vlm_Delete( vlm_t *a ){}
2395 int vlm_ExecuteCommand( vlm_t *a, char *b, vlm_message_t **c ){ return -1; }
2396 void vlm_MessageDelete( vlm_message_t *a ){}
2397 vlm_media_t *vlm_MediaNew( vlm_t *a, char *b, int c ){ return NULL; }
2398 void vlm_MediaDelete( vlm_t *a, vlm_media_t *b, char *c ){}
2399 int vlm_MediaSetup( vlm_t *a, vlm_media_t *b, char *c, char *d ){ return -1; }
2400 int vlm_MediaControl( vlm_t *a, vlm_media_t *b, char *c, char *d, char *e )
2402 vlm_schedule_t * vlm_ScheduleNew( vlm_t *a, char *b ){ return NULL; }
2403 void vlm_ScheduleDelete( vlm_t *a, vlm_schedule_t *b, char *c ){}
2404 int vlm_ScheduleSetup( vlm_schedule_t *a, char *b, char *c ){ return -1; }
2405 int vlm_MediaVodControl( void *a, vod_media_t *b, char *c, int d, va_list e )
2407 int vlm_Save( vlm_t *a, char *b ){ return -1; }
2408 int vlm_Load( vlm_t *a, char *b ){ return -1; }
2410 #endif /* ENABLE_VLM */