blob: 0265ee6cef4ed638372e832137cf29562906df1a [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"
24#include "webrtc/modules/utility/interface/rtp_dump.h"
25#include "webrtc/modules/video_coding/main/interface/video_coding.h"
26#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
wu@webrtc.orgcd701192014-04-24 22:10:24 +000027#include "webrtc/system_wrappers/interface/logging.h"
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000028#include "webrtc/system_wrappers/interface/metrics.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000029#include "webrtc/system_wrappers/interface/tick_util.h"
wu@webrtc.org66773a02014-05-07 17:09:44 +000030#include "webrtc/system_wrappers/interface/timestamp_extrapolator.h"
wu@webrtc.org88abf112014-05-14 16:53:51 +000031#include "webrtc/system_wrappers/interface/trace.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000032
33namespace webrtc {
34
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000035static const int kPacketLogIntervalMs = 10000;
36
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000037ViEReceiver::ViEReceiver(const int32_t channel_id,
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000038 VideoCodingModule* module_vcm,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000039 RemoteBitrateEstimator* remote_bitrate_estimator,
40 RtpFeedback* rtp_feedback)
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +000041 : receive_cs_(CriticalSectionWrapper::CreateCriticalSection()),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000042 clock_(Clock::GetRealTimeClock()),
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000043 rtp_header_parser_(RtpHeaderParser::Create()),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000044 rtp_payload_registry_(
45 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(false))),
46 rtp_receiver_(
47 RtpReceiver::CreateVideoReceiver(channel_id,
48 clock_,
49 this,
50 rtp_feedback,
51 rtp_payload_registry_.get())),
52 rtp_receive_statistics_(ReceiveStatistics::Create(clock_)),
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +000053 fec_receiver_(FecReceiver::Create(this)),
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000054 rtp_rtcp_(NULL),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000055 vcm_(module_vcm),
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000056 remote_bitrate_estimator_(remote_bitrate_estimator),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000057 ntp_estimator_(new RemoteNtpTimeEstimator(clock_)),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000058 rtp_dump_(NULL),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000059 receiving_(false),
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +000060 restored_packet_in_use_(false),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000061 receiving_ast_enabled_(false),
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +000062 receiving_cvo_enabled_(false),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000063 last_packet_log_ms_(-1) {
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000064 assert(remote_bitrate_estimator);
niklase@google.com470e71d2011-07-07 08:21:25 +000065}
66
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000067ViEReceiver::~ViEReceiver() {
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000068 UpdateHistograms();
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000069 if (rtp_dump_) {
70 rtp_dump_->Stop();
71 RtpDump::DestroyRtpDump(rtp_dump_);
72 rtp_dump_ = NULL;
73 }
niklase@google.com470e71d2011-07-07 08:21:25 +000074}
75
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000076void ViEReceiver::UpdateHistograms() {
77 FecPacketCounter counter = fec_receiver_->GetPacketCounter();
78 if (counter.num_packets > 0) {
79 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.ReceivedFecPacketsInPercent",
80 counter.num_fec_packets * 100 / counter.num_packets);
81 }
82 if (counter.num_fec_packets > 0) {
83 RTC_HISTOGRAM_PERCENTAGE(
84 "WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
85 counter.num_recovered_packets * 100 / counter.num_fec_packets);
86 }
87}
88
wu@webrtc.org822fbd82013-08-15 23:38:54 +000089bool ViEReceiver::SetReceiveCodec(const VideoCodec& video_codec) {
90 int8_t old_pltype = -1;
91 if (rtp_payload_registry_->ReceivePayloadType(video_codec.plName,
92 kVideoPayloadTypeFrequency,
93 0,
94 video_codec.maxBitrate,
95 &old_pltype) != -1) {
96 rtp_payload_registry_->DeRegisterReceivePayload(old_pltype);
97 }
98
99 return RegisterPayload(video_codec);
100}
101
102bool ViEReceiver::RegisterPayload(const VideoCodec& video_codec) {
103 return rtp_receiver_->RegisterReceivePayload(video_codec.plName,
104 video_codec.plType,
105 kVideoPayloadTypeFrequency,
106 0,
107 video_codec.maxBitrate) == 0;
108}
109
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000110void ViEReceiver::SetNackStatus(bool enable,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000111 int max_nack_reordering_threshold) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000112 if (!enable) {
113 // Reset the threshold back to the lower default threshold when NACK is
114 // disabled since we no longer will be receiving retransmissions.
115 max_nack_reordering_threshold = kDefaultMaxReorderingThreshold;
116 }
117 rtp_receive_statistics_->SetMaxReorderingThreshold(
118 max_nack_reordering_threshold);
119 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000120}
121
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000122void ViEReceiver::SetRtxPayloadType(int payload_type) {
123 rtp_payload_registry_->SetRtxPayloadType(payload_type);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000124}
125
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000126void ViEReceiver::SetRtxSsrc(uint32_t ssrc) {
127 rtp_payload_registry_->SetRtxSsrc(ssrc);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000128}
129
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000130bool ViEReceiver::GetRtxSsrc(uint32_t* ssrc) const {
131 return rtp_payload_registry_->GetRtxSsrc(ssrc);
132}
133
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000134bool ViEReceiver::IsFecEnabled() const {
135 return rtp_payload_registry_->ulpfec_payload_type() > -1;
136}
137
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000138uint32_t ViEReceiver::GetRemoteSsrc() const {
139 return rtp_receiver_->SSRC();
140}
141
142int ViEReceiver::GetCsrcs(uint32_t* csrcs) const {
143 return rtp_receiver_->CSRCs(csrcs);
144}
145
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000146void ViEReceiver::SetRtpRtcpModule(RtpRtcp* module) {
147 rtp_rtcp_ = module;
148}
149
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000150RtpReceiver* ViEReceiver::GetRtpReceiver() const {
151 return rtp_receiver_.get();
152}
153
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000154bool ViEReceiver::SetReceiveTimestampOffsetStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000155 if (enable) {
156 return rtp_header_parser_->RegisterRtpHeaderExtension(
157 kRtpExtensionTransmissionTimeOffset, id);
158 } else {
159 return rtp_header_parser_->DeregisterRtpHeaderExtension(
160 kRtpExtensionTransmissionTimeOffset);
161 }
162}
163
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000164bool ViEReceiver::SetReceiveAbsoluteSendTimeStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000165 if (enable) {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000166 if (rtp_header_parser_->RegisterRtpHeaderExtension(
167 kRtpExtensionAbsoluteSendTime, id)) {
168 receiving_ast_enabled_ = true;
169 return true;
170 } else {
171 return false;
172 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000173 } else {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000174 receiving_ast_enabled_ = false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000175 return rtp_header_parser_->DeregisterRtpHeaderExtension(
176 kRtpExtensionAbsoluteSendTime);
177 }
178}
179
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000180bool ViEReceiver::SetReceiveVideoRotationStatus(bool enable, int id) {
181 if (enable) {
182 if (rtp_header_parser_->RegisterRtpHeaderExtension(
183 kRtpExtensionVideoRotation, id)) {
184 receiving_cvo_enabled_ = true;
185 return true;
186 } else {
187 return false;
188 }
189 } else {
190 receiving_cvo_enabled_ = false;
191 return rtp_header_parser_->DeregisterRtpHeaderExtension(
192 kRtpExtensionVideoRotation);
193 }
194}
195
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000196int ViEReceiver::ReceivedRTPPacket(const void* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000197 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000198 const PacketTime& packet_time) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000199 return InsertRTPPacket(static_cast<const uint8_t*>(rtp_packet),
wu@webrtc.orga9890802013-12-13 00:21:03 +0000200 rtp_packet_length, packet_time);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000201}
202
203int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000204 size_t rtcp_packet_length) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000205 return InsertRTCPPacket(static_cast<const uint8_t*>(rtcp_packet),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000206 rtcp_packet_length);
207}
208
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000209int32_t ViEReceiver::OnReceivedPayloadData(const uint8_t* payload_data,
210 const size_t payload_size,
211 const WebRtcRTPHeader* rtp_header) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000212 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000213 rtp_header_with_ntp.ntp_time_ms =
214 ntp_estimator_->Estimate(rtp_header->header.timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000215 if (vcm_->IncomingPacket(payload_data,
216 payload_size,
217 rtp_header_with_ntp) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000218 // Check this...
219 return -1;
220 }
221 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000222}
223
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000224bool ViEReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000225 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000226 RTPHeader header;
227 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000228 return false;
229 }
230 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000231 bool in_order = IsPacketInOrder(header);
232 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000233}
234
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000235void ViEReceiver::ReceivedBWEPacket(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000236 int64_t arrival_time_ms, size_t payload_size, const RTPHeader& header) {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000237 // Only forward if the incoming packet *and* the channel are both configured
238 // to receive absolute sender time. RTP time stamps may have different rates
239 // for audio and video and shouldn't be mixed.
240 if (header.extension.hasAbsoluteSendTime && receiving_ast_enabled_) {
241 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_size,
242 header);
243 }
244}
245
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000246int ViEReceiver::InsertRTPPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000247 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000248 const PacketTime& packet_time) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000249 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000250 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000251 if (!receiving_) {
252 return -1;
253 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000254 if (rtp_dump_) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000255 rtp_dump_->DumpPacket(rtp_packet, rtp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000256 }
257 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000258
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000259 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000260 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000261 &header)) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000262 return -1;
263 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000264 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000265 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000266 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000267 if (packet_time.timestamp != -1)
268 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
269 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000270 arrival_time_ms = now_ms;
271
272 {
273 // Periodically log the RTP header of incoming packets.
274 CriticalSectionScoped cs(receive_cs_.get());
275 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
276 std::stringstream ss;
277 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
278 << static_cast<int>(header.payloadType) << ", timestamp: "
279 << header.timestamp << ", sequence number: " << header.sequenceNumber
280 << ", arrival time: " << arrival_time_ms;
281 if (header.extension.hasTransmissionTimeOffset)
282 ss << ", toffset: " << header.extension.transmissionTimeOffset;
283 if (header.extension.hasAbsoluteSendTime)
284 ss << ", abs send time: " << header.extension.absoluteSendTime;
285 LOG(LS_INFO) << ss.str();
286 last_packet_log_ms_ = now_ms;
287 }
288 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000289
290 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000291 payload_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000292 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000293
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000294 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000295 rtp_payload_registry_->SetIncomingPayloadType(header);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000296 int ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order)
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000297 ? 0
298 : -1;
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000299 // Update receive statistics after ReceivePacket.
300 // Receive statistics will be reset if the payload type changes (make sure
301 // that the first packet is included in the stats).
302 rtp_receive_statistics_->IncomingPacket(
303 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
304 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000305}
306
307bool ViEReceiver::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000308 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000309 const RTPHeader& header,
310 bool in_order) {
311 if (rtp_payload_registry_->IsEncapsulated(header)) {
312 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
313 }
314 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000315 assert(packet_length >= header.headerLength);
316 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000317 PayloadUnion payload_specific;
318 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
319 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000320 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000321 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000322 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
323 payload_specific, in_order);
324}
325
326bool ViEReceiver::ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000327 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000328 const RTPHeader& header) {
329 if (rtp_payload_registry_->IsRed(header)) {
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000330 int8_t ulpfec_pt = rtp_payload_registry_->ulpfec_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000331 if (packet[header.headerLength] == ulpfec_pt) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000332 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000333 // Notify vcm about received FEC packets to avoid NACKing these packets.
334 NotifyReceiverOfFecPacket(header);
335 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000336 if (fec_receiver_->AddReceivedRedPacket(
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000337 header, packet, packet_length, ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000338 return false;
339 }
340 return fec_receiver_->ProcessReceivedFec() == 0;
341 } else if (rtp_payload_registry_->IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000342 if (header.headerLength + header.paddingLength == packet_length) {
343 // This is an empty packet and should be silently dropped before trying to
344 // parse the RTX header.
345 return true;
346 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000347 // Remove the RTX header and parse the original RTP header.
348 if (packet_length < header.headerLength)
349 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000350 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000351 return false;
352 CriticalSectionScoped cs(receive_cs_.get());
353 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000354 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000355 return false;
356 }
357 uint8_t* restored_packet_ptr = restored_packet_;
358 if (!rtp_payload_registry_->RestoreOriginalPacket(
359 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
360 header)) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000361 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000362 return false;
363 }
364 restored_packet_in_use_ = true;
365 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
366 restored_packet_in_use_ = false;
367 return ret;
368 }
369 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000370}
371
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000372void ViEReceiver::NotifyReceiverOfFecPacket(const RTPHeader& header) {
373 int8_t last_media_payload_type =
374 rtp_payload_registry_->last_received_media_payload_type();
375 if (last_media_payload_type < 0) {
376 LOG(LS_WARNING) << "Failed to get last media payload type.";
377 return;
378 }
379 // Fake an empty media packet.
380 WebRtcRTPHeader rtp_header = {};
381 rtp_header.header = header;
382 rtp_header.header.payloadType = last_media_payload_type;
383 rtp_header.header.paddingLength = 0;
384 PayloadUnion payload_specific;
385 if (!rtp_payload_registry_->GetPayloadSpecifics(last_media_payload_type,
386 &payload_specific)) {
387 LOG(LS_WARNING) << "Failed to get payload specifics.";
388 return;
389 }
390 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000391 rtp_header.type.Video.rotation = kVideoRotation_0;
392 if (header.extension.hasVideoRotation) {
393 rtp_header.type.Video.rotation =
394 ConvertCVOByteToVideoRotation(header.extension.videoRotation);
395 }
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000396 OnReceivedPayloadData(NULL, 0, &rtp_header);
397}
398
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000399int ViEReceiver::InsertRTCPPacket(const uint8_t* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000400 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000401 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000402 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000403 if (!receiving_) {
404 return -1;
405 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000406
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000407 if (rtp_dump_) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000408 rtp_dump_->DumpPacket(rtcp_packet, rtcp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000409 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000410 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000411 assert(rtp_rtcp_); // Should be set by owner at construction time.
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000412 int ret = rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
413 if (ret != 0) {
414 return ret;
415 }
416
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000417 int64_t rtt = 0;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000418 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, NULL, NULL, NULL);
419 if (rtt == 0) {
420 // Waiting for valid rtt.
421 return 0;
422 }
423 uint32_t ntp_secs = 0;
424 uint32_t ntp_frac = 0;
425 uint32_t rtp_timestamp = 0;
426 if (0 != rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
427 &rtp_timestamp)) {
428 // Waiting for RTCP.
429 return 0;
430 }
431 ntp_estimator_->UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000432
433 return 0;
434}
435
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000436void ViEReceiver::StartReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000437 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000438 receiving_ = true;
439}
440
441void ViEReceiver::StopReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000442 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000443 receiving_ = false;
444}
445
446int ViEReceiver::StartRTPDump(const char file_nameUTF8[1024]) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000447 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000448 if (rtp_dump_) {
449 // Restart it if it already exists and is started
450 rtp_dump_->Stop();
451 } else {
452 rtp_dump_ = RtpDump::CreateRtpDump();
453 if (rtp_dump_ == NULL) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000454 return -1;
455 }
456 }
457 if (rtp_dump_->Start(file_nameUTF8) != 0) {
458 RtpDump::DestroyRtpDump(rtp_dump_);
459 rtp_dump_ = NULL;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000460 return -1;
461 }
462 return 0;
463}
464
465int ViEReceiver::StopRTPDump() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000466 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000467 if (rtp_dump_) {
468 if (rtp_dump_->IsActive()) {
469 rtp_dump_->Stop();
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000470 }
471 RtpDump::DestroyRtpDump(rtp_dump_);
472 rtp_dump_ = NULL;
473 } else {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000474 return -1;
475 }
476 return 0;
477}
478
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000479ReceiveStatistics* ViEReceiver::GetReceiveStatistics() const {
480 return rtp_receive_statistics_.get();
481}
482
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000483bool ViEReceiver::IsPacketInOrder(const RTPHeader& header) const {
484 StreamStatistician* statistician =
485 rtp_receive_statistics_->GetStatistician(header.ssrc);
486 if (!statistician)
487 return false;
488 return statistician->IsPacketInOrder(header.sequenceNumber);
489}
490
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000491bool ViEReceiver::IsPacketRetransmitted(const RTPHeader& header,
492 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000493 // Retransmissions are handled separately if RTX is enabled.
494 if (rtp_payload_registry_->RtxEnabled())
495 return false;
496 StreamStatistician* statistician =
497 rtp_receive_statistics_->GetStatistician(header.ssrc);
498 if (!statistician)
499 return false;
500 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000501 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000502 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000503 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000504 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000505}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000506} // namespace webrtc