]> git.sesse.net Git - vlc/blob - src/input/demux.c
c0bdccb0b6bdf7e19a85e6098de47f77b37412e5
[vlc] / src / input / demux.c
1 /*****************************************************************************
2  * demux.c
3  *****************************************************************************
4  * Copyright (C) 1999-2003 VideoLAN
5  * $Id: demux.c,v 1.3 2003/09/13 17:42:16 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@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 #include <stdlib.h>
25 #include <vlc/vlc.h>
26 #include <vlc/input.h>
27
28 #include "ninput.h"
29
30 int  demux_vaControl( input_thread_t *p_input, int i_query, va_list args )
31 {
32     if( p_input->pf_demux_control )
33     {
34         return p_input->pf_demux_control( p_input, i_query, args );
35     }
36     return VLC_EGENERIC;
37 }
38
39 int  demux_Control  ( input_thread_t *p_input, int i_query, ...  )
40 {
41     va_list args;
42     int     i_result;
43
44     va_start( args, i_query );
45     i_result = demux_vaControl( p_input, i_query, args );
46     va_end( args );
47
48     return i_result;
49 }
50
51 static void SeekOffset( input_thread_t *p_input, int64_t i_pos );
52
53 int  demux_vaControlDefault( input_thread_t *p_input, int i_query, va_list args )
54 {
55     int     i_ret;
56     double  f, *pf;
57     int64_t i64, *pi64;
58
59     vlc_mutex_lock( &p_input->stream.stream_lock );
60     switch( i_query )
61     {
62         case DEMUX_GET_POSITION:
63             pf = (double*)va_arg( args, double * );
64             if( p_input->stream.p_selected_area->i_size <= 0 )
65             {
66                 *pf = 0.0;
67             }
68             else
69             {
70                 *pf = (double)p_input->stream.p_selected_area->i_tell /
71                       (double)p_input->stream.p_selected_area->i_size;
72             }
73             i_ret = VLC_SUCCESS;
74             break;
75
76         case DEMUX_SET_POSITION:
77             f = (double)va_arg( args, double );
78             if( p_input->stream.b_seekable && p_input->pf_seek != NULL && f >= 0.0 && f <= 1.0 )
79             {
80                 SeekOffset( p_input, (int64_t)(f * (double)p_input->stream.p_selected_area->i_size) );
81                 i_ret = VLC_SUCCESS;
82             }
83             else
84             {
85                 i_ret = VLC_EGENERIC;
86             }
87             break;
88
89         case DEMUX_GET_TIME:
90             pi64 = (int64_t*)va_arg( args, int64_t * );
91             if( p_input->stream.i_mux_rate > 0 )
92             {
93                 *pi64 = (int64_t)1000000 *
94                         ( p_input->stream.p_selected_area->i_tell / 50 ) /
95                         p_input->stream.i_mux_rate;
96                 i_ret = VLC_SUCCESS;
97             }
98             else
99             {
100                 *pi64 = 0;
101                 i_ret = VLC_EGENERIC;
102             }
103             break;
104
105         case DEMUX_SET_TIME:
106             i64 = (int64_t)va_arg( args, int64_t );
107             if( p_input->stream.i_mux_rate > 0 &&
108                 p_input->stream.b_seekable && p_input->pf_seek != NULL && i64 >= 0 )
109             {
110                 SeekOffset( p_input, i64 * 50 *
111                                      (int64_t)p_input->stream.i_mux_rate /
112                                      (int64_t)1000000 );
113                 i_ret = VLC_SUCCESS;
114             }
115             else
116             {
117                 i_ret = VLC_EGENERIC;
118             }
119             break;
120
121         case DEMUX_GET_LENGTH:
122             pi64 = (int64_t*)va_arg( args, int64_t * );
123             if( p_input->stream.i_mux_rate > 0 )
124             {
125                 *pi64 = (int64_t)1000000 *
126                         ( p_input->stream.p_selected_area->i_size / 50 ) /
127                         p_input->stream.i_mux_rate;
128                 i_ret = VLC_SUCCESS;
129             }
130             else
131             {
132                 *pi64 = 0;
133                 i_ret = VLC_EGENERIC;
134             }
135             break;
136         case DEMUX_GET_FPS:
137             i_ret = VLC_EGENERIC;
138             break;
139
140         default:
141             msg_Err( p_input, "unknown query in demux_vaControlDefault !!!" );
142             i_ret = VLC_EGENERIC;
143             break;
144     }
145     vlc_mutex_unlock( &p_input->stream.stream_lock );
146
147     return i_ret;
148 }
149
150
151 static void SeekOffset( input_thread_t *p_input, int64_t i_pos )
152 {
153     /* Reinitialize buffer manager. */
154     input_AccessReinit( p_input );
155
156     vlc_mutex_unlock( &p_input->stream.stream_lock );
157     p_input->pf_seek( p_input, i_pos );
158     vlc_mutex_lock( &p_input->stream.stream_lock );
159 }