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 8, 2024
1 parent 9d6671c commit 1439a65
Show file tree
Hide file tree
Showing 4 changed files with 84 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 @@ -111,6 +112,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 */
47 changes: 47 additions & 0 deletions src/rtpdepacketizer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* 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) {
message_vector result;
for (auto &message : messages) {
if (message->type == Message::Control) {
result.push_back(std::move(message));
continue;
}

if (message->size() < sizeof(RtpHeader)) {
PLOG_VERBOSE << "RTP packet is too small, size=" << message->size();
continue;
}

auto pkt = reinterpret_cast<const rtc::RtpHeader *>(message->data());
auto headerSize = sizeof(rtc::RtpHeader) + pkt->csrcCount() + pkt->getExtensionHeaderSize();
result.push_back(make_message(message->begin() + headerSize, message->end(),
Message::Binary, 0, nullptr,
std::make_shared<FrameInfo>(pkt->timestamp())));
}

messages.swap(result);
}

} // namespace rtc

#endif /* RTC_ENABLE_MEDIA */

0 comments on commit 1439a65

Please sign in to comment.