Skip to content

Commit

Permalink
Merge pull request #929 from paullouisageneau/refactor-media-handler
Browse files Browse the repository at this point in the history
Refactor media handlers
  • Loading branch information
paullouisageneau authored Oct 29, 2023
2 parents 5546355 + db9841d commit 8c67a2c
Show file tree
Hide file tree
Showing 53 changed files with 708 additions and 1,566 deletions.
21 changes: 3 additions & 18 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 @@ -75,20 +76,12 @@ 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/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,20 +110,12 @@ 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/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
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/plihandler.hpp
Expand Down
7 changes: 3 additions & 4 deletions examples/media-sfu/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ int main() {

auto track = pc->addTrack(media);

auto session = std::make_shared<rtc::RtcpReceivingSession>();
track->setMediaHandler(session);
track->setMediaHandler(std::make_shared<rtc::RtcpReceivingSession>());

const rtc::SSRC targetSSRC = 42;
track->onMessage(
Expand Down Expand Up @@ -101,8 +100,8 @@ int main() {

r->track = r->conn->addTrack(media);

r->track->onOpen([session]() {
session->requestKeyframe(); // So the receiver can start playing immediately
r->track->onOpen([r]() {
r->track->requestKeyframe(); // So the receiver can start playing immediately
});
r->track->onMessage([](rtc::binary var) {}, nullptr);

Expand Down
18 changes: 7 additions & 11 deletions examples/streamer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,14 @@ shared_ptr<ClientTrackData> addVideo(const shared_ptr<PeerConnection> pc, const
auto rtpConfig = make_shared<RtpPacketizationConfig>(ssrc, cname, payloadType, H264RtpPacketizer::defaultClockRate);
// create packetizer
auto packetizer = make_shared<H264RtpPacketizer>(NalUnit::Separator::Length, rtpConfig);
// create H264 handler
auto h264Handler = make_shared<H264PacketizationHandler>(packetizer);
// add RTCP SR handler
auto srReporter = make_shared<RtcpSrReporter>(rtpConfig);
h264Handler->addToChain(srReporter);
packetizer->addToChain(srReporter);
// add RTCP NACK handler
auto nackResponder = make_shared<RtcpNackResponder>();
h264Handler->addToChain(nackResponder);
packetizer->addToChain(nackResponder);
// set handler
track->setMediaHandler(h264Handler);
track->setMediaHandler(packetizer);
track->onOpen(onOpen);
auto trackData = make_shared<ClientTrackData>(track, srReporter);
return trackData;
Expand All @@ -232,19 +230,17 @@ shared_ptr<ClientTrackData> addAudio(const shared_ptr<PeerConnection> pc, const
audio.addSSRC(ssrc, cname, msid, cname);
auto track = pc->addTrack(audio);
// create RTP configuration
auto rtpConfig = make_shared<RtpPacketizationConfig>(ssrc, cname, payloadType, OpusRtpPacketizer::defaultClockRate);
auto rtpConfig = make_shared<RtpPacketizationConfig>(ssrc, cname, payloadType, OpusRtpPacketizer::DefaultClockRate);
// create packetizer
auto packetizer = make_shared<OpusRtpPacketizer>(rtpConfig);
// create opus handler
auto opusHandler = make_shared<OpusPacketizationHandler>(packetizer);
// add RTCP SR handler
auto srReporter = make_shared<RtcpSrReporter>(rtpConfig);
opusHandler->addToChain(srReporter);
packetizer->addToChain(srReporter);
// add RTCP NACK handler
auto nackResponder = make_shared<RtcpNackResponder>();
opusHandler->addToChain(nackResponder);
packetizer->addToChain(nackResponder);
// set handler
track->setMediaHandler(opusHandler);
track->setMediaHandler(packetizer);
track->onOpen(onOpen);
auto trackData = make_shared<ClientTrackData>(track, srReporter);
return trackData;
Expand Down
2 changes: 1 addition & 1 deletion examples/streamer/stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class StreamSource {
virtual rtc::binary getSample() = 0;
};

class Stream: std::enable_shared_from_this<Stream> {
class Stream: public std::enable_shared_from_this<Stream> {
uint64_t startTime = 0;
std::mutex mutex;
DispatchQueue dispatchQueue = DispatchQueue("StreamQueue");
Expand Down
60 changes: 0 additions & 60 deletions include/rtc/aacrtppacketizer.hpp

This file was deleted.

32 changes: 0 additions & 32 deletions include/rtc/av1packetizationhandler.hpp

This file was deleted.

30 changes: 15 additions & 15 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,23 +29,26 @@ 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 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);
};

// For backward compatibility, do not use
using AV1PacketizationHandler [[deprecated("Add AV1RtpPacketizer directly")]] = PacketizationHandler;

} // namespace rtc

#endif /* RTC_ENABLE_MEDIA */
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.

Loading

0 comments on commit 8c67a2c

Please sign in to comment.