blob: f10f287017f5904cfc35ed757df4a99a14b1281c [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
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
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000121void ViEReceiver::SetRtxSsrc(uint32_t ssrc) {
122 rtp_payload_registry_->SetRtxSsrc(ssrc);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000123}
124
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000125bool ViEReceiver::GetRtxSsrc(uint32_t* ssrc) const {
126 return rtp_payload_registry_->GetRtxSsrc(ssrc);
127}
128
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000129bool ViEReceiver::IsFecEnabled() const {
130 return rtp_payload_registry_->ulpfec_payload_type() > -1;
131}
132
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000133uint32_t ViEReceiver::GetRemoteSsrc() const {
134 return rtp_receiver_->SSRC();
135}
136
137int ViEReceiver::GetCsrcs(uint32_t* csrcs) const {
138 return rtp_receiver_->CSRCs(csrcs);
139}
140
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000141void ViEReceiver::SetRtpRtcpModule(RtpRtcp* module) {
142 rtp_rtcp_ = module;
143}
144
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000145RtpReceiver* ViEReceiver::GetRtpReceiver() const {
146 return rtp_receiver_.get();
147}
148
Peter Boströmd6f1a382015-07-14 16:08:02 +0200149void ViEReceiver::RegisterRtpRtcpModules(
150 const std::vector<RtpRtcp*>& rtp_modules) {
Peter Boström6cff9cf2015-04-21 13:47:17 +0200151 CriticalSectionScoped cs(receive_cs_.get());
Peter Boströmd6f1a382015-07-14 16:08:02 +0200152 // Only change the "simulcast" modules, the base module can be accessed
153 // without a lock whereas the simulcast modules require locking as they can be
154 // changed in runtime.
155 rtp_rtcp_simulcast_ =
156 std::vector<RtpRtcp*>(rtp_modules.begin() + 1, rtp_modules.end());
Peter Boström6cff9cf2015-04-21 13:47:17 +0200157}
158
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000159bool ViEReceiver::SetReceiveTimestampOffsetStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000160 if (enable) {
161 return rtp_header_parser_->RegisterRtpHeaderExtension(
162 kRtpExtensionTransmissionTimeOffset, id);
163 } else {
164 return rtp_header_parser_->DeregisterRtpHeaderExtension(
165 kRtpExtensionTransmissionTimeOffset);
166 }
167}
168
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000169bool ViEReceiver::SetReceiveAbsoluteSendTimeStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000170 if (enable) {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000171 if (rtp_header_parser_->RegisterRtpHeaderExtension(
172 kRtpExtensionAbsoluteSendTime, id)) {
173 receiving_ast_enabled_ = true;
174 return true;
175 } else {
176 return false;
177 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000178 } else {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000179 receiving_ast_enabled_ = false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000180 return rtp_header_parser_->DeregisterRtpHeaderExtension(
181 kRtpExtensionAbsoluteSendTime);
182 }
183}
184
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000185bool ViEReceiver::SetReceiveVideoRotationStatus(bool enable, int id) {
186 if (enable) {
187 if (rtp_header_parser_->RegisterRtpHeaderExtension(
188 kRtpExtensionVideoRotation, id)) {
189 receiving_cvo_enabled_ = true;
190 return true;
191 } else {
192 return false;
193 }
194 } else {
195 receiving_cvo_enabled_ = false;
196 return rtp_header_parser_->DeregisterRtpHeaderExtension(
197 kRtpExtensionVideoRotation);
198 }
199}
200
sprang867fb522015-08-03 04:38:41 -0700201bool ViEReceiver::SetReceiveTransportSequenceNumber(bool enable, int id) {
202 if (enable) {
203 if (rtp_header_parser_->RegisterRtpHeaderExtension(
204 kRtpExtensionTransportSequenceNumber, id)) {
205 receiving_tsn_enabled_ = true;
206 return true;
207 } else {
208 return false;
209 }
210 } else {
211 receiving_tsn_enabled_ = false;
212 return rtp_header_parser_->DeregisterRtpHeaderExtension(
213 kRtpExtensionTransportSequenceNumber);
214 }
215}
216
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000217int ViEReceiver::ReceivedRTPPacket(const void* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000218 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000219 const PacketTime& packet_time) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000220 return InsertRTPPacket(static_cast<const uint8_t*>(rtp_packet),
wu@webrtc.orga9890802013-12-13 00:21:03 +0000221 rtp_packet_length, packet_time);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000222}
223
224int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000225 size_t rtcp_packet_length) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000226 return InsertRTCPPacket(static_cast<const uint8_t*>(rtcp_packet),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000227 rtcp_packet_length);
228}
229
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000230int32_t ViEReceiver::OnReceivedPayloadData(const uint8_t* payload_data,
231 const size_t payload_size,
232 const WebRtcRTPHeader* rtp_header) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000233 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000234 rtp_header_with_ntp.ntp_time_ms =
235 ntp_estimator_->Estimate(rtp_header->header.timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000236 if (vcm_->IncomingPacket(payload_data,
237 payload_size,
238 rtp_header_with_ntp) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000239 // Check this...
240 return -1;
241 }
242 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000243}
244
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000245bool ViEReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000246 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000247 RTPHeader header;
248 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000249 return false;
250 }
251 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000252 bool in_order = IsPacketInOrder(header);
253 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000254}
255
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000256int ViEReceiver::InsertRTPPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000257 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000258 const PacketTime& packet_time) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000259 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000260 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000261 if (!receiving_) {
262 return -1;
263 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000264 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000265
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000266 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000267 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000268 &header)) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000269 return -1;
270 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000271 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000272 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000273 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000274 if (packet_time.timestamp != -1)
275 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
276 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000277 arrival_time_ms = now_ms;
278
279 {
280 // Periodically log the RTP header of incoming packets.
281 CriticalSectionScoped cs(receive_cs_.get());
282 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
283 std::stringstream ss;
284 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
285 << static_cast<int>(header.payloadType) << ", timestamp: "
286 << header.timestamp << ", sequence number: " << header.sequenceNumber
287 << ", arrival time: " << arrival_time_ms;
288 if (header.extension.hasTransmissionTimeOffset)
289 ss << ", toffset: " << header.extension.transmissionTimeOffset;
290 if (header.extension.hasAbsoluteSendTime)
291 ss << ", abs send time: " << header.extension.absoluteSendTime;
292 LOG(LS_INFO) << ss.str();
293 last_packet_log_ms_ = now_ms;
294 }
295 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000296
Stefan Holmerff4ea932015-06-18 16:01:33 +0200297 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_length,
298 header, true);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000299 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000300
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000301 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000302 rtp_payload_registry_->SetIncomingPayloadType(header);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000303 int ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order)
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000304 ? 0
305 : -1;
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000306 // Update receive statistics after ReceivePacket.
307 // Receive statistics will be reset if the payload type changes (make sure
308 // that the first packet is included in the stats).
309 rtp_receive_statistics_->IncomingPacket(
310 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
311 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000312}
313
314bool ViEReceiver::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000315 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000316 const RTPHeader& header,
317 bool in_order) {
318 if (rtp_payload_registry_->IsEncapsulated(header)) {
319 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
320 }
321 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000322 assert(packet_length >= header.headerLength);
323 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000324 PayloadUnion payload_specific;
325 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
326 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000327 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000328 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000329 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
330 payload_specific, in_order);
331}
332
333bool ViEReceiver::ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000334 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000335 const RTPHeader& header) {
336 if (rtp_payload_registry_->IsRed(header)) {
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000337 int8_t ulpfec_pt = rtp_payload_registry_->ulpfec_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000338 if (packet[header.headerLength] == ulpfec_pt) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000339 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000340 // Notify vcm about received FEC packets to avoid NACKing these packets.
341 NotifyReceiverOfFecPacket(header);
342 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000343 if (fec_receiver_->AddReceivedRedPacket(
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000344 header, packet, packet_length, ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000345 return false;
346 }
347 return fec_receiver_->ProcessReceivedFec() == 0;
348 } else if (rtp_payload_registry_->IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000349 if (header.headerLength + header.paddingLength == packet_length) {
350 // This is an empty packet and should be silently dropped before trying to
351 // parse the RTX header.
352 return true;
353 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000354 // Remove the RTX header and parse the original RTP header.
355 if (packet_length < header.headerLength)
356 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000357 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000358 return false;
359 CriticalSectionScoped cs(receive_cs_.get());
360 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000361 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000362 return false;
363 }
364 uint8_t* restored_packet_ptr = restored_packet_;
365 if (!rtp_payload_registry_->RestoreOriginalPacket(
366 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
367 header)) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000368 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000369 return false;
370 }
371 restored_packet_in_use_ = true;
372 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
373 restored_packet_in_use_ = false;
374 return ret;
375 }
376 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000377}
378
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000379void ViEReceiver::NotifyReceiverOfFecPacket(const RTPHeader& header) {
380 int8_t last_media_payload_type =
381 rtp_payload_registry_->last_received_media_payload_type();
382 if (last_media_payload_type < 0) {
383 LOG(LS_WARNING) << "Failed to get last media payload type.";
384 return;
385 }
386 // Fake an empty media packet.
387 WebRtcRTPHeader rtp_header = {};
388 rtp_header.header = header;
389 rtp_header.header.payloadType = last_media_payload_type;
390 rtp_header.header.paddingLength = 0;
391 PayloadUnion payload_specific;
392 if (!rtp_payload_registry_->GetPayloadSpecifics(last_media_payload_type,
393 &payload_specific)) {
394 LOG(LS_WARNING) << "Failed to get payload specifics.";
395 return;
396 }
397 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000398 rtp_header.type.Video.rotation = kVideoRotation_0;
399 if (header.extension.hasVideoRotation) {
400 rtp_header.type.Video.rotation =
401 ConvertCVOByteToVideoRotation(header.extension.videoRotation);
402 }
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000403 OnReceivedPayloadData(NULL, 0, &rtp_header);
404}
405
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000406int ViEReceiver::InsertRTCPPacket(const uint8_t* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000407 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000408 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000409 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000410 if (!receiving_) {
411 return -1;
412 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000413
Peter Boströmd6f1a382015-07-14 16:08:02 +0200414 for (RtpRtcp* rtp_rtcp : rtp_rtcp_simulcast_)
Peter Boström6cff9cf2015-04-21 13:47:17 +0200415 rtp_rtcp->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000416 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000417 assert(rtp_rtcp_); // Should be set by owner at construction time.
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000418 int ret = rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
419 if (ret != 0) {
420 return ret;
421 }
422
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000423 int64_t rtt = 0;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000424 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, NULL, NULL, NULL);
425 if (rtt == 0) {
426 // Waiting for valid rtt.
427 return 0;
428 }
429 uint32_t ntp_secs = 0;
430 uint32_t ntp_frac = 0;
431 uint32_t rtp_timestamp = 0;
432 if (0 != rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
433 &rtp_timestamp)) {
434 // Waiting for RTCP.
435 return 0;
436 }
437 ntp_estimator_->UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000438
439 return 0;
440}
441
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000442void ViEReceiver::StartReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000443 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000444 receiving_ = true;
445}
446
447void ViEReceiver::StopReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000448 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000449 receiving_ = false;
450}
451
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000452ReceiveStatistics* ViEReceiver::GetReceiveStatistics() const {
453 return rtp_receive_statistics_.get();
454}
455
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000456bool ViEReceiver::IsPacketInOrder(const RTPHeader& header) const {
457 StreamStatistician* statistician =
458 rtp_receive_statistics_->GetStatistician(header.ssrc);
459 if (!statistician)
460 return false;
461 return statistician->IsPacketInOrder(header.sequenceNumber);
462}
463
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000464bool ViEReceiver::IsPacketRetransmitted(const RTPHeader& header,
465 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000466 // Retransmissions are handled separately if RTX is enabled.
467 if (rtp_payload_registry_->RtxEnabled())
468 return false;
469 StreamStatistician* statistician =
470 rtp_receive_statistics_->GetStatistician(header.ssrc);
471 if (!statistician)
472 return false;
473 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000474 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000475 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000476 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000477 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000478}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000479} // namespace webrtc