blob: 9e250800527b0779d4d9e1a83f02e1733688ab14 [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) {
Peter Boströmae37abb2015-06-18 19:00:34 +020072 RTC_HISTOGRAM_PERCENTAGE(
73 "WebRTC.Video.ReceivedFecPacketsInPercent",
74 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000075 }
76 if (counter.num_fec_packets > 0) {
Peter Boströmae37abb2015-06-18 19:00:34 +020077 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
78 static_cast<int>(counter.num_recovered_packets *
79 100 / counter.num_fec_packets));
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000080 }
81}
82
wu@webrtc.org822fbd82013-08-15 23:38:54 +000083bool ViEReceiver::SetReceiveCodec(const VideoCodec& video_codec) {
84 int8_t old_pltype = -1;
85 if (rtp_payload_registry_->ReceivePayloadType(video_codec.plName,
86 kVideoPayloadTypeFrequency,
87 0,
88 video_codec.maxBitrate,
89 &old_pltype) != -1) {
90 rtp_payload_registry_->DeRegisterReceivePayload(old_pltype);
91 }
92
93 return RegisterPayload(video_codec);
94}
95
96bool ViEReceiver::RegisterPayload(const VideoCodec& video_codec) {
97 return rtp_receiver_->RegisterReceivePayload(video_codec.plName,
98 video_codec.plType,
99 kVideoPayloadTypeFrequency,
100 0,
101 video_codec.maxBitrate) == 0;
102}
103
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000104void ViEReceiver::SetNackStatus(bool enable,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000105 int max_nack_reordering_threshold) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000106 if (!enable) {
107 // Reset the threshold back to the lower default threshold when NACK is
108 // disabled since we no longer will be receiving retransmissions.
109 max_nack_reordering_threshold = kDefaultMaxReorderingThreshold;
110 }
111 rtp_receive_statistics_->SetMaxReorderingThreshold(
112 max_nack_reordering_threshold);
113 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000114}
115
Shao Changbine62202f2015-04-21 20:24:50 +0800116void ViEReceiver::SetRtxPayloadType(int payload_type,
117 int associated_payload_type) {
118 rtp_payload_registry_->SetRtxPayloadType(payload_type,
119 associated_payload_type);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000120}
121
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000122void ViEReceiver::SetRtxSsrc(uint32_t ssrc) {
123 rtp_payload_registry_->SetRtxSsrc(ssrc);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000124}
125
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000126bool ViEReceiver::GetRtxSsrc(uint32_t* ssrc) const {
127 return rtp_payload_registry_->GetRtxSsrc(ssrc);
128}
129
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000130bool ViEReceiver::IsFecEnabled() const {
131 return rtp_payload_registry_->ulpfec_payload_type() > -1;
132}
133
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000134uint32_t ViEReceiver::GetRemoteSsrc() const {
135 return rtp_receiver_->SSRC();
136}
137
138int ViEReceiver::GetCsrcs(uint32_t* csrcs) const {
139 return rtp_receiver_->CSRCs(csrcs);
140}
141
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000142void ViEReceiver::SetRtpRtcpModule(RtpRtcp* module) {
143 rtp_rtcp_ = module;
144}
145
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000146RtpReceiver* ViEReceiver::GetRtpReceiver() const {
147 return rtp_receiver_.get();
148}
149
Peter Boströmd6f1a382015-07-14 16:08:02 +0200150void ViEReceiver::RegisterRtpRtcpModules(
151 const std::vector<RtpRtcp*>& rtp_modules) {
Peter Boström6cff9cf2015-04-21 13:47:17 +0200152 CriticalSectionScoped cs(receive_cs_.get());
Peter Boströmd6f1a382015-07-14 16:08:02 +0200153 // Only change the "simulcast" modules, the base module can be accessed
154 // without a lock whereas the simulcast modules require locking as they can be
155 // changed in runtime.
156 rtp_rtcp_simulcast_ =
157 std::vector<RtpRtcp*>(rtp_modules.begin() + 1, rtp_modules.end());
Peter Boström6cff9cf2015-04-21 13:47:17 +0200158}
159
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000160bool ViEReceiver::SetReceiveTimestampOffsetStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000161 if (enable) {
162 return rtp_header_parser_->RegisterRtpHeaderExtension(
163 kRtpExtensionTransmissionTimeOffset, id);
164 } else {
165 return rtp_header_parser_->DeregisterRtpHeaderExtension(
166 kRtpExtensionTransmissionTimeOffset);
167 }
168}
169
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000170bool ViEReceiver::SetReceiveAbsoluteSendTimeStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000171 if (enable) {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000172 if (rtp_header_parser_->RegisterRtpHeaderExtension(
173 kRtpExtensionAbsoluteSendTime, id)) {
174 receiving_ast_enabled_ = true;
175 return true;
176 } else {
177 return false;
178 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000179 } else {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000180 receiving_ast_enabled_ = false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000181 return rtp_header_parser_->DeregisterRtpHeaderExtension(
182 kRtpExtensionAbsoluteSendTime);
183 }
184}
185
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000186bool ViEReceiver::SetReceiveVideoRotationStatus(bool enable, int id) {
187 if (enable) {
188 if (rtp_header_parser_->RegisterRtpHeaderExtension(
189 kRtpExtensionVideoRotation, id)) {
190 receiving_cvo_enabled_ = true;
191 return true;
192 } else {
193 return false;
194 }
195 } else {
196 receiving_cvo_enabled_ = false;
197 return rtp_header_parser_->DeregisterRtpHeaderExtension(
198 kRtpExtensionVideoRotation);
199 }
200}
201
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000202int ViEReceiver::ReceivedRTPPacket(const void* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000203 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000204 const PacketTime& packet_time) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000205 return InsertRTPPacket(static_cast<const uint8_t*>(rtp_packet),
wu@webrtc.orga9890802013-12-13 00:21:03 +0000206 rtp_packet_length, packet_time);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000207}
208
209int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000210 size_t rtcp_packet_length) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000211 return InsertRTCPPacket(static_cast<const uint8_t*>(rtcp_packet),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000212 rtcp_packet_length);
213}
214
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000215int32_t ViEReceiver::OnReceivedPayloadData(const uint8_t* payload_data,
216 const size_t payload_size,
217 const WebRtcRTPHeader* rtp_header) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000218 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000219 rtp_header_with_ntp.ntp_time_ms =
220 ntp_estimator_->Estimate(rtp_header->header.timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000221 if (vcm_->IncomingPacket(payload_data,
222 payload_size,
223 rtp_header_with_ntp) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000224 // Check this...
225 return -1;
226 }
227 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000228}
229
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000230bool ViEReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000231 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000232 RTPHeader header;
233 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000234 return false;
235 }
236 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000237 bool in_order = IsPacketInOrder(header);
238 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000239}
240
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000241int ViEReceiver::InsertRTPPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000242 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000243 const PacketTime& packet_time) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000244 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000245 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000246 if (!receiving_) {
247 return -1;
248 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000249 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000250
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000251 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000252 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000253 &header)) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000254 return -1;
255 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000256 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000257 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000258 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000259 if (packet_time.timestamp != -1)
260 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
261 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000262 arrival_time_ms = now_ms;
263
264 {
265 // Periodically log the RTP header of incoming packets.
266 CriticalSectionScoped cs(receive_cs_.get());
267 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
268 std::stringstream ss;
269 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
270 << static_cast<int>(header.payloadType) << ", timestamp: "
271 << header.timestamp << ", sequence number: " << header.sequenceNumber
272 << ", arrival time: " << arrival_time_ms;
273 if (header.extension.hasTransmissionTimeOffset)
274 ss << ", toffset: " << header.extension.transmissionTimeOffset;
275 if (header.extension.hasAbsoluteSendTime)
276 ss << ", abs send time: " << header.extension.absoluteSendTime;
277 LOG(LS_INFO) << ss.str();
278 last_packet_log_ms_ = now_ms;
279 }
280 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000281
Stefan Holmerff4ea932015-06-18 16:01:33 +0200282 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_length,
283 header, true);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000284 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000285
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000286 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000287 rtp_payload_registry_->SetIncomingPayloadType(header);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000288 int ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order)
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000289 ? 0
290 : -1;
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000291 // Update receive statistics after ReceivePacket.
292 // Receive statistics will be reset if the payload type changes (make sure
293 // that the first packet is included in the stats).
294 rtp_receive_statistics_->IncomingPacket(
295 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
296 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000297}
298
299bool ViEReceiver::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000300 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000301 const RTPHeader& header,
302 bool in_order) {
303 if (rtp_payload_registry_->IsEncapsulated(header)) {
304 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
305 }
306 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000307 assert(packet_length >= header.headerLength);
308 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000309 PayloadUnion payload_specific;
310 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
311 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000312 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000313 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000314 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
315 payload_specific, in_order);
316}
317
318bool ViEReceiver::ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000319 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000320 const RTPHeader& header) {
321 if (rtp_payload_registry_->IsRed(header)) {
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000322 int8_t ulpfec_pt = rtp_payload_registry_->ulpfec_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000323 if (packet[header.headerLength] == ulpfec_pt) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000324 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000325 // Notify vcm about received FEC packets to avoid NACKing these packets.
326 NotifyReceiverOfFecPacket(header);
327 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000328 if (fec_receiver_->AddReceivedRedPacket(
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000329 header, packet, packet_length, ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000330 return false;
331 }
332 return fec_receiver_->ProcessReceivedFec() == 0;
333 } else if (rtp_payload_registry_->IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000334 if (header.headerLength + header.paddingLength == packet_length) {
335 // This is an empty packet and should be silently dropped before trying to
336 // parse the RTX header.
337 return true;
338 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000339 // Remove the RTX header and parse the original RTP header.
340 if (packet_length < header.headerLength)
341 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000342 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000343 return false;
344 CriticalSectionScoped cs(receive_cs_.get());
345 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000346 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000347 return false;
348 }
349 uint8_t* restored_packet_ptr = restored_packet_;
350 if (!rtp_payload_registry_->RestoreOriginalPacket(
351 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
352 header)) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000353 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000354 return false;
355 }
356 restored_packet_in_use_ = true;
357 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
358 restored_packet_in_use_ = false;
359 return ret;
360 }
361 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000362}
363
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000364void ViEReceiver::NotifyReceiverOfFecPacket(const RTPHeader& header) {
365 int8_t last_media_payload_type =
366 rtp_payload_registry_->last_received_media_payload_type();
367 if (last_media_payload_type < 0) {
368 LOG(LS_WARNING) << "Failed to get last media payload type.";
369 return;
370 }
371 // Fake an empty media packet.
372 WebRtcRTPHeader rtp_header = {};
373 rtp_header.header = header;
374 rtp_header.header.payloadType = last_media_payload_type;
375 rtp_header.header.paddingLength = 0;
376 PayloadUnion payload_specific;
377 if (!rtp_payload_registry_->GetPayloadSpecifics(last_media_payload_type,
378 &payload_specific)) {
379 LOG(LS_WARNING) << "Failed to get payload specifics.";
380 return;
381 }
382 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000383 rtp_header.type.Video.rotation = kVideoRotation_0;
384 if (header.extension.hasVideoRotation) {
385 rtp_header.type.Video.rotation =
386 ConvertCVOByteToVideoRotation(header.extension.videoRotation);
387 }
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000388 OnReceivedPayloadData(NULL, 0, &rtp_header);
389}
390
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000391int ViEReceiver::InsertRTCPPacket(const uint8_t* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000392 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000393 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000394 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000395 if (!receiving_) {
396 return -1;
397 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000398
Peter Boströmd6f1a382015-07-14 16:08:02 +0200399 for (RtpRtcp* rtp_rtcp : rtp_rtcp_simulcast_)
Peter Boström6cff9cf2015-04-21 13:47:17 +0200400 rtp_rtcp->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000401 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000402 assert(rtp_rtcp_); // Should be set by owner at construction time.
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000403 int ret = rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
404 if (ret != 0) {
405 return ret;
406 }
407
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000408 int64_t rtt = 0;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000409 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, NULL, NULL, NULL);
410 if (rtt == 0) {
411 // Waiting for valid rtt.
412 return 0;
413 }
414 uint32_t ntp_secs = 0;
415 uint32_t ntp_frac = 0;
416 uint32_t rtp_timestamp = 0;
417 if (0 != rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
418 &rtp_timestamp)) {
419 // Waiting for RTCP.
420 return 0;
421 }
422 ntp_estimator_->UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000423
424 return 0;
425}
426
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000427void ViEReceiver::StartReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000428 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000429 receiving_ = true;
430}
431
432void ViEReceiver::StopReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000433 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000434 receiving_ = false;
435}
436
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000437ReceiveStatistics* ViEReceiver::GetReceiveStatistics() const {
438 return rtp_receive_statistics_.get();
439}
440
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000441bool ViEReceiver::IsPacketInOrder(const RTPHeader& header) const {
442 StreamStatistician* statistician =
443 rtp_receive_statistics_->GetStatistician(header.ssrc);
444 if (!statistician)
445 return false;
446 return statistician->IsPacketInOrder(header.sequenceNumber);
447}
448
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000449bool ViEReceiver::IsPacketRetransmitted(const RTPHeader& header,
450 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000451 // Retransmissions are handled separately if RTX is enabled.
452 if (rtp_payload_registry_->RtxEnabled())
453 return false;
454 StreamStatistician* statistician =
455 rtp_receive_statistics_->GetStatistician(header.ssrc);
456 if (!statistician)
457 return false;
458 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000459 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000460 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000461 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000462 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000463}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000464} // namespace webrtc