blob: ec42e3e4d617fbb670a397016ab6ef6e8db42810 [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"
18#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000019#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000020#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000021#include "webrtc/system_wrappers/interface/scoped_ptr.h"
22#include "webrtc/system_wrappers/interface/trace.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000023#include "webrtc/video/video_receive_stream.h"
24#include "webrtc/video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000025#include "webrtc/video_engine/include/vie_base.h"
26#include "webrtc/video_engine/include/vie_codec.h"
27#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000028
29namespace webrtc {
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000030namespace internal {
31class Call : public webrtc::Call, public PacketReceiver {
32 public:
33 Call(webrtc::VideoEngine* video_engine, const Call::Config& config);
34 virtual ~Call();
35
36 virtual PacketReceiver* Receiver() OVERRIDE;
37 virtual std::vector<VideoCodec> GetVideoCodecs() OVERRIDE;
38
39 virtual VideoSendStream::Config GetDefaultSendConfig() OVERRIDE;
40
41 virtual VideoSendStream* CreateSendStream(
42 const VideoSendStream::Config& config) OVERRIDE;
43
pbos@webrtc.org64887612013-11-14 08:58:14 +000044 virtual void DestroySendStream(webrtc::VideoSendStream* send_stream) OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000045
46 virtual VideoReceiveStream::Config GetDefaultReceiveConfig() OVERRIDE;
47
48 virtual VideoReceiveStream* CreateReceiveStream(
49 const VideoReceiveStream::Config& config) OVERRIDE;
50
51 virtual void DestroyReceiveStream(
52 webrtc::VideoReceiveStream* receive_stream) OVERRIDE;
53
54 virtual uint32_t SendBitrateEstimate() OVERRIDE;
55 virtual uint32_t ReceiveBitrateEstimate() OVERRIDE;
56
57 virtual bool DeliverPacket(const uint8_t* packet, size_t length) OVERRIDE;
58
59 private:
60 bool DeliverRtcp(const uint8_t* packet, size_t length);
61 bool DeliverRtp(const RTPHeader& header,
62 const uint8_t* packet,
63 size_t length);
64
65 Call::Config config_;
66
67 std::map<uint32_t, VideoReceiveStream*> receive_ssrcs_;
68 scoped_ptr<RWLockWrapper> receive_lock_;
69
70 std::map<uint32_t, VideoSendStream*> send_ssrcs_;
71 scoped_ptr<RWLockWrapper> send_lock_;
72
73 scoped_ptr<RtpHeaderParser> rtp_header_parser_;
74
75 webrtc::VideoEngine* video_engine_;
76 ViERTP_RTCP* rtp_rtcp_;
77 ViECodec* codec_;
78
79 DISALLOW_COPY_AND_ASSIGN(Call);
80};
81} // internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +000082
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000083class TraceDispatcher : public TraceCallback {
84 public:
85 TraceDispatcher()
86 : crit_(CriticalSectionWrapper::CreateCriticalSection()),
pbos@webrtc.org9b5c8072013-10-02 16:22:18 +000087 initialized_(false),
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000088 filter_(kTraceNone) {}
89
pbos@webrtc.org9b5c8072013-10-02 16:22:18 +000090 ~TraceDispatcher() {
91 if (initialized_) {
92 Trace::ReturnTrace();
93 VideoEngine::SetTraceCallback(NULL);
94 }
95 }
96
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000097 virtual void Print(TraceLevel level,
98 const char* message,
99 int length) OVERRIDE {
100 CriticalSectionScoped lock(crit_.get());
101 for (std::map<Call*, Call::Config*>::iterator it = callbacks_.begin();
102 it != callbacks_.end();
103 ++it) {
104 if ((level & it->second->trace_filter) != kTraceNone)
105 it->second->trace_callback->Print(level, message, length);
106 }
107 }
108
109 void RegisterCallback(Call* call, Call::Config* config) {
110 if (config->trace_callback == NULL)
111 return;
112
113 CriticalSectionScoped lock(crit_.get());
114 callbacks_[call] = config;
115
pbos@webrtc.org9b5c8072013-10-02 16:22:18 +0000116 filter_ |= config->trace_filter;
117 if (filter_ != kTraceNone && !initialized_) {
118 initialized_ = true;
119 Trace::CreateTrace();
120 VideoEngine::SetTraceCallback(this);
pbos@webrtc.orgde74b642013-10-02 13:36:09 +0000121 }
pbos@webrtc.org9b5c8072013-10-02 16:22:18 +0000122 VideoEngine::SetTraceFilter(filter_);
pbos@webrtc.orgde74b642013-10-02 13:36:09 +0000123 }
124
125 void DeregisterCallback(Call* call) {
126 CriticalSectionScoped lock(crit_.get());
127 callbacks_.erase(call);
pbos@webrtc.orgde74b642013-10-02 13:36:09 +0000128
129 filter_ = kTraceNone;
130 for (std::map<Call*, Call::Config*>::iterator it = callbacks_.begin();
131 it != callbacks_.end();
132 ++it) {
133 filter_ |= it->second->trace_filter;
134 }
135
136 VideoEngine::SetTraceFilter(filter_);
pbos@webrtc.orgde74b642013-10-02 13:36:09 +0000137 }
138
139 private:
140 scoped_ptr<CriticalSectionWrapper> crit_;
pbos@webrtc.org9b5c8072013-10-02 16:22:18 +0000141 bool initialized_;
pbos@webrtc.orgde74b642013-10-02 13:36:09 +0000142 unsigned int filter_;
143 std::map<Call*, Call::Config*> callbacks_;
144};
145
146namespace internal {
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000147TraceDispatcher* global_trace_dispatcher = NULL;
pbos@webrtc.orgde74b642013-10-02 13:36:09 +0000148} // internal
149
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000150Call* Call::Create(const Call::Config& config) {
pbos@webrtc.orgde74b642013-10-02 13:36:09 +0000151 if (internal::global_trace_dispatcher == NULL) {
152 TraceDispatcher* dispatcher = new TraceDispatcher();
153 // TODO(pbos): Atomic compare and exchange.
154 if (internal::global_trace_dispatcher == NULL) {
155 internal::global_trace_dispatcher = dispatcher;
156 } else {
157 delete dispatcher;
158 }
159 }
160
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000161 VideoEngine* video_engine = VideoEngine::Create();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000162 assert(video_engine != NULL);
163
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000164 return new internal::Call(video_engine, config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000165}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000166
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000167namespace internal {
168
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000169Call::Call(webrtc::VideoEngine* video_engine, const Call::Config& config)
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000170 : config_(config),
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000171 receive_lock_(RWLockWrapper::CreateRWLock()),
172 send_lock_(RWLockWrapper::CreateRWLock()),
pbos@webrtc.org40523702013-08-05 12:49:22 +0000173 rtp_header_parser_(RtpHeaderParser::Create()),
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000174 video_engine_(video_engine) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000175 assert(video_engine != NULL);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000176 assert(config.send_transport != NULL);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000177
pbos@webrtc.orgde74b642013-10-02 13:36:09 +0000178 global_trace_dispatcher->RegisterCallback(this, &config_);
179
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000180 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
181 assert(rtp_rtcp_ != NULL);
182
183 codec_ = ViECodec::GetInterface(video_engine_);
184 assert(codec_ != NULL);
185}
186
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000187Call::~Call() {
pbos@webrtc.orgde74b642013-10-02 13:36:09 +0000188 global_trace_dispatcher->DeregisterCallback(this);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000189 codec_->Release();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000190 rtp_rtcp_->Release();
191 webrtc::VideoEngine::Delete(video_engine_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000192}
193
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000194PacketReceiver* Call::Receiver() { return this; }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000195
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000196std::vector<VideoCodec> Call::GetVideoCodecs() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000197 std::vector<VideoCodec> codecs;
198
199 VideoCodec codec;
200 for (size_t i = 0; i < static_cast<size_t>(codec_->NumberOfCodecs()); ++i) {
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000201 if (codec_->GetCodec(static_cast<unsigned char>(i), codec) == 0) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000202 codecs.push_back(codec);
203 }
204 }
205 return codecs;
206}
207
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000208VideoSendStream::Config Call::GetDefaultSendConfig() {
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000209 VideoSendStream::Config config;
210 codec_->GetCodec(0, config.codec);
211 return config;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000212}
213
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000214VideoSendStream* Call::CreateSendStream(const VideoSendStream::Config& config) {
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000215 assert(config.rtp.ssrcs.size() > 0);
216 assert(config.codec.numberOfSimulcastStreams == 0 ||
217 config.codec.numberOfSimulcastStreams == config.rtp.ssrcs.size());
218
pbos@webrtc.org40523702013-08-05 12:49:22 +0000219 VideoSendStream* send_stream = new VideoSendStream(
220 config_.send_transport, config_.overuse_detection, video_engine_, config);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000221
222 WriteLockScoped write_lock(*send_lock_);
223 for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) {
224 assert(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end());
225 send_ssrcs_[config.rtp.ssrcs[i]] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000226 }
227 return send_stream;
228}
229
pbos@webrtc.org64887612013-11-14 08:58:14 +0000230void Call::DestroySendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000231 assert(send_stream != NULL);
232
233 VideoSendStream* send_stream_impl = NULL;
234 {
235 WriteLockScoped write_lock(*send_lock_);
236 for (std::map<uint32_t, VideoSendStream*>::iterator it =
237 send_ssrcs_.begin();
238 it != send_ssrcs_.end();
239 ++it) {
240 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
241 send_stream_impl = it->second;
242 send_ssrcs_.erase(it);
243 break;
244 }
245 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000246 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000247
248 assert(send_stream_impl != NULL);
249 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000250}
251
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000252VideoReceiveStream::Config Call::GetDefaultReceiveConfig() {
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000253 return VideoReceiveStream::Config();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000254}
255
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000256VideoReceiveStream* Call::CreateReceiveStream(
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000257 const VideoReceiveStream::Config& config) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000258 VideoReceiveStream* receive_stream =
259 new VideoReceiveStream(video_engine_, config, config_.send_transport);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000260
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000261 WriteLockScoped write_lock(*receive_lock_);
262 assert(receive_ssrcs_.find(config.rtp.ssrc) == receive_ssrcs_.end());
263 receive_ssrcs_[config.rtp.ssrc] = receive_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000264 return receive_stream;
265}
266
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000267void Call::DestroyReceiveStream(webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000268 assert(receive_stream != NULL);
269
270 VideoReceiveStream* receive_stream_impl = NULL;
271 {
272 WriteLockScoped write_lock(*receive_lock_);
273 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
274 receive_ssrcs_.begin();
275 it != receive_ssrcs_.end();
276 ++it) {
277 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
278 receive_stream_impl = it->second;
279 receive_ssrcs_.erase(it);
280 break;
281 }
282 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000283 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000284
285 assert(receive_stream_impl != NULL);
286 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000287}
288
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000289uint32_t Call::SendBitrateEstimate() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000290 // TODO(pbos): Return send-bitrate estimate
291 return 0;
292}
293
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000294uint32_t Call::ReceiveBitrateEstimate() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000295 // TODO(pbos): Return receive-bitrate estimate
296 return 0;
297}
298
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000299bool Call::DeliverRtcp(const uint8_t* packet, size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000300 // TODO(pbos): Figure out what channel needs it actually.
301 // Do NOT broadcast! Also make sure it's a valid packet.
302 bool rtcp_delivered = false;
pbos@webrtc.org40523702013-08-05 12:49:22 +0000303 {
304 ReadLockScoped read_lock(*receive_lock_);
305 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
306 receive_ssrcs_.begin();
307 it != receive_ssrcs_.end();
308 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000309 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000310 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000311 }
312 }
313
pbos@webrtc.org40523702013-08-05 12:49:22 +0000314 {
315 ReadLockScoped read_lock(*send_lock_);
316 for (std::map<uint32_t, VideoSendStream*>::iterator it =
317 send_ssrcs_.begin();
318 it != send_ssrcs_.end();
319 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000320 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000321 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000322 }
323 }
324 return rtcp_delivered;
325}
326
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000327bool Call::DeliverRtp(const RTPHeader& header,
328 const uint8_t* packet,
329 size_t length) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000330 VideoReceiveStream* receiver;
331 {
332 ReadLockScoped read_lock(*receive_lock_);
333 std::map<uint32_t, VideoReceiveStream*>::iterator it =
334 receive_ssrcs_.find(header.ssrc);
335 if (it == receive_ssrcs_.end()) {
336 // TODO(pbos): Log some warning, SSRC without receiver.
337 return false;
338 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000339
pbos@webrtc.org40523702013-08-05 12:49:22 +0000340 receiver = it->second;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000341 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000342 return receiver->DeliverRtp(static_cast<const uint8_t*>(packet), length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000343}
344
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000345bool Call::DeliverPacket(const uint8_t* packet, size_t length) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000346 // TODO(pbos): ExtensionMap if there are extensions.
347 if (RtpHeaderParser::IsRtcp(packet, static_cast<int>(length)))
348 return DeliverRtcp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000349
pbos@webrtc.org40523702013-08-05 12:49:22 +0000350 RTPHeader rtp_header;
351 if (!rtp_header_parser_->Parse(packet, static_cast<int>(length), &rtp_header))
352 return false;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000353
pbos@webrtc.org40523702013-08-05 12:49:22 +0000354 return DeliverRtp(rtp_header, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000355}
356
357} // namespace internal
358} // namespace webrtc