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() */
48 if( p ) { free( p ); (p) = NULL; }
50 /*****************************************************************************
52 *****************************************************************************/
53 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_t *, vlm_schedule_t *, char * );
54 static vlm_message_t *vlm_Help( vlm_t *, char * );
56 static vlm_media_t *vlm_MediaSearch ( vlm_t *, const 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 conf ..." );
121 asprintf(&psz_buffer, "load %s", psz_vlmconf );
124 msg_Dbg( p_this, psz_buffer);
125 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) ){
126 msg_Warn( p_this, "error while loading the vlm conf file" );
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 = 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 file = fopen( psz_file, "r" );
225 if( file == NULL ) return 1;
227 if( fseek( file, 0, SEEK_END) != 0 )
233 i_size = ftell( file );
234 fseek( file, 0, SEEK_SET );
235 psz_buffer = malloc( i_size + 1 );
241 fread( psz_buffer, 1, i_size, file );
242 psz_buffer[ i_size ] = '\0';
243 if( Load( p_vlm, psz_buffer ) )
256 /*****************************************************************************
258 *****************************************************************************/
259 static const char *FindEndCommand( const char *psz_sent )
266 while( ( *psz_sent != '\"' ) && ( *psz_sent != '\0' ) )
268 if( *psz_sent == '\'' )
270 psz_sent = FindEndCommand( psz_sent );
271 if( psz_sent == NULL ) return NULL;
276 if( *psz_sent == '\"' )
282 /* *psz_sent == '\0' -> number of " is incorrect */
290 while( ( *psz_sent != '\'' ) && ( *psz_sent != '\0' ) )
292 if( *psz_sent == '\"' )
294 psz_sent = FindEndCommand( psz_sent );
295 if( psz_sent == NULL ) return NULL;
300 if( *psz_sent == '\'' )
306 /* *psz_sent == '\0' -> number of " is incorrect */
311 default: /* now we can look for spaces */
312 while( ( *psz_sent != ' ' ) && ( *psz_sent != '\0' ) )
314 if( ( *psz_sent == '\'' ) || ( *psz_sent == '\"' ) )
316 psz_sent = FindEndCommand( psz_sent );
317 if( psz_sent == NULL ) return NULL;
326 /*****************************************************************************
327 * ExecuteCommand: The main state machine
328 *****************************************************************************
329 * Execute a command which ends with '\0' (string)
330 *****************************************************************************/
331 static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
332 vlm_message_t **pp_message )
335 char **ppsz_command = NULL;
336 const char *psz_cmd = psz_command;
337 vlm_message_t *p_message = NULL;
340 /* First, parse the line and cut it */
341 while( *psz_cmd != '\0' )
344 if( *psz_cmd == ' ' || *psz_cmd == '\t' )
350 const char *psz_temp;
353 /* support for comments */
354 if( i_command == 0 && *psz_cmd == '#')
356 p_message = vlm_MessageNew( "", NULL );
360 psz_temp = FindEndCommand( psz_cmd );
362 if( psz_temp == NULL )
364 p_message = vlm_MessageNew( "Incomplete command", psz_cmd );
368 i_temp = psz_temp - psz_cmd;
370 ppsz_command = realloc( ppsz_command, (i_command + 1) *
372 ppsz_command[ i_command ] = malloc( (i_temp + 1) * sizeof(char) );
373 strncpy( ppsz_command[ i_command ], psz_cmd, i_temp );
374 ppsz_command[ i_command ][ i_temp ] = '\0';
383 * And then Interpret it
388 p_message = vlm_MessageNew( "", NULL );
392 if( !strcmp(ppsz_command[0], "new") )
396 /* Check the number of arguments */
397 if( i_command < 3 ) goto syntax_error;
400 if( !strcmp(ppsz_command[2], "vod") )
404 else if( !strcmp(ppsz_command[2], "broadcast") )
406 i_type = BROADCAST_TYPE;
408 else if( !strcmp(ppsz_command[2], "schedule") )
410 i_type = SCHEDULE_TYPE;
415 vlm_MessageNew( "new", "%s: Choose between vod, "
416 "broadcast or schedule", ppsz_command[1] );
420 /* Check for forbidden media names */
421 if( !strcmp(ppsz_command[1], "all") ||
422 !strcmp(ppsz_command[1], "media") ||
423 !strcmp(ppsz_command[1], "schedule") )
425 p_message = vlm_MessageNew( "new", "\"all\", \"media\" and "
426 "\"schedule\" are reserved names" );
430 /* Check the name is not already in use */
431 if( vlm_ScheduleSearch( p_vlm, ppsz_command[1] ) ||
432 vlm_MediaSearch( p_vlm, ppsz_command[1] ) )
434 p_message = vlm_MessageNew( "new", "%s: Name already in use",
440 if( i_type == SCHEDULE_TYPE )
442 vlm_schedule_t *p_schedule;
443 p_schedule = vlm_ScheduleNew( p_vlm, ppsz_command[1] );
446 p_message = vlm_MessageNew( "new", "could not create schedule" );
454 vlm_media_t *p_media;
455 p_media = vlm_MediaNew( p_vlm, ppsz_command[1], i_type );
458 p_message = vlm_MessageNew( "new", "could not create media" );
465 p_message = vlm_MessageNew( "new", NULL );
469 /* Properties will be dealt with later on */
472 else if( !strcmp(ppsz_command[0], "setup") )
474 if( i_command < 2 ) goto syntax_error;
476 /* Properties will be dealt with later on */
479 else if( !strcmp(ppsz_command[0], "del") )
481 vlm_media_t *p_media;
482 vlm_schedule_t *p_schedule;
484 if( i_command < 2 ) goto syntax_error;
486 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
487 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
489 if( p_schedule != NULL )
491 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
493 else if( p_media != NULL )
495 vlm_MediaDelete( p_vlm, p_media, NULL );
497 else if( !strcmp(ppsz_command[1], "media") )
499 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
502 else if( !strcmp(ppsz_command[1], "schedule") )
504 while( p_vlm->i_schedule )
505 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
507 else if( !strcmp(ppsz_command[1], "all") )
509 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
512 while( p_vlm->i_schedule )
513 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
517 p_message = vlm_MessageNew( "del", "%s: media unknown",
522 p_message = vlm_MessageNew( "del", NULL );
526 else if( !strcmp(ppsz_command[0], "show") )
528 vlm_media_t *p_media;
529 vlm_schedule_t *p_schedule;
533 p_message = vlm_Show( p_vlm, NULL, NULL, NULL );
536 else if( i_command > 2 ) goto syntax_error;
538 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
539 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
541 if( p_schedule != NULL )
543 p_message = vlm_Show( p_vlm, NULL, p_schedule, NULL );
545 else if( p_media != NULL )
547 p_message = vlm_Show( p_vlm, p_media, NULL, NULL );
551 p_message = vlm_Show( p_vlm, NULL, NULL, ppsz_command[1] );
557 else if( !strcmp(ppsz_command[0], "help") )
559 if( i_command != 1 ) goto syntax_error;
561 p_message = vlm_Help( p_vlm, NULL );
565 else if( !strcmp(ppsz_command[0], "control") )
567 vlm_media_t *p_media;
569 if( i_command < 3 ) goto syntax_error;
571 if( !(p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] ) ) )
573 p_message = vlm_MessageNew( "control", "%s: media unknown",
579 char *psz_command, *psz_arg = 0, *psz_instance = 0;
582 if( strcmp( ppsz_command[2], "play" ) &&
583 strcmp( ppsz_command[2], "stop" ) &&
584 strcmp( ppsz_command[2], "pause" ) &&
585 strcmp( ppsz_command[2], "seek" ) )
588 psz_instance = ppsz_command[2];
590 if( i_command < 4 ) goto syntax_error;
593 psz_command = ppsz_command[i_index];
595 if( i_command >= i_index + 2 ) psz_arg = ppsz_command[i_index + 1];
597 vlm_MediaControl( p_vlm, p_media, psz_instance, psz_command,
599 p_message = vlm_MessageNew( "control", NULL );
604 else if( !strcmp(ppsz_command[0], "save") )
606 if( i_command != 2 ) goto syntax_error;
608 if( vlm_Save( p_vlm, ppsz_command[1] ) )
610 p_message = vlm_MessageNew( "save", "Unable to save to file" );
615 p_message = vlm_MessageNew( "save", NULL );
620 else if( !strcmp(ppsz_command[0], "export" ) )
624 if( i_command != 1 ) goto syntax_error;
626 p_message = vlm_MessageNew( "export", psz_buf = Save( p_vlm ) );
632 else if( !strcmp(ppsz_command[0], "load") )
634 if( i_command != 2 ) goto syntax_error;
636 switch( vlm_Load( p_vlm, ppsz_command[1] ) )
639 p_message = vlm_MessageNew( "load", NULL );
642 p_message = vlm_MessageNew( "load", "read file error" );
646 vlm_MessageNew( "load", "error while loading file" );
650 vlm_MessageNew( "load", "Unable to load from file" );
657 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
661 /* Common code between "new" and "setup" */
662 if( !strcmp(ppsz_command[0], "new") ||
663 !strcmp(ppsz_command[0], "setup") )
665 int i_command_start = strcmp(ppsz_command[0], "new") ? 2 : 3;
666 vlm_media_t *p_media;
667 vlm_schedule_t *p_schedule;
669 if( i_command < i_command_start ) goto syntax_error;
671 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
672 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
674 if( !p_media && !p_schedule )
676 p_message = vlm_MessageNew( ppsz_command[0], "%s unknown",
681 if( p_schedule != NULL )
683 for( i = i_command_start ; i < i_command ; i++ )
685 if( !strcmp( ppsz_command[i], "enabled" ) ||
686 !strcmp( ppsz_command[i], "disabled" ) )
688 vlm_ScheduleSetup( p_schedule, ppsz_command[i], NULL );
691 /* Beware: everything behind append is considered as
693 else if( !strcmp( ppsz_command[i], "append" ) )
695 if( ++i >= i_command ) break;
697 for( j = i + 1; j < i_command; j++ )
700 realloc( ppsz_command[i], strlen(ppsz_command[i]) +
701 strlen(ppsz_command[j]) + 1 + 1 );
702 strcat( ppsz_command[i], " " );
703 strcat( ppsz_command[i], ppsz_command[j] );
706 vlm_ScheduleSetup( p_schedule, ppsz_command[i - 1],
712 if( i + 1 >= i_command && !strcmp(ppsz_command[0], "new") )
714 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
716 vlm_MessageNew( ppsz_command[0],
717 "Wrong properties syntax" );
720 else if( i + 1 >= i_command )
723 vlm_MessageNew( ppsz_command[0],
724 "Wrong properties syntax" );
728 vlm_ScheduleSetup( p_schedule, ppsz_command[i],
735 else if( p_media != NULL )
737 for( i = i_command_start ; i < i_command ; i++ )
739 if( !strcmp( ppsz_command[i], "enabled" ) ||
740 !strcmp( ppsz_command[i], "disabled" ) )
742 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
744 else if( i + 1 >= i_command &&
745 !strcmp( ppsz_command[i], "mux") )
747 if( p_media->i_type != VOD_TYPE )
749 p_message = vlm_MessageNew( ppsz_command[0],
750 "mux only available for broadcast" );
754 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
759 else if( !strcmp( ppsz_command[i], "loop" ) ||
760 !strcmp( ppsz_command[i], "unloop" ) )
762 if( p_media->i_type != BROADCAST_TYPE )
764 p_message = vlm_MessageNew( ppsz_command[0],
765 "loop only available for broadcast" );
769 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
774 if( i + 1 >= i_command &&
775 !strcmp(ppsz_command[0], "new") )
777 vlm_MediaDelete( p_vlm, p_media, NULL );
779 vlm_MessageNew( ppsz_command[0],
780 "Wrong properties syntax" );
783 else if( i + 1 >= i_command )
786 vlm_MessageNew( ppsz_command[0],
787 "Wrong properties syntax" );
791 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
798 p_message = vlm_MessageNew( ppsz_command[0], NULL );
803 for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
804 FREE( ppsz_command );
805 *pp_message = p_message;
810 p_message = vlm_MessageNew( ppsz_command[0], "Wrong command syntax" );
813 for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
814 FREE( ppsz_command );
815 *pp_message = p_message;
820 static vlm_media_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
824 for( i = 0; i < vlm->i_media; i++ )
826 if( strcmp( psz_name, vlm->media[i]->psz_name ) == 0 )
828 return vlm->media[i];
835 /*****************************************************************************
837 *****************************************************************************/
838 static vlm_media_instance_t *
839 vlm_MediaInstanceSearch( vlm_t *vlm, vlm_media_t *media,
840 const char *psz_name )
844 for( i = 0; i < media->i_instance; i++ )
846 if( ( !psz_name && !media->instance[i]->psz_name ) ||
847 ( psz_name && media->instance[i]->psz_name &&
848 !strcmp( psz_name, media->instance[i]->psz_name ) ) )
850 return media->instance[i];
857 vlm_media_t *vlm_MediaNew( vlm_t *vlm, const char *psz_name, int i_type )
859 vlm_media_t *media = malloc( sizeof( vlm_media_t ) );
863 msg_Err( vlm, "out of memory" );
867 /* Check if we need to load the VOD server */
868 if( i_type == VOD_TYPE && !vlm->i_vod )
870 vlm->vod = vlc_object_create( vlm, VLC_OBJECT_VOD );
871 vlc_object_attach( vlm->vod, vlm );
872 vlm->vod->p_module = module_Need( vlm->vod, "vod server", 0, 0 );
873 if( !vlm->vod->p_module )
875 msg_Err( vlm, "cannot find vod server" );
876 vlc_object_detach( vlm->vod );
877 vlc_object_destroy( vlm->vod );
883 vlm->vod->p_data = vlm;
884 vlm->vod->pf_media_control = vlm_MediaVodControl;
887 if( i_type == VOD_TYPE ) vlm->i_vod++;
889 media->psz_name = strdup( psz_name );
890 media->b_enabled = VLC_FALSE;
891 media->b_loop = VLC_FALSE;
892 media->vod_media = NULL;
893 media->psz_vod_output = NULL;
894 media->psz_mux = NULL;
897 media->psz_output = NULL;
899 media->option = NULL;
900 media->i_type = i_type;
901 media->i_instance = 0;
902 media->instance = NULL;
904 vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
906 TAB_APPEND( vlm->i_media, vlm->media, media );
911 /* for now, simple delete. After, del with options (last arg) */
912 void vlm_MediaDelete( vlm_t *vlm, vlm_media_t *media, const char *psz_name )
914 if( media == NULL ) return;
916 while( media->i_instance )
918 vlm_media_instance_t *p_instance = media->instance[0];
919 vlm_MediaControl( vlm, media, p_instance->psz_name, "stop", 0 );
922 TAB_REMOVE( vlm->i_media, vlm->media, media );
924 if( media->i_type == VOD_TYPE )
926 vlm_MediaSetup( vlm, media, "disabled", 0 );
930 /* Check if we need to unload the VOD server */
931 if( media->i_type == VOD_TYPE && !vlm->i_vod )
933 module_Unneed( vlm->vod, vlm->vod->p_module );
934 vlc_object_detach( vlm->vod );
935 vlc_object_destroy( vlm->vod );
939 if( vlm->i_media == 0 && vlm->media ) free( vlm->media );
941 free( media->psz_name );
943 while( media->i_input-- ) free( media->input[media->i_input] );
944 if( media->input ) free( media->input );
946 if( media->psz_output ) free( media->psz_output );
947 if( media->psz_mux ) free( media->psz_mux );
949 while( media->i_option-- ) free( media->option[media->i_option] );
950 if( media->option ) free( media->option );
952 vlc_input_item_Clean( &media->item );
957 int vlm_MediaSetup( vlm_t *vlm, vlm_media_t *media, const char *psz_cmd,
958 const char *psz_value )
960 if( !psz_cmd) return VLC_EGENERIC;
962 if( !strcmp( psz_cmd, "loop" ) )
964 media->b_loop = VLC_TRUE;
966 else if( !strcmp( psz_cmd, "unloop" ) )
968 media->b_loop = VLC_FALSE;
970 else if( !strcmp( psz_cmd, "enabled" ) )
972 media->b_enabled = VLC_TRUE;
974 else if( !strcmp( psz_cmd, "disabled" ) )
976 media->b_enabled = VLC_FALSE;
978 else if( !strcmp( psz_cmd, "mux" ) )
980 if( media->psz_mux ) free( media->psz_mux );
981 media->psz_mux = NULL;
982 if( psz_value ) media->psz_mux = strdup( psz_value );
984 else if( !strcmp( psz_cmd, "input" ) )
988 if( psz_value != NULL && strlen(psz_value) > 1 &&
989 ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
990 ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
991 psz_value[ strlen(psz_value) - 1 ] == '\"' ) )
993 input = malloc( strlen(psz_value) - 1 );
995 memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
996 input[ strlen(psz_value) - 2 ] = '\0';
1000 input = strdup( psz_value );
1003 TAB_APPEND( media->i_input, media->input, input );
1005 else if( !strcmp( psz_cmd, "inputdel" ) && !strcmp( psz_value, "all" ) )
1007 while( media->i_input > 0 )
1009 TAB_REMOVE( media->i_input, media->input, media->input[0] );
1012 else if( !strcmp( psz_cmd, "inputdel" ) )
1017 if( psz_value != NULL && strlen(psz_value) > 1 &&
1018 ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
1019 ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
1020 psz_value[ strlen(psz_value) - 1 ] == '\"' ) )
1022 input = malloc( strlen(psz_value) - 1 );
1024 memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
1025 input[ strlen(psz_value) - 2 ] = '\0';
1029 input = strdup( psz_value );
1032 for( i = 0; i < media->i_input; i++ )
1034 if( !strcmp( input, media->input[i] ) )
1036 TAB_REMOVE( media->i_input, media->input, media->input[i] );
1041 else if( !strcmp( psz_cmd, "inputdeln" ) )
1043 int index = atoi( psz_value );
1044 if( index > 0 && index <= media->i_input )
1046 TAB_REMOVE( media->i_input, media->input, media->input[index-1] );
1049 else if( !strcmp( psz_cmd, "output" ) )
1051 if( media->psz_output != NULL )
1053 free( media->psz_output );
1055 media->psz_output = strdup( psz_value );
1057 else if( !strcmp( psz_cmd, "option" ) )
1060 psz_option = strdup( psz_value );
1062 TAB_APPEND( media->i_option, media->option, psz_option );
1066 return VLC_EGENERIC;
1069 /* Check if we need to create/delete a vod media */
1070 if( media->i_type == VOD_TYPE )
1072 if( !media->b_enabled && media->vod_media )
1074 vlm->vod->pf_media_del( vlm->vod, media->vod_media );
1075 media->vod_media = 0;
1077 else if( media->b_enabled && !media->vod_media && media->i_input )
1079 /* Pre-parse the input */
1080 input_thread_t *p_input;
1085 vlc_input_item_Clean( &media->item );
1086 vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
1088 if( media->psz_output )
1089 asprintf( &psz_output, "%s:description", media->psz_output );
1091 asprintf( &psz_output, "#description" );
1093 media->item.psz_uri = strdup( media->input[0] );
1094 media->item.ppsz_options = malloc( sizeof( char* ) );
1095 asprintf( &media->item.ppsz_options[0], "sout=%s", psz_output);
1096 media->item.i_options = 1;
1097 for( i = 0; i < media->i_option; i++ )
1099 media->item.i_options++;
1100 media->item.ppsz_options =
1101 realloc( media->item.ppsz_options,
1102 media->item.i_options * sizeof( char* ) );
1103 media->item.ppsz_options[ media->item.i_options - 1 ] =
1104 strdup( media->option[i] );
1107 asprintf( &psz_header, _("Media: %s"), media->psz_name );
1109 if( (p_input = input_CreateThread2( vlm, &media->item, psz_header
1112 while( !p_input->b_eof && !p_input->b_error ) msleep( 100000 );
1114 input_StopThread( p_input );
1115 input_DestroyThread( p_input );
1116 vlc_object_detach( p_input );
1117 vlc_object_destroy( p_input );
1122 if( media->psz_mux )
1125 es_format_t es, *p_es = &es;
1128 sprintf( fourcc, "%4.4s", media->psz_mux );
1129 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
1130 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
1135 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
1138 vlm->vod->pf_media_new( vlm->vod, media->psz_name, &item );
1143 vlm->vod->pf_media_new( vlm->vod, media->psz_name,
1151 int vlm_MediaControl( vlm_t *vlm, vlm_media_t *media, const char *psz_id,
1152 const char *psz_command, const char *psz_args )
1154 vlm_media_instance_t *p_instance;
1158 p_instance = vlm_MediaInstanceSearch( vlm, media, psz_id );
1160 if( !strcmp( psz_command, "play" ) )
1162 if( !media->b_enabled || media->i_input == 0 ) return 0;
1166 p_instance = malloc( sizeof(vlm_media_instance_t) );
1167 if( !p_instance ) return VLC_EGENERIC;
1168 memset( p_instance, 0, sizeof(vlm_media_instance_t) );
1169 vlc_input_item_Init( VLC_OBJECT(vlm), &p_instance->item );
1170 p_instance->p_input = NULL;
1172 if( media->psz_output != NULL || media->psz_vod_output != NULL )
1174 p_instance->item.ppsz_options = malloc( sizeof( char* ) );
1175 asprintf( &p_instance->item.ppsz_options[0], "sout=%s%s%s",
1176 media->psz_output ? media->psz_output : "",
1177 (media->psz_output && media->psz_vod_output) ?
1178 ":" : media->psz_vod_output ? "#" : "",
1179 media->psz_vod_output ? media->psz_vod_output : "" );
1180 p_instance->item.i_options = 1;
1183 for( i = 0; i < media->i_option; i++ )
1185 p_instance->item.i_options++;
1186 p_instance->item.ppsz_options =
1187 realloc( p_instance->item.ppsz_options,
1188 p_instance->item.i_options * sizeof( char* ) );
1189 p_instance->item.ppsz_options[p_instance->item.i_options - 1] =
1190 strdup( media->option[i] );
1193 p_instance->psz_name = psz_id ? strdup( psz_id ) : NULL;
1194 TAB_APPEND( media->i_instance, media->instance, p_instance );
1197 if( psz_args && sscanf(psz_args, "%d", &i) == 1 && i < media->i_input )
1199 p_instance->i_index = i;
1202 if( p_instance->item.psz_uri ) free( p_instance->item.psz_uri );
1203 p_instance->item.psz_uri =
1204 strdup( media->input[p_instance->i_index] );
1206 if( p_instance->p_input )
1208 input_StopThread( p_instance->p_input );
1209 input_DestroyThread( p_instance->p_input );
1210 vlc_object_detach( p_instance->p_input );
1211 vlc_object_destroy( p_instance->p_input );
1214 asprintf( &psz_header, _("Media: %s"), media->psz_name );
1215 p_instance->p_input = input_CreateThread2( vlm, &p_instance->item,
1217 if( !p_instance->p_input )
1219 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1220 vlc_input_item_Clean( &p_instance->item );
1221 if( p_instance->psz_name ) free( p_instance->psz_name );
1228 if( !p_instance ) return VLC_EGENERIC;
1230 if( !strcmp( psz_command, "seek" ) )
1235 if( psz_args && sscanf( psz_args, "%f", &f_percentage ) == 1 )
1237 val.f_float = f_percentage / 100.0 ;
1238 var_Set( p_instance->p_input, "position", val );
1242 else if( !strcmp( psz_command, "stop" ) )
1244 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1246 if( p_instance->p_input )
1248 input_StopThread( p_instance->p_input );
1249 input_DestroyThread( p_instance->p_input );
1250 vlc_object_detach( p_instance->p_input );
1251 vlc_object_destroy( p_instance->p_input );
1254 vlc_input_item_Clean( &p_instance->item );
1255 if( p_instance->psz_name ) free( p_instance->psz_name );
1260 else if( !strcmp( psz_command, "pause" ) )
1264 if( !p_instance->p_input ) return VLC_SUCCESS;
1266 var_Get( p_instance->p_input, "state", &val );
1268 if( val.i_int == PAUSE_S ) val.i_int = PLAYING_S;
1269 else val.i_int = PAUSE_S;
1270 var_Set( p_instance->p_input, "state", val );
1275 return VLC_EGENERIC;
1278 /*****************************************************************************
1280 *****************************************************************************/
1281 static int64_t vlm_Date()
1286 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1292 vlm_schedule_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
1294 vlm_schedule_t *p_sched = malloc( sizeof( vlm_schedule_t ) );
1306 p_sched->psz_name = strdup( psz_name );
1307 p_sched->b_enabled = VLC_FALSE;
1308 p_sched->i_command = 0;
1309 p_sched->command = NULL;
1310 p_sched->i_date = 0;
1311 p_sched->i_period = 0;
1312 p_sched->i_repeat = -1;
1314 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1319 /* for now, simple delete. After, del with options (last arg) */
1320 void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_t *sched,
1321 const char *psz_name )
1323 if( sched == NULL ) return;
1325 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1327 if( vlm->i_schedule == 0 && vlm->schedule ) free( vlm->schedule );
1328 free( sched->psz_name );
1329 while( sched->i_command )
1331 char *psz_cmd = sched->command[0];
1332 TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1338 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1342 for( i = 0; i < vlm->i_schedule; i++ )
1344 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1346 return vlm->schedule[i];
1353 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1354 int vlm_ScheduleSetup( vlm_schedule_t *schedule, const char *psz_cmd,
1355 const char *psz_value )
1357 if( !strcmp( psz_cmd, "enabled" ) )
1359 schedule->b_enabled = VLC_TRUE;
1361 else if( !strcmp( psz_cmd, "disabled" ) )
1363 schedule->b_enabled = VLC_FALSE;
1365 #if !defined( UNDER_CE )
1366 else if( !strcmp( psz_cmd, "date" ) )
1372 time.tm_sec = 0; /* seconds */
1373 time.tm_min = 0; /* minutes */
1374 time.tm_hour = 0; /* hours */
1375 time.tm_mday = 0; /* day of the month */
1376 time.tm_mon = 0; /* month */
1377 time.tm_year = 0; /* year */
1378 time.tm_wday = 0; /* day of the week */
1379 time.tm_yday = 0; /* day in the year */
1380 time.tm_isdst = -1; /* daylight saving time */
1382 /* date should be year/month/day-hour:minutes:seconds */
1383 p = strchr( psz_value, '-' );
1385 if( !strcmp( psz_value, "now" ) )
1387 schedule->i_date = 0;
1389 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 */
1397 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1415 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1421 time.tm_mon = i - 1;
1425 time.tm_year = i - 1900;
1426 time.tm_mon = j - 1;
1433 date = mktime( &time );
1434 schedule->i_date = ((mtime_t) date) * 1000000;
1437 else if( !strcmp( psz_cmd, "period" ) )
1441 const char *psz_time = NULL, *psz_date = NULL;
1445 /* First, if date or period are modified, repeat should be equal to -1 */
1446 schedule->i_repeat = -1;
1448 time.tm_sec = 0; /* seconds */
1449 time.tm_min = 0; /* minutes */
1450 time.tm_hour = 0; /* hours */
1451 time.tm_mday = 0; /* day of the month */
1452 time.tm_mon = 0; /* month */
1453 time.tm_year = 0; /* year */
1454 time.tm_wday = 0; /* day of the week */
1455 time.tm_yday = 0; /* day in the year */
1456 time.tm_isdst = -1; /* daylight saving time */
1458 /* date should be year/month/day-hour:minutes:seconds */
1459 p = strchr( psz_value, '-' );
1462 psz_date = psz_value;
1467 psz_time = psz_value;
1471 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1490 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1509 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1510 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1511 schedule->i_period = ((mtime_t) date) * 1000000;
1513 #endif /* UNDER_CE */
1514 else if( !strcmp( psz_cmd, "repeat" ) )
1518 if( sscanf( psz_value, "%d", &i ) == 1 )
1520 schedule->i_repeat = i;
1527 else if( !strcmp( psz_cmd, "append" ) )
1529 char *command = strdup( psz_value );
1531 TAB_APPEND( schedule->i_command, schedule->command, command );
1540 /*****************************************************************************
1541 * Message handling functions
1542 *****************************************************************************/
1543 static vlm_message_t *vlm_MessageNew( char *psz_name,
1544 const char *psz_format, ... )
1546 vlm_message_t *p_message;
1549 if( !psz_name ) return 0;
1551 p_message = malloc( sizeof(vlm_message_t) );
1557 p_message->psz_value = 0;
1561 va_start( args, psz_format );
1562 if( vasprintf( &p_message->psz_value, psz_format, args ) < 0 )
1571 p_message->psz_name = strdup( psz_name );
1572 p_message->i_child = 0;
1573 p_message->child = NULL;
1578 void vlm_MessageDelete( vlm_message_t *p_message )
1580 if( p_message->psz_name ) free( p_message->psz_name );
1581 if( p_message->psz_value ) free( p_message->psz_value );
1582 while( p_message->i_child-- )
1583 vlm_MessageDelete( p_message->child[p_message->i_child] );
1584 if( p_message->child ) free( p_message->child );
1589 static vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1590 vlm_message_t *p_child )
1592 if( p_message == NULL ) return NULL;
1596 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1602 /*****************************************************************************
1603 * Misc utility functions
1604 *****************************************************************************/
1605 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_t *media,
1606 vlm_schedule_t *schedule, char *psz_filter )
1612 vlm_message_t *msg_media;
1613 vlm_message_t *msg_child;
1615 msg = vlm_MessageNew( "show", NULL );
1616 msg_media = vlm_MessageAdd( msg, vlm_MessageNew( media->psz_name, 0 ));
1618 vlm_MessageAdd( msg_media,
1619 vlm_MessageNew( "type", media->i_type == VOD_TYPE ?
1620 "vod" : "broadcast" ) );
1621 vlm_MessageAdd( msg_media,
1622 vlm_MessageNew( "enabled", media->b_enabled ?
1625 vlm_MessageAdd( msg_media,
1626 vlm_MessageNew( "loop", media->b_loop ?
1629 if( media->i_type == VOD_TYPE && media->psz_mux )
1630 vlm_MessageAdd( msg_media,
1631 vlm_MessageNew( "mux", media->psz_mux ) );
1633 msg_child = vlm_MessageAdd( msg_media,
1634 vlm_MessageNew( "inputs", NULL ) );
1636 for( i = 0; i < media->i_input; i++ )
1638 vlm_MessageAdd( msg_child,
1639 vlm_MessageNew( media->input[i], NULL ) );
1642 vlm_MessageAdd( msg_media,
1643 vlm_MessageNew( "output", media->psz_output ?
1644 media->psz_output : "" ) );
1646 msg_child = vlm_MessageAdd( msg_media, vlm_MessageNew( "options", 0 ));
1648 for( i = 0; i < media->i_option; i++ )
1650 vlm_MessageAdd( msg_child, vlm_MessageNew( media->option[i], 0 ) );
1653 msg_child = vlm_MessageAdd( msg_media,
1654 vlm_MessageNew( "instances", NULL ) );
1656 for( i = 0; i < media->i_instance; i++ )
1658 vlm_media_instance_t *p_instance = media->instance[i];
1660 vlm_message_t *msg_instance;
1663 if( !p_instance->p_input ) val.i_int = END_S;
1664 else var_Get( p_instance->p_input, "state", &val );
1666 msg_instance = vlm_MessageNew( "instance" , NULL );
1667 vlm_MessageAdd( msg_instance, vlm_MessageNew( "name" , p_instance->psz_name ? p_instance->psz_name : "default" ) );
1668 vlm_MessageAdd( msg_instance, vlm_MessageNew( "state",
1669 val.i_int == PLAYING_S ? "playing" :
1670 val.i_int == PAUSE_S ? "paused" :
1672 #define APPEND_INPUT_INFO( a, format, type ) \
1673 asprintf( &psz_tmp, format, \
1674 var_Get ## type( p_instance->p_input, a ) ); \
1675 vlm_MessageAdd( msg_instance, vlm_MessageNew( a, psz_tmp ) ); \
1677 APPEND_INPUT_INFO( "position", "%f", Float );
1678 APPEND_INPUT_INFO( "time", "%d", Integer );
1679 APPEND_INPUT_INFO( "length", "%d", Integer );
1680 APPEND_INPUT_INFO( "rate", "%d", Integer );
1681 APPEND_INPUT_INFO( "title", "%d", Integer );
1682 APPEND_INPUT_INFO( "chapter", "%d", Integer );
1683 APPEND_INPUT_INFO( "seekable", "%d", Bool );
1684 #undef APPEND_INPUT_INFO
1685 asprintf( &psz_tmp, "%d", p_instance->i_index + 1 );
1686 vlm_MessageAdd( msg_instance, vlm_MessageNew( "playlistindex", psz_tmp ) );
1688 vlm_MessageAdd( msg_child, msg_instance );
1695 else if( schedule != NULL )
1699 vlm_message_t *msg_schedule;
1700 vlm_message_t *msg_child;
1703 msg = vlm_MessageNew( "show", NULL );
1705 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, 0 ) );
1707 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1709 vlm_MessageAdd( msg_schedule,
1710 vlm_MessageNew( "enabled", schedule->b_enabled ?
1713 #if !defined( UNDER_CE )
1714 if( schedule->i_date != 0 )
1717 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1720 #ifdef HAVE_LOCALTIME_R
1721 localtime_r( &i_time, &date);
1723 struct tm *p_date = localtime( &i_time );
1727 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1728 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1729 date.tm_hour, date.tm_min, date.tm_sec );
1731 vlm_MessageAdd( msg_schedule,
1732 vlm_MessageNew( "date", psz_date ) );
1737 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1740 if( schedule->i_period != 0 )
1742 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1745 date.tm_sec = (int)( i_time % 60 );
1746 i_time = i_time / 60;
1747 date.tm_min = (int)( i_time % 60 );
1748 i_time = i_time / 60;
1749 date.tm_hour = (int)( i_time % 24 );
1750 i_time = i_time / 24;
1751 date.tm_mday = (int)( i_time % 30 );
1752 i_time = i_time / 30;
1753 /* okay, okay, months are not always 30 days long */
1754 date.tm_mon = (int)( i_time % 12 );
1755 i_time = i_time / 12;
1756 date.tm_year = (int)i_time;
1758 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1759 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1761 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1765 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1767 #endif /* UNDER_CE */
1769 sprintf( buffer, "%d", schedule->i_repeat );
1770 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1773 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", 0) );
1775 for( i = 0; i < schedule->i_command; i++ )
1777 vlm_MessageAdd( msg_child,
1778 vlm_MessageNew( schedule->command[i], NULL ) );
1785 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1789 vlm_message_t *msg_child;
1790 int i_vod = 0, i_broadcast = 0;
1793 for( i = 0; i < vlm->i_media; i++ )
1795 if( vlm->media[i]->i_type == VOD_TYPE )
1801 asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1803 msg = vlm_MessageNew( "show", NULL );
1804 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "media", psz_count ) );
1807 for( i = 0; i < vlm->i_media; i++ )
1809 vlm_media_t *m = vlm->media[i];
1810 vlm_message_t *msg_media, *msg_instance;
1812 msg_media = vlm_MessageAdd( msg_child,
1813 vlm_MessageNew( m->psz_name, 0 ) );
1815 vlm_MessageAdd( msg_media,
1816 vlm_MessageNew( "type", m->i_type == VOD_TYPE ?
1817 "vod" : "broadcast" ) );
1819 vlm_MessageAdd( msg_media,
1820 vlm_MessageNew( "enabled", m->b_enabled ?
1823 if( m->i_type == VOD_TYPE && m->psz_mux )
1824 vlm_MessageAdd( msg_media,
1825 vlm_MessageNew( "mux", m->psz_mux ) );
1827 msg_instance = vlm_MessageAdd( msg_media,
1828 vlm_MessageNew( "instances", 0 ) );
1830 for( j = 0; j < m->i_instance; j++ )
1832 vlm_media_instance_t *p_instance = m->instance[j];
1835 if( !p_instance->p_input ) val.i_int = END_S;
1836 else var_Get( p_instance->p_input, "state", &val );
1838 vlm_MessageAdd( msg_instance,
1839 vlm_MessageNew( p_instance->psz_name ?
1840 p_instance->psz_name : "default",
1841 val.i_int == PLAYING_S ? "playing" :
1842 val.i_int == PAUSE_S ? "paused" :
1850 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1854 vlm_message_t *msg_child;
1856 msg = vlm_MessageNew( "show", NULL );
1857 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", NULL ) );
1859 for( i = 0; i < vlm->i_schedule; i++ )
1861 vlm_schedule_t *s = vlm->schedule[i];
1862 vlm_message_t *msg_schedule;
1863 mtime_t i_time, i_next_date;
1865 msg_schedule = vlm_MessageAdd( msg_child,
1866 vlm_MessageNew( s->psz_name, 0 ) );
1867 vlm_MessageAdd( msg_schedule,
1868 vlm_MessageNew( "enabled", s->b_enabled ?
1871 /* calculate next date */
1872 i_time = vlm_Date();
1873 i_next_date = s->i_date;
1875 if( s->i_period != 0 )
1878 while( s->i_date + j * s->i_period <= i_time &&
1884 i_next_date = s->i_date + j * s->i_period;
1887 if( i_next_date > i_time )
1889 time_t i_date = (time_t) (i_next_date / 1000000) ;
1891 #if !defined( UNDER_CE )
1894 ctime_r( &i_date, psz_date );
1896 char *psz_date = ctime( &i_date );
1899 vlm_MessageAdd( msg_schedule,
1900 vlm_MessageNew( "next launch", psz_date ) );
1908 else if( psz_filter == NULL && media == NULL && schedule == NULL )
1910 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1911 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1913 vlm_MessageAdd( show1, show2->child[0] );
1915 /* We must destroy the parent node "show" of show2
1916 * and not the children */
1917 free( show2->psz_name );
1925 return vlm_MessageNew( "show", NULL );
1929 static vlm_message_t *vlm_Help( vlm_t *vlm, char *psz_filter )
1931 vlm_message_t *message, *message_child;
1933 #define MessageAdd( a ) \
1934 vlm_MessageAdd( message, vlm_MessageNew( a, NULL ) );
1935 #define MessageAddChild( a ) \
1936 vlm_MessageAdd( message_child, vlm_MessageNew( a, NULL ) );
1938 if( psz_filter == NULL )
1940 message = vlm_MessageNew( "help", NULL );
1942 message_child = MessageAdd( "Commands Syntax:" );
1943 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
1944 MessageAddChild( "setup (name) (properties)" );
1945 MessageAddChild( "show [(name)|media|schedule]" );
1946 MessageAddChild( "del (name)|all|media|schedule" );
1947 MessageAddChild( "control (name) [instance_name] (command)" );
1948 MessageAddChild( "save (config_file)" );
1949 MessageAddChild( "export" );
1950 MessageAddChild( "load (config_file)" );
1952 message_child = MessageAdd( "Media Proprieties Syntax:" );
1953 MessageAddChild( "input (input_name)" );
1954 MessageAddChild( "inputdel (input_name)|all" );
1955 MessageAddChild( "inputdeln input_number" );
1956 MessageAddChild( "output (output_name)" );
1957 MessageAddChild( "option (option_name)[=value]" );
1958 MessageAddChild( "enabled|disabled" );
1959 MessageAddChild( "loop|unloop (broadcast only)" );
1960 MessageAddChild( "mux (mux_name)" );
1962 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
1963 MessageAddChild( "enabled|disabled" );
1964 MessageAddChild( "append (command_until_rest_of_the_line)" );
1965 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
1967 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
1968 "(days)-(hours):(minutes):(seconds)" );
1969 MessageAddChild( "repeat (number_of_repetitions)" );
1971 message_child = MessageAdd( "Control Commands Syntax:" );
1972 MessageAddChild( "play" );
1973 MessageAddChild( "pause" );
1974 MessageAddChild( "stop" );
1975 MessageAddChild( "seek (percentage)" );
1980 return vlm_MessageNew( "help", NULL );
1983 /*****************************************************************************
1984 * Config handling functions
1985 *****************************************************************************/
1986 static int Load( vlm_t *vlm, char *file )
1991 while( *pf != '\0' )
1993 vlm_message_t *message = NULL;
1996 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
2001 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
2005 if( pf[i_end] == '\n' ) i_end++;
2008 if( *pf && ExecuteCommand( vlm, pf, &message ) )
2012 if( message->psz_value )
2013 msg_Err( vlm, "Load error on line %d: %s: %s",
2014 i_line, message->psz_name, message->psz_value );
2015 vlm_MessageDelete( message );
2019 if( message ) vlm_MessageDelete( message );
2028 static char *Save( vlm_t *vlm )
2031 char psz_header[] = "\n"
2032 "# VLC media player VLM command batch\n"
2033 "# http://www.videolan.org/vlc/\n\n" ;
2036 int i_length = strlen( psz_header );
2038 for( i = 0; i < vlm->i_media; i++ )
2040 vlm_media_t *media = vlm->media[i];
2042 if( media->i_type == VOD_TYPE )
2044 i_length += strlen( "new vod " ) + strlen(media->psz_name);
2048 i_length += strlen( "new broadcast " ) + strlen(media->psz_name);
2051 if( media->b_enabled == VLC_TRUE )
2053 i_length += strlen( "enabled" );
2057 i_length += strlen( "disabled" );
2060 if( media->b_loop == VLC_TRUE )
2062 i_length += strlen( " loop\n" );
2066 i_length += strlen( "\n" );
2069 for( j = 0; j < media->i_input; j++ )
2071 i_length += strlen( "setup input \"\"\n" ) +
2072 strlen( media->psz_name ) + strlen( media->input[j] );
2075 if( media->psz_output != NULL )
2077 i_length += strlen(media->psz_name) + strlen(media->psz_output) +
2078 strlen( "setup output \n" );
2081 for( j=0 ; j < media->i_option ; j++ )
2083 i_length += strlen(media->psz_name) + strlen(media->option[j]) +
2084 strlen("setup option \n");
2088 for( i = 0; i < vlm->i_schedule; i++ )
2090 vlm_schedule_t *schedule = vlm->schedule[i];
2092 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
2094 if( schedule->b_enabled == VLC_TRUE )
2096 i_length += strlen( "date //-:: enabled\n" ) + 14;
2100 i_length += strlen( "date //-:: disabled\n" ) + 14;
2104 if( schedule->i_period != 0 )
2106 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
2107 strlen( "period //-::\n" ) + 14;
2110 if( schedule->i_repeat >= 0 )
2114 sprintf( buffer, "%d", schedule->i_repeat );
2115 i_length += strlen( "setup repeat \n" ) +
2116 strlen( schedule->psz_name ) + strlen( buffer );
2123 for( j = 0; j < schedule->i_command; j++ )
2125 i_length += strlen( "setup append \n" ) +
2126 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
2131 /* Don't forget the '\0' */
2133 /* now we have the length of save */
2135 p = save = malloc( i_length );
2138 p += sprintf( p, "%s", psz_header );
2140 /* finally we can write in it */
2141 for( i = 0; i < vlm->i_media; i++ )
2143 vlm_media_t *media = vlm->media[i];
2145 if( media->i_type == VOD_TYPE )
2147 p += sprintf( p, "new %s vod ", media->psz_name);
2151 p += sprintf( p, "new %s broadcast ", media->psz_name);
2154 if( media->b_enabled == VLC_TRUE )
2156 p += sprintf( p, "enabled" );
2160 p += sprintf( p, "disabled" );
2163 if( media->b_loop == VLC_TRUE )
2165 p += sprintf( p, " loop\n" );
2169 p += sprintf( p, "\n" );
2172 for( j = 0; j < media->i_input; j++ )
2174 p += sprintf( p, "setup %s input \"%s\"\n", media->psz_name,
2178 if( media->psz_output != NULL )
2180 p += sprintf( p, "setup %s output %s\n", media->psz_name,
2181 media->psz_output );
2184 for( j = 0; j < media->i_option; j++ )
2186 p += sprintf( p, "setup %s option %s\n", media->psz_name,
2191 /* and now, the schedule scripts */
2192 #if !defined( UNDER_CE )
2193 for( i = 0; i < vlm->i_schedule; i++ )
2195 vlm_schedule_t *schedule = vlm->schedule[i];
2197 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
2199 #ifdef HAVE_LOCALTIME_R
2200 localtime_r( &i_time, &date);
2202 struct tm *p_date = localtime( &i_time );
2206 p += sprintf( p, "new %s schedule ", schedule->psz_name);
2208 if( schedule->b_enabled == VLC_TRUE )
2210 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
2211 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2212 date.tm_hour, date.tm_min, date.tm_sec );
2216 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
2217 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2218 date.tm_hour, date.tm_min, date.tm_sec);
2221 if( schedule->i_period != 0 )
2223 p += sprintf( p, "setup %s ", schedule->psz_name );
2225 i_time = (time_t) ( schedule->i_period / 1000000 );
2227 date.tm_sec = (int)( i_time % 60 );
2228 i_time = i_time / 60;
2229 date.tm_min = (int)( i_time % 60 );
2230 i_time = i_time / 60;
2231 date.tm_hour = (int)( i_time % 24 );
2232 i_time = i_time / 24;
2233 date.tm_mday = (int)( i_time % 30 );
2234 i_time = i_time / 30;
2235 /* okay, okay, months are not always 30 days long */
2236 date.tm_mon = (int)( i_time % 12 );
2237 i_time = i_time / 12;
2238 date.tm_year = (int)i_time;
2240 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
2241 date.tm_year, date.tm_mon, date.tm_mday,
2242 date.tm_hour, date.tm_min, date.tm_sec);
2245 if( schedule->i_repeat >= 0 )
2247 p += sprintf( p, "setup %s repeat %d\n",
2248 schedule->psz_name, schedule->i_repeat );
2252 p += sprintf( p, "\n" );
2255 for( j = 0; j < schedule->i_command; j++ )
2257 p += sprintf( p, "setup %s append %s\n",
2258 schedule->psz_name, schedule->command[j] );
2262 #endif /* UNDER_CE */
2267 /*****************************************************************************
2269 *****************************************************************************/
2270 int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
2271 const char *psz_id, int i_query, va_list args )
2273 vlm_t *vlm = (vlm_t *)p_private;
2274 int i, i_ret = VLC_EGENERIC;
2276 if( !p_private || !p_vod_media ) return VLC_EGENERIC;
2278 vlc_mutex_lock( &vlm->lock );
2281 for( i = 0; i < vlm->i_media; i++ )
2283 if( p_vod_media == vlm->media[i]->vod_media ) break;
2286 if( i == vlm->i_media )
2288 vlc_mutex_unlock( &vlm->lock );
2289 return VLC_EGENERIC;
2294 case VOD_MEDIA_PLAY:
2295 vlm->media[i]->psz_vod_output = (char *)va_arg( args, char * );
2296 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "play", 0 );
2297 vlm->media[i]->psz_vod_output = 0;
2300 case VOD_MEDIA_PAUSE:
2301 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "pause", 0 );
2304 case VOD_MEDIA_STOP:
2305 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "stop", 0 );
2308 case VOD_MEDIA_SEEK:
2310 double f_pos = (double)va_arg( args, double );
2313 sprintf( psz_pos, "%f", f_pos );
2314 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "seek", psz_pos);
2322 vlc_mutex_unlock( &vlm->lock );
2327 /*****************************************************************************
2329 *****************************************************************************/
2330 static int Manage( vlc_object_t* p_object )
2332 vlm_t *vlm = (vlm_t*)p_object;
2334 mtime_t i_lastcheck;
2337 i_lastcheck = vlm_Date();
2341 while( !vlm->b_die )
2343 char **ppsz_scheduled_commands = NULL;
2344 int i_scheduled_commands = 0;
2345 vlc_mutex_lock( &vlm->lock );
2347 /* destroy the inputs that wants to die, and launch the next input */
2348 for( i = 0; i < vlm->i_media; i++ )
2350 vlm_media_t *p_media = vlm->media[i];
2352 for( j = 0; j < p_media->i_instance; j++ )
2354 vlm_media_instance_t *p_instance = p_media->instance[j];
2356 if( !p_instance->p_input ||
2357 ( !p_instance->p_input->b_eof &&
2358 !p_instance->p_input->b_error ) ) continue;
2360 input_StopThread( p_instance->p_input );
2361 input_DestroyThread( p_instance->p_input );
2362 vlc_object_detach( p_instance->p_input );
2363 vlc_object_destroy( p_instance->p_input );
2365 p_instance->i_index++;
2366 if( p_instance->i_index == p_media->i_input &&
2367 p_media->b_loop ) p_instance->i_index = 0;
2369 if( p_instance->i_index < p_media->i_input )
2371 /* FIXME, find a way to select the right instance */
2373 sprintf( buffer, "%d", p_instance->i_index );
2374 vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2379 if( vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2380 "stop", 0 ) == VLC_SUCCESS ) i--;
2386 i_time = vlm_Date();
2388 for( i = 0; i < vlm->i_schedule; i++ )
2390 mtime_t i_real_date = vlm->schedule[i]->i_date;
2392 if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2394 if( vlm->schedule[i]->i_date == 0 ) // now !
2396 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2397 i_real_date = i_time;
2399 else if( vlm->schedule[i]->i_period != 0 )
2402 while( vlm->schedule[i]->i_date + j *
2403 vlm->schedule[i]->i_period <= i_lastcheck &&
2404 ( vlm->schedule[i]->i_repeat > j ||
2405 vlm->schedule[i]->i_repeat == -1 ) )
2410 i_real_date = vlm->schedule[i]->i_date + j *
2411 vlm->schedule[i]->i_period;
2414 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2416 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2418 TAB_APPEND( i_scheduled_commands,
2419 ppsz_scheduled_commands,
2420 strdup(vlm->schedule[i]->command[j] ) );
2425 while( i_scheduled_commands )
2427 vlm_message_t *message = NULL;
2428 char *psz_command = ppsz_scheduled_commands[0];
2429 ExecuteCommand( vlm, psz_command,&message );
2431 /* for now, drop the message */
2432 vlm_MessageDelete( message );
2433 TAB_REMOVE( i_scheduled_commands,
2434 ppsz_scheduled_commands,
2436 free( psz_command );
2439 i_lastcheck = i_time;
2441 vlc_mutex_unlock( &vlm->lock );
2449 #else /* ENABLE_VLM */
2451 /* We just define an empty wrapper */
2452 vlm_t *__vlm_New( vlc_object_t *a )
2454 msg_Err( a, "VideoLAN manager support is disabled" );
2457 void vlm_Delete( vlm_t *a ){}
2458 int vlm_ExecuteCommand( vlm_t *a, char *b, vlm_message_t **c ){ return -1; }
2459 void vlm_MessageDelete( vlm_message_t *a ){}
2460 vlm_media_t *vlm_MediaNew( vlm_t *a, char *b, int c ){ return NULL; }
2461 void vlm_MediaDelete( vlm_t *a, vlm_media_t *b, char *c ){}
2462 int vlm_MediaSetup( vlm_t *a, vlm_media_t *b, char *c, char *d ){ return -1; }
2463 int vlm_MediaControl( vlm_t *a, vlm_media_t *b, char *c, char *d, char *e )
2465 vlm_schedule_t * vlm_ScheduleNew( vlm_t *a, char *b ){ return NULL; }
2466 void vlm_ScheduleDelete( vlm_t *a, vlm_schedule_t *b, char *c ){}
2467 int vlm_ScheduleSetup( vlm_schedule_t *a, char *b, char *c ){ return -1; }
2468 int vlm_MediaVodControl( void *a, vod_media_t *b, char *c, int d, va_list e )
2470 int vlm_Save( vlm_t *a, char *b ){ return -1; }
2471 int vlm_Load( vlm_t *a, char *b ){ return -1; }
2473 #endif /* ENABLE_VLM */