You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
6.2 KiB

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