blob: 0ed412d5a84918c03d59e038f5fcc5f32da38b16 [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.org32e85282015-01-15 10:09:39 +000025#include "webrtc/system_wrappers/interface/logging.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000026#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000027#include "webrtc/system_wrappers/interface/scoped_ptr.h"
28#include "webrtc/system_wrappers/interface/trace.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000029#include "webrtc/video/video_receive_stream.h"
30#include "webrtc/video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000031#include "webrtc/video_engine/include/vie_base.h"
32#include "webrtc/video_engine/include/vie_codec.h"
33#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
pbos@webrtc.org26c0c412014-09-03 16:17:12 +000034#include "webrtc/video_engine/include/vie_network.h"
35#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000036
37namespace webrtc {
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +000038const char* RtpExtension::kTOffset = "urn:ietf:params:rtp-hdrext:toffset";
39const char* RtpExtension::kAbsSendTime =
40 "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time";
pbos@webrtc.org3c107582014-07-20 15:27:35 +000041
42bool RtpExtension::IsSupported(const std::string& name) {
43 return name == webrtc::RtpExtension::kTOffset ||
44 name == webrtc::RtpExtension::kAbsSendTime;
45}
46
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000047VideoEncoder* VideoEncoder::Create(VideoEncoder::EncoderType codec_type) {
48 switch (codec_type) {
49 case kVp8:
50 return VP8Encoder::Create();
marpan@webrtc.org5b883172014-11-01 06:10:48 +000051 case kVp9:
52 return VP9Encoder::Create();
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000053 }
54 assert(false);
55 return NULL;
56}
57
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000058VideoDecoder* VideoDecoder::Create(VideoDecoder::DecoderType codec_type) {
59 switch (codec_type) {
60 case kVp8:
61 return VP8Decoder::Create();
stefan@webrtc.org7c29e8c2014-11-04 19:41:15 +000062 case kVp9:
63 return VP9Decoder::Create();
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000064 }
65 assert(false);
66 return NULL;
67}
68
pbos@webrtc.orga73a6782014-10-14 11:52:10 +000069const int Call::Config::kDefaultStartBitrateBps = 300000;
70
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000071namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000072
73class CpuOveruseObserverProxy : public webrtc::CpuOveruseObserver {
74 public:
pbos@webrtc.org42684be2014-10-03 11:25:45 +000075 explicit CpuOveruseObserverProxy(LoadObserver* overuse_callback)
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000076 : crit_(CriticalSectionWrapper::CreateCriticalSection()),
77 overuse_callback_(overuse_callback) {
78 assert(overuse_callback != NULL);
79 }
80
81 virtual ~CpuOveruseObserverProxy() {}
82
83 virtual void OveruseDetected() OVERRIDE {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000084 CriticalSectionScoped lock(crit_.get());
pbos@webrtc.org42684be2014-10-03 11:25:45 +000085 overuse_callback_->OnLoadUpdate(LoadObserver::kOveruse);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000086 }
87
88 virtual void NormalUsage() OVERRIDE {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000089 CriticalSectionScoped lock(crit_.get());
pbos@webrtc.org42684be2014-10-03 11:25:45 +000090 overuse_callback_->OnLoadUpdate(LoadObserver::kUnderuse);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000091 }
92
93 private:
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000094 const scoped_ptr<CriticalSectionWrapper> crit_;
pbos@webrtc.org42684be2014-10-03 11:25:45 +000095 LoadObserver* overuse_callback_ GUARDED_BY(crit_);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000096};
97
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000098class Call : public webrtc::Call, public PacketReceiver {
99 public:
100 Call(webrtc::VideoEngine* video_engine, const Call::Config& config);
101 virtual ~Call();
102
103 virtual PacketReceiver* Receiver() OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000104
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000105 virtual VideoSendStream* CreateVideoSendStream(
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000106 const VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000107 const VideoEncoderConfig& encoder_config) OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000108
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000109 virtual void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream)
110 OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000111
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000112 virtual VideoReceiveStream* CreateVideoReceiveStream(
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000113 const VideoReceiveStream::Config& config) OVERRIDE;
114
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000115 virtual void DestroyVideoReceiveStream(
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000116 webrtc::VideoReceiveStream* receive_stream) OVERRIDE;
117
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000118 virtual Stats GetStats() const OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000119
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000120 virtual DeliveryStatus DeliverPacket(const uint8_t* packet,
121 size_t length) OVERRIDE;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000122
pbos@webrtc.org00873182014-11-25 14:03:34 +0000123 virtual void SetBitrateConfig(
124 const webrtc::Call::Config::BitrateConfig& bitrate_config) OVERRIDE;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000125 virtual void SignalNetworkState(NetworkState state) OVERRIDE;
126
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000127 private:
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000128 DeliveryStatus DeliverRtcp(const uint8_t* packet, size_t length);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000129 DeliveryStatus DeliverRtp(const uint8_t* packet, size_t length);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000130
131 Call::Config config_;
132
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000133 // Needs to be held while write-locking |receive_crit_| or |send_crit_|. This
134 // ensures that we have a consistent network state signalled to all senders
135 // and receivers.
136 scoped_ptr<CriticalSectionWrapper> network_enabled_crit_;
137 bool network_enabled_ GUARDED_BY(network_enabled_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000138
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000139 scoped_ptr<RWLockWrapper> receive_crit_;
140 std::map<uint32_t, VideoReceiveStream*> receive_ssrcs_
141 GUARDED_BY(receive_crit_);
142
143 scoped_ptr<RWLockWrapper> send_crit_;
144 std::map<uint32_t, VideoSendStream*> send_ssrcs_ GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000145
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000146 scoped_ptr<CpuOveruseObserverProxy> overuse_observer_proxy_;
147
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000148 VideoSendStream::RtpStateMap suspended_send_ssrcs_;
149
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000150 VideoEngine* video_engine_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000151 ViERTP_RTCP* rtp_rtcp_;
152 ViECodec* codec_;
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000153 ViEBase* base_;
154 int base_channel_id_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000155
156 DISALLOW_COPY_AND_ASSIGN(Call);
157};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000158} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000159
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000160Call* Call::Create(const Call::Config& config) {
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000161 VideoEngine* video_engine = config.webrtc_config != NULL
162 ? VideoEngine::Create(*config.webrtc_config)
163 : VideoEngine::Create();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000164 assert(video_engine != NULL);
165
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000166 return new internal::Call(video_engine, config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000167}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000168
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000169namespace internal {
170
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000171Call::Call(webrtc::VideoEngine* video_engine, const Call::Config& config)
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000172 : config_(config),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000173 network_enabled_crit_(CriticalSectionWrapper::CreateCriticalSection()),
174 network_enabled_(true),
175 receive_crit_(RWLockWrapper::CreateRWLock()),
176 send_crit_(RWLockWrapper::CreateRWLock()),
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000177 video_engine_(video_engine),
178 base_channel_id_(-1) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000179 assert(video_engine != NULL);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000180 assert(config.send_transport != NULL);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000181
pbos@webrtc.org00873182014-11-25 14:03:34 +0000182 assert(config.stream_bitrates.min_bitrate_bps >= 0);
183 assert(config.stream_bitrates.start_bitrate_bps >=
184 config.stream_bitrates.min_bitrate_bps);
185 if (config.stream_bitrates.max_bitrate_bps != -1) {
186 assert(config.stream_bitrates.max_bitrate_bps >=
187 config.stream_bitrates.start_bitrate_bps);
188 }
189
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000190 if (config.overuse_callback) {
191 overuse_observer_proxy_.reset(
192 new CpuOveruseObserverProxy(config.overuse_callback));
193 }
194
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000195 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
196 assert(rtp_rtcp_ != NULL);
197
198 codec_ = ViECodec::GetInterface(video_engine_);
199 assert(codec_ != NULL);
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000200
201 // As a workaround for non-existing calls in the old API, create a base
202 // channel used as default channel when creating send and receive streams.
203 base_ = ViEBase::GetInterface(video_engine_);
204 assert(base_ != NULL);
205
206 base_->CreateChannel(base_channel_id_);
207 assert(base_channel_id_ != -1);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000208}
209
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000210Call::~Call() {
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000211 base_->DeleteChannel(base_channel_id_);
212 base_->Release();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000213 codec_->Release();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000214 rtp_rtcp_->Release();
215 webrtc::VideoEngine::Delete(video_engine_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000216}
217
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000218PacketReceiver* Call::Receiver() { return this; }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000219
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000220VideoSendStream* Call::CreateVideoSendStream(
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000221 const VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000222 const VideoEncoderConfig& encoder_config) {
pbos@webrtc.org32e85282015-01-15 10:09:39 +0000223 LOG(LS_INFO) << "CreateVideoSendStream: " << config.ToString();
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000224 assert(config.rtp.ssrcs.size() > 0);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000225
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000226 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
227 // the call has already started.
pbos@webrtc.org00873182014-11-25 14:03:34 +0000228 VideoSendStream* send_stream = new VideoSendStream(
229 config_.send_transport, overuse_observer_proxy_.get(), video_engine_,
230 config, encoder_config, suspended_send_ssrcs_, base_channel_id_,
231 config_.stream_bitrates);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000232
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000233 // This needs to be taken before send_crit_ as both locks need to be held
234 // while changing network state.
235 CriticalSectionScoped lock(network_enabled_crit_.get());
236 WriteLockScoped write_lock(*send_crit_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000237 for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) {
238 assert(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end());
239 send_ssrcs_[config.rtp.ssrcs[i]] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000240 }
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000241 if (!network_enabled_)
242 send_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000243 return send_stream;
244}
245
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000246void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000247 assert(send_stream != NULL);
248
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000249 send_stream->Stop();
250
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000251 VideoSendStream* send_stream_impl = NULL;
252 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000253 WriteLockScoped write_lock(*send_crit_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000254 std::map<uint32_t, VideoSendStream*>::iterator it = send_ssrcs_.begin();
255 while (it != send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000256 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
257 send_stream_impl = it->second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000258 send_ssrcs_.erase(it++);
259 } else {
260 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000261 }
262 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000263 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000264
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000265 VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates();
266
267 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
268 it != rtp_state.end();
269 ++it) {
270 suspended_send_ssrcs_[it->first] = it->second;
271 }
272
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000273 assert(send_stream_impl != NULL);
274 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000275}
276
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000277VideoReceiveStream* Call::CreateVideoReceiveStream(
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000278 const VideoReceiveStream::Config& config) {
pbos@webrtc.org32e85282015-01-15 10:09:39 +0000279 LOG(LS_INFO) << "CreateVideoReceiveStream: " << config.ToString();
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000280 VideoReceiveStream* receive_stream =
281 new VideoReceiveStream(video_engine_,
282 config,
283 config_.send_transport,
284 config_.voice_engine,
285 base_channel_id_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000286
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000287 // This needs to be taken before receive_crit_ as both locks need to be held
288 // while changing network state.
289 CriticalSectionScoped lock(network_enabled_crit_.get());
290 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgb613b5a2013-12-03 10:13:04 +0000291 assert(receive_ssrcs_.find(config.rtp.remote_ssrc) == receive_ssrcs_.end());
292 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())
297 receive_ssrcs_[it->second.ssrc] = receive_stream;
298
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000299 if (!network_enabled_)
300 receive_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000301 return receive_stream;
302}
303
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000304void Call::DestroyVideoReceiveStream(
305 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000306 assert(receive_stream != NULL);
307
308 VideoReceiveStream* receive_stream_impl = NULL;
309 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000310 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000311 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
312 // separate SSRC there can be either one or two.
313 std::map<uint32_t, VideoReceiveStream*>::iterator it =
314 receive_ssrcs_.begin();
315 while (it != receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000316 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
solenberg@webrtc.org094ac392014-01-29 11:21:58 +0000317 assert(receive_stream_impl == NULL ||
318 receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000319 receive_stream_impl = it->second;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000320 receive_ssrcs_.erase(it++);
321 } else {
322 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000323 }
324 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000325 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000326
327 assert(receive_stream_impl != NULL);
328 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000329}
330
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000331Call::Stats Call::GetStats() const {
332 Stats stats;
333 // Ignoring return values.
334 uint32_t send_bandwidth = 0;
335 rtp_rtcp_->GetEstimatedSendBandwidth(base_channel_id_, &send_bandwidth);
336 stats.send_bandwidth_bps = send_bandwidth;
337 uint32_t recv_bandwidth = 0;
338 rtp_rtcp_->GetEstimatedReceiveBandwidth(base_channel_id_, &recv_bandwidth);
339 stats.recv_bandwidth_bps = recv_bandwidth;
340 {
341 ReadLockScoped read_lock(*send_crit_);
342 for (std::map<uint32_t, VideoSendStream*>::const_iterator it =
343 send_ssrcs_.begin();
344 it != send_ssrcs_.end();
345 ++it) {
346 stats.pacer_delay_ms =
347 std::max(it->second->GetPacerQueuingDelayMs(), stats.pacer_delay_ms);
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000348 int rtt_ms = it->second->GetRtt();
349 if (rtt_ms > 0)
350 stats.rtt_ms = rtt_ms;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000351 }
352 }
353 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000354}
355
pbos@webrtc.org00873182014-11-25 14:03:34 +0000356void Call::SetBitrateConfig(
357 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
358 assert(bitrate_config.min_bitrate_bps >= 0);
359 assert(bitrate_config.max_bitrate_bps == -1 ||
360 bitrate_config.max_bitrate_bps > 0);
361 if (config_.stream_bitrates.min_bitrate_bps ==
362 bitrate_config.min_bitrate_bps &&
363 (bitrate_config.start_bitrate_bps <= 0 ||
364 config_.stream_bitrates.start_bitrate_bps ==
365 bitrate_config.start_bitrate_bps) &&
366 config_.stream_bitrates.max_bitrate_bps ==
367 bitrate_config.max_bitrate_bps) {
368 // Nothing new to set, early abort to avoid encoder reconfigurations.
369 return;
370 }
371 config_.stream_bitrates = bitrate_config;
372 ReadLockScoped read_lock(*send_crit_);
373 for (std::map<uint32_t, VideoSendStream*>::const_iterator it =
374 send_ssrcs_.begin();
375 it != send_ssrcs_.end(); ++it) {
376 it->second->SetBitrateConfig(bitrate_config);
377 }
378}
379
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000380void Call::SignalNetworkState(NetworkState state) {
381 // Take crit for entire function, it needs to be held while updating streams
382 // to guarantee a consistent state across streams.
383 CriticalSectionScoped lock(network_enabled_crit_.get());
384 network_enabled_ = state == kNetworkUp;
385 {
386 ReadLockScoped write_lock(*send_crit_);
387 for (std::map<uint32_t, VideoSendStream*>::iterator it =
388 send_ssrcs_.begin();
389 it != send_ssrcs_.end();
390 ++it) {
391 it->second->SignalNetworkState(state);
392 }
393 }
394 {
395 ReadLockScoped write_lock(*receive_crit_);
396 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
397 receive_ssrcs_.begin();
398 it != receive_ssrcs_.end();
399 ++it) {
400 it->second->SignalNetworkState(state);
401 }
402 }
403}
404
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000405PacketReceiver::DeliveryStatus Call::DeliverRtcp(const uint8_t* packet,
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000406 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000407 // TODO(pbos): Figure out what channel needs it actually.
408 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000409 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
410 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000411 bool rtcp_delivered = false;
pbos@webrtc.org40523702013-08-05 12:49:22 +0000412 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000413 ReadLockScoped read_lock(*receive_crit_);
pbos@webrtc.org40523702013-08-05 12:49:22 +0000414 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
415 receive_ssrcs_.begin();
416 it != receive_ssrcs_.end();
417 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000418 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000419 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000420 }
421 }
422
pbos@webrtc.org40523702013-08-05 12:49:22 +0000423 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000424 ReadLockScoped read_lock(*send_crit_);
pbos@webrtc.org40523702013-08-05 12:49:22 +0000425 for (std::map<uint32_t, VideoSendStream*>::iterator it =
426 send_ssrcs_.begin();
427 it != send_ssrcs_.end();
428 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000429 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000430 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000431 }
432 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000433 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000434}
435
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000436PacketReceiver::DeliveryStatus Call::DeliverRtp(const uint8_t* packet,
437 size_t length) {
438 // Minimum RTP header size.
439 if (length < 12)
440 return DELIVERY_PACKET_ERROR;
441
442 const uint8_t* ptr = &packet[8];
pbos@webrtc.orgdde16f12014-08-05 23:35:43 +0000443 uint32_t ssrc = ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
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