]> git.sesse.net Git - vlc/blob - modules/audio_output/packet.c
DirectSound: apply volume attenuation with DirectSound
[vlc] / modules / audio_output / packet.c
1 /*****************************************************************************
2  * packet.c : helper for legacy audio output plugins
3  *****************************************************************************
4  * Copyright (C) 2002-2012 VLC authors and VideoLAN
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24 #include <assert.h>
25 #include <vlc_common.h>
26 #include <vlc_aout.h>
27
28 /**
29  * Initializes the members of a FIFO.
30  */
31 static void aout_FifoInit( aout_fifo_t *p_fifo, uint32_t i_rate )
32 {
33     assert( i_rate != 0);
34     p_fifo->p_first = NULL;
35     p_fifo->pp_last = &p_fifo->p_first;
36     date_Init( &p_fifo->end_date, i_rate, 1 );
37     date_Set( &p_fifo->end_date, VLC_TS_INVALID );
38 }
39
40 /**
41  * Pushes a packet into the FIFO.
42  */
43 static void aout_FifoPush( aout_fifo_t * p_fifo, aout_buffer_t * p_buffer )
44 {
45     *p_fifo->pp_last = p_buffer;
46     p_fifo->pp_last = &p_buffer->p_next;
47     *p_fifo->pp_last = NULL;
48     /* Enforce the continuity of the stream. */
49     if( date_Get( &p_fifo->end_date ) != VLC_TS_INVALID )
50     {
51         p_buffer->i_pts = date_Get( &p_fifo->end_date );
52         p_buffer->i_length = date_Increment( &p_fifo->end_date,
53                                              p_buffer->i_nb_samples );
54         p_buffer->i_length -= p_buffer->i_pts;
55     }
56     else
57     {
58         date_Set( &p_fifo->end_date, p_buffer->i_pts + p_buffer->i_length );
59     }
60 }
61
62 /**
63  * Trashes all buffers.
64  */
65 static void aout_FifoReset( aout_fifo_t * p_fifo )
66 {
67     aout_buffer_t * p_buffer;
68
69     date_Set( &p_fifo->end_date, VLC_TS_INVALID );
70     p_buffer = p_fifo->p_first;
71     while ( p_buffer != NULL )
72     {
73         aout_buffer_t * p_next = p_buffer->p_next;
74         aout_BufferFree( p_buffer );
75         p_buffer = p_next;
76     }
77     p_fifo->p_first = NULL;
78     p_fifo->pp_last = &p_fifo->p_first;
79 }
80
81 /**
82  * Move forwards or backwards all dates in the FIFO.
83  */
84 static void aout_FifoMoveDates( aout_fifo_t *fifo, mtime_t difference )
85 {
86     if( date_Get( &fifo->end_date ) == VLC_TS_INVALID )
87     {
88         assert( fifo->p_first == NULL );
89         return;
90     }
91
92     date_Move( &fifo->end_date, difference );
93     for( block_t *block = fifo->p_first; block != NULL; block = block->p_next )
94         block->i_pts += difference;
95 }
96
97 /**
98  * Gets the next buffer out of the FIFO
99  */
100 static aout_buffer_t *aout_FifoPop( aout_fifo_t * p_fifo )
101 {
102     aout_buffer_t *p_buffer = p_fifo->p_first;
103     if( p_buffer != NULL )
104     {
105         p_fifo->p_first = p_buffer->p_next;
106         if( p_fifo->p_first == NULL )
107             p_fifo->pp_last = &p_fifo->p_first;
108     }
109     return p_buffer;
110 }
111
112 /**
113  * Destroys a FIFO and its buffers
114  */
115 static void aout_FifoDestroy( aout_fifo_t * p_fifo )
116 {
117     aout_buffer_t * p_buffer;
118
119     p_buffer = p_fifo->p_first;
120     while ( p_buffer != NULL )
121     {
122         aout_buffer_t * p_next = p_buffer->p_next;
123         aout_BufferFree( p_buffer );
124         p_buffer = p_next;
125     }
126
127     p_fifo->p_first = NULL;
128     p_fifo->pp_last = &p_fifo->p_first;
129 }
130
131 static inline aout_packet_t *aout_packet (audio_output_t *aout)
132 {
133     return (aout_packet_t *)(aout->sys);
134 }
135
136 void aout_PacketInit (audio_output_t *aout, aout_packet_t *p, unsigned samples)
137 {
138     assert (p == aout_packet (aout));
139
140     vlc_mutex_init (&p->lock);
141     aout_FifoInit (&p->partial, aout->format.i_rate);
142     aout_FifoInit (&p->fifo, aout->format.i_rate);
143     p->pause_date = VLC_TS_INVALID;
144     p->time_report = VLC_TS_INVALID;
145     p->samples = samples;
146     p->starving = true;
147 }
148
149 void aout_PacketDestroy (audio_output_t *aout)
150 {
151     aout_packet_t *p = aout_packet (aout);
152
153     aout_FifoDestroy (&p->partial);
154     aout_FifoDestroy (&p->fifo);
155     vlc_mutex_destroy (&p->lock);
156 }
157
158 static block_t *aout_OutputSlice (audio_output_t *);
159
160 void aout_PacketPlay (audio_output_t *aout, block_t *block)
161 {
162     aout_packet_t *p = aout_packet (aout);
163     mtime_t time_report;
164
165     vlc_mutex_lock (&p->lock);
166     aout_FifoPush (&p->partial, block);
167     while ((block = aout_OutputSlice (aout)) != NULL)
168         aout_FifoPush (&p->fifo, block);
169
170     time_report = p->time_report;
171     p->time_report = VLC_TS_INVALID;
172     vlc_mutex_unlock (&p->lock);
173
174     if (time_report != VLC_TS_INVALID)
175         aout_TimeReport (aout, mdate () - time_report);
176 }
177
178 void aout_PacketPause (audio_output_t *aout, bool pause, mtime_t date)
179 {
180     aout_packet_t *p = aout_packet (aout);
181
182     if (pause)
183     {
184         assert (p->pause_date == VLC_TS_INVALID);
185         p->pause_date = date;
186     }
187     else
188     {
189         assert (p->pause_date != VLC_TS_INVALID);
190
191         mtime_t duration = date - p->pause_date;
192
193         p->pause_date = VLC_TS_INVALID;
194         vlc_mutex_lock (&p->lock);
195         aout_FifoMoveDates (&p->partial, duration);
196         aout_FifoMoveDates (&p->fifo, duration);
197         vlc_mutex_unlock (&p->lock);
198     }
199 }
200
201 void aout_PacketFlush (audio_output_t *aout, bool drain)
202 {
203     aout_packet_t *p = aout_packet (aout);
204
205     vlc_mutex_lock (&p->lock);
206     aout_FifoReset (&p->partial);
207     aout_FifoReset (&p->fifo);
208     vlc_mutex_unlock (&p->lock);
209
210     (void) drain; /* TODO */
211 }
212
213
214 /**
215  * Rearranges audio blocks in correct number of samples.
216  * @note (FIXME) This is left here for historical reasons. It belongs in the
217  * output code. Besides, this operation should be avoided if possible.
218  */
219 static block_t *aout_OutputSlice (audio_output_t *p_aout)
220 {
221     aout_packet_t *p = aout_packet (p_aout);
222     aout_fifo_t *p_fifo = &p->partial;
223     const unsigned samples = p->samples;
224     assert( samples > 0 );
225
226     /* Retrieve the date of the next buffer. */
227     date_t exact_start_date = p->fifo.end_date;
228     mtime_t start_date = date_Get( &exact_start_date );
229
230     /* Check if there is enough data to slice a new buffer. */
231     aout_buffer_t *p_buffer = p_fifo->p_first;
232     if( p_buffer == NULL )
233         return NULL;
234
235     /* Find the earliest start date available. */
236     if ( start_date == VLC_TS_INVALID )
237     {
238         start_date = p_buffer->i_pts;
239         date_Set( &exact_start_date, start_date );
240     }
241     /* Compute the end date for the new buffer. */
242     mtime_t end_date = date_Increment( &exact_start_date, samples );
243
244     /* Check that we have enough samples (TODO merge with next loop). */
245     for( unsigned available = 0; available < samples; )
246     {
247         p_buffer = p_buffer->p_next;
248         if( p_buffer == NULL )
249             return NULL;
250
251         available += p_buffer->i_nb_samples;
252     }
253
254     if( AOUT_FMT_LINEAR( &p_aout->format ) )
255     {
256         const unsigned framesize = p_aout->format.i_bytes_per_frame;
257         /* Build packet with adequate number of samples */
258         unsigned needed = samples * framesize;
259
260         p_buffer = block_Alloc( needed );
261         if( unlikely(p_buffer == NULL) )
262             /* XXX: should free input buffers */
263             return NULL;
264         p_buffer->i_nb_samples = samples;
265
266         for( uint8_t *p_out = p_buffer->p_buffer; needed > 0; )
267         {
268             aout_buffer_t *p_inbuf = p_fifo->p_first;
269             if( unlikely(p_inbuf == NULL) )
270             {
271                 msg_Err( p_aout, "packetization error" );
272                 vlc_memset( p_out, 0, needed );
273                 break;
274             }
275
276             const uint8_t *p_in = p_inbuf->p_buffer;
277             size_t avail = p_inbuf->i_nb_samples * framesize;
278             if( avail > needed )
279             {
280                 vlc_memcpy( p_out, p_in, needed );
281                 p_fifo->p_first->p_buffer += needed;
282                 p_fifo->p_first->i_buffer -= needed;
283                 needed /= framesize;
284                 p_fifo->p_first->i_nb_samples -= needed;
285
286                 mtime_t t = needed * CLOCK_FREQ / p_aout->format.i_rate;
287                 p_fifo->p_first->i_pts += t;
288                 p_fifo->p_first->i_length -= t;
289                 break;
290             }
291
292             vlc_memcpy( p_out, p_in, avail );
293             needed -= avail;
294             p_out += avail;
295             /* Next buffer */
296             aout_BufferFree( aout_FifoPop( p_fifo ) );
297         }
298     }
299     else
300         p_buffer = aout_FifoPop( p_fifo );
301
302     p_buffer->i_pts = start_date;
303     p_buffer->i_length = end_date - start_date;
304
305     return p_buffer;
306 }
307
308 /**
309  * Dequeues the next audio packet (a.k.a. audio fragment).
310  * The audio output plugin must first call aout_PacketPlay() to queue the
311  * decoded audio samples. Typically, audio_output_t.pf_play is set to, or calls
312  * aout_PacketPlay().
313  * @note This function is considered legacy. Please do not use this function in
314  * new audio output plugins.
315  * @param p_aout audio output instance
316  * @param start_date expected PTS of the audio packet
317  */
318 block_t *aout_PacketNext (audio_output_t *p_aout, mtime_t start_date)
319 {
320     aout_packet_t *p = aout_packet (p_aout);
321     aout_fifo_t *p_fifo = &p->fifo;
322     block_t *p_buffer;
323     const bool b_can_sleek = !AOUT_FMT_LINEAR(&p_aout->format);
324     const mtime_t now = mdate ();
325     const mtime_t threshold =
326         (b_can_sleek ? start_date : now) - AOUT_MAX_PTS_DELAY;
327
328     vlc_mutex_lock( &p->lock );
329     if( p->pause_date != VLC_TS_INVALID )
330         goto out; /* paused: do not dequeue buffers */
331
332     for (;;)
333     {
334         p_buffer = p_fifo->p_first;
335         if (p_buffer == NULL)
336             goto out; /* nothing to play */
337
338         if (p_buffer->i_pts >= threshold)
339             break;
340
341         /* Drop the audio sample if the audio output is really late.
342          * In the case of b_can_sleek, we don't use a resampler so we need to
343          * be a lot more severe. */
344         msg_Dbg (p_aout, "audio output is too slow (%"PRId64" us): "
345                  " trashing %"PRId64" us", threshold - p_buffer->i_pts,
346                  p_buffer->i_length);
347         block_Release (aout_FifoPop (p_fifo));
348     }
349
350     mtime_t delta = start_date - p_buffer->i_pts;
351     /* This assumes that all buffers have the same duration. This is true
352      * since aout_PacketPlay() (aout_OutputSlice()) is used. */
353     if (0 >= delta + p_buffer->i_length)
354     {
355         if (!p->starving)
356         {
357             msg_Dbg (p_aout, "audio output is starving (%"PRId64"), "
358                      "playing silence", delta);
359             p->starving = true;
360         }
361         goto out; /* nothing to play _yet_ */
362     }
363
364     p->starving = false;
365     p_buffer = aout_FifoPop( p_fifo );
366
367     if (!b_can_sleek
368      && (delta < -AOUT_MAX_PTS_ADVANCE || AOUT_MAX_PTS_DELAY < delta))
369     {
370         msg_Warn (p_aout, "audio output out of sync, "
371                           "adjusting dates (%"PRId64" us)", delta);
372         aout_FifoMoveDates (&p->partial, delta);
373         aout_FifoMoveDates (p_fifo, delta);
374         p->time_report = delta;
375     }
376     vlc_mutex_unlock( &p->lock );
377     return p_buffer;
378 out:
379     vlc_mutex_unlock( &p->lock );
380     return NULL;
381 }