Skip to content

Commit

Permalink
Add OpusRtpDepacketizer
Browse files Browse the repository at this point in the history
Inverse of OpusRtpPacketizer. Takes incoming Opus packets and emits
frames.
  • Loading branch information
Sean-Der committed Mar 1, 2024
1 parent 437a758 commit c215665
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions include/rtc/rtc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@
#include "rtcpreceivingsession.hpp"
#include "rtcpsrreporter.hpp"
#include "rtppacketizer.hpp"
#include "rtpdepacketizer.hpp"

#endif // RTC_ENABLE_MEDIA
34 changes: 34 additions & 0 deletions include/rtc/rtpdepacketizer.hpp
Original file line number Diff line number Diff line change
@@ -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 */
50 changes: 50 additions & 0 deletions src/rtpdepacketizer.cpp
Original file line number Diff line number Diff line change
@@ -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 <cmath>
#include <cstring>

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<const rtc::RtpHeader *>(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 */

0 comments on commit c215665

Please sign in to comment.