From 7510ff569798a35751df41c7cad28d76a3f1cdc8 Mon Sep 17 00:00:00 2001 From: Dmytro Bogovych Date: Mon, 3 Feb 2025 11:51:11 +0300 Subject: [PATCH] - better checks for RTP packet + enable EVS codec + improve thread pool --- src/engine/helper/HL_Rtp.cpp | 13 ++++-- src/engine/helper/HL_ThreadPool.cpp | 5 ++ src/engine/helper/HL_ThreadPool.h | 2 + src/engine/media/MT_CodecList.cpp | 71 +++++++++++++++-------------- src/engine/media/MT_CodecList.h | 2 +- src/engine/media/MT_EvsCodec.cpp | 3 +- 6 files changed, 54 insertions(+), 42 deletions(-) diff --git a/src/engine/helper/HL_Rtp.cpp b/src/engine/helper/HL_Rtp.cpp index 790b2816..21cfc18e 100644 --- a/src/engine/helper/HL_Rtp.cpp +++ b/src/engine/helper/HL_Rtp.cpp @@ -52,8 +52,12 @@ bool RtpHelper::isRtp(const void* buffer, size_t length) if (length < 12) return false; - unsigned char _type = reinterpret_cast(buffer)->pt; - bool rtp = ( (_type & 0x7F) >= 96 && (_type & 0x7F) <= 127) || ((_type & 0x7F) < 35); + const RtpHeader* h = reinterpret_cast(buffer); + if (h->version != 0b10) + return false; + + unsigned char pt = h->pt; + bool rtp = ( (pt & 0x7F) >= 96 && (pt & 0x7F) <= 127) || ((pt & 0x7F) < 35); return rtp; } @@ -62,9 +66,8 @@ bool RtpHelper::isRtpOrRtcp(const void* buffer, size_t length) { if (length < 12) return false; - unsigned char b = ((const unsigned char*)buffer)[0]; - - return (b & 0xC0 ) == 128; + const RtcpHeader* h = reinterpret_cast(buffer); + return h->version == 0b10; } bool RtpHelper::isRtcp(const void* buffer, size_t length) diff --git a/src/engine/helper/HL_ThreadPool.cpp b/src/engine/helper/HL_ThreadPool.cpp index aec62638..d58a1afb 100644 --- a/src/engine/helper/HL_ThreadPool.cpp +++ b/src/engine/helper/HL_ThreadPool.cpp @@ -32,6 +32,11 @@ size_t thread_pool::size() return this->tasks.size(); } +size_t thread_pool::threads() +{ + return this->workers.size(); +} + // the destructor joins all threads thread_pool::~thread_pool() { diff --git a/src/engine/helper/HL_ThreadPool.h b/src/engine/helper/HL_ThreadPool.h index ac76ff82..92b1f9d0 100644 --- a/src/engine/helper/HL_ThreadPool.h +++ b/src/engine/helper/HL_ThreadPool.h @@ -24,6 +24,8 @@ public: void enqueue(const task& task); void wait(std::chrono::milliseconds interval = std::chrono::milliseconds(50)); size_t size(); + size_t threads(); + private: // need to keep track of threads so we can join them std::vector< std::thread > workers; diff --git a/src/engine/media/MT_CodecList.cpp b/src/engine/media/MT_CodecList.cpp index e3dd371c..0921e5cf 100644 --- a/src/engine/media/MT_CodecList.cpp +++ b/src/engine/media/MT_CodecList.cpp @@ -1,4 +1,4 @@ -/* Copyright(C) 2007-2023 VoIPobjects (voipobjects.com) +/* Copyright(C) 2007-2025 VoIPobjects (voipobjects.com) * 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 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ @@ -172,14 +172,14 @@ CodecList::Settings::EvsSpec CodecList::Settings::EvsSpec::parse(const std::stri if (bandwidth == "nb" || bandwidth == "NB") result.mBandwidth = Bandwidth_NB; else - if (bandwidth == "wb" || bandwidth == "WB") - result.mBandwidth = Bandwidth_WB; - else - if (bandwidth == "swb" || bandwidth == "SWB") - result.mBandwidth = Bandwidth_SWB; - else - if (bandwidth == "fb" || bandwidth == "FB") - result.mBandwidth = Bandwidth_FB; + if (bandwidth == "wb" || bandwidth == "WB") + result.mBandwidth = Bandwidth_WB; + else + if (bandwidth == "swb" || bandwidth == "SWB") + result.mBandwidth = Bandwidth_SWB; + else + if (bandwidth == "fb" || bandwidth == "FB") + result.mBandwidth = Bandwidth_FB; } return result; @@ -243,33 +243,36 @@ CodecList::Settings CodecList::Settings::parseSdp(const std::list& r.mOpusSpec.push_back({ptype, samplerate, channels}); } else - if (codec_name == "AMR-WB") + if (codec_name == "AMR-WB") + { + int octet_mode = findOctetMode(params.c_str()); + if (octet_mode != -1) { - int octet_mode = findOctetMode(params.c_str()); - if (octet_mode != -1) - { - if (octet_mode == 0) - r.mAmrWbPayloadType.insert(ptype); - else - if (octet_mode == 1) - r.mAmrWbOctetPayloadType.insert(ptype); - } - // std::cout << "AMR-WB parameters: " << params.c_str() << ", found octet-mode: " << octet_mode << std::endl; + if (octet_mode == 0) + r.mAmrWbPayloadType.insert(ptype); + else + if (octet_mode == 1) + r.mAmrWbOctetPayloadType.insert(ptype); } - else - if (codec_name == "AMR") - { - int octet_mode = findOctetMode(params.c_str()); - if (octet_mode != -1) - { - if (octet_mode == 0) - r.mAmrWbPayloadType.insert(ptype); - else - if (octet_mode == 1) - r.mAmrWbOctetPayloadType.insert(ptype); - } - // std::cout << "AMR parameters: " << params.c_str() << ", found octet-mode: " << octet_mode << std::endl; - } + } + else + if (codec_name == "AMR") + { + int octet_mode = findOctetMode(params.c_str()); + if (octet_mode != -1) + { + if (octet_mode == 0) + r.mAmrWbPayloadType.insert(ptype); + else + if (octet_mode == 1) + r.mAmrWbOctetPayloadType.insert(ptype); + } + } + else + if (codec_name == "EVS") + { + r.mEvsSpec.push_back({ptype}); + } } return r; } diff --git a/src/engine/media/MT_CodecList.h b/src/engine/media/MT_CodecList.h index a3ba371f..85ca57ce 100644 --- a/src/engine/media/MT_CodecList.h +++ b/src/engine/media/MT_CodecList.h @@ -59,7 +59,7 @@ public: Bandwidth_FB }; - Bandwidth mBandwidth = Bandwidth_NB; + Bandwidth mBandwidth = Bandwidth_FB; enum Encoding { diff --git a/src/engine/media/MT_EvsCodec.cpp b/src/engine/media/MT_EvsCodec.cpp index 0fc7d3e6..92c79922 100644 --- a/src/engine/media/MT_EvsCodec.cpp +++ b/src/engine/media/MT_EvsCodec.cpp @@ -146,8 +146,7 @@ PCodec EVSCodec::EVSFactory::create() } EVSCodec::EVSCodec(): EVSCodec(StreamParameters()) -{ -} +{} EVSCodec::EVSCodec(const StreamParameters &sp) {