1 /*****************************************************************************
2 * packet.c : helper for legacy audio output plugins
3 *****************************************************************************
4 * Copyright (C) 2002-2012 VLC authors and VideoLAN
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.
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.
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 *****************************************************************************/
26 #include <vlc_common.h>
30 * Initializes the members of a FIFO.
32 static void aout_FifoInit( aout_fifo_t *p_fifo, uint32_t i_rate )
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 );
42 * Pushes a packet into the FIFO.
44 static void aout_FifoPush( aout_fifo_t * p_fifo, block_t * p_buffer )
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 )
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;
59 date_Set( &p_fifo->end_date, p_buffer->i_pts + p_buffer->i_length );
64 * Trashes all buffers.
66 static void aout_FifoReset( aout_fifo_t * p_fifo )
70 date_Set( &p_fifo->end_date, VLC_TS_INVALID );
71 p_buffer = p_fifo->p_first;
72 while ( p_buffer != NULL )
74 block_t * p_next = p_buffer->p_next;
75 block_Release( p_buffer );
78 p_fifo->p_first = NULL;
79 p_fifo->pp_last = &p_fifo->p_first;
83 * Move forwards or backwards all dates in the FIFO.
85 static void aout_FifoMoveDates( aout_fifo_t *fifo, mtime_t difference )
87 if( date_Get( &fifo->end_date ) == VLC_TS_INVALID )
89 assert( fifo->p_first == NULL );
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;
99 * Gets the next buffer out of the FIFO
101 static block_t *aout_FifoPop( aout_fifo_t * p_fifo )
103 block_t *p_buffer = p_fifo->p_first;
104 if( p_buffer != NULL )
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;
114 * Destroys a FIFO and its buffers
116 static void aout_FifoDestroy( aout_fifo_t * p_fifo )
120 p_buffer = p_fifo->p_first;
121 while ( p_buffer != NULL )
123 block_t * p_next = p_buffer->p_next;
124 block_Release( p_buffer );
128 p_fifo->p_first = NULL;
129 p_fifo->pp_last = &p_fifo->p_first;
132 static inline aout_packet_t *aout_packet (audio_output_t *aout)
134 return (aout_packet_t *)(aout->sys);
137 void aout_PacketInit (audio_output_t *aout, aout_packet_t *p, unsigned samples,
138 const audio_sample_format_t *fmt)
140 assert (p == aout_packet (aout));
142 vlc_mutex_init (&p->lock);
144 aout_FormatPrepare (&p->format);
145 aout_FifoInit (&p->partial, p->format.i_rate);
146 aout_FifoInit (&p->fifo, p->format.i_rate);
147 p->pause_date = VLC_TS_INVALID;
148 p->samples = samples;
152 void aout_PacketDestroy (audio_output_t *aout)
154 aout_packet_t *p = aout_packet (aout);
156 aout_FifoDestroy (&p->partial);
157 aout_FifoDestroy (&p->fifo);
158 vlc_mutex_destroy (&p->lock);
161 int aout_PacketTimeGet (audio_output_t *aout, mtime_t *restrict delay)
163 aout_packet_t *p = aout_packet (aout);
166 /* Problem: This measurement is imprecise and prone to jitter.
167 * Solution: Do not use aout_Packet...(). */
168 vlc_mutex_lock (&p->lock);
169 time_report = date_Get (&p->fifo.end_date);
170 vlc_mutex_unlock (&p->lock);
172 if (time_report == VLC_TS_INVALID)
174 *delay = time_report - mdate ();
178 static block_t *aout_OutputSlice (audio_output_t *);
180 void aout_PacketPlay (audio_output_t *aout, block_t *block)
182 aout_packet_t *p = aout_packet (aout);
184 vlc_mutex_lock (&p->lock);
185 aout_FifoPush (&p->partial, block);
186 while ((block = aout_OutputSlice (aout)) != NULL)
187 aout_FifoPush (&p->fifo, block);
188 vlc_mutex_unlock (&p->lock);
191 void aout_PacketFlush (audio_output_t *aout, bool drain)
193 aout_packet_t *p = aout_packet (aout);
195 vlc_mutex_lock (&p->lock);
198 mtime_t pts = date_Get (&p->fifo.end_date);
199 vlc_mutex_unlock (&p->lock);
200 if (pts != VLC_TS_INVALID)
205 aout_FifoReset (&p->partial);
206 aout_FifoReset (&p->fifo);
207 vlc_mutex_unlock (&p->lock);
213 * Rearranges audio blocks in correct number of samples.
214 * @note (FIXME) This is left here for historical reasons. It belongs in the
215 * output code. Besides, this operation should be avoided if possible.
217 static block_t *aout_OutputSlice (audio_output_t *p_aout)
219 aout_packet_t *p = aout_packet (p_aout);
220 aout_fifo_t *p_fifo = &p->partial;
221 const unsigned samples = p->samples;
222 assert( samples > 0 );
224 /* Retrieve the date of the next buffer. */
225 date_t exact_start_date = p->fifo.end_date;
226 mtime_t start_date = date_Get( &exact_start_date );
228 /* Check if there is enough data to slice a new buffer. */
229 block_t *p_buffer = p_fifo->p_first;
230 if( p_buffer == NULL )
233 /* Find the earliest start date available. */
234 if ( start_date == VLC_TS_INVALID )
236 start_date = p_buffer->i_pts;
237 date_Set( &exact_start_date, start_date );
239 /* Compute the end date for the new buffer. */
240 mtime_t end_date = date_Increment( &exact_start_date, samples );
242 /* Check that we have enough samples (TODO merge with next loop). */
243 for( unsigned available = 0; available < samples; )
245 p_buffer = p_buffer->p_next;
246 if( p_buffer == NULL )
249 available += p_buffer->i_nb_samples;
252 if( AOUT_FMT_LINEAR( &p->format ) )
254 const unsigned framesize = p->format.i_bytes_per_frame;
255 /* Build packet with adequate number of samples */
256 unsigned needed = samples * framesize;
258 p_buffer = block_Alloc( needed );
259 if( unlikely(p_buffer == NULL) )
260 /* XXX: should free input buffers */
262 p_buffer->i_nb_samples = samples;
264 for( uint8_t *p_out = p_buffer->p_buffer; needed > 0; )
266 block_t *p_inbuf = p_fifo->p_first;
267 if( unlikely(p_inbuf == NULL) )
269 msg_Err( p_aout, "packetization error" );
270 memset( p_out, 0, needed );
274 const uint8_t *p_in = p_inbuf->p_buffer;
275 size_t avail = p_inbuf->i_nb_samples * framesize;
278 memcpy( p_out, p_in, needed );
279 p_fifo->p_first->p_buffer += needed;
280 p_fifo->p_first->i_buffer -= needed;
282 p_fifo->p_first->i_nb_samples -= needed;
284 mtime_t t = needed * CLOCK_FREQ / p->format.i_rate;
285 p_fifo->p_first->i_pts += t;
286 p_fifo->p_first->i_length -= t;
290 memcpy( p_out, p_in, avail );
294 block_Release( aout_FifoPop( p_fifo ) );
298 p_buffer = aout_FifoPop( p_fifo );
300 p_buffer->i_pts = start_date;
301 p_buffer->i_length = end_date - start_date;
307 * Dequeues the next audio packet (a.k.a. audio fragment).
308 * The audio output plugin must first call aout_PacketPlay() to queue the
309 * decoded audio samples. Typically, audio_output_t.play is set to, or calls
311 * @note This function is considered legacy. Please do not use this function in
312 * new audio output plugins.
313 * @param p_aout audio output instance
314 * @param start_date expected PTS of the audio packet
316 block_t *aout_PacketNext (audio_output_t *p_aout, mtime_t start_date)
318 aout_packet_t *p = aout_packet (p_aout);
319 aout_fifo_t *p_fifo = &p->fifo;
321 const bool b_can_sleek = !AOUT_FMT_LINEAR(&p->format);
322 const mtime_t now = mdate ();
323 const mtime_t threshold =
324 (b_can_sleek ? start_date : now) - AOUT_MAX_PTS_DELAY;
326 vlc_mutex_lock( &p->lock );
327 if( p->pause_date != VLC_TS_INVALID )
328 goto out; /* paused: do not dequeue buffers */
332 p_buffer = p_fifo->p_first;
333 if (p_buffer == NULL)
334 goto out; /* nothing to play */
336 if (p_buffer->i_pts >= threshold)
339 /* Drop the audio sample if the audio output is really late.
340 * In the case of b_can_sleek, we don't use a resampler so we need to
341 * be a lot more severe. */
342 msg_Dbg (p_aout, "audio output is too slow (%"PRId64" us): "
343 " trashing %"PRId64" us", threshold - p_buffer->i_pts,
345 block_Release (aout_FifoPop (p_fifo));
348 mtime_t delta = start_date - p_buffer->i_pts;
349 /* This assumes that all buffers have the same duration. This is true
350 * since aout_PacketPlay() (aout_OutputSlice()) is used. */
351 if (0 >= delta + p_buffer->i_length)
355 msg_Dbg (p_aout, "audio output is starving (%"PRId64"), "
356 "playing silence", delta);
359 goto out; /* nothing to play _yet_ */
363 p_buffer = aout_FifoPop( p_fifo );
366 && (delta < -AOUT_MAX_PTS_ADVANCE || AOUT_MAX_PTS_DELAY < delta))
368 msg_Warn (p_aout, "audio output out of sync, "
369 "adjusting dates (%"PRId64" us)", delta);
370 aout_FifoMoveDates (&p->partial, delta);
371 aout_FifoMoveDates (p_fifo, delta);
373 vlc_mutex_unlock( &p->lock );
376 vlc_mutex_unlock( &p->lock );