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() */
47 if( p ) { free( p ); (p) = NULL; }
49 /*****************************************************************************
51 *****************************************************************************/
52 static char *vlm_Save( vlm_t * );
53 static int vlm_Load( vlm_t *, char *);
54 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_t *, vlm_schedule_t *, char * );
55 static vlm_message_t *vlm_Help( vlm_t *, char * );
57 static vlm_media_t *vlm_MediaNew ( vlm_t *, char *, int );
58 static void vlm_MediaDelete ( vlm_t *, vlm_media_t *, char * );
59 static vlm_media_t *vlm_MediaSearch ( vlm_t *, char * );
60 static int vlm_MediaSetup ( vlm_t *, vlm_media_t *, char *, char * );
61 static int vlm_MediaControl( vlm_t *, vlm_media_t *, char *, char *, char * );
62 static vlm_media_instance_t *vlm_MediaInstanceSearch( vlm_t *, vlm_media_t *, char * );
64 static vlm_message_t *vlm_MessageNew( char *, const char *, ... );
65 static vlm_message_t *vlm_MessageAdd( vlm_message_t*, vlm_message_t* );
67 static vlm_schedule_t *vlm_ScheduleNew( vlm_t *, char *);
68 static void vlm_ScheduleDelete( vlm_t *, vlm_schedule_t *, char *);
69 static int vlm_ScheduleSetup( vlm_schedule_t *, char *, char *);
70 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *, char *);
72 static int vlm_MediaVodControl( void *, vod_media_t *, char *, int, va_list );
74 static int ExecuteCommand( vlm_t *, char *, vlm_message_t **);
75 static int Manage( vlc_object_t* );
77 /*****************************************************************************
79 *****************************************************************************/
80 vlm_t *__vlm_New ( vlc_object_t *p_this )
86 /* to be sure to avoid multiple creation */
87 var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX );
88 var_Get( p_this->p_libvlc, "vlm_mutex", &lockval );
89 vlc_mutex_lock( lockval.p_address );
91 if( !(p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE )) )
93 msg_Info( p_this, "creating vlm" );
94 if( ( p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM ) ) == NULL )
96 vlc_mutex_unlock( lockval.p_address );
100 vlc_mutex_init( p_this->p_vlc, &p_vlm->lock );
104 p_vlm->i_schedule = 0;
105 p_vlm->schedule = NULL;
107 vlc_object_yield( p_vlm );
108 vlc_object_attach( p_vlm, p_this->p_vlc );
110 vlc_mutex_unlock( lockval.p_address );
112 if( vlc_thread_create( p_vlm, "vlm thread",
113 Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
115 vlc_mutex_destroy( &p_vlm->lock );
116 vlc_object_destroy( p_vlm );
120 /* Try loading the vlm conf file given by --vlm-conf */
121 psz_vlmconf = config_GetPsz( p_vlm, "vlm-conf" );
123 if( psz_vlmconf && *psz_vlmconf )
125 vlm_message_t *p_message = NULL;
126 char *psz_buffer = NULL;
128 msg_Dbg( p_this, "loading vlm conf ..." );
129 asprintf(&psz_buffer, "load %s", psz_vlmconf );
132 msg_Dbg( p_this, psz_buffer);
133 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) ){
134 msg_Warn( p_this, "error while loading the vlm conf file" );
145 /*****************************************************************************
147 *****************************************************************************/
148 void vlm_Delete( vlm_t *p_vlm )
152 var_Get( p_vlm->p_libvlc, "vlm_mutex", &lockval );
153 vlc_mutex_lock( lockval.p_address );
155 vlc_object_release( p_vlm );
157 if( p_vlm->i_refcount > 0 )
159 vlc_mutex_unlock( lockval.p_address );
163 p_vlm->b_die = VLC_TRUE;
164 vlc_thread_join( p_vlm );
166 vlc_mutex_destroy( &p_vlm->lock );
168 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0], NULL );
169 FREE( p_vlm->media );
171 while( p_vlm->i_schedule ) vlm_ScheduleDelete( p_vlm,
172 p_vlm->schedule[0], NULL );
173 FREE( p_vlm->schedule );
175 vlc_object_detach( p_vlm );
176 vlc_object_destroy( p_vlm );
177 vlc_mutex_unlock( lockval.p_address );
180 /*****************************************************************************
181 * vlm_ExecuteCommand:
182 *****************************************************************************/
183 int vlm_ExecuteCommand( vlm_t *p_vlm, char *psz_command,
184 vlm_message_t **pp_message)
188 vlc_mutex_lock( &p_vlm->lock );
189 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
190 vlc_mutex_unlock( &p_vlm->lock );
195 /*****************************************************************************
197 *****************************************************************************/
198 static char *FindEndCommand( char *psz )
200 char *psz_sent = psz;
207 while( ( *psz_sent != '\"' ) && ( *psz_sent != '\0' ) )
209 if( *psz_sent == '\'' )
211 psz_sent = FindEndCommand( psz_sent );
212 if( psz_sent == NULL ) return NULL;
217 if( *psz_sent == '\"' )
223 /* *psz_sent == '\0' -> number of " is incorrect */
231 while( ( *psz_sent != '\'' ) && ( *psz_sent != '\0' ) )
233 if( *psz_sent == '\"' )
235 psz_sent = FindEndCommand( psz_sent );
236 if( psz_sent == NULL ) return NULL;
241 if( *psz_sent == '\'' )
247 /* *psz_sent == '\0' -> number of " is incorrect */
252 default: /* now we can look for spaces */
253 while( ( *psz_sent != ' ' ) && ( *psz_sent != '\0' ) )
255 if( ( *psz_sent == '\'' ) || ( *psz_sent == '\"' ) )
257 psz_sent = FindEndCommand( psz_sent );
266 /*****************************************************************************
267 * ExecuteCommand: The main state machine
268 *****************************************************************************
269 * Execute a command which ends with '\0' (string)
270 *****************************************************************************/
271 static int ExecuteCommand( vlm_t *p_vlm, char *psz_command,
272 vlm_message_t **pp_message )
275 char **ppsz_command = NULL;
276 char *psz_cmd = psz_command;
277 vlm_message_t *p_message = NULL;
280 /* First, parse the line and cut it */
281 while( *psz_cmd != '\0' )
284 if( *psz_cmd == ' ' || *psz_cmd == '\t' )
293 /* support for comments */
294 if( i_command == 0 && *psz_cmd == '#')
296 p_message = vlm_MessageNew( "", NULL );
300 psz_temp = FindEndCommand( psz_cmd );
302 if( psz_temp == NULL ) goto error;
304 i_temp = psz_temp - psz_cmd;
306 ppsz_command = realloc( ppsz_command, (i_command + 1) *
308 ppsz_command[ i_command ] = malloc( (i_temp + 1) * sizeof(char) );
309 strncpy( ppsz_command[ i_command ], psz_cmd, i_temp );
310 ppsz_command[ i_command ][ i_temp ] = '\0';
319 * And then Interpret it
324 p_message = vlm_MessageNew( "", NULL );
328 if( !strcmp(ppsz_command[0], "new") )
332 /* Check the number of arguments */
333 if( i_command < 3 ) goto syntax_error;
336 if( !strcmp(ppsz_command[2], "vod") )
340 else if( !strcmp(ppsz_command[2], "broadcast") )
342 i_type = BROADCAST_TYPE;
344 else if( !strcmp(ppsz_command[2], "schedule") )
346 i_type = SCHEDULE_TYPE;
351 vlm_MessageNew( "new", "%s: Choose between vod, "
352 "broadcast or schedule", ppsz_command[1] );
356 /* Check for forbidden media names */
357 if( !strcmp(ppsz_command[1], "all") ||
358 !strcmp(ppsz_command[1], "media") ||
359 !strcmp(ppsz_command[1], "schedule") )
361 p_message = vlm_MessageNew( "new", "\"all\", \"media\" and "
362 "\"schedule\" are reserved names" );
366 /* Check the name is not already in use */
367 if( vlm_ScheduleSearch( p_vlm, ppsz_command[1] ) ||
368 vlm_MediaSearch( p_vlm, ppsz_command[1] ) )
370 p_message = vlm_MessageNew( "new", "%s: Name already in use",
376 if( i_type == SCHEDULE_TYPE )
378 vlm_schedule_t *p_schedule;
379 p_schedule = vlm_ScheduleNew( p_vlm, ppsz_command[1] );
382 p_message = vlm_MessageNew( "new", "could not create schedule" );
390 vlm_media_t *p_media;
391 p_media = vlm_MediaNew( p_vlm, ppsz_command[1], i_type );
394 p_message = vlm_MessageNew( "new", "could not create media" );
401 p_message = vlm_MessageNew( "new", NULL );
405 /* Properties will be dealt with later on */
408 else if( !strcmp(ppsz_command[0], "setup") )
410 if( i_command < 2 ) goto syntax_error;
412 /* Properties will be dealt with later on */
415 else if( !strcmp(ppsz_command[0], "del") )
417 vlm_media_t *p_media;
418 vlm_schedule_t *p_schedule;
420 if( i_command < 2 ) goto syntax_error;
422 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
423 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
425 if( p_schedule != NULL )
427 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
429 else if( p_media != NULL )
431 vlm_MediaDelete( p_vlm, p_media, NULL );
433 else if( !strcmp(ppsz_command[1], "media") )
435 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
438 else if( !strcmp(ppsz_command[1], "schedule") )
440 while( p_vlm->i_schedule )
441 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
443 else if( !strcmp(ppsz_command[1], "all") )
445 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
448 while( p_vlm->i_schedule )
449 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
453 p_message = vlm_MessageNew( "del", "%s: media unknown",
458 p_message = vlm_MessageNew( "del", NULL );
462 else if( !strcmp(ppsz_command[0], "show") )
464 vlm_media_t *p_media;
465 vlm_schedule_t *p_schedule;
469 p_message = vlm_Show( p_vlm, NULL, NULL, NULL );
472 else if( i_command > 2 ) goto syntax_error;
474 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
475 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
477 if( p_schedule != NULL )
479 p_message = vlm_Show( p_vlm, NULL, p_schedule, NULL );
481 else if( p_media != NULL )
483 p_message = vlm_Show( p_vlm, p_media, NULL, NULL );
487 p_message = vlm_Show( p_vlm, NULL, NULL, ppsz_command[1] );
493 else if( !strcmp(ppsz_command[0], "help") )
495 if( i_command != 1 ) goto syntax_error;
497 p_message = vlm_Help( p_vlm, NULL );
501 else if( !strcmp(ppsz_command[0], "control") )
503 vlm_media_t *p_media;
505 if( i_command < 3 ) goto syntax_error;
507 if( !(p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] ) ) )
509 p_message = vlm_MessageNew( "control", "%s: media unknown",
515 char *psz_command, *psz_arg = 0, *psz_instance = 0;
518 if( strcmp( ppsz_command[2], "play" ) &&
519 strcmp( ppsz_command[2], "stop" ) &&
520 strcmp( ppsz_command[2], "pause" ) &&
521 strcmp( ppsz_command[2], "seek" ) )
524 psz_instance = ppsz_command[2];
526 if( i_command < 4 ) goto syntax_error;
529 psz_command = ppsz_command[i_index];
531 if( i_command >= i_index + 2 ) psz_arg = ppsz_command[i_index + 1];
533 vlm_MediaControl( p_vlm, p_media, psz_instance, psz_command,
535 p_message = vlm_MessageNew( "control", NULL );
540 else if( !strcmp(ppsz_command[0], "save") )
544 if( i_command != 2 ) goto syntax_error;
546 file = fopen( ppsz_command[1], "w" );
549 p_message = vlm_MessageNew( "save", "Unable to save to file" );
554 char *psz_save = vlm_Save( p_vlm );
555 if( psz_save == NULL )
560 fwrite( psz_save, strlen( psz_save ), 1, file );
563 p_message = vlm_MessageNew( "save", NULL );
568 else if( !strcmp(ppsz_command[0], "load") )
572 if( i_command != 2 ) goto syntax_error;
574 file = fopen( ppsz_command[1], "r" );
578 p_message = vlm_MessageNew( "load", "Unable to load from file" );
586 if( fseek( file, 0, SEEK_END) == 0 )
588 i_size = ftell( file );
589 fseek( file, 0, SEEK_SET);
590 psz_buffer = malloc( i_size + 1 );
596 fread( psz_buffer, 1, i_size, file);
597 psz_buffer[ i_size ] = '\0';
598 if( vlm_Load( p_vlm, psz_buffer ) )
601 p_message = vlm_MessageNew( "load", "error while loading "
609 p_message = vlm_MessageNew( "load", "read file error" );
614 p_message = vlm_MessageNew( "load", NULL );
621 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
625 /* Common code between "new" and "setup" */
626 if( !strcmp(ppsz_command[0], "new") ||
627 !strcmp(ppsz_command[0], "setup") )
629 int i_command_start = strcmp(ppsz_command[0], "new") ? 2 : 3;
630 vlm_media_t *p_media;
631 vlm_schedule_t *p_schedule;
633 if( i_command < i_command_start ) goto syntax_error;
635 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
636 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
638 if( !p_media && !p_schedule )
640 p_message = vlm_MessageNew( ppsz_command[0], "%s unknown",
645 if( p_schedule != NULL )
647 for( i = i_command_start ; i < i_command ; i++ )
649 if( !strcmp( ppsz_command[i], "enabled" ) ||
650 !strcmp( ppsz_command[i], "disabled" ) )
652 vlm_ScheduleSetup( p_schedule, ppsz_command[i], NULL );
655 /* Beware: everything behind append is considered as
657 else if( !strcmp( ppsz_command[i], "append" ) )
659 if( ++i >= i_command ) break;
661 for( j = i + 1; j < i_command; j++ )
664 realloc( ppsz_command[i], strlen(ppsz_command[i]) +
665 strlen(ppsz_command[j]) + 1 + 1 );
666 strcat( ppsz_command[i], " " );
667 strcat( ppsz_command[i], ppsz_command[j] );
670 vlm_ScheduleSetup( p_schedule, ppsz_command[i - 1],
676 if( i + 1 >= i_command && !strcmp(ppsz_command[0], "new") )
678 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
680 vlm_MessageNew( ppsz_command[0],
681 "Wrong properties syntax" );
684 else if( i + 1 >= i_command )
687 vlm_MessageNew( ppsz_command[0],
688 "Wrong properties syntax" );
692 vlm_ScheduleSetup( p_schedule, ppsz_command[i],
699 else if( p_media != NULL )
701 for( i = i_command_start ; i < i_command ; i++ )
703 if( !strcmp( ppsz_command[i], "enabled" ) ||
704 !strcmp( ppsz_command[i], "disabled" ) )
706 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
708 else if( i + 1 >= i_command &&
709 !strcmp( ppsz_command[i], "mux") )
711 if( p_media->i_type != VOD_TYPE )
713 p_message = vlm_MessageNew( ppsz_command[0],
714 "mux only available for broadcast" );
718 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
723 else if( !strcmp( ppsz_command[i], "loop" ) ||
724 !strcmp( ppsz_command[i], "unloop" ) )
726 if( p_media->i_type != BROADCAST_TYPE )
728 p_message = vlm_MessageNew( ppsz_command[0],
729 "loop only available for broadcast" );
733 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
738 if( i + 1 >= i_command &&
739 !strcmp(ppsz_command[0], "new") )
741 vlm_MediaDelete( p_vlm, p_media, NULL );
743 vlm_MessageNew( ppsz_command[0],
744 "Wrong properties syntax" );
747 else if( i + 1 >= i_command )
750 vlm_MessageNew( ppsz_command[0],
751 "Wrong properties syntax" );
755 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
762 p_message = vlm_MessageNew( ppsz_command[0], NULL );
767 for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
768 FREE( ppsz_command );
769 *pp_message = p_message;
774 p_message = vlm_MessageNew( ppsz_command[0], "Wrong command syntax" );
777 for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
778 FREE( ppsz_command );
779 *pp_message = p_message;
784 static vlm_media_t *vlm_MediaSearch( vlm_t *vlm, char *psz_name )
788 for( i = 0; i < vlm->i_media; i++ )
790 if( strcmp( psz_name, vlm->media[i]->psz_name ) == 0 )
792 return vlm->media[i];
799 /*****************************************************************************
801 *****************************************************************************/
802 static vlm_media_instance_t *
803 vlm_MediaInstanceSearch( vlm_t *vlm, vlm_media_t *media, char *psz_name )
807 for( i = 0; i < media->i_instance; i++ )
809 if( ( !psz_name && !media->instance[i]->psz_name ) ||
810 ( psz_name && media->instance[i]->psz_name &&
811 !strcmp( psz_name, media->instance[i]->psz_name ) ) )
813 return media->instance[i];
820 static vlm_media_t *vlm_MediaNew( vlm_t *vlm, char *psz_name, int i_type )
822 vlm_media_t *media = malloc( sizeof( vlm_media_t ) );
826 msg_Err( vlm, "out of memory" );
830 /* Check if we need to load the VOD server */
831 if( i_type == VOD_TYPE && !vlm->i_vod )
833 vlm->vod = vlc_object_create( vlm, VLC_OBJECT_VOD );
834 vlc_object_attach( vlm->vod, vlm );
835 vlm->vod->p_module = module_Need( vlm->vod, "vod server", 0, 0 );
836 if( !vlm->vod->p_module )
838 msg_Err( vlm, "cannot find vod server" );
839 vlc_object_detach( vlm->vod );
840 vlc_object_destroy( vlm->vod );
846 vlm->vod->p_data = vlm;
847 vlm->vod->pf_media_control = vlm_MediaVodControl;
850 if( i_type == VOD_TYPE ) vlm->i_vod++;
852 media->psz_name = strdup( psz_name );
853 media->b_enabled = VLC_FALSE;
854 media->b_loop = VLC_FALSE;
855 media->vod_media = NULL;
856 media->psz_vod_output = NULL;
857 media->psz_mux = NULL;
860 media->psz_output = NULL;
862 media->option = NULL;
863 media->i_type = i_type;
864 media->i_instance = 0;
865 media->instance = NULL;
867 media->item.psz_uri = strdup( psz_name );
868 vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
870 TAB_APPEND( vlm->i_media, vlm->media, media );
875 /* for now, simple delete. After, del with options (last arg) */
876 static void vlm_MediaDelete( vlm_t *vlm, vlm_media_t *media, char *psz_name )
878 if( media == NULL ) return;
880 while( media->i_instance )
882 vlm_media_instance_t *p_instance = media->instance[0];
883 vlm_MediaControl( vlm, media, p_instance->psz_name, "stop", 0 );
886 TAB_REMOVE( vlm->i_media, vlm->media, media );
888 if( media->i_type == VOD_TYPE )
890 vlm_MediaSetup( vlm, media, "disabled", 0 );
894 /* Check if we need to unload the VOD server */
895 if( media->i_type == VOD_TYPE && !vlm->i_vod )
897 module_Unneed( vlm->vod, vlm->vod->p_module );
898 vlc_object_detach( vlm->vod );
899 vlc_object_destroy( vlm->vod );
903 if( vlm->i_media == 0 && vlm->media ) free( vlm->media );
905 free( media->psz_name );
907 while( media->i_input-- ) free( media->input[media->i_input] );
908 if( media->input ) free( media->input );
910 if( media->psz_output ) free( media->psz_output );
911 if( media->psz_mux ) free( media->psz_mux );
913 while( media->i_option-- ) free( media->option[media->i_option] );
914 if( media->option ) free( media->option );
916 vlc_input_item_Clean( &media->item );
921 static int vlm_MediaSetup( vlm_t *vlm, vlm_media_t *media, char *psz_cmd,
924 if( !psz_cmd) return VLC_EGENERIC;
926 if( !strcmp( psz_cmd, "loop" ) )
928 media->b_loop = VLC_TRUE;
930 else if( !strcmp( psz_cmd, "unloop" ) )
932 media->b_loop = VLC_FALSE;
934 else if( !strcmp( psz_cmd, "enabled" ) )
936 media->b_enabled = VLC_TRUE;
938 else if( !strcmp( psz_cmd, "disabled" ) )
940 media->b_enabled = VLC_FALSE;
942 else if( !strcmp( psz_cmd, "mux" ) )
944 if( media->psz_mux ) free( media->psz_mux );
945 media->psz_mux = NULL;
946 if( psz_value ) media->psz_mux = strdup( psz_value );
948 else if( !strcmp( psz_cmd, "input" ) )
952 if( psz_value != NULL && strlen(psz_value) > 1 &&
953 ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
954 ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
955 psz_value[ strlen(psz_value) - 1 ] == '\"' ) )
957 input = malloc( strlen(psz_value) - 1 );
959 memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
960 input[ strlen(psz_value) - 2 ] = '\0';
964 input = strdup( psz_value );
967 TAB_APPEND( media->i_input, media->input, input );
969 else if( !strcmp( psz_cmd, "output" ) )
971 if( media->psz_output != NULL )
973 free( media->psz_output );
975 media->psz_output = strdup( psz_value );
977 else if( !strcmp( psz_cmd, "option" ) )
980 psz_option = strdup( psz_value );
982 TAB_APPEND( media->i_option, media->option, psz_option );
989 /* Check if we need to create/delete a vod media */
990 if( media->i_type == VOD_TYPE )
992 if( !media->b_enabled && media->vod_media )
994 vlm->vod->pf_media_del( vlm->vod, media->vod_media );
995 media->vod_media = 0;
997 else if( media->b_enabled && !media->vod_media && media->i_input )
999 /* Pre-parse the input */
1000 input_thread_t *p_input;
1004 vlc_input_item_Clean( &media->item );
1005 vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
1007 if( media->psz_output )
1008 asprintf( &psz_output, "%s:description", media->psz_output );
1010 asprintf( &psz_output, "#description" );
1012 media->item.psz_uri = strdup( media->input[0] );
1013 media->item.ppsz_options = malloc( sizeof( char* ) );
1014 asprintf( &media->item.ppsz_options[0], "sout=%s", psz_output);
1015 media->item.i_options = 1;
1016 for( i = 0; i < media->i_option; i++ )
1018 media->item.i_options++;
1019 media->item.ppsz_options =
1020 realloc( media->item.ppsz_options,
1021 media->item.i_options * sizeof( char* ) );
1022 media->item.ppsz_options[ media->item.i_options - 1 ] =
1023 strdup( media->option[i] );
1026 if( (p_input = input_CreateThread( vlm, &media->item ) ) )
1028 while( !p_input->b_eof && !p_input->b_error ) msleep( 100000 );
1030 input_StopThread( p_input );
1031 input_DestroyThread( p_input );
1032 vlc_object_detach( p_input );
1033 vlc_object_destroy( p_input );
1037 if( media->psz_mux )
1040 es_format_t es, *p_es = &es;
1043 sprintf( fourcc, "%4.4s", media->psz_mux );
1044 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
1045 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
1050 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
1053 vlm->vod->pf_media_new( vlm->vod, media->psz_name, &item );
1058 vlm->vod->pf_media_new( vlm->vod, media->psz_name,
1066 static int vlm_MediaControl( vlm_t *vlm, vlm_media_t *media, char *psz_id,
1067 char *psz_command, char *psz_args )
1069 vlm_media_instance_t *p_instance;
1072 p_instance = vlm_MediaInstanceSearch( vlm, media, psz_id );
1074 if( !strcmp( psz_command, "play" ) && !p_instance )
1076 if( !media->b_enabled || media->i_input == 0 ) return 0;
1080 p_instance = malloc( sizeof(vlm_media_instance_t) );
1081 if( !p_instance ) return VLC_EGENERIC;
1082 memset( p_instance, 0, sizeof(vlm_media_instance_t) );
1083 vlc_input_item_Init( VLC_OBJECT(vlm), &p_instance->item );
1084 p_instance->p_input = NULL;
1086 if( media->psz_output != NULL || media->psz_vod_output != NULL )
1088 p_instance->item.ppsz_options = malloc( sizeof( char* ) );
1089 asprintf( &p_instance->item.ppsz_options[0], "sout=%s%s%s",
1090 media->psz_output ? media->psz_output : "",
1091 (media->psz_output && media->psz_vod_output) ?
1092 ":" : media->psz_vod_output ? "#" : "",
1093 media->psz_vod_output ? media->psz_vod_output : "" );
1094 p_instance->item.i_options = 1;
1097 for( i = 0; i < media->i_option; i++ )
1099 p_instance->item.i_options++;
1100 p_instance->item.ppsz_options =
1101 realloc( p_instance->item.ppsz_options,
1102 p_instance->item.i_options * sizeof( char* ) );
1103 p_instance->item.ppsz_options[p_instance->item.i_options - 1] =
1104 strdup( media->option[i] );
1107 p_instance->psz_name = psz_id ? strdup( psz_id ) : NULL;
1108 TAB_APPEND( media->i_instance, media->instance, p_instance );
1111 if( psz_args && sscanf(psz_args, "%d", &i) == 1 && i < media->i_input )
1113 p_instance->i_index = i;
1116 if( p_instance->item.psz_uri ) free( p_instance->item.psz_uri );
1117 p_instance->item.psz_uri =
1118 strdup( media->input[p_instance->i_index] );
1120 if( p_instance->p_input )
1122 input_StopThread( p_instance->p_input );
1123 input_DestroyThread( p_instance->p_input );
1124 vlc_object_detach( p_instance->p_input );
1125 vlc_object_destroy( p_instance->p_input );
1128 p_instance->p_input = input_CreateThread( vlm, &p_instance->item );
1129 if( !p_instance->p_input )
1131 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1132 vlc_input_item_Clean( &p_instance->item );
1133 if( p_instance->psz_name ) free( p_instance->psz_name );
1139 if( !p_instance ) return VLC_EGENERIC;
1141 if( !strcmp( psz_command, "seek" ) )
1146 if( psz_args && sscanf( psz_args, "%f", &f_percentage ) == 1 )
1148 val.f_float = f_percentage / 100.0 ;
1149 var_Set( p_instance->p_input, "position", val );
1153 else if( !strcmp( psz_command, "stop" ) )
1155 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1157 if( p_instance->p_input )
1159 input_StopThread( p_instance->p_input );
1160 input_DestroyThread( p_instance->p_input );
1161 vlc_object_detach( p_instance->p_input );
1162 vlc_object_destroy( p_instance->p_input );
1165 vlc_input_item_Clean( &p_instance->item );
1166 if( p_instance->psz_name ) free( p_instance->psz_name );
1171 else if( !strcmp( psz_command, "pause" ) )
1175 if( !p_instance->p_input ) return VLC_SUCCESS;
1177 var_Get( p_instance->p_input, "state", &val );
1179 if( val.i_int == PAUSE_S ) val.i_int = PLAYING_S;
1180 else val.i_int = PAUSE_S;
1181 var_Set( p_instance->p_input, "state", val );
1186 return VLC_EGENERIC;
1189 /*****************************************************************************
1191 *****************************************************************************/
1192 static vlm_schedule_t *vlm_ScheduleNew( vlm_t *vlm, char *psz_name )
1194 vlm_schedule_t *p_sched = malloc( sizeof( vlm_schedule_t ) );
1206 p_sched->psz_name = strdup( psz_name );
1207 p_sched->b_enabled = VLC_FALSE;
1208 p_sched->i_command = 0;
1209 p_sched->command = NULL;
1210 p_sched->i_date = 0;
1211 p_sched->i_period = 0;
1212 p_sched->i_repeat = -1;
1214 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1219 /* for now, simple delete. After, del with options (last arg) */
1220 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_t *sched,
1223 if( sched == NULL ) return;
1225 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1227 if( vlm->i_schedule == 0 && vlm->schedule ) free( vlm->schedule );
1228 free( sched->psz_name );
1229 while( sched->i_command-- ) free( sched->command[sched->i_command] );
1233 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *vlm, char *psz_name )
1237 for( i = 0; i < vlm->i_schedule; i++ )
1239 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1241 return vlm->schedule[i];
1248 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1249 static int vlm_ScheduleSetup( vlm_schedule_t *schedule, char *psz_cmd,
1252 if( !strcmp( psz_cmd, "enabled" ) )
1254 schedule->b_enabled = VLC_TRUE;
1256 else if( !strcmp( psz_cmd, "disabled" ) )
1258 schedule->b_enabled = VLC_FALSE;
1260 #if !defined( UNDER_CE )
1261 else if( !strcmp( psz_cmd, "date" ) )
1267 time.tm_sec = 0; /* seconds */
1268 time.tm_min = 0; /* minutes */
1269 time.tm_hour = 0; /* hours */
1270 time.tm_mday = 0; /* day of the month */
1271 time.tm_mon = 0; /* month */
1272 time.tm_year = 0; /* year */
1273 time.tm_wday = 0; /* day of the week */
1274 time.tm_yday = 0; /* day in the year */
1275 time.tm_isdst = 0; /* daylight saving time */
1277 /* date should be year/month/day-hour:minutes:seconds */
1278 p = strchr( psz_value, '-' );
1280 if( !strcmp( psz_value, "now" ) )
1282 schedule->i_date = 0;
1284 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 */
1292 switch( sscanf( p + 1, "%d:%d:%d", &i, &j, &k ) )
1312 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1318 time.tm_mon = i - 1;
1322 time.tm_year = i - 1900;
1323 time.tm_mon = j - 1;
1330 date = mktime( &time );
1331 schedule->i_date = ((mtime_t) date) * 1000000;
1334 else if( !strcmp( psz_cmd, "period" ) )
1338 char *psz_time = NULL, *psz_date = NULL;
1342 /* First, if date or period are modified, repeat should be equal to -1 */
1343 schedule->i_repeat = -1;
1345 time.tm_sec = 0; /* seconds */
1346 time.tm_min = 0; /* minutes */
1347 time.tm_hour = 0; /* hours */
1348 time.tm_mday = 0; /* day of the month */
1349 time.tm_mon = 0; /* month */
1350 time.tm_year = 0; /* year */
1351 time.tm_wday = 0; /* day of the week */
1352 time.tm_yday = 0; /* day in the year */
1353 time.tm_isdst = 0; /* daylight saving time */
1355 /* date should be year/month/day-hour:minutes:seconds */
1356 p = strchr( psz_value, '-' );
1359 psz_date = psz_value;
1366 psz_time = psz_value;
1370 switch( sscanf( psz_time, "%d:%d:%d", &i, &j, &k ) )
1389 switch( sscanf( psz_date, "%d/%d/%d", &i, &j, &k ) )
1408 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1409 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1410 schedule->i_period = ((mtime_t) date) * 1000000;
1412 #endif /* UNDER_CE */
1413 else if( !strcmp( psz_cmd, "repeat" ) )
1417 if( sscanf( psz_value, "%d", &i ) == 1 )
1419 schedule->i_repeat = i;
1426 else if( !strcmp( psz_cmd, "append" ) )
1428 char *command = strdup( psz_value );
1430 TAB_APPEND( schedule->i_command, schedule->command, command );
1439 /*****************************************************************************
1440 * Message handling functions
1441 *****************************************************************************/
1442 static vlm_message_t *vlm_MessageNew( char *psz_name,
1443 const char *psz_format, ... )
1445 vlm_message_t *p_message;
1448 if( !psz_name ) return 0;
1450 p_message = malloc( sizeof(vlm_message_t) );
1456 p_message->psz_value = 0;
1460 va_start( args, psz_format );
1461 if( vasprintf( &p_message->psz_value, psz_format, args ) < 0 )
1470 p_message->psz_name = strdup( psz_name );
1471 p_message->i_child = 0;
1472 p_message->child = NULL;
1477 void vlm_MessageDelete( vlm_message_t *p_message )
1479 if( p_message->psz_name ) free( p_message->psz_name );
1480 if( p_message->psz_value ) free( p_message->psz_value );
1481 while( p_message->i_child-- )
1482 vlm_MessageDelete( p_message->child[p_message->i_child] );
1483 if( p_message->child ) free( p_message->child );
1488 static vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1489 vlm_message_t *p_child )
1491 if( p_message == NULL ) return NULL;
1495 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1501 /*****************************************************************************
1502 * Misc utility functions
1503 *****************************************************************************/
1504 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_t *media,
1505 vlm_schedule_t *schedule, char *psz_filter )
1511 vlm_message_t *msg_media;
1512 vlm_message_t *msg_child;
1514 msg = vlm_MessageNew( "show", NULL );
1515 msg_media = vlm_MessageAdd( msg, vlm_MessageNew( media->psz_name, 0 ));
1517 vlm_MessageAdd( msg_media,
1518 vlm_MessageNew( "type", media->i_type == VOD_TYPE ?
1519 "vod" : "broadcast" ) );
1520 vlm_MessageAdd( msg_media,
1521 vlm_MessageNew( "enabled", media->b_enabled ?
1524 vlm_MessageAdd( msg_media,
1525 vlm_MessageNew( "loop", media->b_loop ?
1528 if( media->i_type == VOD_TYPE && media->psz_mux )
1529 vlm_MessageAdd( msg_media,
1530 vlm_MessageNew( "mux", media->psz_mux ) );
1532 msg_child = vlm_MessageAdd( msg_media,
1533 vlm_MessageNew( "inputs", NULL ) );
1535 for( i = 0; i < media->i_input; i++ )
1537 vlm_MessageAdd( msg_child,
1538 vlm_MessageNew( media->input[i], NULL ) );
1541 vlm_MessageAdd( msg_media,
1542 vlm_MessageNew( "output", media->psz_output ?
1543 media->psz_output : "" ) );
1545 msg_child = vlm_MessageAdd( msg_media, vlm_MessageNew( "options", 0 ));
1547 for( i = 0; i < media->i_option; i++ )
1549 vlm_MessageAdd( msg_child, vlm_MessageNew( media->option[i], 0 ) );
1552 msg_child = vlm_MessageAdd( msg_media,
1553 vlm_MessageNew( "instances", NULL ) );
1555 for( i = 0; i < media->i_instance; i++ )
1557 vlm_media_instance_t *p_instance = media->instance[i];
1560 if( !p_instance->p_input ) val.i_int = END_S;
1561 else var_Get( p_instance->p_input, "state", &val );
1563 vlm_MessageAdd( msg_child,
1564 vlm_MessageNew( p_instance->psz_name ?
1565 p_instance->psz_name : "default",
1566 val.i_int == PLAYING_S ? "playing" :
1567 val.i_int == PAUSE_S ? "paused" :
1575 else if( schedule != NULL )
1579 vlm_message_t *msg_schedule;
1580 vlm_message_t *msg_child;
1583 msg = vlm_MessageNew( "show", NULL );
1585 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, 0 ) );
1587 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1589 vlm_MessageAdd( msg_schedule,
1590 vlm_MessageNew( "enabled", schedule->b_enabled ?
1593 #if !defined( UNDER_CE )
1594 if( schedule->i_date != 0 )
1597 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1600 #ifdef HAVE_LOCALTIME_R
1601 localtime_r( &i_time, &date);
1603 struct tm *p_date = localtime( &i_time );
1607 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1608 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1609 date.tm_hour, date.tm_min, date.tm_sec );
1611 vlm_MessageAdd( msg_schedule,
1612 vlm_MessageNew( "date", psz_date ) );
1617 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1620 if( schedule->i_period != 0 )
1622 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1625 date.tm_sec = (int)( i_time % 60 );
1626 i_time = i_time / 60;
1627 date.tm_min = (int)( i_time % 60 );
1628 i_time = i_time / 60;
1629 date.tm_hour = (int)( i_time % 24 );
1630 i_time = i_time / 24;
1631 date.tm_mday = (int)( i_time % 30 );
1632 i_time = i_time / 30;
1633 /* okay, okay, months are not always 30 days long */
1634 date.tm_mon = (int)( i_time % 12 );
1635 i_time = i_time / 12;
1636 date.tm_year = (int)i_time;
1638 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1639 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1641 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1645 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1647 #endif /* UNDER_CE */
1649 sprintf( buffer, "%d", schedule->i_repeat );
1650 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1653 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", 0) );
1655 for( i = 0; i < schedule->i_command; i++ )
1657 vlm_MessageAdd( msg_child,
1658 vlm_MessageNew( schedule->command[i], NULL ) );
1665 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1669 vlm_message_t *msg_child;
1671 msg = vlm_MessageNew( "show", NULL );
1672 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "media", NULL ) );
1674 for( i = 0; i < vlm->i_media; i++ )
1676 vlm_media_t *m = vlm->media[i];
1677 vlm_message_t *msg_media, *msg_instance;
1679 msg_media = vlm_MessageAdd( msg_child,
1680 vlm_MessageNew( m->psz_name, 0 ) );
1682 vlm_MessageAdd( msg_media,
1683 vlm_MessageNew( "type", m->i_type == VOD_TYPE ?
1684 "vod" : "broadcast" ) );
1686 vlm_MessageAdd( msg_media,
1687 vlm_MessageNew( "enabled", m->b_enabled ?
1690 if( m->i_type == VOD_TYPE && m->psz_mux )
1691 vlm_MessageAdd( msg_media,
1692 vlm_MessageNew( "mux", m->psz_mux ) );
1694 msg_instance = vlm_MessageAdd( msg_media,
1695 vlm_MessageNew( "instances", 0 ) );
1697 for( j = 0; j < m->i_instance; j++ )
1699 vlm_media_instance_t *p_instance = m->instance[j];
1702 if( !p_instance->p_input ) val.i_int = END_S;
1703 else var_Get( p_instance->p_input, "state", &val );
1705 vlm_MessageAdd( msg_instance,
1706 vlm_MessageNew( p_instance->psz_name ?
1707 p_instance->psz_name : "default",
1708 val.i_int == PLAYING_S ? "playing" :
1709 val.i_int == PAUSE_S ? "paused" :
1717 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1721 vlm_message_t *msg_child;
1723 msg = vlm_MessageNew( "show", NULL );
1724 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", NULL ) );
1726 for( i = 0; i < vlm->i_schedule; i++ )
1728 vlm_schedule_t *s = vlm->schedule[i];
1729 vlm_message_t *msg_schedule;
1730 mtime_t i_time, i_next_date;
1732 msg_schedule = vlm_MessageAdd( msg_child,
1733 vlm_MessageNew( s->psz_name, 0 ) );
1734 vlm_MessageAdd( msg_schedule,
1735 vlm_MessageNew( "enabled", s->b_enabled ?
1738 if( !s->b_enabled ) return msg;
1741 vlm_MessageAdd( msg_schedule,
1742 vlm_MessageNew( "enabled", "yes" ) );
1744 /* calculate next date */
1746 i_next_date = s->i_date;
1748 if( s->i_period != 0 )
1751 while( s->i_date + j * s->i_period <= i_time &&
1757 i_next_date = s->i_date + j * s->i_period;
1760 if( i_next_date > i_time )
1762 time_t i_date = (time_t) (i_next_date / 1000000) ;
1764 #if !defined( UNDER_CE )
1767 ctime_r( &i_date, psz_date );
1769 char *psz_date = ctime( &i_date );
1772 vlm_MessageAdd( msg_schedule,
1773 vlm_MessageNew( "next launch", psz_date ) );
1781 else if( psz_filter == NULL && media == NULL && schedule == NULL )
1783 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1784 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1786 vlm_MessageAdd( show1, show2->child[0] );
1788 /* We must destroy the parent node "show" of show2
1789 * and not the children */
1790 free( show2->psz_name );
1798 return vlm_MessageNew( "show", NULL );
1802 static vlm_message_t *vlm_Help( vlm_t *vlm, char *psz_filter )
1804 vlm_message_t *message, *message_child;
1806 #define MessageAdd( a ) \
1807 vlm_MessageAdd( message, vlm_MessageNew( a, NULL ) );
1808 #define MessageAddChild( a ) \
1809 vlm_MessageAdd( message_child, vlm_MessageNew( a, NULL ) );
1811 if( psz_filter == NULL )
1813 message = vlm_MessageNew( "help", NULL );
1815 message_child = MessageAdd( "Commands Syntax:" );
1816 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
1817 MessageAddChild( "setup (name) (properties)" );
1818 MessageAddChild( "show [(name)|media|schedule]" );
1819 MessageAddChild( "del (name)|all|media|schedule" );
1820 MessageAddChild( "control (name) [instance_name] (command)" );
1821 MessageAddChild( "save (config_file)" );
1822 MessageAddChild( "load (config_file)" );
1824 message_child = MessageAdd( "Media Proprieties Syntax:" );
1825 MessageAddChild( "input (input_name)" );
1826 MessageAddChild( "output (output_name)" );
1827 MessageAddChild( "enabled|disabled" );
1828 MessageAddChild( "loop|unloop (broadcast only)" );
1829 MessageAddChild( "mux (mux_name)" );
1831 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
1832 MessageAddChild( "enabled|disabled" );
1833 MessageAddChild( "append (command_until_rest_of_the_line)" );
1834 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
1836 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
1837 "(days)-(hours):(minutes):(seconds)" );
1838 MessageAddChild( "repeat (number_of_repetitions)" );
1840 message_child = MessageAdd( "Control Commands Syntax:" );
1841 MessageAddChild( "play" );
1842 MessageAddChild( "pause" );
1843 MessageAddChild( "stop" );
1844 MessageAddChild( "seek (percentage)" );
1849 return vlm_MessageNew( "help", NULL );
1852 /*****************************************************************************
1853 * Config handling functions
1854 *****************************************************************************/
1855 static int vlm_Load( vlm_t *vlm, char *file )
1859 while( *pf != '\0' )
1861 vlm_message_t *message = NULL;
1865 while( pf[i_temp] != '\n' && pf[i_temp] != '\0' && pf[i_temp] != '\r' )
1870 if( pf[i_temp] == '\r' || pf[i_temp] == '\n' )
1873 i_next = i_temp + 1;
1880 if( ExecuteCommand( vlm, pf, &message ) )
1893 static char *vlm_Save( vlm_t *vlm )
1900 for( i = 0; i < vlm->i_media; i++ )
1902 vlm_media_t *media = vlm->media[i];
1904 if( media->i_type == VOD_TYPE )
1906 i_length += strlen( "new vod " ) + strlen(media->psz_name);
1910 i_length += strlen( "new broadcast " ) + strlen(media->psz_name);
1913 if( media->b_enabled == VLC_TRUE )
1915 i_length += strlen( "enabled" );
1919 i_length += strlen( "disabled" );
1922 if( media->b_loop == VLC_TRUE )
1924 i_length += strlen( " loop\n" );
1928 i_length += strlen( "\n" );
1931 for( j = 0; j < media->i_input; j++ )
1933 i_length += strlen( "setup input \"\"\n" ) +
1934 strlen( media->psz_name ) + strlen( media->input[j] );
1937 if( media->psz_output != NULL )
1939 i_length += strlen(media->psz_name) + strlen(media->psz_output) +
1940 strlen( "setup output \n" );
1943 for( j=0 ; j < media->i_option ; j++ )
1945 i_length += strlen(media->psz_name) + strlen(media->option[j]) +
1946 strlen("setup option \n");
1950 for( i = 0; i < vlm->i_schedule; i++ )
1952 vlm_schedule_t *schedule = vlm->schedule[i];
1954 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
1956 if( schedule->b_enabled == VLC_TRUE )
1958 i_length += strlen( "date //-:: enabled\n" ) + 14;
1962 i_length += strlen( "date //-:: disabled\n" ) + 14;
1966 if( schedule->i_period != 0 )
1968 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
1969 strlen( "period //-::\n" ) + 14;
1972 if( schedule->i_repeat >= 0 )
1976 sprintf( buffer, "%d", schedule->i_repeat );
1977 i_length += strlen( "setup repeat \n" ) +
1978 strlen( schedule->psz_name ) + strlen( buffer );
1985 for( j = 0; j < schedule->i_command; j++ )
1987 i_length += strlen( "setup append \n" ) +
1988 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
1993 /* Don't forget the '\0' */
1995 /* now we have the length of save */
1997 p = save = malloc( i_length );
2000 /* finally we can write in it */
2001 for( i = 0; i < vlm->i_media; i++ )
2003 vlm_media_t *media = vlm->media[i];
2005 if( media->i_type == VOD_TYPE )
2007 p += sprintf( p, "new %s vod ", media->psz_name);
2011 p += sprintf( p, "new %s broadcast ", media->psz_name);
2014 if( media->b_enabled == VLC_TRUE )
2016 p += sprintf( p, "enabled" );
2020 p += sprintf( p, "disabled" );
2023 if( media->b_loop == VLC_TRUE )
2025 p += sprintf( p, " loop\n" );
2029 p += sprintf( p, "\n" );
2032 for( j = 0; j < media->i_input; j++ )
2034 p += sprintf( p, "setup %s input \"%s\"\n", media->psz_name,
2038 if( media->psz_output != NULL )
2040 p += sprintf( p, "setup %s output %s\n", media->psz_name,
2041 media->psz_output );
2044 for( j = 0; j < media->i_option; j++ )
2046 p += sprintf( p, "setup %s option %s\n", media->psz_name,
2051 /* and now, the schedule scripts */
2052 #if !defined( UNDER_CE )
2053 for( i = 0; i < vlm->i_schedule; i++ )
2055 vlm_schedule_t *schedule = vlm->schedule[i];
2057 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
2059 #ifdef HAVE_LOCALTIME_R
2060 localtime_r( &i_time, &date);
2062 struct tm *p_date = localtime( &i_time );
2066 p += sprintf( p, "new %s schedule ", schedule->psz_name);
2068 if( schedule->b_enabled == VLC_TRUE )
2070 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
2071 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2072 date.tm_hour, date.tm_min, date.tm_sec );
2076 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
2077 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2078 date.tm_hour, date.tm_min, date.tm_sec);
2081 if( schedule->i_period != 0 )
2083 p += sprintf( p, "setup %s ", schedule->psz_name );
2085 i_time = (time_t) ( schedule->i_period / 1000000 );
2087 date.tm_sec = (int)( i_time % 60 );
2088 i_time = i_time / 60;
2089 date.tm_min = (int)( i_time % 60 );
2090 i_time = i_time / 60;
2091 date.tm_hour = (int)( i_time % 24 );
2092 i_time = i_time / 24;
2093 date.tm_mday = (int)( i_time % 30 );
2094 i_time = i_time / 30;
2095 /* okay, okay, months are not always 30 days long */
2096 date.tm_mon = (int)( i_time % 12 );
2097 i_time = i_time / 12;
2098 date.tm_year = (int)i_time;
2100 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
2101 date.tm_year, date.tm_mon, date.tm_mday,
2102 date.tm_hour, date.tm_min, date.tm_sec);
2105 if( schedule->i_repeat >= 0 )
2107 p += sprintf( p, "setup %s repeat %d\n",
2108 schedule->psz_name, schedule->i_repeat );
2112 p += sprintf( p, "\n" );
2115 for( j = 0; j < schedule->i_command; j++ )
2117 p += sprintf( p, "setup %s append %s\n",
2118 schedule->psz_name, schedule->command[j] );
2122 #endif /* UNDER_CE */
2127 /*****************************************************************************
2129 *****************************************************************************/
2130 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
2131 char *psz_id, int i_query, va_list args )
2133 vlm_t *vlm = (vlm_t *)p_private;
2134 int i, i_ret = VLC_EGENERIC;
2136 if( !p_private || !p_vod_media ) return VLC_EGENERIC;
2138 vlc_mutex_lock( &vlm->lock );
2141 for( i = 0; i < vlm->i_media; i++ )
2143 if( p_vod_media == vlm->media[i]->vod_media ) break;
2146 if( i == vlm->i_media )
2148 vlc_mutex_unlock( &vlm->lock );
2149 return VLC_EGENERIC;
2154 case VOD_MEDIA_PLAY:
2155 vlm->media[i]->psz_vod_output = (char *)va_arg( args, char * );
2156 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "play", 0 );
2157 vlm->media[i]->psz_vod_output = 0;
2160 case VOD_MEDIA_PAUSE:
2161 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "pause", 0 );
2164 case VOD_MEDIA_STOP:
2165 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "stop", 0 );
2168 case VOD_MEDIA_SEEK:
2170 double f_pos = (double)va_arg( args, double );
2173 sprintf( psz_pos, "%f", f_pos );
2174 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "seek", psz_pos);
2182 vlc_mutex_unlock( &vlm->lock );
2187 /*****************************************************************************
2189 *****************************************************************************/
2190 static int Manage( vlc_object_t* p_object )
2192 vlm_t *vlm = (vlm_t*)p_object;
2194 mtime_t i_lastcheck;
2197 i_lastcheck = mdate();
2201 while( !vlm->b_die )
2203 vlc_mutex_lock( &vlm->lock );
2205 /* destroy the inputs that wants to die, and launch the next input */
2206 for( i = 0; i < vlm->i_media; i++ )
2208 vlm_media_t *p_media = vlm->media[i];
2210 for( j = 0; j < p_media->i_instance; j++ )
2212 vlm_media_instance_t *p_instance = p_media->instance[j];
2214 if( !p_instance->p_input ||
2215 ( !p_instance->p_input->b_eof &&
2216 !p_instance->p_input->b_error ) ) continue;
2218 input_StopThread( p_instance->p_input );
2219 input_DestroyThread( p_instance->p_input );
2220 vlc_object_detach( p_instance->p_input );
2221 vlc_object_destroy( p_instance->p_input );
2223 p_instance->i_index++;
2224 if( p_instance->i_index == p_media->i_input &&
2225 p_media->b_loop ) p_instance->i_index = 0;
2227 if( p_instance->i_index < p_media->i_input )
2229 /* FIXME, find a way to select the right instance */
2231 sprintf( buffer, "%d", p_instance->i_index );
2232 vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2237 if( vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2238 "stop", 0 ) == VLC_SUCCESS ) i--;
2246 for( i = 0; i < vlm->i_schedule; i++ )
2248 mtime_t i_real_date = vlm->schedule[i]->i_date;
2250 if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2252 if( vlm->schedule[i]->i_date == 0 ) // now !
2254 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2255 i_real_date = i_time;
2257 else if( vlm->schedule[i]->i_period != 0 )
2260 while( vlm->schedule[i]->i_date + j *
2261 vlm->schedule[i]->i_period <= i_lastcheck &&
2262 ( vlm->schedule[i]->i_repeat > j ||
2263 vlm->schedule[i]->i_repeat == -1 ) )
2268 i_real_date = vlm->schedule[i]->i_date + j *
2269 vlm->schedule[i]->i_period;
2272 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2274 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2276 vlm_message_t *message = NULL;
2278 ExecuteCommand( vlm, vlm->schedule[i]->command[j],
2281 /* for now, drop the message */
2288 i_lastcheck = i_time;
2290 vlc_mutex_unlock( &vlm->lock );
2298 #else /* ENABLE_VLM */
2300 /* We just define an empty wrapper */
2301 vlm_t *__vlm_New( vlc_object_t *a )
2303 msg_Err( a, "VideoLAN manager support is disabled" );
2306 void vlm_Delete( vlm_t *a ){}
2307 int vlm_ExecuteCommand( vlm_t *a, char *b, vlm_message_t **c ){ return -1; }
2308 void vlm_MessageDelete( vlm_message_t *a ){}
2310 #endif /* ENABLE_VLM */