blob: 3b9c0196857af2a21993687c88669234d670c7aa [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 external_decryption_(NULL),
49 decryption_buffer_(NULL),
50 rtp_dump_(NULL),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000051 receiving_(false),
52 restored_packet_in_use_(false) {
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000053 assert(remote_bitrate_estimator);
niklase@google.com470e71d2011-07-07 08:21:25 +000054}
55
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000056ViEReceiver::~ViEReceiver() {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000057 if (decryption_buffer_) {
58 delete[] decryption_buffer_;
59 decryption_buffer_ = NULL;
60 }
61 if (rtp_dump_) {
62 rtp_dump_->Stop();
63 RtpDump::DestroyRtpDump(rtp_dump_);
64 rtp_dump_ = NULL;
65 }
niklase@google.com470e71d2011-07-07 08:21:25 +000066}
67
wu@webrtc.org822fbd82013-08-15 23:38:54 +000068bool ViEReceiver::SetReceiveCodec(const VideoCodec& video_codec) {
69 int8_t old_pltype = -1;
70 if (rtp_payload_registry_->ReceivePayloadType(video_codec.plName,
71 kVideoPayloadTypeFrequency,
72 0,
73 video_codec.maxBitrate,
74 &old_pltype) != -1) {
75 rtp_payload_registry_->DeRegisterReceivePayload(old_pltype);
76 }
77
78 return RegisterPayload(video_codec);
79}
80
81bool ViEReceiver::RegisterPayload(const VideoCodec& video_codec) {
82 return rtp_receiver_->RegisterReceivePayload(video_codec.plName,
83 video_codec.plType,
84 kVideoPayloadTypeFrequency,
85 0,
86 video_codec.maxBitrate) == 0;
87}
88
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000089void ViEReceiver::SetNackStatus(bool enable,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000090 int max_nack_reordering_threshold) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000091 if (!enable) {
92 // Reset the threshold back to the lower default threshold when NACK is
93 // disabled since we no longer will be receiving retransmissions.
94 max_nack_reordering_threshold = kDefaultMaxReorderingThreshold;
95 }
96 rtp_receive_statistics_->SetMaxReorderingThreshold(
97 max_nack_reordering_threshold);
98 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000099}
100
101void ViEReceiver::SetRtxStatus(bool enable, uint32_t ssrc) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000102 rtp_payload_registry_->SetRtxStatus(enable, ssrc);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000103}
104
105void ViEReceiver::SetRtxPayloadType(uint32_t payload_type) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000106 rtp_payload_registry_->SetRtxPayloadType(payload_type);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000107}
108
109uint32_t ViEReceiver::GetRemoteSsrc() const {
110 return rtp_receiver_->SSRC();
111}
112
113int ViEReceiver::GetCsrcs(uint32_t* csrcs) const {
114 return rtp_receiver_->CSRCs(csrcs);
115}
116
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000117int ViEReceiver::RegisterExternalDecryption(Encryption* decryption) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000118 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000119 if (external_decryption_) {
120 return -1;
121 }
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000122 decryption_buffer_ = new uint8_t[kViEMaxMtu];
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000123 if (decryption_buffer_ == NULL) {
124 return -1;
125 }
126 external_decryption_ = decryption;
127 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000128}
129
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000130int ViEReceiver::DeregisterExternalDecryption() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000131 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000132 if (external_decryption_ == NULL) {
133 return -1;
134 }
135 external_decryption_ = NULL;
136 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000137}
138
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000139void ViEReceiver::SetRtpRtcpModule(RtpRtcp* module) {
140 rtp_rtcp_ = module;
141}
142
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000143RtpReceiver* ViEReceiver::GetRtpReceiver() const {
144 return rtp_receiver_.get();
145}
146
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000147void ViEReceiver::RegisterSimulcastRtpRtcpModules(
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000148 const std::list<RtpRtcp*>& rtp_modules) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000149 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000150 rtp_rtcp_simulcast_.clear();
151
152 if (!rtp_modules.empty()) {
153 rtp_rtcp_simulcast_.insert(rtp_rtcp_simulcast_.begin(),
154 rtp_modules.begin(),
155 rtp_modules.end());
156 }
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000157}
158
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000159bool ViEReceiver::SetReceiveTimestampOffsetStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000160 if (enable) {
161 return rtp_header_parser_->RegisterRtpHeaderExtension(
162 kRtpExtensionTransmissionTimeOffset, id);
163 } else {
164 return rtp_header_parser_->DeregisterRtpHeaderExtension(
165 kRtpExtensionTransmissionTimeOffset);
166 }
167}
168
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000169bool ViEReceiver::SetReceiveAbsoluteSendTimeStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000170 if (enable) {
171 return rtp_header_parser_->RegisterRtpHeaderExtension(
172 kRtpExtensionAbsoluteSendTime, id);
173 } else {
174 return rtp_header_parser_->DeregisterRtpHeaderExtension(
175 kRtpExtensionAbsoluteSendTime);
176 }
177}
178
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000179int ViEReceiver::ReceivedRTPPacket(const void* rtp_packet,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000180 int rtp_packet_length,
181 const PacketTime& packet_time) {
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000182 return InsertRTPPacket(static_cast<const int8_t*>(rtp_packet),
wu@webrtc.orga9890802013-12-13 00:21:03 +0000183 rtp_packet_length, packet_time);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000184}
185
186int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
187 int rtcp_packet_length) {
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000188 return InsertRTCPPacket(static_cast<const int8_t*>(rtcp_packet),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000189 rtcp_packet_length);
190}
191
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000192int32_t ViEReceiver::OnReceivedPayloadData(
193 const uint8_t* payload_data, const uint16_t payload_size,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000194 const WebRtcRTPHeader* rtp_header) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000195 if (vcm_->IncomingPacket(payload_data, payload_size, *rtp_header) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000196 // Check this...
197 return -1;
198 }
199 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000200}
201
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000202bool ViEReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
203 int rtp_packet_length) {
204 RTPHeader header;
205 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
206 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVideo, channel_id_,
207 "IncomingPacket invalid RTP header");
208 return false;
209 }
210 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000211 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000212}
213
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000214int ViEReceiver::InsertRTPPacket(const int8_t* rtp_packet,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000215 int rtp_packet_length,
216 const PacketTime& packet_time) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000217 // TODO(mflodman) Change decrypt to get rid of this cast.
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000218 int8_t* tmp_ptr = const_cast<int8_t*>(rtp_packet);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000219 unsigned char* received_packet = reinterpret_cast<unsigned char*>(tmp_ptr);
220 int received_packet_length = rtp_packet_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000221
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000222 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000223 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000224 if (!receiving_) {
225 return -1;
226 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000227
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000228 if (external_decryption_) {
mflodman@webrtc.org34e83b82012-10-17 11:05:54 +0000229 int decrypted_length = kViEMaxMtu;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000230 external_decryption_->decrypt(channel_id_, received_packet,
231 decryption_buffer_, received_packet_length,
232 &decrypted_length);
233 if (decrypted_length <= 0) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000234 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
235 "RTP decryption failed");
niklase@google.com470e71d2011-07-07 08:21:25 +0000236 return -1;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000237 } else if (decrypted_length > kViEMaxMtu) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000238 WEBRTC_TRACE(webrtc::kTraceCritical, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000239 "InsertRTPPacket: %d bytes is allocated as RTP decrytption"
240 " output, external decryption used %d bytes. => memory is "
241 " now corrupted", kViEMaxMtu, decrypted_length);
242 return -1;
243 }
244 received_packet = decryption_buffer_;
245 received_packet_length = decrypted_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000246 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000247
248 if (rtp_dump_) {
249 rtp_dump_->DumpPacket(received_packet,
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000250 static_cast<uint16_t>(received_packet_length));
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000251 }
252 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000253 RTPHeader header;
254 if (!rtp_header_parser_->Parse(received_packet, received_packet_length,
255 &header)) {
256 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, channel_id_,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000257 "Incoming packet: Invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000258 return -1;
259 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000260 int payload_length = received_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000261 int64_t arrival_time_ms;
262 if (packet_time.timestamp != -1)
263 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
264 else
265 arrival_time_ms = TickTime::MillisecondTimestamp();
266
267 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000268 payload_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000269 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000270
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000271 bool in_order = IsPacketInOrder(header);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000272 rtp_receive_statistics_->IncomingPacket(
273 header, received_packet_length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000274 rtp_payload_registry_->SetIncomingPayloadType(header);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000275 return ReceivePacket(
276 received_packet, received_packet_length, header, in_order)
277 ? 0
278 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000279}
280
281bool ViEReceiver::ReceivePacket(const uint8_t* packet,
282 int packet_length,
283 const RTPHeader& header,
284 bool in_order) {
285 if (rtp_payload_registry_->IsEncapsulated(header)) {
286 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
287 }
288 const uint8_t* payload = packet + header.headerLength;
289 int payload_length = packet_length - header.headerLength;
290 assert(payload_length >= 0);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000291 PayloadUnion payload_specific;
292 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
293 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000294 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000295 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000296 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
297 payload_specific, in_order);
298}
299
300bool ViEReceiver::ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
301 int packet_length,
302 const RTPHeader& header) {
303 if (rtp_payload_registry_->IsRed(header)) {
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000304 int8_t ulpfec_pt = rtp_payload_registry_->ulpfec_payload_type();
305 if (packet[header.headerLength] == ulpfec_pt)
306 rtp_receive_statistics_->FecPacketReceived(header.ssrc);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000307 if (fec_receiver_->AddReceivedRedPacket(
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000308 header, packet, packet_length, ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000309 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, channel_id_,
310 "Incoming RED packet error");
311 return false;
312 }
313 return fec_receiver_->ProcessReceivedFec() == 0;
314 } else if (rtp_payload_registry_->IsRtx(header)) {
315 // Remove the RTX header and parse the original RTP header.
316 if (packet_length < header.headerLength)
317 return false;
318 if (packet_length > static_cast<int>(sizeof(restored_packet_)))
319 return false;
320 CriticalSectionScoped cs(receive_cs_.get());
321 if (restored_packet_in_use_) {
322 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, channel_id_,
323 "Multiple RTX headers detected, dropping packet");
324 return false;
325 }
326 uint8_t* restored_packet_ptr = restored_packet_;
327 if (!rtp_payload_registry_->RestoreOriginalPacket(
328 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
329 header)) {
330 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, channel_id_,
331 "Incoming RTX packet: invalid RTP header");
332 return false;
333 }
334 restored_packet_in_use_ = true;
335 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
336 restored_packet_in_use_ = false;
337 return ret;
338 }
339 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000340}
341
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000342int ViEReceiver::InsertRTCPPacket(const int8_t* rtcp_packet,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000343 int rtcp_packet_length) {
344 // TODO(mflodman) Change decrypt to get rid of this cast.
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000345 int8_t* tmp_ptr = const_cast<int8_t*>(rtcp_packet);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000346 unsigned char* received_packet = reinterpret_cast<unsigned char*>(tmp_ptr);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000347 int received_packet_length = rtcp_packet_length;
348 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000349 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000350 if (!receiving_) {
351 return -1;
352 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000353
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000354 if (external_decryption_) {
mflodman@webrtc.org34e83b82012-10-17 11:05:54 +0000355 int decrypted_length = kViEMaxMtu;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000356 external_decryption_->decrypt_rtcp(channel_id_, received_packet,
357 decryption_buffer_,
358 received_packet_length,
359 &decrypted_length);
360 if (decrypted_length <= 0) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000361 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
362 "RTP decryption failed");
niklase@google.com470e71d2011-07-07 08:21:25 +0000363 return -1;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000364 } else if (decrypted_length > kViEMaxMtu) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000365 WEBRTC_TRACE(webrtc::kTraceCritical, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000366 "InsertRTCPPacket: %d bytes is allocated as RTP "
367 " decrytption output, external decryption used %d bytes. "
368 " => memory is now corrupted",
369 kViEMaxMtu, decrypted_length);
370 return -1;
371 }
372 received_packet = decryption_buffer_;
373 received_packet_length = decrypted_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000374 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000375
376 if (rtp_dump_) {
377 rtp_dump_->DumpPacket(
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000378 received_packet, static_cast<uint16_t>(received_packet_length));
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000379 }
380 }
381 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000382 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000383 std::list<RtpRtcp*>::iterator it = rtp_rtcp_simulcast_.begin();
384 while (it != rtp_rtcp_simulcast_.end()) {
385 RtpRtcp* rtp_rtcp = *it++;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000386 rtp_rtcp->IncomingRtcpPacket(received_packet, received_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000387 }
388 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000389 assert(rtp_rtcp_); // Should be set by owner at construction time.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000390 return rtp_rtcp_->IncomingRtcpPacket(received_packet, received_packet_length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000391}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000392
393void ViEReceiver::StartReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000394 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000395 receiving_ = true;
396}
397
398void ViEReceiver::StopReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000399 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000400 receiving_ = false;
401}
402
403int ViEReceiver::StartRTPDump(const char file_nameUTF8[1024]) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000404 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000405 if (rtp_dump_) {
406 // Restart it if it already exists and is started
407 rtp_dump_->Stop();
408 } else {
409 rtp_dump_ = RtpDump::CreateRtpDump();
410 if (rtp_dump_ == NULL) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000411 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000412 "StartRTPDump: Failed to create RTP dump");
413 return -1;
414 }
415 }
416 if (rtp_dump_->Start(file_nameUTF8) != 0) {
417 RtpDump::DestroyRtpDump(rtp_dump_);
418 rtp_dump_ = NULL;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000419 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000420 "StartRTPDump: Failed to start RTP dump");
421 return -1;
422 }
423 return 0;
424}
425
426int ViEReceiver::StopRTPDump() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000427 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000428 if (rtp_dump_) {
429 if (rtp_dump_->IsActive()) {
430 rtp_dump_->Stop();
431 } else {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000432 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000433 "StopRTPDump: Dump not active");
434 }
435 RtpDump::DestroyRtpDump(rtp_dump_);
436 rtp_dump_ = NULL;
437 } else {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000438 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000439 "StopRTPDump: RTP dump not started");
440 return -1;
441 }
442 return 0;
443}
444
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000445// TODO(holmer): To be moved to ViEChannelGroup.
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +0000446void ViEReceiver::EstimatedReceiveBandwidth(
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000447 unsigned int* available_bandwidth) const {
448 std::vector<unsigned int> ssrcs;
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +0000449
450 // LatestEstimate returns an error if there is no valid bitrate estimate, but
451 // ViEReceiver instead returns a zero estimate.
452 remote_bitrate_estimator_->LatestEstimate(&ssrcs, available_bandwidth);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000453 if (std::find(ssrcs.begin(), ssrcs.end(), rtp_receiver_->SSRC()) !=
mflodman@webrtc.orga066cbf2013-05-28 15:00:15 +0000454 ssrcs.end()) {
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000455 *available_bandwidth /= ssrcs.size();
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +0000456 } else {
457 *available_bandwidth = 0;
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000458 }
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000459}
460
jiayl@webrtc.org1f64f062014-02-10 19:12:14 +0000461void ViEReceiver::GetReceiveBandwidthEstimatorStats(
462 ReceiveBandwidthEstimatorStats* output) const {
463 remote_bitrate_estimator_->GetStats(output);
464}
465
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000466ReceiveStatistics* ViEReceiver::GetReceiveStatistics() const {
467 return rtp_receive_statistics_.get();
468}
469
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000470bool ViEReceiver::IsPacketInOrder(const RTPHeader& header) const {
471 StreamStatistician* statistician =
472 rtp_receive_statistics_->GetStatistician(header.ssrc);
473 if (!statistician)
474 return false;
475 return statistician->IsPacketInOrder(header.sequenceNumber);
476}
477
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000478bool ViEReceiver::IsPacketRetransmitted(const RTPHeader& header,
479 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000480 // Retransmissions are handled separately if RTX is enabled.
481 if (rtp_payload_registry_->RtxEnabled())
482 return false;
483 StreamStatistician* statistician =
484 rtp_receive_statistics_->GetStatistician(header.ssrc);
485 if (!statistician)
486 return false;
487 // Check if this is a retransmission.
488 uint16_t min_rtt = 0;
489 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000490 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000491 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000492}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000493} // namespace webrtc