blob: 1e47c895c76b69f69b5fb2240c6244388c2d7eed [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
mflodman@webrtc.org1b1cd782012-06-28 06:34:08 +000011#include "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
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000015#include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
mflodman@webrtc.org1b1cd782012-06-28 06:34:08 +000016#include "modules/rtp_rtcp/interface/rtp_rtcp.h"
17#include "modules/utility/interface/rtp_dump.h"
18#include "modules/video_coding/main/interface/video_coding.h"
19#include "system_wrappers/interface/critical_section_wrapper.h"
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000020#include "system_wrappers/interface/tick_util.h"
mflodman@webrtc.org1b1cd782012-06-28 06:34:08 +000021#include "system_wrappers/interface/trace.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
23namespace webrtc {
24
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000025ViEReceiver::ViEReceiver(const int32_t channel_id,
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000026 VideoCodingModule* module_vcm,
27 RemoteBitrateEstimator* remote_bitrate_estimator)
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +000028 : receive_cs_(CriticalSectionWrapper::CreateCriticalSection()),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000029 channel_id_(channel_id),
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000030 rtp_rtcp_(NULL),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000031 vcm_(module_vcm),
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000032 remote_bitrate_estimator_(remote_bitrate_estimator),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000033 external_decryption_(NULL),
34 decryption_buffer_(NULL),
35 rtp_dump_(NULL),
36 receiving_(false) {
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000037 assert(remote_bitrate_estimator);
niklase@google.com470e71d2011-07-07 08:21:25 +000038}
39
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000040ViEReceiver::~ViEReceiver() {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000041 if (decryption_buffer_) {
42 delete[] decryption_buffer_;
43 decryption_buffer_ = NULL;
44 }
45 if (rtp_dump_) {
46 rtp_dump_->Stop();
47 RtpDump::DestroyRtpDump(rtp_dump_);
48 rtp_dump_ = NULL;
49 }
niklase@google.com470e71d2011-07-07 08:21:25 +000050}
51
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000052int ViEReceiver::RegisterExternalDecryption(Encryption* decryption) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +000053 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000054 if (external_decryption_) {
55 return -1;
56 }
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000057 decryption_buffer_ = new uint8_t[kViEMaxMtu];
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000058 if (decryption_buffer_ == NULL) {
59 return -1;
60 }
61 external_decryption_ = decryption;
62 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000063}
64
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000065int ViEReceiver::DeregisterExternalDecryption() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +000066 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000067 if (external_decryption_ == NULL) {
68 return -1;
69 }
70 external_decryption_ = NULL;
71 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000072}
73
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000074void ViEReceiver::SetRtpRtcpModule(RtpRtcp* module) {
75 rtp_rtcp_ = module;
76}
77
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +000078void ViEReceiver::RegisterSimulcastRtpRtcpModules(
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000079 const std::list<RtpRtcp*>& rtp_modules) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +000080 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000081 rtp_rtcp_simulcast_.clear();
82
83 if (!rtp_modules.empty()) {
84 rtp_rtcp_simulcast_.insert(rtp_rtcp_simulcast_.begin(),
85 rtp_modules.begin(),
86 rtp_modules.end());
87 }
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +000088}
89
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000090int ViEReceiver::ReceivedRTPPacket(const void* rtp_packet,
91 int rtp_packet_length) {
92 if (!receiving_) {
93 return -1;
94 }
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000095 return InsertRTPPacket(static_cast<const int8_t*>(rtp_packet),
96 rtp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000097}
98
99int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
100 int rtcp_packet_length) {
101 if (!receiving_) {
102 return -1;
103 }
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000104 return InsertRTCPPacket(static_cast<const int8_t*>(rtcp_packet),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000105 rtcp_packet_length);
106}
107
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000108int32_t ViEReceiver::OnReceivedPayloadData(
109 const uint8_t* payload_data, const uint16_t payload_size,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000110 const WebRtcRTPHeader* rtp_header) {
111 if (rtp_header == NULL) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000112 return 0;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000113 }
114
stefan@webrtc.org976a7e62012-09-21 13:20:21 +0000115 // TODO(holmer): Make sure packets reconstructed using FEC are not passed to
116 // the bandwidth estimator.
stefan@webrtc.org1a2a6dd2012-10-31 12:21:13 +0000117 const int packet_size = payload_size + rtp_header->header.paddingLength;
stefan@webrtc.org976a7e62012-09-21 13:20:21 +0000118 uint32_t compensated_timestamp = rtp_header->header.timestamp +
119 rtp_header->extension.transmissionTimeOffset;
phoglund@webrtc.org4cebe6c2012-11-07 13:37:19 +0000120 remote_bitrate_estimator_->IncomingPacket(
121 rtp_header->header.ssrc, packet_size,
122 TickTime::MillisecondTimestamp(), compensated_timestamp);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000123 if (vcm_->IncomingPacket(payload_data, payload_size, *rtp_header) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000124 // Check this...
125 return -1;
126 }
127 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000128}
129
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000130void ViEReceiver::OnSendReportReceived(const int32_t id,
131 const uint32_t senderSSRC,
stefan@webrtc.org976a7e62012-09-21 13:20:21 +0000132 uint32_t ntp_secs,
133 uint32_t ntp_frac,
134 uint32_t timestamp) {
135 remote_bitrate_estimator_->IncomingRtcp(senderSSRC, ntp_secs, ntp_frac,
136 timestamp);
137}
138
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000139int ViEReceiver::InsertRTPPacket(const int8_t* rtp_packet,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000140 int rtp_packet_length) {
141 // TODO(mflodman) Change decrypt to get rid of this cast.
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000142 int8_t* tmp_ptr = const_cast<int8_t*>(rtp_packet);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000143 unsigned char* received_packet = reinterpret_cast<unsigned char*>(tmp_ptr);
144 int received_packet_length = rtp_packet_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000146 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000147 CriticalSectionScoped cs(receive_cs_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000148
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000149 if (external_decryption_) {
mflodman@webrtc.org34e83b82012-10-17 11:05:54 +0000150 int decrypted_length = kViEMaxMtu;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000151 external_decryption_->decrypt(channel_id_, received_packet,
152 decryption_buffer_, received_packet_length,
153 &decrypted_length);
154 if (decrypted_length <= 0) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000155 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
156 "RTP decryption failed");
niklase@google.com470e71d2011-07-07 08:21:25 +0000157 return -1;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000158 } else if (decrypted_length > kViEMaxMtu) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000159 WEBRTC_TRACE(webrtc::kTraceCritical, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000160 "InsertRTPPacket: %d bytes is allocated as RTP decrytption"
161 " output, external decryption used %d bytes. => memory is "
162 " now corrupted", kViEMaxMtu, decrypted_length);
163 return -1;
164 }
165 received_packet = decryption_buffer_;
166 received_packet_length = decrypted_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000167 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000168
169 if (rtp_dump_) {
170 rtp_dump_->DumpPacket(received_packet,
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000171 static_cast<uint16_t>(received_packet_length));
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000172 }
173 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000174 assert(rtp_rtcp_); // Should be set by owner at construction time.
175 return rtp_rtcp_->IncomingPacket(received_packet, received_packet_length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000176}
177
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000178int ViEReceiver::InsertRTCPPacket(const int8_t* rtcp_packet,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000179 int rtcp_packet_length) {
180 // TODO(mflodman) Change decrypt to get rid of this cast.
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000181 int8_t* tmp_ptr = const_cast<int8_t*>(rtcp_packet);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000182 unsigned char* received_packet = reinterpret_cast<unsigned char*>(tmp_ptr);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000183 int received_packet_length = rtcp_packet_length;
184 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000185 CriticalSectionScoped cs(receive_cs_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000186
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000187 if (external_decryption_) {
mflodman@webrtc.org34e83b82012-10-17 11:05:54 +0000188 int decrypted_length = kViEMaxMtu;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000189 external_decryption_->decrypt_rtcp(channel_id_, received_packet,
190 decryption_buffer_,
191 received_packet_length,
192 &decrypted_length);
193 if (decrypted_length <= 0) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000194 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
195 "RTP decryption failed");
niklase@google.com470e71d2011-07-07 08:21:25 +0000196 return -1;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000197 } else if (decrypted_length > kViEMaxMtu) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000198 WEBRTC_TRACE(webrtc::kTraceCritical, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000199 "InsertRTCPPacket: %d bytes is allocated as RTP "
200 " decrytption output, external decryption used %d bytes. "
201 " => memory is now corrupted",
202 kViEMaxMtu, decrypted_length);
203 return -1;
204 }
205 received_packet = decryption_buffer_;
206 received_packet_length = decrypted_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000207 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000208
209 if (rtp_dump_) {
210 rtp_dump_->DumpPacket(
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000211 received_packet, static_cast<uint16_t>(received_packet_length));
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000212 }
213 }
214 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000215 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000216 std::list<RtpRtcp*>::iterator it = rtp_rtcp_simulcast_.begin();
217 while (it != rtp_rtcp_simulcast_.end()) {
218 RtpRtcp* rtp_rtcp = *it++;
219 rtp_rtcp->IncomingPacket(received_packet, received_packet_length);
220 }
221 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000222 assert(rtp_rtcp_); // Should be set by owner at construction time.
223 return rtp_rtcp_->IncomingPacket(received_packet, received_packet_length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000224}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000225
226void ViEReceiver::StartReceive() {
227 receiving_ = true;
228}
229
230void ViEReceiver::StopReceive() {
231 receiving_ = false;
232}
233
234int ViEReceiver::StartRTPDump(const char file_nameUTF8[1024]) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000235 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000236 if (rtp_dump_) {
237 // Restart it if it already exists and is started
238 rtp_dump_->Stop();
239 } else {
240 rtp_dump_ = RtpDump::CreateRtpDump();
241 if (rtp_dump_ == NULL) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000242 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000243 "StartRTPDump: Failed to create RTP dump");
244 return -1;
245 }
246 }
247 if (rtp_dump_->Start(file_nameUTF8) != 0) {
248 RtpDump::DestroyRtpDump(rtp_dump_);
249 rtp_dump_ = NULL;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000250 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000251 "StartRTPDump: Failed to start RTP dump");
252 return -1;
253 }
254 return 0;
255}
256
257int ViEReceiver::StopRTPDump() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000258 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000259 if (rtp_dump_) {
260 if (rtp_dump_->IsActive()) {
261 rtp_dump_->Stop();
262 } else {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000263 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000264 "StopRTPDump: Dump not active");
265 }
266 RtpDump::DestroyRtpDump(rtp_dump_);
267 rtp_dump_ = NULL;
268 } else {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000269 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000270 "StopRTPDump: RTP dump not started");
271 return -1;
272 }
273 return 0;
274}
275
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000276// TODO(holmer): To be moved to ViEChannelGroup.
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +0000277void ViEReceiver::EstimatedReceiveBandwidth(
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000278 unsigned int* available_bandwidth) const {
279 std::vector<unsigned int> ssrcs;
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +0000280
281 // LatestEstimate returns an error if there is no valid bitrate estimate, but
282 // ViEReceiver instead returns a zero estimate.
283 remote_bitrate_estimator_->LatestEstimate(&ssrcs, available_bandwidth);
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000284 if (!ssrcs.empty()) {
285 *available_bandwidth /= ssrcs.size();
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +0000286 } else {
287 *available_bandwidth = 0;
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000288 }
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000289}
290
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000291} // namespace webrtc