blob: 6adcffdb66f5411087e75ab736fab901e520228f [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
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +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
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000113bool ViEReceiver::GetRtxSsrc(uint32_t* ssrc) const {
114 return rtp_payload_registry_->GetRtxSsrc(ssrc);
115}
116
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000117uint32_t ViEReceiver::GetRemoteSsrc() const {
118 return rtp_receiver_->SSRC();
119}
120
121int ViEReceiver::GetCsrcs(uint32_t* csrcs) const {
122 return rtp_receiver_->CSRCs(csrcs);
123}
124
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000125void ViEReceiver::SetRtpRtcpModule(RtpRtcp* module) {
126 rtp_rtcp_ = module;
127}
128
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000129RtpReceiver* ViEReceiver::GetRtpReceiver() const {
130 return rtp_receiver_.get();
131}
132
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000133void ViEReceiver::RegisterSimulcastRtpRtcpModules(
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000134 const std::list<RtpRtcp*>& rtp_modules) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000135 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000136 rtp_rtcp_simulcast_.clear();
137
138 if (!rtp_modules.empty()) {
139 rtp_rtcp_simulcast_.insert(rtp_rtcp_simulcast_.begin(),
140 rtp_modules.begin(),
141 rtp_modules.end());
142 }
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000143}
144
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000145bool ViEReceiver::SetReceiveTimestampOffsetStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000146 if (enable) {
147 return rtp_header_parser_->RegisterRtpHeaderExtension(
148 kRtpExtensionTransmissionTimeOffset, id);
149 } else {
150 return rtp_header_parser_->DeregisterRtpHeaderExtension(
151 kRtpExtensionTransmissionTimeOffset);
152 }
153}
154
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000155bool ViEReceiver::SetReceiveAbsoluteSendTimeStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000156 if (enable) {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000157 if (rtp_header_parser_->RegisterRtpHeaderExtension(
158 kRtpExtensionAbsoluteSendTime, id)) {
159 receiving_ast_enabled_ = true;
160 return true;
161 } else {
162 return false;
163 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000164 } else {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000165 receiving_ast_enabled_ = false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000166 return rtp_header_parser_->DeregisterRtpHeaderExtension(
167 kRtpExtensionAbsoluteSendTime);
168 }
169}
170
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000171int ViEReceiver::ReceivedRTPPacket(const void* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000172 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000173 const PacketTime& packet_time) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000174 return InsertRTPPacket(static_cast<const uint8_t*>(rtp_packet),
wu@webrtc.orga9890802013-12-13 00:21:03 +0000175 rtp_packet_length, packet_time);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000176}
177
178int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000179 size_t rtcp_packet_length) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000180 return InsertRTCPPacket(static_cast<const uint8_t*>(rtcp_packet),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000181 rtcp_packet_length);
182}
183
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000184int32_t ViEReceiver::OnReceivedPayloadData(const uint8_t* payload_data,
185 const size_t payload_size,
186 const WebRtcRTPHeader* rtp_header) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000187 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000188 rtp_header_with_ntp.ntp_time_ms =
189 ntp_estimator_->Estimate(rtp_header->header.timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000190 if (vcm_->IncomingPacket(payload_data,
191 payload_size,
192 rtp_header_with_ntp) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000193 // Check this...
194 return -1;
195 }
196 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000197}
198
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000199bool ViEReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000200 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000201 RTPHeader header;
202 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000203 return false;
204 }
205 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000206 bool in_order = IsPacketInOrder(header);
207 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000208}
209
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000210void ViEReceiver::ReceivedBWEPacket(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000211 int64_t arrival_time_ms, size_t payload_size, const RTPHeader& header) {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000212 // Only forward if the incoming packet *and* the channel are both configured
213 // to receive absolute sender time. RTP time stamps may have different rates
214 // for audio and video and shouldn't be mixed.
215 if (header.extension.hasAbsoluteSendTime && receiving_ast_enabled_) {
216 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_size,
217 header);
218 }
219}
220
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000221int ViEReceiver::InsertRTPPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000222 size_t rtp_packet_length,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000223 const PacketTime& packet_time) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000224 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000225 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000226 if (!receiving_) {
227 return -1;
228 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000229 if (rtp_dump_) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000230 rtp_dump_->DumpPacket(rtp_packet, rtp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000231 }
232 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000233
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000234 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000235 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000236 &header)) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000237 return -1;
238 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000239 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000240 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000241 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000242 if (packet_time.timestamp != -1)
243 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
244 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000245 arrival_time_ms = now_ms;
246
247 {
248 // Periodically log the RTP header of incoming packets.
249 CriticalSectionScoped cs(receive_cs_.get());
250 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
251 std::stringstream ss;
252 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
253 << static_cast<int>(header.payloadType) << ", timestamp: "
254 << header.timestamp << ", sequence number: " << header.sequenceNumber
255 << ", arrival time: " << arrival_time_ms;
256 if (header.extension.hasTransmissionTimeOffset)
257 ss << ", toffset: " << header.extension.transmissionTimeOffset;
258 if (header.extension.hasAbsoluteSendTime)
259 ss << ", abs send time: " << header.extension.absoluteSendTime;
260 LOG(LS_INFO) << ss.str();
261 last_packet_log_ms_ = now_ms;
262 }
263 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000264
265 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000266 payload_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000267 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000268
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000269 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000270 rtp_payload_registry_->SetIncomingPayloadType(header);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000271 int ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order)
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000272 ? 0
273 : -1;
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000274 // Update receive statistics after ReceivePacket.
275 // Receive statistics will be reset if the payload type changes (make sure
276 // that the first packet is included in the stats).
277 rtp_receive_statistics_->IncomingPacket(
278 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
279 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000280}
281
282bool ViEReceiver::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000283 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000284 const RTPHeader& header,
285 bool in_order) {
286 if (rtp_payload_registry_->IsEncapsulated(header)) {
287 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
288 }
289 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000290 assert(packet_length >= header.headerLength);
291 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000292 PayloadUnion payload_specific;
293 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
294 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000295 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000296 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000297 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
298 payload_specific, in_order);
299}
300
301bool ViEReceiver::ParseAndHandleEncapsulatingHeader(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 if (rtp_payload_registry_->IsRed(header)) {
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000305 int8_t ulpfec_pt = rtp_payload_registry_->ulpfec_payload_type();
306 if (packet[header.headerLength] == ulpfec_pt)
307 rtp_receive_statistics_->FecPacketReceived(header.ssrc);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000308 if (fec_receiver_->AddReceivedRedPacket(
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000309 header, packet, packet_length, ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000310 return false;
311 }
312 return fec_receiver_->ProcessReceivedFec() == 0;
313 } else if (rtp_payload_registry_->IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000314 if (header.headerLength + header.paddingLength == packet_length) {
315 // This is an empty packet and should be silently dropped before trying to
316 // parse the RTX header.
317 return true;
318 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000319 // Remove the RTX header and parse the original RTP header.
320 if (packet_length < header.headerLength)
321 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000322 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000323 return false;
324 CriticalSectionScoped cs(receive_cs_.get());
325 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000326 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000327 return false;
328 }
329 uint8_t* restored_packet_ptr = restored_packet_;
330 if (!rtp_payload_registry_->RestoreOriginalPacket(
331 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
332 header)) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000333 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000334 return false;
335 }
336 restored_packet_in_use_ = true;
337 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
338 restored_packet_in_use_ = false;
339 return ret;
340 }
341 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000342}
343
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000344int ViEReceiver::InsertRTCPPacket(const uint8_t* rtcp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000345 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000346 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000347 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000348 if (!receiving_) {
349 return -1;
350 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000351
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000352 if (rtp_dump_) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000353 rtp_dump_->DumpPacket(rtcp_packet, rtcp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000354 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000355
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000356 std::list<RtpRtcp*>::iterator it = rtp_rtcp_simulcast_.begin();
357 while (it != rtp_rtcp_simulcast_.end()) {
358 RtpRtcp* rtp_rtcp = *it++;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000359 rtp_rtcp->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000360 }
361 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000362 assert(rtp_rtcp_); // Should be set by owner at construction time.
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000363 int ret = rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
364 if (ret != 0) {
365 return ret;
366 }
367
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000368 int64_t rtt = 0;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000369 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, NULL, NULL, NULL);
370 if (rtt == 0) {
371 // Waiting for valid rtt.
372 return 0;
373 }
374 uint32_t ntp_secs = 0;
375 uint32_t ntp_frac = 0;
376 uint32_t rtp_timestamp = 0;
377 if (0 != rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
378 &rtp_timestamp)) {
379 // Waiting for RTCP.
380 return 0;
381 }
382 ntp_estimator_->UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000383
384 return 0;
385}
386
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000387void ViEReceiver::StartReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000388 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000389 receiving_ = true;
390}
391
392void ViEReceiver::StopReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000393 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000394 receiving_ = false;
395}
396
397int ViEReceiver::StartRTPDump(const char file_nameUTF8[1024]) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000398 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000399 if (rtp_dump_) {
400 // Restart it if it already exists and is started
401 rtp_dump_->Stop();
402 } else {
403 rtp_dump_ = RtpDump::CreateRtpDump();
404 if (rtp_dump_ == NULL) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000405 return -1;
406 }
407 }
408 if (rtp_dump_->Start(file_nameUTF8) != 0) {
409 RtpDump::DestroyRtpDump(rtp_dump_);
410 rtp_dump_ = NULL;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000411 return -1;
412 }
413 return 0;
414}
415
416int ViEReceiver::StopRTPDump() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000417 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000418 if (rtp_dump_) {
419 if (rtp_dump_->IsActive()) {
420 rtp_dump_->Stop();
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000421 }
422 RtpDump::DestroyRtpDump(rtp_dump_);
423 rtp_dump_ = NULL;
424 } else {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000425 return -1;
426 }
427 return 0;
428}
429
jiayl@webrtc.org1f64f062014-02-10 19:12:14 +0000430void ViEReceiver::GetReceiveBandwidthEstimatorStats(
431 ReceiveBandwidthEstimatorStats* output) const {
432 remote_bitrate_estimator_->GetStats(output);
433}
434
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000435ReceiveStatistics* ViEReceiver::GetReceiveStatistics() const {
436 return rtp_receive_statistics_.get();
437}
438
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000439bool ViEReceiver::IsPacketInOrder(const RTPHeader& header) const {
440 StreamStatistician* statistician =
441 rtp_receive_statistics_->GetStatistician(header.ssrc);
442 if (!statistician)
443 return false;
444 return statistician->IsPacketInOrder(header.sequenceNumber);
445}
446
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000447bool ViEReceiver::IsPacketRetransmitted(const RTPHeader& header,
448 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000449 // Retransmissions are handled separately if RTX is enabled.
450 if (rtp_payload_registry_->RtxEnabled())
451 return false;
452 StreamStatistician* statistician =
453 rtp_receive_statistics_->GetStatistician(header.ssrc);
454 if (!statistician)
455 return false;
456 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000457 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000458 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000459 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000460 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000461}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000462} // namespace webrtc