blob: 6103074bf195f1c7c7f657c7c1b9523dd4f75491 [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.org6ae48c62014-06-06 10:49:19 +000073 const VideoSendStream::Config& config,
74 const std::vector<VideoStream>& video_streams,
75 const void* encoder_settings) OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000076
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +000077 virtual void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream)
78 OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000079
80 virtual VideoReceiveStream::Config GetDefaultReceiveConfig() OVERRIDE;
81
pbos@webrtc.org5a636552013-11-20 10:40:25 +000082 virtual VideoReceiveStream* CreateVideoReceiveStream(
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000083 const VideoReceiveStream::Config& config) OVERRIDE;
84
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +000085 virtual void DestroyVideoReceiveStream(
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000086 webrtc::VideoReceiveStream* receive_stream) OVERRIDE;
87
88 virtual uint32_t SendBitrateEstimate() OVERRIDE;
89 virtual uint32_t ReceiveBitrateEstimate() OVERRIDE;
90
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +000091 virtual DeliveryStatus DeliverPacket(const uint8_t* packet,
92 size_t length) OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000093
94 private:
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +000095 DeliveryStatus DeliverRtcp(const uint8_t* packet, size_t length);
96 DeliveryStatus DeliverRtp(const RTPHeader& header,
97 const uint8_t* packet,
98 size_t length);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000099
100 Call::Config config_;
101
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000102 std::map<uint32_t, VideoReceiveStream*> receive_ssrcs_
103 GUARDED_BY(receive_lock_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000104 scoped_ptr<RWLockWrapper> receive_lock_;
105
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000106 std::map<uint32_t, VideoSendStream*> send_ssrcs_ GUARDED_BY(send_lock_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000107 scoped_ptr<RWLockWrapper> send_lock_;
108
109 scoped_ptr<RtpHeaderParser> rtp_header_parser_;
110
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000111 scoped_ptr<CpuOveruseObserverProxy> overuse_observer_proxy_;
112
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000113 VideoEngine* video_engine_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000114 ViERTP_RTCP* rtp_rtcp_;
115 ViECodec* codec_;
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000116 ViEBase* base_;
117 int base_channel_id_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000118
119 DISALLOW_COPY_AND_ASSIGN(Call);
120};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000121} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000122
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000123Call* Call::Create(const Call::Config& config) {
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000124 VideoEngine* video_engine = config.webrtc_config != NULL
125 ? VideoEngine::Create(*config.webrtc_config)
126 : VideoEngine::Create();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000127 assert(video_engine != NULL);
128
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000129 return new internal::Call(video_engine, config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000130}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000131
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000132namespace internal {
133
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000134Call::Call(webrtc::VideoEngine* video_engine, const Call::Config& config)
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000135 : config_(config),
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000136 receive_lock_(RWLockWrapper::CreateRWLock()),
137 send_lock_(RWLockWrapper::CreateRWLock()),
pbos@webrtc.org40523702013-08-05 12:49:22 +0000138 rtp_header_parser_(RtpHeaderParser::Create()),
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000139 video_engine_(video_engine),
140 base_channel_id_(-1) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000141 assert(video_engine != NULL);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000142 assert(config.send_transport != NULL);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000143
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000144 if (config.overuse_callback) {
145 overuse_observer_proxy_.reset(
146 new CpuOveruseObserverProxy(config.overuse_callback));
147 }
148
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000149 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
150 assert(rtp_rtcp_ != NULL);
151
152 codec_ = ViECodec::GetInterface(video_engine_);
153 assert(codec_ != NULL);
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000154
155 // As a workaround for non-existing calls in the old API, create a base
156 // channel used as default channel when creating send and receive streams.
157 base_ = ViEBase::GetInterface(video_engine_);
158 assert(base_ != NULL);
159
160 base_->CreateChannel(base_channel_id_);
161 assert(base_channel_id_ != -1);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000162}
163
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000164Call::~Call() {
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000165 base_->DeleteChannel(base_channel_id_);
166 base_->Release();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000167 codec_->Release();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000168 rtp_rtcp_->Release();
169 webrtc::VideoEngine::Delete(video_engine_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000170}
171
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000172PacketReceiver* Call::Receiver() { return this; }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000173
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000174VideoSendStream::Config Call::GetDefaultSendConfig() {
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000175 VideoSendStream::Config config;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000176 return config;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000177}
178
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000179VideoSendStream* Call::CreateVideoSendStream(
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000180 const VideoSendStream::Config& config,
181 const std::vector<VideoStream>& video_streams,
182 const void* encoder_settings) {
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000183 assert(config.rtp.ssrcs.size() > 0);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000184
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000185 VideoSendStream* send_stream =
186 new VideoSendStream(config_.send_transport,
187 overuse_observer_proxy_.get(),
188 video_engine_,
189 config,
190 video_streams,
191 encoder_settings,
192 base_channel_id_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000193
194 WriteLockScoped write_lock(*send_lock_);
195 for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) {
196 assert(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end());
197 send_ssrcs_[config.rtp.ssrcs[i]] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000198 }
199 return send_stream;
200}
201
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000202void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000203 assert(send_stream != NULL);
204
205 VideoSendStream* send_stream_impl = NULL;
206 {
207 WriteLockScoped write_lock(*send_lock_);
208 for (std::map<uint32_t, VideoSendStream*>::iterator it =
209 send_ssrcs_.begin();
210 it != send_ssrcs_.end();
211 ++it) {
212 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
213 send_stream_impl = it->second;
214 send_ssrcs_.erase(it);
215 break;
216 }
217 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000218 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000219
220 assert(send_stream_impl != NULL);
221 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000222}
223
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000224VideoReceiveStream::Config Call::GetDefaultReceiveConfig() {
mflodman@webrtc.org92c27932013-12-13 16:36:28 +0000225 VideoReceiveStream::Config config;
226 config.rtp.remb = true;
227 return config;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000228}
229
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000230VideoReceiveStream* Call::CreateVideoReceiveStream(
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000231 const VideoReceiveStream::Config& config) {
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000232 VideoReceiveStream* receive_stream =
233 new VideoReceiveStream(video_engine_,
234 config,
235 config_.send_transport,
236 config_.voice_engine,
237 base_channel_id_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000238
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000239 WriteLockScoped write_lock(*receive_lock_);
pbos@webrtc.orgb613b5a2013-12-03 10:13:04 +0000240 assert(receive_ssrcs_.find(config.rtp.remote_ssrc) == receive_ssrcs_.end());
241 receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000242 // TODO(pbos): Configure different RTX payloads per receive payload.
243 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
244 config.rtp.rtx.begin();
245 if (it != config.rtp.rtx.end())
246 receive_ssrcs_[it->second.ssrc] = receive_stream;
247
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000248 return receive_stream;
249}
250
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000251void Call::DestroyVideoReceiveStream(
252 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000253 assert(receive_stream != NULL);
254
255 VideoReceiveStream* receive_stream_impl = NULL;
256 {
257 WriteLockScoped write_lock(*receive_lock_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000258 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
259 // separate SSRC there can be either one or two.
260 std::map<uint32_t, VideoReceiveStream*>::iterator it =
261 receive_ssrcs_.begin();
262 while (it != receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000263 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
solenberg@webrtc.org094ac392014-01-29 11:21:58 +0000264 assert(receive_stream_impl == NULL ||
265 receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000266 receive_stream_impl = it->second;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000267 receive_ssrcs_.erase(it++);
268 } else {
269 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000270 }
271 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000272 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000273
274 assert(receive_stream_impl != NULL);
275 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000276}
277
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000278uint32_t Call::SendBitrateEstimate() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000279 // TODO(pbos): Return send-bitrate estimate
280 return 0;
281}
282
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000283uint32_t Call::ReceiveBitrateEstimate() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000284 // TODO(pbos): Return receive-bitrate estimate
285 return 0;
286}
287
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000288Call::PacketReceiver::DeliveryStatus Call::DeliverRtcp(const uint8_t* packet,
289 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000290 // TODO(pbos): Figure out what channel needs it actually.
291 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000292 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
293 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000294 bool rtcp_delivered = false;
pbos@webrtc.org40523702013-08-05 12:49:22 +0000295 {
296 ReadLockScoped read_lock(*receive_lock_);
297 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
298 receive_ssrcs_.begin();
299 it != receive_ssrcs_.end();
300 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000301 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000302 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000303 }
304 }
305
pbos@webrtc.org40523702013-08-05 12:49:22 +0000306 {
307 ReadLockScoped read_lock(*send_lock_);
308 for (std::map<uint32_t, VideoSendStream*>::iterator it =
309 send_ssrcs_.begin();
310 it != send_ssrcs_.end();
311 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000312 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000313 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000314 }
315 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000316 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000317}
318
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000319Call::PacketReceiver::DeliveryStatus Call::DeliverRtp(const RTPHeader& header,
320 const uint8_t* packet,
321 size_t length) {
solenberg@webrtc.org094ac392014-01-29 11:21:58 +0000322 ReadLockScoped read_lock(*receive_lock_);
323 std::map<uint32_t, VideoReceiveStream*>::iterator it =
324 receive_ssrcs_.find(header.ssrc);
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000325
326 if (it == receive_ssrcs_.end())
327 return DELIVERY_UNKNOWN_SSRC;
328
329 return it->second->DeliverRtp(static_cast<const uint8_t*>(packet), length)
330 ? DELIVERY_OK
331 : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000332}
333
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000334Call::PacketReceiver::DeliveryStatus Call::DeliverPacket(const uint8_t* packet,
335 size_t length) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000336 // TODO(pbos): ExtensionMap if there are extensions.
337 if (RtpHeaderParser::IsRtcp(packet, static_cast<int>(length)))
338 return DeliverRtcp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000339
pbos@webrtc.org40523702013-08-05 12:49:22 +0000340 RTPHeader rtp_header;
341 if (!rtp_header_parser_->Parse(packet, static_cast<int>(length), &rtp_header))
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000342 return DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000343
pbos@webrtc.org40523702013-08-05 12:49:22 +0000344 return DeliverRtp(rtp_header, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000345}
346
347} // namespace internal
348} // namespace webrtc