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