整理代码

This commit is contained in:
蒲爽 2026-09-28 21:33:04 +08:00
commit c7b4e178ec
364 changed files with 59691 additions and 0 deletions

View file

@ -0,0 +1,92 @@
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "AudioDecodeHandler.h"
#include "Logger.h"
#include <cstring>
namespace Video
{
namespace LibOPUS
{
/*
*******************************************************************************
* CLASS DEFINE
*******************************************************************************
*/
AudioDecodeHandler::AudioDecodeHandler(Core::CodecID codecID, Core::AudioComponent::Ptr pPrevious)
: Core::AudioDecodeHandler(Poco::format("LibOPUS::AudioDecodeHandler(%s)", Core::codecIDToString(codecID)), codecID,
pPrevious),
_pDecoder(nullptr), _queue("LibOPUS::AudioDecodeHandler", Core::MediaType::AUDIO, 300)
{
int err;
_pDecoder = opus_decoder_create(pPrevious->getSampleRate(),
Core::channelLayoutChannels(pPrevious->getChannelLayout()), &err);
if (err != OPUS_OK)
throw Poco::InvalidAccessException(
Poco::format("%s: Create decoder FAIL: %d(%s)", name(), err, std::string(opus_strerror(err))), err);
}
AudioDecodeHandler::~AudioDecodeHandler()
{
if (_pDecoder)
{
opus_decoder_destroy(_pDecoder);
_pDecoder = nullptr;
}
}
void AudioDecodeHandler::write(Core::Packet::Ptr pPacket)
{
const int channels = Core::channelLayoutChannels(previous()->getChannelLayout());
int n = opus_decode(_pDecoder, pPacket->data(), (opus_int32)pPacket->length(), _pcm, 5760, 0);
if (n <= 0)
{
logger().error("%s: Decode FAIL: %d(%s)", name(), n, std::string(opus_strerror(n)));
conceal(pPacket);
return;
}
const int bytes = n * channels * (int)sizeof(int16_t);
Core::Frame::Ptr pFrame = new Core::Frame();
pFrame->data[0] = new uint8_t[bytes];
memcpy(pFrame->data[0].get(), _pcm, bytes);
pFrame->length = bytes;
pFrame->timestamp = pPacket->pts();
_queue.enqueue(pFrame, true);
}
void AudioDecodeHandler::conceal(Core::Packet::Ptr pPacket)
{
const int channels = Core::channelLayoutChannels(previous()->getChannelLayout());
int n = opus_decode(_pDecoder, NULL, 0, _pcm, 5760, 0);
if (n <= 0)
return;
const int bytes = n * channels * (int)sizeof(int16_t);
Core::Frame::Ptr pFrame = new Core::Frame();
pFrame->data[0] = new uint8_t[bytes];
memcpy(pFrame->data[0].get(), _pcm, bytes);
pFrame->length = bytes;
pFrame->timestamp = pPacket->pts();
_queue.enqueue(pFrame, true);
}
Core::Frame::Ptr AudioDecodeHandler::read()
{
try
{
return _queue.dequeue(0);
}
catch (Poco::TimeoutException&)
{
return nullptr;
}
}
}
}

View file

@ -0,0 +1,38 @@
#ifndef F7A4D851_0C9D_4A3E_8B52_39B6E1F2C7D1
#define F7A4D851_0C9D_4A3E_8B52_39B6E1F2C7D1
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "../core/AudioDecoder.h"
#include <opus/opus.h>
namespace Video
{
namespace LibOPUS
{
/*
*******************************************************************************
* CLASS DECLARE
*******************************************************************************
*/
class AudioDecodeHandler: public Core::AudioDecodeHandler
{
public:
AudioDecodeHandler(Core::CodecID codecID, Core::AudioComponent::Ptr pPrevious);
~AudioDecodeHandler();
protected:
void write(Core::Packet::Ptr pPacket) override;
void conceal(Core::Packet::Ptr pPacket) override;
Core::Frame::Ptr read() override;
private:
OpusDecoder* _pDecoder;
Core::FrameQueue _queue;
int16_t _pcm[5760 * 2];
};
}
}
#endif

View file

@ -0,0 +1,86 @@
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "AudioEncoder.h"
#include "Logger.h"
namespace Video
{
namespace LibOPUS
{
/*
*******************************************************************************
* CLASS DEFINE
*******************************************************************************
*/
AudioEncoder::AudioEncoder(Core::CodecID codecID)
: Core::AudioEncoder(Poco::format("LibOPUS::AudioEncoder(%s)", Core::codecIDToString(codecID)), codecID),
_pEncoder(nullptr)
{
}
AudioEncoder::~AudioEncoder()
{
}
void AudioEncoder::initialize()
{
setSampleRate(48000);
setFrameSize(960);
setSampleFormat(Core::SampleFormat::S16);
int channels = Core::channelLayoutChannels(getChannelLayout());
int err;
_pEncoder = opus_encoder_create(getSampleRate(), channels, OPUS_APPLICATION_AUDIO, &err);
if (err != OPUS_OK)
throw Poco::InvalidAccessException(
Poco::format("%s: Create encoder FAIL: %d(%s)", name(), err, std::string(opus_strerror(err))), err);
opus_encoder_ctl(_pEncoder, OPUS_SET_BITRATE(getBitrate()));
opus_encoder_ctl(_pEncoder, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_FULLBAND));
opus_encoder_ctl(_pEncoder, OPUS_SET_SIGNAL(OPUS_SIGNAL_MUSIC));
opus_encoder_ctl(_pEncoder, OPUS_SET_VBR(1));
opus_encoder_ctl(_pEncoder, OPUS_SET_LSB_DEPTH(Core::sampleFormatBitWidth(getSampleFormat())));
opus_encoder_ctl(_pEncoder, OPUS_SET_DTX(0));
opus_encoder_ctl(_pEncoder, OPUS_SET_COMPLEXITY(1));
}
void AudioEncoder::deinitialize()
{
if (_pEncoder)
{
opus_encoder_destroy(_pEncoder);
_pEncoder = nullptr;
}
}
#define PACKET_MAX_LEN 8192
Core::Packet::Ptr AudioEncoder::encode()
{
if (!_pEncoder)
return nullptr;
auto pIn = readFrame();
if (pIn.isNull())
return nullptr;
uint8_t* pBuffer = new uint8_t[PACKET_MAX_LEN];
int32_t len = opus_encode(_pEncoder, (int16_t*)pIn->data[0].get(), getFrameSize(), pBuffer, PACKET_MAX_LEN);
if (len < 0 || len > PACKET_MAX_LEN)
{
delete[] pBuffer;
logger().error("%s: Encode FAIL: %d(%s)", name(), len, std::string(opus_strerror(len)));
return nullptr;
}
return new Core::Packet(serialCodeManager().code(), 0, pBuffer, len, pIn->timestamp, pIn->timestamp);
}
void AudioEncoder::onConfigChanged(ParameterNames& names)
{
Core::AudioEncoder::onConfigChanged(names);
}
}
}

View file

@ -0,0 +1,37 @@
#ifndef F1683C08_D3DF_45F8_A40D_C37516C0EEB7
#define F1683C08_D3DF_45F8_A40D_C37516C0EEB7
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "../core/AudioEncoder.h"
#include <opus/opus.h>
namespace Video
{
namespace LibOPUS
{
/*
*******************************************************************************
* CLASS DECLARE
*******************************************************************************
*/
class AudioEncoder: public Core::AudioEncoder
{
public:
AudioEncoder(Core::CodecID codecID);
~AudioEncoder();
protected:
void initialize() override;
void deinitialize() override;
Core::Packet::Ptr encode() override;
void onConfigChanged(ParameterNames& names) override;
private:
OpusEncoder* _pEncoder;
};
}
}
#endif