Line data Source code
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 "webrtc/call/flexfec_receive_stream_impl.h"
12 :
13 : #include <string>
14 :
15 : #include "webrtc/base/checks.h"
16 : #include "webrtc/base/logging.h"
17 : #include "webrtc/modules/rtp_rtcp/include/flexfec_receiver.h"
18 : #include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
19 : #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
20 : #include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
21 : #include "webrtc/modules/utility/include/process_thread.h"
22 : #include "webrtc/system_wrappers/include/clock.h"
23 :
24 : namespace webrtc {
25 :
26 0 : std::string FlexfecReceiveStream::Stats::ToString(int64_t time_ms) const {
27 0 : std::stringstream ss;
28 0 : ss << "FlexfecReceiveStream stats: " << time_ms
29 0 : << ", {flexfec_bitrate_bps: " << flexfec_bitrate_bps << "}";
30 0 : return ss.str();
31 : }
32 :
33 0 : std::string FlexfecReceiveStream::Config::ToString() const {
34 0 : std::stringstream ss;
35 0 : ss << "{payload_type: " << payload_type;
36 0 : ss << ", remote_ssrc: " << remote_ssrc;
37 0 : ss << ", local_ssrc: " << local_ssrc;
38 0 : ss << ", protected_media_ssrcs: [";
39 0 : size_t i = 0;
40 0 : for (; i + 1 < protected_media_ssrcs.size(); ++i)
41 0 : ss << protected_media_ssrcs[i] << ", ";
42 0 : if (!protected_media_ssrcs.empty())
43 0 : ss << protected_media_ssrcs[i];
44 0 : ss << "], transport_cc: " << (transport_cc ? "on" : "off");
45 0 : ss << ", rtp_header_extensions: [";
46 0 : i = 0;
47 0 : for (; i + 1 < rtp_header_extensions.size(); ++i)
48 0 : ss << rtp_header_extensions[i].ToString() << ", ";
49 0 : if (!rtp_header_extensions.empty())
50 0 : ss << rtp_header_extensions[i].ToString();
51 0 : ss << "]}";
52 0 : return ss.str();
53 : }
54 :
55 0 : bool FlexfecReceiveStream::Config::IsCompleteAndEnabled() const {
56 : // Check if FlexFEC is enabled.
57 0 : if (payload_type < 0)
58 0 : return false;
59 : // Do we have the necessary SSRC information?
60 0 : if (remote_ssrc == 0)
61 0 : return false;
62 : // TODO(brandtr): Update this check when we support multistream protection.
63 0 : if (protected_media_ssrcs.size() != 1u)
64 0 : return false;
65 0 : return true;
66 : }
67 :
68 : namespace {
69 :
70 : // TODO(brandtr): Update this function when we support multistream protection.
71 0 : std::unique_ptr<FlexfecReceiver> MaybeCreateFlexfecReceiver(
72 : const FlexfecReceiveStream::Config& config,
73 : RecoveredPacketReceiver* recovered_packet_receiver) {
74 0 : if (config.payload_type < 0) {
75 0 : LOG(LS_WARNING) << "Invalid FlexFEC payload type given. "
76 0 : << "This FlexfecReceiveStream will therefore be useless.";
77 0 : return nullptr;
78 : }
79 0 : RTC_DCHECK_GE(config.payload_type, 0);
80 0 : RTC_DCHECK_LE(config.payload_type, 127);
81 0 : if (config.remote_ssrc == 0) {
82 0 : LOG(LS_WARNING) << "Invalid FlexFEC SSRC given. "
83 0 : << "This FlexfecReceiveStream will therefore be useless.";
84 0 : return nullptr;
85 : }
86 0 : if (config.protected_media_ssrcs.empty()) {
87 0 : LOG(LS_WARNING) << "No protected media SSRC supplied. "
88 0 : << "This FlexfecReceiveStream will therefore be useless.";
89 0 : return nullptr;
90 : }
91 :
92 0 : if (config.protected_media_ssrcs.size() > 1) {
93 0 : LOG(LS_WARNING)
94 : << "The supplied FlexfecConfig contained multiple protected "
95 : "media streams, but our implementation currently only "
96 : "supports protecting a single media stream. "
97 0 : "To avoid confusion, disabling FlexFEC completely.";
98 0 : return nullptr;
99 : }
100 0 : RTC_DCHECK_EQ(1U, config.protected_media_ssrcs.size());
101 : return std::unique_ptr<FlexfecReceiver>(
102 0 : new FlexfecReceiver(config.remote_ssrc, config.protected_media_ssrcs[0],
103 0 : recovered_packet_receiver));
104 : }
105 :
106 0 : std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
107 : ReceiveStatistics* receive_statistics,
108 : Transport* rtcp_send_transport,
109 : RtcpRttStats* rtt_stats) {
110 0 : RtpRtcp::Configuration configuration;
111 0 : configuration.audio = false;
112 0 : configuration.receiver_only = true;
113 0 : configuration.clock = Clock::GetRealTimeClock();
114 0 : configuration.receive_statistics = receive_statistics;
115 0 : configuration.outgoing_transport = rtcp_send_transport;
116 0 : configuration.rtt_stats = rtt_stats;
117 0 : std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration));
118 0 : return rtp_rtcp;
119 : }
120 :
121 : } // namespace
122 :
123 0 : FlexfecReceiveStreamImpl::FlexfecReceiveStreamImpl(
124 : const Config& config,
125 : RecoveredPacketReceiver* recovered_packet_receiver,
126 : RtcpRttStats* rtt_stats,
127 0 : ProcessThread* process_thread)
128 : : config_(config),
129 : started_(false),
130 : receiver_(MaybeCreateFlexfecReceiver(config_, recovered_packet_receiver)),
131 : rtp_receive_statistics_(
132 : ReceiveStatistics::Create(Clock::GetRealTimeClock())),
133 : rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_.get(),
134 0 : config_.rtcp_send_transport,
135 : rtt_stats)),
136 0 : process_thread_(process_thread) {
137 0 : LOG(LS_INFO) << "FlexfecReceiveStreamImpl: " << config_.ToString();
138 :
139 : // RTCP reporting.
140 0 : rtp_rtcp_->SetSendingMediaStatus(false);
141 0 : rtp_rtcp_->SetRTCPStatus(config_.rtcp_mode);
142 0 : rtp_rtcp_->SetSSRC(config_.local_ssrc);
143 0 : process_thread_->RegisterModule(rtp_rtcp_.get());
144 0 : }
145 :
146 0 : FlexfecReceiveStreamImpl::~FlexfecReceiveStreamImpl() {
147 0 : LOG(LS_INFO) << "~FlexfecReceiveStreamImpl: " << config_.ToString();
148 0 : Stop();
149 0 : process_thread_->DeRegisterModule(rtp_rtcp_.get());
150 0 : }
151 :
152 0 : bool FlexfecReceiveStreamImpl::AddAndProcessReceivedPacket(
153 : const RtpPacketReceived& packet) {
154 : {
155 0 : rtc::CritScope cs(&crit_);
156 0 : if (!started_)
157 0 : return false;
158 : }
159 :
160 0 : if (!receiver_)
161 0 : return false;
162 :
163 0 : if (!receiver_->AddAndProcessReceivedPacket(packet))
164 0 : return false;
165 :
166 : // Do not report media packets in the RTCP RRs generated by |rtp_rtcp_|.
167 0 : if (packet.Ssrc() == config_.remote_ssrc) {
168 0 : RTPHeader header;
169 0 : packet.GetHeader(&header);
170 : // FlexFEC packets are never retransmitted.
171 0 : const bool kNotRetransmitted = false;
172 0 : rtp_receive_statistics_->IncomingPacket(header, packet.size(),
173 0 : kNotRetransmitted);
174 : }
175 :
176 0 : return true;
177 : }
178 :
179 0 : void FlexfecReceiveStreamImpl::Start() {
180 0 : rtc::CritScope cs(&crit_);
181 0 : started_ = true;
182 0 : }
183 :
184 0 : void FlexfecReceiveStreamImpl::Stop() {
185 0 : rtc::CritScope cs(&crit_);
186 0 : started_ = false;
187 0 : }
188 :
189 : // TODO(brandtr): Implement this member function when we have designed the
190 : // stats for FlexFEC.
191 0 : FlexfecReceiveStreamImpl::Stats FlexfecReceiveStreamImpl::GetStats() const {
192 0 : return FlexfecReceiveStream::Stats();
193 : }
194 :
195 : } // namespace webrtc
|