blob: 6adb0fccfd51601022e42691b2dce82e665f4398 [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
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +000089 virtual DeliveryStatus DeliverPacket(const uint8_t* packet,
90 size_t length) OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000091
92 private:
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +000093 DeliveryStatus DeliverRtcp(const uint8_t* packet, size_t length);
94 DeliveryStatus DeliverRtp(const RTPHeader& header,
95 const uint8_t* packet,
96 size_t length);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000097
98 Call::Config config_;
99
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000100 std::map<uint32_t, VideoReceiveStream*> receive_ssrcs_
101 GUARDED_BY(receive_lock_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000102 scoped_ptr<RWLockWrapper> receive_lock_;
103
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000104 std::map<uint32_t, VideoSendStream*> send_ssrcs_ GUARDED_BY(send_lock_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000105 scoped_ptr<RWLockWrapper> send_lock_;
106
107 scoped_ptr<RtpHeaderParser> rtp_header_parser_;
108
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000109 scoped_ptr<CpuOveruseObserverProxy> overuse_observer_proxy_;
110
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000111 VideoEngine* video_engine_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000112 ViERTP_RTCP* rtp_rtcp_;
113 ViECodec* codec_;
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000114 ViEBase* base_;
115 int base_channel_id_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000116
117 DISALLOW_COPY_AND_ASSIGN(Call);
118};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000119} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000120
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000121Call* Call::Create(const Call::Config& config) {
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000122 VideoEngine* video_engine = config.webrtc_config != NULL
123 ? VideoEngine::Create(*config.webrtc_config)
124 : VideoEngine::Create();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000125 assert(video_engine != NULL);
126
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000127 return new internal::Call(video_engine, config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000128}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000129
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000130namespace internal {
131
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000132Call::Call(webrtc::VideoEngine* video_engine, const Call::Config& config)
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000133 : config_(config),
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000134 receive_lock_(RWLockWrapper::CreateRWLock()),
135 send_lock_(RWLockWrapper::CreateRWLock()),
pbos@webrtc.org40523702013-08-05 12:49:22 +0000136 rtp_header_parser_(RtpHeaderParser::Create()),
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000137 video_engine_(video_engine),
138 base_channel_id_(-1) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000139 assert(video_engine != NULL);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000140 assert(config.send_transport != NULL);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000141
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000142 if (config.overuse_callback) {
143 overuse_observer_proxy_.reset(
144 new CpuOveruseObserverProxy(config.overuse_callback));
145 }
146
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000147 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
148 assert(rtp_rtcp_ != NULL);
149
150 codec_ = ViECodec::GetInterface(video_engine_);
151 assert(codec_ != NULL);
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000152
153 // As a workaround for non-existing calls in the old API, create a base
154 // channel used as default channel when creating send and receive streams.
155 base_ = ViEBase::GetInterface(video_engine_);
156 assert(base_ != NULL);
157
158 base_->CreateChannel(base_channel_id_);
159 assert(base_channel_id_ != -1);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000160}
161
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000162Call::~Call() {
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000163 base_->DeleteChannel(base_channel_id_);
164 base_->Release();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000165 codec_->Release();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000166 rtp_rtcp_->Release();
167 webrtc::VideoEngine::Delete(video_engine_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000168}
169
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000170PacketReceiver* Call::Receiver() { return this; }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000171
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000172VideoSendStream::Config Call::GetDefaultSendConfig() {
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000173 VideoSendStream::Config config;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000174 return config;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000175}
176
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000177VideoSendStream* Call::CreateVideoSendStream(
178 const VideoSendStream::Config& config) {
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000179 assert(config.rtp.ssrcs.size() > 0);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000180
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000181 VideoSendStream* send_stream = new VideoSendStream(
182 config_.send_transport,
183 overuse_observer_proxy_.get(),
184 video_engine_,
185 config,
186 base_channel_id_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000187
188 WriteLockScoped write_lock(*send_lock_);
189 for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) {
190 assert(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end());
191 send_ssrcs_[config.rtp.ssrcs[i]] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000192 }
193 return send_stream;
194}
195
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000196void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000197 assert(send_stream != NULL);
198
199 VideoSendStream* send_stream_impl = NULL;
200 {
201 WriteLockScoped write_lock(*send_lock_);
202 for (std::map<uint32_t, VideoSendStream*>::iterator it =
203 send_ssrcs_.begin();
204 it != send_ssrcs_.end();
205 ++it) {
206 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
207 send_stream_impl = it->second;
208 send_ssrcs_.erase(it);
209 break;
210 }
211 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000212 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000213
214 assert(send_stream_impl != NULL);
215 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000216}
217
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000218VideoReceiveStream::Config Call::GetDefaultReceiveConfig() {
mflodman@webrtc.org92c27932013-12-13 16:36:28 +0000219 VideoReceiveStream::Config config;
220 config.rtp.remb = true;
221 return config;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000222}
223
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000224VideoReceiveStream* Call::CreateVideoReceiveStream(
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000225 const VideoReceiveStream::Config& config) {
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000226 VideoReceiveStream* receive_stream =
227 new VideoReceiveStream(video_engine_,
228 config,
229 config_.send_transport,
230 config_.voice_engine,
231 base_channel_id_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000232
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000233 WriteLockScoped write_lock(*receive_lock_);
pbos@webrtc.orgb613b5a2013-12-03 10:13:04 +0000234 assert(receive_ssrcs_.find(config.rtp.remote_ssrc) == receive_ssrcs_.end());
235 receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000236 // TODO(pbos): Configure different RTX payloads per receive payload.
237 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
238 config.rtp.rtx.begin();
239 if (it != config.rtp.rtx.end())
240 receive_ssrcs_[it->second.ssrc] = receive_stream;
241
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000242 return receive_stream;
243}
244
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000245void Call::DestroyVideoReceiveStream(
246 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000247 assert(receive_stream != NULL);
248
249 VideoReceiveStream* receive_stream_impl = NULL;
250 {
251 WriteLockScoped write_lock(*receive_lock_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000252 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
253 // separate SSRC there can be either one or two.
254 std::map<uint32_t, VideoReceiveStream*>::iterator it =
255 receive_ssrcs_.begin();
256 while (it != receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000257 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
solenberg@webrtc.org094ac392014-01-29 11:21:58 +0000258 assert(receive_stream_impl == NULL ||
259 receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000260 receive_stream_impl = it->second;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000261 receive_ssrcs_.erase(it++);
262 } else {
263 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000264 }
265 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000266 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000267
268 assert(receive_stream_impl != NULL);
269 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000270}
271
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000272uint32_t Call::SendBitrateEstimate() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000273 // TODO(pbos): Return send-bitrate estimate
274 return 0;
275}
276
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000277uint32_t Call::ReceiveBitrateEstimate() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000278 // TODO(pbos): Return receive-bitrate estimate
279 return 0;
280}
281
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000282Call::PacketReceiver::DeliveryStatus Call::DeliverRtcp(const uint8_t* packet,
283 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000284 // TODO(pbos): Figure out what channel needs it actually.
285 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000286 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
287 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000288 bool rtcp_delivered = false;
pbos@webrtc.org40523702013-08-05 12:49:22 +0000289 {
290 ReadLockScoped read_lock(*receive_lock_);
291 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
292 receive_ssrcs_.begin();
293 it != receive_ssrcs_.end();
294 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000295 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000296 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000297 }
298 }
299
pbos@webrtc.org40523702013-08-05 12:49:22 +0000300 {
301 ReadLockScoped read_lock(*send_lock_);
302 for (std::map<uint32_t, VideoSendStream*>::iterator it =
303 send_ssrcs_.begin();
304 it != send_ssrcs_.end();
305 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000306 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000307 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000308 }
309 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000310 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000311}
312
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000313Call::PacketReceiver::DeliveryStatus Call::DeliverRtp(const RTPHeader& header,
314 const uint8_t* packet,
315 size_t length) {
solenberg@webrtc.org094ac392014-01-29 11:21:58 +0000316 ReadLockScoped read_lock(*receive_lock_);
317 std::map<uint32_t, VideoReceiveStream*>::iterator it =
318 receive_ssrcs_.find(header.ssrc);
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000319
320 if (it == receive_ssrcs_.end())
321 return DELIVERY_UNKNOWN_SSRC;
322
323 return it->second->DeliverRtp(static_cast<const uint8_t*>(packet), length)
324 ? DELIVERY_OK
325 : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000326}
327
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000328Call::PacketReceiver::DeliveryStatus Call::DeliverPacket(const uint8_t* packet,
329 size_t length) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000330 // TODO(pbos): ExtensionMap if there are extensions.
331 if (RtpHeaderParser::IsRtcp(packet, static_cast<int>(length)))
332 return DeliverRtcp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000333
pbos@webrtc.org40523702013-08-05 12:49:22 +0000334 RTPHeader rtp_header;
335 if (!rtp_header_parser_->Parse(packet, static_cast<int>(length), &rtp_header))
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000336 return DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000337
pbos@webrtc.org40523702013-08-05 12:49:22 +0000338 return DeliverRtp(rtp_header, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000339}
340
341} // namespace internal
342} // namespace webrtc