blob: 11445067271071fb367d270a028354af0e7e6755 [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"
solenberg566ef242015-11-06 15:34:49 -080018#include "webrtc/audio/audio_state.h"
19#include "webrtc/audio/scoped_voe_interface.h"
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +000020#include "webrtc/base/checks.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000021#include "webrtc/base/scoped_ptr.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000022#include "webrtc/base/thread_annotations.h"
solenberg5a289392015-10-19 03:39:20 -070023#include "webrtc/base/thread_checker.h"
tommie4f96502015-10-20 23:00:48 -070024#include "webrtc/base/trace_event.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000025#include "webrtc/call.h"
mflodman0c478b32015-10-21 15:52:16 +020026#include "webrtc/call/congestion_controller.h"
Peter Boström5c389d32015-09-25 13:58:30 +020027#include "webrtc/call/rtc_event_log.h"
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000028#include "webrtc/common.h"
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +000029#include "webrtc/config.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010030#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +000031#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010032#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010033#include "webrtc/system_wrappers/include/cpu_info.h"
34#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
35#include "webrtc/system_wrappers/include/logging.h"
36#include "webrtc/system_wrappers/include/rw_lock_wrapper.h"
37#include "webrtc/system_wrappers/include/trace.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000038#include "webrtc/video/video_receive_stream.h"
39#include "webrtc/video/video_send_stream.h"
mflodmane3787022015-10-21 13:24:28 +020040#include "webrtc/video_engine/call_stats.h"
ivocb04965c2015-09-09 00:09:43 -070041#include "webrtc/voice_engine/include/voe_codec.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000042
43namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000044
pbos@webrtc.orga73a6782014-10-14 11:52:10 +000045const int Call::Config::kDefaultStartBitrateBps = 300000;
46
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000047namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000048
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000049class Call : public webrtc::Call, public PacketReceiver {
50 public:
Peter Boström45553ae2015-05-08 13:54:38 +020051 explicit Call(const Call::Config& config);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000052 virtual ~Call();
53
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000054 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000055
Fredrik Solenberg04f49312015-06-08 13:04:56 +020056 webrtc::AudioSendStream* CreateAudioSendStream(
57 const webrtc::AudioSendStream::Config& config) override;
58 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
59
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020060 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
61 const webrtc::AudioReceiveStream::Config& config) override;
62 void DestroyAudioReceiveStream(
63 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000064
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020065 webrtc::VideoSendStream* CreateVideoSendStream(
66 const webrtc::VideoSendStream::Config& config,
67 const VideoEncoderConfig& encoder_config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000068 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000069
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020070 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
71 const webrtc::VideoReceiveStream::Config& config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000072 void DestroyVideoReceiveStream(
73 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000074
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000075 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000076
stefan68786d22015-09-08 05:36:15 -070077 DeliveryStatus DeliverPacket(MediaType media_type,
78 const uint8_t* packet,
79 size_t length,
80 const PacketTime& packet_time) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000081
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000082 void SetBitrateConfig(
83 const webrtc::Call::Config::BitrateConfig& bitrate_config) override;
84 void SignalNetworkState(NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +000085
stefanc1aeaf02015-10-15 07:26:07 -070086 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
87
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000088 private:
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020089 DeliveryStatus DeliverRtcp(MediaType media_type, const uint8_t* packet,
90 size_t length);
stefan68786d22015-09-08 05:36:15 -070091 DeliveryStatus DeliverRtp(MediaType media_type,
92 const uint8_t* packet,
93 size_t length,
94 const PacketTime& packet_time);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000095
pbos8fc7fa72015-07-15 08:02:58 -070096 void ConfigureSync(const std::string& sync_group)
97 EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
98
solenberg566ef242015-11-06 15:34:49 -080099 VoiceEngine* voice_engine() {
100 internal::AudioState* audio_state =
101 static_cast<internal::AudioState*>(config_.audio_state.get());
102 if (audio_state)
103 return audio_state->voice_engine();
104 else
105 return nullptr;
106 }
107
Peter Boström45553ae2015-05-08 13:54:38 +0200108 const int num_cpu_cores_;
109 const rtc::scoped_ptr<ProcessThread> module_process_thread_;
mflodmane3787022015-10-21 13:24:28 +0200110 const rtc::scoped_ptr<CallStats> call_stats_;
mflodman0c478b32015-10-21 15:52:16 +0200111 const rtc::scoped_ptr<CongestionController> congestion_controller_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000112 Call::Config config_;
solenberg5a289392015-10-19 03:39:20 -0700113 rtc::ThreadChecker configuration_thread_checker_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000114
mflodman717432f2015-10-26 16:34:46 +0100115 bool network_enabled_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000116
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000117 rtc::scoped_ptr<RWLockWrapper> receive_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700118 // Audio and Video receive streams are owned by the client that creates them.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200119 std::map<uint32_t, AudioReceiveStream*> audio_receive_ssrcs_
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000120 GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200121 std::map<uint32_t, VideoReceiveStream*> video_receive_ssrcs_
122 GUARDED_BY(receive_crit_);
123 std::set<VideoReceiveStream*> video_receive_streams_
124 GUARDED_BY(receive_crit_);
pbos8fc7fa72015-07-15 08:02:58 -0700125 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
126 GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000127
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000128 rtc::scoped_ptr<RWLockWrapper> send_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700129 // Audio and Video send streams are owned by the client that creates them.
130 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_ GUARDED_BY(send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200131 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_ GUARDED_BY(send_crit_);
132 std::set<VideoSendStream*> video_send_streams_ GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000133
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200134 VideoSendStream::RtpStateMap suspended_video_send_ssrcs_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000135
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200136 RtcEventLog* event_log_ = nullptr;
ivocb04965c2015-09-09 00:09:43 -0700137
henrikg3c089d72015-09-16 05:37:44 -0700138 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000139};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000140} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000141
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000142Call* Call::Create(const Call::Config& config) {
Peter Boström45553ae2015-05-08 13:54:38 +0200143 return new internal::Call(config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000144}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000145
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000146namespace internal {
147
Peter Boström45553ae2015-05-08 13:54:38 +0200148Call::Call(const Call::Config& config)
149 : num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
stefan847855b2015-09-11 09:52:15 -0700150 module_process_thread_(ProcessThread::Create("ModuleProcessThread")),
mflodmane3787022015-10-21 13:24:28 +0200151 call_stats_(new CallStats()),
mflodman0c478b32015-10-21 15:52:16 +0200152 congestion_controller_(new CongestionController(
153 module_process_thread_.get(), call_stats_.get())),
Peter Boström45553ae2015-05-08 13:54:38 +0200154 config_(config),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000155 network_enabled_(true),
156 receive_crit_(RWLockWrapper::CreateRWLock()),
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200157 send_crit_(RWLockWrapper::CreateRWLock()) {
henrikg91d6ede2015-09-17 00:24:34 -0700158 RTC_DCHECK_GE(config.bitrate_config.min_bitrate_bps, 0);
159 RTC_DCHECK_GE(config.bitrate_config.start_bitrate_bps,
160 config.bitrate_config.min_bitrate_bps);
Stefan Holmere5904162015-03-26 11:11:06 +0100161 if (config.bitrate_config.max_bitrate_bps != -1) {
henrikg91d6ede2015-09-17 00:24:34 -0700162 RTC_DCHECK_GE(config.bitrate_config.max_bitrate_bps,
163 config.bitrate_config.start_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000164 }
solenberg566ef242015-11-06 15:34:49 -0800165 if (config.audio_state.get()) {
166 ScopedVoEInterface<VoECodec> voe_codec(voice_engine());
167 event_log_ = voe_codec->GetEventLog();
ivocb04965c2015-09-09 00:09:43 -0700168 }
pbos@webrtc.org00873182014-11-25 14:03:34 +0000169
Peter Boström45553ae2015-05-08 13:54:38 +0200170 Trace::CreateTrace();
171 module_process_thread_->Start();
mflodmane3787022015-10-21 13:24:28 +0200172 module_process_thread_->RegisterModule(call_stats_.get());
Peter Boström45553ae2015-05-08 13:54:38 +0200173
mflodman0c478b32015-10-21 15:52:16 +0200174 congestion_controller_->SetBweBitrates(
175 config_.bitrate_config.min_bitrate_bps,
176 config_.bitrate_config.start_bitrate_bps,
177 config_.bitrate_config.max_bitrate_bps);
terelius006d93d2015-11-05 12:02:15 -0800178
179 congestion_controller_->GetBitrateController()->SetEventLog(event_log_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000180}
181
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000182Call::~Call() {
solenberg5a289392015-10-19 03:39:20 -0700183 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700184 RTC_CHECK(audio_send_ssrcs_.empty());
185 RTC_CHECK(video_send_ssrcs_.empty());
186 RTC_CHECK(video_send_streams_.empty());
187 RTC_CHECK(audio_receive_ssrcs_.empty());
188 RTC_CHECK(video_receive_ssrcs_.empty());
189 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000190
mflodmane3787022015-10-21 13:24:28 +0200191 module_process_thread_->DeRegisterModule(call_stats_.get());
Peter Boström45553ae2015-05-08 13:54:38 +0200192 module_process_thread_->Stop();
193 Trace::ReturnTrace();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000194}
195
solenberg5a289392015-10-19 03:39:20 -0700196PacketReceiver* Call::Receiver() {
197 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
198 // thread. Re-enable once that is fixed.
199 // RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
200 return this;
201}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000202
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200203webrtc::AudioSendStream* Call::CreateAudioSendStream(
204 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700205 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
solenberg5a289392015-10-19 03:39:20 -0700206 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -0700207 AudioSendStream* send_stream =
solenberg566ef242015-11-06 15:34:49 -0800208 new AudioSendStream(config, config_.audio_state);
mflodman717432f2015-10-26 16:34:46 +0100209 if (!network_enabled_)
210 send_stream->SignalNetworkState(kNetworkDown);
solenbergc7a8b082015-10-16 14:35:07 -0700211 {
solenbergc7a8b082015-10-16 14:35:07 -0700212 WriteLockScoped write_lock(*send_crit_);
213 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
214 audio_send_ssrcs_.end());
215 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
solenbergc7a8b082015-10-16 14:35:07 -0700216 }
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,
solenberg566ef242015-11-06 15:34:49 -0800244 config_.audio_state);
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
mflodman717432f2015-10-26 16:34:46 +0100291 if (!network_enabled_)
292 send_stream->SignalNetworkState(kNetworkDown);
293
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.org29d58392013-05-16 12:08:03 +0000304 return send_stream;
305}
306
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000307void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000308 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700309 RTC_DCHECK(send_stream != nullptr);
solenberg5a289392015-10-19 03:39:20 -0700310 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000311
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000312 send_stream->Stop();
313
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000314 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000315 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000316 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200317 auto it = video_send_ssrcs_.begin();
318 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000319 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
320 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200321 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000322 } else {
323 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000324 }
325 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200326 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000327 }
henrikg91d6ede2015-09-17 00:24:34 -0700328 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000329
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000330 VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates();
331
332 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
333 it != rtp_state.end();
334 ++it) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200335 suspended_video_send_ssrcs_[it->first] = it->second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000336 }
337
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000338 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000339}
340
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200341webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
342 const webrtc::VideoReceiveStream::Config& config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000343 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700344 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
Peter Boströmc4188fd2015-04-24 15:16:03 +0200345 VideoReceiveStream* receive_stream = new VideoReceiveStream(
mflodman0c478b32015-10-21 15:52:16 +0200346 num_cpu_cores_, congestion_controller_.get(), config,
solenberg566ef242015-11-06 15:34:49 -0800347 voice_engine(), module_process_thread_.get(), call_stats_.get());
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000348
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000349 WriteLockScoped write_lock(*receive_crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700350 RTC_DCHECK(video_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
351 video_receive_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200352 video_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000353 // TODO(pbos): Configure different RTX payloads per receive payload.
354 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
355 config.rtp.rtx.begin();
356 if (it != config.rtp.rtx.end())
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200357 video_receive_ssrcs_[it->second.ssrc] = receive_stream;
358 video_receive_streams_.insert(receive_stream);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000359
pbos8fc7fa72015-07-15 08:02:58 -0700360 ConfigureSync(config.sync_group);
361
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000362 if (!network_enabled_)
363 receive_stream->SignalNetworkState(kNetworkDown);
pbos8fc7fa72015-07-15 08:02:58 -0700364
ivocb04965c2015-09-09 00:09:43 -0700365 if (event_log_)
366 event_log_->LogVideoReceiveStreamConfig(config);
367
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000368 return receive_stream;
369}
370
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000371void Call::DestroyVideoReceiveStream(
372 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000373 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700374 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700375 RTC_DCHECK(receive_stream != nullptr);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000376 VideoReceiveStream* receive_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000377 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000378 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000379 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
380 // separate SSRC there can be either one or two.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200381 auto it = video_receive_ssrcs_.begin();
382 while (it != video_receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000383 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000384 if (receive_stream_impl != nullptr)
henrikg91d6ede2015-09-17 00:24:34 -0700385 RTC_DCHECK(receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000386 receive_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200387 video_receive_ssrcs_.erase(it++);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000388 } else {
389 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000390 }
391 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200392 video_receive_streams_.erase(receive_stream_impl);
henrikg91d6ede2015-09-17 00:24:34 -0700393 RTC_CHECK(receive_stream_impl != nullptr);
pbos8fc7fa72015-07-15 08:02:58 -0700394 ConfigureSync(receive_stream_impl->config().sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000395 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000396 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000397}
398
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000399Call::Stats Call::GetStats() const {
solenberg5a289392015-10-19 03:39:20 -0700400 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
401 // thread. Re-enable once that is fixed.
402 // RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000403 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +0200404 // Fetch available send/receive bitrates.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000405 uint32_t send_bandwidth = 0;
mflodman0c478b32015-10-21 15:52:16 +0200406 congestion_controller_->GetBitrateController()->AvailableBandwidth(
407 &send_bandwidth);
Peter Boström45553ae2015-05-08 13:54:38 +0200408 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000409 uint32_t recv_bandwidth = 0;
mflodman0c478b32015-10-21 15:52:16 +0200410 congestion_controller_->GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-18 22:08:19 -0700411 &ssrcs, &recv_bandwidth);
Peter Boström45553ae2015-05-08 13:54:38 +0200412 stats.send_bandwidth_bps = send_bandwidth;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000413 stats.recv_bandwidth_bps = recv_bandwidth;
mflodman0c478b32015-10-21 15:52:16 +0200414 stats.pacer_delay_ms = congestion_controller_->GetPacerQueuingDelayMs();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000415 {
416 ReadLockScoped read_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -0700417 // TODO(solenberg): Add audio send streams.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200418 for (const auto& kv : video_send_ssrcs_) {
419 int rtt_ms = kv.second->GetRtt();
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000420 if (rtt_ms > 0)
421 stats.rtt_ms = rtt_ms;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000422 }
423 }
424 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000425}
426
pbos@webrtc.org00873182014-11-25 14:03:34 +0000427void Call::SetBitrateConfig(
428 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000429 TRACE_EVENT0("webrtc", "Call::SetBitrateConfig");
solenberg5a289392015-10-19 03:39:20 -0700430 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700431 RTC_DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000432 if (bitrate_config.max_bitrate_bps != -1)
henrikg91d6ede2015-09-17 00:24:34 -0700433 RTC_DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
Stefan Holmere5904162015-03-26 11:11:06 +0100434 if (config_.bitrate_config.min_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000435 bitrate_config.min_bitrate_bps &&
436 (bitrate_config.start_bitrate_bps <= 0 ||
Stefan Holmere5904162015-03-26 11:11:06 +0100437 config_.bitrate_config.start_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000438 bitrate_config.start_bitrate_bps) &&
Stefan Holmere5904162015-03-26 11:11:06 +0100439 config_.bitrate_config.max_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000440 bitrate_config.max_bitrate_bps) {
441 // Nothing new to set, early abort to avoid encoder reconfigurations.
442 return;
443 }
Stefan Holmere5904162015-03-26 11:11:06 +0100444 config_.bitrate_config = bitrate_config;
mflodman0c478b32015-10-21 15:52:16 +0200445 congestion_controller_->SetBweBitrates(bitrate_config.min_bitrate_bps,
446 bitrate_config.start_bitrate_bps,
447 bitrate_config.max_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000448}
449
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000450void Call::SignalNetworkState(NetworkState state) {
solenberg5a289392015-10-19 03:39:20 -0700451 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000452 network_enabled_ = state == kNetworkUp;
mflodman0c478b32015-10-21 15:52:16 +0200453 congestion_controller_->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000454 {
455 ReadLockScoped write_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -0700456 for (auto& kv : audio_send_ssrcs_) {
457 kv.second->SignalNetworkState(state);
458 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200459 for (auto& kv : video_send_ssrcs_) {
460 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000461 }
462 }
463 {
464 ReadLockScoped write_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200465 for (auto& kv : video_receive_ssrcs_) {
466 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000467 }
468 }
469}
470
stefanc1aeaf02015-10-15 07:26:07 -0700471void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
mflodman0c478b32015-10-21 15:52:16 +0200472 congestion_controller_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -0700473}
474
pbos8fc7fa72015-07-15 08:02:58 -0700475void Call::ConfigureSync(const std::string& sync_group) {
476 // Set sync only if there was no previous one.
solenberg566ef242015-11-06 15:34:49 -0800477 if (voice_engine() == nullptr || sync_group.empty())
pbos8fc7fa72015-07-15 08:02:58 -0700478 return;
479
480 AudioReceiveStream* sync_audio_stream = nullptr;
481 // Find existing audio stream.
482 const auto it = sync_stream_mapping_.find(sync_group);
483 if (it != sync_stream_mapping_.end()) {
484 sync_audio_stream = it->second;
485 } else {
486 // No configured audio stream, see if we can find one.
487 for (const auto& kv : audio_receive_ssrcs_) {
488 if (kv.second->config().sync_group == sync_group) {
489 if (sync_audio_stream != nullptr) {
490 LOG(LS_WARNING) << "Attempting to sync more than one audio stream "
491 "within the same sync group. This is not "
492 "supported in the current implementation.";
493 break;
494 }
495 sync_audio_stream = kv.second;
496 }
497 }
498 }
499 if (sync_audio_stream)
500 sync_stream_mapping_[sync_group] = sync_audio_stream;
501 size_t num_synced_streams = 0;
502 for (VideoReceiveStream* video_stream : video_receive_streams_) {
503 if (video_stream->config().sync_group != sync_group)
504 continue;
505 ++num_synced_streams;
506 if (num_synced_streams > 1) {
507 // TODO(pbos): Support synchronizing more than one A/V pair.
508 // https://code.google.com/p/webrtc/issues/detail?id=4762
509 LOG(LS_WARNING) << "Attempting to sync more than one audio/video pair "
510 "within the same sync group. This is not supported in "
511 "the current implementation.";
512 }
513 // Only sync the first A/V pair within this sync group.
514 if (sync_audio_stream != nullptr && num_synced_streams == 1) {
solenberg566ef242015-11-06 15:34:49 -0800515 video_stream->SetSyncChannel(voice_engine(),
pbos8fc7fa72015-07-15 08:02:58 -0700516 sync_audio_stream->config().voe_channel_id);
517 } else {
solenberg566ef242015-11-06 15:34:49 -0800518 video_stream->SetSyncChannel(voice_engine(), -1);
pbos8fc7fa72015-07-15 08:02:58 -0700519 }
520 }
521}
522
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200523PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
524 const uint8_t* packet,
525 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000526 // TODO(pbos): Figure out what channel needs it actually.
527 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000528 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
529 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000530 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200531 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000532 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200533 for (VideoReceiveStream* stream : video_receive_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700534 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000535 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700536 if (event_log_)
537 event_log_->LogRtcpPacket(true, media_type, packet, length);
538 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000539 }
540 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200541 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000542 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200543 for (VideoSendStream* stream : video_send_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700544 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000545 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700546 if (event_log_)
547 event_log_->LogRtcpPacket(false, media_type, packet, length);
548 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000549 }
550 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000551 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000552}
553
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200554PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
555 const uint8_t* packet,
stefan68786d22015-09-08 05:36:15 -0700556 size_t length,
557 const PacketTime& packet_time) {
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000558 // Minimum RTP header size.
559 if (length < 12)
560 return DELIVERY_PACKET_ERROR;
561
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +0000562 uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(&packet[8]);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000563
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000564 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200565 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
566 auto it = audio_receive_ssrcs_.find(ssrc);
567 if (it != audio_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700568 auto status = it->second->DeliverRtp(packet, length, packet_time)
569 ? DELIVERY_OK
570 : DELIVERY_PACKET_ERROR;
571 if (status == DELIVERY_OK && event_log_)
572 event_log_->LogRtpHeader(true, media_type, packet, length);
573 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200574 }
575 }
576 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
577 auto it = video_receive_ssrcs_.find(ssrc);
578 if (it != video_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700579 auto status = it->second->DeliverRtp(packet, length, packet_time)
580 ? DELIVERY_OK
581 : DELIVERY_PACKET_ERROR;
582 if (status == DELIVERY_OK && event_log_)
583 event_log_->LogRtpHeader(true, media_type, packet, length);
584 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200585 }
586 }
587 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000588}
589
stefan68786d22015-09-08 05:36:15 -0700590PacketReceiver::DeliveryStatus Call::DeliverPacket(
591 MediaType media_type,
592 const uint8_t* packet,
593 size_t length,
594 const PacketTime& packet_time) {
solenberg5a289392015-10-19 03:39:20 -0700595 // TODO(solenberg): Tests call this function on a network thread, libjingle
596 // calls on the worker thread. We should move towards always using a network
597 // thread. Then this check can be enabled.
598 // RTC_DCHECK(!configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000599 if (RtpHeaderParser::IsRtcp(packet, length))
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200600 return DeliverRtcp(media_type, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000601
stefan68786d22015-09-08 05:36:15 -0700602 return DeliverRtp(media_type, packet, length, packet_time);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000603}
604
605} // namespace internal
606} // namespace webrtc