- fix packet time calculation for Opus codec
This commit is contained in:
@@ -549,6 +549,12 @@ size_t OpusCodec::plc(int lostPackets, std::span<uint8_t> output)
|
|||||||
return ((uint8_t*)data_output - output.data());
|
return ((uint8_t*)data_output - output.data());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t OpusCodec::getNumberOfSamples(std::span<const uint8_t> payload)
|
||||||
|
{
|
||||||
|
int r = opus_packet_get_nb_samples(payload.data(), payload.size(), mSamplerate);
|
||||||
|
return r >= 0 ? r : 0;
|
||||||
|
}
|
||||||
|
|
||||||
// -------------- ILBC -------------------
|
// -------------- ILBC -------------------
|
||||||
#define ILBC_CODEC_NAME "ILBC"
|
#define ILBC_CODEC_NAME "ILBC"
|
||||||
|
|
||||||
|
|||||||
@@ -64,8 +64,11 @@ class OpusCodec: public Codec
|
|||||||
protected:
|
protected:
|
||||||
OpusEncoder *mEncoderCtx = nullptr;
|
OpusEncoder *mEncoderCtx = nullptr;
|
||||||
OpusDecoder *mDecoderCtx = nullptr;
|
OpusDecoder *mDecoderCtx = nullptr;
|
||||||
int mPTime = 0, mSamplerate = 0, mChannels = 0;
|
int mPTime = 0,
|
||||||
|
mSamplerate = 0,
|
||||||
|
mChannels = 0;
|
||||||
int mDecoderChannels = 0;
|
int mDecoderChannels = 0;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
struct Params
|
struct Params
|
||||||
{
|
{
|
||||||
@@ -110,6 +113,8 @@ public:
|
|||||||
EncodeResult encode(std::span<const uint8_t> input, std::span<uint8_t> output) override;
|
EncodeResult encode(std::span<const uint8_t> input, std::span<uint8_t> output) override;
|
||||||
DecodeResult decode(std::span<const uint8_t> input, std::span<uint8_t> output) override;
|
DecodeResult decode(std::span<const uint8_t> input, std::span<uint8_t> output) override;
|
||||||
size_t plc(int lostFrames, std::span<uint8_t> output) override;
|
size_t plc(int lostFrames, std::span<uint8_t> output) override;
|
||||||
|
|
||||||
|
size_t getNumberOfSamples(std::span<const uint8_t> payload);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -936,18 +936,31 @@ AudioReceiver::MediaInfo AudioReceiver::infoFor(jrtplib::RTPPacket& p)
|
|||||||
if (!codec)
|
if (!codec)
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
int frame_count = 0;
|
std::chrono::milliseconds packetTime = 0ms;
|
||||||
|
|
||||||
if (codec->rtpLength() != 0)
|
if (codec->rtpLength() != 0)
|
||||||
{
|
{
|
||||||
frame_count = static_cast<int>(p.GetPayloadLength() / codec->rtpLength());
|
int frameCount = static_cast<int>(p.GetPayloadLength() / codec->rtpLength());
|
||||||
if (p.GetPayloadType() == 9/*G729A silence*/ && p.GetPayloadLength() % codec->rtpLength())
|
if (p.GetPayloadType() == 9/*G729A silence*/ && p.GetPayloadLength() % codec->rtpLength())
|
||||||
frame_count++;
|
frameCount++;
|
||||||
|
|
||||||
|
packetTime = std::chrono::milliseconds(frameCount * codec->frameTime());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
frame_count = 1;
|
if (typeid(*codec) == typeid(OpusCodec))
|
||||||
|
{
|
||||||
|
OpusCodec* oc = dynamic_cast<OpusCodec*>(codec.get());
|
||||||
|
assert(oc);
|
||||||
|
size_t samplesCount = oc->getNumberOfSamples({p.GetPayloadData(), p.GetPayloadLength()});
|
||||||
|
int sampleratePerMs = codec->samplerate() / 1000;
|
||||||
|
packetTime = std::chrono::milliseconds(samplesCount / sampleratePerMs);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
packetTime = std::chrono::milliseconds(codec->frameTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
return {packetTime, codec->samplerate()};
|
||||||
return {std::chrono::milliseconds(frame_count * codec->frameTime()), codec->samplerate()};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// int AudioReceiver::timelengthFor(jrtplib::RTPPacket& p)
|
// int AudioReceiver::timelengthFor(jrtplib::RTPPacket& p)
|
||||||
|
|||||||
Reference in New Issue
Block a user