85 lines
2.1 KiB
C++
85 lines
2.1 KiB
C++
/*
|
|
*******************************************************************************
|
|
* INCLUDE
|
|
*******************************************************************************
|
|
*/
|
|
#include "AudioEncoder.h"
|
|
#include "Logger.h"
|
|
#include <cstdint>
|
|
|
|
namespace Video
|
|
{
|
|
namespace Core
|
|
{
|
|
/*
|
|
*******************************************************************************
|
|
* CLASS DEFINE
|
|
*******************************************************************************
|
|
*/
|
|
AudioEncoder::AudioEncoder(const std::string& name, CodecID codecID): AudioAdaptablePipe(name)
|
|
{
|
|
setCodecID(codecID);
|
|
|
|
setBitrate(128000);
|
|
}
|
|
|
|
AudioEncoder::~AudioEncoder()
|
|
{
|
|
}
|
|
|
|
void AudioEncoder::start()
|
|
{
|
|
_task.run(
|
|
[this](std::atomic<bool>& isCancelled) -> void
|
|
{
|
|
Packet::Ptr pPacket;
|
|
while (!isCancelled)
|
|
{
|
|
try
|
|
{
|
|
pPacket = encode();
|
|
if (!pPacket)
|
|
continue;
|
|
|
|
postToNextComponents(sharedFromThis(), pPacket, MediaType::AUDIO);
|
|
}
|
|
catch (Poco::Exception& e)
|
|
{
|
|
logger().error("%s: %s", name(), e.displayText());
|
|
Poco::Thread::sleep(1000);
|
|
}
|
|
catch (std::exception& e)
|
|
{
|
|
logger().error("%s: %s", name(), std::string(e.what()));
|
|
Poco::Thread::sleep(1000);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
void AudioEncoder::stop()
|
|
{
|
|
_task.cancel();
|
|
}
|
|
|
|
Frame::Ptr AudioEncoder::readFrame(uint32_t timeout)
|
|
{
|
|
Frame::Ptr pFrame = AudioAdaptablePipe::readFrame(timeout);
|
|
|
|
const int64_t off = _ptsOffsetUs.load(std::memory_order_relaxed);
|
|
if (off != 0)
|
|
{
|
|
const int64_t ts = (int64_t)pFrame->timestamp + off;
|
|
pFrame->timestamp = (ts > 0) ? (uint64_t)ts : 0ULL;
|
|
}
|
|
|
|
return pFrame;
|
|
}
|
|
|
|
void AudioEncoder::onConfigChanged(ParameterNames& names)
|
|
{
|
|
if (names.exists(_PTSOffset))
|
|
_ptsOffsetUs.store(getPTSOffset(), std::memory_order_relaxed);
|
|
}
|
|
}
|
|
}
|