]> git.sesse.net Git - vlc/blob - src/input/input_ext-intf.c
6605937786e66263d96f7d4fb631674ceb62641f
[vlc] / src / input / input_ext-intf.c
1 /*****************************************************************************
2  * input_ext-intf.c: services to the interface
3  *****************************************************************************
4  * Copyright (C) 1998-2001,2003 VideoLAN
5  * $Id: input_ext-intf.c,v 1.54 2003/12/22 14:32:56 sam 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    
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[MSTRTIME_MAX_SIZE];
201     char psz_time2[MSTRTIME_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         return secstotimestr( psz_buffer, i_seconds );
312     }
313     else
314     {
315         /* Divide by zero is not my friend. */
316         sprintf( psz_buffer, "-:--:--" );
317         return( psz_buffer );
318     }
319 }
320
321 /*****************************************************************************
322  * input_DumpStream: dumps the contents of a stream descriptor
323  *****************************************************************************
324  * BEWARE : this function assumes that you already own the lock on
325  * p_input->stream.stream_lock
326  *****************************************************************************/
327 void input_DumpStream( input_thread_t * p_input )
328 {
329     char psz_time1[MSTRTIME_MAX_SIZE];
330     char psz_time2[MSTRTIME_MAX_SIZE];
331     unsigned int i, j;
332
333 #define S   p_input->stream
334     msg_Dbg( p_input, "dumping stream ID 0x%x [OK:%ld/D:%ld]", S.i_stream_id,
335              S.c_packets_read, S.c_packets_trashed );
336     if( S.b_seekable )
337         msg_Dbg( p_input, "seekable stream, position: "I64Fd"/"I64Fd" (%s/%s)",
338                  S.p_selected_area->i_tell, S.p_selected_area->i_size,
339                  input_OffsetToTime( p_input, psz_time1,
340                                      S.p_selected_area->i_tell ),
341                  input_OffsetToTime( p_input, psz_time2,
342                                      S.p_selected_area->i_size ) );
343     else
344         msg_Dbg( p_input, "pace %scontrolled", S.b_pace_control ? "" : "un-" );
345 #undef S
346     for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
347     {
348 #define P   p_input->stream.pp_programs[i]
349         msg_Dbg( p_input, "dumping program 0x%x, version %d (%s)",
350                  P->i_number, P->i_version,
351                  P->b_is_ok ? "complete" : "partial" );
352 #undef P
353         for( j = 0; j < p_input->stream.pp_programs[i]->i_es_number; j++ )
354         {
355 #define ES  p_input->stream.pp_programs[i]->pp_es[j]
356             msg_Dbg( p_input, "ES 0x%x, "
357                      "stream 0x%x, fourcc `%4.4s', %s [OK:%ld/ERR:%ld]",
358                      ES->i_id, ES->i_stream_id, (char*)&ES->i_fourcc,
359                      ES->p_dec != NULL ? "selected" : "not selected",
360                      ES->c_packets, ES->c_invalid_packets );
361 #undef ES
362         }
363     }
364 }
365
366 /*****************************************************************************
367  * input_ToggleES: answers to a user request with calls to (Un)SelectES
368  *****************************************************************************
369  * Useful since the interface plugins know p_es.
370  * It only works for audio & spu ( to be sure nothing nasty is being done ).
371  * b_select is a boolean to know if we have to select or unselect ES
372  *****************************************************************************/
373 int input_ToggleES( input_thread_t * p_input, es_descriptor_t * p_es,
374                     vlc_bool_t b_select )
375 {
376     vlc_mutex_lock( &p_input->stream.stream_lock );
377
378     if( p_es != NULL )
379     {
380         if( b_select )
381         {
382             p_input->stream.p_newly_selected_es = p_es;
383         }
384         else
385         {
386             p_input->stream.p_removed_es = p_es;
387         }
388     }
389
390     vlc_mutex_unlock( &p_input->stream.stream_lock );
391
392     return 0;
393 }
394
395 /****************************************************************************
396  * input_ChangeArea: interface request an area change
397  ****************************************************************************/
398 int input_ChangeArea( input_thread_t * p_input, input_area_t * p_area )
399 {
400     vlc_mutex_lock( &p_input->stream.stream_lock );
401
402     p_input->stream.p_new_area = p_area;
403
404     vlc_mutex_unlock( &p_input->stream.stream_lock );
405
406     return 0;
407 }
408
409 /****************************************************************************
410  * input_ChangeProgram: interface request a program change
411  ****************************************************************************/
412 int input_ChangeProgram( input_thread_t * p_input, uint16_t i_program_number )
413 {
414     pgrm_descriptor_t *       p_program;
415     vlc_value_t val;
416
417     vlc_mutex_lock( &p_input->stream.stream_lock );
418
419     p_program = input_FindProgram( p_input, i_program_number );
420
421     if ( p_program == NULL )
422     {
423         msg_Err( p_input, "could not find selected program" );
424         return -1;
425     }
426
427     p_input->stream.p_new_program = p_program;
428
429     vlc_mutex_unlock( &p_input->stream.stream_lock );
430
431     /* Update the navigation variables without triggering a callback */
432     val.i_int = i_program_number;
433     var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
434
435     return 0;
436 }
437
438 /****************************************************************************
439  * input_ToggleGrayscale: change to grayscale or color output
440  ****************************************************************************/
441 int input_ToggleGrayscale( input_thread_t * p_input )
442 {
443     /* No need to warn the input thread since only the decoders and outputs
444      * worry about it. */
445     vlc_mutex_lock( &p_input->stream.control.control_lock );
446     p_input->stream.control.b_grayscale =
447                     !p_input->stream.control.b_grayscale;
448
449     msg_Dbg( p_input, "changing to %s output",
450              p_input->stream.control.b_grayscale ? "grayscale" : "color" );
451
452     vlc_mutex_unlock( &p_input->stream.control.control_lock );
453
454     return 0;
455 }
456
457 /****************************************************************************
458  * input_ToggleMute: activate/deactivate mute mode
459  ****************************************************************************/
460 int input_ToggleMute( input_thread_t * p_input )
461 {
462     /* We need to feed the decoders with 0, and only input can do that, so
463      * pass the message to the input thread. */
464     vlc_mutex_lock( &p_input->stream.stream_lock );
465     p_input->stream.b_new_mute = !p_input->stream.control.b_mute;
466
467     msg_Dbg( p_input, "%s mute mode",
468              p_input->stream.control.b_mute ? "activating" : "deactivating" );
469
470     vlc_mutex_unlock( &p_input->stream.stream_lock );
471
472     return 0;
473 }
474