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.

206 lines
7.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2016 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 "call/flexfec_receive_stream_impl.h"
  11. #include <stddef.h>
  12. #include <cstdint>
  13. #include <string>
  14. #include <vector>
  15. #include "api/array_view.h"
  16. #include "api/call/transport.h"
  17. #include "api/rtp_parameters.h"
  18. #include "call/rtp_stream_receiver_controller_interface.h"
  19. #include "modules/rtp_rtcp/include/flexfec_receiver.h"
  20. #include "modules/rtp_rtcp/include/receive_statistics.h"
  21. #include "modules/rtp_rtcp/include/rtp_rtcp.h"
  22. #include "modules/rtp_rtcp/source/rtp_packet_received.h"
  23. #include "modules/utility/include/process_thread.h"
  24. #include "rtc_base/checks.h"
  25. #include "rtc_base/location.h"
  26. #include "rtc_base/logging.h"
  27. #include "rtc_base/strings/string_builder.h"
  28. #include "system_wrappers/include/clock.h"
  29. namespace webrtc {
  30. std::string FlexfecReceiveStream::Stats::ToString(int64_t time_ms) const {
  31. char buf[1024];
  32. rtc::SimpleStringBuilder ss(buf);
  33. ss << "FlexfecReceiveStream stats: " << time_ms
  34. << ", {flexfec_bitrate_bps: " << flexfec_bitrate_bps << "}";
  35. return ss.str();
  36. }
  37. std::string FlexfecReceiveStream::Config::ToString() const {
  38. char buf[1024];
  39. rtc::SimpleStringBuilder ss(buf);
  40. ss << "{payload_type: " << payload_type;
  41. ss << ", remote_ssrc: " << remote_ssrc;
  42. ss << ", local_ssrc: " << local_ssrc;
  43. ss << ", protected_media_ssrcs: [";
  44. size_t i = 0;
  45. for (; i + 1 < protected_media_ssrcs.size(); ++i)
  46. ss << protected_media_ssrcs[i] << ", ";
  47. if (!protected_media_ssrcs.empty())
  48. ss << protected_media_ssrcs[i];
  49. ss << "], transport_cc: " << (transport_cc ? "on" : "off");
  50. ss << ", rtp_header_extensions: [";
  51. i = 0;
  52. for (; i + 1 < rtp_header_extensions.size(); ++i)
  53. ss << rtp_header_extensions[i].ToString() << ", ";
  54. if (!rtp_header_extensions.empty())
  55. ss << rtp_header_extensions[i].ToString();
  56. ss << "]}";
  57. return ss.str();
  58. }
  59. bool FlexfecReceiveStream::Config::IsCompleteAndEnabled() const {
  60. // Check if FlexFEC is enabled.
  61. if (payload_type < 0)
  62. return false;
  63. // Do we have the necessary SSRC information?
  64. if (remote_ssrc == 0)
  65. return false;
  66. // TODO(brandtr): Update this check when we support multistream protection.
  67. if (protected_media_ssrcs.size() != 1u)
  68. return false;
  69. return true;
  70. }
  71. namespace {
  72. // TODO(brandtr): Update this function when we support multistream protection.
  73. std::unique_ptr<FlexfecReceiver> MaybeCreateFlexfecReceiver(
  74. Clock* clock,
  75. const FlexfecReceiveStream::Config& config,
  76. RecoveredPacketReceiver* recovered_packet_receiver) {
  77. if (config.payload_type < 0) {
  78. RTC_LOG(LS_WARNING)
  79. << "Invalid FlexFEC payload type given. "
  80. << "This FlexfecReceiveStream will therefore be useless.";
  81. return nullptr;
  82. }
  83. RTC_DCHECK_GE(config.payload_type, 0);
  84. RTC_DCHECK_LE(config.payload_type, 127);
  85. if (config.remote_ssrc == 0) {
  86. RTC_LOG(LS_WARNING)
  87. << "Invalid FlexFEC SSRC given. "
  88. << "This FlexfecReceiveStream will therefore be useless.";
  89. return nullptr;
  90. }
  91. if (config.protected_media_ssrcs.empty()) {
  92. RTC_LOG(LS_WARNING)
  93. << "No protected media SSRC supplied. "
  94. << "This FlexfecReceiveStream will therefore be useless.";
  95. return nullptr;
  96. }
  97. if (config.protected_media_ssrcs.size() > 1) {
  98. RTC_LOG(LS_WARNING)
  99. << "The supplied FlexfecConfig contained multiple protected "
  100. "media streams, but our implementation currently only "
  101. "supports protecting a single media stream. "
  102. "To avoid confusion, disabling FlexFEC completely.";
  103. return nullptr;
  104. }
  105. RTC_DCHECK_EQ(1U, config.protected_media_ssrcs.size());
  106. return std::unique_ptr<FlexfecReceiver>(new FlexfecReceiver(
  107. clock, config.remote_ssrc, config.protected_media_ssrcs[0],
  108. recovered_packet_receiver));
  109. }
  110. std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
  111. Clock* clock,
  112. ReceiveStatistics* receive_statistics,
  113. const FlexfecReceiveStreamImpl::Config& config,
  114. RtcpRttStats* rtt_stats) {
  115. RtpRtcp::Configuration configuration;
  116. configuration.audio = false;
  117. configuration.receiver_only = true;
  118. configuration.clock = clock;
  119. configuration.receive_statistics = receive_statistics;
  120. configuration.outgoing_transport = config.rtcp_send_transport;
  121. configuration.rtt_stats = rtt_stats;
  122. configuration.local_media_ssrc = config.local_ssrc;
  123. return RtpRtcp::Create(configuration);
  124. }
  125. } // namespace
  126. FlexfecReceiveStreamImpl::FlexfecReceiveStreamImpl(
  127. Clock* clock,
  128. RtpStreamReceiverControllerInterface* receiver_controller,
  129. const Config& config,
  130. RecoveredPacketReceiver* recovered_packet_receiver,
  131. RtcpRttStats* rtt_stats,
  132. ProcessThread* process_thread)
  133. : config_(config),
  134. receiver_(MaybeCreateFlexfecReceiver(clock,
  135. config_,
  136. recovered_packet_receiver)),
  137. rtp_receive_statistics_(ReceiveStatistics::Create(clock)),
  138. rtp_rtcp_(CreateRtpRtcpModule(clock,
  139. rtp_receive_statistics_.get(),
  140. config_,
  141. rtt_stats)),
  142. process_thread_(process_thread) {
  143. RTC_LOG(LS_INFO) << "FlexfecReceiveStreamImpl: " << config_.ToString();
  144. // RTCP reporting.
  145. rtp_rtcp_->SetRTCPStatus(config_.rtcp_mode);
  146. process_thread_->RegisterModule(rtp_rtcp_.get(), RTC_FROM_HERE);
  147. // Register with transport.
  148. // TODO(nisse): OnRtpPacket in this class delegates all real work to
  149. // |receiver_|. So maybe we don't need to implement RtpPacketSinkInterface
  150. // here at all, we'd then delete the OnRtpPacket method and instead register
  151. // |receiver_| as the RtpPacketSinkInterface for this stream.
  152. // TODO(nisse): Passing |this| from the constructor to the RtpDemuxer, before
  153. // the object is fully initialized, is risky. But it works in this case
  154. // because locking in our caller, Call::CreateFlexfecReceiveStream, ensures
  155. // that the demuxer doesn't call OnRtpPacket before this object is fully
  156. // constructed. Registering |receiver_| instead of |this| would solve this
  157. // problem too.
  158. rtp_stream_receiver_ =
  159. receiver_controller->CreateReceiver(config_.remote_ssrc, this);
  160. }
  161. FlexfecReceiveStreamImpl::~FlexfecReceiveStreamImpl() {
  162. RTC_LOG(LS_INFO) << "~FlexfecReceiveStreamImpl: " << config_.ToString();
  163. process_thread_->DeRegisterModule(rtp_rtcp_.get());
  164. }
  165. void FlexfecReceiveStreamImpl::OnRtpPacket(const RtpPacketReceived& packet) {
  166. if (!receiver_)
  167. return;
  168. receiver_->OnRtpPacket(packet);
  169. // Do not report media packets in the RTCP RRs generated by |rtp_rtcp_|.
  170. if (packet.Ssrc() == config_.remote_ssrc) {
  171. rtp_receive_statistics_->OnRtpPacket(packet);
  172. }
  173. }
  174. // TODO(brandtr): Implement this member function when we have designed the
  175. // stats for FlexFEC.
  176. FlexfecReceiveStreamImpl::Stats FlexfecReceiveStreamImpl::GetStats() const {
  177. return FlexfecReceiveStream::Stats();
  178. }
  179. const FlexfecReceiveStream::Config& FlexfecReceiveStreamImpl::GetConfig()
  180. const {
  181. return config_;
  182. }
  183. } // namespace webrtc