blob: 594ddf5c97a1014a4b6ea9dfe4f9723908c6a643 [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 <string.h>
12
pbos@webrtc.org29d58392013-05-16 12:08:03 +000013#include <map>
14#include <vector>
15
Peter Boström5c389d32015-09-25 13:58:30 +020016#include "webrtc/audio/audio_receive_stream.h"
solenbergc7a8b082015-10-16 14:35:07 -070017#include "webrtc/audio/audio_send_stream.h"
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +000018#include "webrtc/base/checks.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000019#include "webrtc/base/scoped_ptr.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000020#include "webrtc/base/thread_annotations.h"
solenberg5a289392015-10-19 03:39:20 -070021#include "webrtc/base/thread_checker.h"
tommie4f96502015-10-20 23:00:48 -070022#include "webrtc/base/trace_event.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000023#include "webrtc/call.h"
mflodman0c478b32015-10-21 15:52:16 +020024#include "webrtc/call/congestion_controller.h"
Peter Boström5c389d32015-09-25 13:58:30 +020025#include "webrtc/call/rtc_event_log.h"
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000026#include "webrtc/common.h"
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +000027#include "webrtc/config.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000028#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +000029#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
Peter Boström45553ae2015-05-08 13:54:38 +020030#include "webrtc/modules/utility/interface/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010031#include "webrtc/system_wrappers/include/cpu_info.h"
32#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
33#include "webrtc/system_wrappers/include/logging.h"
34#include "webrtc/system_wrappers/include/rw_lock_wrapper.h"
35#include "webrtc/system_wrappers/include/trace.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000036#include "webrtc/video/video_receive_stream.h"
37#include "webrtc/video/video_send_stream.h"
mflodmane3787022015-10-21 13:24:28 +020038#include "webrtc/video_engine/call_stats.h"
ivocb04965c2015-09-09 00:09:43 -070039#include "webrtc/voice_engine/include/voe_codec.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000040
41namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000042
pbos@webrtc.orga73a6782014-10-14 11:52:10 +000043const int Call::Config::kDefaultStartBitrateBps = 300000;
44
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000045namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000046
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000047class Call : public webrtc::Call, public PacketReceiver {
48 public:
Peter Boström45553ae2015-05-08 13:54:38 +020049 explicit Call(const Call::Config& config);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000050 virtual ~Call();
51
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000052 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000053
Fredrik Solenberg04f49312015-06-08 13:04:56 +020054 webrtc::AudioSendStream* CreateAudioSendStream(
55 const webrtc::AudioSendStream::Config& config) override;
56 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
57
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020058 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
59 const webrtc::AudioReceiveStream::Config& config) override;
60 void DestroyAudioReceiveStream(
61 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000062
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020063 webrtc::VideoSendStream* CreateVideoSendStream(
64 const webrtc::VideoSendStream::Config& config,
65 const VideoEncoderConfig& encoder_config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000066 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000067
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020068 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
69 const webrtc::VideoReceiveStream::Config& config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000070 void DestroyVideoReceiveStream(
71 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000072
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000073 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000074
stefan68786d22015-09-08 05:36:15 -070075 DeliveryStatus DeliverPacket(MediaType media_type,
76 const uint8_t* packet,
77 size_t length,
78 const PacketTime& packet_time) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000079
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000080 void SetBitrateConfig(
81 const webrtc::Call::Config::BitrateConfig& bitrate_config) override;
82 void SignalNetworkState(NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +000083
stefanc1aeaf02015-10-15 07:26:07 -070084 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
85
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000086 private:
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020087 DeliveryStatus DeliverRtcp(MediaType media_type, const uint8_t* packet,
88 size_t length);
stefan68786d22015-09-08 05:36:15 -070089 DeliveryStatus DeliverRtp(MediaType media_type,
90 const uint8_t* packet,
91 size_t length,
92 const PacketTime& packet_time);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000093
pbos8fc7fa72015-07-15 08:02:58 -070094 void ConfigureSync(const std::string& sync_group)
95 EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
96
Peter Boström45553ae2015-05-08 13:54:38 +020097 const int num_cpu_cores_;
98 const rtc::scoped_ptr<ProcessThread> module_process_thread_;
mflodmane3787022015-10-21 13:24:28 +020099 const rtc::scoped_ptr<CallStats> call_stats_;
mflodman0c478b32015-10-21 15:52:16 +0200100 const rtc::scoped_ptr<CongestionController> congestion_controller_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000101 Call::Config config_;
solenberg5a289392015-10-19 03:39:20 -0700102 rtc::ThreadChecker configuration_thread_checker_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000103
mflodman717432f2015-10-26 16:34:46 +0100104 bool network_enabled_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000105
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000106 rtc::scoped_ptr<RWLockWrapper> receive_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700107 // Audio and Video receive streams are owned by the client that creates them.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200108 std::map<uint32_t, AudioReceiveStream*> audio_receive_ssrcs_
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000109 GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200110 std::map<uint32_t, VideoReceiveStream*> video_receive_ssrcs_
111 GUARDED_BY(receive_crit_);
112 std::set<VideoReceiveStream*> video_receive_streams_
113 GUARDED_BY(receive_crit_);
pbos8fc7fa72015-07-15 08:02:58 -0700114 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
115 GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000116
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000117 rtc::scoped_ptr<RWLockWrapper> send_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700118 // Audio and Video send streams are owned by the client that creates them.
119 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_ GUARDED_BY(send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200120 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_ GUARDED_BY(send_crit_);
121 std::set<VideoSendStream*> video_send_streams_ GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000122
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200123 VideoSendStream::RtpStateMap suspended_video_send_ssrcs_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000124
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200125 RtcEventLog* event_log_ = nullptr;
126 VoECodec* voe_codec_ = nullptr;
ivocb04965c2015-09-09 00:09:43 -0700127
henrikg3c089d72015-09-16 05:37:44 -0700128 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000129};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000130} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000131
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000132Call* Call::Create(const Call::Config& config) {
Peter Boström45553ae2015-05-08 13:54:38 +0200133 return new internal::Call(config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000134}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000135
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000136namespace internal {
137
Peter Boström45553ae2015-05-08 13:54:38 +0200138Call::Call(const Call::Config& config)
139 : num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
stefan847855b2015-09-11 09:52:15 -0700140 module_process_thread_(ProcessThread::Create("ModuleProcessThread")),
mflodmane3787022015-10-21 13:24:28 +0200141 call_stats_(new CallStats()),
mflodman0c478b32015-10-21 15:52:16 +0200142 congestion_controller_(new CongestionController(
143 module_process_thread_.get(), call_stats_.get())),
Peter Boström45553ae2015-05-08 13:54:38 +0200144 config_(config),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000145 network_enabled_(true),
146 receive_crit_(RWLockWrapper::CreateRWLock()),
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200147 send_crit_(RWLockWrapper::CreateRWLock()) {
henrikg91d6ede2015-09-17 00:24:34 -0700148 RTC_DCHECK_GE(config.bitrate_config.min_bitrate_bps, 0);
149 RTC_DCHECK_GE(config.bitrate_config.start_bitrate_bps,
150 config.bitrate_config.min_bitrate_bps);
Stefan Holmere5904162015-03-26 11:11:06 +0100151 if (config.bitrate_config.max_bitrate_bps != -1) {
henrikg91d6ede2015-09-17 00:24:34 -0700152 RTC_DCHECK_GE(config.bitrate_config.max_bitrate_bps,
153 config.bitrate_config.start_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000154 }
ivocb04965c2015-09-09 00:09:43 -0700155 if (config.voice_engine) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200156 // Keep a reference to VoECodec, so we're sure the VoiceEngine lives for the
157 // duration of the call.
158 voe_codec_ = VoECodec::GetInterface(config.voice_engine);
159 if (voe_codec_)
160 event_log_ = voe_codec_->GetEventLog();
ivocb04965c2015-09-09 00:09:43 -0700161 }
pbos@webrtc.org00873182014-11-25 14:03:34 +0000162
Peter Boström45553ae2015-05-08 13:54:38 +0200163 Trace::CreateTrace();
164 module_process_thread_->Start();
mflodmane3787022015-10-21 13:24:28 +0200165 module_process_thread_->RegisterModule(call_stats_.get());
Peter Boström45553ae2015-05-08 13:54:38 +0200166
mflodman0c478b32015-10-21 15:52:16 +0200167 congestion_controller_->SetBweBitrates(
168 config_.bitrate_config.min_bitrate_bps,
169 config_.bitrate_config.start_bitrate_bps,
170 config_.bitrate_config.max_bitrate_bps);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000171}
172
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000173Call::~Call() {
solenberg5a289392015-10-19 03:39:20 -0700174 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700175 RTC_CHECK(audio_send_ssrcs_.empty());
176 RTC_CHECK(video_send_ssrcs_.empty());
177 RTC_CHECK(video_send_streams_.empty());
178 RTC_CHECK(audio_receive_ssrcs_.empty());
179 RTC_CHECK(video_receive_ssrcs_.empty());
180 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000181
mflodmane3787022015-10-21 13:24:28 +0200182 module_process_thread_->DeRegisterModule(call_stats_.get());
Peter Boström45553ae2015-05-08 13:54:38 +0200183 module_process_thread_->Stop();
184 Trace::ReturnTrace();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200185
186 if (voe_codec_)
187 voe_codec_->Release();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000188}
189
solenberg5a289392015-10-19 03:39:20 -0700190PacketReceiver* Call::Receiver() {
191 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
192 // thread. Re-enable once that is fixed.
193 // RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
194 return this;
195}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000196
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200197webrtc::AudioSendStream* Call::CreateAudioSendStream(
198 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700199 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
solenberg5a289392015-10-19 03:39:20 -0700200 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -0700201 AudioSendStream* send_stream =
202 new AudioSendStream(config, config_.voice_engine);
mflodman717432f2015-10-26 16:34:46 +0100203 if (!network_enabled_)
204 send_stream->SignalNetworkState(kNetworkDown);
solenbergc7a8b082015-10-16 14:35:07 -0700205 {
solenbergc7a8b082015-10-16 14:35:07 -0700206 WriteLockScoped write_lock(*send_crit_);
207 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
208 audio_send_ssrcs_.end());
209 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
solenbergc7a8b082015-10-16 14:35:07 -0700210 }
211 return send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200212}
213
214void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 14:35:07 -0700215 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
solenberg5a289392015-10-19 03:39:20 -0700216 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700217 RTC_DCHECK(send_stream != nullptr);
218
219 send_stream->Stop();
220
221 webrtc::internal::AudioSendStream* audio_send_stream =
222 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
223 {
224 WriteLockScoped write_lock(*send_crit_);
225 size_t num_deleted = audio_send_ssrcs_.erase(
226 audio_send_stream->config().rtp.ssrc);
227 RTC_DCHECK(num_deleted == 1);
228 }
229 delete audio_send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200230}
231
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200232webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
233 const webrtc::AudioReceiveStream::Config& config) {
234 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700235 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200236 AudioReceiveStream* receive_stream = new AudioReceiveStream(
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200237 congestion_controller_->GetRemoteBitrateEstimator(false), config,
238 config_.voice_engine);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200239 {
240 WriteLockScoped write_lock(*receive_crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700241 RTC_DCHECK(audio_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
242 audio_receive_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200243 audio_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos8fc7fa72015-07-15 08:02:58 -0700244 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200245 }
246 return receive_stream;
247}
248
249void Call::DestroyAudioReceiveStream(
250 webrtc::AudioReceiveStream* receive_stream) {
251 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700252 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700253 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700254 webrtc::internal::AudioReceiveStream* audio_receive_stream =
255 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200256 {
257 WriteLockScoped write_lock(*receive_crit_);
258 size_t num_deleted = audio_receive_ssrcs_.erase(
259 audio_receive_stream->config().rtp.remote_ssrc);
henrikg91d6ede2015-09-17 00:24:34 -0700260 RTC_DCHECK(num_deleted == 1);
pbos8fc7fa72015-07-15 08:02:58 -0700261 const std::string& sync_group = audio_receive_stream->config().sync_group;
262 const auto it = sync_stream_mapping_.find(sync_group);
263 if (it != sync_stream_mapping_.end() &&
264 it->second == audio_receive_stream) {
265 sync_stream_mapping_.erase(it);
266 ConfigureSync(sync_group);
267 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200268 }
269 delete audio_receive_stream;
270}
271
272webrtc::VideoSendStream* Call::CreateVideoSendStream(
273 const webrtc::VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000274 const VideoEncoderConfig& encoder_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000275 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
solenberg5a289392015-10-19 03:39:20 -0700276 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000277
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000278 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
279 // the call has already started.
mflodman0c478b32015-10-21 15:52:16 +0200280 VideoSendStream* send_stream = new VideoSendStream(
281 num_cpu_cores_, module_process_thread_.get(), call_stats_.get(),
282 congestion_controller_.get(), config, encoder_config,
283 suspended_video_send_ssrcs_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000284
mflodman717432f2015-10-26 16:34:46 +0100285 if (!network_enabled_)
286 send_stream->SignalNetworkState(kNetworkDown);
287
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000288 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200289 for (uint32_t ssrc : config.rtp.ssrcs) {
henrikg91d6ede2015-09-17 00:24:34 -0700290 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200291 video_send_ssrcs_[ssrc] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000292 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200293 video_send_streams_.insert(send_stream);
294
ivocb04965c2015-09-09 00:09:43 -0700295 if (event_log_)
296 event_log_->LogVideoSendStreamConfig(config);
297
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000298 return send_stream;
299}
300
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000301void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000302 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700303 RTC_DCHECK(send_stream != nullptr);
solenberg5a289392015-10-19 03:39:20 -0700304 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000305
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000306 send_stream->Stop();
307
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000308 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000309 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000310 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200311 auto it = video_send_ssrcs_.begin();
312 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000313 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
314 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200315 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000316 } else {
317 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000318 }
319 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200320 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000321 }
henrikg91d6ede2015-09-17 00:24:34 -0700322 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000323
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000324 VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates();
325
326 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
327 it != rtp_state.end();
328 ++it) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200329 suspended_video_send_ssrcs_[it->first] = it->second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000330 }
331
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000332 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000333}
334
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200335webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
336 const webrtc::VideoReceiveStream::Config& config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000337 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700338 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
Peter Boströmc4188fd2015-04-24 15:16:03 +0200339 VideoReceiveStream* receive_stream = new VideoReceiveStream(
mflodman0c478b32015-10-21 15:52:16 +0200340 num_cpu_cores_, congestion_controller_.get(), config,
341 config_.voice_engine, module_process_thread_.get(), call_stats_.get());
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000342
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000343 WriteLockScoped write_lock(*receive_crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700344 RTC_DCHECK(video_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
345 video_receive_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200346 video_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000347 // TODO(pbos): Configure different RTX payloads per receive payload.
348 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
349 config.rtp.rtx.begin();
350 if (it != config.rtp.rtx.end())
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200351 video_receive_ssrcs_[it->second.ssrc] = receive_stream;
352 video_receive_streams_.insert(receive_stream);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000353
pbos8fc7fa72015-07-15 08:02:58 -0700354 ConfigureSync(config.sync_group);
355
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000356 if (!network_enabled_)
357 receive_stream->SignalNetworkState(kNetworkDown);
pbos8fc7fa72015-07-15 08:02:58 -0700358
ivocb04965c2015-09-09 00:09:43 -0700359 if (event_log_)
360 event_log_->LogVideoReceiveStreamConfig(config);
361
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000362 return receive_stream;
363}
364
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000365void Call::DestroyVideoReceiveStream(
366 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000367 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700368 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700369 RTC_DCHECK(receive_stream != nullptr);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000370 VideoReceiveStream* receive_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000371 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000372 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000373 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
374 // separate SSRC there can be either one or two.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200375 auto it = video_receive_ssrcs_.begin();
376 while (it != video_receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000377 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000378 if (receive_stream_impl != nullptr)
henrikg91d6ede2015-09-17 00:24:34 -0700379 RTC_DCHECK(receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000380 receive_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200381 video_receive_ssrcs_.erase(it++);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000382 } else {
383 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000384 }
385 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200386 video_receive_streams_.erase(receive_stream_impl);
henrikg91d6ede2015-09-17 00:24:34 -0700387 RTC_CHECK(receive_stream_impl != nullptr);
pbos8fc7fa72015-07-15 08:02:58 -0700388 ConfigureSync(receive_stream_impl->config().sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000389 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000390 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000391}
392
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000393Call::Stats Call::GetStats() const {
solenberg5a289392015-10-19 03:39:20 -0700394 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
395 // thread. Re-enable once that is fixed.
396 // RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000397 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +0200398 // Fetch available send/receive bitrates.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000399 uint32_t send_bandwidth = 0;
mflodman0c478b32015-10-21 15:52:16 +0200400 congestion_controller_->GetBitrateController()->AvailableBandwidth(
401 &send_bandwidth);
Peter Boström45553ae2015-05-08 13:54:38 +0200402 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000403 uint32_t recv_bandwidth = 0;
mflodman0c478b32015-10-21 15:52:16 +0200404 congestion_controller_->GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-18 22:08:19 -0700405 &ssrcs, &recv_bandwidth);
Peter Boström45553ae2015-05-08 13:54:38 +0200406 stats.send_bandwidth_bps = send_bandwidth;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000407 stats.recv_bandwidth_bps = recv_bandwidth;
mflodman0c478b32015-10-21 15:52:16 +0200408 stats.pacer_delay_ms = congestion_controller_->GetPacerQueuingDelayMs();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000409 {
410 ReadLockScoped read_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -0700411 // TODO(solenberg): Add audio send streams.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200412 for (const auto& kv : video_send_ssrcs_) {
413 int rtt_ms = kv.second->GetRtt();
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000414 if (rtt_ms > 0)
415 stats.rtt_ms = rtt_ms;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000416 }
417 }
418 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000419}
420
pbos@webrtc.org00873182014-11-25 14:03:34 +0000421void Call::SetBitrateConfig(
422 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000423 TRACE_EVENT0("webrtc", "Call::SetBitrateConfig");
solenberg5a289392015-10-19 03:39:20 -0700424 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700425 RTC_DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000426 if (bitrate_config.max_bitrate_bps != -1)
henrikg91d6ede2015-09-17 00:24:34 -0700427 RTC_DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
Stefan Holmere5904162015-03-26 11:11:06 +0100428 if (config_.bitrate_config.min_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000429 bitrate_config.min_bitrate_bps &&
430 (bitrate_config.start_bitrate_bps <= 0 ||
Stefan Holmere5904162015-03-26 11:11:06 +0100431 config_.bitrate_config.start_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000432 bitrate_config.start_bitrate_bps) &&
Stefan Holmere5904162015-03-26 11:11:06 +0100433 config_.bitrate_config.max_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000434 bitrate_config.max_bitrate_bps) {
435 // Nothing new to set, early abort to avoid encoder reconfigurations.
436 return;
437 }
Stefan Holmere5904162015-03-26 11:11:06 +0100438 config_.bitrate_config = bitrate_config;
mflodman0c478b32015-10-21 15:52:16 +0200439 congestion_controller_->SetBweBitrates(bitrate_config.min_bitrate_bps,
440 bitrate_config.start_bitrate_bps,
441 bitrate_config.max_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000442}
443
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000444void Call::SignalNetworkState(NetworkState state) {
solenberg5a289392015-10-19 03:39:20 -0700445 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000446 network_enabled_ = state == kNetworkUp;
mflodman0c478b32015-10-21 15:52:16 +0200447 congestion_controller_->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000448 {
449 ReadLockScoped write_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -0700450 for (auto& kv : audio_send_ssrcs_) {
451 kv.second->SignalNetworkState(state);
452 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200453 for (auto& kv : video_send_ssrcs_) {
454 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000455 }
456 }
457 {
458 ReadLockScoped write_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200459 for (auto& kv : video_receive_ssrcs_) {
460 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000461 }
462 }
463}
464
stefanc1aeaf02015-10-15 07:26:07 -0700465void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
mflodman0c478b32015-10-21 15:52:16 +0200466 congestion_controller_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -0700467}
468
pbos8fc7fa72015-07-15 08:02:58 -0700469void Call::ConfigureSync(const std::string& sync_group) {
470 // Set sync only if there was no previous one.
471 if (config_.voice_engine == nullptr || sync_group.empty())
472 return;
473
474 AudioReceiveStream* sync_audio_stream = nullptr;
475 // Find existing audio stream.
476 const auto it = sync_stream_mapping_.find(sync_group);
477 if (it != sync_stream_mapping_.end()) {
478 sync_audio_stream = it->second;
479 } else {
480 // No configured audio stream, see if we can find one.
481 for (const auto& kv : audio_receive_ssrcs_) {
482 if (kv.second->config().sync_group == sync_group) {
483 if (sync_audio_stream != nullptr) {
484 LOG(LS_WARNING) << "Attempting to sync more than one audio stream "
485 "within the same sync group. This is not "
486 "supported in the current implementation.";
487 break;
488 }
489 sync_audio_stream = kv.second;
490 }
491 }
492 }
493 if (sync_audio_stream)
494 sync_stream_mapping_[sync_group] = sync_audio_stream;
495 size_t num_synced_streams = 0;
496 for (VideoReceiveStream* video_stream : video_receive_streams_) {
497 if (video_stream->config().sync_group != sync_group)
498 continue;
499 ++num_synced_streams;
500 if (num_synced_streams > 1) {
501 // TODO(pbos): Support synchronizing more than one A/V pair.
502 // https://code.google.com/p/webrtc/issues/detail?id=4762
503 LOG(LS_WARNING) << "Attempting to sync more than one audio/video pair "
504 "within the same sync group. This is not supported in "
505 "the current implementation.";
506 }
507 // Only sync the first A/V pair within this sync group.
508 if (sync_audio_stream != nullptr && num_synced_streams == 1) {
509 video_stream->SetSyncChannel(config_.voice_engine,
510 sync_audio_stream->config().voe_channel_id);
511 } else {
512 video_stream->SetSyncChannel(config_.voice_engine, -1);
513 }
514 }
515}
516
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200517PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
518 const uint8_t* packet,
519 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000520 // TODO(pbos): Figure out what channel needs it actually.
521 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000522 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
523 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000524 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200525 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000526 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200527 for (VideoReceiveStream* stream : video_receive_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700528 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000529 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700530 if (event_log_)
531 event_log_->LogRtcpPacket(true, media_type, packet, length);
532 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000533 }
534 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200535 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000536 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200537 for (VideoSendStream* stream : video_send_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700538 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000539 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700540 if (event_log_)
541 event_log_->LogRtcpPacket(false, media_type, packet, length);
542 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000543 }
544 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000545 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000546}
547
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200548PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
549 const uint8_t* packet,
stefan68786d22015-09-08 05:36:15 -0700550 size_t length,
551 const PacketTime& packet_time) {
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000552 // Minimum RTP header size.
553 if (length < 12)
554 return DELIVERY_PACKET_ERROR;
555
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +0000556 uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(&packet[8]);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000557
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000558 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200559 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
560 auto it = audio_receive_ssrcs_.find(ssrc);
561 if (it != audio_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700562 auto status = it->second->DeliverRtp(packet, length, packet_time)
563 ? DELIVERY_OK
564 : DELIVERY_PACKET_ERROR;
565 if (status == DELIVERY_OK && event_log_)
566 event_log_->LogRtpHeader(true, media_type, packet, length);
567 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200568 }
569 }
570 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
571 auto it = video_receive_ssrcs_.find(ssrc);
572 if (it != video_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700573 auto status = it->second->DeliverRtp(packet, length, packet_time)
574 ? DELIVERY_OK
575 : DELIVERY_PACKET_ERROR;
576 if (status == DELIVERY_OK && event_log_)
577 event_log_->LogRtpHeader(true, media_type, packet, length);
578 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200579 }
580 }
581 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000582}
583
stefan68786d22015-09-08 05:36:15 -0700584PacketReceiver::DeliveryStatus Call::DeliverPacket(
585 MediaType media_type,
586 const uint8_t* packet,
587 size_t length,
588 const PacketTime& packet_time) {
solenberg5a289392015-10-19 03:39:20 -0700589 // TODO(solenberg): Tests call this function on a network thread, libjingle
590 // calls on the worker thread. We should move towards always using a network
591 // thread. Then this check can be enabled.
592 // RTC_DCHECK(!configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000593 if (RtpHeaderParser::IsRtcp(packet, length))
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200594 return DeliverRtcp(media_type, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000595
stefan68786d22015-09-08 05:36:15 -0700596 return DeliverRtp(media_type, packet, length, packet_time);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000597}
598
599} // namespace internal
600} // namespace webrtc