blob: 3969bc6b517e97c2f51bcd35a64af8cc9a40654b [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"
Peter Boström45553ae2015-05-08 13:54:38 +020031#include "webrtc/system_wrappers/interface/cpu_info.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000032#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
pbos@webrtc.org32e85282015-01-15 10:09:39 +000033#include "webrtc/system_wrappers/interface/logging.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000034#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000035#include "webrtc/system_wrappers/interface/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
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000104 // Needs to be held while write-locking |receive_crit_| or |send_crit_|. This
105 // ensures that we have a consistent network state signalled to all senders
106 // and receivers.
Peter Boströmf2f82832015-05-01 13:00:41 +0200107 rtc::CriticalSection network_enabled_crit_;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000108 bool network_enabled_ GUARDED_BY(network_enabled_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000109
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000110 rtc::scoped_ptr<RWLockWrapper> receive_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700111 // Audio and Video receive streams are owned by the client that creates them.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200112 std::map<uint32_t, AudioReceiveStream*> audio_receive_ssrcs_
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000113 GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200114 std::map<uint32_t, VideoReceiveStream*> video_receive_ssrcs_
115 GUARDED_BY(receive_crit_);
116 std::set<VideoReceiveStream*> video_receive_streams_
117 GUARDED_BY(receive_crit_);
pbos8fc7fa72015-07-15 08:02:58 -0700118 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
119 GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000120
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000121 rtc::scoped_ptr<RWLockWrapper> send_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700122 // Audio and Video send streams are owned by the client that creates them.
123 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_ GUARDED_BY(send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200124 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_ GUARDED_BY(send_crit_);
125 std::set<VideoSendStream*> video_send_streams_ GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000126
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200127 VideoSendStream::RtpStateMap suspended_video_send_ssrcs_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000128
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200129 RtcEventLog* event_log_ = nullptr;
130 VoECodec* voe_codec_ = nullptr;
ivocb04965c2015-09-09 00:09:43 -0700131
henrikg3c089d72015-09-16 05:37:44 -0700132 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000133};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000134} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000135
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000136Call* Call::Create(const Call::Config& config) {
Peter Boström45553ae2015-05-08 13:54:38 +0200137 return new internal::Call(config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000138}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000139
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000140namespace internal {
141
Peter Boström45553ae2015-05-08 13:54:38 +0200142Call::Call(const Call::Config& config)
143 : num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
stefan847855b2015-09-11 09:52:15 -0700144 module_process_thread_(ProcessThread::Create("ModuleProcessThread")),
mflodmane3787022015-10-21 13:24:28 +0200145 call_stats_(new CallStats()),
mflodman0c478b32015-10-21 15:52:16 +0200146 congestion_controller_(new CongestionController(
147 module_process_thread_.get(), call_stats_.get())),
Peter Boström45553ae2015-05-08 13:54:38 +0200148 config_(config),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000149 network_enabled_(true),
150 receive_crit_(RWLockWrapper::CreateRWLock()),
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200151 send_crit_(RWLockWrapper::CreateRWLock()) {
solenberg5a289392015-10-19 03:39:20 -0700152 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700153 RTC_DCHECK_GE(config.bitrate_config.min_bitrate_bps, 0);
154 RTC_DCHECK_GE(config.bitrate_config.start_bitrate_bps,
155 config.bitrate_config.min_bitrate_bps);
Stefan Holmere5904162015-03-26 11:11:06 +0100156 if (config.bitrate_config.max_bitrate_bps != -1) {
henrikg91d6ede2015-09-17 00:24:34 -0700157 RTC_DCHECK_GE(config.bitrate_config.max_bitrate_bps,
158 config.bitrate_config.start_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000159 }
ivocb04965c2015-09-09 00:09:43 -0700160 if (config.voice_engine) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200161 // Keep a reference to VoECodec, so we're sure the VoiceEngine lives for the
162 // duration of the call.
163 voe_codec_ = VoECodec::GetInterface(config.voice_engine);
164 if (voe_codec_)
165 event_log_ = voe_codec_->GetEventLog();
ivocb04965c2015-09-09 00:09:43 -0700166 }
pbos@webrtc.org00873182014-11-25 14:03:34 +0000167
Peter Boström45553ae2015-05-08 13:54:38 +0200168 Trace::CreateTrace();
169 module_process_thread_->Start();
mflodmane3787022015-10-21 13:24:28 +0200170 module_process_thread_->RegisterModule(call_stats_.get());
Peter Boström45553ae2015-05-08 13:54:38 +0200171
mflodman0c478b32015-10-21 15:52:16 +0200172 congestion_controller_->SetBweBitrates(
173 config_.bitrate_config.min_bitrate_bps,
174 config_.bitrate_config.start_bitrate_bps,
175 config_.bitrate_config.max_bitrate_bps);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000176}
177
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000178Call::~Call() {
solenberg5a289392015-10-19 03:39:20 -0700179 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700180 RTC_CHECK(audio_send_ssrcs_.empty());
181 RTC_CHECK(video_send_ssrcs_.empty());
182 RTC_CHECK(video_send_streams_.empty());
183 RTC_CHECK(audio_receive_ssrcs_.empty());
184 RTC_CHECK(video_receive_ssrcs_.empty());
185 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000186
mflodmane3787022015-10-21 13:24:28 +0200187 module_process_thread_->DeRegisterModule(call_stats_.get());
Peter Boström45553ae2015-05-08 13:54:38 +0200188 module_process_thread_->Stop();
189 Trace::ReturnTrace();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200190
191 if (voe_codec_)
192 voe_codec_->Release();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000193}
194
solenberg5a289392015-10-19 03:39:20 -0700195PacketReceiver* Call::Receiver() {
196 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
197 // thread. Re-enable once that is fixed.
198 // RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
199 return this;
200}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000201
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200202webrtc::AudioSendStream* Call::CreateAudioSendStream(
203 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700204 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
solenberg5a289392015-10-19 03:39:20 -0700205 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700206 AudioSendStream* send_stream = new AudioSendStream(config);
207 {
208 rtc::CritScope lock(&network_enabled_crit_);
209 WriteLockScoped write_lock(*send_crit_);
210 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
211 audio_send_ssrcs_.end());
212 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
213
214 if (!network_enabled_)
215 send_stream->SignalNetworkState(kNetworkDown);
216 }
217 return send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200218}
219
220void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 14:35:07 -0700221 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
solenberg5a289392015-10-19 03:39:20 -0700222 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700223 RTC_DCHECK(send_stream != nullptr);
224
225 send_stream->Stop();
226
227 webrtc::internal::AudioSendStream* audio_send_stream =
228 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
229 {
230 WriteLockScoped write_lock(*send_crit_);
231 size_t num_deleted = audio_send_ssrcs_.erase(
232 audio_send_stream->config().rtp.ssrc);
233 RTC_DCHECK(num_deleted == 1);
234 }
235 delete audio_send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200236}
237
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200238webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
239 const webrtc::AudioReceiveStream::Config& config) {
240 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700241 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200242 AudioReceiveStream* receive_stream = new AudioReceiveStream(
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200243 congestion_controller_->GetRemoteBitrateEstimator(false), config,
244 config_.voice_engine);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200245 {
246 WriteLockScoped write_lock(*receive_crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700247 RTC_DCHECK(audio_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
248 audio_receive_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200249 audio_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos8fc7fa72015-07-15 08:02:58 -0700250 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200251 }
252 return receive_stream;
253}
254
255void Call::DestroyAudioReceiveStream(
256 webrtc::AudioReceiveStream* receive_stream) {
257 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700258 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700259 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700260 webrtc::internal::AudioReceiveStream* audio_receive_stream =
261 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200262 {
263 WriteLockScoped write_lock(*receive_crit_);
264 size_t num_deleted = audio_receive_ssrcs_.erase(
265 audio_receive_stream->config().rtp.remote_ssrc);
henrikg91d6ede2015-09-17 00:24:34 -0700266 RTC_DCHECK(num_deleted == 1);
pbos8fc7fa72015-07-15 08:02:58 -0700267 const std::string& sync_group = audio_receive_stream->config().sync_group;
268 const auto it = sync_stream_mapping_.find(sync_group);
269 if (it != sync_stream_mapping_.end() &&
270 it->second == audio_receive_stream) {
271 sync_stream_mapping_.erase(it);
272 ConfigureSync(sync_group);
273 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200274 }
275 delete audio_receive_stream;
276}
277
278webrtc::VideoSendStream* Call::CreateVideoSendStream(
279 const webrtc::VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000280 const VideoEncoderConfig& encoder_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000281 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
solenberg5a289392015-10-19 03:39:20 -0700282 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000283
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000284 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
285 // the call has already started.
mflodman0c478b32015-10-21 15:52:16 +0200286 VideoSendStream* send_stream = new VideoSendStream(
287 num_cpu_cores_, module_process_thread_.get(), call_stats_.get(),
288 congestion_controller_.get(), config, encoder_config,
289 suspended_video_send_ssrcs_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000290
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000291 // This needs to be taken before send_crit_ as both locks need to be held
292 // while changing network state.
Peter Boströmf2f82832015-05-01 13:00:41 +0200293 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000294 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200295 for (uint32_t ssrc : config.rtp.ssrcs) {
henrikg91d6ede2015-09-17 00:24:34 -0700296 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200297 video_send_ssrcs_[ssrc] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000298 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200299 video_send_streams_.insert(send_stream);
300
ivocb04965c2015-09-09 00:09:43 -0700301 if (event_log_)
302 event_log_->LogVideoSendStreamConfig(config);
303
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000304 if (!network_enabled_)
305 send_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000306 return send_stream;
307}
308
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000309void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000310 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700311 RTC_DCHECK(send_stream != nullptr);
solenberg5a289392015-10-19 03:39:20 -0700312 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000313
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000314 send_stream->Stop();
315
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000316 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000317 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000318 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200319 auto it = video_send_ssrcs_.begin();
320 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000321 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
322 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200323 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000324 } else {
325 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000326 }
327 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200328 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000329 }
henrikg91d6ede2015-09-17 00:24:34 -0700330 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000331
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000332 VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates();
333
334 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
335 it != rtp_state.end();
336 ++it) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200337 suspended_video_send_ssrcs_[it->first] = it->second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000338 }
339
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000340 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000341}
342
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200343webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
344 const webrtc::VideoReceiveStream::Config& config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000345 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700346 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
Peter Boströmc4188fd2015-04-24 15:16:03 +0200347 VideoReceiveStream* receive_stream = new VideoReceiveStream(
mflodman0c478b32015-10-21 15:52:16 +0200348 num_cpu_cores_, congestion_controller_.get(), config,
349 config_.voice_engine, module_process_thread_.get(), call_stats_.get());
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000350
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000351 // This needs to be taken before receive_crit_ as both locks need to be held
352 // while changing network state.
Peter Boströmf2f82832015-05-01 13:00:41 +0200353 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000354 WriteLockScoped write_lock(*receive_crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700355 RTC_DCHECK(video_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
356 video_receive_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200357 video_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000358 // TODO(pbos): Configure different RTX payloads per receive payload.
359 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
360 config.rtp.rtx.begin();
361 if (it != config.rtp.rtx.end())
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200362 video_receive_ssrcs_[it->second.ssrc] = receive_stream;
363 video_receive_streams_.insert(receive_stream);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000364
pbos8fc7fa72015-07-15 08:02:58 -0700365 ConfigureSync(config.sync_group);
366
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000367 if (!network_enabled_)
368 receive_stream->SignalNetworkState(kNetworkDown);
pbos8fc7fa72015-07-15 08:02:58 -0700369
ivocb04965c2015-09-09 00:09:43 -0700370 if (event_log_)
371 event_log_->LogVideoReceiveStreamConfig(config);
372
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000373 return receive_stream;
374}
375
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000376void Call::DestroyVideoReceiveStream(
377 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000378 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700379 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700380 RTC_DCHECK(receive_stream != nullptr);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000381 VideoReceiveStream* receive_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000382 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000383 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000384 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
385 // separate SSRC there can be either one or two.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200386 auto it = video_receive_ssrcs_.begin();
387 while (it != video_receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000388 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000389 if (receive_stream_impl != nullptr)
henrikg91d6ede2015-09-17 00:24:34 -0700390 RTC_DCHECK(receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000391 receive_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200392 video_receive_ssrcs_.erase(it++);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000393 } else {
394 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000395 }
396 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200397 video_receive_streams_.erase(receive_stream_impl);
henrikg91d6ede2015-09-17 00:24:34 -0700398 RTC_CHECK(receive_stream_impl != nullptr);
pbos8fc7fa72015-07-15 08:02:58 -0700399 ConfigureSync(receive_stream_impl->config().sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000400 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000401 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000402}
403
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000404Call::Stats Call::GetStats() const {
solenberg5a289392015-10-19 03:39:20 -0700405 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
406 // thread. Re-enable once that is fixed.
407 // RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000408 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +0200409 // Fetch available send/receive bitrates.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000410 uint32_t send_bandwidth = 0;
mflodman0c478b32015-10-21 15:52:16 +0200411 congestion_controller_->GetBitrateController()->AvailableBandwidth(
412 &send_bandwidth);
Peter Boström45553ae2015-05-08 13:54:38 +0200413 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000414 uint32_t recv_bandwidth = 0;
mflodman0c478b32015-10-21 15:52:16 +0200415 congestion_controller_->GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-18 22:08:19 -0700416 &ssrcs, &recv_bandwidth);
Peter Boström45553ae2015-05-08 13:54:38 +0200417 stats.send_bandwidth_bps = send_bandwidth;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000418 stats.recv_bandwidth_bps = recv_bandwidth;
mflodman0c478b32015-10-21 15:52:16 +0200419 stats.pacer_delay_ms = congestion_controller_->GetPacerQueuingDelayMs();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000420 {
421 ReadLockScoped read_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -0700422 // TODO(solenberg): Add audio send streams.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200423 for (const auto& kv : video_send_ssrcs_) {
424 int rtt_ms = kv.second->GetRtt();
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000425 if (rtt_ms > 0)
426 stats.rtt_ms = rtt_ms;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000427 }
428 }
429 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000430}
431
pbos@webrtc.org00873182014-11-25 14:03:34 +0000432void Call::SetBitrateConfig(
433 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000434 TRACE_EVENT0("webrtc", "Call::SetBitrateConfig");
solenberg5a289392015-10-19 03:39:20 -0700435 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700436 RTC_DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000437 if (bitrate_config.max_bitrate_bps != -1)
henrikg91d6ede2015-09-17 00:24:34 -0700438 RTC_DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
Stefan Holmere5904162015-03-26 11:11:06 +0100439 if (config_.bitrate_config.min_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000440 bitrate_config.min_bitrate_bps &&
441 (bitrate_config.start_bitrate_bps <= 0 ||
Stefan Holmere5904162015-03-26 11:11:06 +0100442 config_.bitrate_config.start_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000443 bitrate_config.start_bitrate_bps) &&
Stefan Holmere5904162015-03-26 11:11:06 +0100444 config_.bitrate_config.max_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000445 bitrate_config.max_bitrate_bps) {
446 // Nothing new to set, early abort to avoid encoder reconfigurations.
447 return;
448 }
Stefan Holmere5904162015-03-26 11:11:06 +0100449 config_.bitrate_config = bitrate_config;
mflodman0c478b32015-10-21 15:52:16 +0200450 congestion_controller_->SetBweBitrates(bitrate_config.min_bitrate_bps,
451 bitrate_config.start_bitrate_bps,
452 bitrate_config.max_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000453}
454
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000455void Call::SignalNetworkState(NetworkState state) {
solenberg5a289392015-10-19 03:39:20 -0700456 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000457 // Take crit for entire function, it needs to be held while updating streams
458 // to guarantee a consistent state across streams.
Peter Boströmf2f82832015-05-01 13:00:41 +0200459 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000460 network_enabled_ = state == kNetworkUp;
mflodman0c478b32015-10-21 15:52:16 +0200461 congestion_controller_->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000462 {
463 ReadLockScoped write_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -0700464 for (auto& kv : audio_send_ssrcs_) {
465 kv.second->SignalNetworkState(state);
466 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200467 for (auto& kv : video_send_ssrcs_) {
468 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000469 }
470 }
471 {
472 ReadLockScoped write_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200473 for (auto& kv : video_receive_ssrcs_) {
474 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000475 }
476 }
477}
478
stefanc1aeaf02015-10-15 07:26:07 -0700479void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
solenberg5a289392015-10-19 03:39:20 -0700480 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
mflodman0c478b32015-10-21 15:52:16 +0200481 congestion_controller_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -0700482}
483
pbos8fc7fa72015-07-15 08:02:58 -0700484void Call::ConfigureSync(const std::string& sync_group) {
485 // Set sync only if there was no previous one.
486 if (config_.voice_engine == nullptr || sync_group.empty())
487 return;
488
489 AudioReceiveStream* sync_audio_stream = nullptr;
490 // Find existing audio stream.
491 const auto it = sync_stream_mapping_.find(sync_group);
492 if (it != sync_stream_mapping_.end()) {
493 sync_audio_stream = it->second;
494 } else {
495 // No configured audio stream, see if we can find one.
496 for (const auto& kv : audio_receive_ssrcs_) {
497 if (kv.second->config().sync_group == sync_group) {
498 if (sync_audio_stream != nullptr) {
499 LOG(LS_WARNING) << "Attempting to sync more than one audio stream "
500 "within the same sync group. This is not "
501 "supported in the current implementation.";
502 break;
503 }
504 sync_audio_stream = kv.second;
505 }
506 }
507 }
508 if (sync_audio_stream)
509 sync_stream_mapping_[sync_group] = sync_audio_stream;
510 size_t num_synced_streams = 0;
511 for (VideoReceiveStream* video_stream : video_receive_streams_) {
512 if (video_stream->config().sync_group != sync_group)
513 continue;
514 ++num_synced_streams;
515 if (num_synced_streams > 1) {
516 // TODO(pbos): Support synchronizing more than one A/V pair.
517 // https://code.google.com/p/webrtc/issues/detail?id=4762
518 LOG(LS_WARNING) << "Attempting to sync more than one audio/video pair "
519 "within the same sync group. This is not supported in "
520 "the current implementation.";
521 }
522 // Only sync the first A/V pair within this sync group.
523 if (sync_audio_stream != nullptr && num_synced_streams == 1) {
524 video_stream->SetSyncChannel(config_.voice_engine,
525 sync_audio_stream->config().voe_channel_id);
526 } else {
527 video_stream->SetSyncChannel(config_.voice_engine, -1);
528 }
529 }
530}
531
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200532PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
533 const uint8_t* packet,
534 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000535 // TODO(pbos): Figure out what channel needs it actually.
536 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000537 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
538 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000539 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200540 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000541 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200542 for (VideoReceiveStream* stream : video_receive_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700543 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000544 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700545 if (event_log_)
546 event_log_->LogRtcpPacket(true, media_type, packet, length);
547 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000548 }
549 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200550 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000551 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200552 for (VideoSendStream* stream : video_send_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700553 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000554 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700555 if (event_log_)
556 event_log_->LogRtcpPacket(false, media_type, packet, length);
557 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000558 }
559 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000560 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000561}
562
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200563PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
564 const uint8_t* packet,
stefan68786d22015-09-08 05:36:15 -0700565 size_t length,
566 const PacketTime& packet_time) {
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000567 // Minimum RTP header size.
568 if (length < 12)
569 return DELIVERY_PACKET_ERROR;
570
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +0000571 uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(&packet[8]);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000572
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000573 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200574 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
575 auto it = audio_receive_ssrcs_.find(ssrc);
576 if (it != audio_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700577 auto status = it->second->DeliverRtp(packet, length, packet_time)
578 ? DELIVERY_OK
579 : DELIVERY_PACKET_ERROR;
580 if (status == DELIVERY_OK && event_log_)
581 event_log_->LogRtpHeader(true, media_type, packet, length);
582 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200583 }
584 }
585 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
586 auto it = video_receive_ssrcs_.find(ssrc);
587 if (it != video_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700588 auto status = it->second->DeliverRtp(packet, length, packet_time)
589 ? DELIVERY_OK
590 : DELIVERY_PACKET_ERROR;
591 if (status == DELIVERY_OK && event_log_)
592 event_log_->LogRtpHeader(true, media_type, packet, length);
593 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200594 }
595 }
596 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000597}
598
stefan68786d22015-09-08 05:36:15 -0700599PacketReceiver::DeliveryStatus Call::DeliverPacket(
600 MediaType media_type,
601 const uint8_t* packet,
602 size_t length,
603 const PacketTime& packet_time) {
solenberg5a289392015-10-19 03:39:20 -0700604 // TODO(solenberg): Tests call this function on a network thread, libjingle
605 // calls on the worker thread. We should move towards always using a network
606 // thread. Then this check can be enabled.
607 // RTC_DCHECK(!configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000608 if (RtpHeaderParser::IsRtcp(packet, length))
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200609 return DeliverRtcp(media_type, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000610
stefan68786d22015-09-08 05:36:15 -0700611 return DeliverRtp(media_type, packet, length, packet_time);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000612}
613
614} // namespace internal
615} // namespace webrtc