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 vlm_message_t *vlm_Show( vlm_t *, vlm_media_t *, vlm_schedule_t *, char * );
53 static vlm_message_t *vlm_Help( vlm_t *, char * );
55 static vlm_media_t *vlm_MediaSearch ( vlm_t *, char * );
56 static vlm_media_instance_t *vlm_MediaInstanceSearch( vlm_t *, vlm_media_t *, char * );
58 static vlm_message_t *vlm_MessageNew( char *, const char *, ... );
59 static vlm_message_t *vlm_MessageAdd( vlm_message_t *, vlm_message_t * );
61 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *, char * );
63 static char *Save( vlm_t * );
64 static int Load( vlm_t *, char * );
65 static int ExecuteCommand( vlm_t *, char *, vlm_message_t ** );
66 static int Manage( vlc_object_t * );
68 /*****************************************************************************
70 *****************************************************************************/
71 vlm_t *__vlm_New ( vlc_object_t *p_this )
77 /* to be sure to avoid multiple creation */
78 var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX );
79 var_Get( p_this->p_libvlc, "vlm_mutex", &lockval );
80 vlc_mutex_lock( lockval.p_address );
82 if( !(p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE )) )
84 msg_Info( p_this, "creating vlm" );
85 if( ( p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM ) ) == NULL )
87 vlc_mutex_unlock( lockval.p_address );
91 vlc_mutex_init( p_this->p_vlc, &p_vlm->lock );
95 p_vlm->i_schedule = 0;
96 p_vlm->schedule = NULL;
98 vlc_object_yield( p_vlm );
99 vlc_object_attach( p_vlm, p_this->p_vlc );
101 vlc_mutex_unlock( lockval.p_address );
103 if( vlc_thread_create( p_vlm, "vlm thread",
104 Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
106 vlc_mutex_destroy( &p_vlm->lock );
107 vlc_object_destroy( p_vlm );
111 /* Try loading the vlm conf file given by --vlm-conf */
112 psz_vlmconf = config_GetPsz( p_vlm, "vlm-conf" );
114 if( psz_vlmconf && *psz_vlmconf )
116 vlm_message_t *p_message = NULL;
117 char *psz_buffer = NULL;
119 msg_Dbg( p_this, "loading vlm conf ..." );
120 asprintf(&psz_buffer, "load %s", psz_vlmconf );
123 msg_Dbg( p_this, psz_buffer);
124 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) ){
125 msg_Warn( p_this, "error while loading the vlm conf file" );
136 /*****************************************************************************
138 *****************************************************************************/
139 void vlm_Delete( vlm_t *p_vlm )
143 var_Get( p_vlm->p_libvlc, "vlm_mutex", &lockval );
144 vlc_mutex_lock( lockval.p_address );
146 vlc_object_release( p_vlm );
148 if( p_vlm->i_refcount > 0 )
150 vlc_mutex_unlock( lockval.p_address );
154 p_vlm->b_die = VLC_TRUE;
155 vlc_thread_join( p_vlm );
157 vlc_mutex_destroy( &p_vlm->lock );
159 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0], NULL );
160 FREE( p_vlm->media );
162 while( p_vlm->i_schedule ) vlm_ScheduleDelete( p_vlm,
163 p_vlm->schedule[0], NULL );
164 FREE( p_vlm->schedule );
166 vlc_object_detach( p_vlm );
167 vlc_object_destroy( p_vlm );
168 vlc_mutex_unlock( lockval.p_address );
171 /*****************************************************************************
172 * vlm_ExecuteCommand:
173 *****************************************************************************/
174 int vlm_ExecuteCommand( vlm_t *p_vlm, char *psz_command,
175 vlm_message_t **pp_message)
179 vlc_mutex_lock( &p_vlm->lock );
180 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
181 vlc_mutex_unlock( &p_vlm->lock );
186 /*****************************************************************************
188 *****************************************************************************/
189 int vlm_Save( vlm_t *p_vlm, char *psz_file )
194 if( !p_vlm || !psz_file ) return 1;
196 file = fopen( psz_file, "w" );
197 if( file == NULL ) return 1;
199 psz_save = Save( p_vlm );
200 if( psz_save == NULL )
205 fwrite( psz_save, strlen( psz_save ), 1, file );
212 /*****************************************************************************
214 *****************************************************************************/
215 int vlm_Load( vlm_t *p_vlm, char *psz_file )
221 if( !p_vlm || !psz_file ) return 1;
223 file = fopen( psz_file, "r" );
224 if( file == NULL ) return 1;
226 if( fseek( file, 0, SEEK_END) != 0 ) return 2;
228 i_size = ftell( file );
229 fseek( file, 0, SEEK_SET);
230 psz_buffer = malloc( i_size + 1 );
236 fread( psz_buffer, 1, i_size, file);
237 psz_buffer[ i_size ] = '\0';
238 if( Load( p_vlm, psz_buffer ) )
250 /*****************************************************************************
252 *****************************************************************************/
253 static char *FindEndCommand( char *psz )
255 char *psz_sent = psz;
262 while( ( *psz_sent != '\"' ) && ( *psz_sent != '\0' ) )
264 if( *psz_sent == '\'' )
266 psz_sent = FindEndCommand( psz_sent );
267 if( psz_sent == NULL ) return NULL;
272 if( *psz_sent == '\"' )
278 /* *psz_sent == '\0' -> number of " is incorrect */
286 while( ( *psz_sent != '\'' ) && ( *psz_sent != '\0' ) )
288 if( *psz_sent == '\"' )
290 psz_sent = FindEndCommand( psz_sent );
291 if( psz_sent == NULL ) return NULL;
296 if( *psz_sent == '\'' )
302 /* *psz_sent == '\0' -> number of " is incorrect */
307 default: /* now we can look for spaces */
308 while( ( *psz_sent != ' ' ) && ( *psz_sent != '\0' ) )
310 if( ( *psz_sent == '\'' ) || ( *psz_sent == '\"' ) )
312 psz_sent = FindEndCommand( psz_sent );
321 /*****************************************************************************
322 * ExecuteCommand: The main state machine
323 *****************************************************************************
324 * Execute a command which ends with '\0' (string)
325 *****************************************************************************/
326 static int ExecuteCommand( vlm_t *p_vlm, char *psz_command,
327 vlm_message_t **pp_message )
330 char **ppsz_command = NULL;
331 char *psz_cmd = psz_command;
332 vlm_message_t *p_message = NULL;
335 /* First, parse the line and cut it */
336 while( *psz_cmd != '\0' )
339 if( *psz_cmd == ' ' || *psz_cmd == '\t' )
348 /* support for comments */
349 if( i_command == 0 && *psz_cmd == '#')
351 p_message = vlm_MessageNew( "", NULL );
355 psz_temp = FindEndCommand( psz_cmd );
357 if( psz_temp == NULL ) goto error;
359 i_temp = psz_temp - psz_cmd;
361 ppsz_command = realloc( ppsz_command, (i_command + 1) *
363 ppsz_command[ i_command ] = malloc( (i_temp + 1) * sizeof(char) );
364 strncpy( ppsz_command[ i_command ], psz_cmd, i_temp );
365 ppsz_command[ i_command ][ i_temp ] = '\0';
374 * And then Interpret it
379 p_message = vlm_MessageNew( "", NULL );
383 if( !strcmp(ppsz_command[0], "new") )
387 /* Check the number of arguments */
388 if( i_command < 3 ) goto syntax_error;
391 if( !strcmp(ppsz_command[2], "vod") )
395 else if( !strcmp(ppsz_command[2], "broadcast") )
397 i_type = BROADCAST_TYPE;
399 else if( !strcmp(ppsz_command[2], "schedule") )
401 i_type = SCHEDULE_TYPE;
406 vlm_MessageNew( "new", "%s: Choose between vod, "
407 "broadcast or schedule", ppsz_command[1] );
411 /* Check for forbidden media names */
412 if( !strcmp(ppsz_command[1], "all") ||
413 !strcmp(ppsz_command[1], "media") ||
414 !strcmp(ppsz_command[1], "schedule") )
416 p_message = vlm_MessageNew( "new", "\"all\", \"media\" and "
417 "\"schedule\" are reserved names" );
421 /* Check the name is not already in use */
422 if( vlm_ScheduleSearch( p_vlm, ppsz_command[1] ) ||
423 vlm_MediaSearch( p_vlm, ppsz_command[1] ) )
425 p_message = vlm_MessageNew( "new", "%s: Name already in use",
431 if( i_type == SCHEDULE_TYPE )
433 vlm_schedule_t *p_schedule;
434 p_schedule = vlm_ScheduleNew( p_vlm, ppsz_command[1] );
437 p_message = vlm_MessageNew( "new", "could not create schedule" );
445 vlm_media_t *p_media;
446 p_media = vlm_MediaNew( p_vlm, ppsz_command[1], i_type );
449 p_message = vlm_MessageNew( "new", "could not create media" );
456 p_message = vlm_MessageNew( "new", NULL );
460 /* Properties will be dealt with later on */
463 else if( !strcmp(ppsz_command[0], "setup") )
465 if( i_command < 2 ) goto syntax_error;
467 /* Properties will be dealt with later on */
470 else if( !strcmp(ppsz_command[0], "del") )
472 vlm_media_t *p_media;
473 vlm_schedule_t *p_schedule;
475 if( i_command < 2 ) goto syntax_error;
477 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
478 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
480 if( p_schedule != NULL )
482 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
484 else if( p_media != NULL )
486 vlm_MediaDelete( p_vlm, p_media, NULL );
488 else if( !strcmp(ppsz_command[1], "media") )
490 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
493 else if( !strcmp(ppsz_command[1], "schedule") )
495 while( p_vlm->i_schedule )
496 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
498 else if( !strcmp(ppsz_command[1], "all") )
500 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
503 while( p_vlm->i_schedule )
504 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
508 p_message = vlm_MessageNew( "del", "%s: media unknown",
513 p_message = vlm_MessageNew( "del", NULL );
517 else if( !strcmp(ppsz_command[0], "show") )
519 vlm_media_t *p_media;
520 vlm_schedule_t *p_schedule;
524 p_message = vlm_Show( p_vlm, NULL, NULL, NULL );
527 else if( i_command > 2 ) goto syntax_error;
529 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
530 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
532 if( p_schedule != NULL )
534 p_message = vlm_Show( p_vlm, NULL, p_schedule, NULL );
536 else if( p_media != NULL )
538 p_message = vlm_Show( p_vlm, p_media, NULL, NULL );
542 p_message = vlm_Show( p_vlm, NULL, NULL, ppsz_command[1] );
548 else if( !strcmp(ppsz_command[0], "help") )
550 if( i_command != 1 ) goto syntax_error;
552 p_message = vlm_Help( p_vlm, NULL );
556 else if( !strcmp(ppsz_command[0], "control") )
558 vlm_media_t *p_media;
560 if( i_command < 3 ) goto syntax_error;
562 if( !(p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] ) ) )
564 p_message = vlm_MessageNew( "control", "%s: media unknown",
570 char *psz_command, *psz_arg = 0, *psz_instance = 0;
573 if( strcmp( ppsz_command[2], "play" ) &&
574 strcmp( ppsz_command[2], "stop" ) &&
575 strcmp( ppsz_command[2], "pause" ) &&
576 strcmp( ppsz_command[2], "seek" ) )
579 psz_instance = ppsz_command[2];
581 if( i_command < 4 ) goto syntax_error;
584 psz_command = ppsz_command[i_index];
586 if( i_command >= i_index + 2 ) psz_arg = ppsz_command[i_index + 1];
588 vlm_MediaControl( p_vlm, p_media, psz_instance, psz_command,
590 p_message = vlm_MessageNew( "control", NULL );
595 else if( !strcmp(ppsz_command[0], "save") )
597 if( i_command != 2 ) goto syntax_error;
599 if( vlm_Save( p_vlm, ppsz_command[1] ) )
601 p_message = vlm_MessageNew( "save", "Unable to save to file" );
606 p_message = vlm_MessageNew( "save", NULL );
611 else if( !strcmp(ppsz_command[0], "load") )
613 if( i_command != 2 ) goto syntax_error;
615 switch( vlm_Load( p_vlm, ppsz_command[1] ) )
618 p_message = vlm_MessageNew( "load", NULL );
621 p_message = vlm_MessageNew( "load", "read file error" );
625 vlm_MessageNew( "load", "error while loading file" );
629 vlm_MessageNew( "load", "Unable to load from file" );
636 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
640 /* Common code between "new" and "setup" */
641 if( !strcmp(ppsz_command[0], "new") ||
642 !strcmp(ppsz_command[0], "setup") )
644 int i_command_start = strcmp(ppsz_command[0], "new") ? 2 : 3;
645 vlm_media_t *p_media;
646 vlm_schedule_t *p_schedule;
648 if( i_command < i_command_start ) goto syntax_error;
650 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
651 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
653 if( !p_media && !p_schedule )
655 p_message = vlm_MessageNew( ppsz_command[0], "%s unknown",
660 if( p_schedule != NULL )
662 for( i = i_command_start ; i < i_command ; i++ )
664 if( !strcmp( ppsz_command[i], "enabled" ) ||
665 !strcmp( ppsz_command[i], "disabled" ) )
667 vlm_ScheduleSetup( p_schedule, ppsz_command[i], NULL );
670 /* Beware: everything behind append is considered as
672 else if( !strcmp( ppsz_command[i], "append" ) )
674 if( ++i >= i_command ) break;
676 for( j = i + 1; j < i_command; j++ )
679 realloc( ppsz_command[i], strlen(ppsz_command[i]) +
680 strlen(ppsz_command[j]) + 1 + 1 );
681 strcat( ppsz_command[i], " " );
682 strcat( ppsz_command[i], ppsz_command[j] );
685 vlm_ScheduleSetup( p_schedule, ppsz_command[i - 1],
691 if( i + 1 >= i_command && !strcmp(ppsz_command[0], "new") )
693 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
695 vlm_MessageNew( ppsz_command[0],
696 "Wrong properties syntax" );
699 else if( i + 1 >= i_command )
702 vlm_MessageNew( ppsz_command[0],
703 "Wrong properties syntax" );
707 vlm_ScheduleSetup( p_schedule, ppsz_command[i],
714 else if( p_media != NULL )
716 for( i = i_command_start ; i < i_command ; i++ )
718 if( !strcmp( ppsz_command[i], "enabled" ) ||
719 !strcmp( ppsz_command[i], "disabled" ) )
721 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
723 else if( i + 1 >= i_command &&
724 !strcmp( ppsz_command[i], "mux") )
726 if( p_media->i_type != VOD_TYPE )
728 p_message = vlm_MessageNew( ppsz_command[0],
729 "mux only available for broadcast" );
733 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
738 else if( !strcmp( ppsz_command[i], "loop" ) ||
739 !strcmp( ppsz_command[i], "unloop" ) )
741 if( p_media->i_type != BROADCAST_TYPE )
743 p_message = vlm_MessageNew( ppsz_command[0],
744 "loop only available for broadcast" );
748 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
753 if( i + 1 >= i_command &&
754 !strcmp(ppsz_command[0], "new") )
756 vlm_MediaDelete( p_vlm, p_media, NULL );
758 vlm_MessageNew( ppsz_command[0],
759 "Wrong properties syntax" );
762 else if( i + 1 >= i_command )
765 vlm_MessageNew( ppsz_command[0],
766 "Wrong properties syntax" );
770 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
777 p_message = vlm_MessageNew( ppsz_command[0], NULL );
782 for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
783 FREE( ppsz_command );
784 *pp_message = p_message;
789 p_message = vlm_MessageNew( ppsz_command[0], "Wrong command syntax" );
792 for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
793 FREE( ppsz_command );
794 *pp_message = p_message;
799 static vlm_media_t *vlm_MediaSearch( vlm_t *vlm, char *psz_name )
803 for( i = 0; i < vlm->i_media; i++ )
805 if( strcmp( psz_name, vlm->media[i]->psz_name ) == 0 )
807 return vlm->media[i];
814 /*****************************************************************************
816 *****************************************************************************/
817 static vlm_media_instance_t *
818 vlm_MediaInstanceSearch( vlm_t *vlm, vlm_media_t *media, char *psz_name )
822 for( i = 0; i < media->i_instance; i++ )
824 if( ( !psz_name && !media->instance[i]->psz_name ) ||
825 ( psz_name && media->instance[i]->psz_name &&
826 !strcmp( psz_name, media->instance[i]->psz_name ) ) )
828 return media->instance[i];
835 vlm_media_t *vlm_MediaNew( vlm_t *vlm, char *psz_name, int i_type )
837 vlm_media_t *media = malloc( sizeof( vlm_media_t ) );
841 msg_Err( vlm, "out of memory" );
845 /* Check if we need to load the VOD server */
846 if( i_type == VOD_TYPE && !vlm->i_vod )
848 vlm->vod = vlc_object_create( vlm, VLC_OBJECT_VOD );
849 vlc_object_attach( vlm->vod, vlm );
850 vlm->vod->p_module = module_Need( vlm->vod, "vod server", 0, 0 );
851 if( !vlm->vod->p_module )
853 msg_Err( vlm, "cannot find vod server" );
854 vlc_object_detach( vlm->vod );
855 vlc_object_destroy( vlm->vod );
861 vlm->vod->p_data = vlm;
862 vlm->vod->pf_media_control = vlm_MediaVodControl;
865 if( i_type == VOD_TYPE ) vlm->i_vod++;
867 media->psz_name = strdup( psz_name );
868 media->b_enabled = VLC_FALSE;
869 media->b_loop = VLC_FALSE;
870 media->vod_media = NULL;
871 media->psz_vod_output = NULL;
872 media->psz_mux = NULL;
875 media->psz_output = NULL;
877 media->option = NULL;
878 media->i_type = i_type;
879 media->i_instance = 0;
880 media->instance = NULL;
882 media->item.psz_uri = strdup( psz_name );
883 vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
885 TAB_APPEND( vlm->i_media, vlm->media, media );
890 /* for now, simple delete. After, del with options (last arg) */
891 void vlm_MediaDelete( vlm_t *vlm, vlm_media_t *media, char *psz_name )
893 if( media == NULL ) return;
895 while( media->i_instance )
897 vlm_media_instance_t *p_instance = media->instance[0];
898 vlm_MediaControl( vlm, media, p_instance->psz_name, "stop", 0 );
901 TAB_REMOVE( vlm->i_media, vlm->media, media );
903 if( media->i_type == VOD_TYPE )
905 vlm_MediaSetup( vlm, media, "disabled", 0 );
909 /* Check if we need to unload the VOD server */
910 if( media->i_type == VOD_TYPE && !vlm->i_vod )
912 module_Unneed( vlm->vod, vlm->vod->p_module );
913 vlc_object_detach( vlm->vod );
914 vlc_object_destroy( vlm->vod );
918 if( vlm->i_media == 0 && vlm->media ) free( vlm->media );
920 free( media->psz_name );
922 while( media->i_input-- ) free( media->input[media->i_input] );
923 if( media->input ) free( media->input );
925 if( media->psz_output ) free( media->psz_output );
926 if( media->psz_mux ) free( media->psz_mux );
928 while( media->i_option-- ) free( media->option[media->i_option] );
929 if( media->option ) free( media->option );
931 vlc_input_item_Clean( &media->item );
936 int vlm_MediaSetup( vlm_t *vlm, vlm_media_t *media, char *psz_cmd,
939 if( !psz_cmd) return VLC_EGENERIC;
941 if( !strcmp( psz_cmd, "loop" ) )
943 media->b_loop = VLC_TRUE;
945 else if( !strcmp( psz_cmd, "unloop" ) )
947 media->b_loop = VLC_FALSE;
949 else if( !strcmp( psz_cmd, "enabled" ) )
951 media->b_enabled = VLC_TRUE;
953 else if( !strcmp( psz_cmd, "disabled" ) )
955 media->b_enabled = VLC_FALSE;
957 else if( !strcmp( psz_cmd, "mux" ) )
959 if( media->psz_mux ) free( media->psz_mux );
960 media->psz_mux = NULL;
961 if( psz_value ) media->psz_mux = strdup( psz_value );
963 else if( !strcmp( psz_cmd, "input" ) )
967 if( psz_value != NULL && strlen(psz_value) > 1 &&
968 ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
969 ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
970 psz_value[ strlen(psz_value) - 1 ] == '\"' ) )
972 input = malloc( strlen(psz_value) - 1 );
974 memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
975 input[ strlen(psz_value) - 2 ] = '\0';
979 input = strdup( psz_value );
982 TAB_APPEND( media->i_input, media->input, input );
984 else if( !strcmp( psz_cmd, "inputdel" ) && !strcmp( psz_value, "all" ) )
986 while( media->i_input > 0 )
988 TAB_REMOVE( media->i_input, media->input, media->input[0] );
991 else if( !strcmp( psz_cmd, "inputdel" ) )
996 if( psz_value != NULL && strlen(psz_value) > 1 &&
997 ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
998 ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
999 psz_value[ strlen(psz_value) - 1 ] == '\"' ) )
1001 input = malloc( strlen(psz_value) - 1 );
1003 memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
1004 input[ strlen(psz_value) - 2 ] = '\0';
1008 input = strdup( psz_value );
1011 for( i = 0; i < media->i_input; i++ )
1013 if( !strcmp( input, media->input[i] ) )
1015 TAB_REMOVE( media->i_input, media->input, media->input[i] );
1020 else if( !strcmp( psz_cmd, "inputdeln" ) )
1022 int index = atoi( psz_value );
1023 if( index > 0 && index <= media->i_input )
1025 TAB_REMOVE( media->i_input, media->input, media->input[index-1] );
1028 else if( !strcmp( psz_cmd, "output" ) )
1030 if( media->psz_output != NULL )
1032 free( media->psz_output );
1034 media->psz_output = strdup( psz_value );
1036 else if( !strcmp( psz_cmd, "option" ) )
1039 psz_option = strdup( psz_value );
1041 TAB_APPEND( media->i_option, media->option, psz_option );
1045 return VLC_EGENERIC;
1048 /* Check if we need to create/delete a vod media */
1049 if( media->i_type == VOD_TYPE )
1051 if( !media->b_enabled && media->vod_media )
1053 vlm->vod->pf_media_del( vlm->vod, media->vod_media );
1054 media->vod_media = 0;
1056 else if( media->b_enabled && !media->vod_media && media->i_input )
1058 /* Pre-parse the input */
1059 input_thread_t *p_input;
1063 vlc_input_item_Clean( &media->item );
1064 vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
1066 if( media->psz_output )
1067 asprintf( &psz_output, "%s:description", media->psz_output );
1069 asprintf( &psz_output, "#description" );
1071 media->item.psz_uri = strdup( media->input[0] );
1072 media->item.ppsz_options = malloc( sizeof( char* ) );
1073 asprintf( &media->item.ppsz_options[0], "sout=%s", psz_output);
1074 media->item.i_options = 1;
1075 for( i = 0; i < media->i_option; i++ )
1077 media->item.i_options++;
1078 media->item.ppsz_options =
1079 realloc( media->item.ppsz_options,
1080 media->item.i_options * sizeof( char* ) );
1081 media->item.ppsz_options[ media->item.i_options - 1 ] =
1082 strdup( media->option[i] );
1085 if( (p_input = input_CreateThread( vlm, &media->item ) ) )
1087 while( !p_input->b_eof && !p_input->b_error ) msleep( 100000 );
1089 input_StopThread( p_input );
1090 input_DestroyThread( p_input );
1091 vlc_object_detach( p_input );
1092 vlc_object_destroy( p_input );
1096 if( media->psz_mux )
1099 es_format_t es, *p_es = &es;
1102 sprintf( fourcc, "%4.4s", media->psz_mux );
1103 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
1104 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
1109 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
1112 vlm->vod->pf_media_new( vlm->vod, media->psz_name, &item );
1117 vlm->vod->pf_media_new( vlm->vod, media->psz_name,
1125 int vlm_MediaControl( vlm_t *vlm, vlm_media_t *media, char *psz_id,
1126 char *psz_command, char *psz_args )
1128 vlm_media_instance_t *p_instance;
1131 p_instance = vlm_MediaInstanceSearch( vlm, media, psz_id );
1133 if( !strcmp( psz_command, "play" ) && !p_instance )
1135 if( !media->b_enabled || media->i_input == 0 ) return 0;
1139 p_instance = malloc( sizeof(vlm_media_instance_t) );
1140 if( !p_instance ) return VLC_EGENERIC;
1141 memset( p_instance, 0, sizeof(vlm_media_instance_t) );
1142 vlc_input_item_Init( VLC_OBJECT(vlm), &p_instance->item );
1143 p_instance->p_input = NULL;
1145 if( media->psz_output != NULL || media->psz_vod_output != NULL )
1147 p_instance->item.ppsz_options = malloc( sizeof( char* ) );
1148 asprintf( &p_instance->item.ppsz_options[0], "sout=%s%s%s",
1149 media->psz_output ? media->psz_output : "",
1150 (media->psz_output && media->psz_vod_output) ?
1151 ":" : media->psz_vod_output ? "#" : "",
1152 media->psz_vod_output ? media->psz_vod_output : "" );
1153 p_instance->item.i_options = 1;
1156 for( i = 0; i < media->i_option; i++ )
1158 p_instance->item.i_options++;
1159 p_instance->item.ppsz_options =
1160 realloc( p_instance->item.ppsz_options,
1161 p_instance->item.i_options * sizeof( char* ) );
1162 p_instance->item.ppsz_options[p_instance->item.i_options - 1] =
1163 strdup( media->option[i] );
1166 p_instance->psz_name = psz_id ? strdup( psz_id ) : NULL;
1167 TAB_APPEND( media->i_instance, media->instance, p_instance );
1170 if( psz_args && sscanf(psz_args, "%d", &i) == 1 && i < media->i_input )
1172 p_instance->i_index = i;
1175 if( p_instance->item.psz_uri ) free( p_instance->item.psz_uri );
1176 p_instance->item.psz_uri =
1177 strdup( media->input[p_instance->i_index] );
1179 if( p_instance->p_input )
1181 input_StopThread( p_instance->p_input );
1182 input_DestroyThread( p_instance->p_input );
1183 vlc_object_detach( p_instance->p_input );
1184 vlc_object_destroy( p_instance->p_input );
1187 p_instance->p_input = input_CreateThread( vlm, &p_instance->item );
1188 if( !p_instance->p_input )
1190 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1191 vlc_input_item_Clean( &p_instance->item );
1192 if( p_instance->psz_name ) free( p_instance->psz_name );
1198 if( !p_instance ) return VLC_EGENERIC;
1200 if( !strcmp( psz_command, "seek" ) )
1205 if( psz_args && sscanf( psz_args, "%f", &f_percentage ) == 1 )
1207 val.f_float = f_percentage / 100.0 ;
1208 var_Set( p_instance->p_input, "position", val );
1212 else if( !strcmp( psz_command, "stop" ) )
1214 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1216 if( p_instance->p_input )
1218 input_StopThread( p_instance->p_input );
1219 input_DestroyThread( p_instance->p_input );
1220 vlc_object_detach( p_instance->p_input );
1221 vlc_object_destroy( p_instance->p_input );
1224 vlc_input_item_Clean( &p_instance->item );
1225 if( p_instance->psz_name ) free( p_instance->psz_name );
1230 else if( !strcmp( psz_command, "pause" ) )
1234 if( !p_instance->p_input ) return VLC_SUCCESS;
1236 var_Get( p_instance->p_input, "state", &val );
1238 if( val.i_int == PAUSE_S ) val.i_int = PLAYING_S;
1239 else val.i_int = PAUSE_S;
1240 var_Set( p_instance->p_input, "state", val );
1245 return VLC_EGENERIC;
1248 /*****************************************************************************
1250 *****************************************************************************/
1251 vlm_schedule_t *vlm_ScheduleNew( vlm_t *vlm, char *psz_name )
1253 vlm_schedule_t *p_sched = malloc( sizeof( vlm_schedule_t ) );
1265 p_sched->psz_name = strdup( psz_name );
1266 p_sched->b_enabled = VLC_FALSE;
1267 p_sched->i_command = 0;
1268 p_sched->command = NULL;
1269 p_sched->i_date = 0;
1270 p_sched->i_period = 0;
1271 p_sched->i_repeat = -1;
1273 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1278 /* for now, simple delete. After, del with options (last arg) */
1279 void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_t *sched,
1282 if( sched == NULL ) return;
1284 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1286 if( vlm->i_schedule == 0 && vlm->schedule ) free( vlm->schedule );
1287 free( sched->psz_name );
1288 while( sched->i_command-- ) free( sched->command[sched->i_command] );
1292 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *vlm, char *psz_name )
1296 for( i = 0; i < vlm->i_schedule; i++ )
1298 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1300 return vlm->schedule[i];
1307 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1308 int vlm_ScheduleSetup( vlm_schedule_t *schedule, char *psz_cmd,
1311 if( !strcmp( psz_cmd, "enabled" ) )
1313 schedule->b_enabled = VLC_TRUE;
1315 else if( !strcmp( psz_cmd, "disabled" ) )
1317 schedule->b_enabled = VLC_FALSE;
1319 #if !defined( UNDER_CE )
1320 else if( !strcmp( psz_cmd, "date" ) )
1326 time.tm_sec = 0; /* seconds */
1327 time.tm_min = 0; /* minutes */
1328 time.tm_hour = 0; /* hours */
1329 time.tm_mday = 0; /* day of the month */
1330 time.tm_mon = 0; /* month */
1331 time.tm_year = 0; /* year */
1332 time.tm_wday = 0; /* day of the week */
1333 time.tm_yday = 0; /* day in the year */
1334 time.tm_isdst = 0; /* daylight saving time */
1336 /* date should be year/month/day-hour:minutes:seconds */
1337 p = strchr( psz_value, '-' );
1339 if( !strcmp( psz_value, "now" ) )
1341 schedule->i_date = 0;
1343 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 */
1351 switch( sscanf( p + 1, "%d:%d:%d", &i, &j, &k ) )
1371 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1377 time.tm_mon = i - 1;
1381 time.tm_year = i - 1900;
1382 time.tm_mon = j - 1;
1389 date = mktime( &time );
1390 schedule->i_date = ((mtime_t) date) * 1000000;
1393 else if( !strcmp( psz_cmd, "period" ) )
1397 char *psz_time = NULL, *psz_date = NULL;
1401 /* First, if date or period are modified, repeat should be equal to -1 */
1402 schedule->i_repeat = -1;
1404 time.tm_sec = 0; /* seconds */
1405 time.tm_min = 0; /* minutes */
1406 time.tm_hour = 0; /* hours */
1407 time.tm_mday = 0; /* day of the month */
1408 time.tm_mon = 0; /* month */
1409 time.tm_year = 0; /* year */
1410 time.tm_wday = 0; /* day of the week */
1411 time.tm_yday = 0; /* day in the year */
1412 time.tm_isdst = 0; /* daylight saving time */
1414 /* date should be year/month/day-hour:minutes:seconds */
1415 p = strchr( psz_value, '-' );
1418 psz_date = psz_value;
1425 psz_time = psz_value;
1429 switch( sscanf( psz_time, "%d:%d:%d", &i, &j, &k ) )
1448 switch( sscanf( psz_date, "%d/%d/%d", &i, &j, &k ) )
1467 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1468 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1469 schedule->i_period = ((mtime_t) date) * 1000000;
1471 #endif /* UNDER_CE */
1472 else if( !strcmp( psz_cmd, "repeat" ) )
1476 if( sscanf( psz_value, "%d", &i ) == 1 )
1478 schedule->i_repeat = i;
1485 else if( !strcmp( psz_cmd, "append" ) )
1487 char *command = strdup( psz_value );
1489 TAB_APPEND( schedule->i_command, schedule->command, command );
1498 /*****************************************************************************
1499 * Message handling functions
1500 *****************************************************************************/
1501 static vlm_message_t *vlm_MessageNew( char *psz_name,
1502 const char *psz_format, ... )
1504 vlm_message_t *p_message;
1507 if( !psz_name ) return 0;
1509 p_message = malloc( sizeof(vlm_message_t) );
1515 p_message->psz_value = 0;
1519 va_start( args, psz_format );
1520 if( vasprintf( &p_message->psz_value, psz_format, args ) < 0 )
1529 p_message->psz_name = strdup( psz_name );
1530 p_message->i_child = 0;
1531 p_message->child = NULL;
1536 void vlm_MessageDelete( vlm_message_t *p_message )
1538 if( p_message->psz_name ) free( p_message->psz_name );
1539 if( p_message->psz_value ) free( p_message->psz_value );
1540 while( p_message->i_child-- )
1541 vlm_MessageDelete( p_message->child[p_message->i_child] );
1542 if( p_message->child ) free( p_message->child );
1547 static vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1548 vlm_message_t *p_child )
1550 if( p_message == NULL ) return NULL;
1554 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1560 /*****************************************************************************
1561 * Misc utility functions
1562 *****************************************************************************/
1563 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_t *media,
1564 vlm_schedule_t *schedule, char *psz_filter )
1570 vlm_message_t *msg_media;
1571 vlm_message_t *msg_child;
1573 msg = vlm_MessageNew( "show", NULL );
1574 msg_media = vlm_MessageAdd( msg, vlm_MessageNew( media->psz_name, 0 ));
1576 vlm_MessageAdd( msg_media,
1577 vlm_MessageNew( "type", media->i_type == VOD_TYPE ?
1578 "vod" : "broadcast" ) );
1579 vlm_MessageAdd( msg_media,
1580 vlm_MessageNew( "enabled", media->b_enabled ?
1583 vlm_MessageAdd( msg_media,
1584 vlm_MessageNew( "loop", media->b_loop ?
1587 if( media->i_type == VOD_TYPE && media->psz_mux )
1588 vlm_MessageAdd( msg_media,
1589 vlm_MessageNew( "mux", media->psz_mux ) );
1591 msg_child = vlm_MessageAdd( msg_media,
1592 vlm_MessageNew( "inputs", NULL ) );
1594 for( i = 0; i < media->i_input; i++ )
1596 vlm_MessageAdd( msg_child,
1597 vlm_MessageNew( media->input[i], NULL ) );
1600 vlm_MessageAdd( msg_media,
1601 vlm_MessageNew( "output", media->psz_output ?
1602 media->psz_output : "" ) );
1604 msg_child = vlm_MessageAdd( msg_media, vlm_MessageNew( "options", 0 ));
1606 for( i = 0; i < media->i_option; i++ )
1608 vlm_MessageAdd( msg_child, vlm_MessageNew( media->option[i], 0 ) );
1611 msg_child = vlm_MessageAdd( msg_media,
1612 vlm_MessageNew( "instances", NULL ) );
1614 for( i = 0; i < media->i_instance; i++ )
1616 vlm_media_instance_t *p_instance = media->instance[i];
1619 if( !p_instance->p_input ) val.i_int = END_S;
1620 else var_Get( p_instance->p_input, "state", &val );
1622 vlm_MessageAdd( msg_child,
1623 vlm_MessageNew( p_instance->psz_name ?
1624 p_instance->psz_name : "default",
1625 val.i_int == PLAYING_S ? "playing" :
1626 val.i_int == PAUSE_S ? "paused" :
1634 else if( schedule != NULL )
1638 vlm_message_t *msg_schedule;
1639 vlm_message_t *msg_child;
1642 msg = vlm_MessageNew( "show", NULL );
1644 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, 0 ) );
1646 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1648 vlm_MessageAdd( msg_schedule,
1649 vlm_MessageNew( "enabled", schedule->b_enabled ?
1652 #if !defined( UNDER_CE )
1653 if( schedule->i_date != 0 )
1656 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1659 #ifdef HAVE_LOCALTIME_R
1660 localtime_r( &i_time, &date);
1662 struct tm *p_date = localtime( &i_time );
1666 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1667 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1668 date.tm_hour, date.tm_min, date.tm_sec );
1670 vlm_MessageAdd( msg_schedule,
1671 vlm_MessageNew( "date", psz_date ) );
1676 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1679 if( schedule->i_period != 0 )
1681 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1684 date.tm_sec = (int)( i_time % 60 );
1685 i_time = i_time / 60;
1686 date.tm_min = (int)( i_time % 60 );
1687 i_time = i_time / 60;
1688 date.tm_hour = (int)( i_time % 24 );
1689 i_time = i_time / 24;
1690 date.tm_mday = (int)( i_time % 30 );
1691 i_time = i_time / 30;
1692 /* okay, okay, months are not always 30 days long */
1693 date.tm_mon = (int)( i_time % 12 );
1694 i_time = i_time / 12;
1695 date.tm_year = (int)i_time;
1697 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1698 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1700 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1704 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1706 #endif /* UNDER_CE */
1708 sprintf( buffer, "%d", schedule->i_repeat );
1709 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1712 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", 0) );
1714 for( i = 0; i < schedule->i_command; i++ )
1716 vlm_MessageAdd( msg_child,
1717 vlm_MessageNew( schedule->command[i], NULL ) );
1724 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1728 vlm_message_t *msg_child;
1730 msg = vlm_MessageNew( "show", NULL );
1731 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "media", NULL ) );
1733 for( i = 0; i < vlm->i_media; i++ )
1735 vlm_media_t *m = vlm->media[i];
1736 vlm_message_t *msg_media, *msg_instance;
1738 msg_media = vlm_MessageAdd( msg_child,
1739 vlm_MessageNew( m->psz_name, 0 ) );
1741 vlm_MessageAdd( msg_media,
1742 vlm_MessageNew( "type", m->i_type == VOD_TYPE ?
1743 "vod" : "broadcast" ) );
1745 vlm_MessageAdd( msg_media,
1746 vlm_MessageNew( "enabled", m->b_enabled ?
1749 if( m->i_type == VOD_TYPE && m->psz_mux )
1750 vlm_MessageAdd( msg_media,
1751 vlm_MessageNew( "mux", m->psz_mux ) );
1753 msg_instance = vlm_MessageAdd( msg_media,
1754 vlm_MessageNew( "instances", 0 ) );
1756 for( j = 0; j < m->i_instance; j++ )
1758 vlm_media_instance_t *p_instance = m->instance[j];
1761 if( !p_instance->p_input ) val.i_int = END_S;
1762 else var_Get( p_instance->p_input, "state", &val );
1764 vlm_MessageAdd( msg_instance,
1765 vlm_MessageNew( p_instance->psz_name ?
1766 p_instance->psz_name : "default",
1767 val.i_int == PLAYING_S ? "playing" :
1768 val.i_int == PAUSE_S ? "paused" :
1776 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1780 vlm_message_t *msg_child;
1782 msg = vlm_MessageNew( "show", NULL );
1783 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", NULL ) );
1785 for( i = 0; i < vlm->i_schedule; i++ )
1787 vlm_schedule_t *s = vlm->schedule[i];
1788 vlm_message_t *msg_schedule;
1789 mtime_t i_time, i_next_date;
1791 msg_schedule = vlm_MessageAdd( msg_child,
1792 vlm_MessageNew( s->psz_name, 0 ) );
1793 vlm_MessageAdd( msg_schedule,
1794 vlm_MessageNew( "enabled", s->b_enabled ?
1797 if( !s->b_enabled ) return msg;
1800 vlm_MessageAdd( msg_schedule,
1801 vlm_MessageNew( "enabled", "yes" ) );
1803 /* calculate next date */
1805 i_next_date = s->i_date;
1807 if( s->i_period != 0 )
1810 while( s->i_date + j * s->i_period <= i_time &&
1816 i_next_date = s->i_date + j * s->i_period;
1819 if( i_next_date > i_time )
1821 time_t i_date = (time_t) (i_next_date / 1000000) ;
1823 #if !defined( UNDER_CE )
1826 ctime_r( &i_date, psz_date );
1828 char *psz_date = ctime( &i_date );
1831 vlm_MessageAdd( msg_schedule,
1832 vlm_MessageNew( "next launch", psz_date ) );
1840 else if( psz_filter == NULL && media == NULL && schedule == NULL )
1842 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1843 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1845 vlm_MessageAdd( show1, show2->child[0] );
1847 /* We must destroy the parent node "show" of show2
1848 * and not the children */
1849 free( show2->psz_name );
1857 return vlm_MessageNew( "show", NULL );
1861 static vlm_message_t *vlm_Help( vlm_t *vlm, char *psz_filter )
1863 vlm_message_t *message, *message_child;
1865 #define MessageAdd( a ) \
1866 vlm_MessageAdd( message, vlm_MessageNew( a, NULL ) );
1867 #define MessageAddChild( a ) \
1868 vlm_MessageAdd( message_child, vlm_MessageNew( a, NULL ) );
1870 if( psz_filter == NULL )
1872 message = vlm_MessageNew( "help", NULL );
1874 message_child = MessageAdd( "Commands Syntax:" );
1875 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
1876 MessageAddChild( "setup (name) (properties)" );
1877 MessageAddChild( "show [(name)|media|schedule]" );
1878 MessageAddChild( "del (name)|all|media|schedule" );
1879 MessageAddChild( "control (name) [instance_name] (command)" );
1880 MessageAddChild( "save (config_file)" );
1881 MessageAddChild( "load (config_file)" );
1883 message_child = MessageAdd( "Media Proprieties Syntax:" );
1884 MessageAddChild( "input (input_name)" );
1885 MessageAddChild( "inputdel (input_name)|all" );
1886 MessageAddChild( "output (output_name)" );
1887 MessageAddChild( "enabled|disabled" );
1888 MessageAddChild( "loop|unloop (broadcast only)" );
1889 MessageAddChild( "mux (mux_name)" );
1891 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
1892 MessageAddChild( "enabled|disabled" );
1893 MessageAddChild( "append (command_until_rest_of_the_line)" );
1894 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
1896 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
1897 "(days)-(hours):(minutes):(seconds)" );
1898 MessageAddChild( "repeat (number_of_repetitions)" );
1900 message_child = MessageAdd( "Control Commands Syntax:" );
1901 MessageAddChild( "play" );
1902 MessageAddChild( "pause" );
1903 MessageAddChild( "stop" );
1904 MessageAddChild( "seek (percentage)" );
1909 return vlm_MessageNew( "help", NULL );
1912 /*****************************************************************************
1913 * Config handling functions
1914 *****************************************************************************/
1915 static int Load( vlm_t *vlm, char *file )
1919 while( *pf != '\0' )
1921 vlm_message_t *message = NULL;
1925 while( pf[i_temp] != '\n' && pf[i_temp] != '\0' && pf[i_temp] != '\r' )
1930 if( pf[i_temp] == '\r' || pf[i_temp] == '\n' )
1933 i_next = i_temp + 1;
1940 if( ExecuteCommand( vlm, pf, &message ) )
1953 static char *Save( vlm_t *vlm )
1960 for( i = 0; i < vlm->i_media; i++ )
1962 vlm_media_t *media = vlm->media[i];
1964 if( media->i_type == VOD_TYPE )
1966 i_length += strlen( "new vod " ) + strlen(media->psz_name);
1970 i_length += strlen( "new broadcast " ) + strlen(media->psz_name);
1973 if( media->b_enabled == VLC_TRUE )
1975 i_length += strlen( "enabled" );
1979 i_length += strlen( "disabled" );
1982 if( media->b_loop == VLC_TRUE )
1984 i_length += strlen( " loop\n" );
1988 i_length += strlen( "\n" );
1991 for( j = 0; j < media->i_input; j++ )
1993 i_length += strlen( "setup input \"\"\n" ) +
1994 strlen( media->psz_name ) + strlen( media->input[j] );
1997 if( media->psz_output != NULL )
1999 i_length += strlen(media->psz_name) + strlen(media->psz_output) +
2000 strlen( "setup output \n" );
2003 for( j=0 ; j < media->i_option ; j++ )
2005 i_length += strlen(media->psz_name) + strlen(media->option[j]) +
2006 strlen("setup option \n");
2010 for( i = 0; i < vlm->i_schedule; i++ )
2012 vlm_schedule_t *schedule = vlm->schedule[i];
2014 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
2016 if( schedule->b_enabled == VLC_TRUE )
2018 i_length += strlen( "date //-:: enabled\n" ) + 14;
2022 i_length += strlen( "date //-:: disabled\n" ) + 14;
2026 if( schedule->i_period != 0 )
2028 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
2029 strlen( "period //-::\n" ) + 14;
2032 if( schedule->i_repeat >= 0 )
2036 sprintf( buffer, "%d", schedule->i_repeat );
2037 i_length += strlen( "setup repeat \n" ) +
2038 strlen( schedule->psz_name ) + strlen( buffer );
2045 for( j = 0; j < schedule->i_command; j++ )
2047 i_length += strlen( "setup append \n" ) +
2048 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
2053 /* Don't forget the '\0' */
2055 /* now we have the length of save */
2057 p = save = malloc( i_length );
2060 /* finally we can write in it */
2061 for( i = 0; i < vlm->i_media; i++ )
2063 vlm_media_t *media = vlm->media[i];
2065 if( media->i_type == VOD_TYPE )
2067 p += sprintf( p, "new %s vod ", media->psz_name);
2071 p += sprintf( p, "new %s broadcast ", media->psz_name);
2074 if( media->b_enabled == VLC_TRUE )
2076 p += sprintf( p, "enabled" );
2080 p += sprintf( p, "disabled" );
2083 if( media->b_loop == VLC_TRUE )
2085 p += sprintf( p, " loop\n" );
2089 p += sprintf( p, "\n" );
2092 for( j = 0; j < media->i_input; j++ )
2094 p += sprintf( p, "setup %s input \"%s\"\n", media->psz_name,
2098 if( media->psz_output != NULL )
2100 p += sprintf( p, "setup %s output %s\n", media->psz_name,
2101 media->psz_output );
2104 for( j = 0; j < media->i_option; j++ )
2106 p += sprintf( p, "setup %s option %s\n", media->psz_name,
2111 /* and now, the schedule scripts */
2112 #if !defined( UNDER_CE )
2113 for( i = 0; i < vlm->i_schedule; i++ )
2115 vlm_schedule_t *schedule = vlm->schedule[i];
2117 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
2119 #ifdef HAVE_LOCALTIME_R
2120 localtime_r( &i_time, &date);
2122 struct tm *p_date = localtime( &i_time );
2126 p += sprintf( p, "new %s schedule ", schedule->psz_name);
2128 if( schedule->b_enabled == VLC_TRUE )
2130 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
2131 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2132 date.tm_hour, date.tm_min, date.tm_sec );
2136 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
2137 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2138 date.tm_hour, date.tm_min, date.tm_sec);
2141 if( schedule->i_period != 0 )
2143 p += sprintf( p, "setup %s ", schedule->psz_name );
2145 i_time = (time_t) ( schedule->i_period / 1000000 );
2147 date.tm_sec = (int)( i_time % 60 );
2148 i_time = i_time / 60;
2149 date.tm_min = (int)( i_time % 60 );
2150 i_time = i_time / 60;
2151 date.tm_hour = (int)( i_time % 24 );
2152 i_time = i_time / 24;
2153 date.tm_mday = (int)( i_time % 30 );
2154 i_time = i_time / 30;
2155 /* okay, okay, months are not always 30 days long */
2156 date.tm_mon = (int)( i_time % 12 );
2157 i_time = i_time / 12;
2158 date.tm_year = (int)i_time;
2160 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
2161 date.tm_year, date.tm_mon, date.tm_mday,
2162 date.tm_hour, date.tm_min, date.tm_sec);
2165 if( schedule->i_repeat >= 0 )
2167 p += sprintf( p, "setup %s repeat %d\n",
2168 schedule->psz_name, schedule->i_repeat );
2172 p += sprintf( p, "\n" );
2175 for( j = 0; j < schedule->i_command; j++ )
2177 p += sprintf( p, "setup %s append %s\n",
2178 schedule->psz_name, schedule->command[j] );
2182 #endif /* UNDER_CE */
2187 /*****************************************************************************
2189 *****************************************************************************/
2190 int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
2191 char *psz_id, int i_query, va_list args )
2193 vlm_t *vlm = (vlm_t *)p_private;
2194 int i, i_ret = VLC_EGENERIC;
2196 if( !p_private || !p_vod_media ) return VLC_EGENERIC;
2198 vlc_mutex_lock( &vlm->lock );
2201 for( i = 0; i < vlm->i_media; i++ )
2203 if( p_vod_media == vlm->media[i]->vod_media ) break;
2206 if( i == vlm->i_media )
2208 vlc_mutex_unlock( &vlm->lock );
2209 return VLC_EGENERIC;
2214 case VOD_MEDIA_PLAY:
2215 vlm->media[i]->psz_vod_output = (char *)va_arg( args, char * );
2216 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "play", 0 );
2217 vlm->media[i]->psz_vod_output = 0;
2220 case VOD_MEDIA_PAUSE:
2221 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "pause", 0 );
2224 case VOD_MEDIA_STOP:
2225 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "stop", 0 );
2228 case VOD_MEDIA_SEEK:
2230 double f_pos = (double)va_arg( args, double );
2233 sprintf( psz_pos, "%f", f_pos );
2234 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "seek", psz_pos);
2242 vlc_mutex_unlock( &vlm->lock );
2247 /*****************************************************************************
2249 *****************************************************************************/
2250 static int Manage( vlc_object_t* p_object )
2252 vlm_t *vlm = (vlm_t*)p_object;
2254 mtime_t i_lastcheck;
2257 i_lastcheck = mdate();
2261 while( !vlm->b_die )
2263 vlc_mutex_lock( &vlm->lock );
2265 /* destroy the inputs that wants to die, and launch the next input */
2266 for( i = 0; i < vlm->i_media; i++ )
2268 vlm_media_t *p_media = vlm->media[i];
2270 for( j = 0; j < p_media->i_instance; j++ )
2272 vlm_media_instance_t *p_instance = p_media->instance[j];
2274 if( !p_instance->p_input ||
2275 ( !p_instance->p_input->b_eof &&
2276 !p_instance->p_input->b_error ) ) continue;
2278 input_StopThread( p_instance->p_input );
2279 input_DestroyThread( p_instance->p_input );
2280 vlc_object_detach( p_instance->p_input );
2281 vlc_object_destroy( p_instance->p_input );
2283 p_instance->i_index++;
2284 if( p_instance->i_index == p_media->i_input &&
2285 p_media->b_loop ) p_instance->i_index = 0;
2287 if( p_instance->i_index < p_media->i_input )
2289 /* FIXME, find a way to select the right instance */
2291 sprintf( buffer, "%d", p_instance->i_index );
2292 vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2297 if( vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2298 "stop", 0 ) == VLC_SUCCESS ) i--;
2306 for( i = 0; i < vlm->i_schedule; i++ )
2308 mtime_t i_real_date = vlm->schedule[i]->i_date;
2310 if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2312 if( vlm->schedule[i]->i_date == 0 ) // now !
2314 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2315 i_real_date = i_time;
2317 else if( vlm->schedule[i]->i_period != 0 )
2320 while( vlm->schedule[i]->i_date + j *
2321 vlm->schedule[i]->i_period <= i_lastcheck &&
2322 ( vlm->schedule[i]->i_repeat > j ||
2323 vlm->schedule[i]->i_repeat == -1 ) )
2328 i_real_date = vlm->schedule[i]->i_date + j *
2329 vlm->schedule[i]->i_period;
2332 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2334 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2336 vlm_message_t *message = NULL;
2338 ExecuteCommand( vlm, vlm->schedule[i]->command[j],
2341 /* for now, drop the message */
2348 i_lastcheck = i_time;
2350 vlc_mutex_unlock( &vlm->lock );
2358 #else /* ENABLE_VLM */
2360 /* We just define an empty wrapper */
2361 vlm_t *__vlm_New( vlc_object_t *a )
2363 msg_Err( a, "VideoLAN manager support is disabled" );
2366 void vlm_Delete( vlm_t *a ){}
2367 int vlm_ExecuteCommand( vlm_t *a, char *b, vlm_message_t **c ){ return -1; }
2368 void vlm_MessageDelete( vlm_message_t *a ){}
2369 vlm_media_t *vlm_MediaNew( vlm_t *a, char *b, int c ){ return NULL; }
2370 void vlm_MediaDelete( vlm_t *a, vlm_media_t *b, char *c ){}
2371 int vlm_MediaSetup( vlm_t *a, vlm_media_t *b, char *c, char *d ){ return -1; }
2372 int vlm_MediaControl( vlm_t *a, vlm_media_t *b, char *c, char *d, char *e )
2374 vlm_schedule_t * vlm_ScheduleNew( vlm_t *a, char *b ){ return NULL; }
2375 void vlm_ScheduleDelete( vlm_t *a, vlm_schedule_t *b, char *c ){}
2376 int vlm_ScheduleSetup( vlm_schedule_t *a, char *b, char *c ){ return -1; }
2377 int vlm_MediaVodControl( void *a, vod_media_t *b, char *c, int d, va_list e )
2379 int vlm_Save( vlm_t *a, char *b ){ return -1; }
2380 int vlm_Load( vlm_t *a, char *b ){ return -1; }
2382 #endif /* ENABLE_VLM */