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