1 /*
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
11 #include "call/flexfec_receive_stream_impl.h"
12
13 #include <stddef.h>
14
15 #include <cstdint>
16 #include <string>
17 #include <utility>
18
19 #include "api/array_view.h"
20 #include "api/call/transport.h"
21 #include "api/rtp_parameters.h"
22 #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"
25 #include "modules/rtp_rtcp/source/rtp_packet_received.h"
26 #include "rtc_base/checks.h"
27 #include "rtc_base/logging.h"
28 #include "rtc_base/strings/string_builder.h"
29 #include "system_wrappers/include/clock.h"
30
31 namespace webrtc {
32
ToString() const33 std::string FlexfecReceiveStream::Config::ToString() const {
34 char buf[1024];
35 rtc::SimpleStringBuilder ss(buf);
36 ss << "{payload_type: " << payload_type;
37 ss << ", remote_ssrc: " << rtp.remote_ssrc;
38 ss << ", local_ssrc: " << rtp.local_ssrc;
39 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];
45 ss << "], transport_cc: " << (rtp.transport_cc ? "on" : "off");
46 ss << ", rtp.extensions: [";
47 i = 0;
48 for (; i + 1 < rtp.extensions.size(); ++i)
49 ss << rtp.extensions[i].ToString() << ", ";
50 if (!rtp.extensions.empty())
51 ss << rtp.extensions[i].ToString();
52 ss << "]}";
53 return ss.str();
54 }
55
IsCompleteAndEnabled() const56 bool FlexfecReceiveStream::Config::IsCompleteAndEnabled() const {
57 // Check if FlexFEC is enabled.
58 if (payload_type < 0)
59 return false;
60 // Do we have the necessary SSRC information?
61 if (rtp.remote_ssrc == 0)
62 return false;
63 // TODO(brandtr): Update this check when we support multistream protection.
64 if (protected_media_ssrcs.size() != 1u)
65 return false;
66 return true;
67 }
68
69 namespace {
70
71 // TODO(brandtr): Update this function when we support multistream protection.
MaybeCreateFlexfecReceiver(Clock * clock,const FlexfecReceiveStream::Config & config,RecoveredPacketReceiver * recovered_packet_receiver)72 std::unique_ptr<FlexfecReceiver> MaybeCreateFlexfecReceiver(
73 Clock* clock,
74 const FlexfecReceiveStream::Config& config,
75 RecoveredPacketReceiver* recovered_packet_receiver) {
76 if (config.payload_type < 0) {
77 RTC_LOG(LS_WARNING)
78 << "Invalid FlexFEC payload type given. "
79 "This FlexfecReceiveStream will therefore be useless.";
80 return nullptr;
81 }
82 RTC_DCHECK_GE(config.payload_type, 0);
83 RTC_DCHECK_LE(config.payload_type, 127);
84 if (config.rtp.remote_ssrc == 0) {
85 RTC_LOG(LS_WARNING)
86 << "Invalid FlexFEC SSRC given. "
87 "This FlexfecReceiveStream will therefore be useless.";
88 return nullptr;
89 }
90 if (config.protected_media_ssrcs.empty()) {
91 RTC_LOG(LS_WARNING)
92 << "No protected media SSRC supplied. "
93 "This FlexfecReceiveStream will therefore be useless.";
94 return nullptr;
95 }
96
97 if (config.protected_media_ssrcs.size() > 1) {
98 RTC_LOG(LS_WARNING)
99 << "The supplied FlexfecConfig contained multiple protected "
100 "media streams, but our implementation currently only "
101 "supports protecting a single media stream. "
102 "To avoid confusion, disabling FlexFEC completely.";
103 return nullptr;
104 }
105 RTC_DCHECK_EQ(1U, config.protected_media_ssrcs.size());
106 return std::unique_ptr<FlexfecReceiver>(new FlexfecReceiver(
107 clock, config.rtp.remote_ssrc, config.protected_media_ssrcs[0],
108 recovered_packet_receiver));
109 }
110
CreateRtpRtcpModule(Clock * clock,ReceiveStatistics * receive_statistics,const FlexfecReceiveStreamImpl::Config & config,RtcpRttStats * rtt_stats)111 std::unique_ptr<ModuleRtpRtcpImpl2> CreateRtpRtcpModule(
112 Clock* clock,
113 ReceiveStatistics* receive_statistics,
114 const FlexfecReceiveStreamImpl::Config& config,
115 RtcpRttStats* rtt_stats) {
116 RtpRtcpInterface::Configuration configuration;
117 configuration.audio = false;
118 configuration.receiver_only = true;
119 configuration.clock = clock;
120 configuration.receive_statistics = receive_statistics;
121 configuration.outgoing_transport = config.rtcp_send_transport;
122 configuration.rtt_stats = rtt_stats;
123 configuration.local_media_ssrc = config.rtp.local_ssrc;
124 return ModuleRtpRtcpImpl2::Create(configuration);
125 }
126
127 } // namespace
128
FlexfecReceiveStreamImpl(Clock * clock,Config config,RecoveredPacketReceiver * recovered_packet_receiver,RtcpRttStats * rtt_stats)129 FlexfecReceiveStreamImpl::FlexfecReceiveStreamImpl(
130 Clock* clock,
131 Config config,
132 RecoveredPacketReceiver* recovered_packet_receiver,
133 RtcpRttStats* rtt_stats)
134 : extension_map_(std::move(config.rtp.extensions)),
135 remote_ssrc_(config.rtp.remote_ssrc),
136 transport_cc_(config.rtp.transport_cc),
137 payload_type_(config.payload_type),
138 receiver_(
139 MaybeCreateFlexfecReceiver(clock, config, recovered_packet_receiver)),
140 rtp_receive_statistics_(ReceiveStatistics::Create(clock)),
141 rtp_rtcp_(CreateRtpRtcpModule(clock,
142 rtp_receive_statistics_.get(),
143 config,
144 rtt_stats)) {
145 RTC_LOG(LS_INFO) << "FlexfecReceiveStreamImpl: " << config.ToString();
146 RTC_DCHECK_GE(payload_type_, -1);
147
148 packet_sequence_checker_.Detach();
149
150 // RTCP reporting.
151 rtp_rtcp_->SetRTCPStatus(config.rtcp_mode);
152 }
153
~FlexfecReceiveStreamImpl()154 FlexfecReceiveStreamImpl::~FlexfecReceiveStreamImpl() {
155 RTC_DLOG(LS_INFO) << "~FlexfecReceiveStreamImpl: ssrc: " << remote_ssrc_;
156 }
157
RegisterWithTransport(RtpStreamReceiverControllerInterface * receiver_controller)158 void FlexfecReceiveStreamImpl::RegisterWithTransport(
159 RtpStreamReceiverControllerInterface* receiver_controller) {
160 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
161 RTC_DCHECK(!rtp_stream_receiver_);
162
163 if (!receiver_)
164 return;
165
166 // TODO(nisse): OnRtpPacket in this class delegates all real work to
167 // `receiver_`. So maybe we don't need to implement RtpPacketSinkInterface
168 // here at all, we'd then delete the OnRtpPacket method and instead register
169 // `receiver_` as the RtpPacketSinkInterface for this stream.
170 rtp_stream_receiver_ =
171 receiver_controller->CreateReceiver(remote_ssrc(), this);
172 }
173
UnregisterFromTransport()174 void FlexfecReceiveStreamImpl::UnregisterFromTransport() {
175 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
176 rtp_stream_receiver_.reset();
177 }
178
OnRtpPacket(const RtpPacketReceived & packet)179 void FlexfecReceiveStreamImpl::OnRtpPacket(const RtpPacketReceived& packet) {
180 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
181 if (!receiver_)
182 return;
183
184 receiver_->OnRtpPacket(packet);
185
186 // Do not report media packets in the RTCP RRs generated by `rtp_rtcp_`.
187 if (packet.Ssrc() == remote_ssrc()) {
188 rtp_receive_statistics_->OnRtpPacket(packet);
189 }
190 }
191
SetPayloadType(int payload_type)192 void FlexfecReceiveStreamImpl::SetPayloadType(int payload_type) {
193 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
194 RTC_DCHECK_GE(payload_type, -1);
195 payload_type_ = payload_type;
196 }
197
payload_type() const198 int FlexfecReceiveStreamImpl::payload_type() const {
199 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
200 return payload_type_;
201 }
202
SetRtpExtensions(std::vector<RtpExtension> extensions)203 void FlexfecReceiveStreamImpl::SetRtpExtensions(
204 std::vector<RtpExtension> extensions) {
205 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
206 extension_map_.Reset(extensions);
207 }
208
GetRtpExtensionMap() const209 RtpHeaderExtensionMap FlexfecReceiveStreamImpl::GetRtpExtensionMap() const {
210 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
211 return extension_map_;
212 }
213
SetLocalSsrc(uint32_t local_ssrc)214 void FlexfecReceiveStreamImpl::SetLocalSsrc(uint32_t local_ssrc) {
215 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
216 if (local_ssrc == rtp_rtcp_->local_media_ssrc())
217 return;
218
219 rtp_rtcp_->SetLocalSsrc(local_ssrc);
220 }
221
222 } // namespace webrtc
223