• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include "video/encoder_rtcp_feedback.h"
12 
13 #include "absl/types/optional.h"
14 #include "api/video_codecs/video_encoder.h"
15 #include "rtc_base/checks.h"
16 #include "rtc_base/experiments/keyframe_interval_settings.h"
17 
18 namespace webrtc {
19 
20 namespace {
21 constexpr int kMinKeyframeSendIntervalMs = 300;
22 }  // namespace
23 
EncoderRtcpFeedback(Clock * clock,const std::vector<uint32_t> & ssrcs,VideoStreamEncoderInterface * encoder)24 EncoderRtcpFeedback::EncoderRtcpFeedback(Clock* clock,
25                                          const std::vector<uint32_t>& ssrcs,
26                                          VideoStreamEncoderInterface* encoder)
27     : clock_(clock),
28       ssrcs_(ssrcs),
29       rtp_video_sender_(nullptr),
30       video_stream_encoder_(encoder),
31       time_last_intra_request_ms_(-1),
32       min_keyframe_send_interval_ms_(
33           KeyframeIntervalSettings::ParseFromFieldTrials()
34               .MinKeyframeSendIntervalMs()
35               .value_or(kMinKeyframeSendIntervalMs)) {
36   RTC_DCHECK(!ssrcs.empty());
37 }
38 
SetRtpVideoSender(const RtpVideoSenderInterface * rtp_video_sender)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 
HasSsrc(uint32_t ssrc)46 bool EncoderRtcpFeedback::HasSsrc(uint32_t ssrc) {
47   for (uint32_t registered_ssrc : ssrcs_) {
48     if (registered_ssrc == ssrc) {
49       return true;
50     }
51   }
52   return false;
53 }
54 
OnReceivedIntraFrameRequest(uint32_t ssrc)55 void EncoderRtcpFeedback::OnReceivedIntraFrameRequest(uint32_t ssrc) {
56   RTC_DCHECK(HasSsrc(ssrc));
57   {
58     int64_t now_ms = clock_->TimeInMilliseconds();
59     MutexLock lock(&mutex_);
60     if (time_last_intra_request_ms_ + min_keyframe_send_interval_ms_ > now_ms) {
61       return;
62     }
63     time_last_intra_request_ms_ = now_ms;
64   }
65 
66   // Always produce key frame for all streams.
67   video_stream_encoder_->SendKeyFrame();
68 }
69 
OnReceivedLossNotification(uint32_t ssrc,uint16_t seq_num_of_last_decodable,uint16_t seq_num_of_last_received,bool decodability_flag)70 void EncoderRtcpFeedback::OnReceivedLossNotification(
71     uint32_t ssrc,
72     uint16_t seq_num_of_last_decodable,
73     uint16_t seq_num_of_last_received,
74     bool decodability_flag) {
75   RTC_DCHECK(rtp_video_sender_) << "Object initialization incomplete.";
76 
77   const std::vector<uint16_t> seq_nums = {seq_num_of_last_decodable,
78                                           seq_num_of_last_received};
79   const std::vector<RtpSequenceNumberMap::Info> infos =
80       rtp_video_sender_->GetSentRtpPacketInfos(ssrc, seq_nums);
81   if (infos.empty()) {
82     return;
83   }
84   RTC_DCHECK_EQ(infos.size(), 2u);
85 
86   const RtpSequenceNumberMap::Info& last_decodable = infos[0];
87   const RtpSequenceNumberMap::Info& last_received = infos[1];
88 
89   VideoEncoder::LossNotification loss_notification;
90   loss_notification.timestamp_of_last_decodable = last_decodable.timestamp;
91   loss_notification.timestamp_of_last_received = last_received.timestamp;
92 
93   // Deduce decodability of the last received frame and of its dependencies.
94   if (last_received.is_first && last_received.is_last) {
95     // The frame consists of a single packet, and that packet has evidently
96     // been received in full; the frame is therefore assemblable.
97     // In this case, the decodability of the dependencies is communicated by
98     // the decodability flag, and the frame itself is decodable if and only
99     // if they are decodable.
100     loss_notification.dependencies_of_last_received_decodable =
101         decodability_flag;
102     loss_notification.last_received_decodable = decodability_flag;
103   } else if (last_received.is_first && !last_received.is_last) {
104     // In this case, the decodability flag communicates the decodability of
105     // the dependencies. If any is undecodable, we also know that the frame
106     // itself will not be decodable; if all are decodable, the frame's own
107     // decodability will remain unknown, as not all of its packets have
108     // been received.
109     loss_notification.dependencies_of_last_received_decodable =
110         decodability_flag;
111     loss_notification.last_received_decodable =
112         !decodability_flag ? absl::make_optional(false) : absl::nullopt;
113   } else if (!last_received.is_first && last_received.is_last) {
114     if (decodability_flag) {
115       // The frame has been received in full, and found to be decodable.
116       // (Messages of this type are not sent by WebRTC at the moment, but are
117       // theoretically possible, for example for serving as acks.)
118       loss_notification.dependencies_of_last_received_decodable = true;
119       loss_notification.last_received_decodable = true;
120     } else {
121       // It is impossible to tell whether some dependencies were undecodable,
122       // or whether the frame was unassemblable, but in either case, the frame
123       // itself was undecodable.
124       loss_notification.dependencies_of_last_received_decodable = absl::nullopt;
125       loss_notification.last_received_decodable = false;
126     }
127   } else {  // !last_received.is_first && !last_received.is_last
128     if (decodability_flag) {
129       // The frame has not yet been received in full, but no gaps have
130       // been encountered so far, and the dependencies were all 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 = absl::nullopt;
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   }
143 
144   video_stream_encoder_->OnLossNotification(loss_notification);
145 }
146 
147 }  // namespace webrtc
148