Skip to content

Commit

Permalink
Refactor media handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
paullouisageneau committed Oct 21, 2023
1 parent 2cea835 commit 7ffd00a
Show file tree
Hide file tree
Showing 47 changed files with 671 additions and 1,358 deletions.
17 changes: 3 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,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 @@ -76,19 +77,13 @@ 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/aacrtppacketizer.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/h265rtppacketizer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/h265nalunit.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/h265packetizationhandler.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 @@ -117,19 +112,13 @@ 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/aacrtppacketizer.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/h265rtppacketizer.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/h265nalunit.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/h265packetizationhandler.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
40 changes: 18 additions & 22 deletions include/rtc/aacrtppacketizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@

#if RTC_ENABLE_MEDIA

#include "mediachainablehandler.hpp"
#include "mediahandlerrootelement.hpp"
#include "rtppacketizer.hpp"

namespace rtc {

/// RTP packetizer for aac
class RTC_CPP_EXPORT AACRtpPacketizer final : public RtpPacketizer, public MediaHandlerRootElement {
// RTP packetizer for AAC
class RTC_CPP_EXPORT AACRtpPacketizer final : public RtpPacketizer {
public:
/// default clock rate used in aac RTP communication
inline static const uint32_t defaultClockRate = 48 * 1000;
Expand All @@ -29,30 +27,28 @@ class RTC_CPP_EXPORT AACRtpPacketizer final : public RtpPacketizer, public Media
/// @param rtpConfig RTP configuration
AACRtpPacketizer(shared_ptr<RtpPacketizationConfig> rtpConfig);

/// Creates RTP packet for given payload based on `rtpConfig`.
/// @note This function increase sequence number after packetization.
/// @param payload RTP payload
/// @param setMark This needs to be `false` for all RTP packets with aac payload
binary_ptr packetize(binary_ptr payload, bool setMark) override;

/// Creates RTP packet for given samples (all samples share same RTP timesamp)
/// @param messages aac samples
/// @param control RTCP
/// @returns RTP packets and unchanged `control`
ChainedOutgoingProduct processOutgoingBinaryMessage(ChainedMessagesProduct messages,
message_ptr control) override;
void incoming(message_vector &messages, const message_callback &send) override;
void outgoing(message_vector &messages, const message_callback &send) override;
};

/// Handler for aac packetization
class RTC_CPP_EXPORT AACPacketizationHandler final : public MediaChainableHandler {

// Dummy wrapper for backward compatibility
class RTC_CPP_EXPORT AACPacketizationHandler final : public MediaHandler {
public:
/// Construct handler for aac packetization.
/// @param packetizer RTP packetizer for aac
AACPacketizationHandler(shared_ptr<AACRtpPacketizer> packetizer)
: MediaChainableHandler(packetizer) {}
: mPacketizer(std::move(packetizer)) {}

inline void incoming(message_vector &messages, const message_callback &send) {
return mPacketizer->incoming(messages, send);
}
inline void outgoing(message_vector &messages, const message_callback &send) {
return mPacketizer->outgoing(messages, send);
}

private:
shared_ptr<AACRtpPacketizer> mPacketizer;
};


} // namespace rtc

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

This file was deleted.

43 changes: 29 additions & 14 deletions include/rtc/av1rtppacketizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,16 @@

#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 {
shared_ptr<NalUnits> splitMessage(binary_ptr message);
const uint16_t maximumFragmentSize;

// RTP packetization of AV1 payload
class RTC_CPP_EXPORT AV1RtpPacketizer final : public RtpPacketizer {
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,21 +29,39 @@ 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;
void incoming(message_vector &messages, const message_callback &send) override;
void outgoing(message_vector &messages, const message_callback &send) override;

private:
shared_ptr<NalUnits> splitMessage(binary_ptr message);
std::vector<shared_ptr<binary>> packetizeObu(binary_ptr message, uint16_t maximumFragmentSize);

const uint16_t maximumFragmentSize;
const Packetization packetization;
std::shared_ptr<binary> sequenceHeader;
};

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 void incoming(message_vector &messages, const message_callback &send) {
return mPacketizer->incoming(messages, send);
}
inline void outgoing(message_vector &messages, const message_callback &send) {
return mPacketizer->outgoing(messages, send);
}

private:
shared_ptr<AV1RtpPacketizer> mPacketizer;
};

} // namespace rtc
Expand Down
4 changes: 4 additions & 0 deletions include/rtc/description.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -208,6 +209,7 @@ 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);
Expand All @@ -233,7 +235,9 @@ 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);
void addAacCodec(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: 33 additions & 15 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,42 +12,59 @@

#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 for H264
class RTC_CPP_EXPORT H264RtpPacketizer final : public RtpPacketizer {
public:
using Separator = NalUnit::Separator;

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

// 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 separator NAL unit separator
// @param rtpConfig RTP configuration
// @param maximumFragmentSize maximum size of one NALU fragment
H264RtpPacketizer(Separator separator, 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
// TODO: deprecate
H264RtpPacketizer(shared_ptr<RtpPacketizationConfig> rtpConfig,
uint16_t maximumFragmentSize = NalUnits::defaultMaximumFragmentSize);

ChainedOutgoingProduct processOutgoingBinaryMessage(ChainedMessagesProduct messages,
message_ptr control) override;
void incoming(message_vector &messages, const message_callback &send) override;
void outgoing(message_vector &messages, const message_callback &send) 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 void incoming(message_vector &messages, const message_callback &send) {
return mPacketizer->incoming(messages, send);
}
inline void outgoing(message_vector &messages, const message_callback &send) {
return mPacketizer->outgoing(messages, send);
}

private:
shared_ptr<H264RtpPacketizer> mPacketizer;
};

} // namespace rtc

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

This file was deleted.

Loading

0 comments on commit 7ffd00a

Please sign in to comment.