blob: 3116221da2b8d1b9a7a77d3071f0a1dc18052bce [file] [log] [blame]
mflodman15d83572016-10-06 08:35:11 -07001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Niels Möllerfa89d842019-01-30 16:33:45 +010011#include "video/encoder_key_frame_callback.h"
mflodman15d83572016-10-06 08:35:11 -070012
Rasmus Brandt3dde4502019-03-21 11:46:17 +010013#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/checks.h"
Rasmus Brandt3dde4502019-03-21 11:46:17 +010015#include "rtc_base/experiments/keyframe_interval_settings.h"
mflodman15d83572016-10-06 08:35:11 -070016
17namespace webrtc {
18
Rasmus Brandt3dde4502019-03-21 11:46:17 +010019namespace {
20constexpr int kMinKeyframeSendIntervalMs = 300;
21} // namespace
22
Niels Möllerfa89d842019-01-30 16:33:45 +010023EncoderKeyFrameCallback::EncoderKeyFrameCallback(
24 Clock* clock,
25 const std::vector<uint32_t>& ssrcs,
26 VideoStreamEncoderInterface* encoder)
mflodman15d83572016-10-06 08:35:11 -070027 : clock_(clock),
28 ssrcs_(ssrcs),
mflodmancc3d4422017-08-03 08:27:51 -070029 video_stream_encoder_(encoder),
Rasmus Brandt3dde4502019-03-21 11:46:17 +010030 time_last_intra_request_ms_(-1),
31 min_keyframe_send_interval_ms_(
32 KeyframeIntervalSettings::ParseFromFieldTrials()
33 .MinKeyframeSendIntervalMs()
34 .value_or(kMinKeyframeSendIntervalMs)) {
mflodman15d83572016-10-06 08:35:11 -070035 RTC_DCHECK(!ssrcs.empty());
36}
37
Niels Möllerfa89d842019-01-30 16:33:45 +010038bool EncoderKeyFrameCallback::HasSsrc(uint32_t ssrc) {
mflodman15d83572016-10-06 08:35:11 -070039 for (uint32_t registered_ssrc : ssrcs_) {
40 if (registered_ssrc == ssrc) {
41 return true;
42 }
43 }
44 return false;
45}
46
Niels Möllerfa89d842019-01-30 16:33:45 +010047void EncoderKeyFrameCallback::OnReceivedIntraFrameRequest(uint32_t ssrc) {
mflodman15d83572016-10-06 08:35:11 -070048 RTC_DCHECK(HasSsrc(ssrc));
mflodman15d83572016-10-06 08:35:11 -070049 {
mflodman15d83572016-10-06 08:35:11 -070050 int64_t now_ms = clock_->TimeInMilliseconds();
51 rtc::CritScope lock(&crit_);
Rasmus Brandt3dde4502019-03-21 11:46:17 +010052 if (time_last_intra_request_ms_ + min_keyframe_send_interval_ms_ > now_ms) {
mflodman15d83572016-10-06 08:35:11 -070053 return;
54 }
Magnus Flodman3c62afd2018-09-24 11:44:49 +020055 time_last_intra_request_ms_ = now_ms;
mflodman15d83572016-10-06 08:35:11 -070056 }
57
Niels Möller1c9aa1e2018-02-16 10:27:23 +010058 // Always produce key frame for all streams.
59 video_stream_encoder_->SendKeyFrame();
mflodman15d83572016-10-06 08:35:11 -070060}
61
Niels Möllerfa89d842019-01-30 16:33:45 +010062void EncoderKeyFrameCallback::OnKeyFrameRequested(uint64_t channel_id) {
63 if (channel_id != ssrcs_[0]) {
64 RTC_LOG(LS_INFO) << "Key frame request on unknown channel id " << channel_id
65 << " expected " << ssrcs_[0];
66 return;
67 }
68
69 video_stream_encoder_->SendKeyFrame();
70}
71
mflodman15d83572016-10-06 08:35:11 -070072} // namespace webrtc