blob: a32a8239437374293405e735a0f117c780ab81b4 [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
stefanc1aeaf02015-10-15 07:26:07 -070080 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
81
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000082 private:
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020083 DeliveryStatus DeliverRtcp(MediaType media_type, const uint8_t* packet,
84 size_t length);
stefan68786d22015-09-08 05:36:15 -070085 DeliveryStatus DeliverRtp(MediaType media_type,
86 const uint8_t* packet,
87 size_t length,
88 const PacketTime& packet_time);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000089
pbos8fc7fa72015-07-15 08:02:58 -070090 void ConfigureSync(const std::string& sync_group)
91 EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
92
Peter Boström45553ae2015-05-08 13:54:38 +020093 const int num_cpu_cores_;
94 const rtc::scoped_ptr<ProcessThread> module_process_thread_;
95 const rtc::scoped_ptr<ChannelGroup> channel_group_;
Peter Boström45553ae2015-05-08 13:54:38 +020096 volatile int next_channel_id_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000097 Call::Config config_;
98
pbos@webrtc.org26c0c412014-09-03 16:17:12 +000099 // Needs to be held while write-locking |receive_crit_| or |send_crit_|. This
100 // ensures that we have a consistent network state signalled to all senders
101 // and receivers.
Peter Boströmf2f82832015-05-01 13:00:41 +0200102 rtc::CriticalSection network_enabled_crit_;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000103 bool network_enabled_ GUARDED_BY(network_enabled_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000104
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000105 rtc::scoped_ptr<RWLockWrapper> receive_crit_;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200106 std::map<uint32_t, AudioReceiveStream*> audio_receive_ssrcs_
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000107 GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200108 std::map<uint32_t, VideoReceiveStream*> video_receive_ssrcs_
109 GUARDED_BY(receive_crit_);
110 std::set<VideoReceiveStream*> video_receive_streams_
111 GUARDED_BY(receive_crit_);
pbos8fc7fa72015-07-15 08:02:58 -0700112 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
113 GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000114
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000115 rtc::scoped_ptr<RWLockWrapper> send_crit_;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200116 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_ GUARDED_BY(send_crit_);
117 std::set<VideoSendStream*> video_send_streams_ GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000118
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200119 VideoSendStream::RtpStateMap suspended_video_send_ssrcs_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000120
ivocb04965c2015-09-09 00:09:43 -0700121 RtcEventLog* event_log_;
122
henrikg3c089d72015-09-16 05:37:44 -0700123 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000124};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000125} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000126
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000127Call* Call::Create(const Call::Config& config) {
Peter Boström45553ae2015-05-08 13:54:38 +0200128 return new internal::Call(config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000129}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000130
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000131namespace internal {
132
Peter Boström45553ae2015-05-08 13:54:38 +0200133Call::Call(const Call::Config& config)
134 : num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
stefan847855b2015-09-11 09:52:15 -0700135 module_process_thread_(ProcessThread::Create("ModuleProcessThread")),
Peter Boström2251d6e2015-05-28 14:10:39 +0200136 channel_group_(new ChannelGroup(module_process_thread_.get())),
pbosd6fc47e2015-07-23 06:58:33 -0700137 next_channel_id_(0),
Peter Boström45553ae2015-05-08 13:54:38 +0200138 config_(config),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000139 network_enabled_(true),
140 receive_crit_(RWLockWrapper::CreateRWLock()),
ivocb04965c2015-09-09 00:09:43 -0700141 send_crit_(RWLockWrapper::CreateRWLock()),
142 event_log_(nullptr) {
henrikg91d6ede2015-09-17 00:24:34 -0700143 RTC_DCHECK_GE(config.bitrate_config.min_bitrate_bps, 0);
144 RTC_DCHECK_GE(config.bitrate_config.start_bitrate_bps,
145 config.bitrate_config.min_bitrate_bps);
Stefan Holmere5904162015-03-26 11:11:06 +0100146 if (config.bitrate_config.max_bitrate_bps != -1) {
henrikg91d6ede2015-09-17 00:24:34 -0700147 RTC_DCHECK_GE(config.bitrate_config.max_bitrate_bps,
148 config.bitrate_config.start_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000149 }
ivocb04965c2015-09-09 00:09:43 -0700150 if (config.voice_engine) {
151 VoECodec* voe_codec = VoECodec::GetInterface(config.voice_engine);
152 if (voe_codec) {
153 event_log_ = voe_codec->GetEventLog();
154 voe_codec->Release();
155 }
156 }
pbos@webrtc.org00873182014-11-25 14:03:34 +0000157
Peter Boström45553ae2015-05-08 13:54:38 +0200158 Trace::CreateTrace();
159 module_process_thread_->Start();
160
stefan4fbd1452015-09-28 03:57:14 -0700161 channel_group_->SetBweBitrates(config_.bitrate_config.min_bitrate_bps,
162 config_.bitrate_config.start_bitrate_bps,
163 config_.bitrate_config.max_bitrate_bps);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000164}
165
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000166Call::~Call() {
henrikg91d6ede2015-09-17 00:24:34 -0700167 RTC_CHECK_EQ(0u, video_send_ssrcs_.size());
168 RTC_CHECK_EQ(0u, video_send_streams_.size());
169 RTC_CHECK_EQ(0u, audio_receive_ssrcs_.size());
170 RTC_CHECK_EQ(0u, video_receive_ssrcs_.size());
171 RTC_CHECK_EQ(0u, video_receive_streams_.size());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000172
Peter Boström45553ae2015-05-08 13:54:38 +0200173 module_process_thread_->Stop();
174 Trace::ReturnTrace();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000175}
176
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000177PacketReceiver* Call::Receiver() { return this; }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000178
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200179webrtc::AudioSendStream* Call::CreateAudioSendStream(
180 const webrtc::AudioSendStream::Config& config) {
pbosa2f30de2015-10-15 05:22:13 -0700181 // TODO(pbos): When adding AudioSendStream, add both TRACE_EVENT0 and config
182 // logging to AudioSendStream constructor.
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200183 return nullptr;
184}
185
186void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
pbosa2f30de2015-10-15 05:22:13 -0700187 // TODO(pbos): When adding AudioSendStream, add both TRACE_EVENT0 and config
188 // logging to AudioSendStream destructor.
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200189}
190
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200191webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
192 const webrtc::AudioReceiveStream::Config& config) {
193 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200194 AudioReceiveStream* receive_stream = new AudioReceiveStream(
195 channel_group_->GetRemoteBitrateEstimator(), config);
196 {
197 WriteLockScoped write_lock(*receive_crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700198 RTC_DCHECK(audio_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
199 audio_receive_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200200 audio_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos8fc7fa72015-07-15 08:02:58 -0700201 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200202 }
203 return receive_stream;
204}
205
206void Call::DestroyAudioReceiveStream(
207 webrtc::AudioReceiveStream* receive_stream) {
208 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
henrikg91d6ede2015-09-17 00:24:34 -0700209 RTC_DCHECK(receive_stream != nullptr);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200210 AudioReceiveStream* audio_receive_stream =
211 static_cast<AudioReceiveStream*>(receive_stream);
212 {
213 WriteLockScoped write_lock(*receive_crit_);
214 size_t num_deleted = audio_receive_ssrcs_.erase(
215 audio_receive_stream->config().rtp.remote_ssrc);
henrikg91d6ede2015-09-17 00:24:34 -0700216 RTC_DCHECK(num_deleted == 1);
pbos8fc7fa72015-07-15 08:02:58 -0700217 const std::string& sync_group = audio_receive_stream->config().sync_group;
218 const auto it = sync_stream_mapping_.find(sync_group);
219 if (it != sync_stream_mapping_.end() &&
220 it->second == audio_receive_stream) {
221 sync_stream_mapping_.erase(it);
222 ConfigureSync(sync_group);
223 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200224 }
225 delete audio_receive_stream;
226}
227
228webrtc::VideoSendStream* Call::CreateVideoSendStream(
229 const webrtc::VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000230 const VideoEncoderConfig& encoder_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000231 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000232
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000233 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
234 // the call has already started.
solenberge5269742015-09-08 05:13:22 -0700235 VideoSendStream* send_stream = new VideoSendStream(num_cpu_cores_,
Peter Boström45553ae2015-05-08 13:54:38 +0200236 module_process_thread_.get(), channel_group_.get(),
237 rtc::AtomicOps::Increment(&next_channel_id_), config, encoder_config,
238 suspended_video_send_ssrcs_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000239
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000240 // This needs to be taken before send_crit_ as both locks need to be held
241 // while changing network state.
Peter Boströmf2f82832015-05-01 13:00:41 +0200242 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000243 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200244 for (uint32_t ssrc : config.rtp.ssrcs) {
henrikg91d6ede2015-09-17 00:24:34 -0700245 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200246 video_send_ssrcs_[ssrc] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000247 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200248 video_send_streams_.insert(send_stream);
249
ivocb04965c2015-09-09 00:09:43 -0700250 if (event_log_)
251 event_log_->LogVideoSendStreamConfig(config);
252
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000253 if (!network_enabled_)
254 send_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000255 return send_stream;
256}
257
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000258void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000259 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700260 RTC_DCHECK(send_stream != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000261
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000262 send_stream->Stop();
263
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000264 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000265 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000266 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200267 auto it = video_send_ssrcs_.begin();
268 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000269 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
270 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200271 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000272 } else {
273 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000274 }
275 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200276 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000277 }
henrikg91d6ede2015-09-17 00:24:34 -0700278 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000279
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000280 VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates();
281
282 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
283 it != rtp_state.end();
284 ++it) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200285 suspended_video_send_ssrcs_[it->first] = it->second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000286 }
287
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000288 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000289}
290
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200291webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
292 const webrtc::VideoReceiveStream::Config& config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000293 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
Peter Boströmc4188fd2015-04-24 15:16:03 +0200294 VideoReceiveStream* receive_stream = new VideoReceiveStream(
pbosd6fc47e2015-07-23 06:58:33 -0700295 num_cpu_cores_, channel_group_.get(),
Peter Boström45553ae2015-05-08 13:54:38 +0200296 rtc::AtomicOps::Increment(&next_channel_id_), config,
solenberg4fbae2b2015-08-28 04:07:10 -0700297 config_.voice_engine);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000298
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000299 // This needs to be taken before receive_crit_ as both locks need to be held
300 // while changing network state.
Peter Boströmf2f82832015-05-01 13:00:41 +0200301 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000302 WriteLockScoped write_lock(*receive_crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700303 RTC_DCHECK(video_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
304 video_receive_ssrcs_.end());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200305 video_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000306 // TODO(pbos): Configure different RTX payloads per receive payload.
307 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
308 config.rtp.rtx.begin();
309 if (it != config.rtp.rtx.end())
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200310 video_receive_ssrcs_[it->second.ssrc] = receive_stream;
311 video_receive_streams_.insert(receive_stream);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000312
pbos8fc7fa72015-07-15 08:02:58 -0700313 ConfigureSync(config.sync_group);
314
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000315 if (!network_enabled_)
316 receive_stream->SignalNetworkState(kNetworkDown);
pbos8fc7fa72015-07-15 08:02:58 -0700317
ivocb04965c2015-09-09 00:09:43 -0700318 if (event_log_)
319 event_log_->LogVideoReceiveStreamConfig(config);
320
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000321 return receive_stream;
322}
323
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000324void Call::DestroyVideoReceiveStream(
325 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000326 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
henrikg91d6ede2015-09-17 00:24:34 -0700327 RTC_DCHECK(receive_stream != nullptr);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000328 VideoReceiveStream* receive_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000329 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000330 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000331 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
332 // separate SSRC there can be either one or two.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200333 auto it = video_receive_ssrcs_.begin();
334 while (it != video_receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000335 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000336 if (receive_stream_impl != nullptr)
henrikg91d6ede2015-09-17 00:24:34 -0700337 RTC_DCHECK(receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000338 receive_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200339 video_receive_ssrcs_.erase(it++);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000340 } else {
341 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000342 }
343 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200344 video_receive_streams_.erase(receive_stream_impl);
henrikg91d6ede2015-09-17 00:24:34 -0700345 RTC_CHECK(receive_stream_impl != nullptr);
pbos8fc7fa72015-07-15 08:02:58 -0700346 ConfigureSync(receive_stream_impl->config().sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000347 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000348 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000349}
350
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000351Call::Stats Call::GetStats() const {
352 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +0200353 // Fetch available send/receive bitrates.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000354 uint32_t send_bandwidth = 0;
Peter Boström45553ae2015-05-08 13:54:38 +0200355 channel_group_->GetBitrateController()->AvailableBandwidth(&send_bandwidth);
356 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000357 uint32_t recv_bandwidth = 0;
Peter Boström45553ae2015-05-08 13:54:38 +0200358 channel_group_->GetRemoteBitrateEstimator()->LatestEstimate(&ssrcs,
359 &recv_bandwidth);
360 stats.send_bandwidth_bps = send_bandwidth;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000361 stats.recv_bandwidth_bps = recv_bandwidth;
Peter Boström59d91dc2015-04-27 17:24:33 +0200362 stats.pacer_delay_ms = channel_group_->GetPacerQueuingDelayMs();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000363 {
364 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200365 for (const auto& kv : video_send_ssrcs_) {
366 int rtt_ms = kv.second->GetRtt();
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000367 if (rtt_ms > 0)
368 stats.rtt_ms = rtt_ms;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000369 }
370 }
371 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000372}
373
pbos@webrtc.org00873182014-11-25 14:03:34 +0000374void Call::SetBitrateConfig(
375 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000376 TRACE_EVENT0("webrtc", "Call::SetBitrateConfig");
henrikg91d6ede2015-09-17 00:24:34 -0700377 RTC_DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000378 if (bitrate_config.max_bitrate_bps != -1)
henrikg91d6ede2015-09-17 00:24:34 -0700379 RTC_DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
Stefan Holmere5904162015-03-26 11:11:06 +0100380 if (config_.bitrate_config.min_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000381 bitrate_config.min_bitrate_bps &&
382 (bitrate_config.start_bitrate_bps <= 0 ||
Stefan Holmere5904162015-03-26 11:11:06 +0100383 config_.bitrate_config.start_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000384 bitrate_config.start_bitrate_bps) &&
Stefan Holmere5904162015-03-26 11:11:06 +0100385 config_.bitrate_config.max_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000386 bitrate_config.max_bitrate_bps) {
387 // Nothing new to set, early abort to avoid encoder reconfigurations.
388 return;
389 }
Stefan Holmere5904162015-03-26 11:11:06 +0100390 config_.bitrate_config = bitrate_config;
stefan4fbd1452015-09-28 03:57:14 -0700391 channel_group_->SetBweBitrates(bitrate_config.min_bitrate_bps,
392 bitrate_config.start_bitrate_bps,
393 bitrate_config.max_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000394}
395
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000396void Call::SignalNetworkState(NetworkState state) {
397 // Take crit for entire function, it needs to be held while updating streams
398 // to guarantee a consistent state across streams.
Peter Boströmf2f82832015-05-01 13:00:41 +0200399 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000400 network_enabled_ = state == kNetworkUp;
stefan457a61d2015-10-14 03:12:59 -0700401 channel_group_->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000402 {
403 ReadLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200404 for (auto& kv : video_send_ssrcs_) {
405 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000406 }
407 }
408 {
409 ReadLockScoped write_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200410 for (auto& kv : video_receive_ssrcs_) {
411 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000412 }
413 }
414}
415
stefanc1aeaf02015-10-15 07:26:07 -0700416void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
417 channel_group_->OnSentPacket(sent_packet);
418}
419
pbos8fc7fa72015-07-15 08:02:58 -0700420void Call::ConfigureSync(const std::string& sync_group) {
421 // Set sync only if there was no previous one.
422 if (config_.voice_engine == nullptr || sync_group.empty())
423 return;
424
425 AudioReceiveStream* sync_audio_stream = nullptr;
426 // Find existing audio stream.
427 const auto it = sync_stream_mapping_.find(sync_group);
428 if (it != sync_stream_mapping_.end()) {
429 sync_audio_stream = it->second;
430 } else {
431 // No configured audio stream, see if we can find one.
432 for (const auto& kv : audio_receive_ssrcs_) {
433 if (kv.second->config().sync_group == sync_group) {
434 if (sync_audio_stream != nullptr) {
435 LOG(LS_WARNING) << "Attempting to sync more than one audio stream "
436 "within the same sync group. This is not "
437 "supported in the current implementation.";
438 break;
439 }
440 sync_audio_stream = kv.second;
441 }
442 }
443 }
444 if (sync_audio_stream)
445 sync_stream_mapping_[sync_group] = sync_audio_stream;
446 size_t num_synced_streams = 0;
447 for (VideoReceiveStream* video_stream : video_receive_streams_) {
448 if (video_stream->config().sync_group != sync_group)
449 continue;
450 ++num_synced_streams;
451 if (num_synced_streams > 1) {
452 // TODO(pbos): Support synchronizing more than one A/V pair.
453 // https://code.google.com/p/webrtc/issues/detail?id=4762
454 LOG(LS_WARNING) << "Attempting to sync more than one audio/video pair "
455 "within the same sync group. This is not supported in "
456 "the current implementation.";
457 }
458 // Only sync the first A/V pair within this sync group.
459 if (sync_audio_stream != nullptr && num_synced_streams == 1) {
460 video_stream->SetSyncChannel(config_.voice_engine,
461 sync_audio_stream->config().voe_channel_id);
462 } else {
463 video_stream->SetSyncChannel(config_.voice_engine, -1);
464 }
465 }
466}
467
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200468PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
469 const uint8_t* packet,
470 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000471 // TODO(pbos): Figure out what channel needs it actually.
472 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000473 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
474 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000475 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200476 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000477 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200478 for (VideoReceiveStream* stream : video_receive_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700479 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000480 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700481 if (event_log_)
482 event_log_->LogRtcpPacket(true, media_type, packet, length);
483 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000484 }
485 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200486 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000487 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200488 for (VideoSendStream* stream : video_send_streams_) {
ivocb04965c2015-09-09 00:09:43 -0700489 if (stream->DeliverRtcp(packet, length)) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000490 rtcp_delivered = true;
ivocb04965c2015-09-09 00:09:43 -0700491 if (event_log_)
492 event_log_->LogRtcpPacket(false, media_type, packet, length);
493 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000494 }
495 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000496 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000497}
498
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200499PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
500 const uint8_t* packet,
stefan68786d22015-09-08 05:36:15 -0700501 size_t length,
502 const PacketTime& packet_time) {
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000503 // Minimum RTP header size.
504 if (length < 12)
505 return DELIVERY_PACKET_ERROR;
506
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +0000507 uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(&packet[8]);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000508
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000509 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200510 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
511 auto it = audio_receive_ssrcs_.find(ssrc);
512 if (it != audio_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700513 auto status = it->second->DeliverRtp(packet, length, packet_time)
514 ? DELIVERY_OK
515 : DELIVERY_PACKET_ERROR;
516 if (status == DELIVERY_OK && event_log_)
517 event_log_->LogRtpHeader(true, media_type, packet, length);
518 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200519 }
520 }
521 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
522 auto it = video_receive_ssrcs_.find(ssrc);
523 if (it != video_receive_ssrcs_.end()) {
ivocb04965c2015-09-09 00:09:43 -0700524 auto status = it->second->DeliverRtp(packet, length, packet_time)
525 ? DELIVERY_OK
526 : DELIVERY_PACKET_ERROR;
527 if (status == DELIVERY_OK && event_log_)
528 event_log_->LogRtpHeader(true, media_type, packet, length);
529 return status;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200530 }
531 }
532 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000533}
534
stefan68786d22015-09-08 05:36:15 -0700535PacketReceiver::DeliveryStatus Call::DeliverPacket(
536 MediaType media_type,
537 const uint8_t* packet,
538 size_t length,
539 const PacketTime& packet_time) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000540 if (RtpHeaderParser::IsRtcp(packet, length))
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200541 return DeliverRtcp(media_type, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000542
stefan68786d22015-09-08 05:36:15 -0700543 return DeliverRtp(media_type, packet, length, packet_time);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000544}
545
546} // namespace internal
547} // namespace webrtc