blob: a1baf29849ff621bc57f3d0f2c90b0afd6140ead [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),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000061 last_packet_log_ms_(-1) {
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000062 assert(remote_bitrate_estimator);
niklase@google.com470e71d2011-07-07 08:21:25 +000063}
64
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000065ViEReceiver::~ViEReceiver() {
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000066 UpdateHistograms();
niklase@google.com470e71d2011-07-07 08:21:25 +000067}
68
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000069void ViEReceiver::UpdateHistograms() {
70 FecPacketCounter counter = fec_receiver_->GetPacketCounter();
71 if (counter.num_packets > 0) {
72 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.ReceivedFecPacketsInPercent",
73 counter.num_fec_packets * 100 / counter.num_packets);
74 }
75 if (counter.num_fec_packets > 0) {
76 RTC_HISTOGRAM_PERCENTAGE(
77 "WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
78 counter.num_recovered_packets * 100 / counter.num_fec_packets);
79 }
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öm6cff9cf2015-04-21 13:47:17 +0200149void ViEReceiver::RegisterSimulcastRtpRtcpModules(
150 const std::list<RtpRtcp*>& rtp_modules) {
151 CriticalSectionScoped cs(receive_cs_.get());
152 rtp_rtcp_simulcast_.clear();
153
154 if (!rtp_modules.empty()) {
155 rtp_rtcp_simulcast_.insert(rtp_rtcp_simulcast_.begin(),
156 rtp_modules.begin(),
157 rtp_modules.end());
158 }
159}
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
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000203int ViEReceiver::ReceivedRTPPacket(const void* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000204 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000205 const PacketTime& packet_time) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000206 return InsertRTPPacket(static_cast<const uint8_t*>(rtp_packet),
wu@webrtc.orga9890802013-12-13 00:21:03 +0000207 rtp_packet_length, packet_time);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000208}
209
210int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000211 size_t rtcp_packet_length) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000212 return InsertRTCPPacket(static_cast<const uint8_t*>(rtcp_packet),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000213 rtcp_packet_length);
214}
215
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000216int32_t ViEReceiver::OnReceivedPayloadData(const uint8_t* payload_data,
217 const size_t payload_size,
218 const WebRtcRTPHeader* rtp_header) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000219 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000220 rtp_header_with_ntp.ntp_time_ms =
221 ntp_estimator_->Estimate(rtp_header->header.timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000222 if (vcm_->IncomingPacket(payload_data,
223 payload_size,
224 rtp_header_with_ntp) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000225 // Check this...
226 return -1;
227 }
228 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000229}
230
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000231bool ViEReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000232 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000233 RTPHeader header;
234 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000235 return false;
236 }
237 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000238 bool in_order = IsPacketInOrder(header);
239 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000240}
241
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000242int ViEReceiver::InsertRTPPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000243 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000244 const PacketTime& packet_time) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000245 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000246 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000247 if (!receiving_) {
248 return -1;
249 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000250 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000251
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000252 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000253 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000254 &header)) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000255 return -1;
256 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000257 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000258 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000259 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000260 if (packet_time.timestamp != -1)
261 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
262 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000263 arrival_time_ms = now_ms;
264
265 {
266 // Periodically log the RTP header of incoming packets.
267 CriticalSectionScoped cs(receive_cs_.get());
268 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
269 std::stringstream ss;
270 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
271 << static_cast<int>(header.payloadType) << ", timestamp: "
272 << header.timestamp << ", sequence number: " << header.sequenceNumber
273 << ", arrival time: " << arrival_time_ms;
274 if (header.extension.hasTransmissionTimeOffset)
275 ss << ", toffset: " << header.extension.transmissionTimeOffset;
276 if (header.extension.hasAbsoluteSendTime)
277 ss << ", abs send time: " << header.extension.absoluteSendTime;
278 LOG(LS_INFO) << ss.str();
279 last_packet_log_ms_ = now_ms;
280 }
281 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000282
Stefan Holmerff4ea932015-06-18 16:01:33 +0200283 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_length,
284 header, true);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000285 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000286
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000287 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000288 rtp_payload_registry_->SetIncomingPayloadType(header);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000289 int ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order)
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000290 ? 0
291 : -1;
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000292 // Update receive statistics after ReceivePacket.
293 // Receive statistics will be reset if the payload type changes (make sure
294 // that the first packet is included in the stats).
295 rtp_receive_statistics_->IncomingPacket(
296 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
297 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000298}
299
300bool ViEReceiver::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000301 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000302 const RTPHeader& header,
303 bool in_order) {
304 if (rtp_payload_registry_->IsEncapsulated(header)) {
305 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
306 }
307 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000308 assert(packet_length >= header.headerLength);
309 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000310 PayloadUnion payload_specific;
311 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
312 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000313 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000314 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000315 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
316 payload_specific, in_order);
317}
318
319bool ViEReceiver::ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000320 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000321 const RTPHeader& header) {
322 if (rtp_payload_registry_->IsRed(header)) {
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000323 int8_t ulpfec_pt = rtp_payload_registry_->ulpfec_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000324 if (packet[header.headerLength] == ulpfec_pt) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000325 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000326 // Notify vcm about received FEC packets to avoid NACKing these packets.
327 NotifyReceiverOfFecPacket(header);
328 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000329 if (fec_receiver_->AddReceivedRedPacket(
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000330 header, packet, packet_length, ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000331 return false;
332 }
333 return fec_receiver_->ProcessReceivedFec() == 0;
334 } else if (rtp_payload_registry_->IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000335 if (header.headerLength + header.paddingLength == packet_length) {
336 // This is an empty packet and should be silently dropped before trying to
337 // parse the RTX header.
338 return true;
339 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000340 // Remove the RTX header and parse the original RTP header.
341 if (packet_length < header.headerLength)
342 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000343 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000344 return false;
345 CriticalSectionScoped cs(receive_cs_.get());
346 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000347 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000348 return false;
349 }
350 uint8_t* restored_packet_ptr = restored_packet_;
351 if (!rtp_payload_registry_->RestoreOriginalPacket(
352 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
353 header)) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000354 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000355 return false;
356 }
357 restored_packet_in_use_ = true;
358 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
359 restored_packet_in_use_ = false;
360 return ret;
361 }
362 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000363}
364
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000365void ViEReceiver::NotifyReceiverOfFecPacket(const RTPHeader& header) {
366 int8_t last_media_payload_type =
367 rtp_payload_registry_->last_received_media_payload_type();
368 if (last_media_payload_type < 0) {
369 LOG(LS_WARNING) << "Failed to get last media payload type.";
370 return;
371 }
372 // Fake an empty media packet.
373 WebRtcRTPHeader rtp_header = {};
374 rtp_header.header = header;
375 rtp_header.header.payloadType = last_media_payload_type;
376 rtp_header.header.paddingLength = 0;
377 PayloadUnion payload_specific;
378 if (!rtp_payload_registry_->GetPayloadSpecifics(last_media_payload_type,
379 &payload_specific)) {
380 LOG(LS_WARNING) << "Failed to get payload specifics.";
381 return;
382 }
383 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000384 rtp_header.type.Video.rotation = kVideoRotation_0;
385 if (header.extension.hasVideoRotation) {
386 rtp_header.type.Video.rotation =
387 ConvertCVOByteToVideoRotation(header.extension.videoRotation);
388 }
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000389 OnReceivedPayloadData(NULL, 0, &rtp_header);
390}
391
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000392int ViEReceiver::InsertRTCPPacket(const uint8_t* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000393 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000394 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000395 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000396 if (!receiving_) {
397 return -1;
398 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000399
Peter Boström6cff9cf2015-04-21 13:47:17 +0200400 std::list<RtpRtcp*>::iterator it = rtp_rtcp_simulcast_.begin();
401 while (it != rtp_rtcp_simulcast_.end()) {
402 RtpRtcp* rtp_rtcp = *it++;
403 rtp_rtcp->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
404 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000405 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000406 assert(rtp_rtcp_); // Should be set by owner at construction time.
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000407 int ret = rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
408 if (ret != 0) {
409 return ret;
410 }
411
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000412 int64_t rtt = 0;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000413 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, NULL, NULL, NULL);
414 if (rtt == 0) {
415 // Waiting for valid rtt.
416 return 0;
417 }
418 uint32_t ntp_secs = 0;
419 uint32_t ntp_frac = 0;
420 uint32_t rtp_timestamp = 0;
421 if (0 != rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
422 &rtp_timestamp)) {
423 // Waiting for RTCP.
424 return 0;
425 }
426 ntp_estimator_->UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000427
428 return 0;
429}
430
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000431void ViEReceiver::StartReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000432 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000433 receiving_ = true;
434}
435
436void ViEReceiver::StopReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000437 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000438 receiving_ = false;
439}
440
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000441ReceiveStatistics* ViEReceiver::GetReceiveStatistics() const {
442 return rtp_receive_statistics_.get();
443}
444
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000445bool ViEReceiver::IsPacketInOrder(const RTPHeader& header) const {
446 StreamStatistician* statistician =
447 rtp_receive_statistics_->GetStatistician(header.ssrc);
448 if (!statistician)
449 return false;
450 return statistician->IsPacketInOrder(header.sequenceNumber);
451}
452
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000453bool ViEReceiver::IsPacketRetransmitted(const RTPHeader& header,
454 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000455 // Retransmissions are handled separately if RTX is enabled.
456 if (rtp_payload_registry_->RtxEnabled())
457 return false;
458 StreamStatistician* statistician =
459 rtp_receive_statistics_->GetStatistician(header.ssrc);
460 if (!statistician)
461 return false;
462 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000463 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000464 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000465 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000466 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000467}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000468} // namespace webrtc