mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-11 08:06:41 +02:00
WIP opus is working, some values are hard coded
This commit is contained in:
parent
8d47371115
commit
258bab4f65
6 changed files with 95 additions and 84 deletions
|
@ -62,7 +62,7 @@ void Controller::onMessageReceived(ClientConnection* /*connection*/, const msg::
|
||||||
{
|
{
|
||||||
auto* pcmChunk = new msg::PcmChunk(sampleFormat_, 0);
|
auto* pcmChunk = new msg::PcmChunk(sampleFormat_, 0);
|
||||||
pcmChunk->deserialize(baseMessage, buffer);
|
pcmChunk->deserialize(baseMessage, buffer);
|
||||||
// LOG(DEBUG) << "chunk: " << pcmChunk->payloadSize << ", sampleFormat: " << sampleFormat_.rate << "\n";
|
// LOG(DEBUG) << "chunk: " << pcmChunk->payloadSize << ", sampleFormat: " << sampleFormat_.rate << "\n";
|
||||||
if (decoder_->decode(pcmChunk))
|
if (decoder_->decode(pcmChunk))
|
||||||
{
|
{
|
||||||
// TODO: do decoding in thread?
|
// TODO: do decoding in thread?
|
||||||
|
@ -115,7 +115,7 @@ void Controller::onMessageReceived(ClientConnection* /*connection*/, const msg::
|
||||||
#endif
|
#endif
|
||||||
#if defined(HAS_OPUS)
|
#if defined(HAS_OPUS)
|
||||||
else if (headerChunk_->codec == "opus")
|
else if (headerChunk_->codec == "opus")
|
||||||
decoder_.reset(new OpusDecoderWrapper());
|
decoder_.reset(new OpusDecoder());
|
||||||
#endif
|
#endif
|
||||||
else
|
else
|
||||||
throw SnapException("codec not supported: \"" + headerChunk_->codec + "\"");
|
throw SnapException("codec not supported: \"" + headerChunk_->codec + "\"");
|
||||||
|
|
|
@ -20,10 +20,10 @@
|
||||||
#include "common/aixlog.hpp"
|
#include "common/aixlog.hpp"
|
||||||
|
|
||||||
|
|
||||||
OpusDecoderWrapper::OpusDecoderWrapper() : Decoder()
|
OpusDecoder::OpusDecoder() : Decoder()
|
||||||
{
|
{
|
||||||
int error;
|
int error;
|
||||||
dec = opus_decoder_create(48000, 2, &error); // fixme
|
dec_ = opus_decoder_create(48000, 2, &error); // fixme
|
||||||
if (error != 0)
|
if (error != 0)
|
||||||
{
|
{
|
||||||
LOG(ERROR) << "Failed to initialize opus decoder: " << error << '\n' << " Rate: " << 48000 << '\n' << " Channels: " << 2 << '\n';
|
LOG(ERROR) << "Failed to initialize opus decoder: " << error << '\n' << " Rate: " << 48000 << '\n' << " Channels: " << 2 << '\n';
|
||||||
|
@ -31,50 +31,41 @@ OpusDecoderWrapper::OpusDecoderWrapper() : Decoder()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
OpusDecoderWrapper::~OpusDecoderWrapper()
|
OpusDecoder::~OpusDecoder()
|
||||||
{
|
{
|
||||||
|
if (dec_ != nullptr)
|
||||||
|
opus_decoder_destroy(dec_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool OpusDecoderWrapper::decode(msg::PcmChunk* /*chunk*/)
|
bool OpusDecoder::decode(msg::PcmChunk* chunk)
|
||||||
{
|
{
|
||||||
// int samples = 480;
|
size_t samples = 480;
|
||||||
// // reserve space for decoded audio
|
// reserve space for decoded audio
|
||||||
// opus_int16 pcm[samples * 2];
|
if (pcm_.size() < samples * 2)
|
||||||
|
pcm_.resize(samples * 2);
|
||||||
|
|
||||||
// msg::PcmChunk* decodedChunk = new msg::PcmChunk();
|
// decode
|
||||||
// decodedChunk->payloadSize = samples * 4;
|
int frame_size = opus_decode(dec_, (unsigned char*)chunk->payload, chunk->payloadSize, pcm_.data(), samples, 0);
|
||||||
// decodedChunk->payload = (char*)realloc(decodedChunk->payload, decodedChunk->payloadSize);
|
if (frame_size < 0)
|
||||||
|
{
|
||||||
|
LOG(ERROR) << "Failed to decode chunk: " << frame_size << '\n' << " IN size: " << chunk->payloadSize << '\n' << " OUT size: " << samples * 4 << '\n';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LOG(DEBUG) << "Decoded chunk: size " << chunk->payloadSize << " bytes, decoded " << frame_size << " bytes" << '\n';
|
||||||
|
|
||||||
// // decode
|
// copy encoded data to chunk
|
||||||
// int frame_size = opus_decode(dec, (unsigned char*)chunk->payload, chunk->payloadSize, pcm, samples, 0);
|
chunk->payloadSize = samples * 4;
|
||||||
// if (frame_size < 0)
|
chunk->payload = (char*)realloc(chunk->payload, chunk->payloadSize);
|
||||||
// {
|
memcpy(chunk->payload, (char*)pcm_.data(), samples * 4);
|
||||||
// LOG(ERROR) << "Failed to decode chunk: " << frame_size << '\n'
|
return true;
|
||||||
// << " IN size: " << chunk->payloadSize << '\n'
|
}
|
||||||
// << " OUT size: " << decodedChunk->payloadSize << '\n';
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// // logD << "Decoded chunk: size " << chunk->payloadSize << " bytes, decoded " << frame_size << " bytes" << '\n';
|
|
||||||
|
|
||||||
// // copy encoded data to chunk
|
|
||||||
// chunk->payloadSize = samples * 4;
|
|
||||||
// chunk->payload = (char*)realloc(chunk->payload, chunk->payloadSize);
|
|
||||||
|
|
||||||
// // uninterleave audio
|
|
||||||
// for (int i = 0; i < samples * 2; i++)
|
|
||||||
// {
|
|
||||||
// chunk->payload[2 * i + 1] = (char)(pcm[i] >> 8);
|
|
||||||
// chunk->payload[2 * i] = (char)(pcm[i] & 0x00ff);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
SampleFormat OpusDecoderWrapper::setHeader(msg::CodecHeader* /*chunk*/)
|
SampleFormat OpusDecoder::setHeader(msg::CodecHeader* /*chunk*/)
|
||||||
{
|
{
|
||||||
return {48000, 16, 2};
|
return {48000, 16, 2};
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,14 +22,15 @@
|
||||||
#include <opus/opus.h>
|
#include <opus/opus.h>
|
||||||
|
|
||||||
|
|
||||||
class OpusDecoderWrapper : public Decoder
|
class OpusDecoder : public Decoder
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
OpusDecoderWrapper();
|
OpusDecoder();
|
||||||
~OpusDecoderWrapper();
|
~OpusDecoder();
|
||||||
bool decode(msg::PcmChunk* chunk) override;
|
bool decode(msg::PcmChunk* chunk) override;
|
||||||
SampleFormat setHeader(msg::CodecHeader* chunk) override;
|
SampleFormat setHeader(msg::CodecHeader* chunk) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
OpusDecoder* dec;
|
OpusDecoder* dec_;
|
||||||
|
std::vector<opus_int16> pcm_;
|
||||||
};
|
};
|
||||||
|
|
|
@ -54,6 +54,10 @@ Encoder* EncoderFactory::createEncoder(const std::string& codecSettings) const
|
||||||
#if defined(HAS_FLAC)
|
#if defined(HAS_FLAC)
|
||||||
else if (codec == "flac")
|
else if (codec == "flac")
|
||||||
encoder = new FlacEncoder(codecOptions);
|
encoder = new FlacEncoder(codecOptions);
|
||||||
|
#endif
|
||||||
|
#if defined(HAS_OPUS)
|
||||||
|
else if (codec == "opus")
|
||||||
|
encoder = new OpusEncoder(codecOptions);
|
||||||
#endif
|
#endif
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -18,52 +18,62 @@
|
||||||
|
|
||||||
#include "opus_encoder.hpp"
|
#include "opus_encoder.hpp"
|
||||||
#include "common/aixlog.hpp"
|
#include "common/aixlog.hpp"
|
||||||
|
#include "common/snap_exception.hpp"
|
||||||
|
#include "common/str_compat.hpp"
|
||||||
|
|
||||||
|
|
||||||
// const msg::SampleFormat& format);
|
// const msg::SampleFormat& format);
|
||||||
OpusEncoderWrapper::OpusEncoderWrapper(const std::string& codecOptions) : Encoder(codecOptions)
|
OpusEncoder::OpusEncoder(const std::string& codecOptions) : Encoder(codecOptions), enc_(nullptr)
|
||||||
{
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
OpusEncoder::~OpusEncoder()
|
||||||
|
{
|
||||||
|
if (enc_ != nullptr)
|
||||||
|
opus_encoder_destroy(enc_);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::string OpusEncoder::name() const
|
||||||
|
{
|
||||||
|
return "opus";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void OpusEncoder::initEncoder()
|
||||||
|
{
|
||||||
|
int error;
|
||||||
|
enc_ = opus_encoder_create(sampleFormat_.rate, sampleFormat_.channels, OPUS_APPLICATION_RESTRICTED_LOWDELAY, &error);
|
||||||
|
if (error != 0)
|
||||||
|
{
|
||||||
|
throw SnapException("Failed to initialize opus encoder: " + cpt::to_string(error));
|
||||||
|
}
|
||||||
headerChunk_.reset(new msg::CodecHeader("opus"));
|
headerChunk_.reset(new msg::CodecHeader("opus"));
|
||||||
// int error;
|
|
||||||
// enc = opus_encoder_create(format.rate, format.channels, OPUS_APPLICATION_RESTRICTED_LOWDELAY, &error);
|
|
||||||
// if (error != 0)
|
|
||||||
// {
|
|
||||||
// LOG(ERROR) << "Failed to initialize opus encoder: " << error << '\n'
|
|
||||||
// << " Rate: " << format.rate << '\n'
|
|
||||||
// << " Channels: " << format.channels << '\n';
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void OpusEncoderWrapper::encode(const msg::PcmChunk* /*chunk*/)
|
void OpusEncoder::encode(const msg::PcmChunk* chunk)
|
||||||
{
|
{
|
||||||
// int samples = chunk->payloadSize / 4;
|
int samples = chunk->payloadSize / 4;
|
||||||
// opus_int16 pcm[samples * 2];
|
if (encoded_.size() < chunk->payloadSize)
|
||||||
|
encoded_.resize(chunk->payloadSize);
|
||||||
|
|
||||||
// unsigned char encoded[chunk->payloadSize];
|
// encode
|
||||||
|
opus_int32 len = opus_encode(enc_, (opus_int16*)chunk->payload, samples, encoded_.data(), chunk->payloadSize);
|
||||||
|
LOG(DEBUG) << "Encoded: size " << chunk->payloadSize << " bytes, encoded: " << len << " bytes" << '\n';
|
||||||
|
|
||||||
// // get 16 bit samples
|
if (len > 0)
|
||||||
// for (int i = 0; i < samples * 2; i++)
|
{
|
||||||
// {
|
// copy encoded data to chunk
|
||||||
// pcm[i] = (opus_int16)(((opus_int16)chunk->payload[2 * i + 1] << 8) | (0x00ff & (opus_int16)chunk->payload[2 * i]));
|
auto* opusChunk = new msg::PcmChunk(chunk->format, 0);
|
||||||
// }
|
opusChunk->payloadSize = len;
|
||||||
|
opusChunk->payload = (char*)realloc(opusChunk->payload, opusChunk->payloadSize);
|
||||||
// // encode
|
memcpy(opusChunk->payload, encoded_.data(), len);
|
||||||
// int len = opus_encode(enc, pcm, samples, encoded, chunk->payloadSize);
|
listener_->onChunkEncoded(this, opusChunk, (double)samples / ((double)sampleFormat_.rate / 1000.));
|
||||||
// // logD << "Encoded: size " << chunk->payloadSize << " bytes, encoded: " << len << " bytes" << '\n';
|
}
|
||||||
|
else
|
||||||
// if (len > 0)
|
{
|
||||||
// {
|
LOG(ERROR) << "Failed to encode chunk: " << len << '\n' << " Frame size: " << samples / 2 << '\n' << " Max bytes: " << chunk->payloadSize << '\n';
|
||||||
// // copy encoded data to chunk
|
}
|
||||||
// chunk->payloadSize = len;
|
|
||||||
// chunk->payload = (char*)realloc(chunk->payload, chunk->payloadSize);
|
|
||||||
// memcpy(chunk->payload, encoded, len);
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// LOG(ERROR) << "Failed to encode chunk: " << len << '\n' << " Frame size: " << samples / 2 << '\n' << " Max bytes: " << chunk->payloadSize << '\n';
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // return chunk duration
|
|
||||||
// return (double)samples / ((double)sampleFormat.rate / 1000.);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,12 +22,17 @@
|
||||||
#include <opus/opus.h>
|
#include <opus/opus.h>
|
||||||
|
|
||||||
|
|
||||||
class OpusEncoderWrapper : public Encoder
|
class OpusEncoder : public Encoder
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
OpusEncoderWrapper(const std::string& codecOptions = "");
|
OpusEncoder(const std::string& codecOptions = "");
|
||||||
void encode(const msg::PcmChunk* chunk) override;
|
~OpusEncoder() override;
|
||||||
|
|
||||||
private:
|
void encode(const msg::PcmChunk* chunk) override;
|
||||||
OpusEncoder* enc;
|
std::string name() const override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void initEncoder() override;
|
||||||
|
OpusEncoder* enc_;
|
||||||
|
std::vector<u_char> encoded_;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue