blob: 0f13aaf8c2e11140ec30a8a328ac3f000c71cf2c [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.org20182692013-12-12 22:54:25 +0000180 int rtp_packet_length) {
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000181 return InsertRTPPacket(static_cast<const int8_t*>(rtp_packet),
wu@webrtc.org20182692013-12-12 22:54:25 +0000182 rtp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000183}
184
185int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
186 int rtcp_packet_length) {
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000187 return InsertRTCPPacket(static_cast<const int8_t*>(rtcp_packet),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000188 rtcp_packet_length);
189}
190
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000191int32_t ViEReceiver::OnReceivedPayloadData(
192 const uint8_t* payload_data, const uint16_t payload_size,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000193 const WebRtcRTPHeader* rtp_header) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000194 if (vcm_->IncomingPacket(payload_data, payload_size, *rtp_header) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000195 // Check this...
196 return -1;
197 }
198 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000199}
200
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000201bool ViEReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
202 int rtp_packet_length) {
203 RTPHeader header;
204 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
205 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVideo, channel_id_,
206 "IncomingPacket invalid RTP header");
207 return false;
208 }
209 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000210 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000211}
212
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000213int ViEReceiver::InsertRTPPacket(const int8_t* rtp_packet,
wu@webrtc.org20182692013-12-12 22:54:25 +0000214 int rtp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000215 // TODO(mflodman) Change decrypt to get rid of this cast.
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000216 int8_t* tmp_ptr = const_cast<int8_t*>(rtp_packet);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000217 unsigned char* received_packet = reinterpret_cast<unsigned char*>(tmp_ptr);
218 int received_packet_length = rtp_packet_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000219
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000220 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000221 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000222 if (!receiving_) {
223 return -1;
224 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000225
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000226 if (external_decryption_) {
mflodman@webrtc.org34e83b82012-10-17 11:05:54 +0000227 int decrypted_length = kViEMaxMtu;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000228 external_decryption_->decrypt(channel_id_, received_packet,
229 decryption_buffer_, received_packet_length,
230 &decrypted_length);
231 if (decrypted_length <= 0) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000232 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
233 "RTP decryption failed");
niklase@google.com470e71d2011-07-07 08:21:25 +0000234 return -1;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000235 } else if (decrypted_length > kViEMaxMtu) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000236 WEBRTC_TRACE(webrtc::kTraceCritical, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000237 "InsertRTPPacket: %d bytes is allocated as RTP decrytption"
238 " output, external decryption used %d bytes. => memory is "
239 " now corrupted", kViEMaxMtu, decrypted_length);
240 return -1;
241 }
242 received_packet = decryption_buffer_;
243 received_packet_length = decrypted_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000244 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000245
246 if (rtp_dump_) {
247 rtp_dump_->DumpPacket(received_packet,
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000248 static_cast<uint16_t>(received_packet_length));
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000249 }
250 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000251 RTPHeader header;
252 if (!rtp_header_parser_->Parse(received_packet, received_packet_length,
253 &header)) {
254 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, channel_id_,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000255 "Incoming packet: Invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000256 return -1;
257 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000258 int payload_length = received_packet_length - header.headerLength;
wu@webrtc.org20182692013-12-12 22:54:25 +0000259 remote_bitrate_estimator_->IncomingPacket(TickTime::MillisecondTimestamp(),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000260 payload_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000261 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000262
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000263 bool in_order = IsPacketInOrder(header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000264 rtp_receive_statistics_->IncomingPacket(header, received_packet_length,
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000265 IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000266 rtp_payload_registry_->SetIncomingPayloadType(header);
267 return ReceivePacket(received_packet, received_packet_length, header,
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000268 in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000269}
270
271bool ViEReceiver::ReceivePacket(const uint8_t* packet,
272 int packet_length,
273 const RTPHeader& header,
274 bool in_order) {
275 if (rtp_payload_registry_->IsEncapsulated(header)) {
276 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
277 }
278 const uint8_t* payload = packet + header.headerLength;
279 int payload_length = packet_length - header.headerLength;
280 assert(payload_length >= 0);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000281 PayloadUnion payload_specific;
282 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
283 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000284 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000285 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000286 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
287 payload_specific, in_order);
288}
289
290bool ViEReceiver::ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
291 int packet_length,
292 const RTPHeader& header) {
293 if (rtp_payload_registry_->IsRed(header)) {
294 if (fec_receiver_->AddReceivedRedPacket(
295 header, packet, packet_length,
296 rtp_payload_registry_->ulpfec_payload_type()) != 0) {
297 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, channel_id_,
298 "Incoming RED packet error");
299 return false;
300 }
301 return fec_receiver_->ProcessReceivedFec() == 0;
302 } else if (rtp_payload_registry_->IsRtx(header)) {
303 // Remove the RTX header and parse the original RTP header.
304 if (packet_length < header.headerLength)
305 return false;
306 if (packet_length > static_cast<int>(sizeof(restored_packet_)))
307 return false;
308 CriticalSectionScoped cs(receive_cs_.get());
309 if (restored_packet_in_use_) {
310 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, channel_id_,
311 "Multiple RTX headers detected, dropping packet");
312 return false;
313 }
314 uint8_t* restored_packet_ptr = restored_packet_;
315 if (!rtp_payload_registry_->RestoreOriginalPacket(
316 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
317 header)) {
318 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, channel_id_,
319 "Incoming RTX packet: invalid RTP header");
320 return false;
321 }
322 restored_packet_in_use_ = true;
323 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
324 restored_packet_in_use_ = false;
325 return ret;
326 }
327 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000328}
329
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000330int ViEReceiver::InsertRTCPPacket(const int8_t* rtcp_packet,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000331 int rtcp_packet_length) {
332 // TODO(mflodman) Change decrypt to get rid of this cast.
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000333 int8_t* tmp_ptr = const_cast<int8_t*>(rtcp_packet);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000334 unsigned char* received_packet = reinterpret_cast<unsigned char*>(tmp_ptr);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000335 int received_packet_length = rtcp_packet_length;
336 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000337 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000338 if (!receiving_) {
339 return -1;
340 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000341
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000342 if (external_decryption_) {
mflodman@webrtc.org34e83b82012-10-17 11:05:54 +0000343 int decrypted_length = kViEMaxMtu;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000344 external_decryption_->decrypt_rtcp(channel_id_, received_packet,
345 decryption_buffer_,
346 received_packet_length,
347 &decrypted_length);
348 if (decrypted_length <= 0) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000349 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
350 "RTP decryption failed");
niklase@google.com470e71d2011-07-07 08:21:25 +0000351 return -1;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000352 } else if (decrypted_length > kViEMaxMtu) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000353 WEBRTC_TRACE(webrtc::kTraceCritical, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000354 "InsertRTCPPacket: %d bytes is allocated as RTP "
355 " decrytption output, external decryption used %d bytes. "
356 " => memory is now corrupted",
357 kViEMaxMtu, decrypted_length);
358 return -1;
359 }
360 received_packet = decryption_buffer_;
361 received_packet_length = decrypted_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000362 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000363
364 if (rtp_dump_) {
365 rtp_dump_->DumpPacket(
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000366 received_packet, static_cast<uint16_t>(received_packet_length));
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000367 }
368 }
369 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000370 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000371 std::list<RtpRtcp*>::iterator it = rtp_rtcp_simulcast_.begin();
372 while (it != rtp_rtcp_simulcast_.end()) {
373 RtpRtcp* rtp_rtcp = *it++;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000374 rtp_rtcp->IncomingRtcpPacket(received_packet, received_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000375 }
376 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000377 assert(rtp_rtcp_); // Should be set by owner at construction time.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000378 return rtp_rtcp_->IncomingRtcpPacket(received_packet, received_packet_length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000379}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000380
381void ViEReceiver::StartReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000382 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000383 receiving_ = true;
384}
385
386void ViEReceiver::StopReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000387 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000388 receiving_ = false;
389}
390
391int ViEReceiver::StartRTPDump(const char file_nameUTF8[1024]) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000392 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000393 if (rtp_dump_) {
394 // Restart it if it already exists and is started
395 rtp_dump_->Stop();
396 } else {
397 rtp_dump_ = RtpDump::CreateRtpDump();
398 if (rtp_dump_ == NULL) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000399 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000400 "StartRTPDump: Failed to create RTP dump");
401 return -1;
402 }
403 }
404 if (rtp_dump_->Start(file_nameUTF8) != 0) {
405 RtpDump::DestroyRtpDump(rtp_dump_);
406 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 start RTP dump");
409 return -1;
410 }
411 return 0;
412}
413
414int ViEReceiver::StopRTPDump() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000415 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000416 if (rtp_dump_) {
417 if (rtp_dump_->IsActive()) {
418 rtp_dump_->Stop();
419 } else {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000420 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000421 "StopRTPDump: Dump not active");
422 }
423 RtpDump::DestroyRtpDump(rtp_dump_);
424 rtp_dump_ = NULL;
425 } else {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000426 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000427 "StopRTPDump: RTP dump not started");
428 return -1;
429 }
430 return 0;
431}
432
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000433// TODO(holmer): To be moved to ViEChannelGroup.
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +0000434void ViEReceiver::EstimatedReceiveBandwidth(
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000435 unsigned int* available_bandwidth) const {
436 std::vector<unsigned int> ssrcs;
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +0000437
438 // LatestEstimate returns an error if there is no valid bitrate estimate, but
439 // ViEReceiver instead returns a zero estimate.
440 remote_bitrate_estimator_->LatestEstimate(&ssrcs, available_bandwidth);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000441 if (std::find(ssrcs.begin(), ssrcs.end(), rtp_receiver_->SSRC()) !=
mflodman@webrtc.orga066cbf2013-05-28 15:00:15 +0000442 ssrcs.end()) {
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000443 *available_bandwidth /= ssrcs.size();
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +0000444 } else {
445 *available_bandwidth = 0;
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000446 }
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000447}
448
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000449ReceiveStatistics* ViEReceiver::GetReceiveStatistics() const {
450 return rtp_receive_statistics_.get();
451}
452
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000453bool ViEReceiver::IsPacketInOrder(const RTPHeader& header) const {
454 StreamStatistician* statistician =
455 rtp_receive_statistics_->GetStatistician(header.ssrc);
456 if (!statistician)
457 return false;
458 return statistician->IsPacketInOrder(header.sequenceNumber);
459}
460
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000461bool ViEReceiver::IsPacketRetransmitted(const RTPHeader& header,
462 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000463 // Retransmissions are handled separately if RTX is enabled.
464 if (rtp_payload_registry_->RtxEnabled())
465 return false;
466 StreamStatistician* statistician =
467 rtp_receive_statistics_->GetStatistician(header.ssrc);
468 if (!statistician)
469 return false;
470 // Check if this is a retransmission.
471 uint16_t min_rtt = 0;
472 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000473 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000474 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000475}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000476} // namespace webrtc