blob: 39357d57df70ef3b3303ba37b093c4f476b376e2 [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"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010028#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +000029#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010030#include "webrtc/modules/utility/include/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);
terelius006d93d2015-11-05 12:02:15 -0800171
172 congestion_controller_->GetBitrateController()->SetEventLog(event_log_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000173}
174
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000175Call::~Call() {
solenberg5a289392015-10-19 03:39:20 -0700176 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700177 RTC_CHECK(audio_send_ssrcs_.empty());
178 RTC_CHECK(video_send_ssrcs_.empty());
179 RTC_CHECK(video_send_streams_.empty());
180 RTC_CHECK(audio_receive_ssrcs_.empty());
181 RTC_CHECK(video_receive_ssrcs_.empty());
182 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000183
mflodmane3787022015-10-21 13:24:28 +0200184 module_process_thread_->DeRegisterModule(call_stats_.get());
Peter Boström45553ae2015-05-08 13:54:38 +0200185 module_process_thread_->Stop();
186 Trace::ReturnTrace();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200187
188 if (voe_codec_)
189 voe_codec_->Release();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000190}
191
solenberg5a289392015-10-19 03:39:20 -0700192PacketReceiver* Call::Receiver() {
193 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
194 // thread. Re-enable once that is fixed.
195 // RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
196 return this;
197}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000198
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200199webrtc::AudioSendStream* Call::CreateAudioSendStream(
200 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700201 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
solenberg5a289392015-10-19 03:39:20 -0700202 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -0700203 AudioSendStream* send_stream =
204 new AudioSendStream(config, config_.voice_engine);
mflodman717432f2015-10-26 16:34:46 +0100205 if (!network_enabled_)
206 send_stream->SignalNetworkState(kNetworkDown);
solenbergc7a8b082015-10-16 14:35:07 -0700207 {
solenbergc7a8b082015-10-16 14:35:07 -0700208 WriteLockScoped write_lock(*send_crit_);
209 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
210 audio_send_ssrcs_.end());
211 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
solenbergc7a8b082015-10-16 14:35:07 -0700212 }
213 return send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200214}
215
216void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 14:35:07 -0700217 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
solenberg5a289392015-10-19 03:39:20 -0700218 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700219 RTC_DCHECK(send_stream != nullptr);
220
221 send_stream->Stop();
222
223 webrtc::internal::AudioSendStream* audio_send_stream =
224 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
225 {
226 WriteLockScoped write_lock(*send_crit_);
227 size_t num_deleted = audio_send_ssrcs_.erase(
228 audio_send_stream->config().rtp.ssrc);
229 RTC_DCHECK(num_deleted == 1);
230 }
231 delete audio_send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200232}
233
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200234webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
235 const webrtc::AudioReceiveStream::Config& config) {
236 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700237 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200238 AudioReceiveStream* receive_stream = new AudioReceiveStream(
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200239 congestion_controller_->GetRemoteBitrateEstimator(false), config,
240 config_.voice_engine);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200241 {
242 WriteLockScoped write_lock(*receive_crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700243 RTC_DCHECK(audio_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
244 audio_receive_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200245 audio_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos8fc7fa72015-07-15 08:02:58 -0700246 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200247 }
248 return receive_stream;
249}
250
251void Call::DestroyAudioReceiveStream(
252 webrtc::AudioReceiveStream* receive_stream) {
253 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700254 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700255 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700256 webrtc::internal::AudioReceiveStream* audio_receive_stream =
257 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200258 {
259 WriteLockScoped write_lock(*receive_crit_);
260 size_t num_deleted = audio_receive_ssrcs_.erase(
261 audio_receive_stream->config().rtp.remote_ssrc);
henrikg91d6ede2015-09-17 00:24:34 -0700262 RTC_DCHECK(num_deleted == 1);
pbos8fc7fa72015-07-15 08:02:58 -0700263 const std::string& sync_group = audio_receive_stream->config().sync_group;
264 const auto it = sync_stream_mapping_.find(sync_group);
265 if (it != sync_stream_mapping_.end() &&
266 it->second == audio_receive_stream) {
267 sync_stream_mapping_.erase(it);
268 ConfigureSync(sync_group);
269 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200270 }
271 delete audio_receive_stream;
272}
273
274webrtc::VideoSendStream* Call::CreateVideoSendStream(
275 const webrtc::VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000276 const VideoEncoderConfig& encoder_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000277 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
solenberg5a289392015-10-19 03:39:20 -0700278 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000279
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000280 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
281 // the call has already started.
mflodman0c478b32015-10-21 15:52:16 +0200282 VideoSendStream* send_stream = new VideoSendStream(
283 num_cpu_cores_, module_process_thread_.get(), call_stats_.get(),
284 congestion_controller_.get(), config, encoder_config,
285 suspended_video_send_ssrcs_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000286
mflodman717432f2015-10-26 16:34:46 +0100287 if (!network_enabled_)
288 send_stream->SignalNetworkState(kNetworkDown);
289
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000290 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200291 for (uint32_t ssrc : config.rtp.ssrcs) {
henrikg91d6ede2015-09-17 00:24:34 -0700292 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200293 video_send_ssrcs_[ssrc] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000294 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200295 video_send_streams_.insert(send_stream);
296
ivocb04965c2015-09-09 00:09:43 -0700297 if (event_log_)
298 event_log_->LogVideoSendStreamConfig(config);
299
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000300 return send_stream;
301}
302
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000303void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000304 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700305 RTC_DCHECK(send_stream != nullptr);
solenberg5a289392015-10-19 03:39:20 -0700306 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000307
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000308 send_stream->Stop();
309
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000310 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000311 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000312 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200313 auto it = video_send_ssrcs_.begin();
314 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000315 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
316 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200317 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000318 } else {
319 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000320 }
321 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200322 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000323 }
henrikg91d6ede2015-09-17 00:24:34 -0700324 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000325
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000326 VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates();
327
328 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
329 it != rtp_state.end();
330 ++it) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200331 suspended_video_send_ssrcs_[it->first] = it->second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000332 }
333
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000334 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000335}
336
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200337webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
338 const webrtc::VideoReceiveStream::Config& config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000339 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700340 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
Peter Boströmc4188fd2015-04-24 15:16:03 +0200341 VideoReceiveStream* receive_stream = new VideoReceiveStream(
mflodman0c478b32015-10-21 15:52:16 +0200342 num_cpu_cores_, congestion_controller_.get(), config,
343 config_.voice_engine, module_process_thread_.get(), call_stats_.get());
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000344
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;
mflodman0c478b32015-10-21 15:52:16 +0200402 congestion_controller_->GetBitrateController()->AvailableBandwidth(
403 &send_bandwidth);
Peter Boström45553ae2015-05-08 13:54:38 +0200404 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000405 uint32_t recv_bandwidth = 0;
mflodman0c478b32015-10-21 15:52:16 +0200406 congestion_controller_->GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-18 22:08:19 -0700407 &ssrcs, &recv_bandwidth);
Peter Boström45553ae2015-05-08 13:54:38 +0200408 stats.send_bandwidth_bps = send_bandwidth;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000409 stats.recv_bandwidth_bps = recv_bandwidth;
mflodman0c478b32015-10-21 15:52:16 +0200410 stats.pacer_delay_ms = congestion_controller_->GetPacerQueuingDelayMs();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000411 {
412 ReadLockScoped read_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -0700413 // TODO(solenberg): Add audio send streams.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200414 for (const auto& kv : video_send_ssrcs_) {
415 int rtt_ms = kv.second->GetRtt();
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000416 if (rtt_ms > 0)
417 stats.rtt_ms = rtt_ms;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000418 }
419 }
420 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000421}
422
pbos@webrtc.org00873182014-11-25 14:03:34 +0000423void Call::SetBitrateConfig(
424 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000425 TRACE_EVENT0("webrtc", "Call::SetBitrateConfig");
solenberg5a289392015-10-19 03:39:20 -0700426 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700427 RTC_DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000428 if (bitrate_config.max_bitrate_bps != -1)
henrikg91d6ede2015-09-17 00:24:34 -0700429 RTC_DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
Stefan Holmere5904162015-03-26 11:11:06 +0100430 if (config_.bitrate_config.min_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000431 bitrate_config.min_bitrate_bps &&
432 (bitrate_config.start_bitrate_bps <= 0 ||
Stefan Holmere5904162015-03-26 11:11:06 +0100433 config_.bitrate_config.start_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000434 bitrate_config.start_bitrate_bps) &&
Stefan Holmere5904162015-03-26 11:11:06 +0100435 config_.bitrate_config.max_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000436 bitrate_config.max_bitrate_bps) {
437 // Nothing new to set, early abort to avoid encoder reconfigurations.
438 return;
439 }
Stefan Holmere5904162015-03-26 11:11:06 +0100440 config_.bitrate_config = bitrate_config;
mflodman0c478b32015-10-21 15:52:16 +0200441 congestion_controller_->SetBweBitrates(bitrate_config.min_bitrate_bps,
442 bitrate_config.start_bitrate_bps,
443 bitrate_config.max_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000444}
445
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000446void Call::SignalNetworkState(NetworkState state) {
solenberg5a289392015-10-19 03:39:20 -0700447 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000448 network_enabled_ = state == kNetworkUp;
mflodman0c478b32015-10-21 15:52:16 +0200449 congestion_controller_->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000450 {
451 ReadLockScoped write_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -0700452 for (auto& kv : audio_send_ssrcs_) {
453 kv.second->SignalNetworkState(state);
454 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200455 for (auto& kv : video_send_ssrcs_) {
456 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000457 }
458 }
459 {
460 ReadLockScoped write_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200461 for (auto& kv : video_receive_ssrcs_) {
462 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000463 }
464 }
465}
466
stefanc1aeaf02015-10-15 07:26:07 -0700467void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
mflodman0c478b32015-10-21 15:52:16 +0200468 congestion_controller_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -0700469}
470
pbos8fc7fa72015-07-15 08:02:58 -0700471void Call::ConfigureSync(const std::string& sync_group) {
472 // Set sync only if there was no previous one.
473 if (config_.voice_engine == nullptr || sync_group.empty())
474 return;
475
476 AudioReceiveStream* sync_audio_stream = nullptr;
477 // Find existing audio stream.
478 const auto it = sync_stream_mapping_.find(sync_group);
479 if (it != sync_stream_mapping_.end()) {
480 sync_audio_stream = it->second;
481 } else {
482 // No configured audio stream, see if we can find one.
483 for (const auto& kv : audio_receive_ssrcs_) {
484 if (kv.second->config().sync_group == sync_group) {
485 if (sync_audio_stream != nullptr) {
486 LOG(LS_WARNING) << "Attempting to sync more than one audio stream "
487 "within the same sync group. This is not "
488 "supported in the current implementation.";
489 break;
490 }
491 sync_audio_stream = kv.second;
492 }
493 }
494 }
495 if (sync_audio_stream)
496 sync_stream_mapping_[sync_group] = sync_audio_stream;
497 size_t num_synced_streams = 0;
498 for (VideoReceiveStream* video_stream : video_receive_streams_) {
499 if (video_stream->config().sync_group != sync_group)
500 continue;
501 ++num_synced_streams;
502 if (num_synced_streams > 1) {
503 // TODO(pbos): Support synchronizing more than one A/V pair.
504 // https://code.google.com/p/webrtc/issues/detail?id=4762
505 LOG(LS_WARNING) << "Attempting to sync more than one audio/video pair "
506 "within the same sync group. This is not supported in "
507 "the current implementation.";
508 }
509 // Only sync the first A/V pair within this sync group.
510 if (sync_audio_stream != nullptr && num_synced_streams == 1) {
511 video_stream->SetSyncChannel(config_.voice_engine,
512 sync_audio_stream->config().voe_channel_id);
513 } else {
514 video_stream->SetSyncChannel(config_.voice_engine, -1);
515 }
516 }
517}
518
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200519PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
520 const uint8_t* packet,
521 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000522 // TODO(pbos): Figure out what channel needs it actually.
523 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000524 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
525 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000526 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200527 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000528 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200529 for (VideoReceiveStream* stream : video_receive_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700530 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000531 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700532 if (event_log_)
533 event_log_->LogRtcpPacket(true, media_type, packet, length);
534 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000535 }
536 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200537 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000538 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200539 for (VideoSendStream* stream : video_send_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700540 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000541 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700542 if (event_log_)
543 event_log_->LogRtcpPacket(false, media_type, packet, length);
544 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000545 }
546 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000547 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000548}
549
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200550PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
551 const uint8_t* packet,
stefan68786d22015-09-08 05:36:15 -0700552 size_t length,
553 const PacketTime& packet_time) {
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000554 // Minimum RTP header size.
555 if (length < 12)
556 return DELIVERY_PACKET_ERROR;
557
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +0000558 uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(&packet[8]);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000559
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000560 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200561 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
562 auto it = audio_receive_ssrcs_.find(ssrc);
563 if (it != audio_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700564 auto status = it->second->DeliverRtp(packet, length, packet_time)
565 ? DELIVERY_OK
566 : DELIVERY_PACKET_ERROR;
567 if (status == DELIVERY_OK && event_log_)
568 event_log_->LogRtpHeader(true, media_type, packet, length);
569 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200570 }
571 }
572 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
573 auto it = video_receive_ssrcs_.find(ssrc);
574 if (it != video_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700575 auto status = it->second->DeliverRtp(packet, length, packet_time)
576 ? DELIVERY_OK
577 : DELIVERY_PACKET_ERROR;
578 if (status == DELIVERY_OK && event_log_)
579 event_log_->LogRtpHeader(true, media_type, packet, length);
580 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200581 }
582 }
583 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000584}
585
stefan68786d22015-09-08 05:36:15 -0700586PacketReceiver::DeliveryStatus Call::DeliverPacket(
587 MediaType media_type,
588 const uint8_t* packet,
589 size_t length,
590 const PacketTime& packet_time) {
solenberg5a289392015-10-19 03:39:20 -0700591 // TODO(solenberg): Tests call this function on a network thread, libjingle
592 // calls on the worker thread. We should move towards always using a network
593 // thread. Then this check can be enabled.
594 // RTC_DCHECK(!configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000595 if (RtpHeaderParser::IsRtcp(packet, length))
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200596 return DeliverRtcp(media_type, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000597
stefan68786d22015-09-08 05:36:15 -0700598 return DeliverRtp(media_type, packet, length, packet_time);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000599}
600
601} // namespace internal
602} // namespace webrtc