2004-05-07 04:42:49 +02:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-05-07 04:42:49 +02:00
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "outputBuffer.h"
|
|
|
|
|
|
|
|
#include "utils.h"
|
2006-07-22 02:53:37 +02:00
|
|
|
#include "normalize.h"
|
2004-05-07 04:42:49 +02:00
|
|
|
|
2008-04-12 06:18:04 +02:00
|
|
|
void initOutputBuffer(OutputBuffer * cb, unsigned int size)
|
2008-03-26 11:38:12 +01:00
|
|
|
{
|
2008-04-12 06:18:04 +02:00
|
|
|
assert(size > 0);
|
|
|
|
|
2008-03-26 11:38:12 +01:00
|
|
|
memset(&cb->convState, 0, sizeof(ConvState));
|
2008-04-12 06:18:04 +02:00
|
|
|
cb->chunks = xmalloc(size * sizeof(*cb->chunks));
|
|
|
|
cb->size = size;
|
2008-04-12 06:14:50 +02:00
|
|
|
cb->currentChunk = -1;
|
2004-06-07 00:13:23 +02:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:18:38 +02:00
|
|
|
void output_buffer_free(OutputBuffer * cb)
|
|
|
|
{
|
|
|
|
assert(cb->chunks != NULL);
|
|
|
|
free(cb->chunks);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void clearOutputBuffer(OutputBuffer * cb)
|
|
|
|
{
|
|
|
|
cb->end = cb->begin;
|
2008-04-12 06:14:50 +02:00
|
|
|
cb->currentChunk = -1;
|
2004-05-18 05:37:55 +02:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:14:55 +02:00
|
|
|
/** return the index of the chunk after i */
|
2008-04-12 06:18:04 +02:00
|
|
|
static inline unsigned successor(const OutputBuffer * cb, unsigned i)
|
2008-04-12 06:14:55 +02:00
|
|
|
{
|
2008-04-12 06:18:04 +02:00
|
|
|
assert(i <= cb->size);
|
2008-04-12 06:14:55 +02:00
|
|
|
|
|
|
|
++i;
|
2008-04-12 06:18:04 +02:00
|
|
|
return i == cb->size ? 0 : i;
|
2008-04-12 06:14:55 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void flushOutputBuffer(OutputBuffer * cb)
|
|
|
|
{
|
2008-04-12 06:18:28 +02:00
|
|
|
if (cb->currentChunk == (int)cb->end) {
|
2008-04-12 06:21:00 +02:00
|
|
|
int was_empty = outputBufferEmpty(cb);
|
|
|
|
|
2008-04-12 06:18:04 +02:00
|
|
|
cb->end = successor(cb, cb->end);
|
2008-04-12 06:14:50 +02:00
|
|
|
cb->currentChunk = -1;
|
2008-04-12 06:21:00 +02:00
|
|
|
|
|
|
|
if (was_empty)
|
|
|
|
/* if the buffer was empty, the player thread
|
|
|
|
might be waiting for us; wake it up now
|
|
|
|
that another decoded buffer has become
|
|
|
|
available. */
|
|
|
|
decoder_wakeup_player();
|
2004-05-07 21:11:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-12 06:12:47 +02:00
|
|
|
int outputBufferEmpty(const OutputBuffer * cb)
|
|
|
|
{
|
|
|
|
return cb->begin == cb->end;
|
|
|
|
}
|
|
|
|
|
2008-04-12 06:13:51 +02:00
|
|
|
void outputBufferShift(OutputBuffer * cb)
|
|
|
|
{
|
|
|
|
assert(cb->begin != cb->end);
|
2008-04-12 06:18:04 +02:00
|
|
|
assert(cb->begin < cb->size);
|
2008-04-12 06:13:51 +02:00
|
|
|
|
2008-04-12 06:18:04 +02:00
|
|
|
cb->begin = successor(cb, cb->begin);
|
2008-04-12 06:13:51 +02:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:12:53 +02:00
|
|
|
unsigned int outputBufferRelative(const OutputBuffer * cb, unsigned i)
|
2008-04-12 06:11:41 +02:00
|
|
|
{
|
2008-04-12 06:12:53 +02:00
|
|
|
if (i >= cb->begin)
|
|
|
|
return i - cb->begin;
|
2008-04-12 06:11:41 +02:00
|
|
|
else
|
2008-04-12 06:18:04 +02:00
|
|
|
return i + cb->size - cb->begin;
|
2008-04-12 06:12:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned availableOutputBuffer(const OutputBuffer * cb)
|
|
|
|
{
|
|
|
|
return outputBufferRelative(cb, cb->end);
|
2008-04-12 06:11:41 +02:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:12:42 +02:00
|
|
|
int outputBufferAbsolute(const OutputBuffer * cb, unsigned relative)
|
|
|
|
{
|
|
|
|
unsigned i, max;
|
|
|
|
|
|
|
|
max = cb->end;
|
|
|
|
if (max < cb->begin)
|
2008-04-12 06:18:04 +02:00
|
|
|
max += cb->size;
|
2008-04-12 06:12:42 +02:00
|
|
|
i = (unsigned)cb->begin + relative;
|
|
|
|
if (i >= max)
|
|
|
|
return -1;
|
|
|
|
|
2008-04-12 06:18:04 +02:00
|
|
|
if (i >= cb->size)
|
|
|
|
i -= cb->size;
|
2008-04-12 06:12:42 +02:00
|
|
|
|
|
|
|
return (int)i;
|
|
|
|
}
|
|
|
|
|
2008-04-12 06:13:24 +02:00
|
|
|
OutputBufferChunk * outputBufferGetChunk(const OutputBuffer * cb, unsigned i)
|
2008-04-12 06:13:11 +02:00
|
|
|
{
|
2008-04-12 06:18:04 +02:00
|
|
|
assert(i < cb->size);
|
2008-04-12 06:13:11 +02:00
|
|
|
|
2008-04-12 06:13:24 +02:00
|
|
|
return &cb->chunks[i];
|
2008-04-12 06:13:11 +02:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:11:23 +02:00
|
|
|
/**
|
2008-04-12 06:17:01 +02:00
|
|
|
* Return the tail chunk which has room for additional data. If there
|
|
|
|
* is no room in the queue, this function blocks until the player
|
|
|
|
* thread has finished playing its current chunk.
|
2008-04-12 06:11:23 +02:00
|
|
|
*
|
|
|
|
* @return the positive index of the new chunk; OUTPUT_BUFFER_DC_SEEK
|
|
|
|
* if another thread requested seeking; OUTPUT_BUFFER_DC_STOP if
|
|
|
|
* another thread requested stopping the decoder.
|
|
|
|
*/
|
|
|
|
static int tailChunk(OutputBuffer * cb, InputStream * inStream,
|
|
|
|
DecoderControl * dc, int seekable,
|
|
|
|
float data_time, mpd_uint16 bitRate)
|
|
|
|
{
|
|
|
|
unsigned int next;
|
2008-04-12 06:13:24 +02:00
|
|
|
OutputBufferChunk *chunk;
|
2008-04-12 06:11:23 +02:00
|
|
|
|
2008-04-12 06:18:28 +02:00
|
|
|
if (cb->currentChunk == (int)cb->end)
|
2008-04-12 06:14:50 +02:00
|
|
|
return cb->currentChunk;
|
2008-04-12 06:11:23 +02:00
|
|
|
|
2008-04-12 06:18:04 +02:00
|
|
|
next = successor(cb, cb->end);
|
2008-04-12 06:11:23 +02:00
|
|
|
while (cb->begin == next && !dc->stop) {
|
|
|
|
if (dc->seek) {
|
|
|
|
if (seekable) {
|
|
|
|
return OUTPUT_BUFFER_DC_SEEK;
|
|
|
|
} else {
|
|
|
|
dc->seekError = 1;
|
|
|
|
dc->seek = 0;
|
|
|
|
decoder_wakeup_player();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!inStream ||
|
|
|
|
bufferInputStream(inStream) <= 0) {
|
2008-04-12 06:18:43 +02:00
|
|
|
decoder_sleep(dc);
|
2008-04-12 06:11:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dc->stop)
|
|
|
|
return OUTPUT_BUFFER_DC_STOP;
|
|
|
|
|
2008-04-12 06:14:50 +02:00
|
|
|
cb->currentChunk = cb->end;
|
|
|
|
chunk = outputBufferGetChunk(cb, cb->currentChunk);
|
2008-04-12 06:13:24 +02:00
|
|
|
chunk->chunkSize = 0;
|
|
|
|
chunk->bitRate = bitRate;
|
|
|
|
chunk->times = data_time;
|
2008-04-12 06:11:23 +02:00
|
|
|
|
2008-04-12 06:14:50 +02:00
|
|
|
return cb->currentChunk;
|
2008-04-12 06:11:23 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int sendDataToOutputBuffer(OutputBuffer * cb, InputStream * inStream,
|
|
|
|
DecoderControl * dc, int seekable, void *dataIn,
|
2008-03-26 11:38:07 +01:00
|
|
|
size_t dataInLen, float data_time, mpd_uint16 bitRate,
|
2006-07-20 18:02:40 +02:00
|
|
|
ReplayGainInfo * replayGainInfo)
|
2004-05-07 04:42:49 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
mpd_uint16 dataToSend;
|
2004-05-07 21:11:43 +02:00
|
|
|
mpd_uint16 chunkLeft;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *data;
|
2004-05-10 16:06:23 +02:00
|
|
|
size_t datalen;
|
2007-01-14 04:07:53 +01:00
|
|
|
static char *convBuffer;
|
2008-03-26 11:38:07 +01:00
|
|
|
static size_t convBufferLen;
|
2004-05-10 16:06:23 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (cmpAudioFormat(&(cb->audioFormat), &(dc->audioFormat)) == 0) {
|
2004-05-10 16:06:23 +02:00
|
|
|
data = dataIn;
|
|
|
|
datalen = dataInLen;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2007-05-23 01:11:36 +02:00
|
|
|
datalen = pcm_sizeOfConvBuffer(&(dc->audioFormat), dataInLen,
|
|
|
|
&(cb->audioFormat));
|
2006-07-20 18:02:40 +02:00
|
|
|
if (datalen > convBufferLen) {
|
2008-04-12 06:11:17 +02:00
|
|
|
if (convBuffer != NULL)
|
|
|
|
free(convBuffer);
|
|
|
|
convBuffer = xmalloc(datalen);
|
2004-05-10 16:06:23 +02:00
|
|
|
convBufferLen = datalen;
|
|
|
|
}
|
|
|
|
data = convBuffer;
|
2007-05-26 18:39:55 +02:00
|
|
|
datalen = pcm_convertAudioFormat(&(dc->audioFormat), dataIn,
|
|
|
|
dataInLen, &(cb->audioFormat),
|
|
|
|
data, &(cb->convState));
|
2004-05-10 16:06:23 +02:00
|
|
|
}
|
2004-05-07 04:42:49 +02:00
|
|
|
|
2006-07-27 02:50:59 +02:00
|
|
|
if (replayGainInfo && (replayGainState != REPLAYGAIN_OFF))
|
2005-03-19 15:30:07 +01:00
|
|
|
doReplayGain(replayGainInfo, data, datalen, &cb->audioFormat);
|
2006-07-27 02:50:59 +02:00
|
|
|
else if (normalizationEnabled)
|
2006-07-22 02:53:37 +02:00
|
|
|
normalizeData(data, datalen, &cb->audioFormat);
|
2004-11-02 20:56:59 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (datalen) {
|
2008-04-12 06:13:24 +02:00
|
|
|
OutputBufferChunk *chunk;
|
2008-04-12 06:11:23 +02:00
|
|
|
int chunk_index = tailChunk(cb, inStream,
|
|
|
|
dc, seekable,
|
|
|
|
data_time, bitRate);
|
|
|
|
if (chunk_index < 0)
|
|
|
|
return chunk_index;
|
2004-05-07 04:42:49 +02:00
|
|
|
|
2008-04-12 06:13:24 +02:00
|
|
|
chunk = outputBufferGetChunk(cb, chunk_index);
|
|
|
|
|
2008-04-12 06:13:30 +02:00
|
|
|
chunkLeft = sizeof(chunk->data) - chunk->chunkSize;
|
2006-07-20 18:02:40 +02:00
|
|
|
dataToSend = datalen > chunkLeft ? chunkLeft : datalen;
|
2004-05-07 04:42:49 +02:00
|
|
|
|
2008-04-12 06:13:24 +02:00
|
|
|
memcpy(chunk->data + chunk->chunkSize, data, dataToSend);
|
|
|
|
chunk->chunkSize += dataToSend;
|
2006-07-20 18:02:40 +02:00
|
|
|
datalen -= dataToSend;
|
|
|
|
data += dataToSend;
|
2004-05-07 21:11:43 +02:00
|
|
|
|
2008-04-12 06:13:30 +02:00
|
|
|
if (chunk->chunkSize == sizeof(chunk->data)) {
|
2004-05-07 21:11:43 +02:00
|
|
|
flushOutputBuffer(cb);
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
2004-05-07 04:42:49 +02:00
|
|
|
|
2004-05-07 21:11:43 +02:00
|
|
|
return 0;
|
2004-05-07 04:42:49 +02:00
|
|
|
}
|
2004-06-06 18:42:14 +02:00
|
|
|
|
2008-04-12 06:18:19 +02:00
|
|
|
void output_buffer_skip(OutputBuffer * cb, unsigned num)
|
|
|
|
{
|
|
|
|
int i = outputBufferAbsolute(cb, num);
|
|
|
|
if (i >= 0)
|
|
|
|
cb->begin = i;
|
|
|
|
}
|