blob: 18b6e347ac3d28fd4a7932a253f06bacb3dba693 [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öm6cff9cf2015-04-21 13:47:17 +0200150void ViEReceiver::RegisterSimulcastRtpRtcpModules(
151 const std::list<RtpRtcp*>& rtp_modules) {
152 CriticalSectionScoped cs(receive_cs_.get());
153 rtp_rtcp_simulcast_.clear();
154
155 if (!rtp_modules.empty()) {
156 rtp_rtcp_simulcast_.insert(rtp_rtcp_simulcast_.begin(),
157 rtp_modules.begin(),
158 rtp_modules.end());
159 }
160}
161
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000162bool ViEReceiver::SetReceiveTimestampOffsetStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000163 if (enable) {
164 return rtp_header_parser_->RegisterRtpHeaderExtension(
165 kRtpExtensionTransmissionTimeOffset, id);
166 } else {
167 return rtp_header_parser_->DeregisterRtpHeaderExtension(
168 kRtpExtensionTransmissionTimeOffset);
169 }
170}
171
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000172bool ViEReceiver::SetReceiveAbsoluteSendTimeStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000173 if (enable) {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000174 if (rtp_header_parser_->RegisterRtpHeaderExtension(
175 kRtpExtensionAbsoluteSendTime, id)) {
176 receiving_ast_enabled_ = true;
177 return true;
178 } else {
179 return false;
180 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000181 } else {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000182 receiving_ast_enabled_ = false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000183 return rtp_header_parser_->DeregisterRtpHeaderExtension(
184 kRtpExtensionAbsoluteSendTime);
185 }
186}
187
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000188bool ViEReceiver::SetReceiveVideoRotationStatus(bool enable, int id) {
189 if (enable) {
190 if (rtp_header_parser_->RegisterRtpHeaderExtension(
191 kRtpExtensionVideoRotation, id)) {
192 receiving_cvo_enabled_ = true;
193 return true;
194 } else {
195 return false;
196 }
197 } else {
198 receiving_cvo_enabled_ = false;
199 return rtp_header_parser_->DeregisterRtpHeaderExtension(
200 kRtpExtensionVideoRotation);
201 }
202}
203
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000204int ViEReceiver::ReceivedRTPPacket(const void* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000205 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000206 const PacketTime& packet_time) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000207 return InsertRTPPacket(static_cast<const uint8_t*>(rtp_packet),
wu@webrtc.orga9890802013-12-13 00:21:03 +0000208 rtp_packet_length, packet_time);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000209}
210
211int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000212 size_t rtcp_packet_length) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000213 return InsertRTCPPacket(static_cast<const uint8_t*>(rtcp_packet),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000214 rtcp_packet_length);
215}
216
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000217int32_t ViEReceiver::OnReceivedPayloadData(const uint8_t* payload_data,
218 const size_t payload_size,
219 const WebRtcRTPHeader* rtp_header) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000220 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000221 rtp_header_with_ntp.ntp_time_ms =
222 ntp_estimator_->Estimate(rtp_header->header.timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000223 if (vcm_->IncomingPacket(payload_data,
224 payload_size,
225 rtp_header_with_ntp) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000226 // Check this...
227 return -1;
228 }
229 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000230}
231
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000232bool ViEReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000233 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000234 RTPHeader header;
235 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000236 return false;
237 }
238 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000239 bool in_order = IsPacketInOrder(header);
240 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000241}
242
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000243int ViEReceiver::InsertRTPPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000244 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000245 const PacketTime& packet_time) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000246 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000247 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000248 if (!receiving_) {
249 return -1;
250 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000251 }
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
Stefan Holmerff4ea932015-06-18 16:01:33 +0200284 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_length,
285 header, true);
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;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000385 rtp_header.type.Video.rotation = kVideoRotation_0;
386 if (header.extension.hasVideoRotation) {
387 rtp_header.type.Video.rotation =
388 ConvertCVOByteToVideoRotation(header.extension.videoRotation);
389 }
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000390 OnReceivedPayloadData(NULL, 0, &rtp_header);
391}
392
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000393int ViEReceiver::InsertRTCPPacket(const uint8_t* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000394 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000395 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000396 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000397 if (!receiving_) {
398 return -1;
399 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000400
Peter Boström6cff9cf2015-04-21 13:47:17 +0200401 std::list<RtpRtcp*>::iterator it = rtp_rtcp_simulcast_.begin();
402 while (it != rtp_rtcp_simulcast_.end()) {
403 RtpRtcp* rtp_rtcp = *it++;
404 rtp_rtcp->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
405 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000406 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000407 assert(rtp_rtcp_); // Should be set by owner at construction time.
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000408 int ret = rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
409 if (ret != 0) {
410 return ret;
411 }
412
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000413 int64_t rtt = 0;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000414 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, NULL, NULL, NULL);
415 if (rtt == 0) {
416 // Waiting for valid rtt.
417 return 0;
418 }
419 uint32_t ntp_secs = 0;
420 uint32_t ntp_frac = 0;
421 uint32_t rtp_timestamp = 0;
422 if (0 != rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
423 &rtp_timestamp)) {
424 // Waiting for RTCP.
425 return 0;
426 }
427 ntp_estimator_->UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000428
429 return 0;
430}
431
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000432void ViEReceiver::StartReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000433 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000434 receiving_ = true;
435}
436
437void ViEReceiver::StopReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000438 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000439 receiving_ = false;
440}
441
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000442ReceiveStatistics* ViEReceiver::GetReceiveStatistics() const {
443 return rtp_receive_statistics_.get();
444}
445
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000446bool ViEReceiver::IsPacketInOrder(const RTPHeader& header) const {
447 StreamStatistician* statistician =
448 rtp_receive_statistics_->GetStatistician(header.ssrc);
449 if (!statistician)
450 return false;
451 return statistician->IsPacketInOrder(header.sequenceNumber);
452}
453
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000454bool ViEReceiver::IsPacketRetransmitted(const RTPHeader& header,
455 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000456 // Retransmissions are handled separately if RTX is enabled.
457 if (rtp_payload_registry_->RtxEnabled())
458 return false;
459 StreamStatistician* statistician =
460 rtp_receive_statistics_->GetStatistician(header.ssrc);
461 if (!statistician)
462 return false;
463 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000464 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000465 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000466 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000467 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000468}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000469} // namespace webrtc