mflodman | 15d8357 | 2016-10-06 08:35:11 -0700 | [diff] [blame] | 1 | /* |
| 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 Alon | 14d1c9d | 2019-04-08 14:16:17 +0200 | [diff] [blame] | 11 | #include "video/encoder_rtcp_feedback.h" |
mflodman | 15d8357 | 2016-10-06 08:35:11 -0700 | [diff] [blame] | 12 | |
Rasmus Brandt | 3dde450 | 2019-03-21 11:46:17 +0100 | [diff] [blame] | 13 | #include "absl/types/optional.h" |
Elad Alon | b6ef99b | 2019-04-10 16:37:07 +0200 | [diff] [blame] | 14 | #include "api/video_codecs/video_encoder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "rtc_base/checks.h" |
Rasmus Brandt | 3dde450 | 2019-03-21 11:46:17 +0100 | [diff] [blame] | 16 | #include "rtc_base/experiments/keyframe_interval_settings.h" |
mflodman | 15d8357 | 2016-10-06 08:35:11 -0700 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
Rasmus Brandt | 3dde450 | 2019-03-21 11:46:17 +0100 | [diff] [blame] | 20 | namespace { |
| 21 | constexpr int kMinKeyframeSendIntervalMs = 300; |
| 22 | } // namespace |
| 23 | |
Elad Alon | 14d1c9d | 2019-04-08 14:16:17 +0200 | [diff] [blame] | 24 | EncoderRtcpFeedback::EncoderRtcpFeedback(Clock* clock, |
| 25 | const std::vector<uint32_t>& ssrcs, |
| 26 | VideoStreamEncoderInterface* encoder) |
mflodman | 15d8357 | 2016-10-06 08:35:11 -0700 | [diff] [blame] | 27 | : clock_(clock), |
| 28 | ssrcs_(ssrcs), |
Elad Alon | b6ef99b | 2019-04-10 16:37:07 +0200 | [diff] [blame] | 29 | rtp_video_sender_(nullptr), |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 30 | video_stream_encoder_(encoder), |
Rasmus Brandt | 3dde450 | 2019-03-21 11:46:17 +0100 | [diff] [blame] | 31 | time_last_intra_request_ms_(-1), |
| 32 | min_keyframe_send_interval_ms_( |
| 33 | KeyframeIntervalSettings::ParseFromFieldTrials() |
| 34 | .MinKeyframeSendIntervalMs() |
| 35 | .value_or(kMinKeyframeSendIntervalMs)) { |
mflodman | 15d8357 | 2016-10-06 08:35:11 -0700 | [diff] [blame] | 36 | RTC_DCHECK(!ssrcs.empty()); |
| 37 | } |
| 38 | |
Elad Alon | b6ef99b | 2019-04-10 16:37:07 +0200 | [diff] [blame] | 39 | void 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 Alon | 14d1c9d | 2019-04-08 14:16:17 +0200 | [diff] [blame] | 46 | bool EncoderRtcpFeedback::HasSsrc(uint32_t ssrc) { |
mflodman | 15d8357 | 2016-10-06 08:35:11 -0700 | [diff] [blame] | 47 | for (uint32_t registered_ssrc : ssrcs_) { |
| 48 | if (registered_ssrc == ssrc) { |
| 49 | return true; |
| 50 | } |
| 51 | } |
| 52 | return false; |
| 53 | } |
| 54 | |
Elad Alon | 14d1c9d | 2019-04-08 14:16:17 +0200 | [diff] [blame] | 55 | void EncoderRtcpFeedback::OnReceivedIntraFrameRequest(uint32_t ssrc) { |
mflodman | 15d8357 | 2016-10-06 08:35:11 -0700 | [diff] [blame] | 56 | RTC_DCHECK(HasSsrc(ssrc)); |
mflodman | 15d8357 | 2016-10-06 08:35:11 -0700 | [diff] [blame] | 57 | { |
mflodman | 15d8357 | 2016-10-06 08:35:11 -0700 | [diff] [blame] | 58 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 59 | rtc::CritScope lock(&crit_); |
Rasmus Brandt | 3dde450 | 2019-03-21 11:46:17 +0100 | [diff] [blame] | 60 | if (time_last_intra_request_ms_ + min_keyframe_send_interval_ms_ > now_ms) { |
mflodman | 15d8357 | 2016-10-06 08:35:11 -0700 | [diff] [blame] | 61 | return; |
| 62 | } |
Magnus Flodman | 3c62afd | 2018-09-24 11:44:49 +0200 | [diff] [blame] | 63 | time_last_intra_request_ms_ = now_ms; |
mflodman | 15d8357 | 2016-10-06 08:35:11 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Niels Möller | 1c9aa1e | 2018-02-16 10:27:23 +0100 | [diff] [blame] | 66 | // Always produce key frame for all streams. |
| 67 | video_stream_encoder_->SendKeyFrame(); |
mflodman | 15d8357 | 2016-10-06 08:35:11 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Elad Alon | 14d1c9d | 2019-04-08 14:16:17 +0200 | [diff] [blame] | 70 | void EncoderRtcpFeedback::OnKeyFrameRequested(uint64_t channel_id) { |
Niels Möller | fa89d84 | 2019-01-30 16:33:45 +0100 | [diff] [blame] | 71 | 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 Alon | 0a8562e | 2019-04-09 11:55:13 +0200 | [diff] [blame] | 80 | void 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 Alon | b6ef99b | 2019-04-10 16:37:07 +0200 | [diff] [blame] | 85 | 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 Alon | 0a8562e | 2019-04-09 11:55:13 +0200 | [diff] [blame] | 155 | } |
| 156 | |
mflodman | 15d8357 | 2016-10-06 08:35:11 -0700 | [diff] [blame] | 157 | } // namespace webrtc |