blob: 6c8378d4b2d04c33dc88fdf0c2db71317a2cba00 [file] [log] [blame]
brandtr76648da2016-10-20 04:54:48 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "call/flexfec_receive_stream_impl.h"
brandtr76648da2016-10-20 04:54:48 -070012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Yves Gerey3e707812018-11-28 16:47:49 +010015#include <cstdint>
brandtrfa5a3682017-01-17 01:33:54 -080016#include <string>
Tommi1f38a382021-09-08 10:44:50 +020017#include <utility>
brandtrb29e6522016-12-21 06:37:18 -080018
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "api/array_view.h"
20#include "api/call/transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "api/rtp_parameters.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/checks.h"
27#include "rtc_base/location.h"
28#include "rtc_base/logging.h"
Jonas Olsson0a713b62018-04-04 15:49:32 +020029#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "system_wrappers/include/clock.h"
brandtr76648da2016-10-20 04:54:48 -070031
32namespace webrtc {
33
34std::string FlexfecReceiveStream::Stats::ToString(int64_t time_ms) const {
Jonas Olsson0a713b62018-04-04 15:49:32 +020035 char buf[1024];
36 rtc::SimpleStringBuilder ss(buf);
brandtr76648da2016-10-20 04:54:48 -070037 ss << "FlexfecReceiveStream stats: " << time_ms
38 << ", {flexfec_bitrate_bps: " << flexfec_bitrate_bps << "}";
39 return ss.str();
40}
41
brandtr1cfbd602016-12-08 04:17:53 -080042std::string FlexfecReceiveStream::Config::ToString() const {
Jonas Olsson0a713b62018-04-04 15:49:32 +020043 char buf[1024];
44 rtc::SimpleStringBuilder ss(buf);
brandtr1cfbd602016-12-08 04:17:53 -080045 ss << "{payload_type: " << payload_type;
Tommi1c1f5402021-06-14 10:54:20 +020046 ss << ", remote_ssrc: " << rtp.remote_ssrc;
47 ss << ", local_ssrc: " << rtp.local_ssrc;
brandtr1cfbd602016-12-08 04:17:53 -080048 ss << ", protected_media_ssrcs: [";
49 size_t i = 0;
50 for (; i + 1 < protected_media_ssrcs.size(); ++i)
51 ss << protected_media_ssrcs[i] << ", ";
52 if (!protected_media_ssrcs.empty())
53 ss << protected_media_ssrcs[i];
Tommi1c1f5402021-06-14 10:54:20 +020054 ss << "], transport_cc: " << (rtp.transport_cc ? "on" : "off");
55 ss << ", rtp.extensions: [";
brandtr1cfbd602016-12-08 04:17:53 -080056 i = 0;
Tommi1c1f5402021-06-14 10:54:20 +020057 for (; i + 1 < rtp.extensions.size(); ++i)
58 ss << rtp.extensions[i].ToString() << ", ";
59 if (!rtp.extensions.empty())
60 ss << rtp.extensions[i].ToString();
brandtr1cfbd602016-12-08 04:17:53 -080061 ss << "]}";
62 return ss.str();
63}
64
brandtr8313a6f2017-01-13 07:41:19 -080065bool FlexfecReceiveStream::Config::IsCompleteAndEnabled() const {
66 // Check if FlexFEC is enabled.
67 if (payload_type < 0)
68 return false;
69 // Do we have the necessary SSRC information?
Tommi1c1f5402021-06-14 10:54:20 +020070 if (rtp.remote_ssrc == 0)
brandtr8313a6f2017-01-13 07:41:19 -080071 return false;
72 // TODO(brandtr): Update this check when we support multistream protection.
73 if (protected_media_ssrcs.size() != 1u)
74 return false;
75 return true;
76}
77
brandtr76648da2016-10-20 04:54:48 -070078namespace {
79
80// TODO(brandtr): Update this function when we support multistream protection.
brandtr43c31e72016-11-15 05:26:45 -080081std::unique_ptr<FlexfecReceiver> MaybeCreateFlexfecReceiver(
Sebastian Jansson8026d602019-03-04 19:39:01 +010082 Clock* clock,
brandtr43c31e72016-11-15 05:26:45 -080083 const FlexfecReceiveStream::Config& config,
brandtrb29e6522016-12-21 06:37:18 -080084 RecoveredPacketReceiver* recovered_packet_receiver) {
brandtr1cfbd602016-12-08 04:17:53 -080085 if (config.payload_type < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010086 RTC_LOG(LS_WARNING)
87 << "Invalid FlexFEC payload type given. "
Jonas Olssonb2b20312020-01-14 12:11:31 +010088 "This FlexfecReceiveStream will therefore be useless.";
brandtr76648da2016-10-20 04:54:48 -070089 return nullptr;
brandtr43c31e72016-11-15 05:26:45 -080090 }
brandtr1cfbd602016-12-08 04:17:53 -080091 RTC_DCHECK_GE(config.payload_type, 0);
92 RTC_DCHECK_LE(config.payload_type, 127);
Tommi1c1f5402021-06-14 10:54:20 +020093 if (config.rtp.remote_ssrc == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010094 RTC_LOG(LS_WARNING)
95 << "Invalid FlexFEC SSRC given. "
Jonas Olssonb2b20312020-01-14 12:11:31 +010096 "This FlexfecReceiveStream will therefore be useless.";
brandtr43c31e72016-11-15 05:26:45 -080097 return nullptr;
98 }
99 if (config.protected_media_ssrcs.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100100 RTC_LOG(LS_WARNING)
101 << "No protected media SSRC supplied. "
Jonas Olssonb2b20312020-01-14 12:11:31 +0100102 "This FlexfecReceiveStream will therefore be useless.";
brandtr43c31e72016-11-15 05:26:45 -0800103 return nullptr;
104 }
105
106 if (config.protected_media_ssrcs.size() > 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100107 RTC_LOG(LS_WARNING)
brandtr76648da2016-10-20 04:54:48 -0700108 << "The supplied FlexfecConfig contained multiple protected "
109 "media streams, but our implementation currently only "
brandtr43c31e72016-11-15 05:26:45 -0800110 "supports protecting a single media stream. "
111 "To avoid confusion, disabling FlexFEC completely.";
112 return nullptr;
brandtr76648da2016-10-20 04:54:48 -0700113 }
brandtr43c31e72016-11-15 05:26:45 -0800114 RTC_DCHECK_EQ(1U, config.protected_media_ssrcs.size());
Sebastian Jansson8026d602019-03-04 19:39:01 +0100115 return std::unique_ptr<FlexfecReceiver>(new FlexfecReceiver(
Tommi1c1f5402021-06-14 10:54:20 +0200116 clock, config.rtp.remote_ssrc, config.protected_media_ssrcs[0],
Sebastian Jansson8026d602019-03-04 19:39:01 +0100117 recovered_packet_receiver));
brandtr76648da2016-10-20 04:54:48 -0700118}
119
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200120std::unique_ptr<ModuleRtpRtcpImpl2> CreateRtpRtcpModule(
Sebastian Jansson8026d602019-03-04 19:39:01 +0100121 Clock* clock,
brandtrfa5a3682017-01-17 01:33:54 -0800122 ReceiveStatistics* receive_statistics,
Erik Språng93f51892019-08-19 12:42:58 +0200123 const FlexfecReceiveStreamImpl::Config& config,
brandtrfa5a3682017-01-17 01:33:54 -0800124 RtcpRttStats* rtt_stats) {
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200125 RtpRtcpInterface::Configuration configuration;
brandtrfa5a3682017-01-17 01:33:54 -0800126 configuration.audio = false;
127 configuration.receiver_only = true;
Sebastian Jansson8026d602019-03-04 19:39:01 +0100128 configuration.clock = clock;
brandtrfa5a3682017-01-17 01:33:54 -0800129 configuration.receive_statistics = receive_statistics;
Erik Språng93f51892019-08-19 12:42:58 +0200130 configuration.outgoing_transport = config.rtcp_send_transport;
brandtrfa5a3682017-01-17 01:33:54 -0800131 configuration.rtt_stats = rtt_stats;
Tommi1c1f5402021-06-14 10:54:20 +0200132 configuration.local_media_ssrc = config.rtp.local_ssrc;
Niels Moller2accc7d2021-01-12 15:54:16 +0000133 return ModuleRtpRtcpImpl2::Create(configuration);
brandtrfa5a3682017-01-17 01:33:54 -0800134}
135
brandtr76648da2016-10-20 04:54:48 -0700136} // namespace
137
brandtr7250b392016-12-19 01:13:46 -0800138FlexfecReceiveStreamImpl::FlexfecReceiveStreamImpl(
Sebastian Jansson8026d602019-03-04 19:39:01 +0100139 Clock* clock,
brandtr446fcb62016-12-08 04:14:24 -0800140 const Config& config,
brandtrfa5a3682017-01-17 01:33:54 -0800141 RecoveredPacketReceiver* recovered_packet_receiver,
Markus Handelleb61b7f2021-06-22 10:46:48 +0200142 RtcpRttStats* rtt_stats)
brandtrfa5a3682017-01-17 01:33:54 -0800143 : config_(config),
Sebastian Jansson8026d602019-03-04 19:39:01 +0100144 receiver_(MaybeCreateFlexfecReceiver(clock,
145 config_,
146 recovered_packet_receiver)),
147 rtp_receive_statistics_(ReceiveStatistics::Create(clock)),
148 rtp_rtcp_(CreateRtpRtcpModule(clock,
149 rtp_receive_statistics_.get(),
Erik Språng93f51892019-08-19 12:42:58 +0200150 config_,
Markus Handelleb61b7f2021-06-22 10:46:48 +0200151 rtt_stats)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100152 RTC_LOG(LS_INFO) << "FlexfecReceiveStreamImpl: " << config_.ToString();
brandtrfa5a3682017-01-17 01:33:54 -0800153
Tommi90738dd2021-05-31 17:36:47 +0200154 packet_sequence_checker_.Detach();
Tommi0377bab2021-05-31 14:26:05 +0200155
brandtrfa5a3682017-01-17 01:33:54 -0800156 // RTCP reporting.
brandtrfa5a3682017-01-17 01:33:54 -0800157 rtp_rtcp_->SetRTCPStatus(config_.rtcp_mode);
brandtr76648da2016-10-20 04:54:48 -0700158}
159
brandtr7250b392016-12-19 01:13:46 -0800160FlexfecReceiveStreamImpl::~FlexfecReceiveStreamImpl() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100161 RTC_LOG(LS_INFO) << "~FlexfecReceiveStreamImpl: " << config_.ToString();
brandtr76648da2016-10-20 04:54:48 -0700162}
163
Tommi0377bab2021-05-31 14:26:05 +0200164void FlexfecReceiveStreamImpl::RegisterWithTransport(
165 RtpStreamReceiverControllerInterface* receiver_controller) {
Tommi90738dd2021-05-31 17:36:47 +0200166 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
Tommi0377bab2021-05-31 14:26:05 +0200167 RTC_DCHECK(!rtp_stream_receiver_);
168
169 if (!receiver_)
170 return;
171
172 // TODO(nisse): OnRtpPacket in this class delegates all real work to
173 // `receiver_`. So maybe we don't need to implement RtpPacketSinkInterface
174 // here at all, we'd then delete the OnRtpPacket method and instead register
175 // `receiver_` as the RtpPacketSinkInterface for this stream.
176 rtp_stream_receiver_ =
Tommi1c1f5402021-06-14 10:54:20 +0200177 receiver_controller->CreateReceiver(config_.rtp.remote_ssrc, this);
Tommi0377bab2021-05-31 14:26:05 +0200178}
179
180void FlexfecReceiveStreamImpl::UnregisterFromTransport() {
Tommi90738dd2021-05-31 17:36:47 +0200181 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
Tommi0377bab2021-05-31 14:26:05 +0200182 rtp_stream_receiver_.reset();
183}
184
nisse5c29a7a2017-02-16 06:52:32 -0800185void FlexfecReceiveStreamImpl::OnRtpPacket(const RtpPacketReceived& packet) {
Tommi1f38a382021-09-08 10:44:50 +0200186 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
brandtr76648da2016-10-20 04:54:48 -0700187 if (!receiver_)
nisse5c29a7a2017-02-16 06:52:32 -0800188 return;
brandtrfa5a3682017-01-17 01:33:54 -0800189
nisse5c29a7a2017-02-16 06:52:32 -0800190 receiver_->OnRtpPacket(packet);
brandtrfa5a3682017-01-17 01:33:54 -0800191
Artem Titovea240272021-07-26 12:40:21 +0200192 // Do not report media packets in the RTCP RRs generated by `rtp_rtcp_`.
Tommi1c1f5402021-06-14 10:54:20 +0200193 if (packet.Ssrc() == config_.rtp.remote_ssrc) {
Niels Möller1f3206c2018-09-14 08:26:32 +0200194 rtp_receive_statistics_->OnRtpPacket(packet);
brandtrfa5a3682017-01-17 01:33:54 -0800195 }
brandtr76648da2016-10-20 04:54:48 -0700196}
197
brandtr76648da2016-10-20 04:54:48 -0700198// TODO(brandtr): Implement this member function when we have designed the
199// stats for FlexFEC.
brandtr7250b392016-12-19 01:13:46 -0800200FlexfecReceiveStreamImpl::Stats FlexfecReceiveStreamImpl::GetStats() const {
201 return FlexfecReceiveStream::Stats();
brandtr76648da2016-10-20 04:54:48 -0700202}
203
Tommi1f38a382021-09-08 10:44:50 +0200204void FlexfecReceiveStreamImpl::SetRtpExtensions(
205 std::vector<RtpExtension> extensions) {
206 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
Tommicb7c7362022-05-09 14:49:37 +0000207 // TODO(tommi): Remove this cast once header extensions are managed outside
208 // of the config struct.
209 const_cast<std::vector<RtpExtension>&>(config_.rtp.extensions) =
210 std::move(extensions);
Tommi1f38a382021-09-08 10:44:50 +0200211}
212
brandtr76648da2016-10-20 04:54:48 -0700213} // namespace webrtc