callback based encoding

This commit is contained in:
badaix 2015-07-26 18:12:06 +02:00
parent b936bffeff
commit c0057d2575
9 changed files with 193 additions and 75 deletions

View file

@ -18,16 +18,29 @@
#ifndef ENCODER_H #ifndef ENCODER_H
#define ENCODER_H #define ENCODER_H
#include <string>
#include <memory>
#include "message/pcmChunk.h" #include "message/pcmChunk.h"
#include "message/header.h" #include "message/header.h"
#include "message/sampleFormat.h" #include "message/sampleFormat.h"
#include <string>
class Encoder;
class EncoderListener
{
public:
virtual void onChunkEncoded(const Encoder* encoder, msg::PcmChunk* chunk, double duration) = 0;
};
class Encoder class Encoder
{ {
public: public:
Encoder() : headerChunk_(NULL) Encoder(const std::string& codecOptions = "") : headerChunk_(NULL), codecOptions_(codecOptions)
{ {
} }
@ -37,28 +50,30 @@ public:
delete headerChunk_; delete headerChunk_;
} }
virtual void init(const msg::SampleFormat& format, const std::string codecOptions = "") virtual void init(EncoderListener* listener, const msg::SampleFormat& format)
{ {
sampleFormat_ = format; if (codecOptions_ == "")
codecOptions_ = codecOptions;
if (codecOptions == "")
codecOptions_ = getDefaultOptions(); codecOptions_ = getDefaultOptions();
listener_ = listener;
sampleFormat_ = format;
initEncoder(); initEncoder();
} }
virtual double encode(msg::PcmChunk* chunk) = 0; virtual void encode(const msg::PcmChunk* chunk) = 0;
virtual std::string getAvailableOptions() virtual std::string name() const = 0;
virtual std::string getAvailableOptions() const
{ {
return "No codec options supported"; return "No codec options supported";
} }
virtual std::string getDefaultOptions() virtual std::string getDefaultOptions() const
{ {
return ""; return "";
} }
virtual msg::Header* getHeader() virtual msg::Header* getHeader() const
{ {
return headerChunk_; return headerChunk_;
} }
@ -68,6 +83,7 @@ protected:
msg::SampleFormat sampleFormat_; msg::SampleFormat sampleFormat_;
msg::Header* headerChunk_; msg::Header* headerChunk_;
EncoderListener* listener_;
std::string codecOptions_; std::string codecOptions_;
}; };

65
server/encoderFactory.cpp Normal file
View file

@ -0,0 +1,65 @@
/***
This file is part of snapcast
Copyright (C) 2015 Johannes Pohl
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 3 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, see <http://www.gnu.org/licenses/>.
***/
#include "encoderFactory.h"
#include "common/utils.h"
#include "pcmEncoder.h"
#include "oggEncoder.h"
#include "flacEncoder.h"
using namespace std;
Encoder* EncoderFactory::createEncoder(const std::string& codecSettings) const
{
Encoder* encoder;
std::string codec(codecSettings);
std::string codecOptions;
if (codec.find(":") != std::string::npos)
{
codecOptions = trim_copy(codec.substr(codec.find(":") + 1));
codec = trim_copy(codec.substr(0, codec.find(":")));
}
if (codec == "ogg")
encoder = new OggEncoder(codecOptions);
else if (codec == "pcm")
encoder = new PcmEncoder(codecOptions);
else if (codec == "flac")
encoder = new FlacEncoder(codecOptions);
else
{
cout << "unknown codec: " << codec << "\n";
return NULL;
}
return encoder;
/* try
{
encoder->init(NULL, format, codecOptions);
}
catch (const std::exception& e)
{
cout << "Error: " << e.what() << "\n";
return 1;
}
*/
}

15
server/encoderFactory.h Normal file
View file

@ -0,0 +1,15 @@
#ifndef ENCODER_FACTORY_H
#define ENCODER_FACTORY_H
#include <string>
#include "encoder.h"
class EncoderFactory
{
public:
// EncoderFactory(const std::string& codecSettings);
Encoder* createEncoder(const std::string& codecSettings) const;
};
#endif

View file

@ -26,9 +26,9 @@
using namespace std; using namespace std;
FlacEncoder::FlacEncoder() : Encoder(), encoder_(NULL), pcmBufferSize_(0), encodedSamples_(0) FlacEncoder::FlacEncoder(const std::string& codecOptions) : Encoder(codecOptions), encoder_(NULL), pcmBufferSize_(0), encodedSamples_(0)
{ {
encodedChunk_ = new msg::PcmChunk(); flacChunk_ = new msg::PcmChunk();
headerChunk_ = new msg::Header("flac"); headerChunk_ = new msg::Header("flac");
pcmBuffer_ = (FLAC__int32*)malloc(pcmBufferSize_ * sizeof(FLAC__int32)); pcmBuffer_ = (FLAC__int32*)malloc(pcmBufferSize_ * sizeof(FLAC__int32));
} }
@ -44,55 +44,57 @@ FlacEncoder::~FlacEncoder()
FLAC__stream_encoder_delete(encoder_); FLAC__stream_encoder_delete(encoder_);
} }
delete encodedChunk_; if (flacChunk_)
delete flacChunk_;
free(pcmBuffer_); free(pcmBuffer_);
} }
std::string FlacEncoder::getAvailableOptions() std::string FlacEncoder::getAvailableOptions() const
{ {
return "compression level: [0..8]"; return "compression level: [0..8]";
} }
std::string FlacEncoder::getDefaultOptions() std::string FlacEncoder::getDefaultOptions() const
{ {
return "2"; return "2";
} }
double FlacEncoder::encode(msg::PcmChunk* chunk) std::string FlacEncoder::name() const
{
return "flac";
}
void FlacEncoder::encode(const msg::PcmChunk* chunk)
{ {
int samples = chunk->getSampleCount(); int samples = chunk->getSampleCount();
int frames = chunk->getFrameCount(); int frames = chunk->getFrameCount();
logD << "payload: " << chunk->payloadSize << "\tframes: " << frames << "\tsamples: " << samples << "\tduration: " << chunk->duration<chronos::msec>().count() << "\n"; // logO << "payload: " << chunk->payloadSize << "\tframes: " << frames << "\tsamples: " << samples << "\tduration: " << chunk->duration<chronos::msec>().count() << "\n";
if (pcmBufferSize_ < samples) if (pcmBufferSize_ < samples)
{ {
pcmBufferSize_ = samples; pcmBufferSize_ = samples;
pcmBuffer_ = (FLAC__int32*)realloc(pcmBuffer_, pcmBufferSize_ * sizeof(FLAC__int32)); pcmBuffer_ = (FLAC__int32*)realloc(pcmBuffer_, pcmBufferSize_ * sizeof(FLAC__int32));
} }
for(int i=0; i<samples; i++) for(int i=0; i<samples; i++)
{ {
pcmBuffer_[i] = (FLAC__int32)(((FLAC__int16)(FLAC__int8)chunk->payload[2*i+1] << 8) | (FLAC__int16)(0x00ff&chunk->payload[2*i])); pcmBuffer_[i] = (FLAC__int32)(((FLAC__int16)(FLAC__int8)chunk->payload[2*i+1] << 8) | (FLAC__int16)(0x00ff&chunk->payload[2*i]));
} }
FLAC__stream_encoder_process_interleaved(encoder_, pcmBuffer_, frames); FLAC__stream_encoder_process_interleaved(encoder_, pcmBuffer_, frames);
double resMs = encodedSamples_ / ((double)sampleFormat_.rate / 1000.);
if (encodedSamples_ > 0) if (encodedSamples_ > 0)
{ {
logD << "encoded: " << chunk->payloadSize << "\tframes: " << encodedSamples_ << "\tres: " << resMs << "\n"; double resMs = encodedSamples_ / ((double)sampleFormat_.rate / 1000.);
// logO << "encoded: " << chunk->payloadSize << "\tframes: " << encodedSamples_ << "\tres: " << resMs << "\n";
encodedSamples_ = 0; encodedSamples_ = 0;
chunk->payloadSize = encodedChunk_->payloadSize; listener_->onChunkEncoded(this, flacChunk_, resMs);
chunk->payload = (char*)realloc(chunk->payload, encodedChunk_->payloadSize); flacChunk_ = new msg::PcmChunk();
memcpy(chunk->payload, encodedChunk_->payload, encodedChunk_->payloadSize);
encodedChunk_->payloadSize = 0;
encodedChunk_->payload = (char*)realloc(encodedChunk_->payload, 0);
} }
return resMs;//chunk->duration<chronos::msec>().count();
} }
@ -102,7 +104,7 @@ FLAC__StreamEncoderWriteStatus FlacEncoder::write_callback(const FLAC__StreamEnc
unsigned samples, unsigned samples,
unsigned current_frame) unsigned current_frame)
{ {
logD << "write_callback: " << bytes << ", " << samples << ", " << current_frame << "\n"; // logO << "write_callback: " << bytes << ", " << samples << ", " << current_frame << "\n";
if ((current_frame == 0) && (bytes > 0) && (samples == 0)) if ((current_frame == 0) && (bytes > 0) && (samples == 0))
{ {
headerChunk_->payload = (char*)realloc(headerChunk_->payload, headerChunk_->payloadSize + bytes); headerChunk_->payload = (char*)realloc(headerChunk_->payload, headerChunk_->payloadSize + bytes);
@ -111,9 +113,9 @@ FLAC__StreamEncoderWriteStatus FlacEncoder::write_callback(const FLAC__StreamEnc
} }
else else
{ {
encodedChunk_->payload = (char*)realloc(encodedChunk_->payload, encodedChunk_->payloadSize + bytes); flacChunk_->payload = (char*)realloc(flacChunk_->payload, flacChunk_->payloadSize + bytes);
memcpy(encodedChunk_->payload + encodedChunk_->payloadSize, buffer, bytes); memcpy(flacChunk_->payload + flacChunk_->payloadSize, buffer, bytes);
encodedChunk_->payloadSize += bytes; flacChunk_->payloadSize += bytes;
encodedSamples_ += samples; encodedSamples_ += samples;
} }
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK; return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
@ -128,7 +130,7 @@ FLAC__StreamEncoderWriteStatus write_callback(const FLAC__StreamEncoder *encoder
void *client_data) void *client_data)
{ {
FlacEncoder* flacEncoder = (FlacEncoder*)client_data; FlacEncoder* flacEncoder = (FlacEncoder*)client_data;
return flacEncoder->write_callback(encoder, buffer, bytes, samples, current_frame); return flacEncoder->write_callback(encoder, buffer, bytes, samples, current_frame);
} }
@ -153,7 +155,7 @@ void FlacEncoder::initEncoder()
FLAC__StreamMetadata_VorbisComment_Entry entry; FLAC__StreamMetadata_VorbisComment_Entry entry;
// allocate the encoder // allocate the encoder
if ((encoder_ = FLAC__stream_encoder_new()) == NULL) if ((encoder_ = FLAC__stream_encoder_new()) == NULL)
throw SnapException("error allocating encoder"); throw SnapException("error allocating encoder");
ok &= FLAC__stream_encoder_set_verify(encoder_, true); ok &= FLAC__stream_encoder_set_verify(encoder_, true);
@ -176,10 +178,10 @@ void FlacEncoder::initEncoder()
(metadata_[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)) == NULL || (metadata_[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)) == NULL ||
// there are many tag (vorbiscomment) functions but these are convenient for this particular use: // there are many tag (vorbiscomment) functions but these are convenient for this particular use:
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "TITLE", "SnapStream") || !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "TITLE", "SnapStream") ||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata_[0], entry, false) || !FLAC__metadata_object_vorbiscomment_append_comment(metadata_[0], entry, false) ||
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "VERSION", VERSION) || !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "VERSION", VERSION) ||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata_[0], entry, false) !FLAC__metadata_object_vorbiscomment_append_comment(metadata_[0], entry, false)
) )
throw SnapException("out of memory or tag error"); throw SnapException("out of memory or tag error");
metadata_[1]->length = 1234; // set the padding length metadata_[1]->length = 1234; // set the padding length
@ -189,7 +191,7 @@ void FlacEncoder::initEncoder()
// initialize encoder // initialize encoder
init_status = FLAC__stream_encoder_init_stream(encoder_, ::write_callback, NULL, NULL, NULL, this); init_status = FLAC__stream_encoder_init_stream(encoder_, ::write_callback, NULL, NULL, NULL, this);
if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK)
throw SnapException("ERROR: initializing encoder: " + string(FLAC__StreamEncoderInitStatusString[init_status])); throw SnapException("ERROR: initializing encoder: " + string(FLAC__StreamEncoderInitStatusString[init_status]));
} }

View file

@ -30,24 +30,25 @@
class FlacEncoder : public Encoder class FlacEncoder : public Encoder
{ {
public: public:
FlacEncoder(); FlacEncoder(const std::string& codecOptions = "");
~FlacEncoder(); ~FlacEncoder();
virtual double encode(msg::PcmChunk* chunk); virtual void encode(const msg::PcmChunk* chunk);
virtual std::string getAvailableOptions(); virtual std::string getAvailableOptions() const;
virtual std::string getDefaultOptions(); virtual std::string getDefaultOptions() const;
virtual std::string name() const;
FLAC__StreamEncoderWriteStatus write_callback(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame); FLAC__StreamEncoderWriteStatus write_callback(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame);
protected: protected:
virtual void initEncoder(); virtual void initEncoder();
FLAC__StreamEncoder *encoder_; FLAC__StreamEncoder *encoder_;
FLAC__StreamMetadata *metadata_[2]; FLAC__StreamMetadata *metadata_[2];
FLAC__int32 *pcmBuffer_; FLAC__int32 *pcmBuffer_;
int pcmBufferSize_; int pcmBufferSize_;
msg::PcmChunk* encodedChunk_; msg::PcmChunk* flacChunk_;
size_t encodedSamples_; size_t encodedSamples_;
}; };

View file

@ -28,24 +28,30 @@
using namespace std; using namespace std;
OggEncoder::OggEncoder() : Encoder(), lastGranulepos(0), eos(0) OggEncoder::OggEncoder(const std::string& codecOptions) : Encoder(codecOptions), lastGranulepos(0), eos(0)
{ {
} }
std::string OggEncoder::getAvailableOptions() std::string OggEncoder::getAvailableOptions() const
{ {
return "VBR:[-0.1 - 1.0]"; return "VBR:[-0.1 - 1.0]";
} }
std::string OggEncoder::getDefaultOptions() std::string OggEncoder::getDefaultOptions() const
{ {
return "VBR:0.9"; return "VBR:0.9";
} }
double OggEncoder::encode(msg::PcmChunk* chunk) std::string OggEncoder::name() const
{
return "ogg";
}
void OggEncoder::encode(const msg::PcmChunk* chunk)
{ {
double res = 0; double res = 0;
logD << "payload: " << chunk->payloadSize << "\tframes: " << chunk->getFrameCount() << "\tduration: " << chunk->duration<chronos::msec>().count() << "\n"; logD << "payload: " << chunk->payloadSize << "\tframes: " << chunk->getFrameCount() << "\tduration: " << chunk->duration<chronos::msec>().count() << "\n";
@ -63,6 +69,8 @@ double OggEncoder::encode(msg::PcmChunk* chunk)
/* tell the library how much we actually submitted */ /* tell the library how much we actually submitted */
vorbis_analysis_wrote(&vd, bytes); vorbis_analysis_wrote(&vd, bytes);
msg::PcmChunk* oggChunk = new msg::PcmChunk(chunk->format, 0);
/* vorbis does some data preanalysis, then divvies up blocks for /* vorbis does some data preanalysis, then divvies up blocks for
more involved (potentially parallel) processing. Get a single more involved (potentially parallel) processing. Get a single
block for encoding now */ block for encoding now */
@ -88,31 +96,32 @@ double OggEncoder::encode(msg::PcmChunk* chunk)
size_t nextLen = pos + og.header_len + og.body_len; size_t nextLen = pos + og.header_len + og.body_len;
// make chunk larger // make chunk larger
if (chunk->payloadSize < nextLen) if (oggChunk->payloadSize < nextLen)
chunk->payload = (char*)realloc(chunk->payload, nextLen); oggChunk->payload = (char*)realloc(oggChunk->payload, nextLen);
memcpy(chunk->payload + pos, og.header, og.header_len); memcpy(oggChunk->payload + pos, og.header, og.header_len);
pos += og.header_len; pos += og.header_len;
memcpy(chunk->payload + pos, og.body, og.body_len); memcpy(oggChunk->payload + pos, og.body, og.body_len);
pos += og.body_len; pos += og.body_len;
if (ogg_page_eos(&og)) if (ogg_page_eos(&og))
break; break;
} }
} }
} }
if (res > 0) if (res > 0)
{ {
res /= (sampleFormat_.rate / 1000.); res /= (sampleFormat_.rate / 1000.);
logD << "res: " << res << "\n"; logO << "res: " << res << "\n";
lastGranulepos = os.granulepos; lastGranulepos = os.granulepos;
// make chunk smaller // make oggChunk smaller
chunk->payload = (char*)realloc(chunk->payload, pos); oggChunk->payload = (char*)realloc(oggChunk->payload, pos);
chunk->payloadSize = pos; oggChunk->payloadSize = pos;
listener_->onChunkEncoded(this, oggChunk, res);
} }
else
return res; delete oggChunk;
} }
@ -123,7 +132,7 @@ void OggEncoder::initEncoder()
string mode = trim_copy(codecOptions_.substr(0, codecOptions_.find(":"))); string mode = trim_copy(codecOptions_.substr(0, codecOptions_.find(":")));
if (mode != "VBR") if (mode != "VBR")
throw SnapException("Unsupported codec mode: \"" + mode + "\". Available: \"VBR\""); throw SnapException("Unsupported codec mode: \"" + mode + "\". Available: \"VBR\"");
string qual = trim_copy(codecOptions_.substr(codecOptions_.find(":") + 1)); string qual = trim_copy(codecOptions_.substr(codecOptions_.find(":") + 1));
double quality = 1.0; double quality = 1.0;
try try
@ -138,7 +147,7 @@ void OggEncoder::initEncoder()
{ {
throw SnapException("compression level has to be between -0.1 and 1.0"); throw SnapException("compression level has to be between -0.1 and 1.0");
} }
/********** Encode setup ************/ /********** Encode setup ************/
vorbis_info_init(&vi); vorbis_info_init(&vi);
@ -205,7 +214,7 @@ void OggEncoder::initEncoder()
ogg_packet header; ogg_packet header;
ogg_packet header_comm; ogg_packet header_comm;
ogg_packet header_code; ogg_packet header_code;
vorbis_analysis_headerout(&vd,&vc,&header,&header_comm,&header_code); vorbis_analysis_headerout(&vd,&vc,&header,&header_comm,&header_code);
ogg_stream_packetin(&os,&header); ogg_stream_packetin(&os,&header);
ogg_stream_packetin(&os,&header_comm); ogg_stream_packetin(&os,&header_comm);

View file

@ -25,13 +25,14 @@
class OggEncoder : public Encoder class OggEncoder : public Encoder
{ {
public: public:
OggEncoder(); OggEncoder(const std::string& codecOptions = "");
virtual double encode(msg::PcmChunk* chunk); virtual void encode(const msg::PcmChunk* chunk);
virtual std::string getAvailableOptions(); virtual std::string getAvailableOptions() const;
virtual std::string getDefaultOptions(); virtual std::string getDefaultOptions() const;
virtual std::string name() const;
protected: protected:
virtual void initEncoder(); virtual void initEncoder();
private: private:
ogg_stream_state os; /* take physical pages, weld into a logical ogg_stream_state os; /* take physical pages, weld into a logical

View file

@ -16,17 +16,19 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
***/ ***/
#include <memory>
#include "pcmEncoder.h" #include "pcmEncoder.h"
PcmEncoder::PcmEncoder() : Encoder() PcmEncoder::PcmEncoder(const std::string& codecOptions) : Encoder(codecOptions)
{ {
headerChunk_ = new msg::Header("pcm"); headerChunk_ = new msg::Header("pcm");
} }
double PcmEncoder::encode(msg::PcmChunk* chunk) void PcmEncoder::encode(const msg::PcmChunk* chunk)
{ {
return chunk->duration<chronos::msec>().count(); msg::PcmChunk* pcmChunk = new msg::PcmChunk(*chunk);
listener_->onChunkEncoded(this, pcmChunk, pcmChunk->duration<chronos::msec>().count());
} }
@ -35,3 +37,9 @@ void PcmEncoder::initEncoder()
} }
std::string PcmEncoder::name() const
{
return "pcm";
}

View file

@ -24,8 +24,9 @@
class PcmEncoder : public Encoder class PcmEncoder : public Encoder
{ {
public: public:
PcmEncoder(); PcmEncoder(const std::string& codecOptions = "");
virtual double encode(msg::PcmChunk* chunk); virtual void encode(const msg::PcmChunk* chunk);
virtual std::string name() const;
protected: protected:
virtual void initEncoder(); virtual void initEncoder();