- fixes for pvqa_pcap project - lazy creation of codecs + compare timespec structures

This commit is contained in:
Dmytro Bogovych 2024-12-24 13:19:54 +03:00
parent b5d0242c74
commit c45c686582
5 changed files with 62 additions and 7 deletions

View File

@ -15,3 +15,39 @@ double now_ms(void)
return 1000.0 * res.tv_sec + (double) res.tv_nsec / 1e6; return 1000.0 * res.tv_sec + (double) res.tv_nsec / 1e6;
#endif #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;
}

View File

@ -1,6 +1,17 @@
#ifndef __HELPER_TIME_H #ifndef __HELPER_TIME_H
#define __HELPER_TIME_H #define __HELPER_TIME_H
#include <time.h>
// Return current monotonic number of milliseconds starting from some point
extern double now_ms(); 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 #endif

View File

@ -420,13 +420,10 @@ bool AudioReceiver::add(const std::shared_ptr<jrtplib::RTPPacket>& p, Codec** co
return false; // Reject packet with unknown payload type return false; // Reject packet with unknown payload type
} }
// Check if codec is created actually // Check if codec is creating lazily
if (!codecIter->second) if (!codecIter->second)
{ {
// Look for ptype codecIter->second = mCodecList.createCodecByPayloadType(ptype);
for (int codecIndex = 0; codecIndex < mCodecList.count(); codecIndex++)
if (mCodecList.codecAt(codecIndex).payloadType() == p->GetPayloadType())
codecIter->second = mCodecList.codecAt(codecIndex).create();
} }
// Return pointer to codec if needed.get() // Return pointer to codec if needed.get()

View File

@ -407,11 +407,21 @@ void CodecList::fillCodecMap(CodecMap& cm)
for (auto& factory: mFactoryList) for (auto& factory: mFactoryList)
{ {
// Create codec here. Although they are not needed right now - they can be needed to find codec's info. // Create codec here. Although they are not needed right now - they can be needed to find codec's info.
PCodec c = factory->create(); // PCodec c = factory->create();
cm.insert({factory->payloadType(), c}); 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() CodecListPriority::CodecListPriority()
{ {

View File

@ -121,6 +121,7 @@ public:
Codec::Factory& codecAt(int index) const; Codec::Factory& codecAt(int index) const;
int findCodec(const std::string& name) const; int findCodec(const std::string& name) const;
void fillCodecMap(CodecMap& cm); void fillCodecMap(CodecMap& cm);
PCodec createCodecByPayloadType(int payloadType);
void clear(); void clear();
protected: protected: