blob: c2b1ec7950463337ef1ce6b0e8ed75ab76bb2082 [file] [log] [blame]
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000011#include <string.h>
12
pbos@webrtc.org29d58392013-05-16 12:08:03 +000013#include <map>
14#include <vector>
15
Peter Boström5c389d32015-09-25 13:58:30 +020016#include "webrtc/audio/audio_receive_stream.h"
solenbergc7a8b082015-10-16 14:35:07 -070017#include "webrtc/audio/audio_send_stream.h"
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +000018#include "webrtc/base/checks.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000019#include "webrtc/base/scoped_ptr.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000020#include "webrtc/base/thread_annotations.h"
solenberg5a289392015-10-19 03:39:20 -070021#include "webrtc/base/thread_checker.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000022#include "webrtc/call.h"
Peter Boström5c389d32015-09-25 13:58:30 +020023#include "webrtc/call/rtc_event_log.h"
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000024#include "webrtc/common.h"
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +000025#include "webrtc/config.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000026#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +000027#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
Peter Boström45553ae2015-05-08 13:54:38 +020028#include "webrtc/modules/utility/interface/process_thread.h"
Peter Boström45553ae2015-05-08 13:54:38 +020029#include "webrtc/system_wrappers/interface/cpu_info.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000030#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
pbos@webrtc.org32e85282015-01-15 10:09:39 +000031#include "webrtc/system_wrappers/interface/logging.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000032#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000033#include "webrtc/system_wrappers/interface/trace.h"
pbos@webrtc.org50fe3592015-01-29 12:33:07 +000034#include "webrtc/system_wrappers/interface/trace_event.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000035#include "webrtc/video/video_receive_stream.h"
36#include "webrtc/video/video_send_stream.h"
ivocb04965c2015-09-09 00:09:43 -070037#include "webrtc/voice_engine/include/voe_codec.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000038
39namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000040
pbos@webrtc.orga73a6782014-10-14 11:52:10 +000041const int Call::Config::kDefaultStartBitrateBps = 300000;
42
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000043namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000044
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000045class Call : public webrtc::Call, public PacketReceiver {
46 public:
Peter Boström45553ae2015-05-08 13:54:38 +020047 explicit Call(const Call::Config& config);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000048 virtual ~Call();
49
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000050 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000051
Fredrik Solenberg04f49312015-06-08 13:04:56 +020052 webrtc::AudioSendStream* CreateAudioSendStream(
53 const webrtc::AudioSendStream::Config& config) override;
54 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
55
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020056 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
57 const webrtc::AudioReceiveStream::Config& config) override;
58 void DestroyAudioReceiveStream(
59 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000060
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020061 webrtc::VideoSendStream* CreateVideoSendStream(
62 const webrtc::VideoSendStream::Config& config,
63 const VideoEncoderConfig& encoder_config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000064 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000065
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020066 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
67 const webrtc::VideoReceiveStream::Config& config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000068 void DestroyVideoReceiveStream(
69 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000070
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000071 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000072
stefan68786d22015-09-08 05:36:15 -070073 DeliveryStatus DeliverPacket(MediaType media_type,
74 const uint8_t* packet,
75 size_t length,
76 const PacketTime& packet_time) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000077
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000078 void SetBitrateConfig(
79 const webrtc::Call::Config::BitrateConfig& bitrate_config) override;
80 void SignalNetworkState(NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +000081
stefanc1aeaf02015-10-15 07:26:07 -070082 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
83
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000084 private:
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020085 DeliveryStatus DeliverRtcp(MediaType media_type, const uint8_t* packet,
86 size_t length);
stefan68786d22015-09-08 05:36:15 -070087 DeliveryStatus DeliverRtp(MediaType media_type,
88 const uint8_t* packet,
89 size_t length,
90 const PacketTime& packet_time);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000091
pbos8fc7fa72015-07-15 08:02:58 -070092 void ConfigureSync(const std::string& sync_group)
93 EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
94
Peter Boström45553ae2015-05-08 13:54:38 +020095 const int num_cpu_cores_;
96 const rtc::scoped_ptr<ProcessThread> module_process_thread_;
97 const rtc::scoped_ptr<ChannelGroup> channel_group_;
Peter Boström45553ae2015-05-08 13:54:38 +020098 volatile int next_channel_id_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000099 Call::Config config_;
solenberg5a289392015-10-19 03:39:20 -0700100 rtc::ThreadChecker configuration_thread_checker_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000101
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000102 // Needs to be held while write-locking |receive_crit_| or |send_crit_|. This
103 // ensures that we have a consistent network state signalled to all senders
104 // and receivers.
Peter Boströmf2f82832015-05-01 13:00:41 +0200105 rtc::CriticalSection network_enabled_crit_;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000106 bool network_enabled_ GUARDED_BY(network_enabled_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000107
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000108 rtc::scoped_ptr<RWLockWrapper> receive_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700109 // Audio and Video receive streams are owned by the client that creates them.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200110 std::map<uint32_t, AudioReceiveStream*> audio_receive_ssrcs_
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000111 GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200112 std::map<uint32_t, VideoReceiveStream*> video_receive_ssrcs_
113 GUARDED_BY(receive_crit_);
114 std::set<VideoReceiveStream*> video_receive_streams_
115 GUARDED_BY(receive_crit_);
pbos8fc7fa72015-07-15 08:02:58 -0700116 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
117 GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000118
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000119 rtc::scoped_ptr<RWLockWrapper> send_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700120 // Audio and Video send streams are owned by the client that creates them.
121 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_ GUARDED_BY(send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200122 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_ GUARDED_BY(send_crit_);
123 std::set<VideoSendStream*> video_send_streams_ GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000124
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200125 VideoSendStream::RtpStateMap suspended_video_send_ssrcs_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000126
ivocb04965c2015-09-09 00:09:43 -0700127 RtcEventLog* event_log_;
128
henrikg3c089d72015-09-16 05:37:44 -0700129 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000130};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000131} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000132
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000133Call* Call::Create(const Call::Config& config) {
Peter Boström45553ae2015-05-08 13:54:38 +0200134 return new internal::Call(config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000135}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000136
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000137namespace internal {
138
Peter Boström45553ae2015-05-08 13:54:38 +0200139Call::Call(const Call::Config& config)
140 : num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
stefan847855b2015-09-11 09:52:15 -0700141 module_process_thread_(ProcessThread::Create("ModuleProcessThread")),
Peter Boström2251d6e2015-05-28 14:10:39 +0200142 channel_group_(new ChannelGroup(module_process_thread_.get())),
pbosd6fc47e2015-07-23 06:58:33 -0700143 next_channel_id_(0),
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()),
ivocb04965c2015-09-09 00:09:43 -0700147 send_crit_(RWLockWrapper::CreateRWLock()),
148 event_log_(nullptr) {
solenberg5a289392015-10-19 03:39:20 -0700149 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700150 RTC_DCHECK_GE(config.bitrate_config.min_bitrate_bps, 0);
151 RTC_DCHECK_GE(config.bitrate_config.start_bitrate_bps,
152 config.bitrate_config.min_bitrate_bps);
Stefan Holmere5904162015-03-26 11:11:06 +0100153 if (config.bitrate_config.max_bitrate_bps != -1) {
henrikg91d6ede2015-09-17 00:24:34 -0700154 RTC_DCHECK_GE(config.bitrate_config.max_bitrate_bps,
155 config.bitrate_config.start_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000156 }
ivocb04965c2015-09-09 00:09:43 -0700157 if (config.voice_engine) {
158 VoECodec* voe_codec = VoECodec::GetInterface(config.voice_engine);
159 if (voe_codec) {
160 event_log_ = voe_codec->GetEventLog();
161 voe_codec->Release();
162 }
163 }
pbos@webrtc.org00873182014-11-25 14:03:34 +0000164
Peter Boström45553ae2015-05-08 13:54:38 +0200165 Trace::CreateTrace();
166 module_process_thread_->Start();
167
stefan4fbd1452015-09-28 03:57:14 -0700168 channel_group_->SetBweBitrates(config_.bitrate_config.min_bitrate_bps,
169 config_.bitrate_config.start_bitrate_bps,
170 config_.bitrate_config.max_bitrate_bps);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000171}
172
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000173Call::~Call() {
solenberg5a289392015-10-19 03:39:20 -0700174 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700175 RTC_CHECK(audio_send_ssrcs_.empty());
176 RTC_CHECK(video_send_ssrcs_.empty());
177 RTC_CHECK(video_send_streams_.empty());
178 RTC_CHECK(audio_receive_ssrcs_.empty());
179 RTC_CHECK(video_receive_ssrcs_.empty());
180 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000181
Peter Boström45553ae2015-05-08 13:54:38 +0200182 module_process_thread_->Stop();
183 Trace::ReturnTrace();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000184}
185
solenberg5a289392015-10-19 03:39:20 -0700186PacketReceiver* Call::Receiver() {
187 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
188 // thread. Re-enable once that is fixed.
189 // RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
190 return this;
191}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000192
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200193webrtc::AudioSendStream* Call::CreateAudioSendStream(
194 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700195 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
solenberg5a289392015-10-19 03:39:20 -0700196 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700197 AudioSendStream* send_stream = new AudioSendStream(config);
198 {
199 rtc::CritScope lock(&network_enabled_crit_);
200 WriteLockScoped write_lock(*send_crit_);
201 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
202 audio_send_ssrcs_.end());
203 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
204
205 if (!network_enabled_)
206 send_stream->SignalNetworkState(kNetworkDown);
207 }
208 return send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200209}
210
211void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 14:35:07 -0700212 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
solenberg5a289392015-10-19 03:39:20 -0700213 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700214 RTC_DCHECK(send_stream != nullptr);
215
216 send_stream->Stop();
217
218 webrtc::internal::AudioSendStream* audio_send_stream =
219 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
220 {
221 WriteLockScoped write_lock(*send_crit_);
222 size_t num_deleted = audio_send_ssrcs_.erase(
223 audio_send_stream->config().rtp.ssrc);
224 RTC_DCHECK(num_deleted == 1);
225 }
226 delete audio_send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200227}
228
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200229webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
230 const webrtc::AudioReceiveStream::Config& config) {
231 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700232 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200233 AudioReceiveStream* receive_stream = new AudioReceiveStream(
mflodmana20de202015-10-18 22:08:19 -0700234 channel_group_->GetRemoteBitrateEstimator(false), config);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200235 {
236 WriteLockScoped write_lock(*receive_crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700237 RTC_DCHECK(audio_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
238 audio_receive_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200239 audio_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos8fc7fa72015-07-15 08:02:58 -0700240 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200241 }
242 return receive_stream;
243}
244
245void Call::DestroyAudioReceiveStream(
246 webrtc::AudioReceiveStream* receive_stream) {
247 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700248 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700249 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700250 webrtc::internal::AudioReceiveStream* audio_receive_stream =
251 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200252 {
253 WriteLockScoped write_lock(*receive_crit_);
254 size_t num_deleted = audio_receive_ssrcs_.erase(
255 audio_receive_stream->config().rtp.remote_ssrc);
henrikg91d6ede2015-09-17 00:24:34 -0700256 RTC_DCHECK(num_deleted == 1);
pbos8fc7fa72015-07-15 08:02:58 -0700257 const std::string& sync_group = audio_receive_stream->config().sync_group;
258 const auto it = sync_stream_mapping_.find(sync_group);
259 if (it != sync_stream_mapping_.end() &&
260 it->second == audio_receive_stream) {
261 sync_stream_mapping_.erase(it);
262 ConfigureSync(sync_group);
263 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200264 }
265 delete audio_receive_stream;
266}
267
268webrtc::VideoSendStream* Call::CreateVideoSendStream(
269 const webrtc::VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000270 const VideoEncoderConfig& encoder_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000271 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
solenberg5a289392015-10-19 03:39:20 -0700272 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000273
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000274 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
275 // the call has already started.
solenberge5269742015-09-08 05:13:22 -0700276 VideoSendStream* send_stream = new VideoSendStream(num_cpu_cores_,
Peter Boström45553ae2015-05-08 13:54:38 +0200277 module_process_thread_.get(), channel_group_.get(),
278 rtc::AtomicOps::Increment(&next_channel_id_), config, encoder_config,
279 suspended_video_send_ssrcs_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000280
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000281 // This needs to be taken before send_crit_ as both locks need to be held
282 // while changing network state.
Peter Boströmf2f82832015-05-01 13:00:41 +0200283 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000284 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200285 for (uint32_t ssrc : config.rtp.ssrcs) {
henrikg91d6ede2015-09-17 00:24:34 -0700286 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200287 video_send_ssrcs_[ssrc] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000288 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200289 video_send_streams_.insert(send_stream);
290
ivocb04965c2015-09-09 00:09:43 -0700291 if (event_log_)
292 event_log_->LogVideoSendStreamConfig(config);
293
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000294 if (!network_enabled_)
295 send_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000296 return send_stream;
297}
298
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000299void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000300 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700301 RTC_DCHECK(send_stream != nullptr);
solenberg5a289392015-10-19 03:39:20 -0700302 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000303
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000304 send_stream->Stop();
305
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000306 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000307 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000308 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200309 auto it = video_send_ssrcs_.begin();
310 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000311 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
312 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200313 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000314 } else {
315 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000316 }
317 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200318 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000319 }
henrikg91d6ede2015-09-17 00:24:34 -0700320 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000321
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000322 VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates();
323
324 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
325 it != rtp_state.end();
326 ++it) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200327 suspended_video_send_ssrcs_[it->first] = it->second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000328 }
329
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000330 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000331}
332
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200333webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
334 const webrtc::VideoReceiveStream::Config& config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000335 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700336 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
Peter Boströmc4188fd2015-04-24 15:16:03 +0200337 VideoReceiveStream* receive_stream = new VideoReceiveStream(
pbosd6fc47e2015-07-23 06:58:33 -0700338 num_cpu_cores_, channel_group_.get(),
Peter Boström45553ae2015-05-08 13:54:38 +0200339 rtc::AtomicOps::Increment(&next_channel_id_), config,
mflodmana20de202015-10-18 22:08:19 -0700340 config_.voice_engine, module_process_thread_.get());
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000341
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000342 // This needs to be taken before receive_crit_ as both locks need to be held
343 // while changing network state.
Peter Boströmf2f82832015-05-01 13:00:41 +0200344 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000345 WriteLockScoped write_lock(*receive_crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700346 RTC_DCHECK(video_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
347 video_receive_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200348 video_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000349 // TODO(pbos): Configure different RTX payloads per receive payload.
350 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
351 config.rtp.rtx.begin();
352 if (it != config.rtp.rtx.end())
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200353 video_receive_ssrcs_[it->second.ssrc] = receive_stream;
354 video_receive_streams_.insert(receive_stream);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000355
pbos8fc7fa72015-07-15 08:02:58 -0700356 ConfigureSync(config.sync_group);
357
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000358 if (!network_enabled_)
359 receive_stream->SignalNetworkState(kNetworkDown);
pbos8fc7fa72015-07-15 08:02:58 -0700360
ivocb04965c2015-09-09 00:09:43 -0700361 if (event_log_)
362 event_log_->LogVideoReceiveStreamConfig(config);
363
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000364 return receive_stream;
365}
366
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000367void Call::DestroyVideoReceiveStream(
368 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000369 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
solenberg5a289392015-10-19 03:39:20 -0700370 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700371 RTC_DCHECK(receive_stream != nullptr);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000372 VideoReceiveStream* receive_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000373 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000374 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000375 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
376 // separate SSRC there can be either one or two.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200377 auto it = video_receive_ssrcs_.begin();
378 while (it != video_receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000379 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000380 if (receive_stream_impl != nullptr)
henrikg91d6ede2015-09-17 00:24:34 -0700381 RTC_DCHECK(receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000382 receive_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200383 video_receive_ssrcs_.erase(it++);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000384 } else {
385 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000386 }
387 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200388 video_receive_streams_.erase(receive_stream_impl);
henrikg91d6ede2015-09-17 00:24:34 -0700389 RTC_CHECK(receive_stream_impl != nullptr);
pbos8fc7fa72015-07-15 08:02:58 -0700390 ConfigureSync(receive_stream_impl->config().sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000391 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000392 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000393}
394
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000395Call::Stats Call::GetStats() const {
solenberg5a289392015-10-19 03:39:20 -0700396 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
397 // thread. Re-enable once that is fixed.
398 // RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000399 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +0200400 // Fetch available send/receive bitrates.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000401 uint32_t send_bandwidth = 0;
Peter Boström45553ae2015-05-08 13:54:38 +0200402 channel_group_->GetBitrateController()->AvailableBandwidth(&send_bandwidth);
403 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000404 uint32_t recv_bandwidth = 0;
mflodmana20de202015-10-18 22:08:19 -0700405 channel_group_->GetRemoteBitrateEstimator(false)->LatestEstimate(
406 &ssrcs, &recv_bandwidth);
Peter Boström45553ae2015-05-08 13:54:38 +0200407 stats.send_bandwidth_bps = send_bandwidth;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000408 stats.recv_bandwidth_bps = recv_bandwidth;
Peter Boström59d91dc2015-04-27 17:24:33 +0200409 stats.pacer_delay_ms = channel_group_->GetPacerQueuingDelayMs();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000410 {
411 ReadLockScoped read_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -0700412 // TODO(solenberg): Add audio send streams.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200413 for (const auto& kv : video_send_ssrcs_) {
414 int rtt_ms = kv.second->GetRtt();
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000415 if (rtt_ms > 0)
416 stats.rtt_ms = rtt_ms;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000417 }
418 }
419 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000420}
421
pbos@webrtc.org00873182014-11-25 14:03:34 +0000422void Call::SetBitrateConfig(
423 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000424 TRACE_EVENT0("webrtc", "Call::SetBitrateConfig");
solenberg5a289392015-10-19 03:39:20 -0700425 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700426 RTC_DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000427 if (bitrate_config.max_bitrate_bps != -1)
henrikg91d6ede2015-09-17 00:24:34 -0700428 RTC_DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
Stefan Holmere5904162015-03-26 11:11:06 +0100429 if (config_.bitrate_config.min_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000430 bitrate_config.min_bitrate_bps &&
431 (bitrate_config.start_bitrate_bps <= 0 ||
Stefan Holmere5904162015-03-26 11:11:06 +0100432 config_.bitrate_config.start_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000433 bitrate_config.start_bitrate_bps) &&
Stefan Holmere5904162015-03-26 11:11:06 +0100434 config_.bitrate_config.max_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000435 bitrate_config.max_bitrate_bps) {
436 // Nothing new to set, early abort to avoid encoder reconfigurations.
437 return;
438 }
Stefan Holmere5904162015-03-26 11:11:06 +0100439 config_.bitrate_config = bitrate_config;
stefan4fbd1452015-09-28 03:57:14 -0700440 channel_group_->SetBweBitrates(bitrate_config.min_bitrate_bps,
441 bitrate_config.start_bitrate_bps,
442 bitrate_config.max_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000443}
444
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000445void Call::SignalNetworkState(NetworkState state) {
solenberg5a289392015-10-19 03:39:20 -0700446 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000447 // Take crit for entire function, it needs to be held while updating streams
448 // to guarantee a consistent state across streams.
Peter Boströmf2f82832015-05-01 13:00:41 +0200449 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000450 network_enabled_ = state == kNetworkUp;
stefan457a61d2015-10-14 03:12:59 -0700451 channel_group_->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000452 {
453 ReadLockScoped write_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -0700454 for (auto& kv : audio_send_ssrcs_) {
455 kv.second->SignalNetworkState(state);
456 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200457 for (auto& kv : video_send_ssrcs_) {
458 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000459 }
460 }
461 {
462 ReadLockScoped write_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200463 for (auto& kv : video_receive_ssrcs_) {
464 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000465 }
466 }
467}
468
stefanc1aeaf02015-10-15 07:26:07 -0700469void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
solenberg5a289392015-10-19 03:39:20 -0700470 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
stefanc1aeaf02015-10-15 07:26:07 -0700471 channel_group_->OnSentPacket(sent_packet);
472}
473
pbos8fc7fa72015-07-15 08:02:58 -0700474void Call::ConfigureSync(const std::string& sync_group) {
475 // Set sync only if there was no previous one.
476 if (config_.voice_engine == nullptr || sync_group.empty())
477 return;
478
479 AudioReceiveStream* sync_audio_stream = nullptr;
480 // Find existing audio stream.
481 const auto it = sync_stream_mapping_.find(sync_group);
482 if (it != sync_stream_mapping_.end()) {
483 sync_audio_stream = it->second;
484 } else {
485 // No configured audio stream, see if we can find one.
486 for (const auto& kv : audio_receive_ssrcs_) {
487 if (kv.second->config().sync_group == sync_group) {
488 if (sync_audio_stream != nullptr) {
489 LOG(LS_WARNING) << "Attempting to sync more than one audio stream "
490 "within the same sync group. This is not "
491 "supported in the current implementation.";
492 break;
493 }
494 sync_audio_stream = kv.second;
495 }
496 }
497 }
498 if (sync_audio_stream)
499 sync_stream_mapping_[sync_group] = sync_audio_stream;
500 size_t num_synced_streams = 0;
501 for (VideoReceiveStream* video_stream : video_receive_streams_) {
502 if (video_stream->config().sync_group != sync_group)
503 continue;
504 ++num_synced_streams;
505 if (num_synced_streams > 1) {
506 // TODO(pbos): Support synchronizing more than one A/V pair.
507 // https://code.google.com/p/webrtc/issues/detail?id=4762
508 LOG(LS_WARNING) << "Attempting to sync more than one audio/video pair "
509 "within the same sync group. This is not supported in "
510 "the current implementation.";
511 }
512 // Only sync the first A/V pair within this sync group.
513 if (sync_audio_stream != nullptr && num_synced_streams == 1) {
514 video_stream->SetSyncChannel(config_.voice_engine,
515 sync_audio_stream->config().voe_channel_id);
516 } else {
517 video_stream->SetSyncChannel(config_.voice_engine, -1);
518 }
519 }
520}
521
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200522PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
523 const uint8_t* packet,
524 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000525 // TODO(pbos): Figure out what channel needs it actually.
526 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000527 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
528 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000529 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200530 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000531 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200532 for (VideoReceiveStream* stream : video_receive_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700533 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000534 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700535 if (event_log_)
536 event_log_->LogRtcpPacket(true, media_type, packet, length);
537 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000538 }
539 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200540 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000541 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200542 for (VideoSendStream* stream : video_send_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700543 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000544 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700545 if (event_log_)
546 event_log_->LogRtcpPacket(false, media_type, packet, length);
547 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000548 }
549 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000550 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000551}
552
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200553PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
554 const uint8_t* packet,
stefan68786d22015-09-08 05:36:15 -0700555 size_t length,
556 const PacketTime& packet_time) {
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000557 // Minimum RTP header size.
558 if (length < 12)
559 return DELIVERY_PACKET_ERROR;
560
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +0000561 uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(&packet[8]);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000562
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000563 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200564 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
565 auto it = audio_receive_ssrcs_.find(ssrc);
566 if (it != audio_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700567 auto status = it->second->DeliverRtp(packet, length, packet_time)
568 ? DELIVERY_OK
569 : DELIVERY_PACKET_ERROR;
570 if (status == DELIVERY_OK && event_log_)
571 event_log_->LogRtpHeader(true, media_type, packet, length);
572 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200573 }
574 }
575 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
576 auto it = video_receive_ssrcs_.find(ssrc);
577 if (it != video_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700578 auto status = it->second->DeliverRtp(packet, length, packet_time)
579 ? DELIVERY_OK
580 : DELIVERY_PACKET_ERROR;
581 if (status == DELIVERY_OK && event_log_)
582 event_log_->LogRtpHeader(true, media_type, packet, length);
583 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200584 }
585 }
586 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000587}
588
stefan68786d22015-09-08 05:36:15 -0700589PacketReceiver::DeliveryStatus Call::DeliverPacket(
590 MediaType media_type,
591 const uint8_t* packet,
592 size_t length,
593 const PacketTime& packet_time) {
solenberg5a289392015-10-19 03:39:20 -0700594 // TODO(solenberg): Tests call this function on a network thread, libjingle
595 // calls on the worker thread. We should move towards always using a network
596 // thread. Then this check can be enabled.
597 // RTC_DCHECK(!configuration_thread_checker_.CalledOnValidThread());
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000598 if (RtpHeaderParser::IsRtcp(packet, length))
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200599 return DeliverRtcp(media_type, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000600
stefan68786d22015-09-08 05:36:15 -0700601 return DeliverRtp(media_type, packet, length, packet_time);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000602}
603
604} // namespace internal
605} // namespace webrtc