mflodman | 15d8357 | 2016-10-06 15:35:11 | [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 12:16:17 | [diff] [blame] | 11 | #include "video/encoder_rtcp_feedback.h" |
mflodman | 15d8357 | 2016-10-06 15:35:11 | [diff] [blame] | 12 | |
Tommi | 28e9653 | 2021-06-03 09:52:15 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | #include <utility> |
| 15 | |
Rasmus Brandt | 3dde450 | 2019-03-21 10:46:17 | [diff] [blame] | 16 | #include "absl/types/optional.h" |
Danil Chapovalov | b57178b | 2024-05-02 14:48:16 | [diff] [blame] | 17 | #include "api/environment/environment.h" |
Elad Alon | b6ef99b | 2019-04-10 14:37:07 | [diff] [blame] | 18 | #include "api/video_codecs/video_encoder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 19 | #include "rtc_base/checks.h" |
Rasmus Brandt | 3dde450 | 2019-03-21 10:46:17 | [diff] [blame] | 20 | #include "rtc_base/experiments/keyframe_interval_settings.h" |
Danil Chapovalov | b57178b | 2024-05-02 14:48:16 | [diff] [blame] | 21 | #include "system_wrappers/include/clock.h" |
mflodman | 15d8357 | 2016-10-06 15:35:11 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
Rasmus Brandt | 3dde450 | 2019-03-21 10:46:17 | [diff] [blame] | 25 | namespace { |
| 26 | constexpr int kMinKeyframeSendIntervalMs = 300; |
| 27 | } // namespace |
| 28 | |
Tommi | 28e9653 | 2021-06-03 09:52:15 | [diff] [blame] | 29 | EncoderRtcpFeedback::EncoderRtcpFeedback( |
Danil Chapovalov | b57178b | 2024-05-02 14:48:16 | [diff] [blame] | 30 | const Environment& env, |
Philipp Hancke | 7c5f9cf | 2024-02-20 14:28:14 | [diff] [blame] | 31 | bool per_layer_keyframes, |
Tommi | 28e9653 | 2021-06-03 09:52:15 | [diff] [blame] | 32 | const std::vector<uint32_t>& ssrcs, |
| 33 | VideoStreamEncoderInterface* encoder, |
| 34 | std::function<std::vector<RtpSequenceNumberMap::Info>( |
| 35 | uint32_t ssrc, |
| 36 | const std::vector<uint16_t>& seq_nums)> get_packet_infos) |
Danil Chapovalov | b57178b | 2024-05-02 14:48:16 | [diff] [blame] | 37 | : env_(env), |
mflodman | 15d8357 | 2016-10-06 15:35:11 | [diff] [blame] | 38 | ssrcs_(ssrcs), |
Philipp Hancke | 7c5f9cf | 2024-02-20 14:28:14 | [diff] [blame] | 39 | per_layer_keyframes_(per_layer_keyframes), |
Tommi | 28e9653 | 2021-06-03 09:52:15 | [diff] [blame] | 40 | get_packet_infos_(std::move(get_packet_infos)), |
mflodman | cc3d442 | 2017-08-03 15:27:51 | [diff] [blame] | 41 | video_stream_encoder_(encoder), |
Philipp Hancke | 7c5f9cf | 2024-02-20 14:28:14 | [diff] [blame] | 42 | time_last_packet_delivery_queue_(per_layer_keyframes ? ssrcs.size() : 1, |
| 43 | Timestamp::Zero()), |
Tommi | 28e9653 | 2021-06-03 09:52:15 | [diff] [blame] | 44 | min_keyframe_send_interval_( |
Danil Chapovalov | b57178b | 2024-05-02 14:48:16 | [diff] [blame] | 45 | TimeDelta::Millis(KeyframeIntervalSettings(env_.field_trials()) |
Tommi | 28e9653 | 2021-06-03 09:52:15 | [diff] [blame] | 46 | .MinKeyframeSendIntervalMs() |
| 47 | .value_or(kMinKeyframeSendIntervalMs))) { |
mflodman | 15d8357 | 2016-10-06 15:35:11 | [diff] [blame] | 48 | RTC_DCHECK(!ssrcs.empty()); |
Tommi | 28e9653 | 2021-06-03 09:52:15 | [diff] [blame] | 49 | packet_delivery_queue_.Detach(); |
mflodman | 15d8357 | 2016-10-06 15:35:11 | [diff] [blame] | 50 | } |
| 51 | |
Tommi | 28e9653 | 2021-06-03 09:52:15 | [diff] [blame] | 52 | // Called via Call::DeliverRtcp. |
Elad Alon | 14d1c9d | 2019-04-08 12:16:17 | [diff] [blame] | 53 | void EncoderRtcpFeedback::OnReceivedIntraFrameRequest(uint32_t ssrc) { |
Tommi | 28e9653 | 2021-06-03 09:52:15 | [diff] [blame] | 54 | RTC_DCHECK_RUN_ON(&packet_delivery_queue_); |
| 55 | RTC_DCHECK(std::find(ssrcs_.begin(), ssrcs_.end(), ssrc) != ssrcs_.end()); |
| 56 | |
Philipp Hancke | 7c5f9cf | 2024-02-20 14:28:14 | [diff] [blame] | 57 | auto it = std::find(ssrcs_.begin(), ssrcs_.end(), ssrc); |
| 58 | if (it == ssrcs_.end()) { |
| 59 | RTC_LOG(LS_WARNING) << "SSRC " << ssrc << " not found."; |
| 60 | return; |
| 61 | } |
| 62 | size_t ssrc_index = |
| 63 | per_layer_keyframes_ ? std::distance(ssrcs_.begin(), it) : 0; |
| 64 | RTC_CHECK_LE(ssrc_index, time_last_packet_delivery_queue_.size()); |
Danil Chapovalov | b57178b | 2024-05-02 14:48:16 | [diff] [blame] | 65 | const Timestamp now = env_.clock().CurrentTime(); |
Philipp Hancke | 7c5f9cf | 2024-02-20 14:28:14 | [diff] [blame] | 66 | if (time_last_packet_delivery_queue_[ssrc_index] + |
| 67 | min_keyframe_send_interval_ > |
| 68 | now) |
Tommi | 28e9653 | 2021-06-03 09:52:15 | [diff] [blame] | 69 | return; |
| 70 | |
Philipp Hancke | 7c5f9cf | 2024-02-20 14:28:14 | [diff] [blame] | 71 | time_last_packet_delivery_queue_[ssrc_index] = now; |
mflodman | 15d8357 | 2016-10-06 15:35:11 | [diff] [blame] | 72 | |
Philipp Hancke | 7c5f9cf | 2024-02-20 14:28:14 | [diff] [blame] | 73 | std::vector<VideoFrameType> layers(ssrcs_.size(), |
| 74 | VideoFrameType::kVideoFrameDelta); |
| 75 | if (!per_layer_keyframes_) { |
| 76 | // Always produce key frame for all streams. |
| 77 | video_stream_encoder_->SendKeyFrame(); |
| 78 | } else { |
| 79 | // Determine on which layer we ask for key frames. |
| 80 | layers[ssrc_index] = VideoFrameType::kVideoFrameKey; |
| 81 | video_stream_encoder_->SendKeyFrame(layers); |
| 82 | } |
mflodman | 15d8357 | 2016-10-06 15:35:11 | [diff] [blame] | 83 | } |
| 84 | |
Elad Alon | 0a8562e | 2019-04-09 09:55:13 | [diff] [blame] | 85 | void EncoderRtcpFeedback::OnReceivedLossNotification( |
| 86 | uint32_t ssrc, |
| 87 | uint16_t seq_num_of_last_decodable, |
| 88 | uint16_t seq_num_of_last_received, |
| 89 | bool decodability_flag) { |
Tommi | 28e9653 | 2021-06-03 09:52:15 | [diff] [blame] | 90 | RTC_DCHECK(get_packet_infos_) << "Object initialization incomplete."; |
Elad Alon | b6ef99b | 2019-04-10 14:37:07 | [diff] [blame] | 91 | |
| 92 | const std::vector<uint16_t> seq_nums = {seq_num_of_last_decodable, |
| 93 | seq_num_of_last_received}; |
| 94 | const std::vector<RtpSequenceNumberMap::Info> infos = |
Tommi | 28e9653 | 2021-06-03 09:52:15 | [diff] [blame] | 95 | get_packet_infos_(ssrc, seq_nums); |
Elad Alon | b6ef99b | 2019-04-10 14:37:07 | [diff] [blame] | 96 | if (infos.empty()) { |
| 97 | return; |
| 98 | } |
| 99 | RTC_DCHECK_EQ(infos.size(), 2u); |
| 100 | |
| 101 | const RtpSequenceNumberMap::Info& last_decodable = infos[0]; |
| 102 | const RtpSequenceNumberMap::Info& last_received = infos[1]; |
| 103 | |
| 104 | VideoEncoder::LossNotification loss_notification; |
| 105 | loss_notification.timestamp_of_last_decodable = last_decodable.timestamp; |
| 106 | loss_notification.timestamp_of_last_received = last_received.timestamp; |
| 107 | |
| 108 | // Deduce decodability of the last received frame and of its dependencies. |
| 109 | if (last_received.is_first && last_received.is_last) { |
| 110 | // The frame consists of a single packet, and that packet has evidently |
| 111 | // been received in full; the frame is therefore assemblable. |
| 112 | // In this case, the decodability of the dependencies is communicated by |
| 113 | // the decodability flag, and the frame itself is decodable if and only |
| 114 | // if they are decodable. |
| 115 | loss_notification.dependencies_of_last_received_decodable = |
| 116 | decodability_flag; |
| 117 | loss_notification.last_received_decodable = decodability_flag; |
| 118 | } else if (last_received.is_first && !last_received.is_last) { |
| 119 | // In this case, the decodability flag communicates the decodability of |
| 120 | // the dependencies. If any is undecodable, we also know that the frame |
| 121 | // itself will not be decodable; if all are decodable, the frame's own |
| 122 | // decodability will remain unknown, as not all of its packets have |
| 123 | // been received. |
| 124 | loss_notification.dependencies_of_last_received_decodable = |
| 125 | decodability_flag; |
| 126 | loss_notification.last_received_decodable = |
| 127 | !decodability_flag ? absl::make_optional(false) : absl::nullopt; |
| 128 | } else if (!last_received.is_first && last_received.is_last) { |
| 129 | if (decodability_flag) { |
| 130 | // The frame has been received in full, and found to be decodable. |
| 131 | // (Messages of this type are not sent by WebRTC at the moment, but are |
| 132 | // theoretically possible, for example for serving as acks.) |
| 133 | loss_notification.dependencies_of_last_received_decodable = true; |
| 134 | loss_notification.last_received_decodable = true; |
| 135 | } else { |
| 136 | // It is impossible to tell whether some dependencies were undecodable, |
| 137 | // or whether the frame was unassemblable, but in either case, the frame |
| 138 | // itself was undecodable. |
| 139 | loss_notification.dependencies_of_last_received_decodable = absl::nullopt; |
| 140 | loss_notification.last_received_decodable = false; |
| 141 | } |
| 142 | } else { // !last_received.is_first && !last_received.is_last |
| 143 | if (decodability_flag) { |
| 144 | // The frame has not yet been received in full, but no gaps have |
| 145 | // been encountered so far, and the dependencies were all decodable. |
| 146 | // (Messages of this type are not sent by WebRTC at the moment, but are |
| 147 | // theoretically possible, for example for serving as acks.) |
| 148 | loss_notification.dependencies_of_last_received_decodable = true; |
| 149 | loss_notification.last_received_decodable = absl::nullopt; |
| 150 | } else { |
| 151 | // It is impossible to tell whether some dependencies were undecodable, |
| 152 | // or whether the frame was unassemblable, but in either case, the frame |
| 153 | // itself was undecodable. |
| 154 | loss_notification.dependencies_of_last_received_decodable = absl::nullopt; |
| 155 | loss_notification.last_received_decodable = false; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | video_stream_encoder_->OnLossNotification(loss_notification); |
Elad Alon | 0a8562e | 2019-04-09 09:55:13 | [diff] [blame] | 160 | } |
| 161 | |
mflodman | 15d8357 | 2016-10-06 15:35:11 | [diff] [blame] | 162 | } // namespace webrtc |