Skip to content

Commit 866c346

Browse files
committed
Add OpusRtpDepacketizer
Inverse of OpusRtpPacketizer. Takes incoming Opus packets and emits frames.
1 parent 437a758 commit 866c346

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ set(LIBDATACHANNEL_SOURCES
7575
${CMAKE_CURRENT_SOURCE_DIR}/src/rtppacketizationconfig.cpp
7676
${CMAKE_CURRENT_SOURCE_DIR}/src/rtcpsrreporter.cpp
7777
${CMAKE_CURRENT_SOURCE_DIR}/src/rtppacketizer.cpp
78+
${CMAKE_CURRENT_SOURCE_DIR}/src/rtpdepacketizer.cpp
7879
${CMAKE_CURRENT_SOURCE_DIR}/src/h264rtppacketizer.cpp
7980
${CMAKE_CURRENT_SOURCE_DIR}/src/h264rtpdepacketizer.cpp
8081
${CMAKE_CURRENT_SOURCE_DIR}/src/nalunit.cpp
@@ -110,6 +111,7 @@ set(LIBDATACHANNEL_HEADERS
110111
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtppacketizationconfig.hpp
111112
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtcpsrreporter.hpp
112113
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtppacketizer.hpp
114+
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtpdepacketizer.hpp
113115
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/h264rtppacketizer.hpp
114116
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/h264rtpdepacketizer.hpp
115117
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/nalunit.hpp

include/rtc/rtc.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@
3838
#include "rtcpreceivingsession.hpp"
3939
#include "rtcpsrreporter.hpp"
4040
#include "rtppacketizer.hpp"
41+
#include "rtpdepacketizer.hpp"
4142

4243
#endif // RTC_ENABLE_MEDIA

include/rtc/rtpdepacketizer.hpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (c) 2024 Paul-Louis Ageneau
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
*/
8+
9+
#ifndef RTC_RTP_DEPACKETIZER_H
10+
#define RTC_RTP_DEPACKETIZER_H
11+
12+
#if RTC_ENABLE_MEDIA
13+
14+
#include "mediahandler.hpp"
15+
#include "message.hpp"
16+
17+
namespace rtc {
18+
19+
/// RTP depacketization for Opus
20+
class RTC_CPP_EXPORT OpusRtpDepacketizer : public MediaHandler {
21+
public:
22+
OpusRtpDepacketizer() = default;
23+
virtual ~OpusRtpDepacketizer() = default;
24+
25+
virtual void incoming(message_vector &messages, const message_callback &send) override;
26+
};
27+
28+
} // namespace rtc
29+
30+
#endif /* RTC_ENABLE_MEDIA */
31+
32+
#endif /* RTC_RTP_DEPACKETIZER_H */

src/rtpdepacketizer.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright (c) 2024 Paul-Louis Ageneau
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
*/
8+
9+
#if RTC_ENABLE_MEDIA
10+
11+
#include "rtpdepacketizer.hpp"
12+
#include "rtp.hpp"
13+
14+
#include "impl/logcounter.hpp"
15+
16+
#include <cmath>
17+
#include <cstring>
18+
19+
namespace rtc {
20+
21+
void OpusRtpDepacketizer::incoming([[maybe_unused]] message_vector &messages,
22+
[[maybe_unused]] const message_callback &send) {
23+
24+
messages.erase(std::remove_if(messages.begin(), messages.end(),
25+
[](message_ptr message) {
26+
// Filter RTCP
27+
if (message->type == Message::Control) {
28+
return true;
29+
}
30+
31+
if (message->size() < sizeof(RtpHeader)) {
32+
PLOG_VERBOSE << "RTP packet is too small, size="
33+
<< message->size();
34+
return true;
35+
}
36+
37+
return false;
38+
}),
39+
messages.end());
40+
41+
for (auto &message : messages) {
42+
auto pkt = reinterpret_cast<const rtc::RtpHeader *>(message->data());
43+
auto headerSize = sizeof(rtc::RtpHeader) + pkt->csrcCount() + pkt->getExtensionHeaderSize();
44+
message->erase(message->begin(), message->begin() + headerSize);
45+
}
46+
}
47+
48+
} // namespace rtc
49+
50+
#endif /* RTC_ENABLE_MEDIA */

0 commit comments

Comments
 (0)