blob: ebba41e8074effb4b6dd79b340e335fa4e962c7c [file] [log] [blame]
mflodman15d83572016-10-06 15:35:111/*
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 12:16:1711#include "video/encoder_rtcp_feedback.h"
mflodman15d83572016-10-06 15:35:1112
Tommi28e96532021-06-03 09:52:1513#include <algorithm>
14#include <utility>
15
Rasmus Brandt3dde4502019-03-21 10:46:1716#include "absl/types/optional.h"
Elad Alonb6ef99b2019-04-10 14:37:0717#include "api/video_codecs/video_encoder.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "rtc_base/checks.h"
Rasmus Brandt3dde4502019-03-21 10:46:1719#include "rtc_base/experiments/keyframe_interval_settings.h"
mflodman15d83572016-10-06 15:35:1120
21namespace webrtc {
22
Rasmus Brandt3dde4502019-03-21 10:46:1723namespace {
24constexpr int kMinKeyframeSendIntervalMs = 300;
25} // namespace
26
Tommi28e96532021-06-03 09:52:1527EncoderRtcpFeedback::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)
mflodman15d83572016-10-06 15:35:1134 : clock_(clock),
35 ssrcs_(ssrcs),
Tommi28e96532021-06-03 09:52:1536 get_packet_infos_(std::move(get_packet_infos)),
mflodmancc3d4422017-08-03 15:27:5137 video_stream_encoder_(encoder),
Philipp Hanckea204ad22022-07-08 16:43:2538 time_last_packet_delivery_queue_(Timestamp::Zero()),
Tommi28e96532021-06-03 09:52:1539 min_keyframe_send_interval_(
40 TimeDelta::Millis(KeyframeIntervalSettings::ParseFromFieldTrials()
41 .MinKeyframeSendIntervalMs()
42 .value_or(kMinKeyframeSendIntervalMs))) {
mflodman15d83572016-10-06 15:35:1143 RTC_DCHECK(!ssrcs.empty());
Tommi28e96532021-06-03 09:52:1544 packet_delivery_queue_.Detach();
mflodman15d83572016-10-06 15:35:1145}
46
Tommi28e96532021-06-03 09:52:1547// Called via Call::DeliverRtcp.
Elad Alon14d1c9d2019-04-08 12:16:1748void EncoderRtcpFeedback::OnReceivedIntraFrameRequest(uint32_t ssrc) {
Tommi28e96532021-06-03 09:52:1549 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;
mflodman15d83572016-10-06 15:35:1157
Niels Möller1c9aa1e2018-02-16 09:27:2358 // Always produce key frame for all streams.
59 video_stream_encoder_->SendKeyFrame();
mflodman15d83572016-10-06 15:35:1160}
61
Elad Alon0a8562e2019-04-09 09:55:1362void 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) {
Tommi28e96532021-06-03 09:52:1567 RTC_DCHECK(get_packet_infos_) << "Object initialization incomplete.";
Elad Alonb6ef99b2019-04-10 14:37:0768
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 =
Tommi28e96532021-06-03 09:52:1572 get_packet_infos_(ssrc, seq_nums);
Elad Alonb6ef99b2019-04-10 14:37:0773 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 Alon0a8562e2019-04-09 09:55:13137}
138
mflodman15d83572016-10-06 15:35:11139} // namespace webrtc