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 12, 2023
1 parent 9b0f18c commit f099372
Show file tree
Hide file tree
Showing 39 changed files with 442 additions and 1,106 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
2 changes: 2 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 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.

39 changes: 27 additions & 12 deletions include/rtc/mediahandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,43 @@
#define RTC_MEDIA_HANDLER_H

#include "common.hpp"
#include "description.hpp"
#include "message.hpp"

namespace rtc {

class RTC_CPP_EXPORT MediaHandler {
protected:
// Use this callback when trying to send custom data (such as RTCP) to the client.
synchronized_callback<message_ptr> outgoingCallback;

class RTC_CPP_EXPORT MediaHandler : public std::enable_shared_from_this<MediaHandler> {
public:
// Called when a media is added or updated
virtual void media([[maybe_unused]] const Description::Media &desc) {}

// Called when there is traffic coming from the peer
virtual message_ptr incoming(message_ptr ptr) = 0;
virtual message_ptr incoming(message_ptr message) = 0;

// Called when there is traffic that needs to be sent to the peer
virtual message_ptr outgoing(message_ptr ptr) = 0;
virtual message_ptr outgoing(message_ptr message) = 0;

// This callback is used to send traffic back to the peer
void onOutgoing(const std::function<void(message_ptr)> &cb);

void send(message_ptr message);

void addToChain(shared_ptr<MediaHandler> handler);
void setNext(shared_ptr<MediaHandler> handler);
shared_ptr<MediaHandler> next();
shared_ptr<const MediaHandler> next() const;
shared_ptr<MediaHandler> last(); // never null
shared_ptr<const MediaHandler> last() const; // never null

virtual bool requestKeyframe();

// This callback is used to send traffic back to the peer.
void onOutgoing(const std::function<void(message_ptr)> &cb) {
this->outgoingCallback = synchronized_callback<message_ptr>(cb);
}
void mediaChain(const Description::Media &desc);
message_ptr incomingChain(message_ptr message);
message_ptr outgoingChain(message_ptr message);

virtual bool requestKeyframe() { return false; }
private:
synchronized_callback<message_ptr> mOutgoingCallback;
shared_ptr<MediaHandler> mNext;
};

} // namespace rtc
Expand Down
Loading

0 comments on commit f099372

Please sign in to comment.