demux: ts: only seek on pcr for current program
[vlc.git] / modules / control / oldrc.c
blob66402293a5a07ee4db9bce58515ad585d4d86ea8
1 /*****************************************************************************
2 * oldrc.c : remote control stdin/stdout module for vlc
3 *****************************************************************************
4 * Copyright (C) 2004-2009 the VideoLAN team
5 * $Id$
7 * Author: Peter Surda <shurdeek@panorama.sth.ac.at>
8 * Jean-Paul Saman <jpsaman #_at_# m2x _replaceWith#dot_ nl>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <errno.h> /* ENOMEM */
34 #include <signal.h>
35 #include <assert.h>
36 #include <math.h>
38 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
39 #include <vlc_common.h>
40 #include <vlc_plugin.h>
41 #include <vlc_interface.h>
42 #include <vlc_input.h>
43 #include <vlc_aout.h>
44 #include <vlc_vout.h>
45 #include <vlc_playlist.h>
46 #include <vlc_actions.h>
48 #include <sys/types.h>
49 #include <unistd.h>
52 #include <vlc_network.h>
53 #include <vlc_url.h>
54 #include <vlc_charset.h>
56 #if defined(PF_UNIX) && !defined(PF_LOCAL)
57 # define PF_LOCAL PF_UNIX
58 #endif
60 #if defined(AF_LOCAL) && ! defined(_WIN32)
61 # include <sys/un.h>
62 #endif
64 #define MAX_LINE_LENGTH 1024
65 #define STATUS_CHANGE "status change: "
67 /* input_state_e from <vlc_input.h> */
68 static const char *ppsz_input_state[] = {
69 [INIT_S] = N_("Initializing"),
70 [OPENING_S] = N_("Opening"),
71 [PLAYING_S] = N_("Play"),
72 [PAUSE_S] = N_("Pause"),
73 [END_S] = N_("End"),
74 [ERROR_S] = N_("Error"),
77 /*****************************************************************************
78 * Local prototypes
79 *****************************************************************************/
80 static int Activate ( vlc_object_t * );
81 static void Deactivate ( vlc_object_t * );
82 static void *Run ( void * );
84 static void Help ( intf_thread_t * );
85 static void RegisterCallbacks( intf_thread_t * );
87 static bool ReadCommand( intf_thread_t *, char *, int * );
89 static input_item_t *parse_MRL( const char * );
91 static int Input ( vlc_object_t *, char const *,
92 vlc_value_t, vlc_value_t, void * );
93 static int Playlist ( vlc_object_t *, char const *,
94 vlc_value_t, vlc_value_t, void * );
95 static int Quit ( vlc_object_t *, char const *,
96 vlc_value_t, vlc_value_t, void * );
97 static int Intf ( vlc_object_t *, char const *,
98 vlc_value_t, vlc_value_t, void * );
99 static int Volume ( vlc_object_t *, char const *,
100 vlc_value_t, vlc_value_t, void * );
101 static int VolumeMove ( vlc_object_t *, char const *,
102 vlc_value_t, vlc_value_t, void * );
103 static int VideoConfig ( vlc_object_t *, char const *,
104 vlc_value_t, vlc_value_t, void * );
105 static int AudioDevice ( vlc_object_t *, char const *,
106 vlc_value_t, vlc_value_t, void * );
107 static int AudioChannel ( vlc_object_t *, char const *,
108 vlc_value_t, vlc_value_t, void * );
109 static int Statistics ( vlc_object_t *, char const *,
110 vlc_value_t, vlc_value_t, void * );
112 static int updateStatistics( intf_thread_t *, input_item_t *);
114 /* Status Callbacks */
115 static int VolumeChanged( vlc_object_t *, char const *,
116 vlc_value_t, vlc_value_t, void * );
117 static int InputEvent( vlc_object_t *, char const *,
118 vlc_value_t, vlc_value_t, void * );
120 struct intf_sys_t
122 int *pi_socket_listen;
123 int i_socket;
124 char *psz_unix_path;
125 vlc_thread_t thread;
127 /* status changes */
128 vlc_mutex_t status_lock;
129 int i_last_state;
130 playlist_t *p_playlist;
131 input_thread_t *p_input;
132 bool b_input_buffering;
134 #ifdef _WIN32
135 HANDLE hConsoleIn;
136 bool b_quiet;
137 #endif
140 VLC_FORMAT(2, 3)
141 static void msg_rc( intf_thread_t *p_intf, const char *psz_fmt, ... )
143 va_list args;
144 char fmt_eol[strlen (psz_fmt) + 3];
146 snprintf (fmt_eol, sizeof (fmt_eol), "%s\r\n", psz_fmt);
147 va_start( args, psz_fmt );
149 if( p_intf->p_sys->i_socket == -1 )
150 utf8_vfprintf( stdout, fmt_eol, args );
151 else
152 net_vaPrintf( p_intf, p_intf->p_sys->i_socket, fmt_eol, args );
153 va_end( args );
155 #define msg_rc( ... ) msg_rc( p_intf, __VA_ARGS__ )
157 /*****************************************************************************
158 * Module descriptor
159 *****************************************************************************/
160 #define POS_TEXT N_("Show stream position")
161 #define POS_LONGTEXT N_("Show the current position in seconds within the " \
162 "stream from time to time." )
164 #define TTY_TEXT N_("Fake TTY")
165 #define TTY_LONGTEXT N_("Force the rc module to use stdin as if it was a TTY.")
167 #define UNIX_TEXT N_("UNIX socket command input")
168 #define UNIX_LONGTEXT N_("Accept commands over a Unix socket rather than " \
169 "stdin." )
171 #define HOST_TEXT N_("TCP command input")
172 #define HOST_LONGTEXT N_("Accept commands over a socket rather than stdin. " \
173 "You can set the address and port the interface will bind to." )
175 #ifdef _WIN32
176 #define QUIET_TEXT N_("Do not open a DOS command box interface")
177 #define QUIET_LONGTEXT N_( \
178 "By default the rc interface plugin will start a DOS command box. " \
179 "Enabling the quiet mode will not bring this command box but can also " \
180 "be pretty annoying when you want to stop VLC and no video window is " \
181 "open." )
182 #if !VLC_WINSTORE_APP
183 #include "intromsg.h"
184 #endif
185 #endif
187 vlc_module_begin ()
188 set_shortname( N_("RC"))
189 set_category( CAT_INTERFACE )
190 set_subcategory( SUBCAT_INTERFACE_MAIN )
191 set_description( N_("Remote control interface") )
192 add_bool( "rc-show-pos", false, POS_TEXT, POS_LONGTEXT, true )
194 #ifdef _WIN32
195 add_bool( "rc-quiet", false, QUIET_TEXT, QUIET_LONGTEXT, false )
196 #else
197 #if defined (HAVE_ISATTY)
198 add_bool( "rc-fake-tty", false, TTY_TEXT, TTY_LONGTEXT, true )
199 #endif
200 add_string( "rc-unix", NULL, UNIX_TEXT, UNIX_LONGTEXT, true )
201 #endif
202 add_string( "rc-host", NULL, HOST_TEXT, HOST_LONGTEXT, true )
204 set_capability( "interface", 20 )
206 set_callbacks( Activate, Deactivate )
207 #ifdef _WIN32
208 add_shortcut( "rc" )
209 #endif
210 vlc_module_end ()
212 /*****************************************************************************
213 * Activate: initialize and create stuff
214 *****************************************************************************/
215 static int Activate( vlc_object_t *p_this )
217 /* FIXME: This function is full of memory leaks and bugs in error paths. */
218 intf_thread_t *p_intf = (intf_thread_t*)p_this;
219 playlist_t *p_playlist = pl_Get( p_intf );
220 char *psz_host, *psz_unix_path = NULL;
221 int *pi_socket = NULL;
223 #ifndef _WIN32
224 #if defined(HAVE_ISATTY)
225 /* Check that stdin is a TTY */
226 if( !var_InheritBool( p_intf, "rc-fake-tty" ) && !isatty( 0 ) )
228 msg_Warn( p_intf, "fd 0 is not a TTY" );
229 return VLC_EGENERIC;
231 #endif
233 psz_unix_path = var_InheritString( p_intf, "rc-unix" );
234 if( psz_unix_path )
236 int i_socket;
238 #ifndef AF_LOCAL
239 msg_Warn( p_intf, "your OS doesn't support filesystem sockets" );
240 free( psz_unix_path );
241 return VLC_EGENERIC;
242 #else
243 struct sockaddr_un addr;
245 memset( &addr, 0, sizeof(struct sockaddr_un) );
247 msg_Dbg( p_intf, "trying UNIX socket" );
249 if( (i_socket = vlc_socket( PF_LOCAL, SOCK_STREAM, 0, false ) ) < 0 )
251 msg_Warn( p_intf, "can't open socket: %s", vlc_strerror_c(errno) );
252 free( psz_unix_path );
253 return VLC_EGENERIC;
256 addr.sun_family = AF_LOCAL;
257 strncpy( addr.sun_path, psz_unix_path, sizeof( addr.sun_path ) );
258 addr.sun_path[sizeof( addr.sun_path ) - 1] = '\0';
260 if (bind (i_socket, (struct sockaddr *)&addr, sizeof (addr))
261 && (errno == EADDRINUSE)
262 && connect (i_socket, (struct sockaddr *)&addr, sizeof (addr))
263 && (errno == ECONNREFUSED))
265 msg_Info (p_intf, "Removing dead UNIX socket: %s", psz_unix_path);
266 unlink (psz_unix_path);
268 if (bind (i_socket, (struct sockaddr *)&addr, sizeof (addr)))
270 msg_Err (p_intf, "cannot bind UNIX socket at %s: %s",
271 psz_unix_path, vlc_strerror_c(errno));
272 free (psz_unix_path);
273 net_Close (i_socket);
274 return VLC_EGENERIC;
278 if( listen( i_socket, 1 ) )
280 msg_Warn (p_intf, "can't listen on socket: %s",
281 vlc_strerror_c(errno));
282 free( psz_unix_path );
283 net_Close( i_socket );
284 return VLC_EGENERIC;
287 /* FIXME: we need a core function to merge listening sockets sets */
288 pi_socket = calloc( 2, sizeof( int ) );
289 if( pi_socket == NULL )
291 free( psz_unix_path );
292 net_Close( i_socket );
293 return VLC_ENOMEM;
295 pi_socket[0] = i_socket;
296 pi_socket[1] = -1;
297 #endif /* AF_LOCAL */
299 #endif /* !_WIN32 */
301 if( ( pi_socket == NULL ) &&
302 ( psz_host = var_InheritString( p_intf, "rc-host" ) ) != NULL )
304 vlc_url_t url;
306 vlc_UrlParse( &url, psz_host );
308 msg_Dbg( p_intf, "base: %s, port: %d", url.psz_host, url.i_port );
310 pi_socket = net_ListenTCP(p_this, url.psz_host, url.i_port);
311 if( pi_socket == NULL )
313 msg_Warn( p_intf, "can't listen to %s port %i",
314 url.psz_host, url.i_port );
315 vlc_UrlClean( &url );
316 free( psz_host );
317 return VLC_EGENERIC;
320 vlc_UrlClean( &url );
321 free( psz_host );
324 intf_sys_t *p_sys = malloc( sizeof( *p_sys ) );
325 if( unlikely(p_sys == NULL) )
327 net_ListenClose( pi_socket );
328 free( psz_unix_path );
329 return VLC_ENOMEM;
332 p_intf->p_sys = p_sys;
333 p_sys->pi_socket_listen = pi_socket;
334 p_sys->i_socket = -1;
335 p_sys->psz_unix_path = psz_unix_path;
336 vlc_mutex_init( &p_sys->status_lock );
337 p_sys->i_last_state = PLAYLIST_STOPPED;
338 p_sys->b_input_buffering = false;
339 p_sys->p_playlist = p_playlist;
340 p_sys->p_input = NULL;
342 /* Non-buffered stdout */
343 setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
345 #if VLC_WINSTORE_APP
346 p_sys->b_quiet = true;
347 #elif defined(_WIN32)
348 p_sys->b_quiet = var_InheritBool( p_intf, "rc-quiet" );
349 if( !p_sys->b_quiet )
350 intf_consoleIntroMsg( p_intf );
351 #endif
353 if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
354 abort();
356 msg_rc( "%s", _("Remote control interface initialized. Type `help' for help.") );
358 /* Listen to audio volume updates */
359 var_AddCallback( p_sys->p_playlist, "volume", VolumeChanged, p_intf );
360 return VLC_SUCCESS;
363 /*****************************************************************************
364 * Deactivate: uninitialize and cleanup
365 *****************************************************************************/
366 static void Deactivate( vlc_object_t *p_this )
368 intf_thread_t *p_intf = (intf_thread_t*)p_this;
369 intf_sys_t *p_sys = p_intf->p_sys;
371 vlc_cancel( p_sys->thread );
372 var_DelCallback( p_sys->p_playlist, "volume", VolumeChanged, p_intf );
373 vlc_join( p_sys->thread, NULL );
375 if( p_sys->p_input != NULL )
377 var_DelCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
378 vlc_object_release( p_sys->p_input );
381 net_ListenClose( p_sys->pi_socket_listen );
382 if( p_sys->i_socket != -1 )
383 net_Close( p_sys->i_socket );
384 if( p_sys->psz_unix_path != NULL )
386 #if defined(AF_LOCAL) && !defined(_WIN32)
387 unlink( p_sys->psz_unix_path );
388 #endif
389 free( p_sys->psz_unix_path );
391 vlc_mutex_destroy( &p_sys->status_lock );
392 free( p_sys );
395 /*****************************************************************************
396 * RegisterCallbacks: Register callbacks to dynamic variables
397 *****************************************************************************/
398 static void RegisterCallbacks( intf_thread_t *p_intf )
400 /* Register commands that will be cleaned up upon object destruction */
401 #define ADD( name, type, target ) \
402 var_Create( p_intf, name, VLC_VAR_ ## type | VLC_VAR_ISCOMMAND ); \
403 var_AddCallback( p_intf, name, target, NULL );
404 ADD( "quit", VOID, Quit )
405 ADD( "intf", STRING, Intf )
407 ADD( "add", STRING, Playlist )
408 ADD( "repeat", STRING, Playlist )
409 ADD( "loop", STRING, Playlist )
410 ADD( "random", STRING, Playlist )
411 ADD( "enqueue", STRING, Playlist )
412 ADD( "playlist", VOID, Playlist )
413 ADD( "sort", VOID, Playlist )
414 ADD( "play", VOID, Playlist )
415 ADD( "stop", VOID, Playlist )
416 ADD( "clear", VOID, Playlist )
417 ADD( "prev", VOID, Playlist )
418 ADD( "next", VOID, Playlist )
419 ADD( "goto", INTEGER, Playlist )
420 ADD( "status", INTEGER, Playlist )
422 /* DVD commands */
423 ADD( "pause", VOID, Input )
424 ADD( "seek", INTEGER, Input )
425 ADD( "title", STRING, Input )
426 ADD( "title_n", VOID, Input )
427 ADD( "title_p", VOID, Input )
428 ADD( "chapter", STRING, Input )
429 ADD( "chapter_n", VOID, Input )
430 ADD( "chapter_p", VOID, Input )
432 ADD( "fastforward", VOID, Input )
433 ADD( "rewind", VOID, Input )
434 ADD( "faster", VOID, Input )
435 ADD( "slower", VOID, Input )
436 ADD( "normal", VOID, Input )
437 ADD( "frame", VOID, Input )
439 ADD( "atrack", STRING, Input )
440 ADD( "vtrack", STRING, Input )
441 ADD( "strack", STRING, Input )
443 /* video commands */
444 ADD( "vratio", STRING, VideoConfig )
445 ADD( "vcrop", STRING, VideoConfig )
446 ADD( "vzoom", STRING, VideoConfig )
447 ADD( "snapshot", VOID, VideoConfig )
449 /* audio commands */
450 ADD( "volume", STRING, Volume )
451 ADD( "volup", STRING, VolumeMove )
452 ADD( "voldown", STRING, VolumeMove )
453 ADD( "adev", STRING, AudioDevice )
454 ADD( "achan", STRING, AudioChannel )
456 /* misc menu commands */
457 ADD( "stats", BOOL, Statistics )
459 #undef ADD
462 /*****************************************************************************
463 * Run: rc thread
464 *****************************************************************************
465 * This part of the interface is in a separate thread so that we can call
466 * exec() from within it without annoying the rest of the program.
467 *****************************************************************************/
468 static void *Run( void *data )
470 intf_thread_t *p_intf = data;
471 intf_sys_t *p_sys = p_intf->p_sys;
473 char p_buffer[ MAX_LINE_LENGTH + 1 ];
474 bool b_showpos = var_InheritBool( p_intf, "rc-show-pos" );
476 int i_size = 0;
477 int i_oldpos = 0;
478 int i_newpos;
479 int canc = vlc_savecancel( );
481 p_buffer[0] = 0;
483 #if defined(_WIN32) && !VLC_WINSTORE_APP
484 /* Get the file descriptor of the console input */
485 p_intf->p_sys->hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
486 if( p_intf->p_sys->hConsoleIn == INVALID_HANDLE_VALUE )
488 msg_Err( p_intf, "couldn't find user input handle" );
489 return NULL;
491 #endif
493 /* Register commands that will be cleaned up upon object destruction */
494 RegisterCallbacks( p_intf );
496 /* status callbacks */
498 for( ;; )
500 char *psz_cmd, *psz_arg;
501 bool b_complete;
503 vlc_restorecancel( canc );
505 if( p_sys->pi_socket_listen != NULL && p_sys->i_socket == -1 )
507 p_sys->i_socket =
508 net_Accept( p_intf, p_sys->pi_socket_listen );
509 if( p_sys->i_socket == -1 ) continue;
512 b_complete = ReadCommand( p_intf, p_buffer, &i_size );
513 canc = vlc_savecancel( );
515 /* Manage the input part */
516 if( p_sys->p_input == NULL )
518 p_sys->p_input = playlist_CurrentInput( p_sys->p_playlist );
519 /* New input has been registered */
520 if( p_sys->p_input )
522 char *psz_uri = input_item_GetURI( input_GetItem( p_sys->p_input ) );
523 msg_rc( STATUS_CHANGE "( new input: %s )", psz_uri );
524 free( psz_uri );
526 var_AddCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
530 int state;
531 if( p_sys->p_input != NULL
532 && ((state = var_GetInteger( p_sys->p_input, "state")) == ERROR_S
533 || state == END_S) )
535 var_DelCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
536 vlc_object_release( p_sys->p_input );
537 p_sys->p_input = NULL;
539 p_sys->i_last_state = PLAYLIST_STOPPED;
540 msg_rc( STATUS_CHANGE "( stop state: 0 )" );
543 if( p_sys->p_input != NULL )
545 playlist_t *p_playlist = p_sys->p_playlist;
547 PL_LOCK;
548 int status = playlist_Status( p_playlist );
549 PL_UNLOCK;
551 if( p_sys->i_last_state != status )
553 if( status == PLAYLIST_STOPPED )
555 p_sys->i_last_state = PLAYLIST_STOPPED;
556 msg_rc( STATUS_CHANGE "( stop state: 5 )" );
558 else if( status == PLAYLIST_RUNNING )
560 p_sys->i_last_state = PLAYLIST_RUNNING;
561 msg_rc( STATUS_CHANGE "( play state: 3 )" );
563 else if( status == PLAYLIST_PAUSED )
565 p_sys->i_last_state = PLAYLIST_PAUSED;
566 msg_rc( STATUS_CHANGE "( pause state: 4 )" );
571 if( p_sys->p_input && b_showpos )
573 i_newpos = 100 * var_GetFloat( p_sys->p_input, "position" );
574 if( i_oldpos != i_newpos )
576 i_oldpos = i_newpos;
577 msg_rc( "pos: %d%%", i_newpos );
581 /* Is there something to do? */
582 if( !b_complete ) continue;
584 /* Skip heading spaces */
585 psz_cmd = p_buffer;
586 while( *psz_cmd == ' ' )
588 psz_cmd++;
591 /* Split psz_cmd at the first space and make sure that
592 * psz_arg is valid */
593 psz_arg = strchr( psz_cmd, ' ' );
594 if( psz_arg )
596 *psz_arg++ = 0;
597 while( *psz_arg == ' ' )
599 psz_arg++;
602 else
604 psz_arg = (char*)"";
607 /* If the user typed a registered local command, try it */
608 if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
610 int i_ret = VLC_SUCCESS;
612 if ((var_Type( p_intf, psz_cmd) & VLC_VAR_CLASS) == VLC_VAR_VOID)
613 var_TriggerCallback( p_intf, psz_cmd );
614 else
615 i_ret = var_SetString( p_intf, psz_cmd, psz_arg );
616 msg_rc( "%s: returned %i (%s)",
617 psz_cmd, i_ret, vlc_error( i_ret ) );
619 /* Or maybe it's a global command */
620 else if( var_Type( p_intf->obj.libvlc, psz_cmd ) & VLC_VAR_ISCOMMAND )
622 int i_ret = VLC_SUCCESS;
624 /* FIXME: it's a global command, but we should pass the
625 * local object as an argument, not p_intf->obj.libvlc. */
626 if ((var_Type( p_intf->obj.libvlc, psz_cmd) & VLC_VAR_CLASS) == VLC_VAR_VOID)
627 var_TriggerCallback( p_intf, psz_cmd );
628 else
629 i_ret = var_SetString( p_intf->obj.libvlc, psz_cmd, psz_arg );
630 if( i_ret != 0 )
632 msg_rc( "%s: returned %i (%s)",
633 psz_cmd, i_ret, vlc_error( i_ret ) );
636 else if( !strcmp( psz_cmd, "logout" ) )
638 /* Close connection */
639 if( p_sys->i_socket != -1 )
641 net_Close( p_sys->i_socket );
642 p_sys->i_socket = -1;
645 else if( !strcmp( psz_cmd, "info" ) )
647 if( p_sys->p_input )
649 int i, j;
650 vlc_mutex_lock( &input_GetItem(p_sys->p_input)->lock );
651 for ( i = 0; i < input_GetItem(p_sys->p_input)->i_categories; i++ )
653 info_category_t *p_category = input_GetItem(p_sys->p_input)
654 ->pp_categories[i];
656 msg_rc( "+----[ %s ]", p_category->psz_name );
657 msg_rc( "| " );
658 for ( j = 0; j < p_category->i_infos; j++ )
660 info_t *p_info = p_category->pp_infos[j];
661 msg_rc( "| %s: %s", p_info->psz_name,
662 p_info->psz_value );
664 msg_rc( "| " );
666 msg_rc( "+----[ end of stream info ]" );
667 vlc_mutex_unlock( &input_GetItem(p_sys->p_input)->lock );
669 else
671 msg_rc( "no input" );
674 else if( !strcmp( psz_cmd, "is_playing" ) )
676 if( p_sys->p_input == NULL )
678 msg_rc( "0" );
680 else
682 msg_rc( "1" );
685 else if( !strcmp( psz_cmd, "get_time" ) )
687 int64_t t = 0;
688 if( p_sys->p_input != NULL )
689 t = var_GetInteger( p_sys->p_input, "time" ) / CLOCK_FREQ;
690 msg_rc( "%"PRIu64, t );
692 else if( !strcmp( psz_cmd, "get_length" ) )
694 int64_t l = 0;
695 if( p_sys->p_input != NULL )
696 l = var_GetInteger( p_sys->p_input, "length" ) / CLOCK_FREQ;
697 msg_rc( "%"PRIu64, l );
699 else if( !strcmp( psz_cmd, "get_title" ) )
701 if( p_sys->p_input == NULL )
703 msg_rc("%s", "");
705 else
707 msg_rc( "%s", input_GetItem(p_sys->p_input)->psz_name );
710 else if( !strcmp( psz_cmd, "longhelp" ) || !strncmp( psz_cmd, "h", 1 )
711 || !strncmp( psz_cmd, "H", 1 ) || !strncmp( psz_cmd, "?", 1 ) )
713 Help( p_intf );
715 else if( !strcmp( psz_cmd, "key" ) || !strcmp( psz_cmd, "hotkey" ) )
717 var_SetInteger( p_intf->obj.libvlc, "key-action",
718 vlc_actions_get_id( psz_arg ) );
720 else switch( psz_cmd[0] )
722 case 'f':
723 case 'F':
725 bool fs;
727 if( !strncasecmp( psz_arg, "on", 2 ) )
728 var_SetBool( p_sys->p_playlist, "fullscreen", fs = true );
729 else if( !strncasecmp( psz_arg, "off", 3 ) )
730 var_SetBool( p_sys->p_playlist, "fullscreen", fs = false );
731 else
732 fs = var_ToggleBool( p_sys->p_playlist, "fullscreen" );
734 if( p_sys->p_input != NULL )
736 vout_thread_t *p_vout = input_GetVout( p_sys->p_input );
737 if( p_vout )
739 var_SetBool( p_vout, "fullscreen", fs );
740 vlc_object_release( p_vout );
743 break;
745 case 's':
746 case 'S':
748 break;
750 case '\0':
751 /* Ignore empty lines */
752 break;
754 default:
755 msg_rc(_("Unknown command `%s'. Type `help' for help."), psz_cmd);
756 break;
759 /* Command processed */
760 i_size = 0; p_buffer[0] = 0;
763 msg_rc( STATUS_CHANGE "( stop state: 0 )" );
764 msg_rc( STATUS_CHANGE "( quit )" );
766 vlc_restorecancel( canc );
768 return NULL;
771 static void Help( intf_thread_t *p_intf)
773 msg_rc("%s", _("+----[ Remote control commands ]"));
774 msg_rc( "| ");
775 msg_rc("%s", _("| add XYZ . . . . . . . . . . . . add XYZ to playlist"));
776 msg_rc("%s", _("| enqueue XYZ . . . . . . . . . queue XYZ to playlist"));
777 msg_rc("%s", _("| playlist . . . . . show items currently in playlist"));
778 msg_rc("%s", _("| play . . . . . . . . . . . . . . . . . . play stream"));
779 msg_rc("%s", _("| stop . . . . . . . . . . . . . . . . . . stop stream"));
780 msg_rc("%s", _("| next . . . . . . . . . . . . . . next playlist item"));
781 msg_rc("%s", _("| prev . . . . . . . . . . . . previous playlist item"));
782 msg_rc("%s", _("| goto . . . . . . . . . . . . . . goto item at index"));
783 msg_rc("%s", _("| repeat [on|off] . . . . toggle playlist item repeat"));
784 msg_rc("%s", _("| loop [on|off] . . . . . . . . . toggle playlist loop"));
785 msg_rc("%s", _("| random [on|off] . . . . . . . toggle random jumping"));
786 msg_rc("%s", _("| clear . . . . . . . . . . . . . . clear the playlist"));
787 msg_rc("%s", _("| status . . . . . . . . . . . current playlist status"));
788 msg_rc("%s", _("| title [X] . . . . . . set/get title in current item"));
789 msg_rc("%s", _("| title_n . . . . . . . . next title in current item"));
790 msg_rc("%s", _("| title_p . . . . . . previous title in current item"));
791 msg_rc("%s", _("| chapter [X] . . . . set/get chapter in current item"));
792 msg_rc("%s", _("| chapter_n . . . . . . next chapter in current item"));
793 msg_rc("%s", _("| chapter_p . . . . previous chapter in current item"));
794 msg_rc( "| ");
795 msg_rc("%s", _("| seek X . . . seek in seconds, for instance `seek 12'"));
796 msg_rc("%s", _("| pause . . . . . . . . . . . . . . . . toggle pause"));
797 msg_rc("%s", _("| fastforward . . . . . . . . . set to maximum rate"));
798 msg_rc("%s", _("| rewind . . . . . . . . . . . . set to minimum rate"));
799 msg_rc("%s", _("| faster . . . . . . . . . . faster playing of stream"));
800 msg_rc("%s", _("| slower . . . . . . . . . . slower playing of stream"));
801 msg_rc("%s", _("| normal . . . . . . . . . . normal playing of stream"));
802 msg_rc("%s", _("| frame. . . . . . . . . . play frame by frame"));
803 msg_rc("%s", _("| f [on|off] . . . . . . . . . . . . toggle fullscreen"));
804 msg_rc("%s", _("| info . . . . . information about the current stream"));
805 msg_rc("%s", _("| stats . . . . . . . . show statistical information"));
806 msg_rc("%s", _("| get_time . . seconds elapsed since stream's beginning"));
807 msg_rc("%s", _("| is_playing . . . . 1 if a stream plays, 0 otherwise"));
808 msg_rc("%s", _("| get_title . . . . . the title of the current stream"));
809 msg_rc("%s", _("| get_length . . . . the length of the current stream"));
810 msg_rc( "| ");
811 msg_rc("%s", _("| volume [X] . . . . . . . . . . set/get audio volume"));
812 msg_rc("%s", _("| volup [X] . . . . . . . raise audio volume X steps"));
813 msg_rc("%s", _("| voldown [X] . . . . . . lower audio volume X steps"));
814 msg_rc("%s", _("| adev [device] . . . . . . . . set/get audio device"));
815 msg_rc("%s", _("| achan [X]. . . . . . . . . . set/get audio channels"));
816 msg_rc("%s", _("| atrack [X] . . . . . . . . . . . set/get audio track"));
817 msg_rc("%s", _("| vtrack [X] . . . . . . . . . . . set/get video track"));
818 msg_rc("%s", _("| vratio [X] . . . . . . . set/get video aspect ratio"));
819 msg_rc("%s", _("| vcrop [X] . . . . . . . . . . . set/get video crop"));
820 msg_rc("%s", _("| vzoom [X] . . . . . . . . . . . set/get video zoom"));
821 msg_rc("%s", _("| snapshot . . . . . . . . . . . . take video snapshot"));
822 msg_rc("%s", _("| strack [X] . . . . . . . . . set/get subtitle track"));
823 msg_rc("%s", _("| key [hotkey name] . . . . . . simulate hotkey press"));
824 msg_rc( "| ");
825 msg_rc("%s", _("| help . . . . . . . . . . . . . . . this help message"));
826 msg_rc("%s", _("| logout . . . . . . . exit (if in socket connection)"));
827 msg_rc("%s", _("| quit . . . . . . . . . . . . . . . . . . . quit vlc"));
828 msg_rc( "| ");
829 msg_rc("%s", _("+----[ end of help ]"));
832 /********************************************************************
833 * Status callback routines
834 ********************************************************************/
835 static int VolumeChanged( vlc_object_t *p_this, char const *psz_cmd,
836 vlc_value_t oldval, vlc_value_t newval, void *p_data )
838 (void) p_this;
839 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(newval);
840 intf_thread_t *p_intf = (intf_thread_t*)p_data;
842 vlc_mutex_lock( &p_intf->p_sys->status_lock );
843 msg_rc( STATUS_CHANGE "( audio volume: %ld )",
844 lroundf(newval.f_float * AOUT_VOLUME_DEFAULT) );
845 vlc_mutex_unlock( &p_intf->p_sys->status_lock );
846 return VLC_SUCCESS;
849 static void StateChanged( intf_thread_t *p_intf, input_thread_t *p_input )
851 playlist_t *p_playlist = p_intf->p_sys->p_playlist;
853 PL_LOCK;
854 const int i_status = playlist_Status( p_playlist );
855 PL_UNLOCK;
857 /* */
858 const char *psz_cmd;
859 switch( i_status )
861 case PLAYLIST_STOPPED:
862 psz_cmd = "stop";
863 break;
864 case PLAYLIST_RUNNING:
865 psz_cmd = "play";
866 break;
867 case PLAYLIST_PAUSED:
868 psz_cmd = "pause";
869 break;
870 default:
871 psz_cmd = "";
872 break;
875 /* */
876 const int i_state = var_GetInteger( p_input, "state" );
878 vlc_mutex_lock( &p_intf->p_sys->status_lock );
879 msg_rc( STATUS_CHANGE "( %s state: %d ): %s", psz_cmd,
880 i_state, ppsz_input_state[i_state] );
881 vlc_mutex_unlock( &p_intf->p_sys->status_lock );
883 static void RateChanged( intf_thread_t *p_intf,
884 input_thread_t *p_input )
886 vlc_mutex_lock( &p_intf->p_sys->status_lock );
887 msg_rc( STATUS_CHANGE "( new rate: %.3f )",
888 var_GetFloat( p_input, "rate" ) );
889 vlc_mutex_unlock( &p_intf->p_sys->status_lock );
891 static void PositionChanged( intf_thread_t *p_intf,
892 input_thread_t *p_input )
894 vlc_mutex_lock( &p_intf->p_sys->status_lock );
895 if( p_intf->p_sys->b_input_buffering )
896 msg_rc( STATUS_CHANGE "( time: %"PRId64"s )",
897 (var_GetInteger( p_input, "time" ) / CLOCK_FREQ) );
898 p_intf->p_sys->b_input_buffering = false;
899 vlc_mutex_unlock( &p_intf->p_sys->status_lock );
901 static void CacheChanged( intf_thread_t *p_intf )
903 vlc_mutex_lock( &p_intf->p_sys->status_lock );
904 p_intf->p_sys->b_input_buffering = true;
905 vlc_mutex_unlock( &p_intf->p_sys->status_lock );
908 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
909 vlc_value_t oldval, vlc_value_t newval, void *p_data )
911 VLC_UNUSED(psz_cmd);
912 VLC_UNUSED(oldval);
913 input_thread_t *p_input = (input_thread_t*)p_this;
914 intf_thread_t *p_intf = p_data;
916 switch( newval.i_int )
918 case INPUT_EVENT_STATE:
919 case INPUT_EVENT_DEAD:
920 StateChanged( p_intf, p_input );
921 break;
922 case INPUT_EVENT_RATE:
923 RateChanged( p_intf, p_input );
924 break;
925 case INPUT_EVENT_POSITION:
926 PositionChanged( p_intf, p_input );
927 break;
928 case INPUT_EVENT_CACHE:
929 CacheChanged( p_intf );
930 break;
931 default:
932 break;
934 return VLC_SUCCESS;
937 /********************************************************************
938 * Command routines
939 ********************************************************************/
940 static int Input( vlc_object_t *p_this, char const *psz_cmd,
941 vlc_value_t oldval, vlc_value_t newval, void *p_data )
943 VLC_UNUSED(oldval); VLC_UNUSED(p_data);
944 intf_thread_t *p_intf = (intf_thread_t*)p_this;
945 input_thread_t *p_input =
946 playlist_CurrentInput( p_intf->p_sys->p_playlist );
947 int i_error = VLC_EGENERIC;
949 if( !p_input )
950 return VLC_ENOOBJ;
952 int state = var_GetInteger( p_input, "state" );
953 if( ( state == PAUSE_S ) &&
954 ( strcmp( psz_cmd, "pause" ) != 0 ) && (strcmp( psz_cmd,"frame") != 0 ) )
956 msg_rc( "%s", _("Press pause to continue.") );
958 else
959 /* Parse commands that only require an input */
960 if( !strcmp( psz_cmd, "pause" ) )
962 playlist_TogglePause( p_intf->p_sys->p_playlist );
963 i_error = VLC_SUCCESS;
965 else if( !strcmp( psz_cmd, "seek" ) )
967 if( strlen( newval.psz_string ) > 0 &&
968 newval.psz_string[strlen( newval.psz_string ) - 1] == '%' )
970 float f = atof( newval.psz_string ) / 100.0;
971 var_SetFloat( p_input, "position", f );
973 else
975 mtime_t t = atoi( newval.psz_string );
976 var_SetInteger( p_input, "time", CLOCK_FREQ * t );
978 i_error = VLC_SUCCESS;
980 else if ( !strcmp( psz_cmd, "fastforward" ) )
982 if( var_GetBool( p_input, "can-rate" ) )
984 float f_rate = var_GetFloat( p_input, "rate" );
985 f_rate = (f_rate < 0) ? -f_rate : f_rate * 2;
986 var_SetFloat( p_input, "rate", f_rate );
988 else
990 var_SetInteger( p_intf->obj.libvlc, "key-action", ACTIONID_JUMP_FORWARD_EXTRASHORT );
992 i_error = VLC_SUCCESS;
994 else if ( !strcmp( psz_cmd, "rewind" ) )
996 if( var_GetBool( p_input, "can-rewind" ) )
998 float f_rate = var_GetFloat( p_input, "rate" );
999 f_rate = (f_rate > 0) ? -f_rate : f_rate * 2;
1000 var_SetFloat( p_input, "rate", f_rate );
1002 else
1004 var_SetInteger( p_intf->obj.libvlc, "key-action", ACTIONID_JUMP_BACKWARD_EXTRASHORT );
1006 i_error = VLC_SUCCESS;
1008 else if ( !strcmp( psz_cmd, "faster" ) )
1010 var_TriggerCallback( p_intf->p_sys->p_playlist, "rate-faster" );
1011 i_error = VLC_SUCCESS;
1013 else if ( !strcmp( psz_cmd, "slower" ) )
1015 var_TriggerCallback( p_intf->p_sys->p_playlist, "rate-slower" );
1016 i_error = VLC_SUCCESS;
1018 else if ( !strcmp( psz_cmd, "normal" ) )
1020 var_SetFloat( p_intf->p_sys->p_playlist, "rate", 1. );
1021 i_error = VLC_SUCCESS;
1023 else if ( !strcmp( psz_cmd, "frame" ) )
1025 var_TriggerCallback( p_input, "frame-next" );
1026 i_error = VLC_SUCCESS;
1028 else if( !strcmp( psz_cmd, "chapter" ) ||
1029 !strcmp( psz_cmd, "chapter_n" ) ||
1030 !strcmp( psz_cmd, "chapter_p" ) )
1032 if( !strcmp( psz_cmd, "chapter" ) )
1034 if ( *newval.psz_string )
1036 /* Set. */
1037 var_SetInteger( p_input, "chapter", atoi( newval.psz_string ) );
1039 else
1041 /* Get. */
1042 int i_chap = var_GetInteger( p_input, "chapter" );
1043 int i_chapter_count = var_CountChoices( p_input, "chapter" );
1044 msg_rc( "Currently playing chapter %d/%d.", i_chap,
1045 i_chapter_count );
1048 else if( !strcmp( psz_cmd, "chapter_n" ) )
1049 var_TriggerCallback( p_input, "next-chapter" );
1050 else if( !strcmp( psz_cmd, "chapter_p" ) )
1051 var_TriggerCallback( p_input, "prev-chapter" );
1052 i_error = VLC_SUCCESS;
1054 else if( !strcmp( psz_cmd, "title" ) ||
1055 !strcmp( psz_cmd, "title_n" ) ||
1056 !strcmp( psz_cmd, "title_p" ) )
1058 if( !strcmp( psz_cmd, "title" ) )
1060 if ( *newval.psz_string )
1061 /* Set. */
1062 var_SetInteger( p_input, "title", atoi( newval.psz_string ) );
1063 else
1065 /* Get. */
1066 int i_title = var_GetInteger( p_input, "title" );
1067 int i_title_count = var_CountChoices( p_input, "title" );
1068 msg_rc( "Currently playing title %d/%d.", i_title,
1069 i_title_count );
1072 else if( !strcmp( psz_cmd, "title_n" ) )
1073 var_TriggerCallback( p_input, "next-title" );
1074 else if( !strcmp( psz_cmd, "title_p" ) )
1075 var_TriggerCallback( p_input, "prev-title" );
1077 i_error = VLC_SUCCESS;
1079 else if( !strcmp( psz_cmd, "atrack" )
1080 || !strcmp( psz_cmd, "vtrack" )
1081 || !strcmp( psz_cmd, "strack" ) )
1083 const char *psz_variable;
1084 vlc_value_t val_name;
1086 if( !strcmp( psz_cmd, "atrack" ) )
1088 psz_variable = "audio-es";
1090 else if( !strcmp( psz_cmd, "vtrack" ) )
1092 psz_variable = "video-es";
1094 else
1096 psz_variable = "spu-es";
1099 /* Get the descriptive name of the variable */
1100 var_Change( p_input, psz_variable, VLC_VAR_GETTEXT,
1101 &val_name, NULL );
1102 if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);
1104 if( newval.psz_string && *newval.psz_string )
1106 /* set */
1107 i_error = var_SetInteger( p_input, psz_variable,
1108 atoi( newval.psz_string ) );
1110 else
1112 /* get */
1113 vlc_value_t val, text;
1115 int i_value = var_GetInteger( p_input, psz_variable );
1117 if ( var_Change( p_input, psz_variable,
1118 VLC_VAR_GETCHOICES, &val, &text ) < 0 )
1119 goto out;
1121 msg_rc( "+----[ %s ]", val_name.psz_string );
1122 for ( int i = 0; i < val.p_list->i_count; i++ )
1124 if ( i_value == val.p_list->p_values[i].i_int )
1125 msg_rc( "| %"PRId64" - %s *",
1126 val.p_list->p_values[i].i_int,
1127 text.p_list->p_values[i].psz_string );
1128 else
1129 msg_rc( "| %"PRId64" - %s",
1130 val.p_list->p_values[i].i_int,
1131 text.p_list->p_values[i].psz_string );
1133 var_FreeList( &val, &text );
1134 msg_rc( "+----[ end of %s ]", val_name.psz_string );
1136 free( val_name.psz_string );
1138 out:
1139 vlc_object_release( p_input );
1140 return i_error;
1143 static void print_playlist( intf_thread_t *p_intf, playlist_item_t *p_item, int i_level )
1145 char psz_buffer[MSTRTIME_MAX_SIZE];
1146 for( int i = 0; i< p_item->i_children; i++ )
1148 if( p_item->pp_children[i]->p_input->i_duration != -1 )
1150 secstotimestr( psz_buffer, p_item->pp_children[i]->p_input->i_duration / 1000000 );
1151 msg_rc( "|%*s- %s (%s)", 2 * i_level, "", p_item->pp_children[i]->p_input->psz_name, psz_buffer );
1153 else
1154 msg_rc( "|%*s- %s", 2 * i_level, "", p_item->pp_children[i]->p_input->psz_name );
1156 if( p_item->pp_children[i]->i_children >= 0 )
1157 print_playlist( p_intf, p_item->pp_children[i], i_level + 1 );
1161 static int Playlist( vlc_object_t *p_this, char const *psz_cmd,
1162 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1164 VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1166 intf_thread_t *p_intf = (intf_thread_t*)p_this;
1167 playlist_t *p_playlist = p_intf->p_sys->p_playlist;
1168 input_thread_t * p_input = playlist_CurrentInput( p_playlist );
1170 if( p_input )
1172 int state = var_GetInteger( p_input, "state" );
1173 vlc_object_release( p_input );
1175 if( state == PAUSE_S )
1177 msg_rc( "%s", _("Type 'pause' to continue.") );
1178 return VLC_EGENERIC;
1182 /* Parse commands that require a playlist */
1183 if( !strcmp( psz_cmd, "prev" ) )
1185 playlist_Prev( p_playlist );
1187 else if( !strcmp( psz_cmd, "next" ) )
1189 playlist_Next( p_playlist );
1191 else if( !strcmp( psz_cmd, "play" ) )
1193 msg_Warn( p_playlist, "play" );
1194 playlist_Play( p_playlist );
1196 else if( !strcmp( psz_cmd, "repeat" ) )
1198 bool b_update = true;
1199 bool b_value = var_GetBool( p_playlist, "repeat" );
1201 if( strlen( newval.psz_string ) > 0 )
1203 if ( ( !strncmp( newval.psz_string, "on", 2 ) && b_value ) ||
1204 ( !strncmp( newval.psz_string, "off", 3 ) && !b_value ) )
1206 b_update = false;
1210 if ( b_update )
1212 b_value = !b_value;
1213 var_SetBool( p_playlist, "repeat", b_value );
1215 msg_rc( "Setting repeat to %s", b_value ? "true" : "false" );
1217 else if( !strcmp( psz_cmd, "loop" ) )
1219 bool b_update = true;
1220 bool b_value = var_GetBool( p_playlist, "loop" );
1222 if( strlen( newval.psz_string ) > 0 )
1224 if ( ( !strncmp( newval.psz_string, "on", 2 ) && b_value ) ||
1225 ( !strncmp( newval.psz_string, "off", 3 ) && !b_value ) )
1227 b_update = false;
1231 if ( b_update )
1233 b_value = !b_value;
1234 var_SetBool( p_playlist, "loop", b_value );
1236 msg_rc( "Setting loop to %s", b_value ? "true" : "false" );
1238 else if( !strcmp( psz_cmd, "random" ) )
1240 bool b_update = true;
1241 bool b_value = var_GetBool( p_playlist, "random" );
1243 if( strlen( newval.psz_string ) > 0 )
1245 if ( ( !strncmp( newval.psz_string, "on", 2 ) && b_value ) ||
1246 ( !strncmp( newval.psz_string, "off", 3 ) && !b_value ) )
1248 b_update = false;
1252 if ( b_update )
1254 b_value = !b_value;
1255 var_SetBool( p_playlist, "random", b_value );
1257 msg_rc( "Setting random to %s", b_value ? "true" : "false" );
1259 else if (!strcmp( psz_cmd, "goto" ) )
1261 PL_LOCK;
1262 unsigned i_pos = atoi( newval.psz_string );
1263 unsigned i_size = p_playlist->items.i_size;
1265 if( i_pos <= 0 )
1266 msg_rc( "%s", _("Error: `goto' needs an argument greater than zero.") );
1267 else if( i_pos <= i_size )
1269 playlist_item_t *p_item, *p_parent;
1270 p_item = p_parent = p_playlist->items.p_elems[i_pos-1];
1271 while( p_parent->p_parent )
1272 p_parent = p_parent->p_parent;
1273 playlist_ViewPlay( p_playlist, p_parent, p_item );
1275 else
1276 msg_rc( vlc_ngettext("Playlist has only %u element",
1277 "Playlist has only %u elements", i_size),
1278 i_size );
1279 PL_UNLOCK;
1281 else if( !strcmp( psz_cmd, "stop" ) )
1283 playlist_Stop( p_playlist );
1285 else if( !strcmp( psz_cmd, "clear" ) )
1287 playlist_Stop( p_playlist );
1288 playlist_Clear( p_playlist, pl_Unlocked );
1290 else if( !strcmp( psz_cmd, "add" ) &&
1291 newval.psz_string && *newval.psz_string )
1293 input_item_t *p_item = parse_MRL( newval.psz_string );
1295 if( p_item )
1297 msg_rc( "Trying to add %s to playlist.", newval.psz_string );
1298 int i_ret = playlist_AddInput( p_playlist, p_item, true, true );
1299 input_item_Release( p_item );
1300 if( i_ret != VLC_SUCCESS )
1302 return VLC_EGENERIC;
1306 else if( !strcmp( psz_cmd, "enqueue" ) &&
1307 newval.psz_string && *newval.psz_string )
1309 input_item_t *p_item = parse_MRL( newval.psz_string );
1311 if( p_item )
1313 msg_rc( "trying to enqueue %s to playlist", newval.psz_string );
1314 int ret = playlist_AddInput( p_playlist, p_item, false, true );
1315 input_item_Release( p_item );
1316 if( ret != VLC_SUCCESS )
1318 return VLC_EGENERIC;
1322 else if( !strcmp( psz_cmd, "playlist" ) )
1324 msg_rc( "+----[ Playlist ]" );
1325 print_playlist( p_intf, &p_playlist->root, 0 );
1326 msg_rc( "+----[ End of playlist ]" );
1329 else if( !strcmp( psz_cmd, "sort" ))
1331 PL_LOCK;
1332 playlist_RecursiveNodeSort( p_playlist, &p_playlist->root,
1333 SORT_ARTIST, ORDER_NORMAL );
1334 PL_UNLOCK;
1336 else if( !strcmp( psz_cmd, "status" ) )
1338 p_input = playlist_CurrentInput( p_playlist );
1339 if( p_input )
1341 /* Replay the current state of the system. */
1342 char *psz_uri =
1343 input_item_GetURI( input_GetItem( p_input ) );
1344 vlc_object_release( p_input );
1345 if( likely(psz_uri != NULL) )
1347 msg_rc( STATUS_CHANGE "( new input: %s )", psz_uri );
1348 free( psz_uri );
1352 float volume = playlist_VolumeGet( p_playlist );
1353 if( volume >= 0.f )
1354 msg_rc( STATUS_CHANGE "( audio volume: %ld )",
1355 lroundf(volume * AOUT_VOLUME_DEFAULT) );
1357 int status;
1358 PL_LOCK;
1359 status = playlist_Status(p_playlist);
1360 PL_UNLOCK;
1361 switch( status )
1363 case PLAYLIST_STOPPED:
1364 msg_rc( STATUS_CHANGE "( stop state: 5 )" );
1365 break;
1366 case PLAYLIST_RUNNING:
1367 msg_rc( STATUS_CHANGE "( play state: 3 )" );
1368 break;
1369 case PLAYLIST_PAUSED:
1370 msg_rc( STATUS_CHANGE "( pause state: 4 )" );
1371 break;
1372 default:
1373 msg_rc( STATUS_CHANGE "( unknown state: -1 )" );
1374 break;
1379 * sanity check
1381 else
1383 msg_rc( "unknown command!" );
1386 return VLC_SUCCESS;
1389 static int Quit( vlc_object_t *p_this, char const *psz_cmd,
1390 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1392 VLC_UNUSED(p_data); VLC_UNUSED(psz_cmd);
1393 VLC_UNUSED(oldval); VLC_UNUSED(newval);
1395 libvlc_Quit( p_this->obj.libvlc );
1396 return VLC_SUCCESS;
1399 static int Intf( vlc_object_t *p_this, char const *psz_cmd,
1400 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1402 intf_thread_t *intf = (intf_thread_t *)p_this;
1404 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1405 return intf_Create(pl_Get(intf), newval.psz_string );
1408 static int Volume( vlc_object_t *p_this, char const *psz_cmd,
1409 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1411 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1412 intf_thread_t *p_intf = (intf_thread_t*)p_this;
1413 playlist_t *p_playlist = p_intf->p_sys->p_playlist;
1414 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
1415 int i_error = VLC_EGENERIC;
1417 if( !p_input )
1418 return VLC_ENOOBJ;
1420 if( p_input )
1422 int state = var_GetInteger( p_input, "state" );
1423 vlc_object_release( p_input );
1424 if( state == PAUSE_S )
1426 msg_rc( "%s", _("Type 'pause' to continue.") );
1427 return VLC_EGENERIC;
1431 if ( *newval.psz_string )
1433 /* Set. */
1434 int i_volume = atoi( newval.psz_string );
1435 if( !playlist_VolumeSet( p_playlist,
1436 i_volume / (float)AOUT_VOLUME_DEFAULT ) )
1437 i_error = VLC_SUCCESS;
1438 playlist_MuteSet( p_playlist, i_volume == 0 );
1439 msg_rc( STATUS_CHANGE "( audio volume: %d )", i_volume );
1441 else
1443 /* Get. */
1444 msg_rc( STATUS_CHANGE "( audio volume: %ld )",
1445 lroundf( playlist_VolumeGet( p_playlist ) * AOUT_VOLUME_DEFAULT ) );
1446 i_error = VLC_SUCCESS;
1449 return i_error;
1452 static int VolumeMove( vlc_object_t *p_this, char const *psz_cmd,
1453 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1455 VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1456 intf_thread_t *p_intf = (intf_thread_t*)p_this;
1457 float volume;
1458 input_thread_t *p_input =
1459 playlist_CurrentInput( p_intf->p_sys->p_playlist );
1460 int i_nb_steps = atoi(newval.psz_string);
1461 int i_error = VLC_SUCCESS;
1463 if( !p_input )
1464 return VLC_ENOOBJ;
1466 int state = var_GetInteger( p_input, "state" );
1467 vlc_object_release( p_input );
1468 if( state == PAUSE_S )
1470 msg_rc( "%s", _("Type 'pause' to continue.") );
1471 return VLC_EGENERIC;
1474 if( !strcmp(psz_cmd, "voldown") )
1475 i_nb_steps *= -1;
1476 if( playlist_VolumeUp( p_intf->p_sys->p_playlist, i_nb_steps, &volume ) < 0 )
1477 i_error = VLC_EGENERIC;
1479 if ( !i_error )
1480 msg_rc( STATUS_CHANGE "( audio volume: %ld )",
1481 lroundf( volume * AOUT_VOLUME_DEFAULT ) );
1482 return i_error;
1486 static int VideoConfig( vlc_object_t *p_this, char const *psz_cmd,
1487 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1489 VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1490 intf_thread_t *p_intf = (intf_thread_t*)p_this;
1491 input_thread_t *p_input =
1492 playlist_CurrentInput( p_intf->p_sys->p_playlist );
1493 vout_thread_t * p_vout;
1494 const char * psz_variable = NULL;
1495 int i_error = VLC_SUCCESS;
1497 if( !p_input )
1498 return VLC_ENOOBJ;
1500 p_vout = input_GetVout( p_input );
1501 vlc_object_release( p_input );
1502 if( !p_vout )
1503 return VLC_ENOOBJ;
1505 if( !strcmp( psz_cmd, "vcrop" ) )
1507 psz_variable = "crop";
1509 else if( !strcmp( psz_cmd, "vratio" ) )
1511 psz_variable = "aspect-ratio";
1513 else if( !strcmp( psz_cmd, "vzoom" ) )
1515 psz_variable = "zoom";
1517 else if( !strcmp( psz_cmd, "snapshot" ) )
1519 psz_variable = "video-snapshot";
1521 else
1522 /* This case can't happen */
1523 vlc_assert_unreachable();
1525 if( newval.psz_string && *newval.psz_string )
1527 /* set */
1528 if( !strcmp( psz_variable, "zoom" ) )
1530 float f_float = atof( newval.psz_string );
1531 i_error = var_SetFloat( p_vout, psz_variable, f_float );
1533 else
1535 i_error = var_SetString( p_vout, psz_variable, newval.psz_string );
1538 else if( !strcmp( psz_cmd, "snapshot" ) )
1540 var_TriggerCallback( p_vout, psz_variable );
1542 else
1544 /* get */
1545 vlc_value_t val_name;
1546 vlc_value_t val, text;
1547 float f_value = 0.;
1548 char *psz_value = NULL;
1550 if( !strcmp( psz_variable, "zoom" ) )
1551 f_value = var_GetFloat( p_vout, "zoom" );
1552 else
1554 psz_value = var_GetString( p_vout, psz_variable );
1555 if( psz_value == NULL )
1557 vlc_object_release( p_vout );
1558 return VLC_EGENERIC;
1562 if ( var_Change( p_vout, psz_variable,
1563 VLC_VAR_GETCHOICES, &val, &text ) < 0 )
1565 vlc_object_release( p_vout );
1566 free( psz_value );
1567 return VLC_EGENERIC;
1570 /* Get the descriptive name of the variable */
1571 var_Change( p_vout, psz_variable, VLC_VAR_GETTEXT,
1572 &val_name, NULL );
1573 if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);
1575 msg_rc( "+----[ %s ]", val_name.psz_string );
1576 if( !strcmp( psz_variable, "zoom" ) )
1578 for ( int i = 0; i < val.p_list->i_count; i++ )
1580 if ( f_value == val.p_list->p_values[i].f_float )
1581 msg_rc( "| %f - %s *", val.p_list->p_values[i].f_float,
1582 text.p_list->p_values[i].psz_string );
1583 else
1584 msg_rc( "| %f - %s", val.p_list->p_values[i].f_float,
1585 text.p_list->p_values[i].psz_string );
1588 else
1590 for ( int i = 0; i < val.p_list->i_count; i++ )
1592 if ( !strcmp( psz_value, val.p_list->p_values[i].psz_string ) )
1593 msg_rc( "| %s - %s *", val.p_list->p_values[i].psz_string,
1594 text.p_list->p_values[i].psz_string );
1595 else
1596 msg_rc( "| %s - %s", val.p_list->p_values[i].psz_string,
1597 text.p_list->p_values[i].psz_string );
1599 free( psz_value );
1601 var_FreeList( &val, &text );
1602 msg_rc( "+----[ end of %s ]", val_name.psz_string );
1604 free( val_name.psz_string );
1606 vlc_object_release( p_vout );
1607 return i_error;
1610 static int AudioDevice( vlc_object_t *obj, char const *cmd,
1611 vlc_value_t old, vlc_value_t cur, void *dummy )
1613 intf_thread_t *p_intf = (intf_thread_t *)obj;
1614 audio_output_t *p_aout = playlist_GetAout( pl_Get(p_intf) );
1615 if( p_aout == NULL )
1616 return VLC_ENOOBJ;
1618 if( !*cur.psz_string )
1620 char **ids, **names;
1621 int n = aout_DevicesList( p_aout, &ids, &names );
1622 if( n < 0 )
1623 goto out;
1625 char *dev = aout_DeviceGet( p_aout );
1626 const char *devstr = (dev != NULL) ? dev : "";
1628 msg_rc( "+----[ %s ]", cmd );
1629 for ( int i = 0; i < n; i++ )
1631 const char *fmt = "| %s - %s";
1633 if( !strcmp(devstr, ids[i]) )
1634 fmt = "| %s - %s *";
1635 msg_rc( fmt, ids[i], names[i] );
1636 free( names[i] );
1637 free( ids[i] );
1639 msg_rc( "+----[ end of %s ]", cmd );
1641 free( dev );
1642 free( names );
1643 free( ids );
1645 else
1646 aout_DeviceSet( p_aout, cur.psz_string );
1647 out:
1648 vlc_object_release( p_aout );
1649 (void) old; (void) dummy;
1650 return VLC_SUCCESS;
1653 static int AudioChannel( vlc_object_t *obj, char const *cmd,
1654 vlc_value_t old, vlc_value_t cur, void *dummy )
1656 intf_thread_t *p_intf = (intf_thread_t*)obj;
1657 vlc_object_t *p_aout = (vlc_object_t *)playlist_GetAout( pl_Get(p_intf) );
1658 if ( p_aout == NULL )
1659 return VLC_ENOOBJ;
1661 int ret = VLC_SUCCESS;
1663 if ( !*cur.psz_string )
1665 /* Retrieve all registered ***. */
1666 vlc_value_t val, text;
1667 if ( var_Change( p_aout, "stereo-mode",
1668 VLC_VAR_GETCHOICES, &val, &text ) < 0 )
1670 ret = VLC_ENOVAR;
1671 goto out;
1674 int i_value = var_GetInteger( p_aout, "stereo-mode" );
1676 msg_rc( "+----[ %s ]", cmd );
1677 for ( int i = 0; i < val.p_list->i_count; i++ )
1679 if ( i_value == val.p_list->p_values[i].i_int )
1680 msg_rc( "| %"PRId64" - %s *", val.p_list->p_values[i].i_int,
1681 text.p_list->p_values[i].psz_string );
1682 else
1683 msg_rc( "| %"PRId64" - %s", val.p_list->p_values[i].i_int,
1684 text.p_list->p_values[i].psz_string );
1686 var_FreeList( &val, &text );
1687 msg_rc( "+----[ end of %s ]", cmd );
1689 else
1690 ret = var_SetInteger( p_aout, "stereo-mode", atoi( cur.psz_string ) );
1691 out:
1692 vlc_object_release( p_aout );
1693 (void) old; (void) dummy;
1694 return ret;
1697 static int Statistics ( vlc_object_t *p_this, char const *psz_cmd,
1698 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1700 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(p_data);
1701 intf_thread_t *p_intf = (intf_thread_t*)p_this;
1702 input_thread_t *p_input =
1703 playlist_CurrentInput( p_intf->p_sys->p_playlist );
1705 if( !p_input )
1706 return VLC_ENOOBJ;
1708 updateStatistics( p_intf, input_GetItem(p_input) );
1709 vlc_object_release( p_input );
1710 return VLC_SUCCESS;
1713 static int updateStatistics( intf_thread_t *p_intf, input_item_t *p_item )
1715 if( !p_item ) return VLC_EGENERIC;
1717 vlc_mutex_lock( &p_item->lock );
1718 vlc_mutex_lock( &p_item->p_stats->lock );
1719 msg_rc( "+----[ begin of statistical info ]" );
1721 /* Input */
1722 msg_rc("%s", _("+-[Incoming]"));
1723 msg_rc(_("| input bytes read : %8.0f KiB"),
1724 (float)(p_item->p_stats->i_read_bytes)/1024 );
1725 msg_rc(_("| input bitrate : %6.0f kb/s"),
1726 (float)(p_item->p_stats->f_input_bitrate)*8000 );
1727 msg_rc(_("| demux bytes read : %8.0f KiB"),
1728 (float)(p_item->p_stats->i_demux_read_bytes)/1024 );
1729 msg_rc(_("| demux bitrate : %6.0f kb/s"),
1730 (float)(p_item->p_stats->f_demux_bitrate)*8000 );
1731 msg_rc(_("| demux corrupted : %5"PRIi64),
1732 p_item->p_stats->i_demux_corrupted );
1733 msg_rc(_("| discontinuities : %5"PRIi64),
1734 p_item->p_stats->i_demux_discontinuity );
1735 msg_rc("|");
1736 /* Video */
1737 msg_rc("%s", _("+-[Video Decoding]"));
1738 msg_rc(_("| video decoded : %5"PRIi64),
1739 p_item->p_stats->i_decoded_video );
1740 msg_rc(_("| frames displayed : %5"PRIi64),
1741 p_item->p_stats->i_displayed_pictures );
1742 msg_rc(_("| frames lost : %5"PRIi64),
1743 p_item->p_stats->i_lost_pictures );
1744 msg_rc("|");
1745 /* Audio*/
1746 msg_rc("%s", _("+-[Audio Decoding]"));
1747 msg_rc(_("| audio decoded : %5"PRIi64),
1748 p_item->p_stats->i_decoded_audio );
1749 msg_rc(_("| buffers played : %5"PRIi64),
1750 p_item->p_stats->i_played_abuffers );
1751 msg_rc(_("| buffers lost : %5"PRIi64),
1752 p_item->p_stats->i_lost_abuffers );
1753 msg_rc("|");
1754 /* Sout */
1755 msg_rc("%s", _("+-[Streaming]"));
1756 msg_rc(_("| packets sent : %5"PRIi64),
1757 p_item->p_stats->i_sent_packets );
1758 msg_rc(_("| bytes sent : %8.0f KiB"),
1759 (float)(p_item->p_stats->i_sent_bytes)/1024 );
1760 msg_rc(_("| sending bitrate : %6.0f kb/s"),
1761 (float)(p_item->p_stats->f_send_bitrate*8)*1000 );
1762 msg_rc("|");
1763 msg_rc( "+----[ end of statistical info ]" );
1764 vlc_mutex_unlock( &p_item->p_stats->lock );
1765 vlc_mutex_unlock( &p_item->lock );
1767 return VLC_SUCCESS;
1770 #if defined(_WIN32) && !VLC_WINSTORE_APP
1771 static bool ReadWin32( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
1773 INPUT_RECORD input_record;
1774 DWORD i_dw;
1776 /* On Win32, select() only works on socket descriptors */
1777 while( WaitForSingleObjectEx( p_intf->p_sys->hConsoleIn,
1778 INTF_IDLE_SLEEP/1000, TRUE ) == WAIT_OBJECT_0 )
1780 while( *pi_size < MAX_LINE_LENGTH &&
1781 ReadConsoleInput( p_intf->p_sys->hConsoleIn, &input_record,
1782 1, &i_dw ) )
1784 if( input_record.EventType != KEY_EVENT ||
1785 !input_record.Event.KeyEvent.bKeyDown ||
1786 input_record.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
1787 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL||
1788 input_record.Event.KeyEvent.wVirtualKeyCode == VK_MENU ||
1789 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CAPITAL )
1791 /* nothing interesting */
1792 continue;
1795 p_buffer[ *pi_size ] = input_record.Event.KeyEvent.uChar.AsciiChar;
1797 /* Echo out the command */
1798 putc( p_buffer[ *pi_size ], stdout );
1800 /* Handle special keys */
1801 if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1803 putc( '\n', stdout );
1804 break;
1806 switch( p_buffer[ *pi_size ] )
1808 case '\b':
1809 if( *pi_size )
1811 *pi_size -= 2;
1812 putc( ' ', stdout );
1813 putc( '\b', stdout );
1815 break;
1816 case '\r':
1817 (*pi_size) --;
1818 break;
1821 (*pi_size)++;
1824 if( *pi_size == MAX_LINE_LENGTH ||
1825 p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1827 p_buffer[ *pi_size ] = 0;
1828 return true;
1832 vlc_testcancel ();
1834 return false;
1836 #endif
1838 bool ReadCommand( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
1840 #if defined(_WIN32) && !VLC_WINSTORE_APP
1841 if( p_intf->p_sys->i_socket == -1 && !p_intf->p_sys->b_quiet )
1842 return ReadWin32( p_intf, p_buffer, pi_size );
1843 else if( p_intf->p_sys->i_socket == -1 )
1845 msleep( INTF_IDLE_SLEEP );
1846 return false;
1848 #endif
1850 while( *pi_size < MAX_LINE_LENGTH )
1852 if( p_intf->p_sys->i_socket == -1 )
1854 if( read( 0/*STDIN_FILENO*/, p_buffer + *pi_size, 1 ) <= 0 )
1855 { /* Standard input closed: exit */
1856 libvlc_Quit( p_intf->obj.libvlc );
1857 p_buffer[*pi_size] = 0;
1858 return true;
1861 else
1862 { /* Connection closed */
1863 if( net_Read( p_intf, p_intf->p_sys->i_socket, p_buffer + *pi_size,
1864 1 ) <= 0 )
1866 net_Close( p_intf->p_sys->i_socket );
1867 p_intf->p_sys->i_socket = -1;
1868 p_buffer[*pi_size] = 0;
1869 return true;
1873 if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1874 break;
1876 (*pi_size)++;
1879 if( *pi_size == MAX_LINE_LENGTH ||
1880 p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1882 p_buffer[ *pi_size ] = 0;
1883 return true;
1886 return false;
1889 /*****************************************************************************
1890 * parse_MRL: build a input item from a full mrl
1891 *****************************************************************************
1892 * MRL format: "simplified-mrl [:option-name[=option-value]]"
1893 * We don't check for '"' or '\'', we just assume that a ':' that follows a
1894 * space is a new option. Should be good enough for our purpose.
1895 *****************************************************************************/
1896 static input_item_t *parse_MRL( const char *mrl )
1898 #define SKIPSPACE( p ) { while( *p == ' ' || *p == '\t' ) p++; }
1899 #define SKIPTRAILINGSPACE( p, d ) \
1900 { char *e=d; while( e > p && (*(e-1)==' ' || *(e-1)=='\t') ){e--;*e=0;} }
1902 input_item_t *p_item = NULL;
1903 char *psz_item = NULL, *psz_item_mrl = NULL, *psz_orig, *psz_mrl;
1904 char **ppsz_options = NULL;
1905 int i_options = 0;
1907 if( !mrl ) return 0;
1909 psz_mrl = psz_orig = strdup( mrl );
1910 if( !psz_mrl )
1911 return NULL;
1912 while( *psz_mrl )
1914 SKIPSPACE( psz_mrl );
1915 psz_item = psz_mrl;
1917 for( ; *psz_mrl; psz_mrl++ )
1919 if( (*psz_mrl == ' ' || *psz_mrl == '\t') && psz_mrl[1] == ':' )
1921 /* We have a complete item */
1922 break;
1924 if( (*psz_mrl == ' ' || *psz_mrl == '\t') &&
1925 (psz_mrl[1] == '"' || psz_mrl[1] == '\'') && psz_mrl[2] == ':')
1927 /* We have a complete item */
1928 break;
1932 if( *psz_mrl ) { *psz_mrl = 0; psz_mrl++; }
1933 SKIPTRAILINGSPACE( psz_item, psz_item + strlen( psz_item ) );
1935 /* Remove '"' and '\'' if necessary */
1936 if( *psz_item == '"' && psz_item[strlen(psz_item)-1] == '"' )
1937 { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
1938 if( *psz_item == '\'' && psz_item[strlen(psz_item)-1] == '\'' )
1939 { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
1941 if( !psz_item_mrl )
1943 if( strstr( psz_item, "://" ) != NULL )
1944 psz_item_mrl = strdup( psz_item );
1945 else
1946 psz_item_mrl = vlc_path2uri( psz_item, NULL );
1947 if( psz_item_mrl == NULL )
1949 free( psz_orig );
1950 return NULL;
1953 else if( *psz_item )
1955 i_options++;
1956 ppsz_options = xrealloc( ppsz_options, i_options * sizeof(char *) );
1957 ppsz_options[i_options - 1] = &psz_item[1];
1960 if( *psz_mrl ) SKIPSPACE( psz_mrl );
1963 /* Now create a playlist item */
1964 if( psz_item_mrl )
1966 p_item = input_item_New( psz_item_mrl, NULL );
1967 for( int i = 0; i < i_options; i++ )
1969 input_item_AddOption( p_item, ppsz_options[i], VLC_INPUT_OPTION_TRUSTED );
1971 free( psz_item_mrl );
1974 if( i_options ) free( ppsz_options );
1975 free( psz_orig );
1977 return p_item;