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 );
93 p_vlm->i_schedule = 0;
94 p_vlm->schedule = NULL;
96 vlc_object_yield( p_vlm );
97 vlc_object_attach( p_vlm, p_this->p_libvlc );
99 vlc_mutex_unlock( lockval.p_address );
101 if( vlc_thread_create( p_vlm, "vlm thread",
102 Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
104 vlc_mutex_destroy( &p_vlm->lock );
105 vlc_object_destroy( p_vlm );
109 /* Try loading the vlm conf file given by --vlm-conf */
110 psz_vlmconf = config_GetPsz( p_vlm, "vlm-conf" );
112 if( psz_vlmconf && *psz_vlmconf )
114 vlm_message_t *p_message = NULL;
115 char *psz_buffer = NULL;
117 msg_Dbg( p_this, "loading VLM configuration" );
118 asprintf(&psz_buffer, "load %s", psz_vlmconf );
121 msg_Dbg( p_this, psz_buffer);
122 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) ){
123 msg_Warn( p_this, "error while loading the configuration file" );
125 vlm_MessageDelete(p_message);
134 /*****************************************************************************
136 *****************************************************************************/
137 void vlm_Delete( vlm_t *p_vlm )
141 var_Get( p_vlm->p_libvlc_global, "vlm_mutex", &lockval );
142 vlc_mutex_lock( lockval.p_address );
144 vlc_object_release( p_vlm );
146 if( p_vlm->i_refcount > 0 )
148 vlc_mutex_unlock( lockval.p_address );
152 p_vlm->b_die = VLC_TRUE;
153 vlc_thread_join( p_vlm );
155 vlc_mutex_destroy( &p_vlm->lock );
157 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0], NULL );
158 FREENULL( p_vlm->media );
160 while( p_vlm->i_schedule ) vlm_ScheduleDelete( p_vlm,
161 p_vlm->schedule[0], NULL );
162 FREENULL( p_vlm->schedule );
164 vlc_object_detach( p_vlm );
165 vlc_object_destroy( p_vlm );
166 vlc_mutex_unlock( lockval.p_address );
169 /*****************************************************************************
170 * vlm_ExecuteCommand:
171 *****************************************************************************/
172 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
173 vlm_message_t **pp_message)
177 vlc_mutex_lock( &p_vlm->lock );
178 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
179 vlc_mutex_unlock( &p_vlm->lock );
184 /*****************************************************************************
186 *****************************************************************************/
187 int vlm_Save( vlm_t *p_vlm, const char *psz_file )
192 if( !p_vlm || !psz_file ) return 1;
194 file = utf8_fopen( psz_file, "wt" );
195 if( file == NULL ) return 1;
197 psz_save = Save( p_vlm );
198 if( psz_save == NULL )
203 fwrite( psz_save, strlen( psz_save ), 1, file );
210 /*****************************************************************************
212 *****************************************************************************/
213 int vlm_Load( vlm_t *p_vlm, const char *psz_file )
219 if( !p_vlm || !psz_file ) return 1;
221 p_stream = stream_UrlNew( p_vlm, psz_file );
222 if( p_stream == NULL ) return 1;
224 if( stream_Seek( p_stream, 0 ) != 0 )
226 stream_Delete( p_stream );
230 i_size = stream_Size( p_stream );
232 psz_buffer = malloc( i_size + 1 );
235 stream_Delete( p_stream );
239 stream_Read( p_stream, psz_buffer, i_size );
240 psz_buffer[ i_size ] = '\0';
242 stream_Delete( p_stream );
244 if( Load( p_vlm, psz_buffer ) )
256 static const char quotes[] = "\"'";
258 * FindCommandEnd: look for the end of a possibly quoted string
259 * @return NULL on mal-formatted string,
260 * pointer paste the last character otherwise.
262 static const char *FindCommandEnd (const char *psz_sent)
264 const char quote = strchr (quotes, psz_sent[0]) ? psz_sent[0] : 0;
267 while ((c = *psz_sent) != '\0')
269 if ((quote == '"') && (c == '\\'))
271 if (*psz_sent == '\0')
272 return NULL; // cannot escape "nothing"
274 psz_sent++; // skips escaped character
277 if (c == quote) // non-escaped matching quote
280 if (isblank (c)) // non-escaped blank
286 // error (NULL) if we could not find a matching quote
287 return quote ? NULL : psz_sent;
292 * Unescape a nul-terminated string.
293 * Note that in and out can be identical.
295 * @param out output buffer (at least <strlen (in) + 1> characters long)
296 * @param in nul-terminated string to be unescaped
298 * @return 0 on success, -1 on error.
300 static int Unescape (char *out, const char *in)
302 const char quote = strchr (quotes, in[0]) ? in[0] : 0;
305 in++; // skips opening quote
311 if ((c == '\0') || (c == quote))
314 if ((quote == '"') && (c == '\\'))
331 // Only allow printable ASCII characters
332 // (in particular, no nul nor extended characters)
344 /*****************************************************************************
345 * ExecuteCommand: The main state machine
346 *****************************************************************************
347 * Execute a command which ends with '\0' (string)
348 *****************************************************************************/
349 static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
350 vlm_message_t **pp_message )
352 size_t i_command = 0;
353 char buf[strlen (psz_command) + 1], *psz_buf = buf;
354 char *ppsz_command[sizeof (buf) / 2];
355 vlm_message_t *p_message = NULL;
357 /* First, parse the line and cut it */
358 while( *psz_command != '\0' )
360 const char *psz_temp;
362 if(isblank (*psz_command))
368 /* support for comments */
369 if( i_command == 0 && *psz_command == '#')
371 p_message = vlm_MessageNew( "", NULL );
375 psz_temp = FindCommandEnd( psz_command );
377 if( psz_temp == NULL )
379 p_message = vlm_MessageNew( "Incomplete command", psz_command );
383 assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
385 ppsz_command[i_command] = psz_buf;
386 memcpy (psz_buf, psz_command, psz_temp - psz_command);
387 psz_buf[psz_temp - psz_command] = '\0';
389 Unescape (psz_buf, psz_buf);
392 psz_buf += psz_temp - psz_command + 1;
393 psz_command = psz_temp;
395 assert (buf + sizeof (buf) >= psz_buf);
399 * And then Interpret it
404 p_message = vlm_MessageNew( "", NULL );
408 if( !strcmp(ppsz_command[0], "new") )
412 /* Check the number of arguments */
413 if( i_command < 3 ) goto syntax_error;
416 if( !strcmp(ppsz_command[2], "vod") )
420 else if( !strcmp(ppsz_command[2], "broadcast") )
422 i_type = BROADCAST_TYPE;
424 else if( !strcmp(ppsz_command[2], "schedule") )
426 i_type = SCHEDULE_TYPE;
431 vlm_MessageNew( "new", "%s: Choose between vod, "
432 "broadcast or schedule", ppsz_command[1] );
436 /* Check for forbidden media names */
437 if( !strcmp(ppsz_command[1], "all") ||
438 !strcmp(ppsz_command[1], "media") ||
439 !strcmp(ppsz_command[1], "schedule") )
441 p_message = vlm_MessageNew( "new", "\"all\", \"media\" and "
442 "\"schedule\" are reserved names" );
446 /* Check the name is not already in use */
447 if( vlm_ScheduleSearch( p_vlm, ppsz_command[1] ) ||
448 vlm_MediaSearch( p_vlm, ppsz_command[1] ) )
450 p_message = vlm_MessageNew( "new", "%s: Name already in use",
456 if( i_type == SCHEDULE_TYPE )
458 vlm_schedule_t *p_schedule;
459 p_schedule = vlm_ScheduleNew( p_vlm, ppsz_command[1] );
462 p_message = vlm_MessageNew( "new", "could not create schedule" );
470 vlm_media_t *p_media;
471 p_media = vlm_MediaNew( p_vlm, ppsz_command[1], i_type );
474 p_message = vlm_MessageNew( "new", "could not create media" );
481 p_message = vlm_MessageNew( "new", NULL );
485 /* Properties will be dealt with later on */
488 else if( !strcmp(ppsz_command[0], "setup") )
490 if( i_command < 2 ) goto syntax_error;
492 /* Properties will be dealt with later on */
495 else if( !strcmp(ppsz_command[0], "del") )
497 vlm_media_t *p_media;
498 vlm_schedule_t *p_schedule;
500 if( i_command < 2 ) goto syntax_error;
502 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
503 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
505 if( p_schedule != NULL )
507 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
509 else if( p_media != NULL )
511 vlm_MediaDelete( p_vlm, p_media, NULL );
513 else if( !strcmp(ppsz_command[1], "media") )
515 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
518 else if( !strcmp(ppsz_command[1], "schedule") )
520 while( p_vlm->i_schedule )
521 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
523 else if( !strcmp(ppsz_command[1], "all") )
525 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
528 while( p_vlm->i_schedule )
529 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
533 p_message = vlm_MessageNew( "del", "%s: media unknown",
538 p_message = vlm_MessageNew( "del", NULL );
542 else if( !strcmp(ppsz_command[0], "show") )
544 vlm_media_t *p_media;
545 vlm_schedule_t *p_schedule;
549 p_message = vlm_Show( p_vlm, NULL, NULL, NULL );
552 else if( i_command > 2 ) goto syntax_error;
554 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
555 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
557 if( p_schedule != NULL )
559 p_message = vlm_Show( p_vlm, NULL, p_schedule, NULL );
561 else if( p_media != NULL )
563 p_message = vlm_Show( p_vlm, p_media, NULL, NULL );
567 p_message = vlm_Show( p_vlm, NULL, NULL, ppsz_command[1] );
573 else if( !strcmp(ppsz_command[0], "help") )
575 if( i_command != 1 ) goto syntax_error;
577 p_message = vlm_Help( p_vlm, NULL );
581 else if( !strcmp(ppsz_command[0], "control") )
583 vlm_media_t *p_media;
585 if( i_command < 3 ) goto syntax_error;
587 if( !(p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] ) ) )
589 p_message = vlm_MessageNew( "control", "%s: media unknown",
595 char *psz_command, *psz_arg = 0, *psz_instance = 0;
598 if( strcmp( ppsz_command[2], "play" ) &&
599 strcmp( ppsz_command[2], "stop" ) &&
600 strcmp( ppsz_command[2], "pause" ) &&
601 strcmp( ppsz_command[2], "seek" ) )
604 psz_instance = ppsz_command[2];
606 if( i_command < 4 ) goto syntax_error;
608 if( strcmp( ppsz_command[3], "play" ) &&
609 strcmp( ppsz_command[3], "stop" ) &&
610 strcmp( ppsz_command[3], "pause" ) &&
611 strcmp( ppsz_command[3], "seek" ) )
615 psz_command = ppsz_command[i_index];
617 if( i_command >= i_index + 2 ) psz_arg = ppsz_command[i_index + 1];
619 vlm_MediaControl( p_vlm, p_media, psz_instance, psz_command,
621 p_message = vlm_MessageNew( "control", NULL );
626 else if( !strcmp(ppsz_command[0], "save") )
628 if( i_command != 2 ) goto syntax_error;
630 if( vlm_Save( p_vlm, ppsz_command[1] ) )
632 p_message = vlm_MessageNew( "save", "Unable to save to file" );
637 p_message = vlm_MessageNew( "save", NULL );
642 else if( !strcmp(ppsz_command[0], "export" ) )
646 if( i_command != 1 ) goto syntax_error;
648 p_message = vlm_MessageNew( "export", psz_buf = Save( p_vlm ) );
654 else if( !strcmp(ppsz_command[0], "load") )
656 if( i_command != 2 ) goto syntax_error;
658 switch( vlm_Load( p_vlm, ppsz_command[1] ) )
661 p_message = vlm_MessageNew( "load", NULL );
664 p_message = vlm_MessageNew( "load", "Read file error" );
668 vlm_MessageNew( "load", "Error while loading file" );
672 vlm_MessageNew( "load", "Unable to load from file" );
679 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
683 /* Common code between "new" and "setup" */
684 if( !strcmp(ppsz_command[0], "new") ||
685 !strcmp(ppsz_command[0], "setup") )
687 size_t i_command_start = strcmp(ppsz_command[0], "new") ? 2 : 3;
688 vlm_media_t *p_media;
689 vlm_schedule_t *p_schedule;
691 if( i_command < i_command_start ) goto syntax_error;
693 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
694 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
696 if( !p_media && !p_schedule )
698 p_message = vlm_MessageNew( ppsz_command[0], "%s unknown",
703 if( p_schedule != NULL )
707 for( i = i_command_start ; i < i_command ; i++ )
709 if( !strcmp( ppsz_command[i], "enabled" ) ||
710 !strcmp( ppsz_command[i], "disabled" ) )
712 vlm_ScheduleSetup( p_schedule, ppsz_command[i], NULL );
715 /* Beware: everything behind append is considered as
717 else if( !strcmp( ppsz_command[i], "append" ) )
721 if( ++i >= i_command ) break;
723 for( j = i + 1; j < i_command; j++ )
726 realloc( ppsz_command[i], strlen(ppsz_command[i]) +
727 strlen(ppsz_command[j]) + 1 + 1 );
728 strcat( ppsz_command[i], " " );
729 strcat( ppsz_command[i], ppsz_command[j] );
732 vlm_ScheduleSetup( p_schedule, ppsz_command[i - 1],
738 if( i + 1 >= i_command && !strcmp(ppsz_command[0], "new") )
740 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
742 vlm_MessageNew( ppsz_command[0],
743 "Wrong properties syntax" );
746 else if( i + 1 >= i_command )
749 vlm_MessageNew( ppsz_command[0],
750 "Wrong properties syntax" );
754 vlm_ScheduleSetup( p_schedule, ppsz_command[i],
761 else if( p_media != NULL )
765 for( i = i_command_start ; i < i_command ; i++ )
767 if( !strcmp( ppsz_command[i], "enabled" ) ||
768 !strcmp( ppsz_command[i], "disabled" ) )
770 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
772 else if( i + 1 >= i_command &&
773 !strcmp( ppsz_command[i], "mux") )
775 if( p_media->i_type != VOD_TYPE )
777 p_message = vlm_MessageNew( ppsz_command[0],
778 "mux only available for broadcast" );
782 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
787 else if( !strcmp( ppsz_command[i], "loop" ) ||
788 !strcmp( ppsz_command[i], "unloop" ) )
790 if( p_media->i_type != BROADCAST_TYPE )
792 p_message = vlm_MessageNew( ppsz_command[0],
793 "loop only available for broadcast" );
797 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
802 if( ( (i + 1) >= i_command ) &&
803 !strcmp(ppsz_command[0], "new") )
805 vlm_MediaDelete( p_vlm, p_media, NULL );
807 vlm_MessageNew( ppsz_command[0],
808 "Wrong properties syntax" );
811 else if( (i + 1) >= i_command )
814 vlm_MessageNew( ppsz_command[0],
815 "Wrong properties syntax" );
819 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
826 p_message = vlm_MessageNew( ppsz_command[0], NULL );
831 *pp_message = p_message;
835 p_message = vlm_MessageNew( ppsz_command[0], "Wrong command syntax" );
838 *pp_message = p_message;
842 vlm_media_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
846 for( i = 0; i < vlm->i_media; i++ )
848 if( strcmp( psz_name, vlm->media[i]->psz_name ) == 0 )
850 return vlm->media[i];
857 /*****************************************************************************
859 *****************************************************************************/
860 static vlm_media_instance_t *
861 vlm_MediaInstanceSearch( vlm_t *vlm, vlm_media_t *media,
862 const char *psz_name )
866 for( i = 0; i < media->i_instance; i++ )
868 if( ( !psz_name && !media->instance[i]->psz_name ) ||
869 ( psz_name && media->instance[i]->psz_name &&
870 !strcmp( psz_name, media->instance[i]->psz_name ) ) )
872 return media->instance[i];
879 vlm_media_t *vlm_MediaNew( vlm_t *vlm, const char *psz_name, int i_type )
881 vlm_media_t *media = malloc( sizeof( vlm_media_t ) );
885 msg_Err( vlm, "out of memory" );
889 /* Check if we need to load the VOD server */
890 if( i_type == VOD_TYPE && !vlm->i_vod )
892 vlm->vod = vlc_object_create( vlm, VLC_OBJECT_VOD );
893 vlc_object_attach( vlm->vod, vlm );
894 vlm->vod->p_module = module_Need( vlm->vod, "vod server", 0, 0 );
895 if( !vlm->vod->p_module )
897 msg_Err( vlm, "cannot find vod server" );
898 vlc_object_detach( vlm->vod );
899 vlc_object_destroy( vlm->vod );
905 vlm->vod->p_data = vlm;
906 vlm->vod->pf_media_control = vlm_MediaVodControl;
909 if( i_type == VOD_TYPE ) vlm->i_vod++;
911 media->psz_name = strdup( psz_name );
912 media->b_enabled = VLC_FALSE;
913 media->b_loop = VLC_FALSE;
914 media->vod_media = NULL;
915 media->psz_vod_output = NULL;
916 media->psz_mux = NULL;
919 media->psz_output = NULL;
921 media->option = NULL;
922 media->i_type = i_type;
923 media->i_instance = 0;
924 media->instance = NULL;
926 input_ItemInit( VLC_OBJECT(vlm), &media->item );
928 TAB_APPEND( vlm->i_media, vlm->media, media );
933 /* for now, simple delete. After, del with options (last arg) */
934 void vlm_MediaDelete( vlm_t *vlm, vlm_media_t *media, const char *psz_name )
936 if( media == NULL ) return;
938 while( media->i_instance )
940 vlm_media_instance_t *p_instance = media->instance[0];
941 vlm_MediaControl( vlm, media, p_instance->psz_name, "stop", 0 );
944 TAB_REMOVE( vlm->i_media, vlm->media, media );
946 if( media->i_type == VOD_TYPE )
948 vlm_MediaSetup( vlm, media, "disabled", 0 );
952 /* Check if we need to unload the VOD server */
953 if( media->i_type == VOD_TYPE && !vlm->i_vod )
955 module_Unneed( vlm->vod, vlm->vod->p_module );
956 vlc_object_detach( vlm->vod );
957 vlc_object_destroy( vlm->vod );
961 if( vlm->i_media == 0 && vlm->media ) free( vlm->media );
963 free( media->psz_name );
965 while( media->i_input-- ) free( media->input[media->i_input] );
966 if( media->input ) free( media->input );
968 if( media->psz_output ) free( media->psz_output );
969 if( media->psz_mux ) free( media->psz_mux );
971 while( media->i_option-- ) free( media->option[media->i_option] );
972 if( media->option ) free( media->option );
974 input_ItemClean( &media->item );
979 int vlm_MediaSetup( vlm_t *vlm, vlm_media_t *media, const char *psz_cmd,
980 const char *psz_value )
982 if( !psz_cmd) return VLC_EGENERIC;
984 if( !strcmp( psz_cmd, "loop" ) )
986 media->b_loop = VLC_TRUE;
988 else if( !strcmp( psz_cmd, "unloop" ) )
990 media->b_loop = VLC_FALSE;
992 else if( !strcmp( psz_cmd, "enabled" ) )
994 media->b_enabled = VLC_TRUE;
996 else if( !strcmp( psz_cmd, "disabled" ) )
998 media->b_enabled = VLC_FALSE;
1000 else if( !strcmp( psz_cmd, "mux" ) )
1002 if( media->psz_mux ) free( media->psz_mux );
1003 media->psz_mux = NULL;
1004 if( psz_value ) media->psz_mux = strdup( psz_value );
1006 else if( !strcmp( psz_cmd, "input" ) )
1010 if( psz_value != NULL && strlen(psz_value) > 1 &&
1011 ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
1012 ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
1013 psz_value[ strlen(psz_value) - 1 ] == '\"' ) )
1015 input = malloc( strlen(psz_value) - 1 );
1017 memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
1018 input[ strlen(psz_value) - 2 ] = '\0';
1022 input = strdup( psz_value );
1025 TAB_APPEND( media->i_input, media->input, input );
1027 else if( !strcmp( psz_cmd, "inputdel" ) && !strcmp( psz_value, "all" ) )
1029 while( media->i_input > 0 )
1031 TAB_REMOVE( media->i_input, media->input, media->input[0] );
1034 else if( !strcmp( psz_cmd, "inputdel" ) )
1039 if( psz_value != NULL && strlen(psz_value) > 1 &&
1040 ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
1041 ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
1042 psz_value[ strlen(psz_value) - 1 ] == '\"' ) )
1044 input = malloc( strlen(psz_value) - 1 );
1046 memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
1047 input[ strlen(psz_value) - 2 ] = '\0';
1051 input = strdup( psz_value );
1054 for( i = 0; i < media->i_input; i++ )
1056 if( !strcmp( input, media->input[i] ) )
1058 TAB_REMOVE( media->i_input, media->input, media->input[i] );
1063 else if( !strcmp( psz_cmd, "inputdeln" ) )
1065 int index = atoi( psz_value );
1066 if( index > 0 && index <= media->i_input )
1068 TAB_REMOVE( media->i_input, media->input, media->input[index-1] );
1071 else if( !strcmp( psz_cmd, "output" ) )
1073 if( media->psz_output != NULL )
1075 free( media->psz_output );
1077 media->psz_output = strdup( psz_value );
1079 else if( !strcmp( psz_cmd, "option" ) )
1082 psz_option = strdup( psz_value );
1084 TAB_APPEND( media->i_option, media->option, psz_option );
1088 return VLC_EGENERIC;
1091 /* Check if we need to create/delete a vod media */
1092 if( media->i_type == VOD_TYPE )
1094 if( !media->b_enabled && media->vod_media )
1096 vlm->vod->pf_media_del( vlm->vod, media->vod_media );
1097 media->vod_media = 0;
1099 else if( media->b_enabled && !media->vod_media && media->i_input )
1101 /* Pre-parse the input */
1102 input_thread_t *p_input;
1107 input_ItemClean( &media->item );
1108 input_ItemInit( VLC_OBJECT(vlm), &media->item );
1110 if( media->psz_output )
1111 asprintf( &psz_output, "%s:description", media->psz_output );
1113 asprintf( &psz_output, "#description" );
1115 media->item.psz_uri = strdup( media->input[0] );
1116 media->item.ppsz_options = malloc( sizeof( char* ) );
1117 asprintf( &media->item.ppsz_options[0], "sout=%s", psz_output);
1118 media->item.i_options = 1;
1119 for( i = 0; i < media->i_option; i++ )
1121 media->item.i_options++;
1122 media->item.ppsz_options =
1123 realloc( media->item.ppsz_options,
1124 media->item.i_options * sizeof( char* ) );
1125 media->item.ppsz_options[ media->item.i_options - 1 ] =
1126 strdup( media->option[i] );
1129 asprintf( &psz_header, _("Media: %s"), media->psz_name );
1131 if( (p_input = input_CreateThread2( vlm, &media->item, psz_header
1134 while( !p_input->b_eof && !p_input->b_error ) msleep( 100000 );
1136 input_StopThread( p_input );
1137 input_DestroyThread( p_input );
1138 vlc_object_detach( p_input );
1139 vlc_object_destroy( p_input );
1144 if( media->psz_mux )
1147 es_format_t es, *p_es = &es;
1150 sprintf( fourcc, "%4.4s", media->psz_mux );
1151 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
1152 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
1157 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
1160 vlm->vod->pf_media_new( vlm->vod, media->psz_name, &item );
1165 vlm->vod->pf_media_new( vlm->vod, media->psz_name,
1173 int vlm_MediaControl( vlm_t *vlm, vlm_media_t *media, const char *psz_id,
1174 const char *psz_command, const char *psz_args )
1176 vlm_media_instance_t *p_instance;
1180 p_instance = vlm_MediaInstanceSearch( vlm, media, psz_id );
1182 if( !strcmp( psz_command, "play" ) )
1184 if( !media->b_enabled || media->i_input == 0 ) return 0;
1188 p_instance = malloc( sizeof(vlm_media_instance_t) );
1189 if( !p_instance ) return VLC_EGENERIC;
1190 memset( p_instance, 0, sizeof(vlm_media_instance_t) );
1191 input_ItemInit( VLC_OBJECT(vlm), &p_instance->item );
1192 p_instance->p_input = NULL;
1194 if( ( media->psz_output != NULL ) || ( media->psz_vod_output != NULL ) )
1196 p_instance->item.ppsz_options = malloc( sizeof( char* ) );
1197 asprintf( &p_instance->item.ppsz_options[0], "sout=%s%s%s",
1198 media->psz_output ? media->psz_output : "",
1199 (media->psz_output && media->psz_vod_output) ?
1200 ":" : media->psz_vod_output ? "#" : "",
1201 media->psz_vod_output ? media->psz_vod_output : "" );
1202 p_instance->item.i_options = 1;
1205 for( i = 0; i < media->i_option; i++ )
1207 p_instance->item.i_options++;
1208 p_instance->item.ppsz_options =
1209 realloc( p_instance->item.ppsz_options,
1210 p_instance->item.i_options * sizeof( char* ) );
1211 p_instance->item.ppsz_options[p_instance->item.i_options - 1] =
1212 strdup( media->option[i] );
1215 p_instance->psz_name = psz_id ? strdup( psz_id ) : NULL;
1216 TAB_APPEND( media->i_instance, media->instance, p_instance );
1219 if( ( psz_args && sscanf(psz_args, "%d", &i) == 1 ) && ( i < media->i_input ) )
1221 p_instance->i_index = i;
1224 if( p_instance->item.psz_uri ) free( p_instance->item.psz_uri );
1225 p_instance->item.psz_uri =
1226 strdup( media->input[p_instance->i_index] );
1228 if( p_instance->p_input )
1230 input_StopThread( p_instance->p_input );
1231 input_DestroyThread( p_instance->p_input );
1232 vlc_object_detach( p_instance->p_input );
1233 vlc_object_destroy( p_instance->p_input );
1236 asprintf( &psz_header, _("Media: %s"), media->psz_name );
1237 p_instance->p_input = input_CreateThread2( vlm, &p_instance->item,
1239 if( !p_instance->p_input )
1241 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1242 input_ItemClean( &p_instance->item );
1243 if( p_instance->psz_name ) free( p_instance->psz_name );
1250 if( !p_instance ) return VLC_EGENERIC;
1252 if( !strcmp( psz_command, "seek" ) )
1257 float f_value = i18n_atof( psz_args );
1258 if( psz_args[0] == '+' || psz_args[0] == '-' )
1262 if( strstr( psz_args, "ms" ) )
1265 int64_t i_msec = 1000 * (int64_t)atoi( psz_args );
1268 var_SetTime( p_instance->p_input, "time-offset",
1271 else if( i_msec >= 0
1272 && i_msec < var_GetTime( p_instance->p_input, "length" ) )
1274 var_SetTime( p_instance->p_input, "time",
1278 else if( strchr( psz_args, 's' ) )
1281 int64_t i_sec = 1000000 * (int64_t)atoi( psz_args );
1284 var_SetTime( p_instance->p_input, "time-offset",
1288 && i_sec < var_GetTime( p_instance->p_input, "length" ) )
1290 var_SetTime( p_instance->p_input, "time",
1300 float f_orig = var_GetFloat( p_instance->p_input,
1304 if( f_value >= 0.0 && f_value <= 1.0 )
1306 var_SetFloat( p_instance->p_input, "position",
1313 else if( !strcmp( psz_command, "rewind" ) )
1320 f_scale = i18n_atof( psz_args );
1321 f_pos = var_GetFloat( p_instance->p_input, "position" );
1322 f_pos -= (f_scale / 1000.0);
1325 var_SetFloat( p_instance->p_input, "position", f_pos );
1329 else if( !strcmp( psz_command, "forward" ) )
1336 f_scale = i18n_atof( psz_args );
1337 f_pos = var_GetFloat( p_instance->p_input, "position" );
1338 f_pos += (f_scale / 1000.0);
1341 var_SetFloat( p_instance->p_input, "position", f_pos );
1345 else if( !strcmp( psz_command, "stop" ) )
1347 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1349 if( p_instance->p_input )
1351 input_StopThread( p_instance->p_input );
1352 input_DestroyThread( p_instance->p_input );
1353 vlc_object_detach( p_instance->p_input );
1354 vlc_object_destroy( p_instance->p_input );
1357 input_ItemClean( &p_instance->item );
1358 if( p_instance->psz_name ) free( p_instance->psz_name );
1363 else if( !strcmp( psz_command, "pause" ) )
1367 if( !p_instance->p_input ) return VLC_SUCCESS;
1369 i_state = var_GetInteger( p_instance->p_input, "state" );
1371 if( i_state == PAUSE_S ) i_state = PLAYING_S;
1372 else i_state = PAUSE_S;
1373 var_SetInteger( p_instance->p_input, "state", i_state );
1378 return VLC_EGENERIC;
1381 /*****************************************************************************
1383 *****************************************************************************/
1384 static int64_t vlm_Date(void)
1389 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1395 vlm_schedule_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
1397 vlm_schedule_t *p_sched = malloc( sizeof( vlm_schedule_t ) );
1409 p_sched->psz_name = strdup( psz_name );
1410 p_sched->b_enabled = VLC_FALSE;
1411 p_sched->i_command = 0;
1412 p_sched->command = NULL;
1413 p_sched->i_date = 0;
1414 p_sched->i_period = 0;
1415 p_sched->i_repeat = -1;
1417 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1422 /* for now, simple delete. After, del with options (last arg) */
1423 void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_t *sched,
1424 const char *psz_name )
1426 if( sched == NULL ) return;
1428 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1430 if( vlm->i_schedule == 0 && vlm->schedule ) free( vlm->schedule );
1431 free( sched->psz_name );
1432 while( sched->i_command )
1434 char *psz_cmd = sched->command[0];
1435 TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1441 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1445 for( i = 0; i < vlm->i_schedule; i++ )
1447 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1449 return vlm->schedule[i];
1456 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1457 int vlm_ScheduleSetup( vlm_schedule_t *schedule, const char *psz_cmd,
1458 const char *psz_value )
1460 if( !strcmp( psz_cmd, "enabled" ) )
1462 schedule->b_enabled = VLC_TRUE;
1464 else if( !strcmp( psz_cmd, "disabled" ) )
1466 schedule->b_enabled = VLC_FALSE;
1468 #if !defined( UNDER_CE )
1469 else if( !strcmp( psz_cmd, "date" ) )
1475 time.tm_sec = 0; /* seconds */
1476 time.tm_min = 0; /* minutes */
1477 time.tm_hour = 0; /* hours */
1478 time.tm_mday = 0; /* day of the month */
1479 time.tm_mon = 0; /* month */
1480 time.tm_year = 0; /* year */
1481 time.tm_wday = 0; /* day of the week */
1482 time.tm_yday = 0; /* day in the year */
1483 time.tm_isdst = -1; /* daylight saving time */
1485 /* date should be year/month/day-hour:minutes:seconds */
1486 p = strchr( psz_value, '-' );
1488 if( !strcmp( psz_value, "now" ) )
1490 schedule->i_date = 0;
1492 else if( (p == NULL) && sscanf( psz_value, "%d:%d:%d", &time.tm_hour,
1493 &time.tm_min, &time.tm_sec ) != 3 )
1494 /* it must be a hour:minutes:seconds */
1502 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1520 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1526 time.tm_mon = i - 1;
1530 time.tm_year = i - 1900;
1531 time.tm_mon = j - 1;
1538 date = mktime( &time );
1539 schedule->i_date = ((mtime_t) date) * 1000000;
1542 else if( !strcmp( psz_cmd, "period" ) )
1546 const char *psz_time = NULL, *psz_date = NULL;
1550 /* First, if date or period are modified, repeat should be equal to -1 */
1551 schedule->i_repeat = -1;
1553 time.tm_sec = 0; /* seconds */
1554 time.tm_min = 0; /* minutes */
1555 time.tm_hour = 0; /* hours */
1556 time.tm_mday = 0; /* day of the month */
1557 time.tm_mon = 0; /* month */
1558 time.tm_year = 0; /* year */
1559 time.tm_wday = 0; /* day of the week */
1560 time.tm_yday = 0; /* day in the year */
1561 time.tm_isdst = -1; /* daylight saving time */
1563 /* date should be year/month/day-hour:minutes:seconds */
1564 p = strchr( psz_value, '-' );
1567 psz_date = psz_value;
1572 psz_time = psz_value;
1575 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1594 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1613 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1614 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1615 schedule->i_period = ((mtime_t) date) * 1000000;
1617 #endif /* UNDER_CE */
1618 else if( !strcmp( psz_cmd, "repeat" ) )
1622 if( sscanf( psz_value, "%d", &i ) == 1 )
1624 schedule->i_repeat = i;
1631 else if( !strcmp( psz_cmd, "append" ) )
1633 char *command = strdup( psz_value );
1635 TAB_APPEND( schedule->i_command, schedule->command, command );
1644 /*****************************************************************************
1645 * Message handling functions
1646 *****************************************************************************/
1647 vlm_message_t *vlm_MessageNew( const char *psz_name,
1648 const char *psz_format, ... )
1650 vlm_message_t *p_message;
1653 if( !psz_name ) return NULL;
1655 p_message = malloc( sizeof(vlm_message_t) );
1661 p_message->psz_value = 0;
1665 va_start( args, psz_format );
1666 if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1675 p_message->psz_name = strdup( psz_name );
1676 p_message->i_child = 0;
1677 p_message->child = NULL;
1682 void vlm_MessageDelete( vlm_message_t *p_message )
1684 if( p_message->psz_name ) free( p_message->psz_name );
1685 if( p_message->psz_value ) free( p_message->psz_value );
1686 while( p_message->i_child-- )
1687 vlm_MessageDelete( p_message->child[p_message->i_child] );
1688 if( p_message->child ) free( p_message->child );
1693 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1694 vlm_message_t *p_child )
1696 if( p_message == NULL ) return NULL;
1700 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1706 /*****************************************************************************
1707 * Misc utility functions
1708 *****************************************************************************/
1709 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_t *media,
1710 vlm_schedule_t *schedule,
1711 const char *psz_filter )
1717 vlm_message_t *msg_media;
1718 vlm_message_t *msg_child;
1720 msg = vlm_MessageNew( "show", NULL );
1721 msg_media = vlm_MessageAdd( msg, vlm_MessageNew( media->psz_name, 0 ));
1723 vlm_MessageAdd( msg_media,
1724 vlm_MessageNew( "type", media->i_type == VOD_TYPE ?
1725 "vod" : "broadcast" ) );
1726 vlm_MessageAdd( msg_media,
1727 vlm_MessageNew( "enabled", media->b_enabled ?
1730 vlm_MessageAdd( msg_media,
1731 vlm_MessageNew( "loop", media->b_loop ?
1734 if( media->i_type == VOD_TYPE && media->psz_mux )
1735 vlm_MessageAdd( msg_media,
1736 vlm_MessageNew( "mux", media->psz_mux ) );
1738 msg_child = vlm_MessageAdd( msg_media,
1739 vlm_MessageNew( "inputs", NULL ) );
1741 for( i = 0; i < media->i_input; i++ )
1743 vlm_MessageAdd( msg_child,
1744 vlm_MessageNew( media->input[i], NULL ) );
1747 vlm_MessageAdd( msg_media,
1748 vlm_MessageNew( "output", media->psz_output ?
1749 media->psz_output : "" ) );
1751 msg_child = vlm_MessageAdd( msg_media, vlm_MessageNew( "options", 0 ));
1753 for( i = 0; i < media->i_option; i++ )
1755 vlm_MessageAdd( msg_child, vlm_MessageNew( media->option[i], 0 ) );
1758 msg_child = vlm_MessageAdd( msg_media,
1759 vlm_MessageNew( "instances", NULL ) );
1761 for( i = 0; i < media->i_instance; i++ )
1763 vlm_media_instance_t *p_instance = media->instance[i];
1765 vlm_message_t *msg_instance;
1768 if( !p_instance->p_input ) val.i_int = END_S;
1769 else var_Get( p_instance->p_input, "state", &val );
1771 msg_instance = vlm_MessageNew( "instance" , NULL );
1772 vlm_MessageAdd( msg_instance, vlm_MessageNew( "name" , p_instance->psz_name ? p_instance->psz_name : "default" ) );
1773 vlm_MessageAdd( msg_instance, vlm_MessageNew( "state",
1774 val.i_int == PLAYING_S ? "playing" :
1775 val.i_int == PAUSE_S ? "paused" :
1777 #define APPEND_INPUT_INFO( a, format, type ) \
1778 asprintf( &psz_tmp, format, \
1779 var_Get ## type( p_instance->p_input, a ) ); \
1780 vlm_MessageAdd( msg_instance, vlm_MessageNew( a, psz_tmp ) ); \
1782 APPEND_INPUT_INFO( "position", "%f", Float );
1783 APPEND_INPUT_INFO( "time", I64Fi, Time );
1784 APPEND_INPUT_INFO( "length", I64Fi, Time );
1785 APPEND_INPUT_INFO( "rate", "%d", Integer );
1786 APPEND_INPUT_INFO( "title", "%d", Integer );
1787 APPEND_INPUT_INFO( "chapter", "%d", Integer );
1788 APPEND_INPUT_INFO( "seekable", "%d", Bool );
1789 #undef APPEND_INPUT_INFO
1790 asprintf( &psz_tmp, "%d", p_instance->i_index + 1 );
1791 vlm_MessageAdd( msg_instance, vlm_MessageNew( "playlistindex", psz_tmp ) );
1793 vlm_MessageAdd( msg_child, msg_instance );
1800 else if( schedule != NULL )
1804 vlm_message_t *msg_schedule;
1805 vlm_message_t *msg_child;
1808 msg = vlm_MessageNew( "show", NULL );
1810 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, 0 ) );
1812 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1814 vlm_MessageAdd( msg_schedule,
1815 vlm_MessageNew( "enabled", schedule->b_enabled ?
1818 #if !defined( UNDER_CE )
1819 if( schedule->i_date != 0 )
1822 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1825 #ifdef HAVE_LOCALTIME_R
1826 localtime_r( &i_time, &date);
1828 struct tm *p_date = localtime( &i_time );
1832 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1833 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1834 date.tm_hour, date.tm_min, date.tm_sec );
1836 vlm_MessageAdd( msg_schedule,
1837 vlm_MessageNew( "date", psz_date ) );
1842 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1845 if( schedule->i_period != 0 )
1847 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1850 date.tm_sec = (int)( i_time % 60 );
1851 i_time = i_time / 60;
1852 date.tm_min = (int)( i_time % 60 );
1853 i_time = i_time / 60;
1854 date.tm_hour = (int)( i_time % 24 );
1855 i_time = i_time / 24;
1856 date.tm_mday = (int)( i_time % 30 );
1857 i_time = i_time / 30;
1858 /* okay, okay, months are not always 30 days long */
1859 date.tm_mon = (int)( i_time % 12 );
1860 i_time = i_time / 12;
1861 date.tm_year = (int)i_time;
1863 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1864 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1866 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1870 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1872 #endif /* UNDER_CE */
1874 sprintf( buffer, "%d", schedule->i_repeat );
1875 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1878 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", 0) );
1880 for( i = 0; i < schedule->i_command; i++ )
1882 vlm_MessageAdd( msg_child,
1883 vlm_MessageNew( schedule->command[i], NULL ) );
1890 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1894 vlm_message_t *msg_child;
1895 int i_vod = 0, i_broadcast = 0;
1898 for( i = 0; i < vlm->i_media; i++ )
1900 if( vlm->media[i]->i_type == VOD_TYPE )
1906 asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1908 msg = vlm_MessageNew( "show", NULL );
1909 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "media", psz_count ) );
1912 for( i = 0; i < vlm->i_media; i++ )
1914 vlm_media_t *m = vlm->media[i];
1915 vlm_message_t *msg_media, *msg_instance;
1917 msg_media = vlm_MessageAdd( msg_child,
1918 vlm_MessageNew( m->psz_name, 0 ) );
1920 vlm_MessageAdd( msg_media,
1921 vlm_MessageNew( "type", m->i_type == VOD_TYPE ?
1922 "vod" : "broadcast" ) );
1924 vlm_MessageAdd( msg_media,
1925 vlm_MessageNew( "enabled", m->b_enabled ?
1928 if( m->i_type == VOD_TYPE && m->psz_mux )
1929 vlm_MessageAdd( msg_media,
1930 vlm_MessageNew( "mux", m->psz_mux ) );
1932 msg_instance = vlm_MessageAdd( msg_media,
1933 vlm_MessageNew( "instances", 0 ) );
1935 for( j = 0; j < m->i_instance; j++ )
1937 vlm_media_instance_t *p_instance = m->instance[j];
1940 if( !p_instance->p_input ) val.i_int = END_S;
1941 else var_Get( p_instance->p_input, "state", &val );
1943 vlm_MessageAdd( msg_instance,
1944 vlm_MessageNew( p_instance->psz_name ?
1945 p_instance->psz_name : "default",
1946 val.i_int == PLAYING_S ? "playing" :
1947 val.i_int == PAUSE_S ? "paused" :
1955 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1959 vlm_message_t *msg_child;
1961 msg = vlm_MessageNew( "show", NULL );
1962 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", NULL ) );
1964 for( i = 0; i < vlm->i_schedule; i++ )
1966 vlm_schedule_t *s = vlm->schedule[i];
1967 vlm_message_t *msg_schedule;
1968 mtime_t i_time, i_next_date;
1970 msg_schedule = vlm_MessageAdd( msg_child,
1971 vlm_MessageNew( s->psz_name, 0 ) );
1972 vlm_MessageAdd( msg_schedule,
1973 vlm_MessageNew( "enabled", s->b_enabled ?
1976 /* calculate next date */
1977 i_time = vlm_Date();
1978 i_next_date = s->i_date;
1980 if( s->i_period != 0 )
1983 while( s->i_date + j * s->i_period <= i_time &&
1989 i_next_date = s->i_date + j * s->i_period;
1992 if( i_next_date > i_time )
1994 time_t i_date = (time_t) (i_next_date / 1000000) ;
1996 #if !defined( UNDER_CE )
1999 ctime_r( &i_date, psz_date );
2001 char *psz_date = ctime( &i_date );
2004 vlm_MessageAdd( msg_schedule,
2005 vlm_MessageNew( "next launch", psz_date ) );
2013 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
2015 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
2016 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
2018 vlm_MessageAdd( show1, show2->child[0] );
2020 /* We must destroy the parent node "show" of show2
2021 * and not the children */
2022 free( show2->psz_name );
2030 return vlm_MessageNew( "show", NULL );
2034 static vlm_message_t *vlm_Help( vlm_t *vlm, char *psz_filter )
2036 vlm_message_t *message, *message_child;
2038 #define MessageAdd( a ) \
2039 vlm_MessageAdd( message, vlm_MessageNew( a, NULL ) );
2040 #define MessageAddChild( a ) \
2041 vlm_MessageAdd( message_child, vlm_MessageNew( a, NULL ) );
2043 if( psz_filter == NULL )
2045 message = vlm_MessageNew( "help", NULL );
2047 message_child = MessageAdd( "Commands Syntax:" );
2048 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
2049 MessageAddChild( "setup (name) (properties)" );
2050 MessageAddChild( "show [(name)|media|schedule]" );
2051 MessageAddChild( "del (name)|all|media|schedule" );
2052 MessageAddChild( "control (name) [instance_name] (command)" );
2053 MessageAddChild( "save (config_file)" );
2054 MessageAddChild( "export" );
2055 MessageAddChild( "load (config_file)" );
2057 message_child = MessageAdd( "Media Proprieties Syntax:" );
2058 MessageAddChild( "input (input_name)" );
2059 MessageAddChild( "inputdel (input_name)|all" );
2060 MessageAddChild( "inputdeln input_number" );
2061 MessageAddChild( "output (output_name)" );
2062 MessageAddChild( "option (option_name)[=value]" );
2063 MessageAddChild( "enabled|disabled" );
2064 MessageAddChild( "loop|unloop (broadcast only)" );
2065 MessageAddChild( "mux (mux_name)" );
2067 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
2068 MessageAddChild( "enabled|disabled" );
2069 MessageAddChild( "append (command_until_rest_of_the_line)" );
2070 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
2072 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
2073 "(days)-(hours):(minutes):(seconds)" );
2074 MessageAddChild( "repeat (number_of_repetitions)" );
2076 message_child = MessageAdd( "Control Commands Syntax:" );
2077 MessageAddChild( "play" );
2078 MessageAddChild( "pause" );
2079 MessageAddChild( "stop" );
2080 MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](miliseconds)ms" );
2085 return vlm_MessageNew( "help", NULL );
2088 /*****************************************************************************
2089 * Config handling functions
2090 *****************************************************************************/
2091 static int Load( vlm_t *vlm, char *file )
2096 while( *pf != '\0' )
2098 vlm_message_t *message = NULL;
2101 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
2106 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
2110 if( pf[i_end] == '\n' ) i_end++;
2113 if( *pf && ExecuteCommand( vlm, pf, &message ) )
2117 if( message->psz_value )
2118 msg_Err( vlm, "Load error on line %d: %s: %s",
2119 i_line, message->psz_name, message->psz_value );
2120 vlm_MessageDelete( message );
2124 if( message ) vlm_MessageDelete( message );
2133 static char *Save( vlm_t *vlm )
2136 char psz_header[] = "\n"
2137 "# VLC media player VLM command batch\n"
2138 "# http://www.videolan.org/vlc/\n\n" ;
2141 int i_length = strlen( psz_header );
2143 for( i = 0; i < vlm->i_media; i++ )
2145 vlm_media_t *media = vlm->media[i];
2147 if( media->i_type == VOD_TYPE )
2149 i_length += strlen( "new vod " ) + strlen(media->psz_name);
2153 i_length += strlen( "new broadcast " ) + strlen(media->psz_name);
2156 if( media->b_enabled == VLC_TRUE )
2158 i_length += strlen( "enabled" );
2162 i_length += strlen( "disabled" );
2165 if( media->b_loop == VLC_TRUE )
2167 i_length += strlen( " loop\n" );
2171 i_length += strlen( "\n" );
2174 for( j = 0; j < media->i_input; j++ )
2176 i_length += strlen( "setup input \"\"\n" ) +
2177 strlen( media->psz_name ) + strlen( media->input[j] );
2180 if( media->psz_output != NULL )
2182 i_length += strlen(media->psz_name) + strlen(media->psz_output) +
2183 strlen( "setup output \n" );
2186 for( j=0 ; j < media->i_option ; j++ )
2188 i_length += strlen(media->psz_name) + strlen(media->option[j]) +
2189 strlen("setup option \n");
2193 for( i = 0; i < vlm->i_schedule; i++ )
2195 vlm_schedule_t *schedule = vlm->schedule[i];
2197 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
2199 if( schedule->b_enabled == VLC_TRUE )
2201 i_length += strlen( "date //-:: enabled\n" ) + 14;
2205 i_length += strlen( "date //-:: disabled\n" ) + 14;
2209 if( schedule->i_period != 0 )
2211 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
2212 strlen( "period //-::\n" ) + 14;
2215 if( schedule->i_repeat >= 0 )
2219 sprintf( buffer, "%d", schedule->i_repeat );
2220 i_length += strlen( "setup repeat \n" ) +
2221 strlen( schedule->psz_name ) + strlen( buffer );
2228 for( j = 0; j < schedule->i_command; j++ )
2230 i_length += strlen( "setup append \n" ) +
2231 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
2236 /* Don't forget the '\0' */
2238 /* now we have the length of save */
2240 p = save = malloc( i_length );
2243 p += sprintf( p, "%s", psz_header );
2245 /* finally we can write in it */
2246 for( i = 0; i < vlm->i_media; i++ )
2248 vlm_media_t *media = vlm->media[i];
2250 if( media->i_type == VOD_TYPE )
2252 p += sprintf( p, "new %s vod ", media->psz_name);
2256 p += sprintf( p, "new %s broadcast ", media->psz_name);
2259 if( media->b_enabled == VLC_TRUE )
2261 p += sprintf( p, "enabled" );
2265 p += sprintf( p, "disabled" );
2268 if( media->b_loop == VLC_TRUE )
2270 p += sprintf( p, " loop\n" );
2274 p += sprintf( p, "\n" );
2277 for( j = 0; j < media->i_input; j++ )
2279 p += sprintf( p, "setup %s input \"%s\"\n", media->psz_name,
2283 if( media->psz_output != NULL )
2285 p += sprintf( p, "setup %s output %s\n", media->psz_name,
2286 media->psz_output );
2289 for( j = 0; j < media->i_option; j++ )
2291 p += sprintf( p, "setup %s option %s\n", media->psz_name,
2296 /* and now, the schedule scripts */
2297 #if !defined( UNDER_CE )
2298 for( i = 0; i < vlm->i_schedule; i++ )
2300 vlm_schedule_t *schedule = vlm->schedule[i];
2302 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
2304 #ifdef HAVE_LOCALTIME_R
2305 localtime_r( &i_time, &date);
2307 struct tm *p_date = localtime( &i_time );
2311 p += sprintf( p, "new %s schedule ", schedule->psz_name);
2313 if( schedule->b_enabled == VLC_TRUE )
2315 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
2316 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2317 date.tm_hour, date.tm_min, date.tm_sec );
2321 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
2322 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2323 date.tm_hour, date.tm_min, date.tm_sec);
2326 if( schedule->i_period != 0 )
2328 p += sprintf( p, "setup %s ", schedule->psz_name );
2330 i_time = (time_t) ( schedule->i_period / 1000000 );
2332 date.tm_sec = (int)( i_time % 60 );
2333 i_time = i_time / 60;
2334 date.tm_min = (int)( i_time % 60 );
2335 i_time = i_time / 60;
2336 date.tm_hour = (int)( i_time % 24 );
2337 i_time = i_time / 24;
2338 date.tm_mday = (int)( i_time % 30 );
2339 i_time = i_time / 30;
2340 /* okay, okay, months are not always 30 days long */
2341 date.tm_mon = (int)( i_time % 12 );
2342 i_time = i_time / 12;
2343 date.tm_year = (int)i_time;
2345 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
2346 date.tm_year, date.tm_mon, date.tm_mday,
2347 date.tm_hour, date.tm_min, date.tm_sec);
2350 if( schedule->i_repeat >= 0 )
2352 p += sprintf( p, "setup %s repeat %d\n",
2353 schedule->psz_name, schedule->i_repeat );
2357 p += sprintf( p, "\n" );
2360 for( j = 0; j < schedule->i_command; j++ )
2362 p += sprintf( p, "setup %s append %s\n",
2363 schedule->psz_name, schedule->command[j] );
2367 #endif /* UNDER_CE */
2372 /*****************************************************************************
2374 *****************************************************************************/
2375 int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
2376 const char *psz_id, int i_query, va_list args )
2378 vlm_t *vlm = (vlm_t *)p_private;
2379 int i, i_ret = VLC_EGENERIC;
2381 if( !p_private || !p_vod_media ) return VLC_EGENERIC;
2383 vlc_mutex_lock( &vlm->lock );
2386 for( i = 0; i < vlm->i_media; i++ )
2388 if( p_vod_media == vlm->media[i]->vod_media ) break;
2391 if( i == vlm->i_media )
2393 vlc_mutex_unlock( &vlm->lock );
2394 return VLC_EGENERIC;
2399 case VOD_MEDIA_PLAY:
2400 vlm->media[i]->psz_vod_output = (char *)va_arg( args, char * );
2401 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "play", 0 );
2402 vlm->media[i]->psz_vod_output = 0;
2405 case VOD_MEDIA_PAUSE:
2406 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "pause", 0 );
2409 case VOD_MEDIA_STOP:
2410 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "stop", 0 );
2413 case VOD_MEDIA_SEEK:
2415 double f_pos = (double)va_arg( args, double );
2417 lldiv_t div = lldiv( f_pos * 10000000, 10000000 );
2418 sprintf( psz_pos, I64Fd".%07u", div.quot, (unsigned int) div.rem );
2419 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "seek", psz_pos);
2423 case VOD_MEDIA_REWIND:
2425 double f_scale = (double)va_arg( args, double );
2427 lldiv_t div = lldiv( f_scale * 10000000, 10000000 );
2428 sprintf( psz_scale, I64Fd".%07u", div.quot, (unsigned int) div.rem );
2429 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "rewind", psz_scale );
2433 case VOD_MEDIA_FORWARD:
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, "forward", psz_scale );
2447 vlc_mutex_unlock( &vlm->lock );
2453 /*****************************************************************************
2455 *****************************************************************************/
2456 static int Manage( vlc_object_t* p_object )
2458 vlm_t *vlm = (vlm_t*)p_object;
2460 mtime_t i_lastcheck;
2463 i_lastcheck = vlm_Date();
2467 while( !vlm->b_die )
2469 char **ppsz_scheduled_commands = NULL;
2470 int i_scheduled_commands = 0;
2471 vlc_mutex_lock( &vlm->lock );
2473 /* destroy the inputs that wants to die, and launch the next input */
2474 for( i = 0; i < vlm->i_media; i++ )
2476 vlm_media_t *p_media = vlm->media[i];
2478 for( j = 0; j < p_media->i_instance; j++ )
2480 vlm_media_instance_t *p_instance = p_media->instance[j];
2482 if( !p_instance->p_input ||
2483 ( !p_instance->p_input->b_eof &&
2484 !p_instance->p_input->b_error ) ) continue;
2486 input_StopThread( p_instance->p_input );
2487 input_DestroyThread( p_instance->p_input );
2488 vlc_object_detach( p_instance->p_input );
2489 vlc_object_destroy( p_instance->p_input );
2491 p_instance->i_index++;
2492 if( p_instance->i_index == p_media->i_input &&
2493 p_media->b_loop ) p_instance->i_index = 0;
2495 if( p_instance->i_index < p_media->i_input )
2497 /* FIXME, find a way to select the right instance */
2499 sprintf( buffer, "%d", p_instance->i_index );
2500 vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2505 if( vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2506 "stop", 0 ) == VLC_SUCCESS )
2508 j--; /* the aray is one element smaller now. */
2515 i_time = vlm_Date();
2517 for( i = 0; i < vlm->i_schedule; i++ )
2519 mtime_t i_real_date = vlm->schedule[i]->i_date;
2521 if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2523 if( vlm->schedule[i]->i_date == 0 ) // now !
2525 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2526 i_real_date = i_time;
2528 else if( vlm->schedule[i]->i_period != 0 )
2531 while( vlm->schedule[i]->i_date + j *
2532 vlm->schedule[i]->i_period <= i_lastcheck &&
2533 ( vlm->schedule[i]->i_repeat > j ||
2534 vlm->schedule[i]->i_repeat == -1 ) )
2539 i_real_date = vlm->schedule[i]->i_date + j *
2540 vlm->schedule[i]->i_period;
2543 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2545 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2547 TAB_APPEND( i_scheduled_commands,
2548 ppsz_scheduled_commands,
2549 strdup(vlm->schedule[i]->command[j] ) );
2554 while( i_scheduled_commands )
2556 vlm_message_t *message = NULL;
2557 char *psz_command = ppsz_scheduled_commands[0];
2558 ExecuteCommand( vlm, psz_command,&message );
2560 /* for now, drop the message */
2561 vlm_MessageDelete( message );
2562 TAB_REMOVE( i_scheduled_commands,
2563 ppsz_scheduled_commands,
2565 free( psz_command );
2568 i_lastcheck = i_time;
2570 vlc_mutex_unlock( &vlm->lock );
2578 #else /* ENABLE_VLM */
2580 /* We just define an empty wrapper */
2581 vlm_t *__vlm_New( vlc_object_t *a )
2583 msg_Err( a, "VideoLAN manager support is disabled" );
2586 void vlm_Delete( vlm_t *a ){}
2587 int vlm_ExecuteCommand( vlm_t *a, char *b, vlm_message_t **c ){ return -1; }
2588 void vlm_MessageDelete( vlm_message_t *a ){}
2589 vlm_media_t *vlm_MediaNew( vlm_t *a, char *b, int c ){ return NULL; }
2590 vlm_media_t *vlm_MediaSearch (vlm_t *a, const char *b ) { return NULL; }
2591 void vlm_MediaDelete( vlm_t *a, vlm_media_t *b, char *c ){}
2592 int vlm_MediaSetup( vlm_t *a, vlm_media_t *b, char *c, char *d ){ return -1; }
2593 int vlm_MediaControl( vlm_t *a, vlm_media_t *b, char *c, char *d, char *e )
2595 vlm_schedule_t * vlm_ScheduleNew( vlm_t *a, char *b ){ return NULL; }
2596 void vlm_ScheduleDelete( vlm_t *a, vlm_schedule_t *b, char *c ){}
2597 int vlm_ScheduleSetup( vlm_schedule_t *a, char *b, char *c ){ return -1; }
2598 int vlm_MediaVodControl( void *a, vod_media_t *b, char *c, int d, va_list e )
2600 int vlm_Save( vlm_t *a, char *b ){ return -1; }
2601 int vlm_Load( vlm_t *a, char *b ){ return -1; }
2603 #endif /* ENABLE_VLM */