Skip to content

Commit

Permalink
Register callback for key frame request from media transport.
Browse files Browse the repository at this point in the history
Bug: webrtc:9719
Change-Id: Ibeadadb8e477d6d712fd69427c95e1e4f1940854
Reviewed-on: https://webrtc-review.googlesource.com/c/120340
Commit-Queue: Niels Moller <nisse@webrtc.org>
Commit-Queue: Peter Slatala <psla@webrtc.org>
Reviewed-by: Peter Slatala <psla@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26473}
  • Loading branch information
Niels Möller authored and Commit Bot committed Jan 30, 2019
1 parent 9846262 commit fa89d84
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 30 deletions.
6 changes: 3 additions & 3 deletions video/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ rtc_static_library("video") {
"buffered_frame_decryptor.h",
"call_stats.cc",
"call_stats.h",
"encoder_rtcp_feedback.cc",
"encoder_rtcp_feedback.h",
"encoder_key_frame_callback.cc",
"encoder_key_frame_callback.h",
"quality_threshold.cc",
"quality_threshold.h",
"receive_statistics_proxy.cc",
Expand Down Expand Up @@ -454,7 +454,7 @@ if (rtc_include_tests) {
"buffered_frame_decryptor_unittest.cc",
"call_stats_unittest.cc",
"cpu_scaling_tests.cc",
"encoder_rtcp_feedback_unittest.cc",
"encoder_key_frame_callback_unittest.cc",
"end_to_end_tests/bandwidth_tests.cc",
"end_to_end_tests/call_operation_tests.cc",
"end_to_end_tests/codec_tests.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@
* be found in the AUTHORS file in the root of the source tree.
*/

#include "video/encoder_rtcp_feedback.h"
#include "video/encoder_key_frame_callback.h"

#include "api/video/video_stream_encoder_interface.h"
#include "rtc_base/checks.h"

static const int kMinKeyFrameRequestIntervalMs = 300;

namespace webrtc {

EncoderRtcpFeedback::EncoderRtcpFeedback(Clock* clock,
const std::vector<uint32_t>& ssrcs,
VideoStreamEncoderInterface* encoder)
EncoderKeyFrameCallback::EncoderKeyFrameCallback(
Clock* clock,
const std::vector<uint32_t>& ssrcs,
VideoStreamEncoderInterface* encoder)
: clock_(clock),
ssrcs_(ssrcs),
video_stream_encoder_(encoder),
time_last_intra_request_ms_(-1) {
RTC_DCHECK(!ssrcs.empty());
}

bool EncoderRtcpFeedback::HasSsrc(uint32_t ssrc) {
bool EncoderKeyFrameCallback::HasSsrc(uint32_t ssrc) {
for (uint32_t registered_ssrc : ssrcs_) {
if (registered_ssrc == ssrc) {
return true;
Expand All @@ -36,7 +36,7 @@ bool EncoderRtcpFeedback::HasSsrc(uint32_t ssrc) {
return false;
}

void EncoderRtcpFeedback::OnReceivedIntraFrameRequest(uint32_t ssrc) {
void EncoderKeyFrameCallback::OnReceivedIntraFrameRequest(uint32_t ssrc) {
RTC_DCHECK(HasSsrc(ssrc));
{
int64_t now_ms = clock_->TimeInMilliseconds();
Expand All @@ -51,4 +51,14 @@ void EncoderRtcpFeedback::OnReceivedIntraFrameRequest(uint32_t ssrc) {
video_stream_encoder_->SendKeyFrame();
}

void EncoderKeyFrameCallback::OnKeyFrameRequested(uint64_t channel_id) {
if (channel_id != ssrcs_[0]) {
RTC_LOG(LS_INFO) << "Key frame request on unknown channel id " << channel_id
<< " expected " << ssrcs_[0];
return;
}

video_stream_encoder_->SendKeyFrame();
}

} // namespace webrtc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef VIDEO_ENCODER_RTCP_FEEDBACK_H_
#define VIDEO_ENCODER_RTCP_FEEDBACK_H_
#ifndef VIDEO_ENCODER_KEY_FRAME_CALLBACK_H_
#define VIDEO_ENCODER_KEY_FRAME_CALLBACK_H_

#include <vector>

#include "api/media_transport_interface.h"
#include "api/video/video_stream_encoder_interface.h"
#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
#include "rtc_base/critical_section.h"
#include "system_wrappers/include/clock.h"
Expand All @@ -20,13 +22,21 @@ namespace webrtc {

class VideoStreamEncoderInterface;

class EncoderRtcpFeedback : public RtcpIntraFrameObserver {
// This class receives keyframe requests from either Mediatransport or the
// RtpRtcp module.
// TODO(bugs.webrtc.org/9719): Should be eliminated when RtpMediaTransport is
// implemented.
class EncoderKeyFrameCallback : public RtcpIntraFrameObserver,
public MediaTransportKeyFrameRequestCallback {
public:
EncoderRtcpFeedback(Clock* clock,
const std::vector<uint32_t>& ssrcs,
VideoStreamEncoderInterface* encoder);
EncoderKeyFrameCallback(Clock* clock,
const std::vector<uint32_t>& ssrcs,
VideoStreamEncoderInterface* encoder);
void OnReceivedIntraFrameRequest(uint32_t ssrc) override;

// Implements MediaTransportKeyFrameRequestCallback
void OnKeyFrameRequested(uint64_t channel_id) override;

private:
bool HasSsrc(uint32_t ssrc);

Expand All @@ -40,4 +50,4 @@ class EncoderRtcpFeedback : public RtcpIntraFrameObserver {

} // namespace webrtc

#endif // VIDEO_ENCODER_RTCP_FEEDBACK_H_
#endif // VIDEO_ENCODER_KEY_FRAME_CALLBACK_H_
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* be found in the AUTHORS file in the root of the source tree.
*/

#include "video/encoder_rtcp_feedback.h"
#include "video/encoder_key_frame_callback.h"

#include <memory>

Expand All @@ -25,7 +25,7 @@ class VieKeyRequestTest : public ::testing::Test {
VieKeyRequestTest()
: simulated_clock_(123456789),
encoder_(),
encoder_rtcp_feedback_(
encoder_key_frame_callback_(
&simulated_clock_,
std::vector<uint32_t>(1, VieKeyRequestTest::kSsrc),
&encoder_) {}
Expand All @@ -35,26 +35,31 @@ class VieKeyRequestTest : public ::testing::Test {

SimulatedClock simulated_clock_;
testing::StrictMock<MockVideoStreamEncoder> encoder_;
EncoderRtcpFeedback encoder_rtcp_feedback_;
EncoderKeyFrameCallback encoder_key_frame_callback_;
};

TEST_F(VieKeyRequestTest, CreateAndTriggerRequests) {
EXPECT_CALL(encoder_, SendKeyFrame()).Times(1);
encoder_rtcp_feedback_.OnReceivedIntraFrameRequest(kSsrc);
encoder_key_frame_callback_.OnReceivedIntraFrameRequest(kSsrc);
}

TEST_F(VieKeyRequestTest, TooManyOnReceivedIntraFrameRequest) {
EXPECT_CALL(encoder_, SendKeyFrame()).Times(1);
encoder_rtcp_feedback_.OnReceivedIntraFrameRequest(kSsrc);
encoder_rtcp_feedback_.OnReceivedIntraFrameRequest(kSsrc);
encoder_key_frame_callback_.OnReceivedIntraFrameRequest(kSsrc);
encoder_key_frame_callback_.OnReceivedIntraFrameRequest(kSsrc);
simulated_clock_.AdvanceTimeMilliseconds(10);
encoder_rtcp_feedback_.OnReceivedIntraFrameRequest(kSsrc);
encoder_key_frame_callback_.OnReceivedIntraFrameRequest(kSsrc);

EXPECT_CALL(encoder_, SendKeyFrame()).Times(1);
simulated_clock_.AdvanceTimeMilliseconds(300);
encoder_rtcp_feedback_.OnReceivedIntraFrameRequest(kSsrc);
encoder_rtcp_feedback_.OnReceivedIntraFrameRequest(kSsrc);
encoder_rtcp_feedback_.OnReceivedIntraFrameRequest(kSsrc);
encoder_key_frame_callback_.OnReceivedIntraFrameRequest(kSsrc);
encoder_key_frame_callback_.OnReceivedIntraFrameRequest(kSsrc);
encoder_key_frame_callback_.OnReceivedIntraFrameRequest(kSsrc);
}

TEST_F(VieKeyRequestTest, TriggerRequestFromMediaTransport) {
EXPECT_CALL(encoder_, SendKeyFrame()).Times(1);
encoder_key_frame_callback_.OnKeyFrameRequested(kSsrc);
}

} // namespace webrtc
6 changes: 5 additions & 1 deletion video/video_send_stream_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ RtpSenderFrameEncryptionConfig CreateFrameEncryptionConfig(
}

RtpSenderObservers CreateObservers(CallStats* call_stats,
EncoderRtcpFeedback* encoder_feedback,
EncoderKeyFrameCallback* encoder_feedback,
SendStatisticsProxy* stats_proxy,
SendDelayStats* send_delay_stats) {
RtpSenderObservers observers;
Expand Down Expand Up @@ -242,6 +242,7 @@ VideoSendStreamImpl::VideoSendStreamImpl(
// The configured ssrc is interpreted as a channel id, so there must be
// exactly one.
RTC_DCHECK_EQ(config_->rtp.ssrcs.size(), 1);
media_transport_->SetKeyFrameRequestCallback(&encoder_feedback_);
} else {
RTC_DCHECK(!config_->rtp.ssrcs.empty());
}
Expand Down Expand Up @@ -324,6 +325,9 @@ VideoSendStreamImpl::~VideoSendStreamImpl() {
<< "VideoSendStreamImpl::Stop not called";
RTC_LOG(LS_INFO) << "~VideoSendStreamInternal: " << config_->ToString();
transport_->DestroyRtpVideoSender(rtp_video_sender_);
if (media_transport_) {
media_transport_->SetKeyFrameRequestCallback(nullptr);
}
}

void VideoSendStreamImpl::RegisterProcessThread(
Expand Down
4 changes: 2 additions & 2 deletions video/video_send_stream_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include "rtc_base/thread_annotations.h"
#include "rtc_base/weak_ptr.h"
#include "video/call_stats.h"
#include "video/encoder_rtcp_feedback.h"
#include "video/encoder_key_frame_callback.h"
#include "video/send_delay_stats.h"
#include "video/send_statistics_proxy.h"
#include "video/video_send_stream.h"
Expand Down Expand Up @@ -166,7 +166,7 @@ class VideoSendStreamImpl : public webrtc::BitrateAllocatorObserver,
bool has_packet_feedback_;

VideoStreamEncoderInterface* const video_stream_encoder_;
EncoderRtcpFeedback encoder_feedback_;
EncoderKeyFrameCallback encoder_feedback_;

RtcpBandwidthObserver* const bandwidth_observer_;
RtpVideoSenderInterface* const rtp_video_sender_;
Expand Down

0 comments on commit fa89d84

Please sign in to comment.