2011-07-07 08:21:25 +00:00
|
|
|
/*
|
2012-03-01 18:22:48 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-07-07 08:21:25 +00:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-06-28 06:34:08 +00:00
|
|
|
#include "video_engine/vie_receiver.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-09-21 13:20:21 +00:00
|
|
|
#include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
|
2012-06-28 06:34:08 +00:00
|
|
|
#include "modules/rtp_rtcp/interface/rtp_rtcp.h"
|
|
|
|
|
#include "modules/utility/interface/rtp_dump.h"
|
|
|
|
|
#include "modules/video_coding/main/interface/video_coding.h"
|
|
|
|
|
#include "system_wrappers/interface/critical_section_wrapper.h"
|
2012-09-21 13:20:21 +00:00
|
|
|
#include "system_wrappers/interface/tick_util.h"
|
2012-06-28 06:34:08 +00:00
|
|
|
#include "system_wrappers/interface/trace.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2012-05-11 11:08:54 +00:00
|
|
|
ViEReceiver::ViEReceiver(const int32_t channel_id,
|
2012-09-21 13:20:21 +00:00
|
|
|
VideoCodingModule* module_vcm,
|
|
|
|
|
RemoteBitrateEstimator* remote_bitrate_estimator)
|
2011-12-22 14:17:53 +00:00
|
|
|
: receive_cs_(CriticalSectionWrapper::CreateCriticalSection()),
|
2011-11-28 22:39:24 +00:00
|
|
|
channel_id_(channel_id),
|
2012-05-11 11:08:54 +00:00
|
|
|
rtp_rtcp_(NULL),
|
2011-11-28 22:39:24 +00:00
|
|
|
vcm_(module_vcm),
|
2012-09-21 13:20:21 +00:00
|
|
|
remote_bitrate_estimator_(remote_bitrate_estimator),
|
2011-11-28 22:39:24 +00:00
|
|
|
external_decryption_(NULL),
|
|
|
|
|
decryption_buffer_(NULL),
|
|
|
|
|
rtp_dump_(NULL),
|
|
|
|
|
receiving_(false) {
|
2012-09-21 13:20:21 +00:00
|
|
|
assert(remote_bitrate_estimator);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-28 22:39:24 +00:00
|
|
|
ViEReceiver::~ViEReceiver() {
|
|
|
|
|
if (decryption_buffer_) {
|
|
|
|
|
delete[] decryption_buffer_;
|
|
|
|
|
decryption_buffer_ = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (rtp_dump_) {
|
|
|
|
|
rtp_dump_->Stop();
|
|
|
|
|
RtpDump::DestroyRtpDump(rtp_dump_);
|
|
|
|
|
rtp_dump_ = NULL;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-28 22:39:24 +00:00
|
|
|
int ViEReceiver::RegisterExternalDecryption(Encryption* decryption) {
|
2011-12-22 14:17:53 +00:00
|
|
|
CriticalSectionScoped cs(receive_cs_.get());
|
2011-11-28 22:39:24 +00:00
|
|
|
if (external_decryption_) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
decryption_buffer_ = new WebRtc_UWord8[kViEMaxMtu];
|
|
|
|
|
if (decryption_buffer_ == NULL) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
external_decryption_ = decryption;
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-28 22:39:24 +00:00
|
|
|
int ViEReceiver::DeregisterExternalDecryption() {
|
2011-12-22 14:17:53 +00:00
|
|
|
CriticalSectionScoped cs(receive_cs_.get());
|
2011-11-28 22:39:24 +00:00
|
|
|
if (external_decryption_ == NULL) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
external_decryption_ = NULL;
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-11 11:08:54 +00:00
|
|
|
void ViEReceiver::SetRtpRtcpModule(RtpRtcp* module) {
|
|
|
|
|
rtp_rtcp_ = module;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-13 15:19:55 +00:00
|
|
|
void ViEReceiver::RegisterSimulcastRtpRtcpModules(
|
2011-11-28 22:39:24 +00:00
|
|
|
const std::list<RtpRtcp*>& rtp_modules) {
|
2011-12-22 14:17:53 +00:00
|
|
|
CriticalSectionScoped cs(receive_cs_.get());
|
2011-11-28 22:39:24 +00:00
|
|
|
rtp_rtcp_simulcast_.clear();
|
|
|
|
|
|
|
|
|
|
if (!rtp_modules.empty()) {
|
|
|
|
|
rtp_rtcp_simulcast_.insert(rtp_rtcp_simulcast_.begin(),
|
|
|
|
|
rtp_modules.begin(),
|
|
|
|
|
rtp_modules.end());
|
|
|
|
|
}
|
2011-10-13 15:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-28 22:39:24 +00:00
|
|
|
void ViEReceiver::IncomingRTPPacket(const WebRtc_Word8* rtp_packet,
|
|
|
|
|
const WebRtc_Word32 rtp_packet_length,
|
2012-03-01 18:22:48 +00:00
|
|
|
const char* from_ip,
|
2011-11-28 22:39:24 +00:00
|
|
|
const WebRtc_UWord16 from_port) {
|
|
|
|
|
InsertRTPPacket(rtp_packet, rtp_packet_length);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-28 22:39:24 +00:00
|
|
|
void ViEReceiver::IncomingRTCPPacket(const WebRtc_Word8* rtcp_packet,
|
|
|
|
|
const WebRtc_Word32 rtcp_packet_length,
|
2012-03-01 18:22:48 +00:00
|
|
|
const char* from_ip,
|
2011-11-28 22:39:24 +00:00
|
|
|
const WebRtc_UWord16 from_port) {
|
|
|
|
|
InsertRTCPPacket(rtcp_packet, rtcp_packet_length);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-28 22:39:24 +00:00
|
|
|
int ViEReceiver::ReceivedRTPPacket(const void* rtp_packet,
|
|
|
|
|
int rtp_packet_length) {
|
|
|
|
|
if (!receiving_) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return InsertRTPPacket((const WebRtc_Word8*) rtp_packet, rtp_packet_length);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-28 22:39:24 +00:00
|
|
|
int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
|
|
|
|
|
int rtcp_packet_length) {
|
|
|
|
|
if (!receiving_) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return InsertRTCPPacket((const WebRtc_Word8*) rtcp_packet,
|
|
|
|
|
rtcp_packet_length);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-28 22:39:24 +00:00
|
|
|
WebRtc_Word32 ViEReceiver::OnReceivedPayloadData(
|
|
|
|
|
const WebRtc_UWord8* payload_data, const WebRtc_UWord16 payload_size,
|
|
|
|
|
const WebRtcRTPHeader* rtp_header) {
|
|
|
|
|
if (rtp_header == NULL) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-09-21 13:20:21 +00:00
|
|
|
// TODO(holmer): Make sure packets reconstructed using FEC are not passed to
|
|
|
|
|
// the bandwidth estimator.
|
2012-10-31 12:21:13 +00:00
|
|
|
const int packet_size = payload_size + rtp_header->header.paddingLength;
|
2012-09-21 13:20:21 +00:00
|
|
|
uint32_t compensated_timestamp = rtp_header->header.timestamp +
|
|
|
|
|
rtp_header->extension.transmissionTimeOffset;
|
2012-11-07 13:37:19 +00:00
|
|
|
remote_bitrate_estimator_->IncomingPacket(
|
|
|
|
|
rtp_header->header.ssrc, packet_size,
|
|
|
|
|
TickTime::MillisecondTimestamp(), compensated_timestamp);
|
2012-05-11 11:08:54 +00:00
|
|
|
if (vcm_->IncomingPacket(payload_data, payload_size, *rtp_header) != 0) {
|
2011-11-28 22:39:24 +00:00
|
|
|
// Check this...
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-09-21 13:20:21 +00:00
|
|
|
void ViEReceiver::OnSendReportReceived(const WebRtc_Word32 id,
|
|
|
|
|
const WebRtc_UWord32 senderSSRC,
|
|
|
|
|
uint32_t ntp_secs,
|
|
|
|
|
uint32_t ntp_frac,
|
|
|
|
|
uint32_t timestamp) {
|
|
|
|
|
remote_bitrate_estimator_->IncomingRtcp(senderSSRC, ntp_secs, ntp_frac,
|
|
|
|
|
timestamp);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-28 22:39:24 +00:00
|
|
|
int ViEReceiver::InsertRTPPacket(const WebRtc_Word8* rtp_packet,
|
|
|
|
|
int rtp_packet_length) {
|
|
|
|
|
// TODO(mflodman) Change decrypt to get rid of this cast.
|
|
|
|
|
WebRtc_Word8* tmp_ptr = const_cast<WebRtc_Word8*>(rtp_packet);
|
|
|
|
|
unsigned char* received_packet = reinterpret_cast<unsigned char*>(tmp_ptr);
|
|
|
|
|
int received_packet_length = rtp_packet_length;
|
|
|
|
|
|
|
|
|
|
{
|
2011-12-22 14:17:53 +00:00
|
|
|
CriticalSectionScoped cs(receive_cs_.get());
|
2011-11-28 22:39:24 +00:00
|
|
|
|
|
|
|
|
if (external_decryption_) {
|
2012-10-17 11:05:54 +00:00
|
|
|
int decrypted_length = kViEMaxMtu;
|
2011-11-28 22:39:24 +00:00
|
|
|
external_decryption_->decrypt(channel_id_, received_packet,
|
|
|
|
|
decryption_buffer_, received_packet_length,
|
|
|
|
|
&decrypted_length);
|
|
|
|
|
if (decrypted_length <= 0) {
|
2012-05-11 11:08:54 +00:00
|
|
|
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
|
|
|
|
|
"RTP decryption failed");
|
2011-07-07 08:21:25 +00:00
|
|
|
return -1;
|
2011-11-28 22:39:24 +00:00
|
|
|
} else if (decrypted_length > kViEMaxMtu) {
|
2012-05-11 11:08:54 +00:00
|
|
|
WEBRTC_TRACE(webrtc::kTraceCritical, webrtc::kTraceVideo, channel_id_,
|
2011-11-28 22:39:24 +00:00
|
|
|
"InsertRTPPacket: %d bytes is allocated as RTP decrytption"
|
|
|
|
|
" output, external decryption used %d bytes. => memory is "
|
|
|
|
|
" now corrupted", kViEMaxMtu, decrypted_length);
|
2011-07-07 08:21:25 +00:00
|
|
|
return -1;
|
2011-11-28 22:39:24 +00:00
|
|
|
}
|
|
|
|
|
received_packet = decryption_buffer_;
|
|
|
|
|
received_packet_length = decrypted_length;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-28 22:39:24 +00:00
|
|
|
if (rtp_dump_) {
|
|
|
|
|
rtp_dump_->DumpPacket(received_packet,
|
|
|
|
|
static_cast<WebRtc_UWord16>(received_packet_length));
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2011-11-28 22:39:24 +00:00
|
|
|
}
|
2012-05-11 11:08:54 +00:00
|
|
|
assert(rtp_rtcp_); // Should be set by owner at construction time.
|
|
|
|
|
return rtp_rtcp_->IncomingPacket(received_packet, received_packet_length);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-28 22:39:24 +00:00
|
|
|
int ViEReceiver::InsertRTCPPacket(const WebRtc_Word8* rtcp_packet,
|
|
|
|
|
int rtcp_packet_length) {
|
|
|
|
|
// TODO(mflodman) Change decrypt to get rid of this cast.
|
2012-05-11 11:08:54 +00:00
|
|
|
WebRtc_Word8* tmp_ptr = const_cast<WebRtc_Word8*>(rtcp_packet);
|
|
|
|
|
unsigned char* received_packet = reinterpret_cast<unsigned char*>(tmp_ptr);
|
2011-11-28 22:39:24 +00:00
|
|
|
int received_packet_length = rtcp_packet_length;
|
|
|
|
|
{
|
2011-12-22 14:17:53 +00:00
|
|
|
CriticalSectionScoped cs(receive_cs_.get());
|
2011-11-28 22:39:24 +00:00
|
|
|
|
|
|
|
|
if (external_decryption_) {
|
2012-10-17 11:05:54 +00:00
|
|
|
int decrypted_length = kViEMaxMtu;
|
2011-11-28 22:39:24 +00:00
|
|
|
external_decryption_->decrypt_rtcp(channel_id_, received_packet,
|
|
|
|
|
decryption_buffer_,
|
|
|
|
|
received_packet_length,
|
|
|
|
|
&decrypted_length);
|
|
|
|
|
if (decrypted_length <= 0) {
|
2012-05-11 11:08:54 +00:00
|
|
|
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
|
|
|
|
|
"RTP decryption failed");
|
2011-11-28 22:39:24 +00:00
|
|
|
return -1;
|
|
|
|
|
} else if (decrypted_length > kViEMaxMtu) {
|
2012-05-11 11:08:54 +00:00
|
|
|
WEBRTC_TRACE(webrtc::kTraceCritical, webrtc::kTraceVideo, channel_id_,
|
2011-11-28 22:39:24 +00:00
|
|
|
"InsertRTCPPacket: %d bytes is allocated as RTP "
|
|
|
|
|
" decrytption output, external decryption used %d bytes. "
|
|
|
|
|
" => memory is now corrupted",
|
|
|
|
|
kViEMaxMtu, decrypted_length);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
received_packet = decryption_buffer_;
|
|
|
|
|
received_packet_length = decrypted_length;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-28 22:39:24 +00:00
|
|
|
if (rtp_dump_) {
|
|
|
|
|
rtp_dump_->DumpPacket(
|
|
|
|
|
received_packet, static_cast<WebRtc_UWord16>(received_packet_length));
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2011-11-28 22:39:24 +00:00
|
|
|
}
|
|
|
|
|
{
|
2011-12-22 14:17:53 +00:00
|
|
|
CriticalSectionScoped cs(receive_cs_.get());
|
2011-11-28 22:39:24 +00:00
|
|
|
std::list<RtpRtcp*>::iterator it = rtp_rtcp_simulcast_.begin();
|
|
|
|
|
while (it != rtp_rtcp_simulcast_.end()) {
|
|
|
|
|
RtpRtcp* rtp_rtcp = *it++;
|
|
|
|
|
rtp_rtcp->IncomingPacket(received_packet, received_packet_length);
|
2011-10-13 15:19:55 +00:00
|
|
|
}
|
2011-11-28 22:39:24 +00:00
|
|
|
}
|
2012-05-11 11:08:54 +00:00
|
|
|
assert(rtp_rtcp_); // Should be set by owner at construction time.
|
|
|
|
|
return rtp_rtcp_->IncomingPacket(received_packet, received_packet_length);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-28 22:39:24 +00:00
|
|
|
void ViEReceiver::StartReceive() {
|
|
|
|
|
receiving_ = true;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-28 22:39:24 +00:00
|
|
|
void ViEReceiver::StopReceive() {
|
|
|
|
|
receiving_ = false;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-28 22:39:24 +00:00
|
|
|
int ViEReceiver::StartRTPDump(const char file_nameUTF8[1024]) {
|
2011-12-22 14:17:53 +00:00
|
|
|
CriticalSectionScoped cs(receive_cs_.get());
|
2011-11-28 22:39:24 +00:00
|
|
|
if (rtp_dump_) {
|
|
|
|
|
// Restart it if it already exists and is started
|
|
|
|
|
rtp_dump_->Stop();
|
|
|
|
|
} else {
|
|
|
|
|
rtp_dump_ = RtpDump::CreateRtpDump();
|
|
|
|
|
if (rtp_dump_ == NULL) {
|
2012-05-11 11:08:54 +00:00
|
|
|
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
|
2011-11-28 22:39:24 +00:00
|
|
|
"StartRTPDump: Failed to create RTP dump");
|
|
|
|
|
return -1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2011-11-28 22:39:24 +00:00
|
|
|
}
|
|
|
|
|
if (rtp_dump_->Start(file_nameUTF8) != 0) {
|
|
|
|
|
RtpDump::DestroyRtpDump(rtp_dump_);
|
|
|
|
|
rtp_dump_ = NULL;
|
2012-05-11 11:08:54 +00:00
|
|
|
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
|
2011-11-28 22:39:24 +00:00
|
|
|
"StartRTPDump: Failed to start RTP dump");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-28 22:39:24 +00:00
|
|
|
int ViEReceiver::StopRTPDump() {
|
2011-12-22 14:17:53 +00:00
|
|
|
CriticalSectionScoped cs(receive_cs_.get());
|
2011-11-28 22:39:24 +00:00
|
|
|
if (rtp_dump_) {
|
|
|
|
|
if (rtp_dump_->IsActive()) {
|
|
|
|
|
rtp_dump_->Stop();
|
|
|
|
|
} else {
|
2012-05-11 11:08:54 +00:00
|
|
|
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
|
2011-11-28 22:39:24 +00:00
|
|
|
"StopRTPDump: Dump not active");
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2011-11-28 22:39:24 +00:00
|
|
|
RtpDump::DestroyRtpDump(rtp_dump_);
|
|
|
|
|
rtp_dump_ = NULL;
|
|
|
|
|
} else {
|
2012-05-11 11:08:54 +00:00
|
|
|
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
|
2011-11-28 22:39:24 +00:00
|
|
|
"StopRTPDump: RTP dump not started");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2011-11-28 22:39:24 +00:00
|
|
|
|
2013-02-01 14:33:42 +00:00
|
|
|
// TODO(holmer): To be moved to ViEChannelGroup.
|
|
|
|
|
bool ViEReceiver::EstimatedReceiveBandwidth(
|
|
|
|
|
unsigned int* available_bandwidth) const {
|
|
|
|
|
std::vector<unsigned int> ssrcs;
|
|
|
|
|
if (!remote_bitrate_estimator_->LatestEstimate(&ssrcs,
|
|
|
|
|
available_bandwidth)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!ssrcs.empty()) {
|
|
|
|
|
*available_bandwidth /= ssrcs.size();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-28 22:39:24 +00:00
|
|
|
} // namespace webrtc
|