Change stream reader to use actual chunk duration

e.g. a configured chunk_ms of 11 will read a 44.1kHz stream in chunks of 485 frames (=10.9977ms)
This commit is contained in:
badaix 2020-02-29 19:24:47 +01:00
parent 12f828ed43
commit a8998997e9
8 changed files with 45 additions and 33 deletions

View file

@ -28,9 +28,8 @@ using namespace std;
namespace encoder
{
FlacEncoder::FlacEncoder(const std::string& codecOptions) : Encoder(codecOptions), encoder_(nullptr), pcmBufferSize_(0), encodedSamples_(0)
FlacEncoder::FlacEncoder(const std::string& codecOptions) : Encoder(codecOptions), encoder_(nullptr), pcmBufferSize_(0), encodedSamples_(0), flacChunk_(nullptr)
{
flacChunk_ = new msg::PcmChunk();
headerChunk_.reset(new msg::CodecHeader("flac"));
pcmBuffer_ = (FLAC__int32*)malloc(pcmBufferSize_ * sizeof(FLAC__int32));
}
@ -46,7 +45,8 @@ FlacEncoder::~FlacEncoder()
FLAC__stream_encoder_delete(encoder_);
}
delete flacChunk_;
if (flacChunk_ != nullptr)
delete flacChunk_;
free(pcmBuffer_);
}
@ -71,6 +71,9 @@ std::string FlacEncoder::name() const
void FlacEncoder::encode(const msg::PcmChunk* chunk)
{
if (flacChunk_ == nullptr)
flacChunk_ = new msg::PcmChunk(chunk->format, 0);
int samples = chunk->getSampleCount();
int frames = chunk->getFrameCount();
// LOG(INFO) << "payload: " << chunk->payloadSize << "\tframes: " << frames << "\tsamples: " << samples << "\tduration: " <<
@ -106,11 +109,11 @@ void FlacEncoder::encode(const msg::PcmChunk* chunk)
if (encodedSamples_ > 0)
{
double resMs = encodedSamples_ / sampleFormat_.msRate();
double resMs = static_cast<double>(encodedSamples_) / sampleFormat_.msRate();
// LOG(INFO) << "encoded: " << chunk->payloadSize << "\tframes: " << encodedSamples_ << "\tres: " << resMs << "\n";
encodedSamples_ = 0;
listener_->onChunkEncoded(this, flacChunk_, resMs);
flacChunk_ = new msg::PcmChunk(chunk->format, 0);
flacChunk_ = nullptr;
}
}