]> git.sesse.net Git - vlc/blob - src/audio_output/dec.c
aout_TimeReport: feedback timing from audio output to audio decoder
[vlc] / src / audio_output / dec.c
1 /*****************************************************************************
2  * dec.c : audio output API towards decoders
3  *****************************************************************************
4  * Copyright (C) 2002-2007 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <assert.h>
32
33 #include <vlc_common.h>
34
35 #include <vlc_aout.h>
36 #include <vlc_input.h>
37
38 #include "aout_internal.h"
39 #include "libvlc.h"
40
41 /**
42  * Creates an audio output
43  */
44 int aout_DecNew( audio_output_t *p_aout,
45                  const audio_sample_format_t *p_format,
46                  const audio_replay_gain_t *p_replay_gain,
47                  const aout_request_vout_t *p_request_vout )
48 {
49     /* Sanitize audio format */
50     if( p_format->i_channels > 32 )
51     {
52         msg_Err( p_aout, "too many audio channels (%u)",
53                  p_format->i_channels );
54         return -1;
55     }
56     if( p_format->i_channels <= 0 )
57     {
58         msg_Err( p_aout, "no audio channels" );
59         return -1;
60     }
61     if( p_format->i_channels != aout_FormatNbChannels( p_format ) )
62     {
63         msg_Err( p_aout, "incompatible audio channels count with layout mask" );
64         return -1;
65     }
66
67     if( p_format->i_rate > 192000 )
68     {
69         msg_Err( p_aout, "excessive audio sample frequency (%u)",
70                  p_format->i_rate );
71         return -1;
72     }
73     if( p_format->i_rate < 4000 )
74     {
75         msg_Err( p_aout, "too low audio sample frequency (%u)",
76                  p_format->i_rate );
77         return -1;
78     }
79
80     aout_input_t *p_input = calloc( 1, sizeof(aout_input_t));
81     if( !p_input )
82         return -1;
83
84     p_input->b_error = true;
85
86     memcpy( &p_input->input, p_format,
87             sizeof(audio_sample_format_t) );
88     if( p_replay_gain )
89         p_input->replay_gain = *p_replay_gain;
90
91     /* We can only be called by the decoder, so no need to lock
92      * p_input->lock. */
93     aout_owner_t *owner = aout_owner(p_aout);
94     aout_lock( p_aout );
95     assert (owner->input == NULL);
96
97     var_Destroy( p_aout, "audio-device" );
98     var_Destroy( p_aout, "audio-channels" );
99
100     /* Recreate the output using the new format. */
101     if( aout_OutputNew( p_aout, p_format ) < 0 )
102         goto error;
103
104     assert (owner->volume.mixer == NULL);
105     owner->volume.mixer = aout_MixerNew (p_aout, owner->mixer_format.i_format);
106     if (owner->volume.mixer == NULL)
107     {
108         aout_OutputDelete( p_aout );
109         goto error;
110     }
111
112     date_Init (&owner->sync.date, owner->mixer_format.i_rate, 1);
113     date_Set (&owner->sync.date, VLC_TS_INVALID);
114
115     owner->input = p_input;
116     aout_InputNew( p_aout, p_input, p_request_vout );
117     aout_unlock( p_aout );
118     return 0;
119 error:
120     aout_unlock( p_aout );
121     free( p_input );
122     return -1;
123 }
124
125 /*****************************************************************************
126  * aout_DecDelete : delete a decoder
127  *****************************************************************************/
128 void aout_DecDelete( audio_output_t * p_aout )
129 {
130     aout_owner_t *owner = aout_owner (p_aout);
131     aout_input_t *input;
132     struct audio_mixer *mixer;
133
134     aout_lock( p_aout );
135     /* Remove the input. */
136     input = owner->input;
137     aout_InputDelete (p_aout, input);
138     owner->input = NULL;
139
140     aout_OutputDelete( p_aout );
141     mixer = owner->volume.mixer;
142     owner->volume.mixer = NULL;
143     var_Destroy( p_aout, "audio-device" );
144     var_Destroy( p_aout, "audio-channels" );
145
146     aout_unlock( p_aout );
147
148     aout_MixerDelete (mixer);
149     free (input);
150 }
151
152 static void aout_CheckRestart (audio_output_t *aout)
153 {
154     aout_owner_t *owner = aout_owner (aout);
155     aout_input_t *input = owner->input;
156
157     aout_assert_locked (aout);
158
159     if (likely(!owner->need_restart))
160         return;
161     owner->need_restart = false;
162
163     /* Reinitializes the output */
164     aout_InputDelete (aout, owner->input);
165     aout_MixerDelete (owner->volume.mixer);
166     owner->volume.mixer = NULL;
167     aout_OutputDelete (aout);
168
169     if (aout_OutputNew (aout, &input->input))
170     {
171 error:
172         input->b_error = true;
173         return; /* we are officially screwed */
174     }
175
176     owner->volume.mixer = aout_MixerNew (aout, owner->mixer_format.i_format);
177     if (owner->volume.mixer == NULL)
178     {
179         aout_OutputDelete (aout);
180         goto error;
181     }
182
183     if (aout_InputNew (aout, input, &input->request_vout))
184         assert (input->b_error);
185     else
186         assert (!input->b_error);
187 }
188
189
190 /*
191  * Buffer management
192  */
193
194 /*****************************************************************************
195  * aout_DecNewBuffer : ask for a new empty buffer
196  *****************************************************************************/
197 block_t *aout_DecNewBuffer (audio_output_t *aout, size_t samples)
198 {
199     /* NOTE: the caller is responsible for serializing input change */
200     aout_owner_t *owner = aout_owner (aout);
201     aout_input_t *input = owner->input;
202
203     size_t length = samples * input->input.i_bytes_per_frame
204                             / input->input.i_frame_length;
205     block_t *block = block_Alloc( length );
206     if( likely(block != NULL) )
207     {
208         block->i_nb_samples = samples;
209         block->i_pts = block->i_length = 0;
210     }
211     return block;
212 }
213
214 /*****************************************************************************
215  * aout_DecDeleteBuffer : destroy an undecoded buffer
216  *****************************************************************************/
217 void aout_DecDeleteBuffer (audio_output_t *aout, block_t *block)
218 {
219     (void) aout;
220     aout_BufferFree (block);
221 }
222
223 /*****************************************************************************
224  * aout_DecPlay : filter & mix the decoded buffer
225  *****************************************************************************/
226 int aout_DecPlay (audio_output_t *p_aout, block_t *p_buffer, int i_input_rate)
227 {
228     aout_owner_t *owner = aout_owner (p_aout);
229     aout_input_t *p_input = owner->input;
230
231     assert( i_input_rate >= INPUT_RATE_DEFAULT / AOUT_MAX_INPUT_RATE &&
232             i_input_rate <= INPUT_RATE_DEFAULT * AOUT_MAX_INPUT_RATE );
233     assert( p_buffer->i_pts > 0 );
234
235     p_buffer->i_length = (mtime_t)p_buffer->i_nb_samples * 1000000
236                                 / p_input->input.i_rate;
237
238     aout_lock( p_aout );
239     if( p_input->b_error )
240     {
241         aout_unlock( p_aout );
242         aout_BufferFree( p_buffer );
243         return -1;
244     }
245
246     aout_CheckRestart( p_aout );
247     aout_InputCheckAndRestart( p_aout, p_input );
248
249     /* Input */
250     p_buffer = aout_InputPlay (p_aout, p_input, p_buffer, i_input_rate,
251                                &owner->sync.date);
252     if( p_buffer != NULL )
253     {
254         date_Increment (&owner->sync.date, p_buffer->i_nb_samples);
255
256         /* Mixer */
257         float amp = owner->volume.multiplier * p_input->multiplier;
258         aout_MixerRun (owner->volume.mixer, p_buffer, amp);
259
260         /* Output */
261         aout_OutputPlay( p_aout, p_buffer );
262     }
263
264     aout_unlock( p_aout );
265     return 0;
266 }
267
268 int aout_DecGetResetLost (audio_output_t *aout)
269 {
270     aout_owner_t *owner = aout_owner (aout);
271     aout_input_t *input = owner->input;
272     int val;
273
274     aout_lock (aout);
275     val = input->i_buffer_lost;
276     input->i_buffer_lost = 0;
277     aout_unlock (aout);
278
279     return val;
280 }
281
282 void aout_DecChangePause (audio_output_t *aout, bool paused, mtime_t date)
283 {
284     aout_owner_t *owner = aout_owner (aout);
285
286     aout_lock (aout);
287     /* XXX: Should the date be offset by the pause duration instead? */
288     date_Set (&owner->sync.date, VLC_TS_INVALID);
289     aout_OutputPause (aout, paused, date);
290     aout_unlock (aout);
291 }
292
293 void aout_DecFlush (audio_output_t *aout)
294 {
295     aout_owner_t *owner = aout_owner (aout);
296
297     aout_lock (aout);
298     date_Set (&owner->sync.date, VLC_TS_INVALID);
299     aout_OutputFlush (aout, false);
300     aout_unlock (aout);
301 }
302
303 bool aout_DecIsEmpty (audio_output_t *aout)
304 {
305     aout_owner_t *owner = aout_owner (aout);
306     mtime_t end_date;
307
308     aout_lock (aout);
309     /* FIXME: tell output to drain */
310     end_date = date_Get (&owner->sync.date);
311     aout_unlock (aout);
312     return end_date == VLC_TS_INVALID || end_date <= mdate();
313 }
314
315 /**
316  * Notifies the audio input of the drift from the requested audio
317  * playback timestamp (@ref block_t.i_pts) to the anticipated playback time
318  * as reported by the audio output hardware.
319  * Depending on the drift amplitude, the input core may ignore the drift
320  * trigger upsampling or downsampling, or even discard samples.
321  * Future VLC versions may instead adjust the input decoding speed.
322  *
323  * The audio output plugin is responsible for estimating the ideal current
324  * playback time defined as follows:
325  *  ideal time = buffer timestamp - (output latency + pending buffer duration)
326  *
327  * Practically, this is the PTS (block_t.i_pts) of the current buffer minus
328  * the latency reported by the output programming interface.
329  * Computing the estimated drift directly would probably be more intuitive.
330  * However the use of an absolute time value does not introduce extra
331  * measurement errors due to the CPU scheduling jitter and clock resolution.
332  * Furthermore, the ideal while it is an abstract value, is easy for most
333  * audio output plugins to compute.
334  * The following definition is equivalent but depends on the clock time:
335  *  ideal time = real time + drift
336
337  * @note If aout_LatencyReport() is never called, the core will assume that
338  * there is no drift.
339  *
340  * @param ideal estimated ideal time as defined above.
341  */
342 void aout_TimeReport (audio_output_t *aout, mtime_t ideal)
343 {
344     mtime_t delta = mdate() - ideal /* = -drift */;
345
346     aout_assert_locked (aout);
347     if (delta < -AOUT_MAX_PTS_ADVANCE || +AOUT_MAX_PTS_DELAY < delta)
348     {
349         aout_owner_t *owner = aout_owner (aout);
350
351         msg_Warn (aout, "not synchronized (%"PRId64" us), resampling",
352                   delta);
353         if (date_Get (&owner->sync.date) != VLC_TS_INVALID)
354             date_Move (&owner->sync.date, delta);
355     }
356 }