blob: d757f93726508bbe87354f431e947dbad927973b [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
leozwang@webrtc.org39e96592012-03-01 18:22:48 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000011#include "webrtc/video_engine/vie_receiver.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +000013#include <vector>
14
Peter Boström415d2cd2015-10-26 11:35:17 +010015#include "webrtc/base/logging.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000016#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000017#include "webrtc/modules/rtp_rtcp/interface/fec_receiver.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000018#include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h"
wu@webrtc.org88abf112014-05-14 16:53:51 +000019#include "webrtc/modules/rtp_rtcp/interface/remote_ntp_time_estimator.h"
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +000020#include "webrtc/modules/rtp_rtcp/interface/rtp_cvo.h"
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000021#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000022#include "webrtc/modules/rtp_rtcp/interface/rtp_payload_registry.h"
23#include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000024#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000025#include "webrtc/modules/video_coding/main/interface/video_coding.h"
26#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000027#include "webrtc/system_wrappers/interface/metrics.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000028#include "webrtc/system_wrappers/interface/tick_util.h"
wu@webrtc.org66773a02014-05-07 17:09:44 +000029#include "webrtc/system_wrappers/interface/timestamp_extrapolator.h"
wu@webrtc.org88abf112014-05-14 16:53:51 +000030#include "webrtc/system_wrappers/interface/trace.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000031
32namespace webrtc {
33
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000034static const int kPacketLogIntervalMs = 10000;
35
Peter Boströmac547a62015-09-17 23:03:57 +020036ViEReceiver::ViEReceiver(VideoCodingModule* module_vcm,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000037 RemoteBitrateEstimator* remote_bitrate_estimator,
38 RtpFeedback* rtp_feedback)
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +000039 : receive_cs_(CriticalSectionWrapper::CreateCriticalSection()),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000040 clock_(Clock::GetRealTimeClock()),
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000041 rtp_header_parser_(RtpHeaderParser::Create()),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000042 rtp_payload_registry_(
43 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(false))),
44 rtp_receiver_(
Peter Boströmac547a62015-09-17 23:03:57 +020045 RtpReceiver::CreateVideoReceiver(clock_,
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000046 this,
47 rtp_feedback,
48 rtp_payload_registry_.get())),
49 rtp_receive_statistics_(ReceiveStatistics::Create(clock_)),
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +000050 fec_receiver_(FecReceiver::Create(this)),
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000051 rtp_rtcp_(NULL),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000052 vcm_(module_vcm),
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000053 remote_bitrate_estimator_(remote_bitrate_estimator),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000054 ntp_estimator_(new RemoteNtpTimeEstimator(clock_)),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000055 receiving_(false),
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +000056 restored_packet_in_use_(false),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000057 receiving_ast_enabled_(false),
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +000058 receiving_cvo_enabled_(false),
sprang867fb522015-08-03 04:38:41 -070059 receiving_tsn_enabled_(false),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000060 last_packet_log_ms_(-1) {
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000061 assert(remote_bitrate_estimator);
niklase@google.com470e71d2011-07-07 08:21:25 +000062}
63
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000064ViEReceiver::~ViEReceiver() {
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000065 UpdateHistograms();
niklase@google.com470e71d2011-07-07 08:21:25 +000066}
67
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000068void ViEReceiver::UpdateHistograms() {
69 FecPacketCounter counter = fec_receiver_->GetPacketCounter();
70 if (counter.num_packets > 0) {
Peter Boströmae37abb2015-06-18 19:00:34 +020071 RTC_HISTOGRAM_PERCENTAGE(
72 "WebRTC.Video.ReceivedFecPacketsInPercent",
73 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000074 }
75 if (counter.num_fec_packets > 0) {
Peter Boströmae37abb2015-06-18 19:00:34 +020076 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
77 static_cast<int>(counter.num_recovered_packets *
78 100 / counter.num_fec_packets));
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000079 }
80}
81
wu@webrtc.org822fbd82013-08-15 23:38:54 +000082bool ViEReceiver::SetReceiveCodec(const VideoCodec& video_codec) {
83 int8_t old_pltype = -1;
84 if (rtp_payload_registry_->ReceivePayloadType(video_codec.plName,
85 kVideoPayloadTypeFrequency,
86 0,
87 video_codec.maxBitrate,
88 &old_pltype) != -1) {
89 rtp_payload_registry_->DeRegisterReceivePayload(old_pltype);
90 }
91
92 return RegisterPayload(video_codec);
93}
94
95bool ViEReceiver::RegisterPayload(const VideoCodec& video_codec) {
96 return rtp_receiver_->RegisterReceivePayload(video_codec.plName,
97 video_codec.plType,
98 kVideoPayloadTypeFrequency,
99 0,
100 video_codec.maxBitrate) == 0;
101}
102
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000103void ViEReceiver::SetNackStatus(bool enable,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000104 int max_nack_reordering_threshold) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000105 if (!enable) {
106 // Reset the threshold back to the lower default threshold when NACK is
107 // disabled since we no longer will be receiving retransmissions.
108 max_nack_reordering_threshold = kDefaultMaxReorderingThreshold;
109 }
110 rtp_receive_statistics_->SetMaxReorderingThreshold(
111 max_nack_reordering_threshold);
112 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000113}
114
Shao Changbine62202f2015-04-21 20:24:50 +0800115void ViEReceiver::SetRtxPayloadType(int payload_type,
116 int associated_payload_type) {
117 rtp_payload_registry_->SetRtxPayloadType(payload_type,
118 associated_payload_type);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000119}
120
noahric65220a72015-10-14 11:29:49 -0700121void ViEReceiver::SetUseRtxPayloadMappingOnRestore(bool val) {
122 rtp_payload_registry_->set_use_rtx_payload_mapping_on_restore(val);
123}
124
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000125void ViEReceiver::SetRtxSsrc(uint32_t ssrc) {
126 rtp_payload_registry_->SetRtxSsrc(ssrc);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000127}
128
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000129bool ViEReceiver::GetRtxSsrc(uint32_t* ssrc) const {
130 return rtp_payload_registry_->GetRtxSsrc(ssrc);
131}
132
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000133bool ViEReceiver::IsFecEnabled() const {
134 return rtp_payload_registry_->ulpfec_payload_type() > -1;
135}
136
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000137uint32_t ViEReceiver::GetRemoteSsrc() const {
138 return rtp_receiver_->SSRC();
139}
140
141int ViEReceiver::GetCsrcs(uint32_t* csrcs) const {
142 return rtp_receiver_->CSRCs(csrcs);
143}
144
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000145void ViEReceiver::SetRtpRtcpModule(RtpRtcp* module) {
146 rtp_rtcp_ = module;
147}
148
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000149RtpReceiver* ViEReceiver::GetRtpReceiver() const {
150 return rtp_receiver_.get();
151}
152
Peter Boströmd6f1a382015-07-14 16:08:02 +0200153void ViEReceiver::RegisterRtpRtcpModules(
154 const std::vector<RtpRtcp*>& rtp_modules) {
Peter Boström6cff9cf2015-04-21 13:47:17 +0200155 CriticalSectionScoped cs(receive_cs_.get());
Peter Boströmd6f1a382015-07-14 16:08:02 +0200156 // Only change the "simulcast" modules, the base module can be accessed
157 // without a lock whereas the simulcast modules require locking as they can be
158 // changed in runtime.
159 rtp_rtcp_simulcast_ =
160 std::vector<RtpRtcp*>(rtp_modules.begin() + 1, rtp_modules.end());
Peter Boström6cff9cf2015-04-21 13:47:17 +0200161}
162
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000163bool ViEReceiver::SetReceiveTimestampOffsetStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000164 if (enable) {
165 return rtp_header_parser_->RegisterRtpHeaderExtension(
166 kRtpExtensionTransmissionTimeOffset, id);
167 } else {
168 return rtp_header_parser_->DeregisterRtpHeaderExtension(
169 kRtpExtensionTransmissionTimeOffset);
170 }
171}
172
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000173bool ViEReceiver::SetReceiveAbsoluteSendTimeStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000174 if (enable) {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000175 if (rtp_header_parser_->RegisterRtpHeaderExtension(
176 kRtpExtensionAbsoluteSendTime, id)) {
177 receiving_ast_enabled_ = true;
178 return true;
179 } else {
180 return false;
181 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000182 } else {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000183 receiving_ast_enabled_ = false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000184 return rtp_header_parser_->DeregisterRtpHeaderExtension(
185 kRtpExtensionAbsoluteSendTime);
186 }
187}
188
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000189bool ViEReceiver::SetReceiveVideoRotationStatus(bool enable, int id) {
190 if (enable) {
191 if (rtp_header_parser_->RegisterRtpHeaderExtension(
192 kRtpExtensionVideoRotation, id)) {
193 receiving_cvo_enabled_ = true;
194 return true;
195 } else {
196 return false;
197 }
198 } else {
199 receiving_cvo_enabled_ = false;
200 return rtp_header_parser_->DeregisterRtpHeaderExtension(
201 kRtpExtensionVideoRotation);
202 }
203}
204
sprang867fb522015-08-03 04:38:41 -0700205bool ViEReceiver::SetReceiveTransportSequenceNumber(bool enable, int id) {
206 if (enable) {
207 if (rtp_header_parser_->RegisterRtpHeaderExtension(
208 kRtpExtensionTransportSequenceNumber, id)) {
209 receiving_tsn_enabled_ = true;
210 return true;
211 } else {
212 return false;
213 }
214 } else {
215 receiving_tsn_enabled_ = false;
216 return rtp_header_parser_->DeregisterRtpHeaderExtension(
217 kRtpExtensionTransportSequenceNumber);
218 }
219}
220
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000221int ViEReceiver::ReceivedRTPPacket(const void* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000222 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000223 const PacketTime& packet_time) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000224 return InsertRTPPacket(static_cast<const uint8_t*>(rtp_packet),
wu@webrtc.orga9890802013-12-13 00:21:03 +0000225 rtp_packet_length, packet_time);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000226}
227
228int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000229 size_t rtcp_packet_length) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000230 return InsertRTCPPacket(static_cast<const uint8_t*>(rtcp_packet),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000231 rtcp_packet_length);
232}
233
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000234int32_t ViEReceiver::OnReceivedPayloadData(const uint8_t* payload_data,
235 const size_t payload_size,
236 const WebRtcRTPHeader* rtp_header) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000237 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000238 rtp_header_with_ntp.ntp_time_ms =
239 ntp_estimator_->Estimate(rtp_header->header.timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000240 if (vcm_->IncomingPacket(payload_data,
241 payload_size,
242 rtp_header_with_ntp) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000243 // Check this...
244 return -1;
245 }
246 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000247}
248
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000249bool ViEReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000250 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000251 RTPHeader header;
252 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000253 return false;
254 }
255 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000256 bool in_order = IsPacketInOrder(header);
257 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000258}
259
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000260int ViEReceiver::InsertRTPPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000261 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000262 const PacketTime& packet_time) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000263 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000264 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000265 if (!receiving_) {
266 return -1;
267 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000268 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000269
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000270 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000271 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000272 &header)) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000273 return -1;
274 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000275 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000276 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000277 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000278 if (packet_time.timestamp != -1)
279 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
280 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000281 arrival_time_ms = now_ms;
282
283 {
284 // Periodically log the RTP header of incoming packets.
285 CriticalSectionScoped cs(receive_cs_.get());
286 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
287 std::stringstream ss;
288 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
289 << static_cast<int>(header.payloadType) << ", timestamp: "
290 << header.timestamp << ", sequence number: " << header.sequenceNumber
291 << ", arrival time: " << arrival_time_ms;
292 if (header.extension.hasTransmissionTimeOffset)
293 ss << ", toffset: " << header.extension.transmissionTimeOffset;
294 if (header.extension.hasAbsoluteSendTime)
295 ss << ", abs send time: " << header.extension.absoluteSendTime;
296 LOG(LS_INFO) << ss.str();
297 last_packet_log_ms_ = now_ms;
298 }
299 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000300
Stefan Holmerff4ea932015-06-18 16:01:33 +0200301 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_length,
302 header, true);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000303 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000304
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000305 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000306 rtp_payload_registry_->SetIncomingPayloadType(header);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000307 int ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order)
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000308 ? 0
309 : -1;
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000310 // Update receive statistics after ReceivePacket.
311 // Receive statistics will be reset if the payload type changes (make sure
312 // that the first packet is included in the stats).
313 rtp_receive_statistics_->IncomingPacket(
314 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
315 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000316}
317
318bool ViEReceiver::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000319 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000320 const RTPHeader& header,
321 bool in_order) {
322 if (rtp_payload_registry_->IsEncapsulated(header)) {
323 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
324 }
325 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000326 assert(packet_length >= header.headerLength);
327 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000328 PayloadUnion payload_specific;
329 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
330 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000331 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000332 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000333 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
334 payload_specific, in_order);
335}
336
337bool ViEReceiver::ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000338 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000339 const RTPHeader& header) {
340 if (rtp_payload_registry_->IsRed(header)) {
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000341 int8_t ulpfec_pt = rtp_payload_registry_->ulpfec_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000342 if (packet[header.headerLength] == ulpfec_pt) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000343 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000344 // Notify vcm about received FEC packets to avoid NACKing these packets.
345 NotifyReceiverOfFecPacket(header);
346 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000347 if (fec_receiver_->AddReceivedRedPacket(
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000348 header, packet, packet_length, ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000349 return false;
350 }
351 return fec_receiver_->ProcessReceivedFec() == 0;
352 } else if (rtp_payload_registry_->IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000353 if (header.headerLength + header.paddingLength == packet_length) {
354 // This is an empty packet and should be silently dropped before trying to
355 // parse the RTX header.
356 return true;
357 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000358 // Remove the RTX header and parse the original RTP header.
359 if (packet_length < header.headerLength)
360 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000361 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000362 return false;
363 CriticalSectionScoped cs(receive_cs_.get());
364 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000365 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000366 return false;
367 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000368 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -0700369 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
370 header)) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000371 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000372 return false;
373 }
374 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -0700375 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000376 restored_packet_in_use_ = false;
377 return ret;
378 }
379 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000380}
381
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000382void ViEReceiver::NotifyReceiverOfFecPacket(const RTPHeader& header) {
383 int8_t last_media_payload_type =
384 rtp_payload_registry_->last_received_media_payload_type();
385 if (last_media_payload_type < 0) {
386 LOG(LS_WARNING) << "Failed to get last media payload type.";
387 return;
388 }
389 // Fake an empty media packet.
390 WebRtcRTPHeader rtp_header = {};
391 rtp_header.header = header;
392 rtp_header.header.payloadType = last_media_payload_type;
393 rtp_header.header.paddingLength = 0;
394 PayloadUnion payload_specific;
395 if (!rtp_payload_registry_->GetPayloadSpecifics(last_media_payload_type,
396 &payload_specific)) {
397 LOG(LS_WARNING) << "Failed to get payload specifics.";
398 return;
399 }
400 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000401 rtp_header.type.Video.rotation = kVideoRotation_0;
402 if (header.extension.hasVideoRotation) {
403 rtp_header.type.Video.rotation =
404 ConvertCVOByteToVideoRotation(header.extension.videoRotation);
405 }
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000406 OnReceivedPayloadData(NULL, 0, &rtp_header);
407}
408
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000409int ViEReceiver::InsertRTCPPacket(const uint8_t* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000410 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000411 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000412 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000413 if (!receiving_) {
414 return -1;
415 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000416
Peter Boströmd6f1a382015-07-14 16:08:02 +0200417 for (RtpRtcp* rtp_rtcp : rtp_rtcp_simulcast_)
Peter Boström6cff9cf2015-04-21 13:47:17 +0200418 rtp_rtcp->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000419 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000420 assert(rtp_rtcp_); // Should be set by owner at construction time.
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000421 int ret = rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
422 if (ret != 0) {
423 return ret;
424 }
425
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000426 int64_t rtt = 0;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000427 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, NULL, NULL, NULL);
428 if (rtt == 0) {
429 // Waiting for valid rtt.
430 return 0;
431 }
432 uint32_t ntp_secs = 0;
433 uint32_t ntp_frac = 0;
434 uint32_t rtp_timestamp = 0;
435 if (0 != rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
436 &rtp_timestamp)) {
437 // Waiting for RTCP.
438 return 0;
439 }
440 ntp_estimator_->UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000441
442 return 0;
443}
444
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000445void ViEReceiver::StartReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000446 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000447 receiving_ = true;
448}
449
450void ViEReceiver::StopReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000451 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000452 receiving_ = false;
453}
454
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000455ReceiveStatistics* ViEReceiver::GetReceiveStatistics() const {
456 return rtp_receive_statistics_.get();
457}
458
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000459bool ViEReceiver::IsPacketInOrder(const RTPHeader& header) const {
460 StreamStatistician* statistician =
461 rtp_receive_statistics_->GetStatistician(header.ssrc);
462 if (!statistician)
463 return false;
464 return statistician->IsPacketInOrder(header.sequenceNumber);
465}
466
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000467bool ViEReceiver::IsPacketRetransmitted(const RTPHeader& header,
468 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000469 // Retransmissions are handled separately if RTX is enabled.
470 if (rtp_payload_registry_->RtxEnabled())
471 return false;
472 StreamStatistician* statistician =
473 rtp_receive_statistics_->GetStatistician(header.ssrc);
474 if (!statistician)
475 return false;
476 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000477 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000478 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000479 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000480 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000481}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000482} // namespace webrtc