1 /*****************************************************************************
2 * input_ext-intf.c: services to the interface
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
5 * $Id: input_ext-intf.c,v 1.51 2003/07/13 19:58:41 massiot Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <string.h> /* memcpy(), memset() */
31 #include "stream_control.h"
32 #include "input_ext-dec.h"
33 #include "input_ext-intf.h"
34 #include "input_ext-plugins.h"
36 /*****************************************************************************
37 * input_SetStatus: change the reading status
38 *****************************************************************************/
39 void __input_SetStatus( vlc_object_t * p_this, int i_mode )
41 input_thread_t *p_input;
43 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
47 msg_Err( p_this, "no input found" );
51 vlc_mutex_lock( &p_input->stream.stream_lock );
55 case INPUT_STATUS_END:
56 p_input->stream.i_new_status = PLAYING_S;
58 msg_Dbg( p_input, "end of stream" );
61 case INPUT_STATUS_PLAY:
62 p_input->stream.i_new_status = PLAYING_S;
63 msg_Dbg( p_input, "playing at normal rate" );
66 case INPUT_STATUS_PAUSE:
67 /* XXX: we don't need to check i_status, because input_clock.c
69 p_input->stream.i_new_status = PAUSE_S;
70 msg_Dbg( p_input, "toggling pause" );
73 case INPUT_STATUS_FASTER:
74 if( p_input->stream.control.i_rate * 4 <= DEFAULT_RATE )
76 msg_Dbg( p_input, "can not play any faster" );
80 p_input->stream.i_new_status = FORWARD_S;
81 p_input->stream.i_new_rate =
82 p_input->stream.control.i_rate / 2;
84 if ( p_input->stream.i_new_rate < DEFAULT_RATE )
86 msg_Dbg( p_input, "playing at %i:1 fast forward",
87 DEFAULT_RATE / p_input->stream.i_new_rate );
89 else if ( p_input->stream.i_new_rate > DEFAULT_RATE )
91 msg_Dbg( p_input, "playing at 1:%i slow motion",
92 p_input->stream.i_new_rate / DEFAULT_RATE );
94 else if ( p_input->stream.i_new_rate == DEFAULT_RATE )
96 p_input->stream.i_new_status = PLAYING_S;
97 msg_Dbg( p_input, "playing at normal rate" );
102 case INPUT_STATUS_SLOWER:
103 if( p_input->stream.control.i_rate >= 8 * DEFAULT_RATE )
105 msg_Dbg( p_input, "can not play any slower" );
109 p_input->stream.i_new_status = FORWARD_S;
110 p_input->stream.i_new_rate =
111 p_input->stream.control.i_rate * 2;
113 if ( p_input->stream.i_new_rate < DEFAULT_RATE )
115 msg_Dbg( p_input, "playing at %i:1 fast forward",
116 DEFAULT_RATE / p_input->stream.i_new_rate );
118 else if ( p_input->stream.i_new_rate > DEFAULT_RATE )
120 msg_Dbg( p_input, "playing at 1:%i slow motion",
121 p_input->stream.i_new_rate / DEFAULT_RATE );
123 else if ( p_input->stream.i_new_rate == DEFAULT_RATE )
125 p_input->stream.i_new_status = PLAYING_S;
126 msg_Dbg( p_input, "playing at normal rate" );
135 vlc_cond_signal( &p_input->stream.stream_wait );
136 vlc_mutex_unlock( &p_input->stream.stream_lock );
138 vlc_object_release( p_input );
141 void __input_SetRate( vlc_object_t * p_this, int i_rate )
143 input_thread_t *p_input;
145 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
147 if( p_input == NULL )
149 msg_Err( p_this, "no input found" );
153 vlc_mutex_lock( &p_input->stream.stream_lock );
155 if( i_rate * 8 < DEFAULT_RATE )
157 msg_Dbg( p_input, "can not play faster than 8x" );
158 vlc_mutex_unlock( &p_input->stream.stream_lock );
161 if( i_rate > DEFAULT_RATE * 8 )
163 msg_Dbg( p_input, "can not play slower than 1/8x" );
164 vlc_mutex_unlock( &p_input->stream.stream_lock );
168 p_input->stream.i_new_status = FORWARD_S;
169 p_input->stream.i_new_rate = i_rate;
171 if ( p_input->stream.i_new_rate < DEFAULT_RATE )
173 msg_Dbg( p_input, "playing at %i:1 fast forward",
174 DEFAULT_RATE / p_input->stream.i_new_rate );
176 else if ( p_input->stream.i_new_rate > DEFAULT_RATE )
178 msg_Dbg( p_input, "playing at 1:%i slow motion",
179 p_input->stream.i_new_rate / DEFAULT_RATE );
181 else if ( p_input->stream.i_new_rate == DEFAULT_RATE )
183 p_input->stream.i_new_status = PLAYING_S;
184 msg_Dbg( p_input, "playing at normal rate" );
187 vlc_cond_signal( &p_input->stream.stream_wait );
188 vlc_mutex_unlock( &p_input->stream.stream_lock );
190 vlc_object_release( p_input );
193 /*****************************************************************************
194 * input_Seek: changes the stream postion
195 *****************************************************************************/
196 void __input_Seek( vlc_object_t * p_this, off_t i_position, int i_whence )
198 input_thread_t *p_input;
200 char psz_time1[OFFSETTOTIME_MAX_SIZE];
201 char psz_time2[OFFSETTOTIME_MAX_SIZE];
203 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
205 if( p_input == NULL )
207 msg_Err( p_this, "no input found" );
211 vlc_mutex_lock( &p_input->stream.stream_lock );
213 #define A p_input->stream.p_selected_area
214 switch( i_whence & 0x30 )
216 case INPUT_SEEK_SECONDS:
217 i_position *= (off_t)50 * p_input->stream.i_mux_rate;
220 case INPUT_SEEK_PERCENT:
221 i_position = A->i_size * i_position / (off_t)100;
224 case INPUT_SEEK_BYTES:
229 switch( i_whence & 0x03 )
232 A->i_seek = A->i_tell + i_position;
236 A->i_seek = A->i_size + i_position;
241 A->i_seek = i_position;
249 else if( A->i_seek > A->i_size )
251 A->i_seek = A->i_size;
254 msg_Dbg( p_input, "seeking position "I64Fd"/"I64Fd" (%s/%s)",
255 A->i_seek, A->i_size,
256 input_OffsetToTime( p_input, psz_time1, i_position ),
257 input_OffsetToTime( p_input, psz_time2, A->i_size ) );
260 vlc_cond_signal( &p_input->stream.stream_wait );
261 vlc_mutex_unlock( &p_input->stream.stream_lock );
263 vlc_object_release( p_input );
266 /*****************************************************************************
267 * input_Tell: requests the stream postion
268 *****************************************************************************/
269 void __input_Tell( vlc_object_t * p_this, stream_position_t * p_position )
271 input_thread_t *p_input;
273 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
275 if( p_input == NULL )
277 p_position->i_tell = 0;
278 p_position->i_size = 0;
279 p_position->i_mux_rate = 0;
280 msg_Err( p_this, "no input found" );
284 vlc_mutex_lock( &p_input->stream.stream_lock );
286 #define A p_input->stream.p_selected_area
287 p_position->i_tell = A->i_tell;
288 p_position->i_size = A->i_size;
289 p_position->i_mux_rate = p_input->stream.i_mux_rate;
292 vlc_mutex_unlock( &p_input->stream.stream_lock );
293 vlc_object_release( p_input );
296 /*****************************************************************************
297 * input_OffsetToTime : converts an off_t value to a time indicator, using
299 *****************************************************************************
300 * BEWARE : this function assumes that you already own the lock on
301 * p_input->stream.stream_lock
302 *****************************************************************************/
303 char * input_OffsetToTime( input_thread_t * p_input, char * psz_buffer,
308 if( p_input->stream.i_mux_rate )
310 i_seconds = i_offset / 50 / p_input->stream.i_mux_rate;
311 snprintf( psz_buffer, OFFSETTOTIME_MAX_SIZE, "%d:%02d:%02d",
312 (int) (i_seconds / (60 * 60)),
313 (int) (i_seconds / 60 % 60),
314 (int) (i_seconds % 60) );
315 return( psz_buffer );
319 /* Divide by zero is not my friend. */
320 sprintf( psz_buffer, "-:--:--" );
321 return( psz_buffer );
325 /*****************************************************************************
326 * input_DumpStream: dumps the contents of a stream descriptor
327 *****************************************************************************
328 * BEWARE : this function assumes that you already own the lock on
329 * p_input->stream.stream_lock
330 *****************************************************************************/
331 void input_DumpStream( input_thread_t * p_input )
333 char psz_time1[OFFSETTOTIME_MAX_SIZE];
334 char psz_time2[OFFSETTOTIME_MAX_SIZE];
337 #define S p_input->stream
338 msg_Dbg( p_input, "dumping stream ID 0x%x [OK:%ld/D:%ld]", S.i_stream_id,
339 S.c_packets_read, S.c_packets_trashed );
341 msg_Dbg( p_input, "seekable stream, position: "I64Fd"/"I64Fd" (%s/%s)",
342 S.p_selected_area->i_tell, S.p_selected_area->i_size,
343 input_OffsetToTime( p_input, psz_time1,
344 S.p_selected_area->i_tell ),
345 input_OffsetToTime( p_input, psz_time2,
346 S.p_selected_area->i_size ) );
348 msg_Dbg( p_input, "pace %scontrolled", S.b_pace_control ? "" : "un-" );
350 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
352 #define P p_input->stream.pp_programs[i]
353 msg_Dbg( p_input, "dumping program 0x%x, version %d (%s)",
354 P->i_number, P->i_version,
355 P->b_is_ok ? "complete" : "partial" );
357 for( j = 0; j < p_input->stream.pp_programs[i]->i_es_number; j++ )
359 #define ES p_input->stream.pp_programs[i]->pp_es[j]
360 msg_Dbg( p_input, "ES 0x%x, "
361 "stream 0x%x, fourcc `%4.4s', %s [OK:%ld/ERR:%ld]",
362 ES->i_id, ES->i_stream_id, (char*)&ES->i_fourcc,
363 ES->p_decoder_fifo != NULL ? "selected" : "not selected",
364 ES->c_packets, ES->c_invalid_packets );
370 /*****************************************************************************
371 * input_ToggleES: answers to a user request with calls to (Un)SelectES
372 *****************************************************************************
373 * Useful since the interface plugins know p_es.
374 * It only works for audio & spu ( to be sure nothing nasty is being done ).
375 * b_select is a boolean to know if we have to select or unselect ES
376 *****************************************************************************/
377 int input_ToggleES( input_thread_t * p_input, es_descriptor_t * p_es,
378 vlc_bool_t b_select )
380 vlc_mutex_lock( &p_input->stream.stream_lock );
386 p_input->stream.p_newly_selected_es = p_es;
390 p_input->stream.p_removed_es = p_es;
394 vlc_mutex_unlock( &p_input->stream.stream_lock );
399 /****************************************************************************
400 * input_ChangeArea: interface request an area change
401 ****************************************************************************/
402 int input_ChangeArea( input_thread_t * p_input, input_area_t * p_area )
404 vlc_mutex_lock( &p_input->stream.stream_lock );
406 p_input->stream.p_new_area = p_area;
408 vlc_mutex_unlock( &p_input->stream.stream_lock );
413 /****************************************************************************
414 * input_ChangeProgram: interface request a program change
415 ****************************************************************************/
416 int input_ChangeProgram( input_thread_t * p_input, uint16_t i_program_number )
418 pgrm_descriptor_t * p_program;
421 vlc_mutex_lock( &p_input->stream.stream_lock );
423 p_program = input_FindProgram( p_input, i_program_number );
425 if ( p_program == NULL )
427 msg_Err( p_input, "could not find selected program" );
431 p_input->stream.p_new_program = p_program;
433 vlc_mutex_unlock( &p_input->stream.stream_lock );
435 /* Update the navigation variables without triggering a callback */
436 val.i_int = i_program_number;
437 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
442 /****************************************************************************
443 * input_ToggleGrayscale: change to grayscale or color output
444 ****************************************************************************/
445 int input_ToggleGrayscale( input_thread_t * p_input )
447 /* No need to warn the input thread since only the decoders and outputs
449 vlc_mutex_lock( &p_input->stream.control.control_lock );
450 p_input->stream.control.b_grayscale =
451 !p_input->stream.control.b_grayscale;
453 msg_Dbg( p_input, "changing to %s output",
454 p_input->stream.control.b_grayscale ? "grayscale" : "color" );
456 vlc_mutex_unlock( &p_input->stream.control.control_lock );
461 /****************************************************************************
462 * input_ToggleMute: activate/deactivate mute mode
463 ****************************************************************************/
464 int input_ToggleMute( input_thread_t * p_input )
466 /* We need to feed the decoders with 0, and only input can do that, so
467 * pass the message to the input thread. */
468 vlc_mutex_lock( &p_input->stream.stream_lock );
469 p_input->stream.b_new_mute = !p_input->stream.control.b_mute;
471 msg_Dbg( p_input, "%s mute mode",
472 p_input->stream.control.b_mute ? "activating" : "deactivating" );
474 vlc_mutex_unlock( &p_input->stream.stream_lock );