blob: 038c1f6a1b7903516470288cd4e8a50909d9652d [file] [log] [blame]
brandtr76648da2016-10-20 11:54:481/*
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 04:47:3111#include "call/flexfec_receive_stream_impl.h"
brandtr76648da2016-10-20 11:54:4812
brandtrfa5a3682017-01-17 09:33:5413#include <string>
brandtrb29e6522016-12-21 14:37:1814
Mirko Bonadei92ea95e2017-09-15 04:47:3115#include "call/rtp_stream_receiver_controller_interface.h"
16#include "modules/rtp_rtcp/include/flexfec_receiver.h"
17#include "modules/rtp_rtcp/include/receive_statistics.h"
18#include "modules/rtp_rtcp/include/rtp_rtcp.h"
19#include "modules/rtp_rtcp/source/rtp_packet_received.h"
20#include "modules/utility/include/process_thread.h"
21#include "rtc_base/checks.h"
22#include "rtc_base/location.h"
23#include "rtc_base/logging.h"
24#include "system_wrappers/include/clock.h"
brandtr76648da2016-10-20 11:54:4825
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 12:17:5335std::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 14:37:1847 ss << ", rtp_header_extensions: [";
brandtr1cfbd602016-12-08 12:17:5348 i = 0;
brandtrb29e6522016-12-21 14:37:1849 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 12:17:5353 ss << "]}";
54 return ss.str();
55}
56
brandtr8313a6f2017-01-13 15:41:1957bool 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 11:54:4870namespace {
71
72// TODO(brandtr): Update this function when we support multistream protection.
brandtr43c31e72016-11-15 13:26:4573std::unique_ptr<FlexfecReceiver> MaybeCreateFlexfecReceiver(
74 const FlexfecReceiveStream::Config& config,
brandtrb29e6522016-12-21 14:37:1875 RecoveredPacketReceiver* recovered_packet_receiver) {
brandtr1cfbd602016-12-08 12:17:5376 if (config.payload_type < 0) {
Mirko Bonadei675513b2017-11-09 10:09:2577 RTC_LOG(LS_WARNING)
78 << "Invalid FlexFEC payload type given. "
79 << "This FlexfecReceiveStream will therefore be useless.";
brandtr76648da2016-10-20 11:54:4880 return nullptr;
brandtr43c31e72016-11-15 13:26:4581 }
brandtr1cfbd602016-12-08 12:17:5382 RTC_DCHECK_GE(config.payload_type, 0);
83 RTC_DCHECK_LE(config.payload_type, 127);
84 if (config.remote_ssrc == 0) {
Mirko Bonadei675513b2017-11-09 10:09:2585 RTC_LOG(LS_WARNING)
86 << "Invalid FlexFEC SSRC given. "
87 << "This FlexfecReceiveStream will therefore be useless.";
brandtr43c31e72016-11-15 13:26:4588 return nullptr;
89 }
90 if (config.protected_media_ssrcs.empty()) {
Mirko Bonadei675513b2017-11-09 10:09:2591 RTC_LOG(LS_WARNING)
92 << "No protected media SSRC supplied. "
93 << "This FlexfecReceiveStream will therefore be useless.";
brandtr43c31e72016-11-15 13:26:4594 return nullptr;
95 }
96
97 if (config.protected_media_ssrcs.size() > 1) {
Mirko Bonadei675513b2017-11-09 10:09:2598 RTC_LOG(LS_WARNING)
brandtr76648da2016-10-20 11:54:4899 << "The supplied FlexfecConfig contained multiple protected "
100 "media streams, but our implementation currently only "
brandtr43c31e72016-11-15 13:26:45101 "supports protecting a single media stream. "
102 "To avoid confusion, disabling FlexFEC completely.";
103 return nullptr;
brandtr76648da2016-10-20 11:54:48104 }
brandtr43c31e72016-11-15 13:26:45105 RTC_DCHECK_EQ(1U, config.protected_media_ssrcs.size());
106 return std::unique_ptr<FlexfecReceiver>(
brandtr1cfbd602016-12-08 12:17:53107 new FlexfecReceiver(config.remote_ssrc, config.protected_media_ssrcs[0],
brandtrb29e6522016-12-21 14:37:18108 recovered_packet_receiver));
brandtr76648da2016-10-20 11:54:48109}
110
brandtrfa5a3682017-01-17 09:33:54111std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
112 ReceiveStatistics* receive_statistics,
113 Transport* rtcp_send_transport,
114 RtcpRttStats* rtt_stats) {
115 RtpRtcp::Configuration configuration;
116 configuration.audio = false;
117 configuration.receiver_only = true;
118 configuration.clock = Clock::GetRealTimeClock();
119 configuration.receive_statistics = receive_statistics;
120 configuration.outgoing_transport = rtcp_send_transport;
121 configuration.rtt_stats = rtt_stats;
122 std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration));
123 return rtp_rtcp;
124}
125
brandtr76648da2016-10-20 11:54:48126} // namespace
127
brandtr7250b392016-12-19 09:13:46128FlexfecReceiveStreamImpl::FlexfecReceiveStreamImpl(
nisse0f15f922017-06-21 08:05:22129 RtpStreamReceiverControllerInterface* receiver_controller,
brandtr446fcb62016-12-08 12:14:24130 const Config& config,
brandtrfa5a3682017-01-17 09:33:54131 RecoveredPacketReceiver* recovered_packet_receiver,
132 RtcpRttStats* rtt_stats,
133 ProcessThread* process_thread)
134 : config_(config),
brandtrfa5a3682017-01-17 09:33:54135 receiver_(MaybeCreateFlexfecReceiver(config_, recovered_packet_receiver)),
136 rtp_receive_statistics_(
137 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
138 rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_.get(),
139 config_.rtcp_send_transport,
140 rtt_stats)),
141 process_thread_(process_thread) {
Mirko Bonadei675513b2017-11-09 10:09:25142 RTC_LOG(LS_INFO) << "FlexfecReceiveStreamImpl: " << config_.ToString();
brandtrfa5a3682017-01-17 09:33:54143
144 // RTCP reporting.
brandtrfa5a3682017-01-17 09:33:54145 rtp_rtcp_->SetRTCPStatus(config_.rtcp_mode);
146 rtp_rtcp_->SetSSRC(config_.local_ssrc);
tommidea489f2017-03-03 11:20:24147 process_thread_->RegisterModule(rtp_rtcp_.get(), RTC_FROM_HERE);
nisse0f15f922017-06-21 08:05:22148
149 // Register with transport.
150 // TODO(nisse): OnRtpPacket in this class delegates all real work to
151 // |receiver_|. So maybe we don't need to implement RtpPacketSinkInterface
152 // here at all, we'd then delete the OnRtpPacket method and instead register
153 // |receiver_| as the RtpPacketSinkInterface for this stream.
154 // TODO(nisse): Passing |this| from the constructor to the RtpDemuxer, before
155 // the object is fully initialized, is risky. But it works in this case
156 // because locking in our caller, Call::CreateFlexfecReceiveStream, ensures
157 // that the demuxer doesn't call OnRtpPacket before this object is fully
158 // constructed. Registering |receiver_| instead of |this| would solve this
159 // problem too.
160 rtp_stream_receiver_ =
161 receiver_controller->CreateReceiver(config_.remote_ssrc, this);
brandtr76648da2016-10-20 11:54:48162}
163
brandtr7250b392016-12-19 09:13:46164FlexfecReceiveStreamImpl::~FlexfecReceiveStreamImpl() {
Mirko Bonadei675513b2017-11-09 10:09:25165 RTC_LOG(LS_INFO) << "~FlexfecReceiveStreamImpl: " << config_.ToString();
brandtrfa5a3682017-01-17 09:33:54166 process_thread_->DeRegisterModule(rtp_rtcp_.get());
brandtr76648da2016-10-20 11:54:48167}
168
nisse5c29a7a2017-02-16 14:52:32169void FlexfecReceiveStreamImpl::OnRtpPacket(const RtpPacketReceived& packet) {
brandtr76648da2016-10-20 11:54:48170 if (!receiver_)
nisse5c29a7a2017-02-16 14:52:32171 return;
brandtrfa5a3682017-01-17 09:33:54172
nisse5c29a7a2017-02-16 14:52:32173 receiver_->OnRtpPacket(packet);
brandtrfa5a3682017-01-17 09:33:54174
175 // Do not report media packets in the RTCP RRs generated by |rtp_rtcp_|.
176 if (packet.Ssrc() == config_.remote_ssrc) {
177 RTPHeader header;
178 packet.GetHeader(&header);
179 // FlexFEC packets are never retransmitted.
180 const bool kNotRetransmitted = false;
181 rtp_receive_statistics_->IncomingPacket(header, packet.size(),
182 kNotRetransmitted);
183 }
brandtr76648da2016-10-20 11:54:48184}
185
brandtr76648da2016-10-20 11:54:48186// TODO(brandtr): Implement this member function when we have designed the
187// stats for FlexFEC.
brandtr7250b392016-12-19 09:13:46188FlexfecReceiveStreamImpl::Stats FlexfecReceiveStreamImpl::GetStats() const {
189 return FlexfecReceiveStream::Stats();
brandtr76648da2016-10-20 11:54:48190}
191
eladalon42f44f92017-07-25 13:40:06192const FlexfecReceiveStream::Config& FlexfecReceiveStreamImpl::GetConfig()
193 const {
194 return config_;
195}
196
brandtr76648da2016-10-20 11:54:48197} // namespace webrtc