Skip to content

Commit

Permalink
Refactor media handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
paullouisageneau committed Aug 22, 2023
1 parent be51f7e commit 0dfbc8b
Show file tree
Hide file tree
Showing 40 changed files with 520 additions and 1,180 deletions.
17 changes: 3 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ set(LIBDATACHANNEL_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/configuration.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/datachannel.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/description.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/mediahandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/global.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/message.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/peerconnection.cpp
Expand All @@ -75,15 +76,9 @@ set(LIBDATACHANNEL_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/rtcpsrreporter.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/rtppacketizer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/opusrtppacketizer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/opuspacketizationhandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/h264rtppacketizer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/nalunit.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/h264packetizationhandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/av1rtppacketizer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/av1packetizationhandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/mediachainablehandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/mediahandlerelement.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/mediahandlerrootelement.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/nalunit.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/rtcpnackresponder.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/rtp.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/capi.cpp
Expand Down Expand Up @@ -112,15 +107,9 @@ set(LIBDATACHANNEL_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtcpsrreporter.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtppacketizer.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/opusrtppacketizer.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/opuspacketizationhandler.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/h264rtppacketizer.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/nalunit.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/h264packetizationhandler.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/av1rtppacketizer.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/av1packetizationhandler.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/mediachainablehandler.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/mediahandlerelement.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/mediahandlerrootelement.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/nalunit.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtcpnackresponder.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/utils.hpp
)
Expand Down
32 changes: 0 additions & 32 deletions include/rtc/av1packetizationhandler.hpp

This file was deleted.

36 changes: 26 additions & 10 deletions include/rtc/av1rtppacketizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@

#if RTC_ENABLE_MEDIA

#include "mediahandlerrootelement.hpp"
#include "mediahandler.hpp"
#include "nalunit.hpp"
#include "rtppacketizer.hpp"

namespace rtc {

/// RTP packetization of AV1 payload
class RTC_CPP_EXPORT AV1RtpPacketizer final : public RtpPacketizer, public MediaHandlerRootElement {
// RTP packetization of AV1 payload
class RTC_CPP_EXPORT AV1RtpPacketizer final : public RtpPacketizer {
shared_ptr<NalUnits> splitMessage(binary_ptr message);
const uint16_t maximumFragmentSize;

public:
/// Default clock rate for AV1 in RTP
// Default clock rate for AV1 in RTP
inline static const uint32_t defaultClockRate = 90 * 1000;

// Define how OBUs are seperated in a AV1 Sample
Expand All @@ -32,15 +32,14 @@ class RTC_CPP_EXPORT AV1RtpPacketizer final : public RtpPacketizer, public Media
TemporalUnit = RTC_OBU_PACKETIZED_TEMPORAL_UNIT,
};

/// Constructs AV1 payload packetizer with given RTP configuration.
/// @note RTP configuration is used in packetization process which may change some configuration
/// properties such as sequence number.
/// @param rtpConfig RTP configuration
// Constructs AV1 payload packetizer with given RTP configuration.
// @note RTP configuration is used in packetization process which may change some configuration
// properties such as sequence number.
AV1RtpPacketizer(Packetization packetization, shared_ptr<RtpPacketizationConfig> rtpConfig,
uint16_t maximumFragmentSize = NalUnits::defaultMaximumFragmentSize);

ChainedOutgoingProduct processOutgoingBinaryMessage(ChainedMessagesProduct messages,
message_ptr control) override;
message_ptr incoming(message_ptr message) override;
message_ptr outgoing(message_ptr message) override;

private:
const Packetization packetization;
Expand All @@ -49,6 +48,23 @@ class RTC_CPP_EXPORT AV1RtpPacketizer final : public RtpPacketizer, public Media
std::vector<shared_ptr<binary>> packetizeObu(binary_ptr message, uint16_t maximumFragmentSize);
};

// Dummy wrapper for backward compatibility
class RTC_CPP_EXPORT AV1PacketizationHandler final : public MediaHandler {
public:
AV1PacketizationHandler(shared_ptr<AV1RtpPacketizer> packetizer)
: mPacketizer(std::move(packetizer)) {}

inline message_ptr incoming(message_ptr message) override {
return mPacketizer->incoming(std::move(message));
}
inline message_ptr outgoing(message_ptr message) override {
return mPacketizer->outgoing(std::move(message));
}

private:
shared_ptr<AV1RtpPacketizer> mPacketizer;
};

} // namespace rtc

#endif /* RTC_ENABLE_MEDIA */
Expand Down
26 changes: 12 additions & 14 deletions include/rtc/description.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ class RTC_CPP_EXPORT Description {
public:
virtual ~Entry() = default;

virtual string type() const { return mType; }
virtual string description() const { return mDescription; }
virtual string mid() const { return mMid; }
virtual string type() const;
virtual string description() const;
virtual string mid() const;

Direction direction() const { return mDirection; }
Direction direction() const;
void setDirection(Direction dir);

bool isRemoved() const { return mIsRemoved; }
bool isRemoved() const;
void markRemoved();

std::vector<string> attributes() const;
Expand All @@ -109,6 +109,7 @@ class RTC_CPP_EXPORT Description {

std::vector<int> extIds();
ExtMap *extMap(int id);
const ExtMap *extMap(int id) const;
void addExtMap(ExtMap map);
void removeExtMap(int id);

Expand Down Expand Up @@ -144,12 +145,11 @@ class RTC_CPP_EXPORT Description {
string description() const override;
Application reciprocate() const;

void setSctpPort(uint16_t port) { mSctpPort = port; }
void hintSctpPort(uint16_t port) { mSctpPort = mSctpPort.value_or(port); }
void setMaxMessageSize(size_t size) { mMaxMessageSize = size; }

optional<uint16_t> sctpPort() const { return mSctpPort; }
optional<size_t> maxMessageSize() const { return mMaxMessageSize; }
void setSctpPort(uint16_t port);
void hintSctpPort(uint16_t port);
void setMaxMessageSize(size_t size);
optional<uint16_t> sctpPort() const;
optional<size_t> maxMessageSize() const;

virtual void parseSdpLine(string_view line) override;

Expand Down Expand Up @@ -208,10 +208,10 @@ class RTC_CPP_EXPORT Description {
bool hasPayloadType(int payloadType) const;
std::vector<int> payloadTypes() const;
RtpMap *rtpMap(int payloadType);
const RtpMap *rtpMap(int payloadType) const;
void addRtpMap(RtpMap map);
void removeRtpMap(int payloadType);
void removeFormat(const string &format);

void addRtxCodec(int payloadType, int origPayloadType, unsigned int clockRate);

virtual void parseSdpLine(string_view line) override;
Expand All @@ -233,9 +233,7 @@ class RTC_CPP_EXPORT Description {
void addAudioCodec(int payloadType, string codec, optional<string> profile = std::nullopt);

void addOpusCodec(int payloadType, optional<string> profile = DEFAULT_OPUS_AUDIO_PROFILE);

void addPCMACodec(int payloadType, optional<string> profile = std::nullopt);

void addPCMUCodec(int payloadType, optional<string> profile = std::nullopt);
};

Expand Down
32 changes: 0 additions & 32 deletions include/rtc/h264packetizationhandler.hpp

This file was deleted.

48 changes: 32 additions & 16 deletions include/rtc/h264rtppacketizer.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (c) 2020 Filip Klembara (in2core)
* Copyright (c) 2023 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
Expand All @@ -11,23 +12,18 @@

#if RTC_ENABLE_MEDIA

#include "mediahandlerrootelement.hpp"
#include "nalunit.hpp"
#include "rtppacketizer.hpp"

namespace rtc {

/// RTP packetization of h264 payload
class RTC_CPP_EXPORT H264RtpPacketizer final : public RtpPacketizer,
public MediaHandlerRootElement {
shared_ptr<NalUnits> splitMessage(binary_ptr message);
const uint16_t maximumFragmentSize;

// RTP packetization of h264 payload
class RTC_CPP_EXPORT H264RtpPacketizer final : public RtpPacketizer {
public:
/// Default clock rate for H264 in RTP
// Default clock rate for H264 in RTP
inline static const uint32_t defaultClockRate = 90 * 1000;

/// NAL unit separator
// NAL unit separator
enum class Separator {
Length = RTC_NAL_SEPARATOR_LENGTH, // first 4 bytes are NAL unit length
LongStartSequence = RTC_NAL_SEPARATOR_LONG_START_SEQUENCE, // 0x00, 0x00, 0x00, 0x01
Expand All @@ -39,21 +35,41 @@ class RTC_CPP_EXPORT H264RtpPacketizer final : public RtpPacketizer,
shared_ptr<RtpPacketizationConfig> rtpConfig,
uint16_t maximumFragmentSize = NalUnits::defaultMaximumFragmentSize);

/// Constructs h264 payload packetizer with given RTP configuration.
/// @note RTP configuration is used in packetization process which may change some configuration
/// properties such as sequence number.
/// @param rtpConfig RTP configuration
/// @param maximumFragmentSize maximum size of one NALU fragment
// Constructs h264 payload packetizer with given RTP configuration.
// @note RTP configuration is used in packetization process which may change some configuration
// properties such as sequence number.
// @param rtpConfig RTP configuration
// @param maximumFragmentSize maximum size of one NALU fragment
H264RtpPacketizer(shared_ptr<RtpPacketizationConfig> rtpConfig,
uint16_t maximumFragmentSize = NalUnits::defaultMaximumFragmentSize);

ChainedOutgoingProduct processOutgoingBinaryMessage(ChainedMessagesProduct messages,
message_ptr control) override;
message_ptr incoming(message_ptr message) override;
message_ptr outgoing(message_ptr message) override;

private:
shared_ptr<NalUnits> splitMessage(binary_ptr message);

const uint16_t maximumFragmentSize;
const Separator separator;
};

// Dummy wrapper for backward compatibility
class RTC_CPP_EXPORT H264PacketizationHandler final : public MediaHandler {
public:
H264PacketizationHandler(shared_ptr<H264RtpPacketizer> packetizer)
: mPacketizer(std::move(packetizer)) {}

inline message_ptr incoming(message_ptr message) override {
return mPacketizer->incoming(std::move(message));
}
inline message_ptr outgoing(message_ptr message) override {
return mPacketizer->outgoing(std::move(message));
}

private:
shared_ptr<H264RtpPacketizer> mPacketizer;
};

} // namespace rtc

#endif /* RTC_ENABLE_MEDIA */
Expand Down
48 changes: 0 additions & 48 deletions include/rtc/mediachainablehandler.hpp

This file was deleted.

Loading

0 comments on commit 0dfbc8b

Please sign in to comment.