blob: e61c82bb9dd57a148c52c74e8b51232cf89b93eb [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
Peter Boström6cff9cf2015-04-21 13:47:17 +0200154void ViEReceiver::RegisterSimulcastRtpRtcpModules(
155 const std::list<RtpRtcp*>& rtp_modules) {
156 CriticalSectionScoped cs(receive_cs_.get());
157 rtp_rtcp_simulcast_.clear();
158
159 if (!rtp_modules.empty()) {
160 rtp_rtcp_simulcast_.insert(rtp_rtcp_simulcast_.begin(),
161 rtp_modules.begin(),
162 rtp_modules.end());
163 }
164}
165
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000166bool ViEReceiver::SetReceiveTimestampOffsetStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000167 if (enable) {
168 return rtp_header_parser_->RegisterRtpHeaderExtension(
169 kRtpExtensionTransmissionTimeOffset, id);
170 } else {
171 return rtp_header_parser_->DeregisterRtpHeaderExtension(
172 kRtpExtensionTransmissionTimeOffset);
173 }
174}
175
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000176bool ViEReceiver::SetReceiveAbsoluteSendTimeStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000177 if (enable) {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000178 if (rtp_header_parser_->RegisterRtpHeaderExtension(
179 kRtpExtensionAbsoluteSendTime, id)) {
180 receiving_ast_enabled_ = true;
181 return true;
182 } else {
183 return false;
184 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000185 } else {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000186 receiving_ast_enabled_ = false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000187 return rtp_header_parser_->DeregisterRtpHeaderExtension(
188 kRtpExtensionAbsoluteSendTime);
189 }
190}
191
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000192bool ViEReceiver::SetReceiveVideoRotationStatus(bool enable, int id) {
193 if (enable) {
194 if (rtp_header_parser_->RegisterRtpHeaderExtension(
195 kRtpExtensionVideoRotation, id)) {
196 receiving_cvo_enabled_ = true;
197 return true;
198 } else {
199 return false;
200 }
201 } else {
202 receiving_cvo_enabled_ = false;
203 return rtp_header_parser_->DeregisterRtpHeaderExtension(
204 kRtpExtensionVideoRotation);
205 }
206}
207
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000208int ViEReceiver::ReceivedRTPPacket(const void* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000209 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000210 const PacketTime& packet_time) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000211 return InsertRTPPacket(static_cast<const uint8_t*>(rtp_packet),
wu@webrtc.orga9890802013-12-13 00:21:03 +0000212 rtp_packet_length, packet_time);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000213}
214
215int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000216 size_t rtcp_packet_length) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000217 return InsertRTCPPacket(static_cast<const uint8_t*>(rtcp_packet),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000218 rtcp_packet_length);
219}
220
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000221int32_t ViEReceiver::OnReceivedPayloadData(const uint8_t* payload_data,
222 const size_t payload_size,
223 const WebRtcRTPHeader* rtp_header) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000224 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000225 rtp_header_with_ntp.ntp_time_ms =
226 ntp_estimator_->Estimate(rtp_header->header.timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000227 if (vcm_->IncomingPacket(payload_data,
228 payload_size,
229 rtp_header_with_ntp) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000230 // Check this...
231 return -1;
232 }
233 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000234}
235
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000236bool ViEReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000237 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000238 RTPHeader header;
239 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000240 return false;
241 }
242 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000243 bool in_order = IsPacketInOrder(header);
244 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000245}
246
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000247void ViEReceiver::ReceivedBWEPacket(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000248 int64_t arrival_time_ms, size_t payload_size, const RTPHeader& header) {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000249 // Only forward if the incoming packet *and* the channel are both configured
250 // to receive absolute sender time. RTP time stamps may have different rates
251 // for audio and video and shouldn't be mixed.
252 if (header.extension.hasAbsoluteSendTime && receiving_ast_enabled_) {
253 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_size,
254 header);
255 }
256}
257
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000258int ViEReceiver::InsertRTPPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000259 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000260 const PacketTime& packet_time) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000261 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000262 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000263 if (!receiving_) {
264 return -1;
265 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000266 if (rtp_dump_) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000267 rtp_dump_->DumpPacket(rtp_packet, rtp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000268 }
269 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000270
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000271 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000272 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000273 &header)) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000274 return -1;
275 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000276 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000277 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000278 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000279 if (packet_time.timestamp != -1)
280 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
281 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000282 arrival_time_ms = now_ms;
283
284 {
285 // Periodically log the RTP header of incoming packets.
286 CriticalSectionScoped cs(receive_cs_.get());
287 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
288 std::stringstream ss;
289 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
290 << static_cast<int>(header.payloadType) << ", timestamp: "
291 << header.timestamp << ", sequence number: " << header.sequenceNumber
292 << ", arrival time: " << arrival_time_ms;
293 if (header.extension.hasTransmissionTimeOffset)
294 ss << ", toffset: " << header.extension.transmissionTimeOffset;
295 if (header.extension.hasAbsoluteSendTime)
296 ss << ", abs send time: " << header.extension.absoluteSendTime;
297 LOG(LS_INFO) << ss.str();
298 last_packet_log_ms_ = now_ms;
299 }
300 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000301
302 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000303 payload_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000304 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000305
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000306 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000307 rtp_payload_registry_->SetIncomingPayloadType(header);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000308 int ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order)
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000309 ? 0
310 : -1;
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000311 // Update receive statistics after ReceivePacket.
312 // Receive statistics will be reset if the payload type changes (make sure
313 // that the first packet is included in the stats).
314 rtp_receive_statistics_->IncomingPacket(
315 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
316 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000317}
318
319bool ViEReceiver::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000320 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000321 const RTPHeader& header,
322 bool in_order) {
323 if (rtp_payload_registry_->IsEncapsulated(header)) {
324 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
325 }
326 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000327 assert(packet_length >= header.headerLength);
328 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000329 PayloadUnion payload_specific;
330 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
331 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000332 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000333 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000334 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
335 payload_specific, in_order);
336}
337
338bool ViEReceiver::ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000339 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000340 const RTPHeader& header) {
341 if (rtp_payload_registry_->IsRed(header)) {
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000342 int8_t ulpfec_pt = rtp_payload_registry_->ulpfec_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000343 if (packet[header.headerLength] == ulpfec_pt) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000344 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000345 // Notify vcm about received FEC packets to avoid NACKing these packets.
346 NotifyReceiverOfFecPacket(header);
347 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000348 if (fec_receiver_->AddReceivedRedPacket(
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000349 header, packet, packet_length, ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000350 return false;
351 }
352 return fec_receiver_->ProcessReceivedFec() == 0;
353 } else if (rtp_payload_registry_->IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000354 if (header.headerLength + header.paddingLength == packet_length) {
355 // This is an empty packet and should be silently dropped before trying to
356 // parse the RTX header.
357 return true;
358 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000359 // Remove the RTX header and parse the original RTP header.
360 if (packet_length < header.headerLength)
361 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000362 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000363 return false;
364 CriticalSectionScoped cs(receive_cs_.get());
365 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000366 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000367 return false;
368 }
369 uint8_t* restored_packet_ptr = restored_packet_;
370 if (!rtp_payload_registry_->RestoreOriginalPacket(
371 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
372 header)) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000373 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000374 return false;
375 }
376 restored_packet_in_use_ = true;
377 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
378 restored_packet_in_use_ = false;
379 return ret;
380 }
381 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000382}
383
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000384void ViEReceiver::NotifyReceiverOfFecPacket(const RTPHeader& header) {
385 int8_t last_media_payload_type =
386 rtp_payload_registry_->last_received_media_payload_type();
387 if (last_media_payload_type < 0) {
388 LOG(LS_WARNING) << "Failed to get last media payload type.";
389 return;
390 }
391 // Fake an empty media packet.
392 WebRtcRTPHeader rtp_header = {};
393 rtp_header.header = header;
394 rtp_header.header.payloadType = last_media_payload_type;
395 rtp_header.header.paddingLength = 0;
396 PayloadUnion payload_specific;
397 if (!rtp_payload_registry_->GetPayloadSpecifics(last_media_payload_type,
398 &payload_specific)) {
399 LOG(LS_WARNING) << "Failed to get payload specifics.";
400 return;
401 }
402 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000403 rtp_header.type.Video.rotation = kVideoRotation_0;
404 if (header.extension.hasVideoRotation) {
405 rtp_header.type.Video.rotation =
406 ConvertCVOByteToVideoRotation(header.extension.videoRotation);
407 }
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000408 OnReceivedPayloadData(NULL, 0, &rtp_header);
409}
410
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000411int ViEReceiver::InsertRTCPPacket(const uint8_t* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000412 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000413 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000414 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000415 if (!receiving_) {
416 return -1;
417 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000418
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000419 if (rtp_dump_) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000420 rtp_dump_->DumpPacket(rtcp_packet, rtcp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000421 }
Peter Boström6cff9cf2015-04-21 13:47:17 +0200422
423 std::list<RtpRtcp*>::iterator it = rtp_rtcp_simulcast_.begin();
424 while (it != rtp_rtcp_simulcast_.end()) {
425 RtpRtcp* rtp_rtcp = *it++;
426 rtp_rtcp->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
427 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000428 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000429 assert(rtp_rtcp_); // Should be set by owner at construction time.
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000430 int ret = rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
431 if (ret != 0) {
432 return ret;
433 }
434
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000435 int64_t rtt = 0;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000436 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, NULL, NULL, NULL);
437 if (rtt == 0) {
438 // Waiting for valid rtt.
439 return 0;
440 }
441 uint32_t ntp_secs = 0;
442 uint32_t ntp_frac = 0;
443 uint32_t rtp_timestamp = 0;
444 if (0 != rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
445 &rtp_timestamp)) {
446 // Waiting for RTCP.
447 return 0;
448 }
449 ntp_estimator_->UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000450
451 return 0;
452}
453
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000454void ViEReceiver::StartReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000455 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000456 receiving_ = true;
457}
458
459void ViEReceiver::StopReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000460 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000461 receiving_ = false;
462}
463
464int ViEReceiver::StartRTPDump(const char file_nameUTF8[1024]) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000465 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000466 if (rtp_dump_) {
467 // Restart it if it already exists and is started
468 rtp_dump_->Stop();
469 } else {
470 rtp_dump_ = RtpDump::CreateRtpDump();
471 if (rtp_dump_ == NULL) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000472 return -1;
473 }
474 }
475 if (rtp_dump_->Start(file_nameUTF8) != 0) {
476 RtpDump::DestroyRtpDump(rtp_dump_);
477 rtp_dump_ = NULL;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000478 return -1;
479 }
480 return 0;
481}
482
483int ViEReceiver::StopRTPDump() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000484 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000485 if (rtp_dump_) {
486 if (rtp_dump_->IsActive()) {
487 rtp_dump_->Stop();
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000488 }
489 RtpDump::DestroyRtpDump(rtp_dump_);
490 rtp_dump_ = NULL;
491 } else {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000492 return -1;
493 }
494 return 0;
495}
496
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000497ReceiveStatistics* ViEReceiver::GetReceiveStatistics() const {
498 return rtp_receive_statistics_.get();
499}
500
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000501bool ViEReceiver::IsPacketInOrder(const RTPHeader& header) const {
502 StreamStatistician* statistician =
503 rtp_receive_statistics_->GetStatistician(header.ssrc);
504 if (!statistician)
505 return false;
506 return statistician->IsPacketInOrder(header.sequenceNumber);
507}
508
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000509bool ViEReceiver::IsPacketRetransmitted(const RTPHeader& header,
510 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000511 // Retransmissions are handled separately if RTX is enabled.
512 if (rtp_payload_registry_->RtxEnabled())
513 return false;
514 StreamStatistician* statistician =
515 rtp_receive_statistics_->GetStatistician(header.ssrc);
516 if (!statistician)
517 return false;
518 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000519 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000520 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000521 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000522 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000523}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000524} // namespace webrtc