blob: cbfbe55297ee7ed101e3918335fd5e00dbe9e9ec [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/logging.h"
Jonas Olsson0a713b62018-04-04 15:49:32 +020028#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "system_wrappers/include/clock.h"
brandtr76648da2016-10-20 04:54:48 -070030
31namespace webrtc {
32
brandtr1cfbd602016-12-08 04:17:53 -080033std::string FlexfecReceiveStream::Config::ToString() const {
Jonas Olsson0a713b62018-04-04 15:49:32 +020034 char buf[1024];
35 rtc::SimpleStringBuilder ss(buf);
brandtr1cfbd602016-12-08 04:17:53 -080036 ss << "{payload_type: " << payload_type;
Tommi1c1f5402021-06-14 10:54:20 +020037 ss << ", remote_ssrc: " << rtp.remote_ssrc;
38 ss << ", local_ssrc: " << rtp.local_ssrc;
brandtr1cfbd602016-12-08 04:17:53 -080039 ss << ", protected_media_ssrcs: [";
40 size_t i = 0;
41 for (; i + 1 < protected_media_ssrcs.size(); ++i)
42 ss << protected_media_ssrcs[i] << ", ";
43 if (!protected_media_ssrcs.empty())
44 ss << protected_media_ssrcs[i];
Per K217b3842023-01-30 16:04:01 +010045 ss << "}";
brandtr1cfbd602016-12-08 04:17:53 -080046 return ss.str();
47}
48
brandtr8313a6f2017-01-13 07:41:19 -080049bool FlexfecReceiveStream::Config::IsCompleteAndEnabled() const {
50 // Check if FlexFEC is enabled.
51 if (payload_type < 0)
52 return false;
53 // Do we have the necessary SSRC information?
Tommi1c1f5402021-06-14 10:54:20 +020054 if (rtp.remote_ssrc == 0)
brandtr8313a6f2017-01-13 07:41:19 -080055 return false;
56 // TODO(brandtr): Update this check when we support multistream protection.
57 if (protected_media_ssrcs.size() != 1u)
58 return false;
59 return true;
60}
61
brandtr76648da2016-10-20 04:54:48 -070062namespace {
63
64// TODO(brandtr): Update this function when we support multistream protection.
brandtr43c31e72016-11-15 05:26:45 -080065std::unique_ptr<FlexfecReceiver> MaybeCreateFlexfecReceiver(
Sebastian Jansson8026d602019-03-04 19:39:01 +010066 Clock* clock,
brandtr43c31e72016-11-15 05:26:45 -080067 const FlexfecReceiveStream::Config& config,
brandtrb29e6522016-12-21 06:37:18 -080068 RecoveredPacketReceiver* recovered_packet_receiver) {
brandtr1cfbd602016-12-08 04:17:53 -080069 if (config.payload_type < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010070 RTC_LOG(LS_WARNING)
71 << "Invalid FlexFEC payload type given. "
Jonas Olssonb2b20312020-01-14 12:11:31 +010072 "This FlexfecReceiveStream will therefore be useless.";
brandtr76648da2016-10-20 04:54:48 -070073 return nullptr;
brandtr43c31e72016-11-15 05:26:45 -080074 }
brandtr1cfbd602016-12-08 04:17:53 -080075 RTC_DCHECK_GE(config.payload_type, 0);
76 RTC_DCHECK_LE(config.payload_type, 127);
Tommi1c1f5402021-06-14 10:54:20 +020077 if (config.rtp.remote_ssrc == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010078 RTC_LOG(LS_WARNING)
79 << "Invalid FlexFEC SSRC given. "
Jonas Olssonb2b20312020-01-14 12:11:31 +010080 "This FlexfecReceiveStream will therefore be useless.";
brandtr43c31e72016-11-15 05:26:45 -080081 return nullptr;
82 }
83 if (config.protected_media_ssrcs.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010084 RTC_LOG(LS_WARNING)
85 << "No protected media SSRC supplied. "
Jonas Olssonb2b20312020-01-14 12:11:31 +010086 "This FlexfecReceiveStream will therefore be useless.";
brandtr43c31e72016-11-15 05:26:45 -080087 return nullptr;
88 }
89
90 if (config.protected_media_ssrcs.size() > 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010091 RTC_LOG(LS_WARNING)
brandtr76648da2016-10-20 04:54:48 -070092 << "The supplied FlexfecConfig contained multiple protected "
93 "media streams, but our implementation currently only "
brandtr43c31e72016-11-15 05:26:45 -080094 "supports protecting a single media stream. "
95 "To avoid confusion, disabling FlexFEC completely.";
96 return nullptr;
brandtr76648da2016-10-20 04:54:48 -070097 }
brandtr43c31e72016-11-15 05:26:45 -080098 RTC_DCHECK_EQ(1U, config.protected_media_ssrcs.size());
Sebastian Jansson8026d602019-03-04 19:39:01 +010099 return std::unique_ptr<FlexfecReceiver>(new FlexfecReceiver(
Tommi1c1f5402021-06-14 10:54:20 +0200100 clock, config.rtp.remote_ssrc, config.protected_media_ssrcs[0],
Sebastian Jansson8026d602019-03-04 19:39:01 +0100101 recovered_packet_receiver));
brandtr76648da2016-10-20 04:54:48 -0700102}
103
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200104std::unique_ptr<ModuleRtpRtcpImpl2> CreateRtpRtcpModule(
Sebastian Jansson8026d602019-03-04 19:39:01 +0100105 Clock* clock,
brandtrfa5a3682017-01-17 01:33:54 -0800106 ReceiveStatistics* receive_statistics,
Erik Språng93f51892019-08-19 12:42:58 +0200107 const FlexfecReceiveStreamImpl::Config& config,
brandtrfa5a3682017-01-17 01:33:54 -0800108 RtcpRttStats* rtt_stats) {
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200109 RtpRtcpInterface::Configuration configuration;
brandtrfa5a3682017-01-17 01:33:54 -0800110 configuration.audio = false;
111 configuration.receiver_only = true;
Sebastian Jansson8026d602019-03-04 19:39:01 +0100112 configuration.clock = clock;
brandtrfa5a3682017-01-17 01:33:54 -0800113 configuration.receive_statistics = receive_statistics;
Erik Språng93f51892019-08-19 12:42:58 +0200114 configuration.outgoing_transport = config.rtcp_send_transport;
brandtrfa5a3682017-01-17 01:33:54 -0800115 configuration.rtt_stats = rtt_stats;
Tommi1c1f5402021-06-14 10:54:20 +0200116 configuration.local_media_ssrc = config.rtp.local_ssrc;
Niels Moller2accc7d2021-01-12 15:54:16 +0000117 return ModuleRtpRtcpImpl2::Create(configuration);
brandtrfa5a3682017-01-17 01:33:54 -0800118}
119
brandtr76648da2016-10-20 04:54:48 -0700120} // namespace
121
brandtr7250b392016-12-19 01:13:46 -0800122FlexfecReceiveStreamImpl::FlexfecReceiveStreamImpl(
Sebastian Jansson8026d602019-03-04 19:39:01 +0100123 Clock* clock,
Tommicf4ed152022-05-09 20:46:57 +0000124 Config config,
brandtrfa5a3682017-01-17 01:33:54 -0800125 RecoveredPacketReceiver* recovered_packet_receiver,
Markus Handelleb61b7f2021-06-22 10:46:48 +0200126 RtcpRttStats* rtt_stats)
Per K217b3842023-01-30 16:04:01 +0100127 : remote_ssrc_(config.rtp.remote_ssrc),
Tommi43b15c32022-07-28 09:24:52 +0200128 payload_type_(config.payload_type),
Tommifc2c24e2022-05-25 20:54:24 +0200129 receiver_(
130 MaybeCreateFlexfecReceiver(clock, config, recovered_packet_receiver)),
Sebastian Jansson8026d602019-03-04 19:39:01 +0100131 rtp_receive_statistics_(ReceiveStatistics::Create(clock)),
132 rtp_rtcp_(CreateRtpRtcpModule(clock,
133 rtp_receive_statistics_.get(),
Tommifc2c24e2022-05-25 20:54:24 +0200134 config,
Markus Handelleb61b7f2021-06-22 10:46:48 +0200135 rtt_stats)) {
Tommifc2c24e2022-05-25 20:54:24 +0200136 RTC_LOG(LS_INFO) << "FlexfecReceiveStreamImpl: " << config.ToString();
Tommi43b15c32022-07-28 09:24:52 +0200137 RTC_DCHECK_GE(payload_type_, -1);
brandtrfa5a3682017-01-17 01:33:54 -0800138
Tommi90738dd2021-05-31 17:36:47 +0200139 packet_sequence_checker_.Detach();
Tommi0377bab2021-05-31 14:26:05 +0200140
brandtrfa5a3682017-01-17 01:33:54 -0800141 // RTCP reporting.
Tommifc2c24e2022-05-25 20:54:24 +0200142 rtp_rtcp_->SetRTCPStatus(config.rtcp_mode);
brandtr76648da2016-10-20 04:54:48 -0700143}
144
brandtr7250b392016-12-19 01:13:46 -0800145FlexfecReceiveStreamImpl::~FlexfecReceiveStreamImpl() {
Tommifc2c24e2022-05-25 20:54:24 +0200146 RTC_DLOG(LS_INFO) << "~FlexfecReceiveStreamImpl: ssrc: " << remote_ssrc_;
brandtr76648da2016-10-20 04:54:48 -0700147}
148
Tommi0377bab2021-05-31 14:26:05 +0200149void FlexfecReceiveStreamImpl::RegisterWithTransport(
150 RtpStreamReceiverControllerInterface* receiver_controller) {
Tommi90738dd2021-05-31 17:36:47 +0200151 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
Tommi0377bab2021-05-31 14:26:05 +0200152 RTC_DCHECK(!rtp_stream_receiver_);
153
154 if (!receiver_)
155 return;
156
157 // TODO(nisse): OnRtpPacket in this class delegates all real work to
158 // `receiver_`. So maybe we don't need to implement RtpPacketSinkInterface
159 // here at all, we'd then delete the OnRtpPacket method and instead register
160 // `receiver_` as the RtpPacketSinkInterface for this stream.
161 rtp_stream_receiver_ =
Tommicf4ed152022-05-09 20:46:57 +0000162 receiver_controller->CreateReceiver(remote_ssrc(), this);
Tommi0377bab2021-05-31 14:26:05 +0200163}
164
165void FlexfecReceiveStreamImpl::UnregisterFromTransport() {
Tommi90738dd2021-05-31 17:36:47 +0200166 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
Tommi0377bab2021-05-31 14:26:05 +0200167 rtp_stream_receiver_.reset();
168}
169
nisse5c29a7a2017-02-16 06:52:32 -0800170void FlexfecReceiveStreamImpl::OnRtpPacket(const RtpPacketReceived& packet) {
Tommi1f38a382021-09-08 10:44:50 +0200171 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
brandtr76648da2016-10-20 04:54:48 -0700172 if (!receiver_)
nisse5c29a7a2017-02-16 06:52:32 -0800173 return;
brandtrfa5a3682017-01-17 01:33:54 -0800174
nisse5c29a7a2017-02-16 06:52:32 -0800175 receiver_->OnRtpPacket(packet);
brandtrfa5a3682017-01-17 01:33:54 -0800176
Artem Titovea240272021-07-26 12:40:21 +0200177 // Do not report media packets in the RTCP RRs generated by `rtp_rtcp_`.
Tommicf4ed152022-05-09 20:46:57 +0000178 if (packet.Ssrc() == remote_ssrc()) {
Niels Möller1f3206c2018-09-14 08:26:32 +0200179 rtp_receive_statistics_->OnRtpPacket(packet);
brandtrfa5a3682017-01-17 01:33:54 -0800180 }
brandtr76648da2016-10-20 04:54:48 -0700181}
182
Tommi43b15c32022-07-28 09:24:52 +0200183void FlexfecReceiveStreamImpl::SetPayloadType(int payload_type) {
184 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
185 RTC_DCHECK_GE(payload_type, -1);
186 payload_type_ = payload_type;
187}
188
189int FlexfecReceiveStreamImpl::payload_type() const {
190 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
191 return payload_type_;
192}
193
Tommi1331c182022-05-17 10:13:52 +0200194void FlexfecReceiveStreamImpl::SetLocalSsrc(uint32_t local_ssrc) {
195 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
Tommifc2c24e2022-05-25 20:54:24 +0200196 if (local_ssrc == rtp_rtcp_->local_media_ssrc())
Tommi1331c182022-05-17 10:13:52 +0200197 return;
198
Tommi1331c182022-05-17 10:13:52 +0200199 rtp_rtcp_->SetLocalSsrc(local_ssrc);
200}
201
brandtr76648da2016-10-20 04:54:48 -0700202} // namespace webrtc