* ./src/libvlc.c: added a debug message to test the translation system.
[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.46 2002/12/25 23:39:01 sam 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 we are already going too fast, go back to default rate */
75         if( p_input->stream.control.i_rate * 8 <= DEFAULT_RATE )
76         {
77             p_input->stream.i_new_status = PLAYING_S;
78             msg_Dbg( p_input, "playing at normal rate" );
79         }
80         else
81         {
82             p_input->stream.i_new_status = FORWARD_S;
83
84             if( p_input->stream.control.i_rate < DEFAULT_RATE
85                     && p_input->stream.control.i_status == FORWARD_S )
86             {
87                 p_input->stream.i_new_rate =
88                                     p_input->stream.control.i_rate / 2;
89             }
90             else
91             {
92                 p_input->stream.i_new_rate = DEFAULT_RATE / 2;
93             }
94             msg_Dbg( p_input, "playing at %i:1 fast forward",
95                      DEFAULT_RATE / p_input->stream.i_new_rate );
96         }
97         break;
98
99     case INPUT_STATUS_SLOWER:
100         /* If we are already going too slow, go back to default rate */
101         if( p_input->stream.control.i_rate >= 8 * DEFAULT_RATE )
102         {
103             p_input->stream.i_new_status = PLAYING_S;
104             msg_Dbg( p_input, "playing at normal rate" );
105         }
106         else
107         {
108             p_input->stream.i_new_status = FORWARD_S;
109
110             if( p_input->stream.control.i_rate > DEFAULT_RATE
111                     && p_input->stream.control.i_status == FORWARD_S )
112             {
113                 p_input->stream.i_new_rate =
114                                     p_input->stream.control.i_rate * 2;
115             }
116             else
117             {
118                 p_input->stream.i_new_rate = DEFAULT_RATE * 2;
119             }
120             msg_Dbg( p_input, "playing at 1:%i slow motion",
121                       p_input->stream.i_new_rate / DEFAULT_RATE );
122         }
123         break;
124
125     default:
126         break;
127     }
128
129     vlc_cond_signal( &p_input->stream.stream_wait );
130     vlc_mutex_unlock( &p_input->stream.stream_lock );
131
132     vlc_object_release( p_input );
133 }
134
135 /*****************************************************************************
136  * input_Seek: changes the stream postion
137  *****************************************************************************/
138 void __input_Seek( vlc_object_t * p_this, off_t i_position, int i_whence )
139 {
140     input_thread_t *p_input;
141
142     char psz_time1[OFFSETTOTIME_MAX_SIZE];
143     char psz_time2[OFFSETTOTIME_MAX_SIZE];
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 #define A p_input->stream.p_selected_area
156     switch( i_whence & 0x30 )
157     {
158         case INPUT_SEEK_SECONDS:
159             i_position *= (off_t)50 * p_input->stream.i_mux_rate;
160             break;
161
162         case INPUT_SEEK_PERCENT:
163             i_position = A->i_size * i_position / (off_t)100;
164             break;
165
166         case INPUT_SEEK_BYTES:
167         default:
168             break;
169     }
170
171     switch( i_whence & 0x03 )
172     {
173         case INPUT_SEEK_CUR:
174             A->i_seek = A->i_tell + i_position;
175             break;
176
177         case INPUT_SEEK_END:
178             A->i_seek = A->i_size + i_position;
179             break;
180
181         case INPUT_SEEK_SET:
182         default:
183             A->i_seek = i_position;
184             break;
185     }
186
187     if( A->i_seek < 0 )
188     {
189         A->i_seek = 0;
190     }
191     else if( A->i_seek > A->i_size )
192     {
193         A->i_seek = A->i_size;
194     }
195
196     msg_Dbg( p_input, "seeking position "I64Fd"/"I64Fd" (%s/%s)",
197              A->i_seek, A->i_size,
198              input_OffsetToTime( p_input, psz_time1, i_position ),
199              input_OffsetToTime( p_input, psz_time2, A->i_size ) );
200 #undef A
201
202     vlc_cond_signal( &p_input->stream.stream_wait );
203     vlc_mutex_unlock( &p_input->stream.stream_lock );
204
205     vlc_object_release( p_input );
206 }
207
208 /*****************************************************************************
209  * input_Tell: requests the stream postion
210  *****************************************************************************/
211 void __input_Tell( vlc_object_t * p_this, stream_position_t * p_position )
212 {
213     input_thread_t *p_input;
214
215     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
216
217     if( p_input == NULL )
218     {
219         p_position->i_tell = 0;
220         p_position->i_size = 0;
221         p_position->i_mux_rate = 0;
222         msg_Err( p_this, "no input found" );
223         return;
224     }
225
226     vlc_mutex_lock( &p_input->stream.stream_lock );
227
228 #define A p_input->stream.p_selected_area
229     p_position->i_tell = A->i_tell;
230     p_position->i_size = A->i_size;
231     p_position->i_mux_rate = p_input->stream.i_mux_rate;
232 #undef A
233
234     vlc_mutex_unlock( &p_input->stream.stream_lock );
235     vlc_object_release( p_input );
236 }
237
238 /*****************************************************************************
239  * input_OffsetToTime : converts an off_t value to a time indicator, using
240  *                      mux_rate
241  *****************************************************************************
242  * BEWARE : this function assumes that you already own the lock on
243  * p_input->stream.stream_lock
244  *****************************************************************************/
245 char * input_OffsetToTime( input_thread_t * p_input, char * psz_buffer,
246                            off_t i_offset )
247 {
248     mtime_t         i_seconds;
249
250     if( p_input->stream.i_mux_rate )
251     {
252         i_seconds = i_offset / 50 / p_input->stream.i_mux_rate;
253         snprintf( psz_buffer, OFFSETTOTIME_MAX_SIZE, "%d:%02d:%02d",
254                  (int) (i_seconds / (60 * 60)),
255                  (int) (i_seconds / 60 % 60),
256                  (int) (i_seconds % 60) );
257         return( psz_buffer );
258     }
259     else
260     {
261         /* Divide by zero is not my friend. */
262         sprintf( psz_buffer, "-:--:--" );
263         return( psz_buffer );
264     }
265 }
266
267 /*****************************************************************************
268  * input_DumpStream: dumps the contents of a stream descriptor
269  *****************************************************************************
270  * BEWARE : this function assumes that you already own the lock on
271  * p_input->stream.stream_lock
272  *****************************************************************************/
273 void input_DumpStream( input_thread_t * p_input )
274 {
275     char psz_time1[OFFSETTOTIME_MAX_SIZE];
276     char psz_time2[OFFSETTOTIME_MAX_SIZE];
277     unsigned int i, j;
278
279 #define S   p_input->stream
280     msg_Dbg( p_input, "dumping stream ID 0x%x [OK:%ld/D:%ld]", S.i_stream_id,
281              S.c_packets_read, S.c_packets_trashed );
282     if( S.b_seekable )
283         msg_Dbg( p_input, "seekable stream, position: "I64Fd"/"I64Fd" (%s/%s)",
284                  S.p_selected_area->i_tell, S.p_selected_area->i_size,
285                  input_OffsetToTime( p_input, psz_time1,
286                                      S.p_selected_area->i_tell ),
287                  input_OffsetToTime( p_input, psz_time2,
288                                      S.p_selected_area->i_size ) );
289     else
290         msg_Dbg( p_input, "pace %scontrolled", S.b_pace_control ? "" : "un-" );
291 #undef S
292     for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
293     {
294 #define P   p_input->stream.pp_programs[i]
295         msg_Dbg( p_input, "dumping program 0x%x, version %d (%s)",
296                  P->i_number, P->i_version,
297                  P->b_is_ok ? "complete" : "partial" );
298 #undef P
299         for( j = 0; j < p_input->stream.pp_programs[i]->i_es_number; j++ )
300         {
301 #define ES  p_input->stream.pp_programs[i]->pp_es[j]
302             msg_Dbg( p_input, "ES 0x%x, "
303                      "stream 0x%x, fourcc `%4.4s', %s [OK:%ld/ERR:%ld]",
304                      ES->i_id, ES->i_stream_id, (char*)&ES->i_fourcc,
305                      ES->p_decoder_fifo != NULL ? "selected" : "not selected",
306                      ES->c_packets, ES->c_invalid_packets );
307 #undef ES
308         }
309     }
310 }
311
312 /*****************************************************************************
313  * input_ToggleES: answers to a user request with calls to (Un)SelectES
314  *****************************************************************************
315  * Useful since the interface plugins know p_es.
316  * It only works for audio & spu ( to be sure nothing nasty is being done ).
317  * b_select is a boolean to know if we have to select or unselect ES
318  *****************************************************************************/
319 int input_ToggleES( input_thread_t * p_input, es_descriptor_t * p_es,
320                     vlc_bool_t b_select )
321 {
322     vlc_mutex_lock( &p_input->stream.stream_lock );
323
324     if( p_es != NULL )
325     {
326         if( b_select )
327         {
328             p_input->stream.p_newly_selected_es = p_es;
329         }
330         else
331         {
332             p_input->stream.p_removed_es = p_es;
333         }
334     }
335
336     vlc_mutex_unlock( &p_input->stream.stream_lock );
337
338     return 0;
339 }
340
341 /****************************************************************************
342  * input_ChangeArea: interface request an area change
343  ****************************************************************************/
344 int input_ChangeArea( input_thread_t * p_input, input_area_t * p_area )
345 {
346     vlc_mutex_lock( &p_input->stream.stream_lock );
347
348     p_input->stream.p_new_area = p_area;
349
350     vlc_mutex_unlock( &p_input->stream.stream_lock );
351
352     return 0;
353 }
354
355 /****************************************************************************
356  * input_ChangeProgram: interface request a program change
357  ****************************************************************************/
358 int input_ChangeProgram( input_thread_t * p_input, uint16_t i_program_number )
359 {
360     pgrm_descriptor_t *       p_program;
361
362     vlc_mutex_lock( &p_input->stream.stream_lock );
363
364     p_program = input_FindProgram( p_input, i_program_number );
365
366     if ( p_program == NULL )
367     {
368         msg_Err( p_input, "could not find selected program" );
369         return -1;
370     }
371
372     p_input->stream.p_new_program = p_program;
373
374     vlc_mutex_unlock( &p_input->stream.stream_lock );
375
376     return 0;
377 }
378
379 /****************************************************************************
380  * input_ToggleGrayscale: change to grayscale or color output
381  ****************************************************************************/
382 int input_ToggleGrayscale( input_thread_t * p_input )
383 {
384     /* No need to warn the input thread since only the decoders and outputs
385      * worry about it. */
386     vlc_mutex_lock( &p_input->stream.control.control_lock );
387     p_input->stream.control.b_grayscale =
388                     !p_input->stream.control.b_grayscale;
389
390     msg_Dbg( p_input, "changing to %s output",
391              p_input->stream.control.b_grayscale ? "grayscale" : "color" );
392
393     vlc_mutex_unlock( &p_input->stream.control.control_lock );
394
395     return 0;
396 }
397
398 /****************************************************************************
399  * input_ToggleMute: activate/deactivate mute mode
400  ****************************************************************************/
401 int input_ToggleMute( input_thread_t * p_input )
402 {
403     /* We need to feed the decoders with 0, and only input can do that, so
404      * pass the message to the input thread. */
405     vlc_mutex_lock( &p_input->stream.stream_lock );
406     p_input->stream.b_new_mute = !p_input->stream.control.b_mute;
407
408     msg_Dbg( p_input, "%s mute mode",
409              p_input->stream.control.b_mute ? "activating" : "deactivating" );
410
411     vlc_mutex_unlock( &p_input->stream.stream_lock );
412
413     return 0;
414 }
415