blob: f3a5db4735d65f26a1fd017e2cdc8f437392de0c [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"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000035#include "webrtc/video/video_receive_stream.h"
36#include "webrtc/video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000037
38namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000039
pbos@webrtc.orga73a6782014-10-14 11:52:10 +000040const int Call::Config::kDefaultStartBitrateBps = 300000;
41
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000042namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000043
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000044class Call : public webrtc::Call, public PacketReceiver {
45 public:
Peter Boström45553ae2015-05-08 13:54:38 +020046 explicit Call(const Call::Config& config);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000047 virtual ~Call();
48
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000049 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000050
Fredrik Solenberg04f49312015-06-08 13:04:56 +020051 webrtc::AudioSendStream* CreateAudioSendStream(
52 const webrtc::AudioSendStream::Config& config) override;
53 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
54
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020055 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
56 const webrtc::AudioReceiveStream::Config& config) override;
57 void DestroyAudioReceiveStream(
58 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000059
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020060 webrtc::VideoSendStream* CreateVideoSendStream(
61 const webrtc::VideoSendStream::Config& config,
62 const VideoEncoderConfig& encoder_config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000063 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000064
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020065 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
66 const webrtc::VideoReceiveStream::Config& config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000067 void DestroyVideoReceiveStream(
68 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000069
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000070 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000071
stefan68786d22015-09-08 05:36:15 -070072 DeliveryStatus DeliverPacket(MediaType media_type,
73 const uint8_t* packet,
74 size_t length,
75 const PacketTime& packet_time) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000076
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000077 void SetBitrateConfig(
78 const webrtc::Call::Config::BitrateConfig& bitrate_config) override;
79 void SignalNetworkState(NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +000080
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000081 private:
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020082 DeliveryStatus DeliverRtcp(MediaType media_type, const uint8_t* packet,
83 size_t length);
stefan68786d22015-09-08 05:36:15 -070084 DeliveryStatus DeliverRtp(MediaType media_type,
85 const uint8_t* packet,
86 size_t length,
87 const PacketTime& packet_time);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000088
Peter Boström45553ae2015-05-08 13:54:38 +020089 void SetBitrateControllerConfig(
90 const webrtc::Call::Config::BitrateConfig& bitrate_config);
91
pbos8fc7fa72015-07-15 08:02:58 -070092 void ConfigureSync(const std::string& sync_group)
93 EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
94
Peter Boström45553ae2015-05-08 13:54:38 +020095 const int num_cpu_cores_;
96 const rtc::scoped_ptr<ProcessThread> module_process_thread_;
97 const rtc::scoped_ptr<ChannelGroup> channel_group_;
Peter Boström45553ae2015-05-08 13:54:38 +020098 volatile int next_channel_id_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000099 Call::Config config_;
100
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000101 // Needs to be held while write-locking |receive_crit_| or |send_crit_|. This
102 // ensures that we have a consistent network state signalled to all senders
103 // and receivers.
Peter Boströmf2f82832015-05-01 13:00:41 +0200104 rtc::CriticalSection network_enabled_crit_;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000105 bool network_enabled_ GUARDED_BY(network_enabled_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000106
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000107 rtc::scoped_ptr<RWLockWrapper> receive_crit_;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200108 std::map<uint32_t, AudioReceiveStream*> audio_receive_ssrcs_
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000109 GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200110 std::map<uint32_t, VideoReceiveStream*> video_receive_ssrcs_
111 GUARDED_BY(receive_crit_);
112 std::set<VideoReceiveStream*> video_receive_streams_
113 GUARDED_BY(receive_crit_);
pbos8fc7fa72015-07-15 08:02:58 -0700114 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
115 GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000116
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000117 rtc::scoped_ptr<RWLockWrapper> send_crit_;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200118 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_ GUARDED_BY(send_crit_);
119 std::set<VideoSendStream*> video_send_streams_ GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000120
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200121 VideoSendStream::RtpStateMap suspended_video_send_ssrcs_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000122
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000123 DISALLOW_COPY_AND_ASSIGN(Call);
124};
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()),
135 module_process_thread_(ProcessThread::Create()),
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()),
Peter Boström45553ae2015-05-08 13:54:38 +0200141 send_crit_(RWLockWrapper::CreateRWLock()) {
Stefan Holmere5904162015-03-26 11:11:06 +0100142 DCHECK_GE(config.bitrate_config.min_bitrate_bps, 0);
143 DCHECK_GE(config.bitrate_config.start_bitrate_bps,
144 config.bitrate_config.min_bitrate_bps);
145 if (config.bitrate_config.max_bitrate_bps != -1) {
146 DCHECK_GE(config.bitrate_config.max_bitrate_bps,
147 config.bitrate_config.start_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000148 }
149
Peter Boström45553ae2015-05-08 13:54:38 +0200150 Trace::CreateTrace();
151 module_process_thread_->Start();
152
Peter Boström45553ae2015-05-08 13:54:38 +0200153 SetBitrateControllerConfig(config_.bitrate_config);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000154}
155
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000156Call::~Call() {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200157 CHECK_EQ(0u, video_send_ssrcs_.size());
158 CHECK_EQ(0u, video_send_streams_.size());
159 CHECK_EQ(0u, audio_receive_ssrcs_.size());
160 CHECK_EQ(0u, video_receive_ssrcs_.size());
161 CHECK_EQ(0u, video_receive_streams_.size());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000162
Peter Boström45553ae2015-05-08 13:54:38 +0200163 module_process_thread_->Stop();
164 Trace::ReturnTrace();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000165}
166
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000167PacketReceiver* Call::Receiver() { return this; }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000168
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200169webrtc::AudioSendStream* Call::CreateAudioSendStream(
170 const webrtc::AudioSendStream::Config& config) {
171 return nullptr;
172}
173
174void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
175}
176
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200177webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
178 const webrtc::AudioReceiveStream::Config& config) {
179 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
180 LOG(LS_INFO) << "CreateAudioReceiveStream: " << config.ToString();
181 AudioReceiveStream* receive_stream = new AudioReceiveStream(
182 channel_group_->GetRemoteBitrateEstimator(), config);
183 {
184 WriteLockScoped write_lock(*receive_crit_);
185 DCHECK(audio_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
186 audio_receive_ssrcs_.end());
187 audio_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos8fc7fa72015-07-15 08:02:58 -0700188 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200189 }
190 return receive_stream;
191}
192
193void Call::DestroyAudioReceiveStream(
194 webrtc::AudioReceiveStream* receive_stream) {
195 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
196 DCHECK(receive_stream != nullptr);
197 AudioReceiveStream* audio_receive_stream =
198 static_cast<AudioReceiveStream*>(receive_stream);
199 {
200 WriteLockScoped write_lock(*receive_crit_);
201 size_t num_deleted = audio_receive_ssrcs_.erase(
202 audio_receive_stream->config().rtp.remote_ssrc);
203 DCHECK(num_deleted == 1);
pbos8fc7fa72015-07-15 08:02:58 -0700204 const std::string& sync_group = audio_receive_stream->config().sync_group;
205 const auto it = sync_stream_mapping_.find(sync_group);
206 if (it != sync_stream_mapping_.end() &&
207 it->second == audio_receive_stream) {
208 sync_stream_mapping_.erase(it);
209 ConfigureSync(sync_group);
210 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200211 }
212 delete audio_receive_stream;
213}
214
215webrtc::VideoSendStream* Call::CreateVideoSendStream(
216 const webrtc::VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000217 const VideoEncoderConfig& encoder_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000218 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
pbos@webrtc.org32e85282015-01-15 10:09:39 +0000219 LOG(LS_INFO) << "CreateVideoSendStream: " << config.ToString();
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000220 DCHECK(!config.rtp.ssrcs.empty());
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000221
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000222 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
223 // the call has already started.
solenberge5269742015-09-08 05:13:22 -0700224 VideoSendStream* send_stream = new VideoSendStream(num_cpu_cores_,
Peter Boström45553ae2015-05-08 13:54:38 +0200225 module_process_thread_.get(), channel_group_.get(),
226 rtc::AtomicOps::Increment(&next_channel_id_), config, encoder_config,
227 suspended_video_send_ssrcs_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000228
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000229 // This needs to be taken before send_crit_ as both locks need to be held
230 // while changing network state.
Peter Boströmf2f82832015-05-01 13:00:41 +0200231 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000232 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200233 for (uint32_t ssrc : config.rtp.ssrcs) {
234 DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
235 video_send_ssrcs_[ssrc] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000236 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200237 video_send_streams_.insert(send_stream);
238
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000239 if (!network_enabled_)
240 send_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000241 return send_stream;
242}
243
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000244void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000245 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000246 DCHECK(send_stream != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000247
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000248 send_stream->Stop();
249
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000250 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000251 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000252 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200253 auto it = video_send_ssrcs_.begin();
254 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000255 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
256 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200257 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000258 } else {
259 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000260 }
261 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200262 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000263 }
Peter Boström9b5f96e2015-03-26 11:25:49 +0100264 CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000265
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000266 VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates();
267
268 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
269 it != rtp_state.end();
270 ++it) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200271 suspended_video_send_ssrcs_[it->first] = it->second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000272 }
273
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000274 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000275}
276
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200277webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
278 const webrtc::VideoReceiveStream::Config& config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000279 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
pbos@webrtc.org32e85282015-01-15 10:09:39 +0000280 LOG(LS_INFO) << "CreateVideoReceiveStream: " << config.ToString();
Peter Boströmc4188fd2015-04-24 15:16:03 +0200281 VideoReceiveStream* receive_stream = new VideoReceiveStream(
pbosd6fc47e2015-07-23 06:58:33 -0700282 num_cpu_cores_, channel_group_.get(),
Peter Boström45553ae2015-05-08 13:54:38 +0200283 rtc::AtomicOps::Increment(&next_channel_id_), config,
solenberg4fbae2b2015-08-28 04:07:10 -0700284 config_.voice_engine);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000285
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000286 // This needs to be taken before receive_crit_ as both locks need to be held
287 // while changing network state.
Peter Boströmf2f82832015-05-01 13:00:41 +0200288 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000289 WriteLockScoped write_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200290 DCHECK(video_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
291 video_receive_ssrcs_.end());
292 video_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000293 // TODO(pbos): Configure different RTX payloads per receive payload.
294 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
295 config.rtp.rtx.begin();
296 if (it != config.rtp.rtx.end())
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200297 video_receive_ssrcs_[it->second.ssrc] = receive_stream;
298 video_receive_streams_.insert(receive_stream);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000299
pbos8fc7fa72015-07-15 08:02:58 -0700300 ConfigureSync(config.sync_group);
301
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000302 if (!network_enabled_)
303 receive_stream->SignalNetworkState(kNetworkDown);
pbos8fc7fa72015-07-15 08:02:58 -0700304
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000305 return receive_stream;
306}
307
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000308void Call::DestroyVideoReceiveStream(
309 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000310 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000311 DCHECK(receive_stream != nullptr);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000312 VideoReceiveStream* receive_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000313 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000314 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000315 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
316 // separate SSRC there can be either one or two.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200317 auto it = video_receive_ssrcs_.begin();
318 while (it != video_receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000319 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000320 if (receive_stream_impl != nullptr)
321 DCHECK(receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000322 receive_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200323 video_receive_ssrcs_.erase(it++);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000324 } else {
325 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000326 }
327 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200328 video_receive_streams_.erase(receive_stream_impl);
pbos8fc7fa72015-07-15 08:02:58 -0700329 CHECK(receive_stream_impl != nullptr);
330 ConfigureSync(receive_stream_impl->config().sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000331 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000332 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000333}
334
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000335Call::Stats Call::GetStats() const {
336 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +0200337 // Fetch available send/receive bitrates.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000338 uint32_t send_bandwidth = 0;
Peter Boström45553ae2015-05-08 13:54:38 +0200339 channel_group_->GetBitrateController()->AvailableBandwidth(&send_bandwidth);
340 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000341 uint32_t recv_bandwidth = 0;
Peter Boström45553ae2015-05-08 13:54:38 +0200342 channel_group_->GetRemoteBitrateEstimator()->LatestEstimate(&ssrcs,
343 &recv_bandwidth);
344 stats.send_bandwidth_bps = send_bandwidth;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000345 stats.recv_bandwidth_bps = recv_bandwidth;
Peter Boström59d91dc2015-04-27 17:24:33 +0200346 stats.pacer_delay_ms = channel_group_->GetPacerQueuingDelayMs();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000347 {
348 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200349 for (const auto& kv : video_send_ssrcs_) {
350 int rtt_ms = kv.second->GetRtt();
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000351 if (rtt_ms > 0)
352 stats.rtt_ms = rtt_ms;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000353 }
354 }
355 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000356}
357
pbos@webrtc.org00873182014-11-25 14:03:34 +0000358void Call::SetBitrateConfig(
359 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000360 TRACE_EVENT0("webrtc", "Call::SetBitrateConfig");
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000361 DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
362 if (bitrate_config.max_bitrate_bps != -1)
363 DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
Stefan Holmere5904162015-03-26 11:11:06 +0100364 if (config_.bitrate_config.min_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000365 bitrate_config.min_bitrate_bps &&
366 (bitrate_config.start_bitrate_bps <= 0 ||
Stefan Holmere5904162015-03-26 11:11:06 +0100367 config_.bitrate_config.start_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000368 bitrate_config.start_bitrate_bps) &&
Stefan Holmere5904162015-03-26 11:11:06 +0100369 config_.bitrate_config.max_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000370 bitrate_config.max_bitrate_bps) {
371 // Nothing new to set, early abort to avoid encoder reconfigurations.
372 return;
373 }
Stefan Holmere5904162015-03-26 11:11:06 +0100374 config_.bitrate_config = bitrate_config;
Peter Boström45553ae2015-05-08 13:54:38 +0200375 SetBitrateControllerConfig(bitrate_config);
376}
377
378void Call::SetBitrateControllerConfig(
379 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
380 BitrateController* bitrate_controller =
381 channel_group_->GetBitrateController();
382 if (bitrate_config.start_bitrate_bps > 0)
383 bitrate_controller->SetStartBitrate(bitrate_config.start_bitrate_bps);
384 bitrate_controller->SetMinMaxBitrate(bitrate_config.min_bitrate_bps,
385 bitrate_config.max_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000386}
387
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000388void Call::SignalNetworkState(NetworkState state) {
389 // Take crit for entire function, it needs to be held while updating streams
390 // to guarantee a consistent state across streams.
Peter Boströmf2f82832015-05-01 13:00:41 +0200391 rtc::CritScope lock(&network_enabled_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000392 network_enabled_ = state == kNetworkUp;
393 {
394 ReadLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200395 for (auto& kv : video_send_ssrcs_) {
396 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000397 }
398 }
399 {
400 ReadLockScoped write_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200401 for (auto& kv : video_receive_ssrcs_) {
402 kv.second->SignalNetworkState(state);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000403 }
404 }
405}
406
pbos8fc7fa72015-07-15 08:02:58 -0700407void Call::ConfigureSync(const std::string& sync_group) {
408 // Set sync only if there was no previous one.
409 if (config_.voice_engine == nullptr || sync_group.empty())
410 return;
411
412 AudioReceiveStream* sync_audio_stream = nullptr;
413 // Find existing audio stream.
414 const auto it = sync_stream_mapping_.find(sync_group);
415 if (it != sync_stream_mapping_.end()) {
416 sync_audio_stream = it->second;
417 } else {
418 // No configured audio stream, see if we can find one.
419 for (const auto& kv : audio_receive_ssrcs_) {
420 if (kv.second->config().sync_group == sync_group) {
421 if (sync_audio_stream != nullptr) {
422 LOG(LS_WARNING) << "Attempting to sync more than one audio stream "
423 "within the same sync group. This is not "
424 "supported in the current implementation.";
425 break;
426 }
427 sync_audio_stream = kv.second;
428 }
429 }
430 }
431 if (sync_audio_stream)
432 sync_stream_mapping_[sync_group] = sync_audio_stream;
433 size_t num_synced_streams = 0;
434 for (VideoReceiveStream* video_stream : video_receive_streams_) {
435 if (video_stream->config().sync_group != sync_group)
436 continue;
437 ++num_synced_streams;
438 if (num_synced_streams > 1) {
439 // TODO(pbos): Support synchronizing more than one A/V pair.
440 // https://code.google.com/p/webrtc/issues/detail?id=4762
441 LOG(LS_WARNING) << "Attempting to sync more than one audio/video pair "
442 "within the same sync group. This is not supported in "
443 "the current implementation.";
444 }
445 // Only sync the first A/V pair within this sync group.
446 if (sync_audio_stream != nullptr && num_synced_streams == 1) {
447 video_stream->SetSyncChannel(config_.voice_engine,
448 sync_audio_stream->config().voe_channel_id);
449 } else {
450 video_stream->SetSyncChannel(config_.voice_engine, -1);
451 }
452 }
453}
454
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200455PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
456 const uint8_t* packet,
457 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000458 // TODO(pbos): Figure out what channel needs it actually.
459 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000460 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
461 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000462 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200463 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000464 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200465 for (VideoReceiveStream* stream : video_receive_streams_) {
Peter Boström3f4eed02015-04-16 15:59:43 +0200466 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000467 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000468 }
469 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200470 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000471 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200472 for (VideoSendStream* stream : video_send_streams_) {
Peter Boström74b97692015-04-14 13:31:46 +0200473 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000474 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000475 }
476 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000477 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000478}
479
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200480PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
481 const uint8_t* packet,
stefan68786d22015-09-08 05:36:15 -0700482 size_t length,
483 const PacketTime& packet_time) {
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000484 // Minimum RTP header size.
485 if (length < 12)
486 return DELIVERY_PACKET_ERROR;
487
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +0000488 uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(&packet[8]);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000489
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000490 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200491 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
492 auto it = audio_receive_ssrcs_.find(ssrc);
493 if (it != audio_receive_ssrcs_.end()) {
stefan68786d22015-09-08 05:36:15 -0700494 return it->second->DeliverRtp(packet, length, packet_time)
495 ? DELIVERY_OK
496 : DELIVERY_PACKET_ERROR;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200497 }
498 }
499 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
500 auto it = video_receive_ssrcs_.find(ssrc);
501 if (it != video_receive_ssrcs_.end()) {
stefan68786d22015-09-08 05:36:15 -0700502 return it->second->DeliverRtp(packet, length, packet_time)
503 ? DELIVERY_OK
504 : DELIVERY_PACKET_ERROR;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200505 }
506 }
507 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000508}
509
stefan68786d22015-09-08 05:36:15 -0700510PacketReceiver::DeliveryStatus Call::DeliverPacket(
511 MediaType media_type,
512 const uint8_t* packet,
513 size_t length,
514 const PacketTime& packet_time) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000515 if (RtpHeaderParser::IsRtcp(packet, length))
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200516 return DeliverRtcp(media_type, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000517
stefan68786d22015-09-08 05:36:15 -0700518 return DeliverRtp(media_type, packet, length, packet_time);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000519}
520
521} // namespace internal
522} // namespace webrtc