- fix packet time calculation for Opus codec

This commit is contained in:
2026-05-22 13:36:28 +03:00
parent 97c4c3aef0
commit dd21bc8c35
3 changed files with 31 additions and 7 deletions
+19 -6
View File
@@ -936,18 +936,31 @@ AudioReceiver::MediaInfo AudioReceiver::infoFor(jrtplib::RTPPacket& p)
if (!codec)
return {};
int frame_count = 0;
std::chrono::milliseconds packetTime = 0ms;
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())
frame_count++;
frameCount++;
packetTime = std::chrono::milliseconds(frameCount * codec->frameTime());
}
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 {std::chrono::milliseconds(frame_count * codec->frameTime()), codec->samplerate()};
return {packetTime, codec->samplerate()};
}
// int AudioReceiver::timelengthFor(jrtplib::RTPPacket& p)