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 /*****************************************************************************
54 *****************************************************************************/
55 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_t *, vlm_schedule_t *, const char * );
56 static vlm_message_t *vlm_Help( vlm_t *, char * );
58 static vlm_media_instance_t *vlm_MediaInstanceSearch( vlm_t *, vlm_media_t *, const char * );
60 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *, const char * );
62 static char *Save( vlm_t * );
63 static int Load( vlm_t *, char * );
64 static int ExecuteCommand( vlm_t *, const char *, vlm_message_t ** );
65 static int Manage( vlc_object_t * );
67 static int vlm_Save( vlm_t *p_vlm, const char *psz_file );
68 static int vlm_Load( vlm_t *p_vlm, const char *psz_file );
71 static vlm_schedule_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name );
72 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_t *sched, const char *psz_name );
73 static int vlm_ScheduleSetup( vlm_schedule_t *schedule, const char *psz_cmd,
74 const char *psz_value );
76 static int vlm_MediaVodControl( void *, vod_media_t *, const char *, int, va_list );
79 /*****************************************************************************
81 *****************************************************************************/
82 vlm_t *__vlm_New ( vlc_object_t *p_this )
88 /* Avoid multiple creation */
89 if( var_Create( p_this->p_libvlc_global, "vlm_mutex", VLC_VAR_MUTEX ) ||
90 var_Get( p_this->p_libvlc_global, "vlm_mutex", &lockval ) )
93 vlc_mutex_lock( lockval.p_address );
95 p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE );
98 vlc_object_yield( p_vlm );
99 vlc_mutex_unlock( lockval.p_address );
103 msg_Dbg( p_this, "creating VLM" );
105 p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM );
108 vlc_mutex_unlock( lockval.p_address );
112 vlc_mutex_init( p_this->p_libvlc, &p_vlm->lock );
113 TAB_INIT( p_vlm->i_media, p_vlm->media );
114 TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
117 vlc_object_yield( p_vlm );
118 vlc_object_attach( p_vlm, p_this->p_libvlc );
120 if( vlc_thread_create( p_vlm, "vlm thread",
121 Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
123 vlc_mutex_destroy( &p_vlm->lock );
124 vlc_object_destroy( p_vlm );
128 /* Load our configuration file */
129 psz_vlmconf = var_CreateGetString( p_vlm, "vlm-conf" );
130 if( psz_vlmconf && *psz_vlmconf )
132 vlm_message_t *p_message = NULL;
133 char *psz_buffer = NULL;
135 msg_Dbg( p_this, "loading VLM configuration" );
136 asprintf(&psz_buffer, "load %s", psz_vlmconf );
139 msg_Dbg( p_this, psz_buffer );
140 if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
141 msg_Warn( p_this, "error while loading the configuration file" );
143 vlm_MessageDelete(p_message);
149 vlc_mutex_unlock( lockval.p_address );
154 /*****************************************************************************
156 *****************************************************************************/
157 void vlm_Delete( vlm_t *p_vlm )
161 msg_Err( p_vlm, "vlm_Delete called" );
162 var_Get( p_vlm->p_libvlc_global, "vlm_mutex", &lockval );
163 vlc_mutex_lock( lockval.p_address );
165 vlc_object_release( p_vlm );
167 if( p_vlm->i_refcount > 0 )
169 vlc_mutex_unlock( lockval.p_address );
173 p_vlm->b_die = VLC_TRUE;
174 vlc_thread_join( p_vlm );
176 vlc_object_detach( p_vlm );
178 vlc_mutex_destroy( &p_vlm->lock );
180 while( p_vlm->i_media )
181 vlm_MediaDelete( p_vlm, p_vlm->media[0], NULL );
182 TAB_CLEAN( p_vlm->i_media, p_vlm->media );
184 while( p_vlm->i_schedule )
185 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
186 TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
188 vlc_object_destroy( p_vlm );
189 vlc_mutex_unlock( lockval.p_address );
192 /*****************************************************************************
193 * vlm_ExecuteCommand:
194 *****************************************************************************/
195 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
196 vlm_message_t **pp_message)
200 vlc_mutex_lock( &p_vlm->lock );
201 i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
202 vlc_mutex_unlock( &p_vlm->lock );
208 static const char quotes[] = "\"'";
210 * FindCommandEnd: look for the end of a possibly quoted string
211 * @return NULL on mal-formatted string,
212 * pointer past the last character otherwise.
214 static const char *FindCommandEnd (const char *psz_sent)
218 while ((c = *psz_sent) != '\0')
222 if (strchr(quotes,c)) // opening quote
225 if (isspace(c)) // non-escaped space
230 psz_sent++; // skip escaped character
231 if (*psz_sent == '\0')
237 if (c == quote) // non-escaped matching quote
240 if ((quote == '"') && (c == '\\'))
242 psz_sent++; // skip escaped character
243 if (*psz_sent == '\0')
244 return NULL; // error, closing quote missing
251 // error (NULL) if we could not find a matching quote
252 return quote ? NULL : psz_sent;
257 * Unescape a nul-terminated string.
258 * Note that in and out can be identical.
260 * @param out output buffer (at least <strlen (in) + 1> characters long)
261 * @param in nul-terminated string to be unescaped
263 * @return 0 on success, -1 on error.
265 static int Unescape (char *out, const char *in)
269 while ((c = *in++) != '\0')
273 if (strchr(quotes,c)) // opening quote
298 /* None of the special cases - copy the backslash */
304 if (c == quote) // non-escaped matching quote
309 if ((quote == '"') && (c == '\\'))
318 case '\0': // should never happen
322 /* None of the special cases - copy the backslash */
334 /*****************************************************************************
335 * ExecuteCommand: The main state machine
336 *****************************************************************************
337 * Execute a command which ends with '\0' (string)
338 *****************************************************************************/
339 static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
340 vlm_message_t **pp_message )
342 size_t i_command = 0;
343 char buf[strlen (psz_command) + 1], *psz_buf = buf;
344 char *ppsz_command[sizeof (buf) / 2];
345 vlm_message_t *p_message = NULL;
347 /* First, parse the line and cut it */
348 while( *psz_command != '\0' )
350 const char *psz_temp;
352 if(isspace (*psz_command))
358 /* support for comments */
359 if( i_command == 0 && *psz_command == '#')
361 p_message = vlm_MessageNew( "", NULL );
365 psz_temp = FindCommandEnd( psz_command );
367 if( psz_temp == NULL )
369 p_message = vlm_MessageNew( "Incomplete command", psz_command );
373 assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
375 ppsz_command[i_command] = psz_buf;
376 memcpy (psz_buf, psz_command, psz_temp - psz_command);
377 psz_buf[psz_temp - psz_command] = '\0';
379 Unescape (psz_buf, psz_buf);
382 psz_buf += psz_temp - psz_command + 1;
383 psz_command = psz_temp;
385 assert (buf + sizeof (buf) >= psz_buf);
389 * And then Interpret it
394 p_message = vlm_MessageNew( "", NULL );
398 if( !strcmp(ppsz_command[0], "new") )
402 /* Check the number of arguments */
403 if( i_command < 3 ) goto syntax_error;
406 if( !strcmp(ppsz_command[2], "vod") )
410 else if( !strcmp(ppsz_command[2], "broadcast") )
412 i_type = BROADCAST_TYPE;
414 else if( !strcmp(ppsz_command[2], "schedule") )
416 i_type = SCHEDULE_TYPE;
421 vlm_MessageNew( "new", "%s: Choose between vod, "
422 "broadcast or schedule", ppsz_command[1] );
426 /* Check for forbidden media names */
427 if( !strcmp(ppsz_command[1], "all") ||
428 !strcmp(ppsz_command[1], "media") ||
429 !strcmp(ppsz_command[1], "schedule") )
431 p_message = vlm_MessageNew( "new", "\"all\", \"media\" and "
432 "\"schedule\" are reserved names" );
436 /* Check the name is not already in use */
437 if( vlm_ScheduleSearch( p_vlm, ppsz_command[1] ) ||
438 vlm_MediaSearch( p_vlm, ppsz_command[1] ) )
440 p_message = vlm_MessageNew( "new", "%s: Name already in use",
446 if( i_type == SCHEDULE_TYPE )
448 vlm_schedule_t *p_schedule;
449 p_schedule = vlm_ScheduleNew( p_vlm, ppsz_command[1] );
452 p_message = vlm_MessageNew( "new", "could not create schedule" );
460 vlm_media_t *p_media;
461 p_media = vlm_MediaNew( p_vlm, ppsz_command[1], i_type );
464 p_message = vlm_MessageNew( "new", "could not create media" );
471 p_message = vlm_MessageNew( "new", NULL );
475 /* Properties will be dealt with later on */
478 else if( !strcmp(ppsz_command[0], "setup") )
480 if( i_command < 2 ) goto syntax_error;
482 /* Properties will be dealt with later on */
485 else if( !strcmp(ppsz_command[0], "del") )
487 vlm_media_t *p_media;
488 vlm_schedule_t *p_schedule;
490 if( i_command < 2 ) goto syntax_error;
492 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
493 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
495 if( p_schedule != NULL )
497 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
499 else if( p_media != NULL )
501 vlm_MediaDelete( p_vlm, p_media, NULL );
503 else if( !strcmp(ppsz_command[1], "media") )
505 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
508 else if( !strcmp(ppsz_command[1], "schedule") )
510 while( p_vlm->i_schedule )
511 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
513 else if( !strcmp(ppsz_command[1], "all") )
515 while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
518 while( p_vlm->i_schedule )
519 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
523 p_message = vlm_MessageNew( "del", "%s: media unknown",
528 p_message = vlm_MessageNew( "del", NULL );
532 else if( !strcmp(ppsz_command[0], "show") )
534 vlm_media_t *p_media;
535 vlm_schedule_t *p_schedule;
539 p_message = vlm_Show( p_vlm, NULL, NULL, NULL );
542 else if( i_command > 2 ) goto syntax_error;
544 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
545 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
547 if( p_schedule != NULL )
549 p_message = vlm_Show( p_vlm, NULL, p_schedule, NULL );
551 else if( p_media != NULL )
553 p_message = vlm_Show( p_vlm, p_media, NULL, NULL );
557 p_message = vlm_Show( p_vlm, NULL, NULL, ppsz_command[1] );
563 else if( !strcmp(ppsz_command[0], "help") )
565 if( i_command != 1 ) goto syntax_error;
567 p_message = vlm_Help( p_vlm, NULL );
571 else if( !strcmp(ppsz_command[0], "control") )
573 vlm_media_t *p_media;
575 if( i_command < 3 ) goto syntax_error;
577 if( !(p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] ) ) )
579 p_message = vlm_MessageNew( "control", "%s: media unknown",
585 char *psz_command, *psz_arg = 0, *psz_instance = 0;
588 if( strcmp( ppsz_command[2], "play" ) &&
589 strcmp( ppsz_command[2], "stop" ) &&
590 strcmp( ppsz_command[2], "pause" ) &&
591 strcmp( ppsz_command[2], "seek" ) )
594 psz_instance = ppsz_command[2];
596 if( i_command < 4 ) goto syntax_error;
598 if( strcmp( ppsz_command[3], "play" ) &&
599 strcmp( ppsz_command[3], "stop" ) &&
600 strcmp( ppsz_command[3], "pause" ) &&
601 strcmp( ppsz_command[3], "seek" ) )
605 psz_command = ppsz_command[i_index];
607 if( i_command >= i_index + 2 ) psz_arg = ppsz_command[i_index + 1];
609 vlm_MediaControl( p_vlm, p_media, psz_instance, psz_command,
611 p_message = vlm_MessageNew( "control", NULL );
616 else if( !strcmp(ppsz_command[0], "save") )
618 if( i_command != 2 ) goto syntax_error;
620 if( vlm_Save( p_vlm, ppsz_command[1] ) )
622 p_message = vlm_MessageNew( "save", "Unable to save to file" );
627 p_message = vlm_MessageNew( "save", NULL );
632 else if( !strcmp(ppsz_command[0], "export" ) )
636 if( i_command != 1 ) goto syntax_error;
638 p_message = vlm_MessageNew( "export", psz_buf = Save( p_vlm ) );
644 else if( !strcmp(ppsz_command[0], "load") )
646 if( i_command != 2 ) goto syntax_error;
648 switch( vlm_Load( p_vlm, ppsz_command[1] ) )
651 p_message = vlm_MessageNew( "load", NULL );
654 p_message = vlm_MessageNew( "load", "Read file error" );
658 vlm_MessageNew( "load", "Error while loading file" );
662 vlm_MessageNew( "load", "Unable to load from file" );
669 p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
673 /* Common code between "new" and "setup" */
674 if( !strcmp(ppsz_command[0], "new") ||
675 !strcmp(ppsz_command[0], "setup") )
677 size_t i_command_start = strcmp(ppsz_command[0], "new") ? 2 : 3;
678 vlm_media_t *p_media;
679 vlm_schedule_t *p_schedule;
681 if( i_command < i_command_start ) goto syntax_error;
683 p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
684 p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
686 if( !p_media && !p_schedule )
688 p_message = vlm_MessageNew( ppsz_command[0], "%s unknown",
693 if( p_schedule != NULL )
697 for( i = i_command_start ; i < i_command ; i++ )
699 if( !strcmp( ppsz_command[i], "enabled" ) ||
700 !strcmp( ppsz_command[i], "disabled" ) )
702 vlm_ScheduleSetup( p_schedule, ppsz_command[i], NULL );
705 /* Beware: everything behind append is considered as
707 else if( !strcmp( ppsz_command[i], "append" ) )
711 if( ++i >= i_command ) break;
713 for( j = i + 1; j < i_command; j++ )
716 realloc( ppsz_command[i], strlen(ppsz_command[i]) +
717 strlen(ppsz_command[j]) + 1 + 1 );
718 strcat( ppsz_command[i], " " );
719 strcat( ppsz_command[i], ppsz_command[j] );
722 vlm_ScheduleSetup( p_schedule, ppsz_command[i - 1],
728 if( i + 1 >= i_command && !strcmp(ppsz_command[0], "new") )
730 vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
732 vlm_MessageNew( ppsz_command[0],
733 "Wrong properties syntax" );
736 else if( i + 1 >= i_command )
739 vlm_MessageNew( ppsz_command[0],
740 "Wrong properties syntax" );
744 vlm_ScheduleSetup( p_schedule, ppsz_command[i],
751 else if( p_media != NULL )
755 for( i = i_command_start ; i < i_command ; i++ )
757 if( !strcmp( ppsz_command[i], "enabled" ) ||
758 !strcmp( ppsz_command[i], "disabled" ) )
760 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
762 else if( i + 1 >= i_command &&
763 !strcmp( ppsz_command[i], "mux") )
765 if( p_media->i_type != VOD_TYPE )
767 p_message = vlm_MessageNew( ppsz_command[0],
768 "mux only available for broadcast" );
772 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
777 else if( !strcmp( ppsz_command[i], "loop" ) ||
778 !strcmp( ppsz_command[i], "unloop" ) )
780 if( p_media->i_type != BROADCAST_TYPE )
782 p_message = vlm_MessageNew( ppsz_command[0],
783 "loop only available for broadcast" );
787 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
792 if( ( (i + 1) >= i_command ) &&
793 !strcmp(ppsz_command[0], "new") )
795 vlm_MediaDelete( p_vlm, p_media, NULL );
797 vlm_MessageNew( ppsz_command[0],
798 "Wrong properties syntax" );
801 else if( (i + 1) >= i_command )
804 vlm_MessageNew( ppsz_command[0],
805 "Wrong properties syntax" );
809 vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
816 p_message = vlm_MessageNew( ppsz_command[0], NULL );
821 *pp_message = p_message;
825 p_message = vlm_MessageNew( ppsz_command[0], "Wrong command syntax" );
828 *pp_message = p_message;
832 vlm_media_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
836 for( i = 0; i < vlm->i_media; i++ )
838 if( strcmp( psz_name, vlm->media[i]->psz_name ) == 0 )
840 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 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 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 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 int vlm_MediaControl( vlm_t *vlm, vlm_media_t *media, const char *psz_id,
1211 const char *psz_command, const char *psz_args )
1213 vlm_media_instance_t *p_instance;
1217 p_instance = vlm_MediaInstanceSearch( vlm, media, psz_id );
1219 if( !strcmp( psz_command, "play" ) )
1221 if( !media->b_enabled || media->i_input == 0 ) return 0;
1227 p_instance = malloc( sizeof(vlm_media_instance_t) );
1231 memset( p_instance, 0, sizeof(vlm_media_instance_t) );
1233 p_instance->psz_name = psz_id ? strdup( psz_id ) : NULL;
1234 input_ItemInit( VLC_OBJECT(vlm), &p_instance->item );
1235 p_instance->p_input = NULL;
1237 TAB_INIT( p_instance->item.i_options, p_instance->item.ppsz_options );
1239 if( media->psz_output != NULL || media->psz_vod_output != NULL )
1241 asprintf( &psz_dup, "sout=%s%s%s",
1242 media->psz_output ? media->psz_output : "",
1243 (media->psz_output && media->psz_vod_output) ? ":" : media->psz_vod_output ? "#" : "",
1244 media->psz_vod_output ? media->psz_vod_output : "" );
1245 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, psz_dup );
1248 for( i = 0; i < media->i_option; i++ )
1250 psz_dup = strdup( media->option[i] );
1251 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, psz_dup );
1253 psz_dup = strdup( "no-sout-keep" );
1254 TAB_APPEND( p_instance->item.i_options, p_instance->item.ppsz_options, psz_dup );
1256 TAB_APPEND( media->i_instance, media->instance, p_instance );
1259 if( ( psz_args && sscanf(psz_args, "%d", &i) == 1 ) && i > 0 && i-1 < media->i_input )
1261 p_instance->i_index = i-1;
1264 if( p_instance->item.psz_uri ) free( p_instance->item.psz_uri );
1265 p_instance->item.psz_uri =
1266 strdup( media->input[p_instance->i_index] );
1268 if( p_instance->p_input )
1270 input_StopThread( p_instance->p_input );
1271 input_DestroyThreadExtended( p_instance->p_input, NULL );
1274 asprintf( &psz_header, _("Media: %s"), media->psz_name );
1275 p_instance->p_input = input_CreateThreadExtended( vlm, &p_instance->item, psz_header, NULL );
1276 if( !p_instance->p_input )
1278 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1279 input_ItemClean( &p_instance->item );
1280 if( p_instance->psz_name ) free( p_instance->psz_name );
1287 if( !p_instance ) return VLC_EGENERIC;
1289 if( !strcmp( psz_command, "seek" ) )
1294 float f_value = i18n_atof( psz_args );
1295 if( psz_args[0] == '+' || psz_args[0] == '-' )
1299 if( strstr( psz_args, "ms" ) )
1302 int64_t i_msec = 1000 * (int64_t)atoi( psz_args );
1305 var_SetTime( p_instance->p_input, "time-offset",
1308 else if( i_msec >= 0
1309 && i_msec < var_GetTime( p_instance->p_input, "length" ) )
1311 var_SetTime( p_instance->p_input, "time",
1315 else if( strchr( psz_args, 's' ) )
1318 int64_t i_sec = 1000000 * (int64_t)atoi( psz_args );
1321 var_SetTime( p_instance->p_input, "time-offset",
1325 && i_sec < var_GetTime( p_instance->p_input, "length" ) )
1327 var_SetTime( p_instance->p_input, "time",
1337 float f_orig = var_GetFloat( p_instance->p_input,
1341 if( f_value >= 0.0 && f_value <= 1.0 )
1343 var_SetFloat( p_instance->p_input, "position",
1350 else if( !strcmp( psz_command, "rewind" ) )
1357 f_scale = i18n_atof( psz_args );
1358 f_pos = var_GetFloat( p_instance->p_input, "position" );
1359 f_pos -= (f_scale / 1000.0);
1362 var_SetFloat( p_instance->p_input, "position", f_pos );
1366 else if( !strcmp( psz_command, "forward" ) )
1373 f_scale = i18n_atof( psz_args );
1374 f_pos = var_GetFloat( p_instance->p_input, "position" );
1375 f_pos += (f_scale / 1000.0);
1378 var_SetFloat( p_instance->p_input, "position", f_pos );
1382 else if( !strcmp( psz_command, "stop" ) )
1384 TAB_REMOVE( media->i_instance, media->instance, p_instance );
1386 if( p_instance->p_input )
1388 input_StopThread( p_instance->p_input );
1389 input_DestroyThreadExtended( p_instance->p_input, NULL );
1392 input_ItemClean( &p_instance->item );
1393 if( p_instance->psz_name ) free( p_instance->psz_name );
1398 else if( !strcmp( psz_command, "pause" ) )
1402 if( !p_instance->p_input ) return VLC_SUCCESS;
1404 i_state = var_GetInteger( p_instance->p_input, "state" );
1406 if( i_state == PAUSE_S ) i_state = PLAYING_S;
1407 else i_state = PAUSE_S;
1408 var_SetInteger( p_instance->p_input, "state", i_state );
1413 return VLC_EGENERIC;
1416 /*****************************************************************************
1418 *****************************************************************************/
1419 static int64_t vlm_Date(void)
1424 return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1430 static vlm_schedule_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
1432 vlm_schedule_t *p_sched = malloc( sizeof( vlm_schedule_t ) );
1444 p_sched->psz_name = strdup( psz_name );
1445 p_sched->b_enabled = VLC_FALSE;
1446 p_sched->i_command = 0;
1447 p_sched->command = NULL;
1448 p_sched->i_date = 0;
1449 p_sched->i_period = 0;
1450 p_sched->i_repeat = -1;
1452 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1457 /* for now, simple delete. After, del with options (last arg) */
1458 static void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_t *sched,
1459 const char *psz_name )
1461 if( sched == NULL ) return;
1463 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1465 if( vlm->i_schedule == 0 && vlm->schedule ) free( vlm->schedule );
1466 free( sched->psz_name );
1467 while( sched->i_command )
1469 char *psz_cmd = sched->command[0];
1470 TAB_REMOVE( sched->i_command, sched->command, psz_cmd );
1476 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1480 for( i = 0; i < vlm->i_schedule; i++ )
1482 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1484 return vlm->schedule[i];
1491 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
1492 static int vlm_ScheduleSetup( vlm_schedule_t *schedule, const char *psz_cmd,
1493 const char *psz_value )
1495 if( !strcmp( psz_cmd, "enabled" ) )
1497 schedule->b_enabled = VLC_TRUE;
1499 else if( !strcmp( psz_cmd, "disabled" ) )
1501 schedule->b_enabled = VLC_FALSE;
1503 #if !defined( UNDER_CE )
1504 else if( !strcmp( psz_cmd, "date" ) )
1510 time.tm_sec = 0; /* seconds */
1511 time.tm_min = 0; /* minutes */
1512 time.tm_hour = 0; /* hours */
1513 time.tm_mday = 0; /* day of the month */
1514 time.tm_mon = 0; /* month */
1515 time.tm_year = 0; /* year */
1516 time.tm_wday = 0; /* day of the week */
1517 time.tm_yday = 0; /* day in the year */
1518 time.tm_isdst = -1; /* daylight saving time */
1520 /* date should be year/month/day-hour:minutes:seconds */
1521 p = strchr( psz_value, '-' );
1523 if( !strcmp( psz_value, "now" ) )
1525 schedule->i_date = 0;
1527 else if( (p == NULL) && sscanf( psz_value, "%d:%d:%d", &time.tm_hour,
1528 &time.tm_min, &time.tm_sec ) != 3 )
1529 /* it must be a hour:minutes:seconds */
1537 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1555 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1561 time.tm_mon = i - 1;
1565 time.tm_year = i - 1900;
1566 time.tm_mon = j - 1;
1573 date = mktime( &time );
1574 schedule->i_date = ((mtime_t) date) * 1000000;
1577 else if( !strcmp( psz_cmd, "period" ) )
1581 const char *psz_time = NULL, *psz_date = NULL;
1585 /* First, if date or period are modified, repeat should be equal to -1 */
1586 schedule->i_repeat = -1;
1588 time.tm_sec = 0; /* seconds */
1589 time.tm_min = 0; /* minutes */
1590 time.tm_hour = 0; /* hours */
1591 time.tm_mday = 0; /* day of the month */
1592 time.tm_mon = 0; /* month */
1593 time.tm_year = 0; /* year */
1594 time.tm_wday = 0; /* day of the week */
1595 time.tm_yday = 0; /* day in the year */
1596 time.tm_isdst = -1; /* daylight saving time */
1598 /* date should be year/month/day-hour:minutes:seconds */
1599 p = strchr( psz_value, '-' );
1602 psz_date = psz_value;
1607 psz_time = psz_value;
1610 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1629 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1648 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1649 date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1650 schedule->i_period = ((mtime_t) date) * 1000000;
1652 #endif /* UNDER_CE */
1653 else if( !strcmp( psz_cmd, "repeat" ) )
1657 if( sscanf( psz_value, "%d", &i ) == 1 )
1659 schedule->i_repeat = i;
1666 else if( !strcmp( psz_cmd, "append" ) )
1668 char *command = strdup( psz_value );
1670 TAB_APPEND( schedule->i_command, schedule->command, command );
1679 /*****************************************************************************
1680 * Message handling functions
1681 *****************************************************************************/
1682 vlm_message_t *vlm_MessageNew( const char *psz_name,
1683 const char *psz_format, ... )
1685 vlm_message_t *p_message;
1688 if( !psz_name ) return NULL;
1690 p_message = malloc( sizeof(vlm_message_t) );
1696 p_message->psz_value = 0;
1700 va_start( args, psz_format );
1701 if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1710 p_message->psz_name = strdup( psz_name );
1711 p_message->i_child = 0;
1712 p_message->child = NULL;
1717 void vlm_MessageDelete( vlm_message_t *p_message )
1719 if( p_message->psz_name ) free( p_message->psz_name );
1720 if( p_message->psz_value ) free( p_message->psz_value );
1721 while( p_message->i_child-- )
1722 vlm_MessageDelete( p_message->child[p_message->i_child] );
1723 if( p_message->child ) free( p_message->child );
1728 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1729 vlm_message_t *p_child )
1731 if( p_message == NULL ) return NULL;
1735 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1741 /*****************************************************************************
1742 * Misc utility functions
1743 *****************************************************************************/
1744 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_t *media,
1745 vlm_schedule_t *schedule,
1746 const char *psz_filter )
1752 vlm_message_t *msg_media;
1753 vlm_message_t *msg_child;
1755 msg = vlm_MessageNew( "show", NULL );
1756 msg_media = vlm_MessageAdd( msg, vlm_MessageNew( media->psz_name, 0 ));
1758 vlm_MessageAdd( msg_media,
1759 vlm_MessageNew( "type", media->i_type == VOD_TYPE ?
1760 "vod" : "broadcast" ) );
1761 vlm_MessageAdd( msg_media,
1762 vlm_MessageNew( "enabled", media->b_enabled ?
1765 vlm_MessageAdd( msg_media,
1766 vlm_MessageNew( "loop", media->b_loop ?
1769 if( media->i_type == VOD_TYPE && media->psz_mux )
1770 vlm_MessageAdd( msg_media,
1771 vlm_MessageNew( "mux", media->psz_mux ) );
1773 msg_child = vlm_MessageAdd( msg_media,
1774 vlm_MessageNew( "inputs", NULL ) );
1776 for( i = 0; i < media->i_input; i++ )
1778 vlm_MessageAdd( msg_child,
1779 vlm_MessageNew( media->input[i], NULL ) );
1782 vlm_MessageAdd( msg_media,
1783 vlm_MessageNew( "output", media->psz_output ?
1784 media->psz_output : "" ) );
1786 msg_child = vlm_MessageAdd( msg_media, vlm_MessageNew( "options", 0 ));
1788 for( i = 0; i < media->i_option; i++ )
1790 vlm_MessageAdd( msg_child, vlm_MessageNew( media->option[i], 0 ) );
1793 msg_child = vlm_MessageAdd( msg_media,
1794 vlm_MessageNew( "instances", NULL ) );
1796 for( i = 0; i < media->i_instance; i++ )
1798 vlm_media_instance_t *p_instance = media->instance[i];
1800 vlm_message_t *msg_instance;
1803 if( !p_instance->p_input ) val.i_int = END_S;
1804 else var_Get( p_instance->p_input, "state", &val );
1806 msg_instance = vlm_MessageNew( "instance" , NULL );
1807 vlm_MessageAdd( msg_instance, vlm_MessageNew( "name" , p_instance->psz_name ? p_instance->psz_name : "default" ) );
1808 vlm_MessageAdd( msg_instance, vlm_MessageNew( "state",
1809 val.i_int == PLAYING_S ? "playing" :
1810 val.i_int == PAUSE_S ? "paused" :
1812 #define APPEND_INPUT_INFO( a, format, type ) \
1813 asprintf( &psz_tmp, format, \
1814 var_Get ## type( p_instance->p_input, a ) ); \
1815 vlm_MessageAdd( msg_instance, vlm_MessageNew( a, psz_tmp ) ); \
1817 APPEND_INPUT_INFO( "position", "%f", Float );
1818 APPEND_INPUT_INFO( "time", I64Fi, Time );
1819 APPEND_INPUT_INFO( "length", I64Fi, Time );
1820 APPEND_INPUT_INFO( "rate", "%d", Integer );
1821 APPEND_INPUT_INFO( "title", "%d", Integer );
1822 APPEND_INPUT_INFO( "chapter", "%d", Integer );
1823 APPEND_INPUT_INFO( "seekable", "%d", Bool );
1824 #undef APPEND_INPUT_INFO
1825 asprintf( &psz_tmp, "%d", p_instance->i_index + 1 );
1826 vlm_MessageAdd( msg_instance, vlm_MessageNew( "playlistindex", psz_tmp ) );
1828 vlm_MessageAdd( msg_child, msg_instance );
1835 else if( schedule != NULL )
1839 vlm_message_t *msg_schedule;
1840 vlm_message_t *msg_child;
1843 msg = vlm_MessageNew( "show", NULL );
1845 vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, 0 ) );
1847 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1849 vlm_MessageAdd( msg_schedule,
1850 vlm_MessageNew( "enabled", schedule->b_enabled ?
1853 #if !defined( UNDER_CE )
1854 if( schedule->i_date != 0 )
1857 time_t i_time = (time_t)( schedule->i_date / 1000000 );
1860 #ifdef HAVE_LOCALTIME_R
1861 localtime_r( &i_time, &date);
1863 struct tm *p_date = localtime( &i_time );
1867 asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1868 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1869 date.tm_hour, date.tm_min, date.tm_sec );
1871 vlm_MessageAdd( msg_schedule,
1872 vlm_MessageNew( "date", psz_date ) );
1877 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1880 if( schedule->i_period != 0 )
1882 time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1885 date.tm_sec = (int)( i_time % 60 );
1886 i_time = i_time / 60;
1887 date.tm_min = (int)( i_time % 60 );
1888 i_time = i_time / 60;
1889 date.tm_hour = (int)( i_time % 24 );
1890 i_time = i_time / 24;
1891 date.tm_mday = (int)( i_time % 30 );
1892 i_time = i_time / 30;
1893 /* okay, okay, months are not always 30 days long */
1894 date.tm_mon = (int)( i_time % 12 );
1895 i_time = i_time / 12;
1896 date.tm_year = (int)i_time;
1898 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1899 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1901 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1905 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1907 #endif /* UNDER_CE */
1909 sprintf( buffer, "%d", schedule->i_repeat );
1910 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1913 vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", 0) );
1915 for( i = 0; i < schedule->i_command; i++ )
1917 vlm_MessageAdd( msg_child,
1918 vlm_MessageNew( schedule->command[i], NULL ) );
1925 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1929 vlm_message_t *msg_child;
1930 int i_vod = 0, i_broadcast = 0;
1933 for( i = 0; i < vlm->i_media; i++ )
1935 if( vlm->media[i]->i_type == VOD_TYPE )
1941 asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1943 msg = vlm_MessageNew( "show", NULL );
1944 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "media", psz_count ) );
1947 for( i = 0; i < vlm->i_media; i++ )
1949 vlm_media_t *m = vlm->media[i];
1950 vlm_message_t *msg_media, *msg_instance;
1952 msg_media = vlm_MessageAdd( msg_child,
1953 vlm_MessageNew( m->psz_name, 0 ) );
1955 vlm_MessageAdd( msg_media,
1956 vlm_MessageNew( "type", m->i_type == VOD_TYPE ?
1957 "vod" : "broadcast" ) );
1959 vlm_MessageAdd( msg_media,
1960 vlm_MessageNew( "enabled", m->b_enabled ?
1963 if( m->i_type == VOD_TYPE && m->psz_mux )
1964 vlm_MessageAdd( msg_media,
1965 vlm_MessageNew( "mux", m->psz_mux ) );
1967 msg_instance = vlm_MessageAdd( msg_media,
1968 vlm_MessageNew( "instances", 0 ) );
1970 for( j = 0; j < m->i_instance; j++ )
1972 vlm_media_instance_t *p_instance = m->instance[j];
1975 if( !p_instance->p_input ) val.i_int = END_S;
1976 else var_Get( p_instance->p_input, "state", &val );
1978 vlm_MessageAdd( msg_instance,
1979 vlm_MessageNew( p_instance->psz_name ?
1980 p_instance->psz_name : "default",
1981 val.i_int == PLAYING_S ? "playing" :
1982 val.i_int == PAUSE_S ? "paused" :
1990 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1994 vlm_message_t *msg_child;
1996 msg = vlm_MessageNew( "show", NULL );
1997 msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", NULL ) );
1999 for( i = 0; i < vlm->i_schedule; i++ )
2001 vlm_schedule_t *s = vlm->schedule[i];
2002 vlm_message_t *msg_schedule;
2003 mtime_t i_time, i_next_date;
2005 msg_schedule = vlm_MessageAdd( msg_child,
2006 vlm_MessageNew( s->psz_name, 0 ) );
2007 vlm_MessageAdd( msg_schedule,
2008 vlm_MessageNew( "enabled", s->b_enabled ?
2011 /* calculate next date */
2012 i_time = vlm_Date();
2013 i_next_date = s->i_date;
2015 if( s->i_period != 0 )
2018 while( s->i_date + j * s->i_period <= i_time &&
2024 i_next_date = s->i_date + j * s->i_period;
2027 if( i_next_date > i_time )
2029 time_t i_date = (time_t) (i_next_date / 1000000) ;
2031 #if !defined( UNDER_CE )
2034 ctime_r( &i_date, psz_date );
2036 char *psz_date = ctime( &i_date );
2039 vlm_MessageAdd( msg_schedule,
2040 vlm_MessageNew( "next launch", psz_date ) );
2048 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
2050 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
2051 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
2053 vlm_MessageAdd( show1, show2->child[0] );
2055 /* We must destroy the parent node "show" of show2
2056 * and not the children */
2057 free( show2->psz_name );
2065 return vlm_MessageNew( "show", NULL );
2069 static vlm_message_t *vlm_Help( vlm_t *vlm, char *psz_filter )
2071 vlm_message_t *message, *message_child;
2073 #define MessageAdd( a ) \
2074 vlm_MessageAdd( message, vlm_MessageNew( a, NULL ) );
2075 #define MessageAddChild( a ) \
2076 vlm_MessageAdd( message_child, vlm_MessageNew( a, NULL ) );
2078 if( psz_filter == NULL )
2080 message = vlm_MessageNew( "help", NULL );
2082 message_child = MessageAdd( "Commands Syntax:" );
2083 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
2084 MessageAddChild( "setup (name) (properties)" );
2085 MessageAddChild( "show [(name)|media|schedule]" );
2086 MessageAddChild( "del (name)|all|media|schedule" );
2087 MessageAddChild( "control (name) [instance_name] (command)" );
2088 MessageAddChild( "save (config_file)" );
2089 MessageAddChild( "export" );
2090 MessageAddChild( "load (config_file)" );
2092 message_child = MessageAdd( "Media Proprieties Syntax:" );
2093 MessageAddChild( "input (input_name)" );
2094 MessageAddChild( "inputdel (input_name)|all" );
2095 MessageAddChild( "inputdeln input_number" );
2096 MessageAddChild( "output (output_name)" );
2097 MessageAddChild( "option (option_name)[=value]" );
2098 MessageAddChild( "enabled|disabled" );
2099 MessageAddChild( "loop|unloop (broadcast only)" );
2100 MessageAddChild( "mux (mux_name)" );
2102 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
2103 MessageAddChild( "enabled|disabled" );
2104 MessageAddChild( "append (command_until_rest_of_the_line)" );
2105 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
2107 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
2108 "(days)-(hours):(minutes):(seconds)" );
2109 MessageAddChild( "repeat (number_of_repetitions)" );
2111 message_child = MessageAdd( "Control Commands Syntax:" );
2112 MessageAddChild( "play [input_number]" );
2113 MessageAddChild( "pause" );
2114 MessageAddChild( "stop" );
2115 MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](miliseconds)ms" );
2120 return vlm_MessageNew( "help", NULL );
2123 /*****************************************************************************
2124 * Config handling functions
2125 *****************************************************************************/
2126 static int Load( vlm_t *vlm, char *file )
2131 while( *pf != '\0' )
2133 vlm_message_t *message = NULL;
2136 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
2141 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
2145 if( pf[i_end] == '\n' ) i_end++;
2148 if( *pf && ExecuteCommand( vlm, pf, &message ) )
2152 if( message->psz_value )
2153 msg_Err( vlm, "Load error on line %d: %s: %s",
2154 i_line, message->psz_name, message->psz_value );
2155 vlm_MessageDelete( message );
2159 if( message ) vlm_MessageDelete( message );
2168 static char *Save( vlm_t *vlm )
2171 char psz_header[] = "\n"
2172 "# VLC media player VLM command batch\n"
2173 "# http://www.videolan.org/vlc/\n\n" ;
2176 int i_length = strlen( psz_header );
2178 for( i = 0; i < vlm->i_media; i++ )
2180 vlm_media_t *media = vlm->media[i];
2182 if( media->i_type == VOD_TYPE )
2184 i_length += strlen( "new vod " ) + strlen(media->psz_name);
2188 i_length += strlen( "new broadcast " ) + strlen(media->psz_name);
2191 if( media->b_enabled == VLC_TRUE )
2193 i_length += strlen( "enabled" );
2197 i_length += strlen( "disabled" );
2200 if( media->b_loop == VLC_TRUE )
2202 i_length += strlen( " loop\n" );
2206 i_length += strlen( "\n" );
2209 for( j = 0; j < media->i_input; j++ )
2211 i_length += strlen( "setup input \"\"\n" ) +
2212 strlen( media->psz_name ) + strlen( media->input[j] );
2215 if( media->psz_output != NULL )
2217 i_length += strlen(media->psz_name) + strlen(media->psz_output) +
2218 strlen( "setup output \n" );
2221 for( j=0 ; j < media->i_option ; j++ )
2223 i_length += strlen(media->psz_name) + strlen(media->option[j]) +
2224 strlen("setup option \n");
2228 for( i = 0; i < vlm->i_schedule; i++ )
2230 vlm_schedule_t *schedule = vlm->schedule[i];
2232 i_length += strlen( "new schedule " ) + strlen( schedule->psz_name );
2234 if( schedule->b_enabled == VLC_TRUE )
2236 i_length += strlen( "date //-:: enabled\n" ) + 14;
2240 i_length += strlen( "date //-:: disabled\n" ) + 14;
2244 if( schedule->i_period != 0 )
2246 i_length += strlen( "setup " ) + strlen( schedule->psz_name ) +
2247 strlen( "period //-::\n" ) + 14;
2250 if( schedule->i_repeat >= 0 )
2254 sprintf( buffer, "%d", schedule->i_repeat );
2255 i_length += strlen( "setup repeat \n" ) +
2256 strlen( schedule->psz_name ) + strlen( buffer );
2263 for( j = 0; j < schedule->i_command; j++ )
2265 i_length += strlen( "setup append \n" ) +
2266 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
2271 /* Don't forget the '\0' */
2273 /* now we have the length of save */
2275 p = save = malloc( i_length );
2278 p += sprintf( p, "%s", psz_header );
2280 /* finally we can write in it */
2281 for( i = 0; i < vlm->i_media; i++ )
2283 vlm_media_t *media = vlm->media[i];
2285 if( media->i_type == VOD_TYPE )
2287 p += sprintf( p, "new %s vod ", media->psz_name);
2291 p += sprintf( p, "new %s broadcast ", media->psz_name);
2294 if( media->b_enabled == VLC_TRUE )
2296 p += sprintf( p, "enabled" );
2300 p += sprintf( p, "disabled" );
2303 if( media->b_loop == VLC_TRUE )
2305 p += sprintf( p, " loop\n" );
2309 p += sprintf( p, "\n" );
2312 for( j = 0; j < media->i_input; j++ )
2314 p += sprintf( p, "setup %s input \"%s\"\n", media->psz_name,
2318 if( media->psz_output != NULL )
2320 p += sprintf( p, "setup %s output %s\n", media->psz_name,
2321 media->psz_output );
2324 for( j = 0; j < media->i_option; j++ )
2326 p += sprintf( p, "setup %s option %s\n", media->psz_name,
2331 /* and now, the schedule scripts */
2332 #if !defined( UNDER_CE )
2333 for( i = 0; i < vlm->i_schedule; i++ )
2335 vlm_schedule_t *schedule = vlm->schedule[i];
2337 time_t i_time = (time_t) ( schedule->i_date / 1000000 );
2339 #ifdef HAVE_LOCALTIME_R
2340 localtime_r( &i_time, &date);
2342 struct tm *p_date = localtime( &i_time );
2346 p += sprintf( p, "new %s schedule ", schedule->psz_name);
2348 if( schedule->b_enabled == VLC_TRUE )
2350 p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
2351 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2352 date.tm_hour, date.tm_min, date.tm_sec );
2356 p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
2357 date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2358 date.tm_hour, date.tm_min, date.tm_sec);
2361 if( schedule->i_period != 0 )
2363 p += sprintf( p, "setup %s ", schedule->psz_name );
2365 i_time = (time_t) ( schedule->i_period / 1000000 );
2367 date.tm_sec = (int)( i_time % 60 );
2368 i_time = i_time / 60;
2369 date.tm_min = (int)( i_time % 60 );
2370 i_time = i_time / 60;
2371 date.tm_hour = (int)( i_time % 24 );
2372 i_time = i_time / 24;
2373 date.tm_mday = (int)( i_time % 30 );
2374 i_time = i_time / 30;
2375 /* okay, okay, months are not always 30 days long */
2376 date.tm_mon = (int)( i_time % 12 );
2377 i_time = i_time / 12;
2378 date.tm_year = (int)i_time;
2380 p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
2381 date.tm_year, date.tm_mon, date.tm_mday,
2382 date.tm_hour, date.tm_min, date.tm_sec);
2385 if( schedule->i_repeat >= 0 )
2387 p += sprintf( p, "setup %s repeat %d\n",
2388 schedule->psz_name, schedule->i_repeat );
2392 p += sprintf( p, "\n" );
2395 for( j = 0; j < schedule->i_command; j++ )
2397 p += sprintf( p, "setup %s append %s\n",
2398 schedule->psz_name, schedule->command[j] );
2402 #endif /* UNDER_CE */
2407 /*****************************************************************************
2409 *****************************************************************************/
2410 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
2411 const char *psz_id, int i_query, va_list args )
2413 vlm_t *vlm = (vlm_t *)p_private;
2414 int i, i_ret = VLC_EGENERIC;
2416 if( !p_private || !p_vod_media ) return VLC_EGENERIC;
2418 vlc_mutex_lock( &vlm->lock );
2421 for( i = 0; i < vlm->i_media; i++ )
2423 if( p_vod_media == vlm->media[i]->vod_media ) break;
2426 if( i == vlm->i_media )
2428 vlc_mutex_unlock( &vlm->lock );
2429 return VLC_EGENERIC;
2434 case VOD_MEDIA_PLAY:
2435 vlm->media[i]->psz_vod_output = (char *)va_arg( args, char * );
2436 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "play", 0 );
2437 vlm->media[i]->psz_vod_output = 0;
2440 case VOD_MEDIA_PAUSE:
2441 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "pause", 0 );
2444 case VOD_MEDIA_STOP:
2445 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "stop", 0 );
2448 case VOD_MEDIA_SEEK:
2450 double f_pos = (double)va_arg( args, double );
2452 lldiv_t div = lldiv( f_pos * 10000000, 10000000 );
2453 sprintf( psz_pos, I64Fd".%07u", div.quot, (unsigned int) div.rem );
2454 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "seek", psz_pos);
2458 case VOD_MEDIA_REWIND:
2460 double f_scale = (double)va_arg( args, double );
2462 lldiv_t div = lldiv( f_scale * 10000000, 10000000 );
2463 sprintf( psz_scale, I64Fd".%07u", div.quot, (unsigned int) div.rem );
2464 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "rewind", psz_scale );
2468 case VOD_MEDIA_FORWARD:
2470 double f_scale = (double)va_arg( args, double );
2472 lldiv_t div = lldiv( f_scale * 10000000, 10000000 );
2473 sprintf( psz_scale, I64Fd".%07u", div.quot, (unsigned int) div.rem );
2474 i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "forward", psz_scale );
2482 vlc_mutex_unlock( &vlm->lock );
2488 /*****************************************************************************
2490 *****************************************************************************/
2491 static int Manage( vlc_object_t* p_object )
2493 vlm_t *vlm = (vlm_t*)p_object;
2495 mtime_t i_lastcheck;
2498 i_lastcheck = vlm_Date();
2502 while( !vlm->b_die )
2504 char **ppsz_scheduled_commands = NULL;
2505 int i_scheduled_commands = 0;
2506 vlc_mutex_lock( &vlm->lock );
2508 /* destroy the inputs that wants to die, and launch the next input */
2509 for( i = 0; i < vlm->i_media; i++ )
2511 vlm_media_t *p_media = vlm->media[i];
2513 for( j = 0; j < p_media->i_instance; j++ )
2515 vlm_media_instance_t *p_instance = p_media->instance[j];
2517 if( !p_instance->p_input ||
2518 ( !p_instance->p_input->b_eof && !p_instance->p_input->b_error ) )
2521 p_instance->i_index++;
2522 if( p_instance->i_index >= p_media->i_input && p_media->b_loop )
2523 p_instance->i_index = 0;
2525 if( p_instance->i_index < p_media->i_input )
2529 input_StopThread( p_instance->p_input );
2530 input_DestroyThreadExtended( p_instance->p_input, NULL );
2531 p_instance->p_input = NULL;
2533 /* FIXME, find a way to select the right instance */
2534 sprintf( buffer, "%d", p_instance->i_index+1 );
2536 vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2541 if( vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2542 "stop", 0 ) == VLC_SUCCESS )
2544 j--; /* the aray is one element smaller now. */
2551 i_time = vlm_Date();
2553 for( i = 0; i < vlm->i_schedule; i++ )
2555 mtime_t i_real_date = vlm->schedule[i]->i_date;
2557 if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2559 if( vlm->schedule[i]->i_date == 0 ) // now !
2561 vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2562 i_real_date = i_time;
2564 else if( vlm->schedule[i]->i_period != 0 )
2567 while( vlm->schedule[i]->i_date + j *
2568 vlm->schedule[i]->i_period <= i_lastcheck &&
2569 ( vlm->schedule[i]->i_repeat > j ||
2570 vlm->schedule[i]->i_repeat == -1 ) )
2575 i_real_date = vlm->schedule[i]->i_date + j *
2576 vlm->schedule[i]->i_period;
2579 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2581 for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2583 TAB_APPEND( i_scheduled_commands,
2584 ppsz_scheduled_commands,
2585 strdup(vlm->schedule[i]->command[j] ) );
2590 while( i_scheduled_commands )
2592 vlm_message_t *message = NULL;
2593 char *psz_command = ppsz_scheduled_commands[0];
2594 ExecuteCommand( vlm, psz_command,&message );
2596 /* for now, drop the message */
2597 vlm_MessageDelete( message );
2598 TAB_REMOVE( i_scheduled_commands,
2599 ppsz_scheduled_commands,
2601 free( psz_command );
2604 i_lastcheck = i_time;
2606 vlc_mutex_unlock( &vlm->lock );
2614 #else /* ENABLE_VLM */
2616 /* We just define an empty wrapper */
2617 vlm_t *__vlm_New( vlc_object_t *a )
2619 msg_Err( a, "VideoLAN manager support is disabled" );
2622 void vlm_Delete( vlm_t *a )
2626 int vlm_ExecuteCommand( vlm_t *a, char *b, vlm_message_t **c )
2630 void vlm_MessageDelete( vlm_message_t *a )
2634 vlm_media_t *vlm_MediaNew( vlm_t *a, char *b, int c )
2638 vlm_media_t *vlm_MediaSearch (vlm_t *a, const char *b )
2642 void vlm_MediaDelete( vlm_t *a, vlm_media_t *b, char *c )
2646 int vlm_MediaSetup( vlm_t *a, vlm_media_t *b, char *c, char *d )
2650 int vlm_MediaControl( vlm_t *a, vlm_media_t *b, char *c, char *d, char *e )
2655 #endif /* ENABLE_VLM */