]> git.sesse.net Git - vlc/blob - src/input/input_ext-intf.c
* input/input_dec.c: we automaticaly switch to minimize thread mode
[vlc] / src / input / input_ext-intf.c
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 $
6  *
7  * Author: Christophe Massiot <massiot@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <string.h>                                    /* memcpy(), memset() */
28
29 #include <vlc/vlc.h>
30
31 #include "stream_control.h"
32 #include "input_ext-dec.h"
33 #include "input_ext-intf.h"
34 #include "input_ext-plugins.h"
35
36 /*****************************************************************************
37  * input_SetStatus: change the reading status
38  *****************************************************************************/
39 void __input_SetStatus( vlc_object_t * p_this, int i_mode )
40 {
41     input_thread_t *p_input;
42
43     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
44
45     if( p_input == NULL )
46     {
47         msg_Err( p_this, "no input found" );
48         return;
49     }
50
51     vlc_mutex_lock( &p_input->stream.stream_lock );
52
53     switch( i_mode )
54     {
55     case INPUT_STATUS_END:
56         p_input->stream.i_new_status = PLAYING_S;
57         p_input->b_eof = 1;
58         msg_Dbg( p_input, "end of stream" );
59         break;
60
61     case INPUT_STATUS_PLAY:
62         p_input->stream.i_new_status = PLAYING_S;
63         msg_Dbg( p_input, "playing at normal rate" );
64         break;
65
66     case INPUT_STATUS_PAUSE:
67         /* XXX: we don't need to check i_status, because input_clock.c
68          * does it for us */
69         p_input->stream.i_new_status = PAUSE_S;
70         msg_Dbg( p_input, "toggling pause" );
71         break;
72
73     case INPUT_STATUS_FASTER:
74         if( p_input->stream.control.i_rate * 4 <= DEFAULT_RATE )
75         {
76             msg_Dbg( p_input, "can not play any faster" );
77         }
78         else
79         {
80             p_input->stream.i_new_status = FORWARD_S;
81             p_input->stream.i_new_rate =
82                                     p_input->stream.control.i_rate / 2;
83
84             if ( p_input->stream.i_new_rate < DEFAULT_RATE )
85             {
86                 msg_Dbg( p_input, "playing at %i:1 fast forward",
87                      DEFAULT_RATE / p_input->stream.i_new_rate );
88             }
89             else if ( p_input->stream.i_new_rate > DEFAULT_RATE )
90             {
91                 msg_Dbg( p_input, "playing at 1:%i slow motion",
92                       p_input->stream.i_new_rate / DEFAULT_RATE );
93             }
94             else if ( p_input->stream.i_new_rate == DEFAULT_RATE )
95             {
96                 p_input->stream.i_new_status = PLAYING_S;
97                 msg_Dbg( p_input, "playing at normal rate" );
98             }
99         }
100         break;
101
102     case INPUT_STATUS_SLOWER:
103         if( p_input->stream.control.i_rate >= 8 * DEFAULT_RATE )
104         {
105             msg_Dbg( p_input, "can not play any slower" );
106         }
107         else
108         {
109             p_input->stream.i_new_status = FORWARD_S;
110             p_input->stream.i_new_rate =
111                                     p_input->stream.control.i_rate * 2;
112
113             if ( p_input->stream.i_new_rate < DEFAULT_RATE )
114             {
115                 msg_Dbg( p_input, "playing at %i:1 fast forward",
116                      DEFAULT_RATE / p_input->stream.i_new_rate );
117             }
118             else if ( p_input->stream.i_new_rate > DEFAULT_RATE )
119             {
120                 msg_Dbg( p_input, "playing at 1:%i slow motion",
121                       p_input->stream.i_new_rate / DEFAULT_RATE );
122             }
123             else if ( p_input->stream.i_new_rate == DEFAULT_RATE )
124             {
125                 p_input->stream.i_new_status = PLAYING_S;
126                 msg_Dbg( p_input, "playing at normal rate" );
127             }
128         }
129         break;
130
131     default:
132         break;
133     }
134
135     vlc_cond_signal( &p_input->stream.stream_wait );
136     vlc_mutex_unlock( &p_input->stream.stream_lock );
137
138     vlc_object_release( p_input );
139 }
140
141 void __input_SetRate( vlc_object_t * p_this, int i_rate )
142 {
143     input_thread_t *p_input;
144
145     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
146
147     if( p_input == NULL )
148     {
149         msg_Err( p_this, "no input found" );
150         return;
151     }
152
153     vlc_mutex_lock( &p_input->stream.stream_lock );
154
155     if( i_rate * 8 < DEFAULT_RATE )
156     {
157         msg_Dbg( p_input, "can not play faster than 8x" );
158         vlc_mutex_unlock( &p_input->stream.stream_lock );
159         return;
160     }
161     if( i_rate > DEFAULT_RATE * 8 )
162     {
163         msg_Dbg( p_input, "can not play slower than 1/8x" );
164         vlc_mutex_unlock( &p_input->stream.stream_lock );
165         return;
166     }
167     p_input->stream.i_new_status = FORWARD_S;
168     p_input->stream.i_new_rate = i_rate;
169
170     if ( p_input->stream.i_new_rate < DEFAULT_RATE )
171     {
172         msg_Dbg( p_input, "playing at %i:1 fast forward",
173              DEFAULT_RATE / p_input->stream.i_new_rate );
174     }
175     else if ( p_input->stream.i_new_rate > DEFAULT_RATE )
176     {
177         msg_Dbg( p_input, "playing at 1:%i slow motion",
178               p_input->stream.i_new_rate / DEFAULT_RATE );
179     }
180     else if ( p_input->stream.i_new_rate == DEFAULT_RATE )
181     {
182         p_input->stream.i_new_status = PLAYING_S;
183         msg_Dbg( p_input, "playing at normal rate" );
184     }
185
186     vlc_cond_signal( &p_input->stream.stream_wait );
187     vlc_mutex_unlock( &p_input->stream.stream_lock );
188
189     vlc_object_release( p_input );
190 }
191
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 )
196 {
197     input_thread_t *p_input;
198
199     char psz_time1[MSTRTIME_MAX_SIZE];
200     char psz_time2[MSTRTIME_MAX_SIZE];
201
202     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
203
204     if( p_input == NULL )
205     {
206         msg_Err( p_this, "no input found" );
207         return;
208     }
209
210     vlc_mutex_lock( &p_input->stream.stream_lock );
211
212 #define A p_input->stream.p_selected_area
213     switch( i_whence & 0x30 )
214     {
215         case INPUT_SEEK_SECONDS:
216             i_position *= (off_t)50 * p_input->stream.i_mux_rate;
217             break;
218
219         case INPUT_SEEK_PERCENT:
220             i_position = A->i_size * i_position / (off_t)100;
221             break;
222
223         case INPUT_SEEK_BYTES:
224         default:
225             break;
226     }
227
228     switch( i_whence & 0x03 )
229     {
230         case INPUT_SEEK_CUR:
231             A->i_seek = A->i_tell + i_position;
232             break;
233
234         case INPUT_SEEK_END:
235             A->i_seek = A->i_size + i_position;
236             break;
237
238         case INPUT_SEEK_SET:
239         default:
240             A->i_seek = i_position;
241             break;
242     }
243
244     if( A->i_seek < 0 )
245     {
246         A->i_seek = 0;
247     }
248     else if( A->i_seek > A->i_size )
249     {
250         A->i_seek = A->i_size;
251     }
252
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 ) );
257 #undef A
258
259     vlc_cond_signal( &p_input->stream.stream_wait );
260     vlc_mutex_unlock( &p_input->stream.stream_lock );
261
262     vlc_object_release( p_input );
263 }
264
265 /*****************************************************************************
266  * input_Tell: requests the stream postion
267  *****************************************************************************/
268 void __input_Tell( vlc_object_t * p_this, stream_position_t * p_position )
269 {
270     input_thread_t *p_input;
271
272     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
273
274     if( p_input == NULL )
275     {
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" );
280         return;
281     }
282
283     vlc_mutex_lock( &p_input->stream.stream_lock );
284
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;
289 #undef A
290
291     vlc_mutex_unlock( &p_input->stream.stream_lock );
292     vlc_object_release( p_input );
293 }
294
295 /*****************************************************************************
296  * input_OffsetToTime : converts an off_t value to a time indicator, using
297  *                      mux_rate
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,
303                            off_t i_offset )
304 {
305     mtime_t         i_seconds;
306
307     if( p_input->stream.i_mux_rate )
308     {
309         i_seconds = i_offset / 50 / p_input->stream.i_mux_rate;
310         return secstotimestr( psz_buffer, i_seconds );
311     }
312     else
313     {
314         /* Divide by zero is not my friend. */
315         sprintf( psz_buffer, "-:--:--" );
316         return( psz_buffer );
317     }
318 }
319
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 )
327 {
328     char psz_time1[MSTRTIME_MAX_SIZE];
329     char psz_time2[MSTRTIME_MAX_SIZE];
330     unsigned int i, j;
331
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 );
335     if( S.b_seekable )
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 ) );
342     else
343         msg_Dbg( p_input, "pace %scontrolled", S.b_pace_control ? "" : "un-" );
344 #undef S
345     for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
346     {
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" );
351 #undef P
352         for( j = 0; j < p_input->stream.pp_programs[i]->i_es_number; j++ )
353         {
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 );
360 #undef ES
361         }
362     }
363 }
364
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 )
374 {
375     vlc_mutex_lock( &p_input->stream.stream_lock );
376
377     if( p_es != NULL )
378     {
379         if( b_select )
380         {
381             p_input->stream.p_newly_selected_es = p_es;
382         }
383         else
384         {
385             p_input->stream.p_removed_es = p_es;
386         }
387     }
388
389     vlc_mutex_unlock( &p_input->stream.stream_lock );
390
391     return 0;
392 }
393
394 /****************************************************************************
395  * input_ChangeArea: interface request an area change
396  ****************************************************************************/
397 int input_ChangeArea( input_thread_t * p_input, input_area_t * p_area )
398 {
399     vlc_mutex_lock( &p_input->stream.stream_lock );
400
401     p_input->stream.p_new_area = p_area;
402
403     vlc_mutex_unlock( &p_input->stream.stream_lock );
404
405     return 0;
406 }
407
408 /****************************************************************************
409  * input_ChangeProgram: interface request a program change
410  ****************************************************************************/
411 int input_ChangeProgram( input_thread_t * p_input, uint16_t i_program_number )
412 {
413     pgrm_descriptor_t *       p_program;
414     vlc_value_t val;
415
416     vlc_mutex_lock( &p_input->stream.stream_lock );
417
418     p_program = input_FindProgram( p_input, i_program_number );
419
420     if ( p_program == NULL )
421     {
422         msg_Err( p_input, "could not find selected program" );
423         return -1;
424     }
425
426     p_input->stream.p_new_program = p_program;
427
428     vlc_mutex_unlock( &p_input->stream.stream_lock );
429
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 );
433
434     return 0;
435 }
436
437 /****************************************************************************
438  * input_ToggleGrayscale: change to grayscale or color output
439  ****************************************************************************/
440 int input_ToggleGrayscale( input_thread_t * p_input )
441 {
442     /* No need to warn the input thread since only the decoders and outputs
443      * worry about it. */
444     vlc_mutex_lock( &p_input->stream.control.control_lock );
445     p_input->stream.control.b_grayscale =
446                     !p_input->stream.control.b_grayscale;
447
448     msg_Dbg( p_input, "changing to %s output",
449              p_input->stream.control.b_grayscale ? "grayscale" : "color" );
450
451     vlc_mutex_unlock( &p_input->stream.control.control_lock );
452
453     return 0;
454 }
455
456 /****************************************************************************
457  * input_ToggleMute: activate/deactivate mute mode
458  ****************************************************************************/
459 int input_ToggleMute( input_thread_t * p_input )
460 {
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;
465
466     msg_Dbg( p_input, "%s mute mode",
467              p_input->stream.control.b_mute ? "activating" : "deactivating" );
468
469     vlc_mutex_unlock( &p_input->stream.stream_lock );
470
471     return 0;
472 }
473