blob: 3ef113c16bba7615b3a071d540966205e8d33f65 [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
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +000016#include "webrtc/base/checks.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000017#include "webrtc/base/scoped_ptr.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000018#include "webrtc/base/thread_annotations.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000019#include "webrtc/call.h"
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000020#include "webrtc/common.h"
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +000021#include "webrtc/config.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000022#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +000023#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
Peter Boström45553ae2015-05-08 13:54:38 +020024#include "webrtc/modules/utility/interface/process_thread.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000025#include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
marpan@webrtc.org5b883172014-11-01 06:10:48 +000026#include "webrtc/modules/video_coding/codecs/vp9/include/vp9.h"
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +000027#include "webrtc/modules/video_render/include/video_render.h"
Peter Boström45553ae2015-05-08 13:54:38 +020028#include "webrtc/system_wrappers/interface/cpu_info.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000029#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
pbos@webrtc.org32e85282015-01-15 10:09:39 +000030#include "webrtc/system_wrappers/interface/logging.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000031#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000032#include "webrtc/system_wrappers/interface/trace.h"
pbos@webrtc.org50fe3592015-01-29 12:33:07 +000033#include "webrtc/system_wrappers/interface/trace_event.h"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020034#include "webrtc/video/audio_receive_stream.h"
ivocb04965c2015-09-09 00:09:43 -070035#include "webrtc/video/rtc_event_log.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000036#include "webrtc/video/video_receive_stream.h"
37#include "webrtc/video/video_send_stream.h"
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
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000083 private:
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020084 DeliveryStatus DeliverRtcp(MediaType media_type, const uint8_t* packet,
85 size_t length);
stefan68786d22015-09-08 05:36:15 -070086 DeliveryStatus DeliverRtp(MediaType media_type,
87 const uint8_t* packet,
88 size_t length,
89 const PacketTime& packet_time);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000090
Peter Boström45553ae2015-05-08 13:54:38 +020091 void SetBitrateControllerConfig(
92 const webrtc::Call::Config::BitrateConfig& bitrate_config);
93
pbos8fc7fa72015-07-15 08:02:58 -070094 void ConfigureSync(const std::string& sync_group)
95 EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
96
Peter Boström45553ae2015-05-08 13:54:38 +020097 const int num_cpu_cores_;
98 const rtc::scoped_ptr<ProcessThread> module_process_thread_;
99 const rtc::scoped_ptr<ChannelGroup> channel_group_;
Peter Boström45553ae2015-05-08 13:54:38 +0200100 volatile int next_channel_id_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000101 Call::Config config_;
102
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_;
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_;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200120 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_ GUARDED_BY(send_crit_);
121 std::set<VideoSendStream*> video_send_streams_ GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000122
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200123 VideoSendStream::RtpStateMap suspended_video_send_ssrcs_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000124
ivocb04965c2015-09-09 00:09:43 -0700125 RtcEventLog* event_log_;
126
henrikg3c089d72015-09-16 05:37:44 -0700127 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000128};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000129} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000130
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000131Call* Call::Create(const Call::Config& config) {
Peter Boström45553ae2015-05-08 13:54:38 +0200132 return new internal::Call(config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000133}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000134
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000135namespace internal {
136
Peter Boström45553ae2015-05-08 13:54:38 +0200137Call::Call(const Call::Config& config)
138 : num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
stefan847855b2015-09-11 09:52:15 -0700139 module_process_thread_(ProcessThread::Create("ModuleProcessThread")),
Peter Boström2251d6e2015-05-28 14:10:39 +0200140 channel_group_(new ChannelGroup(module_process_thread_.get())),
pbosd6fc47e2015-07-23 06:58:33 -0700141 next_channel_id_(0),
Peter Boström45553ae2015-05-08 13:54:38 +0200142 config_(config),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000143 network_enabled_(true),
144 receive_crit_(RWLockWrapper::CreateRWLock()),
ivocb04965c2015-09-09 00:09:43 -0700145 send_crit_(RWLockWrapper::CreateRWLock()),
146 event_log_(nullptr) {
Stefan Holmere5904162015-03-26 11:11:06 +0100147 DCHECK_GE(config.bitrate_config.min_bitrate_bps, 0);
148 DCHECK_GE(config.bitrate_config.start_bitrate_bps,
149 config.bitrate_config.min_bitrate_bps);
150 if (config.bitrate_config.max_bitrate_bps != -1) {
151 DCHECK_GE(config.bitrate_config.max_bitrate_bps,
152 config.bitrate_config.start_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000153 }
ivocb04965c2015-09-09 00:09:43 -0700154 if (config.voice_engine) {
155 VoECodec* voe_codec = VoECodec::GetInterface(config.voice_engine);
156 if (voe_codec) {
157 event_log_ = voe_codec->GetEventLog();
158 voe_codec->Release();
159 }
160 }
pbos@webrtc.org00873182014-11-25 14:03:34 +0000161
Peter Boström45553ae2015-05-08 13:54:38 +0200162 Trace::CreateTrace();
163 module_process_thread_->Start();
164
Peter Boström45553ae2015-05-08 13:54:38 +0200165 SetBitrateControllerConfig(config_.bitrate_config);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000166}
167
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000168Call::~Call() {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200169 CHECK_EQ(0u, video_send_ssrcs_.size());
170 CHECK_EQ(0u, video_send_streams_.size());
171 CHECK_EQ(0u, audio_receive_ssrcs_.size());
172 CHECK_EQ(0u, video_receive_ssrcs_.size());
173 CHECK_EQ(0u, video_receive_streams_.size());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000174
Peter Boström45553ae2015-05-08 13:54:38 +0200175 module_process_thread_->Stop();
176 Trace::ReturnTrace();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000177}
178
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000179PacketReceiver* Call::Receiver() { return this; }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000180
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200181webrtc::AudioSendStream* Call::CreateAudioSendStream(
182 const webrtc::AudioSendStream::Config& config) {
183 return nullptr;
184}
185
186void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
187}
188
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200189webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
190 const webrtc::AudioReceiveStream::Config& config) {
191 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
192 LOG(LS_INFO) << "CreateAudioReceiveStream: " << config.ToString();
193 AudioReceiveStream* receive_stream = new AudioReceiveStream(
194 channel_group_->GetRemoteBitrateEstimator(), config);
195 {
196 WriteLockScoped write_lock(*receive_crit_);
197 DCHECK(audio_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
198 audio_receive_ssrcs_.end());
199 audio_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos8fc7fa72015-07-15 08:02:58 -0700200 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200201 }
202 return receive_stream;
203}
204
205void Call::DestroyAudioReceiveStream(
206 webrtc::AudioReceiveStream* receive_stream) {
207 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
208 DCHECK(receive_stream != nullptr);
209 AudioReceiveStream* audio_receive_stream =
210 static_cast<AudioReceiveStream*>(receive_stream);
211 {
212 WriteLockScoped write_lock(*receive_crit_);
213 size_t num_deleted = audio_receive_ssrcs_.erase(
214 audio_receive_stream->config().rtp.remote_ssrc);
215 DCHECK(num_deleted == 1);
pbos8fc7fa72015-07-15 08:02:58 -0700216 const std::string& sync_group = audio_receive_stream->config().sync_group;
217 const auto it = sync_stream_mapping_.find(sync_group);
218 if (it != sync_stream_mapping_.end() &&
219 it->second == audio_receive_stream) {
220 sync_stream_mapping_.erase(it);
221 ConfigureSync(sync_group);
222 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200223 }
224 delete audio_receive_stream;
225}
226
227webrtc::VideoSendStream* Call::CreateVideoSendStream(
228 const webrtc::VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000229 const VideoEncoderConfig& encoder_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000230 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
pbos@webrtc.org32e85282015-01-15 10:09:39 +0000231 LOG(LS_INFO) << "CreateVideoSendStream: " << config.ToString();
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000232 DCHECK(!config.rtp.ssrcs.empty());
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000233
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000234 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
235 // the call has already started.
solenberge5269742015-09-08 05:13:22 -0700236 VideoSendStream* send_stream = new VideoSendStream(num_cpu_cores_,
Peter Boström45553ae2015-05-08 13:54:38 +0200237 module_process_thread_.get(), channel_group_.get(),
238 rtc::AtomicOps::Increment(&next_channel_id_), config, encoder_config,
239 suspended_video_send_ssrcs_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000240
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000241 // This needs to be taken before send_crit_ as both locks need to be held
242 // while changing network state.
Peter Boströmf2f82832015-05-01 13:00:41 +0200243 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000244 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200245 for (uint32_t ssrc : config.rtp.ssrcs) {
246 DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
247 video_send_ssrcs_[ssrc] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000248 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200249 video_send_streams_.insert(send_stream);
250
ivocb04965c2015-09-09 00:09:43 -0700251 if (event_log_)
252 event_log_->LogVideoSendStreamConfig(config);
253
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000254 if (!network_enabled_)
255 send_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000256 return send_stream;
257}
258
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000259void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000260 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000261 DCHECK(send_stream != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000262
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000263 send_stream->Stop();
264
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000265 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000266 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000267 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200268 auto it = video_send_ssrcs_.begin();
269 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000270 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
271 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200272 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000273 } else {
274 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000275 }
276 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200277 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000278 }
Peter Boström9b5f96e2015-03-26 11:25:49 +0100279 CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000280
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000281 VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates();
282
283 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
284 it != rtp_state.end();
285 ++it) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200286 suspended_video_send_ssrcs_[it->first] = it->second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000287 }
288
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000289 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000290}
291
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200292webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
293 const webrtc::VideoReceiveStream::Config& config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000294 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
pbos@webrtc.org32e85282015-01-15 10:09:39 +0000295 LOG(LS_INFO) << "CreateVideoReceiveStream: " << config.ToString();
Peter Boströmc4188fd2015-04-24 15:16:03 +0200296 VideoReceiveStream* receive_stream = new VideoReceiveStream(
pbosd6fc47e2015-07-23 06:58:33 -0700297 num_cpu_cores_, channel_group_.get(),
Peter Boström45553ae2015-05-08 13:54:38 +0200298 rtc::AtomicOps::Increment(&next_channel_id_), config,
solenberg4fbae2b2015-08-28 04:07:10 -0700299 config_.voice_engine);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000300
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000301 // This needs to be taken before receive_crit_ as both locks need to be held
302 // while changing network state.
Peter Boströmf2f82832015-05-01 13:00:41 +0200303 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000304 WriteLockScoped write_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200305 DCHECK(video_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
306 video_receive_ssrcs_.end());
307 video_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000308 // TODO(pbos): Configure different RTX payloads per receive payload.
309 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
310 config.rtp.rtx.begin();
311 if (it != config.rtp.rtx.end())
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200312 video_receive_ssrcs_[it->second.ssrc] = receive_stream;
313 video_receive_streams_.insert(receive_stream);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000314
pbos8fc7fa72015-07-15 08:02:58 -0700315 ConfigureSync(config.sync_group);
316
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000317 if (!network_enabled_)
318 receive_stream->SignalNetworkState(kNetworkDown);
pbos8fc7fa72015-07-15 08:02:58 -0700319
ivocb04965c2015-09-09 00:09:43 -0700320 if (event_log_)
321 event_log_->LogVideoReceiveStreamConfig(config);
322
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000323 return receive_stream;
324}
325
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000326void Call::DestroyVideoReceiveStream(
327 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000328 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000329 DCHECK(receive_stream != nullptr);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000330 VideoReceiveStream* receive_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000331 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000332 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000333 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
334 // separate SSRC there can be either one or two.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200335 auto it = video_receive_ssrcs_.begin();
336 while (it != video_receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000337 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000338 if (receive_stream_impl != nullptr)
339 DCHECK(receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000340 receive_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200341 video_receive_ssrcs_.erase(it++);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000342 } else {
343 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000344 }
345 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200346 video_receive_streams_.erase(receive_stream_impl);
pbos8fc7fa72015-07-15 08:02:58 -0700347 CHECK(receive_stream_impl != nullptr);
348 ConfigureSync(receive_stream_impl->config().sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000349 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000350 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000351}
352
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000353Call::Stats Call::GetStats() const {
354 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +0200355 // Fetch available send/receive bitrates.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000356 uint32_t send_bandwidth = 0;
Peter Boström45553ae2015-05-08 13:54:38 +0200357 channel_group_->GetBitrateController()->AvailableBandwidth(&send_bandwidth);
358 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000359 uint32_t recv_bandwidth = 0;
Peter Boström45553ae2015-05-08 13:54:38 +0200360 channel_group_->GetRemoteBitrateEstimator()->LatestEstimate(&ssrcs,
361 &recv_bandwidth);
362 stats.send_bandwidth_bps = send_bandwidth;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000363 stats.recv_bandwidth_bps = recv_bandwidth;
Peter Boström59d91dc2015-04-27 17:24:33 +0200364 stats.pacer_delay_ms = channel_group_->GetPacerQueuingDelayMs();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000365 {
366 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200367 for (const auto& kv : video_send_ssrcs_) {
368 int rtt_ms = kv.second->GetRtt();
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000369 if (rtt_ms > 0)
370 stats.rtt_ms = rtt_ms;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000371 }
372 }
373 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000374}
375
pbos@webrtc.org00873182014-11-25 14:03:34 +0000376void Call::SetBitrateConfig(
377 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000378 TRACE_EVENT0("webrtc", "Call::SetBitrateConfig");
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000379 DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
380 if (bitrate_config.max_bitrate_bps != -1)
381 DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
Stefan Holmere5904162015-03-26 11:11:06 +0100382 if (config_.bitrate_config.min_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000383 bitrate_config.min_bitrate_bps &&
384 (bitrate_config.start_bitrate_bps <= 0 ||
Stefan Holmere5904162015-03-26 11:11:06 +0100385 config_.bitrate_config.start_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000386 bitrate_config.start_bitrate_bps) &&
Stefan Holmere5904162015-03-26 11:11:06 +0100387 config_.bitrate_config.max_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000388 bitrate_config.max_bitrate_bps) {
389 // Nothing new to set, early abort to avoid encoder reconfigurations.
390 return;
391 }
Stefan Holmere5904162015-03-26 11:11:06 +0100392 config_.bitrate_config = bitrate_config;
Peter Boström45553ae2015-05-08 13:54:38 +0200393 SetBitrateControllerConfig(bitrate_config);
394}
395
396void Call::SetBitrateControllerConfig(
397 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
398 BitrateController* bitrate_controller =
399 channel_group_->GetBitrateController();
400 if (bitrate_config.start_bitrate_bps > 0)
401 bitrate_controller->SetStartBitrate(bitrate_config.start_bitrate_bps);
402 bitrate_controller->SetMinMaxBitrate(bitrate_config.min_bitrate_bps,
403 bitrate_config.max_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000404}
405
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000406void Call::SignalNetworkState(NetworkState state) {
407 // Take crit for entire function, it needs to be held while updating streams
408 // to guarantee a consistent state across streams.
Peter Boströmf2f82832015-05-01 13:00:41 +0200409 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000410 network_enabled_ = state == kNetworkUp;
411 {
412 ReadLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200413 for (auto& kv : video_send_ssrcs_) {
414 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000415 }
416 }
417 {
418 ReadLockScoped write_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200419 for (auto& kv : video_receive_ssrcs_) {
420 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000421 }
422 }
423}
424
pbos8fc7fa72015-07-15 08:02:58 -0700425void Call::ConfigureSync(const std::string& sync_group) {
426 // Set sync only if there was no previous one.
427 if (config_.voice_engine == nullptr || sync_group.empty())
428 return;
429
430 AudioReceiveStream* sync_audio_stream = nullptr;
431 // Find existing audio stream.
432 const auto it = sync_stream_mapping_.find(sync_group);
433 if (it != sync_stream_mapping_.end()) {
434 sync_audio_stream = it->second;
435 } else {
436 // No configured audio stream, see if we can find one.
437 for (const auto& kv : audio_receive_ssrcs_) {
438 if (kv.second->config().sync_group == sync_group) {
439 if (sync_audio_stream != nullptr) {
440 LOG(LS_WARNING) << "Attempting to sync more than one audio stream "
441 "within the same sync group. This is not "
442 "supported in the current implementation.";
443 break;
444 }
445 sync_audio_stream = kv.second;
446 }
447 }
448 }
449 if (sync_audio_stream)
450 sync_stream_mapping_[sync_group] = sync_audio_stream;
451 size_t num_synced_streams = 0;
452 for (VideoReceiveStream* video_stream : video_receive_streams_) {
453 if (video_stream->config().sync_group != sync_group)
454 continue;
455 ++num_synced_streams;
456 if (num_synced_streams > 1) {
457 // TODO(pbos): Support synchronizing more than one A/V pair.
458 // https://code.google.com/p/webrtc/issues/detail?id=4762
459 LOG(LS_WARNING) << "Attempting to sync more than one audio/video pair "
460 "within the same sync group. This is not supported in "
461 "the current implementation.";
462 }
463 // Only sync the first A/V pair within this sync group.
464 if (sync_audio_stream != nullptr && num_synced_streams == 1) {
465 video_stream->SetSyncChannel(config_.voice_engine,
466 sync_audio_stream->config().voe_channel_id);
467 } else {
468 video_stream->SetSyncChannel(config_.voice_engine, -1);
469 }
470 }
471}
472
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200473PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
474 const uint8_t* packet,
475 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000476 // TODO(pbos): Figure out what channel needs it actually.
477 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000478 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
479 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000480 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200481 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000482 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200483 for (VideoReceiveStream* stream : video_receive_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700484 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000485 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700486 if (event_log_)
487 event_log_->LogRtcpPacket(true, media_type, packet, length);
488 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000489 }
490 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200491 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000492 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200493 for (VideoSendStream* stream : video_send_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700494 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000495 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700496 if (event_log_)
497 event_log_->LogRtcpPacket(false, media_type, packet, length);
498 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000499 }
500 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000501 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000502}
503
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200504PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
505 const uint8_t* packet,
stefan68786d22015-09-08 05:36:15 -0700506 size_t length,
507 const PacketTime& packet_time) {
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000508 // Minimum RTP header size.
509 if (length < 12)
510 return DELIVERY_PACKET_ERROR;
511
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +0000512 uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(&packet[8]);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000513
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000514 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200515 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
516 auto it = audio_receive_ssrcs_.find(ssrc);
517 if (it != audio_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700518 auto status = it->second->DeliverRtp(packet, length, packet_time)
519 ? DELIVERY_OK
520 : DELIVERY_PACKET_ERROR;
521 if (status == DELIVERY_OK && event_log_)
522 event_log_->LogRtpHeader(true, media_type, packet, length);
523 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200524 }
525 }
526 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
527 auto it = video_receive_ssrcs_.find(ssrc);
528 if (it != video_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700529 auto status = it->second->DeliverRtp(packet, length, packet_time)
530 ? DELIVERY_OK
531 : DELIVERY_PACKET_ERROR;
532 if (status == DELIVERY_OK && event_log_)
533 event_log_->LogRtpHeader(true, media_type, packet, length);
534 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200535 }
536 }
537 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000538}
539
stefan68786d22015-09-08 05:36:15 -0700540PacketReceiver::DeliveryStatus Call::DeliverPacket(
541 MediaType media_type,
542 const uint8_t* packet,
543 size_t length,
544 const PacketTime& packet_time) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000545 if (RtpHeaderParser::IsRtcp(packet, length))
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200546 return DeliverRtcp(media_type, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000547
stefan68786d22015-09-08 05:36:15 -0700548 return DeliverRtp(media_type, packet, length, packet_time);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000549}
550
551} // namespace internal
552} // namespace webrtc