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