blob: eff441c5fabc638ddfcf0fff5eaed74a248e01a0 [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"
Peter Boström5c389d32015-09-25 13:58:30 +020024#include "webrtc/call/rtc_event_log.h"
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000025#include "webrtc/common.h"
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +000026#include "webrtc/config.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000027#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +000028#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
Peter Boström45553ae2015-05-08 13:54:38 +020029#include "webrtc/modules/utility/interface/process_thread.h"
Peter Boström45553ae2015-05-08 13:54:38 +020030#include "webrtc/system_wrappers/interface/cpu_info.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000031#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
pbos@webrtc.org32e85282015-01-15 10:09:39 +000032#include "webrtc/system_wrappers/interface/logging.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000033#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000034#include "webrtc/system_wrappers/interface/trace.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"
mflodmane3787022015-10-21 13:24:28 +020037#include "webrtc/video_engine/call_stats.h"
ivocb04965c2015-09-09 00:09:43 -070038#include "webrtc/voice_engine/include/voe_codec.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000039
40namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000041
pbos@webrtc.orga73a6782014-10-14 11:52:10 +000042const int Call::Config::kDefaultStartBitrateBps = 300000;
43
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000044namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000045
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000046class Call : public webrtc::Call, public PacketReceiver {
47 public:
Peter Boström45553ae2015-05-08 13:54:38 +020048 explicit Call(const Call::Config& config);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000049 virtual ~Call();
50
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000051 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000052
Fredrik Solenberg04f49312015-06-08 13:04:56 +020053 webrtc::AudioSendStream* CreateAudioSendStream(
54 const webrtc::AudioSendStream::Config& config) override;
55 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
56
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020057 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
58 const webrtc::AudioReceiveStream::Config& config) override;
59 void DestroyAudioReceiveStream(
60 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000061
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020062 webrtc::VideoSendStream* CreateVideoSendStream(
63 const webrtc::VideoSendStream::Config& config,
64 const VideoEncoderConfig& encoder_config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000065 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000066
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020067 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
68 const webrtc::VideoReceiveStream::Config& config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000069 void DestroyVideoReceiveStream(
70 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000071
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000072 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000073
stefan68786d22015-09-08 05:36:15 -070074 DeliveryStatus DeliverPacket(MediaType media_type,
75 const uint8_t* packet,
76 size_t length,
77 const PacketTime& packet_time) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000078
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000079 void SetBitrateConfig(
80 const webrtc::Call::Config::BitrateConfig& bitrate_config) override;
81 void SignalNetworkState(NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +000082
stefanc1aeaf02015-10-15 07:26:07 -070083 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
84
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000085 private:
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020086 DeliveryStatus DeliverRtcp(MediaType media_type, const uint8_t* packet,
87 size_t length);
stefan68786d22015-09-08 05:36:15 -070088 DeliveryStatus DeliverRtp(MediaType media_type,
89 const uint8_t* packet,
90 size_t length,
91 const PacketTime& packet_time);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000092
pbos8fc7fa72015-07-15 08:02:58 -070093 void ConfigureSync(const std::string& sync_group)
94 EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
95
Peter Boström45553ae2015-05-08 13:54:38 +020096 const int num_cpu_cores_;
97 const rtc::scoped_ptr<ProcessThread> module_process_thread_;
mflodmane3787022015-10-21 13:24:28 +020098 const rtc::scoped_ptr<CallStats> call_stats_;
Peter Boström45553ae2015-05-08 13:54:38 +020099 const rtc::scoped_ptr<ChannelGroup> channel_group_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000100 Call::Config config_;
solenberg5a289392015-10-19 03:39:20 -0700101 rtc::ThreadChecker configuration_thread_checker_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000102
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000103 // Needs to be held while write-locking |receive_crit_| or |send_crit_|. This
104 // ensures that we have a consistent network state signalled to all senders
105 // and receivers.
Peter Boströmf2f82832015-05-01 13:00:41 +0200106 rtc::CriticalSection network_enabled_crit_;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000107 bool network_enabled_ GUARDED_BY(network_enabled_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000108
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000109 rtc::scoped_ptr<RWLockWrapper> receive_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700110 // Audio and Video receive streams are owned by the client that creates them.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200111 std::map<uint32_t, AudioReceiveStream*> audio_receive_ssrcs_
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000112 GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200113 std::map<uint32_t, VideoReceiveStream*> video_receive_ssrcs_
114 GUARDED_BY(receive_crit_);
115 std::set<VideoReceiveStream*> video_receive_streams_
116 GUARDED_BY(receive_crit_);
pbos8fc7fa72015-07-15 08:02:58 -0700117 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
118 GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000119
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000120 rtc::scoped_ptr<RWLockWrapper> send_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700121 // Audio and Video send streams are owned by the client that creates them.
122 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_ GUARDED_BY(send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200123 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_ GUARDED_BY(send_crit_);
124 std::set<VideoSendStream*> video_send_streams_ GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000125
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200126 VideoSendStream::RtpStateMap suspended_video_send_ssrcs_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000127
solenberg43e83d42015-10-20 06:41:01 -0700128 RtcEventLog* event_log_;
ivocb04965c2015-09-09 00:09:43 -0700129
henrikg3c089d72015-09-16 05:37:44 -0700130 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000131};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000132} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000133
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000134Call* Call::Create(const Call::Config& config) {
Peter Boström45553ae2015-05-08 13:54:38 +0200135 return new internal::Call(config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000136}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000137
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000138namespace internal {
139
Peter Boström45553ae2015-05-08 13:54:38 +0200140Call::Call(const Call::Config& config)
141 : num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
stefan847855b2015-09-11 09:52:15 -0700142 module_process_thread_(ProcessThread::Create("ModuleProcessThread")),
mflodmane3787022015-10-21 13:24:28 +0200143 call_stats_(new CallStats()),
144 channel_group_(new ChannelGroup(module_process_thread_.get(),
145 call_stats_.get())),
Peter Boström45553ae2015-05-08 13:54:38 +0200146 config_(config),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000147 network_enabled_(true),
148 receive_crit_(RWLockWrapper::CreateRWLock()),
solenberg43e83d42015-10-20 06:41:01 -0700149 send_crit_(RWLockWrapper::CreateRWLock()),
150 event_log_(nullptr) {
solenberg5a289392015-10-19 03:39:20 -0700151 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700152 RTC_DCHECK_GE(config.bitrate_config.min_bitrate_bps, 0);
153 RTC_DCHECK_GE(config.bitrate_config.start_bitrate_bps,
154 config.bitrate_config.min_bitrate_bps);
Stefan Holmere5904162015-03-26 11:11:06 +0100155 if (config.bitrate_config.max_bitrate_bps != -1) {
henrikg91d6ede2015-09-17 00:24:34 -0700156 RTC_DCHECK_GE(config.bitrate_config.max_bitrate_bps,
157 config.bitrate_config.start_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000158 }
ivocb04965c2015-09-09 00:09:43 -0700159 if (config.voice_engine) {
solenberg43e83d42015-10-20 06:41:01 -0700160 VoECodec* voe_codec = VoECodec::GetInterface(config.voice_engine);
161 if (voe_codec) {
162 event_log_ = voe_codec->GetEventLog();
163 voe_codec->Release();
164 }
ivocb04965c2015-09-09 00:09:43 -0700165 }
pbos@webrtc.org00873182014-11-25 14:03:34 +0000166
Peter Boström45553ae2015-05-08 13:54:38 +0200167 Trace::CreateTrace();
168 module_process_thread_->Start();
mflodmane3787022015-10-21 13:24:28 +0200169 module_process_thread_->RegisterModule(call_stats_.get());
Peter Boström45553ae2015-05-08 13:54:38 +0200170
stefan4fbd1452015-09-28 03:57:14 -0700171 channel_group_->SetBweBitrates(config_.bitrate_config.min_bitrate_bps,
172 config_.bitrate_config.start_bitrate_bps,
173 config_.bitrate_config.max_bitrate_bps);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000174}
175
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000176Call::~Call() {
solenberg5a289392015-10-19 03:39:20 -0700177 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700178 RTC_CHECK(audio_send_ssrcs_.empty());
179 RTC_CHECK(video_send_ssrcs_.empty());
180 RTC_CHECK(video_send_streams_.empty());
181 RTC_CHECK(audio_receive_ssrcs_.empty());
182 RTC_CHECK(video_receive_ssrcs_.empty());
183 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000184
mflodmane3787022015-10-21 13:24:28 +0200185 module_process_thread_->DeRegisterModule(call_stats_.get());
Peter Boström45553ae2015-05-08 13:54:38 +0200186 module_process_thread_->Stop();
187 Trace::ReturnTrace();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000188}
189
solenberg5a289392015-10-19 03:39:20 -0700190PacketReceiver* Call::Receiver() {
191 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
192 // thread. Re-enable once that is fixed.
193 // RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
194 return this;
195}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000196
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200197webrtc::AudioSendStream* Call::CreateAudioSendStream(
198 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700199 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
solenberg5a289392015-10-19 03:39:20 -0700200 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700201 AudioSendStream* send_stream = new AudioSendStream(config);
202 {
203 rtc::CritScope lock(&network_enabled_crit_);
204 WriteLockScoped write_lock(*send_crit_);
205 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
206 audio_send_ssrcs_.end());
207 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
208
209 if (!network_enabled_)
210 send_stream->SignalNetworkState(kNetworkDown);
211 }
212 return send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200213}
214
215void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 14:35:07 -0700216 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
solenberg5a289392015-10-19 03:39:20 -0700217 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700218 RTC_DCHECK(send_stream != nullptr);
219
220 send_stream->Stop();
221
222 webrtc::internal::AudioSendStream* audio_send_stream =
223 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
224 {
225 WriteLockScoped write_lock(*send_crit_);
226 size_t num_deleted = audio_send_ssrcs_.erase(
227 audio_send_stream->config().rtp.ssrc);
228 RTC_DCHECK(num_deleted == 1);
229 }
230 delete audio_send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200231}
232
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200233webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
234 const webrtc::AudioReceiveStream::Config& config) {
235 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700236 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200237 AudioReceiveStream* receive_stream = new AudioReceiveStream(
solenberg43e83d42015-10-20 06:41:01 -0700238 channel_group_->GetRemoteBitrateEstimator(false), config);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200239 {
240 WriteLockScoped write_lock(*receive_crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700241 RTC_DCHECK(audio_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
242 audio_receive_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200243 audio_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos8fc7fa72015-07-15 08:02:58 -0700244 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200245 }
246 return receive_stream;
247}
248
249void Call::DestroyAudioReceiveStream(
250 webrtc::AudioReceiveStream* receive_stream) {
251 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700252 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700253 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700254 webrtc::internal::AudioReceiveStream* audio_receive_stream =
255 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200256 {
257 WriteLockScoped write_lock(*receive_crit_);
258 size_t num_deleted = audio_receive_ssrcs_.erase(
259 audio_receive_stream->config().rtp.remote_ssrc);
henrikg91d6ede2015-09-17 00:24:34 -0700260 RTC_DCHECK(num_deleted == 1);
pbos8fc7fa72015-07-15 08:02:58 -0700261 const std::string& sync_group = audio_receive_stream->config().sync_group;
262 const auto it = sync_stream_mapping_.find(sync_group);
263 if (it != sync_stream_mapping_.end() &&
264 it->second == audio_receive_stream) {
265 sync_stream_mapping_.erase(it);
266 ConfigureSync(sync_group);
267 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200268 }
269 delete audio_receive_stream;
270}
271
272webrtc::VideoSendStream* Call::CreateVideoSendStream(
273 const webrtc::VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000274 const VideoEncoderConfig& encoder_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000275 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
solenberg5a289392015-10-19 03:39:20 -0700276 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000277
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000278 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
279 // the call has already started.
solenberge5269742015-09-08 05:13:22 -0700280 VideoSendStream* send_stream = new VideoSendStream(num_cpu_cores_,
mflodmane3787022015-10-21 13:24:28 +0200281 module_process_thread_.get(), call_stats_.get(), channel_group_.get(),
282 config, encoder_config, suspended_video_send_ssrcs_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000283
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000284 // This needs to be taken before send_crit_ as both locks need to be held
285 // while changing network state.
Peter Boströmf2f82832015-05-01 13:00:41 +0200286 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000287 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200288 for (uint32_t ssrc : config.rtp.ssrcs) {
henrikg91d6ede2015-09-17 00:24:34 -0700289 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200290 video_send_ssrcs_[ssrc] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000291 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200292 video_send_streams_.insert(send_stream);
293
ivocb04965c2015-09-09 00:09:43 -0700294 if (event_log_)
295 event_log_->LogVideoSendStreamConfig(config);
296
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000297 if (!network_enabled_)
298 send_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000299 return send_stream;
300}
301
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000302void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000303 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700304 RTC_DCHECK(send_stream != nullptr);
solenberg5a289392015-10-19 03:39:20 -0700305 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000306
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000307 send_stream->Stop();
308
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000309 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000310 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000311 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200312 auto it = video_send_ssrcs_.begin();
313 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000314 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
315 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200316 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000317 } else {
318 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000319 }
320 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200321 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000322 }
henrikg91d6ede2015-09-17 00:24:34 -0700323 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000324
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000325 VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates();
326
327 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
328 it != rtp_state.end();
329 ++it) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200330 suspended_video_send_ssrcs_[it->first] = it->second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000331 }
332
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000333 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000334}
335
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200336webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
337 const webrtc::VideoReceiveStream::Config& config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000338 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700339 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
Peter Boströmc4188fd2015-04-24 15:16:03 +0200340 VideoReceiveStream* receive_stream = new VideoReceiveStream(
mflodman0dbf0092015-10-19 08:12:12 -0700341 num_cpu_cores_, channel_group_.get(), config, config_.voice_engine,
mflodmane3787022015-10-21 13:24:28 +0200342 module_process_thread_.get(), call_stats_.get());
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000343
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000344 // This needs to be taken before receive_crit_ as both locks need to be held
345 // while changing network state.
Peter Boströmf2f82832015-05-01 13:00:41 +0200346 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000347 WriteLockScoped write_lock(*receive_crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700348 RTC_DCHECK(video_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
349 video_receive_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200350 video_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000351 // TODO(pbos): Configure different RTX payloads per receive payload.
352 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
353 config.rtp.rtx.begin();
354 if (it != config.rtp.rtx.end())
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200355 video_receive_ssrcs_[it->second.ssrc] = receive_stream;
356 video_receive_streams_.insert(receive_stream);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000357
pbos8fc7fa72015-07-15 08:02:58 -0700358 ConfigureSync(config.sync_group);
359
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000360 if (!network_enabled_)
361 receive_stream->SignalNetworkState(kNetworkDown);
pbos8fc7fa72015-07-15 08:02:58 -0700362
ivocb04965c2015-09-09 00:09:43 -0700363 if (event_log_)
364 event_log_->LogVideoReceiveStreamConfig(config);
365
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000366 return receive_stream;
367}
368
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000369void Call::DestroyVideoReceiveStream(
370 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000371 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700372 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700373 RTC_DCHECK(receive_stream != nullptr);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000374 VideoReceiveStream* receive_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000375 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000376 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000377 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
378 // separate SSRC there can be either one or two.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200379 auto it = video_receive_ssrcs_.begin();
380 while (it != video_receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000381 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000382 if (receive_stream_impl != nullptr)
henrikg91d6ede2015-09-17 00:24:34 -0700383 RTC_DCHECK(receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000384 receive_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200385 video_receive_ssrcs_.erase(it++);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000386 } else {
387 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000388 }
389 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200390 video_receive_streams_.erase(receive_stream_impl);
henrikg91d6ede2015-09-17 00:24:34 -0700391 RTC_CHECK(receive_stream_impl != nullptr);
pbos8fc7fa72015-07-15 08:02:58 -0700392 ConfigureSync(receive_stream_impl->config().sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000393 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000394 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000395}
396
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000397Call::Stats Call::GetStats() const {
solenberg5a289392015-10-19 03:39:20 -0700398 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
399 // thread. Re-enable once that is fixed.
400 // RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000401 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +0200402 // Fetch available send/receive bitrates.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000403 uint32_t send_bandwidth = 0;
Peter Boström45553ae2015-05-08 13:54:38 +0200404 channel_group_->GetBitrateController()->AvailableBandwidth(&send_bandwidth);
405 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000406 uint32_t recv_bandwidth = 0;
mflodmana20de202015-10-18 22:08:19 -0700407 channel_group_->GetRemoteBitrateEstimator(false)->LatestEstimate(
408 &ssrcs, &recv_bandwidth);
Peter Boström45553ae2015-05-08 13:54:38 +0200409 stats.send_bandwidth_bps = send_bandwidth;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000410 stats.recv_bandwidth_bps = recv_bandwidth;
Peter Boström59d91dc2015-04-27 17:24:33 +0200411 stats.pacer_delay_ms = channel_group_->GetPacerQueuingDelayMs();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000412 {
413 ReadLockScoped read_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -0700414 // TODO(solenberg): Add audio send streams.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200415 for (const auto& kv : video_send_ssrcs_) {
416 int rtt_ms = kv.second->GetRtt();
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000417 if (rtt_ms > 0)
418 stats.rtt_ms = rtt_ms;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000419 }
420 }
421 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000422}
423
pbos@webrtc.org00873182014-11-25 14:03:34 +0000424void Call::SetBitrateConfig(
425 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000426 TRACE_EVENT0("webrtc", "Call::SetBitrateConfig");
solenberg5a289392015-10-19 03:39:20 -0700427 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700428 RTC_DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000429 if (bitrate_config.max_bitrate_bps != -1)
henrikg91d6ede2015-09-17 00:24:34 -0700430 RTC_DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
Stefan Holmere5904162015-03-26 11:11:06 +0100431 if (config_.bitrate_config.min_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000432 bitrate_config.min_bitrate_bps &&
433 (bitrate_config.start_bitrate_bps <= 0 ||
Stefan Holmere5904162015-03-26 11:11:06 +0100434 config_.bitrate_config.start_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000435 bitrate_config.start_bitrate_bps) &&
Stefan Holmere5904162015-03-26 11:11:06 +0100436 config_.bitrate_config.max_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000437 bitrate_config.max_bitrate_bps) {
438 // Nothing new to set, early abort to avoid encoder reconfigurations.
439 return;
440 }
Stefan Holmere5904162015-03-26 11:11:06 +0100441 config_.bitrate_config = bitrate_config;
stefan4fbd1452015-09-28 03:57:14 -0700442 channel_group_->SetBweBitrates(bitrate_config.min_bitrate_bps,
443 bitrate_config.start_bitrate_bps,
444 bitrate_config.max_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000445}
446
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000447void Call::SignalNetworkState(NetworkState state) {
solenberg5a289392015-10-19 03:39:20 -0700448 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000449 // Take crit for entire function, it needs to be held while updating streams
450 // to guarantee a consistent state across streams.
Peter Boströmf2f82832015-05-01 13:00:41 +0200451 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000452 network_enabled_ = state == kNetworkUp;
stefan457a61d2015-10-14 03:12:59 -0700453 channel_group_->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) {
solenberg5a289392015-10-19 03:39:20 -0700472 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
stefanc1aeaf02015-10-15 07:26:07 -0700473 channel_group_->OnSentPacket(sent_packet);
474}
475
pbos8fc7fa72015-07-15 08:02:58 -0700476void Call::ConfigureSync(const std::string& sync_group) {
477 // Set sync only if there was no previous one.
478 if (config_.voice_engine == nullptr || sync_group.empty())
479 return;
480
481 AudioReceiveStream* sync_audio_stream = nullptr;
482 // Find existing audio stream.
483 const auto it = sync_stream_mapping_.find(sync_group);
484 if (it != sync_stream_mapping_.end()) {
485 sync_audio_stream = it->second;
486 } else {
487 // No configured audio stream, see if we can find one.
488 for (const auto& kv : audio_receive_ssrcs_) {
489 if (kv.second->config().sync_group == sync_group) {
490 if (sync_audio_stream != nullptr) {
491 LOG(LS_WARNING) << "Attempting to sync more than one audio stream "
492 "within the same sync group. This is not "
493 "supported in the current implementation.";
494 break;
495 }
496 sync_audio_stream = kv.second;
497 }
498 }
499 }
500 if (sync_audio_stream)
501 sync_stream_mapping_[sync_group] = sync_audio_stream;
502 size_t num_synced_streams = 0;
503 for (VideoReceiveStream* video_stream : video_receive_streams_) {
504 if (video_stream->config().sync_group != sync_group)
505 continue;
506 ++num_synced_streams;
507 if (num_synced_streams > 1) {
508 // TODO(pbos): Support synchronizing more than one A/V pair.
509 // https://code.google.com/p/webrtc/issues/detail?id=4762
510 LOG(LS_WARNING) << "Attempting to sync more than one audio/video pair "
511 "within the same sync group. This is not supported in "
512 "the current implementation.";
513 }
514 // Only sync the first A/V pair within this sync group.
515 if (sync_audio_stream != nullptr && num_synced_streams == 1) {
516 video_stream->SetSyncChannel(config_.voice_engine,
517 sync_audio_stream->config().voe_channel_id);
518 } else {
519 video_stream->SetSyncChannel(config_.voice_engine, -1);
520 }
521 }
522}
523
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200524PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
525 const uint8_t* packet,
526 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000527 // TODO(pbos): Figure out what channel needs it actually.
528 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000529 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
530 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000531 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200532 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000533 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200534 for (VideoReceiveStream* stream : video_receive_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700535 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000536 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700537 if (event_log_)
538 event_log_->LogRtcpPacket(true, media_type, packet, length);
539 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000540 }
541 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200542 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000543 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200544 for (VideoSendStream* stream : video_send_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700545 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000546 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700547 if (event_log_)
548 event_log_->LogRtcpPacket(false, media_type, packet, length);
549 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000550 }
551 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000552 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000553}
554
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200555PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
556 const uint8_t* packet,
stefan68786d22015-09-08 05:36:15 -0700557 size_t length,
558 const PacketTime& packet_time) {
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000559 // Minimum RTP header size.
560 if (length < 12)
561 return DELIVERY_PACKET_ERROR;
562
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +0000563 uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(&packet[8]);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000564
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000565 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200566 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
567 auto it = audio_receive_ssrcs_.find(ssrc);
568 if (it != audio_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700569 auto status = it->second->DeliverRtp(packet, length, packet_time)
570 ? DELIVERY_OK
571 : DELIVERY_PACKET_ERROR;
572 if (status == DELIVERY_OK && event_log_)
573 event_log_->LogRtpHeader(true, media_type, packet, length);
574 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200575 }
576 }
577 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
578 auto it = video_receive_ssrcs_.find(ssrc);
579 if (it != video_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700580 auto status = it->second->DeliverRtp(packet, length, packet_time)
581 ? DELIVERY_OK
582 : DELIVERY_PACKET_ERROR;
583 if (status == DELIVERY_OK && event_log_)
584 event_log_->LogRtpHeader(true, media_type, packet, length);
585 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200586 }
587 }
588 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000589}
590
stefan68786d22015-09-08 05:36:15 -0700591PacketReceiver::DeliveryStatus Call::DeliverPacket(
592 MediaType media_type,
593 const uint8_t* packet,
594 size_t length,
595 const PacketTime& packet_time) {
solenberg5a289392015-10-19 03:39:20 -0700596 // TODO(solenberg): Tests call this function on a network thread, libjingle
597 // calls on the worker thread. We should move towards always using a network
598 // thread. Then this check can be enabled.
599 // RTC_DCHECK(!configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000600 if (RtpHeaderParser::IsRtcp(packet, length))
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200601 return DeliverRtcp(media_type, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000602
stefan68786d22015-09-08 05:36:15 -0700603 return DeliverRtp(media_type, packet, length, packet_time);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000604}
605
606} // namespace internal
607} // namespace webrtc