Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a media handler to respond to intra frame requests #999

Merged
merged 4 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ set(LIBDATACHANNEL_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/rtcpnackresponder.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/rtp.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/capi.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/pliresponder.cpp
)

set(LIBDATACHANNEL_HEADERS
Expand Down Expand Up @@ -132,6 +133,7 @@ set(LIBDATACHANNEL_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/mediahandlerrootelement.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtcpnackresponder.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/utils.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/pliresponder.hpp
)

set(LIBDATACHANNEL_IMPL_SOURCES
Expand Down
35 changes: 35 additions & 0 deletions include/rtc/pliresponder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) 2023 Arda Cinar
*
* 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_PLI_RESPONDER_H
#define RTC_PLI_RESPONDER_H

#if RTC_ENABLE_MEDIA

#include "mediahandlerelement.hpp"
#include <functional>

namespace rtc {

/// Responds to PLI and FIR messages sent by the receiver. The sender should respond to these
/// messages by sending an intra.
class RTC_CPP_EXPORT PliResponder final : public MediaHandlerElement {
kuzux marked this conversation as resolved.
Show resolved Hide resolved
std::function<void(void)> onPli;
kuzux marked this conversation as resolved.
Show resolved Hide resolved

public:
/// Constructs the PLIResponder object to notify whenever a new intra frame is requested
/// @param onPli The callback that gets called whenever an intra frame is requested by the receiver
PliResponder(std::function<void(void)> onPli);
ChainedIncomingControlProduct processIncomingControlMessage(message_ptr) override;
};

}

#endif // RTC_ENABLE_MEDIA

#endif // RTC_PLI_RESPONDER_H
2 changes: 2 additions & 0 deletions include/rtc/rtc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "rtcpreceivingsession.hpp"
#include "rtcpsrreporter.hpp"

#include "pliresponder.hpp"

// Opus/AAC/h264/h265/AV1 streaming
#include "aacrtppacketizer.hpp"
#include "av1packetizationhandler.hpp"
Expand Down
44 changes: 44 additions & 0 deletions src/pliresponder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright (c) 2023 Arda Cinar
*
* 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/.
*/

#include "pliresponder.hpp"

#if RTC_ENABLE_MEDIA

namespace rtc {

ChainedIncomingControlProduct PliResponder::processIncomingControlMessage(message_ptr message) {
size_t offset = 0;

while ((sizeof(RtcpHeader) + offset) <= message->size()) {
auto header = reinterpret_cast<rtc::RtcpHeader*>(message->data() + offset);
uint8_t payload_type = header->payloadType();

if (payload_type == 196) {
// FIR message, call pli handler anyway
onPli();
break;
} else if (payload_type == 206) {
// On a payload specific fb message, there is a "feedback message type" (FMT) in the
// header instead of a report count. PT = 206, FMT = 1 means a PLI message
uint8_t feedback_message_type = header->reportCount();
if (feedback_message_type == 1) {
onPli();
break;
}
}
offset += header->lengthInBytes();
}
return { message, std::nullopt };
}

PliResponder::PliResponder(std::function<void(void)> onPli) : onPli(onPli) { }

}

#endif // RTC_ENABLE_MEDIA
Loading