Line data Source code
1 : /*
2 : * Copyright (c) 2012 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/modules/rtp_rtcp/source/rtp_rtcp_impl.h"
12 :
13 : #include <string.h>
14 :
15 : #include <set>
16 : #include <string>
17 :
18 : #include "webrtc/base/checks.h"
19 : #include "webrtc/base/logging.h"
20 : #include "webrtc/common_types.h"
21 : #include "webrtc/config.h"
22 : #include "webrtc/system_wrappers/include/trace.h"
23 :
24 : #ifdef _WIN32
25 : // Disable warning C4355: 'this' : used in base member initializer list.
26 : #pragma warning(disable : 4355)
27 : #endif
28 :
29 : namespace webrtc {
30 :
31 0 : RTPExtensionType StringToRtpExtensionType(const std::string& extension) {
32 0 : if (extension == RtpExtension::kTimestampOffsetUri)
33 0 : return kRtpExtensionTransmissionTimeOffset;
34 0 : if (extension == RtpExtension::kAudioLevelUri)
35 0 : return kRtpExtensionAudioLevel;
36 0 : if (extension == RtpExtension::kAbsSendTimeUri)
37 0 : return kRtpExtensionAbsoluteSendTime;
38 0 : if (extension == RtpExtension::kVideoRotationUri)
39 0 : return kRtpExtensionVideoRotation;
40 0 : if (extension == RtpExtension::kTransportSequenceNumberUri)
41 0 : return kRtpExtensionTransportSequenceNumber;
42 0 : if (extension == RtpExtension::kPlayoutDelayUri)
43 0 : return kRtpExtensionPlayoutDelay;
44 0 : if (extension == RtpExtension::kRtpStreamIdUri)
45 0 : return kRtpExtensionRtpStreamId;
46 0 : RTC_NOTREACHED() << "Looking up unsupported RTP extension.";
47 0 : return kRtpExtensionNone;
48 : }
49 :
50 0 : RtpRtcp::Configuration::Configuration()
51 0 : : receive_statistics(NullObjectReceiveStatistics()) {}
52 :
53 0 : RtpRtcp* RtpRtcp::CreateRtpRtcp(const RtpRtcp::Configuration& configuration) {
54 0 : if (configuration.clock) {
55 0 : return new ModuleRtpRtcpImpl(configuration);
56 : } else {
57 : // No clock implementation provided, use default clock.
58 0 : RtpRtcp::Configuration configuration_copy;
59 : memcpy(&configuration_copy, &configuration,
60 0 : sizeof(RtpRtcp::Configuration));
61 0 : configuration_copy.clock = Clock::GetRealTimeClock();
62 0 : return new ModuleRtpRtcpImpl(configuration_copy);
63 : }
64 : }
65 :
66 : // Deprecated.
67 0 : int32_t RtpRtcp::SetFecParameters(const FecProtectionParams* delta_params,
68 : const FecProtectionParams* key_params) {
69 0 : RTC_DCHECK(delta_params);
70 0 : RTC_DCHECK(key_params);
71 0 : return SetFecParameters(*delta_params, *key_params) ? 0 : -1;
72 : }
73 :
74 0 : ModuleRtpRtcpImpl::ModuleRtpRtcpImpl(const Configuration& configuration)
75 0 : : rtp_sender_(configuration.audio,
76 0 : configuration.clock,
77 0 : configuration.outgoing_transport,
78 0 : configuration.paced_sender,
79 0 : configuration.flexfec_sender,
80 0 : configuration.transport_sequence_number_allocator,
81 0 : configuration.transport_feedback_callback,
82 0 : configuration.send_bitrate_observer,
83 0 : configuration.send_frame_count_observer,
84 0 : configuration.send_side_delay_observer,
85 0 : configuration.event_log,
86 0 : configuration.send_packet_observer,
87 0 : configuration.retransmission_rate_limiter,
88 0 : configuration.overhead_observer),
89 0 : rtcp_sender_(configuration.audio,
90 0 : configuration.clock,
91 0 : configuration.receive_statistics,
92 0 : configuration.rtcp_packet_type_counter_observer,
93 0 : configuration.event_log,
94 0 : configuration.outgoing_transport),
95 0 : rtcp_receiver_(configuration.clock,
96 0 : configuration.receiver_only,
97 0 : configuration.rtcp_packet_type_counter_observer,
98 0 : configuration.bandwidth_callback,
99 0 : configuration.intra_frame_callback,
100 0 : configuration.transport_feedback_callback,
101 0 : configuration.bitrate_allocation_observer,
102 : this),
103 0 : clock_(configuration.clock),
104 0 : audio_(configuration.audio),
105 : collision_detected_(false),
106 0 : last_process_time_(configuration.clock->TimeInMilliseconds()),
107 0 : last_bitrate_process_time_(configuration.clock->TimeInMilliseconds()),
108 0 : last_rtt_process_time_(configuration.clock->TimeInMilliseconds()),
109 : packet_overhead_(28), // IPV4 UDP.
110 : nack_last_time_sent_full_(0),
111 : nack_last_time_sent_full_prev_(0),
112 : nack_last_seq_number_sent_(0),
113 : key_frame_req_method_(kKeyFrameReqPliRtcp),
114 0 : remote_bitrate_(configuration.remote_bitrate_estimator),
115 0 : rtt_stats_(configuration.rtt_stats),
116 0 : rtt_ms_(0) {
117 : // Make sure that RTCP objects are aware of our SSRC.
118 0 : uint32_t SSRC = rtp_sender_.SSRC();
119 0 : rtcp_sender_.SetSSRC(SSRC);
120 0 : SetRtcpReceiverSsrcs(SSRC);
121 :
122 : // Make sure rtcp sender use same timestamp offset as rtp sender.
123 0 : rtcp_sender_.SetTimestampOffset(rtp_sender_.TimestampOffset());
124 :
125 : // Set default packet size limit.
126 : // TODO(nisse): Kind-of duplicates
127 : // webrtc::VideoSendStream::Config::Rtp::kDefaultMaxPacketSize.
128 0 : const size_t kTcpOverIpv4HeaderSize = 40;
129 0 : SetMaxRtpPacketSize(IP_PACKET_SIZE - kTcpOverIpv4HeaderSize);
130 0 : }
131 :
132 : // Returns the number of milliseconds until the module want a worker thread
133 : // to call Process.
134 0 : int64_t ModuleRtpRtcpImpl::TimeUntilNextProcess() {
135 0 : const int64_t now = clock_->TimeInMilliseconds();
136 0 : const int64_t kRtpRtcpMaxIdleTimeProcessMs = 5;
137 0 : return kRtpRtcpMaxIdleTimeProcessMs - (now - last_process_time_);
138 : }
139 :
140 : // Process any pending tasks such as timeouts (non time critical events).
141 0 : void ModuleRtpRtcpImpl::Process() {
142 0 : const int64_t now = clock_->TimeInMilliseconds();
143 0 : last_process_time_ = now;
144 :
145 0 : const int64_t kRtpRtcpBitrateProcessTimeMs = 10;
146 0 : if (now >= last_bitrate_process_time_ + kRtpRtcpBitrateProcessTimeMs) {
147 0 : rtp_sender_.ProcessBitrate();
148 0 : last_bitrate_process_time_ = now;
149 : }
150 :
151 0 : const int64_t kRtpRtcpRttProcessTimeMs = 1000;
152 0 : bool process_rtt = now >= last_rtt_process_time_ + kRtpRtcpRttProcessTimeMs;
153 0 : if (rtcp_sender_.Sending()) {
154 : // Process RTT if we have received a receiver report and we haven't
155 : // processed RTT for at least |kRtpRtcpRttProcessTimeMs| milliseconds.
156 0 : if (rtcp_receiver_.LastReceivedReceiverReport() >
157 0 : last_rtt_process_time_ && process_rtt) {
158 0 : std::vector<RTCPReportBlock> receive_blocks;
159 0 : rtcp_receiver_.StatisticsReceived(&receive_blocks);
160 0 : int64_t max_rtt = 0;
161 0 : for (std::vector<RTCPReportBlock>::iterator it = receive_blocks.begin();
162 0 : it != receive_blocks.end(); ++it) {
163 0 : int64_t rtt = 0;
164 0 : rtcp_receiver_.RTT(it->remoteSSRC, &rtt, NULL, NULL, NULL);
165 0 : max_rtt = (rtt > max_rtt) ? rtt : max_rtt;
166 : }
167 : // Report the rtt.
168 0 : if (rtt_stats_ && max_rtt != 0)
169 0 : rtt_stats_->OnRttUpdate(max_rtt);
170 : }
171 :
172 : // Verify receiver reports are delivered and the reported sequence number
173 : // is increasing.
174 0 : int64_t rtcp_interval = RtcpReportInterval();
175 0 : if (rtcp_receiver_.RtcpRrTimeout(rtcp_interval)) {
176 0 : LOG_F(LS_WARNING) << "Timeout: No RTCP RR received.";
177 0 : } else if (rtcp_receiver_.RtcpRrSequenceNumberTimeout(rtcp_interval)) {
178 0 : LOG_F(LS_WARNING) <<
179 0 : "Timeout: No increase in RTCP RR extended highest sequence number.";
180 : }
181 :
182 0 : if (remote_bitrate_ && rtcp_sender_.TMMBR()) {
183 0 : unsigned int target_bitrate = 0;
184 0 : std::vector<unsigned int> ssrcs;
185 0 : if (remote_bitrate_->LatestEstimate(&ssrcs, &target_bitrate)) {
186 0 : if (!ssrcs.empty()) {
187 0 : target_bitrate = target_bitrate / ssrcs.size();
188 : }
189 0 : rtcp_sender_.SetTargetBitrate(target_bitrate);
190 : }
191 : }
192 : } else {
193 : // Report rtt from receiver.
194 0 : if (process_rtt) {
195 : int64_t rtt_ms;
196 0 : if (rtt_stats_ && rtcp_receiver_.GetAndResetXrRrRtt(&rtt_ms)) {
197 0 : rtt_stats_->OnRttUpdate(rtt_ms);
198 : }
199 : }
200 : }
201 :
202 : // Get processed rtt.
203 0 : if (process_rtt) {
204 0 : last_rtt_process_time_ = now;
205 0 : if (rtt_stats_) {
206 : // Make sure we have a valid RTT before setting.
207 0 : int64_t last_rtt = rtt_stats_->LastProcessedRtt();
208 0 : if (last_rtt >= 0)
209 0 : set_rtt_ms(last_rtt);
210 : }
211 : }
212 :
213 0 : if (rtcp_sender_.TimeToSendRTCPReport())
214 0 : rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpReport);
215 :
216 0 : if (UpdateRTCPReceiveInformationTimers()) {
217 : // A receiver has timed out.
218 0 : rtcp_receiver_.UpdateTmmbr();
219 : }
220 0 : }
221 :
222 0 : void ModuleRtpRtcpImpl::SetRtxSendStatus(int mode) {
223 0 : rtp_sender_.SetRtxStatus(mode);
224 0 : }
225 :
226 0 : int ModuleRtpRtcpImpl::RtxSendStatus() const {
227 0 : return rtp_sender_.RtxStatus();
228 : }
229 :
230 0 : void ModuleRtpRtcpImpl::SetRtxSsrc(uint32_t ssrc) {
231 0 : rtp_sender_.SetRtxSsrc(ssrc);
232 0 : }
233 :
234 0 : void ModuleRtpRtcpImpl::SetRtxSendPayloadType(int payload_type,
235 : int associated_payload_type) {
236 0 : rtp_sender_.SetRtxPayloadType(payload_type, associated_payload_type);
237 0 : }
238 :
239 0 : rtc::Optional<uint32_t> ModuleRtpRtcpImpl::FlexfecSsrc() const {
240 0 : return rtp_sender_.FlexfecSsrc();
241 : }
242 :
243 0 : int32_t ModuleRtpRtcpImpl::IncomingRtcpPacket(
244 : const uint8_t* rtcp_packet,
245 : const size_t length) {
246 0 : return rtcp_receiver_.IncomingPacket(rtcp_packet, length) ? 0 : -1;
247 : }
248 :
249 0 : int32_t ModuleRtpRtcpImpl::RegisterSendPayload(
250 : const CodecInst& voice_codec) {
251 0 : return rtp_sender_.RegisterPayload(
252 0 : voice_codec.plname, voice_codec.pltype, voice_codec.plfreq,
253 0 : voice_codec.channels, (voice_codec.rate < 0) ? 0 : voice_codec.rate);
254 : }
255 :
256 0 : int32_t ModuleRtpRtcpImpl::RegisterSendPayload(const VideoCodec& video_codec) {
257 0 : return rtp_sender_.RegisterPayload(video_codec.plName, video_codec.plType,
258 0 : 90000, 0, 0);
259 : }
260 :
261 0 : void ModuleRtpRtcpImpl::RegisterVideoSendPayload(int payload_type,
262 : const char* payload_name) {
263 0 : RTC_CHECK_EQ(
264 0 : 0, rtp_sender_.RegisterPayload(payload_name, payload_type, 90000, 0, 0));
265 0 : }
266 :
267 0 : int32_t ModuleRtpRtcpImpl::DeRegisterSendPayload(const int8_t payload_type) {
268 0 : return rtp_sender_.DeRegisterSendPayload(payload_type);
269 : }
270 :
271 0 : int8_t ModuleRtpRtcpImpl::SendPayloadType() const {
272 0 : return rtp_sender_.SendPayloadType();
273 : }
274 :
275 0 : uint32_t ModuleRtpRtcpImpl::StartTimestamp() const {
276 0 : return rtp_sender_.TimestampOffset();
277 : }
278 :
279 : // Configure start timestamp, default is a random number.
280 0 : void ModuleRtpRtcpImpl::SetStartTimestamp(const uint32_t timestamp) {
281 0 : rtcp_sender_.SetTimestampOffset(timestamp);
282 0 : rtp_sender_.SetTimestampOffset(timestamp);
283 0 : }
284 :
285 0 : uint16_t ModuleRtpRtcpImpl::SequenceNumber() const {
286 0 : return rtp_sender_.SequenceNumber();
287 : }
288 :
289 : // Set SequenceNumber, default is a random number.
290 0 : void ModuleRtpRtcpImpl::SetSequenceNumber(const uint16_t seq_num) {
291 0 : rtp_sender_.SetSequenceNumber(seq_num);
292 0 : }
293 :
294 0 : void ModuleRtpRtcpImpl::SetRtpState(const RtpState& rtp_state) {
295 0 : rtp_sender_.SetRtpState(rtp_state);
296 0 : rtcp_sender_.SetTimestampOffset(rtp_state.start_timestamp);
297 0 : }
298 :
299 0 : void ModuleRtpRtcpImpl::SetRtxState(const RtpState& rtp_state) {
300 0 : rtp_sender_.SetRtxRtpState(rtp_state);
301 0 : }
302 :
303 0 : RtpState ModuleRtpRtcpImpl::GetRtpState() const {
304 0 : return rtp_sender_.GetRtpState();
305 : }
306 :
307 0 : RtpState ModuleRtpRtcpImpl::GetRtxState() const {
308 0 : return rtp_sender_.GetRtxRtpState();
309 : }
310 :
311 0 : uint32_t ModuleRtpRtcpImpl::SSRC() const {
312 0 : return rtp_sender_.SSRC();
313 : }
314 :
315 : // Configure SSRC, default is a random number.
316 0 : void ModuleRtpRtcpImpl::SetSSRC(const uint32_t ssrc) {
317 0 : rtp_sender_.SetSSRC(ssrc);
318 0 : rtcp_sender_.SetSSRC(ssrc);
319 0 : SetRtcpReceiverSsrcs(ssrc);
320 0 : }
321 :
322 0 : void ModuleRtpRtcpImpl::SetCsrcs(const std::vector<uint32_t>& csrcs) {
323 0 : rtcp_sender_.SetCsrcs(csrcs);
324 0 : rtp_sender_.SetCsrcs(csrcs);
325 0 : }
326 :
327 0 : int32_t ModuleRtpRtcpImpl::SetRID(const char *rid) {
328 : //XXX rtcp_sender_.SetRID(rid);
329 0 : return rtp_sender_.SetRID(rid);
330 : }
331 :
332 : // TODO(pbos): Handle media and RTX streams separately (separate RTCP
333 : // feedbacks).
334 0 : RTCPSender::FeedbackState ModuleRtpRtcpImpl::GetFeedbackState() {
335 0 : StreamDataCounters rtp_stats;
336 0 : StreamDataCounters rtx_stats;
337 0 : rtp_sender_.GetDataCounters(&rtp_stats, &rtx_stats);
338 :
339 0 : RTCPSender::FeedbackState state;
340 0 : state.send_payload_type = SendPayloadType();
341 0 : state.packets_sent = rtp_stats.transmitted.packets +
342 0 : rtx_stats.transmitted.packets;
343 0 : state.media_bytes_sent = rtp_stats.transmitted.payload_bytes +
344 0 : rtx_stats.transmitted.payload_bytes;
345 0 : state.module = this;
346 :
347 : LastReceivedNTP(&state.last_rr_ntp_secs,
348 : &state.last_rr_ntp_frac,
349 0 : &state.remote_sr);
350 :
351 0 : state.has_last_xr_rr =
352 0 : rtcp_receiver_.LastReceivedXrReferenceTimeInfo(&state.last_xr_rr);
353 :
354 : uint32_t tmp;
355 0 : BitrateSent(&state.send_bitrate, &tmp, &tmp, &tmp);
356 0 : return state;
357 : }
358 :
359 0 : int32_t ModuleRtpRtcpImpl::SetSendingStatus(const bool sending) {
360 0 : if (rtcp_sender_.Sending() != sending) {
361 : // Sends RTCP BYE when going from true to false
362 0 : if (rtcp_sender_.SetSendingStatus(GetFeedbackState(), sending) != 0) {
363 0 : LOG(LS_WARNING) << "Failed to send RTCP BYE";
364 : }
365 :
366 0 : collision_detected_ = false;
367 :
368 : // Generate a new SSRC for the next "call" if false
369 0 : rtp_sender_.SetSendingStatus(sending);
370 :
371 : // Make sure that RTCP objects are aware of our SSRC (it could have changed
372 : // Due to collision)
373 0 : uint32_t SSRC = rtp_sender_.SSRC();
374 0 : rtcp_sender_.SetSSRC(SSRC);
375 0 : SetRtcpReceiverSsrcs(SSRC);
376 :
377 0 : return 0;
378 : }
379 0 : return 0;
380 : }
381 :
382 0 : bool ModuleRtpRtcpImpl::Sending() const {
383 0 : return rtcp_sender_.Sending();
384 : }
385 :
386 0 : void ModuleRtpRtcpImpl::SetSendingMediaStatus(const bool sending) {
387 0 : rtp_sender_.SetSendingMediaStatus(sending);
388 0 : }
389 :
390 0 : bool ModuleRtpRtcpImpl::SendingMedia() const {
391 0 : return rtp_sender_.SendingMedia();
392 : }
393 :
394 0 : bool ModuleRtpRtcpImpl::SendOutgoingData(
395 : FrameType frame_type,
396 : int8_t payload_type,
397 : uint32_t time_stamp,
398 : int64_t capture_time_ms,
399 : const uint8_t* payload_data,
400 : size_t payload_size,
401 : const RTPFragmentationHeader* fragmentation,
402 : const RTPVideoHeader* rtp_video_header,
403 : uint32_t* transport_frame_id_out) {
404 0 : rtcp_sender_.SetLastRtpTime(time_stamp, capture_time_ms);
405 : // Make sure an RTCP report isn't queued behind a key frame.
406 0 : if (rtcp_sender_.TimeToSendRTCPReport(kVideoFrameKey == frame_type)) {
407 0 : rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpReport);
408 : }
409 0 : return rtp_sender_.SendOutgoingData(
410 : frame_type, payload_type, time_stamp, capture_time_ms, payload_data,
411 0 : payload_size, fragmentation, rtp_video_header, transport_frame_id_out);
412 : }
413 :
414 0 : bool ModuleRtpRtcpImpl::TimeToSendPacket(uint32_t ssrc,
415 : uint16_t sequence_number,
416 : int64_t capture_time_ms,
417 : bool retransmission,
418 : int probe_cluster_id) {
419 0 : return rtp_sender_.TimeToSendPacket(ssrc, sequence_number, capture_time_ms,
420 0 : retransmission, probe_cluster_id);
421 : }
422 :
423 0 : size_t ModuleRtpRtcpImpl::TimeToSendPadding(size_t bytes,
424 : int probe_cluster_id) {
425 0 : return rtp_sender_.TimeToSendPadding(bytes, probe_cluster_id);
426 : }
427 :
428 0 : size_t ModuleRtpRtcpImpl::MaxPayloadSize() const {
429 0 : return rtp_sender_.MaxPayloadSize();
430 : }
431 :
432 0 : size_t ModuleRtpRtcpImpl::MaxRtpPacketSize() const {
433 0 : return rtp_sender_.MaxRtpPacketSize();
434 : }
435 :
436 0 : void ModuleRtpRtcpImpl::SetMaxRtpPacketSize(size_t rtp_packet_size) {
437 0 : RTC_DCHECK_LE(rtp_packet_size, IP_PACKET_SIZE)
438 0 : << "rtp packet size too large: " << rtp_packet_size;
439 0 : RTC_DCHECK_GT(rtp_packet_size, packet_overhead_)
440 0 : << "rtp packet size too small: " << rtp_packet_size;
441 :
442 0 : rtcp_sender_.SetMaxRtpPacketSize(rtp_packet_size);
443 0 : rtp_sender_.SetMaxRtpPacketSize(rtp_packet_size);
444 0 : }
445 :
446 0 : RtcpMode ModuleRtpRtcpImpl::RTCP() const {
447 0 : return rtcp_sender_.Status();
448 : }
449 :
450 : // Configure RTCP status i.e on/off.
451 0 : void ModuleRtpRtcpImpl::SetRTCPStatus(const RtcpMode method) {
452 0 : rtcp_sender_.SetRTCPStatus(method);
453 0 : }
454 :
455 0 : int32_t ModuleRtpRtcpImpl::SetCNAME(const char* c_name) {
456 0 : return rtcp_sender_.SetCNAME(c_name);
457 : }
458 :
459 0 : int32_t ModuleRtpRtcpImpl::AddMixedCNAME(uint32_t ssrc, const char* c_name) {
460 0 : return rtcp_sender_.AddMixedCNAME(ssrc, c_name);
461 : }
462 :
463 0 : int32_t ModuleRtpRtcpImpl::RemoveMixedCNAME(const uint32_t ssrc) {
464 0 : return rtcp_sender_.RemoveMixedCNAME(ssrc);
465 : }
466 :
467 0 : int32_t ModuleRtpRtcpImpl::RemoteCNAME(
468 : const uint32_t remote_ssrc,
469 : char c_name[RTCP_CNAME_SIZE]) const {
470 0 : return rtcp_receiver_.CNAME(remote_ssrc, c_name);
471 : }
472 :
473 0 : int32_t ModuleRtpRtcpImpl::RemoteNTP(
474 : uint32_t* received_ntpsecs,
475 : uint32_t* received_ntpfrac,
476 : uint32_t* rtcp_arrival_time_secs,
477 : uint32_t* rtcp_arrival_time_frac,
478 : uint32_t* rtcp_timestamp) const {
479 0 : return rtcp_receiver_.NTP(received_ntpsecs,
480 : received_ntpfrac,
481 : rtcp_arrival_time_secs,
482 : rtcp_arrival_time_frac,
483 : rtcp_timestamp)
484 0 : ? 0
485 0 : : -1;
486 : }
487 :
488 : // Get RoundTripTime.
489 0 : int32_t ModuleRtpRtcpImpl::RTT(const uint32_t remote_ssrc,
490 : int64_t* rtt,
491 : int64_t* avg_rtt,
492 : int64_t* min_rtt,
493 : int64_t* max_rtt) const {
494 0 : int32_t ret = rtcp_receiver_.RTT(remote_ssrc, rtt, avg_rtt, min_rtt, max_rtt);
495 0 : if (rtt && *rtt == 0) {
496 : // Try to get RTT from RtcpRttStats class.
497 0 : *rtt = rtt_ms();
498 : }
499 0 : return ret;
500 : }
501 :
502 : // Force a send of an RTCP packet.
503 : // Normal SR and RR are triggered via the process function.
504 0 : int32_t ModuleRtpRtcpImpl::SendRTCP(RTCPPacketType packet_type) {
505 0 : return rtcp_sender_.SendRTCP(GetFeedbackState(), packet_type);
506 : }
507 :
508 : // Force a send of an RTCP packet.
509 : // Normal SR and RR are triggered via the process function.
510 0 : int32_t ModuleRtpRtcpImpl::SendCompoundRTCP(
511 : const std::set<RTCPPacketType>& packet_types) {
512 0 : return rtcp_sender_.SendCompoundRTCP(GetFeedbackState(), packet_types);
513 : }
514 :
515 0 : int32_t ModuleRtpRtcpImpl::SetRTCPApplicationSpecificData(
516 : const uint8_t sub_type,
517 : const uint32_t name,
518 : const uint8_t* data,
519 : const uint16_t length) {
520 0 : return rtcp_sender_.SetApplicationSpecificData(sub_type, name, data, length);
521 : }
522 :
523 : // (XR) VOIP metric.
524 0 : int32_t ModuleRtpRtcpImpl::SetRTCPVoIPMetrics(
525 : const RTCPVoIPMetric* voip_metric) {
526 0 : return rtcp_sender_.SetRTCPVoIPMetrics(voip_metric);
527 : }
528 :
529 0 : void ModuleRtpRtcpImpl::SetRtcpXrRrtrStatus(bool enable) {
530 0 : rtcp_receiver_.SetRtcpXrRrtrStatus(enable);
531 0 : rtcp_sender_.SendRtcpXrReceiverReferenceTime(enable);
532 0 : }
533 :
534 0 : bool ModuleRtpRtcpImpl::RtcpXrRrtrStatus() const {
535 0 : return rtcp_sender_.RtcpXrReceiverReferenceTime();
536 : }
537 :
538 : // TODO(asapersson): Replace this method with the one below.
539 0 : int32_t ModuleRtpRtcpImpl::DataCountersRTP(
540 : size_t* bytes_sent,
541 : uint32_t* packets_sent) const {
542 0 : StreamDataCounters rtp_stats;
543 0 : StreamDataCounters rtx_stats;
544 0 : rtp_sender_.GetDataCounters(&rtp_stats, &rtx_stats);
545 :
546 0 : if (bytes_sent) {
547 0 : *bytes_sent = rtp_stats.transmitted.payload_bytes +
548 0 : rtp_stats.transmitted.padding_bytes +
549 0 : rtp_stats.transmitted.header_bytes +
550 0 : rtx_stats.transmitted.payload_bytes +
551 0 : rtx_stats.transmitted.padding_bytes +
552 0 : rtx_stats.transmitted.header_bytes;
553 : }
554 0 : if (packets_sent) {
555 0 : *packets_sent = rtp_stats.transmitted.packets +
556 0 : rtx_stats.transmitted.packets;
557 : }
558 0 : return 0;
559 : }
560 :
561 0 : void ModuleRtpRtcpImpl::GetSendStreamDataCounters(
562 : StreamDataCounters* rtp_counters,
563 : StreamDataCounters* rtx_counters) const {
564 0 : rtp_sender_.GetDataCounters(rtp_counters, rtx_counters);
565 0 : }
566 :
567 0 : void ModuleRtpRtcpImpl::GetRtpPacketLossStats(
568 : bool outgoing,
569 : uint32_t ssrc,
570 : struct RtpPacketLossStats* loss_stats) const {
571 0 : if (!loss_stats) return;
572 0 : const PacketLossStats* stats_source = NULL;
573 0 : if (outgoing) {
574 0 : if (SSRC() == ssrc) {
575 0 : stats_source = &send_loss_stats_;
576 : }
577 : } else {
578 0 : if (rtcp_receiver_.RemoteSSRC() == ssrc) {
579 0 : stats_source = &receive_loss_stats_;
580 : }
581 : }
582 0 : if (stats_source) {
583 0 : loss_stats->single_packet_loss_count =
584 0 : stats_source->GetSingleLossCount();
585 0 : loss_stats->multiple_packet_loss_event_count =
586 0 : stats_source->GetMultipleLossEventCount();
587 0 : loss_stats->multiple_packet_loss_packet_count =
588 0 : stats_source->GetMultipleLossPacketCount();
589 : }
590 : }
591 :
592 0 : int32_t ModuleRtpRtcpImpl::RemoteRTCPStat(RTCPSenderInfo* sender_info) {
593 0 : return rtcp_receiver_.SenderInfoReceived(sender_info);
594 : }
595 :
596 : // Received RTCP report.
597 0 : int32_t ModuleRtpRtcpImpl::RemoteRTCPStat(
598 : std::vector<RTCPReportBlock>* receive_blocks) const {
599 0 : return rtcp_receiver_.StatisticsReceived(receive_blocks);
600 : }
601 :
602 : // (REMB) Receiver Estimated Max Bitrate.
603 0 : bool ModuleRtpRtcpImpl::REMB() const {
604 0 : return rtcp_sender_.REMB();
605 : }
606 :
607 0 : void ModuleRtpRtcpImpl::SetREMBStatus(const bool enable) {
608 0 : rtcp_sender_.SetREMBStatus(enable);
609 0 : }
610 :
611 0 : void ModuleRtpRtcpImpl::SetREMBData(const uint32_t bitrate,
612 : const std::vector<uint32_t>& ssrcs) {
613 0 : rtcp_sender_.SetREMBData(bitrate, ssrcs);
614 0 : }
615 :
616 0 : int32_t ModuleRtpRtcpImpl::RegisterSendRtpHeaderExtension(
617 : const RTPExtensionType type,
618 : const uint8_t id) {
619 0 : return rtp_sender_.RegisterRtpHeaderExtension(type, id);
620 : }
621 :
622 0 : int32_t ModuleRtpRtcpImpl::DeregisterSendRtpHeaderExtension(
623 : const RTPExtensionType type) {
624 0 : return rtp_sender_.DeregisterRtpHeaderExtension(type);
625 : }
626 :
627 : // (TMMBR) Temporary Max Media Bit Rate.
628 0 : bool ModuleRtpRtcpImpl::TMMBR() const {
629 0 : return rtcp_sender_.TMMBR();
630 : }
631 :
632 0 : void ModuleRtpRtcpImpl::SetTMMBRStatus(const bool enable) {
633 0 : rtcp_sender_.SetTMMBRStatus(enable);
634 0 : }
635 :
636 0 : void ModuleRtpRtcpImpl::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) {
637 0 : rtcp_sender_.SetTmmbn(std::move(bounding_set));
638 0 : }
639 :
640 : // Returns the currently configured retransmission mode.
641 0 : int ModuleRtpRtcpImpl::SelectiveRetransmissions() const {
642 0 : return rtp_sender_.SelectiveRetransmissions();
643 : }
644 :
645 : // Enable or disable a retransmission mode, which decides which packets will
646 : // be retransmitted if NACKed.
647 0 : int ModuleRtpRtcpImpl::SetSelectiveRetransmissions(uint8_t settings) {
648 0 : return rtp_sender_.SetSelectiveRetransmissions(settings);
649 : }
650 :
651 : // Send a Negative acknowledgment packet.
652 0 : int32_t ModuleRtpRtcpImpl::SendNACK(const uint16_t* nack_list,
653 : const uint16_t size) {
654 0 : for (int i = 0; i < size; ++i) {
655 0 : receive_loss_stats_.AddLostPacket(nack_list[i]);
656 : }
657 0 : uint16_t nack_length = size;
658 0 : uint16_t start_id = 0;
659 0 : int64_t now = clock_->TimeInMilliseconds();
660 0 : if (TimeToSendFullNackList(now)) {
661 0 : nack_last_time_sent_full_ = now;
662 0 : nack_last_time_sent_full_prev_ = now;
663 : } else {
664 : // Only send extended list.
665 0 : if (nack_last_seq_number_sent_ == nack_list[size - 1]) {
666 : // Last sequence number is the same, do not send list.
667 0 : return 0;
668 : }
669 : // Send new sequence numbers.
670 0 : for (int i = 0; i < size; ++i) {
671 0 : if (nack_last_seq_number_sent_ == nack_list[i]) {
672 0 : start_id = i + 1;
673 0 : break;
674 : }
675 : }
676 0 : nack_length = size - start_id;
677 : }
678 :
679 : // Our RTCP NACK implementation is limited to kRtcpMaxNackFields sequence
680 : // numbers per RTCP packet.
681 0 : if (nack_length > kRtcpMaxNackFields) {
682 0 : nack_length = kRtcpMaxNackFields;
683 : }
684 0 : nack_last_seq_number_sent_ = nack_list[start_id + nack_length - 1];
685 :
686 0 : return rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpNack, nack_length,
687 0 : &nack_list[start_id]);
688 : }
689 :
690 0 : void ModuleRtpRtcpImpl::SendNack(
691 : const std::vector<uint16_t>& sequence_numbers) {
692 0 : rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpNack, sequence_numbers.size(),
693 0 : sequence_numbers.data());
694 0 : }
695 :
696 0 : bool ModuleRtpRtcpImpl::TimeToSendFullNackList(int64_t now) const {
697 : // Use RTT from RtcpRttStats class if provided.
698 0 : int64_t rtt = rtt_ms();
699 0 : if (rtt == 0) {
700 0 : rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
701 : }
702 :
703 0 : const int64_t kStartUpRttMs = 100;
704 0 : int64_t wait_time = 5 + ((rtt * 3) >> 1); // 5 + RTT * 1.5.
705 0 : if (rtt == 0) {
706 0 : wait_time = kStartUpRttMs;
707 : }
708 :
709 : // Send a full NACK list once within every |wait_time|.
710 0 : if (rtt_stats_) {
711 0 : return now - nack_last_time_sent_full_ > wait_time;
712 : }
713 0 : return now - nack_last_time_sent_full_prev_ > wait_time;
714 : }
715 :
716 : // Store the sent packets, needed to answer to Negative acknowledgment requests.
717 0 : void ModuleRtpRtcpImpl::SetStorePacketsStatus(const bool enable,
718 : const uint16_t number_to_store) {
719 0 : rtp_sender_.SetStorePacketsStatus(enable, number_to_store);
720 0 : }
721 :
722 0 : bool ModuleRtpRtcpImpl::StorePackets() const {
723 0 : return rtp_sender_.StorePackets();
724 : }
725 :
726 0 : void ModuleRtpRtcpImpl::RegisterRtcpStatisticsCallback(
727 : RtcpStatisticsCallback* callback) {
728 0 : rtcp_receiver_.RegisterRtcpStatisticsCallback(callback);
729 0 : }
730 :
731 0 : RtcpStatisticsCallback* ModuleRtpRtcpImpl::GetRtcpStatisticsCallback() {
732 0 : return rtcp_receiver_.GetRtcpStatisticsCallback();
733 : }
734 :
735 0 : bool ModuleRtpRtcpImpl::SendFeedbackPacket(
736 : const rtcp::TransportFeedback& packet) {
737 0 : return rtcp_sender_.SendFeedbackPacket(packet);
738 : }
739 :
740 : // Send a TelephoneEvent tone using RFC 2833 (4733).
741 0 : int32_t ModuleRtpRtcpImpl::SendTelephoneEventOutband(
742 : const uint8_t key,
743 : const uint16_t time_ms,
744 : const uint8_t level) {
745 0 : return rtp_sender_.SendTelephoneEvent(key, time_ms, level);
746 : }
747 :
748 0 : int32_t ModuleRtpRtcpImpl::SetAudioPacketSize(
749 : const uint16_t packet_size_samples) {
750 0 : return audio_ ? 0 : -1;
751 : }
752 :
753 0 : int32_t ModuleRtpRtcpImpl::SetAudioLevel(
754 : const uint8_t level_d_bov) {
755 0 : return rtp_sender_.SetAudioLevel(level_d_bov);
756 : }
757 :
758 0 : int32_t ModuleRtpRtcpImpl::SetKeyFrameRequestMethod(
759 : const KeyFrameRequestMethod method) {
760 0 : key_frame_req_method_ = method;
761 0 : return 0;
762 : }
763 :
764 0 : int32_t ModuleRtpRtcpImpl::RequestKeyFrame() {
765 0 : switch (key_frame_req_method_) {
766 : case kKeyFrameReqPliRtcp:
767 0 : return SendRTCP(kRtcpPli);
768 : case kKeyFrameReqFirRtcp:
769 0 : return SendRTCP(kRtcpFir);
770 : }
771 0 : return -1;
772 : }
773 :
774 0 : int32_t ModuleRtpRtcpImpl::SendRTCPSliceLossIndication(
775 : const uint8_t picture_id) {
776 0 : return rtcp_sender_.SendRTCP(
777 0 : GetFeedbackState(), kRtcpSli, 0, 0, false, picture_id);
778 : }
779 :
780 0 : void ModuleRtpRtcpImpl::SetUlpfecConfig(int red_payload_type,
781 : int ulpfec_payload_type) {
782 0 : rtp_sender_.SetUlpfecConfig(red_payload_type, ulpfec_payload_type);
783 0 : }
784 :
785 0 : bool ModuleRtpRtcpImpl::SetFecParameters(
786 : const FecProtectionParams& delta_params,
787 : const FecProtectionParams& key_params) {
788 0 : return rtp_sender_.SetFecParameters(delta_params, key_params);
789 : }
790 :
791 0 : void ModuleRtpRtcpImpl::SetRemoteSSRC(const uint32_t ssrc) {
792 : // Inform about the incoming SSRC.
793 0 : rtcp_sender_.SetRemoteSSRC(ssrc);
794 0 : rtcp_receiver_.SetRemoteSSRC(ssrc);
795 :
796 : // Check for a SSRC collision.
797 0 : if (rtp_sender_.SSRC() == ssrc && !collision_detected_) {
798 : // If we detect a collision change the SSRC but only once.
799 0 : collision_detected_ = true;
800 0 : uint32_t new_ssrc = rtp_sender_.GenerateNewSSRC();
801 0 : if (new_ssrc == 0) {
802 : // Configured via API ignore.
803 0 : return;
804 : }
805 0 : if (RtcpMode::kOff != rtcp_sender_.Status()) {
806 : // Send RTCP bye on the current SSRC.
807 0 : SendRTCP(kRtcpBye);
808 : }
809 : // Change local SSRC and inform all objects about the new SSRC.
810 0 : rtcp_sender_.SetSSRC(new_ssrc);
811 0 : SetRtcpReceiverSsrcs(new_ssrc);
812 : }
813 : }
814 :
815 0 : void ModuleRtpRtcpImpl::BitrateSent(uint32_t* total_rate,
816 : uint32_t* video_rate,
817 : uint32_t* fec_rate,
818 : uint32_t* nack_rate) const {
819 0 : *total_rate = rtp_sender_.BitrateSent();
820 0 : *video_rate = rtp_sender_.VideoBitrateSent();
821 0 : *fec_rate = rtp_sender_.FecOverheadRate();
822 0 : *nack_rate = rtp_sender_.NackOverheadRate();
823 0 : }
824 :
825 0 : void ModuleRtpRtcpImpl::OnRequestSendReport() {
826 0 : SendRTCP(kRtcpSr);
827 0 : }
828 :
829 0 : int32_t ModuleRtpRtcpImpl::SendRTCPReferencePictureSelection(
830 : const uint64_t picture_id) {
831 0 : return rtcp_sender_.SendRTCP(
832 0 : GetFeedbackState(), kRtcpRpsi, 0, 0, false, picture_id);
833 : }
834 :
835 0 : bool ModuleRtpRtcpImpl::GetSendReportMetadata(const uint32_t send_report,
836 : uint64_t *time_of_send,
837 : uint32_t *packet_count,
838 : uint64_t *octet_count) {
839 0 : return rtcp_sender_.GetSendReportMetadata(send_report,
840 : time_of_send,
841 : packet_count,
842 0 : octet_count);
843 : }
844 :
845 0 : void ModuleRtpRtcpImpl::OnReceivedNack(
846 : const std::vector<uint16_t>& nack_sequence_numbers) {
847 0 : for (uint16_t nack_sequence_number : nack_sequence_numbers) {
848 0 : send_loss_stats_.AddLostPacket(nack_sequence_number);
849 : }
850 0 : if (!rtp_sender_.StorePackets() ||
851 0 : nack_sequence_numbers.size() == 0) {
852 0 : return;
853 : }
854 : // Use RTT from RtcpRttStats class if provided.
855 0 : int64_t rtt = rtt_ms();
856 0 : if (rtt == 0) {
857 0 : rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
858 : }
859 0 : rtp_sender_.OnReceivedNack(nack_sequence_numbers, rtt);
860 : }
861 :
862 0 : void ModuleRtpRtcpImpl::OnReceivedRtcpReportBlocks(
863 : const ReportBlockList& report_blocks) {
864 0 : rtp_sender_.OnReceivedRtcpReportBlocks(report_blocks);
865 0 : }
866 :
867 0 : bool ModuleRtpRtcpImpl::LastReceivedNTP(
868 : uint32_t* rtcp_arrival_time_secs, // When we got the last report.
869 : uint32_t* rtcp_arrival_time_frac,
870 : uint32_t* remote_sr) const {
871 : // Remote SR: NTP inside the last received (mid 16 bits from sec and frac).
872 0 : uint32_t ntp_secs = 0;
873 0 : uint32_t ntp_frac = 0;
874 :
875 0 : if (!rtcp_receiver_.NTP(&ntp_secs,
876 : &ntp_frac,
877 : rtcp_arrival_time_secs,
878 : rtcp_arrival_time_frac,
879 : NULL)) {
880 0 : return false;
881 : }
882 0 : *remote_sr =
883 0 : ((ntp_secs & 0x0000ffff) << 16) + ((ntp_frac & 0xffff0000) >> 16);
884 0 : return true;
885 : }
886 :
887 0 : bool ModuleRtpRtcpImpl::UpdateRTCPReceiveInformationTimers() {
888 : // If this returns true this channel has timed out.
889 : // Periodically check if this is true and if so call UpdateTMMBR.
890 0 : return rtcp_receiver_.UpdateRTCPReceiveInformationTimers();
891 : }
892 :
893 : // Called from RTCPsender.
894 0 : std::vector<rtcp::TmmbItem> ModuleRtpRtcpImpl::BoundingSet(bool* tmmbr_owner) {
895 0 : return rtcp_receiver_.BoundingSet(tmmbr_owner);
896 : }
897 :
898 0 : int64_t ModuleRtpRtcpImpl::RtcpReportInterval() {
899 0 : if (audio_)
900 0 : return RTCP_INTERVAL_AUDIO_MS;
901 : else
902 0 : return RTCP_INTERVAL_VIDEO_MS;
903 : }
904 :
905 0 : void ModuleRtpRtcpImpl::SetRtcpReceiverSsrcs(uint32_t main_ssrc) {
906 0 : std::set<uint32_t> ssrcs;
907 0 : ssrcs.insert(main_ssrc);
908 0 : if (rtp_sender_.RtxStatus() != kRtxOff)
909 0 : ssrcs.insert(rtp_sender_.RtxSsrc());
910 0 : rtcp_receiver_.SetSsrcs(main_ssrc, ssrcs);
911 0 : }
912 :
913 0 : void ModuleRtpRtcpImpl::set_rtt_ms(int64_t rtt_ms) {
914 0 : rtc::CritScope cs(&critical_section_rtt_);
915 0 : rtt_ms_ = rtt_ms;
916 0 : }
917 :
918 0 : int64_t ModuleRtpRtcpImpl::rtt_ms() const {
919 0 : rtc::CritScope cs(&critical_section_rtt_);
920 0 : return rtt_ms_;
921 : }
922 :
923 0 : void ModuleRtpRtcpImpl::RegisterSendChannelRtpStatisticsCallback(
924 : StreamDataCountersCallback* callback) {
925 0 : rtp_sender_.RegisterRtpStatisticsCallback(callback);
926 0 : }
927 :
928 : StreamDataCountersCallback*
929 0 : ModuleRtpRtcpImpl::GetSendChannelRtpStatisticsCallback() const {
930 0 : return rtp_sender_.GetRtpStatisticsCallback();
931 : }
932 :
933 0 : void ModuleRtpRtcpImpl::SetVideoBitrateAllocation(
934 : const BitrateAllocation& bitrate) {
935 0 : rtcp_sender_.SetVideoBitrateAllocation(bitrate);
936 0 : }
937 : } // namespace webrtc
|