blob: 19a8f64054cb2c799ca12f429f5be731b354b143 [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
Elad Alon14d1c9d2019-04-08 14:16:17 +020011#include "video/encoder_rtcp_feedback.h"
mflodman15d83572016-10-06 08:35:11 -070012
Rasmus Brandt3dde4502019-03-21 11:46:17 +010013#include "absl/types/optional.h"
Elad Alonb6ef99b2019-04-10 16:37:07 +020014#include "api/video_codecs/video_encoder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/checks.h"
Rasmus Brandt3dde4502019-03-21 11:46:17 +010016#include "rtc_base/experiments/keyframe_interval_settings.h"
mflodman15d83572016-10-06 08:35:11 -070017
18namespace webrtc {
19
Rasmus Brandt3dde4502019-03-21 11:46:17 +010020namespace {
21constexpr int kMinKeyframeSendIntervalMs = 300;
22} // namespace
23
Elad Alon14d1c9d2019-04-08 14:16:17 +020024EncoderRtcpFeedback::EncoderRtcpFeedback(Clock* clock,
25 const std::vector<uint32_t>& ssrcs,
26 VideoStreamEncoderInterface* encoder)
mflodman15d83572016-10-06 08:35:11 -070027 : clock_(clock),
28 ssrcs_(ssrcs),
Elad Alonb6ef99b2019-04-10 16:37:07 +020029 rtp_video_sender_(nullptr),
mflodmancc3d4422017-08-03 08:27:51 -070030 video_stream_encoder_(encoder),
Rasmus Brandt3dde4502019-03-21 11:46:17 +010031 time_last_intra_request_ms_(-1),
32 min_keyframe_send_interval_ms_(
33 KeyframeIntervalSettings::ParseFromFieldTrials()
34 .MinKeyframeSendIntervalMs()
35 .value_or(kMinKeyframeSendIntervalMs)) {
mflodman15d83572016-10-06 08:35:11 -070036 RTC_DCHECK(!ssrcs.empty());
37}
38
Elad Alonb6ef99b2019-04-10 16:37:07 +020039void EncoderRtcpFeedback::SetRtpVideoSender(
40 const RtpVideoSenderInterface* rtp_video_sender) {
41 RTC_DCHECK(rtp_video_sender);
42 RTC_DCHECK(!rtp_video_sender_);
43 rtp_video_sender_ = rtp_video_sender;
44}
45
Elad Alon14d1c9d2019-04-08 14:16:17 +020046bool EncoderRtcpFeedback::HasSsrc(uint32_t ssrc) {
mflodman15d83572016-10-06 08:35:11 -070047 for (uint32_t registered_ssrc : ssrcs_) {
48 if (registered_ssrc == ssrc) {
49 return true;
50 }
51 }
52 return false;
53}
54
Elad Alon14d1c9d2019-04-08 14:16:17 +020055void EncoderRtcpFeedback::OnReceivedIntraFrameRequest(uint32_t ssrc) {
mflodman15d83572016-10-06 08:35:11 -070056 RTC_DCHECK(HasSsrc(ssrc));
mflodman15d83572016-10-06 08:35:11 -070057 {
mflodman15d83572016-10-06 08:35:11 -070058 int64_t now_ms = clock_->TimeInMilliseconds();
59 rtc::CritScope lock(&crit_);
Rasmus Brandt3dde4502019-03-21 11:46:17 +010060 if (time_last_intra_request_ms_ + min_keyframe_send_interval_ms_ > now_ms) {
mflodman15d83572016-10-06 08:35:11 -070061 return;
62 }
Magnus Flodman3c62afd2018-09-24 11:44:49 +020063 time_last_intra_request_ms_ = now_ms;
mflodman15d83572016-10-06 08:35:11 -070064 }
65
Niels Möller1c9aa1e2018-02-16 10:27:23 +010066 // Always produce key frame for all streams.
67 video_stream_encoder_->SendKeyFrame();
mflodman15d83572016-10-06 08:35:11 -070068}
69
Elad Alon14d1c9d2019-04-08 14:16:17 +020070void EncoderRtcpFeedback::OnKeyFrameRequested(uint64_t channel_id) {
Niels Möllerfa89d842019-01-30 16:33:45 +010071 if (channel_id != ssrcs_[0]) {
72 RTC_LOG(LS_INFO) << "Key frame request on unknown channel id " << channel_id
73 << " expected " << ssrcs_[0];
74 return;
75 }
76
77 video_stream_encoder_->SendKeyFrame();
78}
79
Elad Alon0a8562e2019-04-09 11:55:13 +020080void EncoderRtcpFeedback::OnReceivedLossNotification(
81 uint32_t ssrc,
82 uint16_t seq_num_of_last_decodable,
83 uint16_t seq_num_of_last_received,
84 bool decodability_flag) {
Elad Alonb6ef99b2019-04-10 16:37:07 +020085 RTC_DCHECK(rtp_video_sender_) << "Object initialization incomplete.";
86
87 const std::vector<uint16_t> seq_nums = {seq_num_of_last_decodable,
88 seq_num_of_last_received};
89 const std::vector<RtpSequenceNumberMap::Info> infos =
90 rtp_video_sender_->GetSentRtpPacketInfos(ssrc, seq_nums);
91 if (infos.empty()) {
92 return;
93 }
94 RTC_DCHECK_EQ(infos.size(), 2u);
95
96 const RtpSequenceNumberMap::Info& last_decodable = infos[0];
97 const RtpSequenceNumberMap::Info& last_received = infos[1];
98
99 VideoEncoder::LossNotification loss_notification;
100 loss_notification.timestamp_of_last_decodable = last_decodable.timestamp;
101 loss_notification.timestamp_of_last_received = last_received.timestamp;
102
103 // Deduce decodability of the last received frame and of its dependencies.
104 if (last_received.is_first && last_received.is_last) {
105 // The frame consists of a single packet, and that packet has evidently
106 // been received in full; the frame is therefore assemblable.
107 // In this case, the decodability of the dependencies is communicated by
108 // the decodability flag, and the frame itself is decodable if and only
109 // if they are decodable.
110 loss_notification.dependencies_of_last_received_decodable =
111 decodability_flag;
112 loss_notification.last_received_decodable = decodability_flag;
113 } else if (last_received.is_first && !last_received.is_last) {
114 // In this case, the decodability flag communicates the decodability of
115 // the dependencies. If any is undecodable, we also know that the frame
116 // itself will not be decodable; if all are decodable, the frame's own
117 // decodability will remain unknown, as not all of its packets have
118 // been received.
119 loss_notification.dependencies_of_last_received_decodable =
120 decodability_flag;
121 loss_notification.last_received_decodable =
122 !decodability_flag ? absl::make_optional(false) : absl::nullopt;
123 } else if (!last_received.is_first && last_received.is_last) {
124 if (decodability_flag) {
125 // The frame has been received in full, and found to be decodable.
126 // (Messages of this type are not sent by WebRTC at the moment, but are
127 // theoretically possible, for example for serving as acks.)
128 loss_notification.dependencies_of_last_received_decodable = true;
129 loss_notification.last_received_decodable = true;
130 } else {
131 // It is impossible to tell whether some dependencies were undecodable,
132 // or whether the frame was unassemblable, but in either case, the frame
133 // itself was undecodable.
134 loss_notification.dependencies_of_last_received_decodable = absl::nullopt;
135 loss_notification.last_received_decodable = false;
136 }
137 } else { // !last_received.is_first && !last_received.is_last
138 if (decodability_flag) {
139 // The frame has not yet been received in full, but no gaps have
140 // been encountered so far, and the dependencies were all decodable.
141 // (Messages of this type are not sent by WebRTC at the moment, but are
142 // theoretically possible, for example for serving as acks.)
143 loss_notification.dependencies_of_last_received_decodable = true;
144 loss_notification.last_received_decodable = absl::nullopt;
145 } else {
146 // It is impossible to tell whether some dependencies were undecodable,
147 // or whether the frame was unassemblable, but in either case, the frame
148 // itself was undecodable.
149 loss_notification.dependencies_of_last_received_decodable = absl::nullopt;
150 loss_notification.last_received_decodable = false;
151 }
152 }
153
154 video_stream_encoder_->OnLossNotification(loss_notification);
Elad Alon0a8562e2019-04-09 11:55:13 +0200155}
156
mflodman15d83572016-10-06 08:35:11 -0700157} // namespace webrtc