blob: 2c80ccff0dc038b7e23ddafc28f8b598bda3667e [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
pbos8fc7fa72015-07-15 08:02:58 -070088 void ConfigureSync(const std::string& sync_group)
89 EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
90
Peter Boström45553ae2015-05-08 13:54:38 +020091 const int num_cpu_cores_;
92 const rtc::scoped_ptr<ProcessThread> module_process_thread_;
93 const rtc::scoped_ptr<ChannelGroup> channel_group_;
Peter Boström45553ae2015-05-08 13:54:38 +020094 volatile int next_channel_id_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000095 Call::Config config_;
96
pbos@webrtc.org26c0c412014-09-03 16:17:12 +000097 // Needs to be held while write-locking |receive_crit_| or |send_crit_|. This
98 // ensures that we have a consistent network state signalled to all senders
99 // and receivers.
Peter Boströmf2f82832015-05-01 13:00:41 +0200100 rtc::CriticalSection network_enabled_crit_;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000101 bool network_enabled_ GUARDED_BY(network_enabled_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000102
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000103 rtc::scoped_ptr<RWLockWrapper> receive_crit_;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200104 std::map<uint32_t, AudioReceiveStream*> audio_receive_ssrcs_
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000105 GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200106 std::map<uint32_t, VideoReceiveStream*> video_receive_ssrcs_
107 GUARDED_BY(receive_crit_);
108 std::set<VideoReceiveStream*> video_receive_streams_
109 GUARDED_BY(receive_crit_);
pbos8fc7fa72015-07-15 08:02:58 -0700110 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
111 GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000112
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000113 rtc::scoped_ptr<RWLockWrapper> send_crit_;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200114 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_ GUARDED_BY(send_crit_);
115 std::set<VideoSendStream*> video_send_streams_ GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000116
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200117 VideoSendStream::RtpStateMap suspended_video_send_ssrcs_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000118
ivocb04965c2015-09-09 00:09:43 -0700119 RtcEventLog* event_log_;
120
henrikg3c089d72015-09-16 05:37:44 -0700121 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000122};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000123} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000124
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000125Call* Call::Create(const Call::Config& config) {
Peter Boström45553ae2015-05-08 13:54:38 +0200126 return new internal::Call(config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000127}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000128
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000129namespace internal {
130
Peter Boström45553ae2015-05-08 13:54:38 +0200131Call::Call(const Call::Config& config)
132 : num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
stefan847855b2015-09-11 09:52:15 -0700133 module_process_thread_(ProcessThread::Create("ModuleProcessThread")),
Peter Boström2251d6e2015-05-28 14:10:39 +0200134 channel_group_(new ChannelGroup(module_process_thread_.get())),
pbosd6fc47e2015-07-23 06:58:33 -0700135 next_channel_id_(0),
Peter Boström45553ae2015-05-08 13:54:38 +0200136 config_(config),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000137 network_enabled_(true),
138 receive_crit_(RWLockWrapper::CreateRWLock()),
ivocb04965c2015-09-09 00:09:43 -0700139 send_crit_(RWLockWrapper::CreateRWLock()),
140 event_log_(nullptr) {
henrikg91d6ede2015-09-17 00:24:34 -0700141 RTC_DCHECK_GE(config.bitrate_config.min_bitrate_bps, 0);
142 RTC_DCHECK_GE(config.bitrate_config.start_bitrate_bps,
143 config.bitrate_config.min_bitrate_bps);
Stefan Holmere5904162015-03-26 11:11:06 +0100144 if (config.bitrate_config.max_bitrate_bps != -1) {
henrikg91d6ede2015-09-17 00:24:34 -0700145 RTC_DCHECK_GE(config.bitrate_config.max_bitrate_bps,
146 config.bitrate_config.start_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000147 }
ivocb04965c2015-09-09 00:09:43 -0700148 if (config.voice_engine) {
149 VoECodec* voe_codec = VoECodec::GetInterface(config.voice_engine);
150 if (voe_codec) {
151 event_log_ = voe_codec->GetEventLog();
152 voe_codec->Release();
153 }
154 }
pbos@webrtc.org00873182014-11-25 14:03:34 +0000155
Peter Boström45553ae2015-05-08 13:54:38 +0200156 Trace::CreateTrace();
157 module_process_thread_->Start();
158
stefan4fbd1452015-09-28 03:57:14 -0700159 channel_group_->SetBweBitrates(config_.bitrate_config.min_bitrate_bps,
160 config_.bitrate_config.start_bitrate_bps,
161 config_.bitrate_config.max_bitrate_bps);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000162}
163
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000164Call::~Call() {
henrikg91d6ede2015-09-17 00:24:34 -0700165 RTC_CHECK_EQ(0u, video_send_ssrcs_.size());
166 RTC_CHECK_EQ(0u, video_send_streams_.size());
167 RTC_CHECK_EQ(0u, audio_receive_ssrcs_.size());
168 RTC_CHECK_EQ(0u, video_receive_ssrcs_.size());
169 RTC_CHECK_EQ(0u, video_receive_streams_.size());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000170
Peter Boström45553ae2015-05-08 13:54:38 +0200171 module_process_thread_->Stop();
172 Trace::ReturnTrace();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000173}
174
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000175PacketReceiver* Call::Receiver() { return this; }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000176
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200177webrtc::AudioSendStream* Call::CreateAudioSendStream(
178 const webrtc::AudioSendStream::Config& config) {
179 return nullptr;
180}
181
182void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
183}
184
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200185webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
186 const webrtc::AudioReceiveStream::Config& config) {
187 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
188 LOG(LS_INFO) << "CreateAudioReceiveStream: " << config.ToString();
189 AudioReceiveStream* receive_stream = new AudioReceiveStream(
190 channel_group_->GetRemoteBitrateEstimator(), config);
191 {
192 WriteLockScoped write_lock(*receive_crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700193 RTC_DCHECK(audio_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
194 audio_receive_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200195 audio_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos8fc7fa72015-07-15 08:02:58 -0700196 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200197 }
198 return receive_stream;
199}
200
201void Call::DestroyAudioReceiveStream(
202 webrtc::AudioReceiveStream* receive_stream) {
203 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
henrikg91d6ede2015-09-17 00:24:34 -0700204 RTC_DCHECK(receive_stream != nullptr);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200205 AudioReceiveStream* audio_receive_stream =
206 static_cast<AudioReceiveStream*>(receive_stream);
207 {
208 WriteLockScoped write_lock(*receive_crit_);
209 size_t num_deleted = audio_receive_ssrcs_.erase(
210 audio_receive_stream->config().rtp.remote_ssrc);
henrikg91d6ede2015-09-17 00:24:34 -0700211 RTC_DCHECK(num_deleted == 1);
pbos8fc7fa72015-07-15 08:02:58 -0700212 const std::string& sync_group = audio_receive_stream->config().sync_group;
213 const auto it = sync_stream_mapping_.find(sync_group);
214 if (it != sync_stream_mapping_.end() &&
215 it->second == audio_receive_stream) {
216 sync_stream_mapping_.erase(it);
217 ConfigureSync(sync_group);
218 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200219 }
220 delete audio_receive_stream;
221}
222
223webrtc::VideoSendStream* Call::CreateVideoSendStream(
224 const webrtc::VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000225 const VideoEncoderConfig& encoder_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000226 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
pbos@webrtc.org32e85282015-01-15 10:09:39 +0000227 LOG(LS_INFO) << "CreateVideoSendStream: " << config.ToString();
henrikg91d6ede2015-09-17 00:24:34 -0700228 RTC_DCHECK(!config.rtp.ssrcs.empty());
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000229
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000230 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
231 // the call has already started.
solenberge5269742015-09-08 05:13:22 -0700232 VideoSendStream* send_stream = new VideoSendStream(num_cpu_cores_,
Peter Boström45553ae2015-05-08 13:54:38 +0200233 module_process_thread_.get(), channel_group_.get(),
234 rtc::AtomicOps::Increment(&next_channel_id_), config, encoder_config,
235 suspended_video_send_ssrcs_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000236
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000237 // This needs to be taken before send_crit_ as both locks need to be held
238 // while changing network state.
Peter Boströmf2f82832015-05-01 13:00:41 +0200239 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000240 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200241 for (uint32_t ssrc : config.rtp.ssrcs) {
henrikg91d6ede2015-09-17 00:24:34 -0700242 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200243 video_send_ssrcs_[ssrc] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000244 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200245 video_send_streams_.insert(send_stream);
246
ivocb04965c2015-09-09 00:09:43 -0700247 if (event_log_)
248 event_log_->LogVideoSendStreamConfig(config);
249
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000250 if (!network_enabled_)
251 send_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000252 return send_stream;
253}
254
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000255void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000256 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700257 RTC_DCHECK(send_stream != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000258
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000259 send_stream->Stop();
260
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000261 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000262 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000263 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200264 auto it = video_send_ssrcs_.begin();
265 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000266 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
267 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200268 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000269 } else {
270 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000271 }
272 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200273 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000274 }
henrikg91d6ede2015-09-17 00:24:34 -0700275 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000276
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000277 VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates();
278
279 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
280 it != rtp_state.end();
281 ++it) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200282 suspended_video_send_ssrcs_[it->first] = it->second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000283 }
284
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000285 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000286}
287
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200288webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
289 const webrtc::VideoReceiveStream::Config& config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000290 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
pbos@webrtc.org32e85282015-01-15 10:09:39 +0000291 LOG(LS_INFO) << "CreateVideoReceiveStream: " << config.ToString();
Peter Boströmc4188fd2015-04-24 15:16:03 +0200292 VideoReceiveStream* receive_stream = new VideoReceiveStream(
pbosd6fc47e2015-07-23 06:58:33 -0700293 num_cpu_cores_, channel_group_.get(),
Peter Boström45553ae2015-05-08 13:54:38 +0200294 rtc::AtomicOps::Increment(&next_channel_id_), config,
solenberg4fbae2b2015-08-28 04:07:10 -0700295 config_.voice_engine);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000296
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000297 // This needs to be taken before receive_crit_ as both locks need to be held
298 // while changing network state.
Peter Boströmf2f82832015-05-01 13:00:41 +0200299 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000300 WriteLockScoped write_lock(*receive_crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700301 RTC_DCHECK(video_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
302 video_receive_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200303 video_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000304 // TODO(pbos): Configure different RTX payloads per receive payload.
305 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
306 config.rtp.rtx.begin();
307 if (it != config.rtp.rtx.end())
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200308 video_receive_ssrcs_[it->second.ssrc] = receive_stream;
309 video_receive_streams_.insert(receive_stream);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000310
pbos8fc7fa72015-07-15 08:02:58 -0700311 ConfigureSync(config.sync_group);
312
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000313 if (!network_enabled_)
314 receive_stream->SignalNetworkState(kNetworkDown);
pbos8fc7fa72015-07-15 08:02:58 -0700315
ivocb04965c2015-09-09 00:09:43 -0700316 if (event_log_)
317 event_log_->LogVideoReceiveStreamConfig(config);
318
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000319 return receive_stream;
320}
321
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000322void Call::DestroyVideoReceiveStream(
323 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000324 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
henrikg91d6ede2015-09-17 00:24:34 -0700325 RTC_DCHECK(receive_stream != nullptr);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000326 VideoReceiveStream* receive_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000327 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000328 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000329 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
330 // separate SSRC there can be either one or two.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200331 auto it = video_receive_ssrcs_.begin();
332 while (it != video_receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000333 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000334 if (receive_stream_impl != nullptr)
henrikg91d6ede2015-09-17 00:24:34 -0700335 RTC_DCHECK(receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000336 receive_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200337 video_receive_ssrcs_.erase(it++);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000338 } else {
339 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000340 }
341 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200342 video_receive_streams_.erase(receive_stream_impl);
henrikg91d6ede2015-09-17 00:24:34 -0700343 RTC_CHECK(receive_stream_impl != nullptr);
pbos8fc7fa72015-07-15 08:02:58 -0700344 ConfigureSync(receive_stream_impl->config().sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000345 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000346 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000347}
348
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000349Call::Stats Call::GetStats() const {
350 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +0200351 // Fetch available send/receive bitrates.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000352 uint32_t send_bandwidth = 0;
Peter Boström45553ae2015-05-08 13:54:38 +0200353 channel_group_->GetBitrateController()->AvailableBandwidth(&send_bandwidth);
354 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000355 uint32_t recv_bandwidth = 0;
Peter Boström45553ae2015-05-08 13:54:38 +0200356 channel_group_->GetRemoteBitrateEstimator()->LatestEstimate(&ssrcs,
357 &recv_bandwidth);
358 stats.send_bandwidth_bps = send_bandwidth;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000359 stats.recv_bandwidth_bps = recv_bandwidth;
Peter Boström59d91dc2015-04-27 17:24:33 +0200360 stats.pacer_delay_ms = channel_group_->GetPacerQueuingDelayMs();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000361 {
362 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200363 for (const auto& kv : video_send_ssrcs_) {
364 int rtt_ms = kv.second->GetRtt();
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000365 if (rtt_ms > 0)
366 stats.rtt_ms = rtt_ms;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000367 }
368 }
369 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000370}
371
pbos@webrtc.org00873182014-11-25 14:03:34 +0000372void Call::SetBitrateConfig(
373 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000374 TRACE_EVENT0("webrtc", "Call::SetBitrateConfig");
henrikg91d6ede2015-09-17 00:24:34 -0700375 RTC_DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000376 if (bitrate_config.max_bitrate_bps != -1)
henrikg91d6ede2015-09-17 00:24:34 -0700377 RTC_DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
Stefan Holmere5904162015-03-26 11:11:06 +0100378 if (config_.bitrate_config.min_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000379 bitrate_config.min_bitrate_bps &&
380 (bitrate_config.start_bitrate_bps <= 0 ||
Stefan Holmere5904162015-03-26 11:11:06 +0100381 config_.bitrate_config.start_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000382 bitrate_config.start_bitrate_bps) &&
Stefan Holmere5904162015-03-26 11:11:06 +0100383 config_.bitrate_config.max_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000384 bitrate_config.max_bitrate_bps) {
385 // Nothing new to set, early abort to avoid encoder reconfigurations.
386 return;
387 }
Stefan Holmere5904162015-03-26 11:11:06 +0100388 config_.bitrate_config = bitrate_config;
stefan4fbd1452015-09-28 03:57:14 -0700389 channel_group_->SetBweBitrates(bitrate_config.min_bitrate_bps,
390 bitrate_config.start_bitrate_bps,
391 bitrate_config.max_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000392}
393
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000394void Call::SignalNetworkState(NetworkState state) {
395 // Take crit for entire function, it needs to be held while updating streams
396 // to guarantee a consistent state across streams.
Peter Boströmf2f82832015-05-01 13:00:41 +0200397 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000398 network_enabled_ = state == kNetworkUp;
399 {
400 ReadLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200401 for (auto& kv : video_send_ssrcs_) {
402 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000403 }
404 }
405 {
406 ReadLockScoped write_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200407 for (auto& kv : video_receive_ssrcs_) {
408 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000409 }
410 }
411}
412
pbos8fc7fa72015-07-15 08:02:58 -0700413void Call::ConfigureSync(const std::string& sync_group) {
414 // Set sync only if there was no previous one.
415 if (config_.voice_engine == nullptr || sync_group.empty())
416 return;
417
418 AudioReceiveStream* sync_audio_stream = nullptr;
419 // Find existing audio stream.
420 const auto it = sync_stream_mapping_.find(sync_group);
421 if (it != sync_stream_mapping_.end()) {
422 sync_audio_stream = it->second;
423 } else {
424 // No configured audio stream, see if we can find one.
425 for (const auto& kv : audio_receive_ssrcs_) {
426 if (kv.second->config().sync_group == sync_group) {
427 if (sync_audio_stream != nullptr) {
428 LOG(LS_WARNING) << "Attempting to sync more than one audio stream "
429 "within the same sync group. This is not "
430 "supported in the current implementation.";
431 break;
432 }
433 sync_audio_stream = kv.second;
434 }
435 }
436 }
437 if (sync_audio_stream)
438 sync_stream_mapping_[sync_group] = sync_audio_stream;
439 size_t num_synced_streams = 0;
440 for (VideoReceiveStream* video_stream : video_receive_streams_) {
441 if (video_stream->config().sync_group != sync_group)
442 continue;
443 ++num_synced_streams;
444 if (num_synced_streams > 1) {
445 // TODO(pbos): Support synchronizing more than one A/V pair.
446 // https://code.google.com/p/webrtc/issues/detail?id=4762
447 LOG(LS_WARNING) << "Attempting to sync more than one audio/video pair "
448 "within the same sync group. This is not supported in "
449 "the current implementation.";
450 }
451 // Only sync the first A/V pair within this sync group.
452 if (sync_audio_stream != nullptr && num_synced_streams == 1) {
453 video_stream->SetSyncChannel(config_.voice_engine,
454 sync_audio_stream->config().voe_channel_id);
455 } else {
456 video_stream->SetSyncChannel(config_.voice_engine, -1);
457 }
458 }
459}
460
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200461PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
462 const uint8_t* packet,
463 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000464 // TODO(pbos): Figure out what channel needs it actually.
465 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000466 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
467 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000468 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200469 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000470 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200471 for (VideoReceiveStream* stream : video_receive_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700472 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000473 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700474 if (event_log_)
475 event_log_->LogRtcpPacket(true, media_type, packet, length);
476 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000477 }
478 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200479 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000480 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200481 for (VideoSendStream* stream : video_send_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700482 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000483 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700484 if (event_log_)
485 event_log_->LogRtcpPacket(false, media_type, packet, length);
486 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000487 }
488 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000489 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000490}
491
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200492PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
493 const uint8_t* packet,
stefan68786d22015-09-08 05:36:15 -0700494 size_t length,
495 const PacketTime& packet_time) {
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000496 // Minimum RTP header size.
497 if (length < 12)
498 return DELIVERY_PACKET_ERROR;
499
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +0000500 uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(&packet[8]);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000501
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000502 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200503 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
504 auto it = audio_receive_ssrcs_.find(ssrc);
505 if (it != audio_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700506 auto status = it->second->DeliverRtp(packet, length, packet_time)
507 ? DELIVERY_OK
508 : DELIVERY_PACKET_ERROR;
509 if (status == DELIVERY_OK && event_log_)
510 event_log_->LogRtpHeader(true, media_type, packet, length);
511 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200512 }
513 }
514 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
515 auto it = video_receive_ssrcs_.find(ssrc);
516 if (it != video_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700517 auto status = it->second->DeliverRtp(packet, length, packet_time)
518 ? DELIVERY_OK
519 : DELIVERY_PACKET_ERROR;
520 if (status == DELIVERY_OK && event_log_)
521 event_log_->LogRtpHeader(true, media_type, packet, length);
522 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200523 }
524 }
525 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000526}
527
stefan68786d22015-09-08 05:36:15 -0700528PacketReceiver::DeliveryStatus Call::DeliverPacket(
529 MediaType media_type,
530 const uint8_t* packet,
531 size_t length,
532 const PacketTime& packet_time) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000533 if (RtpHeaderParser::IsRtcp(packet, length))
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200534 return DeliverRtcp(media_type, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000535
stefan68786d22015-09-08 05:36:15 -0700536 return DeliverRtp(media_type, packet, length, packet_time);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000537}
538
539} // namespace internal
540} // namespace webrtc