blob: 3b33be6b71a91bf36292d96fb9c7fb64b22ed39c [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>
14#include <cstdint>
brandtrfa5a3682017-01-17 01:33:54 -080015#include <string>
Yves Gerey3e707812018-11-28 16:47:49 +010016#include <vector>
brandtrb29e6522016-12-21 06:37:18 -080017
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "api/array_view.h"
19#include "api/call/transport.h"
20#include "api/rtpparameters.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#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 Olsson0a713b62018-04-04 15:49:32 +020030#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "system_wrappers/include/clock.h"
brandtr76648da2016-10-20 04:54:48 -070032
33namespace webrtc {
34
35std::string FlexfecReceiveStream::Stats::ToString(int64_t time_ms) const {
Jonas Olsson0a713b62018-04-04 15:49:32 +020036 char buf[1024];
37 rtc::SimpleStringBuilder ss(buf);
brandtr76648da2016-10-20 04:54:48 -070038 ss << "FlexfecReceiveStream stats: " << time_ms
39 << ", {flexfec_bitrate_bps: " << flexfec_bitrate_bps << "}";
40 return ss.str();
41}
42
brandtr1cfbd602016-12-08 04:17:53 -080043std::string FlexfecReceiveStream::Config::ToString() const {
Jonas Olsson0a713b62018-04-04 15:49:32 +020044 char buf[1024];
45 rtc::SimpleStringBuilder ss(buf);
brandtr1cfbd602016-12-08 04:17:53 -080046 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");
brandtrb29e6522016-12-21 06:37:18 -080056 ss << ", rtp_header_extensions: [";
brandtr1cfbd602016-12-08 04:17:53 -080057 i = 0;
brandtrb29e6522016-12-21 06:37:18 -080058 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();
brandtr1cfbd602016-12-08 04:17:53 -080062 ss << "]}";
63 return ss.str();
64}
65
brandtr8313a6f2017-01-13 07:41:19 -080066bool 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
brandtr76648da2016-10-20 04:54:48 -070079namespace {
80
81// TODO(brandtr): Update this function when we support multistream protection.
brandtr43c31e72016-11-15 05:26:45 -080082std::unique_ptr<FlexfecReceiver> MaybeCreateFlexfecReceiver(
83 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. "
88 << "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);
93 if (config.remote_ssrc == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010094 RTC_LOG(LS_WARNING)
95 << "Invalid FlexFEC SSRC given. "
96 << "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. "
102 << "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());
115 return std::unique_ptr<FlexfecReceiver>(
brandtr1cfbd602016-12-08 04:17:53 -0800116 new FlexfecReceiver(config.remote_ssrc, config.protected_media_ssrcs[0],
brandtrb29e6522016-12-21 06:37:18 -0800117 recovered_packet_receiver));
brandtr76648da2016-10-20 04:54:48 -0700118}
119
brandtrfa5a3682017-01-17 01:33:54 -0800120std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
121 ReceiveStatistics* receive_statistics,
122 Transport* rtcp_send_transport,
123 RtcpRttStats* rtt_stats) {
124 RtpRtcp::Configuration configuration;
125 configuration.audio = false;
126 configuration.receiver_only = true;
127 configuration.clock = Clock::GetRealTimeClock();
128 configuration.receive_statistics = receive_statistics;
129 configuration.outgoing_transport = rtcp_send_transport;
130 configuration.rtt_stats = rtt_stats;
131 std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration));
132 return rtp_rtcp;
133}
134
brandtr76648da2016-10-20 04:54:48 -0700135} // namespace
136
brandtr7250b392016-12-19 01:13:46 -0800137FlexfecReceiveStreamImpl::FlexfecReceiveStreamImpl(
nisse0f15f922017-06-21 01:05:22 -0700138 RtpStreamReceiverControllerInterface* receiver_controller,
brandtr446fcb62016-12-08 04:14:24 -0800139 const Config& config,
brandtrfa5a3682017-01-17 01:33:54 -0800140 RecoveredPacketReceiver* recovered_packet_receiver,
141 RtcpRttStats* rtt_stats,
142 ProcessThread* process_thread)
143 : config_(config),
brandtrfa5a3682017-01-17 01:33:54 -0800144 receiver_(MaybeCreateFlexfecReceiver(config_, recovered_packet_receiver)),
145 rtp_receive_statistics_(
146 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
147 rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_.get(),
148 config_.rtcp_send_transport,
149 rtt_stats)),
150 process_thread_(process_thread) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100151 RTC_LOG(LS_INFO) << "FlexfecReceiveStreamImpl: " << config_.ToString();
brandtrfa5a3682017-01-17 01:33:54 -0800152
153 // RTCP reporting.
brandtrfa5a3682017-01-17 01:33:54 -0800154 rtp_rtcp_->SetRTCPStatus(config_.rtcp_mode);
155 rtp_rtcp_->SetSSRC(config_.local_ssrc);
tommidea489f2017-03-03 03:20:24 -0800156 process_thread_->RegisterModule(rtp_rtcp_.get(), RTC_FROM_HERE);
nisse0f15f922017-06-21 01:05:22 -0700157
158 // Register with transport.
159 // TODO(nisse): OnRtpPacket in this class delegates all real work to
160 // |receiver_|. So maybe we don't need to implement RtpPacketSinkInterface
161 // here at all, we'd then delete the OnRtpPacket method and instead register
162 // |receiver_| as the RtpPacketSinkInterface for this stream.
163 // TODO(nisse): Passing |this| from the constructor to the RtpDemuxer, before
164 // the object is fully initialized, is risky. But it works in this case
165 // because locking in our caller, Call::CreateFlexfecReceiveStream, ensures
166 // that the demuxer doesn't call OnRtpPacket before this object is fully
167 // constructed. Registering |receiver_| instead of |this| would solve this
168 // problem too.
169 rtp_stream_receiver_ =
170 receiver_controller->CreateReceiver(config_.remote_ssrc, this);
brandtr76648da2016-10-20 04:54:48 -0700171}
172
brandtr7250b392016-12-19 01:13:46 -0800173FlexfecReceiveStreamImpl::~FlexfecReceiveStreamImpl() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100174 RTC_LOG(LS_INFO) << "~FlexfecReceiveStreamImpl: " << config_.ToString();
brandtrfa5a3682017-01-17 01:33:54 -0800175 process_thread_->DeRegisterModule(rtp_rtcp_.get());
brandtr76648da2016-10-20 04:54:48 -0700176}
177
nisse5c29a7a2017-02-16 06:52:32 -0800178void FlexfecReceiveStreamImpl::OnRtpPacket(const RtpPacketReceived& packet) {
brandtr76648da2016-10-20 04:54:48 -0700179 if (!receiver_)
nisse5c29a7a2017-02-16 06:52:32 -0800180 return;
brandtrfa5a3682017-01-17 01:33:54 -0800181
nisse5c29a7a2017-02-16 06:52:32 -0800182 receiver_->OnRtpPacket(packet);
brandtrfa5a3682017-01-17 01:33:54 -0800183
184 // Do not report media packets in the RTCP RRs generated by |rtp_rtcp_|.
185 if (packet.Ssrc() == config_.remote_ssrc) {
Niels Möller1f3206c2018-09-14 08:26:32 +0200186 rtp_receive_statistics_->OnRtpPacket(packet);
brandtrfa5a3682017-01-17 01:33:54 -0800187 }
brandtr76648da2016-10-20 04:54:48 -0700188}
189
brandtr76648da2016-10-20 04:54:48 -0700190// TODO(brandtr): Implement this member function when we have designed the
191// stats for FlexFEC.
brandtr7250b392016-12-19 01:13:46 -0800192FlexfecReceiveStreamImpl::Stats FlexfecReceiveStreamImpl::GetStats() const {
193 return FlexfecReceiveStream::Stats();
brandtr76648da2016-10-20 04:54:48 -0700194}
195
eladalon42f44f92017-07-25 06:40:06 -0700196const FlexfecReceiveStream::Config& FlexfecReceiveStreamImpl::GetConfig()
197 const {
198 return config_;
199}
200
brandtr76648da2016-10-20 04:54:48 -0700201} // namespace webrtc