1 /*****************************************************************************
2 * intf_ctrl.c: interface commands access to control functions
3 * Library of functions common to all interfaces, allowing access to various
4 * structures and settings. Interfaces should only use those functions
5 * to read or write informations from other threads.
6 * A control function must be declared in the `local prototypes' section (it's
7 * type is fixed), and copied into the control_command array. Functions should
8 * be listed in alphabetical order, so when `help' is called they are also
9 * displayed in this order.
10 * A control function can use any function of the program, but should respect
11 * two points: first, it should not block, since if it does so, the whole
12 * interface thread will hang and in particular miscelannous interface events
13 * won't be handled. Secondly, it should send it's output messages exclusively
14 * with intf_IntfMsg() function, except particularly critical messages which
15 * can use over intf_*Msg() functions.
16 * Control functions should return 0 (INTF_NO_ERROR) on success, or one of the
17 * error codes defined in command.h. Custom error codes are allowed, but should
19 * More informations about parameters stand in `list of commands' section.
20 *****************************************************************************
21 * Copyright (C) 1999, 2000 VideoLAN
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33 * General Public License for more details.
35 * You should have received a copy of the GNU General Public
36 * License along with this program; if not, write to the
37 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
38 * Boston, MA 02111-1307, USA.
39 *****************************************************************************/
41 /*****************************************************************************
43 *****************************************************************************/
44 #include <sys/types.h> /* on BSD, uio.h needs types.h */
45 #include <sys/stat.h> /* on BSD, fstat() needs stat.h */
46 #include <sys/uio.h> /* "input.h" */
47 #include <stdio.h> /* fprintf() */
48 #include <stdlib.h> /* malloc(), free() */
49 #include <unistd.h> /* close(), read() */
50 #include <fcntl.h> /* open() */
61 #include "audio_output.h"
63 #include "interface.h"
69 static int Demo ( int i_argc, intf_arg_t *p_argv );
70 static int DisplayImage ( int i_argc, intf_arg_t *p_argv );
71 static int Exec ( int i_argc, intf_arg_t *p_argv );
72 static int Help ( int i_argc, intf_arg_t *p_argv );
73 static int PlayAudio ( int i_argc, intf_arg_t *p_argv );
74 static int PlayVideo ( int i_argc, intf_arg_t *p_argv );
75 static int Quit ( int i_argc, intf_arg_t *p_argv );
76 static int SelectPID ( int i_argc, intf_arg_t *p_argv );
77 static int SpawnInput ( int i_argc, intf_arg_t *p_argv );
79 static int Test ( int i_argc, intf_arg_t *p_argv );
81 static int Vlan ( int i_argc, intf_arg_t *p_argv );
82 static int Psi ( int i_argc, intf_arg_t *p_argv );
86 * This list is used by intf_ExecCommand function to find functions to
87 * execute and prepare its arguments. It is terminated by an element which name
88 * is a null pointer. intf_command_t is defined in command.h.
90 * Here is a description of a command description elements:
91 * name is the name of the function, as it should be typed on command line,
92 * function is a pointer to the control function,
93 * format is an argument descriptor (see below),
94 * summary is a text string displayed in regard of the command name when `help'
95 * is called without parameters, and whith usage on syntax error,
96 * usage is a short syntax indicator displayed with summary when the command
97 * causes a syntax error,
98 * help is a complete help about the command, displayed when `help' is called with
99 * the command name as parameter.
101 * Format string is a list of ' ' separated strings, which have following
106 * ? optionnal argument
107 * * argument can be repeated
108 * name= named argument
109 * Example: "channel=i? s*? i " means that any number of string arguments,
110 * followed by a single mandatory integer argument are waited. A named argument,
111 * which name is `channel' and must have an integer value can be optionnaly
112 * specified at beginning. The last space is mandatory if there is at least one
113 * element, since it acts as an item terminator.
114 * Named arguments MUST be at the beginning of the format string, and in
115 * alphabetic order, but their order on command line has no importance.
116 * The format string can't have more than INTF_MAX_ARGS elements.
118 const intf_command_t control_command[] =
120 { "demo", Demo, /* demo */
122 /* summary: */ "program demonstration",
124 /* help: */ "Start program capabilities demonstration." },
125 { "display", DisplayImage, /* display */
127 /* summary: */ "load and display an image",
128 /* usage: */ "display <file>",
129 /* help: */ "Load and display an image. Image format is automatically "\
130 "identified from file name extension." },
131 { "exec", Exec, /* exec */
133 /* summary: */ "execute a script file",
134 /* usage: */ "exec <file>",
135 /* help: */ "Load an execute a script." },
136 { "exit", Quit, /* exit (quit alias) */
138 /* summary: */ "quit program",
140 /* help: */ "see `quit'." },
141 { "help", Help, /* help */
143 /* summary: */ "list all functions or print help about a specific function",
144 /* usage: */ "help [command]",
145 /* help: */ "If called without argument, list all available " \
146 " functions.\nIf a command name is provided as argument, displays a short "\
147 "inline help about the command.\n" },
148 { "play-audio", PlayAudio, /* play-audio */
149 /* format: */ "channels=i? rate=i? s ",
150 /* summary: */ "play an audio file",
151 /* usage: */ "play-audio [channels=1/2] [rate=r] <file>",
152 /* help: */ "Load and play an audio file." },
153 { "play-video", PlayVideo, /* play-video */
155 /* summary: */ "play a video (.vlp) file",
156 /* usage: */ "play-video <file>",
157 /* help: */ "Load and play a video file." },
158 { "quit", Quit, /* quit */
160 /* summary: */ "quit program",
162 /* help: */ "Terminates the program execution... There is not much to"\
164 { "select-pid", SelectPID, /* select-pid */
165 /* format: */ "i i ",
166 /* summary: */ "spawn a decoder thread for a specified PID",
167 /* summary: */ "select-pid <input> <pid>",
168 /* help: */ "Spawn a decoder thread for <pid>. The stream will be" \
169 " received by <input>." },
170 { "spawn-input", SpawnInput, /* spawn-input */
171 /* format: */ "method=i? filename=s? hostname=s? ip=s? port=i? vlan=i?",
172 /* summary: */ "spawn an input thread",
173 /* summary: */ "spawn-input [method=<method>]\n" \
174 "[filename=<file>|hostname=<hostname>|ip=<ip>]\n" \
175 "[port=<port>] [vlan=<vlan>]",
176 /* help: */ "Spawn an input thread. Method is 10, 20, 21, 22, 32, "\
177 "hostname is the fully-qualified domain name, ip is a dotted-decimal address." },
179 { "test", Test, /* test */
181 /* summary: */ "crazy developper's test",
182 /* usage: */ "depends on the last coder :-)",
183 /* help: */ "`test' works only in DEBUG mode, and is provide for " \
184 "developpers as an easy way to test part of their code. If you don't know "\
185 "what it should do, just try !" },
188 /* format: */ "intf=s? s i? ",
189 /* summary: */ "vlan operations",
190 /* usage: */ "vlan synchro\n" \
191 "vlan [intf=<interface>] request\n" \
192 "vlan [intf=<interface>] join <vlan>\n" \
193 "vlan [intf=<interface>] leave",
194 /* help: */ "Perform various operations on vlans. 'synchro' resynchronize " \
195 "with the server. 'request' ask which is the current vlan (for the default "\
196 "interface or for a given one). 'join' and 'leave' try to change vlan." },
199 /* summary: */ "Dump PSI tables",
200 /* usage: */ "psi <input thread index>",
201 /* help: */ "Display the PSI tables on the console. Warning: this is debug" \
202 "command, it can leads to pb since locks are not taken yet" },
203 { 0, 0, 0, 0, 0 } /* array terminator */
206 /* following functions are local */
208 /*****************************************************************************
210 *****************************************************************************
211 * This function is provided to display a demo of program possibilities.
212 *****************************************************************************/
213 static int Demo( int i_argc, intf_arg_t *p_argv )
215 intf_IntfMsg( COPYRIGHT_MESSAGE );
217 return( INTF_NO_ERROR );
220 /*****************************************************************************
221 * Exec: execute a script
222 *****************************************************************************
223 * This function load and execute a script.
224 *****************************************************************************/
225 static int Exec( int i_argc, intf_arg_t *p_argv )
227 int i_err; /* error code */
229 i_err = intf_ExecScript( p_argv[1].psz_str );
230 return( i_err ? INTF_OTHER_ERROR : INTF_NO_ERROR );
233 /*****************************************************************************
234 * DisplayImage: load and display an image (ok ?)
235 *****************************************************************************
236 * Try to load an image identified by it's filename and displays it as a still
237 * image using interface video heap.
238 *****************************************************************************/
239 static int DisplayImage( int i_argc, intf_arg_t *p_argv )
242 return( INTF_NO_ERROR );
245 /*****************************************************************************
246 * Help: list all available commands (ok ?)
247 *****************************************************************************
248 * This function print a list of available commands
249 *****************************************************************************/
250 static int Help( int i_argc, intf_arg_t *p_argv )
252 int i_index; /* command index */
254 /* If called with an argument: look for the command and display it's help */
257 fprintf( stderr, "maxx debug: coin\n" );
258 for( i_index = 0; control_command[i_index].psz_name
259 && strcmp( control_command[i_index].psz_name, p_argv[1].psz_str );
264 fprintf( stderr, "maxx debug: meuh\n" );
265 /* Command has been found in list */
266 if( control_command[i_index].psz_name )
268 fprintf( stderr, "maxx debug: meow\n" );
269 intf_IntfMsg( control_command[i_index].psz_usage );
270 fprintf( stderr, "maxx debug: blah\n" );
271 intf_IntfMsg( control_command[i_index].psz_help );
272 fprintf( stderr, "maxx debug: blih\n" );
274 /* Command is unknown */
277 intf_IntfMsg("help: don't know command `%s'", p_argv[1].psz_str);
278 return( INTF_OTHER_ERROR );
281 /* If called without argument: print all commands help field */
284 for( i_index = 0; control_command[i_index].psz_name; i_index++ )
286 intf_IntfMsg( "%s: %s", control_command[i_index].psz_name,
287 control_command[i_index].psz_summary );
291 return( INTF_NO_ERROR );
294 /*****************************************************************************
295 * PlayAudio: play an audio file (ok ?)
296 *****************************************************************************
297 * Play a raw audio file from a file, at a given rate.
298 *****************************************************************************/
299 static int PlayAudio( int i_argc, intf_arg_t *p_argv )
301 char * psz_file = NULL; /* name of the audio raw file (s16) */
302 int i_fd; /* file descriptor of the audio file that is to be loaded */
303 aout_fifo_t fifo; /* fifo stores the informations about the file */
304 struct stat stat_buffer; /* needed to find out the size of psz_file */
305 int i_arg; /* argument index */
307 if ( !p_main->b_audio ) /* audio is disabled */
309 intf_IntfMsg("play-audio error: audio is disabled");
310 return( INTF_NO_ERROR );
313 /* Set default configuration */
314 fifo.i_channels = 1 + ( fifo.b_stereo = AOUT_DEFAULT_STEREO );
315 fifo.l_rate = AOUT_DEFAULT_RATE;
317 /* The channels and rate parameters are essential ! */
318 /* Parse parameters - see command list above */
319 for ( i_arg = 1; i_arg < i_argc; i_arg++ )
321 switch( p_argv[i_arg].i_index )
323 case 0: /* channels */
324 fifo.i_channels = p_argv[i_arg].i_num;
327 fifo.l_rate = p_argv[i_arg].i_num;
329 case 2: /* filename */
330 psz_file = p_argv[i_arg].psz_str;
335 /* Setting up the type of the fifo */
336 switch ( fifo.i_channels )
339 fifo.i_type = AOUT_INTF_MONO_FIFO;
343 fifo.i_type = AOUT_INTF_STEREO_FIFO;
347 intf_IntfMsg("play-audio error: channels must be 1 or 2");
348 return( INTF_OTHER_ERROR );
352 i_fd = open( psz_file, O_RDONLY );
353 if ( i_fd < 0 ) /* error */
355 intf_IntfMsg("play-audio error: can't open `%s'", psz_file);
356 return( INTF_OTHER_ERROR );
359 /* Get file size to calculate number of audio units */
360 fstat( i_fd, &stat_buffer );
361 fifo.l_units = ( long )( stat_buffer.st_size / (sizeof(s16) << fifo.b_stereo) );
363 /* Allocate memory, read file and close it */
364 if ( (fifo.buffer = malloc(sizeof(s16)*(fifo.l_units << fifo.b_stereo))) == NULL ) /* !! */
366 intf_IntfMsg("play-audio error: not enough memory to read `%s'", psz_file );
367 close( i_fd ); /* close file */
368 return( INTF_OTHER_ERROR );
370 if ( read(i_fd, fifo.buffer, sizeof(s16)*(fifo.l_units << fifo.b_stereo))
371 != sizeof(s16)*(fifo.l_units << fifo.b_stereo) )
373 intf_IntfMsg("play-audio error: can't read %s", psz_file);
376 return( INTF_OTHER_ERROR );
380 /* Now we can work out how many output units we can compute with the fifo */
381 fifo.l_units = (long)(((s64)fifo.l_units*(s64)p_main->p_aout->l_rate)/(s64)fifo.l_rate);
383 /* Create the fifo */
384 if ( aout_CreateFifo(p_main->p_aout, &fifo) == NULL )
386 intf_IntfMsg("play-audio error: can't create audio fifo");
388 return( INTF_OTHER_ERROR );
391 return( INTF_NO_ERROR );
394 /*****************************************************************************
395 * PlayVideo: play a video sequence from a file
396 *****************************************************************************
398 *****************************************************************************/
399 static int PlayVideo( int i_argc, intf_arg_t *p_argv )
402 return( INTF_NO_ERROR );
405 /*****************************************************************************
406 * Quit: quit program (ok ?)
407 *****************************************************************************
408 * This function set `die' flag of interface, asking the program to terminate.
409 *****************************************************************************/
410 static int Quit( int i_argc, intf_arg_t *p_argv )
412 p_main->p_intf->b_die = 1;
413 return( INTF_NO_ERROR );
417 /*****************************************************************************
419 *****************************************************************************
421 *****************************************************************************/
422 static int SelectPID( int i_argc, intf_arg_t *p_argv )
424 int i_input = -1, i_pid = -1;
427 /* Parse parameters - see command list above */
428 for ( i_arg = 1; i_arg < i_argc; i_arg++ )
430 switch( p_argv[i_arg].i_index )
433 /* FIXME: useless ?? */
434 i_input = p_argv[i_arg].i_num;
437 i_pid = p_argv[i_arg].i_num;
443 /* Find to which input this command is destinated */
444 intf_IntfMsg( "Adding PID %d to input %d\n", i_pid, i_input );
445 //XXX?? input_AddPgrmElem( p_main->p_intf->p_x11->p_input,
447 return( INTF_NO_ERROR );
451 /*****************************************************************************
452 * SpawnInput: spawn an input thread (ok ?)
453 *****************************************************************************
454 * Spawn an input thread
455 *****************************************************************************/
456 static int SpawnInput( int i_argc, intf_arg_t *p_argv )
459 int i_method = 0; /* method parameter */
460 char * psz_source = NULL; /* source parameter */
461 int i_port = 0; /* port parameter */
462 int i_vlan = 0; /* vlan parameter */
464 /* Parse parameters - see command list above */
465 for ( i_arg = 1; i_arg < i_argc; i_arg++ )
467 switch( p_argv[i_arg].i_index )
470 i_method = p_argv[i_arg].i_num;
472 case 1: /* filename, hostname, ip */
475 psz_source = p_argv[i_arg].psz_str;
478 i_port = p_argv[i_arg].i_num;
481 i_vlan = p_argv[i_arg].i_num;
486 /* Destroy current input, if any */
487 if( p_main->p_intf->p_input != NULL )
489 input_DestroyThread( p_main->p_intf->p_input, NULL );
492 p_main->p_intf->p_input = input_CreateThread( i_method, psz_source, i_port, i_vlan,
493 p_main->p_intf->p_vout, p_main->p_aout,
495 return( INTF_NO_ERROR );
498 /*****************************************************************************
499 * Test: test function
500 *****************************************************************************
501 * This function is provided to test new functions in the program. Fell free
503 * This function is only defined in DEBUG mode.
504 *****************************************************************************/
506 static int Test( int i_argc, intf_arg_t *p_argv )
510 /*XXX?? if( i_argc == 1 )
512 i_thread = intf_CreateVoutThread( &p_main->intf_thread, NULL, -1, -1);
513 intf_IntfMsg("return value: %d", i_thread );
517 i_thread = p_argv[1].i_num;
518 //XXX?? intf_DestroyVoutThread( &p_main->intf_thread, i_thread );
521 return( INTF_NO_ERROR );
525 /*****************************************************************************
526 * Vlan: vlan operations
527 *****************************************************************************
528 * This function performs various vlan operations.
529 *****************************************************************************/
530 static int Vlan( int i_argc, intf_arg_t *p_argv )
532 int i_command; /* command argument number */
534 /* Do not try anything if vlans are desactivated */
535 if( !p_main->b_vlans )
537 intf_IntfMsg("vlans are desactivated");
538 return( INTF_OTHER_ERROR );
541 /* Look for command in list of arguments - this argument is mandatory and
542 * imposed by the calling function */
543 for( i_command = 1; p_argv[i_command].i_index == 1; i_command++ )
548 /* Command is 'join' */
549 if( !strcmp(p_argv[i_command].psz_str, "join") )
553 /* Command is 'leave' */
554 else if( !strcmp(p_argv[i_command].psz_str, "leave") )
558 /* Command is unknown */
561 intf_IntfMsg("vlan error: unknown command %s", p_argv[i_command].psz_str );
562 return( INTF_USAGE_ERROR );
565 return( INTF_NO_ERROR );
569 /*****************************************************************************
571 *****************************************************************************
572 * This function is provided to display PSI tables.
573 *****************************************************************************/
574 static int Psi( int i_argc, intf_arg_t *p_argv )
576 int i_index = p_argv[1].i_num;
578 intf_IntfMsg("Reading PSI table for input %d\n", i_index);
579 //XXX?? input_PsiRead(p_main->p_intf->p_x11->p_input );
580 return( INTF_NO_ERROR );