blob: 2946c4a08f9760b30e5f77047e47ab1776ac7a0d [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);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000272 rtp_receive_statistics_->IncomingPacket(header, received_packet_length,
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000273 IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000274 rtp_payload_registry_->SetIncomingPayloadType(header);
275 return ReceivePacket(received_packet, received_packet_length, header,
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000276 in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000277}
278
279bool ViEReceiver::ReceivePacket(const uint8_t* packet,
280 int packet_length,
281 const RTPHeader& header,
282 bool in_order) {
283 if (rtp_payload_registry_->IsEncapsulated(header)) {
284 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
285 }
286 const uint8_t* payload = packet + header.headerLength;
287 int payload_length = packet_length - header.headerLength;
288 assert(payload_length >= 0);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000289 PayloadUnion payload_specific;
290 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
291 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000292 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000293 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000294 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
295 payload_specific, in_order);
296}
297
298bool ViEReceiver::ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
299 int packet_length,
300 const RTPHeader& header) {
301 if (rtp_payload_registry_->IsRed(header)) {
302 if (fec_receiver_->AddReceivedRedPacket(
303 header, packet, packet_length,
304 rtp_payload_registry_->ulpfec_payload_type()) != 0) {
305 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, channel_id_,
306 "Incoming RED packet error");
307 return false;
308 }
309 return fec_receiver_->ProcessReceivedFec() == 0;
310 } else if (rtp_payload_registry_->IsRtx(header)) {
311 // Remove the RTX header and parse the original RTP header.
312 if (packet_length < header.headerLength)
313 return false;
314 if (packet_length > static_cast<int>(sizeof(restored_packet_)))
315 return false;
316 CriticalSectionScoped cs(receive_cs_.get());
317 if (restored_packet_in_use_) {
318 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, channel_id_,
319 "Multiple RTX headers detected, dropping packet");
320 return false;
321 }
322 uint8_t* restored_packet_ptr = restored_packet_;
323 if (!rtp_payload_registry_->RestoreOriginalPacket(
324 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
325 header)) {
326 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, channel_id_,
327 "Incoming RTX packet: invalid RTP header");
328 return false;
329 }
330 restored_packet_in_use_ = true;
331 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
332 restored_packet_in_use_ = false;
333 return ret;
334 }
335 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000336}
337
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000338int ViEReceiver::InsertRTCPPacket(const int8_t* rtcp_packet,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000339 int rtcp_packet_length) {
340 // TODO(mflodman) Change decrypt to get rid of this cast.
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000341 int8_t* tmp_ptr = const_cast<int8_t*>(rtcp_packet);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000342 unsigned char* received_packet = reinterpret_cast<unsigned char*>(tmp_ptr);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000343 int received_packet_length = rtcp_packet_length;
344 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000345 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000346 if (!receiving_) {
347 return -1;
348 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000349
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000350 if (external_decryption_) {
mflodman@webrtc.org34e83b82012-10-17 11:05:54 +0000351 int decrypted_length = kViEMaxMtu;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000352 external_decryption_->decrypt_rtcp(channel_id_, received_packet,
353 decryption_buffer_,
354 received_packet_length,
355 &decrypted_length);
356 if (decrypted_length <= 0) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000357 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
358 "RTP decryption failed");
niklase@google.com470e71d2011-07-07 08:21:25 +0000359 return -1;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000360 } else if (decrypted_length > kViEMaxMtu) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000361 WEBRTC_TRACE(webrtc::kTraceCritical, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000362 "InsertRTCPPacket: %d bytes is allocated as RTP "
363 " decrytption output, external decryption used %d bytes. "
364 " => memory is now corrupted",
365 kViEMaxMtu, decrypted_length);
366 return -1;
367 }
368 received_packet = decryption_buffer_;
369 received_packet_length = decrypted_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000370 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000371
372 if (rtp_dump_) {
373 rtp_dump_->DumpPacket(
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000374 received_packet, static_cast<uint16_t>(received_packet_length));
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000375 }
376 }
377 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000378 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000379 std::list<RtpRtcp*>::iterator it = rtp_rtcp_simulcast_.begin();
380 while (it != rtp_rtcp_simulcast_.end()) {
381 RtpRtcp* rtp_rtcp = *it++;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000382 rtp_rtcp->IncomingRtcpPacket(received_packet, received_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000383 }
384 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000385 assert(rtp_rtcp_); // Should be set by owner at construction time.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000386 return rtp_rtcp_->IncomingRtcpPacket(received_packet, received_packet_length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000387}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000388
389void ViEReceiver::StartReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000390 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000391 receiving_ = true;
392}
393
394void ViEReceiver::StopReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000395 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000396 receiving_ = false;
397}
398
399int ViEReceiver::StartRTPDump(const char file_nameUTF8[1024]) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000400 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000401 if (rtp_dump_) {
402 // Restart it if it already exists and is started
403 rtp_dump_->Stop();
404 } else {
405 rtp_dump_ = RtpDump::CreateRtpDump();
406 if (rtp_dump_ == NULL) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000407 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000408 "StartRTPDump: Failed to create RTP dump");
409 return -1;
410 }
411 }
412 if (rtp_dump_->Start(file_nameUTF8) != 0) {
413 RtpDump::DestroyRtpDump(rtp_dump_);
414 rtp_dump_ = NULL;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000415 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000416 "StartRTPDump: Failed to start RTP dump");
417 return -1;
418 }
419 return 0;
420}
421
422int ViEReceiver::StopRTPDump() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000423 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000424 if (rtp_dump_) {
425 if (rtp_dump_->IsActive()) {
426 rtp_dump_->Stop();
427 } else {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000428 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000429 "StopRTPDump: Dump not active");
430 }
431 RtpDump::DestroyRtpDump(rtp_dump_);
432 rtp_dump_ = NULL;
433 } else {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000434 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000435 "StopRTPDump: RTP dump not started");
436 return -1;
437 }
438 return 0;
439}
440
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000441// TODO(holmer): To be moved to ViEChannelGroup.
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +0000442void ViEReceiver::EstimatedReceiveBandwidth(
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000443 unsigned int* available_bandwidth) const {
444 std::vector<unsigned int> ssrcs;
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +0000445
446 // LatestEstimate returns an error if there is no valid bitrate estimate, but
447 // ViEReceiver instead returns a zero estimate.
448 remote_bitrate_estimator_->LatestEstimate(&ssrcs, available_bandwidth);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000449 if (std::find(ssrcs.begin(), ssrcs.end(), rtp_receiver_->SSRC()) !=
mflodman@webrtc.orga066cbf2013-05-28 15:00:15 +0000450 ssrcs.end()) {
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000451 *available_bandwidth /= ssrcs.size();
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +0000452 } else {
453 *available_bandwidth = 0;
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000454 }
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000455}
456
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000457ReceiveStatistics* ViEReceiver::GetReceiveStatistics() const {
458 return rtp_receive_statistics_.get();
459}
460
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000461bool ViEReceiver::IsPacketInOrder(const RTPHeader& header) const {
462 StreamStatistician* statistician =
463 rtp_receive_statistics_->GetStatistician(header.ssrc);
464 if (!statistician)
465 return false;
466 return statistician->IsPacketInOrder(header.sequenceNumber);
467}
468
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000469bool ViEReceiver::IsPacketRetransmitted(const RTPHeader& header,
470 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000471 // Retransmissions are handled separately if RTX is enabled.
472 if (rtp_payload_registry_->RtxEnabled())
473 return false;
474 StreamStatistician* statistician =
475 rtp_receive_statistics_->GetStatistician(header.ssrc);
476 if (!statistician)
477 return false;
478 // Check if this is a retransmission.
479 uint16_t min_rtt = 0;
480 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000481 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000482 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000483}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000484} // namespace webrtc