1 /*****************************************************************************
2 * vlm.c: VLM interface plugin
3 *****************************************************************************
4 * Copyright (C) 2000-2005 the VideoLAN team
7 * Authors: Simon Latapie <garf@videolan.org>
8 * Laurent Aimar <fenrir@videolan.org>
9 * Gildas Bazin <gbazin@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
30 #include <ctype.h> /* tolower() */
37 #include <vlc/input.h>
40 # include <time.h> /* ctime() */
41 # include <sys/timeb.h> /* ftime() */
49 if( p ) { free( p ); (p) = NULL; }
51 /*****************************************************************************
53 *****************************************************************************/
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_instance_t *vlm_MediaInstanceSearch( vlm_t *, vlm_media_t *, const char * );
59 static vlm_message_t *vlm_MessageNew( char *, const char *, ... );
60 static vlm_message_t *vlm_MessageAdd( vlm_message_t *, vlm_message_t * );
62 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *, const char * );
64 static char *Save( vlm_t * );
65 static int Load( vlm_t *, char * );
66 static int ExecuteCommand( vlm_t *, const char *, vlm_message_t ** );
67 static int Manage( vlc_object_t * );
69 /*****************************************************************************
71 *****************************************************************************/
72 vlm_t *__vlm_New ( vlc_object_t *p_this )
78 /* to be sure to avoid multiple creation */
79 var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX );
80 var_Get( p_this->p_libvlc, "vlm_mutex", &lockval );
81 vlc_mutex_lock( lockval.p_address );
83 if( !(p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE )) )
85 msg_Info( p_this, "creating VLM" );
86 if( ( p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM ) ) == NULL )
88 vlc_mutex_unlock( lockval.p_address );
92 vlc_mutex_init( p_this->p_vlc, &p_vlm->lock );
96 p_vlm->i_schedule = 0;
97 p_vlm->schedule = NULL;
99 vlc_object_yield( p_vlm );
100 vlc_object_attach( p_vlm, p_this->p_vlc );
102 vlc_mutex_unlock( lockval.p_address );
104 if( vlc_thread_create( p_vlm, "vlm thread",
105 Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
107 vlc_mutex_destroy( &p_vlm->lock );
108 vlc_object_destroy( p_vlm );
112 /* Try loading the vlm conf file given by --vlm-conf */
113 psz_vlmconf = config_GetPsz( p_vlm, "vlm-conf" );
115 if( psz_vlmconf && *psz_vlmconf )
117 vlm_message_t *p_message = NULL;
118 char *psz_buffer = NULL;
120 msg_Dbg( p_this, "loading VLM configuration" );
121 asprintf(&psz_buffer, "load %s", psz_vlmconf );
124 msg_Dbg( p_this, psz_buffer);
125 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) ){
126 msg_Warn( p_this, "error while loading the configuration file" );
128 vlm_MessageDelete(p_message);
137 /*****************************************************************************
139 *****************************************************************************/
140 void vlm_Delete( vlm_t *p_vlm )
144 var_Get( p_vlm->p_libvlc, "vlm_mutex", &lockval );
145 vlc_mutex_lock( lockval.p_address );
147 vlc_object_release( p_vlm );
149 if( p_vlm->i_refcount > 0 )
151 vlc_mutex_unlock( lockval.p_address );
155 p_vlm->b_die = VLC_TRUE;
156 vlc_thread_join( p_vlm );
158 vlc_mutex_destroy( &p_vlm->lock );
160 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0], NULL );
161 FREE( p_vlm->media );
163 while( p_vlm->i_schedule ) vlm_ScheduleDelete( p_vlm,
164 p_vlm->schedule[0], NULL );
165 FREE( p_vlm->schedule );
167 vlc_object_detach( p_vlm );
168 vlc_object_destroy( p_vlm );
169 vlc_mutex_unlock( lockval.p_address );
172 /*****************************************************************************
173 * vlm_ExecuteCommand:
174 *****************************************************************************/
175 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
176 vlm_message_t **pp_message)
180 vlc_mutex_lock( &p_vlm->lock );
181 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
182 vlc_mutex_unlock( &p_vlm->lock );
187 /*****************************************************************************
189 *****************************************************************************/
190 int vlm_Save( vlm_t *p_vlm, const char *psz_file )
195 if( !p_vlm || !psz_file ) return 1;
197 file = utf8_fopen( psz_file, "wt" );
198 if( file == NULL ) return 1;
200 psz_save = Save( p_vlm );
201 if( psz_save == NULL )
206 fwrite( psz_save, strlen( psz_save ), 1, file );
213 /*****************************************************************************
215 *****************************************************************************/
216 int vlm_Load( vlm_t *p_vlm, const char *psz_file )
222 if( !p_vlm || !psz_file ) return 1;
224 p_stream = stream_UrlNew( p_vlm, psz_file );
225 if( p_stream == NULL ) return 1;
227 if( stream_Seek( p_stream, 0 ) != 0 )
229 stream_Delete( p_stream );
233 i_size = stream_Size( p_stream );
235 psz_buffer = malloc( i_size + 1 );
238 stream_Delete( p_stream );
242 stream_Read( p_stream, psz_buffer, i_size );
243 psz_buffer[ i_size ] = '\0';
245 stream_Delete( p_stream );
247 if( Load( p_vlm, psz_buffer ) )
258 /*****************************************************************************
260 *****************************************************************************/
261 static const char *FindEndCommand( const char *psz_sent )
263 vlc_bool_t b_escape = VLC_FALSE;
269 while( ( *psz_sent != '\"' || b_escape == VLC_TRUE )
270 && ( *psz_sent != '\0' ) )
272 if( *psz_sent == '\'' && b_escape == VLC_FALSE )
274 psz_sent = FindEndCommand( psz_sent );
275 if( psz_sent == NULL ) return NULL;
277 else if( *psz_sent++ == '\\' && b_escape == VLC_FALSE )
280 b_escape = VLC_FALSE;
283 if( *psz_sent == '\"' )
289 /* *psz_sent == '\0' -> number of " is incorrect */
296 while( ( *psz_sent != '\'' || b_escape == VLC_TRUE )
297 && ( *psz_sent != '\0' ) )
299 if( *psz_sent == '\"' && b_escape == VLC_FALSE )
301 psz_sent = FindEndCommand( psz_sent );
302 if( psz_sent == NULL ) return NULL;
304 else if( *psz_sent++ == '\\' && b_escape == VLC_FALSE )
307 b_escape = VLC_FALSE;
310 if( *psz_sent == '\'' )
316 /* *psz_sent == '\0' -> number of " is incorrect */
321 default: /* now we can look for spaces */
322 while( ( *psz_sent != ' ' ) && ( *psz_sent != '\0' ) )
324 if( ( ( *psz_sent == '\'' ) || ( *psz_sent == '\"' ) )
325 && b_escape == VLC_FALSE )
327 psz_sent = FindEndCommand( psz_sent );
328 if( psz_sent == NULL ) return NULL;
330 else if( *psz_sent++ == '\\' && b_escape == VLC_FALSE )
333 b_escape = VLC_FALSE;
341 /*****************************************************************************
342 * ExecuteCommand: The main state machine
343 *****************************************************************************
344 * Execute a command which ends with '\0' (string)
345 *****************************************************************************/
346 static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
347 vlm_message_t **pp_message )
350 char **ppsz_command = NULL;
351 const char *psz_cmd = psz_command;
352 vlm_message_t *p_message = NULL;
355 /* First, parse the line and cut it */
356 while( *psz_cmd != '\0' )
359 if( *psz_cmd == ' ' || *psz_cmd == '\t' )
365 const char *psz_temp;
368 vlc_bool_t b_escape = VLC_FALSE;
371 /* support for comments */
372 if( i_command == 0 && *psz_cmd == '#')
374 p_message = vlm_MessageNew( "", NULL );
378 psz_temp = FindEndCommand( psz_cmd );
380 if( psz_temp == NULL )
382 p_message = vlm_MessageNew( "Incomplete command", psz_cmd );
386 i_temp = psz_temp - psz_cmd;
388 ppsz_command = realloc( ppsz_command, (i_command + 1) *
390 ppsz_command[ i_command ] = malloc( (i_temp + 1) * sizeof(char) );
392 /* unescape ", ' and \ ... and everything else */
394 psz_dst = ppsz_command[ i_command ];
397 if( *psz_buf == '\\' && b_escape == VLC_FALSE )
401 b_escape = VLC_FALSE;
416 * And then Interpret it
421 p_message = vlm_MessageNew( "", NULL );
425 if( !strcmp(ppsz_command[0], "new") )
429 /* Check the number of arguments */
430 if( i_command < 3 ) goto syntax_error;
433 if( !strcmp(ppsz_command[2], "vod") )
437 else if( !strcmp(ppsz_command[2], "broadcast") )
439 i_type = BROADCAST_TYPE;
441 else if( !strcmp(ppsz_command[2], "schedule") )
443 i_type = SCHEDULE_TYPE;
448 vlm_MessageNew( "new", "%s: Choose between vod, "
449 "broadcast or schedule", ppsz_command[1] );
453 /* Check for forbidden media names */
454 if( !strcmp(ppsz_command[1], "all") ||
455 !strcmp(ppsz_command[1], "media") ||
456 !strcmp(ppsz_command[1], "schedule") )
458 p_message = vlm_MessageNew( "new", "\"all\", \"media\" and "
459 "\"schedule\" are reserved names" );
463 /* Check the name is not already in use */
464 if( vlm_ScheduleSearch( p_vlm, ppsz_command[1] ) ||
465 vlm_MediaSearch( p_vlm, ppsz_command[1] ) )
467 p_message = vlm_MessageNew( "new", "%s: Name already in use",
473 if( i_type == SCHEDULE_TYPE )
475 vlm_schedule_t *p_schedule;
476 p_schedule = vlm_ScheduleNew( p_vlm, ppsz_command[1] );
479 p_message = vlm_MessageNew( "new", "could not create schedule" );
487 vlm_media_t *p_media;
488 p_media = vlm_MediaNew( p_vlm, ppsz_command[1], i_type );
491 p_message = vlm_MessageNew( "new", "could not create media" );
498 p_message = vlm_MessageNew( "new", NULL );
502 /* Properties will be dealt with later on */
505 else if( !strcmp(ppsz_command[0], "setup") )
507 if( i_command < 2 ) goto syntax_error;
509 /* Properties will be dealt with later on */
512 else if( !strcmp(ppsz_command[0], "del") )
514 vlm_media_t *p_media;
515 vlm_schedule_t *p_schedule;
517 if( i_command < 2 ) goto syntax_error;
519 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
520 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
522 if( p_schedule != NULL )
524 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
526 else if( p_media != NULL )
528 vlm_MediaDelete( p_vlm, p_media, NULL );
530 else if( !strcmp(ppsz_command[1], "media") )
532 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
535 else if( !strcmp(ppsz_command[1], "schedule") )
537 while( p_vlm->i_schedule )
538 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
540 else if( !strcmp(ppsz_command[1], "all") )
542 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
545 while( p_vlm->i_schedule )
546 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
550 p_message = vlm_MessageNew( "del", "%s: media unknown",
555 p_message = vlm_MessageNew( "del", NULL );
559 else if( !strcmp(ppsz_command[0], "show") )
561 vlm_media_t *p_media;
562 vlm_schedule_t *p_schedule;
566 p_message = vlm_Show( p_vlm, NULL, NULL, NULL );
569 else if( i_command > 2 ) goto syntax_error;
571 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
572 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
574 if( p_schedule != NULL )
576 p_message = vlm_Show( p_vlm, NULL, p_schedule, NULL );
578 else if( p_media != NULL )
580 p_message = vlm_Show( p_vlm, p_media, NULL, NULL );
584 p_message = vlm_Show( p_vlm, NULL, NULL, ppsz_command[1] );
590 else if( !strcmp(ppsz_command[0], "help") )
592 if( i_command != 1 ) goto syntax_error;
594 p_message = vlm_Help( p_vlm, NULL );
598 else if( !strcmp(ppsz_command[0], "control") )
600 vlm_media_t *p_media;
602 if( i_command < 3 ) goto syntax_error;
604 if( !(p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] ) ) )
606 p_message = vlm_MessageNew( "control", "%s: media unknown",
612 char *psz_command, *psz_arg = 0, *psz_instance = 0;
615 if( strcmp( ppsz_command[2], "play" ) &&
616 strcmp( ppsz_command[2], "stop" ) &&
617 strcmp( ppsz_command[2], "pause" ) &&
618 strcmp( ppsz_command[2], "seek" ) )
621 psz_instance = ppsz_command[2];
623 if( i_command < 4 ) goto syntax_error;
626 psz_command = ppsz_command[i_index];
628 if( i_command >= i_index + 2 ) psz_arg = ppsz_command[i_index + 1];
630 vlm_MediaControl( p_vlm, p_media, psz_instance, psz_command,
632 p_message = vlm_MessageNew( "control", NULL );
637 else if( !strcmp(ppsz_command[0], "save") )
639 if( i_command != 2 ) goto syntax_error;
641 if( vlm_Save( p_vlm, ppsz_command[1] ) )
643 p_message = vlm_MessageNew( "save", "Unable to save to file" );
648 p_message = vlm_MessageNew( "save", NULL );
653 else if( !strcmp(ppsz_command[0], "export" ) )
657 if( i_command != 1 ) goto syntax_error;
659 p_message = vlm_MessageNew( "export", psz_buf = Save( p_vlm ) );
665 else if( !strcmp(ppsz_command[0], "load") )
667 if( i_command != 2 ) goto syntax_error;
669 switch( vlm_Load( p_vlm, ppsz_command[1] ) )
672 p_message = vlm_MessageNew( "load", NULL );
675 p_message = vlm_MessageNew( "load", "Read file error" );
679 vlm_MessageNew( "load", "Error while loading file" );
683 vlm_MessageNew( "load", "Unable to load from file" );
690 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
694 /* Common code between "new" and "setup" */
695 if( !strcmp(ppsz_command[0], "new") ||
696 !strcmp(ppsz_command[0], "setup") )
698 int i_command_start = strcmp(ppsz_command[0], "new") ? 2 : 3;
699 vlm_media_t *p_media;
700 vlm_schedule_t *p_schedule;
702 if( i_command < i_command_start ) goto syntax_error;
704 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
705 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
707 if( !p_media && !p_schedule )
709 p_message = vlm_MessageNew( ppsz_command[0], "%s unknown",
714 if( p_schedule != 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_ScheduleSetup( p_schedule, ppsz_command[i], NULL );
724 /* Beware: everything behind append is considered as
726 else if( !strcmp( ppsz_command[i], "append" ) )
728 if( ++i >= i_command ) break;
730 for( j = i + 1; j < i_command; j++ )
733 realloc( ppsz_command[i], strlen(ppsz_command[i]) +
734 strlen(ppsz_command[j]) + 1 + 1 );
735 strcat( ppsz_command[i], " " );
736 strcat( ppsz_command[i], ppsz_command[j] );
739 vlm_ScheduleSetup( p_schedule, ppsz_command[i - 1],
745 if( i + 1 >= i_command && !strcmp(ppsz_command[0], "new") )
747 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
749 vlm_MessageNew( ppsz_command[0],
750 "Wrong properties syntax" );
753 else if( i + 1 >= i_command )
756 vlm_MessageNew( ppsz_command[0],
757 "Wrong properties syntax" );
761 vlm_ScheduleSetup( p_schedule, ppsz_command[i],
768 else if( p_media != NULL )
770 for( i = i_command_start ; i < i_command ; i++ )
772 if( !strcmp( ppsz_command[i], "enabled" ) ||
773 !strcmp( ppsz_command[i], "disabled" ) )
775 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
777 else if( i + 1 >= i_command &&
778 !strcmp( ppsz_command[i], "mux") )
780 if( p_media->i_type != VOD_TYPE )
782 p_message = vlm_MessageNew( ppsz_command[0],
783 "mux only available for broadcast" );
787 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
792 else if( !strcmp( ppsz_command[i], "loop" ) ||
793 !strcmp( ppsz_command[i], "unloop" ) )
795 if( p_media->i_type != BROADCAST_TYPE )
797 p_message = vlm_MessageNew( ppsz_command[0],
798 "loop only available for broadcast" );
802 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
807 if( ( (i + 1) >= i_command ) &&
808 !strcmp(ppsz_command[0], "new") )
810 vlm_MediaDelete( p_vlm, p_media, NULL );
812 vlm_MessageNew( ppsz_command[0],
813 "Wrong properties syntax" );
816 else if( (i + 1) >= i_command )
819 vlm_MessageNew( ppsz_command[0],
820 "Wrong properties syntax" );
824 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
831 p_message = vlm_MessageNew( ppsz_command[0], NULL );
836 for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
837 FREE( ppsz_command );
838 *pp_message = p_message;
843 p_message = vlm_MessageNew( ppsz_command[0], "Wrong command syntax" );
846 for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
847 FREE( ppsz_command );
848 *pp_message = p_message;
853 vlm_media_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
857 for( i = 0; i < vlm->i_media; i++ )
859 if( strcmp( psz_name, vlm->media[i]->psz_name ) == 0 )
861 return vlm->media[i];
868 /*****************************************************************************
870 *****************************************************************************/
871 static vlm_media_instance_t *
872 vlm_MediaInstanceSearch( vlm_t *vlm, vlm_media_t *media,
873 const char *psz_name )
877 for( i = 0; i < media->i_instance; i++ )
879 if( ( !psz_name && !media->instance[i]->psz_name ) ||
880 ( psz_name && media->instance[i]->psz_name &&
881 !strcmp( psz_name, media->instance[i]->psz_name ) ) )
883 return media->instance[i];
890 vlm_media_t *vlm_MediaNew( vlm_t *vlm, const char *psz_name, int i_type )
892 vlm_media_t *media = malloc( sizeof( vlm_media_t ) );
896 msg_Err( vlm, "out of memory" );
900 /* Check if we need to load the VOD server */
901 if( i_type == VOD_TYPE && !vlm->i_vod )
903 vlm->vod = vlc_object_create( vlm, VLC_OBJECT_VOD );
904 vlc_object_attach( vlm->vod, vlm );
905 vlm->vod->p_module = module_Need( vlm->vod, "vod server", 0, 0 );
906 if( !vlm->vod->p_module )
908 msg_Err( vlm, "cannot find vod server" );
909 vlc_object_detach( vlm->vod );
910 vlc_object_destroy( vlm->vod );
916 vlm->vod->p_data = vlm;
917 vlm->vod->pf_media_control = vlm_MediaVodControl;
920 if( i_type == VOD_TYPE ) vlm->i_vod++;
922 media->psz_name = strdup( psz_name );
923 media->b_enabled = VLC_FALSE;
924 media->b_loop = VLC_FALSE;
925 media->vod_media = NULL;
926 media->psz_vod_output = NULL;
927 media->psz_mux = NULL;
930 media->psz_output = NULL;
932 media->option = NULL;
933 media->i_type = i_type;
934 media->i_instance = 0;
935 media->instance = NULL;
937 vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
939 TAB_APPEND( vlm->i_media, vlm->media, media );
944 /* for now, simple delete. After, del with options (last arg) */
945 void vlm_MediaDelete( vlm_t *vlm, vlm_media_t *media, const char *psz_name )
947 if( media == NULL ) return;
949 while( media->i_instance )
951 vlm_media_instance_t *p_instance = media->instance[0];
952 vlm_MediaControl( vlm, media, p_instance->psz_name, "stop", 0 );
955 TAB_REMOVE( vlm->i_media, vlm->media, media );
957 if( media->i_type == VOD_TYPE )
959 vlm_MediaSetup( vlm, media, "disabled", 0 );
963 /* Check if we need to unload the VOD server */
964 if( media->i_type == VOD_TYPE && !vlm->i_vod )
966 module_Unneed( vlm->vod, vlm->vod->p_module );
967 vlc_object_detach( vlm->vod );
968 vlc_object_destroy( vlm->vod );
972 if( vlm->i_media == 0 && vlm->media ) free( vlm->media );
974 free( media->psz_name );
976 while( media->i_input-- ) free( media->input[media->i_input] );
977 if( media->input ) free( media->input );
979 if( media->psz_output ) free( media->psz_output );
980 if( media->psz_mux ) free( media->psz_mux );
982 while( media->i_option-- ) free( media->option[media->i_option] );
983 if( media->option ) free( media->option );
985 vlc_input_item_Clean( &media->item );
990 int vlm_MediaSetup( vlm_t *vlm, vlm_media_t *media, const char *psz_cmd,
991 const char *psz_value )
993 if( !psz_cmd) return VLC_EGENERIC;
995 if( !strcmp( psz_cmd, "loop" ) )
997 media->b_loop = VLC_TRUE;
999 else if( !strcmp( psz_cmd, "unloop" ) )
1001 media->b_loop = VLC_FALSE;
1003 else if( !strcmp( psz_cmd, "enabled" ) )
1005 media->b_enabled = VLC_TRUE;
1007 else if( !strcmp( psz_cmd, "disabled" ) )
1009 media->b_enabled = VLC_FALSE;
1011 else if( !strcmp( psz_cmd, "mux" ) )
1013 if( media->psz_mux ) free( media->psz_mux );
1014 media->psz_mux = NULL;
1015 if( psz_value ) media->psz_mux = strdup( psz_value );
1017 else if( !strcmp( psz_cmd, "input" ) )
1021 if( psz_value != NULL && strlen(psz_value) > 1 &&
1022 ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
1023 ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
1024 psz_value[ strlen(psz_value) - 1 ] == '\"' ) )
1026 input = malloc( strlen(psz_value) - 1 );
1028 memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
1029 input[ strlen(psz_value) - 2 ] = '\0';
1033 input = strdup( psz_value );
1036 TAB_APPEND( media->i_input, media->input, input );
1038 else if( !strcmp( psz_cmd, "inputdel" ) && !strcmp( psz_value, "all" ) )
1040 while( media->i_input > 0 )
1042 TAB_REMOVE( media->i_input, media->input, media->input[0] );
1045 else if( !strcmp( psz_cmd, "inputdel" ) )
1050 if( psz_value != NULL && strlen(psz_value) > 1 &&
1051 ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
1052 ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
1053 psz_value[ strlen(psz_value) - 1 ] == '\"' ) )
1055 input = malloc( strlen(psz_value) - 1 );
1057 memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
1058 input[ strlen(psz_value) - 2 ] = '\0';
1062 input = strdup( psz_value );
1065 for( i = 0; i < media->i_input; i++ )
1067 if( !strcmp( input, media->input[i] ) )
1069 TAB_REMOVE( media->i_input, media->input, media->input[i] );
1074 else if( !strcmp( psz_cmd, "inputdeln" ) )
1076 int index = atoi( psz_value );
1077 if( index > 0 && index <= media->i_input )
1079 TAB_REMOVE( media->i_input, media->input, media->input[index-1] );
1082 else if( !strcmp( psz_cmd, "output" ) )
1084 if( media->psz_output != NULL )
1086 free( media->psz_output );
1088 media->psz_output = strdup( psz_value );
1090 else if( !strcmp( psz_cmd, "option" ) )
1093 psz_option = strdup( psz_value );
1095 TAB_APPEND( media->i_option, media->option, psz_option );
1099 return VLC_EGENERIC;
1102 /* Check if we need to create/delete a vod media */
1103 if( media->i_type == VOD_TYPE )
1105 if( !media->b_enabled && media->vod_media )
1107 vlm->vod->pf_media_del( vlm->vod, media->vod_media );
1108 media->vod_media = 0;
1110 else if( media->b_enabled && !media->vod_media && media->i_input )
1112 /* Pre-parse the input */
1113 input_thread_t *p_input;
1118 vlc_input_item_Clean( &media->item );
1119 vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
1121 if( media->psz_output )
1122 asprintf( &psz_output, "%s:description", media->psz_output );
1124 asprintf( &psz_output, "#description" );
1126 media->item.psz_uri = strdup( media->input[0] );
1127 media->item.ppsz_options = malloc( sizeof( char* ) );
1128 asprintf( &media->item.ppsz_options[0], "sout=%s", psz_output);
1129 media->item.i_options = 1;
1130 for( i = 0; i < media->i_option; i++ )
1132 media->item.i_options++;
1133 media->item.ppsz_options =
1134 realloc( media->item.ppsz_options,
1135 media->item.i_options * sizeof( char* ) );
1136 media->item.ppsz_options[ media->item.i_options - 1 ] =
1137 strdup( media->option[i] );
1140 asprintf( &psz_header, _("Media: %s"), media->psz_name );
1142 if( (p_input = input_CreateThread2( vlm, &media->item, psz_header
1145 while( !p_input->b_eof && !p_input->b_error ) msleep( 100000 );
1147 input_StopThread( p_input );
1148 input_DestroyThread( p_input );
1149 vlc_object_detach( p_input );
1150 vlc_object_destroy( p_input );
1155 if( media->psz_mux )
1158 es_format_t es, *p_es = &es;
1161 sprintf( fourcc, "%4.4s", media->psz_mux );
1162 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
1163 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
1168 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
1171 vlm->vod->pf_media_new( vlm->vod, media->psz_name, &item );
1176 vlm->vod->pf_media_new( vlm->vod, media->psz_name,
1184 int vlm_MediaControl( vlm_t *vlm, vlm_media_t *media, const char *psz_id,
1185 const char *psz_command, const char *psz_args )
1187 vlm_media_instance_t *p_instance;
1191 p_instance = vlm_MediaInstanceSearch( vlm, media, psz_id );
1193 if( !strcmp( psz_command, "play" ) )
1195 if( !media->b_enabled || media->i_input == 0 ) return 0;
1199 p_instance = malloc( sizeof(vlm_media_instance_t) );
1200 if( !p_instance ) return VLC_EGENERIC;
1201 memset( p_instance, 0, sizeof(vlm_media_instance_t) );
1202 vlc_input_item_Init( VLC_OBJECT(vlm), &p_instance->item );
1203 p_instance->p_input = NULL;
1205 if( ( media->psz_output != NULL ) || ( media->psz_vod_output != NULL ) )
1207 p_instance->item.ppsz_options = malloc( sizeof( char* ) );
1208 asprintf( &p_instance->item.ppsz_options[0], "sout=%s%s%s",
1209 media->psz_output ? media->psz_output : "",
1210 (media->psz_output && media->psz_vod_output) ?
1211 ":" : media->psz_vod_output ? "#" : "",
1212 media->psz_vod_output ? media->psz_vod_output : "" );
1213 p_instance->item.i_options = 1;
1216 for( i = 0; i < media->i_option; i++ )
1218 p_instance->item.i_options++;
1219 p_instance->item.ppsz_options =
1220 realloc( p_instance->item.ppsz_options,
1221 p_instance->item.i_options * sizeof( char* ) );
1222 p_instance->item.ppsz_options[p_instance->item.i_options - 1] =
1223 strdup( media->option[i] );
1226 p_instance->psz_name = psz_id ? strdup( psz_id ) : NULL;
1227 TAB_APPEND( media->i_instance, media->instance, p_instance );
1230 if( ( psz_args && sscanf(psz_args, "%d", &i) == 1 ) && ( i < media->i_input ) )
1232 p_instance->i_index = i;
1235 if( p_instance->item.psz_uri ) free( p_instance->item.psz_uri );
1236 p_instance->item.psz_uri =
1237 strdup( media->input[p_instance->i_index] );
1239 if( p_instance->p_input )
1241 input_StopThread( p_instance->p_input );
1242 input_DestroyThread( p_instance->p_input );
1243 vlc_object_detach( p_instance->p_input );
1244 vlc_object_destroy( p_instance->p_input );
1247 asprintf( &psz_header, _("Media: %s"), media->psz_name );
1248 p_instance->p_input = input_CreateThread2( vlm, &p_instance->item,
1250 if( !p_instance->p_input )
1252 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1253 vlc_input_item_Clean( &p_instance->item );
1254 if( p_instance->psz_name ) free( p_instance->psz_name );
1261 if( !p_instance ) return VLC_EGENERIC;
1263 if( !strcmp( psz_command, "seek" ) )
1270 f_percentage = i18n_atof( psz_args );
1271 if( f_percentage >= 0.0 && f_percentage <= 100.0 )
1273 val.f_float = f_percentage / 100.0 ;
1274 var_Set( p_instance->p_input, "position", val );
1279 else if( !strcmp( psz_command, "stop" ) )
1281 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1283 if( p_instance->p_input )
1285 input_StopThread( p_instance->p_input );
1286 input_DestroyThread( p_instance->p_input );
1287 vlc_object_detach( p_instance->p_input );
1288 vlc_object_destroy( p_instance->p_input );
1291 vlc_input_item_Clean( &p_instance->item );
1292 if( p_instance->psz_name ) free( p_instance->psz_name );
1297 else if( !strcmp( psz_command, "pause" ) )
1301 if( !p_instance->p_input ) return VLC_SUCCESS;
1303 var_Get( p_instance->p_input, "state", &val );
1305 if( val.i_int == PAUSE_S ) val.i_int = PLAYING_S;
1306 else val.i_int = PAUSE_S;
1307 var_Set( p_instance->p_input, "state", val );
1312 return VLC_EGENERIC;
1315 /*****************************************************************************
1317 *****************************************************************************/
1318 static int64_t vlm_Date()
1323 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1329 vlm_schedule_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
1331 vlm_schedule_t *p_sched = malloc( sizeof( vlm_schedule_t ) );
1343 p_sched->psz_name = strdup( psz_name );
1344 p_sched->b_enabled = VLC_FALSE;
1345 p_sched->i_command = 0;
1346 p_sched->command = NULL;
1347 p_sched->i_date = 0;
1348 p_sched->i_period = 0;
1349 p_sched->i_repeat = -1;
1351 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1356 /* for now, simple delete. After, del with options (last arg) */
1357 void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_t *sched,
1358 const char *psz_name )
1360 if( sched == NULL ) return;
1362 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1364 if( vlm->i_schedule == 0 && vlm->schedule ) free( vlm->schedule );
1365 free( sched->psz_name );
1366 while( sched->i_command )
1368 char *psz_cmd = sched->command[0];
1369 TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1375 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1379 for( i = 0; i < vlm->i_schedule; i++ )
1381 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1383 return vlm->schedule[i];
1390 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1391 int vlm_ScheduleSetup( vlm_schedule_t *schedule, const char *psz_cmd,
1392 const char *psz_value )
1394 if( !strcmp( psz_cmd, "enabled" ) )
1396 schedule->b_enabled = VLC_TRUE;
1398 else if( !strcmp( psz_cmd, "disabled" ) )
1400 schedule->b_enabled = VLC_FALSE;
1402 #if !defined( UNDER_CE )
1403 else if( !strcmp( psz_cmd, "date" ) )
1409 time.tm_sec = 0; /* seconds */
1410 time.tm_min = 0; /* minutes */
1411 time.tm_hour = 0; /* hours */
1412 time.tm_mday = 0; /* day of the month */
1413 time.tm_mon = 0; /* month */
1414 time.tm_year = 0; /* year */
1415 time.tm_wday = 0; /* day of the week */
1416 time.tm_yday = 0; /* day in the year */
1417 time.tm_isdst = -1; /* daylight saving time */
1419 /* date should be year/month/day-hour:minutes:seconds */
1420 p = strchr( psz_value, '-' );
1422 if( !strcmp( psz_value, "now" ) )
1424 schedule->i_date = 0;
1426 else if( (p == NULL) && sscanf( psz_value, "%d:%d:%d", &time.tm_hour,
1427 &time.tm_min, &time.tm_sec ) != 3 )
1428 /* it must be a hour:minutes:seconds */
1436 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1454 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1460 time.tm_mon = i - 1;
1464 time.tm_year = i - 1900;
1465 time.tm_mon = j - 1;
1472 date = mktime( &time );
1473 schedule->i_date = ((mtime_t) date) * 1000000;
1476 else if( !strcmp( psz_cmd, "period" ) )
1480 const char *psz_time = NULL, *psz_date = NULL;
1484 /* First, if date or period are modified, repeat should be equal to -1 */
1485 schedule->i_repeat = -1;
1487 time.tm_sec = 0; /* seconds */
1488 time.tm_min = 0; /* minutes */
1489 time.tm_hour = 0; /* hours */
1490 time.tm_mday = 0; /* day of the month */
1491 time.tm_mon = 0; /* month */
1492 time.tm_year = 0; /* year */
1493 time.tm_wday = 0; /* day of the week */
1494 time.tm_yday = 0; /* day in the year */
1495 time.tm_isdst = -1; /* daylight saving time */
1497 /* date should be year/month/day-hour:minutes:seconds */
1498 p = strchr( psz_value, '-' );
1501 psz_date = psz_value;
1506 psz_time = psz_value;
1509 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1528 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1547 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1548 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1549 schedule->i_period = ((mtime_t) date) * 1000000;
1551 #endif /* UNDER_CE */
1552 else if( !strcmp( psz_cmd, "repeat" ) )
1556 if( sscanf( psz_value, "%d", &i ) == 1 )
1558 schedule->i_repeat = i;
1565 else if( !strcmp( psz_cmd, "append" ) )
1567 char *command = strdup( psz_value );
1569 TAB_APPEND( schedule->i_command, schedule->command, command );
1578 /*****************************************************************************
1579 * Message handling functions
1580 *****************************************************************************/
1581 static vlm_message_t *vlm_MessageNew( char *psz_name,
1582 const char *psz_format, ... )
1584 vlm_message_t *p_message;
1587 if( !psz_name ) return 0;
1589 p_message = malloc( sizeof(vlm_message_t) );
1595 p_message->psz_value = 0;
1599 va_start( args, psz_format );
1600 if( vasprintf( &p_message->psz_value, psz_format, args ) < 0 )
1609 p_message->psz_name = strdup( psz_name );
1610 p_message->i_child = 0;
1611 p_message->child = NULL;
1616 void vlm_MessageDelete( vlm_message_t *p_message )
1618 if( p_message->psz_name ) free( p_message->psz_name );
1619 if( p_message->psz_value ) free( p_message->psz_value );
1620 while( p_message->i_child-- )
1621 vlm_MessageDelete( p_message->child[p_message->i_child] );
1622 if( p_message->child ) free( p_message->child );
1627 static vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1628 vlm_message_t *p_child )
1630 if( p_message == NULL ) return NULL;
1634 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1640 /*****************************************************************************
1641 * Misc utility functions
1642 *****************************************************************************/
1643 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_t *media,
1644 vlm_schedule_t *schedule, char *psz_filter )
1650 vlm_message_t *msg_media;
1651 vlm_message_t *msg_child;
1653 msg = vlm_MessageNew( "show", NULL );
1654 msg_media = vlm_MessageAdd( msg, vlm_MessageNew( media->psz_name, 0 ));
1656 vlm_MessageAdd( msg_media,
1657 vlm_MessageNew( "type", media->i_type == VOD_TYPE ?
1658 "vod" : "broadcast" ) );
1659 vlm_MessageAdd( msg_media,
1660 vlm_MessageNew( "enabled", media->b_enabled ?
1663 vlm_MessageAdd( msg_media,
1664 vlm_MessageNew( "loop", media->b_loop ?
1667 if( media->i_type == VOD_TYPE && media->psz_mux )
1668 vlm_MessageAdd( msg_media,
1669 vlm_MessageNew( "mux", media->psz_mux ) );
1671 msg_child = vlm_MessageAdd( msg_media,
1672 vlm_MessageNew( "inputs", NULL ) );
1674 for( i = 0; i < media->i_input; i++ )
1676 vlm_MessageAdd( msg_child,
1677 vlm_MessageNew( media->input[i], NULL ) );
1680 vlm_MessageAdd( msg_media,
1681 vlm_MessageNew( "output", media->psz_output ?
1682 media->psz_output : "" ) );
1684 msg_child = vlm_MessageAdd( msg_media, vlm_MessageNew( "options", 0 ));
1686 for( i = 0; i < media->i_option; i++ )
1688 vlm_MessageAdd( msg_child, vlm_MessageNew( media->option[i], 0 ) );
1691 msg_child = vlm_MessageAdd( msg_media,
1692 vlm_MessageNew( "instances", NULL ) );
1694 for( i = 0; i < media->i_instance; i++ )
1696 vlm_media_instance_t *p_instance = media->instance[i];
1698 vlm_message_t *msg_instance;
1701 if( !p_instance->p_input ) val.i_int = END_S;
1702 else var_Get( p_instance->p_input, "state", &val );
1704 msg_instance = vlm_MessageNew( "instance" , NULL );
1705 vlm_MessageAdd( msg_instance, vlm_MessageNew( "name" , p_instance->psz_name ? p_instance->psz_name : "default" ) );
1706 vlm_MessageAdd( msg_instance, vlm_MessageNew( "state",
1707 val.i_int == PLAYING_S ? "playing" :
1708 val.i_int == PAUSE_S ? "paused" :
1710 #define APPEND_INPUT_INFO( a, format, type ) \
1711 asprintf( &psz_tmp, format, \
1712 var_Get ## type( p_instance->p_input, a ) ); \
1713 vlm_MessageAdd( msg_instance, vlm_MessageNew( a, psz_tmp ) ); \
1715 APPEND_INPUT_INFO( "position", "%f", Float );
1716 APPEND_INPUT_INFO( "time", "%d", Integer );
1717 APPEND_INPUT_INFO( "length", "%d", Integer );
1718 APPEND_INPUT_INFO( "rate", "%d", Integer );
1719 APPEND_INPUT_INFO( "title", "%d", Integer );
1720 APPEND_INPUT_INFO( "chapter", "%d", Integer );
1721 APPEND_INPUT_INFO( "seekable", "%d", Bool );
1722 #undef APPEND_INPUT_INFO
1723 asprintf( &psz_tmp, "%d", p_instance->i_index + 1 );
1724 vlm_MessageAdd( msg_instance, vlm_MessageNew( "playlistindex", psz_tmp ) );
1726 vlm_MessageAdd( msg_child, msg_instance );
1733 else if( schedule != NULL )
1737 vlm_message_t *msg_schedule;
1738 vlm_message_t *msg_child;
1741 msg = vlm_MessageNew( "show", NULL );
1743 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, 0 ) );
1745 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1747 vlm_MessageAdd( msg_schedule,
1748 vlm_MessageNew( "enabled", schedule->b_enabled ?
1751 #if !defined( UNDER_CE )
1752 if( schedule->i_date != 0 )
1755 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1758 #ifdef HAVE_LOCALTIME_R
1759 localtime_r( &i_time, &date);
1761 struct tm *p_date = localtime( &i_time );
1765 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1766 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1767 date.tm_hour, date.tm_min, date.tm_sec );
1769 vlm_MessageAdd( msg_schedule,
1770 vlm_MessageNew( "date", psz_date ) );
1775 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1778 if( schedule->i_period != 0 )
1780 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1783 date.tm_sec = (int)( i_time % 60 );
1784 i_time = i_time / 60;
1785 date.tm_min = (int)( i_time % 60 );
1786 i_time = i_time / 60;
1787 date.tm_hour = (int)( i_time % 24 );
1788 i_time = i_time / 24;
1789 date.tm_mday = (int)( i_time % 30 );
1790 i_time = i_time / 30;
1791 /* okay, okay, months are not always 30 days long */
1792 date.tm_mon = (int)( i_time % 12 );
1793 i_time = i_time / 12;
1794 date.tm_year = (int)i_time;
1796 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1797 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1799 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1803 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1805 #endif /* UNDER_CE */
1807 sprintf( buffer, "%d", schedule->i_repeat );
1808 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1811 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", 0) );
1813 for( i = 0; i < schedule->i_command; i++ )
1815 vlm_MessageAdd( msg_child,
1816 vlm_MessageNew( schedule->command[i], NULL ) );
1823 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1827 vlm_message_t *msg_child;
1828 int i_vod = 0, i_broadcast = 0;
1831 for( i = 0; i < vlm->i_media; i++ )
1833 if( vlm->media[i]->i_type == VOD_TYPE )
1839 asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1841 msg = vlm_MessageNew( "show", NULL );
1842 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "media", psz_count ) );
1845 for( i = 0; i < vlm->i_media; i++ )
1847 vlm_media_t *m = vlm->media[i];
1848 vlm_message_t *msg_media, *msg_instance;
1850 msg_media = vlm_MessageAdd( msg_child,
1851 vlm_MessageNew( m->psz_name, 0 ) );
1853 vlm_MessageAdd( msg_media,
1854 vlm_MessageNew( "type", m->i_type == VOD_TYPE ?
1855 "vod" : "broadcast" ) );
1857 vlm_MessageAdd( msg_media,
1858 vlm_MessageNew( "enabled", m->b_enabled ?
1861 if( m->i_type == VOD_TYPE && m->psz_mux )
1862 vlm_MessageAdd( msg_media,
1863 vlm_MessageNew( "mux", m->psz_mux ) );
1865 msg_instance = vlm_MessageAdd( msg_media,
1866 vlm_MessageNew( "instances", 0 ) );
1868 for( j = 0; j < m->i_instance; j++ )
1870 vlm_media_instance_t *p_instance = m->instance[j];
1873 if( !p_instance->p_input ) val.i_int = END_S;
1874 else var_Get( p_instance->p_input, "state", &val );
1876 vlm_MessageAdd( msg_instance,
1877 vlm_MessageNew( p_instance->psz_name ?
1878 p_instance->psz_name : "default",
1879 val.i_int == PLAYING_S ? "playing" :
1880 val.i_int == PAUSE_S ? "paused" :
1888 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1892 vlm_message_t *msg_child;
1894 msg = vlm_MessageNew( "show", NULL );
1895 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", NULL ) );
1897 for( i = 0; i < vlm->i_schedule; i++ )
1899 vlm_schedule_t *s = vlm->schedule[i];
1900 vlm_message_t *msg_schedule;
1901 mtime_t i_time, i_next_date;
1903 msg_schedule = vlm_MessageAdd( msg_child,
1904 vlm_MessageNew( s->psz_name, 0 ) );
1905 vlm_MessageAdd( msg_schedule,
1906 vlm_MessageNew( "enabled", s->b_enabled ?
1909 /* calculate next date */
1910 i_time = vlm_Date();
1911 i_next_date = s->i_date;
1913 if( s->i_period != 0 )
1916 while( s->i_date + j * s->i_period <= i_time &&
1922 i_next_date = s->i_date + j * s->i_period;
1925 if( i_next_date > i_time )
1927 time_t i_date = (time_t) (i_next_date / 1000000) ;
1929 #if !defined( UNDER_CE )
1932 ctime_r( &i_date, psz_date );
1934 char *psz_date = ctime( &i_date );
1937 vlm_MessageAdd( msg_schedule,
1938 vlm_MessageNew( "next launch", psz_date ) );
1946 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
1948 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1949 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1951 vlm_MessageAdd( show1, show2->child[0] );
1953 /* We must destroy the parent node "show" of show2
1954 * and not the children */
1955 free( show2->psz_name );
1963 return vlm_MessageNew( "show", NULL );
1967 static vlm_message_t *vlm_Help( vlm_t *vlm, char *psz_filter )
1969 vlm_message_t *message, *message_child;
1971 #define MessageAdd( a ) \
1972 vlm_MessageAdd( message, vlm_MessageNew( a, NULL ) );
1973 #define MessageAddChild( a ) \
1974 vlm_MessageAdd( message_child, vlm_MessageNew( a, NULL ) );
1976 if( psz_filter == NULL )
1978 message = vlm_MessageNew( "help", NULL );
1980 message_child = MessageAdd( "Commands Syntax:" );
1981 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
1982 MessageAddChild( "setup (name) (properties)" );
1983 MessageAddChild( "show [(name)|media|schedule]" );
1984 MessageAddChild( "del (name)|all|media|schedule" );
1985 MessageAddChild( "control (name) [instance_name] (command)" );
1986 MessageAddChild( "save (config_file)" );
1987 MessageAddChild( "export" );
1988 MessageAddChild( "load (config_file)" );
1990 message_child = MessageAdd( "Media Proprieties Syntax:" );
1991 MessageAddChild( "input (input_name)" );
1992 MessageAddChild( "inputdel (input_name)|all" );
1993 MessageAddChild( "inputdeln input_number" );
1994 MessageAddChild( "output (output_name)" );
1995 MessageAddChild( "option (option_name)[=value]" );
1996 MessageAddChild( "enabled|disabled" );
1997 MessageAddChild( "loop|unloop (broadcast only)" );
1998 MessageAddChild( "mux (mux_name)" );
2000 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
2001 MessageAddChild( "enabled|disabled" );
2002 MessageAddChild( "append (command_until_rest_of_the_line)" );
2003 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
2005 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
2006 "(days)-(hours):(minutes):(seconds)" );
2007 MessageAddChild( "repeat (number_of_repetitions)" );
2009 message_child = MessageAdd( "Control Commands Syntax:" );
2010 MessageAddChild( "play" );
2011 MessageAddChild( "pause" );
2012 MessageAddChild( "stop" );
2013 MessageAddChild( "seek (percentage)" );
2018 return vlm_MessageNew( "help", NULL );
2021 /*****************************************************************************
2022 * Config handling functions
2023 *****************************************************************************/
2024 static int Load( vlm_t *vlm, char *file )
2029 while( *pf != '\0' )
2031 vlm_message_t *message = NULL;
2034 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
2039 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
2043 if( pf[i_end] == '\n' ) i_end++;
2046 if( *pf && ExecuteCommand( vlm, pf, &message ) )
2050 if( message->psz_value )
2051 msg_Err( vlm, "Load error on line %d: %s: %s",
2052 i_line, message->psz_name, message->psz_value );
2053 vlm_MessageDelete( message );
2057 if( message ) vlm_MessageDelete( message );
2066 static char *Save( vlm_t *vlm )
2069 char psz_header[] = "\n"
2070 "# VLC media player VLM command batch\n"
2071 "# http://www.videolan.org/vlc/\n\n" ;
2074 int i_length = strlen( psz_header );
2076 for( i = 0; i < vlm->i_media; i++ )
2078 vlm_media_t *media = vlm->media[i];
2080 if( media->i_type == VOD_TYPE )
2082 i_length += strlen( "new vod " ) + strlen(media->psz_name);
2086 i_length += strlen( "new broadcast " ) + strlen(media->psz_name);
2089 if( media->b_enabled == VLC_TRUE )
2091 i_length += strlen( "enabled" );
2095 i_length += strlen( "disabled" );
2098 if( media->b_loop == VLC_TRUE )
2100 i_length += strlen( " loop\n" );
2104 i_length += strlen( "\n" );
2107 for( j = 0; j < media->i_input; j++ )
2109 i_length += strlen( "setup input \"\"\n" ) +
2110 strlen( media->psz_name ) + strlen( media->input[j] );
2113 if( media->psz_output != NULL )
2115 i_length += strlen(media->psz_name) + strlen(media->psz_output) +
2116 strlen( "setup output \n" );
2119 for( j=0 ; j < media->i_option ; j++ )
2121 i_length += strlen(media->psz_name) + strlen(media->option[j]) +
2122 strlen("setup option \n");
2126 for( i = 0; i < vlm->i_schedule; i++ )
2128 vlm_schedule_t *schedule = vlm->schedule[i];
2130 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
2132 if( schedule->b_enabled == VLC_TRUE )
2134 i_length += strlen( "date //-:: enabled\n" ) + 14;
2138 i_length += strlen( "date //-:: disabled\n" ) + 14;
2142 if( schedule->i_period != 0 )
2144 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
2145 strlen( "period //-::\n" ) + 14;
2148 if( schedule->i_repeat >= 0 )
2152 sprintf( buffer, "%d", schedule->i_repeat );
2153 i_length += strlen( "setup repeat \n" ) +
2154 strlen( schedule->psz_name ) + strlen( buffer );
2161 for( j = 0; j < schedule->i_command; j++ )
2163 i_length += strlen( "setup append \n" ) +
2164 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
2169 /* Don't forget the '\0' */
2171 /* now we have the length of save */
2173 p = save = malloc( i_length );
2176 p += sprintf( p, "%s", psz_header );
2178 /* finally we can write in it */
2179 for( i = 0; i < vlm->i_media; i++ )
2181 vlm_media_t *media = vlm->media[i];
2183 if( media->i_type == VOD_TYPE )
2185 p += sprintf( p, "new %s vod ", media->psz_name);
2189 p += sprintf( p, "new %s broadcast ", media->psz_name);
2192 if( media->b_enabled == VLC_TRUE )
2194 p += sprintf( p, "enabled" );
2198 p += sprintf( p, "disabled" );
2201 if( media->b_loop == VLC_TRUE )
2203 p += sprintf( p, " loop\n" );
2207 p += sprintf( p, "\n" );
2210 for( j = 0; j < media->i_input; j++ )
2212 p += sprintf( p, "setup %s input \"%s\"\n", media->psz_name,
2216 if( media->psz_output != NULL )
2218 p += sprintf( p, "setup %s output %s\n", media->psz_name,
2219 media->psz_output );
2222 for( j = 0; j < media->i_option; j++ )
2224 p += sprintf( p, "setup %s option %s\n", media->psz_name,
2229 /* and now, the schedule scripts */
2230 #if !defined( UNDER_CE )
2231 for( i = 0; i < vlm->i_schedule; i++ )
2233 vlm_schedule_t *schedule = vlm->schedule[i];
2235 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
2237 #ifdef HAVE_LOCALTIME_R
2238 localtime_r( &i_time, &date);
2240 struct tm *p_date = localtime( &i_time );
2244 p += sprintf( p, "new %s schedule ", schedule->psz_name);
2246 if( schedule->b_enabled == VLC_TRUE )
2248 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
2249 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2250 date.tm_hour, date.tm_min, date.tm_sec );
2254 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
2255 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2256 date.tm_hour, date.tm_min, date.tm_sec);
2259 if( schedule->i_period != 0 )
2261 p += sprintf( p, "setup %s ", schedule->psz_name );
2263 i_time = (time_t) ( schedule->i_period / 1000000 );
2265 date.tm_sec = (int)( i_time % 60 );
2266 i_time = i_time / 60;
2267 date.tm_min = (int)( i_time % 60 );
2268 i_time = i_time / 60;
2269 date.tm_hour = (int)( i_time % 24 );
2270 i_time = i_time / 24;
2271 date.tm_mday = (int)( i_time % 30 );
2272 i_time = i_time / 30;
2273 /* okay, okay, months are not always 30 days long */
2274 date.tm_mon = (int)( i_time % 12 );
2275 i_time = i_time / 12;
2276 date.tm_year = (int)i_time;
2278 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
2279 date.tm_year, date.tm_mon, date.tm_mday,
2280 date.tm_hour, date.tm_min, date.tm_sec);
2283 if( schedule->i_repeat >= 0 )
2285 p += sprintf( p, "setup %s repeat %d\n",
2286 schedule->psz_name, schedule->i_repeat );
2290 p += sprintf( p, "\n" );
2293 for( j = 0; j < schedule->i_command; j++ )
2295 p += sprintf( p, "setup %s append %s\n",
2296 schedule->psz_name, schedule->command[j] );
2300 #endif /* UNDER_CE */
2305 /*****************************************************************************
2307 *****************************************************************************/
2308 int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
2309 const char *psz_id, int i_query, va_list args )
2311 vlm_t *vlm = (vlm_t *)p_private;
2312 int i, i_ret = VLC_EGENERIC;
2314 if( !p_private || !p_vod_media ) return VLC_EGENERIC;
2316 vlc_mutex_lock( &vlm->lock );
2319 for( i = 0; i < vlm->i_media; i++ )
2321 if( p_vod_media == vlm->media[i]->vod_media ) break;
2324 if( i == vlm->i_media )
2326 vlc_mutex_unlock( &vlm->lock );
2327 return VLC_EGENERIC;
2332 case VOD_MEDIA_PLAY:
2333 vlm->media[i]->psz_vod_output = (char *)va_arg( args, char * );
2334 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "play", 0 );
2335 vlm->media[i]->psz_vod_output = 0;
2338 case VOD_MEDIA_PAUSE:
2339 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "pause", 0 );
2342 case VOD_MEDIA_STOP:
2343 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "stop", 0 );
2346 case VOD_MEDIA_SEEK:
2348 double f_pos = (double)va_arg( args, double );
2350 lldiv_t div = lldiv( f_pos * 10000000, 10000000 );
2351 sprintf( psz_pos, I64Fd".%07u", div.quot, (unsigned int) div.rem );
2352 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "seek", psz_pos);
2360 vlc_mutex_unlock( &vlm->lock );
2365 /*****************************************************************************
2367 *****************************************************************************/
2368 static int Manage( vlc_object_t* p_object )
2370 vlm_t *vlm = (vlm_t*)p_object;
2372 mtime_t i_lastcheck;
2375 i_lastcheck = vlm_Date();
2379 while( !vlm->b_die )
2381 char **ppsz_scheduled_commands = NULL;
2382 int i_scheduled_commands = 0;
2383 vlc_mutex_lock( &vlm->lock );
2385 /* destroy the inputs that wants to die, and launch the next input */
2386 for( i = 0; i < vlm->i_media; i++ )
2388 vlm_media_t *p_media = vlm->media[i];
2390 for( j = 0; j < p_media->i_instance; j++ )
2392 vlm_media_instance_t *p_instance = p_media->instance[j];
2394 if( !p_instance->p_input ||
2395 ( !p_instance->p_input->b_eof &&
2396 !p_instance->p_input->b_error ) ) continue;
2398 input_StopThread( p_instance->p_input );
2399 input_DestroyThread( p_instance->p_input );
2400 vlc_object_detach( p_instance->p_input );
2401 vlc_object_destroy( p_instance->p_input );
2403 p_instance->i_index++;
2404 if( p_instance->i_index == p_media->i_input &&
2405 p_media->b_loop ) p_instance->i_index = 0;
2407 if( p_instance->i_index < p_media->i_input )
2409 /* FIXME, find a way to select the right instance */
2411 sprintf( buffer, "%d", p_instance->i_index );
2412 vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2417 if( vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2418 "stop", 0 ) == VLC_SUCCESS ) i--;
2424 i_time = vlm_Date();
2426 for( i = 0; i < vlm->i_schedule; i++ )
2428 mtime_t i_real_date = vlm->schedule[i]->i_date;
2430 if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2432 if( vlm->schedule[i]->i_date == 0 ) // now !
2434 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2435 i_real_date = i_time;
2437 else if( vlm->schedule[i]->i_period != 0 )
2440 while( vlm->schedule[i]->i_date + j *
2441 vlm->schedule[i]->i_period <= i_lastcheck &&
2442 ( vlm->schedule[i]->i_repeat > j ||
2443 vlm->schedule[i]->i_repeat == -1 ) )
2448 i_real_date = vlm->schedule[i]->i_date + j *
2449 vlm->schedule[i]->i_period;
2452 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2454 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2456 TAB_APPEND( i_scheduled_commands,
2457 ppsz_scheduled_commands,
2458 strdup(vlm->schedule[i]->command[j] ) );
2463 while( i_scheduled_commands )
2465 vlm_message_t *message = NULL;
2466 char *psz_command = ppsz_scheduled_commands[0];
2467 ExecuteCommand( vlm, psz_command,&message );
2469 /* for now, drop the message */
2470 vlm_MessageDelete( message );
2471 TAB_REMOVE( i_scheduled_commands,
2472 ppsz_scheduled_commands,
2474 free( psz_command );
2477 i_lastcheck = i_time;
2479 vlc_mutex_unlock( &vlm->lock );
2487 #else /* ENABLE_VLM */
2489 /* We just define an empty wrapper */
2490 vlm_t *__vlm_New( vlc_object_t *a )
2492 msg_Err( a, "VideoLAN manager support is disabled" );
2495 void vlm_Delete( vlm_t *a ){}
2496 int vlm_ExecuteCommand( vlm_t *a, char *b, vlm_message_t **c ){ return -1; }
2497 void vlm_MessageDelete( vlm_message_t *a ){}
2498 vlm_media_t *vlm_MediaNew( vlm_t *a, char *b, int c ){ return NULL; }
2499 vlm_media_t *vlm_MediaSearch (vlm_t *a, const char *b ) { return NULL; }
2500 void vlm_MediaDelete( vlm_t *a, vlm_media_t *b, char *c ){}
2501 int vlm_MediaSetup( vlm_t *a, vlm_media_t *b, char *c, char *d ){ return -1; }
2502 int vlm_MediaControl( vlm_t *a, vlm_media_t *b, char *c, char *d, char *e )
2504 vlm_schedule_t * vlm_ScheduleNew( vlm_t *a, char *b ){ return NULL; }
2505 void vlm_ScheduleDelete( vlm_t *a, vlm_schedule_t *b, char *c ){}
2506 int vlm_ScheduleSetup( vlm_schedule_t *a, char *b, char *c ){ return -1; }
2507 int vlm_MediaVodControl( void *a, vod_media_t *b, char *c, int d, va_list e )
2509 int vlm_Save( vlm_t *a, char *b ){ return -1; }
2510 int vlm_Load( vlm_t *a, char *b ){ return -1; }
2512 #endif /* ENABLE_VLM */