1 /*****************************************************************************
2 * vlmshell.c: VLM interface plugin
3 *****************************************************************************
4 * Copyright (C) 2000-2005 VLC authors and VideoLAN
5 * $Id: 51e6a123254a9224c59f7ef3b139860ab5da862a $
6 *
7 * Authors: Simon Latapie <garf@videolan.org>
8 * Laurent Aimar <fenrir@videolan.org>
9 * Gildas Bazin <gbazin@videolan.org>
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
15 *
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 Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
25
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34
35 #include <stdio.h>
36 #include <ctype.h> /* tolower() */
37 #include <assert.h>
38
39 #include <vlc_vlm.h>
40
41 #ifdef ENABLE_VLM
42
43 #include <time.h> /* ctime() */
44 #include <limits.h>
45 #include <fcntl.h>
46 #include <sys/stat.h>
47
48 #include <vlc_input.h>
49 #include "input_internal.h"
50 #include <vlc_stream.h>
51 #include "vlm_internal.h"
52 #include <vlc_charset.h>
53 #include <vlc_fs.h>
54 #include <vlc_sout.h>
55 #include <vlc_memstream.h>
56 #include "../stream_output/stream_output.h"
57 #include "../libvlc.h"
58
59 /*****************************************************************************
60 * Local prototypes.
61 *****************************************************************************/
62
63 /* */
64 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_sys_t *, vlm_schedule_sys_t *, const char * );
65
66 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *, const char * );
67
68 static char *Save( vlm_t * );
69 static int Load( vlm_t *, char * );
70
71 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name );
72 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
73 const char *psz_value );
74
75 /* */
76 static vlm_media_sys_t *vlm_MediaSearch( vlm_t *, const char *);
77
78 static const char quotes[] = "\"'";
79 /**
80 * FindCommandEnd: look for the end of a possibly quoted string
81 * @return NULL on mal-formatted string,
82 * pointer past the last character otherwise.
83 */
FindCommandEnd(const char * psz_sent)84 static const char *FindCommandEnd( const char *psz_sent )
85 {
86 unsigned char c, quote = 0;
87
88 while( (c = *psz_sent) != '\0' )
89 {
90 if( !quote )
91 {
92 if( strchr(quotes,c) ) // opening quote
93 quote = c;
94 else if( isspace(c) ) // non-escaped space
95 return psz_sent;
96 else if( c == '\\' )
97 {
98 psz_sent++; // skip escaped character
99 if( *psz_sent == '\0' )
100 return psz_sent;
101 }
102 }
103 else
104 {
105 if( c == quote ) // non-escaped matching quote
106 quote = 0;
107 else if( (quote == '"') && (c == '\\') )
108 {
109 psz_sent++; // skip escaped character
110 if (*psz_sent == '\0')
111 return NULL; // error, closing quote missing
112 }
113 }
114 psz_sent++;
115 }
116
117 // error (NULL) if we could not find a matching quote
118 return quote ? NULL : psz_sent;
119 }
120
121
122 /**
123 * Unescape a nul-terminated string.
124 * Note that in and out can be identical.
125 *
126 * @param out output buffer (at least <strlen (in) + 1> characters long)
127 * @param in nul-terminated string to be unescaped
128 *
129 * @return 0 on success, -1 on error.
130 */
Unescape(char * out,const char * in)131 static int Unescape( char *out, const char *in )
132 {
133 unsigned char c, quote = 0;
134 bool param = false;
135
136 while( (c = *in++) != '\0' )
137 {
138 // Don't escape the end of the string if we find a '#'
139 // that's the beginning of a vlc command
140 // TODO: find a better solution
141 if( ( c == '#' && !quote ) || param )
142 {
143 param = true;
144 *out++ = c;
145 continue;
146 }
147
148 if( !quote )
149 {
150 if (strchr(quotes,c)) // opening quote
151 {
152 quote = c;
153 continue;
154 }
155 else if( c == '\\' )
156 {
157 switch (c = *in++)
158 {
159 case '"':
160 case '\'':
161 case '\\':
162 *out++ = c;
163 continue;
164
165 case '\0':
166 *out = '\0';
167 return 0;
168 }
169 if( isspace(c) )
170 {
171 *out++ = c;
172 continue;
173 }
174 /* None of the special cases - copy the backslash */
175 *out++ = '\\';
176 }
177 }
178 else
179 {
180 if( c == quote ) // non-escaped matching quote
181 {
182 quote = 0;
183 continue;
184 }
185 if( (quote == '"') && (c == '\\') )
186 {
187 switch( c = *in++ )
188 {
189 case '"':
190 case '\\':
191 *out++ = c;
192 continue;
193
194 case '\0': // should never happen
195 *out = '\0';
196 return -1;
197 }
198 /* None of the special cases - copy the backslash */
199 *out++ = '\\';
200 }
201 }
202 *out++ = c;
203 }
204
205 *out = '\0';
206 return 0;
207 }
208
209
210 /*****************************************************************************
211 * ExecuteCommand: The main state machine
212 *****************************************************************************
213 * Execute a command which ends with '\0' (string)
214 *****************************************************************************/
ExecuteSyntaxError(const char * psz_cmd,vlm_message_t ** pp_status)215 static int ExecuteSyntaxError( const char *psz_cmd, vlm_message_t **pp_status )
216 {
217 *pp_status = vlm_MessageNew( psz_cmd, "Wrong command syntax" );
218 return VLC_EGENERIC;
219 }
220
ExecuteIsMedia(vlm_t * p_vlm,const char * psz_name)221 static bool ExecuteIsMedia( vlm_t *p_vlm, const char *psz_name )
222 {
223 int64_t id;
224
225 if( !psz_name || vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
226 return false;
227 return true;
228 }
ExecuteIsSchedule(vlm_t * p_vlm,const char * psz_name)229 static bool ExecuteIsSchedule( vlm_t *p_vlm, const char *psz_name )
230 {
231 if( !psz_name || !vlm_ScheduleSearch( p_vlm, psz_name ) )
232 return false;
233 return true;
234 }
235
ExecuteDel(vlm_t * p_vlm,const char * psz_name,vlm_message_t ** pp_status)236 static int ExecuteDel( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
237 {
238 vlm_media_sys_t *p_media;
239 vlm_schedule_sys_t *p_schedule;
240
241 p_media = vlm_MediaSearch( p_vlm, psz_name );
242 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
243
244 if( p_schedule != NULL )
245 {
246 vlm_ScheduleDelete( p_vlm, p_schedule );
247 }
248 else if( p_media != NULL )
249 {
250 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_media->cfg.id );
251 }
252 else if( !strcmp(psz_name, "media") )
253 {
254 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
255 }
256 else if( !strcmp(psz_name, "schedule") )
257 {
258 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
259 }
260 else if( !strcmp(psz_name, "all") )
261 {
262 vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
263 vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
264 }
265 else
266 {
267 *pp_status = vlm_MessageNew( "del", "%s: media unknown", psz_name );
268 return VLC_EGENERIC;
269 }
270
271 *pp_status = vlm_MessageSimpleNew( "del" );
272 return VLC_SUCCESS;
273 }
274
ExecuteShow(vlm_t * p_vlm,const char * psz_name,vlm_message_t ** pp_status)275 static int ExecuteShow( vlm_t *p_vlm, const char *psz_name, vlm_message_t **pp_status )
276 {
277 vlm_media_sys_t *p_media;
278 vlm_schedule_sys_t *p_schedule;
279
280 if( !psz_name )
281 {
282 *pp_status = vlm_Show( p_vlm, NULL, NULL, NULL );
283 return VLC_SUCCESS;
284 }
285
286 p_media = vlm_MediaSearch( p_vlm, psz_name );
287 p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
288
289 if( p_schedule != NULL )
290 *pp_status = vlm_Show( p_vlm, NULL, p_schedule, NULL );
291 else if( p_media != NULL )
292 *pp_status = vlm_Show( p_vlm, p_media, NULL, NULL );
293 else
294 *pp_status = vlm_Show( p_vlm, NULL, NULL, psz_name );
295
296 return VLC_SUCCESS;
297 }
298
ExecuteHelp(vlm_message_t ** pp_status)299 static int ExecuteHelp( vlm_message_t **pp_status )
300 {
301 vlm_message_t *message_child;
302
303 #define MessageAdd( a ) \
304 vlm_MessageAdd( *pp_status, vlm_MessageSimpleNew( a ) );
305 #define MessageAddChild( a ) \
306 vlm_MessageAdd( message_child, vlm_MessageSimpleNew( a ) );
307
308 *pp_status = vlm_MessageSimpleNew( "help" );
309
310 message_child = MessageAdd( "Commands Syntax:" );
311 MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
312 MessageAddChild( "setup (name) (properties)" );
313 MessageAddChild( "show [(name)|media|schedule]" );
314 MessageAddChild( "del (name)|all|media|schedule" );
315 MessageAddChild( "control (name) [instance_name] (command)" );
316 MessageAddChild( "save (config_file)" );
317 MessageAddChild( "export" );
318 MessageAddChild( "load (config_file)" );
319
320 message_child = MessageAdd( "Media Proprieties Syntax:" );
321 MessageAddChild( "input (input_name)" );
322 MessageAddChild( "inputdel (input_name)|all" );
323 MessageAddChild( "inputdeln input_number" );
324 MessageAddChild( "output (output_name)" );
325 MessageAddChild( "option (option_name)[=value]" );
326 MessageAddChild( "enabled|disabled" );
327 MessageAddChild( "loop|unloop (broadcast only)" );
328 MessageAddChild( "mux (mux_name)" );
329
330 message_child = MessageAdd( "Schedule Proprieties Syntax:" );
331 MessageAddChild( "enabled|disabled" );
332 MessageAddChild( "append (command_until_rest_of_the_line)" );
333 MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
334 "(seconds)|now" );
335 MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
336 "(days)-(hours):(minutes):(seconds)" );
337 MessageAddChild( "repeat (number_of_repetitions)" );
338
339 message_child = MessageAdd( "Control Commands Syntax:" );
340 MessageAddChild( "play [input_number]" );
341 MessageAddChild( "pause" );
342 MessageAddChild( "stop" );
343 MessageAddChild( "seek [+-](percentage) | [+-](seconds)s | [+-](milliseconds)ms" );
344
345 return VLC_SUCCESS;
346 }
347
ExecuteControl(vlm_t * p_vlm,const char * psz_name,const int i_arg,char ** ppsz_arg,vlm_message_t ** pp_status)348 static int ExecuteControl( vlm_t *p_vlm, const char *psz_name, const int i_arg, char ** ppsz_arg, vlm_message_t **pp_status )
349 {
350 vlm_media_sys_t *p_media;
351 const char *psz_control = NULL;
352 const char *psz_instance = NULL;
353 const char *psz_argument = NULL;
354 int i_index;
355 int i_result;
356
357 if( !ExecuteIsMedia( p_vlm, psz_name ) )
358 {
359 *pp_status = vlm_MessageNew( "control", "%s: media unknown", psz_name );
360 return VLC_EGENERIC;
361 }
362
363 assert( i_arg > 0 );
364
365 #define IS(txt) ( !strcmp( ppsz_arg[i_index], (txt) ) )
366 i_index = 0;
367 if( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") )
368 {
369 i_index = 1;
370 psz_instance = ppsz_arg[0];
371
372 if( i_index >= i_arg || ( !IS("play") && !IS("stop") && !IS("pause") && !IS("seek") ) )
373 return ExecuteSyntaxError( "control", pp_status );
374 }
375 #undef IS
376 psz_control = ppsz_arg[i_index];
377
378 if( i_index+1 < i_arg )
379 psz_argument = ppsz_arg[i_index+1];
380
381 p_media = vlm_MediaSearch( p_vlm, psz_name );
382 assert( p_media );
383
384 if( !strcmp( psz_control, "play" ) )
385 {
386 int i_input_index = 0;
387 int i;
388
389 if( ( psz_argument && sscanf(psz_argument, "%d", &i) == 1 ) && i > 0 && i-1 < p_media->cfg.i_input )
390 {
391 i_input_index = i-1;
392 }
393 else if( psz_argument )
394 {
395 int j;
396 vlm_media_t *p_cfg = &p_media->cfg;
397 for ( j=0; j < p_cfg->i_input; j++)
398 {
399 if( !strcmp( p_cfg->ppsz_input[j], psz_argument ) )
400 {
401 i_input_index = j;
402 break;
403 }
404 }
405 }
406
407 if( p_media->cfg.b_vod )
408 i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_VOD_INSTANCE, p_media->cfg.id, psz_instance, i_input_index, NULL ); // we should get here now
409 else
410 i_result = vlm_ControlInternal( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, psz_instance, i_input_index );
411 }
412 else if( !strcmp( psz_control, "seek" ) )
413 {
414 if( psz_argument )
415 {
416 bool b_relative;
417 if( psz_argument[0] == '+' || psz_argument[0] == '-' )
418 b_relative = true;
419 else
420 b_relative = false;
421
422 if( strstr( psz_argument, "ms" ) || strstr( psz_argument, "s" ) )
423 {
424 /* Time (ms or s) */
425 int64_t i_new_time;
426
427 if( strstr( psz_argument, "ms" ) )
428 i_new_time = 1000 * (int64_t)atoi( psz_argument );
429 else
430 i_new_time = 1000000 * (int64_t)atoi( psz_argument );
431
432 if( b_relative )
433 {
434 int64_t i_time = 0;
435 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, &i_time );
436 i_new_time += i_time;
437 }
438 if( i_new_time < 0 )
439 i_new_time = 0;
440 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_TIME, p_media->cfg.id, psz_instance, i_new_time );
441 }
442 else
443 {
444 /* Percent */
445 double d_new_position = us_atof( psz_argument ) / 100.0;
446
447 if( b_relative )
448 {
449 double d_position = 0.0;
450
451 vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, &d_position );
452 d_new_position += d_position;
453 }
454 if( d_new_position < 0.0 )
455 d_new_position = 0.0;
456 else if( d_new_position > 1.0 )
457 d_new_position = 1.0;
458 i_result = vlm_ControlInternal( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, p_media->cfg.id, psz_instance, d_new_position );
459 }
460 }
461 else
462 {
463 i_result = VLC_EGENERIC;
464 }
465 }
466 else if( !strcmp( psz_control, "stop" ) )
467 {
468 i_result = vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
469 }
470 else if( !strcmp( psz_control, "pause" ) )
471 {
472 i_result = vlm_ControlInternal( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, p_media->cfg.id, psz_instance );
473 }
474 else
475 {
476 i_result = VLC_EGENERIC;
477 }
478
479 if( i_result )
480 {
481 *pp_status = vlm_MessageNew( "control", "unknown error" );
482 return VLC_SUCCESS;
483 }
484 *pp_status = vlm_MessageSimpleNew( "control" );
485 return VLC_SUCCESS;
486 }
487
ExecuteExport(vlm_t * p_vlm,vlm_message_t ** pp_status)488 static int ExecuteExport( vlm_t *p_vlm, vlm_message_t **pp_status )
489 {
490 char *psz_export = Save( p_vlm );
491
492 *pp_status = vlm_MessageNew( "export", "%s", psz_export );
493 free( psz_export );
494 return VLC_SUCCESS;
495 }
496
ExecuteSave(vlm_t * p_vlm,const char * psz_file,vlm_message_t ** pp_status)497 static int ExecuteSave( vlm_t *p_vlm, const char *psz_file, vlm_message_t **pp_status )
498 {
499 FILE *f = vlc_fopen( psz_file, "wt" );
500 char *psz_save = NULL;
501
502 if( !f )
503 goto error;
504
505 psz_save = Save( p_vlm );
506 if( psz_save == NULL )
507 goto error;
508 if( fputs( psz_save, f ) == EOF )
509 goto error;;
510 if( fclose( f ) )
511 {
512 f = NULL;
513 goto error;
514 }
515
516 free( psz_save );
517
518 *pp_status = vlm_MessageSimpleNew( "save" );
519 return VLC_SUCCESS;
520
521 error:
522 free( psz_save );
523 if( f )
524 fclose( f );
525 *pp_status = vlm_MessageNew( "save", "Unable to save to file");
526 return VLC_EGENERIC;
527 }
528
ExecuteLoad(vlm_t * p_vlm,const char * psz_path,vlm_message_t ** pp_status)529 static int ExecuteLoad( vlm_t *p_vlm, const char *psz_path, vlm_message_t **pp_status )
530 {
531 int fd = vlc_open( psz_path, O_RDONLY|O_NONBLOCK );
532 if( fd == -1 )
533 {
534 *pp_status = vlm_MessageNew( "load", "Unable to load from file" );
535 return VLC_EGENERIC;
536 }
537
538 struct stat st;
539 char *psz_buffer = NULL;
540
541 if( fstat( fd, &st ) || !S_ISREG( st.st_mode )
542 || st.st_size >= SSIZE_MAX
543 || ((psz_buffer = malloc( st.st_size + 1 )) == NULL)
544 || read( fd, psz_buffer, st.st_size ) < (ssize_t)st.st_size )
545 {
546 free( psz_buffer );
547 vlc_close( fd );
548
549 *pp_status = vlm_MessageNew( "load", "Read file error" );
550 return VLC_EGENERIC;
551 }
552
553 vlc_close( fd );
554
555 psz_buffer[st.st_size] = '\0';
556
557 if( Load( p_vlm, psz_buffer ) )
558 {
559 free( psz_buffer );
560
561 *pp_status = vlm_MessageNew( "load", "Error while loading file" );
562 return VLC_EGENERIC;
563 }
564
565 free( psz_buffer );
566
567 *pp_status = vlm_MessageSimpleNew( "load" );
568 return VLC_SUCCESS;
569 }
570
ExecuteScheduleProperty(vlm_t * p_vlm,vlm_schedule_sys_t * p_schedule,bool b_new,const int i_property,char * ppsz_property[],vlm_message_t ** pp_status)571 static int ExecuteScheduleProperty( vlm_t *p_vlm, vlm_schedule_sys_t *p_schedule, bool b_new,
572 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
573 {
574 const char *psz_cmd = b_new ? "new" : "setup";
575 int i;
576
577 for( i = 0; i < i_property; i++ )
578 {
579 if( !strcmp( ppsz_property[i], "enabled" ) ||
580 !strcmp( ppsz_property[i], "disabled" ) )
581 {
582 if ( vlm_ScheduleSetup( p_schedule, ppsz_property[i], NULL ) )
583 goto error;
584 }
585 else if( !strcmp( ppsz_property[i], "append" ) )
586 {
587 char *psz_line, *psz_realloc;
588 int j, i_ret = VLC_SUCCESS;
589 /* Beware: everything behind append is considered as
590 * command line */
591
592 if( ++i >= i_property )
593 break;
594
595 psz_line = strdup( ppsz_property[i] );
596 if( unlikely(psz_line == NULL) )
597 goto error;
598
599 for( j = i+1; j < i_property; j++ )
600 {
601 psz_realloc = realloc( psz_line,
602 strlen(psz_line) + strlen(ppsz_property[j]) + 1 + 1 );
603 if( likely(psz_realloc) )
604 {
605 psz_line = psz_realloc;
606 strcat( psz_line, " " );
607 strcat( psz_line, ppsz_property[j] );
608 }
609 else
610 {
611 i_ret = VLC_ENOMEM;
612 break;
613 }
614 }
615
616 if( i_ret == VLC_SUCCESS )
617 i_ret = vlm_ScheduleSetup( p_schedule, "append", psz_line );
618 free( psz_line );
619
620 if( i_ret )
621 goto error;
622 break;
623 }
624 else
625 {
626 if( i + 1 >= i_property )
627 {
628 if( b_new )
629 vlm_ScheduleDelete( p_vlm, p_schedule );
630 return ExecuteSyntaxError( psz_cmd, pp_status );
631 }
632
633 if( vlm_ScheduleSetup( p_schedule, ppsz_property[i], ppsz_property[i+1] ) )
634 goto error;
635 i++;
636 }
637 }
638 *pp_status = vlm_MessageSimpleNew( psz_cmd );
639
640 vlc_mutex_lock( &p_vlm->lock_manage );
641 p_vlm->input_state_changed = true;
642 vlc_cond_signal( &p_vlm->wait_manage );
643 vlc_mutex_unlock( &p_vlm->lock_manage );
644
645 return VLC_SUCCESS;
646
647 error:
648 *pp_status = vlm_MessageNew( psz_cmd, "Error while setting the property '%s' to the schedule",
649 ppsz_property[i] );
650 return VLC_EGENERIC;
651 }
652
ExecuteMediaProperty(vlm_t * p_vlm,int64_t id,bool b_new,const int i_property,char * ppsz_property[],vlm_message_t ** pp_status)653 static int ExecuteMediaProperty( vlm_t *p_vlm, int64_t id, bool b_new,
654 const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
655 {
656 const char *psz_cmd = b_new ? "new" : "setup";
657 vlm_media_t *p_cfg = NULL;
658 int i_result;
659 int i;
660
661 #undef ERROR
662 #undef MISSING
663 #define ERROR( txt ) do { *pp_status = vlm_MessageNew( psz_cmd, txt); goto error; } while(0)
664 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA, id, &p_cfg ) )
665 ERROR( "unknown media" );
666
667 #define MISSING(cmd) do { if( !psz_value ) ERROR( "missing argument for " cmd ); } while(0)
668 for( i = 0; i < i_property; i++ )
669 {
670 const char *psz_option = ppsz_property[i];
671 const char *psz_value = i+1 < i_property ? ppsz_property[i+1] : NULL;
672
673 if( !strcmp( psz_option, "enabled" ) )
674 {
675 p_cfg->b_enabled = true;
676 }
677 else if( !strcmp( psz_option, "disabled" ) )
678 {
679 p_cfg->b_enabled = false;
680 }
681 else if( !strcmp( psz_option, "input" ) )
682 {
683 MISSING( "input" );
684 TAB_APPEND( p_cfg->i_input, p_cfg->ppsz_input, strdup(psz_value) );
685 i++;
686 }
687 else if( !strcmp( psz_option, "inputdel" ) && psz_value && !strcmp( psz_value, "all" ) )
688 {
689 while( p_cfg->i_input > 0 )
690 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[0] );
691 i++;
692 }
693 else if( !strcmp( psz_option, "inputdel" ) )
694 {
695 int j;
696
697 MISSING( "inputdel" );
698
699 for( j = 0; j < p_cfg->i_input; j++ )
700 {
701 if( !strcmp( p_cfg->ppsz_input[j], psz_value ) )
702 {
703 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[j] );
704 break;
705 }
706 }
707 i++;
708 }
709 else if( !strcmp( psz_option, "inputdeln" ) )
710 {
711 MISSING( "inputdeln" );
712
713 int idx = atoi( psz_value );
714 if( idx > 0 && idx <= p_cfg->i_input )
715 TAB_REMOVE( p_cfg->i_input, p_cfg->ppsz_input, p_cfg->ppsz_input[idx-1] );
716 i++;
717 }
718 else if( !strcmp( psz_option, "output" ) )
719 {
720 MISSING( "output" );
721
722 free( p_cfg->psz_output );
723 p_cfg->psz_output = *psz_value ? strdup( psz_value ) : NULL;
724 i++;
725 }
726 else if( !strcmp( psz_option, "option" ) )
727 {
728 MISSING( "option" );
729
730 TAB_APPEND( p_cfg->i_option, p_cfg->ppsz_option, strdup( psz_value ) );
731 i++;
732 }
733 else if( !strcmp( psz_option, "loop" ) )
734 {
735 if( p_cfg->b_vod )
736 ERROR( "invalid loop option for vod" );
737 p_cfg->broadcast.b_loop = true;
738 }
739 else if( !strcmp( psz_option, "unloop" ) )
740 {
741 if( p_cfg->b_vod )
742 ERROR( "invalid unloop option for vod" );
743 p_cfg->broadcast.b_loop = false;
744 }
745 else if( !strcmp( psz_option, "mux" ) )
746 {
747 MISSING( "mux" );
748 if( !p_cfg->b_vod )
749 ERROR( "invalid mux option for broadcast" );
750
751 free( p_cfg->vod.psz_mux );
752 p_cfg->vod.psz_mux = *psz_value ? strdup( psz_value ) : NULL;
753 i++;
754 }
755 else
756 {
757 fprintf( stderr, "PROP: name=%s unknown\n", psz_option );
758 ERROR( "Wrong command syntax" );
759 }
760 }
761 #undef MISSING
762 #undef ERROR
763
764 /* */
765 i_result = vlm_ControlInternal( p_vlm, VLM_CHANGE_MEDIA, p_cfg );
766 vlm_media_Delete( p_cfg );
767
768 *pp_status = vlm_MessageSimpleNew( psz_cmd );
769 return i_result;
770
771 error:
772 if( p_cfg )
773 {
774 if( b_new )
775 vlm_ControlInternal( p_vlm, VLM_DEL_MEDIA, p_cfg->id );
776 vlm_media_Delete( p_cfg );
777 }
778 return VLC_EGENERIC;
779 }
780
ExecuteNew(vlm_t * p_vlm,const char * psz_name,const char * psz_type,const int i_property,char * ppsz_property[],vlm_message_t ** pp_status)781 static int ExecuteNew( vlm_t *p_vlm, const char *psz_name, const char *psz_type, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
782 {
783 /* Check name */
784 if( !strcmp( psz_name, "all" ) || !strcmp( psz_name, "media" ) || !strcmp( psz_name, "schedule" ) )
785 {
786 *pp_status = vlm_MessageNew( "new", "\"all\", \"media\" and \"schedule\" are reserved names" );
787 return VLC_EGENERIC;
788 }
789 if( ExecuteIsMedia( p_vlm, psz_name ) || ExecuteIsSchedule( p_vlm, psz_name ) )
790 {
791 *pp_status = vlm_MessageNew( "new", "%s: Name already in use", psz_name );
792 return VLC_EGENERIC;
793 }
794 /* */
795 if( !strcmp( psz_type, "schedule" ) )
796 {
797 vlm_schedule_sys_t *p_schedule = vlm_ScheduleNew( p_vlm, psz_name );
798 if( !p_schedule )
799 {
800 *pp_status = vlm_MessageNew( "new", "could not create schedule" );
801 return VLC_EGENERIC;
802 }
803 return ExecuteScheduleProperty( p_vlm, p_schedule, true, i_property, ppsz_property, pp_status );
804 }
805 else if( !strcmp( psz_type, "vod" ) || !strcmp( psz_type, "broadcast" ) )
806 {
807 vlm_media_t cfg;
808 int64_t id;
809
810 vlm_media_Init( &cfg );
811 cfg.psz_name = strdup( psz_name );
812 cfg.b_vod = !strcmp( psz_type, "vod" );
813
814 if( vlm_ControlInternal( p_vlm, VLM_ADD_MEDIA, &cfg, &id ) )
815 {
816 vlm_media_Clean( &cfg );
817 *pp_status = vlm_MessageNew( "new", "could not create media" );
818 return VLC_EGENERIC;
819 }
820 vlm_media_Clean( &cfg );
821 return ExecuteMediaProperty( p_vlm, id, true, i_property, ppsz_property, pp_status );
822 }
823 else
824 {
825 *pp_status = vlm_MessageNew( "new", "%s: Choose between vod, broadcast or schedule", psz_type );
826 return VLC_EGENERIC;
827 }
828 }
829
ExecuteSetup(vlm_t * p_vlm,const char * psz_name,const int i_property,char * ppsz_property[],vlm_message_t ** pp_status)830 static int ExecuteSetup( vlm_t *p_vlm, const char *psz_name, const int i_property, char *ppsz_property[], vlm_message_t **pp_status )
831 {
832 if( ExecuteIsSchedule( p_vlm, psz_name ) )
833 {
834 vlm_schedule_sys_t *p_schedule = vlm_ScheduleSearch( p_vlm, psz_name );
835 return ExecuteScheduleProperty( p_vlm, p_schedule, false, i_property, ppsz_property, pp_status );
836 }
837 else if( ExecuteIsMedia( p_vlm, psz_name ) )
838 {
839 int64_t id;
840 if( vlm_ControlInternal( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) )
841 goto error;
842 return ExecuteMediaProperty( p_vlm, id, false, i_property, ppsz_property, pp_status );
843 }
844
845 error:
846 *pp_status = vlm_MessageNew( "setup", "%s unknown", psz_name );
847 return VLC_EGENERIC;
848 }
849
ExecuteCommand(vlm_t * p_vlm,const char * psz_command,vlm_message_t ** pp_message)850 int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
851 vlm_message_t **pp_message )
852 {
853 size_t i_command = 0;
854 size_t i_command_len = strlen( psz_command );
855 char *buf = malloc( i_command_len + 1 ), *psz_buf = buf;
856 size_t i_ppsz_command_len = (3 + (i_command_len + 1) / 2);
857 char **ppsz_command = vlc_alloc( i_ppsz_command_len, sizeof(char *) );
858 vlm_message_t *p_message = NULL;
859 int i_ret = 0;
860
861 if( !psz_buf || !ppsz_command )
862 {
863 p_message = vlm_MessageNew( "Memory error",
864 "allocation failed for command of length %zu",
865 i_command_len );
866 goto error;
867 }
868
869 /* First, parse the line and cut it */
870 while( *psz_command != '\0' )
871 {
872 const char *psz_temp;
873
874 if(isspace ((unsigned char)*psz_command))
875 {
876 psz_command++;
877 continue;
878 }
879
880 /* support for comments */
881 if( i_command == 0 && *psz_command == '#')
882 {
883 p_message = vlm_MessageSimpleNew( "" );
884 goto success;
885 }
886
887 psz_temp = FindCommandEnd( psz_command );
888
889 if( psz_temp == NULL )
890 {
891 p_message = vlm_MessageNew( "Incomplete command", "%s", psz_command );
892 goto error;
893 }
894
895 assert (i_command < i_ppsz_command_len);
896
897 ppsz_command[i_command] = psz_buf;
898 memcpy (psz_buf, psz_command, psz_temp - psz_command);
899 psz_buf[psz_temp - psz_command] = '\0';
900
901 Unescape (psz_buf, psz_buf);
902
903 i_command++;
904 psz_buf += psz_temp - psz_command + 1;
905 psz_command = psz_temp;
906
907 assert (buf + i_command_len + 1 >= psz_buf);
908 }
909
910 /*
911 * And then Interpret it
912 */
913
914 #define IF_EXECUTE( name, check, cmd ) if( !strcmp(ppsz_command[0], name ) ) { if( (check) ) goto syntax_error; if( (cmd) ) goto error; goto success; }
915 if( i_command == 0 )
916 {
917 p_message = vlm_MessageSimpleNew( "" );
918 goto success;
919 }
920 else IF_EXECUTE( "del", (i_command != 2), ExecuteDel(p_vlm, ppsz_command[1], &p_message) )
921 else IF_EXECUTE( "show", (i_command > 2), ExecuteShow(p_vlm, i_command > 1 ? ppsz_command[1] : NULL, &p_message) )
922 else IF_EXECUTE( "help", (i_command != 1), ExecuteHelp( &p_message ) )
923 else IF_EXECUTE( "control", (i_command < 3), ExecuteControl(p_vlm, ppsz_command[1], i_command - 2, &ppsz_command[2], &p_message) )
924 else IF_EXECUTE( "save", (i_command != 2), ExecuteSave(p_vlm, ppsz_command[1], &p_message) )
925 else IF_EXECUTE( "export", (i_command != 1), ExecuteExport(p_vlm, &p_message) )
926 else IF_EXECUTE( "load", (i_command != 2), ExecuteLoad(p_vlm, ppsz_command[1], &p_message) )
927 else IF_EXECUTE( "new", (i_command < 3), ExecuteNew(p_vlm, ppsz_command[1], ppsz_command[2], i_command-3, &ppsz_command[3], &p_message) )
928 else IF_EXECUTE( "setup", (i_command < 2), ExecuteSetup(p_vlm, ppsz_command[1], i_command-2, &ppsz_command[2], &p_message) )
929 else
930 {
931 p_message = vlm_MessageNew( ppsz_command[0], "Unknown VLM command" );
932 goto error;
933 }
934 #undef IF_EXECUTE
935
936 success:
937 *pp_message = p_message;
938 free( buf );
939 free( ppsz_command );
940 return VLC_SUCCESS;
941
942 syntax_error:
943 i_ret = ExecuteSyntaxError( ppsz_command[0], pp_message );
944 free( buf );
945 free( ppsz_command );
946 return i_ret;
947
948 error:
949 *pp_message = p_message;
950 free( buf );
951 free( ppsz_command );
952 return VLC_EGENERIC;
953 }
954
955 /*****************************************************************************
956 * Media handling
957 *****************************************************************************/
vlm_MediaSearch(vlm_t * vlm,const char * psz_name)958 vlm_media_sys_t *vlm_MediaSearch( vlm_t *vlm, const char *psz_name )
959 {
960 int i;
961
962 for( i = 0; i < vlm->i_media; i++ )
963 {
964 if( strcmp( psz_name, vlm->media[i]->cfg.psz_name ) == 0 )
965 return vlm->media[i];
966 }
967
968 return NULL;
969 }
970
971 /*****************************************************************************
972 * Schedule handling
973 *****************************************************************************/
vlm_ScheduleNew(vlm_t * vlm,const char * psz_name)974 static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
975 {
976 if( !psz_name )
977 return NULL;
978
979 vlm_schedule_sys_t *p_sched = malloc( sizeof( vlm_schedule_sys_t ) );
980 if( !p_sched )
981 return NULL;
982
983 p_sched->psz_name = strdup( psz_name );
984 p_sched->b_enabled = false;
985 p_sched->i_command = 0;
986 p_sched->command = NULL;
987 p_sched->date = 0;
988 p_sched->period = 0;
989 p_sched->i_repeat = -1;
990
991 TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
992
993 return p_sched;
994 }
995
996 /* for now, simple delete. After, del with options (last arg) */
vlm_ScheduleDelete(vlm_t * vlm,vlm_schedule_sys_t * sched)997 void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched )
998 {
999 int i;
1000 if( sched == NULL ) return;
1001
1002 TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1003
1004 if( vlm->i_schedule == 0 ) free( vlm->schedule );
1005 free( sched->psz_name );
1006
1007 for ( i = 0; i < sched->i_command; i++ )
1008 free( sched->command[i] );
1009 free( sched->command );
1010 free( sched );
1011 }
1012
vlm_ScheduleSearch(vlm_t * vlm,const char * psz_name)1013 static vlm_schedule_sys_t *vlm_ScheduleSearch( vlm_t *vlm, const char *psz_name )
1014 {
1015 int i;
1016
1017 for( i = 0; i < vlm->i_schedule; i++ )
1018 {
1019 if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1020 {
1021 return vlm->schedule[i];
1022 }
1023 }
1024
1025 return NULL;
1026 }
1027
1028 /* Ok, setup schedule command will be able to support only one (argument value) at a time */
vlm_ScheduleSetup(vlm_schedule_sys_t * schedule,const char * psz_cmd,const char * psz_value)1029 static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
1030 const char *psz_value )
1031 {
1032 if( !strcmp( psz_cmd, "enabled" ) )
1033 {
1034 schedule->b_enabled = true;
1035 }
1036 else if( !strcmp( psz_cmd, "disabled" ) )
1037 {
1038 schedule->b_enabled = false;
1039 }
1040 else if( !strcmp( psz_cmd, "date" ) )
1041 {
1042 struct tm time;
1043 const char *p;
1044
1045 time.tm_sec = 0; /* seconds */
1046 time.tm_min = 0; /* minutes */
1047 time.tm_hour = 0; /* hours */
1048 time.tm_mday = 0; /* day of the month */
1049 time.tm_mon = 0; /* month */
1050 time.tm_year = 0; /* year */
1051 time.tm_wday = 0; /* day of the week */
1052 time.tm_yday = 0; /* day in the year */
1053 time.tm_isdst = -1; /* daylight saving time */
1054
1055 /* date should be year/month/day-hour:minutes:seconds */
1056 p = strchr( psz_value, '-' );
1057
1058 if( !strcmp( psz_value, "now" ) )
1059 {
1060 schedule->date = 0;
1061 }
1062 else if(p == NULL)
1063 {
1064 return 1;
1065 }
1066 else
1067 {
1068 unsigned i,j,k;
1069
1070 switch( sscanf( p + 1, "%u:%u:%u", &i, &j, &k ) )
1071 {
1072 case 1:
1073 time.tm_sec = i;
1074 break;
1075 case 2:
1076 time.tm_min = i;
1077 time.tm_sec = j;
1078 break;
1079 case 3:
1080 time.tm_hour = i;
1081 time.tm_min = j;
1082 time.tm_sec = k;
1083 break;
1084 default:
1085 return 1;
1086 }
1087
1088 switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1089 {
1090 case 1:
1091 time.tm_mday = i;
1092 break;
1093 case 2:
1094 time.tm_mon = i - 1;
1095 time.tm_mday = j;
1096 break;
1097 case 3:
1098 time.tm_year = i - 1900;
1099 time.tm_mon = j - 1;
1100 time.tm_mday = k;
1101 break;
1102 default:
1103 return 1;
1104 }
1105
1106 schedule->date = mktime(&time);
1107 }
1108 }
1109 else if( !strcmp( psz_cmd, "period" ) )
1110 {
1111 struct tm time;
1112 const char *p;
1113 const char *psz_time = NULL, *psz_date = NULL;
1114 unsigned i,j,k;
1115
1116 /* First, if date or period are modified, repeat should be equal to -1 */
1117 schedule->i_repeat = -1;
1118
1119 time.tm_sec = 0; /* seconds */
1120 time.tm_min = 0; /* minutes */
1121 time.tm_hour = 0; /* hours */
1122 time.tm_mday = 0; /* day of the month */
1123 time.tm_mon = 0; /* month */
1124 time.tm_year = 0; /* year */
1125 time.tm_wday = 0; /* day of the week */
1126 time.tm_yday = 0; /* day in the year */
1127 time.tm_isdst = -1; /* daylight saving time */
1128
1129 /* date should be year/month/day-hour:minutes:seconds */
1130 p = strchr( psz_value, '-' );
1131 if( p )
1132 {
1133 psz_date = psz_value;
1134 psz_time = p + 1;
1135 }
1136 else
1137 {
1138 psz_time = psz_value;
1139 }
1140
1141 switch( sscanf( psz_time, "%u:%u:%u", &i, &j, &k ) )
1142 {
1143 case 1:
1144 time.tm_sec = i;
1145 break;
1146 case 2:
1147 time.tm_min = i;
1148 time.tm_sec = j;
1149 break;
1150 case 3:
1151 time.tm_hour = i;
1152 time.tm_min = j;
1153 time.tm_sec = k;
1154 break;
1155 default:
1156 return 1;
1157 }
1158 if( psz_date )
1159 {
1160 switch( sscanf( psz_date, "%u/%u/%u", &i, &j, &k ) )
1161 {
1162 case 1:
1163 time.tm_mday = i;
1164 break;
1165 case 2:
1166 time.tm_mon = i;
1167 time.tm_mday = j;
1168 break;
1169 case 3:
1170 time.tm_year = i;
1171 time.tm_mon = j;
1172 time.tm_mday = k;
1173 break;
1174 default:
1175 return 1;
1176 }
1177 }
1178
1179 /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1180 schedule->period = ((((time.tm_year * 12 + time.tm_mon) * 30
1181 + time.tm_mday) * 24 + time.tm_hour) * 60 + time.tm_min) * 60
1182 + time.tm_sec;
1183 }
1184 else if( !strcmp( psz_cmd, "repeat" ) )
1185 {
1186 int i;
1187
1188 if( sscanf( psz_value, "%d", &i ) == 1 )
1189 {
1190 schedule->i_repeat = i;
1191 }
1192 else
1193 {
1194 return 1;
1195 }
1196 }
1197 else if( !strcmp( psz_cmd, "append" ) )
1198 {
1199 char *command = strdup( psz_value );
1200
1201 TAB_APPEND( schedule->i_command, schedule->command, command );
1202 }
1203 else
1204 {
1205 return 1;
1206 }
1207
1208 return 0;
1209 }
1210
1211 /*****************************************************************************
1212 * Message handling functions
1213 *****************************************************************************/
vlm_MessageSimpleNew(const char * psz_name)1214 vlm_message_t *vlm_MessageSimpleNew( const char *psz_name )
1215 {
1216 if( !psz_name ) return NULL;
1217
1218 vlm_message_t *p_message = malloc( sizeof(*p_message) );
1219 if( !p_message )
1220 return NULL;
1221
1222 p_message->psz_name = strdup( psz_name );
1223 if( !p_message->psz_name )
1224 {
1225 free( p_message );
1226 return NULL;
1227 }
1228 p_message->psz_value = NULL;
1229 p_message->i_child = 0;
1230 p_message->child = NULL;
1231
1232 return p_message;
1233 }
1234
vlm_MessageNew(const char * psz_name,const char * psz_format,...)1235 vlm_message_t *vlm_MessageNew( const char *psz_name,
1236 const char *psz_format, ... )
1237 {
1238 vlm_message_t *p_message = vlm_MessageSimpleNew( psz_name );
1239 va_list args;
1240
1241 if( !p_message )
1242 return NULL;
1243
1244 assert( psz_format );
1245 va_start( args, psz_format );
1246 if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
1247 p_message->psz_value = NULL;
1248 va_end( args );
1249
1250 if( !p_message->psz_value )
1251 {
1252 vlm_MessageDelete( p_message );
1253 return NULL;
1254 }
1255 return p_message;
1256 }
1257
vlm_MessageDelete(vlm_message_t * p_message)1258 void vlm_MessageDelete( vlm_message_t *p_message )
1259 {
1260 free( p_message->psz_name );
1261 free( p_message->psz_value );
1262 while( p_message->i_child-- )
1263 vlm_MessageDelete( p_message->child[p_message->i_child] );
1264 free( p_message->child );
1265 free( p_message );
1266 }
1267
1268 /* Add a child */
vlm_MessageAdd(vlm_message_t * p_message,vlm_message_t * p_child)1269 vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1270 vlm_message_t *p_child )
1271 {
1272 if( p_message == NULL ) return NULL;
1273
1274 if( p_child )
1275 {
1276 TAB_APPEND( p_message->i_child, p_message->child, p_child );
1277 }
1278
1279 return p_child;
1280 }
1281
1282 /*****************************************************************************
1283 * Misc utility functions
1284 *****************************************************************************/
vlm_ShowMedia(vlm_media_sys_t * p_media)1285 static vlm_message_t *vlm_ShowMedia( vlm_media_sys_t *p_media )
1286 {
1287 vlm_media_t *p_cfg = &p_media->cfg;
1288 vlm_message_t *p_msg;
1289 vlm_message_t *p_msg_sub;
1290 int i;
1291
1292 p_msg = vlm_MessageSimpleNew( p_cfg->psz_name );
1293 vlm_MessageAdd( p_msg,
1294 vlm_MessageNew( "type", p_cfg->b_vod ? "vod" : "broadcast" ) );
1295 vlm_MessageAdd( p_msg,
1296 vlm_MessageNew( "enabled", p_cfg->b_enabled ? "yes" : "no" ) );
1297
1298 if( p_cfg->b_vod )
1299 vlm_MessageAdd( p_msg,
1300 vlm_MessageNew( "mux", "%s", p_cfg->vod.psz_mux ) );
1301 else
1302 vlm_MessageAdd( p_msg,
1303 vlm_MessageNew( "loop", p_cfg->broadcast.b_loop ? "yes" : "no" ) );
1304
1305 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageSimpleNew( "inputs" ) );
1306 for( i = 0; i < p_cfg->i_input; i++ )
1307 {
1308 char *psz_tmp;
1309 if( asprintf( &psz_tmp, "%d", i+1 ) != -1 )
1310 {
1311 vlm_MessageAdd( p_msg_sub,
1312 vlm_MessageNew( psz_tmp, "%s", p_cfg->ppsz_input[i] ) );
1313 free( psz_tmp );
1314 }
1315 }
1316
1317 vlm_MessageAdd( p_msg,
1318 vlm_MessageNew( "output", "%s", p_cfg->psz_output ? p_cfg->psz_output : "" ) );
1319
1320 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageSimpleNew( "options" ) );
1321 for( i = 0; i < p_cfg->i_option; i++ )
1322 vlm_MessageAdd( p_msg_sub, vlm_MessageSimpleNew( p_cfg->ppsz_option[i] ) );
1323
1324 p_msg_sub = vlm_MessageAdd( p_msg, vlm_MessageSimpleNew( "instances" ) );
1325 for( i = 0; i < p_media->i_instance; i++ )
1326 {
1327 vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1328 vlc_value_t val;
1329 vlm_message_t *p_msg_instance;
1330
1331 val.i_int = END_S;
1332 if( p_instance->p_input )
1333 var_Get( p_instance->p_input, "state", &val );
1334
1335 p_msg_instance = vlm_MessageAdd( p_msg_sub, vlm_MessageSimpleNew( "instance" ) );
1336
1337 vlm_MessageAdd( p_msg_instance,
1338 vlm_MessageNew( "name" , "%s", p_instance->psz_name ? p_instance->psz_name : "default" ) );
1339 vlm_MessageAdd( p_msg_instance,
1340 vlm_MessageNew( "state",
1341 val.i_int == PLAYING_S ? "playing" :
1342 val.i_int == PAUSE_S ? "paused" :
1343 "stopped" ) );
1344
1345 /* FIXME should not do that this way */
1346 if( p_instance->p_input )
1347 {
1348 #define APPEND_INPUT_INFO( key, format, type ) \
1349 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( key, format, \
1350 var_Get ## type( p_instance->p_input, key ) ) )
1351 APPEND_INPUT_INFO( "position", "%f", Float );
1352 APPEND_INPUT_INFO( "time", "%"PRId64, Integer );
1353 APPEND_INPUT_INFO( "length", "%"PRId64, Integer );
1354 APPEND_INPUT_INFO( "rate", "%f", Float );
1355 APPEND_INPUT_INFO( "title", "%"PRId64, Integer );
1356 APPEND_INPUT_INFO( "chapter", "%"PRId64, Integer );
1357 APPEND_INPUT_INFO( "can-seek", "%d", Bool );
1358 }
1359 #undef APPEND_INPUT_INFO
1360 vlm_MessageAdd( p_msg_instance, vlm_MessageNew( "playlistindex",
1361 "%d", p_instance->i_index + 1 ) );
1362 }
1363 return p_msg;
1364 }
1365
vlm_Show(vlm_t * vlm,vlm_media_sys_t * media,vlm_schedule_sys_t * schedule,const char * psz_filter)1366 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
1367 vlm_schedule_sys_t *schedule,
1368 const char *psz_filter )
1369 {
1370 if( media != NULL )
1371 {
1372 vlm_message_t *p_msg = vlm_MessageSimpleNew( "show" );
1373 if( p_msg )
1374 vlm_MessageAdd( p_msg, vlm_ShowMedia( media ) );
1375 return p_msg;
1376 }
1377
1378 else if( schedule != NULL )
1379 {
1380 int i;
1381 vlm_message_t *msg;
1382 vlm_message_t *msg_schedule;
1383 vlm_message_t *msg_child;
1384 char buffer[100];
1385
1386 msg = vlm_MessageSimpleNew( "show" );
1387 msg_schedule =
1388 vlm_MessageAdd( msg, vlm_MessageSimpleNew( schedule->psz_name ) );
1389
1390 vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1391
1392 vlm_MessageAdd( msg_schedule,
1393 vlm_MessageNew( "enabled", schedule->b_enabled ?
1394 "yes" : "no" ) );
1395
1396 if( schedule->date != 0 )
1397 {
1398 struct tm date;
1399
1400 localtime_r( &schedule->date, &date);
1401 vlm_MessageAdd( msg_schedule,
1402 vlm_MessageNew( "date", "%d/%d/%d-%d:%d:%d",
1403 date.tm_year + 1900, date.tm_mon + 1,
1404 date.tm_mday, date.tm_hour, date.tm_min,
1405 date.tm_sec ) );
1406 }
1407 else
1408 vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1409
1410 if( schedule->period != 0 )
1411 {
1412 div_t d;
1413 struct tm date;
1414
1415 d = div(schedule->period, 60);
1416 date.tm_sec = d.rem;
1417 d = div(d.quot, 60);
1418 date.tm_min = d.rem;
1419 d = div(d.quot, 24);
1420 date.tm_hour = d.rem;
1421 /* okay, okay, months are not always 30 days long */
1422 d = div(d.quot, 30);
1423 date.tm_mday = d.rem;
1424 d = div(d.quot, 12);
1425 date.tm_mon = d.rem;
1426 date.tm_year = d.quot;
1427
1428 sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1429 date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1430
1431 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "%s", buffer) );
1432 }
1433 else
1434 vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1435
1436 sprintf( buffer, "%d", schedule->i_repeat );
1437 vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", "%s", buffer ) );
1438
1439 msg_child =
1440 vlm_MessageAdd( msg_schedule, vlm_MessageSimpleNew("commands" ) );
1441
1442 for( i = 0; i < schedule->i_command; i++ )
1443 {
1444 vlm_MessageAdd( msg_child,
1445 vlm_MessageSimpleNew( schedule->command[i] ) );
1446 }
1447
1448 return msg;
1449
1450 }
1451
1452 else if( psz_filter && !strcmp( psz_filter, "media" ) )
1453 {
1454 vlm_message_t *p_msg;
1455 vlm_message_t *p_msg_child;
1456 int i_vod = 0, i_broadcast = 0;
1457
1458 for( int i = 0; i < vlm->i_media; i++ )
1459 {
1460 if( vlm->media[i]->cfg.b_vod )
1461 i_vod++;
1462 else
1463 i_broadcast++;
1464 }
1465
1466 p_msg = vlm_MessageSimpleNew( "show" );
1467 p_msg_child = vlm_MessageAdd( p_msg, vlm_MessageNew( "media",
1468 "( %d broadcast - %d vod )", i_broadcast,
1469 i_vod ) );
1470
1471 for( int i = 0; i < vlm->i_media; i++ )
1472 vlm_MessageAdd( p_msg_child, vlm_ShowMedia( vlm->media[i] ) );
1473
1474 return p_msg;
1475 }
1476
1477 else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1478 {
1479 int i;
1480 vlm_message_t *msg;
1481 vlm_message_t *msg_child;
1482
1483 msg = vlm_MessageSimpleNew( "show" );
1484 msg_child = vlm_MessageAdd( msg, vlm_MessageSimpleNew( "schedule" ) );
1485
1486 for( i = 0; i < vlm->i_schedule; i++ )
1487 {
1488 vlm_schedule_sys_t *s = vlm->schedule[i];
1489 vlm_message_t *msg_schedule;
1490 time_t now, next_date;
1491
1492 msg_schedule = vlm_MessageAdd( msg_child,
1493 vlm_MessageSimpleNew( s->psz_name ) );
1494 vlm_MessageAdd( msg_schedule,
1495 vlm_MessageNew( "enabled", s->b_enabled ?
1496 "yes" : "no" ) );
1497
1498 /* calculate next date */
1499 time(&now);
1500 next_date = s->date;
1501
1502 if( s->period != 0 )
1503 {
1504 int j = 0;
1505 while( ((s->date + j * s->period) <= now) &&
1506 ( s->i_repeat > j || s->i_repeat < 0 ) )
1507 {
1508 j++;
1509 }
1510
1511 next_date = s->date + j * s->period;
1512 }
1513
1514 if( next_date > now )
1515 {
1516 struct tm tm;
1517 char psz_date[32];
1518
1519 strftime( psz_date, sizeof(psz_date), "%Y-%m-%d %H:%M:%S (%a)",
1520 localtime_r( &next_date, &tm ) );
1521 vlm_MessageAdd( msg_schedule,
1522 vlm_MessageNew( "next launch", "%s", psz_date ) );
1523 }
1524 }
1525
1526 return msg;
1527 }
1528
1529 else if( ( psz_filter == NULL ) && ( media == NULL ) && ( schedule == NULL ) )
1530 {
1531 vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1532 vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1533
1534 vlm_MessageAdd( show1, show2->child[0] );
1535
1536 /* We must destroy the parent node "show" of show2
1537 * and not the children */
1538 free( show2->child );
1539 free( show2->psz_name );
1540 free( show2 );
1541
1542 return show1;
1543 }
1544
1545 else
1546 {
1547 return vlm_MessageSimpleNew( "show" );
1548 }
1549 }
1550
1551 /*****************************************************************************
1552 * Config handling functions
1553 *****************************************************************************/
Load(vlm_t * vlm,char * file)1554 static int Load( vlm_t *vlm, char *file )
1555 {
1556 char *pf = file;
1557 int i_line = 1;
1558
1559 while( *pf != '\0' )
1560 {
1561 vlm_message_t *message = NULL;
1562 int i_end = 0;
1563
1564 while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1565 {
1566 i_end++;
1567 }
1568
1569 if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1570 {
1571 pf[i_end] = '\0';
1572 i_end++;
1573 if( pf[i_end] == '\n' ) i_end++;
1574 }
1575
1576 if( *pf && ExecuteCommand( vlm, pf, &message ) )
1577 {
1578 if( message )
1579 {
1580 if( message->psz_value )
1581 msg_Err( vlm, "Load error on line %d: %s: %s",
1582 i_line, message->psz_name, message->psz_value );
1583 vlm_MessageDelete( message );
1584 }
1585 return 1;
1586 }
1587 if( message ) vlm_MessageDelete( message );
1588
1589 pf += i_end;
1590 i_line++;
1591 }
1592
1593 return 0;
1594 }
1595
Save(vlm_t * vlm)1596 static char *Save( vlm_t *vlm )
1597 {
1598 const char *psz_header = "\n"
1599 "# VLC media player VLM command batch\n"
1600 "# http://www.videolan.org/vlc/\n\n" ;
1601
1602 struct vlc_memstream stream;
1603
1604 vlc_memstream_open( &stream );
1605 vlc_memstream_puts( &stream, psz_header );
1606
1607 for( int i = 0; i < vlm->i_media; i++ )
1608 {
1609 vlm_media_sys_t *media = vlm->media[i];
1610 vlm_media_t *p_cfg = &media->cfg;
1611
1612 vlc_memstream_printf( &stream, "new %s %s %sabled", p_cfg->psz_name,
1613 p_cfg->b_vod ? "vod" : "broadcast",
1614 p_cfg->b_enabled ? "en" : "dis" );
1615
1616 if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
1617 vlc_memstream_puts( &stream, " loop" );
1618 vlc_memstream_putc( &stream, '\n' );
1619
1620 for( int j = 0; j < p_cfg->i_input; j++ )
1621 vlc_memstream_printf( &stream, "setup %s input \"%s\"\n",
1622 p_cfg->psz_name, p_cfg->ppsz_input[j] );
1623
1624 if( p_cfg->psz_output )
1625 vlc_memstream_printf( &stream, "setup %s output %s\n",
1626 p_cfg->psz_name, p_cfg->psz_output );
1627
1628 for( int j = 0; j < p_cfg->i_option; j++ )
1629 vlc_memstream_printf( &stream, "setup %s option %s\n",
1630 p_cfg->psz_name, p_cfg->ppsz_option[j] );
1631
1632 if( p_cfg->b_vod && p_cfg->vod.psz_mux )
1633 vlc_memstream_printf( &stream, "setup %s mux %s\n",
1634 p_cfg->psz_name, p_cfg->vod.psz_mux );
1635 }
1636
1637 /* and now, the schedule scripts */
1638 for( int i = 0; i < vlm->i_schedule; i++ )
1639 {
1640 vlm_schedule_sys_t *schedule = vlm->schedule[i];
1641 struct tm tm;
1642
1643 localtime_r( &schedule->date, &tm );
1644 vlc_memstream_printf( &stream, "new %s schedule date "
1645 "%d/%d/%d-%d:%d:%d %sabled\n",
1646 schedule->psz_name,
1647 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
1648 tm.tm_hour, tm.tm_min, tm.tm_sec,
1649 schedule->b_enabled ? "en" : "dis" );
1650
1651 if( schedule->period != 0 )
1652 {
1653 div_t d;
1654
1655 d = div(schedule->period, 60);
1656 tm.tm_sec = d.rem;
1657 d = div(d.quot, 60);
1658 tm.tm_min = d.rem;
1659 d = div(d.quot, 24);
1660 tm.tm_hour = d.rem;
1661 d = div(d.quot, 30);
1662 tm.tm_mday = d.rem;
1663 /* okay, okay, months are not always 30 days long */
1664 d = div(d.quot, 12);
1665 tm.tm_mon = d.rem;
1666 tm.tm_year = d.quot;
1667
1668 vlc_memstream_printf( &stream, "setup %s "
1669 "period %d/%d/%d-%d:%d:%d\n",
1670 schedule->psz_name,
1671 tm.tm_year, tm.tm_mon, tm.tm_mday,
1672 tm.tm_hour, tm.tm_min, tm.tm_sec);
1673 }
1674
1675 if( schedule->i_repeat >= 0 )
1676 vlc_memstream_printf( &stream, "setup %s repeat %d",
1677 schedule->psz_name, schedule->i_repeat );
1678 vlc_memstream_putc( &stream, '\n' );
1679
1680 for( int j = 0; j < schedule->i_command; j++ )
1681 vlc_memstream_printf( &stream, "setup %s append %s\n",
1682 schedule->psz_name, schedule->command[j] );
1683 }
1684
1685 if( vlc_memstream_close( &stream ) )
1686 return NULL;
1687 return stream.ptr;
1688 }
1689
1690 #endif /* ENABLE_VLM */
1691