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 *****************************************************************************/
32 #include <stdlib.h> /* malloc(), free() */
33 #include <ctype.h> /* tolower() */
40 # include <time.h> /* ctime() */
41 # include <sys/timeb.h> /* ftime() */
44 #include <vlc_input.h>
45 #include "input_internal.h"
46 #include <vlc_stream.h>
49 #include <vlc_charset.h>
51 /*****************************************************************************
53 *****************************************************************************/
54 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_t *, vlm_schedule_t *, const 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_schedule_t *vlm_ScheduleSearch( vlm_t *, const char * );
61 static char *Save( vlm_t * );
62 static int Load( vlm_t *, char * );
63 static int ExecuteCommand( vlm_t *, const char *, vlm_message_t ** );
64 static int Manage( vlc_object_t * );
66 /*****************************************************************************
68 *****************************************************************************/
69 vlm_t *__vlm_New ( vlc_object_t *p_this )
75 /* to be sure to avoid multiple creation */
76 var_Create( p_this->p_libvlc_global, "vlm_mutex", VLC_VAR_MUTEX );
77 var_Get( p_this->p_libvlc_global, "vlm_mutex", &lockval );
78 vlc_mutex_lock( lockval.p_address );
80 if( !(p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE )) )
82 msg_Info( p_this, "creating VLM" );
83 if( ( p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM ) ) == NULL )
85 vlc_mutex_unlock( lockval.p_address );
89 vlc_mutex_init( p_this->p_libvlc, &p_vlm->lock );
90 TAB_INIT( p_vlm->i_media, p_vlm->media );
91 TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
95 vlc_object_yield( p_vlm );
96 vlc_object_attach( p_vlm, p_this->p_libvlc );
98 vlc_mutex_unlock( lockval.p_address );
100 if( vlc_thread_create( p_vlm, "vlm thread",
101 Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
103 vlc_mutex_destroy( &p_vlm->lock );
104 vlc_object_destroy( p_vlm );
108 /* Try loading the vlm conf file given by --vlm-conf */
109 psz_vlmconf = config_GetPsz( p_vlm, "vlm-conf" );
111 if( psz_vlmconf && *psz_vlmconf )
113 vlm_message_t *p_message = NULL;
114 char *psz_buffer = NULL;
116 msg_Dbg( p_this, "loading VLM configuration" );
117 asprintf(&psz_buffer, "load %s", psz_vlmconf );
120 msg_Dbg( p_this, psz_buffer);
121 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) ){
122 msg_Warn( p_this, "error while loading the configuration file" );
124 vlm_MessageDelete(p_message);
133 /*****************************************************************************
135 *****************************************************************************/
136 void vlm_Delete( vlm_t *p_vlm )
140 var_Get( p_vlm->p_libvlc_global, "vlm_mutex", &lockval );
141 vlc_mutex_lock( lockval.p_address );
143 vlc_object_release( p_vlm );
145 if( p_vlm->i_refcount > 0 )
147 vlc_mutex_unlock( lockval.p_address );
151 p_vlm->b_die = VLC_TRUE;
152 vlc_thread_join( p_vlm );
154 vlc_mutex_destroy( &p_vlm->lock );
156 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0], NULL );
157 FREENULL( p_vlm->media );
159 while( p_vlm->i_schedule ) vlm_ScheduleDelete( p_vlm,
160 p_vlm->schedule[0], NULL );
161 FREENULL( p_vlm->schedule );
163 vlc_object_detach( p_vlm );
164 vlc_object_destroy( p_vlm );
165 vlc_mutex_unlock( lockval.p_address );
168 /*****************************************************************************
169 * vlm_ExecuteCommand:
170 *****************************************************************************/
171 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
172 vlm_message_t **pp_message)
176 vlc_mutex_lock( &p_vlm->lock );
177 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
178 vlc_mutex_unlock( &p_vlm->lock );
183 /*****************************************************************************
185 *****************************************************************************/
186 int vlm_Save( vlm_t *p_vlm, const char *psz_file )
191 if( !p_vlm || !psz_file ) return 1;
193 file = utf8_fopen( psz_file, "wt" );
194 if( file == NULL ) return 1;
196 psz_save = Save( p_vlm );
197 if( psz_save == NULL )
202 fwrite( psz_save, strlen( psz_save ), 1, file );
209 /*****************************************************************************
211 *****************************************************************************/
212 int vlm_Load( vlm_t *p_vlm, const char *psz_file )
218 if( !p_vlm || !psz_file ) return 1;
220 p_stream = stream_UrlNew( p_vlm, psz_file );
221 if( p_stream == NULL ) return 1;
223 if( stream_Seek( p_stream, 0 ) != 0 )
225 stream_Delete( p_stream );
229 i_size = stream_Size( p_stream );
231 psz_buffer = malloc( i_size + 1 );
234 stream_Delete( p_stream );
238 stream_Read( p_stream, psz_buffer, i_size );
239 psz_buffer[ i_size ] = '\0';
241 stream_Delete( p_stream );
243 if( Load( p_vlm, psz_buffer ) )
255 static const char quotes[] = "\"'";
257 * FindCommandEnd: look for the end of a possibly quoted string
258 * @return NULL on mal-formatted string,
259 * pointer past the last character otherwise.
261 static const char *FindCommandEnd (const char *psz_sent)
265 while ((c = *psz_sent) != '\0')
269 if (strchr(quotes,c)) // opening quote
272 if (isspace(c)) // non-escaped space
277 psz_sent++; // skip escaped character
278 if (*psz_sent == '\0')
284 if (c == quote) // non-escaped matching quote
287 if ((quote == '"') && (c == '\\'))
289 psz_sent++; // skip escaped character
290 if (*psz_sent == '\0')
291 return NULL; // error, closing quote missing
298 // error (NULL) if we could not find a matching quote
299 return quote ? NULL : psz_sent;
304 * Unescape a nul-terminated string.
305 * Note that in and out can be identical.
307 * @param out output buffer (at least <strlen (in) + 1> characters long)
308 * @param in nul-terminated string to be unescaped
310 * @return 0 on success, -1 on error.
312 static int Unescape (char *out, const char *in)
316 while ((c = *in++) != '\0')
320 if (strchr(quotes,c)) // opening quote
345 /* None of the special cases - copy the backslash */
351 if (c == quote) // non-escaped matching quote
356 if ((quote == '"') && (c == '\\'))
365 case '\0': // should never happen
369 /* None of the special cases - copy the backslash */
381 /*****************************************************************************
382 * ExecuteCommand: The main state machine
383 *****************************************************************************
384 * Execute a command which ends with '\0' (string)
385 *****************************************************************************/
386 static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
387 vlm_message_t **pp_message )
389 size_t i_command = 0;
390 char buf[strlen (psz_command) + 1], *psz_buf = buf;
391 char *ppsz_command[sizeof (buf) / 2];
392 vlm_message_t *p_message = NULL;
394 /* First, parse the line and cut it */
395 while( *psz_command != '\0' )
397 const char *psz_temp;
399 if(isspace (*psz_command))
405 /* support for comments */
406 if( i_command == 0 && *psz_command == '#')
408 p_message = vlm_MessageNew( "", NULL );
412 psz_temp = FindCommandEnd( psz_command );
414 if( psz_temp == NULL )
416 p_message = vlm_MessageNew( "Incomplete command", psz_command );
420 assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
422 ppsz_command[i_command] = psz_buf;
423 memcpy (psz_buf, psz_command, psz_temp - psz_command);
424 psz_buf[psz_temp - psz_command] = '\0';
426 Unescape (psz_buf, psz_buf);
429 psz_buf += psz_temp - psz_command + 1;
430 psz_command = psz_temp;
432 assert (buf + sizeof (buf) >= psz_buf);
436 * And then Interpret it
441 p_message = vlm_MessageNew( "", NULL );
445 if( !strcmp(ppsz_command[0], "new") )
449 /* Check the number of arguments */
450 if( i_command < 3 ) goto syntax_error;
453 if( !strcmp(ppsz_command[2], "vod") )
457 else if( !strcmp(ppsz_command[2], "broadcast") )
459 i_type = BROADCAST_TYPE;
461 else if( !strcmp(ppsz_command[2], "schedule") )
463 i_type = SCHEDULE_TYPE;
468 vlm_MessageNew( "new", "%s: Choose between vod, "
469 "broadcast or schedule", ppsz_command[1] );
473 /* Check for forbidden media names */
474 if( !strcmp(ppsz_command[1], "all") ||
475 !strcmp(ppsz_command[1], "media") ||
476 !strcmp(ppsz_command[1], "schedule") )
478 p_message = vlm_MessageNew( "new", "\"all\", \"media\" and "
479 "\"schedule\" are reserved names" );
483 /* Check the name is not already in use */
484 if( vlm_ScheduleSearch( p_vlm, ppsz_command[1] ) ||
485 vlm_MediaSearch( p_vlm, ppsz_command[1] ) )
487 p_message = vlm_MessageNew( "new", "%s: Name already in use",
493 if( i_type == SCHEDULE_TYPE )
495 vlm_schedule_t *p_schedule;
496 p_schedule = vlm_ScheduleNew( p_vlm, ppsz_command[1] );
499 p_message = vlm_MessageNew( "new", "could not create schedule" );
507 vlm_media_t *p_media;
508 p_media = vlm_MediaNew( p_vlm, ppsz_command[1], i_type );
511 p_message = vlm_MessageNew( "new", "could not create media" );
518 p_message = vlm_MessageNew( "new", NULL );
522 /* Properties will be dealt with later on */
525 else if( !strcmp(ppsz_command[0], "setup") )
527 if( i_command < 2 ) goto syntax_error;
529 /* Properties will be dealt with later on */
532 else if( !strcmp(ppsz_command[0], "del") )
534 vlm_media_t *p_media;
535 vlm_schedule_t *p_schedule;
537 if( i_command < 2 ) goto syntax_error;
539 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
540 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
542 if( p_schedule != NULL )
544 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
546 else if( p_media != NULL )
548 vlm_MediaDelete( p_vlm, p_media, NULL );
550 else if( !strcmp(ppsz_command[1], "media") )
552 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
555 else if( !strcmp(ppsz_command[1], "schedule") )
557 while( p_vlm->i_schedule )
558 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
560 else if( !strcmp(ppsz_command[1], "all") )
562 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
565 while( p_vlm->i_schedule )
566 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
570 p_message = vlm_MessageNew( "del", "%s: media unknown",
575 p_message = vlm_MessageNew( "del", NULL );
579 else if( !strcmp(ppsz_command[0], "show") )
581 vlm_media_t *p_media;
582 vlm_schedule_t *p_schedule;
586 p_message = vlm_Show( p_vlm, NULL, NULL, NULL );
589 else if( i_command > 2 ) goto syntax_error;
591 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
592 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
594 if( p_schedule != NULL )
596 p_message = vlm_Show( p_vlm, NULL, p_schedule, NULL );
598 else if( p_media != NULL )
600 p_message = vlm_Show( p_vlm, p_media, NULL, NULL );
604 p_message = vlm_Show( p_vlm, NULL, NULL, ppsz_command[1] );
610 else if( !strcmp(ppsz_command[0], "help") )
612 if( i_command != 1 ) goto syntax_error;
614 p_message = vlm_Help( p_vlm, NULL );
618 else if( !strcmp(ppsz_command[0], "control") )
620 vlm_media_t *p_media;
622 if( i_command < 3 ) goto syntax_error;
624 if( !(p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] ) ) )
626 p_message = vlm_MessageNew( "control", "%s: media unknown",
632 char *psz_command, *psz_arg = 0, *psz_instance = 0;
635 if( strcmp( ppsz_command[2], "play" ) &&
636 strcmp( ppsz_command[2], "stop" ) &&
637 strcmp( ppsz_command[2], "pause" ) &&
638 strcmp( ppsz_command[2], "seek" ) )
641 psz_instance = ppsz_command[2];
643 if( i_command < 4 ) goto syntax_error;
645 if( strcmp( ppsz_command[3], "play" ) &&
646 strcmp( ppsz_command[3], "stop" ) &&
647 strcmp( ppsz_command[3], "pause" ) &&
648 strcmp( ppsz_command[3], "seek" ) )
652 psz_command = ppsz_command[i_index];
654 if( i_command >= i_index + 2 ) psz_arg = ppsz_command[i_index + 1];
656 vlm_MediaControl( p_vlm, p_media, psz_instance, psz_command,
658 p_message = vlm_MessageNew( "control", NULL );
663 else if( !strcmp(ppsz_command[0], "save") )
665 if( i_command != 2 ) goto syntax_error;
667 if( vlm_Save( p_vlm, ppsz_command[1] ) )
669 p_message = vlm_MessageNew( "save", "Unable to save to file" );
674 p_message = vlm_MessageNew( "save", NULL );
679 else if( !strcmp(ppsz_command[0], "export" ) )
683 if( i_command != 1 ) goto syntax_error;
685 p_message = vlm_MessageNew( "export", psz_buf = Save( p_vlm ) );
691 else if( !strcmp(ppsz_command[0], "load") )
693 if( i_command != 2 ) goto syntax_error;
695 switch( vlm_Load( p_vlm, ppsz_command[1] ) )
698 p_message = vlm_MessageNew( "load", NULL );
701 p_message = vlm_MessageNew( "load", "Read file error" );
705 vlm_MessageNew( "load", "Error while loading file" );
709 vlm_MessageNew( "load", "Unable to load from file" );
716 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
720 /* Common code between "new" and "setup" */
721 if( !strcmp(ppsz_command[0], "new") ||
722 !strcmp(ppsz_command[0], "setup") )
724 size_t i_command_start = strcmp(ppsz_command[0], "new") ? 2 : 3;
725 vlm_media_t *p_media;
726 vlm_schedule_t *p_schedule;
728 if( i_command < i_command_start ) goto syntax_error;
730 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
731 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
733 if( !p_media && !p_schedule )
735 p_message = vlm_MessageNew( ppsz_command[0], "%s unknown",
740 if( p_schedule != NULL )
744 for( i = i_command_start ; i < i_command ; i++ )
746 if( !strcmp( ppsz_command[i], "enabled" ) ||
747 !strcmp( ppsz_command[i], "disabled" ) )
749 vlm_ScheduleSetup( p_schedule, ppsz_command[i], NULL );
752 /* Beware: everything behind append is considered as
754 else if( !strcmp( ppsz_command[i], "append" ) )
758 if( ++i >= i_command ) break;
760 for( j = i + 1; j < i_command; j++ )
763 realloc( ppsz_command[i], strlen(ppsz_command[i]) +
764 strlen(ppsz_command[j]) + 1 + 1 );
765 strcat( ppsz_command[i], " " );
766 strcat( ppsz_command[i], ppsz_command[j] );
769 vlm_ScheduleSetup( p_schedule, ppsz_command[i - 1],
775 if( i + 1 >= i_command && !strcmp(ppsz_command[0], "new") )
777 vlm_ScheduleDelete( p_vlm, p_schedule, 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_ScheduleSetup( p_schedule, ppsz_command[i],
798 else if( p_media != NULL )
802 for( i = i_command_start ; i < i_command ; i++ )
804 if( !strcmp( ppsz_command[i], "enabled" ) ||
805 !strcmp( ppsz_command[i], "disabled" ) )
807 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
809 else if( i + 1 >= i_command &&
810 !strcmp( ppsz_command[i], "mux") )
812 if( p_media->i_type != VOD_TYPE )
814 p_message = vlm_MessageNew( ppsz_command[0],
815 "mux only available for broadcast" );
819 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
824 else if( !strcmp( ppsz_command[i], "loop" ) ||
825 !strcmp( ppsz_command[i], "unloop" ) )
827 if( p_media->i_type != BROADCAST_TYPE )
829 p_message = vlm_MessageNew( ppsz_command[0],
830 "loop only available for broadcast" );
834 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
839 if( ( (i + 1) >= i_command ) &&
840 !strcmp(ppsz_command[0], "new") )
842 vlm_MediaDelete( p_vlm, p_media, NULL );
844 vlm_MessageNew( ppsz_command[0],
845 "Wrong properties syntax" );
848 else if( (i + 1) >= i_command )
851 vlm_MessageNew( ppsz_command[0],
852 "Wrong properties syntax" );
856 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
863 p_message = vlm_MessageNew( ppsz_command[0], NULL );
868 *pp_message = p_message;
872 p_message = vlm_MessageNew( ppsz_command[0], "Wrong command syntax" );
875 *pp_message = p_message;
879 vlm_media_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
883 for( i = 0; i < vlm->i_media; i++ )
885 if( strcmp( psz_name, vlm->media[i]->psz_name ) == 0 )
887 return vlm->media[i];
894 /*****************************************************************************
896 *****************************************************************************/
897 static vlm_media_instance_t *
898 vlm_MediaInstanceSearch( vlm_t *vlm, vlm_media_t *media,
899 const char *psz_name )
903 for( i = 0; i < media->i_instance; i++ )
905 if( ( !psz_name && !media->instance[i]->psz_name ) ||
906 ( psz_name && media->instance[i]->psz_name &&
907 !strcmp( psz_name, media->instance[i]->psz_name ) ) )
909 return media->instance[i];
916 vlm_media_t *vlm_MediaNew( vlm_t *vlm, const char *psz_name, int i_type )
918 vlm_media_t *media = malloc( sizeof( vlm_media_t ) );
922 msg_Err( vlm, "out of memory" );
926 /* Check if we need to load the VOD server */
927 if( i_type == VOD_TYPE && !vlm->i_vod )
929 vlm->vod = vlc_object_create( vlm, VLC_OBJECT_VOD );
930 vlc_object_attach( vlm->vod, vlm );
931 vlm->vod->p_module = module_Need( vlm->vod, "vod server", 0, 0 );
932 if( !vlm->vod->p_module )
934 msg_Err( vlm, "cannot find vod server" );
935 vlc_object_detach( vlm->vod );
936 vlc_object_destroy( vlm->vod );
942 vlm->vod->p_data = vlm;
943 vlm->vod->pf_media_control = vlm_MediaVodControl;
946 if( i_type == VOD_TYPE ) vlm->i_vod++;
948 media->psz_name = strdup( psz_name );
949 media->b_enabled = VLC_FALSE;
950 media->b_loop = VLC_FALSE;
951 media->vod_media = NULL;
952 media->psz_vod_output = NULL;
953 media->psz_mux = NULL;
956 media->psz_output = NULL;
958 media->option = NULL;
959 media->i_type = i_type;
960 media->i_instance = 0;
961 media->instance = NULL;
963 input_ItemInit( VLC_OBJECT(vlm), &media->item );
965 TAB_APPEND( vlm->i_media, vlm->media, media );
970 /* for now, simple delete. After, del with options (last arg) */
971 void vlm_MediaDelete( vlm_t *vlm, vlm_media_t *media, const char *psz_name )
973 if( media == NULL ) return;
975 while( media->i_instance )
977 vlm_media_instance_t *p_instance = media->instance[0];
978 vlm_MediaControl( vlm, media, p_instance->psz_name, "stop", 0 );
981 TAB_REMOVE( vlm->i_media, vlm->media, media );
983 if( media->i_type == VOD_TYPE )
985 vlm_MediaSetup( vlm, media, "disabled", 0 );
989 /* Check if we need to unload the VOD server */
990 if( media->i_type == VOD_TYPE && !vlm->i_vod )
992 module_Unneed( vlm->vod, vlm->vod->p_module );
993 vlc_object_detach( vlm->vod );
994 vlc_object_destroy( vlm->vod );
998 if( vlm->i_media == 0 && vlm->media ) free( vlm->media );
1000 free( media->psz_name );
1002 while( media->i_input-- ) free( media->input[media->i_input] );
1003 if( media->input ) free( media->input );
1005 if( media->psz_output ) free( media->psz_output );
1006 if( media->psz_mux ) free( media->psz_mux );
1008 while( media->i_option-- ) free( media->option[media->i_option] );
1009 if( media->option ) free( media->option );
1011 input_ItemClean( &media->item );
1016 int vlm_MediaSetup( vlm_t *vlm, vlm_media_t *media, const char *psz_cmd,
1017 const char *psz_value )
1019 if( !psz_cmd) return VLC_EGENERIC;
1021 if( !strcmp( psz_cmd, "loop" ) )
1023 media->b_loop = VLC_TRUE;
1025 else if( !strcmp( psz_cmd, "unloop" ) )
1027 media->b_loop = VLC_FALSE;
1029 else if( !strcmp( psz_cmd, "enabled" ) )
1031 media->b_enabled = VLC_TRUE;
1033 else if( !strcmp( psz_cmd, "disabled" ) )
1035 media->b_enabled = VLC_FALSE;
1037 else if( !strcmp( psz_cmd, "mux" ) )
1039 if( media->psz_mux ) free( media->psz_mux );
1040 media->psz_mux = NULL;
1041 if( psz_value ) media->psz_mux = strdup( psz_value );
1043 else if( !strcmp( psz_cmd, "input" ) )
1047 input = strdup( psz_value );
1049 TAB_APPEND( media->i_input, media->input, input );
1051 else if( !strcmp( psz_cmd, "inputdel" ) && !strcmp( psz_value, "all" ) )
1053 while( media->i_input > 0 )
1055 TAB_REMOVE( media->i_input, media->input, media->input[0] );
1058 else if( !strcmp( psz_cmd, "inputdel" ) )
1063 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;
1119 input_ItemClean( &media->item );
1120 input_ItemInit( VLC_OBJECT(vlm), &media->item );
1122 if( media->psz_output )
1123 asprintf( &psz_output, "%s:description", media->psz_output );
1125 asprintf( &psz_output, "#description" );
1127 media->item.psz_uri = strdup( media->input[0] );
1129 TAB_INIT( media->item.i_options, media->item.ppsz_options );
1131 asprintf( &psz_dup, "sout=%s", psz_output);
1132 TAB_APPEND( media->item.i_options, media->item.ppsz_options, psz_dup );
1133 for( i = 0; i < media->i_option; i++ )
1135 psz_dup = strdup( media->option[i] );
1136 TAB_APPEND( media->item.i_options, media->item.ppsz_options, psz_dup );
1138 psz_dup = strdup( "no-sout-keep" );
1139 TAB_APPEND( media->item.i_options, media->item.ppsz_options, psz_dup );
1141 asprintf( &psz_header, _("Media: %s"), media->psz_name );
1143 if( (p_input = input_CreateThread2( vlm, &media->item, psz_header
1146 while( !p_input->b_eof && !p_input->b_error )
1149 input_StopThread( p_input );
1150 input_DestroyThread( 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;
1201 p_instance = malloc( sizeof(vlm_media_instance_t) );
1205 memset( p_instance, 0, sizeof(vlm_media_instance_t) );
1207 p_instance->psz_name = psz_id ? strdup( psz_id ) : NULL;
1208 input_ItemInit( VLC_OBJECT(vlm), &p_instance->item );
1209 p_instance->p_input = NULL;
1211 TAB_INIT( p_instance->item.i_options, p_instance->item.ppsz_options );
1213 if( media->psz_output != NULL || media->psz_vod_output != NULL )
1215 asprintf( &psz_dup, "sout=%s%s%s",
1216 media->psz_output ? media->psz_output : "",
1217 (media->psz_output && media->psz_vod_output) ? ":" : media->psz_vod_output ? "#" : "",
1218 media->psz_vod_output ? media->psz_vod_output : "" );
1219 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, psz_dup );
1222 for( i = 0; i < media->i_option; i++ )
1224 psz_dup = strdup( media->option[i] );
1225 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, psz_dup );
1227 psz_dup = strdup( "no-sout-keep" );
1228 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, psz_dup );
1230 TAB_APPEND( media->i_instance, media->instance, p_instance );
1233 if( ( psz_args && sscanf(psz_args, "%d", &i) == 1 ) && i > 0 && i-1 < media->i_input )
1235 p_instance->i_index = i-1;
1238 if( p_instance->item.psz_uri ) free( p_instance->item.psz_uri );
1239 p_instance->item.psz_uri =
1240 strdup( media->input[p_instance->i_index] );
1242 if( p_instance->p_input )
1244 input_StopThread( p_instance->p_input );
1245 input_DestroyThread( p_instance->p_input );
1248 asprintf( &psz_header, _("Media: %s"), media->psz_name );
1249 p_instance->p_input = input_CreateThread2( vlm, &p_instance->item,
1251 if( !p_instance->p_input )
1253 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1254 input_ItemClean( &p_instance->item );
1255 if( p_instance->psz_name ) free( p_instance->psz_name );
1262 if( !p_instance ) return VLC_EGENERIC;
1264 if( !strcmp( psz_command, "seek" ) )
1269 float f_value = i18n_atof( psz_args );
1270 if( psz_args[0] == '+' || psz_args[0] == '-' )
1274 if( strstr( psz_args, "ms" ) )
1277 int64_t i_msec = 1000 * (int64_t)atoi( psz_args );
1280 var_SetTime( p_instance->p_input, "time-offset",
1283 else if( i_msec >= 0
1284 && i_msec < var_GetTime( p_instance->p_input, "length" ) )
1286 var_SetTime( p_instance->p_input, "time",
1290 else if( strchr( psz_args, 's' ) )
1293 int64_t i_sec = 1000000 * (int64_t)atoi( psz_args );
1296 var_SetTime( p_instance->p_input, "time-offset",
1300 && i_sec < var_GetTime( p_instance->p_input, "length" ) )
1302 var_SetTime( p_instance->p_input, "time",
1312 float f_orig = var_GetFloat( p_instance->p_input,
1316 if( f_value >= 0.0 && f_value <= 1.0 )
1318 var_SetFloat( p_instance->p_input, "position",
1325 else if( !strcmp( psz_command, "rewind" ) )
1332 f_scale = i18n_atof( psz_args );
1333 f_pos = var_GetFloat( p_instance->p_input, "position" );
1334 f_pos -= (f_scale / 1000.0);
1337 var_SetFloat( p_instance->p_input, "position", f_pos );
1341 else if( !strcmp( psz_command, "forward" ) )
1348 f_scale = i18n_atof( psz_args );
1349 f_pos = var_GetFloat( p_instance->p_input, "position" );
1350 f_pos += (f_scale / 1000.0);
1353 var_SetFloat( p_instance->p_input, "position", f_pos );
1357 else if( !strcmp( psz_command, "stop" ) )
1359 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1361 if( p_instance->p_input )
1363 input_StopThread( p_instance->p_input );
1364 input_DestroyThread( p_instance->p_input );
1367 input_ItemClean( &p_instance->item );
1368 if( p_instance->psz_name ) free( p_instance->psz_name );
1373 else if( !strcmp( psz_command, "pause" ) )
1377 if( !p_instance->p_input ) return VLC_SUCCESS;
1379 i_state = var_GetInteger( p_instance->p_input, "state" );
1381 if( i_state == PAUSE_S ) i_state = PLAYING_S;
1382 else i_state = PAUSE_S;
1383 var_SetInteger( p_instance->p_input, "state", i_state );
1388 return VLC_EGENERIC;
1391 /*****************************************************************************
1393 *****************************************************************************/
1394 static int64_t vlm_Date(void)
1399 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1405 vlm_schedule_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
1407 vlm_schedule_t *p_sched = malloc( sizeof( vlm_schedule_t ) );
1419 p_sched->psz_name = strdup( psz_name );
1420 p_sched->b_enabled = VLC_FALSE;
1421 p_sched->i_command = 0;
1422 p_sched->command = NULL;
1423 p_sched->i_date = 0;
1424 p_sched->i_period = 0;
1425 p_sched->i_repeat = -1;
1427 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1432 /* for now, simple delete. After, del with options (last arg) */
1433 void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_t *sched,
1434 const char *psz_name )
1436 if( sched == NULL ) return;
1438 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1440 if( vlm->i_schedule == 0 && vlm->schedule ) free( vlm->schedule );
1441 free( sched->psz_name );
1442 while( sched->i_command )
1444 char *psz_cmd = sched->command[0];
1445 TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1451 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1455 for( i = 0; i < vlm->i_schedule; i++ )
1457 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1459 return vlm->schedule[i];
1466 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1467 int vlm_ScheduleSetup( vlm_schedule_t *schedule, const char *psz_cmd,
1468 const char *psz_value )
1470 if( !strcmp( psz_cmd, "enabled" ) )
1472 schedule->b_enabled = VLC_TRUE;
1474 else if( !strcmp( psz_cmd, "disabled" ) )
1476 schedule->b_enabled = VLC_FALSE;
1478 #if !defined( UNDER_CE )
1479 else if( !strcmp( psz_cmd, "date" ) )
1485 time.tm_sec = 0; /* seconds */
1486 time.tm_min = 0; /* minutes */
1487 time.tm_hour = 0; /* hours */
1488 time.tm_mday = 0; /* day of the month */
1489 time.tm_mon = 0; /* month */
1490 time.tm_year = 0; /* year */
1491 time.tm_wday = 0; /* day of the week */
1492 time.tm_yday = 0; /* day in the year */
1493 time.tm_isdst = -1; /* daylight saving time */
1495 /* date should be year/month/day-hour:minutes:seconds */
1496 p = strchr( psz_value, '-' );
1498 if( !strcmp( psz_value, "now" ) )
1500 schedule->i_date = 0;
1502 else if( (p == NULL) && sscanf( psz_value, "%d:%d:%d", &time.tm_hour,
1503 &time.tm_min, &time.tm_sec ) != 3 )
1504 /* it must be a hour:minutes:seconds */
1512 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1530 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1536 time.tm_mon = i - 1;
1540 time.tm_year = i - 1900;
1541 time.tm_mon = j - 1;
1548 date = mktime( &time );
1549 schedule->i_date = ((mtime_t) date) * 1000000;
1552 else if( !strcmp( psz_cmd, "period" ) )
1556 const char *psz_time = NULL, *psz_date = NULL;
1560 /* First, if date or period are modified, repeat should be equal to -1 */
1561 schedule->i_repeat = -1;
1563 time.tm_sec = 0; /* seconds */
1564 time.tm_min = 0; /* minutes */
1565 time.tm_hour = 0; /* hours */
1566 time.tm_mday = 0; /* day of the month */
1567 time.tm_mon = 0; /* month */
1568 time.tm_year = 0; /* year */
1569 time.tm_wday = 0; /* day of the week */
1570 time.tm_yday = 0; /* day in the year */
1571 time.tm_isdst = -1; /* daylight saving time */
1573 /* date should be year/month/day-hour:minutes:seconds */
1574 p = strchr( psz_value, '-' );
1577 psz_date = psz_value;
1582 psz_time = psz_value;
1585 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1604 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1623 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1624 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1625 schedule->i_period = ((mtime_t) date) * 1000000;
1627 #endif /* UNDER_CE */
1628 else if( !strcmp( psz_cmd, "repeat" ) )
1632 if( sscanf( psz_value, "%d", &i ) == 1 )
1634 schedule->i_repeat = i;
1641 else if( !strcmp( psz_cmd, "append" ) )
1643 char *command = strdup( psz_value );
1645 TAB_APPEND( schedule->i_command, schedule->command, command );
1654 /*****************************************************************************
1655 * Message handling functions
1656 *****************************************************************************/
1657 vlm_message_t *vlm_MessageNew( const char *psz_name,
1658 const char *psz_format, ... )
1660 vlm_message_t *p_message;
1663 if( !psz_name ) return NULL;
1665 p_message = malloc( sizeof(vlm_message_t) );
1671 p_message->psz_value = 0;
1675 va_start( args, psz_format );
1676 if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1685 p_message->psz_name = strdup( psz_name );
1686 p_message->i_child = 0;
1687 p_message->child = NULL;
1692 void vlm_MessageDelete( vlm_message_t *p_message )
1694 if( p_message->psz_name ) free( p_message->psz_name );
1695 if( p_message->psz_value ) free( p_message->psz_value );
1696 while( p_message->i_child-- )
1697 vlm_MessageDelete( p_message->child[p_message->i_child] );
1698 if( p_message->child ) free( p_message->child );
1703 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1704 vlm_message_t *p_child )
1706 if( p_message == NULL ) return NULL;
1710 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1716 /*****************************************************************************
1717 * Misc utility functions
1718 *****************************************************************************/
1719 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_t *media,
1720 vlm_schedule_t *schedule,
1721 const char *psz_filter )
1727 vlm_message_t *msg_media;
1728 vlm_message_t *msg_child;
1730 msg = vlm_MessageNew( "show", NULL );
1731 msg_media = vlm_MessageAdd( msg, vlm_MessageNew( media->psz_name, 0 ));
1733 vlm_MessageAdd( msg_media,
1734 vlm_MessageNew( "type", media->i_type == VOD_TYPE ?
1735 "vod" : "broadcast" ) );
1736 vlm_MessageAdd( msg_media,
1737 vlm_MessageNew( "enabled", media->b_enabled ?
1740 vlm_MessageAdd( msg_media,
1741 vlm_MessageNew( "loop", media->b_loop ?
1744 if( media->i_type == VOD_TYPE && media->psz_mux )
1745 vlm_MessageAdd( msg_media,
1746 vlm_MessageNew( "mux", media->psz_mux ) );
1748 msg_child = vlm_MessageAdd( msg_media,
1749 vlm_MessageNew( "inputs", NULL ) );
1751 for( i = 0; i < media->i_input; i++ )
1753 vlm_MessageAdd( msg_child,
1754 vlm_MessageNew( media->input[i], NULL ) );
1757 vlm_MessageAdd( msg_media,
1758 vlm_MessageNew( "output", media->psz_output ?
1759 media->psz_output : "" ) );
1761 msg_child = vlm_MessageAdd( msg_media, vlm_MessageNew( "options", 0 ));
1763 for( i = 0; i < media->i_option; i++ )
1765 vlm_MessageAdd( msg_child, vlm_MessageNew( media->option[i], 0 ) );
1768 msg_child = vlm_MessageAdd( msg_media,
1769 vlm_MessageNew( "instances", NULL ) );
1771 for( i = 0; i < media->i_instance; i++ )
1773 vlm_media_instance_t *p_instance = media->instance[i];
1775 vlm_message_t *msg_instance;
1778 if( !p_instance->p_input ) val.i_int = END_S;
1779 else var_Get( p_instance->p_input, "state", &val );
1781 msg_instance = vlm_MessageNew( "instance" , NULL );
1782 vlm_MessageAdd( msg_instance, vlm_MessageNew( "name" , p_instance->psz_name ? p_instance->psz_name : "default" ) );
1783 vlm_MessageAdd( msg_instance, vlm_MessageNew( "state",
1784 val.i_int == PLAYING_S ? "playing" :
1785 val.i_int == PAUSE_S ? "paused" :
1787 #define APPEND_INPUT_INFO( a, format, type ) \
1788 asprintf( &psz_tmp, format, \
1789 var_Get ## type( p_instance->p_input, a ) ); \
1790 vlm_MessageAdd( msg_instance, vlm_MessageNew( a, psz_tmp ) ); \
1792 APPEND_INPUT_INFO( "position", "%f", Float );
1793 APPEND_INPUT_INFO( "time", I64Fi, Time );
1794 APPEND_INPUT_INFO( "length", I64Fi, Time );
1795 APPEND_INPUT_INFO( "rate", "%d", Integer );
1796 APPEND_INPUT_INFO( "title", "%d", Integer );
1797 APPEND_INPUT_INFO( "chapter", "%d", Integer );
1798 APPEND_INPUT_INFO( "seekable", "%d", Bool );
1799 #undef APPEND_INPUT_INFO
1800 asprintf( &psz_tmp, "%d", p_instance->i_index + 1 );
1801 vlm_MessageAdd( msg_instance, vlm_MessageNew( "playlistindex", psz_tmp ) );
1803 vlm_MessageAdd( msg_child, msg_instance );
1810 else if( schedule != NULL )
1814 vlm_message_t *msg_schedule;
1815 vlm_message_t *msg_child;
1818 msg = vlm_MessageNew( "show", NULL );
1820 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, 0 ) );
1822 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1824 vlm_MessageAdd( msg_schedule,
1825 vlm_MessageNew( "enabled", schedule->b_enabled ?
1828 #if !defined( UNDER_CE )
1829 if( schedule->i_date != 0 )
1832 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1835 #ifdef HAVE_LOCALTIME_R
1836 localtime_r( &i_time, &date);
1838 struct tm *p_date = localtime( &i_time );
1842 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1843 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1844 date.tm_hour, date.tm_min, date.tm_sec );
1846 vlm_MessageAdd( msg_schedule,
1847 vlm_MessageNew( "date", psz_date ) );
1852 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1855 if( schedule->i_period != 0 )
1857 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1860 date.tm_sec = (int)( i_time % 60 );
1861 i_time = i_time / 60;
1862 date.tm_min = (int)( i_time % 60 );
1863 i_time = i_time / 60;
1864 date.tm_hour = (int)( i_time % 24 );
1865 i_time = i_time / 24;
1866 date.tm_mday = (int)( i_time % 30 );
1867 i_time = i_time / 30;
1868 /* okay, okay, months are not always 30 days long */
1869 date.tm_mon = (int)( i_time % 12 );
1870 i_time = i_time / 12;
1871 date.tm_year = (int)i_time;
1873 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1874 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1876 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1880 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1882 #endif /* UNDER_CE */
1884 sprintf( buffer, "%d", schedule->i_repeat );
1885 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1888 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", 0) );
1890 for( i = 0; i < schedule->i_command; i++ )
1892 vlm_MessageAdd( msg_child,
1893 vlm_MessageNew( schedule->command[i], NULL ) );
1900 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1904 vlm_message_t *msg_child;
1905 int i_vod = 0, i_broadcast = 0;
1908 for( i = 0; i < vlm->i_media; i++ )
1910 if( vlm->media[i]->i_type == VOD_TYPE )
1916 asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1918 msg = vlm_MessageNew( "show", NULL );
1919 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "media", psz_count ) );
1922 for( i = 0; i < vlm->i_media; i++ )
1924 vlm_media_t *m = vlm->media[i];
1925 vlm_message_t *msg_media, *msg_instance;
1927 msg_media = vlm_MessageAdd( msg_child,
1928 vlm_MessageNew( m->psz_name, 0 ) );
1930 vlm_MessageAdd( msg_media,
1931 vlm_MessageNew( "type", m->i_type == VOD_TYPE ?
1932 "vod" : "broadcast" ) );
1934 vlm_MessageAdd( msg_media,
1935 vlm_MessageNew( "enabled", m->b_enabled ?
1938 if( m->i_type == VOD_TYPE && m->psz_mux )
1939 vlm_MessageAdd( msg_media,
1940 vlm_MessageNew( "mux", m->psz_mux ) );
1942 msg_instance = vlm_MessageAdd( msg_media,
1943 vlm_MessageNew( "instances", 0 ) );
1945 for( j = 0; j < m->i_instance; j++ )
1947 vlm_media_instance_t *p_instance = m->instance[j];
1950 if( !p_instance->p_input ) val.i_int = END_S;
1951 else var_Get( p_instance->p_input, "state", &val );
1953 vlm_MessageAdd( msg_instance,
1954 vlm_MessageNew( p_instance->psz_name ?
1955 p_instance->psz_name : "default",
1956 val.i_int == PLAYING_S ? "playing" :
1957 val.i_int == PAUSE_S ? "paused" :
1965 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1969 vlm_message_t *msg_child;
1971 msg = vlm_MessageNew( "show", NULL );
1972 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", NULL ) );
1974 for( i = 0; i < vlm->i_schedule; i++ )
1976 vlm_schedule_t *s = vlm->schedule[i];
1977 vlm_message_t *msg_schedule;
1978 mtime_t i_time, i_next_date;
1980 msg_schedule = vlm_MessageAdd( msg_child,
1981 vlm_MessageNew( s->psz_name, 0 ) );
1982 vlm_MessageAdd( msg_schedule,
1983 vlm_MessageNew( "enabled", s->b_enabled ?
1986 /* calculate next date */
1987 i_time = vlm_Date();
1988 i_next_date = s->i_date;
1990 if( s->i_period != 0 )
1993 while( s->i_date + j * s->i_period <= i_time &&
1999 i_next_date = s->i_date + j * s->i_period;
2002 if( i_next_date > i_time )
2004 time_t i_date = (time_t) (i_next_date / 1000000) ;
2006 #if !defined( UNDER_CE )
2009 ctime_r( &i_date, psz_date );
2011 char *psz_date = ctime( &i_date );
2014 vlm_MessageAdd( msg_schedule,
2015 vlm_MessageNew( "next launch", psz_date ) );
2023 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
2025 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
2026 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
2028 vlm_MessageAdd( show1, show2->child[0] );
2030 /* We must destroy the parent node "show" of show2
2031 * and not the children */
2032 free( show2->psz_name );
2040 return vlm_MessageNew( "show", NULL );
2044 static vlm_message_t *vlm_Help( vlm_t *vlm, char *psz_filter )
2046 vlm_message_t *message, *message_child;
2048 #define MessageAdd( a ) \
2049 vlm_MessageAdd( message, vlm_MessageNew( a, NULL ) );
2050 #define MessageAddChild( a ) \
2051 vlm_MessageAdd( message_child, vlm_MessageNew( a, NULL ) );
2053 if( psz_filter == NULL )
2055 message = vlm_MessageNew( "help", NULL );
2057 message_child = MessageAdd( "Commands Syntax:" );
2058 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
2059 MessageAddChild( "setup (name) (properties)" );
2060 MessageAddChild( "show [(name)|media|schedule]" );
2061 MessageAddChild( "del (name)|all|media|schedule" );
2062 MessageAddChild( "control (name) [instance_name] (command)" );
2063 MessageAddChild( "save (config_file)" );
2064 MessageAddChild( "export" );
2065 MessageAddChild( "load (config_file)" );
2067 message_child = MessageAdd( "Media Proprieties Syntax:" );
2068 MessageAddChild( "input (input_name)" );
2069 MessageAddChild( "inputdel (input_name)|all" );
2070 MessageAddChild( "inputdeln input_number" );
2071 MessageAddChild( "output (output_name)" );
2072 MessageAddChild( "option (option_name)[=value]" );
2073 MessageAddChild( "enabled|disabled" );
2074 MessageAddChild( "loop|unloop (broadcast only)" );
2075 MessageAddChild( "mux (mux_name)" );
2077 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
2078 MessageAddChild( "enabled|disabled" );
2079 MessageAddChild( "append (command_until_rest_of_the_line)" );
2080 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
2082 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
2083 "(days)-(hours):(minutes):(seconds)" );
2084 MessageAddChild( "repeat (number_of_repetitions)" );
2086 message_child = MessageAdd( "Control Commands Syntax:" );
2087 MessageAddChild( "play [input_number]" );
2088 MessageAddChild( "pause" );
2089 MessageAddChild( "stop" );
2090 MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](miliseconds)ms" );
2095 return vlm_MessageNew( "help", NULL );
2098 /*****************************************************************************
2099 * Config handling functions
2100 *****************************************************************************/
2101 static int Load( vlm_t *vlm, char *file )
2106 while( *pf != '\0' )
2108 vlm_message_t *message = NULL;
2111 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
2116 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
2120 if( pf[i_end] == '\n' ) i_end++;
2123 if( *pf && ExecuteCommand( vlm, pf, &message ) )
2127 if( message->psz_value )
2128 msg_Err( vlm, "Load error on line %d: %s: %s",
2129 i_line, message->psz_name, message->psz_value );
2130 vlm_MessageDelete( message );
2134 if( message ) vlm_MessageDelete( message );
2143 static char *Save( vlm_t *vlm )
2146 char psz_header[] = "\n"
2147 "# VLC media player VLM command batch\n"
2148 "# http://www.videolan.org/vlc/\n\n" ;
2151 int i_length = strlen( psz_header );
2153 for( i = 0; i < vlm->i_media; i++ )
2155 vlm_media_t *media = vlm->media[i];
2157 if( media->i_type == VOD_TYPE )
2159 i_length += strlen( "new vod " ) + strlen(media->psz_name);
2163 i_length += strlen( "new broadcast " ) + strlen(media->psz_name);
2166 if( media->b_enabled == VLC_TRUE )
2168 i_length += strlen( "enabled" );
2172 i_length += strlen( "disabled" );
2175 if( media->b_loop == VLC_TRUE )
2177 i_length += strlen( " loop\n" );
2181 i_length += strlen( "\n" );
2184 for( j = 0; j < media->i_input; j++ )
2186 i_length += strlen( "setup input \"\"\n" ) +
2187 strlen( media->psz_name ) + strlen( media->input[j] );
2190 if( media->psz_output != NULL )
2192 i_length += strlen(media->psz_name) + strlen(media->psz_output) +
2193 strlen( "setup output \n" );
2196 for( j=0 ; j < media->i_option ; j++ )
2198 i_length += strlen(media->psz_name) + strlen(media->option[j]) +
2199 strlen("setup option \n");
2203 for( i = 0; i < vlm->i_schedule; i++ )
2205 vlm_schedule_t *schedule = vlm->schedule[i];
2207 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
2209 if( schedule->b_enabled == VLC_TRUE )
2211 i_length += strlen( "date //-:: enabled\n" ) + 14;
2215 i_length += strlen( "date //-:: disabled\n" ) + 14;
2219 if( schedule->i_period != 0 )
2221 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
2222 strlen( "period //-::\n" ) + 14;
2225 if( schedule->i_repeat >= 0 )
2229 sprintf( buffer, "%d", schedule->i_repeat );
2230 i_length += strlen( "setup repeat \n" ) +
2231 strlen( schedule->psz_name ) + strlen( buffer );
2238 for( j = 0; j < schedule->i_command; j++ )
2240 i_length += strlen( "setup append \n" ) +
2241 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
2246 /* Don't forget the '\0' */
2248 /* now we have the length of save */
2250 p = save = malloc( i_length );
2253 p += sprintf( p, "%s", psz_header );
2255 /* finally we can write in it */
2256 for( i = 0; i < vlm->i_media; i++ )
2258 vlm_media_t *media = vlm->media[i];
2260 if( media->i_type == VOD_TYPE )
2262 p += sprintf( p, "new %s vod ", media->psz_name);
2266 p += sprintf( p, "new %s broadcast ", media->psz_name);
2269 if( media->b_enabled == VLC_TRUE )
2271 p += sprintf( p, "enabled" );
2275 p += sprintf( p, "disabled" );
2278 if( media->b_loop == VLC_TRUE )
2280 p += sprintf( p, " loop\n" );
2284 p += sprintf( p, "\n" );
2287 for( j = 0; j < media->i_input; j++ )
2289 p += sprintf( p, "setup %s input \"%s\"\n", media->psz_name,
2293 if( media->psz_output != NULL )
2295 p += sprintf( p, "setup %s output %s\n", media->psz_name,
2296 media->psz_output );
2299 for( j = 0; j < media->i_option; j++ )
2301 p += sprintf( p, "setup %s option %s\n", media->psz_name,
2306 /* and now, the schedule scripts */
2307 #if !defined( UNDER_CE )
2308 for( i = 0; i < vlm->i_schedule; i++ )
2310 vlm_schedule_t *schedule = vlm->schedule[i];
2312 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
2314 #ifdef HAVE_LOCALTIME_R
2315 localtime_r( &i_time, &date);
2317 struct tm *p_date = localtime( &i_time );
2321 p += sprintf( p, "new %s schedule ", schedule->psz_name);
2323 if( schedule->b_enabled == VLC_TRUE )
2325 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
2326 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2327 date.tm_hour, date.tm_min, date.tm_sec );
2331 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
2332 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2333 date.tm_hour, date.tm_min, date.tm_sec);
2336 if( schedule->i_period != 0 )
2338 p += sprintf( p, "setup %s ", schedule->psz_name );
2340 i_time = (time_t) ( schedule->i_period / 1000000 );
2342 date.tm_sec = (int)( i_time % 60 );
2343 i_time = i_time / 60;
2344 date.tm_min = (int)( i_time % 60 );
2345 i_time = i_time / 60;
2346 date.tm_hour = (int)( i_time % 24 );
2347 i_time = i_time / 24;
2348 date.tm_mday = (int)( i_time % 30 );
2349 i_time = i_time / 30;
2350 /* okay, okay, months are not always 30 days long */
2351 date.tm_mon = (int)( i_time % 12 );
2352 i_time = i_time / 12;
2353 date.tm_year = (int)i_time;
2355 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
2356 date.tm_year, date.tm_mon, date.tm_mday,
2357 date.tm_hour, date.tm_min, date.tm_sec);
2360 if( schedule->i_repeat >= 0 )
2362 p += sprintf( p, "setup %s repeat %d\n",
2363 schedule->psz_name, schedule->i_repeat );
2367 p += sprintf( p, "\n" );
2370 for( j = 0; j < schedule->i_command; j++ )
2372 p += sprintf( p, "setup %s append %s\n",
2373 schedule->psz_name, schedule->command[j] );
2377 #endif /* UNDER_CE */
2382 /*****************************************************************************
2384 *****************************************************************************/
2385 int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
2386 const char *psz_id, int i_query, va_list args )
2388 vlm_t *vlm = (vlm_t *)p_private;
2389 int i, i_ret = VLC_EGENERIC;
2391 if( !p_private || !p_vod_media ) return VLC_EGENERIC;
2393 vlc_mutex_lock( &vlm->lock );
2396 for( i = 0; i < vlm->i_media; i++ )
2398 if( p_vod_media == vlm->media[i]->vod_media ) break;
2401 if( i == vlm->i_media )
2403 vlc_mutex_unlock( &vlm->lock );
2404 return VLC_EGENERIC;
2409 case VOD_MEDIA_PLAY:
2410 vlm->media[i]->psz_vod_output = (char *)va_arg( args, char * );
2411 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "play", 0 );
2412 vlm->media[i]->psz_vod_output = 0;
2415 case VOD_MEDIA_PAUSE:
2416 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "pause", 0 );
2419 case VOD_MEDIA_STOP:
2420 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "stop", 0 );
2423 case VOD_MEDIA_SEEK:
2425 double f_pos = (double)va_arg( args, double );
2427 lldiv_t div = lldiv( f_pos * 10000000, 10000000 );
2428 sprintf( psz_pos, I64Fd".%07u", div.quot, (unsigned int) div.rem );
2429 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "seek", psz_pos);
2433 case VOD_MEDIA_REWIND:
2435 double f_scale = (double)va_arg( args, double );
2437 lldiv_t div = lldiv( f_scale * 10000000, 10000000 );
2438 sprintf( psz_scale, I64Fd".%07u", div.quot, (unsigned int) div.rem );
2439 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "rewind", psz_scale );
2443 case VOD_MEDIA_FORWARD:
2445 double f_scale = (double)va_arg( args, double );
2447 lldiv_t div = lldiv( f_scale * 10000000, 10000000 );
2448 sprintf( psz_scale, I64Fd".%07u", div.quot, (unsigned int) div.rem );
2449 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "forward", psz_scale );
2457 vlc_mutex_unlock( &vlm->lock );
2463 /*****************************************************************************
2465 *****************************************************************************/
2466 static int Manage( vlc_object_t* p_object )
2468 vlm_t *vlm = (vlm_t*)p_object;
2470 mtime_t i_lastcheck;
2473 i_lastcheck = vlm_Date();
2477 while( !vlm->b_die )
2479 char **ppsz_scheduled_commands = NULL;
2480 int i_scheduled_commands = 0;
2481 vlc_mutex_lock( &vlm->lock );
2483 /* destroy the inputs that wants to die, and launch the next input */
2484 for( i = 0; i < vlm->i_media; i++ )
2486 vlm_media_t *p_media = vlm->media[i];
2488 for( j = 0; j < p_media->i_instance; j++ )
2490 vlm_media_instance_t *p_instance = p_media->instance[j];
2492 if( !p_instance->p_input ||
2493 ( !p_instance->p_input->b_eof && !p_instance->p_input->b_error ) )
2496 p_instance->i_index++;
2497 if( p_instance->i_index >= p_media->i_input && p_media->b_loop )
2498 p_instance->i_index = 0;
2500 if( p_instance->i_index < p_media->i_input )
2504 input_StopThread( p_instance->p_input );
2505 input_DestroyThread( p_instance->p_input );
2506 p_instance->p_input = NULL;
2508 /* FIXME, find a way to select the right instance */
2509 sprintf( buffer, "%d", p_instance->i_index+1 );
2511 vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2516 if( vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2517 "stop", 0 ) == VLC_SUCCESS )
2519 j--; /* the aray is one element smaller now. */
2526 i_time = vlm_Date();
2528 for( i = 0; i < vlm->i_schedule; i++ )
2530 mtime_t i_real_date = vlm->schedule[i]->i_date;
2532 if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2534 if( vlm->schedule[i]->i_date == 0 ) // now !
2536 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2537 i_real_date = i_time;
2539 else if( vlm->schedule[i]->i_period != 0 )
2542 while( vlm->schedule[i]->i_date + j *
2543 vlm->schedule[i]->i_period <= i_lastcheck &&
2544 ( vlm->schedule[i]->i_repeat > j ||
2545 vlm->schedule[i]->i_repeat == -1 ) )
2550 i_real_date = vlm->schedule[i]->i_date + j *
2551 vlm->schedule[i]->i_period;
2554 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2556 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2558 TAB_APPEND( i_scheduled_commands,
2559 ppsz_scheduled_commands,
2560 strdup(vlm->schedule[i]->command[j] ) );
2565 while( i_scheduled_commands )
2567 vlm_message_t *message = NULL;
2568 char *psz_command = ppsz_scheduled_commands[0];
2569 ExecuteCommand( vlm, psz_command,&message );
2571 /* for now, drop the message */
2572 vlm_MessageDelete( message );
2573 TAB_REMOVE( i_scheduled_commands,
2574 ppsz_scheduled_commands,
2576 free( psz_command );
2579 i_lastcheck = i_time;
2581 vlc_mutex_unlock( &vlm->lock );
2589 #else /* ENABLE_VLM */
2591 /* We just define an empty wrapper */
2592 vlm_t *__vlm_New( vlc_object_t *a )
2594 msg_Err( a, "VideoLAN manager support is disabled" );
2597 void vlm_Delete( vlm_t *a ){}
2598 int vlm_ExecuteCommand( vlm_t *a, char *b, vlm_message_t **c ){ return -1; }
2599 void vlm_MessageDelete( vlm_message_t *a ){}
2600 vlm_media_t *vlm_MediaNew( vlm_t *a, char *b, int c ){ return NULL; }
2601 vlm_media_t *vlm_MediaSearch (vlm_t *a, const char *b ) { return NULL; }
2602 void vlm_MediaDelete( vlm_t *a, vlm_media_t *b, char *c ){}
2603 int vlm_MediaSetup( vlm_t *a, vlm_media_t *b, char *c, char *d ){ return -1; }
2604 int vlm_MediaControl( vlm_t *a, vlm_media_t *b, char *c, char *d, char *e )
2606 vlm_schedule_t * vlm_ScheduleNew( vlm_t *a, char *b ){ return NULL; }
2607 void vlm_ScheduleDelete( vlm_t *a, vlm_schedule_t *b, char *c ){}
2608 int vlm_ScheduleSetup( vlm_schedule_t *a, char *b, char *c ){ return -1; }
2609 int vlm_MediaVodControl( void *a, vod_media_t *b, char *c, int d, va_list e )
2611 int vlm_Save( vlm_t *a, char *b ){ return -1; }
2612 int vlm_Load( vlm_t *a, char *b ){ return -1; }
2614 #endif /* ENABLE_VLM */