blob: a69be98bbb85dc1b79e4d3e12e8af9ddc99bbe88 [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"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000022#include "webrtc/call.h"
Peter Boström5c389d32015-09-25 13:58:30 +020023#include "webrtc/call/rtc_event_log.h"
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000024#include "webrtc/common.h"
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +000025#include "webrtc/config.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000026#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +000027#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
Peter Boström45553ae2015-05-08 13:54:38 +020028#include "webrtc/modules/utility/interface/process_thread.h"
Peter Boström45553ae2015-05-08 13:54:38 +020029#include "webrtc/system_wrappers/interface/cpu_info.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000030#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
pbos@webrtc.org32e85282015-01-15 10:09:39 +000031#include "webrtc/system_wrappers/interface/logging.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000032#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000033#include "webrtc/system_wrappers/interface/trace.h"
pbos@webrtc.org50fe3592015-01-29 12:33:07 +000034#include "webrtc/system_wrappers/interface/trace_event.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000035#include "webrtc/video/video_receive_stream.h"
36#include "webrtc/video/video_send_stream.h"
ivocb04965c2015-09-09 00:09:43 -070037#include "webrtc/voice_engine/include/voe_codec.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000038
39namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000040
pbos@webrtc.orga73a6782014-10-14 11:52:10 +000041const int Call::Config::kDefaultStartBitrateBps = 300000;
42
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000043namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000044
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000045class Call : public webrtc::Call, public PacketReceiver {
46 public:
Peter Boström45553ae2015-05-08 13:54:38 +020047 explicit Call(const Call::Config& config);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000048 virtual ~Call();
49
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000050 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000051
Fredrik Solenberg04f49312015-06-08 13:04:56 +020052 webrtc::AudioSendStream* CreateAudioSendStream(
53 const webrtc::AudioSendStream::Config& config) override;
54 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
55
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020056 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
57 const webrtc::AudioReceiveStream::Config& config) override;
58 void DestroyAudioReceiveStream(
59 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000060
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020061 webrtc::VideoSendStream* CreateVideoSendStream(
62 const webrtc::VideoSendStream::Config& config,
63 const VideoEncoderConfig& encoder_config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000064 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000065
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020066 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
67 const webrtc::VideoReceiveStream::Config& config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000068 void DestroyVideoReceiveStream(
69 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000070
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000071 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000072
stefan68786d22015-09-08 05:36:15 -070073 DeliveryStatus DeliverPacket(MediaType media_type,
74 const uint8_t* packet,
75 size_t length,
76 const PacketTime& packet_time) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000077
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000078 void SetBitrateConfig(
79 const webrtc::Call::Config::BitrateConfig& bitrate_config) override;
80 void SignalNetworkState(NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +000081
stefanc1aeaf02015-10-15 07:26:07 -070082 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
83
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000084 private:
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020085 DeliveryStatus DeliverRtcp(MediaType media_type, const uint8_t* packet,
86 size_t length);
stefan68786d22015-09-08 05:36:15 -070087 DeliveryStatus DeliverRtp(MediaType media_type,
88 const uint8_t* packet,
89 size_t length,
90 const PacketTime& packet_time);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000091
pbos8fc7fa72015-07-15 08:02:58 -070092 void ConfigureSync(const std::string& sync_group)
93 EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
94
Peter Boström45553ae2015-05-08 13:54:38 +020095 const int num_cpu_cores_;
96 const rtc::scoped_ptr<ProcessThread> module_process_thread_;
97 const rtc::scoped_ptr<ChannelGroup> channel_group_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000098 Call::Config config_;
solenberg5a289392015-10-19 03:39:20 -070099 rtc::ThreadChecker configuration_thread_checker_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000100
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000101 // Needs to be held while write-locking |receive_crit_| or |send_crit_|. This
102 // ensures that we have a consistent network state signalled to all senders
103 // and receivers.
Peter Boströmf2f82832015-05-01 13:00:41 +0200104 rtc::CriticalSection network_enabled_crit_;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000105 bool network_enabled_ GUARDED_BY(network_enabled_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000106
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000107 rtc::scoped_ptr<RWLockWrapper> receive_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700108 // Audio and Video receive streams are owned by the client that creates them.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200109 std::map<uint32_t, AudioReceiveStream*> audio_receive_ssrcs_
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000110 GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200111 std::map<uint32_t, VideoReceiveStream*> video_receive_ssrcs_
112 GUARDED_BY(receive_crit_);
113 std::set<VideoReceiveStream*> video_receive_streams_
114 GUARDED_BY(receive_crit_);
pbos8fc7fa72015-07-15 08:02:58 -0700115 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
116 GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000117
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000118 rtc::scoped_ptr<RWLockWrapper> send_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700119 // Audio and Video send streams are owned by the client that creates them.
120 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_ GUARDED_BY(send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200121 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_ GUARDED_BY(send_crit_);
122 std::set<VideoSendStream*> video_send_streams_ GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000123
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200124 VideoSendStream::RtpStateMap suspended_video_send_ssrcs_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000125
Fredrik Solenberga4577522015-10-20 15:01:35 +0200126 RtcEventLog* event_log_ = nullptr;
127 VoECodec* voe_codec_ = nullptr;
ivocb04965c2015-09-09 00:09:43 -0700128
henrikg3c089d72015-09-16 05:37:44 -0700129 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000130};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000131} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000132
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000133Call* Call::Create(const Call::Config& config) {
Peter Boström45553ae2015-05-08 13:54:38 +0200134 return new internal::Call(config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000135}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000136
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000137namespace internal {
138
Peter Boström45553ae2015-05-08 13:54:38 +0200139Call::Call(const Call::Config& config)
140 : num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
stefan847855b2015-09-11 09:52:15 -0700141 module_process_thread_(ProcessThread::Create("ModuleProcessThread")),
Peter Boström2251d6e2015-05-28 14:10:39 +0200142 channel_group_(new ChannelGroup(module_process_thread_.get())),
Peter Boström45553ae2015-05-08 13:54:38 +0200143 config_(config),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000144 network_enabled_(true),
145 receive_crit_(RWLockWrapper::CreateRWLock()),
Fredrik Solenberga4577522015-10-20 15:01:35 +0200146 send_crit_(RWLockWrapper::CreateRWLock()) {
solenberg5a289392015-10-19 03:39:20 -0700147 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
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 Solenberga4577522015-10-20 15:01:35 +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();
165
stefan4fbd1452015-09-28 03:57:14 -0700166 channel_group_->SetBweBitrates(config_.bitrate_config.min_bitrate_bps,
167 config_.bitrate_config.start_bitrate_bps,
168 config_.bitrate_config.max_bitrate_bps);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000169}
170
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000171Call::~Call() {
solenberg5a289392015-10-19 03:39:20 -0700172 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700173 RTC_CHECK(audio_send_ssrcs_.empty());
174 RTC_CHECK(video_send_ssrcs_.empty());
175 RTC_CHECK(video_send_streams_.empty());
176 RTC_CHECK(audio_receive_ssrcs_.empty());
177 RTC_CHECK(video_receive_ssrcs_.empty());
178 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000179
Peter Boström45553ae2015-05-08 13:54:38 +0200180 module_process_thread_->Stop();
181 Trace::ReturnTrace();
Fredrik Solenberga4577522015-10-20 15:01:35 +0200182
183 if (voe_codec_)
184 voe_codec_->Release();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000185}
186
solenberg5a289392015-10-19 03:39:20 -0700187PacketReceiver* Call::Receiver() {
188 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
189 // thread. Re-enable once that is fixed.
190 // RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
191 return this;
192}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000193
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200194webrtc::AudioSendStream* Call::CreateAudioSendStream(
195 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700196 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
solenberg5a289392015-10-19 03:39:20 -0700197 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700198 AudioSendStream* send_stream = new AudioSendStream(config);
199 {
200 rtc::CritScope lock(&network_enabled_crit_);
201 WriteLockScoped write_lock(*send_crit_);
202 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
203 audio_send_ssrcs_.end());
204 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
205
206 if (!network_enabled_)
207 send_stream->SignalNetworkState(kNetworkDown);
208 }
209 return send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200210}
211
212void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 14:35:07 -0700213 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
solenberg5a289392015-10-19 03:39:20 -0700214 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700215 RTC_DCHECK(send_stream != nullptr);
216
217 send_stream->Stop();
218
219 webrtc::internal::AudioSendStream* audio_send_stream =
220 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
221 {
222 WriteLockScoped write_lock(*send_crit_);
223 size_t num_deleted = audio_send_ssrcs_.erase(
224 audio_send_stream->config().rtp.ssrc);
225 RTC_DCHECK(num_deleted == 1);
226 }
227 delete audio_send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200228}
229
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200230webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
231 const webrtc::AudioReceiveStream::Config& config) {
232 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700233 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200234 AudioReceiveStream* receive_stream = new AudioReceiveStream(
Fredrik Solenberga4577522015-10-20 15:01:35 +0200235 channel_group_->GetRemoteBitrateEstimator(false), config,
236 config_.voice_engine);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200237 {
238 WriteLockScoped write_lock(*receive_crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700239 RTC_DCHECK(audio_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
240 audio_receive_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200241 audio_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos8fc7fa72015-07-15 08:02:58 -0700242 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200243 }
244 return receive_stream;
245}
246
247void Call::DestroyAudioReceiveStream(
248 webrtc::AudioReceiveStream* receive_stream) {
249 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700250 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700251 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700252 webrtc::internal::AudioReceiveStream* audio_receive_stream =
253 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200254 {
255 WriteLockScoped write_lock(*receive_crit_);
256 size_t num_deleted = audio_receive_ssrcs_.erase(
257 audio_receive_stream->config().rtp.remote_ssrc);
henrikg91d6ede2015-09-17 00:24:34 -0700258 RTC_DCHECK(num_deleted == 1);
pbos8fc7fa72015-07-15 08:02:58 -0700259 const std::string& sync_group = audio_receive_stream->config().sync_group;
260 const auto it = sync_stream_mapping_.find(sync_group);
261 if (it != sync_stream_mapping_.end() &&
262 it->second == audio_receive_stream) {
263 sync_stream_mapping_.erase(it);
264 ConfigureSync(sync_group);
265 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200266 }
267 delete audio_receive_stream;
268}
269
270webrtc::VideoSendStream* Call::CreateVideoSendStream(
271 const webrtc::VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000272 const VideoEncoderConfig& encoder_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000273 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
solenberg5a289392015-10-19 03:39:20 -0700274 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000275
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000276 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
277 // the call has already started.
solenberge5269742015-09-08 05:13:22 -0700278 VideoSendStream* send_stream = new VideoSendStream(num_cpu_cores_,
mflodman0dbf0092015-10-19 08:12:12 -0700279 module_process_thread_.get(), channel_group_.get(), config,
280 encoder_config, suspended_video_send_ssrcs_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000281
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000282 // This needs to be taken before send_crit_ as both locks need to be held
283 // while changing network state.
Peter Boströmf2f82832015-05-01 13:00:41 +0200284 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000285 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200286 for (uint32_t ssrc : config.rtp.ssrcs) {
henrikg91d6ede2015-09-17 00:24:34 -0700287 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200288 video_send_ssrcs_[ssrc] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000289 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200290 video_send_streams_.insert(send_stream);
291
ivocb04965c2015-09-09 00:09:43 -0700292 if (event_log_)
293 event_log_->LogVideoSendStreamConfig(config);
294
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000295 if (!network_enabled_)
296 send_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000297 return send_stream;
298}
299
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000300void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000301 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700302 RTC_DCHECK(send_stream != nullptr);
solenberg5a289392015-10-19 03:39:20 -0700303 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000304
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000305 send_stream->Stop();
306
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000307 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000308 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000309 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200310 auto it = video_send_ssrcs_.begin();
311 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000312 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
313 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200314 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000315 } else {
316 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000317 }
318 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200319 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000320 }
henrikg91d6ede2015-09-17 00:24:34 -0700321 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000322
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000323 VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates();
324
325 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
326 it != rtp_state.end();
327 ++it) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200328 suspended_video_send_ssrcs_[it->first] = it->second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000329 }
330
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000331 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000332}
333
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200334webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
335 const webrtc::VideoReceiveStream::Config& config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000336 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700337 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
Peter Boströmc4188fd2015-04-24 15:16:03 +0200338 VideoReceiveStream* receive_stream = new VideoReceiveStream(
mflodman0dbf0092015-10-19 08:12:12 -0700339 num_cpu_cores_, channel_group_.get(), config, config_.voice_engine,
340 module_process_thread_.get());
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000341
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000342 // This needs to be taken before receive_crit_ as both locks need to be held
343 // while changing network state.
Peter Boströmf2f82832015-05-01 13:00:41 +0200344 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000345 WriteLockScoped write_lock(*receive_crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700346 RTC_DCHECK(video_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
347 video_receive_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200348 video_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000349 // TODO(pbos): Configure different RTX payloads per receive payload.
350 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
351 config.rtp.rtx.begin();
352 if (it != config.rtp.rtx.end())
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200353 video_receive_ssrcs_[it->second.ssrc] = receive_stream;
354 video_receive_streams_.insert(receive_stream);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000355
pbos8fc7fa72015-07-15 08:02:58 -0700356 ConfigureSync(config.sync_group);
357
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000358 if (!network_enabled_)
359 receive_stream->SignalNetworkState(kNetworkDown);
pbos8fc7fa72015-07-15 08:02:58 -0700360
ivocb04965c2015-09-09 00:09:43 -0700361 if (event_log_)
362 event_log_->LogVideoReceiveStreamConfig(config);
363
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000364 return receive_stream;
365}
366
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000367void Call::DestroyVideoReceiveStream(
368 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000369 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700370 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700371 RTC_DCHECK(receive_stream != nullptr);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000372 VideoReceiveStream* receive_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000373 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000374 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000375 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
376 // separate SSRC there can be either one or two.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200377 auto it = video_receive_ssrcs_.begin();
378 while (it != video_receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000379 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000380 if (receive_stream_impl != nullptr)
henrikg91d6ede2015-09-17 00:24:34 -0700381 RTC_DCHECK(receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000382 receive_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200383 video_receive_ssrcs_.erase(it++);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000384 } else {
385 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000386 }
387 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200388 video_receive_streams_.erase(receive_stream_impl);
henrikg91d6ede2015-09-17 00:24:34 -0700389 RTC_CHECK(receive_stream_impl != nullptr);
pbos8fc7fa72015-07-15 08:02:58 -0700390 ConfigureSync(receive_stream_impl->config().sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000391 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000392 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000393}
394
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000395Call::Stats Call::GetStats() const {
solenberg5a289392015-10-19 03:39:20 -0700396 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
397 // thread. Re-enable once that is fixed.
398 // RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000399 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +0200400 // Fetch available send/receive bitrates.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000401 uint32_t send_bandwidth = 0;
Peter Boström45553ae2015-05-08 13:54:38 +0200402 channel_group_->GetBitrateController()->AvailableBandwidth(&send_bandwidth);
403 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000404 uint32_t recv_bandwidth = 0;
mflodmana20de202015-10-18 22:08:19 -0700405 channel_group_->GetRemoteBitrateEstimator(false)->LatestEstimate(
406 &ssrcs, &recv_bandwidth);
Peter Boström45553ae2015-05-08 13:54:38 +0200407 stats.send_bandwidth_bps = send_bandwidth;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000408 stats.recv_bandwidth_bps = recv_bandwidth;
Peter Boström59d91dc2015-04-27 17:24:33 +0200409 stats.pacer_delay_ms = channel_group_->GetPacerQueuingDelayMs();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000410 {
411 ReadLockScoped read_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -0700412 // TODO(solenberg): Add audio send streams.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200413 for (const auto& kv : video_send_ssrcs_) {
414 int rtt_ms = kv.second->GetRtt();
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000415 if (rtt_ms > 0)
416 stats.rtt_ms = rtt_ms;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000417 }
418 }
419 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000420}
421
pbos@webrtc.org00873182014-11-25 14:03:34 +0000422void Call::SetBitrateConfig(
423 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000424 TRACE_EVENT0("webrtc", "Call::SetBitrateConfig");
solenberg5a289392015-10-19 03:39:20 -0700425 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700426 RTC_DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000427 if (bitrate_config.max_bitrate_bps != -1)
henrikg91d6ede2015-09-17 00:24:34 -0700428 RTC_DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
Stefan Holmere5904162015-03-26 11:11:06 +0100429 if (config_.bitrate_config.min_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000430 bitrate_config.min_bitrate_bps &&
431 (bitrate_config.start_bitrate_bps <= 0 ||
Stefan Holmere5904162015-03-26 11:11:06 +0100432 config_.bitrate_config.start_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000433 bitrate_config.start_bitrate_bps) &&
Stefan Holmere5904162015-03-26 11:11:06 +0100434 config_.bitrate_config.max_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000435 bitrate_config.max_bitrate_bps) {
436 // Nothing new to set, early abort to avoid encoder reconfigurations.
437 return;
438 }
Stefan Holmere5904162015-03-26 11:11:06 +0100439 config_.bitrate_config = bitrate_config;
stefan4fbd1452015-09-28 03:57:14 -0700440 channel_group_->SetBweBitrates(bitrate_config.min_bitrate_bps,
441 bitrate_config.start_bitrate_bps,
442 bitrate_config.max_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000443}
444
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000445void Call::SignalNetworkState(NetworkState state) {
solenberg5a289392015-10-19 03:39:20 -0700446 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000447 // Take crit for entire function, it needs to be held while updating streams
448 // to guarantee a consistent state across streams.
Peter Boströmf2f82832015-05-01 13:00:41 +0200449 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000450 network_enabled_ = state == kNetworkUp;
stefan457a61d2015-10-14 03:12:59 -0700451 channel_group_->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000452 {
453 ReadLockScoped write_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -0700454 for (auto& kv : audio_send_ssrcs_) {
455 kv.second->SignalNetworkState(state);
456 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200457 for (auto& kv : video_send_ssrcs_) {
458 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000459 }
460 }
461 {
462 ReadLockScoped write_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200463 for (auto& kv : video_receive_ssrcs_) {
464 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000465 }
466 }
467}
468
stefanc1aeaf02015-10-15 07:26:07 -0700469void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
solenberg5a289392015-10-19 03:39:20 -0700470 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
stefanc1aeaf02015-10-15 07:26:07 -0700471 channel_group_->OnSentPacket(sent_packet);
472}
473
pbos8fc7fa72015-07-15 08:02:58 -0700474void Call::ConfigureSync(const std::string& sync_group) {
475 // Set sync only if there was no previous one.
476 if (config_.voice_engine == nullptr || sync_group.empty())
477 return;
478
479 AudioReceiveStream* sync_audio_stream = nullptr;
480 // Find existing audio stream.
481 const auto it = sync_stream_mapping_.find(sync_group);
482 if (it != sync_stream_mapping_.end()) {
483 sync_audio_stream = it->second;
484 } else {
485 // No configured audio stream, see if we can find one.
486 for (const auto& kv : audio_receive_ssrcs_) {
487 if (kv.second->config().sync_group == sync_group) {
488 if (sync_audio_stream != nullptr) {
489 LOG(LS_WARNING) << "Attempting to sync more than one audio stream "
490 "within the same sync group. This is not "
491 "supported in the current implementation.";
492 break;
493 }
494 sync_audio_stream = kv.second;
495 }
496 }
497 }
498 if (sync_audio_stream)
499 sync_stream_mapping_[sync_group] = sync_audio_stream;
500 size_t num_synced_streams = 0;
501 for (VideoReceiveStream* video_stream : video_receive_streams_) {
502 if (video_stream->config().sync_group != sync_group)
503 continue;
504 ++num_synced_streams;
505 if (num_synced_streams > 1) {
506 // TODO(pbos): Support synchronizing more than one A/V pair.
507 // https://code.google.com/p/webrtc/issues/detail?id=4762
508 LOG(LS_WARNING) << "Attempting to sync more than one audio/video pair "
509 "within the same sync group. This is not supported in "
510 "the current implementation.";
511 }
512 // Only sync the first A/V pair within this sync group.
513 if (sync_audio_stream != nullptr && num_synced_streams == 1) {
514 video_stream->SetSyncChannel(config_.voice_engine,
515 sync_audio_stream->config().voe_channel_id);
516 } else {
517 video_stream->SetSyncChannel(config_.voice_engine, -1);
518 }
519 }
520}
521
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200522PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
523 const uint8_t* packet,
524 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000525 // TODO(pbos): Figure out what channel needs it actually.
526 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000527 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
528 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000529 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200530 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000531 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200532 for (VideoReceiveStream* stream : video_receive_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700533 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000534 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700535 if (event_log_)
536 event_log_->LogRtcpPacket(true, media_type, packet, length);
537 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000538 }
539 }
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(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200542 for (VideoSendStream* stream : video_send_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(false, media_type, packet, length);
547 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000548 }
549 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000550 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000551}
552
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200553PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
554 const uint8_t* packet,
stefan68786d22015-09-08 05:36:15 -0700555 size_t length,
556 const PacketTime& packet_time) {
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000557 // Minimum RTP header size.
558 if (length < 12)
559 return DELIVERY_PACKET_ERROR;
560
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +0000561 uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(&packet[8]);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000562
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000563 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200564 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
565 auto it = audio_receive_ssrcs_.find(ssrc);
566 if (it != audio_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700567 auto status = it->second->DeliverRtp(packet, length, packet_time)
568 ? DELIVERY_OK
569 : DELIVERY_PACKET_ERROR;
570 if (status == DELIVERY_OK && event_log_)
571 event_log_->LogRtpHeader(true, media_type, packet, length);
572 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200573 }
574 }
575 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
576 auto it = video_receive_ssrcs_.find(ssrc);
577 if (it != video_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700578 auto status = it->second->DeliverRtp(packet, length, packet_time)
579 ? DELIVERY_OK
580 : DELIVERY_PACKET_ERROR;
581 if (status == DELIVERY_OK && event_log_)
582 event_log_->LogRtpHeader(true, media_type, packet, length);
583 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200584 }
585 }
586 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000587}
588
stefan68786d22015-09-08 05:36:15 -0700589PacketReceiver::DeliveryStatus Call::DeliverPacket(
590 MediaType media_type,
591 const uint8_t* packet,
592 size_t length,
593 const PacketTime& packet_time) {
solenberg5a289392015-10-19 03:39:20 -0700594 // TODO(solenberg): Tests call this function on a network thread, libjingle
595 // calls on the worker thread. We should move towards always using a network
596 // thread. Then this check can be enabled.
597 // RTC_DCHECK(!configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000598 if (RtpHeaderParser::IsRtcp(packet, length))
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200599 return DeliverRtcp(media_type, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000600
stefan68786d22015-09-08 05:36:15 -0700601 return DeliverRtp(media_type, packet, length, packet_time);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000602}
603
604} // namespace internal
605} // namespace webrtc