2013-05-16 12:08:03 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "webrtc/video_engine/internal/video_call.h"
|
|
|
|
|
|
2013-08-05 16:22:53 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2013-05-16 12:08:03 +00:00
|
|
|
#include <map>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "webrtc/video_engine/include/vie_base.h"
|
|
|
|
|
#include "webrtc/video_engine/include/vie_codec.h"
|
|
|
|
|
#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
|
|
|
|
|
#include "webrtc/video_engine/internal/video_receive_stream.h"
|
|
|
|
|
#include "webrtc/video_engine/internal/video_send_stream.h"
|
|
|
|
|
|
|
|
|
|
namespace webrtc {
|
2013-08-14 13:52:52 +00:00
|
|
|
|
2013-08-23 09:19:30 +00:00
|
|
|
VideoCall* VideoCall::Create(const VideoCall::Config& config) {
|
2013-08-14 13:52:52 +00:00
|
|
|
webrtc::VideoEngine* video_engine = webrtc::VideoEngine::Create();
|
|
|
|
|
assert(video_engine != NULL);
|
|
|
|
|
|
|
|
|
|
return new internal::VideoCall(video_engine, config);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-16 12:08:03 +00:00
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
|
VideoCall::VideoCall(webrtc::VideoEngine* video_engine,
|
2013-08-23 09:19:30 +00:00
|
|
|
const VideoCall::Config& config)
|
2013-07-23 11:35:00 +00:00
|
|
|
: config_(config),
|
2013-06-10 13:48:26 +00:00
|
|
|
receive_lock_(RWLockWrapper::CreateRWLock()),
|
|
|
|
|
send_lock_(RWLockWrapper::CreateRWLock()),
|
2013-08-05 12:49:22 +00:00
|
|
|
rtp_header_parser_(RtpHeaderParser::Create()),
|
2013-06-10 13:48:26 +00:00
|
|
|
video_engine_(video_engine) {
|
2013-05-16 12:08:03 +00:00
|
|
|
assert(video_engine != NULL);
|
2013-07-23 11:35:00 +00:00
|
|
|
assert(config.send_transport != NULL);
|
2013-05-16 12:08:03 +00:00
|
|
|
|
|
|
|
|
rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
|
|
|
|
|
assert(rtp_rtcp_ != NULL);
|
|
|
|
|
|
|
|
|
|
codec_ = ViECodec::GetInterface(video_engine_);
|
|
|
|
|
assert(codec_ != NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VideoCall::~VideoCall() {
|
|
|
|
|
codec_->Release();
|
2013-08-14 13:52:52 +00:00
|
|
|
rtp_rtcp_->Release();
|
|
|
|
|
webrtc::VideoEngine::Delete(video_engine_);
|
2013-05-16 12:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
2013-08-23 09:19:30 +00:00
|
|
|
PacketReceiver* VideoCall::Receiver() { return this; }
|
2013-05-16 12:08:03 +00:00
|
|
|
|
|
|
|
|
std::vector<VideoCodec> VideoCall::GetVideoCodecs() {
|
|
|
|
|
std::vector<VideoCodec> codecs;
|
|
|
|
|
|
|
|
|
|
VideoCodec codec;
|
|
|
|
|
for (size_t i = 0; i < static_cast<size_t>(codec_->NumberOfCodecs()); ++i) {
|
|
|
|
|
if (codec_->GetCodec(i, codec) == 0) {
|
|
|
|
|
codecs.push_back(codec);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return codecs;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-05 11:33:21 +00:00
|
|
|
VideoSendStream::Config VideoCall::GetDefaultSendConfig() {
|
|
|
|
|
VideoSendStream::Config config;
|
|
|
|
|
codec_->GetCodec(0, config.codec);
|
|
|
|
|
return config;
|
2013-05-16 12:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
2013-08-23 09:19:30 +00:00
|
|
|
VideoSendStream* VideoCall::CreateSendStream(
|
|
|
|
|
const VideoSendStream::Config& config) {
|
2013-06-10 13:48:26 +00:00
|
|
|
assert(config.rtp.ssrcs.size() > 0);
|
|
|
|
|
assert(config.codec.numberOfSimulcastStreams == 0 ||
|
|
|
|
|
config.codec.numberOfSimulcastStreams == config.rtp.ssrcs.size());
|
|
|
|
|
|
2013-08-05 12:49:22 +00:00
|
|
|
VideoSendStream* send_stream = new VideoSendStream(
|
|
|
|
|
config_.send_transport, config_.overuse_detection, video_engine_, config);
|
2013-06-10 13:48:26 +00:00
|
|
|
|
|
|
|
|
WriteLockScoped write_lock(*send_lock_);
|
|
|
|
|
for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) {
|
|
|
|
|
assert(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end());
|
|
|
|
|
send_ssrcs_[config.rtp.ssrcs[i]] = send_stream;
|
2013-05-16 12:08:03 +00:00
|
|
|
}
|
|
|
|
|
return send_stream;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-23 09:19:30 +00:00
|
|
|
SendStreamState* VideoCall::DestroySendStream(
|
|
|
|
|
webrtc::VideoSendStream* send_stream) {
|
2013-09-05 12:38:54 +00:00
|
|
|
assert(send_stream != NULL);
|
|
|
|
|
|
|
|
|
|
VideoSendStream* send_stream_impl = NULL;
|
|
|
|
|
{
|
|
|
|
|
WriteLockScoped write_lock(*send_lock_);
|
|
|
|
|
for (std::map<uint32_t, VideoSendStream*>::iterator it =
|
|
|
|
|
send_ssrcs_.begin();
|
|
|
|
|
it != send_ssrcs_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
if (it->second == static_cast<VideoSendStream*>(send_stream)) {
|
|
|
|
|
send_stream_impl = it->second;
|
|
|
|
|
send_ssrcs_.erase(it);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-16 12:08:03 +00:00
|
|
|
}
|
2013-09-05 12:38:54 +00:00
|
|
|
|
|
|
|
|
assert(send_stream_impl != NULL);
|
|
|
|
|
delete send_stream_impl;
|
2013-05-16 12:08:03 +00:00
|
|
|
|
|
|
|
|
// TODO(pbos): Return its previous state
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-05 11:33:21 +00:00
|
|
|
VideoReceiveStream::Config VideoCall::GetDefaultReceiveConfig() {
|
2013-08-23 09:19:30 +00:00
|
|
|
return VideoReceiveStream::Config();
|
2013-05-16 12:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
2013-08-23 09:19:30 +00:00
|
|
|
VideoReceiveStream* VideoCall::CreateReceiveStream(
|
|
|
|
|
const VideoReceiveStream::Config& config) {
|
2013-08-05 12:49:22 +00:00
|
|
|
VideoReceiveStream* receive_stream =
|
|
|
|
|
new VideoReceiveStream(video_engine_, config, config_.send_transport);
|
2013-05-16 12:08:03 +00:00
|
|
|
|
2013-06-10 13:48:26 +00:00
|
|
|
WriteLockScoped write_lock(*receive_lock_);
|
|
|
|
|
assert(receive_ssrcs_.find(config.rtp.ssrc) == receive_ssrcs_.end());
|
|
|
|
|
receive_ssrcs_[config.rtp.ssrc] = receive_stream;
|
2013-05-16 12:08:03 +00:00
|
|
|
return receive_stream;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VideoCall::DestroyReceiveStream(
|
2013-08-23 09:19:30 +00:00
|
|
|
webrtc::VideoReceiveStream* receive_stream) {
|
2013-09-05 12:38:54 +00:00
|
|
|
assert(receive_stream != NULL);
|
|
|
|
|
|
|
|
|
|
VideoReceiveStream* receive_stream_impl = NULL;
|
|
|
|
|
{
|
|
|
|
|
WriteLockScoped write_lock(*receive_lock_);
|
|
|
|
|
for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
|
|
|
|
|
receive_ssrcs_.begin();
|
|
|
|
|
it != receive_ssrcs_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
|
|
|
|
|
receive_stream_impl = it->second;
|
|
|
|
|
receive_ssrcs_.erase(it);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-16 12:08:03 +00:00
|
|
|
}
|
2013-09-05 12:38:54 +00:00
|
|
|
|
|
|
|
|
assert(receive_stream_impl != NULL);
|
|
|
|
|
delete receive_stream_impl;
|
2013-05-16 12:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t VideoCall::SendBitrateEstimate() {
|
|
|
|
|
// TODO(pbos): Return send-bitrate estimate
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t VideoCall::ReceiveBitrateEstimate() {
|
|
|
|
|
// TODO(pbos): Return receive-bitrate estimate
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-05 12:49:22 +00:00
|
|
|
bool VideoCall::DeliverRtcp(const uint8_t* packet, size_t length) {
|
2013-05-16 12:08:03 +00:00
|
|
|
// TODO(pbos): Figure out what channel needs it actually.
|
|
|
|
|
// Do NOT broadcast! Also make sure it's a valid packet.
|
|
|
|
|
bool rtcp_delivered = false;
|
2013-08-05 12:49:22 +00:00
|
|
|
{
|
|
|
|
|
ReadLockScoped read_lock(*receive_lock_);
|
|
|
|
|
for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
|
|
|
|
|
receive_ssrcs_.begin();
|
|
|
|
|
it != receive_ssrcs_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
if (it->second->DeliverRtcp(static_cast<const uint8_t*>(packet),
|
|
|
|
|
length)) {
|
|
|
|
|
rtcp_delivered = true;
|
|
|
|
|
}
|
2013-08-05 12:01:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-05 12:49:22 +00:00
|
|
|
{
|
|
|
|
|
ReadLockScoped read_lock(*send_lock_);
|
|
|
|
|
for (std::map<uint32_t, VideoSendStream*>::iterator it =
|
|
|
|
|
send_ssrcs_.begin();
|
|
|
|
|
it != send_ssrcs_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
if (it->second->DeliverRtcp(static_cast<const uint8_t*>(packet),
|
|
|
|
|
length)) {
|
|
|
|
|
rtcp_delivered = true;
|
|
|
|
|
}
|
2013-05-16 12:08:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return rtcp_delivered;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-05 12:49:22 +00:00
|
|
|
bool VideoCall::DeliverRtp(const RTPHeader& header,
|
|
|
|
|
const uint8_t* packet,
|
|
|
|
|
size_t length) {
|
|
|
|
|
VideoReceiveStream* receiver;
|
|
|
|
|
{
|
|
|
|
|
ReadLockScoped read_lock(*receive_lock_);
|
|
|
|
|
std::map<uint32_t, VideoReceiveStream*>::iterator it =
|
|
|
|
|
receive_ssrcs_.find(header.ssrc);
|
|
|
|
|
if (it == receive_ssrcs_.end()) {
|
|
|
|
|
// TODO(pbos): Log some warning, SSRC without receiver.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-05-16 12:08:03 +00:00
|
|
|
|
2013-08-05 12:49:22 +00:00
|
|
|
receiver = it->second;
|
2013-05-16 12:08:03 +00:00
|
|
|
}
|
2013-08-05 12:01:36 +00:00
|
|
|
return receiver->DeliverRtp(static_cast<const uint8_t*>(packet), length);
|
2013-05-16 12:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
2013-08-05 12:49:22 +00:00
|
|
|
bool VideoCall::DeliverPacket(const uint8_t* packet, size_t length) {
|
|
|
|
|
// TODO(pbos): ExtensionMap if there are extensions.
|
|
|
|
|
if (RtpHeaderParser::IsRtcp(packet, static_cast<int>(length)))
|
|
|
|
|
return DeliverRtcp(packet, length);
|
2013-05-16 12:08:03 +00:00
|
|
|
|
2013-08-05 12:49:22 +00:00
|
|
|
RTPHeader rtp_header;
|
|
|
|
|
if (!rtp_header_parser_->Parse(packet, static_cast<int>(length), &rtp_header))
|
|
|
|
|
return false;
|
2013-05-16 12:08:03 +00:00
|
|
|
|
2013-08-05 12:49:22 +00:00
|
|
|
return DeliverRtp(rtp_header, packet, length);
|
2013-05-16 12:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
} // namespace webrtc
|