blob: a3ef2cfc3c5aeb61f9c1a17983300212e56c6634 [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
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000015#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000016#include "webrtc/modules/rtp_rtcp/interface/fec_receiver.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000017#include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h"
wu@webrtc.org88abf112014-05-14 16:53:51 +000018#include "webrtc/modules/rtp_rtcp/interface/remote_ntp_time_estimator.h"
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +000019#include "webrtc/modules/rtp_rtcp/interface/rtp_cvo.h"
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000020#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000021#include "webrtc/modules/rtp_rtcp/interface/rtp_payload_registry.h"
22#include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000023#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000024#include "webrtc/modules/video_coding/main/interface/video_coding.h"
25#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
wu@webrtc.orgcd701192014-04-24 22:10:24 +000026#include "webrtc/system_wrappers/interface/logging.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
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000036ViEReceiver::ViEReceiver(const int32_t channel_id,
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000037 VideoCodingModule* module_vcm,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000038 RemoteBitrateEstimator* remote_bitrate_estimator,
39 RtpFeedback* rtp_feedback)
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +000040 : receive_cs_(CriticalSectionWrapper::CreateCriticalSection()),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000041 clock_(Clock::GetRealTimeClock()),
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000042 rtp_header_parser_(RtpHeaderParser::Create()),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000043 rtp_payload_registry_(
44 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(false))),
45 rtp_receiver_(
46 RtpReceiver::CreateVideoReceiver(channel_id,
47 clock_,
48 this,
49 rtp_feedback,
50 rtp_payload_registry_.get())),
51 rtp_receive_statistics_(ReceiveStatistics::Create(clock_)),
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +000052 fec_receiver_(FecReceiver::Create(this)),
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000053 rtp_rtcp_(NULL),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000054 vcm_(module_vcm),
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000055 remote_bitrate_estimator_(remote_bitrate_estimator),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000056 ntp_estimator_(new RemoteNtpTimeEstimator(clock_)),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000057 receiving_(false),
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +000058 restored_packet_in_use_(false),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000059 receiving_ast_enabled_(false),
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +000060 receiving_cvo_enabled_(false),
sprang867fb522015-08-03 04:38:41 -070061 receiving_tsn_enabled_(false),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000062 last_packet_log_ms_(-1) {
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000063 assert(remote_bitrate_estimator);
niklase@google.com470e71d2011-07-07 08:21:25 +000064}
65
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000066ViEReceiver::~ViEReceiver() {
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000067 UpdateHistograms();
niklase@google.com470e71d2011-07-07 08:21:25 +000068}
69
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000070void ViEReceiver::UpdateHistograms() {
71 FecPacketCounter counter = fec_receiver_->GetPacketCounter();
72 if (counter.num_packets > 0) {
Peter Boströmae37abb2015-06-18 19:00:34 +020073 RTC_HISTOGRAM_PERCENTAGE(
74 "WebRTC.Video.ReceivedFecPacketsInPercent",
75 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000076 }
77 if (counter.num_fec_packets > 0) {
Peter Boströmae37abb2015-06-18 19:00:34 +020078 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
79 static_cast<int>(counter.num_recovered_packets *
80 100 / counter.num_fec_packets));
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000081 }
82}
83
wu@webrtc.org822fbd82013-08-15 23:38:54 +000084bool ViEReceiver::SetReceiveCodec(const VideoCodec& video_codec) {
85 int8_t old_pltype = -1;
86 if (rtp_payload_registry_->ReceivePayloadType(video_codec.plName,
87 kVideoPayloadTypeFrequency,
88 0,
89 video_codec.maxBitrate,
90 &old_pltype) != -1) {
91 rtp_payload_registry_->DeRegisterReceivePayload(old_pltype);
92 }
93
94 return RegisterPayload(video_codec);
95}
96
97bool ViEReceiver::RegisterPayload(const VideoCodec& video_codec) {
98 return rtp_receiver_->RegisterReceivePayload(video_codec.plName,
99 video_codec.plType,
100 kVideoPayloadTypeFrequency,
101 0,
102 video_codec.maxBitrate) == 0;
103}
104
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000105void ViEReceiver::SetNackStatus(bool enable,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000106 int max_nack_reordering_threshold) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000107 if (!enable) {
108 // Reset the threshold back to the lower default threshold when NACK is
109 // disabled since we no longer will be receiving retransmissions.
110 max_nack_reordering_threshold = kDefaultMaxReorderingThreshold;
111 }
112 rtp_receive_statistics_->SetMaxReorderingThreshold(
113 max_nack_reordering_threshold);
114 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000115}
116
Shao Changbine62202f2015-04-21 20:24:50 +0800117void ViEReceiver::SetRtxPayloadType(int payload_type,
118 int associated_payload_type) {
119 rtp_payload_registry_->SetRtxPayloadType(payload_type,
120 associated_payload_type);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000121}
122
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000123void ViEReceiver::SetRtxSsrc(uint32_t ssrc) {
124 rtp_payload_registry_->SetRtxSsrc(ssrc);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000125}
126
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000127bool ViEReceiver::GetRtxSsrc(uint32_t* ssrc) const {
128 return rtp_payload_registry_->GetRtxSsrc(ssrc);
129}
130
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000131bool ViEReceiver::IsFecEnabled() const {
132 return rtp_payload_registry_->ulpfec_payload_type() > -1;
133}
134
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000135uint32_t ViEReceiver::GetRemoteSsrc() const {
136 return rtp_receiver_->SSRC();
137}
138
139int ViEReceiver::GetCsrcs(uint32_t* csrcs) const {
140 return rtp_receiver_->CSRCs(csrcs);
141}
142
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000143void ViEReceiver::SetRtpRtcpModule(RtpRtcp* module) {
144 rtp_rtcp_ = module;
145}
146
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000147RtpReceiver* ViEReceiver::GetRtpReceiver() const {
148 return rtp_receiver_.get();
149}
150
Peter Boströmd6f1a382015-07-14 16:08:02 +0200151void ViEReceiver::RegisterRtpRtcpModules(
152 const std::vector<RtpRtcp*>& rtp_modules) {
Peter Boström6cff9cf2015-04-21 13:47:17 +0200153 CriticalSectionScoped cs(receive_cs_.get());
Peter Boströmd6f1a382015-07-14 16:08:02 +0200154 // Only change the "simulcast" modules, the base module can be accessed
155 // without a lock whereas the simulcast modules require locking as they can be
156 // changed in runtime.
157 rtp_rtcp_simulcast_ =
158 std::vector<RtpRtcp*>(rtp_modules.begin() + 1, rtp_modules.end());
Peter Boström6cff9cf2015-04-21 13:47:17 +0200159}
160
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000161bool ViEReceiver::SetReceiveTimestampOffsetStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000162 if (enable) {
163 return rtp_header_parser_->RegisterRtpHeaderExtension(
164 kRtpExtensionTransmissionTimeOffset, id);
165 } else {
166 return rtp_header_parser_->DeregisterRtpHeaderExtension(
167 kRtpExtensionTransmissionTimeOffset);
168 }
169}
170
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000171bool ViEReceiver::SetReceiveAbsoluteSendTimeStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000172 if (enable) {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000173 if (rtp_header_parser_->RegisterRtpHeaderExtension(
174 kRtpExtensionAbsoluteSendTime, id)) {
175 receiving_ast_enabled_ = true;
176 return true;
177 } else {
178 return false;
179 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000180 } else {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000181 receiving_ast_enabled_ = false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000182 return rtp_header_parser_->DeregisterRtpHeaderExtension(
183 kRtpExtensionAbsoluteSendTime);
184 }
185}
186
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000187bool ViEReceiver::SetReceiveVideoRotationStatus(bool enable, int id) {
188 if (enable) {
189 if (rtp_header_parser_->RegisterRtpHeaderExtension(
190 kRtpExtensionVideoRotation, id)) {
191 receiving_cvo_enabled_ = true;
192 return true;
193 } else {
194 return false;
195 }
196 } else {
197 receiving_cvo_enabled_ = false;
198 return rtp_header_parser_->DeregisterRtpHeaderExtension(
199 kRtpExtensionVideoRotation);
200 }
201}
202
sprang867fb522015-08-03 04:38:41 -0700203bool ViEReceiver::SetReceiveTransportSequenceNumber(bool enable, int id) {
204 if (enable) {
205 if (rtp_header_parser_->RegisterRtpHeaderExtension(
206 kRtpExtensionTransportSequenceNumber, id)) {
207 receiving_tsn_enabled_ = true;
208 return true;
209 } else {
210 return false;
211 }
212 } else {
213 receiving_tsn_enabled_ = false;
214 return rtp_header_parser_->DeregisterRtpHeaderExtension(
215 kRtpExtensionTransportSequenceNumber);
216 }
217}
218
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000219int ViEReceiver::ReceivedRTPPacket(const void* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000220 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000221 const PacketTime& packet_time) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000222 return InsertRTPPacket(static_cast<const uint8_t*>(rtp_packet),
wu@webrtc.orga9890802013-12-13 00:21:03 +0000223 rtp_packet_length, packet_time);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000224}
225
226int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000227 size_t rtcp_packet_length) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000228 return InsertRTCPPacket(static_cast<const uint8_t*>(rtcp_packet),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000229 rtcp_packet_length);
230}
231
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000232int32_t ViEReceiver::OnReceivedPayloadData(const uint8_t* payload_data,
233 const size_t payload_size,
234 const WebRtcRTPHeader* rtp_header) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000235 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000236 rtp_header_with_ntp.ntp_time_ms =
237 ntp_estimator_->Estimate(rtp_header->header.timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000238 if (vcm_->IncomingPacket(payload_data,
239 payload_size,
240 rtp_header_with_ntp) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000241 // Check this...
242 return -1;
243 }
244 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000245}
246
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000247bool ViEReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000248 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000249 RTPHeader header;
250 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000251 return false;
252 }
253 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000254 bool in_order = IsPacketInOrder(header);
255 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000256}
257
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000258int ViEReceiver::InsertRTPPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000259 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000260 const PacketTime& packet_time) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000261 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000262 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000263 if (!receiving_) {
264 return -1;
265 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000266 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000267
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000268 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000269 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000270 &header)) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000271 return -1;
272 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000273 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000274 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000275 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000276 if (packet_time.timestamp != -1)
277 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
278 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000279 arrival_time_ms = now_ms;
280
281 {
282 // Periodically log the RTP header of incoming packets.
283 CriticalSectionScoped cs(receive_cs_.get());
284 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
285 std::stringstream ss;
286 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
287 << static_cast<int>(header.payloadType) << ", timestamp: "
288 << header.timestamp << ", sequence number: " << header.sequenceNumber
289 << ", arrival time: " << arrival_time_ms;
290 if (header.extension.hasTransmissionTimeOffset)
291 ss << ", toffset: " << header.extension.transmissionTimeOffset;
292 if (header.extension.hasAbsoluteSendTime)
293 ss << ", abs send time: " << header.extension.absoluteSendTime;
294 LOG(LS_INFO) << ss.str();
295 last_packet_log_ms_ = now_ms;
296 }
297 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000298
Stefan Holmerff4ea932015-06-18 16:01:33 +0200299 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_length,
300 header, true);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000301 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000302
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000303 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000304 rtp_payload_registry_->SetIncomingPayloadType(header);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000305 int ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order)
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000306 ? 0
307 : -1;
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000308 // Update receive statistics after ReceivePacket.
309 // Receive statistics will be reset if the payload type changes (make sure
310 // that the first packet is included in the stats).
311 rtp_receive_statistics_->IncomingPacket(
312 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
313 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000314}
315
316bool ViEReceiver::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000317 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000318 const RTPHeader& header,
319 bool in_order) {
320 if (rtp_payload_registry_->IsEncapsulated(header)) {
321 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
322 }
323 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000324 assert(packet_length >= header.headerLength);
325 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000326 PayloadUnion payload_specific;
327 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
328 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000329 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000330 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000331 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
332 payload_specific, in_order);
333}
334
335bool ViEReceiver::ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000336 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000337 const RTPHeader& header) {
338 if (rtp_payload_registry_->IsRed(header)) {
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000339 int8_t ulpfec_pt = rtp_payload_registry_->ulpfec_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000340 if (packet[header.headerLength] == ulpfec_pt) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000341 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000342 // Notify vcm about received FEC packets to avoid NACKing these packets.
343 NotifyReceiverOfFecPacket(header);
344 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000345 if (fec_receiver_->AddReceivedRedPacket(
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000346 header, packet, packet_length, ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000347 return false;
348 }
349 return fec_receiver_->ProcessReceivedFec() == 0;
350 } else if (rtp_payload_registry_->IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000351 if (header.headerLength + header.paddingLength == packet_length) {
352 // This is an empty packet and should be silently dropped before trying to
353 // parse the RTX header.
354 return true;
355 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000356 // Remove the RTX header and parse the original RTP header.
357 if (packet_length < header.headerLength)
358 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000359 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000360 return false;
361 CriticalSectionScoped cs(receive_cs_.get());
362 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000363 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000364 return false;
365 }
366 uint8_t* restored_packet_ptr = restored_packet_;
367 if (!rtp_payload_registry_->RestoreOriginalPacket(
368 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
369 header)) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000370 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000371 return false;
372 }
373 restored_packet_in_use_ = true;
374 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
375 restored_packet_in_use_ = false;
376 return ret;
377 }
378 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000379}
380
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000381void ViEReceiver::NotifyReceiverOfFecPacket(const RTPHeader& header) {
382 int8_t last_media_payload_type =
383 rtp_payload_registry_->last_received_media_payload_type();
384 if (last_media_payload_type < 0) {
385 LOG(LS_WARNING) << "Failed to get last media payload type.";
386 return;
387 }
388 // Fake an empty media packet.
389 WebRtcRTPHeader rtp_header = {};
390 rtp_header.header = header;
391 rtp_header.header.payloadType = last_media_payload_type;
392 rtp_header.header.paddingLength = 0;
393 PayloadUnion payload_specific;
394 if (!rtp_payload_registry_->GetPayloadSpecifics(last_media_payload_type,
395 &payload_specific)) {
396 LOG(LS_WARNING) << "Failed to get payload specifics.";
397 return;
398 }
399 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000400 rtp_header.type.Video.rotation = kVideoRotation_0;
401 if (header.extension.hasVideoRotation) {
402 rtp_header.type.Video.rotation =
403 ConvertCVOByteToVideoRotation(header.extension.videoRotation);
404 }
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000405 OnReceivedPayloadData(NULL, 0, &rtp_header);
406}
407
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000408int ViEReceiver::InsertRTCPPacket(const uint8_t* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000409 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000410 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000411 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000412 if (!receiving_) {
413 return -1;
414 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000415
Peter Boströmd6f1a382015-07-14 16:08:02 +0200416 for (RtpRtcp* rtp_rtcp : rtp_rtcp_simulcast_)
Peter Boström6cff9cf2015-04-21 13:47:17 +0200417 rtp_rtcp->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000418 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000419 assert(rtp_rtcp_); // Should be set by owner at construction time.
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000420 int ret = rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
421 if (ret != 0) {
422 return ret;
423 }
424
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000425 int64_t rtt = 0;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000426 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, NULL, NULL, NULL);
427 if (rtt == 0) {
428 // Waiting for valid rtt.
429 return 0;
430 }
431 uint32_t ntp_secs = 0;
432 uint32_t ntp_frac = 0;
433 uint32_t rtp_timestamp = 0;
434 if (0 != rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
435 &rtp_timestamp)) {
436 // Waiting for RTCP.
437 return 0;
438 }
439 ntp_estimator_->UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000440
441 return 0;
442}
443
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000444void ViEReceiver::StartReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000445 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000446 receiving_ = true;
447}
448
449void ViEReceiver::StopReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000450 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000451 receiving_ = false;
452}
453
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000454ReceiveStatistics* ViEReceiver::GetReceiveStatistics() const {
455 return rtp_receive_statistics_.get();
456}
457
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000458bool ViEReceiver::IsPacketInOrder(const RTPHeader& header) const {
459 StreamStatistician* statistician =
460 rtp_receive_statistics_->GetStatistician(header.ssrc);
461 if (!statistician)
462 return false;
463 return statistician->IsPacketInOrder(header.sequenceNumber);
464}
465
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000466bool ViEReceiver::IsPacketRetransmitted(const RTPHeader& header,
467 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000468 // Retransmissions are handled separately if RTX is enabled.
469 if (rtp_payload_registry_->RtxEnabled())
470 return false;
471 StreamStatistician* statistician =
472 rtp_receive_statistics_->GetStatistician(header.ssrc);
473 if (!statistician)
474 return false;
475 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000476 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000477 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000478 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000479 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000480}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000481} // namespace webrtc