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