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 *****************************************************************************/
25 #include <vlc_common.h>
29 * Initializes the members of a FIFO.
31 static void aout_FifoInit( aout_fifo_t *p_fifo, uint32_t i_rate )
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 );
41 * Pushes a packet into the FIFO.
43 static void aout_FifoPush( aout_fifo_t * p_fifo, aout_buffer_t * p_buffer )
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 )
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;
58 date_Set( &p_fifo->end_date, p_buffer->i_pts + p_buffer->i_length );
63 * Trashes all buffers.
65 static void aout_FifoReset( aout_fifo_t * p_fifo )
67 aout_buffer_t * p_buffer;
69 date_Set( &p_fifo->end_date, VLC_TS_INVALID );
70 p_buffer = p_fifo->p_first;
71 while ( p_buffer != NULL )
73 aout_buffer_t * p_next = p_buffer->p_next;
74 aout_BufferFree( p_buffer );
77 p_fifo->p_first = NULL;
78 p_fifo->pp_last = &p_fifo->p_first;
82 * Move forwards or backwards all dates in the FIFO.
84 static void aout_FifoMoveDates( aout_fifo_t *fifo, mtime_t difference )
86 if( date_Get( &fifo->end_date ) == VLC_TS_INVALID )
88 assert( fifo->p_first == NULL );
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;
98 * Gets the next buffer out of the FIFO
100 static aout_buffer_t *aout_FifoPop( aout_fifo_t * p_fifo )
102 aout_buffer_t *p_buffer = p_fifo->p_first;
103 if( p_buffer != NULL )
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;
113 * Destroys a FIFO and its buffers
115 static void aout_FifoDestroy( aout_fifo_t * p_fifo )
117 aout_buffer_t * p_buffer;
119 p_buffer = p_fifo->p_first;
120 while ( p_buffer != NULL )
122 aout_buffer_t * p_next = p_buffer->p_next;
123 aout_BufferFree( p_buffer );
127 p_fifo->p_first = NULL;
128 p_fifo->pp_last = &p_fifo->p_first;
131 static inline aout_packet_t *aout_packet (audio_output_t *aout)
133 return (aout_packet_t *)(aout->sys);
136 void aout_PacketInit (audio_output_t *aout, aout_packet_t *p, unsigned samples)
138 assert (p == aout_packet (aout));
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;
149 void aout_PacketDestroy (audio_output_t *aout)
151 aout_packet_t *p = aout_packet (aout);
153 aout_FifoDestroy (&p->partial);
154 aout_FifoDestroy (&p->fifo);
155 vlc_mutex_destroy (&p->lock);
158 static block_t *aout_OutputSlice (audio_output_t *);
160 void aout_PacketPlay (audio_output_t *aout, block_t *block)
162 aout_packet_t *p = aout_packet (aout);
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);
170 time_report = p->time_report;
171 p->time_report = VLC_TS_INVALID;
172 vlc_mutex_unlock (&p->lock);
174 if (time_report != VLC_TS_INVALID)
175 aout_TimeReport (aout, mdate () - time_report);
178 void aout_PacketPause (audio_output_t *aout, bool pause, mtime_t date)
180 aout_packet_t *p = aout_packet (aout);
184 assert (p->pause_date == VLC_TS_INVALID);
185 p->pause_date = date;
189 assert (p->pause_date != VLC_TS_INVALID);
191 mtime_t duration = date - p->pause_date;
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);
201 void aout_PacketFlush (audio_output_t *aout, bool drain)
203 aout_packet_t *p = aout_packet (aout);
205 vlc_mutex_lock (&p->lock);
206 aout_FifoReset (&p->partial);
207 aout_FifoReset (&p->fifo);
208 vlc_mutex_unlock (&p->lock);
210 (void) drain; /* TODO */
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.
219 static block_t *aout_OutputSlice (audio_output_t *p_aout)
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 );
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 );
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 )
235 /* Find the earliest start date available. */
236 if ( start_date == VLC_TS_INVALID )
238 start_date = p_buffer->i_pts;
239 date_Set( &exact_start_date, start_date );
241 /* Compute the end date for the new buffer. */
242 mtime_t end_date = date_Increment( &exact_start_date, samples );
244 /* Check that we have enough samples (TODO merge with next loop). */
245 for( unsigned available = 0; available < samples; )
247 p_buffer = p_buffer->p_next;
248 if( p_buffer == NULL )
251 available += p_buffer->i_nb_samples;
254 if( AOUT_FMT_LINEAR( &p_aout->format ) )
256 const unsigned framesize = p_aout->format.i_bytes_per_frame;
257 /* Build packet with adequate number of samples */
258 unsigned needed = samples * framesize;
260 p_buffer = block_Alloc( needed );
261 if( unlikely(p_buffer == NULL) )
262 /* XXX: should free input buffers */
264 p_buffer->i_nb_samples = samples;
266 for( uint8_t *p_out = p_buffer->p_buffer; needed > 0; )
268 aout_buffer_t *p_inbuf = p_fifo->p_first;
269 if( unlikely(p_inbuf == NULL) )
271 msg_Err( p_aout, "packetization error" );
272 vlc_memset( p_out, 0, needed );
276 const uint8_t *p_in = p_inbuf->p_buffer;
277 size_t avail = p_inbuf->i_nb_samples * framesize;
280 vlc_memcpy( p_out, p_in, needed );
281 p_fifo->p_first->p_buffer += needed;
282 p_fifo->p_first->i_buffer -= needed;
284 p_fifo->p_first->i_nb_samples -= needed;
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;
292 vlc_memcpy( p_out, p_in, avail );
296 aout_BufferFree( aout_FifoPop( p_fifo ) );
300 p_buffer = aout_FifoPop( p_fifo );
302 p_buffer->i_pts = start_date;
303 p_buffer->i_length = end_date - start_date;
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
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
318 block_t *aout_PacketNext (audio_output_t *p_aout, mtime_t start_date)
320 aout_packet_t *p = aout_packet (p_aout);
321 aout_fifo_t *p_fifo = &p->fifo;
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;
328 vlc_mutex_lock( &p->lock );
329 if( p->pause_date != VLC_TS_INVALID )
330 goto out; /* paused: do not dequeue buffers */
334 p_buffer = p_fifo->p_first;
335 if (p_buffer == NULL)
336 goto out; /* nothing to play */
338 if (p_buffer->i_pts >= threshold)
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,
347 block_Release (aout_FifoPop (p_fifo));
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)
357 msg_Dbg (p_aout, "audio output is starving (%"PRId64"), "
358 "playing silence", delta);
361 goto out; /* nothing to play _yet_ */
365 p_buffer = aout_FifoPop( p_fifo );
368 && (delta < -AOUT_MAX_PTS_ADVANCE || AOUT_MAX_PTS_DELAY < delta))
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;
376 vlc_mutex_unlock( &p->lock );
379 vlc_mutex_unlock( &p->lock );