blob: 21109c21f5d005bec7244171fe178d709ba83c24 [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"
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +000017#include "webrtc/base/checks.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000018#include "webrtc/base/scoped_ptr.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000019#include "webrtc/base/thread_annotations.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000020#include "webrtc/call.h"
Peter Boström5c389d32015-09-25 13:58:30 +020021#include "webrtc/call/rtc_event_log.h"
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000022#include "webrtc/common.h"
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +000023#include "webrtc/config.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000024#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +000025#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
Peter Boström45553ae2015-05-08 13:54:38 +020026#include "webrtc/modules/utility/interface/process_thread.h"
Peter Boström45553ae2015-05-08 13:54:38 +020027#include "webrtc/system_wrappers/interface/cpu_info.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000028#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
pbos@webrtc.org32e85282015-01-15 10:09:39 +000029#include "webrtc/system_wrappers/interface/logging.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000030#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000031#include "webrtc/system_wrappers/interface/trace.h"
pbos@webrtc.org50fe3592015-01-29 12:33:07 +000032#include "webrtc/system_wrappers/interface/trace_event.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000033#include "webrtc/video/video_receive_stream.h"
34#include "webrtc/video/video_send_stream.h"
ivocb04965c2015-09-09 00:09:43 -070035#include "webrtc/voice_engine/include/voe_codec.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000036
37namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000038
pbos@webrtc.orga73a6782014-10-14 11:52:10 +000039const int Call::Config::kDefaultStartBitrateBps = 300000;
40
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000041namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000042
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000043class Call : public webrtc::Call, public PacketReceiver {
44 public:
Peter Boström45553ae2015-05-08 13:54:38 +020045 explicit Call(const Call::Config& config);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000046 virtual ~Call();
47
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000048 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000049
Fredrik Solenberg04f49312015-06-08 13:04:56 +020050 webrtc::AudioSendStream* CreateAudioSendStream(
51 const webrtc::AudioSendStream::Config& config) override;
52 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
53
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020054 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
55 const webrtc::AudioReceiveStream::Config& config) override;
56 void DestroyAudioReceiveStream(
57 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000058
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020059 webrtc::VideoSendStream* CreateVideoSendStream(
60 const webrtc::VideoSendStream::Config& config,
61 const VideoEncoderConfig& encoder_config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000062 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000063
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020064 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
65 const webrtc::VideoReceiveStream::Config& config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000066 void DestroyVideoReceiveStream(
67 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000068
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000069 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000070
stefan68786d22015-09-08 05:36:15 -070071 DeliveryStatus DeliverPacket(MediaType media_type,
72 const uint8_t* packet,
73 size_t length,
74 const PacketTime& packet_time) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000075
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000076 void SetBitrateConfig(
77 const webrtc::Call::Config::BitrateConfig& bitrate_config) override;
78 void SignalNetworkState(NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +000079
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000080 private:
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020081 DeliveryStatus DeliverRtcp(MediaType media_type, const uint8_t* packet,
82 size_t length);
stefan68786d22015-09-08 05:36:15 -070083 DeliveryStatus DeliverRtp(MediaType media_type,
84 const uint8_t* packet,
85 size_t length,
86 const PacketTime& packet_time);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000087
Peter Boström45553ae2015-05-08 13:54:38 +020088 void SetBitrateControllerConfig(
89 const webrtc::Call::Config::BitrateConfig& bitrate_config);
90
pbos8fc7fa72015-07-15 08:02:58 -070091 void ConfigureSync(const std::string& sync_group)
92 EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
93
Peter Boström45553ae2015-05-08 13:54:38 +020094 const int num_cpu_cores_;
95 const rtc::scoped_ptr<ProcessThread> module_process_thread_;
96 const rtc::scoped_ptr<ChannelGroup> channel_group_;
Peter Boström45553ae2015-05-08 13:54:38 +020097 volatile int next_channel_id_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000098 Call::Config config_;
99
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000100 // Needs to be held while write-locking |receive_crit_| or |send_crit_|. This
101 // ensures that we have a consistent network state signalled to all senders
102 // and receivers.
Peter Boströmf2f82832015-05-01 13:00:41 +0200103 rtc::CriticalSection network_enabled_crit_;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000104 bool network_enabled_ GUARDED_BY(network_enabled_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000105
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000106 rtc::scoped_ptr<RWLockWrapper> receive_crit_;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200107 std::map<uint32_t, AudioReceiveStream*> audio_receive_ssrcs_
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000108 GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200109 std::map<uint32_t, VideoReceiveStream*> video_receive_ssrcs_
110 GUARDED_BY(receive_crit_);
111 std::set<VideoReceiveStream*> video_receive_streams_
112 GUARDED_BY(receive_crit_);
pbos8fc7fa72015-07-15 08:02:58 -0700113 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
114 GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000115
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000116 rtc::scoped_ptr<RWLockWrapper> send_crit_;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200117 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_ GUARDED_BY(send_crit_);
118 std::set<VideoSendStream*> video_send_streams_ GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000119
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200120 VideoSendStream::RtpStateMap suspended_video_send_ssrcs_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000121
ivocb04965c2015-09-09 00:09:43 -0700122 RtcEventLog* event_log_;
123
henrikg3c089d72015-09-16 05:37:44 -0700124 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000125};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000126} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000127
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000128Call* Call::Create(const Call::Config& config) {
Peter Boström45553ae2015-05-08 13:54:38 +0200129 return new internal::Call(config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000130}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000131
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000132namespace internal {
133
Peter Boström45553ae2015-05-08 13:54:38 +0200134Call::Call(const Call::Config& config)
135 : num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
stefan847855b2015-09-11 09:52:15 -0700136 module_process_thread_(ProcessThread::Create("ModuleProcessThread")),
Peter Boström2251d6e2015-05-28 14:10:39 +0200137 channel_group_(new ChannelGroup(module_process_thread_.get())),
pbosd6fc47e2015-07-23 06:58:33 -0700138 next_channel_id_(0),
Peter Boström45553ae2015-05-08 13:54:38 +0200139 config_(config),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000140 network_enabled_(true),
141 receive_crit_(RWLockWrapper::CreateRWLock()),
ivocb04965c2015-09-09 00:09:43 -0700142 send_crit_(RWLockWrapper::CreateRWLock()),
143 event_log_(nullptr) {
henrikg91d6ede2015-09-17 00:24:34 -0700144 RTC_DCHECK_GE(config.bitrate_config.min_bitrate_bps, 0);
145 RTC_DCHECK_GE(config.bitrate_config.start_bitrate_bps,
146 config.bitrate_config.min_bitrate_bps);
Stefan Holmere5904162015-03-26 11:11:06 +0100147 if (config.bitrate_config.max_bitrate_bps != -1) {
henrikg91d6ede2015-09-17 00:24:34 -0700148 RTC_DCHECK_GE(config.bitrate_config.max_bitrate_bps,
149 config.bitrate_config.start_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000150 }
ivocb04965c2015-09-09 00:09:43 -0700151 if (config.voice_engine) {
152 VoECodec* voe_codec = VoECodec::GetInterface(config.voice_engine);
153 if (voe_codec) {
154 event_log_ = voe_codec->GetEventLog();
155 voe_codec->Release();
156 }
157 }
pbos@webrtc.org00873182014-11-25 14:03:34 +0000158
Peter Boström45553ae2015-05-08 13:54:38 +0200159 Trace::CreateTrace();
160 module_process_thread_->Start();
161
Peter Boström45553ae2015-05-08 13:54:38 +0200162 SetBitrateControllerConfig(config_.bitrate_config);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000163}
164
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000165Call::~Call() {
henrikg91d6ede2015-09-17 00:24:34 -0700166 RTC_CHECK_EQ(0u, video_send_ssrcs_.size());
167 RTC_CHECK_EQ(0u, video_send_streams_.size());
168 RTC_CHECK_EQ(0u, audio_receive_ssrcs_.size());
169 RTC_CHECK_EQ(0u, video_receive_ssrcs_.size());
170 RTC_CHECK_EQ(0u, video_receive_streams_.size());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000171
Peter Boström45553ae2015-05-08 13:54:38 +0200172 module_process_thread_->Stop();
173 Trace::ReturnTrace();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000174}
175
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000176PacketReceiver* Call::Receiver() { return this; }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000177
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200178webrtc::AudioSendStream* Call::CreateAudioSendStream(
179 const webrtc::AudioSendStream::Config& config) {
180 return nullptr;
181}
182
183void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
184}
185
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200186webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
187 const webrtc::AudioReceiveStream::Config& config) {
188 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
189 LOG(LS_INFO) << "CreateAudioReceiveStream: " << config.ToString();
190 AudioReceiveStream* receive_stream = new AudioReceiveStream(
191 channel_group_->GetRemoteBitrateEstimator(), config);
192 {
193 WriteLockScoped write_lock(*receive_crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700194 RTC_DCHECK(audio_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
195 audio_receive_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200196 audio_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos8fc7fa72015-07-15 08:02:58 -0700197 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200198 }
199 return receive_stream;
200}
201
202void Call::DestroyAudioReceiveStream(
203 webrtc::AudioReceiveStream* receive_stream) {
204 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
henrikg91d6ede2015-09-17 00:24:34 -0700205 RTC_DCHECK(receive_stream != nullptr);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200206 AudioReceiveStream* audio_receive_stream =
207 static_cast<AudioReceiveStream*>(receive_stream);
208 {
209 WriteLockScoped write_lock(*receive_crit_);
210 size_t num_deleted = audio_receive_ssrcs_.erase(
211 audio_receive_stream->config().rtp.remote_ssrc);
henrikg91d6ede2015-09-17 00:24:34 -0700212 RTC_DCHECK(num_deleted == 1);
pbos8fc7fa72015-07-15 08:02:58 -0700213 const std::string& sync_group = audio_receive_stream->config().sync_group;
214 const auto it = sync_stream_mapping_.find(sync_group);
215 if (it != sync_stream_mapping_.end() &&
216 it->second == audio_receive_stream) {
217 sync_stream_mapping_.erase(it);
218 ConfigureSync(sync_group);
219 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200220 }
221 delete audio_receive_stream;
222}
223
224webrtc::VideoSendStream* Call::CreateVideoSendStream(
225 const webrtc::VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000226 const VideoEncoderConfig& encoder_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000227 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
pbos@webrtc.org32e85282015-01-15 10:09:39 +0000228 LOG(LS_INFO) << "CreateVideoSendStream: " << config.ToString();
henrikg91d6ede2015-09-17 00:24:34 -0700229 RTC_DCHECK(!config.rtp.ssrcs.empty());
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000230
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000231 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
232 // the call has already started.
solenberge5269742015-09-08 05:13:22 -0700233 VideoSendStream* send_stream = new VideoSendStream(num_cpu_cores_,
Peter Boström45553ae2015-05-08 13:54:38 +0200234 module_process_thread_.get(), channel_group_.get(),
235 rtc::AtomicOps::Increment(&next_channel_id_), config, encoder_config,
236 suspended_video_send_ssrcs_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000237
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000238 // This needs to be taken before send_crit_ as both locks need to be held
239 // while changing network state.
Peter Boströmf2f82832015-05-01 13:00:41 +0200240 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000241 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200242 for (uint32_t ssrc : config.rtp.ssrcs) {
henrikg91d6ede2015-09-17 00:24:34 -0700243 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200244 video_send_ssrcs_[ssrc] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000245 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200246 video_send_streams_.insert(send_stream);
247
ivocb04965c2015-09-09 00:09:43 -0700248 if (event_log_)
249 event_log_->LogVideoSendStreamConfig(config);
250
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000251 if (!network_enabled_)
252 send_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000253 return send_stream;
254}
255
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000256void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000257 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700258 RTC_DCHECK(send_stream != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000259
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000260 send_stream->Stop();
261
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000262 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000263 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000264 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200265 auto it = video_send_ssrcs_.begin();
266 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000267 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
268 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200269 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000270 } else {
271 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000272 }
273 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200274 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000275 }
henrikg91d6ede2015-09-17 00:24:34 -0700276 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000277
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000278 VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates();
279
280 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
281 it != rtp_state.end();
282 ++it) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200283 suspended_video_send_ssrcs_[it->first] = it->second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000284 }
285
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000286 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000287}
288
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200289webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
290 const webrtc::VideoReceiveStream::Config& config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000291 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
pbos@webrtc.org32e85282015-01-15 10:09:39 +0000292 LOG(LS_INFO) << "CreateVideoReceiveStream: " << config.ToString();
Peter Boströmc4188fd2015-04-24 15:16:03 +0200293 VideoReceiveStream* receive_stream = new VideoReceiveStream(
pbosd6fc47e2015-07-23 06:58:33 -0700294 num_cpu_cores_, channel_group_.get(),
Peter Boström45553ae2015-05-08 13:54:38 +0200295 rtc::AtomicOps::Increment(&next_channel_id_), config,
solenberg4fbae2b2015-08-28 04:07:10 -0700296 config_.voice_engine);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000297
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000298 // This needs to be taken before receive_crit_ as both locks need to be held
299 // while changing network state.
Peter Boströmf2f82832015-05-01 13:00:41 +0200300 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000301 WriteLockScoped write_lock(*receive_crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700302 RTC_DCHECK(video_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
303 video_receive_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200304 video_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000305 // TODO(pbos): Configure different RTX payloads per receive payload.
306 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
307 config.rtp.rtx.begin();
308 if (it != config.rtp.rtx.end())
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200309 video_receive_ssrcs_[it->second.ssrc] = receive_stream;
310 video_receive_streams_.insert(receive_stream);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000311
pbos8fc7fa72015-07-15 08:02:58 -0700312 ConfigureSync(config.sync_group);
313
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000314 if (!network_enabled_)
315 receive_stream->SignalNetworkState(kNetworkDown);
pbos8fc7fa72015-07-15 08:02:58 -0700316
ivocb04965c2015-09-09 00:09:43 -0700317 if (event_log_)
318 event_log_->LogVideoReceiveStreamConfig(config);
319
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000320 return receive_stream;
321}
322
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000323void Call::DestroyVideoReceiveStream(
324 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000325 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
henrikg91d6ede2015-09-17 00:24:34 -0700326 RTC_DCHECK(receive_stream != nullptr);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000327 VideoReceiveStream* receive_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000328 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000329 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000330 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
331 // separate SSRC there can be either one or two.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200332 auto it = video_receive_ssrcs_.begin();
333 while (it != video_receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000334 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000335 if (receive_stream_impl != nullptr)
henrikg91d6ede2015-09-17 00:24:34 -0700336 RTC_DCHECK(receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000337 receive_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200338 video_receive_ssrcs_.erase(it++);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000339 } else {
340 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000341 }
342 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200343 video_receive_streams_.erase(receive_stream_impl);
henrikg91d6ede2015-09-17 00:24:34 -0700344 RTC_CHECK(receive_stream_impl != nullptr);
pbos8fc7fa72015-07-15 08:02:58 -0700345 ConfigureSync(receive_stream_impl->config().sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000346 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000347 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000348}
349
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000350Call::Stats Call::GetStats() const {
351 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +0200352 // Fetch available send/receive bitrates.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000353 uint32_t send_bandwidth = 0;
Peter Boström45553ae2015-05-08 13:54:38 +0200354 channel_group_->GetBitrateController()->AvailableBandwidth(&send_bandwidth);
355 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000356 uint32_t recv_bandwidth = 0;
Peter Boström45553ae2015-05-08 13:54:38 +0200357 channel_group_->GetRemoteBitrateEstimator()->LatestEstimate(&ssrcs,
358 &recv_bandwidth);
359 stats.send_bandwidth_bps = send_bandwidth;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000360 stats.recv_bandwidth_bps = recv_bandwidth;
Peter Boström59d91dc2015-04-27 17:24:33 +0200361 stats.pacer_delay_ms = channel_group_->GetPacerQueuingDelayMs();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000362 {
363 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200364 for (const auto& kv : video_send_ssrcs_) {
365 int rtt_ms = kv.second->GetRtt();
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000366 if (rtt_ms > 0)
367 stats.rtt_ms = rtt_ms;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000368 }
369 }
370 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000371}
372
pbos@webrtc.org00873182014-11-25 14:03:34 +0000373void Call::SetBitrateConfig(
374 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000375 TRACE_EVENT0("webrtc", "Call::SetBitrateConfig");
henrikg91d6ede2015-09-17 00:24:34 -0700376 RTC_DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000377 if (bitrate_config.max_bitrate_bps != -1)
henrikg91d6ede2015-09-17 00:24:34 -0700378 RTC_DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
Stefan Holmere5904162015-03-26 11:11:06 +0100379 if (config_.bitrate_config.min_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000380 bitrate_config.min_bitrate_bps &&
381 (bitrate_config.start_bitrate_bps <= 0 ||
Stefan Holmere5904162015-03-26 11:11:06 +0100382 config_.bitrate_config.start_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000383 bitrate_config.start_bitrate_bps) &&
Stefan Holmere5904162015-03-26 11:11:06 +0100384 config_.bitrate_config.max_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000385 bitrate_config.max_bitrate_bps) {
386 // Nothing new to set, early abort to avoid encoder reconfigurations.
387 return;
388 }
Stefan Holmere5904162015-03-26 11:11:06 +0100389 config_.bitrate_config = bitrate_config;
Peter Boström45553ae2015-05-08 13:54:38 +0200390 SetBitrateControllerConfig(bitrate_config);
391}
392
393void Call::SetBitrateControllerConfig(
394 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
395 BitrateController* bitrate_controller =
396 channel_group_->GetBitrateController();
397 if (bitrate_config.start_bitrate_bps > 0)
398 bitrate_controller->SetStartBitrate(bitrate_config.start_bitrate_bps);
399 bitrate_controller->SetMinMaxBitrate(bitrate_config.min_bitrate_bps,
400 bitrate_config.max_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000401}
402
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000403void Call::SignalNetworkState(NetworkState state) {
404 // Take crit for entire function, it needs to be held while updating streams
405 // to guarantee a consistent state across streams.
Peter Boströmf2f82832015-05-01 13:00:41 +0200406 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000407 network_enabled_ = state == kNetworkUp;
408 {
409 ReadLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200410 for (auto& kv : video_send_ssrcs_) {
411 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000412 }
413 }
414 {
415 ReadLockScoped write_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200416 for (auto& kv : video_receive_ssrcs_) {
417 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000418 }
419 }
420}
421
pbos8fc7fa72015-07-15 08:02:58 -0700422void Call::ConfigureSync(const std::string& sync_group) {
423 // Set sync only if there was no previous one.
424 if (config_.voice_engine == nullptr || sync_group.empty())
425 return;
426
427 AudioReceiveStream* sync_audio_stream = nullptr;
428 // Find existing audio stream.
429 const auto it = sync_stream_mapping_.find(sync_group);
430 if (it != sync_stream_mapping_.end()) {
431 sync_audio_stream = it->second;
432 } else {
433 // No configured audio stream, see if we can find one.
434 for (const auto& kv : audio_receive_ssrcs_) {
435 if (kv.second->config().sync_group == sync_group) {
436 if (sync_audio_stream != nullptr) {
437 LOG(LS_WARNING) << "Attempting to sync more than one audio stream "
438 "within the same sync group. This is not "
439 "supported in the current implementation.";
440 break;
441 }
442 sync_audio_stream = kv.second;
443 }
444 }
445 }
446 if (sync_audio_stream)
447 sync_stream_mapping_[sync_group] = sync_audio_stream;
448 size_t num_synced_streams = 0;
449 for (VideoReceiveStream* video_stream : video_receive_streams_) {
450 if (video_stream->config().sync_group != sync_group)
451 continue;
452 ++num_synced_streams;
453 if (num_synced_streams > 1) {
454 // TODO(pbos): Support synchronizing more than one A/V pair.
455 // https://code.google.com/p/webrtc/issues/detail?id=4762
456 LOG(LS_WARNING) << "Attempting to sync more than one audio/video pair "
457 "within the same sync group. This is not supported in "
458 "the current implementation.";
459 }
460 // Only sync the first A/V pair within this sync group.
461 if (sync_audio_stream != nullptr && num_synced_streams == 1) {
462 video_stream->SetSyncChannel(config_.voice_engine,
463 sync_audio_stream->config().voe_channel_id);
464 } else {
465 video_stream->SetSyncChannel(config_.voice_engine, -1);
466 }
467 }
468}
469
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200470PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
471 const uint8_t* packet,
472 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000473 // TODO(pbos): Figure out what channel needs it actually.
474 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000475 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
476 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000477 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200478 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000479 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200480 for (VideoReceiveStream* stream : video_receive_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700481 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000482 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700483 if (event_log_)
484 event_log_->LogRtcpPacket(true, media_type, packet, length);
485 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000486 }
487 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200488 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000489 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200490 for (VideoSendStream* stream : video_send_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700491 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000492 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700493 if (event_log_)
494 event_log_->LogRtcpPacket(false, media_type, packet, length);
495 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000496 }
497 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000498 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000499}
500
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200501PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
502 const uint8_t* packet,
stefan68786d22015-09-08 05:36:15 -0700503 size_t length,
504 const PacketTime& packet_time) {
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000505 // Minimum RTP header size.
506 if (length < 12)
507 return DELIVERY_PACKET_ERROR;
508
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +0000509 uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(&packet[8]);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000510
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000511 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200512 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
513 auto it = audio_receive_ssrcs_.find(ssrc);
514 if (it != audio_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700515 auto status = it->second->DeliverRtp(packet, length, packet_time)
516 ? DELIVERY_OK
517 : DELIVERY_PACKET_ERROR;
518 if (status == DELIVERY_OK && event_log_)
519 event_log_->LogRtpHeader(true, media_type, packet, length);
520 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200521 }
522 }
523 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
524 auto it = video_receive_ssrcs_.find(ssrc);
525 if (it != video_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700526 auto status = it->second->DeliverRtp(packet, length, packet_time)
527 ? DELIVERY_OK
528 : DELIVERY_PACKET_ERROR;
529 if (status == DELIVERY_OK && event_log_)
530 event_log_->LogRtpHeader(true, media_type, packet, length);
531 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200532 }
533 }
534 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000535}
536
stefan68786d22015-09-08 05:36:15 -0700537PacketReceiver::DeliveryStatus Call::DeliverPacket(
538 MediaType media_type,
539 const uint8_t* packet,
540 size_t length,
541 const PacketTime& packet_time) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000542 if (RtpHeaderParser::IsRtcp(packet, length))
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200543 return DeliverRtcp(media_type, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000544
stefan68786d22015-09-08 05:36:15 -0700545 return DeliverRtp(media_type, packet, length, packet_time);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000546}
547
548} // namespace internal
549} // namespace webrtc