2016-10-06 08:35:11 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-01-30 16:33:45 +01:00
|
|
|
#include "video/encoder_key_frame_callback.h"
|
2016-10-06 08:35:11 -07:00
|
|
|
|
2019-03-21 11:46:17 +01:00
|
|
|
#include "absl/types/optional.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/checks.h"
|
2019-03-21 11:46:17 +01:00
|
|
|
#include "rtc_base/experiments/keyframe_interval_settings.h"
|
2016-10-06 08:35:11 -07:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2019-03-21 11:46:17 +01:00
|
|
|
namespace {
|
|
|
|
|
constexpr int kMinKeyframeSendIntervalMs = 300;
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2019-01-30 16:33:45 +01:00
|
|
|
EncoderKeyFrameCallback::EncoderKeyFrameCallback(
|
|
|
|
|
Clock* clock,
|
|
|
|
|
const std::vector<uint32_t>& ssrcs,
|
|
|
|
|
VideoStreamEncoderInterface* encoder)
|
2016-10-06 08:35:11 -07:00
|
|
|
: clock_(clock),
|
|
|
|
|
ssrcs_(ssrcs),
|
2017-08-03 08:27:51 -07:00
|
|
|
video_stream_encoder_(encoder),
|
2019-03-21 11:46:17 +01:00
|
|
|
time_last_intra_request_ms_(-1),
|
|
|
|
|
min_keyframe_send_interval_ms_(
|
|
|
|
|
KeyframeIntervalSettings::ParseFromFieldTrials()
|
|
|
|
|
.MinKeyframeSendIntervalMs()
|
|
|
|
|
.value_or(kMinKeyframeSendIntervalMs)) {
|
2016-10-06 08:35:11 -07:00
|
|
|
RTC_DCHECK(!ssrcs.empty());
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-30 16:33:45 +01:00
|
|
|
bool EncoderKeyFrameCallback::HasSsrc(uint32_t ssrc) {
|
2016-10-06 08:35:11 -07:00
|
|
|
for (uint32_t registered_ssrc : ssrcs_) {
|
|
|
|
|
if (registered_ssrc == ssrc) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-30 16:33:45 +01:00
|
|
|
void EncoderKeyFrameCallback::OnReceivedIntraFrameRequest(uint32_t ssrc) {
|
2016-10-06 08:35:11 -07:00
|
|
|
RTC_DCHECK(HasSsrc(ssrc));
|
|
|
|
|
{
|
|
|
|
|
int64_t now_ms = clock_->TimeInMilliseconds();
|
|
|
|
|
rtc::CritScope lock(&crit_);
|
2019-03-21 11:46:17 +01:00
|
|
|
if (time_last_intra_request_ms_ + min_keyframe_send_interval_ms_ > now_ms) {
|
2016-10-06 08:35:11 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2018-09-24 11:44:49 +02:00
|
|
|
time_last_intra_request_ms_ = now_ms;
|
2016-10-06 08:35:11 -07:00
|
|
|
}
|
|
|
|
|
|
2018-02-16 10:27:23 +01:00
|
|
|
// Always produce key frame for all streams.
|
|
|
|
|
video_stream_encoder_->SendKeyFrame();
|
2016-10-06 08:35:11 -07:00
|
|
|
}
|
|
|
|
|
|
2019-01-30 16:33:45 +01:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-06 08:35:11 -07:00
|
|
|
} // namespace webrtc
|