1 /*****************************************************************************
2 * vlm.c: VLM interface plugin
3 *****************************************************************************
4 * Copyright (C) 2000-2005 the VideoLAN team
7 * Authors: Simon Latapie <garf@videolan.org>
8 * Laurent Aimar <fenrir@videolan.org>
9 * Gildas Bazin <gbazin@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 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 )
365 p_message = vlm_MessageNew( "Incomplete command", psz_cmd );
369 i_temp = psz_temp - psz_cmd;
371 ppsz_command = realloc( ppsz_command, (i_command + 1) *
373 ppsz_command[ i_command ] = malloc( (i_temp + 1) * sizeof(char) );
374 strncpy( ppsz_command[ i_command ], psz_cmd, i_temp );
375 ppsz_command[ i_command ][ i_temp ] = '\0';
384 * And then Interpret it
389 p_message = vlm_MessageNew( "", NULL );
393 if( !strcmp(ppsz_command[0], "new") )
397 /* Check the number of arguments */
398 if( i_command < 3 ) goto syntax_error;
401 if( !strcmp(ppsz_command[2], "vod") )
405 else if( !strcmp(ppsz_command[2], "broadcast") )
407 i_type = BROADCAST_TYPE;
409 else if( !strcmp(ppsz_command[2], "schedule") )
411 i_type = SCHEDULE_TYPE;
416 vlm_MessageNew( "new", "%s: Choose between vod, "
417 "broadcast or schedule", ppsz_command[1] );
421 /* Check for forbidden media names */
422 if( !strcmp(ppsz_command[1], "all") ||
423 !strcmp(ppsz_command[1], "media") ||
424 !strcmp(ppsz_command[1], "schedule") )
426 p_message = vlm_MessageNew( "new", "\"all\", \"media\" and "
427 "\"schedule\" are reserved names" );
431 /* Check the name is not already in use */
432 if( vlm_ScheduleSearch( p_vlm, ppsz_command[1] ) ||
433 vlm_MediaSearch( p_vlm, ppsz_command[1] ) )
435 p_message = vlm_MessageNew( "new", "%s: Name already in use",
441 if( i_type == SCHEDULE_TYPE )
443 vlm_schedule_t *p_schedule;
444 p_schedule = vlm_ScheduleNew( p_vlm, ppsz_command[1] );
447 p_message = vlm_MessageNew( "new", "could not create schedule" );
455 vlm_media_t *p_media;
456 p_media = vlm_MediaNew( p_vlm, ppsz_command[1], i_type );
459 p_message = vlm_MessageNew( "new", "could not create media" );
466 p_message = vlm_MessageNew( "new", NULL );
470 /* Properties will be dealt with later on */
473 else if( !strcmp(ppsz_command[0], "setup") )
475 if( i_command < 2 ) goto syntax_error;
477 /* Properties will be dealt with later on */
480 else if( !strcmp(ppsz_command[0], "del") )
482 vlm_media_t *p_media;
483 vlm_schedule_t *p_schedule;
485 if( i_command < 2 ) goto syntax_error;
487 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
488 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
490 if( p_schedule != NULL )
492 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
494 else if( p_media != NULL )
496 vlm_MediaDelete( p_vlm, p_media, NULL );
498 else if( !strcmp(ppsz_command[1], "media") )
500 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
503 else if( !strcmp(ppsz_command[1], "schedule") )
505 while( p_vlm->i_schedule )
506 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
508 else if( !strcmp(ppsz_command[1], "all") )
510 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
513 while( p_vlm->i_schedule )
514 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
518 p_message = vlm_MessageNew( "del", "%s: media unknown",
523 p_message = vlm_MessageNew( "del", NULL );
527 else if( !strcmp(ppsz_command[0], "show") )
529 vlm_media_t *p_media;
530 vlm_schedule_t *p_schedule;
534 p_message = vlm_Show( p_vlm, NULL, NULL, NULL );
537 else if( i_command > 2 ) goto syntax_error;
539 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
540 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
542 if( p_schedule != NULL )
544 p_message = vlm_Show( p_vlm, NULL, p_schedule, NULL );
546 else if( p_media != NULL )
548 p_message = vlm_Show( p_vlm, p_media, NULL, NULL );
552 p_message = vlm_Show( p_vlm, NULL, NULL, ppsz_command[1] );
558 else if( !strcmp(ppsz_command[0], "help") )
560 if( i_command != 1 ) goto syntax_error;
562 p_message = vlm_Help( p_vlm, NULL );
566 else if( !strcmp(ppsz_command[0], "control") )
568 vlm_media_t *p_media;
570 if( i_command < 3 ) goto syntax_error;
572 if( !(p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] ) ) )
574 p_message = vlm_MessageNew( "control", "%s: media unknown",
580 char *psz_command, *psz_arg = 0, *psz_instance = 0;
583 if( strcmp( ppsz_command[2], "play" ) &&
584 strcmp( ppsz_command[2], "stop" ) &&
585 strcmp( ppsz_command[2], "pause" ) &&
586 strcmp( ppsz_command[2], "seek" ) )
589 psz_instance = ppsz_command[2];
591 if( i_command < 4 ) goto syntax_error;
594 psz_command = ppsz_command[i_index];
596 if( i_command >= i_index + 2 ) psz_arg = ppsz_command[i_index + 1];
598 vlm_MediaControl( p_vlm, p_media, psz_instance, psz_command,
600 p_message = vlm_MessageNew( "control", NULL );
605 else if( !strcmp(ppsz_command[0], "save") )
607 if( i_command != 2 ) goto syntax_error;
609 if( vlm_Save( p_vlm, ppsz_command[1] ) )
611 p_message = vlm_MessageNew( "save", "Unable to save to file" );
616 p_message = vlm_MessageNew( "save", NULL );
621 else if( !strcmp(ppsz_command[0], "load") )
623 if( i_command != 2 ) goto syntax_error;
625 switch( vlm_Load( p_vlm, ppsz_command[1] ) )
628 p_message = vlm_MessageNew( "load", NULL );
631 p_message = vlm_MessageNew( "load", "read file error" );
635 vlm_MessageNew( "load", "error while loading file" );
639 vlm_MessageNew( "load", "Unable to load from file" );
646 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
650 /* Common code between "new" and "setup" */
651 if( !strcmp(ppsz_command[0], "new") ||
652 !strcmp(ppsz_command[0], "setup") )
654 int i_command_start = strcmp(ppsz_command[0], "new") ? 2 : 3;
655 vlm_media_t *p_media;
656 vlm_schedule_t *p_schedule;
658 if( i_command < i_command_start ) goto syntax_error;
660 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
661 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
663 if( !p_media && !p_schedule )
665 p_message = vlm_MessageNew( ppsz_command[0], "%s unknown",
670 if( p_schedule != NULL )
672 for( i = i_command_start ; i < i_command ; i++ )
674 if( !strcmp( ppsz_command[i], "enabled" ) ||
675 !strcmp( ppsz_command[i], "disabled" ) )
677 vlm_ScheduleSetup( p_schedule, ppsz_command[i], NULL );
680 /* Beware: everything behind append is considered as
682 else if( !strcmp( ppsz_command[i], "append" ) )
684 if( ++i >= i_command ) break;
686 for( j = i + 1; j < i_command; j++ )
689 realloc( ppsz_command[i], strlen(ppsz_command[i]) +
690 strlen(ppsz_command[j]) + 1 + 1 );
691 strcat( ppsz_command[i], " " );
692 strcat( ppsz_command[i], ppsz_command[j] );
695 vlm_ScheduleSetup( p_schedule, ppsz_command[i - 1],
701 if( i + 1 >= i_command && !strcmp(ppsz_command[0], "new") )
703 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
705 vlm_MessageNew( ppsz_command[0],
706 "Wrong properties syntax" );
709 else if( i + 1 >= i_command )
712 vlm_MessageNew( ppsz_command[0],
713 "Wrong properties syntax" );
717 vlm_ScheduleSetup( p_schedule, ppsz_command[i],
724 else if( p_media != NULL )
726 for( i = i_command_start ; i < i_command ; i++ )
728 if( !strcmp( ppsz_command[i], "enabled" ) ||
729 !strcmp( ppsz_command[i], "disabled" ) )
731 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
733 else if( i + 1 >= i_command &&
734 !strcmp( ppsz_command[i], "mux") )
736 if( p_media->i_type != VOD_TYPE )
738 p_message = vlm_MessageNew( ppsz_command[0],
739 "mux only available for broadcast" );
743 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
748 else if( !strcmp( ppsz_command[i], "loop" ) ||
749 !strcmp( ppsz_command[i], "unloop" ) )
751 if( p_media->i_type != BROADCAST_TYPE )
753 p_message = vlm_MessageNew( ppsz_command[0],
754 "loop only available for broadcast" );
758 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
763 if( i + 1 >= i_command &&
764 !strcmp(ppsz_command[0], "new") )
766 vlm_MediaDelete( p_vlm, p_media, NULL );
768 vlm_MessageNew( ppsz_command[0],
769 "Wrong properties syntax" );
772 else if( i + 1 >= i_command )
775 vlm_MessageNew( ppsz_command[0],
776 "Wrong properties syntax" );
780 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
787 p_message = vlm_MessageNew( ppsz_command[0], NULL );
792 for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
793 FREE( ppsz_command );
794 *pp_message = p_message;
799 p_message = vlm_MessageNew( ppsz_command[0], "Wrong command syntax" );
802 for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
803 FREE( ppsz_command );
804 *pp_message = p_message;
809 static vlm_media_t *vlm_MediaSearch( vlm_t *vlm, char *psz_name )
813 for( i = 0; i < vlm->i_media; i++ )
815 if( strcmp( psz_name, vlm->media[i]->psz_name ) == 0 )
817 return vlm->media[i];
824 /*****************************************************************************
826 *****************************************************************************/
827 static vlm_media_instance_t *
828 vlm_MediaInstanceSearch( vlm_t *vlm, vlm_media_t *media, char *psz_name )
832 for( i = 0; i < media->i_instance; i++ )
834 if( ( !psz_name && !media->instance[i]->psz_name ) ||
835 ( psz_name && media->instance[i]->psz_name &&
836 !strcmp( psz_name, media->instance[i]->psz_name ) ) )
838 return media->instance[i];
845 vlm_media_t *vlm_MediaNew( vlm_t *vlm, char *psz_name, int i_type )
847 vlm_media_t *media = malloc( sizeof( vlm_media_t ) );
851 msg_Err( vlm, "out of memory" );
855 /* Check if we need to load the VOD server */
856 if( i_type == VOD_TYPE && !vlm->i_vod )
858 vlm->vod = vlc_object_create( vlm, VLC_OBJECT_VOD );
859 vlc_object_attach( vlm->vod, vlm );
860 vlm->vod->p_module = module_Need( vlm->vod, "vod server", 0, 0 );
861 if( !vlm->vod->p_module )
863 msg_Err( vlm, "cannot find vod server" );
864 vlc_object_detach( vlm->vod );
865 vlc_object_destroy( vlm->vod );
871 vlm->vod->p_data = vlm;
872 vlm->vod->pf_media_control = vlm_MediaVodControl;
875 if( i_type == VOD_TYPE ) vlm->i_vod++;
877 media->psz_name = strdup( psz_name );
878 media->b_enabled = VLC_FALSE;
879 media->b_loop = VLC_FALSE;
880 media->vod_media = NULL;
881 media->psz_vod_output = NULL;
882 media->psz_mux = NULL;
885 media->psz_output = NULL;
887 media->option = NULL;
888 media->i_type = i_type;
889 media->i_instance = 0;
890 media->instance = NULL;
892 media->item.psz_uri = strdup( psz_name );
893 vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
895 TAB_APPEND( vlm->i_media, vlm->media, media );
900 /* for now, simple delete. After, del with options (last arg) */
901 void vlm_MediaDelete( vlm_t *vlm, vlm_media_t *media, char *psz_name )
903 if( media == NULL ) return;
905 while( media->i_instance )
907 vlm_media_instance_t *p_instance = media->instance[0];
908 vlm_MediaControl( vlm, media, p_instance->psz_name, "stop", 0 );
911 TAB_REMOVE( vlm->i_media, vlm->media, media );
913 if( media->i_type == VOD_TYPE )
915 vlm_MediaSetup( vlm, media, "disabled", 0 );
919 /* Check if we need to unload the VOD server */
920 if( media->i_type == VOD_TYPE && !vlm->i_vod )
922 module_Unneed( vlm->vod, vlm->vod->p_module );
923 vlc_object_detach( vlm->vod );
924 vlc_object_destroy( vlm->vod );
928 if( vlm->i_media == 0 && vlm->media ) free( vlm->media );
930 free( media->psz_name );
932 while( media->i_input-- ) free( media->input[media->i_input] );
933 if( media->input ) free( media->input );
935 if( media->psz_output ) free( media->psz_output );
936 if( media->psz_mux ) free( media->psz_mux );
938 while( media->i_option-- ) free( media->option[media->i_option] );
939 if( media->option ) free( media->option );
941 vlc_input_item_Clean( &media->item );
946 int vlm_MediaSetup( vlm_t *vlm, vlm_media_t *media, char *psz_cmd,
949 if( !psz_cmd) return VLC_EGENERIC;
951 if( !strcmp( psz_cmd, "loop" ) )
953 media->b_loop = VLC_TRUE;
955 else if( !strcmp( psz_cmd, "unloop" ) )
957 media->b_loop = VLC_FALSE;
959 else if( !strcmp( psz_cmd, "enabled" ) )
961 media->b_enabled = VLC_TRUE;
963 else if( !strcmp( psz_cmd, "disabled" ) )
965 media->b_enabled = VLC_FALSE;
967 else if( !strcmp( psz_cmd, "mux" ) )
969 if( media->psz_mux ) free( media->psz_mux );
970 media->psz_mux = NULL;
971 if( psz_value ) media->psz_mux = strdup( psz_value );
973 else if( !strcmp( psz_cmd, "input" ) )
977 if( psz_value != NULL && strlen(psz_value) > 1 &&
978 ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
979 ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
980 psz_value[ strlen(psz_value) - 1 ] == '\"' ) )
982 input = malloc( strlen(psz_value) - 1 );
984 memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
985 input[ strlen(psz_value) - 2 ] = '\0';
989 input = strdup( psz_value );
992 TAB_APPEND( media->i_input, media->input, input );
994 else if( !strcmp( psz_cmd, "inputdel" ) && !strcmp( psz_value, "all" ) )
996 while( media->i_input > 0 )
998 TAB_REMOVE( media->i_input, media->input, media->input[0] );
1001 else if( !strcmp( psz_cmd, "inputdel" ) )
1006 if( psz_value != NULL && strlen(psz_value) > 1 &&
1007 ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
1008 ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
1009 psz_value[ strlen(psz_value) - 1 ] == '\"' ) )
1011 input = malloc( strlen(psz_value) - 1 );
1013 memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
1014 input[ strlen(psz_value) - 2 ] = '\0';
1018 input = strdup( psz_value );
1021 for( i = 0; i < media->i_input; i++ )
1023 if( !strcmp( input, media->input[i] ) )
1025 TAB_REMOVE( media->i_input, media->input, media->input[i] );
1030 else if( !strcmp( psz_cmd, "inputdeln" ) )
1032 int index = atoi( psz_value );
1033 if( index > 0 && index <= media->i_input )
1035 TAB_REMOVE( media->i_input, media->input, media->input[index-1] );
1038 else if( !strcmp( psz_cmd, "output" ) )
1040 if( media->psz_output != NULL )
1042 free( media->psz_output );
1044 media->psz_output = strdup( psz_value );
1046 else if( !strcmp( psz_cmd, "option" ) )
1049 psz_option = strdup( psz_value );
1051 TAB_APPEND( media->i_option, media->option, psz_option );
1055 return VLC_EGENERIC;
1058 /* Check if we need to create/delete a vod media */
1059 if( media->i_type == VOD_TYPE )
1061 if( !media->b_enabled && media->vod_media )
1063 vlm->vod->pf_media_del( vlm->vod, media->vod_media );
1064 media->vod_media = 0;
1066 else if( media->b_enabled && !media->vod_media && media->i_input )
1068 /* Pre-parse the input */
1069 input_thread_t *p_input;
1073 vlc_input_item_Clean( &media->item );
1074 vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
1076 if( media->psz_output )
1077 asprintf( &psz_output, "%s:description", media->psz_output );
1079 asprintf( &psz_output, "#description" );
1081 media->item.psz_uri = strdup( media->input[0] );
1082 media->item.ppsz_options = malloc( sizeof( char* ) );
1083 asprintf( &media->item.ppsz_options[0], "sout=%s", psz_output);
1084 media->item.i_options = 1;
1085 for( i = 0; i < media->i_option; i++ )
1087 media->item.i_options++;
1088 media->item.ppsz_options =
1089 realloc( media->item.ppsz_options,
1090 media->item.i_options * sizeof( char* ) );
1091 media->item.ppsz_options[ media->item.i_options - 1 ] =
1092 strdup( media->option[i] );
1095 if( (p_input = input_CreateThread( vlm, &media->item ) ) )
1097 while( !p_input->b_eof && !p_input->b_error ) msleep( 100000 );
1099 input_StopThread( p_input );
1100 input_DestroyThread( p_input );
1101 vlc_object_detach( p_input );
1102 vlc_object_destroy( p_input );
1106 if( media->psz_mux )
1109 es_format_t es, *p_es = &es;
1112 sprintf( fourcc, "%4.4s", media->psz_mux );
1113 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
1114 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
1119 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
1122 vlm->vod->pf_media_new( vlm->vod, media->psz_name, &item );
1127 vlm->vod->pf_media_new( vlm->vod, media->psz_name,
1135 int vlm_MediaControl( vlm_t *vlm, vlm_media_t *media, char *psz_id,
1136 char *psz_command, char *psz_args )
1138 vlm_media_instance_t *p_instance;
1141 p_instance = vlm_MediaInstanceSearch( vlm, media, psz_id );
1143 if( !strcmp( psz_command, "play" ) )
1145 if( !media->b_enabled || media->i_input == 0 ) return 0;
1149 p_instance = malloc( sizeof(vlm_media_instance_t) );
1150 if( !p_instance ) return VLC_EGENERIC;
1151 memset( p_instance, 0, sizeof(vlm_media_instance_t) );
1152 vlc_input_item_Init( VLC_OBJECT(vlm), &p_instance->item );
1153 p_instance->p_input = NULL;
1155 if( media->psz_output != NULL || media->psz_vod_output != NULL )
1157 p_instance->item.ppsz_options = malloc( sizeof( char* ) );
1158 asprintf( &p_instance->item.ppsz_options[0], "sout=%s%s%s",
1159 media->psz_output ? media->psz_output : "",
1160 (media->psz_output && media->psz_vod_output) ?
1161 ":" : media->psz_vod_output ? "#" : "",
1162 media->psz_vod_output ? media->psz_vod_output : "" );
1163 p_instance->item.i_options = 1;
1166 for( i = 0; i < media->i_option; i++ )
1168 p_instance->item.i_options++;
1169 p_instance->item.ppsz_options =
1170 realloc( p_instance->item.ppsz_options,
1171 p_instance->item.i_options * sizeof( char* ) );
1172 p_instance->item.ppsz_options[p_instance->item.i_options - 1] =
1173 strdup( media->option[i] );
1176 p_instance->psz_name = psz_id ? strdup( psz_id ) : NULL;
1177 TAB_APPEND( media->i_instance, media->instance, p_instance );
1180 if( psz_args && sscanf(psz_args, "%d", &i) == 1 && i < media->i_input )
1182 p_instance->i_index = i;
1185 if( p_instance->item.psz_uri ) free( p_instance->item.psz_uri );
1186 p_instance->item.psz_uri =
1187 strdup( media->input[p_instance->i_index] );
1189 if( p_instance->p_input )
1191 input_StopThread( p_instance->p_input );
1192 input_DestroyThread( p_instance->p_input );
1193 vlc_object_detach( p_instance->p_input );
1194 vlc_object_destroy( p_instance->p_input );
1197 p_instance->p_input = input_CreateThread( vlm, &p_instance->item );
1198 if( !p_instance->p_input )
1200 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1201 vlc_input_item_Clean( &p_instance->item );
1202 if( p_instance->psz_name ) free( p_instance->psz_name );
1208 if( !p_instance ) return VLC_EGENERIC;
1210 if( !strcmp( psz_command, "seek" ) )
1215 if( psz_args && sscanf( psz_args, "%f", &f_percentage ) == 1 )
1217 val.f_float = f_percentage / 100.0 ;
1218 var_Set( p_instance->p_input, "position", val );
1222 else if( !strcmp( psz_command, "stop" ) )
1224 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1226 if( p_instance->p_input )
1228 input_StopThread( p_instance->p_input );
1229 input_DestroyThread( p_instance->p_input );
1230 vlc_object_detach( p_instance->p_input );
1231 vlc_object_destroy( p_instance->p_input );
1234 vlc_input_item_Clean( &p_instance->item );
1235 if( p_instance->psz_name ) free( p_instance->psz_name );
1240 else if( !strcmp( psz_command, "pause" ) )
1244 if( !p_instance->p_input ) return VLC_SUCCESS;
1246 var_Get( p_instance->p_input, "state", &val );
1248 if( val.i_int == PAUSE_S ) val.i_int = PLAYING_S;
1249 else val.i_int = PAUSE_S;
1250 var_Set( p_instance->p_input, "state", val );
1255 return VLC_EGENERIC;
1258 /*****************************************************************************
1260 *****************************************************************************/
1261 static int64_t vlm_Date()
1266 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1272 vlm_schedule_t *vlm_ScheduleNew( vlm_t *vlm, char *psz_name )
1274 vlm_schedule_t *p_sched = malloc( sizeof( vlm_schedule_t ) );
1286 p_sched->psz_name = strdup( psz_name );
1287 p_sched->b_enabled = VLC_FALSE;
1288 p_sched->i_command = 0;
1289 p_sched->command = NULL;
1290 p_sched->i_date = 0;
1291 p_sched->i_period = 0;
1292 p_sched->i_repeat = -1;
1294 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1299 /* for now, simple delete. After, del with options (last arg) */
1300 void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_t *sched,
1303 if( sched == NULL ) return;
1305 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1307 if( vlm->i_schedule == 0 && vlm->schedule ) free( vlm->schedule );
1308 free( sched->psz_name );
1309 while( sched->i_command-- ) free( sched->command[sched->i_command] );
1313 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *vlm, char *psz_name )
1317 for( i = 0; i < vlm->i_schedule; i++ )
1319 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1321 return vlm->schedule[i];
1328 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1329 int vlm_ScheduleSetup( vlm_schedule_t *schedule, char *psz_cmd,
1332 if( !strcmp( psz_cmd, "enabled" ) )
1334 schedule->b_enabled = VLC_TRUE;
1336 else if( !strcmp( psz_cmd, "disabled" ) )
1338 schedule->b_enabled = VLC_FALSE;
1340 #if !defined( UNDER_CE )
1341 else if( !strcmp( psz_cmd, "date" ) )
1347 time.tm_sec = 0; /* seconds */
1348 time.tm_min = 0; /* minutes */
1349 time.tm_hour = 0; /* hours */
1350 time.tm_mday = 0; /* day of the month */
1351 time.tm_mon = 0; /* month */
1352 time.tm_year = 0; /* year */
1353 time.tm_wday = 0; /* day of the week */
1354 time.tm_yday = 0; /* day in the year */
1355 time.tm_isdst = -1; /* daylight saving time */
1357 /* date should be year/month/day-hour:minutes:seconds */
1358 p = strchr( psz_value, '-' );
1360 if( !strcmp( psz_value, "now" ) )
1362 schedule->i_date = 0;
1364 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 */
1372 switch( sscanf( p + 1, "%d:%d:%d", &i, &j, &k ) )
1392 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1398 time.tm_mon = i - 1;
1402 time.tm_year = i - 1900;
1403 time.tm_mon = j - 1;
1410 date = mktime( &time );
1411 schedule->i_date = ((mtime_t) date) * 1000000;
1414 else if( !strcmp( psz_cmd, "period" ) )
1418 char *psz_time = NULL, *psz_date = NULL;
1422 /* First, if date or period are modified, repeat should be equal to -1 */
1423 schedule->i_repeat = -1;
1425 time.tm_sec = 0; /* seconds */
1426 time.tm_min = 0; /* minutes */
1427 time.tm_hour = 0; /* hours */
1428 time.tm_mday = 0; /* day of the month */
1429 time.tm_mon = 0; /* month */
1430 time.tm_year = 0; /* year */
1431 time.tm_wday = 0; /* day of the week */
1432 time.tm_yday = 0; /* day in the year */
1433 time.tm_isdst = -1; /* daylight saving time */
1435 /* date should be year/month/day-hour:minutes:seconds */
1436 p = strchr( psz_value, '-' );
1439 psz_date = psz_value;
1446 psz_time = psz_value;
1450 switch( sscanf( psz_time, "%d:%d:%d", &i, &j, &k ) )
1469 switch( sscanf( psz_date, "%d/%d/%d", &i, &j, &k ) )
1488 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1489 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1490 schedule->i_period = ((mtime_t) date) * 1000000;
1492 #endif /* UNDER_CE */
1493 else if( !strcmp( psz_cmd, "repeat" ) )
1497 if( sscanf( psz_value, "%d", &i ) == 1 )
1499 schedule->i_repeat = i;
1506 else if( !strcmp( psz_cmd, "append" ) )
1508 char *command = strdup( psz_value );
1510 TAB_APPEND( schedule->i_command, schedule->command, command );
1519 /*****************************************************************************
1520 * Message handling functions
1521 *****************************************************************************/
1522 static vlm_message_t *vlm_MessageNew( char *psz_name,
1523 const char *psz_format, ... )
1525 vlm_message_t *p_message;
1528 if( !psz_name ) return 0;
1530 p_message = malloc( sizeof(vlm_message_t) );
1536 p_message->psz_value = 0;
1540 va_start( args, psz_format );
1541 if( vasprintf( &p_message->psz_value, psz_format, args ) < 0 )
1550 p_message->psz_name = strdup( psz_name );
1551 p_message->i_child = 0;
1552 p_message->child = NULL;
1557 void vlm_MessageDelete( vlm_message_t *p_message )
1559 if( p_message->psz_name ) free( p_message->psz_name );
1560 if( p_message->psz_value ) free( p_message->psz_value );
1561 while( p_message->i_child-- )
1562 vlm_MessageDelete( p_message->child[p_message->i_child] );
1563 if( p_message->child ) free( p_message->child );
1568 static vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1569 vlm_message_t *p_child )
1571 if( p_message == NULL ) return NULL;
1575 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1581 /*****************************************************************************
1582 * Misc utility functions
1583 *****************************************************************************/
1584 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_t *media,
1585 vlm_schedule_t *schedule, char *psz_filter )
1591 vlm_message_t *msg_media;
1592 vlm_message_t *msg_child;
1594 msg = vlm_MessageNew( "show", NULL );
1595 msg_media = vlm_MessageAdd( msg, vlm_MessageNew( media->psz_name, 0 ));
1597 vlm_MessageAdd( msg_media,
1598 vlm_MessageNew( "type", media->i_type == VOD_TYPE ?
1599 "vod" : "broadcast" ) );
1600 vlm_MessageAdd( msg_media,
1601 vlm_MessageNew( "enabled", media->b_enabled ?
1604 vlm_MessageAdd( msg_media,
1605 vlm_MessageNew( "loop", media->b_loop ?
1608 if( media->i_type == VOD_TYPE && media->psz_mux )
1609 vlm_MessageAdd( msg_media,
1610 vlm_MessageNew( "mux", media->psz_mux ) );
1612 msg_child = vlm_MessageAdd( msg_media,
1613 vlm_MessageNew( "inputs", NULL ) );
1615 for( i = 0; i < media->i_input; i++ )
1617 vlm_MessageAdd( msg_child,
1618 vlm_MessageNew( media->input[i], NULL ) );
1621 vlm_MessageAdd( msg_media,
1622 vlm_MessageNew( "output", media->psz_output ?
1623 media->psz_output : "" ) );
1625 msg_child = vlm_MessageAdd( msg_media, vlm_MessageNew( "options", 0 ));
1627 for( i = 0; i < media->i_option; i++ )
1629 vlm_MessageAdd( msg_child, vlm_MessageNew( media->option[i], 0 ) );
1632 msg_child = vlm_MessageAdd( msg_media,
1633 vlm_MessageNew( "instances", NULL ) );
1635 for( i = 0; i < media->i_instance; i++ )
1637 vlm_media_instance_t *p_instance = media->instance[i];
1640 if( !p_instance->p_input ) val.i_int = END_S;
1641 else var_Get( p_instance->p_input, "state", &val );
1643 vlm_MessageAdd( msg_child,
1644 vlm_MessageNew( p_instance->psz_name ?
1645 p_instance->psz_name : "default",
1646 val.i_int == PLAYING_S ? "playing" :
1647 val.i_int == PAUSE_S ? "paused" :
1655 else if( schedule != NULL )
1659 vlm_message_t *msg_schedule;
1660 vlm_message_t *msg_child;
1663 msg = vlm_MessageNew( "show", NULL );
1665 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, 0 ) );
1667 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1669 vlm_MessageAdd( msg_schedule,
1670 vlm_MessageNew( "enabled", schedule->b_enabled ?
1673 #if !defined( UNDER_CE )
1674 if( schedule->i_date != 0 )
1677 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1680 #ifdef HAVE_LOCALTIME_R
1681 localtime_r( &i_time, &date);
1683 struct tm *p_date = localtime( &i_time );
1687 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1688 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1689 date.tm_hour, date.tm_min, date.tm_sec );
1691 vlm_MessageAdd( msg_schedule,
1692 vlm_MessageNew( "date", psz_date ) );
1697 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1700 if( schedule->i_period != 0 )
1702 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1705 date.tm_sec = (int)( i_time % 60 );
1706 i_time = i_time / 60;
1707 date.tm_min = (int)( i_time % 60 );
1708 i_time = i_time / 60;
1709 date.tm_hour = (int)( i_time % 24 );
1710 i_time = i_time / 24;
1711 date.tm_mday = (int)( i_time % 30 );
1712 i_time = i_time / 30;
1713 /* okay, okay, months are not always 30 days long */
1714 date.tm_mon = (int)( i_time % 12 );
1715 i_time = i_time / 12;
1716 date.tm_year = (int)i_time;
1718 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1719 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1721 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1725 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1727 #endif /* UNDER_CE */
1729 sprintf( buffer, "%d", schedule->i_repeat );
1730 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1733 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", 0) );
1735 for( i = 0; i < schedule->i_command; i++ )
1737 vlm_MessageAdd( msg_child,
1738 vlm_MessageNew( schedule->command[i], NULL ) );
1745 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1749 vlm_message_t *msg_child;
1750 int i_vod = 0, i_broadcast = 0;
1753 for( i = 0; i < vlm->i_media; i++ )
1755 if( vlm->media[i]->i_type == VOD_TYPE )
1761 asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1763 msg = vlm_MessageNew( "show", NULL );
1764 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "media", psz_count ) );
1767 for( i = 0; i < vlm->i_media; i++ )
1769 vlm_media_t *m = vlm->media[i];
1770 vlm_message_t *msg_media, *msg_instance;
1772 msg_media = vlm_MessageAdd( msg_child,
1773 vlm_MessageNew( m->psz_name, 0 ) );
1775 vlm_MessageAdd( msg_media,
1776 vlm_MessageNew( "type", m->i_type == VOD_TYPE ?
1777 "vod" : "broadcast" ) );
1779 vlm_MessageAdd( msg_media,
1780 vlm_MessageNew( "enabled", m->b_enabled ?
1783 if( m->i_type == VOD_TYPE && m->psz_mux )
1784 vlm_MessageAdd( msg_media,
1785 vlm_MessageNew( "mux", m->psz_mux ) );
1787 msg_instance = vlm_MessageAdd( msg_media,
1788 vlm_MessageNew( "instances", 0 ) );
1790 for( j = 0; j < m->i_instance; j++ )
1792 vlm_media_instance_t *p_instance = m->instance[j];
1795 if( !p_instance->p_input ) val.i_int = END_S;
1796 else var_Get( p_instance->p_input, "state", &val );
1798 vlm_MessageAdd( msg_instance,
1799 vlm_MessageNew( p_instance->psz_name ?
1800 p_instance->psz_name : "default",
1801 val.i_int == PLAYING_S ? "playing" :
1802 val.i_int == PAUSE_S ? "paused" :
1810 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1814 vlm_message_t *msg_child;
1816 msg = vlm_MessageNew( "show", NULL );
1817 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", NULL ) );
1819 for( i = 0; i < vlm->i_schedule; i++ )
1821 vlm_schedule_t *s = vlm->schedule[i];
1822 vlm_message_t *msg_schedule;
1823 mtime_t i_time, i_next_date;
1825 msg_schedule = vlm_MessageAdd( msg_child,
1826 vlm_MessageNew( s->psz_name, 0 ) );
1827 vlm_MessageAdd( msg_schedule,
1828 vlm_MessageNew( "enabled", s->b_enabled ?
1831 if( !s->b_enabled ) return msg;
1834 vlm_MessageAdd( msg_schedule,
1835 vlm_MessageNew( "enabled", "yes" ) );
1837 /* calculate next date */
1838 i_time = vlm_Date();
1839 i_next_date = s->i_date;
1841 if( s->i_period != 0 )
1844 while( s->i_date + j * s->i_period <= i_time &&
1850 i_next_date = s->i_date + j * s->i_period;
1853 if( i_next_date > i_time )
1855 time_t i_date = (time_t) (i_next_date / 1000000) ;
1857 #if !defined( UNDER_CE )
1860 ctime_r( &i_date, psz_date );
1862 char *psz_date = ctime( &i_date );
1865 vlm_MessageAdd( msg_schedule,
1866 vlm_MessageNew( "next launch", psz_date ) );
1874 else if( psz_filter == NULL && media == NULL && schedule == NULL )
1876 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1877 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1879 vlm_MessageAdd( show1, show2->child[0] );
1881 /* We must destroy the parent node "show" of show2
1882 * and not the children */
1883 free( show2->psz_name );
1891 return vlm_MessageNew( "show", NULL );
1895 static vlm_message_t *vlm_Help( vlm_t *vlm, char *psz_filter )
1897 vlm_message_t *message, *message_child;
1899 #define MessageAdd( a ) \
1900 vlm_MessageAdd( message, vlm_MessageNew( a, NULL ) );
1901 #define MessageAddChild( a ) \
1902 vlm_MessageAdd( message_child, vlm_MessageNew( a, NULL ) );
1904 if( psz_filter == NULL )
1906 message = vlm_MessageNew( "help", NULL );
1908 message_child = MessageAdd( "Commands Syntax:" );
1909 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
1910 MessageAddChild( "setup (name) (properties)" );
1911 MessageAddChild( "show [(name)|media|schedule]" );
1912 MessageAddChild( "del (name)|all|media|schedule" );
1913 MessageAddChild( "control (name) [instance_name] (command)" );
1914 MessageAddChild( "save (config_file)" );
1915 MessageAddChild( "load (config_file)" );
1917 message_child = MessageAdd( "Media Proprieties Syntax:" );
1918 MessageAddChild( "input (input_name)" );
1919 MessageAddChild( "inputdel (input_name)|all" );
1920 MessageAddChild( "inputdeln input_number" );
1921 MessageAddChild( "output (output_name)" );
1922 MessageAddChild( "option (option_name)[=value]" );
1923 MessageAddChild( "enabled|disabled" );
1924 MessageAddChild( "loop|unloop (broadcast only)" );
1925 MessageAddChild( "mux (mux_name)" );
1927 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
1928 MessageAddChild( "enabled|disabled" );
1929 MessageAddChild( "append (command_until_rest_of_the_line)" );
1930 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
1932 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
1933 "(days)-(hours):(minutes):(seconds)" );
1934 MessageAddChild( "repeat (number_of_repetitions)" );
1936 message_child = MessageAdd( "Control Commands Syntax:" );
1937 MessageAddChild( "play" );
1938 MessageAddChild( "pause" );
1939 MessageAddChild( "stop" );
1940 MessageAddChild( "seek (percentage)" );
1945 return vlm_MessageNew( "help", NULL );
1948 /*****************************************************************************
1949 * Config handling functions
1950 *****************************************************************************/
1951 static int Load( vlm_t *vlm, char *file )
1955 while( *pf != '\0' )
1957 vlm_message_t *message = NULL;
1960 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1965 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1969 if( pf[i_end] == '\n' ) i_end++;
1972 if( *pf && ExecuteCommand( vlm, pf, &message ) )
1974 if( message ) free( message );
1977 if( message ) free( message );
1985 static char *Save( vlm_t *vlm )
1992 for( i = 0; i < vlm->i_media; i++ )
1994 vlm_media_t *media = vlm->media[i];
1996 if( media->i_type == VOD_TYPE )
1998 i_length += strlen( "new vod " ) + strlen(media->psz_name);
2002 i_length += strlen( "new broadcast " ) + strlen(media->psz_name);
2005 if( media->b_enabled == VLC_TRUE )
2007 i_length += strlen( "enabled" );
2011 i_length += strlen( "disabled" );
2014 if( media->b_loop == VLC_TRUE )
2016 i_length += strlen( " loop\n" );
2020 i_length += strlen( "\n" );
2023 for( j = 0; j < media->i_input; j++ )
2025 i_length += strlen( "setup input \"\"\n" ) +
2026 strlen( media->psz_name ) + strlen( media->input[j] );
2029 if( media->psz_output != NULL )
2031 i_length += strlen(media->psz_name) + strlen(media->psz_output) +
2032 strlen( "setup output \n" );
2035 for( j=0 ; j < media->i_option ; j++ )
2037 i_length += strlen(media->psz_name) + strlen(media->option[j]) +
2038 strlen("setup option \n");
2042 for( i = 0; i < vlm->i_schedule; i++ )
2044 vlm_schedule_t *schedule = vlm->schedule[i];
2046 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
2048 if( schedule->b_enabled == VLC_TRUE )
2050 i_length += strlen( "date //-:: enabled\n" ) + 14;
2054 i_length += strlen( "date //-:: disabled\n" ) + 14;
2058 if( schedule->i_period != 0 )
2060 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
2061 strlen( "period //-::\n" ) + 14;
2064 if( schedule->i_repeat >= 0 )
2068 sprintf( buffer, "%d", schedule->i_repeat );
2069 i_length += strlen( "setup repeat \n" ) +
2070 strlen( schedule->psz_name ) + strlen( buffer );
2077 for( j = 0; j < schedule->i_command; j++ )
2079 i_length += strlen( "setup append \n" ) +
2080 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
2085 /* Don't forget the '\0' */
2087 /* now we have the length of save */
2089 p = save = malloc( i_length );
2092 /* finally we can write in it */
2093 for( i = 0; i < vlm->i_media; i++ )
2095 vlm_media_t *media = vlm->media[i];
2097 if( media->i_type == VOD_TYPE )
2099 p += sprintf( p, "new %s vod ", media->psz_name);
2103 p += sprintf( p, "new %s broadcast ", media->psz_name);
2106 if( media->b_enabled == VLC_TRUE )
2108 p += sprintf( p, "enabled" );
2112 p += sprintf( p, "disabled" );
2115 if( media->b_loop == VLC_TRUE )
2117 p += sprintf( p, " loop\n" );
2121 p += sprintf( p, "\n" );
2124 for( j = 0; j < media->i_input; j++ )
2126 p += sprintf( p, "setup %s input \"%s\"\n", media->psz_name,
2130 if( media->psz_output != NULL )
2132 p += sprintf( p, "setup %s output %s\n", media->psz_name,
2133 media->psz_output );
2136 for( j = 0; j < media->i_option; j++ )
2138 p += sprintf( p, "setup %s option %s\n", media->psz_name,
2143 /* and now, the schedule scripts */
2144 #if !defined( UNDER_CE )
2145 for( i = 0; i < vlm->i_schedule; i++ )
2147 vlm_schedule_t *schedule = vlm->schedule[i];
2149 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
2151 #ifdef HAVE_LOCALTIME_R
2152 localtime_r( &i_time, &date);
2154 struct tm *p_date = localtime( &i_time );
2158 p += sprintf( p, "new %s schedule ", schedule->psz_name);
2160 if( schedule->b_enabled == VLC_TRUE )
2162 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
2163 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2164 date.tm_hour, date.tm_min, date.tm_sec );
2168 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
2169 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2170 date.tm_hour, date.tm_min, date.tm_sec);
2173 if( schedule->i_period != 0 )
2175 p += sprintf( p, "setup %s ", schedule->psz_name );
2177 i_time = (time_t) ( schedule->i_period / 1000000 );
2179 date.tm_sec = (int)( i_time % 60 );
2180 i_time = i_time / 60;
2181 date.tm_min = (int)( i_time % 60 );
2182 i_time = i_time / 60;
2183 date.tm_hour = (int)( i_time % 24 );
2184 i_time = i_time / 24;
2185 date.tm_mday = (int)( i_time % 30 );
2186 i_time = i_time / 30;
2187 /* okay, okay, months are not always 30 days long */
2188 date.tm_mon = (int)( i_time % 12 );
2189 i_time = i_time / 12;
2190 date.tm_year = (int)i_time;
2192 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
2193 date.tm_year, date.tm_mon, date.tm_mday,
2194 date.tm_hour, date.tm_min, date.tm_sec);
2197 if( schedule->i_repeat >= 0 )
2199 p += sprintf( p, "setup %s repeat %d\n",
2200 schedule->psz_name, schedule->i_repeat );
2204 p += sprintf( p, "\n" );
2207 for( j = 0; j < schedule->i_command; j++ )
2209 p += sprintf( p, "setup %s append %s\n",
2210 schedule->psz_name, schedule->command[j] );
2214 #endif /* UNDER_CE */
2219 /*****************************************************************************
2221 *****************************************************************************/
2222 int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
2223 char *psz_id, int i_query, va_list args )
2225 vlm_t *vlm = (vlm_t *)p_private;
2226 int i, i_ret = VLC_EGENERIC;
2228 if( !p_private || !p_vod_media ) return VLC_EGENERIC;
2230 vlc_mutex_lock( &vlm->lock );
2233 for( i = 0; i < vlm->i_media; i++ )
2235 if( p_vod_media == vlm->media[i]->vod_media ) break;
2238 if( i == vlm->i_media )
2240 vlc_mutex_unlock( &vlm->lock );
2241 return VLC_EGENERIC;
2246 case VOD_MEDIA_PLAY:
2247 vlm->media[i]->psz_vod_output = (char *)va_arg( args, char * );
2248 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "play", 0 );
2249 vlm->media[i]->psz_vod_output = 0;
2252 case VOD_MEDIA_PAUSE:
2253 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "pause", 0 );
2256 case VOD_MEDIA_STOP:
2257 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "stop", 0 );
2260 case VOD_MEDIA_SEEK:
2262 double f_pos = (double)va_arg( args, double );
2265 sprintf( psz_pos, "%f", f_pos );
2266 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "seek", psz_pos);
2274 vlc_mutex_unlock( &vlm->lock );
2279 /*****************************************************************************
2281 *****************************************************************************/
2282 static int Manage( vlc_object_t* p_object )
2284 vlm_t *vlm = (vlm_t*)p_object;
2286 mtime_t i_lastcheck;
2289 i_lastcheck = vlm_Date();
2293 while( !vlm->b_die )
2295 vlc_mutex_lock( &vlm->lock );
2297 /* destroy the inputs that wants to die, and launch the next input */
2298 for( i = 0; i < vlm->i_media; i++ )
2300 vlm_media_t *p_media = vlm->media[i];
2302 for( j = 0; j < p_media->i_instance; j++ )
2304 vlm_media_instance_t *p_instance = p_media->instance[j];
2306 if( !p_instance->p_input ||
2307 ( !p_instance->p_input->b_eof &&
2308 !p_instance->p_input->b_error ) ) continue;
2310 input_StopThread( p_instance->p_input );
2311 input_DestroyThread( p_instance->p_input );
2312 vlc_object_detach( p_instance->p_input );
2313 vlc_object_destroy( p_instance->p_input );
2315 p_instance->i_index++;
2316 if( p_instance->i_index == p_media->i_input &&
2317 p_media->b_loop ) p_instance->i_index = 0;
2319 if( p_instance->i_index < p_media->i_input )
2321 /* FIXME, find a way to select the right instance */
2323 sprintf( buffer, "%d", p_instance->i_index );
2324 vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2329 if( vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2330 "stop", 0 ) == VLC_SUCCESS ) i--;
2336 i_time = vlm_Date();
2338 for( i = 0; i < vlm->i_schedule; i++ )
2340 mtime_t i_real_date = vlm->schedule[i]->i_date;
2342 if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2344 if( vlm->schedule[i]->i_date == 0 ) // now !
2346 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2347 i_real_date = i_time;
2349 else if( vlm->schedule[i]->i_period != 0 )
2352 while( vlm->schedule[i]->i_date + j *
2353 vlm->schedule[i]->i_period <= i_lastcheck &&
2354 ( vlm->schedule[i]->i_repeat > j ||
2355 vlm->schedule[i]->i_repeat == -1 ) )
2360 i_real_date = vlm->schedule[i]->i_date + j *
2361 vlm->schedule[i]->i_period;
2364 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2366 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2368 vlm_message_t *message = NULL;
2370 ExecuteCommand( vlm, vlm->schedule[i]->command[j],
2373 /* for now, drop the message */
2380 i_lastcheck = i_time;
2382 vlc_mutex_unlock( &vlm->lock );
2390 #else /* ENABLE_VLM */
2392 /* We just define an empty wrapper */
2393 vlm_t *__vlm_New( vlc_object_t *a )
2395 msg_Err( a, "VideoLAN manager support is disabled" );
2398 void vlm_Delete( vlm_t *a ){}
2399 int vlm_ExecuteCommand( vlm_t *a, char *b, vlm_message_t **c ){ return -1; }
2400 void vlm_MessageDelete( vlm_message_t *a ){}
2401 vlm_media_t *vlm_MediaNew( vlm_t *a, char *b, int c ){ return NULL; }
2402 void vlm_MediaDelete( vlm_t *a, vlm_media_t *b, char *c ){}
2403 int vlm_MediaSetup( vlm_t *a, vlm_media_t *b, char *c, char *d ){ return -1; }
2404 int vlm_MediaControl( vlm_t *a, vlm_media_t *b, char *c, char *d, char *e )
2406 vlm_schedule_t * vlm_ScheduleNew( vlm_t *a, char *b ){ return NULL; }
2407 void vlm_ScheduleDelete( vlm_t *a, vlm_schedule_t *b, char *c ){}
2408 int vlm_ScheduleSetup( vlm_schedule_t *a, char *b, char *c ){ return -1; }
2409 int vlm_MediaVodControl( void *a, vod_media_t *b, char *c, int d, va_list e )
2411 int vlm_Save( vlm_t *a, char *b ){ return -1; }
2412 int vlm_Load( vlm_t *a, char *b ){ return -1; }
2414 #endif /* ENABLE_VLM */