blob: 7e1034f39c7f567ed31a057418c4126621e82bb5 [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"
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000019#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000020#include "webrtc/modules/rtp_rtcp/interface/rtp_payload_registry.h"
21#include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000022#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
23#include "webrtc/modules/utility/interface/rtp_dump.h"
24#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_)),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000057 rtp_dump_(NULL),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000058 receiving_(false),
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +000059 restored_packet_in_use_(false),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000060 receiving_ast_enabled_(false),
61 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();
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000067 if (rtp_dump_) {
68 rtp_dump_->Stop();
69 RtpDump::DestroyRtpDump(rtp_dump_);
70 rtp_dump_ = NULL;
71 }
niklase@google.com470e71d2011-07-07 08:21:25 +000072}
73
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000074void ViEReceiver::UpdateHistograms() {
75 FecPacketCounter counter = fec_receiver_->GetPacketCounter();
76 if (counter.num_packets > 0) {
77 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.ReceivedFecPacketsInPercent",
78 counter.num_fec_packets * 100 / counter.num_packets);
79 }
80 if (counter.num_fec_packets > 0) {
81 RTC_HISTOGRAM_PERCENTAGE(
82 "WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
83 counter.num_recovered_packets * 100 / counter.num_fec_packets);
84 }
85}
86
wu@webrtc.org822fbd82013-08-15 23:38:54 +000087bool ViEReceiver::SetReceiveCodec(const VideoCodec& video_codec) {
88 int8_t old_pltype = -1;
89 if (rtp_payload_registry_->ReceivePayloadType(video_codec.plName,
90 kVideoPayloadTypeFrequency,
91 0,
92 video_codec.maxBitrate,
93 &old_pltype) != -1) {
94 rtp_payload_registry_->DeRegisterReceivePayload(old_pltype);
95 }
96
97 return RegisterPayload(video_codec);
98}
99
100bool ViEReceiver::RegisterPayload(const VideoCodec& video_codec) {
101 return rtp_receiver_->RegisterReceivePayload(video_codec.plName,
102 video_codec.plType,
103 kVideoPayloadTypeFrequency,
104 0,
105 video_codec.maxBitrate) == 0;
106}
107
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000108void ViEReceiver::SetNackStatus(bool enable,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000109 int max_nack_reordering_threshold) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000110 if (!enable) {
111 // Reset the threshold back to the lower default threshold when NACK is
112 // disabled since we no longer will be receiving retransmissions.
113 max_nack_reordering_threshold = kDefaultMaxReorderingThreshold;
114 }
115 rtp_receive_statistics_->SetMaxReorderingThreshold(
116 max_nack_reordering_threshold);
117 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000118}
119
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000120void ViEReceiver::SetRtxPayloadType(int payload_type) {
121 rtp_payload_registry_->SetRtxPayloadType(payload_type);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000122}
123
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000124void ViEReceiver::SetRtxSsrc(uint32_t ssrc) {
125 rtp_payload_registry_->SetRtxSsrc(ssrc);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000126}
127
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000128bool ViEReceiver::GetRtxSsrc(uint32_t* ssrc) const {
129 return rtp_payload_registry_->GetRtxSsrc(ssrc);
130}
131
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000132uint32_t ViEReceiver::GetRemoteSsrc() const {
133 return rtp_receiver_->SSRC();
134}
135
136int ViEReceiver::GetCsrcs(uint32_t* csrcs) const {
137 return rtp_receiver_->CSRCs(csrcs);
138}
139
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000140void ViEReceiver::SetRtpRtcpModule(RtpRtcp* module) {
141 rtp_rtcp_ = module;
142}
143
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000144RtpReceiver* ViEReceiver::GetRtpReceiver() const {
145 return rtp_receiver_.get();
146}
147
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000148void ViEReceiver::RegisterSimulcastRtpRtcpModules(
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000149 const std::list<RtpRtcp*>& rtp_modules) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000150 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000151 rtp_rtcp_simulcast_.clear();
152
153 if (!rtp_modules.empty()) {
154 rtp_rtcp_simulcast_.insert(rtp_rtcp_simulcast_.begin(),
155 rtp_modules.begin(),
156 rtp_modules.end());
157 }
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000158}
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
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000186int ViEReceiver::ReceivedRTPPacket(const void* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000187 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000188 const PacketTime& packet_time) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000189 return InsertRTPPacket(static_cast<const uint8_t*>(rtp_packet),
wu@webrtc.orga9890802013-12-13 00:21:03 +0000190 rtp_packet_length, packet_time);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000191}
192
193int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000194 size_t rtcp_packet_length) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000195 return InsertRTCPPacket(static_cast<const uint8_t*>(rtcp_packet),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000196 rtcp_packet_length);
197}
198
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000199int32_t ViEReceiver::OnReceivedPayloadData(const uint8_t* payload_data,
200 const size_t payload_size,
201 const WebRtcRTPHeader* rtp_header) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000202 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000203 rtp_header_with_ntp.ntp_time_ms =
204 ntp_estimator_->Estimate(rtp_header->header.timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000205 if (vcm_->IncomingPacket(payload_data,
206 payload_size,
207 rtp_header_with_ntp) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000208 // Check this...
209 return -1;
210 }
211 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000212}
213
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000214bool ViEReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000215 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000216 RTPHeader header;
217 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000218 return false;
219 }
220 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000221 bool in_order = IsPacketInOrder(header);
222 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000223}
224
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000225void ViEReceiver::ReceivedBWEPacket(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000226 int64_t arrival_time_ms, size_t payload_size, const RTPHeader& header) {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000227 // Only forward if the incoming packet *and* the channel are both configured
228 // to receive absolute sender time. RTP time stamps may have different rates
229 // for audio and video and shouldn't be mixed.
230 if (header.extension.hasAbsoluteSendTime && receiving_ast_enabled_) {
231 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_size,
232 header);
233 }
234}
235
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000236int ViEReceiver::InsertRTPPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000237 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000238 const PacketTime& packet_time) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000239 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000240 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000241 if (!receiving_) {
242 return -1;
243 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000244 if (rtp_dump_) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000245 rtp_dump_->DumpPacket(rtp_packet, rtp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000246 }
247 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000248
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000249 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000250 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000251 &header)) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000252 return -1;
253 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000254 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000255 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000256 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000257 if (packet_time.timestamp != -1)
258 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
259 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000260 arrival_time_ms = now_ms;
261
262 {
263 // Periodically log the RTP header of incoming packets.
264 CriticalSectionScoped cs(receive_cs_.get());
265 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
266 std::stringstream ss;
267 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
268 << static_cast<int>(header.payloadType) << ", timestamp: "
269 << header.timestamp << ", sequence number: " << header.sequenceNumber
270 << ", arrival time: " << arrival_time_ms;
271 if (header.extension.hasTransmissionTimeOffset)
272 ss << ", toffset: " << header.extension.transmissionTimeOffset;
273 if (header.extension.hasAbsoluteSendTime)
274 ss << ", abs send time: " << header.extension.absoluteSendTime;
275 LOG(LS_INFO) << ss.str();
276 last_packet_log_ms_ = now_ms;
277 }
278 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000279
280 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000281 payload_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000282 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000283
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000284 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000285 rtp_payload_registry_->SetIncomingPayloadType(header);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000286 int ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order)
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000287 ? 0
288 : -1;
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000289 // Update receive statistics after ReceivePacket.
290 // Receive statistics will be reset if the payload type changes (make sure
291 // that the first packet is included in the stats).
292 rtp_receive_statistics_->IncomingPacket(
293 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
294 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000295}
296
297bool ViEReceiver::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000298 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000299 const RTPHeader& header,
300 bool in_order) {
301 if (rtp_payload_registry_->IsEncapsulated(header)) {
302 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
303 }
304 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000305 assert(packet_length >= header.headerLength);
306 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000307 PayloadUnion payload_specific;
308 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
309 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000310 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000311 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000312 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
313 payload_specific, in_order);
314}
315
316bool ViEReceiver::ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000317 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000318 const RTPHeader& header) {
319 if (rtp_payload_registry_->IsRed(header)) {
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000320 int8_t ulpfec_pt = rtp_payload_registry_->ulpfec_payload_type();
321 if (packet[header.headerLength] == ulpfec_pt)
322 rtp_receive_statistics_->FecPacketReceived(header.ssrc);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000323 if (fec_receiver_->AddReceivedRedPacket(
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000324 header, packet, packet_length, ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000325 return false;
326 }
327 return fec_receiver_->ProcessReceivedFec() == 0;
328 } else if (rtp_payload_registry_->IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000329 if (header.headerLength + header.paddingLength == packet_length) {
330 // This is an empty packet and should be silently dropped before trying to
331 // parse the RTX header.
332 return true;
333 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000334 // Remove the RTX header and parse the original RTP header.
335 if (packet_length < header.headerLength)
336 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000337 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000338 return false;
339 CriticalSectionScoped cs(receive_cs_.get());
340 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000341 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000342 return false;
343 }
344 uint8_t* restored_packet_ptr = restored_packet_;
345 if (!rtp_payload_registry_->RestoreOriginalPacket(
346 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
347 header)) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000348 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000349 return false;
350 }
351 restored_packet_in_use_ = true;
352 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
353 restored_packet_in_use_ = false;
354 return ret;
355 }
356 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000357}
358
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000359int ViEReceiver::InsertRTCPPacket(const uint8_t* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000360 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000361 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000362 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000363 if (!receiving_) {
364 return -1;
365 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000366
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000367 if (rtp_dump_) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000368 rtp_dump_->DumpPacket(rtcp_packet, rtcp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000369 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000370
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000371 std::list<RtpRtcp*>::iterator it = rtp_rtcp_simulcast_.begin();
372 while (it != rtp_rtcp_simulcast_.end()) {
373 RtpRtcp* rtp_rtcp = *it++;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000374 rtp_rtcp->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000375 }
376 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000377 assert(rtp_rtcp_); // Should be set by owner at construction time.
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000378 int ret = rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
379 if (ret != 0) {
380 return ret;
381 }
382
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000383 int64_t rtt = 0;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000384 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, NULL, NULL, NULL);
385 if (rtt == 0) {
386 // Waiting for valid rtt.
387 return 0;
388 }
389 uint32_t ntp_secs = 0;
390 uint32_t ntp_frac = 0;
391 uint32_t rtp_timestamp = 0;
392 if (0 != rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
393 &rtp_timestamp)) {
394 // Waiting for RTCP.
395 return 0;
396 }
397 ntp_estimator_->UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000398
399 return 0;
400}
401
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000402void ViEReceiver::StartReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000403 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000404 receiving_ = true;
405}
406
407void ViEReceiver::StopReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000408 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000409 receiving_ = false;
410}
411
412int ViEReceiver::StartRTPDump(const char file_nameUTF8[1024]) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000413 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000414 if (rtp_dump_) {
415 // Restart it if it already exists and is started
416 rtp_dump_->Stop();
417 } else {
418 rtp_dump_ = RtpDump::CreateRtpDump();
419 if (rtp_dump_ == NULL) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000420 return -1;
421 }
422 }
423 if (rtp_dump_->Start(file_nameUTF8) != 0) {
424 RtpDump::DestroyRtpDump(rtp_dump_);
425 rtp_dump_ = NULL;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000426 return -1;
427 }
428 return 0;
429}
430
431int ViEReceiver::StopRTPDump() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000432 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000433 if (rtp_dump_) {
434 if (rtp_dump_->IsActive()) {
435 rtp_dump_->Stop();
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000436 }
437 RtpDump::DestroyRtpDump(rtp_dump_);
438 rtp_dump_ = NULL;
439 } else {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000440 return -1;
441 }
442 return 0;
443}
444
jiayl@webrtc.org1f64f062014-02-10 19:12:14 +0000445void ViEReceiver::GetReceiveBandwidthEstimatorStats(
446 ReceiveBandwidthEstimatorStats* output) const {
447 remote_bitrate_estimator_->GetStats(output);
448}
449
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000450ReceiveStatistics* ViEReceiver::GetReceiveStatistics() const {
451 return rtp_receive_statistics_.get();
452}
453
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000454bool ViEReceiver::IsPacketInOrder(const RTPHeader& header) const {
455 StreamStatistician* statistician =
456 rtp_receive_statistics_->GetStatistician(header.ssrc);
457 if (!statistician)
458 return false;
459 return statistician->IsPacketInOrder(header.sequenceNumber);
460}
461
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000462bool ViEReceiver::IsPacketRetransmitted(const RTPHeader& header,
463 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000464 // Retransmissions are handled separately if RTX is enabled.
465 if (rtp_payload_registry_->RtxEnabled())
466 return false;
467 StreamStatistician* statistician =
468 rtp_receive_statistics_->GetStatistician(header.ssrc);
469 if (!statistician)
470 return false;
471 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000472 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000473 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000474 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000475 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000476}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000477} // namespace webrtc