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>
48 #include "vlm_internal.h"
50 #include <vlc_charset.h>
52 #include "../stream_output/stream_output.h"
54 /*****************************************************************************
56 *****************************************************************************/
57 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_t *, vlm_schedule_t *, const char * );
58 static vlm_message_t *vlm_Help( vlm_t *, char * );
60 static vlm_media_instance_t *vlm_MediaInstanceSearch( vlm_t *, vlm_media_t *, const char * );
62 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *, const char * );
64 static char *Save( vlm_t * );
65 static int Load( vlm_t *, char * );
66 static int ExecuteCommand( vlm_t *, const char *, vlm_message_t ** );
67 static int Manage( vlc_object_t * );
69 static int vlm_Save( vlm_t *p_vlm, const char *psz_file );
70 static int vlm_Load( vlm_t *p_vlm, const char *psz_file );
73 static vlm_schedule_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name );
74 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_t *sched, const char *psz_name );
75 static int vlm_ScheduleSetup( vlm_schedule_t *schedule, const char *psz_cmd,
76 const char *psz_value );
78 static int vlm_MediaVodControl( void *, vod_media_t *, const char *, int, va_list );
81 static vlm_media_t *vlm_MediaNew( vlm_t *, const char *, int );
82 static void vlm_MediaDelete( vlm_t *, vlm_media_t *, const char * );
83 static int vlm_MediaSetup( vlm_t *, vlm_media_t *, const char *, const char * );
84 static int vlm_MediaControl( vlm_t *, vlm_media_t *, const char *, const char *, const char * );
85 static vlm_media_t *vlm_MediaSearch( vlm_t *, const char *);
87 /*****************************************************************************
89 *****************************************************************************/
90 vlm_t *__vlm_New ( vlc_object_t *p_this )
96 /* Avoid multiple creation */
97 if( var_Create( p_this->p_libvlc_global, "vlm_mutex", VLC_VAR_MUTEX ) ||
98 var_Get( p_this->p_libvlc_global, "vlm_mutex", &lockval ) )
101 vlc_mutex_lock( lockval.p_address );
103 p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE );
106 vlc_object_yield( p_vlm );
107 vlc_mutex_unlock( lockval.p_address );
111 msg_Dbg( p_this, "creating VLM" );
113 p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM );
116 vlc_mutex_unlock( lockval.p_address );
120 vlc_mutex_init( p_this->p_libvlc, &p_vlm->lock );
121 TAB_INIT( p_vlm->i_media, p_vlm->media );
122 TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
125 vlc_object_yield( p_vlm );
126 vlc_object_attach( p_vlm, p_this->p_libvlc );
128 if( vlc_thread_create( p_vlm, "vlm thread",
129 Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
131 vlc_mutex_destroy( &p_vlm->lock );
132 vlc_object_destroy( p_vlm );
136 /* Load our configuration file */
137 psz_vlmconf = var_CreateGetString( p_vlm, "vlm-conf" );
138 if( psz_vlmconf && *psz_vlmconf )
140 vlm_message_t *p_message = NULL;
141 char *psz_buffer = NULL;
143 msg_Dbg( p_this, "loading VLM configuration" );
144 asprintf(&psz_buffer, "load %s", psz_vlmconf );
147 msg_Dbg( p_this, psz_buffer );
148 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
149 msg_Warn( p_this, "error while loading the configuration file" );
151 vlm_MessageDelete(p_message);
157 vlc_mutex_unlock( lockval.p_address );
162 /*****************************************************************************
164 *****************************************************************************/
165 void vlm_Delete( vlm_t *p_vlm )
169 var_Get( p_vlm->p_libvlc_global, "vlm_mutex", &lockval );
170 vlc_mutex_lock( lockval.p_address );
172 vlc_object_release( p_vlm );
174 if( p_vlm->i_refcount > 0 )
176 vlc_mutex_unlock( lockval.p_address );
180 p_vlm->b_die = VLC_TRUE;
181 vlc_thread_join( p_vlm );
183 vlc_object_detach( p_vlm );
185 while( p_vlm->i_media )
186 vlm_MediaDelete( p_vlm, p_vlm->media[0], NULL );
187 TAB_CLEAN( p_vlm->i_media, p_vlm->media );
189 while( p_vlm->i_schedule )
190 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
191 TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
193 vlc_mutex_destroy( &p_vlm->lock );
195 vlc_object_destroy( p_vlm );
196 vlc_mutex_unlock( lockval.p_address );
199 /*****************************************************************************
200 * vlm_ExecuteCommand:
201 *****************************************************************************/
202 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
203 vlm_message_t **pp_message)
207 vlc_mutex_lock( &p_vlm->lock );
208 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
209 vlc_mutex_unlock( &p_vlm->lock );
215 static const char quotes[] = "\"'";
217 * FindCommandEnd: look for the end of a possibly quoted string
218 * @return NULL on mal-formatted string,
219 * pointer past the last character otherwise.
221 static const char *FindCommandEnd( const char *psz_sent )
225 while( (c = *psz_sent) != '\0' )
229 if( strchr(quotes,c) ) // opening quote
231 else if( isspace(c) ) // non-escaped space
235 psz_sent++; // skip escaped character
236 if( *psz_sent == '\0' )
242 if( c == quote ) // non-escaped matching quote
244 else if( (quote == '"') && (c == '\\') )
246 psz_sent++; // skip escaped character
247 if (*psz_sent == '\0')
248 return NULL; // error, closing quote missing
254 // error (NULL) if we could not find a matching quote
255 return quote ? NULL : psz_sent;
260 * Unescape a nul-terminated string.
261 * Note that in and out can be identical.
263 * @param out output buffer (at least <strlen (in) + 1> characters long)
264 * @param in nul-terminated string to be unescaped
266 * @return 0 on success, -1 on error.
268 static int Unescape( char *out, const char *in )
272 while( (c = *in++) != '\0' )
276 if (strchr(quotes,c)) // opening quote
300 /* None of the special cases - copy the backslash */
306 if( c == quote ) // non-escaped matching quote
311 if( (quote == '"') && (c == '\\') )
320 case '\0': // should never happen
324 /* None of the special cases - copy the backslash */
336 /*****************************************************************************
337 * ExecuteCommand: The main state machine
338 *****************************************************************************
339 * Execute a command which ends with '\0' (string)
340 *****************************************************************************/
341 static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
342 vlm_message_t **pp_message )
344 size_t i_command = 0;
345 char buf[strlen (psz_command) + 1], *psz_buf = buf;
346 char *ppsz_command[sizeof (buf) / 2];
347 vlm_message_t *p_message = NULL;
349 /* First, parse the line and cut it */
350 while( *psz_command != '\0' )
352 const char *psz_temp;
354 if(isspace (*psz_command))
360 /* support for comments */
361 if( i_command == 0 && *psz_command == '#')
363 p_message = vlm_MessageNew( "", NULL );
367 psz_temp = FindCommandEnd( psz_command );
369 if( psz_temp == NULL )
371 p_message = vlm_MessageNew( "Incomplete command", psz_command );
375 assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
377 ppsz_command[i_command] = psz_buf;
378 memcpy (psz_buf, psz_command, psz_temp - psz_command);
379 psz_buf[psz_temp - psz_command] = '\0';
381 Unescape (psz_buf, psz_buf);
384 psz_buf += psz_temp - psz_command + 1;
385 psz_command = psz_temp;
387 assert (buf + sizeof (buf) >= psz_buf);
391 * And then Interpret it
396 p_message = vlm_MessageNew( "", NULL );
400 if( !strcmp(ppsz_command[0], "new") )
404 /* Check the number of arguments */
405 if( i_command < 3 ) goto syntax_error;
408 if( !strcmp(ppsz_command[2], "vod") )
412 else if( !strcmp(ppsz_command[2], "broadcast") )
414 i_type = BROADCAST_TYPE;
416 else if( !strcmp(ppsz_command[2], "schedule") )
418 i_type = SCHEDULE_TYPE;
423 vlm_MessageNew( "new", "%s: Choose between vod, "
424 "broadcast or schedule", ppsz_command[1] );
428 /* Check for forbidden media names */
429 if( !strcmp(ppsz_command[1], "all") ||
430 !strcmp(ppsz_command[1], "media") ||
431 !strcmp(ppsz_command[1], "schedule") )
433 p_message = vlm_MessageNew( "new", "\"all\", \"media\" and "
434 "\"schedule\" are reserved names" );
438 /* Check the name is not already in use */
439 if( vlm_ScheduleSearch( p_vlm, ppsz_command[1] ) ||
440 vlm_MediaSearch( p_vlm, ppsz_command[1] ) )
442 p_message = vlm_MessageNew( "new", "%s: Name already in use",
448 if( i_type == SCHEDULE_TYPE )
450 vlm_schedule_t *p_schedule;
451 p_schedule = vlm_ScheduleNew( p_vlm, ppsz_command[1] );
454 p_message = vlm_MessageNew( "new", "could not create schedule" );
462 vlm_media_t *p_media;
463 p_media = vlm_MediaNew( p_vlm, ppsz_command[1], i_type );
466 p_message = vlm_MessageNew( "new", "could not create media" );
473 p_message = vlm_MessageNew( "new", NULL );
477 /* Properties will be dealt with later on */
480 else if( !strcmp(ppsz_command[0], "setup") )
482 if( i_command < 2 ) goto syntax_error;
484 /* Properties will be dealt with later on */
487 else if( !strcmp(ppsz_command[0], "del") )
489 vlm_media_t *p_media;
490 vlm_schedule_t *p_schedule;
492 if( i_command < 2 ) goto syntax_error;
494 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
495 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
497 if( p_schedule != NULL )
499 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
501 else if( p_media != NULL )
503 vlm_MediaDelete( p_vlm, p_media, NULL );
505 else if( !strcmp(ppsz_command[1], "media") )
507 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
510 else if( !strcmp(ppsz_command[1], "schedule") )
512 while( p_vlm->i_schedule )
513 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
515 else if( !strcmp(ppsz_command[1], "all") )
517 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
520 while( p_vlm->i_schedule )
521 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
525 p_message = vlm_MessageNew( "del", "%s: media unknown",
530 p_message = vlm_MessageNew( "del", NULL );
534 else if( !strcmp(ppsz_command[0], "show") )
536 vlm_media_t *p_media;
537 vlm_schedule_t *p_schedule;
541 p_message = vlm_Show( p_vlm, NULL, NULL, NULL );
544 else if( i_command > 2 ) goto syntax_error;
546 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
547 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
549 if( p_schedule != NULL )
551 p_message = vlm_Show( p_vlm, NULL, p_schedule, NULL );
553 else if( p_media != NULL )
555 p_message = vlm_Show( p_vlm, p_media, NULL, NULL );
559 p_message = vlm_Show( p_vlm, NULL, NULL, ppsz_command[1] );
565 else if( !strcmp(ppsz_command[0], "help") )
567 if( i_command != 1 ) goto syntax_error;
569 p_message = vlm_Help( p_vlm, NULL );
573 else if( !strcmp(ppsz_command[0], "control") )
575 vlm_media_t *p_media;
577 if( i_command < 3 ) goto syntax_error;
579 if( !(p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] ) ) )
581 p_message = vlm_MessageNew( "control", "%s: media unknown",
587 char *psz_command, *psz_arg = 0, *psz_instance = 0;
590 if( strcmp( ppsz_command[2], "play" ) &&
591 strcmp( ppsz_command[2], "stop" ) &&
592 strcmp( ppsz_command[2], "pause" ) &&
593 strcmp( ppsz_command[2], "seek" ) )
596 psz_instance = ppsz_command[2];
598 if( i_command < 4 ) goto syntax_error;
600 if( strcmp( ppsz_command[3], "play" ) &&
601 strcmp( ppsz_command[3], "stop" ) &&
602 strcmp( ppsz_command[3], "pause" ) &&
603 strcmp( ppsz_command[3], "seek" ) )
607 psz_command = ppsz_command[i_index];
609 if( i_command >= i_index + 2 ) psz_arg = ppsz_command[i_index + 1];
611 vlm_MediaControl( p_vlm, p_media, psz_instance, psz_command,
613 p_message = vlm_MessageNew( "control", NULL );
618 else if( !strcmp(ppsz_command[0], "save") )
620 if( i_command != 2 ) goto syntax_error;
622 if( vlm_Save( p_vlm, ppsz_command[1] ) )
624 p_message = vlm_MessageNew( "save", "Unable to save to file" );
629 p_message = vlm_MessageNew( "save", NULL );
634 else if( !strcmp(ppsz_command[0], "export" ) )
638 if( i_command != 1 ) goto syntax_error;
640 p_message = vlm_MessageNew( "export", psz_buf = Save( p_vlm ) );
646 else if( !strcmp(ppsz_command[0], "load") )
648 if( i_command != 2 ) goto syntax_error;
650 switch( vlm_Load( p_vlm, ppsz_command[1] ) )
653 p_message = vlm_MessageNew( "load", NULL );
656 p_message = vlm_MessageNew( "load", "Read file error" );
660 vlm_MessageNew( "load", "Error while loading file" );
664 vlm_MessageNew( "load", "Unable to load from file" );
671 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
675 /* Common code between "new" and "setup" */
676 if( !strcmp(ppsz_command[0], "new") ||
677 !strcmp(ppsz_command[0], "setup") )
679 size_t i_command_start = strcmp(ppsz_command[0], "new") ? 2 : 3;
680 vlm_media_t *p_media;
681 vlm_schedule_t *p_schedule;
683 if( i_command < i_command_start ) goto syntax_error;
685 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
686 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
688 if( !p_media && !p_schedule )
690 p_message = vlm_MessageNew( ppsz_command[0], "%s unknown",
695 if( p_schedule != NULL )
699 for( i = i_command_start ; i < i_command ; i++ )
701 if( !strcmp( ppsz_command[i], "enabled" ) ||
702 !strcmp( ppsz_command[i], "disabled" ) )
704 vlm_ScheduleSetup( p_schedule, ppsz_command[i], NULL );
707 /* Beware: everything behind append is considered as
709 else if( !strcmp( ppsz_command[i], "append" ) )
713 if( ++i >= i_command ) break;
715 for( j = i + 1; j < i_command; j++ )
718 realloc( ppsz_command[i], strlen(ppsz_command[i]) +
719 strlen(ppsz_command[j]) + 1 + 1 );
720 strcat( ppsz_command[i], " " );
721 strcat( ppsz_command[i], ppsz_command[j] );
724 vlm_ScheduleSetup( p_schedule, ppsz_command[i - 1],
730 if( i + 1 >= i_command && !strcmp(ppsz_command[0], "new") )
732 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
734 vlm_MessageNew( ppsz_command[0],
735 "Wrong properties syntax" );
738 else if( i + 1 >= i_command )
741 vlm_MessageNew( ppsz_command[0],
742 "Wrong properties syntax" );
746 vlm_ScheduleSetup( p_schedule, ppsz_command[i],
753 else if( p_media != NULL )
757 for( i = i_command_start ; i < i_command ; i++ )
759 if( !strcmp( ppsz_command[i], "enabled" ) ||
760 !strcmp( ppsz_command[i], "disabled" ) )
762 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
764 else if( i + 1 >= i_command &&
765 !strcmp( ppsz_command[i], "mux") )
767 if( p_media->i_type != VOD_TYPE )
769 p_message = vlm_MessageNew( ppsz_command[0],
770 "mux only available for broadcast" );
774 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
779 else if( !strcmp( ppsz_command[i], "loop" ) ||
780 !strcmp( ppsz_command[i], "unloop" ) )
782 if( p_media->i_type != BROADCAST_TYPE )
784 p_message = vlm_MessageNew( ppsz_command[0],
785 "loop only available for broadcast" );
789 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
794 if( ( (i + 1) >= i_command ) &&
795 !strcmp(ppsz_command[0], "new") )
797 vlm_MediaDelete( p_vlm, p_media, NULL );
799 vlm_MessageNew( ppsz_command[0],
800 "Wrong properties syntax" );
803 else if( (i + 1) >= i_command )
806 vlm_MessageNew( ppsz_command[0],
807 "Wrong properties syntax" );
811 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
818 p_message = vlm_MessageNew( ppsz_command[0], NULL );
823 *pp_message = p_message;
827 p_message = vlm_MessageNew( ppsz_command[0], "Wrong command syntax" );
830 *pp_message = p_message;
834 vlm_media_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
838 for( i = 0; i < vlm->i_media; i++ )
840 if( strcmp( psz_name, vlm->media[i]->psz_name ) == 0 )
841 return vlm->media[i];
847 /*****************************************************************************
849 *****************************************************************************/
850 static int vlm_Save( vlm_t *p_vlm, const char *psz_file )
855 if( !p_vlm || !psz_file )
858 file = utf8_fopen( psz_file, "wt" );
859 if( file == NULL ) return 1;
861 psz_save = Save( p_vlm );
862 if( psz_save == NULL )
867 fwrite( psz_save, strlen( psz_save ), 1, file );
874 /*****************************************************************************
876 *****************************************************************************/
877 static int vlm_Load( vlm_t *p_vlm, const char *psz_file )
883 if( !p_vlm || !psz_file )
886 p_stream = stream_UrlNew( p_vlm, psz_file );
887 if( p_stream == NULL ) return 1;
889 if( stream_Seek( p_stream, 0 ) != 0 )
891 stream_Delete( p_stream );
895 i_size = stream_Size( p_stream );
897 psz_buffer = malloc( i_size + 1 );
900 stream_Delete( p_stream );
904 stream_Read( p_stream, psz_buffer, i_size );
905 psz_buffer[ i_size ] = '\0';
907 stream_Delete( p_stream );
909 if( Load( p_vlm, psz_buffer ) )
921 /*****************************************************************************
923 *****************************************************************************/
924 static vlm_media_instance_t *
925 vlm_MediaInstanceSearch( vlm_t *vlm, vlm_media_t *media,
926 const char *psz_name )
930 for( i = 0; i < media->i_instance; i++ )
932 if( ( !psz_name && !media->instance[i]->psz_name ) ||
933 ( psz_name && media->instance[i]->psz_name &&
934 !strcmp( psz_name, media->instance[i]->psz_name ) ) )
936 return media->instance[i];
943 static vlm_media_t *vlm_MediaNew( vlm_t *vlm, const char *psz_name, int i_type )
945 vlm_media_t *media = malloc( sizeof( vlm_media_t ) );
949 msg_Err( vlm, "out of memory" );
953 /* Check if we need to load the VOD server */
954 if( i_type == VOD_TYPE && !vlm->i_vod )
956 vlm->vod = vlc_object_create( vlm, VLC_OBJECT_VOD );
957 vlc_object_attach( vlm->vod, vlm );
958 vlm->vod->p_module = module_Need( vlm->vod, "vod server", 0, 0 );
959 if( !vlm->vod->p_module )
961 msg_Err( vlm, "cannot find vod server" );
962 vlc_object_detach( vlm->vod );
963 vlc_object_destroy( vlm->vod );
969 vlm->vod->p_data = vlm;
970 vlm->vod->pf_media_control = vlm_MediaVodControl;
973 if( i_type == VOD_TYPE ) vlm->i_vod++;
975 media->psz_name = strdup( psz_name );
976 media->b_enabled = VLC_FALSE;
977 media->b_loop = VLC_FALSE;
978 media->vod_media = NULL;
979 media->psz_vod_output = NULL;
980 media->psz_mux = NULL;
983 media->psz_output = NULL;
985 media->option = NULL;
986 media->i_type = i_type;
987 media->i_instance = 0;
988 media->instance = NULL;
990 input_ItemInit( VLC_OBJECT(vlm), &media->item );
992 TAB_APPEND( vlm->i_media, vlm->media, media );
997 /* for now, simple delete. After, del with options (last arg) */
998 static void vlm_MediaDelete( vlm_t *vlm, vlm_media_t *media, const char *psz_name )
1000 if( media == NULL ) return;
1002 while( media->i_instance )
1004 vlm_media_instance_t *p_instance = media->instance[0];
1005 vlm_MediaControl( vlm, media, p_instance->psz_name, "stop", 0 );
1008 TAB_REMOVE( vlm->i_media, vlm->media, media );
1010 if( media->i_type == VOD_TYPE )
1012 vlm_MediaSetup( vlm, media, "disabled", 0 );
1016 /* Check if we need to unload the VOD server */
1017 if( media->i_type == VOD_TYPE && !vlm->i_vod )
1019 module_Unneed( vlm->vod, vlm->vod->p_module );
1020 vlc_object_detach( vlm->vod );
1021 vlc_object_destroy( vlm->vod );
1025 if( vlm->i_media == 0 && vlm->media ) free( vlm->media );
1027 free( media->psz_name );
1029 while( media->i_input-- ) free( media->input[media->i_input] );
1030 if( media->input ) free( media->input );
1032 if( media->psz_output ) free( media->psz_output );
1033 if( media->psz_mux ) free( media->psz_mux );
1035 while( media->i_option-- ) free( media->option[media->i_option] );
1036 if( media->option ) free( media->option );
1038 input_ItemClean( &media->item );
1043 static int vlm_MediaSetup( vlm_t *vlm, vlm_media_t *media, const char *psz_cmd,
1044 const char *psz_value )
1046 if( !psz_cmd) return VLC_EGENERIC;
1048 if( !strcmp( psz_cmd, "loop" ) )
1050 media->b_loop = VLC_TRUE;
1052 else if( !strcmp( psz_cmd, "unloop" ) )
1054 media->b_loop = VLC_FALSE;
1056 else if( !strcmp( psz_cmd, "enabled" ) )
1058 media->b_enabled = VLC_TRUE;
1060 else if( !strcmp( psz_cmd, "disabled" ) )
1062 media->b_enabled = VLC_FALSE;
1064 else if( !strcmp( psz_cmd, "mux" ) )
1066 if( media->psz_mux ) free( media->psz_mux );
1067 media->psz_mux = NULL;
1068 if( psz_value ) media->psz_mux = strdup( psz_value );
1070 else if( !strcmp( psz_cmd, "input" ) )
1074 input = strdup( psz_value );
1076 TAB_APPEND( media->i_input, media->input, input );
1078 else if( !strcmp( psz_cmd, "inputdel" ) && !strcmp( psz_value, "all" ) )
1080 while( media->i_input > 0 )
1082 TAB_REMOVE( media->i_input, media->input, media->input[0] );
1085 else if( !strcmp( psz_cmd, "inputdel" ) )
1090 input = strdup( psz_value );
1092 for( i = 0; i < media->i_input; i++ )
1094 if( !strcmp( input, media->input[i] ) )
1096 TAB_REMOVE( media->i_input, media->input, media->input[i] );
1101 else if( !strcmp( psz_cmd, "inputdeln" ) )
1103 int index = atoi( psz_value );
1104 if( index > 0 && index <= media->i_input )
1106 TAB_REMOVE( media->i_input, media->input, media->input[index-1] );
1109 else if( !strcmp( psz_cmd, "output" ) )
1111 if( media->psz_output != NULL )
1113 free( media->psz_output );
1115 media->psz_output = strdup( psz_value );
1117 else if( !strcmp( psz_cmd, "option" ) )
1120 psz_option = strdup( psz_value );
1122 TAB_APPEND( media->i_option, media->option, psz_option );
1126 return VLC_EGENERIC;
1129 /* Check if we need to create/delete a vod media */
1130 if( media->i_type == VOD_TYPE )
1132 if( !media->b_enabled && media->vod_media )
1134 vlm->vod->pf_media_del( vlm->vod, media->vod_media );
1135 media->vod_media = 0;
1137 else if( media->b_enabled && !media->vod_media && media->i_input )
1139 /* Pre-parse the input */
1140 input_thread_t *p_input;
1146 input_ItemClean( &media->item );
1147 input_ItemInit( VLC_OBJECT(vlm), &media->item );
1149 if( media->psz_output )
1150 asprintf( &psz_output, "%s:description", media->psz_output );
1152 asprintf( &psz_output, "#description" );
1154 media->item.psz_uri = strdup( media->input[0] );
1156 TAB_INIT( media->item.i_options, media->item.ppsz_options );
1158 asprintf( &psz_dup, "sout=%s", psz_output);
1159 TAB_APPEND( media->item.i_options, media->item.ppsz_options, psz_dup );
1160 for( i = 0; i < media->i_option; i++ )
1162 psz_dup = strdup( media->option[i] );
1163 TAB_APPEND( media->item.i_options, media->item.ppsz_options, psz_dup );
1165 psz_dup = strdup( "no-sout-keep" );
1166 TAB_APPEND( media->item.i_options, media->item.ppsz_options, psz_dup );
1168 asprintf( &psz_header, _("Media: %s"), media->psz_name );
1170 if( (p_input = input_CreateThreadExtended( vlm, &media->item, psz_header, NULL ) ) )
1172 while( !p_input->b_eof && !p_input->b_error )
1175 input_StopThread( p_input );
1176 input_DestroyThreadExtended( p_input, NULL );
1181 if( media->psz_mux )
1184 es_format_t es, *p_es = &es;
1187 sprintf( fourcc, "%4.4s", media->psz_mux );
1188 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
1189 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
1194 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
1197 vlm->vod->pf_media_new( vlm->vod, media->psz_name, &item );
1202 vlm->vod->pf_media_new( vlm->vod, media->psz_name,
1210 static vlm_media_instance_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
1212 vlm_media_instance_t *p_instance = malloc( sizeof(vlm_media_instance_t) );
1216 memset( p_instance, 0, sizeof(vlm_media_instance_t) );
1218 p_instance->psz_name = NULL;
1220 p_instance->psz_name = strdup( psz_name );
1222 input_ItemInit( VLC_OBJECT(p_vlm), &p_instance->item );
1224 p_instance->i_index = 0;
1225 p_instance->b_sout_keep = VLC_FALSE;
1226 p_instance->p_input = NULL;
1227 p_instance->p_sout = NULL;
1231 static void vlm_MediaInstanceDelete( vlm_media_instance_t *p_instance )
1233 if( p_instance->p_input )
1235 input_StopThread( p_instance->p_input );
1236 input_DestroyThreadExtended( p_instance->p_input, &p_instance->p_sout );
1238 if( p_instance->p_sout )
1239 sout_DeleteInstance( p_instance->p_sout );
1241 input_ItemClean( &p_instance->item );
1242 if( p_instance->psz_name )
1243 free( p_instance->psz_name );
1247 static int vlm_MediaControl( vlm_t *vlm, vlm_media_t *media, const char *psz_id,
1248 const char *psz_command, const char *psz_args )
1250 vlm_media_instance_t *p_instance;
1254 p_instance = vlm_MediaInstanceSearch( vlm, media, psz_id );
1256 if( !strcmp( psz_command, "play" ) )
1258 if( !media->b_enabled || media->i_input == 0 ) return 0;
1264 p_instance = vlm_MediaInstanceNew( vlm, psz_id );
1268 TAB_INIT( p_instance->item.i_options, p_instance->item.ppsz_options );
1270 if( media->psz_output != NULL || media->psz_vod_output != NULL )
1272 asprintf( &psz_dup, "sout=%s%s%s",
1273 media->psz_output ? media->psz_output : "",
1274 (media->psz_output && media->psz_vod_output) ? ":" : media->psz_vod_output ? "#" : "",
1275 media->psz_vod_output ? media->psz_vod_output : "" );
1276 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, psz_dup );
1279 for( i = 0; i < media->i_option; i++ )
1281 if( !strcmp( media->option[i], "sout-keep" ) )
1283 p_instance->b_sout_keep = VLC_TRUE;
1285 else if( !strcmp( media->option[i], "nosout-keep" ) || !strcmp( media->option[i], "no-sout-keep" ) )
1287 p_instance->b_sout_keep = VLC_FALSE;
1291 psz_dup = strdup( media->option[i] );
1292 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, psz_dup );
1295 /* We force the right sout-keep value (avoid using the sout-keep from the global configuration) */
1296 if( media->i_type != VOD_TYPE && p_instance->b_sout_keep )
1297 psz_dup = strdup( "sout-keep" );
1299 psz_dup = strdup( "no-sout-keep" );
1300 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, psz_dup );
1302 TAB_APPEND( media->i_instance, media->instance, p_instance );
1305 if( ( psz_args && sscanf(psz_args, "%d", &i) == 1 ) && i > 0 && i-1 < media->i_input )
1307 p_instance->i_index = i-1;
1310 if( p_instance->item.psz_uri )
1311 free( p_instance->item.psz_uri );
1312 p_instance->item.psz_uri =
1313 strdup( media->input[p_instance->i_index] );
1315 if( p_instance->p_input )
1317 input_StopThread( p_instance->p_input );
1318 input_DestroyThreadExtended( p_instance->p_input, &p_instance->p_sout );
1319 if( !p_instance->b_sout_keep && p_instance->p_sout )
1321 sout_DeleteInstance( p_instance->p_sout );
1322 p_instance->p_sout = NULL;
1326 asprintf( &psz_header, _("Media: %s"), media->psz_name );
1327 p_instance->p_input = input_CreateThreadExtended( vlm, &p_instance->item, psz_header, p_instance->p_sout );
1328 if( !p_instance->p_input )
1330 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1331 vlm_MediaInstanceDelete( p_instance );
1339 return VLC_EGENERIC;
1341 if( !strcmp( psz_command, "seek" ) )
1346 float f_value = i18n_atof( psz_args );
1347 if( psz_args[0] == '+' || psz_args[0] == '-' )
1351 if( strstr( psz_args, "ms" ) )
1354 int64_t i_msec = 1000 * (int64_t)atoi( psz_args );
1357 var_SetTime( p_instance->p_input, "time-offset",
1360 else if( i_msec >= 0
1361 && i_msec < var_GetTime( p_instance->p_input, "length" ) )
1363 var_SetTime( p_instance->p_input, "time",
1367 else if( strchr( psz_args, 's' ) )
1370 int64_t i_sec = 1000000 * (int64_t)atoi( psz_args );
1373 var_SetTime( p_instance->p_input, "time-offset",
1377 && i_sec < var_GetTime( p_instance->p_input, "length" ) )
1379 var_SetTime( p_instance->p_input, "time",
1389 float f_orig = var_GetFloat( p_instance->p_input,
1393 if( f_value >= 0.0 && f_value <= 1.0 )
1395 var_SetFloat( p_instance->p_input, "position",
1402 else if( !strcmp( psz_command, "rewind" ) )
1409 f_scale = i18n_atof( psz_args );
1410 f_pos = var_GetFloat( p_instance->p_input, "position" );
1411 f_pos -= (f_scale / 1000.0);
1414 var_SetFloat( p_instance->p_input, "position", f_pos );
1418 else if( !strcmp( psz_command, "forward" ) )
1425 f_scale = i18n_atof( psz_args );
1426 f_pos = var_GetFloat( p_instance->p_input, "position" );
1427 f_pos += (f_scale / 1000.0);
1430 var_SetFloat( p_instance->p_input, "position", f_pos );
1434 else if( !strcmp( psz_command, "stop" ) )
1436 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1438 vlm_MediaInstanceDelete( p_instance );
1441 else if( !strcmp( psz_command, "pause" ) )
1445 if( !p_instance->p_input ) return VLC_SUCCESS;
1447 i_state = var_GetInteger( p_instance->p_input, "state" );
1449 if( i_state == PAUSE_S ) i_state = PLAYING_S;
1450 else i_state = PAUSE_S;
1451 var_SetInteger( p_instance->p_input, "state", i_state );
1456 return VLC_EGENERIC;
1459 /*****************************************************************************
1461 *****************************************************************************/
1462 static int64_t vlm_Date(void)
1467 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1473 static vlm_schedule_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
1475 vlm_schedule_t *p_sched = malloc( sizeof( vlm_schedule_t ) );
1487 p_sched->psz_name = strdup( psz_name );
1488 p_sched->b_enabled = VLC_FALSE;
1489 p_sched->i_command = 0;
1490 p_sched->command = NULL;
1491 p_sched->i_date = 0;
1492 p_sched->i_period = 0;
1493 p_sched->i_repeat = -1;
1495 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1500 /* for now, simple delete. After, del with options (last arg) */
1501 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_t *sched,
1502 const char *psz_name )
1504 if( sched == NULL ) return;
1506 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1508 if( vlm->i_schedule == 0 && vlm->schedule ) free( vlm->schedule );
1509 free( sched->psz_name );
1510 while( sched->i_command )
1512 char *psz_cmd = sched->command[0];
1513 TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1519 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1523 for( i = 0; i < vlm->i_schedule; i++ )
1525 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1527 return vlm->schedule[i];
1534 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1535 static int vlm_ScheduleSetup( vlm_schedule_t *schedule, const char *psz_cmd,
1536 const char *psz_value )
1538 if( !strcmp( psz_cmd, "enabled" ) )
1540 schedule->b_enabled = VLC_TRUE;
1542 else if( !strcmp( psz_cmd, "disabled" ) )
1544 schedule->b_enabled = VLC_FALSE;
1546 #if !defined( UNDER_CE )
1547 else if( !strcmp( psz_cmd, "date" ) )
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, '-' );
1566 if( !strcmp( psz_value, "now" ) )
1568 schedule->i_date = 0;
1570 else if( (p == NULL) && sscanf( psz_value, "%d:%d:%d", &time.tm_hour,
1571 &time.tm_min, &time.tm_sec ) != 3 )
1572 /* it must be a hour:minutes:seconds */
1580 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1598 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1604 time.tm_mon = i - 1;
1608 time.tm_year = i - 1900;
1609 time.tm_mon = j - 1;
1616 date = mktime( &time );
1617 schedule->i_date = ((mtime_t) date) * 1000000;
1620 else if( !strcmp( psz_cmd, "period" ) )
1624 const char *psz_time = NULL, *psz_date = NULL;
1628 /* First, if date or period are modified, repeat should be equal to -1 */
1629 schedule->i_repeat = -1;
1631 time.tm_sec = 0; /* seconds */
1632 time.tm_min = 0; /* minutes */
1633 time.tm_hour = 0; /* hours */
1634 time.tm_mday = 0; /* day of the month */
1635 time.tm_mon = 0; /* month */
1636 time.tm_year = 0; /* year */
1637 time.tm_wday = 0; /* day of the week */
1638 time.tm_yday = 0; /* day in the year */
1639 time.tm_isdst = -1; /* daylight saving time */
1641 /* date should be year/month/day-hour:minutes:seconds */
1642 p = strchr( psz_value, '-' );
1645 psz_date = psz_value;
1650 psz_time = psz_value;
1653 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1672 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1691 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1692 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1693 schedule->i_period = ((mtime_t) date) * 1000000;
1695 #endif /* UNDER_CE */
1696 else if( !strcmp( psz_cmd, "repeat" ) )
1700 if( sscanf( psz_value, "%d", &i ) == 1 )
1702 schedule->i_repeat = i;
1709 else if( !strcmp( psz_cmd, "append" ) )
1711 char *command = strdup( psz_value );
1713 TAB_APPEND( schedule->i_command, schedule->command, command );
1722 /*****************************************************************************
1723 * Message handling functions
1724 *****************************************************************************/
1725 vlm_message_t *vlm_MessageNew( const char *psz_name,
1726 const char *psz_format, ... )
1728 vlm_message_t *p_message;
1731 if( !psz_name ) return NULL;
1733 p_message = malloc( sizeof(vlm_message_t) );
1739 p_message->psz_value = 0;
1743 va_start( args, psz_format );
1744 if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1753 p_message->psz_name = strdup( psz_name );
1754 p_message->i_child = 0;
1755 p_message->child = NULL;
1760 void vlm_MessageDelete( vlm_message_t *p_message )
1762 if( p_message->psz_name ) free( p_message->psz_name );
1763 if( p_message->psz_value ) free( p_message->psz_value );
1764 while( p_message->i_child-- )
1765 vlm_MessageDelete( p_message->child[p_message->i_child] );
1766 if( p_message->child ) free( p_message->child );
1771 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1772 vlm_message_t *p_child )
1774 if( p_message == NULL ) return NULL;
1778 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1784 /*****************************************************************************
1785 * Misc utility functions
1786 *****************************************************************************/
1787 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_t *media,
1788 vlm_schedule_t *schedule,
1789 const char *psz_filter )
1795 vlm_message_t *msg_media;
1796 vlm_message_t *msg_child;
1798 msg = vlm_MessageNew( "show", NULL );
1799 msg_media = vlm_MessageAdd( msg, vlm_MessageNew( media->psz_name, 0 ));
1801 vlm_MessageAdd( msg_media,
1802 vlm_MessageNew( "type", media->i_type == VOD_TYPE ?
1803 "vod" : "broadcast" ) );
1804 vlm_MessageAdd( msg_media,
1805 vlm_MessageNew( "enabled", media->b_enabled ?
1808 vlm_MessageAdd( msg_media,
1809 vlm_MessageNew( "loop", media->b_loop ?
1812 if( media->i_type == VOD_TYPE && media->psz_mux )
1813 vlm_MessageAdd( msg_media,
1814 vlm_MessageNew( "mux", media->psz_mux ) );
1816 msg_child = vlm_MessageAdd( msg_media,
1817 vlm_MessageNew( "inputs", NULL ) );
1819 for( i = 0; i < media->i_input; i++ )
1821 vlm_MessageAdd( msg_child,
1822 vlm_MessageNew( media->input[i], NULL ) );
1825 vlm_MessageAdd( msg_media,
1826 vlm_MessageNew( "output", media->psz_output ?
1827 media->psz_output : "" ) );
1829 msg_child = vlm_MessageAdd( msg_media, vlm_MessageNew( "options", 0 ));
1831 for( i = 0; i < media->i_option; i++ )
1833 vlm_MessageAdd( msg_child, vlm_MessageNew( media->option[i], 0 ) );
1836 msg_child = vlm_MessageAdd( msg_media,
1837 vlm_MessageNew( "instances", NULL ) );
1839 for( i = 0; i < media->i_instance; i++ )
1841 vlm_media_instance_t *p_instance = media->instance[i];
1843 vlm_message_t *msg_instance;
1846 if( !p_instance->p_input ) val.i_int = END_S;
1847 else var_Get( p_instance->p_input, "state", &val );
1849 msg_instance = vlm_MessageNew( "instance" , NULL );
1850 vlm_MessageAdd( msg_instance, vlm_MessageNew( "name" , p_instance->psz_name ? p_instance->psz_name : "default" ) );
1851 vlm_MessageAdd( msg_instance, vlm_MessageNew( "state",
1852 val.i_int == PLAYING_S ? "playing" :
1853 val.i_int == PAUSE_S ? "paused" :
1855 #define APPEND_INPUT_INFO( a, format, type ) \
1856 asprintf( &psz_tmp, format, \
1857 var_Get ## type( p_instance->p_input, a ) ); \
1858 vlm_MessageAdd( msg_instance, vlm_MessageNew( a, psz_tmp ) ); \
1860 APPEND_INPUT_INFO( "position", "%f", Float );
1861 APPEND_INPUT_INFO( "time", I64Fi, Time );
1862 APPEND_INPUT_INFO( "length", I64Fi, Time );
1863 APPEND_INPUT_INFO( "rate", "%d", Integer );
1864 APPEND_INPUT_INFO( "title", "%d", Integer );
1865 APPEND_INPUT_INFO( "chapter", "%d", Integer );
1866 APPEND_INPUT_INFO( "seekable", "%d", Bool );
1867 #undef APPEND_INPUT_INFO
1868 asprintf( &psz_tmp, "%d", p_instance->i_index + 1 );
1869 vlm_MessageAdd( msg_instance, vlm_MessageNew( "playlistindex", psz_tmp ) );
1871 vlm_MessageAdd( msg_child, msg_instance );
1878 else if( schedule != NULL )
1882 vlm_message_t *msg_schedule;
1883 vlm_message_t *msg_child;
1886 msg = vlm_MessageNew( "show", NULL );
1888 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, 0 ) );
1890 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1892 vlm_MessageAdd( msg_schedule,
1893 vlm_MessageNew( "enabled", schedule->b_enabled ?
1896 #if !defined( UNDER_CE )
1897 if( schedule->i_date != 0 )
1900 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1903 #ifdef HAVE_LOCALTIME_R
1904 localtime_r( &i_time, &date);
1906 struct tm *p_date = localtime( &i_time );
1910 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1911 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1912 date.tm_hour, date.tm_min, date.tm_sec );
1914 vlm_MessageAdd( msg_schedule,
1915 vlm_MessageNew( "date", psz_date ) );
1920 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1923 if( schedule->i_period != 0 )
1925 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1928 date.tm_sec = (int)( i_time % 60 );
1929 i_time = i_time / 60;
1930 date.tm_min = (int)( i_time % 60 );
1931 i_time = i_time / 60;
1932 date.tm_hour = (int)( i_time % 24 );
1933 i_time = i_time / 24;
1934 date.tm_mday = (int)( i_time % 30 );
1935 i_time = i_time / 30;
1936 /* okay, okay, months are not always 30 days long */
1937 date.tm_mon = (int)( i_time % 12 );
1938 i_time = i_time / 12;
1939 date.tm_year = (int)i_time;
1941 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1942 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1944 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1948 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1950 #endif /* UNDER_CE */
1952 sprintf( buffer, "%d", schedule->i_repeat );
1953 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1956 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", 0) );
1958 for( i = 0; i < schedule->i_command; i++ )
1960 vlm_MessageAdd( msg_child,
1961 vlm_MessageNew( schedule->command[i], NULL ) );
1968 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1972 vlm_message_t *msg_child;
1973 int i_vod = 0, i_broadcast = 0;
1976 for( i = 0; i < vlm->i_media; i++ )
1978 if( vlm->media[i]->i_type == VOD_TYPE )
1984 asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1986 msg = vlm_MessageNew( "show", NULL );
1987 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "media", psz_count ) );
1990 for( i = 0; i < vlm->i_media; i++ )
1992 vlm_media_t *m = vlm->media[i];
1993 vlm_message_t *msg_media, *msg_instance;
1995 msg_media = vlm_MessageAdd( msg_child,
1996 vlm_MessageNew( m->psz_name, 0 ) );
1998 vlm_MessageAdd( msg_media,
1999 vlm_MessageNew( "type", m->i_type == VOD_TYPE ?
2000 "vod" : "broadcast" ) );
2002 vlm_MessageAdd( msg_media,
2003 vlm_MessageNew( "enabled", m->b_enabled ?
2006 if( m->i_type == VOD_TYPE && m->psz_mux )
2007 vlm_MessageAdd( msg_media,
2008 vlm_MessageNew( "mux", m->psz_mux ) );
2010 msg_instance = vlm_MessageAdd( msg_media,
2011 vlm_MessageNew( "instances", 0 ) );
2013 for( j = 0; j < m->i_instance; j++ )
2015 vlm_media_instance_t *p_instance = m->instance[j];
2018 if( !p_instance->p_input ) val.i_int = END_S;
2019 else var_Get( p_instance->p_input, "state", &val );
2021 vlm_MessageAdd( msg_instance,
2022 vlm_MessageNew( p_instance->psz_name ?
2023 p_instance->psz_name : "default",
2024 val.i_int == PLAYING_S ? "playing" :
2025 val.i_int == PAUSE_S ? "paused" :
2033 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
2037 vlm_message_t *msg_child;
2039 msg = vlm_MessageNew( "show", NULL );
2040 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", NULL ) );
2042 for( i = 0; i < vlm->i_schedule; i++ )
2044 vlm_schedule_t *s = vlm->schedule[i];
2045 vlm_message_t *msg_schedule;
2046 mtime_t i_time, i_next_date;
2048 msg_schedule = vlm_MessageAdd( msg_child,
2049 vlm_MessageNew( s->psz_name, 0 ) );
2050 vlm_MessageAdd( msg_schedule,
2051 vlm_MessageNew( "enabled", s->b_enabled ?
2054 /* calculate next date */
2055 i_time = vlm_Date();
2056 i_next_date = s->i_date;
2058 if( s->i_period != 0 )
2061 while( s->i_date + j * s->i_period <= i_time &&
2067 i_next_date = s->i_date + j * s->i_period;
2070 if( i_next_date > i_time )
2072 time_t i_date = (time_t) (i_next_date / 1000000) ;
2074 #if !defined( UNDER_CE )
2077 ctime_r( &i_date, psz_date );
2079 char *psz_date = ctime( &i_date );
2082 vlm_MessageAdd( msg_schedule,
2083 vlm_MessageNew( "next launch", psz_date ) );
2091 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
2093 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
2094 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
2096 vlm_MessageAdd( show1, show2->child[0] );
2098 /* We must destroy the parent node "show" of show2
2099 * and not the children */
2100 free( show2->psz_name );
2108 return vlm_MessageNew( "show", NULL );
2112 static vlm_message_t *vlm_Help( vlm_t *vlm, char *psz_filter )
2114 vlm_message_t *message, *message_child;
2116 #define MessageAdd( a ) \
2117 vlm_MessageAdd( message, vlm_MessageNew( a, NULL ) );
2118 #define MessageAddChild( a ) \
2119 vlm_MessageAdd( message_child, vlm_MessageNew( a, NULL ) );
2121 if( psz_filter == NULL )
2123 message = vlm_MessageNew( "help", NULL );
2125 message_child = MessageAdd( "Commands Syntax:" );
2126 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
2127 MessageAddChild( "setup (name) (properties)" );
2128 MessageAddChild( "show [(name)|media|schedule]" );
2129 MessageAddChild( "del (name)|all|media|schedule" );
2130 MessageAddChild( "control (name) [instance_name] (command)" );
2131 MessageAddChild( "save (config_file)" );
2132 MessageAddChild( "export" );
2133 MessageAddChild( "load (config_file)" );
2135 message_child = MessageAdd( "Media Proprieties Syntax:" );
2136 MessageAddChild( "input (input_name)" );
2137 MessageAddChild( "inputdel (input_name)|all" );
2138 MessageAddChild( "inputdeln input_number" );
2139 MessageAddChild( "output (output_name)" );
2140 MessageAddChild( "option (option_name)[=value]" );
2141 MessageAddChild( "enabled|disabled" );
2142 MessageAddChild( "loop|unloop (broadcast only)" );
2143 MessageAddChild( "mux (mux_name)" );
2145 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
2146 MessageAddChild( "enabled|disabled" );
2147 MessageAddChild( "append (command_until_rest_of_the_line)" );
2148 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
2150 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
2151 "(days)-(hours):(minutes):(seconds)" );
2152 MessageAddChild( "repeat (number_of_repetitions)" );
2154 message_child = MessageAdd( "Control Commands Syntax:" );
2155 MessageAddChild( "play [input_number]" );
2156 MessageAddChild( "pause" );
2157 MessageAddChild( "stop" );
2158 MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](miliseconds)ms" );
2163 return vlm_MessageNew( "help", NULL );
2166 /*****************************************************************************
2167 * Config handling functions
2168 *****************************************************************************/
2169 static int Load( vlm_t *vlm, char *file )
2174 while( *pf != '\0' )
2176 vlm_message_t *message = NULL;
2179 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
2184 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
2188 if( pf[i_end] == '\n' ) i_end++;
2191 if( *pf && ExecuteCommand( vlm, pf, &message ) )
2195 if( message->psz_value )
2196 msg_Err( vlm, "Load error on line %d: %s: %s",
2197 i_line, message->psz_name, message->psz_value );
2198 vlm_MessageDelete( message );
2202 if( message ) vlm_MessageDelete( message );
2211 static char *Save( vlm_t *vlm )
2214 char psz_header[] = "\n"
2215 "# VLC media player VLM command batch\n"
2216 "# http://www.videolan.org/vlc/\n\n" ;
2219 int i_length = strlen( psz_header );
2221 for( i = 0; i < vlm->i_media; i++ )
2223 vlm_media_t *media = vlm->media[i];
2225 if( media->i_type == VOD_TYPE )
2227 i_length += strlen( "new vod " ) + strlen(media->psz_name);
2231 i_length += strlen( "new broadcast " ) + strlen(media->psz_name);
2234 if( media->b_enabled == VLC_TRUE )
2236 i_length += strlen( "enabled" );
2240 i_length += strlen( "disabled" );
2243 if( media->b_loop == VLC_TRUE )
2245 i_length += strlen( " loop\n" );
2249 i_length += strlen( "\n" );
2252 for( j = 0; j < media->i_input; j++ )
2254 i_length += strlen( "setup input \"\"\n" ) +
2255 strlen( media->psz_name ) + strlen( media->input[j] );
2258 if( media->psz_output != NULL )
2260 i_length += strlen(media->psz_name) + strlen(media->psz_output) +
2261 strlen( "setup output \n" );
2264 for( j=0 ; j < media->i_option ; j++ )
2266 i_length += strlen(media->psz_name) + strlen(media->option[j]) +
2267 strlen("setup option \n");
2271 for( i = 0; i < vlm->i_schedule; i++ )
2273 vlm_schedule_t *schedule = vlm->schedule[i];
2275 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
2277 if( schedule->b_enabled == VLC_TRUE )
2279 i_length += strlen( "date //-:: enabled\n" ) + 14;
2283 i_length += strlen( "date //-:: disabled\n" ) + 14;
2287 if( schedule->i_period != 0 )
2289 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
2290 strlen( "period //-::\n" ) + 14;
2293 if( schedule->i_repeat >= 0 )
2297 sprintf( buffer, "%d", schedule->i_repeat );
2298 i_length += strlen( "setup repeat \n" ) +
2299 strlen( schedule->psz_name ) + strlen( buffer );
2306 for( j = 0; j < schedule->i_command; j++ )
2308 i_length += strlen( "setup append \n" ) +
2309 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
2314 /* Don't forget the '\0' */
2316 /* now we have the length of save */
2318 p = save = malloc( i_length );
2321 p += sprintf( p, "%s", psz_header );
2323 /* finally we can write in it */
2324 for( i = 0; i < vlm->i_media; i++ )
2326 vlm_media_t *media = vlm->media[i];
2328 if( media->i_type == VOD_TYPE )
2330 p += sprintf( p, "new %s vod ", media->psz_name);
2334 p += sprintf( p, "new %s broadcast ", media->psz_name);
2337 if( media->b_enabled == VLC_TRUE )
2339 p += sprintf( p, "enabled" );
2343 p += sprintf( p, "disabled" );
2346 if( media->b_loop == VLC_TRUE )
2348 p += sprintf( p, " loop\n" );
2352 p += sprintf( p, "\n" );
2355 for( j = 0; j < media->i_input; j++ )
2357 p += sprintf( p, "setup %s input \"%s\"\n", media->psz_name,
2361 if( media->psz_output != NULL )
2363 p += sprintf( p, "setup %s output %s\n", media->psz_name,
2364 media->psz_output );
2367 for( j = 0; j < media->i_option; j++ )
2369 p += sprintf( p, "setup %s option %s\n", media->psz_name,
2374 /* and now, the schedule scripts */
2375 #if !defined( UNDER_CE )
2376 for( i = 0; i < vlm->i_schedule; i++ )
2378 vlm_schedule_t *schedule = vlm->schedule[i];
2380 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
2382 #ifdef HAVE_LOCALTIME_R
2383 localtime_r( &i_time, &date);
2385 struct tm *p_date = localtime( &i_time );
2389 p += sprintf( p, "new %s schedule ", schedule->psz_name);
2391 if( schedule->b_enabled == VLC_TRUE )
2393 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
2394 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2395 date.tm_hour, date.tm_min, date.tm_sec );
2399 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
2400 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2401 date.tm_hour, date.tm_min, date.tm_sec);
2404 if( schedule->i_period != 0 )
2406 p += sprintf( p, "setup %s ", schedule->psz_name );
2408 i_time = (time_t) ( schedule->i_period / 1000000 );
2410 date.tm_sec = (int)( i_time % 60 );
2411 i_time = i_time / 60;
2412 date.tm_min = (int)( i_time % 60 );
2413 i_time = i_time / 60;
2414 date.tm_hour = (int)( i_time % 24 );
2415 i_time = i_time / 24;
2416 date.tm_mday = (int)( i_time % 30 );
2417 i_time = i_time / 30;
2418 /* okay, okay, months are not always 30 days long */
2419 date.tm_mon = (int)( i_time % 12 );
2420 i_time = i_time / 12;
2421 date.tm_year = (int)i_time;
2423 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
2424 date.tm_year, date.tm_mon, date.tm_mday,
2425 date.tm_hour, date.tm_min, date.tm_sec);
2428 if( schedule->i_repeat >= 0 )
2430 p += sprintf( p, "setup %s repeat %d\n",
2431 schedule->psz_name, schedule->i_repeat );
2435 p += sprintf( p, "\n" );
2438 for( j = 0; j < schedule->i_command; j++ )
2440 p += sprintf( p, "setup %s append %s\n",
2441 schedule->psz_name, schedule->command[j] );
2445 #endif /* UNDER_CE */
2450 /*****************************************************************************
2452 *****************************************************************************/
2453 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
2454 const char *psz_id, int i_query, va_list args )
2456 vlm_t *vlm = (vlm_t *)p_private;
2457 int i, i_ret = VLC_EGENERIC;
2459 if( !p_private || !p_vod_media ) return VLC_EGENERIC;
2461 vlc_mutex_lock( &vlm->lock );
2464 for( i = 0; i < vlm->i_media; i++ )
2466 if( p_vod_media == vlm->media[i]->vod_media ) break;
2469 if( i == vlm->i_media )
2471 vlc_mutex_unlock( &vlm->lock );
2472 return VLC_EGENERIC;
2477 case VOD_MEDIA_PLAY:
2478 vlm->media[i]->psz_vod_output = (char *)va_arg( args, char * );
2479 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "play", 0 );
2480 vlm->media[i]->psz_vod_output = 0;
2483 case VOD_MEDIA_PAUSE:
2484 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "pause", 0 );
2487 case VOD_MEDIA_STOP:
2488 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "stop", 0 );
2491 case VOD_MEDIA_SEEK:
2493 double f_pos = (double)va_arg( args, double );
2495 lldiv_t div = lldiv( f_pos * 10000000, 10000000 );
2496 sprintf( psz_pos, I64Fd".%07u", div.quot, (unsigned int) div.rem );
2497 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "seek", psz_pos);
2501 case VOD_MEDIA_REWIND:
2503 double f_scale = (double)va_arg( args, double );
2505 lldiv_t div = lldiv( f_scale * 10000000, 10000000 );
2506 sprintf( psz_scale, I64Fd".%07u", div.quot, (unsigned int) div.rem );
2507 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "rewind", psz_scale );
2511 case VOD_MEDIA_FORWARD:
2513 double f_scale = (double)va_arg( args, double );
2515 lldiv_t div = lldiv( f_scale * 10000000, 10000000 );
2516 sprintf( psz_scale, I64Fd".%07u", div.quot, (unsigned int) div.rem );
2517 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "forward", psz_scale );
2525 vlc_mutex_unlock( &vlm->lock );
2531 /*****************************************************************************
2533 *****************************************************************************/
2534 static int Manage( vlc_object_t* p_object )
2536 vlm_t *vlm = (vlm_t*)p_object;
2538 mtime_t i_lastcheck;
2541 i_lastcheck = vlm_Date();
2543 while( !vlm->b_die )
2545 char **ppsz_scheduled_commands = NULL;
2546 int i_scheduled_commands = 0;
2547 vlc_mutex_lock( &vlm->lock );
2549 /* destroy the inputs that wants to die, and launch the next input */
2550 for( i = 0; i < vlm->i_media; i++ )
2552 vlm_media_t *p_media = vlm->media[i];
2554 for( j = 0; j < p_media->i_instance; j++ )
2556 vlm_media_instance_t *p_instance = p_media->instance[j];
2558 if( !p_instance->p_input ||
2559 ( !p_instance->p_input->b_eof && !p_instance->p_input->b_error ) )
2562 p_instance->i_index++;
2563 if( p_instance->i_index >= p_media->i_input && p_media->b_loop )
2564 p_instance->i_index = 0;
2566 if( p_instance->i_index < p_media->i_input )
2570 /* FIXME, find a way to select the right instance */
2571 snprintf( psz_index, sizeof(psz_index), "%d", p_instance->i_index+1 );
2572 vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2573 "play", psz_index );
2577 if( vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2578 "stop", 0 ) == VLC_SUCCESS )
2580 j--; /* the aray is one element smaller now. */
2587 i_time = vlm_Date();
2589 for( i = 0; i < vlm->i_schedule; i++ )
2591 mtime_t i_real_date = vlm->schedule[i]->i_date;
2593 if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2595 if( vlm->schedule[i]->i_date == 0 ) // now !
2597 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2598 i_real_date = i_time;
2600 else if( vlm->schedule[i]->i_period != 0 )
2603 while( vlm->schedule[i]->i_date + j *
2604 vlm->schedule[i]->i_period <= i_lastcheck &&
2605 ( vlm->schedule[i]->i_repeat > j ||
2606 vlm->schedule[i]->i_repeat == -1 ) )
2611 i_real_date = vlm->schedule[i]->i_date + j *
2612 vlm->schedule[i]->i_period;
2615 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2617 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2619 TAB_APPEND( i_scheduled_commands,
2620 ppsz_scheduled_commands,
2621 strdup(vlm->schedule[i]->command[j] ) );
2626 while( i_scheduled_commands )
2628 vlm_message_t *message = NULL;
2629 char *psz_command = ppsz_scheduled_commands[0];
2630 ExecuteCommand( vlm, psz_command,&message );
2632 /* for now, drop the message */
2633 vlm_MessageDelete( message );
2634 TAB_REMOVE( i_scheduled_commands,
2635 ppsz_scheduled_commands,
2637 free( psz_command );
2640 i_lastcheck = i_time;
2642 vlc_mutex_unlock( &vlm->lock );
2650 #else /* ENABLE_VLM */
2652 /* We just define an empty wrapper */
2653 vlm_t *__vlm_New( vlc_object_t *a )
2655 msg_Err( a, "VideoLAN manager support is disabled" );
2658 void vlm_Delete( vlm_t *a )
2662 int vlm_ExecuteCommand( vlm_t *a, char *b, vlm_message_t **c )
2666 void vlm_MessageDelete( vlm_message_t *a )
2670 vlm_media_t *vlm_MediaNew( vlm_t *a, char *b, int c )
2674 vlm_media_t *vlm_MediaSearch (vlm_t *a, const char *b )
2678 void vlm_MediaDelete( vlm_t *a, vlm_media_t *b, char *c )
2682 int vlm_MediaSetup( vlm_t *a, vlm_media_t *b, char *c, char *d )
2686 int vlm_MediaControl( vlm_t *a, vlm_media_t *b, char *c, char *d, char *e )
2691 #endif /* ENABLE_VLM */