blob: 9dec53147c49b30204eccafb0f485d146f163dfa [file] [log] [blame]
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
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
11#include "webrtc/video_engine/internal/video_call.h"
12
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h>
14#include <string.h>
15
pbos@webrtc.org29d58392013-05-16 12:08:03 +000016#include <map>
17#include <vector>
18
pbos@webrtc.org29d58392013-05-16 12:08:03 +000019#include "webrtc/video_engine/include/vie_base.h"
20#include "webrtc/video_engine/include/vie_codec.h"
21#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000022#include "webrtc/video_engine/internal/video_receive_stream.h"
23#include "webrtc/video_engine/internal/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000024
25namespace webrtc {
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +000026
27namespace newapi {
28VideoCall* VideoCall::Create(const newapi::VideoCall::Config& config) {
29 webrtc::VideoEngine* video_engine = webrtc::VideoEngine::Create();
30 assert(video_engine != NULL);
31
32 ViEBase* video_engine_base = ViEBase::GetInterface(video_engine);
33 assert(video_engine_base != NULL);
34 if (video_engine_base->Init() != 0) {
35 abort();
36 }
37 video_engine_base->Release();
38
39 return new internal::VideoCall(video_engine, config);
40}
41} // namespace newapi
42
pbos@webrtc.org29d58392013-05-16 12:08:03 +000043namespace internal {
44
45VideoCall::VideoCall(webrtc::VideoEngine* video_engine,
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000046 const newapi::VideoCall::Config& config)
47 : config_(config),
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000048 receive_lock_(RWLockWrapper::CreateRWLock()),
49 send_lock_(RWLockWrapper::CreateRWLock()),
pbos@webrtc.org40523702013-08-05 12:49:22 +000050 rtp_header_parser_(RtpHeaderParser::Create()),
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000051 video_engine_(video_engine) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000052 assert(video_engine != NULL);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000053 assert(config.send_transport != NULL);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000054
55 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
56 assert(rtp_rtcp_ != NULL);
57
58 codec_ = ViECodec::GetInterface(video_engine_);
59 assert(codec_ != NULL);
60}
61
62VideoCall::~VideoCall() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000063 codec_->Release();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +000064 rtp_rtcp_->Release();
65 webrtc::VideoEngine::Delete(video_engine_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000066}
67
68newapi::PacketReceiver* VideoCall::Receiver() { return this; }
69
70std::vector<VideoCodec> VideoCall::GetVideoCodecs() {
71 std::vector<VideoCodec> codecs;
72
73 VideoCodec codec;
74 for (size_t i = 0; i < static_cast<size_t>(codec_->NumberOfCodecs()); ++i) {
75 if (codec_->GetCodec(i, codec) == 0) {
76 codecs.push_back(codec);
77 }
78 }
79 return codecs;
80}
81
pbos@webrtc.org025f4f12013-06-05 11:33:21 +000082VideoSendStream::Config VideoCall::GetDefaultSendConfig() {
83 VideoSendStream::Config config;
84 codec_->GetCodec(0, config.codec);
85 return config;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000086}
87
88newapi::VideoSendStream* VideoCall::CreateSendStream(
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000089 const newapi::VideoSendStream::Config& config) {
90 assert(config.rtp.ssrcs.size() > 0);
91 assert(config.codec.numberOfSimulcastStreams == 0 ||
92 config.codec.numberOfSimulcastStreams == config.rtp.ssrcs.size());
93
pbos@webrtc.org40523702013-08-05 12:49:22 +000094 VideoSendStream* send_stream = new VideoSendStream(
95 config_.send_transport, config_.overuse_detection, video_engine_, config);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000096
97 WriteLockScoped write_lock(*send_lock_);
98 for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) {
99 assert(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end());
100 send_ssrcs_[config.rtp.ssrcs[i]] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000101 }
102 return send_stream;
103}
104
105newapi::SendStreamState* VideoCall::DestroySendStream(
106 newapi::VideoSendStream* send_stream) {
107 if (send_stream == NULL) {
108 return NULL;
109 }
110 // TODO(pbos): Remove it properly! Free the SSRCs!
111 delete static_cast<VideoSendStream*>(send_stream);
112
113 // TODO(pbos): Return its previous state
114 return NULL;
115}
116
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000117VideoReceiveStream::Config VideoCall::GetDefaultReceiveConfig() {
118 return newapi::VideoReceiveStream::Config();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000119}
120
121newapi::VideoReceiveStream* VideoCall::CreateReceiveStream(
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000122 const newapi::VideoReceiveStream::Config& config) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000123 VideoReceiveStream* receive_stream =
124 new VideoReceiveStream(video_engine_, config, config_.send_transport);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000125
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000126 WriteLockScoped write_lock(*receive_lock_);
127 assert(receive_ssrcs_.find(config.rtp.ssrc) == receive_ssrcs_.end());
128 receive_ssrcs_[config.rtp.ssrc] = receive_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000129 return receive_stream;
130}
131
132void VideoCall::DestroyReceiveStream(
133 newapi::VideoReceiveStream* receive_stream) {
134 if (receive_stream == NULL) {
135 return;
136 }
137 // TODO(pbos): Remove its SSRCs!
138 delete static_cast<VideoReceiveStream*>(receive_stream);
139}
140
141uint32_t VideoCall::SendBitrateEstimate() {
142 // TODO(pbos): Return send-bitrate estimate
143 return 0;
144}
145
146uint32_t VideoCall::ReceiveBitrateEstimate() {
147 // TODO(pbos): Return receive-bitrate estimate
148 return 0;
149}
150
pbos@webrtc.org40523702013-08-05 12:49:22 +0000151bool VideoCall::DeliverRtcp(const uint8_t* packet, size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000152 // TODO(pbos): Figure out what channel needs it actually.
153 // Do NOT broadcast! Also make sure it's a valid packet.
154 bool rtcp_delivered = false;
pbos@webrtc.org40523702013-08-05 12:49:22 +0000155 {
156 ReadLockScoped read_lock(*receive_lock_);
157 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
158 receive_ssrcs_.begin();
159 it != receive_ssrcs_.end();
160 ++it) {
161 if (it->second->DeliverRtcp(static_cast<const uint8_t*>(packet),
162 length)) {
163 rtcp_delivered = true;
164 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000165 }
166 }
167
pbos@webrtc.org40523702013-08-05 12:49:22 +0000168 {
169 ReadLockScoped read_lock(*send_lock_);
170 for (std::map<uint32_t, VideoSendStream*>::iterator it =
171 send_ssrcs_.begin();
172 it != send_ssrcs_.end();
173 ++it) {
174 if (it->second->DeliverRtcp(static_cast<const uint8_t*>(packet),
175 length)) {
176 rtcp_delivered = true;
177 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000178 }
179 }
180 return rtcp_delivered;
181}
182
pbos@webrtc.org40523702013-08-05 12:49:22 +0000183bool VideoCall::DeliverRtp(const RTPHeader& header,
184 const uint8_t* packet,
185 size_t length) {
186 VideoReceiveStream* receiver;
187 {
188 ReadLockScoped read_lock(*receive_lock_);
189 std::map<uint32_t, VideoReceiveStream*>::iterator it =
190 receive_ssrcs_.find(header.ssrc);
191 if (it == receive_ssrcs_.end()) {
192 // TODO(pbos): Log some warning, SSRC without receiver.
193 return false;
194 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000195
pbos@webrtc.org40523702013-08-05 12:49:22 +0000196 receiver = it->second;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000197 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000198 return receiver->DeliverRtp(static_cast<const uint8_t*>(packet), length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000199}
200
pbos@webrtc.org40523702013-08-05 12:49:22 +0000201bool VideoCall::DeliverPacket(const uint8_t* packet, size_t length) {
202 // TODO(pbos): ExtensionMap if there are extensions.
203 if (RtpHeaderParser::IsRtcp(packet, static_cast<int>(length)))
204 return DeliverRtcp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000205
pbos@webrtc.org40523702013-08-05 12:49:22 +0000206 RTPHeader rtp_header;
207 if (!rtp_header_parser_->Parse(packet, static_cast<int>(length), &rtp_header))
208 return false;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000209
pbos@webrtc.org40523702013-08-05 12:49:22 +0000210 return DeliverRtp(rtp_header, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000211}
212
213} // namespace internal
214} // namespace webrtc