blob: a3276540b11e5cc8731f5528b461a25a6e7e3770 [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
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000011#include <assert.h>
12#include <string.h>
13
pbos@webrtc.org29d58392013-05-16 12:08:03 +000014#include <map>
15#include <vector>
16
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000017#include "webrtc/call.h"
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000018#include "webrtc/common.h"
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +000019#include "webrtc/config.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000020#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000021#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000022#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000023#include "webrtc/system_wrappers/interface/scoped_ptr.h"
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000024#include "webrtc/system_wrappers/interface/thread_annotations.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000025#include "webrtc/system_wrappers/interface/trace.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000026#include "webrtc/video/video_receive_stream.h"
27#include "webrtc/video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000028#include "webrtc/video_engine/include/vie_base.h"
29#include "webrtc/video_engine/include/vie_codec.h"
30#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000031
32namespace webrtc {
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +000033const char* RtpExtension::kTOffset = "urn:ietf:params:rtp-hdrext:toffset";
34const char* RtpExtension::kAbsSendTime =
35 "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time";
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000036namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000037
38class CpuOveruseObserverProxy : public webrtc::CpuOveruseObserver {
39 public:
40 CpuOveruseObserverProxy(OveruseCallback* overuse_callback)
41 : crit_(CriticalSectionWrapper::CreateCriticalSection()),
42 overuse_callback_(overuse_callback) {
43 assert(overuse_callback != NULL);
44 }
45
46 virtual ~CpuOveruseObserverProxy() {}
47
48 virtual void OveruseDetected() OVERRIDE {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000049 CriticalSectionScoped lock(crit_.get());
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000050 overuse_callback_->OnOveruse();
51 }
52
53 virtual void NormalUsage() OVERRIDE {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000054 CriticalSectionScoped lock(crit_.get());
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000055 overuse_callback_->OnNormalUse();
56 }
57
58 private:
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000059 const scoped_ptr<CriticalSectionWrapper> crit_;
60 OveruseCallback* overuse_callback_ GUARDED_BY(crit_);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000061};
62
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000063class Call : public webrtc::Call, public PacketReceiver {
64 public:
65 Call(webrtc::VideoEngine* video_engine, const Call::Config& config);
66 virtual ~Call();
67
68 virtual PacketReceiver* Receiver() OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000069
70 virtual VideoSendStream::Config GetDefaultSendConfig() OVERRIDE;
71
pbos@webrtc.org5a636552013-11-20 10:40:25 +000072 virtual VideoSendStream* CreateVideoSendStream(
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000073 const VideoSendStream::Config& config) OVERRIDE;
74
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +000075 virtual void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream)
76 OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000077
78 virtual VideoReceiveStream::Config GetDefaultReceiveConfig() OVERRIDE;
79
pbos@webrtc.org5a636552013-11-20 10:40:25 +000080 virtual VideoReceiveStream* CreateVideoReceiveStream(
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000081 const VideoReceiveStream::Config& config) OVERRIDE;
82
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +000083 virtual void DestroyVideoReceiveStream(
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000084 webrtc::VideoReceiveStream* receive_stream) OVERRIDE;
85
86 virtual uint32_t SendBitrateEstimate() OVERRIDE;
87 virtual uint32_t ReceiveBitrateEstimate() OVERRIDE;
88
89 virtual bool DeliverPacket(const uint8_t* packet, size_t length) OVERRIDE;
90
91 private:
92 bool DeliverRtcp(const uint8_t* packet, size_t length);
93 bool DeliverRtp(const RTPHeader& header,
94 const uint8_t* packet,
95 size_t length);
96
97 Call::Config config_;
98
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000099 std::map<uint32_t, VideoReceiveStream*> receive_ssrcs_
100 GUARDED_BY(receive_lock_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000101 scoped_ptr<RWLockWrapper> receive_lock_;
102
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000103 std::map<uint32_t, VideoSendStream*> send_ssrcs_ GUARDED_BY(send_lock_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000104 scoped_ptr<RWLockWrapper> send_lock_;
105
106 scoped_ptr<RtpHeaderParser> rtp_header_parser_;
107
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000108 scoped_ptr<CpuOveruseObserverProxy> overuse_observer_proxy_;
109
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000110 VideoEngine* video_engine_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000111 ViERTP_RTCP* rtp_rtcp_;
112 ViECodec* codec_;
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000113 ViEBase* base_;
114 int base_channel_id_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000115
116 DISALLOW_COPY_AND_ASSIGN(Call);
117};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000118} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000119
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000120Call* Call::Create(const Call::Config& config) {
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000121 VideoEngine* video_engine = config.webrtc_config != NULL
122 ? VideoEngine::Create(*config.webrtc_config)
123 : VideoEngine::Create();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000124 assert(video_engine != NULL);
125
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000126 return new internal::Call(video_engine, config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000127}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000128
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000129namespace internal {
130
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000131Call::Call(webrtc::VideoEngine* video_engine, const Call::Config& config)
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000132 : config_(config),
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000133 receive_lock_(RWLockWrapper::CreateRWLock()),
134 send_lock_(RWLockWrapper::CreateRWLock()),
pbos@webrtc.org40523702013-08-05 12:49:22 +0000135 rtp_header_parser_(RtpHeaderParser::Create()),
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000136 video_engine_(video_engine),
137 base_channel_id_(-1) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000138 assert(video_engine != NULL);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000139 assert(config.send_transport != NULL);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000140
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000141 if (config.overuse_callback) {
142 overuse_observer_proxy_.reset(
143 new CpuOveruseObserverProxy(config.overuse_callback));
144 }
145
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000146 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
147 assert(rtp_rtcp_ != NULL);
148
149 codec_ = ViECodec::GetInterface(video_engine_);
150 assert(codec_ != NULL);
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000151
152 // As a workaround for non-existing calls in the old API, create a base
153 // channel used as default channel when creating send and receive streams.
154 base_ = ViEBase::GetInterface(video_engine_);
155 assert(base_ != NULL);
156
157 base_->CreateChannel(base_channel_id_);
158 assert(base_channel_id_ != -1);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000159}
160
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000161Call::~Call() {
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000162 base_->DeleteChannel(base_channel_id_);
163 base_->Release();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000164 codec_->Release();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000165 rtp_rtcp_->Release();
166 webrtc::VideoEngine::Delete(video_engine_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000167}
168
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000169PacketReceiver* Call::Receiver() { return this; }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000170
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000171VideoSendStream::Config Call::GetDefaultSendConfig() {
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000172 VideoSendStream::Config config;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000173 return config;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000174}
175
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000176VideoSendStream* Call::CreateVideoSendStream(
177 const VideoSendStream::Config& config) {
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000178 assert(config.rtp.ssrcs.size() > 0);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000179
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000180 VideoSendStream* send_stream = new VideoSendStream(
181 config_.send_transport,
182 overuse_observer_proxy_.get(),
183 video_engine_,
184 config,
185 base_channel_id_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000186
187 WriteLockScoped write_lock(*send_lock_);
188 for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) {
189 assert(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end());
190 send_ssrcs_[config.rtp.ssrcs[i]] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000191 }
192 return send_stream;
193}
194
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000195void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000196 assert(send_stream != NULL);
197
198 VideoSendStream* send_stream_impl = NULL;
199 {
200 WriteLockScoped write_lock(*send_lock_);
201 for (std::map<uint32_t, VideoSendStream*>::iterator it =
202 send_ssrcs_.begin();
203 it != send_ssrcs_.end();
204 ++it) {
205 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
206 send_stream_impl = it->second;
207 send_ssrcs_.erase(it);
208 break;
209 }
210 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000211 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000212
213 assert(send_stream_impl != NULL);
214 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000215}
216
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000217VideoReceiveStream::Config Call::GetDefaultReceiveConfig() {
mflodman@webrtc.org92c27932013-12-13 16:36:28 +0000218 VideoReceiveStream::Config config;
219 config.rtp.remb = true;
220 return config;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000221}
222
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000223VideoReceiveStream* Call::CreateVideoReceiveStream(
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000224 const VideoReceiveStream::Config& config) {
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000225 VideoReceiveStream* receive_stream =
226 new VideoReceiveStream(video_engine_,
227 config,
228 config_.send_transport,
229 config_.voice_engine,
230 base_channel_id_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000231
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000232 WriteLockScoped write_lock(*receive_lock_);
pbos@webrtc.orgb613b5a2013-12-03 10:13:04 +0000233 assert(receive_ssrcs_.find(config.rtp.remote_ssrc) == receive_ssrcs_.end());
234 receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000235 // TODO(pbos): Configure different RTX payloads per receive payload.
236 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
237 config.rtp.rtx.begin();
238 if (it != config.rtp.rtx.end())
239 receive_ssrcs_[it->second.ssrc] = receive_stream;
240
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000241 return receive_stream;
242}
243
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000244void Call::DestroyVideoReceiveStream(
245 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000246 assert(receive_stream != NULL);
247
248 VideoReceiveStream* receive_stream_impl = NULL;
249 {
250 WriteLockScoped write_lock(*receive_lock_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000251 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
252 // separate SSRC there can be either one or two.
253 std::map<uint32_t, VideoReceiveStream*>::iterator it =
254 receive_ssrcs_.begin();
255 while (it != receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000256 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
solenberg@webrtc.org094ac392014-01-29 11:21:58 +0000257 assert(receive_stream_impl == NULL ||
258 receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000259 receive_stream_impl = it->second;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000260 receive_ssrcs_.erase(it++);
261 } else {
262 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000263 }
264 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000265 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000266
267 assert(receive_stream_impl != NULL);
268 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000269}
270
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000271uint32_t Call::SendBitrateEstimate() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000272 // TODO(pbos): Return send-bitrate estimate
273 return 0;
274}
275
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000276uint32_t Call::ReceiveBitrateEstimate() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000277 // TODO(pbos): Return receive-bitrate estimate
278 return 0;
279}
280
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000281bool Call::DeliverRtcp(const uint8_t* packet, size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000282 // TODO(pbos): Figure out what channel needs it actually.
283 // Do NOT broadcast! Also make sure it's a valid packet.
284 bool rtcp_delivered = false;
pbos@webrtc.org40523702013-08-05 12:49:22 +0000285 {
286 ReadLockScoped read_lock(*receive_lock_);
287 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
288 receive_ssrcs_.begin();
289 it != receive_ssrcs_.end();
290 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000291 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000292 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000293 }
294 }
295
pbos@webrtc.org40523702013-08-05 12:49:22 +0000296 {
297 ReadLockScoped read_lock(*send_lock_);
298 for (std::map<uint32_t, VideoSendStream*>::iterator it =
299 send_ssrcs_.begin();
300 it != send_ssrcs_.end();
301 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000302 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000303 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000304 }
305 }
306 return rtcp_delivered;
307}
308
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000309bool Call::DeliverRtp(const RTPHeader& header,
310 const uint8_t* packet,
311 size_t length) {
solenberg@webrtc.org094ac392014-01-29 11:21:58 +0000312 ReadLockScoped read_lock(*receive_lock_);
313 std::map<uint32_t, VideoReceiveStream*>::iterator it =
314 receive_ssrcs_.find(header.ssrc);
315 if (it == receive_ssrcs_.end()) {
316 // TODO(pbos): Log some warning, SSRC without receiver.
317 return false;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000318 }
solenberg@webrtc.org094ac392014-01-29 11:21:58 +0000319 return it->second->DeliverRtp(static_cast<const uint8_t*>(packet), length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000320}
321
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000322bool Call::DeliverPacket(const uint8_t* packet, size_t length) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000323 // TODO(pbos): ExtensionMap if there are extensions.
324 if (RtpHeaderParser::IsRtcp(packet, static_cast<int>(length)))
325 return DeliverRtcp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000326
pbos@webrtc.org40523702013-08-05 12:49:22 +0000327 RTPHeader rtp_header;
328 if (!rtp_header_parser_->Parse(packet, static_cast<int>(length), &rtp_header))
329 return false;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000330
pbos@webrtc.org40523702013-08-05 12:49:22 +0000331 return DeliverRtp(rtp_header, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000332}
333
334} // namespace internal
335} // namespace webrtc