blob: ff184cdf0f293a537d295f8a79d137a1b5e8ea8c [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.org16e03b72013-10-28 16:32:01 +000019#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000020#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000021#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000022#include "webrtc/system_wrappers/interface/scoped_ptr.h"
23#include "webrtc/system_wrappers/interface/trace.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000024#include "webrtc/video/video_receive_stream.h"
25#include "webrtc/video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000026#include "webrtc/video_engine/include/vie_base.h"
27#include "webrtc/video_engine/include/vie_codec.h"
28#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000029
30namespace webrtc {
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000031namespace internal {
32class Call : public webrtc::Call, public PacketReceiver {
33 public:
34 Call(webrtc::VideoEngine* video_engine, const Call::Config& config);
35 virtual ~Call();
36
37 virtual PacketReceiver* Receiver() OVERRIDE;
38 virtual std::vector<VideoCodec> GetVideoCodecs() OVERRIDE;
39
40 virtual VideoSendStream::Config GetDefaultSendConfig() OVERRIDE;
41
pbos@webrtc.org5a636552013-11-20 10:40:25 +000042 virtual VideoSendStream* CreateVideoSendStream(
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000043 const VideoSendStream::Config& config) OVERRIDE;
44
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +000045 virtual void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream)
46 OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000047
48 virtual VideoReceiveStream::Config GetDefaultReceiveConfig() OVERRIDE;
49
pbos@webrtc.org5a636552013-11-20 10:40:25 +000050 virtual VideoReceiveStream* CreateVideoReceiveStream(
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000051 const VideoReceiveStream::Config& config) OVERRIDE;
52
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +000053 virtual void DestroyVideoReceiveStream(
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000054 webrtc::VideoReceiveStream* receive_stream) OVERRIDE;
55
56 virtual uint32_t SendBitrateEstimate() OVERRIDE;
57 virtual uint32_t ReceiveBitrateEstimate() OVERRIDE;
58
59 virtual bool DeliverPacket(const uint8_t* packet, size_t length) OVERRIDE;
60
61 private:
62 bool DeliverRtcp(const uint8_t* packet, size_t length);
63 bool DeliverRtp(const RTPHeader& header,
64 const uint8_t* packet,
65 size_t length);
66
67 Call::Config config_;
68
69 std::map<uint32_t, VideoReceiveStream*> receive_ssrcs_;
70 scoped_ptr<RWLockWrapper> receive_lock_;
71
72 std::map<uint32_t, VideoSendStream*> send_ssrcs_;
73 scoped_ptr<RWLockWrapper> send_lock_;
74
75 scoped_ptr<RtpHeaderParser> rtp_header_parser_;
76
77 webrtc::VideoEngine* video_engine_;
78 ViERTP_RTCP* rtp_rtcp_;
79 ViECodec* codec_;
80
81 DISALLOW_COPY_AND_ASSIGN(Call);
82};
83} // internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +000084
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000085class TraceDispatcher : public TraceCallback {
86 public:
87 TraceDispatcher()
88 : crit_(CriticalSectionWrapper::CreateCriticalSection()),
pbos@webrtc.org9b5c8072013-10-02 16:22:18 +000089 initialized_(false),
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000090 filter_(kTraceNone) {}
91
pbos@webrtc.org9b5c8072013-10-02 16:22:18 +000092 ~TraceDispatcher() {
93 if (initialized_) {
94 Trace::ReturnTrace();
95 VideoEngine::SetTraceCallback(NULL);
96 }
97 }
98
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000099 virtual void Print(TraceLevel level,
100 const char* message,
101 int length) OVERRIDE {
102 CriticalSectionScoped lock(crit_.get());
103 for (std::map<Call*, Call::Config*>::iterator it = callbacks_.begin();
104 it != callbacks_.end();
105 ++it) {
106 if ((level & it->second->trace_filter) != kTraceNone)
107 it->second->trace_callback->Print(level, message, length);
108 }
109 }
110
111 void RegisterCallback(Call* call, Call::Config* config) {
112 if (config->trace_callback == NULL)
113 return;
114
115 CriticalSectionScoped lock(crit_.get());
116 callbacks_[call] = config;
117
pbos@webrtc.org9b5c8072013-10-02 16:22:18 +0000118 filter_ |= config->trace_filter;
119 if (filter_ != kTraceNone && !initialized_) {
120 initialized_ = true;
121 Trace::CreateTrace();
122 VideoEngine::SetTraceCallback(this);
pbos@webrtc.orgde74b642013-10-02 13:36:09 +0000123 }
pbos@webrtc.org9b5c8072013-10-02 16:22:18 +0000124 VideoEngine::SetTraceFilter(filter_);
pbos@webrtc.orgde74b642013-10-02 13:36:09 +0000125 }
126
127 void DeregisterCallback(Call* call) {
128 CriticalSectionScoped lock(crit_.get());
129 callbacks_.erase(call);
pbos@webrtc.orgde74b642013-10-02 13:36:09 +0000130
131 filter_ = kTraceNone;
132 for (std::map<Call*, Call::Config*>::iterator it = callbacks_.begin();
133 it != callbacks_.end();
134 ++it) {
135 filter_ |= it->second->trace_filter;
136 }
137
138 VideoEngine::SetTraceFilter(filter_);
pbos@webrtc.orgde74b642013-10-02 13:36:09 +0000139 }
140
141 private:
142 scoped_ptr<CriticalSectionWrapper> crit_;
pbos@webrtc.org9b5c8072013-10-02 16:22:18 +0000143 bool initialized_;
pbos@webrtc.orgde74b642013-10-02 13:36:09 +0000144 unsigned int filter_;
145 std::map<Call*, Call::Config*> callbacks_;
146};
147
148namespace internal {
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000149TraceDispatcher* global_trace_dispatcher = NULL;
pbos@webrtc.orgde74b642013-10-02 13:36:09 +0000150} // internal
151
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000152void CreateTraceDispatcher() {
pbos@webrtc.orgde74b642013-10-02 13:36:09 +0000153 if (internal::global_trace_dispatcher == NULL) {
154 TraceDispatcher* dispatcher = new TraceDispatcher();
155 // TODO(pbos): Atomic compare and exchange.
156 if (internal::global_trace_dispatcher == NULL) {
157 internal::global_trace_dispatcher = dispatcher;
158 } else {
159 delete dispatcher;
160 }
161 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000162}
pbos@webrtc.orgde74b642013-10-02 13:36:09 +0000163
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000164Call* Call::Create(const Call::Config& config) {
165 CreateTraceDispatcher();
166
167 VideoEngine* video_engine = config.webrtc_config != NULL ?
168 VideoEngine::Create(*config.webrtc_config) : VideoEngine::Create();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000169 assert(video_engine != NULL);
170
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000171 return new internal::Call(video_engine, config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000172}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000173
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000174namespace internal {
175
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000176Call::Call(webrtc::VideoEngine* video_engine, const Call::Config& config)
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000177 : config_(config),
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000178 receive_lock_(RWLockWrapper::CreateRWLock()),
179 send_lock_(RWLockWrapper::CreateRWLock()),
pbos@webrtc.org40523702013-08-05 12:49:22 +0000180 rtp_header_parser_(RtpHeaderParser::Create()),
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000181 video_engine_(video_engine) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000182 assert(video_engine != NULL);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000183 assert(config.send_transport != NULL);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000184
pbos@webrtc.orgde74b642013-10-02 13:36:09 +0000185 global_trace_dispatcher->RegisterCallback(this, &config_);
186
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000187 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
188 assert(rtp_rtcp_ != NULL);
189
190 codec_ = ViECodec::GetInterface(video_engine_);
191 assert(codec_ != NULL);
192}
193
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000194Call::~Call() {
pbos@webrtc.orgde74b642013-10-02 13:36:09 +0000195 global_trace_dispatcher->DeregisterCallback(this);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000196 codec_->Release();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000197 rtp_rtcp_->Release();
198 webrtc::VideoEngine::Delete(video_engine_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000199}
200
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000201PacketReceiver* Call::Receiver() { return this; }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000202
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000203std::vector<VideoCodec> Call::GetVideoCodecs() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000204 std::vector<VideoCodec> codecs;
205
206 VideoCodec codec;
207 for (size_t i = 0; i < static_cast<size_t>(codec_->NumberOfCodecs()); ++i) {
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000208 if (codec_->GetCodec(static_cast<unsigned char>(i), codec) == 0) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000209 codecs.push_back(codec);
210 }
211 }
212 return codecs;
213}
214
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000215VideoSendStream::Config Call::GetDefaultSendConfig() {
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000216 VideoSendStream::Config config;
217 codec_->GetCodec(0, config.codec);
218 return config;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000219}
220
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000221VideoSendStream* Call::CreateVideoSendStream(
222 const VideoSendStream::Config& config) {
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000223 assert(config.rtp.ssrcs.size() > 0);
pbos@webrtc.org13d38a12013-11-28 11:59:31 +0000224 assert(config.rtp.ssrcs.size() >= config.codec.numberOfSimulcastStreams);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000225
pbos@webrtc.org40523702013-08-05 12:49:22 +0000226 VideoSendStream* send_stream = new VideoSendStream(
227 config_.send_transport, config_.overuse_detection, video_engine_, config);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000228
229 WriteLockScoped write_lock(*send_lock_);
230 for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) {
231 assert(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end());
232 send_ssrcs_[config.rtp.ssrcs[i]] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000233 }
234 return send_stream;
235}
236
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000237void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000238 assert(send_stream != NULL);
239
240 VideoSendStream* send_stream_impl = NULL;
241 {
242 WriteLockScoped write_lock(*send_lock_);
243 for (std::map<uint32_t, VideoSendStream*>::iterator it =
244 send_ssrcs_.begin();
245 it != send_ssrcs_.end();
246 ++it) {
247 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
248 send_stream_impl = it->second;
249 send_ssrcs_.erase(it);
250 break;
251 }
252 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000253 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000254
255 assert(send_stream_impl != NULL);
256 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000257}
258
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000259VideoReceiveStream::Config Call::GetDefaultReceiveConfig() {
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000260 return VideoReceiveStream::Config();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000261}
262
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000263VideoReceiveStream* Call::CreateVideoReceiveStream(
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000264 const VideoReceiveStream::Config& config) {
stefan@webrtc.orgb082ade2013-11-18 11:45:11 +0000265 VideoReceiveStream* receive_stream = new VideoReceiveStream(
266 video_engine_, config, config_.send_transport, config_.voice_engine);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000267
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000268 WriteLockScoped write_lock(*receive_lock_);
pbos@webrtc.orgb613b5a2013-12-03 10:13:04 +0000269 assert(receive_ssrcs_.find(config.rtp.remote_ssrc) == receive_ssrcs_.end());
270 receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000271 return receive_stream;
272}
273
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000274void Call::DestroyVideoReceiveStream(
275 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000276 assert(receive_stream != NULL);
277
278 VideoReceiveStream* receive_stream_impl = NULL;
279 {
280 WriteLockScoped write_lock(*receive_lock_);
281 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
282 receive_ssrcs_.begin();
283 it != receive_ssrcs_.end();
284 ++it) {
285 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
286 receive_stream_impl = it->second;
287 receive_ssrcs_.erase(it);
288 break;
289 }
290 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000291 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000292
293 assert(receive_stream_impl != NULL);
294 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000295}
296
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000297uint32_t Call::SendBitrateEstimate() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000298 // TODO(pbos): Return send-bitrate estimate
299 return 0;
300}
301
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000302uint32_t Call::ReceiveBitrateEstimate() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000303 // TODO(pbos): Return receive-bitrate estimate
304 return 0;
305}
306
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000307bool Call::DeliverRtcp(const uint8_t* packet, size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000308 // TODO(pbos): Figure out what channel needs it actually.
309 // Do NOT broadcast! Also make sure it's a valid packet.
310 bool rtcp_delivered = false;
pbos@webrtc.org40523702013-08-05 12:49:22 +0000311 {
312 ReadLockScoped read_lock(*receive_lock_);
313 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
314 receive_ssrcs_.begin();
315 it != receive_ssrcs_.end();
316 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000317 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000318 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000319 }
320 }
321
pbos@webrtc.org40523702013-08-05 12:49:22 +0000322 {
323 ReadLockScoped read_lock(*send_lock_);
324 for (std::map<uint32_t, VideoSendStream*>::iterator it =
325 send_ssrcs_.begin();
326 it != send_ssrcs_.end();
327 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000328 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000329 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000330 }
331 }
332 return rtcp_delivered;
333}
334
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000335bool Call::DeliverRtp(const RTPHeader& header,
336 const uint8_t* packet,
337 size_t length) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000338 VideoReceiveStream* receiver;
339 {
340 ReadLockScoped read_lock(*receive_lock_);
341 std::map<uint32_t, VideoReceiveStream*>::iterator it =
342 receive_ssrcs_.find(header.ssrc);
343 if (it == receive_ssrcs_.end()) {
344 // TODO(pbos): Log some warning, SSRC without receiver.
345 return false;
346 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000347
pbos@webrtc.org40523702013-08-05 12:49:22 +0000348 receiver = it->second;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000349 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000350 return receiver->DeliverRtp(static_cast<const uint8_t*>(packet), length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000351}
352
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000353bool Call::DeliverPacket(const uint8_t* packet, size_t length) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000354 // TODO(pbos): ExtensionMap if there are extensions.
355 if (RtpHeaderParser::IsRtcp(packet, static_cast<int>(length)))
356 return DeliverRtcp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000357
pbos@webrtc.org40523702013-08-05 12:49:22 +0000358 RTPHeader rtp_header;
359 if (!rtp_header_parser_->Parse(packet, static_cast<int>(length), &rtp_header))
360 return false;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000361
pbos@webrtc.org40523702013-08-05 12:49:22 +0000362 return DeliverRtp(rtp_header, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000363}
364
365} // namespace internal
366} // namespace webrtc