blob: b80d5c038ed618cef4d78d3d6ac8e71beddfb466 [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 <assert.h>
12#include <string.h>
13
pbos@webrtc.org29d58392013-05-16 12:08:03 +000014#include <map>
15#include <vector>
16
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000017#include "webrtc/base/thread_annotations.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000018#include "webrtc/call.h"
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000019#include "webrtc/common.h"
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +000020#include "webrtc/config.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000021#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +000022#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000023#include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
marpan@webrtc.org5b883172014-11-01 06:10:48 +000024#include "webrtc/modules/video_coding/codecs/vp9/include/vp9.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000025#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
pbos@webrtc.org32e85282015-01-15 10:09:39 +000026#include "webrtc/system_wrappers/interface/logging.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000027#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000028#include "webrtc/system_wrappers/interface/scoped_ptr.h"
29#include "webrtc/system_wrappers/interface/trace.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000030#include "webrtc/video/video_receive_stream.h"
31#include "webrtc/video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000032#include "webrtc/video_engine/include/vie_base.h"
33#include "webrtc/video_engine/include/vie_codec.h"
34#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
pbos@webrtc.org26c0c412014-09-03 16:17:12 +000035#include "webrtc/video_engine/include/vie_network.h"
36#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000037
38namespace webrtc {
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +000039const char* RtpExtension::kTOffset = "urn:ietf:params:rtp-hdrext:toffset";
40const char* RtpExtension::kAbsSendTime =
41 "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time";
pbos@webrtc.org3c107582014-07-20 15:27:35 +000042
43bool RtpExtension::IsSupported(const std::string& name) {
44 return name == webrtc::RtpExtension::kTOffset ||
45 name == webrtc::RtpExtension::kAbsSendTime;
46}
47
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000048VideoEncoder* VideoEncoder::Create(VideoEncoder::EncoderType codec_type) {
49 switch (codec_type) {
50 case kVp8:
51 return VP8Encoder::Create();
marpan@webrtc.org5b883172014-11-01 06:10:48 +000052 case kVp9:
53 return VP9Encoder::Create();
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000054 }
55 assert(false);
56 return NULL;
57}
58
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000059VideoDecoder* VideoDecoder::Create(VideoDecoder::DecoderType codec_type) {
60 switch (codec_type) {
61 case kVp8:
62 return VP8Decoder::Create();
stefan@webrtc.org7c29e8c2014-11-04 19:41:15 +000063 case kVp9:
64 return VP9Decoder::Create();
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000065 }
66 assert(false);
67 return NULL;
68}
69
pbos@webrtc.orga73a6782014-10-14 11:52:10 +000070const int Call::Config::kDefaultStartBitrateBps = 300000;
71
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000072namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000073
74class CpuOveruseObserverProxy : public webrtc::CpuOveruseObserver {
75 public:
pbos@webrtc.org42684be2014-10-03 11:25:45 +000076 explicit CpuOveruseObserverProxy(LoadObserver* overuse_callback)
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000077 : crit_(CriticalSectionWrapper::CreateCriticalSection()),
78 overuse_callback_(overuse_callback) {
79 assert(overuse_callback != NULL);
80 }
81
82 virtual ~CpuOveruseObserverProxy() {}
83
84 virtual void OveruseDetected() OVERRIDE {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000085 CriticalSectionScoped lock(crit_.get());
pbos@webrtc.org42684be2014-10-03 11:25:45 +000086 overuse_callback_->OnLoadUpdate(LoadObserver::kOveruse);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000087 }
88
89 virtual void NormalUsage() OVERRIDE {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000090 CriticalSectionScoped lock(crit_.get());
pbos@webrtc.org42684be2014-10-03 11:25:45 +000091 overuse_callback_->OnLoadUpdate(LoadObserver::kUnderuse);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000092 }
93
94 private:
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000095 const scoped_ptr<CriticalSectionWrapper> crit_;
pbos@webrtc.org42684be2014-10-03 11:25:45 +000096 LoadObserver* overuse_callback_ GUARDED_BY(crit_);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000097};
98
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000099class Call : public webrtc::Call, public PacketReceiver {
100 public:
101 Call(webrtc::VideoEngine* video_engine, const Call::Config& config);
102 virtual ~Call();
103
104 virtual PacketReceiver* Receiver() OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000105
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000106 virtual VideoSendStream* CreateVideoSendStream(
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000107 const VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000108 const VideoEncoderConfig& encoder_config) OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000109
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000110 virtual void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream)
111 OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000112
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000113 virtual VideoReceiveStream* CreateVideoReceiveStream(
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000114 const VideoReceiveStream::Config& config) OVERRIDE;
115
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000116 virtual void DestroyVideoReceiveStream(
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000117 webrtc::VideoReceiveStream* receive_stream) OVERRIDE;
118
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000119 virtual Stats GetStats() const OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000120
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000121 virtual DeliveryStatus DeliverPacket(const uint8_t* packet,
122 size_t length) OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000123
pbos@webrtc.org00873182014-11-25 14:03:34 +0000124 virtual void SetBitrateConfig(
125 const webrtc::Call::Config::BitrateConfig& bitrate_config) OVERRIDE;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000126 virtual void SignalNetworkState(NetworkState state) OVERRIDE;
127
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000128 private:
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000129 DeliveryStatus DeliverRtcp(const uint8_t* packet, size_t length);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000130 DeliveryStatus DeliverRtp(const uint8_t* packet, size_t length);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000131
132 Call::Config config_;
133
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000134 // Needs to be held while write-locking |receive_crit_| or |send_crit_|. This
135 // ensures that we have a consistent network state signalled to all senders
136 // and receivers.
137 scoped_ptr<CriticalSectionWrapper> network_enabled_crit_;
138 bool network_enabled_ GUARDED_BY(network_enabled_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000139
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000140 scoped_ptr<RWLockWrapper> receive_crit_;
141 std::map<uint32_t, VideoReceiveStream*> receive_ssrcs_
142 GUARDED_BY(receive_crit_);
143
144 scoped_ptr<RWLockWrapper> send_crit_;
145 std::map<uint32_t, VideoSendStream*> send_ssrcs_ GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000146
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000147 scoped_ptr<CpuOveruseObserverProxy> overuse_observer_proxy_;
148
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000149 VideoSendStream::RtpStateMap suspended_send_ssrcs_;
150
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000151 VideoEngine* video_engine_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000152 ViERTP_RTCP* rtp_rtcp_;
153 ViECodec* codec_;
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000154 ViEBase* base_;
155 int base_channel_id_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000156
157 DISALLOW_COPY_AND_ASSIGN(Call);
158};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000159} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000160
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000161Call* Call::Create(const Call::Config& config) {
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000162 VideoEngine* video_engine = config.webrtc_config != NULL
163 ? VideoEngine::Create(*config.webrtc_config)
164 : VideoEngine::Create();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000165 assert(video_engine != NULL);
166
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000167 return new internal::Call(video_engine, config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000168}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000169
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000170namespace internal {
171
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000172Call::Call(webrtc::VideoEngine* video_engine, const Call::Config& config)
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000173 : config_(config),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000174 network_enabled_crit_(CriticalSectionWrapper::CreateCriticalSection()),
175 network_enabled_(true),
176 receive_crit_(RWLockWrapper::CreateRWLock()),
177 send_crit_(RWLockWrapper::CreateRWLock()),
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000178 video_engine_(video_engine),
179 base_channel_id_(-1) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000180 assert(video_engine != NULL);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000181 assert(config.send_transport != NULL);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000182
pbos@webrtc.org00873182014-11-25 14:03:34 +0000183 assert(config.stream_bitrates.min_bitrate_bps >= 0);
184 assert(config.stream_bitrates.start_bitrate_bps >=
185 config.stream_bitrates.min_bitrate_bps);
186 if (config.stream_bitrates.max_bitrate_bps != -1) {
187 assert(config.stream_bitrates.max_bitrate_bps >=
188 config.stream_bitrates.start_bitrate_bps);
189 }
190
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000191 if (config.overuse_callback) {
192 overuse_observer_proxy_.reset(
193 new CpuOveruseObserverProxy(config.overuse_callback));
194 }
195
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000196 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
197 assert(rtp_rtcp_ != NULL);
198
199 codec_ = ViECodec::GetInterface(video_engine_);
200 assert(codec_ != NULL);
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000201
202 // As a workaround for non-existing calls in the old API, create a base
203 // channel used as default channel when creating send and receive streams.
204 base_ = ViEBase::GetInterface(video_engine_);
205 assert(base_ != NULL);
206
207 base_->CreateChannel(base_channel_id_);
208 assert(base_channel_id_ != -1);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000209}
210
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000211Call::~Call() {
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000212 base_->DeleteChannel(base_channel_id_);
213 base_->Release();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000214 codec_->Release();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000215 rtp_rtcp_->Release();
216 webrtc::VideoEngine::Delete(video_engine_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000217}
218
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000219PacketReceiver* Call::Receiver() { return this; }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000220
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000221VideoSendStream* Call::CreateVideoSendStream(
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000222 const VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000223 const VideoEncoderConfig& encoder_config) {
pbos@webrtc.org32e85282015-01-15 10:09:39 +0000224 LOG(LS_INFO) << "CreateVideoSendStream: " << config.ToString();
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000225 assert(config.rtp.ssrcs.size() > 0);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000226
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000227 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
228 // the call has already started.
pbos@webrtc.org00873182014-11-25 14:03:34 +0000229 VideoSendStream* send_stream = new VideoSendStream(
230 config_.send_transport, overuse_observer_proxy_.get(), video_engine_,
231 config, encoder_config, suspended_send_ssrcs_, base_channel_id_,
232 config_.stream_bitrates);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000233
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000234 // This needs to be taken before send_crit_ as both locks need to be held
235 // while changing network state.
236 CriticalSectionScoped lock(network_enabled_crit_.get());
237 WriteLockScoped write_lock(*send_crit_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000238 for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) {
239 assert(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end());
240 send_ssrcs_[config.rtp.ssrcs[i]] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000241 }
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000242 if (!network_enabled_)
243 send_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000244 return send_stream;
245}
246
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000247void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000248 assert(send_stream != NULL);
249
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000250 send_stream->Stop();
251
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000252 VideoSendStream* send_stream_impl = NULL;
253 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000254 WriteLockScoped write_lock(*send_crit_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000255 std::map<uint32_t, VideoSendStream*>::iterator it = send_ssrcs_.begin();
256 while (it != send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000257 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
258 send_stream_impl = it->second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000259 send_ssrcs_.erase(it++);
260 } else {
261 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000262 }
263 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000264 }
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) {
271 suspended_send_ssrcs_[it->first] = it->second;
272 }
273
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000274 assert(send_stream_impl != NULL);
275 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000276}
277
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000278VideoReceiveStream* Call::CreateVideoReceiveStream(
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000279 const VideoReceiveStream::Config& config) {
pbos@webrtc.org32e85282015-01-15 10:09:39 +0000280 LOG(LS_INFO) << "CreateVideoReceiveStream: " << config.ToString();
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000281 VideoReceiveStream* receive_stream =
282 new VideoReceiveStream(video_engine_,
283 config,
284 config_.send_transport,
285 config_.voice_engine,
286 base_channel_id_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000287
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000288 // This needs to be taken before receive_crit_ as both locks need to be held
289 // while changing network state.
290 CriticalSectionScoped lock(network_enabled_crit_.get());
291 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgb613b5a2013-12-03 10:13:04 +0000292 assert(receive_ssrcs_.find(config.rtp.remote_ssrc) == receive_ssrcs_.end());
293 receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000294 // TODO(pbos): Configure different RTX payloads per receive payload.
295 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
296 config.rtp.rtx.begin();
297 if (it != config.rtp.rtx.end())
298 receive_ssrcs_[it->second.ssrc] = receive_stream;
299
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000300 if (!network_enabled_)
301 receive_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000302 return receive_stream;
303}
304
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000305void Call::DestroyVideoReceiveStream(
306 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000307 assert(receive_stream != NULL);
308
309 VideoReceiveStream* receive_stream_impl = NULL;
310 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000311 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000312 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
313 // separate SSRC there can be either one or two.
314 std::map<uint32_t, VideoReceiveStream*>::iterator it =
315 receive_ssrcs_.begin();
316 while (it != receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000317 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
solenberg@webrtc.org094ac392014-01-29 11:21:58 +0000318 assert(receive_stream_impl == NULL ||
319 receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000320 receive_stream_impl = it->second;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000321 receive_ssrcs_.erase(it++);
322 } else {
323 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000324 }
325 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000326 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000327
328 assert(receive_stream_impl != NULL);
329 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000330}
331
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000332Call::Stats Call::GetStats() const {
333 Stats stats;
334 // Ignoring return values.
335 uint32_t send_bandwidth = 0;
336 rtp_rtcp_->GetEstimatedSendBandwidth(base_channel_id_, &send_bandwidth);
337 stats.send_bandwidth_bps = send_bandwidth;
338 uint32_t recv_bandwidth = 0;
339 rtp_rtcp_->GetEstimatedReceiveBandwidth(base_channel_id_, &recv_bandwidth);
340 stats.recv_bandwidth_bps = recv_bandwidth;
341 {
342 ReadLockScoped read_lock(*send_crit_);
343 for (std::map<uint32_t, VideoSendStream*>::const_iterator it =
344 send_ssrcs_.begin();
345 it != send_ssrcs_.end();
346 ++it) {
347 stats.pacer_delay_ms =
348 std::max(it->second->GetPacerQueuingDelayMs(), stats.pacer_delay_ms);
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000349 int rtt_ms = it->second->GetRtt();
350 if (rtt_ms > 0)
351 stats.rtt_ms = rtt_ms;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000352 }
353 }
354 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000355}
356
pbos@webrtc.org00873182014-11-25 14:03:34 +0000357void Call::SetBitrateConfig(
358 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
359 assert(bitrate_config.min_bitrate_bps >= 0);
360 assert(bitrate_config.max_bitrate_bps == -1 ||
361 bitrate_config.max_bitrate_bps > 0);
362 if (config_.stream_bitrates.min_bitrate_bps ==
363 bitrate_config.min_bitrate_bps &&
364 (bitrate_config.start_bitrate_bps <= 0 ||
365 config_.stream_bitrates.start_bitrate_bps ==
366 bitrate_config.start_bitrate_bps) &&
367 config_.stream_bitrates.max_bitrate_bps ==
368 bitrate_config.max_bitrate_bps) {
369 // Nothing new to set, early abort to avoid encoder reconfigurations.
370 return;
371 }
372 config_.stream_bitrates = bitrate_config;
373 ReadLockScoped read_lock(*send_crit_);
374 for (std::map<uint32_t, VideoSendStream*>::const_iterator it =
375 send_ssrcs_.begin();
376 it != send_ssrcs_.end(); ++it) {
377 it->second->SetBitrateConfig(bitrate_config);
378 }
379}
380
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000381void Call::SignalNetworkState(NetworkState state) {
382 // Take crit for entire function, it needs to be held while updating streams
383 // to guarantee a consistent state across streams.
384 CriticalSectionScoped lock(network_enabled_crit_.get());
385 network_enabled_ = state == kNetworkUp;
386 {
387 ReadLockScoped write_lock(*send_crit_);
388 for (std::map<uint32_t, VideoSendStream*>::iterator it =
389 send_ssrcs_.begin();
390 it != send_ssrcs_.end();
391 ++it) {
392 it->second->SignalNetworkState(state);
393 }
394 }
395 {
396 ReadLockScoped write_lock(*receive_crit_);
397 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
398 receive_ssrcs_.begin();
399 it != receive_ssrcs_.end();
400 ++it) {
401 it->second->SignalNetworkState(state);
402 }
403 }
404}
405
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000406PacketReceiver::DeliveryStatus Call::DeliverRtcp(const uint8_t* packet,
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000407 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000408 // TODO(pbos): Figure out what channel needs it actually.
409 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000410 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
411 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000412 bool rtcp_delivered = false;
pbos@webrtc.org40523702013-08-05 12:49:22 +0000413 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000414 ReadLockScoped read_lock(*receive_crit_);
pbos@webrtc.org40523702013-08-05 12:49:22 +0000415 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
416 receive_ssrcs_.begin();
417 it != receive_ssrcs_.end();
418 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000419 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000420 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000421 }
422 }
423
pbos@webrtc.org40523702013-08-05 12:49:22 +0000424 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000425 ReadLockScoped read_lock(*send_crit_);
pbos@webrtc.org40523702013-08-05 12:49:22 +0000426 for (std::map<uint32_t, VideoSendStream*>::iterator it =
427 send_ssrcs_.begin();
428 it != send_ssrcs_.end();
429 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000430 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000431 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000432 }
433 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000434 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000435}
436
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000437PacketReceiver::DeliveryStatus Call::DeliverRtp(const uint8_t* packet,
438 size_t length) {
439 // Minimum RTP header size.
440 if (length < 12)
441 return DELIVERY_PACKET_ERROR;
442
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +0000443 uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(&packet[8]);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000444
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000445 ReadLockScoped read_lock(*receive_crit_);
solenberg@webrtc.org094ac392014-01-29 11:21:58 +0000446 std::map<uint32_t, VideoReceiveStream*>::iterator it =
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000447 receive_ssrcs_.find(ssrc);
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000448
449 if (it == receive_ssrcs_.end())
450 return DELIVERY_UNKNOWN_SSRC;
451
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000452 return it->second->DeliverRtp(packet, length) ? DELIVERY_OK
453 : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000454}
455
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000456PacketReceiver::DeliveryStatus Call::DeliverPacket(const uint8_t* packet,
457 size_t length) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000458 if (RtpHeaderParser::IsRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000459 return DeliverRtcp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000460
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000461 return DeliverRtp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000462}
463
464} // namespace internal
465} // namespace webrtc