blob: 9e00078f96da075f11e886d6c980413aea78291d [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
brandtr1cfbd602016-12-08 04:17:53 -080034std::string FlexfecReceiveStream::Config::ToString() const {
Jonas Olsson0a713b62018-04-04 15:49:32 +020035 char buf[1024];
36 rtc::SimpleStringBuilder ss(buf);
brandtr1cfbd602016-12-08 04:17:53 -080037 ss << "{payload_type: " << payload_type;
Tommi1c1f5402021-06-14 10:54:20 +020038 ss << ", remote_ssrc: " << rtp.remote_ssrc;
39 ss << ", local_ssrc: " << rtp.local_ssrc;
brandtr1cfbd602016-12-08 04:17:53 -080040 ss << ", protected_media_ssrcs: [";
41 size_t i = 0;
42 for (; i + 1 < protected_media_ssrcs.size(); ++i)
43 ss << protected_media_ssrcs[i] << ", ";
44 if (!protected_media_ssrcs.empty())
45 ss << protected_media_ssrcs[i];
Tommi1c1f5402021-06-14 10:54:20 +020046 ss << "], transport_cc: " << (rtp.transport_cc ? "on" : "off");
47 ss << ", rtp.extensions: [";
brandtr1cfbd602016-12-08 04:17:53 -080048 i = 0;
Tommi1c1f5402021-06-14 10:54:20 +020049 for (; i + 1 < rtp.extensions.size(); ++i)
50 ss << rtp.extensions[i].ToString() << ", ";
51 if (!rtp.extensions.empty())
52 ss << rtp.extensions[i].ToString();
brandtr1cfbd602016-12-08 04:17:53 -080053 ss << "]}";
54 return ss.str();
55}
56
brandtr8313a6f2017-01-13 07:41:19 -080057bool FlexfecReceiveStream::Config::IsCompleteAndEnabled() const {
58 // Check if FlexFEC is enabled.
59 if (payload_type < 0)
60 return false;
61 // Do we have the necessary SSRC information?
Tommi1c1f5402021-06-14 10:54:20 +020062 if (rtp.remote_ssrc == 0)
brandtr8313a6f2017-01-13 07:41:19 -080063 return false;
64 // TODO(brandtr): Update this check when we support multistream protection.
65 if (protected_media_ssrcs.size() != 1u)
66 return false;
67 return true;
68}
69
brandtr76648da2016-10-20 04:54:48 -070070namespace {
71
72// TODO(brandtr): Update this function when we support multistream protection.
brandtr43c31e72016-11-15 05:26:45 -080073std::unique_ptr<FlexfecReceiver> MaybeCreateFlexfecReceiver(
Sebastian Jansson8026d602019-03-04 19:39:01 +010074 Clock* clock,
brandtr43c31e72016-11-15 05:26:45 -080075 const FlexfecReceiveStream::Config& config,
brandtrb29e6522016-12-21 06:37:18 -080076 RecoveredPacketReceiver* recovered_packet_receiver) {
brandtr1cfbd602016-12-08 04:17:53 -080077 if (config.payload_type < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010078 RTC_LOG(LS_WARNING)
79 << "Invalid FlexFEC payload type given. "
Jonas Olssonb2b20312020-01-14 12:11:31 +010080 "This FlexfecReceiveStream will therefore be useless.";
brandtr76648da2016-10-20 04:54:48 -070081 return nullptr;
brandtr43c31e72016-11-15 05:26:45 -080082 }
brandtr1cfbd602016-12-08 04:17:53 -080083 RTC_DCHECK_GE(config.payload_type, 0);
84 RTC_DCHECK_LE(config.payload_type, 127);
Tommi1c1f5402021-06-14 10:54:20 +020085 if (config.rtp.remote_ssrc == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010086 RTC_LOG(LS_WARNING)
87 << "Invalid FlexFEC SSRC given. "
Jonas Olssonb2b20312020-01-14 12:11:31 +010088 "This FlexfecReceiveStream will therefore be useless.";
brandtr43c31e72016-11-15 05:26:45 -080089 return nullptr;
90 }
91 if (config.protected_media_ssrcs.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010092 RTC_LOG(LS_WARNING)
93 << "No protected media SSRC supplied. "
Jonas Olssonb2b20312020-01-14 12:11:31 +010094 "This FlexfecReceiveStream will therefore be useless.";
brandtr43c31e72016-11-15 05:26:45 -080095 return nullptr;
96 }
97
98 if (config.protected_media_ssrcs.size() > 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010099 RTC_LOG(LS_WARNING)
brandtr76648da2016-10-20 04:54:48 -0700100 << "The supplied FlexfecConfig contained multiple protected "
101 "media streams, but our implementation currently only "
brandtr43c31e72016-11-15 05:26:45 -0800102 "supports protecting a single media stream. "
103 "To avoid confusion, disabling FlexFEC completely.";
104 return nullptr;
brandtr76648da2016-10-20 04:54:48 -0700105 }
brandtr43c31e72016-11-15 05:26:45 -0800106 RTC_DCHECK_EQ(1U, config.protected_media_ssrcs.size());
Sebastian Jansson8026d602019-03-04 19:39:01 +0100107 return std::unique_ptr<FlexfecReceiver>(new FlexfecReceiver(
Tommi1c1f5402021-06-14 10:54:20 +0200108 clock, config.rtp.remote_ssrc, config.protected_media_ssrcs[0],
Sebastian Jansson8026d602019-03-04 19:39:01 +0100109 recovered_packet_receiver));
brandtr76648da2016-10-20 04:54:48 -0700110}
111
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200112std::unique_ptr<ModuleRtpRtcpImpl2> CreateRtpRtcpModule(
Sebastian Jansson8026d602019-03-04 19:39:01 +0100113 Clock* clock,
brandtrfa5a3682017-01-17 01:33:54 -0800114 ReceiveStatistics* receive_statistics,
Erik Språng93f51892019-08-19 12:42:58 +0200115 const FlexfecReceiveStreamImpl::Config& config,
brandtrfa5a3682017-01-17 01:33:54 -0800116 RtcpRttStats* rtt_stats) {
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200117 RtpRtcpInterface::Configuration configuration;
brandtrfa5a3682017-01-17 01:33:54 -0800118 configuration.audio = false;
119 configuration.receiver_only = true;
Sebastian Jansson8026d602019-03-04 19:39:01 +0100120 configuration.clock = clock;
brandtrfa5a3682017-01-17 01:33:54 -0800121 configuration.receive_statistics = receive_statistics;
Erik Språng93f51892019-08-19 12:42:58 +0200122 configuration.outgoing_transport = config.rtcp_send_transport;
brandtrfa5a3682017-01-17 01:33:54 -0800123 configuration.rtt_stats = rtt_stats;
Tommi1c1f5402021-06-14 10:54:20 +0200124 configuration.local_media_ssrc = config.rtp.local_ssrc;
Niels Moller2accc7d2021-01-12 15:54:16 +0000125 return ModuleRtpRtcpImpl2::Create(configuration);
brandtrfa5a3682017-01-17 01:33:54 -0800126}
127
brandtr76648da2016-10-20 04:54:48 -0700128} // namespace
129
brandtr7250b392016-12-19 01:13:46 -0800130FlexfecReceiveStreamImpl::FlexfecReceiveStreamImpl(
Sebastian Jansson8026d602019-03-04 19:39:01 +0100131 Clock* clock,
Tommicf4ed152022-05-09 20:46:57 +0000132 Config config,
brandtrfa5a3682017-01-17 01:33:54 -0800133 RecoveredPacketReceiver* recovered_packet_receiver,
Markus Handelleb61b7f2021-06-22 10:46:48 +0200134 RtcpRttStats* rtt_stats)
Tommicf4ed152022-05-09 20:46:57 +0000135 : extension_map_(std::move(config.rtp.extensions)),
Tommifc2c24e2022-05-25 20:54:24 +0200136 remote_ssrc_(config.rtp.remote_ssrc),
137 transport_cc_(config.rtp.transport_cc),
Tommi43b15c32022-07-28 09:24:52 +0200138 payload_type_(config.payload_type),
Tommifc2c24e2022-05-25 20:54:24 +0200139 receiver_(
140 MaybeCreateFlexfecReceiver(clock, config, recovered_packet_receiver)),
Sebastian Jansson8026d602019-03-04 19:39:01 +0100141 rtp_receive_statistics_(ReceiveStatistics::Create(clock)),
142 rtp_rtcp_(CreateRtpRtcpModule(clock,
143 rtp_receive_statistics_.get(),
Tommifc2c24e2022-05-25 20:54:24 +0200144 config,
Markus Handelleb61b7f2021-06-22 10:46:48 +0200145 rtt_stats)) {
Tommifc2c24e2022-05-25 20:54:24 +0200146 RTC_LOG(LS_INFO) << "FlexfecReceiveStreamImpl: " << config.ToString();
Tommi43b15c32022-07-28 09:24:52 +0200147 RTC_DCHECK_GE(payload_type_, -1);
brandtrfa5a3682017-01-17 01:33:54 -0800148
Tommi90738dd2021-05-31 17:36:47 +0200149 packet_sequence_checker_.Detach();
Tommi0377bab2021-05-31 14:26:05 +0200150
brandtrfa5a3682017-01-17 01:33:54 -0800151 // RTCP reporting.
Tommifc2c24e2022-05-25 20:54:24 +0200152 rtp_rtcp_->SetRTCPStatus(config.rtcp_mode);
brandtr76648da2016-10-20 04:54:48 -0700153}
154
brandtr7250b392016-12-19 01:13:46 -0800155FlexfecReceiveStreamImpl::~FlexfecReceiveStreamImpl() {
Tommifc2c24e2022-05-25 20:54:24 +0200156 RTC_DLOG(LS_INFO) << "~FlexfecReceiveStreamImpl: ssrc: " << remote_ssrc_;
brandtr76648da2016-10-20 04:54:48 -0700157}
158
Tommi0377bab2021-05-31 14:26:05 +0200159void FlexfecReceiveStreamImpl::RegisterWithTransport(
160 RtpStreamReceiverControllerInterface* receiver_controller) {
Tommi90738dd2021-05-31 17:36:47 +0200161 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
Tommi0377bab2021-05-31 14:26:05 +0200162 RTC_DCHECK(!rtp_stream_receiver_);
163
164 if (!receiver_)
165 return;
166
167 // TODO(nisse): OnRtpPacket in this class delegates all real work to
168 // `receiver_`. So maybe we don't need to implement RtpPacketSinkInterface
169 // here at all, we'd then delete the OnRtpPacket method and instead register
170 // `receiver_` as the RtpPacketSinkInterface for this stream.
171 rtp_stream_receiver_ =
Tommicf4ed152022-05-09 20:46:57 +0000172 receiver_controller->CreateReceiver(remote_ssrc(), this);
Tommi0377bab2021-05-31 14:26:05 +0200173}
174
175void FlexfecReceiveStreamImpl::UnregisterFromTransport() {
Tommi90738dd2021-05-31 17:36:47 +0200176 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
Tommi0377bab2021-05-31 14:26:05 +0200177 rtp_stream_receiver_.reset();
178}
179
nisse5c29a7a2017-02-16 06:52:32 -0800180void FlexfecReceiveStreamImpl::OnRtpPacket(const RtpPacketReceived& packet) {
Tommi1f38a382021-09-08 10:44:50 +0200181 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
brandtr76648da2016-10-20 04:54:48 -0700182 if (!receiver_)
nisse5c29a7a2017-02-16 06:52:32 -0800183 return;
brandtrfa5a3682017-01-17 01:33:54 -0800184
nisse5c29a7a2017-02-16 06:52:32 -0800185 receiver_->OnRtpPacket(packet);
brandtrfa5a3682017-01-17 01:33:54 -0800186
Artem Titovea240272021-07-26 12:40:21 +0200187 // Do not report media packets in the RTCP RRs generated by `rtp_rtcp_`.
Tommicf4ed152022-05-09 20:46:57 +0000188 if (packet.Ssrc() == remote_ssrc()) {
Niels Möller1f3206c2018-09-14 08:26:32 +0200189 rtp_receive_statistics_->OnRtpPacket(packet);
brandtrfa5a3682017-01-17 01:33:54 -0800190 }
brandtr76648da2016-10-20 04:54:48 -0700191}
192
Tommi43b15c32022-07-28 09:24:52 +0200193void FlexfecReceiveStreamImpl::SetPayloadType(int payload_type) {
194 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
195 RTC_DCHECK_GE(payload_type, -1);
196 payload_type_ = payload_type;
197}
198
199int FlexfecReceiveStreamImpl::payload_type() const {
200 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
201 return payload_type_;
202}
203
Tommi1f38a382021-09-08 10:44:50 +0200204void FlexfecReceiveStreamImpl::SetRtpExtensions(
205 std::vector<RtpExtension> extensions) {
206 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
Tommicf4ed152022-05-09 20:46:57 +0000207 extension_map_.Reset(extensions);
Tommi1f38a382021-09-08 10:44:50 +0200208}
209
Tommicf4ed152022-05-09 20:46:57 +0000210RtpHeaderExtensionMap FlexfecReceiveStreamImpl::GetRtpExtensionMap() const {
Tommi6be3e782022-05-09 15:20:24 +0000211 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
Tommicf4ed152022-05-09 20:46:57 +0000212 return extension_map_;
Tommi6be3e782022-05-09 15:20:24 +0000213}
214
Tommi1331c182022-05-17 10:13:52 +0200215void FlexfecReceiveStreamImpl::SetLocalSsrc(uint32_t local_ssrc) {
216 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
Tommifc2c24e2022-05-25 20:54:24 +0200217 if (local_ssrc == rtp_rtcp_->local_media_ssrc())
Tommi1331c182022-05-17 10:13:52 +0200218 return;
219
Tommi1331c182022-05-17 10:13:52 +0200220 rtp_rtcp_->SetLocalSsrc(local_ssrc);
221}
222
brandtr76648da2016-10-20 04:54:48 -0700223} // namespace webrtc