blob: 447a3af95596cfa8be6108f8ac71a5312fc8bcbe [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/checks.h"
mflodman15d83572016-10-06 08:35:11 -070014
15static const int kMinKeyFrameRequestIntervalMs = 300;
16
17namespace webrtc {
18
Niels Möllerfa89d842019-01-30 16:33:45 +010019EncoderKeyFrameCallback::EncoderKeyFrameCallback(
20 Clock* clock,
21 const std::vector<uint32_t>& ssrcs,
22 VideoStreamEncoderInterface* encoder)
mflodman15d83572016-10-06 08:35:11 -070023 : clock_(clock),
24 ssrcs_(ssrcs),
mflodmancc3d4422017-08-03 08:27:51 -070025 video_stream_encoder_(encoder),
Magnus Flodman3c62afd2018-09-24 11:44:49 +020026 time_last_intra_request_ms_(-1) {
mflodman15d83572016-10-06 08:35:11 -070027 RTC_DCHECK(!ssrcs.empty());
28}
29
Niels Möllerfa89d842019-01-30 16:33:45 +010030bool EncoderKeyFrameCallback::HasSsrc(uint32_t ssrc) {
mflodman15d83572016-10-06 08:35:11 -070031 for (uint32_t registered_ssrc : ssrcs_) {
32 if (registered_ssrc == ssrc) {
33 return true;
34 }
35 }
36 return false;
37}
38
Niels Möllerfa89d842019-01-30 16:33:45 +010039void EncoderKeyFrameCallback::OnReceivedIntraFrameRequest(uint32_t ssrc) {
mflodman15d83572016-10-06 08:35:11 -070040 RTC_DCHECK(HasSsrc(ssrc));
mflodman15d83572016-10-06 08:35:11 -070041 {
mflodman15d83572016-10-06 08:35:11 -070042 int64_t now_ms = clock_->TimeInMilliseconds();
43 rtc::CritScope lock(&crit_);
Magnus Flodman3c62afd2018-09-24 11:44:49 +020044 if (time_last_intra_request_ms_ + kMinKeyFrameRequestIntervalMs > now_ms) {
mflodman15d83572016-10-06 08:35:11 -070045 return;
46 }
Magnus Flodman3c62afd2018-09-24 11:44:49 +020047 time_last_intra_request_ms_ = now_ms;
mflodman15d83572016-10-06 08:35:11 -070048 }
49
Niels Möller1c9aa1e2018-02-16 10:27:23 +010050 // Always produce key frame for all streams.
51 video_stream_encoder_->SendKeyFrame();
mflodman15d83572016-10-06 08:35:11 -070052}
53
Niels Möllerfa89d842019-01-30 16:33:45 +010054void EncoderKeyFrameCallback::OnKeyFrameRequested(uint64_t channel_id) {
55 if (channel_id != ssrcs_[0]) {
56 RTC_LOG(LS_INFO) << "Key frame request on unknown channel id " << channel_id
57 << " expected " << ssrcs_[0];
58 return;
59 }
60
61 video_stream_encoder_->SendKeyFrame();
62}
63
mflodman15d83572016-10-06 08:35:11 -070064} // namespace webrtc