* src/input/input_ext-intf.c : added an input_SetRate function; it is
[vlc.git] / src / input / input_ext-intf.c
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.50 2003/05/31 12:24:39 titer Exp $
6  *
7  * Authors: 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 * 8 <= 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    
168     p_input->stream.i_new_status = FORWARD_S;
169     p_input->stream.i_new_rate = i_rate;
170
171     if ( p_input->stream.i_new_rate < DEFAULT_RATE )
172     {
173         msg_Dbg( p_input, "playing at %i:1 fast forward",
174              DEFAULT_RATE / p_input->stream.i_new_rate );
175     }
176     else if ( p_input->stream.i_new_rate > DEFAULT_RATE )
177     {
178         msg_Dbg( p_input, "playing at 1:%i slow motion",
179               p_input->stream.i_new_rate / DEFAULT_RATE );
180     }
181     else if ( p_input->stream.i_new_rate == DEFAULT_RATE )
182     {
183         p_input->stream.i_new_status = PLAYING_S;
184         msg_Dbg( p_input, "playing at normal rate" );
185     }
186
187     vlc_cond_signal( &p_input->stream.stream_wait );
188     vlc_mutex_unlock( &p_input->stream.stream_lock );
189
190     vlc_object_release( p_input );
191 }
192
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 )
197 {
198     input_thread_t *p_input;
199
200     char psz_time1[OFFSETTOTIME_MAX_SIZE];
201     char psz_time2[OFFSETTOTIME_MAX_SIZE];
202
203     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
204
205     if( p_input == NULL )
206     {
207         msg_Err( p_this, "no input found" );
208         return;
209     }
210
211     vlc_mutex_lock( &p_input->stream.stream_lock );
212
213 #define A p_input->stream.p_selected_area
214     switch( i_whence & 0x30 )
215     {
216         case INPUT_SEEK_SECONDS:
217             i_position *= (off_t)50 * p_input->stream.i_mux_rate;
218             break;
219
220         case INPUT_SEEK_PERCENT:
221             i_position = A->i_size * i_position / (off_t)100;
222             break;
223
224         case INPUT_SEEK_BYTES:
225         default:
226             break;
227     }
228
229     switch( i_whence & 0x03 )
230     {
231         case INPUT_SEEK_CUR:
232             A->i_seek = A->i_tell + i_position;
233             break;
234
235         case INPUT_SEEK_END:
236             A->i_seek = A->i_size + i_position;
237             break;
238
239         case INPUT_SEEK_SET:
240         default:
241             A->i_seek = i_position;
242             break;
243     }
244
245     if( A->i_seek < 0 )
246     {
247         A->i_seek = 0;
248     }
249     else if( A->i_seek > A->i_size )
250     {
251         A->i_seek = A->i_size;
252     }
253
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 ) );
258 #undef A
259
260     vlc_cond_signal( &p_input->stream.stream_wait );
261     vlc_mutex_unlock( &p_input->stream.stream_lock );
262
263     vlc_object_release( p_input );
264 }
265
266 /*****************************************************************************
267  * input_Tell: requests the stream postion
268  *****************************************************************************/
269 void __input_Tell( vlc_object_t * p_this, stream_position_t * p_position )
270 {
271     input_thread_t *p_input;
272
273     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
274
275     if( p_input == NULL )
276     {
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" );
281         return;
282     }
283
284     vlc_mutex_lock( &p_input->stream.stream_lock );
285
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;
290 #undef A
291
292     vlc_mutex_unlock( &p_input->stream.stream_lock );
293     vlc_object_release( p_input );
294 }
295
296 /*****************************************************************************
297  * input_OffsetToTime : converts an off_t value to a time indicator, using
298  *                      mux_rate
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,
304                            off_t i_offset )
305 {
306     mtime_t         i_seconds;
307
308     if( p_input->stream.i_mux_rate )
309     {
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 );
316     }
317     else
318     {
319         /* Divide by zero is not my friend. */
320         sprintf( psz_buffer, "-:--:--" );
321         return( psz_buffer );
322     }
323 }
324
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 )
332 {
333     char psz_time1[OFFSETTOTIME_MAX_SIZE];
334     char psz_time2[OFFSETTOTIME_MAX_SIZE];
335     unsigned int i, j;
336
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 );
340     if( S.b_seekable )
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 ) );
347     else
348         msg_Dbg( p_input, "pace %scontrolled", S.b_pace_control ? "" : "un-" );
349 #undef S
350     for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
351     {
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" );
356 #undef P
357         for( j = 0; j < p_input->stream.pp_programs[i]->i_es_number; j++ )
358         {
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 );
365 #undef ES
366         }
367     }
368 }
369
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 )
379 {
380     vlc_mutex_lock( &p_input->stream.stream_lock );
381
382     if( p_es != NULL )
383     {
384         if( b_select )
385         {
386             p_input->stream.p_newly_selected_es = p_es;
387         }
388         else
389         {
390             p_input->stream.p_removed_es = p_es;
391         }
392     }
393
394     vlc_mutex_unlock( &p_input->stream.stream_lock );
395
396     return 0;
397 }
398
399 /****************************************************************************
400  * input_ChangeArea: interface request an area change
401  ****************************************************************************/
402 int input_ChangeArea( input_thread_t * p_input, input_area_t * p_area )
403 {
404     vlc_mutex_lock( &p_input->stream.stream_lock );
405
406     p_input->stream.p_new_area = p_area;
407
408     vlc_mutex_unlock( &p_input->stream.stream_lock );
409
410     return 0;
411 }
412
413 /****************************************************************************
414  * input_ChangeProgram: interface request a program change
415  ****************************************************************************/
416 int input_ChangeProgram( input_thread_t * p_input, uint16_t i_program_number )
417 {
418     pgrm_descriptor_t *       p_program;
419     vlc_value_t val;
420
421     vlc_mutex_lock( &p_input->stream.stream_lock );
422
423     p_program = input_FindProgram( p_input, i_program_number );
424
425     if ( p_program == NULL )
426     {
427         msg_Err( p_input, "could not find selected program" );
428         return -1;
429     }
430
431     p_input->stream.p_new_program = p_program;
432
433     vlc_mutex_unlock( &p_input->stream.stream_lock );
434
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 );
438
439     return 0;
440 }
441
442 /****************************************************************************
443  * input_ToggleGrayscale: change to grayscale or color output
444  ****************************************************************************/
445 int input_ToggleGrayscale( input_thread_t * p_input )
446 {
447     /* No need to warn the input thread since only the decoders and outputs
448      * worry about it. */
449     vlc_mutex_lock( &p_input->stream.control.control_lock );
450     p_input->stream.control.b_grayscale =
451                     !p_input->stream.control.b_grayscale;
452
453     msg_Dbg( p_input, "changing to %s output",
454              p_input->stream.control.b_grayscale ? "grayscale" : "color" );
455
456     vlc_mutex_unlock( &p_input->stream.control.control_lock );
457
458     return 0;
459 }
460
461 /****************************************************************************
462  * input_ToggleMute: activate/deactivate mute mode
463  ****************************************************************************/
464 int input_ToggleMute( input_thread_t * p_input )
465 {
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;
470
471     msg_Dbg( p_input, "%s mute mode",
472              p_input->stream.control.b_mute ? "activating" : "deactivating" );
473
474     vlc_mutex_unlock( &p_input->stream.stream_lock );
475
476     return 0;
477 }
478