]> git.sesse.net Git - vlc/blob - src/audio_output/dec.c
2ca232475b37097adf05d844b1c8edae6e7772bb
[vlc] / src / audio_output / dec.c
1 /*****************************************************************************
2  * dec.c : audio output API towards decoders
3  *****************************************************************************
4  * Copyright (C) 2002-2007 VLC authors and VideoLAN
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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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 #include <vlc_aout.h>
35 #include <vlc_aout_mixer.h>
36 #include <vlc_input.h>
37 #include <vlc_atomic.h>
38
39 #include "aout_internal.h"
40 #include "libvlc.h"
41
42 static int ReplayGainCallback (vlc_object_t *, char const *,
43                                vlc_value_t, vlc_value_t, void *);
44
45 /**
46  * Creates an audio output
47  */
48 int aout_DecNew( audio_output_t *p_aout,
49                  const audio_sample_format_t *p_format,
50                  const audio_replay_gain_t *p_replay_gain,
51                  const aout_request_vout_t *p_request_vout )
52 {
53     /* Sanitize audio format */
54     if( p_format->i_channels != aout_FormatNbChannels( p_format ) )
55     {
56         msg_Err( p_aout, "incompatible audio channels count with layout mask" );
57         return -1;
58     }
59
60     if( p_format->i_rate > 192000 )
61     {
62         msg_Err( p_aout, "excessive audio sample frequency (%u)",
63                  p_format->i_rate );
64         return -1;
65     }
66     if( p_format->i_rate < 4000 )
67     {
68         msg_Err( p_aout, "too low audio sample frequency (%u)",
69                  p_format->i_rate );
70         return -1;
71     }
72
73     aout_owner_t *owner = aout_owner(p_aout);
74 #ifdef RECYCLE
75     /* Calling decoder is responsible for serializing aout_DecNew() and
76      * aout_DecDelete(). So no need to lock to _read_ those properties. */
77     if (owner->module != NULL) /* <- output exists */
78     {   /* Check if we can recycle the existing output and pipelines */
79         if (AOUT_FMTS_IDENTICAL(&owner->input_format, p_format))
80             return 0;
81
82         /* TODO? If the new input format is closer to the output format than
83          * the old input format was, then the output could be recycled. The
84          * input pipeline however would need to be restarted. */
85
86         /* No recycling: delete everything and restart from scratch */
87         aout_Shutdown (p_aout);
88     }
89 #endif
90     int ret = 0;
91
92     /* TODO: reduce lock scope depending on decoder's real need */
93     aout_lock( p_aout );
94     assert (owner->module == NULL);
95
96     /* Create the audio output stream */
97     var_Destroy( p_aout, "audio-device" );
98     var_Destroy( p_aout, "audio-channels" );
99
100     owner->input_format = *p_format;
101     vlc_atomic_set (&owner->restart, 0);
102     if( aout_OutputNew( p_aout, p_format ) < 0 )
103     {
104         ret = -1;
105         goto error;
106     }
107
108     /* Allocate a software mixer */
109     assert (owner->volume.mixer == NULL);
110     owner->volume.mixer = aout_MixerNew (p_aout, owner->mixer_format.i_format);
111
112     aout_ReplayGainInit (&owner->gain.data, p_replay_gain);
113     var_AddCallback (p_aout, "audio-replay-gain-mode",
114                      ReplayGainCallback, owner);
115     var_TriggerCallback (p_aout, "audio-replay-gain-mode");
116
117     /* Create the audio filtering "input" pipeline */
118     date_Init (&owner->sync.date, owner->mixer_format.i_rate, 1);
119     date_Set (&owner->sync.date, VLC_TS_INVALID);
120
121     assert (owner->input == NULL);
122     owner->input = aout_InputNew (p_aout, p_format, &owner->mixer_format,
123                                   p_request_vout);
124     if (owner->input == NULL)
125     {
126         struct audio_mixer *mixer = owner->volume.mixer;
127
128         owner->volume.mixer = NULL;
129         aout_OutputDelete (p_aout);
130         aout_unlock (p_aout);
131         aout_MixerDelete (mixer);
132         return -1;
133     }
134 error:
135     aout_unlock( p_aout );
136     return ret;
137 }
138
139 /**
140  * Stops all plugins involved in the audio output.
141  */
142 void aout_Shutdown (audio_output_t *p_aout)
143 {
144     aout_owner_t *owner = aout_owner (p_aout);
145     aout_input_t *input;
146     struct audio_mixer *mixer;
147
148     aout_lock( p_aout );
149     /* Remove the input. */
150     input = owner->input;
151     if (likely(input != NULL))
152         aout_InputDelete (p_aout, input);
153     owner->input = NULL;
154
155     mixer = owner->volume.mixer;
156     owner->volume.mixer = NULL;
157
158     var_DelCallback (p_aout, "audio-replay-gain-mode",
159                      ReplayGainCallback, owner);
160
161     aout_OutputDelete( p_aout );
162     var_Destroy( p_aout, "audio-device" );
163     var_Destroy( p_aout, "audio-channels" );
164
165     aout_unlock( p_aout );
166
167     aout_MixerDelete (mixer);
168     free (input);
169 }
170
171 /**
172  * Stops the decoded audio input.
173  * @note Due to output recycling, this function is esssentially a stub.
174  */
175 void aout_DecDelete (audio_output_t *aout)
176 {
177 #ifdef RECYCLE
178     (void) aout;
179 #else
180     aout_Shutdown (aout);
181 #endif
182 }
183
184 #define AOUT_RESTART_OUTPUT 1
185 #define AOUT_RESTART_INPUT  2
186 static void aout_CheckRestart (audio_output_t *aout)
187 {
188     aout_owner_t *owner = aout_owner (aout);
189
190     aout_assert_locked (aout);
191
192     int restart = vlc_atomic_swap (&owner->restart, 0);
193     if (likely(restart == 0))
194         return;
195
196     assert (restart & AOUT_RESTART_INPUT);
197
198     const aout_request_vout_t request_vout = owner->input->request_vout;
199
200     if (likely(owner->input != NULL))
201         aout_InputDelete (aout, owner->input);
202     owner->input = NULL;
203
204     /* Reinitializes the output */
205     if (restart & AOUT_RESTART_OUTPUT)
206     {
207         aout_MixerDelete (owner->volume.mixer);
208         owner->volume.mixer = NULL;
209         aout_OutputDelete (aout);
210
211         if (aout_OutputNew (aout, &owner->input_format))
212             return; /* we are officially screwed */
213         owner->volume.mixer = aout_MixerNew (aout,
214                                              owner->mixer_format.i_format);
215     }
216
217     owner->input = aout_InputNew (aout, &owner->input_format,
218                                   &owner->mixer_format, &request_vout);
219 }
220
221 /**
222  * Marks the audio output for restart, to update any parameter of the output
223  * plug-in (e.g. output device or channel mapping).
224  */
225 void aout_RequestRestart (audio_output_t *aout)
226 {
227     aout_owner_t *owner = aout_owner (aout);
228
229     /* DO NOT remove AOUT_RESTART_INPUT. You need to change the atomic ops. */
230     vlc_atomic_set (&owner->restart, AOUT_RESTART_OUTPUT|AOUT_RESTART_INPUT);
231 }
232
233 /**
234  * This function will safely mark aout input to be restarted as soon as
235  * possible to take configuration changes into account
236  */
237 void aout_InputRequestRestart (audio_output_t *aout)
238 {
239     aout_owner_t *owner = aout_owner (aout);
240
241     vlc_atomic_compare_swap (&owner->restart, 0, AOUT_RESTART_INPUT);
242 }
243
244
245 /*
246  * Buffer management
247  */
248
249 /*****************************************************************************
250  * aout_DecNewBuffer : ask for a new empty buffer
251  *****************************************************************************/
252 block_t *aout_DecNewBuffer (audio_output_t *aout, size_t samples)
253 {
254     /* NOTE: the caller is responsible for serializing input change */
255     aout_owner_t *owner = aout_owner (aout);
256
257     size_t length = samples * owner->input_format.i_bytes_per_frame
258                             / owner->input_format.i_frame_length;
259     block_t *block = block_Alloc( length );
260     if( likely(block != NULL) )
261     {
262         block->i_nb_samples = samples;
263         block->i_pts = block->i_length = 0;
264     }
265     return block;
266 }
267
268 /*****************************************************************************
269  * aout_DecDeleteBuffer : destroy an undecoded buffer
270  *****************************************************************************/
271 void aout_DecDeleteBuffer (audio_output_t *aout, block_t *block)
272 {
273     (void) aout;
274     aout_BufferFree (block);
275 }
276
277 /*****************************************************************************
278  * aout_DecPlay : filter & mix the decoded buffer
279  *****************************************************************************/
280 int aout_DecPlay (audio_output_t *p_aout, block_t *p_buffer, int i_input_rate)
281 {
282     aout_owner_t *owner = aout_owner (p_aout);
283     aout_input_t *input;
284
285     assert( i_input_rate >= INPUT_RATE_DEFAULT / AOUT_MAX_INPUT_RATE &&
286             i_input_rate <= INPUT_RATE_DEFAULT * AOUT_MAX_INPUT_RATE );
287     assert( p_buffer->i_pts > 0 );
288
289     p_buffer->i_length = (mtime_t)p_buffer->i_nb_samples * 1000000
290                                 / owner->input_format.i_rate;
291
292     aout_lock( p_aout );
293     aout_CheckRestart( p_aout );
294
295     input = owner->input;
296     if (unlikely(input == NULL)) /* can happen due to restart */
297     {
298         aout_unlock( p_aout );
299         aout_BufferFree( p_buffer );
300         return -1;
301     }
302
303     /* Input */
304     p_buffer = aout_InputPlay (p_aout, input, p_buffer, i_input_rate,
305                                &owner->sync.date);
306     if( p_buffer != NULL )
307     {
308         date_Increment (&owner->sync.date, p_buffer->i_nb_samples);
309
310         /* Mixer */
311         if (owner->volume.mixer != NULL)
312         {
313             float amp = owner->volume.multiplier
314                       * vlc_atomic_getf (&owner->gain.multiplier);
315             aout_MixerRun (owner->volume.mixer, p_buffer, amp);
316         }
317
318         /* Output */
319         aout_OutputPlay( p_aout, p_buffer );
320     }
321
322     aout_unlock( p_aout );
323     return 0;
324 }
325
326 int aout_DecGetResetLost (audio_output_t *aout)
327 {
328     aout_owner_t *owner = aout_owner (aout);
329     aout_input_t *input = owner->input;
330     int val;
331
332     aout_lock (aout);
333     if (likely(input != NULL))
334     {
335         val = input->i_buffer_lost;
336         input->i_buffer_lost = 0;
337     }
338     else
339         val = 0; /* if aout_CheckRestart() failed */
340     aout_unlock (aout);
341
342     return val;
343 }
344
345 void aout_DecChangePause (audio_output_t *aout, bool paused, mtime_t date)
346 {
347     aout_owner_t *owner = aout_owner (aout);
348
349     aout_lock (aout);
350     /* XXX: Should the date be offset by the pause duration instead? */
351     date_Set (&owner->sync.date, VLC_TS_INVALID);
352     aout_OutputPause (aout, paused, date);
353     aout_unlock (aout);
354 }
355
356 void aout_DecFlush (audio_output_t *aout)
357 {
358     aout_owner_t *owner = aout_owner (aout);
359
360     aout_lock (aout);
361     date_Set (&owner->sync.date, VLC_TS_INVALID);
362     aout_OutputFlush (aout, false);
363     aout_unlock (aout);
364 }
365
366 bool aout_DecIsEmpty (audio_output_t *aout)
367 {
368     aout_owner_t *owner = aout_owner (aout);
369     mtime_t end_date, now = mdate ();
370     bool empty;
371
372     aout_lock (aout);
373     end_date = date_Get (&owner->sync.date);
374     empty = end_date == VLC_TS_INVALID || end_date <= now;
375     if (empty)
376         /* The last PTS has elapsed already. So the underlying audio output
377          * buffer should be empty or almost. Thus draining should be fast
378          * and will not block the caller too long. */
379         aout_OutputFlush (aout, true);
380     aout_unlock (aout);
381     return empty;
382 }
383
384 /**
385  * Notifies the audio input of the drift from the requested audio
386  * playback timestamp (@ref block_t.i_pts) to the anticipated playback time
387  * as reported by the audio output hardware.
388  * Depending on the drift amplitude, the input core may ignore the drift
389  * trigger upsampling or downsampling, or even discard samples.
390  * Future VLC versions may instead adjust the input decoding speed.
391  *
392  * The audio output plugin is responsible for estimating the ideal current
393  * playback time defined as follows:
394  *  ideal time = buffer timestamp - (output latency + pending buffer duration)
395  *
396  * Practically, this is the PTS (block_t.i_pts) of the current buffer minus
397  * the latency reported by the output programming interface.
398  * Computing the estimated drift directly would probably be more intuitive.
399  * However the use of an absolute time value does not introduce extra
400  * measurement errors due to the CPU scheduling jitter and clock resolution.
401  * Furthermore, the ideal while it is an abstract value, is easy for most
402  * audio output plugins to compute.
403  * The following definition is equivalent but depends on the clock time:
404  *  ideal time = real time + drift
405
406  * @note If aout_LatencyReport() is never called, the core will assume that
407  * there is no drift.
408  *
409  * @param ideal estimated ideal time as defined above.
410  */
411 void aout_TimeReport (audio_output_t *aout, mtime_t ideal)
412 {
413     mtime_t delta = mdate() - ideal /* = -drift */;
414
415     aout_assert_locked (aout);
416     if (delta < -AOUT_MAX_PTS_ADVANCE || +AOUT_MAX_PTS_DELAY < delta)
417     {
418         aout_owner_t *owner = aout_owner (aout);
419
420         msg_Warn (aout, "not synchronized (%"PRId64" us), resampling",
421                   delta);
422         if (date_Get (&owner->sync.date) != VLC_TS_INVALID)
423             date_Move (&owner->sync.date, delta);
424     }
425 }
426
427 static int ReplayGainCallback (vlc_object_t *obj, char const *var,
428                                vlc_value_t oldval, vlc_value_t val, void *data)
429 {
430     aout_owner_t *owner = data;
431     float multiplier = aout_ReplayGainSelect (obj, val.psz_string,
432                                               &owner->gain.data);
433     vlc_atomic_setf (&owner->gain.multiplier, multiplier);
434     VLC_UNUSED(var); VLC_UNUSED(oldval);
435     return VLC_SUCCESS;
436 }