blob: 1d2485ed4a25cf0648d961b29256fe51c76f8845 [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
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000132bool ViEReceiver::IsFecEnabled() const {
133 return rtp_payload_registry_->ulpfec_payload_type() > -1;
134}
135
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000136uint32_t ViEReceiver::GetRemoteSsrc() const {
137 return rtp_receiver_->SSRC();
138}
139
140int ViEReceiver::GetCsrcs(uint32_t* csrcs) const {
141 return rtp_receiver_->CSRCs(csrcs);
142}
143
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000144void ViEReceiver::SetRtpRtcpModule(RtpRtcp* module) {
145 rtp_rtcp_ = module;
146}
147
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000148RtpReceiver* ViEReceiver::GetRtpReceiver() const {
149 return rtp_receiver_.get();
150}
151
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000152void ViEReceiver::RegisterSimulcastRtpRtcpModules(
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000153 const std::list<RtpRtcp*>& rtp_modules) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000154 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000155 rtp_rtcp_simulcast_.clear();
156
157 if (!rtp_modules.empty()) {
158 rtp_rtcp_simulcast_.insert(rtp_rtcp_simulcast_.begin(),
159 rtp_modules.begin(),
160 rtp_modules.end());
161 }
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000162}
163
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000164bool ViEReceiver::SetReceiveTimestampOffsetStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000165 if (enable) {
166 return rtp_header_parser_->RegisterRtpHeaderExtension(
167 kRtpExtensionTransmissionTimeOffset, id);
168 } else {
169 return rtp_header_parser_->DeregisterRtpHeaderExtension(
170 kRtpExtensionTransmissionTimeOffset);
171 }
172}
173
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000174bool ViEReceiver::SetReceiveAbsoluteSendTimeStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000175 if (enable) {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000176 if (rtp_header_parser_->RegisterRtpHeaderExtension(
177 kRtpExtensionAbsoluteSendTime, id)) {
178 receiving_ast_enabled_ = true;
179 return true;
180 } else {
181 return false;
182 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000183 } else {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000184 receiving_ast_enabled_ = false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000185 return rtp_header_parser_->DeregisterRtpHeaderExtension(
186 kRtpExtensionAbsoluteSendTime);
187 }
188}
189
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000190int ViEReceiver::ReceivedRTPPacket(const void* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000191 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000192 const PacketTime& packet_time) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000193 return InsertRTPPacket(static_cast<const uint8_t*>(rtp_packet),
wu@webrtc.orga9890802013-12-13 00:21:03 +0000194 rtp_packet_length, packet_time);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000195}
196
197int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000198 size_t rtcp_packet_length) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000199 return InsertRTCPPacket(static_cast<const uint8_t*>(rtcp_packet),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000200 rtcp_packet_length);
201}
202
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000203int32_t ViEReceiver::OnReceivedPayloadData(const uint8_t* payload_data,
204 const size_t payload_size,
205 const WebRtcRTPHeader* rtp_header) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000206 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000207 rtp_header_with_ntp.ntp_time_ms =
208 ntp_estimator_->Estimate(rtp_header->header.timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000209 if (vcm_->IncomingPacket(payload_data,
210 payload_size,
211 rtp_header_with_ntp) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000212 // Check this...
213 return -1;
214 }
215 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000216}
217
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000218bool ViEReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000219 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000220 RTPHeader header;
221 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000222 return false;
223 }
224 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000225 bool in_order = IsPacketInOrder(header);
226 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000227}
228
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000229void ViEReceiver::ReceivedBWEPacket(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000230 int64_t arrival_time_ms, size_t payload_size, const RTPHeader& header) {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000231 // Only forward if the incoming packet *and* the channel are both configured
232 // to receive absolute sender time. RTP time stamps may have different rates
233 // for audio and video and shouldn't be mixed.
234 if (header.extension.hasAbsoluteSendTime && receiving_ast_enabled_) {
235 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_size,
236 header);
237 }
238}
239
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000240int ViEReceiver::InsertRTPPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000241 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000242 const PacketTime& packet_time) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000243 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000244 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000245 if (!receiving_) {
246 return -1;
247 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000248 if (rtp_dump_) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000249 rtp_dump_->DumpPacket(rtp_packet, rtp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000250 }
251 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000252
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000253 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000254 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000255 &header)) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000256 return -1;
257 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000258 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000259 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000260 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000261 if (packet_time.timestamp != -1)
262 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
263 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000264 arrival_time_ms = now_ms;
265
266 {
267 // Periodically log the RTP header of incoming packets.
268 CriticalSectionScoped cs(receive_cs_.get());
269 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
270 std::stringstream ss;
271 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
272 << static_cast<int>(header.payloadType) << ", timestamp: "
273 << header.timestamp << ", sequence number: " << header.sequenceNumber
274 << ", arrival time: " << arrival_time_ms;
275 if (header.extension.hasTransmissionTimeOffset)
276 ss << ", toffset: " << header.extension.transmissionTimeOffset;
277 if (header.extension.hasAbsoluteSendTime)
278 ss << ", abs send time: " << header.extension.absoluteSendTime;
279 LOG(LS_INFO) << ss.str();
280 last_packet_log_ms_ = now_ms;
281 }
282 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000283
284 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000285 payload_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000286 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000287
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000288 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000289 rtp_payload_registry_->SetIncomingPayloadType(header);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000290 int ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order)
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000291 ? 0
292 : -1;
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000293 // Update receive statistics after ReceivePacket.
294 // Receive statistics will be reset if the payload type changes (make sure
295 // that the first packet is included in the stats).
296 rtp_receive_statistics_->IncomingPacket(
297 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
298 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000299}
300
301bool ViEReceiver::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000302 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000303 const RTPHeader& header,
304 bool in_order) {
305 if (rtp_payload_registry_->IsEncapsulated(header)) {
306 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
307 }
308 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000309 assert(packet_length >= header.headerLength);
310 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000311 PayloadUnion payload_specific;
312 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
313 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000314 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000315 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000316 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
317 payload_specific, in_order);
318}
319
320bool ViEReceiver::ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000321 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000322 const RTPHeader& header) {
323 if (rtp_payload_registry_->IsRed(header)) {
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000324 int8_t ulpfec_pt = rtp_payload_registry_->ulpfec_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000325 if (packet[header.headerLength] == ulpfec_pt) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000326 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000327 // Notify vcm about received FEC packets to avoid NACKing these packets.
328 NotifyReceiverOfFecPacket(header);
329 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000330 if (fec_receiver_->AddReceivedRedPacket(
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000331 header, packet, packet_length, ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000332 return false;
333 }
334 return fec_receiver_->ProcessReceivedFec() == 0;
335 } else if (rtp_payload_registry_->IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000336 if (header.headerLength + header.paddingLength == packet_length) {
337 // This is an empty packet and should be silently dropped before trying to
338 // parse the RTX header.
339 return true;
340 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000341 // Remove the RTX header and parse the original RTP header.
342 if (packet_length < header.headerLength)
343 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000344 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000345 return false;
346 CriticalSectionScoped cs(receive_cs_.get());
347 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000348 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000349 return false;
350 }
351 uint8_t* restored_packet_ptr = restored_packet_;
352 if (!rtp_payload_registry_->RestoreOriginalPacket(
353 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
354 header)) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000355 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000356 return false;
357 }
358 restored_packet_in_use_ = true;
359 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
360 restored_packet_in_use_ = false;
361 return ret;
362 }
363 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000364}
365
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000366void ViEReceiver::NotifyReceiverOfFecPacket(const RTPHeader& header) {
367 int8_t last_media_payload_type =
368 rtp_payload_registry_->last_received_media_payload_type();
369 if (last_media_payload_type < 0) {
370 LOG(LS_WARNING) << "Failed to get last media payload type.";
371 return;
372 }
373 // Fake an empty media packet.
374 WebRtcRTPHeader rtp_header = {};
375 rtp_header.header = header;
376 rtp_header.header.payloadType = last_media_payload_type;
377 rtp_header.header.paddingLength = 0;
378 PayloadUnion payload_specific;
379 if (!rtp_payload_registry_->GetPayloadSpecifics(last_media_payload_type,
380 &payload_specific)) {
381 LOG(LS_WARNING) << "Failed to get payload specifics.";
382 return;
383 }
384 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType;
385 OnReceivedPayloadData(NULL, 0, &rtp_header);
386}
387
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000388int ViEReceiver::InsertRTCPPacket(const uint8_t* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000389 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000390 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000391 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000392 if (!receiving_) {
393 return -1;
394 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000395
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000396 if (rtp_dump_) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000397 rtp_dump_->DumpPacket(rtcp_packet, rtcp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000398 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000399
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000400 std::list<RtpRtcp*>::iterator it = rtp_rtcp_simulcast_.begin();
401 while (it != rtp_rtcp_simulcast_.end()) {
402 RtpRtcp* rtp_rtcp = *it++;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000403 rtp_rtcp->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000404 }
405 }
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
441int ViEReceiver::StartRTPDump(const char file_nameUTF8[1024]) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000442 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000443 if (rtp_dump_) {
444 // Restart it if it already exists and is started
445 rtp_dump_->Stop();
446 } else {
447 rtp_dump_ = RtpDump::CreateRtpDump();
448 if (rtp_dump_ == NULL) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000449 return -1;
450 }
451 }
452 if (rtp_dump_->Start(file_nameUTF8) != 0) {
453 RtpDump::DestroyRtpDump(rtp_dump_);
454 rtp_dump_ = NULL;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000455 return -1;
456 }
457 return 0;
458}
459
460int ViEReceiver::StopRTPDump() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000461 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000462 if (rtp_dump_) {
463 if (rtp_dump_->IsActive()) {
464 rtp_dump_->Stop();
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000465 }
466 RtpDump::DestroyRtpDump(rtp_dump_);
467 rtp_dump_ = NULL;
468 } else {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000469 return -1;
470 }
471 return 0;
472}
473
jiayl@webrtc.org1f64f062014-02-10 19:12:14 +0000474void ViEReceiver::GetReceiveBandwidthEstimatorStats(
475 ReceiveBandwidthEstimatorStats* output) const {
476 remote_bitrate_estimator_->GetStats(output);
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