From c215665e6261246e663940e3b2188bf778019039 Mon Sep 17 00:00:00 2001 From: Sean DuBois Date: Thu, 29 Feb 2024 11:00:30 -0500 Subject: [PATCH] Add OpusRtpDepacketizer Inverse of OpusRtpPacketizer. Takes incoming Opus packets and emits frames. --- CMakeLists.txt | 2 ++ include/rtc/rtc.hpp | 1 + include/rtc/rtpdepacketizer.hpp | 34 ++++++++++++++++++++++ src/rtpdepacketizer.cpp | 50 +++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 include/rtc/rtpdepacketizer.hpp create mode 100644 src/rtpdepacketizer.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ca0f12e52..652af0969 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,6 +75,7 @@ set(LIBDATACHANNEL_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/rtppacketizationconfig.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/rtcpsrreporter.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/rtppacketizer.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/rtpdepacketizer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/h264rtppacketizer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/h264rtpdepacketizer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/nalunit.cpp @@ -110,6 +111,7 @@ set(LIBDATACHANNEL_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtppacketizationconfig.hpp ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtcpsrreporter.hpp ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtppacketizer.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtpdepacketizer.hpp ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/h264rtppacketizer.hpp ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/h264rtpdepacketizer.hpp ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/nalunit.hpp diff --git a/include/rtc/rtc.hpp b/include/rtc/rtc.hpp index dc7f62eac..683219fc9 100644 --- a/include/rtc/rtc.hpp +++ b/include/rtc/rtc.hpp @@ -38,5 +38,6 @@ #include "rtcpreceivingsession.hpp" #include "rtcpsrreporter.hpp" #include "rtppacketizer.hpp" +#include "rtpdepacketizer.hpp" #endif // RTC_ENABLE_MEDIA diff --git a/include/rtc/rtpdepacketizer.hpp b/include/rtc/rtpdepacketizer.hpp new file mode 100644 index 000000000..feed8479f --- /dev/null +++ b/include/rtc/rtpdepacketizer.hpp @@ -0,0 +1,34 @@ +/** + * Copyright (c) 2024 Paul-Louis Ageneau + * + * 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 https://mozilla.org/MPL/2.0/. + */ + +#ifndef RTC_RTP_DEPACKETIZER_H +#define RTC_RTP_DEPACKETIZER_H + +#if RTC_ENABLE_MEDIA + +#include "mediahandler.hpp" +#include "message.hpp" + +namespace rtc { + +class RTC_CPP_EXPORT RtpDepacketizer : public MediaHandler { +public: + RtpDepacketizer() = default; + virtual ~RtpDepacketizer() = default; + + virtual void incoming(message_vector &messages, const message_callback &send) override; +}; + +using OpusRtpDepacketizer = RtpDepacketizer; +using AACRtpDepacketizer = RtpDepacketizer; + +} // namespace rtc + +#endif /* RTC_ENABLE_MEDIA */ + +#endif /* RTC_RTP_DEPACKETIZER_H */ diff --git a/src/rtpdepacketizer.cpp b/src/rtpdepacketizer.cpp new file mode 100644 index 000000000..cc2dbca6f --- /dev/null +++ b/src/rtpdepacketizer.cpp @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2024 Paul-Louis Ageneau + * + * 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 https://mozilla.org/MPL/2.0/. + */ + +#if RTC_ENABLE_MEDIA + +#include "rtpdepacketizer.hpp" +#include "rtp.hpp" + +#include "impl/logcounter.hpp" + +#include +#include + +namespace rtc { + +void RtpDepacketizer::incoming([[maybe_unused]] message_vector &messages, + [[maybe_unused]] const message_callback &send) { + + messages.erase(std::remove_if(messages.begin(), messages.end(), + [](message_ptr message) { + // Filter RTCP + if (message->type == Message::Control) { + return true; + } + + if (message->size() < sizeof(RtpHeader)) { + PLOG_VERBOSE << "RTP packet is too small, size=" + << message->size(); + return true; + } + + return false; + }), + messages.end()); + + for (auto &message : messages) { + auto pkt = reinterpret_cast(message->data()); + auto headerSize = sizeof(rtc::RtpHeader) + pkt->csrcCount() + pkt->getExtensionHeaderSize(); + message = make_message(message->begin() + headerSize, message->end()); + } +} + +} // namespace rtc + +#endif /* RTC_ENABLE_MEDIA */