- source codes cleanups and minor fixes

This commit is contained in:
Dmytro Bogovych 2025-04-16 09:58:06 +03:00
parent a0b7cdcd99
commit 8d0c8ba4de
4 changed files with 182 additions and 173 deletions

View File

@ -62,6 +62,8 @@ std::vector<short>& RtpBuffer::Packet::pcm()
RtpBuffer::RtpBuffer(Statistics& stat) RtpBuffer::RtpBuffer(Statistics& stat)
:mStat(stat) :mStat(stat)
{ {
if (mStat.mPacketLoss)
std::cout << "Warning: packet loss is not zero" << std::endl;
} }
RtpBuffer::~RtpBuffer() RtpBuffer::~RtpBuffer()
@ -203,6 +205,7 @@ RtpBuffer::FetchResult RtpBuffer::fetch(ResultList& rl)
// Save it as last packet however - to not confuse loss packet counter // Save it as last packet however - to not confuse loss packet counter
mFetchedPacket = mPacketList.front(); mFetchedPacket = mPacketList.front();
mLastSeqno = mPacketList.front()->rtp()->GetExtendedSequenceNumber();
// Erase from packet list // Erase from packet list
mPacketList.erase(mPacketList.begin()); mPacketList.erase(mPacketList.begin());
@ -212,47 +215,48 @@ RtpBuffer::FetchResult RtpBuffer::fetch(ResultList& rl)
} }
if (total < mLow) if (total < mLow)
{
// Still not prebuffered
result = FetchResult::NoPacket; result = FetchResult::NoPacket;
}
else else
{ {
// Did we fetch any packet before ?
bool is_fetched_packet = mFetchedPacket.get() != nullptr; bool is_fetched_packet = mFetchedPacket.get() != nullptr;
if (is_fetched_packet) if (is_fetched_packet)
is_fetched_packet &= mFetchedPacket->rtp().get() != nullptr; is_fetched_packet &= mFetchedPacket->rtp().get() != nullptr;
if (is_fetched_packet) if (mLastSeqno.has_value())
{ {
if (mPacketList.empty()) if (mPacketList.empty())
{ {
result = FetchResult::NoPacket; result = FetchResult::NoPacket;
mStat.mPacketLoss++; // Don't increase counter of lost packets here; maybe it is DTX
} }
else else
{ {
// Current sequence number ? // Current sequence number ?
unsigned seqno = mPacketList.front()->rtp()->GetExtendedSequenceNumber(); uint32_t seqno = mPacketList.front()->rtp()->GetExtendedSequenceNumber();
// Gap between new packet and previous on // Gap between new packet and previous on
int gap = (int64_t)seqno - (int64_t)mFetchedPacket->rtp()->GetExtendedSequenceNumber() - 1; int gap = (int64_t)seqno - (int64_t)*mLastSeqno - 1;
gap = std::min(gap, 127); gap = std::min(gap, 127);
if (gap > 0 && mPacketList.empty()) if (gap > 0)
{ {
// std::cout << "Increase the packet loss for SSRC " << std::hex << mSsrc << std::endl;
mStat.mPacketLoss++;
//mStat.mLoss[gap]++;
mLastSeqno = *mLastSeqno + 1;
result = FetchResult::Gap; result = FetchResult::Gap;
mStat.mPacketLoss += gap;
mStat.mLoss[gap]++;
} }
else else
{ {
if (gap > 0)
{
mStat.mPacketLoss += gap;
mStat.mLoss[gap]++;
}
result = FetchResult::RegularPacket; result = FetchResult::RegularPacket;
rl.push_back(mPacketList.front()); rl.push_back(mPacketList.front());
// Save last returned normal packet // Save last returned normal packet
mFetchedPacket = mPacketList.front(); mFetchedPacket = mPacketList.front();
mLastSeqno = mPacketList.front()->rtp()->GetExtendedSequenceNumber();
// Remove returned packet from the list // Remove returned packet from the list
mPacketList.erase(mPacketList.begin()); mPacketList.erase(mPacketList.begin());
@ -262,7 +266,7 @@ RtpBuffer::FetchResult RtpBuffer::fetch(ResultList& rl)
else else
{ {
// See if prebuffer limit is reached // See if prebuffer limit is reached
if (findTimelength() >= mPrebuffer) if (findTimelength() >= mPrebuffer && !mPacketList.empty())
{ {
// Normal packet will be returned // Normal packet will be returned
result = FetchResult::RegularPacket; result = FetchResult::RegularPacket;
@ -272,6 +276,7 @@ RtpBuffer::FetchResult RtpBuffer::fetch(ResultList& rl)
// Remember returned packet // Remember returned packet
mFetchedPacket = mPacketList.front(); mFetchedPacket = mPacketList.front();
mLastSeqno = mPacketList.front()->rtp()->GetExtendedSequenceNumber();
// Remove returned packet from buffer list // Remove returned packet from buffer list
mPacketList.erase(mPacketList.begin()); mPacketList.erase(mPacketList.begin());
@ -402,10 +407,11 @@ size_t decode_packet(Codec& codec, RTPPacket& p, void* output_buffer, size_t out
return result; return result;
} }
bool AudioReceiver::add(const std::shared_ptr<jrtplib::RTPPacket>& p, Codec** codec) bool AudioReceiver::add(const std::shared_ptr<jrtplib::RTPPacket>& p, Codec** detectedCodec)
{ {
// Estimate time length // Estimate time length
int time_length = 0, int time_length = 0,
samplerate = 8000,
payloadLength = p->GetPayloadLength(), payloadLength = p->GetPayloadLength(),
ptype = p->GetPayloadType(); ptype = p->GetPayloadType();
@ -414,34 +420,43 @@ bool AudioReceiver::add(const std::shared_ptr<jrtplib::RTPPacket>& p, Codec** co
mStat.mCodecCount[ptype]++; mStat.mCodecCount[ptype]++;
// Check if codec can be handled // Check if codec can be handled
Codec* codec = nullptr;
CodecMap::iterator codecIter = mCodecMap.find(ptype); CodecMap::iterator codecIter = mCodecMap.find(ptype);
if (codecIter == mCodecMap.end()) if (codecIter == mCodecMap.end())
{ {
ICELogMedia(<< "Cannot find codec in available codecs"); time_length = 10;
return false; // Reject packet with unknown payload type
} }
else
{
// Check if codec is creating lazily // Check if codec is creating lazily
if (!codecIter->second) if (!codecIter->second)
{ {
codecIter->second = mCodecList.createCodecByPayloadType(ptype); codecIter->second = mCodecList.createCodecByPayloadType(ptype);
} }
codec = codecIter->second.get();
// Return pointer to codec if needed.get() // Return pointer to codec if needed.get()
if (codec) if (detectedCodec)
*codec = codecIter->second.get(); *detectedCodec = codec;
if (mStat.mCodecName.empty()) if (mStat.mCodecName.empty() && codec)
mStat.mCodecName = codecIter->second->name(); mStat.mCodecName = codec->name();
if (!codecIter->second->rtpLength()) if (!codec)
time_length = codecIter->second->frameTime(); time_length = 10;
else else
time_length = lround(double(payloadLength) / codecIter->second->rtpLength() * codecIter->second->frameTime()); if (!codec->rtpLength())
time_length = codec->frameTime();
else
time_length = lround(double(payloadLength) / codec->rtpLength() * codec->frameTime());
if (codec)
samplerate = codec->samplerate();
}
// Process jitter // Process jitter
mJitterStats.process(p.get(), codecIter->second->samplerate()); mJitterStats.process(p.get(), samplerate);
mStat.mJitter = static_cast<float>(mJitterStats.get()); mStat.mJitter = static_cast<float>(mJitterStats.get());
// Check if packet is CNG // Check if packet is CNG
@ -453,20 +468,20 @@ bool AudioReceiver::add(const std::shared_ptr<jrtplib::RTPPacket>& p, Codec** co
{ {
// It will cause statistics to report about bad RTP packet // It will cause statistics to report about bad RTP packet
// I have to replay last packet payload here to avoid report about lost packet // I have to replay last packet payload here to avoid report about lost packet
mBuffer.add(p, time_length, codecIter->second->samplerate()); mBuffer.add(p, time_length, samplerate);
return false; return false;
} }
// Queue packet to buffer // Queue packet to buffer
auto packet = mBuffer.add(p, time_length, codecIter->second->samplerate()).get(); auto packet = mBuffer.add(p, time_length, samplerate).get();
if (packet) if (packet)
{ {
// Check if early decoding configured // Check if early decoding configured
if (mEarlyDecode && *codec) if (mEarlyDecode && codec)
{ {
// Move data to packet buffer // Move data to packet buffer
size_t available = decode_packet(**codec, *p, mDecodedFrame, sizeof mDecodedFrame); size_t available = decode_packet(*codec, *p, mDecodedFrame, sizeof mDecodedFrame);
if (available > 0) if (available > 0)
{ {
packet->pcm().resize(available / 2); packet->pcm().resize(available / 2);
@ -549,7 +564,6 @@ AudioReceiver::DecodeResult AudioReceiver::getAudio(Audio::DataWindow& output, i
case RtpBuffer::FetchResult::RegularPacket: case RtpBuffer::FetchResult::RegularPacket:
mFailedCount = 0; mFailedCount = 0;
for (std::shared_ptr<RtpBuffer::Packet>& p: rl) for (std::shared_ptr<RtpBuffer::Packet>& p: rl)
{ {
assert(p); assert(p);

View File

@ -1,4 +1,4 @@
/* Copyright(C) 2007-2017 VoIPobjects (voipobjects.com) /* Copyright(C) 2007-2025 VoIPobjects (voipobjects.com)
* This Source Code Form is subject to the terms of the Mozilla Public * This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
@ -8,22 +8,16 @@
#include "MT_Stream.h" #include "MT_Stream.h"
#include "MT_CodecList.h" #include "MT_CodecList.h"
#include "MT_AudioCodec.h"
#include "MT_CngHelper.h" #include "MT_CngHelper.h"
#include "../helper/HL_Pointer.h"
#include "../helper/HL_Sync.h" #include "../helper/HL_Sync.h"
#include "../helper/HL_Optional.hpp"
#include "jrtplib/src/rtppacket.h" #include "jrtplib/src/rtppacket.h"
#include "jrtplib/src/rtcppacket.h"
#include "jrtplib/src/rtpsourcedata.h" #include "jrtplib/src/rtpsourcedata.h"
#include "../audio/Audio_DataWindow.h" #include "../audio/Audio_DataWindow.h"
#include "../audio/Audio_Resampler.h" #include "../audio/Audio_Resampler.h"
#include <map> #include <optional>
// #define DUMP_DECODED
namespace MT namespace MT
{ {
@ -101,6 +95,7 @@ namespace MT
bool mFirstPacketWillGo = true; bool mFirstPacketWillGo = true;
jrtplib::RTPSourceStats mRtpStats; jrtplib::RTPSourceStats mRtpStats;
std::shared_ptr<Packet> mFetchedPacket; std::shared_ptr<Packet> mFetchedPacket;
std::optional<uint32_t> mLastSeqno;
// To calculate average interval between packet add. It is close to jitter but more useful in debugging. // To calculate average interval between packet add. It is close to jitter but more useful in debugging.
float mLastAddTime = 0.0; float mLastAddTime = 0.0;

View File

@ -423,7 +423,7 @@ PCodec CodecList::createCodecByPayloadType(int payloadType)
if (factory->payloadType() == payloadType) if (factory->payloadType() == payloadType)
return factory->create(); return factory->create();
} }
return PCodec(); return {};
} }
CodecListPriority::CodecListPriority() CodecListPriority::CodecListPriority()

View File

@ -68,7 +68,7 @@ NetworkAddress NetworkAddress::parse(const std::string& s)
if (ip4Pos == std::string::npos && ip6Pos == std::string::npos) if (ip4Pos == std::string::npos && ip6Pos == std::string::npos)
{ {
// Parse usual IP[:port] pair // Parse usual IP[:port] pair
std::string::size_type cp = s.find(":"); std::string::size_type cp = s.find_last_of(":");
if (cp == std::string::npos) if (cp == std::string::npos)
result.setIp(cp); result.setIp(cp);
else else