]> git.sesse.net Git - vlc/blob - src/input/input_ext-intf.c
* COMPLETE CVS BREAKAGE !! The MAIN branch is going to be a playground
[vlc] / 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.31 2001/12/09 17:01:37 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 "defs.h"
28
29 #include <string.h>                                    /* memcpy(), memset() */
30 #include <sys/types.h>                                              /* off_t */
31
32 #include "common.h"
33 #include "intf_msg.h"
34 #include "threads.h"
35 #include "mtime.h"
36
37 #include "stream_control.h"
38 #include "input_ext-dec.h"
39 #include "input_ext-intf.h"
40 #include "input_ext-plugins.h"
41
42 /*****************************************************************************
43  * input_SetStatus: change the reading status
44  *****************************************************************************/
45 void input_SetStatus( input_thread_t * p_input, int i_mode )
46 {
47     vlc_mutex_lock( &p_input->stream.stream_lock );
48
49     switch( i_mode )
50     {
51     case INPUT_STATUS_END:
52         p_input->stream.i_new_status = PLAYING_S;
53         p_input->b_eof = 1;
54         intf_Msg( "input: end of stream" );
55         break;
56
57     case INPUT_STATUS_PLAY:
58         p_input->stream.i_new_status = PLAYING_S;
59         intf_Msg( "input: playing at normal rate" );
60         break;
61
62     case INPUT_STATUS_PAUSE:
63         /* XXX: we don't need to check i_status, because input_clock.c
64          * does it for us */
65         p_input->stream.i_new_status = PAUSE_S;
66         intf_Msg( "input: toggling pause" );
67         break;
68
69     case INPUT_STATUS_FASTER:
70         /* If we are already going too fast, go back to default rate */
71         if( p_input->stream.control.i_rate * 8 <= DEFAULT_RATE )
72         {
73             p_input->stream.i_new_status = PLAYING_S;
74             intf_Msg( "input: playing at normal rate" );
75         }
76         else
77         {
78             p_input->stream.i_new_status = FORWARD_S;
79
80             if( p_input->stream.control.i_rate < DEFAULT_RATE
81                     && p_input->stream.control.i_status == FORWARD_S )
82             {
83                 p_input->stream.i_new_rate =
84                                     p_input->stream.control.i_rate / 2;
85             }
86             else
87             {
88                 p_input->stream.i_new_rate = DEFAULT_RATE / 2;
89             }
90             intf_Msg( "input: playing at %i:1 fast forward",
91                       DEFAULT_RATE / p_input->stream.i_new_rate );
92         }
93         break;
94
95     case INPUT_STATUS_SLOWER:
96         /* If we are already going too slow, go back to default rate */
97         if( p_input->stream.control.i_rate >= 8 * DEFAULT_RATE )
98         {
99             p_input->stream.i_new_status = PLAYING_S;
100             intf_Msg( "input: playing at normal rate" );
101         }
102         else
103         {
104             p_input->stream.i_new_status = FORWARD_S;
105
106             if( p_input->stream.control.i_rate > DEFAULT_RATE
107                     && p_input->stream.control.i_status == FORWARD_S )
108             {
109                 p_input->stream.i_new_rate =
110                                     p_input->stream.control.i_rate * 2;
111             }
112             else
113             {
114                 p_input->stream.i_new_rate = DEFAULT_RATE * 2;
115             }
116             intf_Msg( "input: playing at 1:%i slow motion",
117                       p_input->stream.i_new_rate / DEFAULT_RATE );
118         }
119         break;
120
121     default:
122         break;
123     }
124
125     vlc_cond_signal( &p_input->stream.stream_wait );
126     vlc_mutex_unlock( &p_input->stream.stream_lock );
127 }
128
129 /*****************************************************************************
130  * input_Seek: changes the stream postion
131  *****************************************************************************/
132 void input_Seek( input_thread_t * p_input, off_t i_position )
133 {
134     char        psz_time1[OFFSETTOTIME_MAX_SIZE];
135     char        psz_time2[OFFSETTOTIME_MAX_SIZE];
136
137     vlc_mutex_lock( &p_input->stream.stream_lock );
138     p_input->stream.p_selected_area->i_seek = i_position;
139
140     intf_WarnMsg( 3, "input: seeking position %lld/%lld (%s/%s)", i_position,
141                   p_input->stream.p_selected_area->i_size,
142                   input_OffsetToTime( p_input, psz_time1, i_position ),
143                   input_OffsetToTime( p_input, psz_time2,
144                               p_input->stream.p_selected_area->i_size ) );
145
146     vlc_cond_signal( &p_input->stream.stream_wait );
147     vlc_mutex_unlock( &p_input->stream.stream_lock );
148 }
149
150 /*****************************************************************************
151  * input_OffsetToTime : converts an off_t value to a time indicator, using
152  *                      mux_rate
153  *****************************************************************************
154  * BEWARE : this function assumes that you already own the lock on
155  * p_input->stream.stream_lock
156  *****************************************************************************/
157 char * input_OffsetToTime( input_thread_t * p_input, char * psz_buffer,
158                            off_t i_offset )
159 {
160     mtime_t         i_seconds;
161
162     if( p_input->stream.i_mux_rate )
163     {
164         i_seconds = i_offset / 50 / p_input->stream.i_mux_rate;
165         snprintf( psz_buffer, OFFSETTOTIME_MAX_SIZE, "%d:%02d:%02d",
166                  (int) (i_seconds / (60 * 60)),
167                  (int) (i_seconds / 60 % 60),
168                  (int) (i_seconds % 60) );
169         return( psz_buffer );
170     }
171     else
172     {
173         /* Divide by zero is not my friend. */
174         sprintf( psz_buffer, "-:--:--" );
175         return( psz_buffer );
176     }
177 }
178
179 /*****************************************************************************
180  * input_DumpStream: dumps the contents of a stream descriptor
181  *****************************************************************************
182  * BEWARE : this function assumes that you already own the lock on
183  * p_input->stream.stream_lock
184  *****************************************************************************/
185 void input_DumpStream( input_thread_t * p_input )
186 {
187     int         i, j;
188     char        psz_time1[OFFSETTOTIME_MAX_SIZE];
189     char        psz_time2[OFFSETTOTIME_MAX_SIZE];
190
191 #define S   p_input->stream
192     intf_Msg( "input info: Dumping stream ID 0x%x [OK:%d/D:%d]", S.i_stream_id,
193               S.c_packets_read, S.c_packets_trashed );
194     if( S.b_seekable )
195         intf_Msg( "input info: seekable stream, position: %lld/%lld (%s/%s)",
196                   S.p_selected_area->i_tell, S.p_selected_area->i_size,
197                   input_OffsetToTime( p_input, psz_time1,
198                                       S.p_selected_area->i_tell ),
199                   input_OffsetToTime( p_input, psz_time2,
200                                       S.p_selected_area->i_size ) );
201     else
202         intf_Msg( "input info: %s", S.b_pace_control ? "pace controlled" :
203                   "pace un-controlled" );
204 #undef S
205     for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
206     {
207 #define P   p_input->stream.pp_programs[i]
208         intf_Msg( "input info: Dumping program 0x%x, version %d (%s)",
209                   P->i_number, P->i_version,
210                   P->b_is_ok ? "complete" : "partial" );
211 #undef P
212         for( j = 0; j < p_input->stream.pp_programs[i]->i_es_number; j++ )
213         {
214 #define ES  p_input->stream.pp_programs[i]->pp_es[j]
215             intf_Msg( "input info: ES 0x%x, stream 0x%x, type 0x%x, %s [OK:%d/ERR:%d]",
216                       ES->i_id, ES->i_stream_id, ES->i_type,
217                       ES->p_decoder_fifo != NULL ? "selected" : "not selected",
218                       ES->c_packets, ES->c_invalid_packets );
219 #undef ES
220         }
221     }
222 }
223
224 /*****************************************************************************
225  * input_ChangeES: answers to a user request with calls to (Un)SelectES
226  *****************************************************************************
227  * Useful since the interface plugins know p_es
228  * This functon is deprecated, use input_ToggleEs instead.
229  *****************************************************************************/
230 int input_ChangeES( input_thread_t * p_input, es_descriptor_t * p_es,
231                     u8 i_cat )
232 {
233     int                     i_index;
234     int                     i;
235
236     i_index = -1;
237
238     vlc_mutex_lock( &p_input->stream.stream_lock );
239
240     for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
241     {
242         if( p_input->stream.pp_selected_es[i]->i_cat == i_cat )
243         {
244             i_index = i;
245             break;
246         }
247     }
248
249
250     if( p_es != NULL )
251     {
252
253     
254         if( i_index != -1 )
255         {
256             
257             if( p_input->stream.pp_selected_es[i_index] != p_es )
258             {
259                 input_UnselectES( p_input,
260                                   p_input->stream.pp_selected_es[i_index] );
261                 input_SelectES( p_input, p_es );
262                 intf_WarnMsg( 3, "input info: es selected -> %s (0x%x)",
263                                                 p_es->psz_desc, p_es->i_id );
264             }
265         }
266         else
267         {
268             input_SelectES( p_input, p_es );
269             intf_WarnMsg( 3, "input info: es selected -> %s (0x%x)",
270                           p_es->psz_desc, p_es->i_id );
271         }
272     }
273     else
274     {
275         if( i_index != -1 )
276         {
277             intf_WarnMsg( 3, "input info: es unselected -> %s (0x%x)",
278                           p_input->stream.pp_selected_es[i_index]->psz_desc,
279                           p_input->stream.pp_selected_es[i_index]->i_id );
280
281             input_UnselectES( p_input,
282                               p_input->stream.pp_selected_es[i_index] );
283         }
284     }
285
286     vlc_mutex_unlock( &p_input->stream.stream_lock );
287
288     return 0;
289 }
290
291 /*****************************************************************************
292  * input_ToggleES: answers to a user request with calls to (Un)SelectES
293  *****************************************************************************
294  * Useful since the interface plugins know p_es.
295  * It only works for audio & spu ( to be sure nothing nasty is being done ).
296  * b_select is a boolean to know if we have to select or unselect ES
297  *****************************************************************************/
298 int input_ToggleES( input_thread_t * p_input, es_descriptor_t * p_es,
299                     boolean_t b_select )
300 {
301
302     vlc_mutex_lock( &p_input->stream.stream_lock );
303
304     if( p_es != NULL )
305     {
306         if( b_select )
307         {
308             p_input->stream.p_newly_selected_es = p_es;
309         }
310         else
311         {
312             p_input->stream.p_removed_es = p_es;
313         }
314     }
315
316     vlc_mutex_unlock( &p_input->stream.stream_lock );
317
318     return 0;
319 }
320
321 /****************************************************************************
322  * input_ChangeArea: interface request an area change
323  ****************************************************************************/
324 int input_ChangeArea( input_thread_t * p_input, input_area_t * p_area )
325 {
326     vlc_mutex_lock( &p_input->stream.stream_lock );
327
328     p_input->stream.p_new_area = p_area;
329
330     vlc_mutex_unlock( &p_input->stream.stream_lock );
331
332     return 0;
333 }
334
335 /****************************************************************************
336  * input_ChangeProgram: interface request an area change
337  ****************************************************************************/
338 int input_ChangeProgram( input_thread_t * p_input, 
339             pgrm_descriptor_t * p_program )
340 {
341     vlc_mutex_lock( &p_input->stream.stream_lock );
342
343     p_input->stream.p_new_program = p_program;
344
345     vlc_mutex_unlock( &p_input->stream.stream_lock );
346
347     return 0;
348 }
349
350 /****************************************************************************
351  * input_ToggleGrayscale: change to grayscale or color output
352  ****************************************************************************/
353 int input_ToggleGrayscale( input_thread_t * p_input )
354 {
355     /* No need to warn the input thread since only the decoders and outputs
356      * worry about it. */
357     vlc_mutex_lock( &p_input->stream.control.control_lock );
358     p_input->stream.control.b_grayscale =
359                     !p_input->stream.control.b_grayscale;
360
361     intf_WarnMsg( 3, "input warning: changing to %s output",
362             p_input->stream.control.b_grayscale ? "grayscale" : "color" );
363
364     vlc_mutex_unlock( &p_input->stream.control.control_lock );
365
366     return 0;
367 }
368
369 /****************************************************************************
370  * input_ToggleMute: activate/deactivate mute mode
371  ****************************************************************************/
372 int input_ToggleMute( input_thread_t * p_input )
373 {
374     /* We need to feed the decoders with 0, and only input can do that, so
375      * pass the message to the input thread. */
376     vlc_mutex_lock( &p_input->stream.stream_lock );
377     p_input->stream.b_new_mute = !p_input->stream.control.b_mute;
378
379     intf_WarnMsg( 3, "input warning: %s mute mode",
380             p_input->stream.control.b_mute ? "activating" : "deactivating" );
381
382     vlc_mutex_unlock( &p_input->stream.stream_lock );
383
384     return 0;
385 }
386
387 /****************************************************************************
388  * input_SetSMP: change the number of video decoder threads
389  ****************************************************************************/
390 int input_SetSMP( input_thread_t * p_input, int i_smp )
391 {
392     /* No need to warn the input thread since only the decoders
393      * worry about it. */
394     vlc_mutex_lock( &p_input->stream.control.control_lock );
395     p_input->stream.control.i_smp = i_smp;
396     vlc_mutex_unlock( &p_input->stream.control.control_lock );
397
398     return 0;
399 }
400