2006-03-16 07:52:46 +01: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)
|
2006-03-16 07:52:46 +01:00
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* OggFLAC support (half-stolen from flac_plugin.c :))
|
|
|
|
* (c) 2005 by Eric Wong <normalperson@yhbt.net>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2006-12-04 05:45:47 +01:00
|
|
|
#include "_flac_common.h"
|
2006-03-16 07:52:46 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_OGGFLAC
|
|
|
|
|
|
|
|
#include "_ogg_common.h"
|
|
|
|
|
|
|
|
#include "../utils.h"
|
|
|
|
#include "../log.h"
|
|
|
|
#include "../pcm_utils.h"
|
|
|
|
#include "../inputStream.h"
|
|
|
|
#include "../outputBuffer.h"
|
|
|
|
#include "../replayGain.h"
|
|
|
|
#include "../audio.h"
|
2008-01-03 08:29:49 +01:00
|
|
|
#include "../os_compat.h"
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2008-01-01 11:09:56 +01:00
|
|
|
static void oggflac_cleanup(FlacData * data,
|
2006-07-20 18:02:40 +02:00
|
|
|
OggFLAC__SeekableStreamDecoder * decoder)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
|
|
|
if (data->replayGainInfo)
|
|
|
|
freeReplayGainInfo(data->replayGainInfo);
|
|
|
|
if (decoder)
|
|
|
|
OggFLAC__seekable_stream_decoder_delete(decoder);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static OggFLAC__SeekableStreamDecoderReadStatus of_read_cb(const
|
|
|
|
OggFLAC__SeekableStreamDecoder
|
|
|
|
* decoder,
|
|
|
|
FLAC__byte buf[],
|
|
|
|
unsigned *bytes,
|
|
|
|
void *fdata)
|
|
|
|
{
|
|
|
|
FlacData *data = (FlacData *) fdata;
|
2006-03-16 07:52:46 +01:00
|
|
|
size_t r;
|
|
|
|
|
|
|
|
while (1) {
|
2006-07-20 18:02:40 +02:00
|
|
|
r = readFromInputStream(data->inStream, (void *)buf, 1, *bytes);
|
2006-03-16 07:52:46 +01:00
|
|
|
if (r == 0 && !inputStreamAtEOF(data->inStream) &&
|
2006-07-20 18:02:40 +02:00
|
|
|
!data->dc->stop)
|
2006-03-16 07:52:46 +01:00
|
|
|
my_usleep(10000);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*bytes = r;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2006-03-16 07:52:46 +01:00
|
|
|
if (r == 0 && !inputStreamAtEOF(data->inStream) && !data->dc->stop)
|
|
|
|
return OggFLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
return OggFLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK;
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static OggFLAC__SeekableStreamDecoderSeekStatus of_seek_cb(const
|
|
|
|
OggFLAC__SeekableStreamDecoder
|
|
|
|
* decoder,
|
|
|
|
FLAC__uint64 offset,
|
|
|
|
void *fdata)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
FlacData *data = (FlacData *) fdata;
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (seekInputStream(data->inStream, offset, SEEK_SET) < 0) {
|
|
|
|
return OggFLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
|
|
|
|
}
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
return OggFLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK;
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static OggFLAC__SeekableStreamDecoderTellStatus of_tell_cb(const
|
|
|
|
OggFLAC__SeekableStreamDecoder
|
|
|
|
* decoder,
|
|
|
|
FLAC__uint64 *
|
|
|
|
offset, void *fdata)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
FlacData *data = (FlacData *) fdata;
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
*offset = (long)(data->inStream->offset);
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
return OggFLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK;
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static OggFLAC__SeekableStreamDecoderLengthStatus of_length_cb(const
|
|
|
|
OggFLAC__SeekableStreamDecoder
|
|
|
|
* decoder,
|
|
|
|
FLAC__uint64 *
|
|
|
|
length,
|
|
|
|
void *fdata)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
FlacData *data = (FlacData *) fdata;
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
*length = (size_t) (data->inStream->size);
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
return OggFLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK;
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static FLAC__bool of_EOF_cb(const OggFLAC__SeekableStreamDecoder * decoder,
|
2006-07-20 18:02:40 +02:00
|
|
|
void *fdata)
|
|
|
|
{
|
|
|
|
FlacData *data = (FlacData *) fdata;
|
2006-03-16 07:52:46 +01:00
|
|
|
|
|
|
|
if (inputStreamAtEOF(data->inStream) == 1)
|
2006-07-20 18:02:40 +02:00
|
|
|
return true;
|
2006-03-16 07:52:46 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void of_error_cb(const OggFLAC__SeekableStreamDecoder * decoder,
|
|
|
|
FLAC__StreamDecoderErrorStatus status, void *fdata)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
flac_error_common_cb("oggflac", status, (FlacData *) fdata);
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void oggflacPrintErroredState(OggFLAC__SeekableStreamDecoderState state)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (state) {
|
2006-03-16 07:52:46 +01:00
|
|
|
case OggFLAC__SEEKABLE_STREAM_DECODER_MEMORY_ALLOCATION_ERROR:
|
|
|
|
ERROR("oggflac allocation error\n");
|
|
|
|
break;
|
|
|
|
case OggFLAC__SEEKABLE_STREAM_DECODER_READ_ERROR:
|
|
|
|
ERROR("oggflac read error\n");
|
|
|
|
break;
|
|
|
|
case OggFLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR:
|
|
|
|
ERROR("oggflac seek error\n");
|
|
|
|
break;
|
|
|
|
case OggFLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR:
|
|
|
|
ERROR("oggflac seekable stream error\n");
|
|
|
|
break;
|
|
|
|
case OggFLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED:
|
|
|
|
ERROR("oggflac decoder already initialized\n");
|
|
|
|
break;
|
|
|
|
case OggFLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK:
|
|
|
|
ERROR("invalid oggflac callback\n");
|
|
|
|
break;
|
|
|
|
case OggFLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED:
|
|
|
|
ERROR("oggflac decoder uninitialized\n");
|
|
|
|
break;
|
|
|
|
case OggFLAC__SEEKABLE_STREAM_DECODER_OK:
|
|
|
|
case OggFLAC__SEEKABLE_STREAM_DECODER_SEEKING:
|
|
|
|
case OggFLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static FLAC__StreamDecoderWriteStatus oggflacWrite(const
|
|
|
|
OggFLAC__SeekableStreamDecoder
|
|
|
|
* decoder,
|
|
|
|
const FLAC__Frame * frame,
|
|
|
|
const FLAC__int32 *
|
|
|
|
const buf[], void *vdata)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
FlacData *data = (FlacData *) vdata;
|
2006-03-16 07:52:46 +01:00
|
|
|
FLAC__uint32 samples = frame->header.blocksize;
|
|
|
|
FLAC__uint16 u16;
|
2006-07-20 18:02:40 +02:00
|
|
|
unsigned char *uc;
|
2008-04-12 06:06:39 +02:00
|
|
|
unsigned int c_samp, c_chan;
|
2006-03-16 07:52:46 +01:00
|
|
|
int i;
|
|
|
|
float timeChange;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
timeChange = ((float)samples) / frame->header.sample_rate;
|
|
|
|
data->time += timeChange;
|
2006-03-16 07:52:46 +01:00
|
|
|
|
|
|
|
/* ogg123 uses a complicated method of calculating bitrate
|
|
|
|
* with averaging which I'm not too fond of.
|
|
|
|
* (waste of memory/CPU cycles, especially given this is _lossless_)
|
|
|
|
* a get_decode_position() is not available in OggFLAC, either
|
|
|
|
*
|
|
|
|
* this does not give an accurate bitrate:
|
|
|
|
* (bytes_last_read was set in the read callback)
|
2006-07-20 18:02:40 +02:00
|
|
|
data->bitRate = ((8.0 * data->bytes_last_read *
|
|
|
|
frame->header.sample_rate)
|
|
|
|
/((float)samples * 1000)) + 0.5;
|
|
|
|
*/
|
|
|
|
|
2008-04-12 06:06:28 +02:00
|
|
|
for (c_samp = 0; c_samp < frame->header.blocksize; c_samp++) {
|
2006-07-20 18:02:40 +02:00
|
|
|
for (c_chan = 0; c_chan < frame->header.channels;
|
2008-04-12 06:06:28 +02:00
|
|
|
c_chan++) {
|
2006-03-16 07:52:46 +01:00
|
|
|
u16 = buf[c_chan][c_samp];
|
|
|
|
uc = (unsigned char *)&u16;
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < (data->dc->audioFormat.bits / 8); i++) {
|
|
|
|
if (data->chunk_length >= FLAC_CHUNK_SIZE) {
|
|
|
|
if (flacSendChunk(data) < 0) {
|
|
|
|
return
|
|
|
|
FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
data->chunk_length = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (data->dc->seek) {
|
|
|
|
return
|
|
|
|
FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
data->chunk[data->chunk_length++] = *(uc++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* used by TagDup */
|
2006-07-20 18:02:40 +02:00
|
|
|
static void of_metadata_dup_cb(const OggFLAC__SeekableStreamDecoder * decoder,
|
|
|
|
const FLAC__StreamMetadata * block, void *vdata)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
FlacData *data = (FlacData *) vdata;
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (block->type) {
|
|
|
|
case FLAC__METADATA_TYPE_STREAMINFO:
|
|
|
|
if (!data->tag)
|
|
|
|
data->tag = newMpdTag();
|
2006-03-16 07:52:46 +01:00
|
|
|
data->tag->time = ((float)block->data.stream_info.
|
2006-07-20 18:02:40 +02:00
|
|
|
total_samples) /
|
|
|
|
block->data.stream_info.sample_rate + 0.5;
|
2006-03-16 07:52:46 +01:00
|
|
|
return;
|
2006-07-20 18:02:40 +02:00
|
|
|
case FLAC__METADATA_TYPE_VORBIS_COMMENT:
|
|
|
|
copyVorbisCommentBlockToMpdTag(block, data->tag);
|
2006-03-16 07:52:46 +01:00
|
|
|
default:
|
|
|
|
break;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* used by decode */
|
|
|
|
static void of_metadata_decode_cb(const OggFLAC__SeekableStreamDecoder * dec,
|
2006-07-20 18:02:40 +02:00
|
|
|
const FLAC__StreamMetadata * block,
|
|
|
|
void *vdata)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
flac_metadata_common_cb(block, (FlacData *) vdata);
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static OggFLAC__SeekableStreamDecoder
|
2006-07-20 20:53:56 +02:00
|
|
|
* full_decoder_init_and_read_metadata(FlacData * data,
|
|
|
|
unsigned int metadata_only)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
OggFLAC__SeekableStreamDecoder *decoder = NULL;
|
2006-03-16 07:52:46 +01:00
|
|
|
unsigned int s = 1;
|
|
|
|
|
|
|
|
if (!(decoder = OggFLAC__seekable_stream_decoder_new()))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (metadata_only) {
|
2006-07-20 18:02:40 +02:00
|
|
|
s &= OggFLAC__seekable_stream_decoder_set_metadata_callback
|
|
|
|
(decoder, of_metadata_dup_cb);
|
|
|
|
s &= OggFLAC__seekable_stream_decoder_set_metadata_respond
|
|
|
|
(decoder, FLAC__METADATA_TYPE_STREAMINFO);
|
2006-03-16 07:52:46 +01:00
|
|
|
} else {
|
2006-07-20 18:02:40 +02:00
|
|
|
s &= OggFLAC__seekable_stream_decoder_set_metadata_callback
|
|
|
|
(decoder, of_metadata_decode_cb);
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2006-03-16 07:52:46 +01:00
|
|
|
s &= OggFLAC__seekable_stream_decoder_set_read_callback(decoder,
|
2006-07-20 18:02:40 +02:00
|
|
|
of_read_cb);
|
2006-03-16 07:52:46 +01:00
|
|
|
s &= OggFLAC__seekable_stream_decoder_set_seek_callback(decoder,
|
2006-07-20 18:02:40 +02:00
|
|
|
of_seek_cb);
|
2006-03-16 07:52:46 +01:00
|
|
|
s &= OggFLAC__seekable_stream_decoder_set_tell_callback(decoder,
|
2006-07-20 18:02:40 +02:00
|
|
|
of_tell_cb);
|
2006-03-16 07:52:46 +01:00
|
|
|
s &= OggFLAC__seekable_stream_decoder_set_length_callback(decoder,
|
2006-07-20 18:02:40 +02:00
|
|
|
of_length_cb);
|
2006-03-16 07:52:46 +01:00
|
|
|
s &= OggFLAC__seekable_stream_decoder_set_eof_callback(decoder,
|
2006-07-20 18:02:40 +02:00
|
|
|
of_EOF_cb);
|
2006-03-16 07:52:46 +01:00
|
|
|
s &= OggFLAC__seekable_stream_decoder_set_write_callback(decoder,
|
2006-07-20 18:02:40 +02:00
|
|
|
oggflacWrite);
|
2006-03-16 07:52:46 +01:00
|
|
|
s &= OggFLAC__seekable_stream_decoder_set_metadata_respond(decoder,
|
2006-07-20 18:02:40 +02:00
|
|
|
FLAC__METADATA_TYPE_VORBIS_COMMENT);
|
2006-03-16 07:52:46 +01:00
|
|
|
s &= OggFLAC__seekable_stream_decoder_set_error_callback(decoder,
|
2006-07-20 18:02:40 +02:00
|
|
|
of_error_cb);
|
2006-03-16 07:52:46 +01:00
|
|
|
s &= OggFLAC__seekable_stream_decoder_set_client_data(decoder,
|
2006-07-20 18:02:40 +02:00
|
|
|
(void *)data);
|
2006-03-16 07:52:46 +01:00
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
ERROR("oggflac problem before init()\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (OggFLAC__seekable_stream_decoder_init(decoder) !=
|
2006-07-20 18:02:40 +02:00
|
|
|
OggFLAC__SEEKABLE_STREAM_DECODER_OK) {
|
2006-03-16 07:52:46 +01:00
|
|
|
ERROR("oggflac problem doing init()\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!OggFLAC__seekable_stream_decoder_process_until_end_of_metadata
|
|
|
|
(decoder)) {
|
2006-03-16 07:52:46 +01:00
|
|
|
ERROR("oggflac problem reading metadata\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
return decoder;
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2006-08-20 12:13:54 +02:00
|
|
|
fail:
|
2006-07-20 18:02:40 +02:00
|
|
|
oggflacPrintErroredState(OggFLAC__seekable_stream_decoder_get_state
|
|
|
|
(decoder));
|
2006-03-16 07:52:46 +01:00
|
|
|
OggFLAC__seekable_stream_decoder_delete(decoder);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* public functions: */
|
2006-07-20 18:02:40 +02:00
|
|
|
static MpdTag *oggflac_TagDup(char *file)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
|
|
|
InputStream inStream;
|
2006-07-20 18:02:40 +02:00
|
|
|
OggFLAC__SeekableStreamDecoder *decoder;
|
2006-03-16 07:52:46 +01:00
|
|
|
FlacData data;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2006-03-16 07:52:46 +01:00
|
|
|
if (openInputStream(&inStream, file) < 0)
|
|
|
|
return NULL;
|
|
|
|
if (ogg_stream_type_detect(&inStream) != FLAC) {
|
|
|
|
closeInputStream(&inStream);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
init_FlacData(&data, NULL, NULL, &inStream);
|
|
|
|
|
|
|
|
/* errors here won't matter,
|
|
|
|
* data.tag will be set or unset, that's all we care about */
|
2006-07-20 18:02:40 +02:00
|
|
|
decoder = full_decoder_init_and_read_metadata(&data, 1);
|
|
|
|
|
2008-01-01 11:09:56 +01:00
|
|
|
oggflac_cleanup(&data, decoder);
|
|
|
|
closeInputStream(&inStream);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2006-03-16 07:52:46 +01:00
|
|
|
return data.tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int oggflac_try_decode(InputStream * inStream)
|
|
|
|
{
|
|
|
|
return (ogg_stream_type_detect(inStream) == FLAC) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int oggflac_decode(OutputBuffer * cb, DecoderControl * dc,
|
2006-07-20 18:02:40 +02:00
|
|
|
InputStream * inStream)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
OggFLAC__SeekableStreamDecoder *decoder = NULL;
|
2006-03-16 07:52:46 +01:00
|
|
|
FlacData data;
|
2006-07-20 18:02:40 +02:00
|
|
|
int ret = 0;
|
2006-03-16 07:52:46 +01:00
|
|
|
|
|
|
|
init_FlacData(&data, cb, dc, inStream);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!(decoder = full_decoder_init_and_read_metadata(&data, 0))) {
|
|
|
|
ret = -1;
|
|
|
|
goto fail;
|
|
|
|
}
|
2006-03-16 07:52:46 +01:00
|
|
|
|
|
|
|
dc->state = DECODE_STATE_DECODE;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (1) {
|
2006-03-16 07:52:46 +01:00
|
|
|
OggFLAC__seekable_stream_decoder_process_single(decoder);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (OggFLAC__seekable_stream_decoder_get_state(decoder) !=
|
|
|
|
OggFLAC__SEEKABLE_STREAM_DECODER_OK) {
|
2006-03-16 07:52:46 +01:00
|
|
|
break;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (dc->seek) {
|
|
|
|
FLAC__uint64 sampleToSeek = dc->seekWhere *
|
|
|
|
dc->audioFormat.sampleRate + 0.5;
|
|
|
|
if (OggFLAC__seekable_stream_decoder_seek_absolute
|
|
|
|
(decoder, sampleToSeek)) {
|
|
|
|
clearOutputBuffer(cb);
|
|
|
|
data.time = ((float)sampleToSeek) /
|
|
|
|
dc->audioFormat.sampleRate;
|
2006-03-16 07:52:46 +01:00
|
|
|
data.position = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
|
|
|
dc->seekError = 1;
|
2006-03-16 07:52:46 +01:00
|
|
|
dc->seek = 0;
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
decoder_wakeup_player();
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
if (!dc->stop) {
|
|
|
|
oggflacPrintErroredState
|
|
|
|
(OggFLAC__seekable_stream_decoder_get_state(decoder));
|
2006-03-16 07:52:46 +01:00
|
|
|
OggFLAC__seekable_stream_decoder_finish(decoder);
|
|
|
|
}
|
|
|
|
/* send last little bit */
|
2006-07-20 18:02:40 +02:00
|
|
|
if (data.chunk_length > 0 && !dc->stop) {
|
2006-03-16 07:52:46 +01:00
|
|
|
flacSendChunk(&data);
|
|
|
|
flushOutputBuffer(data.cb);
|
|
|
|
}
|
|
|
|
|
2006-08-20 12:13:54 +02:00
|
|
|
fail:
|
2008-01-01 11:09:56 +01:00
|
|
|
oggflac_cleanup(&data, decoder);
|
2006-03-16 07:52:46 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-03-20 06:27:04 +01:00
|
|
|
static const char *oggflac_Suffixes[] = { "ogg", "oga",NULL };
|
2008-02-05 11:17:33 +01:00
|
|
|
static const char *oggflac_mime_types[] = { "audio/x-flac+ogg",
|
|
|
|
"application/ogg",
|
|
|
|
"application/x-ogg",
|
|
|
|
NULL };
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
InputPlugin oggflacPlugin = {
|
2006-03-16 07:52:46 +01:00
|
|
|
"oggflac",
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
oggflac_try_decode,
|
|
|
|
oggflac_decode,
|
|
|
|
NULL,
|
|
|
|
oggflac_TagDup,
|
|
|
|
INPUT_PLUGIN_STREAM_URL | INPUT_PLUGIN_STREAM_FILE,
|
|
|
|
oggflac_Suffixes,
|
|
|
|
oggflac_mime_types
|
|
|
|
};
|
|
|
|
|
2006-07-20 20:53:56 +02:00
|
|
|
#else /* !HAVE_FLAC */
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2007-01-14 04:07:53 +01:00
|
|
|
InputPlugin oggflacPlugin;
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2006-07-20 20:53:56 +02:00
|
|
|
#endif /* HAVE_OGGFLAC */
|