From c45c686582950d532d05988bacbd52830d0306da Mon Sep 17 00:00:00 2001 From: Dmytro Bogovych Date: Tue, 24 Dec 2024 13:19:54 +0300 Subject: [PATCH] - fixes for pvqa_pcap project - lazy creation of codecs + compare timespec structures --- src/engine/helper/HL_Time.cpp | 36 +++++++++++++++++++++++++++ src/engine/helper/HL_Time.h | 11 ++++++++ src/engine/media/MT_AudioReceiver.cpp | 7 ++---- src/engine/media/MT_CodecList.cpp | 14 +++++++++-- src/engine/media/MT_CodecList.h | 1 + 5 files changed, 62 insertions(+), 7 deletions(-) diff --git a/src/engine/helper/HL_Time.cpp b/src/engine/helper/HL_Time.cpp index 49128118..ac579365 100644 --- a/src/engine/helper/HL_Time.cpp +++ b/src/engine/helper/HL_Time.cpp @@ -15,3 +15,39 @@ double now_ms(void) return 1000.0 * res.tv_sec + (double) res.tv_nsec / 1e6; #endif } + +int compare_timespec(const timespec& lhs, const timespec& rhs) +{ + if (lhs.tv_sec < rhs.tv_sec) + return -1; + if (lhs.tv_sec > rhs.tv_sec) + return 1; + + if (lhs.tv_nsec < rhs.tv_nsec) + return -1; + if (lhs.tv_nsec > rhs.tv_nsec) + return 1; + + return 0; +} + +bool operator < (const timespec& lhs, const timespec& rhs) +{ + return compare_timespec(lhs, rhs) < 0; +} + +bool operator == (const timespec& lhs, const timespec& rhs) +{ + return compare_timespec(lhs, rhs) == 0; +} + +bool operator > (const timespec& lhs, const timespec& rhs) +{ + return compare_timespec(lhs, rhs) > 0; +} + +bool is_zero(const timespec& ts) +{ + return !ts.tv_sec && !ts.tv_nsec; +} + diff --git a/src/engine/helper/HL_Time.h b/src/engine/helper/HL_Time.h index 639dd49f..1af72a23 100644 --- a/src/engine/helper/HL_Time.h +++ b/src/engine/helper/HL_Time.h @@ -1,6 +1,17 @@ #ifndef __HELPER_TIME_H #define __HELPER_TIME_H +#include + +// Return current monotonic number of milliseconds starting from some point extern double now_ms(); +// Compare the timespec. +// Returns -1 if lhs < rhs, 1 if lhs > rhs, 0 if equal +extern int compare_timespec(const timespec& lhs, const timespec& rhs); +extern bool operator < (const timespec& lhs, const timespec& rhs); +extern bool operator == (const timespec& lhs, const timespec& rhs); +extern bool operator > (const timespec& lhs, const timespec& rhs); +extern bool is_zero(const timespec& ts); + #endif diff --git a/src/engine/media/MT_AudioReceiver.cpp b/src/engine/media/MT_AudioReceiver.cpp index 5b735b88..9f25ffab 100644 --- a/src/engine/media/MT_AudioReceiver.cpp +++ b/src/engine/media/MT_AudioReceiver.cpp @@ -420,13 +420,10 @@ bool AudioReceiver::add(const std::shared_ptr& p, Codec** co return false; // Reject packet with unknown payload type } - // Check if codec is created actually + // Check if codec is creating lazily if (!codecIter->second) { - // Look for ptype - for (int codecIndex = 0; codecIndex < mCodecList.count(); codecIndex++) - if (mCodecList.codecAt(codecIndex).payloadType() == p->GetPayloadType()) - codecIter->second = mCodecList.codecAt(codecIndex).create(); + codecIter->second = mCodecList.createCodecByPayloadType(ptype); } // Return pointer to codec if needed.get() diff --git a/src/engine/media/MT_CodecList.cpp b/src/engine/media/MT_CodecList.cpp index ce62bd26..e3dd371c 100644 --- a/src/engine/media/MT_CodecList.cpp +++ b/src/engine/media/MT_CodecList.cpp @@ -407,11 +407,21 @@ void CodecList::fillCodecMap(CodecMap& cm) for (auto& factory: mFactoryList) { // Create codec here. Although they are not needed right now - they can be needed to find codec's info. - PCodec c = factory->create(); - cm.insert({factory->payloadType(), c}); + // PCodec c = factory->create(); + cm.insert({factory->payloadType(), PCodec()}); } } +PCodec CodecList::createCodecByPayloadType(int payloadType) +{ + for (auto& factory: mFactoryList) + { + if (factory->payloadType() == payloadType) + return factory->create(); + } + return PCodec(); +} + CodecListPriority::CodecListPriority() { diff --git a/src/engine/media/MT_CodecList.h b/src/engine/media/MT_CodecList.h index 76b028ea..a3ba371f 100644 --- a/src/engine/media/MT_CodecList.h +++ b/src/engine/media/MT_CodecList.h @@ -121,6 +121,7 @@ public: Codec::Factory& codecAt(int index) const; int findCodec(const std::string& name) const; void fillCodecMap(CodecMap& cm); + PCodec createCodecByPayloadType(int payloadType); void clear(); protected: