blob: 512e82ca6416a1c02ee31a5fd05384279323efa2 [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"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000022#include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
marpan@webrtc.org5b883172014-11-01 06:10:48 +000023#include "webrtc/modules/video_coding/codecs/vp9/include/vp9.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000024#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000025#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000026#include "webrtc/system_wrappers/interface/scoped_ptr.h"
27#include "webrtc/system_wrappers/interface/trace.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000028#include "webrtc/video/video_receive_stream.h"
29#include "webrtc/video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000030#include "webrtc/video_engine/include/vie_base.h"
31#include "webrtc/video_engine/include/vie_codec.h"
32#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
pbos@webrtc.org26c0c412014-09-03 16:17:12 +000033#include "webrtc/video_engine/include/vie_network.h"
34#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000035
36namespace webrtc {
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +000037const char* RtpExtension::kTOffset = "urn:ietf:params:rtp-hdrext:toffset";
38const char* RtpExtension::kAbsSendTime =
39 "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time";
pbos@webrtc.org3c107582014-07-20 15:27:35 +000040
41bool RtpExtension::IsSupported(const std::string& name) {
42 return name == webrtc::RtpExtension::kTOffset ||
43 name == webrtc::RtpExtension::kAbsSendTime;
44}
45
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000046VideoEncoder* VideoEncoder::Create(VideoEncoder::EncoderType codec_type) {
47 switch (codec_type) {
48 case kVp8:
49 return VP8Encoder::Create();
marpan@webrtc.org5b883172014-11-01 06:10:48 +000050 case kVp9:
51 return VP9Encoder::Create();
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000052 }
53 assert(false);
54 return NULL;
55}
56
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000057VideoDecoder* VideoDecoder::Create(VideoDecoder::DecoderType codec_type) {
58 switch (codec_type) {
59 case kVp8:
60 return VP8Decoder::Create();
stefan@webrtc.org7c29e8c2014-11-04 19:41:15 +000061 case kVp9:
62 return VP9Decoder::Create();
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000063 }
64 assert(false);
65 return NULL;
66}
67
pbos@webrtc.orga73a6782014-10-14 11:52:10 +000068const int Call::Config::kDefaultStartBitrateBps = 300000;
69
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000070namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000071
72class CpuOveruseObserverProxy : public webrtc::CpuOveruseObserver {
73 public:
pbos@webrtc.org42684be2014-10-03 11:25:45 +000074 explicit CpuOveruseObserverProxy(LoadObserver* overuse_callback)
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000075 : crit_(CriticalSectionWrapper::CreateCriticalSection()),
76 overuse_callback_(overuse_callback) {
77 assert(overuse_callback != NULL);
78 }
79
80 virtual ~CpuOveruseObserverProxy() {}
81
82 virtual void OveruseDetected() OVERRIDE {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000083 CriticalSectionScoped lock(crit_.get());
pbos@webrtc.org42684be2014-10-03 11:25:45 +000084 overuse_callback_->OnLoadUpdate(LoadObserver::kOveruse);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000085 }
86
87 virtual void NormalUsage() OVERRIDE {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000088 CriticalSectionScoped lock(crit_.get());
pbos@webrtc.org42684be2014-10-03 11:25:45 +000089 overuse_callback_->OnLoadUpdate(LoadObserver::kUnderuse);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000090 }
91
92 private:
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000093 const scoped_ptr<CriticalSectionWrapper> crit_;
pbos@webrtc.org42684be2014-10-03 11:25:45 +000094 LoadObserver* overuse_callback_ GUARDED_BY(crit_);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000095};
96
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000097class Call : public webrtc::Call, public PacketReceiver {
98 public:
99 Call(webrtc::VideoEngine* video_engine, const Call::Config& config);
100 virtual ~Call();
101
102 virtual PacketReceiver* Receiver() OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000103
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000104 virtual VideoSendStream* CreateVideoSendStream(
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000105 const VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000106 const VideoEncoderConfig& encoder_config) OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000107
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000108 virtual void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream)
109 OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000110
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000111 virtual VideoReceiveStream* CreateVideoReceiveStream(
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000112 const VideoReceiveStream::Config& config) OVERRIDE;
113
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000114 virtual void DestroyVideoReceiveStream(
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000115 webrtc::VideoReceiveStream* receive_stream) OVERRIDE;
116
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000117 virtual Stats GetStats() const OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000118
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000119 virtual DeliveryStatus DeliverPacket(const uint8_t* packet,
120 size_t length) OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000121
pbos@webrtc.org00873182014-11-25 14:03:34 +0000122 virtual void SetBitrateConfig(
123 const webrtc::Call::Config::BitrateConfig& bitrate_config) OVERRIDE;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000124 virtual void SignalNetworkState(NetworkState state) OVERRIDE;
125
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000126 private:
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000127 DeliveryStatus DeliverRtcp(const uint8_t* packet, size_t length);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000128 DeliveryStatus DeliverRtp(const uint8_t* packet, size_t length);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000129
130 Call::Config config_;
131
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000132 // Needs to be held while write-locking |receive_crit_| or |send_crit_|. This
133 // ensures that we have a consistent network state signalled to all senders
134 // and receivers.
135 scoped_ptr<CriticalSectionWrapper> network_enabled_crit_;
136 bool network_enabled_ GUARDED_BY(network_enabled_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000137
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000138 scoped_ptr<RWLockWrapper> receive_crit_;
139 std::map<uint32_t, VideoReceiveStream*> receive_ssrcs_
140 GUARDED_BY(receive_crit_);
141
142 scoped_ptr<RWLockWrapper> send_crit_;
143 std::map<uint32_t, VideoSendStream*> send_ssrcs_ GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000144
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000145 scoped_ptr<CpuOveruseObserverProxy> overuse_observer_proxy_;
146
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000147 VideoSendStream::RtpStateMap suspended_send_ssrcs_;
148
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000149 VideoEngine* video_engine_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000150 ViERTP_RTCP* rtp_rtcp_;
151 ViECodec* codec_;
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000152 ViEBase* base_;
153 int base_channel_id_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000154
155 DISALLOW_COPY_AND_ASSIGN(Call);
156};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000157} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000158
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000159Call* Call::Create(const Call::Config& config) {
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000160 VideoEngine* video_engine = config.webrtc_config != NULL
161 ? VideoEngine::Create(*config.webrtc_config)
162 : VideoEngine::Create();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000163 assert(video_engine != NULL);
164
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000165 return new internal::Call(video_engine, config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000166}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000167
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000168namespace internal {
169
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000170Call::Call(webrtc::VideoEngine* video_engine, const Call::Config& config)
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000171 : config_(config),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000172 network_enabled_crit_(CriticalSectionWrapper::CreateCriticalSection()),
173 network_enabled_(true),
174 receive_crit_(RWLockWrapper::CreateRWLock()),
175 send_crit_(RWLockWrapper::CreateRWLock()),
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000176 video_engine_(video_engine),
177 base_channel_id_(-1) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000178 assert(video_engine != NULL);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000179 assert(config.send_transport != NULL);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000180
pbos@webrtc.org00873182014-11-25 14:03:34 +0000181 assert(config.stream_bitrates.min_bitrate_bps >= 0);
182 assert(config.stream_bitrates.start_bitrate_bps >=
183 config.stream_bitrates.min_bitrate_bps);
184 if (config.stream_bitrates.max_bitrate_bps != -1) {
185 assert(config.stream_bitrates.max_bitrate_bps >=
186 config.stream_bitrates.start_bitrate_bps);
187 }
188
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000189 if (config.overuse_callback) {
190 overuse_observer_proxy_.reset(
191 new CpuOveruseObserverProxy(config.overuse_callback));
192 }
193
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000194 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
195 assert(rtp_rtcp_ != NULL);
196
197 codec_ = ViECodec::GetInterface(video_engine_);
198 assert(codec_ != NULL);
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000199
200 // As a workaround for non-existing calls in the old API, create a base
201 // channel used as default channel when creating send and receive streams.
202 base_ = ViEBase::GetInterface(video_engine_);
203 assert(base_ != NULL);
204
205 base_->CreateChannel(base_channel_id_);
206 assert(base_channel_id_ != -1);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000207}
208
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000209Call::~Call() {
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000210 base_->DeleteChannel(base_channel_id_);
211 base_->Release();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000212 codec_->Release();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000213 rtp_rtcp_->Release();
214 webrtc::VideoEngine::Delete(video_engine_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000215}
216
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000217PacketReceiver* Call::Receiver() { return this; }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000218
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000219VideoSendStream* Call::CreateVideoSendStream(
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000220 const VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000221 const VideoEncoderConfig& encoder_config) {
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000222 assert(config.rtp.ssrcs.size() > 0);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000223
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000224 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
225 // the call has already started.
pbos@webrtc.org00873182014-11-25 14:03:34 +0000226 VideoSendStream* send_stream = new VideoSendStream(
227 config_.send_transport, overuse_observer_proxy_.get(), video_engine_,
228 config, encoder_config, suspended_send_ssrcs_, base_channel_id_,
229 config_.stream_bitrates);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000230
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000231 // This needs to be taken before send_crit_ as both locks need to be held
232 // while changing network state.
233 CriticalSectionScoped lock(network_enabled_crit_.get());
234 WriteLockScoped write_lock(*send_crit_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000235 for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) {
236 assert(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end());
237 send_ssrcs_[config.rtp.ssrcs[i]] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000238 }
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.org95e51f52013-09-05 12:38:54 +0000245 assert(send_stream != NULL);
246
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000247 send_stream->Stop();
248
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000249 VideoSendStream* send_stream_impl = NULL;
250 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000251 WriteLockScoped write_lock(*send_crit_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000252 std::map<uint32_t, VideoSendStream*>::iterator it = send_ssrcs_.begin();
253 while (it != send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000254 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
255 send_stream_impl = it->second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000256 send_ssrcs_.erase(it++);
257 } else {
258 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000259 }
260 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000261 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000262
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000263 VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates();
264
265 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
266 it != rtp_state.end();
267 ++it) {
268 suspended_send_ssrcs_[it->first] = it->second;
269 }
270
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000271 assert(send_stream_impl != NULL);
272 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000273}
274
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000275VideoReceiveStream* Call::CreateVideoReceiveStream(
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000276 const VideoReceiveStream::Config& config) {
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000277 VideoReceiveStream* receive_stream =
278 new VideoReceiveStream(video_engine_,
279 config,
280 config_.send_transport,
281 config_.voice_engine,
282 base_channel_id_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000283
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000284 // This needs to be taken before receive_crit_ as both locks need to be held
285 // while changing network state.
286 CriticalSectionScoped lock(network_enabled_crit_.get());
287 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgb613b5a2013-12-03 10:13:04 +0000288 assert(receive_ssrcs_.find(config.rtp.remote_ssrc) == receive_ssrcs_.end());
289 receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000290 // TODO(pbos): Configure different RTX payloads per receive payload.
291 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
292 config.rtp.rtx.begin();
293 if (it != config.rtp.rtx.end())
294 receive_ssrcs_[it->second.ssrc] = receive_stream;
295
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000296 if (!network_enabled_)
297 receive_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000298 return receive_stream;
299}
300
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000301void Call::DestroyVideoReceiveStream(
302 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000303 assert(receive_stream != NULL);
304
305 VideoReceiveStream* receive_stream_impl = NULL;
306 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000307 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000308 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
309 // separate SSRC there can be either one or two.
310 std::map<uint32_t, VideoReceiveStream*>::iterator it =
311 receive_ssrcs_.begin();
312 while (it != receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000313 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
solenberg@webrtc.org094ac392014-01-29 11:21:58 +0000314 assert(receive_stream_impl == NULL ||
315 receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000316 receive_stream_impl = it->second;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000317 receive_ssrcs_.erase(it++);
318 } else {
319 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000320 }
321 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000322 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000323
324 assert(receive_stream_impl != NULL);
325 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000326}
327
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000328Call::Stats Call::GetStats() const {
329 Stats stats;
330 // Ignoring return values.
331 uint32_t send_bandwidth = 0;
332 rtp_rtcp_->GetEstimatedSendBandwidth(base_channel_id_, &send_bandwidth);
333 stats.send_bandwidth_bps = send_bandwidth;
334 uint32_t recv_bandwidth = 0;
335 rtp_rtcp_->GetEstimatedReceiveBandwidth(base_channel_id_, &recv_bandwidth);
336 stats.recv_bandwidth_bps = recv_bandwidth;
337 {
338 ReadLockScoped read_lock(*send_crit_);
339 for (std::map<uint32_t, VideoSendStream*>::const_iterator it =
340 send_ssrcs_.begin();
341 it != send_ssrcs_.end();
342 ++it) {
343 stats.pacer_delay_ms =
344 std::max(it->second->GetPacerQueuingDelayMs(), stats.pacer_delay_ms);
345 }
346 }
347 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000348}
349
pbos@webrtc.org00873182014-11-25 14:03:34 +0000350void Call::SetBitrateConfig(
351 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
352 assert(bitrate_config.min_bitrate_bps >= 0);
353 assert(bitrate_config.max_bitrate_bps == -1 ||
354 bitrate_config.max_bitrate_bps > 0);
355 if (config_.stream_bitrates.min_bitrate_bps ==
356 bitrate_config.min_bitrate_bps &&
357 (bitrate_config.start_bitrate_bps <= 0 ||
358 config_.stream_bitrates.start_bitrate_bps ==
359 bitrate_config.start_bitrate_bps) &&
360 config_.stream_bitrates.max_bitrate_bps ==
361 bitrate_config.max_bitrate_bps) {
362 // Nothing new to set, early abort to avoid encoder reconfigurations.
363 return;
364 }
365 config_.stream_bitrates = bitrate_config;
366 ReadLockScoped read_lock(*send_crit_);
367 for (std::map<uint32_t, VideoSendStream*>::const_iterator it =
368 send_ssrcs_.begin();
369 it != send_ssrcs_.end(); ++it) {
370 it->second->SetBitrateConfig(bitrate_config);
371 }
372}
373
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000374void Call::SignalNetworkState(NetworkState state) {
375 // Take crit for entire function, it needs to be held while updating streams
376 // to guarantee a consistent state across streams.
377 CriticalSectionScoped lock(network_enabled_crit_.get());
378 network_enabled_ = state == kNetworkUp;
379 {
380 ReadLockScoped write_lock(*send_crit_);
381 for (std::map<uint32_t, VideoSendStream*>::iterator it =
382 send_ssrcs_.begin();
383 it != send_ssrcs_.end();
384 ++it) {
385 it->second->SignalNetworkState(state);
386 }
387 }
388 {
389 ReadLockScoped write_lock(*receive_crit_);
390 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
391 receive_ssrcs_.begin();
392 it != receive_ssrcs_.end();
393 ++it) {
394 it->second->SignalNetworkState(state);
395 }
396 }
397}
398
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000399PacketReceiver::DeliveryStatus Call::DeliverRtcp(const uint8_t* packet,
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000400 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000401 // TODO(pbos): Figure out what channel needs it actually.
402 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000403 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
404 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000405 bool rtcp_delivered = false;
pbos@webrtc.org40523702013-08-05 12:49:22 +0000406 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000407 ReadLockScoped read_lock(*receive_crit_);
pbos@webrtc.org40523702013-08-05 12:49:22 +0000408 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
409 receive_ssrcs_.begin();
410 it != receive_ssrcs_.end();
411 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000412 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000413 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000414 }
415 }
416
pbos@webrtc.org40523702013-08-05 12:49:22 +0000417 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000418 ReadLockScoped read_lock(*send_crit_);
pbos@webrtc.org40523702013-08-05 12:49:22 +0000419 for (std::map<uint32_t, VideoSendStream*>::iterator it =
420 send_ssrcs_.begin();
421 it != send_ssrcs_.end();
422 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000423 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000424 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000425 }
426 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000427 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000428}
429
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000430PacketReceiver::DeliveryStatus Call::DeliverRtp(const uint8_t* packet,
431 size_t length) {
432 // Minimum RTP header size.
433 if (length < 12)
434 return DELIVERY_PACKET_ERROR;
435
436 const uint8_t* ptr = &packet[8];
pbos@webrtc.orgdde16f12014-08-05 23:35:43 +0000437 uint32_t ssrc = ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000438
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000439 ReadLockScoped read_lock(*receive_crit_);
solenberg@webrtc.org094ac392014-01-29 11:21:58 +0000440 std::map<uint32_t, VideoReceiveStream*>::iterator it =
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000441 receive_ssrcs_.find(ssrc);
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000442
443 if (it == receive_ssrcs_.end())
444 return DELIVERY_UNKNOWN_SSRC;
445
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000446 return it->second->DeliverRtp(packet, length) ? DELIVERY_OK
447 : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000448}
449
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000450PacketReceiver::DeliveryStatus Call::DeliverPacket(const uint8_t* packet,
451 size_t length) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000452 if (RtpHeaderParser::IsRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000453 return DeliverRtcp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000454
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000455 return DeliverRtp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000456}
457
458} // namespace internal
459} // namespace webrtc