blob: 8f359bf7dad1cf4bd8b6a6868982ec234e45d38a [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"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000027#include "webrtc/system_wrappers/interface/tick_util.h"
wu@webrtc.org66773a02014-05-07 17:09:44 +000028#include "webrtc/system_wrappers/interface/timestamp_extrapolator.h"
wu@webrtc.org88abf112014-05-14 16:53:51 +000029#include "webrtc/system_wrappers/interface/trace.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000030
31namespace webrtc {
32
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000033static const int kPacketLogIntervalMs = 10000;
34
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000035ViEReceiver::ViEReceiver(const int32_t channel_id,
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000036 VideoCodingModule* module_vcm,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000037 RemoteBitrateEstimator* remote_bitrate_estimator,
38 RtpFeedback* rtp_feedback)
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +000039 : receive_cs_(CriticalSectionWrapper::CreateCriticalSection()),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000040 clock_(Clock::GetRealTimeClock()),
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000041 rtp_header_parser_(RtpHeaderParser::Create()),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000042 rtp_payload_registry_(
43 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(false))),
44 rtp_receiver_(
45 RtpReceiver::CreateVideoReceiver(channel_id,
46 clock_,
47 this,
48 rtp_feedback,
49 rtp_payload_registry_.get())),
50 rtp_receive_statistics_(ReceiveStatistics::Create(clock_)),
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +000051 fec_receiver_(FecReceiver::Create(this)),
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000052 rtp_rtcp_(NULL),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000053 vcm_(module_vcm),
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000054 remote_bitrate_estimator_(remote_bitrate_estimator),
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000055 ntp_estimator_(new RemoteNtpTimeEstimator(clock_)),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000056 rtp_dump_(NULL),
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),
60 last_packet_log_ms_(-1) {
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000061 assert(remote_bitrate_estimator);
niklase@google.com470e71d2011-07-07 08:21:25 +000062}
63
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000064ViEReceiver::~ViEReceiver() {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000065 if (rtp_dump_) {
66 rtp_dump_->Stop();
67 RtpDump::DestroyRtpDump(rtp_dump_);
68 rtp_dump_ = NULL;
69 }
niklase@google.com470e71d2011-07-07 08:21:25 +000070}
71
wu@webrtc.org822fbd82013-08-15 23:38:54 +000072bool ViEReceiver::SetReceiveCodec(const VideoCodec& video_codec) {
73 int8_t old_pltype = -1;
74 if (rtp_payload_registry_->ReceivePayloadType(video_codec.plName,
75 kVideoPayloadTypeFrequency,
76 0,
77 video_codec.maxBitrate,
78 &old_pltype) != -1) {
79 rtp_payload_registry_->DeRegisterReceivePayload(old_pltype);
80 }
81
82 return RegisterPayload(video_codec);
83}
84
85bool ViEReceiver::RegisterPayload(const VideoCodec& video_codec) {
86 return rtp_receiver_->RegisterReceivePayload(video_codec.plName,
87 video_codec.plType,
88 kVideoPayloadTypeFrequency,
89 0,
90 video_codec.maxBitrate) == 0;
91}
92
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000093void ViEReceiver::SetNackStatus(bool enable,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000094 int max_nack_reordering_threshold) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000095 if (!enable) {
96 // Reset the threshold back to the lower default threshold when NACK is
97 // disabled since we no longer will be receiving retransmissions.
98 max_nack_reordering_threshold = kDefaultMaxReorderingThreshold;
99 }
100 rtp_receive_statistics_->SetMaxReorderingThreshold(
101 max_nack_reordering_threshold);
102 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000103}
104
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000105void ViEReceiver::SetRtxPayloadType(int payload_type) {
106 rtp_payload_registry_->SetRtxPayloadType(payload_type);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000107}
108
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000109void ViEReceiver::SetRtxSsrc(uint32_t ssrc) {
110 rtp_payload_registry_->SetRtxSsrc(ssrc);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000111}
112
113uint32_t ViEReceiver::GetRemoteSsrc() const {
114 return rtp_receiver_->SSRC();
115}
116
117int ViEReceiver::GetCsrcs(uint32_t* csrcs) const {
118 return rtp_receiver_->CSRCs(csrcs);
119}
120
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000121void ViEReceiver::SetRtpRtcpModule(RtpRtcp* module) {
122 rtp_rtcp_ = module;
123}
124
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000125RtpReceiver* ViEReceiver::GetRtpReceiver() const {
126 return rtp_receiver_.get();
127}
128
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000129void ViEReceiver::RegisterSimulcastRtpRtcpModules(
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000130 const std::list<RtpRtcp*>& rtp_modules) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000131 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000132 rtp_rtcp_simulcast_.clear();
133
134 if (!rtp_modules.empty()) {
135 rtp_rtcp_simulcast_.insert(rtp_rtcp_simulcast_.begin(),
136 rtp_modules.begin(),
137 rtp_modules.end());
138 }
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000139}
140
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000141bool ViEReceiver::SetReceiveTimestampOffsetStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000142 if (enable) {
143 return rtp_header_parser_->RegisterRtpHeaderExtension(
144 kRtpExtensionTransmissionTimeOffset, id);
145 } else {
146 return rtp_header_parser_->DeregisterRtpHeaderExtension(
147 kRtpExtensionTransmissionTimeOffset);
148 }
149}
150
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000151bool ViEReceiver::SetReceiveAbsoluteSendTimeStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000152 if (enable) {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000153 if (rtp_header_parser_->RegisterRtpHeaderExtension(
154 kRtpExtensionAbsoluteSendTime, id)) {
155 receiving_ast_enabled_ = true;
156 return true;
157 } else {
158 return false;
159 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000160 } else {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000161 receiving_ast_enabled_ = false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000162 return rtp_header_parser_->DeregisterRtpHeaderExtension(
163 kRtpExtensionAbsoluteSendTime);
164 }
165}
166
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000167int ViEReceiver::ReceivedRTPPacket(const void* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000168 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000169 const PacketTime& packet_time) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000170 return InsertRTPPacket(static_cast<const uint8_t*>(rtp_packet),
wu@webrtc.orga9890802013-12-13 00:21:03 +0000171 rtp_packet_length, packet_time);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000172}
173
174int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000175 size_t rtcp_packet_length) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000176 return InsertRTCPPacket(static_cast<const uint8_t*>(rtcp_packet),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000177 rtcp_packet_length);
178}
179
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000180int32_t ViEReceiver::OnReceivedPayloadData(const uint8_t* payload_data,
181 const size_t payload_size,
182 const WebRtcRTPHeader* rtp_header) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000183 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000184 rtp_header_with_ntp.ntp_time_ms =
185 ntp_estimator_->Estimate(rtp_header->header.timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000186 if (vcm_->IncomingPacket(payload_data,
187 payload_size,
188 rtp_header_with_ntp) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000189 // Check this...
190 return -1;
191 }
192 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000193}
194
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000195bool ViEReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000196 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000197 RTPHeader header;
198 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000199 return false;
200 }
201 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000202 bool in_order = IsPacketInOrder(header);
203 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000204}
205
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000206void ViEReceiver::ReceivedBWEPacket(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000207 int64_t arrival_time_ms, size_t payload_size, const RTPHeader& header) {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000208 // Only forward if the incoming packet *and* the channel are both configured
209 // to receive absolute sender time. RTP time stamps may have different rates
210 // for audio and video and shouldn't be mixed.
211 if (header.extension.hasAbsoluteSendTime && receiving_ast_enabled_) {
212 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_size,
213 header);
214 }
215}
216
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000217int ViEReceiver::InsertRTPPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000218 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000219 const PacketTime& packet_time) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000220 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000221 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000222 if (!receiving_) {
223 return -1;
224 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000225 if (rtp_dump_) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000226 rtp_dump_->DumpPacket(rtp_packet, rtp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000227 }
228 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000229
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000230 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000231 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000232 &header)) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000233 return -1;
234 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000235 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000236 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000237 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000238 if (packet_time.timestamp != -1)
239 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
240 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000241 arrival_time_ms = now_ms;
242
243 {
244 // Periodically log the RTP header of incoming packets.
245 CriticalSectionScoped cs(receive_cs_.get());
246 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
247 std::stringstream ss;
248 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
249 << static_cast<int>(header.payloadType) << ", timestamp: "
250 << header.timestamp << ", sequence number: " << header.sequenceNumber
251 << ", arrival time: " << arrival_time_ms;
252 if (header.extension.hasTransmissionTimeOffset)
253 ss << ", toffset: " << header.extension.transmissionTimeOffset;
254 if (header.extension.hasAbsoluteSendTime)
255 ss << ", abs send time: " << header.extension.absoluteSendTime;
256 LOG(LS_INFO) << ss.str();
257 last_packet_log_ms_ = now_ms;
258 }
259 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000260
261 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000262 payload_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000263 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000264
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000265 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000266 rtp_payload_registry_->SetIncomingPayloadType(header);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000267 int ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order)
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000268 ? 0
269 : -1;
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000270 // Update receive statistics after ReceivePacket.
271 // Receive statistics will be reset if the payload type changes (make sure
272 // that the first packet is included in the stats).
273 rtp_receive_statistics_->IncomingPacket(
274 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
275 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000276}
277
278bool ViEReceiver::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000279 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000280 const RTPHeader& header,
281 bool in_order) {
282 if (rtp_payload_registry_->IsEncapsulated(header)) {
283 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
284 }
285 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000286 assert(packet_length >= header.headerLength);
287 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000288 PayloadUnion payload_specific;
289 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
290 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000291 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000292 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000293 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
294 payload_specific, in_order);
295}
296
297bool ViEReceiver::ParseAndHandleEncapsulatingHeader(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 if (rtp_payload_registry_->IsRed(header)) {
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000301 int8_t ulpfec_pt = rtp_payload_registry_->ulpfec_payload_type();
302 if (packet[header.headerLength] == ulpfec_pt)
303 rtp_receive_statistics_->FecPacketReceived(header.ssrc);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000304 if (fec_receiver_->AddReceivedRedPacket(
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000305 header, packet, packet_length, ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000306 return false;
307 }
308 return fec_receiver_->ProcessReceivedFec() == 0;
309 } else if (rtp_payload_registry_->IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000310 if (header.headerLength + header.paddingLength == packet_length) {
311 // This is an empty packet and should be silently dropped before trying to
312 // parse the RTX header.
313 return true;
314 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000315 // Remove the RTX header and parse the original RTP header.
316 if (packet_length < header.headerLength)
317 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000318 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000319 return false;
320 CriticalSectionScoped cs(receive_cs_.get());
321 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000322 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000323 return false;
324 }
325 uint8_t* restored_packet_ptr = restored_packet_;
326 if (!rtp_payload_registry_->RestoreOriginalPacket(
327 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
328 header)) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000329 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000330 return false;
331 }
332 restored_packet_in_use_ = true;
333 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
334 restored_packet_in_use_ = false;
335 return ret;
336 }
337 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000338}
339
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000340int ViEReceiver::InsertRTCPPacket(const uint8_t* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000341 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000342 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000343 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000344 if (!receiving_) {
345 return -1;
346 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000347
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000348 if (rtp_dump_) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000349 rtp_dump_->DumpPacket(rtcp_packet, rtcp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000350 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000351
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000352 std::list<RtpRtcp*>::iterator it = rtp_rtcp_simulcast_.begin();
353 while (it != rtp_rtcp_simulcast_.end()) {
354 RtpRtcp* rtp_rtcp = *it++;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000355 rtp_rtcp->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000356 }
357 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000358 assert(rtp_rtcp_); // Should be set by owner at construction time.
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000359 int ret = rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
360 if (ret != 0) {
361 return ret;
362 }
363
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000364 uint16_t rtt = 0;
365 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, NULL, NULL, NULL);
366 if (rtt == 0) {
367 // Waiting for valid rtt.
368 return 0;
369 }
370 uint32_t ntp_secs = 0;
371 uint32_t ntp_frac = 0;
372 uint32_t rtp_timestamp = 0;
373 if (0 != rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
374 &rtp_timestamp)) {
375 // Waiting for RTCP.
376 return 0;
377 }
378 ntp_estimator_->UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000379
380 return 0;
381}
382
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000383void ViEReceiver::StartReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000384 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000385 receiving_ = true;
386}
387
388void ViEReceiver::StopReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000389 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000390 receiving_ = false;
391}
392
393int ViEReceiver::StartRTPDump(const char file_nameUTF8[1024]) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000394 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000395 if (rtp_dump_) {
396 // Restart it if it already exists and is started
397 rtp_dump_->Stop();
398 } else {
399 rtp_dump_ = RtpDump::CreateRtpDump();
400 if (rtp_dump_ == NULL) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000401 return -1;
402 }
403 }
404 if (rtp_dump_->Start(file_nameUTF8) != 0) {
405 RtpDump::DestroyRtpDump(rtp_dump_);
406 rtp_dump_ = NULL;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000407 return -1;
408 }
409 return 0;
410}
411
412int ViEReceiver::StopRTPDump() {
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 if (rtp_dump_->IsActive()) {
416 rtp_dump_->Stop();
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000417 }
418 RtpDump::DestroyRtpDump(rtp_dump_);
419 rtp_dump_ = NULL;
420 } else {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000421 return -1;
422 }
423 return 0;
424}
425
jiayl@webrtc.org1f64f062014-02-10 19:12:14 +0000426void ViEReceiver::GetReceiveBandwidthEstimatorStats(
427 ReceiveBandwidthEstimatorStats* output) const {
428 remote_bitrate_estimator_->GetStats(output);
429}
430
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000431ReceiveStatistics* ViEReceiver::GetReceiveStatistics() const {
432 return rtp_receive_statistics_.get();
433}
434
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000435bool ViEReceiver::IsPacketInOrder(const RTPHeader& header) const {
436 StreamStatistician* statistician =
437 rtp_receive_statistics_->GetStatistician(header.ssrc);
438 if (!statistician)
439 return false;
440 return statistician->IsPacketInOrder(header.sequenceNumber);
441}
442
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000443bool ViEReceiver::IsPacketRetransmitted(const RTPHeader& header,
444 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000445 // Retransmissions are handled separately if RTX is enabled.
446 if (rtp_payload_registry_->RtxEnabled())
447 return false;
448 StreamStatistician* statistician =
449 rtp_receive_statistics_->GetStatistician(header.ssrc);
450 if (!statistician)
451 return false;
452 // Check if this is a retransmission.
453 uint16_t min_rtt = 0;
454 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000455 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000456 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000457}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000458} // namespace webrtc