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