blob: c73d9e98e59f55985821ef5be2b794f52b73aa26 [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
brandtr7250b392016-12-19 01:13:46 -080011#include "webrtc/call/flexfec_receive_stream_impl.h"
brandtr76648da2016-10-20 04:54:48 -070012
brandtrfa5a3682017-01-17 01:33:54 -080013#include <string>
brandtrb29e6522016-12-21 06:37:18 -080014
Henrik Kjellandera80c16a2017-07-01 16:48:15 +020015#include "webrtc/base/checks.h"
16#include "webrtc/base/location.h"
17#include "webrtc/base/logging.h"
nisse0f15f922017-06-21 01:05:22 -070018#include "webrtc/call/rtp_stream_receiver_controller_interface.h"
brandtrfa5a3682017-01-17 01:33:54 -080019#include "webrtc/modules/rtp_rtcp/include/flexfec_receiver.h"
20#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
21#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
22#include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
23#include "webrtc/modules/utility/include/process_thread.h"
24#include "webrtc/system_wrappers/include/clock.h"
brandtr76648da2016-10-20 04:54:48 -070025
26namespace webrtc {
27
28std::string FlexfecReceiveStream::Stats::ToString(int64_t time_ms) const {
29 std::stringstream ss;
30 ss << "FlexfecReceiveStream stats: " << time_ms
31 << ", {flexfec_bitrate_bps: " << flexfec_bitrate_bps << "}";
32 return ss.str();
33}
34
brandtr1cfbd602016-12-08 04:17:53 -080035std::string FlexfecReceiveStream::Config::ToString() const {
36 std::stringstream ss;
37 ss << "{payload_type: " << payload_type;
38 ss << ", remote_ssrc: " << remote_ssrc;
39 ss << ", local_ssrc: " << local_ssrc;
40 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];
46 ss << "], transport_cc: " << (transport_cc ? "on" : "off");
brandtrb29e6522016-12-21 06:37:18 -080047 ss << ", rtp_header_extensions: [";
brandtr1cfbd602016-12-08 04:17:53 -080048 i = 0;
brandtrb29e6522016-12-21 06:37:18 -080049 for (; i + 1 < rtp_header_extensions.size(); ++i)
50 ss << rtp_header_extensions[i].ToString() << ", ";
51 if (!rtp_header_extensions.empty())
52 ss << rtp_header_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?
62 if (remote_ssrc == 0)
63 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(
74 const FlexfecReceiveStream::Config& config,
brandtrb29e6522016-12-21 06:37:18 -080075 RecoveredPacketReceiver* recovered_packet_receiver) {
brandtr1cfbd602016-12-08 04:17:53 -080076 if (config.payload_type < 0) {
brandtr43c31e72016-11-15 05:26:45 -080077 LOG(LS_WARNING) << "Invalid FlexFEC payload type given. "
78 << "This FlexfecReceiveStream will therefore be useless.";
brandtr76648da2016-10-20 04:54:48 -070079 return nullptr;
brandtr43c31e72016-11-15 05:26:45 -080080 }
brandtr1cfbd602016-12-08 04:17:53 -080081 RTC_DCHECK_GE(config.payload_type, 0);
82 RTC_DCHECK_LE(config.payload_type, 127);
83 if (config.remote_ssrc == 0) {
brandtr43c31e72016-11-15 05:26:45 -080084 LOG(LS_WARNING) << "Invalid FlexFEC SSRC given. "
85 << "This FlexfecReceiveStream will therefore be useless.";
86 return nullptr;
87 }
88 if (config.protected_media_ssrcs.empty()) {
89 LOG(LS_WARNING) << "No protected media SSRC supplied. "
90 << "This FlexfecReceiveStream will therefore be useless.";
91 return nullptr;
92 }
93
94 if (config.protected_media_ssrcs.size() > 1) {
brandtr76648da2016-10-20 04:54:48 -070095 LOG(LS_WARNING)
96 << "The supplied FlexfecConfig contained multiple protected "
97 "media streams, but our implementation currently only "
brandtr43c31e72016-11-15 05:26:45 -080098 "supports protecting a single media stream. "
99 "To avoid confusion, disabling FlexFEC completely.";
100 return nullptr;
brandtr76648da2016-10-20 04:54:48 -0700101 }
brandtr43c31e72016-11-15 05:26:45 -0800102 RTC_DCHECK_EQ(1U, config.protected_media_ssrcs.size());
103 return std::unique_ptr<FlexfecReceiver>(
brandtr1cfbd602016-12-08 04:17:53 -0800104 new FlexfecReceiver(config.remote_ssrc, config.protected_media_ssrcs[0],
brandtrb29e6522016-12-21 06:37:18 -0800105 recovered_packet_receiver));
brandtr76648da2016-10-20 04:54:48 -0700106}
107
brandtrfa5a3682017-01-17 01:33:54 -0800108std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
109 ReceiveStatistics* receive_statistics,
110 Transport* rtcp_send_transport,
111 RtcpRttStats* rtt_stats) {
112 RtpRtcp::Configuration configuration;
113 configuration.audio = false;
114 configuration.receiver_only = true;
115 configuration.clock = Clock::GetRealTimeClock();
116 configuration.receive_statistics = receive_statistics;
117 configuration.outgoing_transport = rtcp_send_transport;
118 configuration.rtt_stats = rtt_stats;
119 std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration));
120 return rtp_rtcp;
121}
122
brandtr76648da2016-10-20 04:54:48 -0700123} // namespace
124
brandtr7250b392016-12-19 01:13:46 -0800125FlexfecReceiveStreamImpl::FlexfecReceiveStreamImpl(
nisse0f15f922017-06-21 01:05:22 -0700126 RtpStreamReceiverControllerInterface* receiver_controller,
brandtr446fcb62016-12-08 04:14:24 -0800127 const Config& config,
brandtrfa5a3682017-01-17 01:33:54 -0800128 RecoveredPacketReceiver* recovered_packet_receiver,
129 RtcpRttStats* rtt_stats,
130 ProcessThread* process_thread)
131 : config_(config),
132 started_(false),
133 receiver_(MaybeCreateFlexfecReceiver(config_, recovered_packet_receiver)),
134 rtp_receive_statistics_(
135 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
136 rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_.get(),
137 config_.rtcp_send_transport,
138 rtt_stats)),
139 process_thread_(process_thread) {
brandtr7250b392016-12-19 01:13:46 -0800140 LOG(LS_INFO) << "FlexfecReceiveStreamImpl: " << config_.ToString();
brandtrfa5a3682017-01-17 01:33:54 -0800141
142 // RTCP reporting.
brandtrfa5a3682017-01-17 01:33:54 -0800143 rtp_rtcp_->SetRTCPStatus(config_.rtcp_mode);
144 rtp_rtcp_->SetSSRC(config_.local_ssrc);
tommidea489f2017-03-03 03:20:24 -0800145 process_thread_->RegisterModule(rtp_rtcp_.get(), RTC_FROM_HERE);
nisse0f15f922017-06-21 01:05:22 -0700146
147 // Register with transport.
148 // TODO(nisse): OnRtpPacket in this class delegates all real work to
149 // |receiver_|. So maybe we don't need to implement RtpPacketSinkInterface
150 // here at all, we'd then delete the OnRtpPacket method and instead register
151 // |receiver_| as the RtpPacketSinkInterface for this stream.
152 // TODO(nisse): Passing |this| from the constructor to the RtpDemuxer, before
153 // the object is fully initialized, is risky. But it works in this case
154 // because locking in our caller, Call::CreateFlexfecReceiveStream, ensures
155 // that the demuxer doesn't call OnRtpPacket before this object is fully
156 // constructed. Registering |receiver_| instead of |this| would solve this
157 // problem too.
158 rtp_stream_receiver_ =
159 receiver_controller->CreateReceiver(config_.remote_ssrc, this);
160 for (uint32_t ssrc : config.protected_media_ssrcs)
161 receiver_controller->AddSink(ssrc, this);
brandtr76648da2016-10-20 04:54:48 -0700162}
163
brandtr7250b392016-12-19 01:13:46 -0800164FlexfecReceiveStreamImpl::~FlexfecReceiveStreamImpl() {
165 LOG(LS_INFO) << "~FlexfecReceiveStreamImpl: " << config_.ToString();
brandtr76648da2016-10-20 04:54:48 -0700166 Stop();
brandtrfa5a3682017-01-17 01:33:54 -0800167 process_thread_->DeRegisterModule(rtp_rtcp_.get());
brandtr76648da2016-10-20 04:54:48 -0700168}
169
nisse5c29a7a2017-02-16 06:52:32 -0800170void FlexfecReceiveStreamImpl::OnRtpPacket(const RtpPacketReceived& packet) {
brandtr76648da2016-10-20 04:54:48 -0700171 {
172 rtc::CritScope cs(&crit_);
173 if (!started_)
nisse5c29a7a2017-02-16 06:52:32 -0800174 return;
brandtr76648da2016-10-20 04:54:48 -0700175 }
brandtrfa5a3682017-01-17 01:33:54 -0800176
brandtr76648da2016-10-20 04:54:48 -0700177 if (!receiver_)
nisse5c29a7a2017-02-16 06:52:32 -0800178 return;
brandtrfa5a3682017-01-17 01:33:54 -0800179
nisse5c29a7a2017-02-16 06:52:32 -0800180 receiver_->OnRtpPacket(packet);
brandtrfa5a3682017-01-17 01:33:54 -0800181
182 // Do not report media packets in the RTCP RRs generated by |rtp_rtcp_|.
183 if (packet.Ssrc() == config_.remote_ssrc) {
184 RTPHeader header;
185 packet.GetHeader(&header);
186 // FlexFEC packets are never retransmitted.
187 const bool kNotRetransmitted = false;
188 rtp_receive_statistics_->IncomingPacket(header, packet.size(),
189 kNotRetransmitted);
190 }
brandtr76648da2016-10-20 04:54:48 -0700191}
192
brandtr7250b392016-12-19 01:13:46 -0800193void FlexfecReceiveStreamImpl::Start() {
brandtr76648da2016-10-20 04:54:48 -0700194 rtc::CritScope cs(&crit_);
195 started_ = true;
196}
197
brandtr7250b392016-12-19 01:13:46 -0800198void FlexfecReceiveStreamImpl::Stop() {
brandtr76648da2016-10-20 04:54:48 -0700199 rtc::CritScope cs(&crit_);
200 started_ = false;
201}
202
203// TODO(brandtr): Implement this member function when we have designed the
204// stats for FlexFEC.
brandtr7250b392016-12-19 01:13:46 -0800205FlexfecReceiveStreamImpl::Stats FlexfecReceiveStreamImpl::GetStats() const {
206 return FlexfecReceiveStream::Stats();
brandtr76648da2016-10-20 04:54:48 -0700207}
208
brandtr76648da2016-10-20 04:54:48 -0700209} // namespace webrtc