blob: 87b06288b424b1cae71dfbff15594111b36187ee [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"
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000018#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000019#include "webrtc/modules/rtp_rtcp/interface/rtp_payload_registry.h"
20#include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000021#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
22#include "webrtc/modules/utility/interface/rtp_dump.h"
23#include "webrtc/modules/video_coding/main/interface/video_coding.h"
24#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
25#include "webrtc/system_wrappers/interface/tick_util.h"
26#include "webrtc/system_wrappers/interface/trace.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000027
28namespace webrtc {
29
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000030ViEReceiver::ViEReceiver(const int32_t channel_id,
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000031 VideoCodingModule* module_vcm,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000032 RemoteBitrateEstimator* remote_bitrate_estimator,
33 RtpFeedback* rtp_feedback)
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +000034 : receive_cs_(CriticalSectionWrapper::CreateCriticalSection()),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000035 channel_id_(channel_id),
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000036 rtp_header_parser_(RtpHeaderParser::Create()),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000037 rtp_payload_registry_(new RTPPayloadRegistry(
38 channel_id, RTPPayloadStrategy::CreateStrategy(false))),
39 rtp_receiver_(RtpReceiver::CreateVideoReceiver(
40 channel_id, Clock::GetRealTimeClock(), this, rtp_feedback,
41 rtp_payload_registry_.get())),
42 rtp_receive_statistics_(ReceiveStatistics::Create(
43 Clock::GetRealTimeClock())),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000044 fec_receiver_(FecReceiver::Create(channel_id, this)),
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000045 rtp_rtcp_(NULL),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000046 vcm_(module_vcm),
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000047 remote_bitrate_estimator_(remote_bitrate_estimator),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000048 rtp_dump_(NULL),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000049 receiving_(false),
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +000050 restored_packet_in_use_(false),
51 receiving_ast_enabled_(false) {
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000052 assert(remote_bitrate_estimator);
niklase@google.com470e71d2011-07-07 08:21:25 +000053}
54
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000055ViEReceiver::~ViEReceiver() {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000056 if (rtp_dump_) {
57 rtp_dump_->Stop();
58 RtpDump::DestroyRtpDump(rtp_dump_);
59 rtp_dump_ = NULL;
60 }
niklase@google.com470e71d2011-07-07 08:21:25 +000061}
62
wu@webrtc.org822fbd82013-08-15 23:38:54 +000063bool ViEReceiver::SetReceiveCodec(const VideoCodec& video_codec) {
64 int8_t old_pltype = -1;
65 if (rtp_payload_registry_->ReceivePayloadType(video_codec.plName,
66 kVideoPayloadTypeFrequency,
67 0,
68 video_codec.maxBitrate,
69 &old_pltype) != -1) {
70 rtp_payload_registry_->DeRegisterReceivePayload(old_pltype);
71 }
72
73 return RegisterPayload(video_codec);
74}
75
76bool ViEReceiver::RegisterPayload(const VideoCodec& video_codec) {
77 return rtp_receiver_->RegisterReceivePayload(video_codec.plName,
78 video_codec.plType,
79 kVideoPayloadTypeFrequency,
80 0,
81 video_codec.maxBitrate) == 0;
82}
83
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000084void ViEReceiver::SetNackStatus(bool enable,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000085 int max_nack_reordering_threshold) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000086 if (!enable) {
87 // Reset the threshold back to the lower default threshold when NACK is
88 // disabled since we no longer will be receiving retransmissions.
89 max_nack_reordering_threshold = kDefaultMaxReorderingThreshold;
90 }
91 rtp_receive_statistics_->SetMaxReorderingThreshold(
92 max_nack_reordering_threshold);
93 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000094}
95
96void ViEReceiver::SetRtxStatus(bool enable, uint32_t ssrc) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000097 rtp_payload_registry_->SetRtxStatus(enable, ssrc);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000098}
99
100void ViEReceiver::SetRtxPayloadType(uint32_t payload_type) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000101 rtp_payload_registry_->SetRtxPayloadType(payload_type);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000102}
103
104uint32_t ViEReceiver::GetRemoteSsrc() const {
105 return rtp_receiver_->SSRC();
106}
107
108int ViEReceiver::GetCsrcs(uint32_t* csrcs) const {
109 return rtp_receiver_->CSRCs(csrcs);
110}
111
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000112void ViEReceiver::SetRtpRtcpModule(RtpRtcp* module) {
113 rtp_rtcp_ = module;
114}
115
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000116RtpReceiver* ViEReceiver::GetRtpReceiver() const {
117 return rtp_receiver_.get();
118}
119
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000120void ViEReceiver::RegisterSimulcastRtpRtcpModules(
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000121 const std::list<RtpRtcp*>& rtp_modules) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000122 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000123 rtp_rtcp_simulcast_.clear();
124
125 if (!rtp_modules.empty()) {
126 rtp_rtcp_simulcast_.insert(rtp_rtcp_simulcast_.begin(),
127 rtp_modules.begin(),
128 rtp_modules.end());
129 }
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000130}
131
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000132bool ViEReceiver::SetReceiveTimestampOffsetStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000133 if (enable) {
134 return rtp_header_parser_->RegisterRtpHeaderExtension(
135 kRtpExtensionTransmissionTimeOffset, id);
136 } else {
137 return rtp_header_parser_->DeregisterRtpHeaderExtension(
138 kRtpExtensionTransmissionTimeOffset);
139 }
140}
141
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000142bool ViEReceiver::SetReceiveAbsoluteSendTimeStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000143 if (enable) {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000144 if (rtp_header_parser_->RegisterRtpHeaderExtension(
145 kRtpExtensionAbsoluteSendTime, id)) {
146 receiving_ast_enabled_ = true;
147 return true;
148 } else {
149 return false;
150 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000151 } else {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000152 receiving_ast_enabled_ = false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000153 return rtp_header_parser_->DeregisterRtpHeaderExtension(
154 kRtpExtensionAbsoluteSendTime);
155 }
156}
157
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000158int ViEReceiver::ReceivedRTPPacket(const void* rtp_packet,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000159 int rtp_packet_length,
160 const PacketTime& packet_time) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000161 return InsertRTPPacket(static_cast<const uint8_t*>(rtp_packet),
wu@webrtc.orga9890802013-12-13 00:21:03 +0000162 rtp_packet_length, packet_time);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000163}
164
165int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
166 int rtcp_packet_length) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000167 return InsertRTCPPacket(static_cast<const uint8_t*>(rtcp_packet),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000168 rtcp_packet_length);
169}
170
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000171int32_t ViEReceiver::OnReceivedPayloadData(
172 const uint8_t* payload_data, const uint16_t payload_size,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000173 const WebRtcRTPHeader* rtp_header) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000174 if (vcm_->IncomingPacket(payload_data, payload_size, *rtp_header) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000175 // Check this...
176 return -1;
177 }
178 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000179}
180
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000181bool ViEReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
182 int rtp_packet_length) {
183 RTPHeader header;
184 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
185 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVideo, channel_id_,
186 "IncomingPacket invalid RTP header");
187 return false;
188 }
189 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000190 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000191}
192
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000193void ViEReceiver::ReceivedBWEPacket(
194 int64_t arrival_time_ms, int payload_size, const RTPHeader& header) {
195 // Only forward if the incoming packet *and* the channel are both configured
196 // to receive absolute sender time. RTP time stamps may have different rates
197 // for audio and video and shouldn't be mixed.
198 if (header.extension.hasAbsoluteSendTime && receiving_ast_enabled_) {
199 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_size,
200 header);
201 }
202}
203
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000204int ViEReceiver::InsertRTPPacket(const uint8_t* rtp_packet,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000205 int rtp_packet_length,
206 const PacketTime& packet_time) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000207 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000208 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000209 if (!receiving_) {
210 return -1;
211 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000212 if (rtp_dump_) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000213 rtp_dump_->DumpPacket(rtp_packet,
214 static_cast<uint16_t>(rtp_packet_length));
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000215 }
216 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000217
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000218 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000219 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000220 &header)) {
221 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, channel_id_,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000222 "Incoming packet: Invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000223 return -1;
224 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000225 int payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000226 int64_t arrival_time_ms;
227 if (packet_time.timestamp != -1)
228 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
229 else
230 arrival_time_ms = TickTime::MillisecondTimestamp();
231
232 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000233 payload_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000234 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000235
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000236 bool in_order = IsPacketInOrder(header);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000237 rtp_receive_statistics_->IncomingPacket(
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000238 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000239 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000240 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order)
241 ? 0
242 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000243}
244
245bool ViEReceiver::ReceivePacket(const uint8_t* packet,
246 int packet_length,
247 const RTPHeader& header,
248 bool in_order) {
249 if (rtp_payload_registry_->IsEncapsulated(header)) {
250 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
251 }
252 const uint8_t* payload = packet + header.headerLength;
253 int payload_length = packet_length - header.headerLength;
254 assert(payload_length >= 0);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000255 PayloadUnion payload_specific;
256 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
257 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000258 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000259 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000260 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
261 payload_specific, in_order);
262}
263
264bool ViEReceiver::ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
265 int packet_length,
266 const RTPHeader& header) {
267 if (rtp_payload_registry_->IsRed(header)) {
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000268 int8_t ulpfec_pt = rtp_payload_registry_->ulpfec_payload_type();
269 if (packet[header.headerLength] == ulpfec_pt)
270 rtp_receive_statistics_->FecPacketReceived(header.ssrc);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000271 if (fec_receiver_->AddReceivedRedPacket(
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000272 header, packet, packet_length, ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000273 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, channel_id_,
274 "Incoming RED packet error");
275 return false;
276 }
277 return fec_receiver_->ProcessReceivedFec() == 0;
278 } else if (rtp_payload_registry_->IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000279 if (header.headerLength + header.paddingLength == packet_length) {
280 // This is an empty packet and should be silently dropped before trying to
281 // parse the RTX header.
282 return true;
283 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000284 // Remove the RTX header and parse the original RTP header.
285 if (packet_length < header.headerLength)
286 return false;
287 if (packet_length > static_cast<int>(sizeof(restored_packet_)))
288 return false;
289 CriticalSectionScoped cs(receive_cs_.get());
290 if (restored_packet_in_use_) {
291 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, channel_id_,
292 "Multiple RTX headers detected, dropping packet");
293 return false;
294 }
295 uint8_t* restored_packet_ptr = restored_packet_;
296 if (!rtp_payload_registry_->RestoreOriginalPacket(
297 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
298 header)) {
299 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, channel_id_,
300 "Incoming RTX packet: invalid RTP header");
301 return false;
302 }
303 restored_packet_in_use_ = true;
304 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
305 restored_packet_in_use_ = false;
306 return ret;
307 }
308 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000309}
310
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000311int ViEReceiver::InsertRTCPPacket(const uint8_t* rtcp_packet,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000312 int rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000313 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000314 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000315 if (!receiving_) {
316 return -1;
317 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000318
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000319 if (rtp_dump_) {
320 rtp_dump_->DumpPacket(
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000321 rtcp_packet, static_cast<uint16_t>(rtcp_packet_length));
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000322 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000323
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000324 std::list<RtpRtcp*>::iterator it = rtp_rtcp_simulcast_.begin();
325 while (it != rtp_rtcp_simulcast_.end()) {
326 RtpRtcp* rtp_rtcp = *it++;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000327 rtp_rtcp->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000328 }
329 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000330 assert(rtp_rtcp_); // Should be set by owner at construction time.
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000331 return rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000332}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000333
334void ViEReceiver::StartReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000335 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000336 receiving_ = true;
337}
338
339void ViEReceiver::StopReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000340 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000341 receiving_ = false;
342}
343
344int ViEReceiver::StartRTPDump(const char file_nameUTF8[1024]) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000345 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000346 if (rtp_dump_) {
347 // Restart it if it already exists and is started
348 rtp_dump_->Stop();
349 } else {
350 rtp_dump_ = RtpDump::CreateRtpDump();
351 if (rtp_dump_ == NULL) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000352 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000353 "StartRTPDump: Failed to create RTP dump");
354 return -1;
355 }
356 }
357 if (rtp_dump_->Start(file_nameUTF8) != 0) {
358 RtpDump::DestroyRtpDump(rtp_dump_);
359 rtp_dump_ = NULL;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000360 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000361 "StartRTPDump: Failed to start RTP dump");
362 return -1;
363 }
364 return 0;
365}
366
367int ViEReceiver::StopRTPDump() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000368 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000369 if (rtp_dump_) {
370 if (rtp_dump_->IsActive()) {
371 rtp_dump_->Stop();
372 } else {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000373 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000374 "StopRTPDump: Dump not active");
375 }
376 RtpDump::DestroyRtpDump(rtp_dump_);
377 rtp_dump_ = NULL;
378 } else {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000379 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000380 "StopRTPDump: RTP dump not started");
381 return -1;
382 }
383 return 0;
384}
385
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000386// TODO(holmer): To be moved to ViEChannelGroup.
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +0000387void ViEReceiver::EstimatedReceiveBandwidth(
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000388 unsigned int* available_bandwidth) const {
389 std::vector<unsigned int> ssrcs;
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +0000390
391 // LatestEstimate returns an error if there is no valid bitrate estimate, but
392 // ViEReceiver instead returns a zero estimate.
393 remote_bitrate_estimator_->LatestEstimate(&ssrcs, available_bandwidth);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000394 if (std::find(ssrcs.begin(), ssrcs.end(), rtp_receiver_->SSRC()) !=
mflodman@webrtc.orga066cbf2013-05-28 15:00:15 +0000395 ssrcs.end()) {
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000396 *available_bandwidth /= ssrcs.size();
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +0000397 } else {
398 *available_bandwidth = 0;
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000399 }
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000400}
401
jiayl@webrtc.org1f64f062014-02-10 19:12:14 +0000402void ViEReceiver::GetReceiveBandwidthEstimatorStats(
403 ReceiveBandwidthEstimatorStats* output) const {
404 remote_bitrate_estimator_->GetStats(output);
405}
406
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000407ReceiveStatistics* ViEReceiver::GetReceiveStatistics() const {
408 return rtp_receive_statistics_.get();
409}
410
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000411bool ViEReceiver::IsPacketInOrder(const RTPHeader& header) const {
412 StreamStatistician* statistician =
413 rtp_receive_statistics_->GetStatistician(header.ssrc);
414 if (!statistician)
415 return false;
416 return statistician->IsPacketInOrder(header.sequenceNumber);
417}
418
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000419bool ViEReceiver::IsPacketRetransmitted(const RTPHeader& header,
420 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000421 // Retransmissions are handled separately if RTX is enabled.
422 if (rtp_payload_registry_->RtxEnabled())
423 return false;
424 StreamStatistician* statistician =
425 rtp_receive_statistics_->GetStatistician(header.ssrc);
426 if (!statistician)
427 return false;
428 // Check if this is a retransmission.
429 uint16_t min_rtt = 0;
430 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000431 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000432 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000433}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000434} // namespace webrtc