1 /*****************************************************************************
2 * input_ext-intf.c: services to the interface
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VideoLAN
5 * $Id: input_ext-intf.c,v 1.56 2004/01/25 17:16:05 zorglub Exp $
7 * Author: 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 );
167 p_input->stream.i_new_status = FORWARD_S;
168 p_input->stream.i_new_rate = i_rate;
170 if ( p_input->stream.i_new_rate < DEFAULT_RATE )
172 msg_Dbg( p_input, "playing at %i:1 fast forward",
173 DEFAULT_RATE / p_input->stream.i_new_rate );
175 else if ( p_input->stream.i_new_rate > DEFAULT_RATE )
177 msg_Dbg( p_input, "playing at 1:%i slow motion",
178 p_input->stream.i_new_rate / DEFAULT_RATE );
180 else if ( p_input->stream.i_new_rate == DEFAULT_RATE )
182 p_input->stream.i_new_status = PLAYING_S;
183 msg_Dbg( p_input, "playing at normal rate" );
186 vlc_cond_signal( &p_input->stream.stream_wait );
187 vlc_mutex_unlock( &p_input->stream.stream_lock );
189 vlc_object_release( p_input );
192 /*****************************************************************************
193 * input_Seek: changes the stream postion
194 *****************************************************************************/
195 void __input_Seek( vlc_object_t * p_this, off_t i_position, int i_whence )
197 input_thread_t *p_input;
199 char psz_time1[MSTRTIME_MAX_SIZE];
200 char psz_time2[MSTRTIME_MAX_SIZE];
202 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
204 if( p_input == NULL )
206 msg_Err( p_this, "no input found" );
210 vlc_mutex_lock( &p_input->stream.stream_lock );
212 #define A p_input->stream.p_selected_area
213 switch( i_whence & 0x30 )
215 case INPUT_SEEK_SECONDS:
216 i_position *= (off_t)50 * p_input->stream.i_mux_rate;
219 case INPUT_SEEK_PERCENT:
220 i_position = A->i_size * i_position / (off_t)100;
223 case INPUT_SEEK_BYTES:
228 switch( i_whence & 0x03 )
231 A->i_seek = A->i_tell + i_position;
235 A->i_seek = A->i_size + i_position;
240 A->i_seek = i_position;
248 else if( A->i_seek > A->i_size )
250 A->i_seek = A->i_size;
253 msg_Dbg( p_input, "seeking position "I64Fd"/"I64Fd" (%s/%s)",
254 A->i_seek, A->i_size,
255 input_OffsetToTime( p_input, psz_time1, i_position ),
256 input_OffsetToTime( p_input, psz_time2, A->i_size ) );
259 vlc_cond_signal( &p_input->stream.stream_wait );
260 vlc_mutex_unlock( &p_input->stream.stream_lock );
262 vlc_object_release( p_input );
265 /*****************************************************************************
266 * input_Tell: requests the stream postion
267 *****************************************************************************/
268 void __input_Tell( vlc_object_t * p_this, stream_position_t * p_position )
270 input_thread_t *p_input;
272 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
274 if( p_input == NULL )
276 p_position->i_tell = 0;
277 p_position->i_size = 0;
278 p_position->i_mux_rate = 0;
279 msg_Err( p_this, "no input found" );
283 vlc_mutex_lock( &p_input->stream.stream_lock );
285 #define A p_input->stream.p_selected_area
286 p_position->i_tell = A->i_tell;
287 p_position->i_size = A->i_size;
288 p_position->i_mux_rate = p_input->stream.i_mux_rate;
291 vlc_mutex_unlock( &p_input->stream.stream_lock );
292 vlc_object_release( p_input );
295 /*****************************************************************************
296 * input_OffsetToTime : converts an off_t value to a time indicator, using
298 *****************************************************************************
299 * BEWARE : this function assumes that you already own the lock on
300 * p_input->stream.stream_lock
301 *****************************************************************************/
302 char * input_OffsetToTime( input_thread_t * p_input, char * psz_buffer,
307 if( p_input->stream.i_mux_rate )
309 i_seconds = i_offset / 50 / p_input->stream.i_mux_rate;
310 return secstotimestr( psz_buffer, i_seconds );
314 /* Divide by zero is not my friend. */
315 sprintf( psz_buffer, "-:--:--" );
316 return( psz_buffer );
320 /*****************************************************************************
321 * input_DumpStream: dumps the contents of a stream descriptor
322 *****************************************************************************
323 * BEWARE : this function assumes that you already own the lock on
324 * p_input->stream.stream_lock
325 *****************************************************************************/
326 void input_DumpStream( input_thread_t * p_input )
328 char psz_time1[MSTRTIME_MAX_SIZE];
329 char psz_time2[MSTRTIME_MAX_SIZE];
332 #define S p_input->stream
333 msg_Dbg( p_input, "dumping stream ID 0x%x [OK:%ld/D:%ld]", S.i_stream_id,
334 S.c_packets_read, S.c_packets_trashed );
336 msg_Dbg( p_input, "seekable stream, position: "I64Fd"/"I64Fd" (%s/%s)",
337 S.p_selected_area->i_tell, S.p_selected_area->i_size,
338 input_OffsetToTime( p_input, psz_time1,
339 S.p_selected_area->i_tell ),
340 input_OffsetToTime( p_input, psz_time2,
341 S.p_selected_area->i_size ) );
343 msg_Dbg( p_input, "pace %scontrolled", S.b_pace_control ? "" : "un-" );
345 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
347 #define P p_input->stream.pp_programs[i]
348 msg_Dbg( p_input, "dumping program 0x%x, version %d (%s)",
349 P->i_number, P->i_version,
350 P->b_is_ok ? "complete" : "partial" );
352 for( j = 0; j < p_input->stream.pp_programs[i]->i_es_number; j++ )
354 #define ES p_input->stream.pp_programs[i]->pp_es[j]
355 msg_Dbg( p_input, "ES 0x%x, "
356 "stream 0x%x, fourcc `%4.4s', %s [OK:%ld/ERR:%ld]",
357 ES->i_id, ES->i_stream_id, (char*)&ES->i_fourcc,
358 ES->p_dec != NULL ? "selected" : "not selected",
359 ES->c_packets, ES->c_invalid_packets );
365 /*****************************************************************************
366 * input_ToggleES: answers to a user request with calls to (Un)SelectES
367 *****************************************************************************
368 * Useful since the interface plugins know p_es.
369 * It only works for audio & spu ( to be sure nothing nasty is being done ).
370 * b_select is a boolean to know if we have to select or unselect ES
371 *****************************************************************************/
372 int input_ToggleES( input_thread_t * p_input, es_descriptor_t * p_es,
373 vlc_bool_t b_select )
375 vlc_mutex_lock( &p_input->stream.stream_lock );
381 p_input->stream.p_newly_selected_es = p_es;
385 p_input->stream.p_removed_es = p_es;
389 vlc_mutex_unlock( &p_input->stream.stream_lock );
394 /****************************************************************************
395 * input_ChangeArea: interface request an area change
396 ****************************************************************************/
397 int input_ChangeArea( input_thread_t * p_input, input_area_t * p_area )
399 vlc_mutex_lock( &p_input->stream.stream_lock );
401 p_input->stream.p_new_area = p_area;
403 vlc_mutex_unlock( &p_input->stream.stream_lock );
408 /****************************************************************************
409 * input_ChangeProgram: interface request a program change
410 ****************************************************************************/
411 int input_ChangeProgram( input_thread_t * p_input, uint16_t i_program_number )
413 pgrm_descriptor_t * p_program;
416 vlc_mutex_lock( &p_input->stream.stream_lock );
418 p_program = input_FindProgram( p_input, i_program_number );
420 if ( p_program == NULL )
422 msg_Err( p_input, "could not find selected program" );
426 p_input->stream.p_new_program = p_program;
428 vlc_mutex_unlock( &p_input->stream.stream_lock );
430 /* Update the navigation variables without triggering a callback */
431 val.i_int = i_program_number;
432 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
437 /****************************************************************************
438 * input_ToggleGrayscale: change to grayscale or color output
439 ****************************************************************************/
440 int input_ToggleGrayscale( input_thread_t * p_input )
442 /* No need to warn the input thread since only the decoders and outputs
444 vlc_mutex_lock( &p_input->stream.control.control_lock );
445 p_input->stream.control.b_grayscale =
446 !p_input->stream.control.b_grayscale;
448 msg_Dbg( p_input, "changing to %s output",
449 p_input->stream.control.b_grayscale ? "grayscale" : "color" );
451 vlc_mutex_unlock( &p_input->stream.control.control_lock );
456 /****************************************************************************
457 * input_ToggleMute: activate/deactivate mute mode
458 ****************************************************************************/
459 int input_ToggleMute( input_thread_t * p_input )
461 /* We need to feed the decoders with 0, and only input can do that, so
462 * pass the message to the input thread. */
463 vlc_mutex_lock( &p_input->stream.stream_lock );
464 p_input->stream.b_new_mute = !p_input->stream.control.b_mute;
466 msg_Dbg( p_input, "%s mute mode",
467 p_input->stream.control.b_mute ? "activating" : "deactivating" );
469 vlc_mutex_unlock( &p_input->stream.stream_lock );