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"
|
2008-04-13 03:16:03 +02:00
|
|
|
#include "playerData.h"
|
2004-05-07 04:42:49 +02:00
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
void ob_init(unsigned int size)
|
2008-03-26 11:38:12 +01:00
|
|
|
{
|
2008-04-12 06:18:04 +02:00
|
|
|
assert(size > 0);
|
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
memset(&ob.convState, 0, sizeof(ConvState));
|
|
|
|
ob.chunks = xmalloc(size * sizeof(*ob.chunks));
|
|
|
|
ob.size = size;
|
|
|
|
ob.begin = 0;
|
|
|
|
ob.end = 0;
|
2008-04-15 07:57:22 +02:00
|
|
|
ob.lazy = 0;
|
2008-04-13 03:16:27 +02:00
|
|
|
ob.chunks[0].chunkSize = 0;
|
2004-06-07 00:13:23 +02:00
|
|
|
}
|
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
void ob_free(void)
|
2008-04-12 06:18:38 +02:00
|
|
|
{
|
2008-04-13 03:16:27 +02:00
|
|
|
assert(ob.chunks != NULL);
|
|
|
|
free(ob.chunks);
|
2008-04-12 06:18:38 +02:00
|
|
|
}
|
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
void ob_clear(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-04-13 03:16:27 +02:00
|
|
|
ob.end = ob.begin;
|
|
|
|
ob.chunks[ob.end].chunkSize = 0;
|
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-13 03:16:15 +02:00
|
|
|
static inline unsigned successor(unsigned i)
|
2008-04-12 06:14:55 +02:00
|
|
|
{
|
2008-04-13 03:16:27 +02:00
|
|
|
assert(i <= ob.size);
|
2008-04-12 06:14:55 +02:00
|
|
|
|
|
|
|
++i;
|
2008-04-13 03:16:27 +02:00
|
|
|
return i == ob.size ? 0 : i;
|
2008-04-12 06:14:55 +02:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:21:09 +02:00
|
|
|
/**
|
|
|
|
* Mark the tail chunk as "full" and wake up the player if is waiting
|
|
|
|
* for the decoder.
|
|
|
|
*/
|
2008-04-13 03:16:15 +02:00
|
|
|
static void output_buffer_expand(unsigned i)
|
2008-04-12 06:21:09 +02:00
|
|
|
{
|
2008-04-15 07:57:22 +02:00
|
|
|
int was_empty = !ob.lazy || ob_is_empty();
|
2008-04-12 06:21:09 +02:00
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
assert(i == (ob.end + 1) % ob.size);
|
|
|
|
assert(i != ob.end);
|
2008-04-12 06:21:09 +02:00
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
ob.end = i;
|
|
|
|
ob.chunks[i].chunkSize = 0;
|
2008-04-12 06:21:09 +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. */
|
2008-08-26 08:27:04 +02:00
|
|
|
notify_signal(&pc.notify);
|
2008-04-12 06:21:09 +02:00
|
|
|
}
|
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
void ob_flush(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-04-13 03:16:27 +02:00
|
|
|
ob_chunk *chunk = ob_get_chunk(ob.end);
|
2008-04-12 06:21:13 +02:00
|
|
|
|
|
|
|
if (chunk->chunkSize > 0) {
|
2008-04-13 03:16:27 +02:00
|
|
|
unsigned int next = successor(ob.end);
|
|
|
|
if (next == ob.begin)
|
2008-04-12 06:21:13 +02:00
|
|
|
/* all buffers are full; we have to wait for
|
|
|
|
the player to free one, so don't flush
|
|
|
|
right now */
|
|
|
|
return;
|
|
|
|
|
2008-04-13 03:16:15 +02:00
|
|
|
output_buffer_expand(next);
|
2008-04-12 06:21:13 +02:00
|
|
|
}
|
2004-05-07 21:11:43 +02:00
|
|
|
}
|
|
|
|
|
2008-04-15 07:57:22 +02:00
|
|
|
void ob_set_lazy(int lazy)
|
|
|
|
{
|
|
|
|
ob.lazy = lazy;
|
|
|
|
}
|
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
int ob_is_empty(void)
|
2008-04-12 06:12:47 +02:00
|
|
|
{
|
2008-04-13 03:16:27 +02:00
|
|
|
return ob.begin == ob.end;
|
2008-04-12 06:12:47 +02:00
|
|
|
}
|
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
void ob_shift(void)
|
2008-04-12 06:13:51 +02:00
|
|
|
{
|
2008-04-13 03:16:27 +02:00
|
|
|
assert(ob.begin != ob.end);
|
|
|
|
assert(ob.begin < ob.size);
|
2008-04-12 06:13:51 +02:00
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
ob.begin = successor(ob.begin);
|
2008-04-12 06:13:51 +02:00
|
|
|
}
|
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
unsigned int ob_relative(const unsigned i)
|
2008-04-12 06:11:41 +02:00
|
|
|
{
|
2008-04-13 03:16:27 +02:00
|
|
|
if (i >= ob.begin)
|
|
|
|
return i - ob.begin;
|
2008-04-12 06:11:41 +02:00
|
|
|
else
|
2008-04-13 03:16:27 +02:00
|
|
|
return i + ob.size - ob.begin;
|
2008-04-12 06:12:53 +02:00
|
|
|
}
|
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
unsigned ob_available(void)
|
2008-04-12 06:12:53 +02:00
|
|
|
{
|
2008-04-13 03:16:27 +02:00
|
|
|
return ob_relative(ob.end);
|
2008-04-12 06:11:41 +02:00
|
|
|
}
|
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
int ob_absolute(const unsigned relative)
|
2008-04-12 06:12:42 +02:00
|
|
|
{
|
|
|
|
unsigned i, max;
|
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
max = ob.end;
|
|
|
|
if (max < ob.begin)
|
|
|
|
max += ob.size;
|
|
|
|
i = (unsigned)ob.begin + relative;
|
2008-04-12 06:12:42 +02:00
|
|
|
if (i >= max)
|
|
|
|
return -1;
|
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
if (i >= ob.size)
|
|
|
|
i -= ob.size;
|
2008-04-12 06:12:42 +02:00
|
|
|
|
|
|
|
return (int)i;
|
|
|
|
}
|
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
ob_chunk * ob_get_chunk(const unsigned i)
|
2008-04-12 06:13:11 +02:00
|
|
|
{
|
2008-04-13 03:16:27 +02:00
|
|
|
assert(i < ob.size);
|
2008-04-12 06:13:11 +02:00
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
return &ob.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.
|
|
|
|
*/
|
2008-04-13 03:16:15 +02:00
|
|
|
static int tailChunk(InputStream * inStream,
|
2008-04-13 03:16:03 +02:00
|
|
|
int seekable, float data_time, mpd_uint16 bitRate)
|
2008-04-12 06:11:23 +02:00
|
|
|
{
|
|
|
|
unsigned int next;
|
2008-04-13 03:16:27 +02:00
|
|
|
ob_chunk *chunk;
|
2008-04-12 06:11:23 +02:00
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
chunk = ob_get_chunk(ob.end);
|
2008-04-12 06:21:13 +02:00
|
|
|
assert(chunk->chunkSize <= sizeof(chunk->data));
|
|
|
|
if (chunk->chunkSize == sizeof(chunk->data)) {
|
|
|
|
/* this chunk is full; allocate a new chunk */
|
2008-04-13 03:16:27 +02:00
|
|
|
next = successor(ob.end);
|
|
|
|
while (ob.begin == next) {
|
2008-04-12 06:21:13 +02:00
|
|
|
/* all chunks are full of decoded data; wait
|
|
|
|
for the player to free one */
|
|
|
|
|
2008-08-26 08:27:04 +02:00
|
|
|
if (dc.command == DECODE_COMMAND_STOP)
|
2008-04-12 06:21:18 +02:00
|
|
|
return OUTPUT_BUFFER_DC_STOP;
|
|
|
|
|
2008-08-26 08:27:04 +02:00
|
|
|
if (dc.command == DECODE_COMMAND_SEEK) {
|
2008-04-12 06:21:13 +02:00
|
|
|
if (seekable) {
|
|
|
|
return OUTPUT_BUFFER_DC_SEEK;
|
|
|
|
} else {
|
2008-04-13 03:16:03 +02:00
|
|
|
dc.seekError = 1;
|
2008-08-26 08:27:04 +02:00
|
|
|
dc_command_finished();
|
2008-04-12 06:21:13 +02:00
|
|
|
}
|
|
|
|
}
|
2008-04-13 03:16:03 +02:00
|
|
|
if (!inStream || bufferInputStream(inStream) <= 0) {
|
2008-08-26 08:27:04 +02:00
|
|
|
notify_wait(&dc.notify);
|
|
|
|
notify_signal(&pc.notify);
|
2008-04-12 06:11:23 +02:00
|
|
|
}
|
|
|
|
}
|
2008-04-12 06:21:13 +02:00
|
|
|
|
2008-04-13 03:16:15 +02:00
|
|
|
output_buffer_expand(next);
|
2008-04-13 03:16:27 +02:00
|
|
|
chunk = ob_get_chunk(next);
|
2008-04-12 06:21:13 +02:00
|
|
|
assert(chunk->chunkSize == 0);
|
2008-04-12 06:11:23 +02:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:21:13 +02:00
|
|
|
if (chunk->chunkSize == 0) {
|
|
|
|
/* if the chunk is empty, nobody has set bitRate and
|
|
|
|
times yet */
|
|
|
|
|
|
|
|
chunk->bitRate = bitRate;
|
|
|
|
chunk->times = data_time;
|
|
|
|
}
|
2008-04-12 06:11:23 +02:00
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
return ob.end;
|
2008-04-12 06:11:23 +02:00
|
|
|
}
|
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
int ob_send(InputStream * inStream,
|
2008-04-13 03:16:03 +02:00
|
|
|
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
|
|
|
{
|
2008-04-12 06:21:22 +02:00
|
|
|
size_t dataToSend;
|
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;
|
2008-04-13 03:16:27 +02:00
|
|
|
ob_chunk *chunk = NULL;
|
2004-05-10 16:06:23 +02:00
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
if (cmpAudioFormat(&(ob.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 {
|
2008-04-13 03:16:03 +02:00
|
|
|
datalen = pcm_sizeOfConvBuffer(&(dc.audioFormat), dataInLen,
|
2008-04-13 03:16:27 +02:00
|
|
|
&(ob.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;
|
2008-04-13 03:16:03 +02:00
|
|
|
datalen = pcm_convertAudioFormat(&(dc.audioFormat), dataIn,
|
2008-04-13 03:16:27 +02:00
|
|
|
dataInLen, &(ob.audioFormat),
|
|
|
|
data, &(ob.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))
|
2008-04-13 03:16:27 +02:00
|
|
|
doReplayGain(replayGainInfo, data, datalen, &ob.audioFormat);
|
2006-07-27 02:50:59 +02:00
|
|
|
else if (normalizationEnabled)
|
2008-04-13 03:16:27 +02:00
|
|
|
normalizeData(data, datalen, &ob.audioFormat);
|
2004-11-02 20:56:59 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (datalen) {
|
2008-04-13 03:16:15 +02:00
|
|
|
int chunk_index = tailChunk(inStream, seekable,
|
2008-04-12 06:11:23 +02:00
|
|
|
data_time, bitRate);
|
|
|
|
if (chunk_index < 0)
|
|
|
|
return chunk_index;
|
2004-05-07 04:42:49 +02:00
|
|
|
|
2008-04-13 03:16:27 +02:00
|
|
|
chunk = ob_get_chunk(chunk_index);
|
2008-04-12 06:13:24 +02:00
|
|
|
|
2008-04-12 06:21:22 +02:00
|
|
|
dataToSend = sizeof(chunk->data) - chunk->chunkSize;
|
|
|
|
if (dataToSend > datalen)
|
|
|
|
dataToSend = 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 04:42:49 +02:00
|
|
|
|
2008-04-12 06:21:34 +02:00
|
|
|
if (chunk != NULL && chunk->chunkSize == sizeof(chunk->data))
|
2008-04-13 03:16:27 +02:00
|
|
|
ob_flush();
|
2008-04-12 06:21:34 +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-13 03:16:27 +02:00
|
|
|
void ob_skip(unsigned num)
|
2008-04-12 06:18:19 +02:00
|
|
|
{
|
2008-04-13 03:16:27 +02:00
|
|
|
int i = ob_absolute(num);
|
2008-04-12 06:18:19 +02:00
|
|
|
if (i >= 0)
|
2008-04-13 03:16:27 +02:00
|
|
|
ob.begin = i;
|
2008-04-12 06:18:19 +02:00
|
|
|
}
|