]> git.sesse.net Git - vlc/blob - src/input/input_clock.c
* Added 2 fields, p_selected_program and p_new_program, to the
[vlc] / src / input / input_clock.c
1 /*****************************************************************************
2  * input_clock.c: Clock/System date convertions, stream management
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: input_clock.c,v 1.25 2001/12/05 03:31:04 jobi 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 "config.h"
33 #include "common.h"
34 #include "intf_msg.h"
35 #include "threads.h"
36 #include "mtime.h"
37
38 #include "stream_control.h"
39 #include "input_ext-intf.h"
40 #include "input_ext-dec.h"
41 #include "input_ext-plugins.h"
42
43 /*
44  * DISCUSSION : SYNCHRONIZATION METHOD
45  *
46  * In some cases we can impose the pace of reading (when reading from a
47  * file or a pipe), and for the synchronization we simply sleep() until
48  * it is time to deliver the packet to the decoders. When reading from
49  * the network, we must be read at the same pace as the server writes,
50  * otherwise the kernel's buffer will trash packets. The risk is now to
51  * overflow the input buffers in case the server goes too fast, that is
52  * why we do these calculations :
53  *
54  * We compute a mean for the pcr because we want to eliminate the
55  * network jitter and keep the low frequency variations. The mean is
56  * in fact a low pass filter and the jitter is a high frequency signal
57  * that is why it is eliminated by the filter/average.
58  *
59  * The low frequency variations enable us to synchronize the client clock
60  * with the server clock because they represent the time variation between
61  * the 2 clocks. Those variations (ie the filtered pcr) are used to compute
62  * the presentation dates for the audio and video frames. With those dates
63  * we can decode (or trash) the MPEG2 stream at "exactly" the same rate
64  * as it is sent by the server and so we keep the synchronization between
65  * the server and the client.
66  *
67  * It is a very important matter if you want to avoid underflow or overflow
68  * in all the FIFOs, but it may be not enough.
69  */
70
71 /*****************************************************************************
72  * Constants
73  *****************************************************************************/
74
75 /* Maximum number of samples used to compute the dynamic average value.
76  * We use the following formula :
77  * new_average = (old_average * c_average + new_sample_value) / (c_average +1)
78  */
79 #define CR_MAX_AVERAGE_COUNTER 40
80
81 /* Maximum gap allowed between two CRs. */
82 #define CR_MAX_GAP 1000000
83
84 /*****************************************************************************
85  * ClockToSysdate: converts a movie clock to system date
86  *****************************************************************************/
87 static void ClockNewRef( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm,
88                          mtime_t i_clock, mtime_t i_sysdate );
89 static mtime_t ClockToSysdate( input_thread_t * p_input,
90                                pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
91 {
92     mtime_t     i_sysdate = 0;
93
94     if( p_pgrm->i_synchro_state == SYNCHRO_OK )
95     {
96         i_sysdate = (mtime_t)(i_clock - p_pgrm->cr_ref) 
97                         * (mtime_t)p_input->stream.control.i_rate
98                         * (mtime_t)300;
99         i_sysdate /= 27;
100         i_sysdate /= 1000;
101         i_sysdate += (mtime_t)p_pgrm->sysdate_ref;
102     }
103
104     return( i_sysdate );
105 }
106
107 /*****************************************************************************
108  * ClockCurrent: converts current system date to clock units
109  *****************************************************************************
110  * Caution : the synchro state must be SYNCHRO_OK for this to operate.
111  *****************************************************************************/
112 static mtime_t ClockCurrent( input_thread_t * p_input,
113                              pgrm_descriptor_t * p_pgrm )
114 {
115     return( (mdate() - p_pgrm->sysdate_ref) * 27 * DEFAULT_RATE
116              / p_input->stream.control.i_rate / 300
117              + p_pgrm->cr_ref );
118 }
119
120 /*****************************************************************************
121  * ClockNewRef: writes a new clock reference
122  *****************************************************************************/
123 static void ClockNewRef( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm,
124                          mtime_t i_clock, mtime_t i_sysdate )
125 {
126     p_pgrm->cr_ref = i_clock;
127     /* this is actually a kludge, but it gives better results when scr
128     * is zero in DVDs: we are 3-4 ms in advance instead of sometimes
129     * 100ms late  */
130     p_pgrm->sysdate_ref = ( p_pgrm->last_syscr && !i_clock )
131                           ? p_pgrm->last_syscr
132                           : i_sysdate ;
133 }
134
135 /*****************************************************************************
136  * input_ClockInit: reinitializes the clock reference after a stream
137  *                  discontinuity
138  *****************************************************************************/
139 void input_ClockInit( pgrm_descriptor_t * p_pgrm )
140 {
141     p_pgrm->last_cr = 0;
142     p_pgrm->last_syscr = 0;
143     p_pgrm->cr_ref = 0;
144     p_pgrm->sysdate_ref = 0;
145     p_pgrm->delta_cr = 0;
146     p_pgrm->c_average_count = 0;
147 }
148
149 /*****************************************************************************
150  * input_ClockManageControl: handles the messages from the interface
151  *****************************************************************************
152  * Returns UNDEF_S if nothing happened, PAUSE_S if the stream was paused
153  *****************************************************************************/
154 int input_ClockManageControl( input_thread_t * p_input,
155                                pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
156 {
157     int i_return_value = UNDEF_S;
158
159     vlc_mutex_lock( &p_input->stream.stream_lock );
160
161     if( p_input->stream.i_new_status == PAUSE_S )
162     {
163         int i_old_status;
164         vlc_mutex_lock( &p_input->stream.control.control_lock );
165         i_old_status = p_input->stream.control.i_status;
166
167         p_input->stream.control.i_status = PAUSE_S;
168         vlc_cond_wait( &p_input->stream.stream_wait,
169                        &p_input->stream.stream_lock );
170         p_pgrm->last_syscr = 0;
171         ClockNewRef( p_input, p_pgrm, i_clock, mdate() );
172
173         if( p_input->stream.i_new_status == PAUSE_S )
174         { 
175             /* PAUSE_S undoes the pause state: Return to old state. */
176             p_input->stream.control.i_status = i_old_status;
177             p_input->stream.i_new_status = UNDEF_S;
178             p_input->stream.i_new_rate = UNDEF_S;
179         }
180
181         /* We handle i_new_status != PAUSE_S below... */
182         vlc_mutex_unlock( &p_input->stream.control.control_lock );
183
184         i_return_value = PAUSE_S;
185     }
186
187     if( p_input->stream.i_new_status != UNDEF_S )
188     {
189         vlc_mutex_lock( &p_input->stream.control.control_lock );
190
191         p_input->stream.control.i_status = p_input->stream.i_new_status;
192
193         ClockNewRef( p_input, p_pgrm, i_clock,
194                      ClockToSysdate( p_input, p_pgrm, i_clock ) );
195
196         if( p_input->stream.control.i_status == PLAYING_S )
197         {
198             p_input->stream.control.i_rate = DEFAULT_RATE;
199             p_input->stream.control.b_mute = 0;
200         }
201         else
202         {
203             p_input->stream.control.i_rate = p_input->stream.i_new_rate;
204             p_input->stream.control.b_mute = 1;
205
206             /* Feed the audio decoders with a NULL packet to avoid
207              * discontinuities. */
208             input_EscapeAudioDiscontinuity( p_input );
209         }
210
211         p_input->stream.i_new_status = UNDEF_S;
212         p_input->stream.i_new_rate = UNDEF_S;
213
214         vlc_mutex_unlock( &p_input->stream.control.control_lock );
215     }
216
217     vlc_mutex_unlock( &p_input->stream.stream_lock );
218
219     return( i_return_value );
220 }
221
222 /*****************************************************************************
223  * input_ClockManageRef: manages a clock reference
224  *****************************************************************************/
225 void input_ClockManageRef( input_thread_t * p_input,
226                            pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
227 {
228     if( ( p_pgrm->i_synchro_state != SYNCHRO_OK ) || ( i_clock == 0 ) )
229     {
230         /* Feed synchro with a new reference point. */
231         ClockNewRef( p_input, p_pgrm, i_clock, mdate() );
232         p_pgrm->i_synchro_state = SYNCHRO_OK;
233
234         if( p_input->stream.b_pace_control
235              && p_input->stream.p_selected_program == p_pgrm )
236         {
237             p_pgrm->last_cr = i_clock;
238             mwait( ClockToSysdate( p_input, p_pgrm, i_clock ) );
239         }
240         else
241         {
242             p_pgrm->last_cr = 0;
243             p_pgrm->last_syscr = 0;
244             p_pgrm->delta_cr = 0;
245             p_pgrm->c_average_count = 0;
246         }
247     }
248     else
249     {
250         if ( p_pgrm->last_cr != 0 &&
251                (    (p_pgrm->last_cr - i_clock) > CR_MAX_GAP
252                  || (p_pgrm->last_cr - i_clock) < - CR_MAX_GAP ) )
253         {
254             /* Stream discontinuity, for which we haven't received a
255              * warning from the stream control facilities (dd-edited
256              * stream ?). */
257             intf_WarnMsg( 1, "Clock gap, unexpected stream discontinuity" );
258             input_ClockInit( p_pgrm );
259             p_pgrm->i_synchro_state = SYNCHRO_START;
260             input_EscapeDiscontinuity( p_input, p_pgrm );
261         }
262
263         p_pgrm->last_cr = i_clock;
264
265         if( p_input->stream.b_pace_control
266              && p_input->stream.p_selected_program == p_pgrm )
267         {
268             /* We remember the last system date to be able to restart
269              * the synchro we statistically better continuity, after 
270              * a zero scr */
271             p_pgrm->last_syscr = ClockToSysdate( p_input, p_pgrm, i_clock );
272             
273             /* Wait a while before delivering the packets to the decoder.
274              * In case of multiple programs, we arbitrarily follow the
275              * clock of the first program. */
276             mwait( p_pgrm->last_syscr );
277
278             /* Now take into account interface changes. */
279             input_ClockManageControl( p_input, p_pgrm, i_clock );
280         }
281         else
282         {
283             /* Smooth clock reference variations. */
284             mtime_t     i_extrapoled_clock = ClockCurrent( p_input, p_pgrm );
285
286             /* Bresenham algorithm to smooth variations. */
287             if( p_pgrm->c_average_count == CR_MAX_AVERAGE_COUNTER )
288             {
289                 p_pgrm->delta_cr = ( p_pgrm->delta_cr
290                                         * (CR_MAX_AVERAGE_COUNTER - 1)
291                                       + ( i_extrapoled_clock - i_clock ) )
292                                     / CR_MAX_AVERAGE_COUNTER;
293             }
294             else
295             {
296                 p_pgrm->delta_cr = ( p_pgrm->delta_cr
297                                         * p_pgrm->c_average_count
298                                       + ( i_extrapoled_clock - i_clock ) )
299                                     / (p_pgrm->c_average_count + 1);
300                 p_pgrm->c_average_count++;
301             }
302         }
303     }
304 }
305
306 /*****************************************************************************
307  * input_ClockGetTS: manages a PTS or DTS
308  *****************************************************************************/
309 mtime_t input_ClockGetTS( input_thread_t * p_input,
310                           pgrm_descriptor_t * p_pgrm, mtime_t i_ts )
311 {
312     if( p_pgrm->i_synchro_state == SYNCHRO_OK )
313     {
314         return( ClockToSysdate( p_input, p_pgrm, i_ts + p_pgrm->delta_cr )
315                  + DEFAULT_PTS_DELAY );
316     }
317     else
318     {
319         return 0;
320     }
321 }
322