mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-10 07:36:41 +02:00
Pass encoded chunks as shared_ptr
This commit is contained in:
parent
d9d403d729
commit
63ad64dfff
10 changed files with 15 additions and 21 deletions
|
@ -38,7 +38,7 @@ class Encoder;
|
||||||
class EncoderListener
|
class EncoderListener
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual void onChunkEncoded(const Encoder* encoder, msg::PcmChunk* chunk, double duration) = 0;
|
virtual void onChunkEncoded(const Encoder* encoder, std::shared_ptr<msg::PcmChunk> chunk, double duration) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -28,9 +28,8 @@ using namespace std;
|
||||||
namespace encoder
|
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"));
|
headerChunk_.reset(new msg::CodecHeader("flac"));
|
||||||
pcmBuffer_ = (FLAC__int32*)malloc(pcmBufferSize_ * sizeof(FLAC__int32));
|
pcmBuffer_ = (FLAC__int32*)malloc(pcmBufferSize_ * sizeof(FLAC__int32));
|
||||||
}
|
}
|
||||||
|
@ -46,7 +45,6 @@ FlacEncoder::~FlacEncoder()
|
||||||
FLAC__stream_encoder_delete(encoder_);
|
FLAC__stream_encoder_delete(encoder_);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete flacChunk_;
|
|
||||||
free(pcmBuffer_);
|
free(pcmBuffer_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,8 +69,8 @@ std::string FlacEncoder::name() const
|
||||||
|
|
||||||
void FlacEncoder::encode(const msg::PcmChunk* chunk)
|
void FlacEncoder::encode(const msg::PcmChunk* chunk)
|
||||||
{
|
{
|
||||||
if (!flacChunk_->format.isInitialized())
|
if (flacChunk_ == nullptr)
|
||||||
flacChunk_->format = chunk->format;
|
flacChunk_ = make_shared<msg::PcmChunk>(chunk->format, 0);
|
||||||
|
|
||||||
int samples = chunk->getSampleCount();
|
int samples = chunk->getSampleCount();
|
||||||
int frames = chunk->getFrameCount();
|
int frames = chunk->getFrameCount();
|
||||||
|
@ -113,7 +111,7 @@ void FlacEncoder::encode(const msg::PcmChunk* chunk)
|
||||||
// LOG(INFO) << "encoded: " << chunk->payloadSize << "\tframes: " << encodedSamples_ << "\tres: " << resMs << "\n";
|
// LOG(INFO) << "encoded: " << chunk->payloadSize << "\tframes: " << encodedSamples_ << "\tres: " << resMs << "\n";
|
||||||
encodedSamples_ = 0;
|
encodedSamples_ = 0;
|
||||||
listener_->onChunkEncoded(this, flacChunk_, resMs);
|
listener_->onChunkEncoded(this, flacChunk_, resMs);
|
||||||
flacChunk_ = new msg::PcmChunk(chunk->format, 0);
|
flacChunk_ = make_shared<msg::PcmChunk>(chunk->format, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ protected:
|
||||||
FLAC__int32* pcmBuffer_;
|
FLAC__int32* pcmBuffer_;
|
||||||
int pcmBufferSize_;
|
int pcmBufferSize_;
|
||||||
|
|
||||||
msg::PcmChunk* flacChunk_;
|
std::shared_ptr<msg::PcmChunk> flacChunk_;
|
||||||
size_t encodedSamples_;
|
size_t encodedSamples_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -98,7 +98,7 @@ void OggEncoder::encode(const msg::PcmChunk* chunk)
|
||||||
/* tell the library how much we actually submitted */
|
/* tell the library how much we actually submitted */
|
||||||
vorbis_analysis_wrote(&vd_, frames);
|
vorbis_analysis_wrote(&vd_, frames);
|
||||||
|
|
||||||
auto* oggChunk = new msg::PcmChunk(chunk->format, 0);
|
auto oggChunk = make_shared<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
|
||||||
|
@ -149,8 +149,6 @@ void OggEncoder::encode(const msg::PcmChunk* chunk)
|
||||||
oggChunk->payloadSize = pos;
|
oggChunk->payloadSize = pos;
|
||||||
listener_->onChunkEncoded(this, oggChunk, res);
|
listener_->onChunkEncoded(this, oggChunk, res);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
delete oggChunk;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -227,7 +227,7 @@ void OpusEncoder::encode(const SampleFormat& format, const char* data, size_t si
|
||||||
if (len > 0)
|
if (len > 0)
|
||||||
{
|
{
|
||||||
// copy encoded data to chunk
|
// copy encoded data to chunk
|
||||||
auto* opusChunk = new msg::PcmChunk(format, 0);
|
auto opusChunk = make_shared<msg::PcmChunk>(format, 0);
|
||||||
opusChunk->payloadSize = len;
|
opusChunk->payloadSize = len;
|
||||||
opusChunk->payload = (char*)realloc(opusChunk->payload, opusChunk->payloadSize);
|
opusChunk->payload = (char*)realloc(opusChunk->payload, opusChunk->payloadSize);
|
||||||
memcpy(opusChunk->payload, encoded_.data(), len);
|
memcpy(opusChunk->payload, encoded_.data(), len);
|
||||||
|
|
|
@ -49,7 +49,7 @@ PcmEncoder::PcmEncoder(const std::string& codecOptions) : Encoder(codecOptions)
|
||||||
|
|
||||||
void PcmEncoder::encode(const msg::PcmChunk* chunk)
|
void PcmEncoder::encode(const msg::PcmChunk* chunk)
|
||||||
{
|
{
|
||||||
auto* pcmChunk = new msg::PcmChunk(*chunk);
|
auto pcmChunk = std::make_shared<msg::PcmChunk>(*chunk);
|
||||||
listener_->onChunkEncoded(this, pcmChunk, pcmChunk->durationMs());
|
listener_->onChunkEncoded(this, pcmChunk, pcmChunk->durationMs());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -90,14 +90,12 @@ void StreamServer::onStateChanged(const PcmStream* pcmStream, const ReaderState&
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void StreamServer::onChunkRead(const PcmStream* pcmStream, msg::PcmChunk* chunk, double /*duration*/)
|
void StreamServer::onChunkRead(const PcmStream* pcmStream, std::shared_ptr<msg::PcmChunk> chunk, double /*duration*/)
|
||||||
{
|
{
|
||||||
// LOG(INFO) << "onChunkRead (" << pcmStream->getName() << "): " << duration << "ms\n";
|
// LOG(INFO) << "onChunkRead (" << pcmStream->getName() << "): " << duration << "ms\n";
|
||||||
bool isDefaultStream(pcmStream == streamManager_->getDefaultStream().get());
|
bool isDefaultStream(pcmStream == streamManager_->getDefaultStream().get());
|
||||||
// wrap it into a unique_ptr to ensure that the memory will be freed
|
|
||||||
unique_ptr<msg::PcmChunk> chunk_ptr(chunk);
|
|
||||||
|
|
||||||
shared_const_buffer buffer(*chunk_ptr);
|
shared_const_buffer buffer(*chunk);
|
||||||
|
|
||||||
std::vector<std::shared_ptr<StreamSession>> sessions;
|
std::vector<std::shared_ptr<StreamSession>> sessions;
|
||||||
{
|
{
|
||||||
|
|
|
@ -73,7 +73,7 @@ public:
|
||||||
/// Implementation of PcmListener
|
/// Implementation of PcmListener
|
||||||
void onMetaChanged(const PcmStream* pcmStream) override;
|
void onMetaChanged(const PcmStream* pcmStream) override;
|
||||||
void onStateChanged(const PcmStream* pcmStream, const ReaderState& state) override;
|
void onStateChanged(const PcmStream* pcmStream, const ReaderState& state) override;
|
||||||
void onChunkRead(const PcmStream* pcmStream, msg::PcmChunk* chunk, double duration) override;
|
void onChunkRead(const PcmStream* pcmStream, std::shared_ptr<msg::PcmChunk> chunk, double duration) override;
|
||||||
void onResync(const PcmStream* pcmStream, double ms) override;
|
void onResync(const PcmStream* pcmStream, double ms) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -127,7 +127,7 @@ void PcmStream::setState(const ReaderState& newState)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PcmStream::onChunkEncoded(const encoder::Encoder* /*encoder*/, msg::PcmChunk* chunk, double duration)
|
void PcmStream::onChunkEncoded(const encoder::Encoder* /*encoder*/, std::shared_ptr<msg::PcmChunk> chunk, double duration)
|
||||||
{
|
{
|
||||||
// LOG(TRACE, LOG_TAG) << "onChunkEncoded: " << duration << " ms, compression ratio: " << 100 - ceil(100 * (chunk->durationMs() / duration)) << "%\n";
|
// LOG(TRACE, LOG_TAG) << "onChunkEncoded: " << duration << " ms, compression ratio: " << 100 - ceil(100 * (chunk->durationMs() / duration)) << "%\n";
|
||||||
if (duration <= 0)
|
if (duration <= 0)
|
||||||
|
|
|
@ -61,7 +61,7 @@ class PcmListener
|
||||||
public:
|
public:
|
||||||
virtual void onMetaChanged(const PcmStream* pcmStream) = 0;
|
virtual void onMetaChanged(const PcmStream* pcmStream) = 0;
|
||||||
virtual void onStateChanged(const PcmStream* pcmStream, const ReaderState& state) = 0;
|
virtual void onStateChanged(const PcmStream* pcmStream, const ReaderState& state) = 0;
|
||||||
virtual void onChunkRead(const PcmStream* pcmStream, msg::PcmChunk* chunk, double duration) = 0;
|
virtual void onChunkRead(const PcmStream* pcmStream, std::shared_ptr<msg::PcmChunk> chunk, double duration) = 0;
|
||||||
virtual void onResync(const PcmStream* pcmStream, double ms) = 0;
|
virtual void onResync(const PcmStream* pcmStream, double ms) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -83,7 +83,7 @@ public:
|
||||||
virtual void stop();
|
virtual void stop();
|
||||||
|
|
||||||
/// Implementation of EncoderListener::onChunkEncoded
|
/// Implementation of EncoderListener::onChunkEncoded
|
||||||
void onChunkEncoded(const encoder::Encoder* encoder, msg::PcmChunk* chunk, double duration) override;
|
void onChunkEncoded(const encoder::Encoder* encoder, std::shared_ptr<msg::PcmChunk> chunk, double duration) override;
|
||||||
virtual std::shared_ptr<msg::CodecHeader> getHeader();
|
virtual std::shared_ptr<msg::CodecHeader> getHeader();
|
||||||
|
|
||||||
virtual const StreamUri& getUri() const;
|
virtual const StreamUri& getUri() const;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue