blob: 512e82ca6416a1c02ee31a5fd05384279323efa2 [file] [log] [blame]
pbos@webrtc.org2a9108f2013-05-16 12:08:031/*
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.org3f45c2e2013-08-05 16:22:5311#include <assert.h>
12#include <string.h>
13
pbos@webrtc.org2a9108f2013-05-16 12:08:0314#include <map>
15#include <vector>
16
pbos@webrtc.orgd54aa962014-09-24 06:05:0017#include "webrtc/base/thread_annotations.h"
pbos@webrtc.org24e20892013-10-28 16:32:0118#include "webrtc/call.h"
stefan@webrtc.org47f0c412013-12-04 10:24:2619#include "webrtc/common.h"
pbos@webrtc.orgd05597a2013-12-05 12:11:4720#include "webrtc/config.h"
pbos@webrtc.org24e20892013-10-28 16:32:0121#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
pbos@webrtc.org1c655452014-09-17 09:02:2522#include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
marpan@webrtc.org66373882014-11-01 06:10:4823#include "webrtc/modules/video_coding/codecs/vp9/include/vp9.h"
pbos@webrtc.orgb5d2d162013-10-02 13:36:0924#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
pbos@webrtc.org24e20892013-10-28 16:32:0125#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
pbos@webrtc.orgb5d2d162013-10-02 13:36:0926#include "webrtc/system_wrappers/interface/scoped_ptr.h"
27#include "webrtc/system_wrappers/interface/trace.h"
pbos@webrtc.org24e20892013-10-28 16:32:0128#include "webrtc/video/video_receive_stream.h"
29#include "webrtc/video/video_send_stream.h"
pbos@webrtc.org2a9108f2013-05-16 12:08:0330#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.org9b707ca2014-09-03 16:17:1233#include "webrtc/video_engine/include/vie_network.h"
34#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
pbos@webrtc.org2a9108f2013-05-16 12:08:0335
36namespace webrtc {
pbos@webrtc.orgd05597a2013-12-05 12:11:4737const 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.orgf0a119f2014-07-20 15:27:3540
41bool RtpExtension::IsSupported(const std::string& name) {
42 return name == webrtc::RtpExtension::kTOffset ||
43 name == webrtc::RtpExtension::kAbsSendTime;
44}
45
pbos@webrtc.org1c655452014-09-17 09:02:2546VideoEncoder* VideoEncoder::Create(VideoEncoder::EncoderType codec_type) {
47 switch (codec_type) {
48 case kVp8:
49 return VP8Encoder::Create();
marpan@webrtc.org66373882014-11-01 06:10:4850 case kVp9:
51 return VP9Encoder::Create();
pbos@webrtc.org1c655452014-09-17 09:02:2552 }
53 assert(false);
54 return NULL;
55}
56
pbos@webrtc.org7b5a8962014-10-29 15:28:3957VideoDecoder* VideoDecoder::Create(VideoDecoder::DecoderType codec_type) {
58 switch (codec_type) {
59 case kVp8:
60 return VP8Decoder::Create();
stefan@webrtc.org385606f2014-11-04 19:41:1561 case kVp9:
62 return VP9Decoder::Create();
pbos@webrtc.org7b5a8962014-10-29 15:28:3963 }
64 assert(false);
65 return NULL;
66}
67
pbos@webrtc.org7da30672014-10-14 11:52:1068const int Call::Config::kDefaultStartBitrateBps = 300000;
69
pbos@webrtc.org24e20892013-10-28 16:32:0170namespace internal {
asapersson@webrtc.org8ef65482014-01-31 10:05:0771
72class CpuOveruseObserverProxy : public webrtc::CpuOveruseObserver {
73 public:
pbos@webrtc.orgd3e3c9b2014-10-03 11:25:4574 explicit CpuOveruseObserverProxy(LoadObserver* overuse_callback)
asapersson@webrtc.org8ef65482014-01-31 10:05:0775 : 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.orgc476e642014-04-28 13:00:2183 CriticalSectionScoped lock(crit_.get());
pbos@webrtc.orgd3e3c9b2014-10-03 11:25:4584 overuse_callback_->OnLoadUpdate(LoadObserver::kOveruse);
asapersson@webrtc.org8ef65482014-01-31 10:05:0785 }
86
87 virtual void NormalUsage() OVERRIDE {
pbos@webrtc.orgc476e642014-04-28 13:00:2188 CriticalSectionScoped lock(crit_.get());
pbos@webrtc.orgd3e3c9b2014-10-03 11:25:4589 overuse_callback_->OnLoadUpdate(LoadObserver::kUnderuse);
asapersson@webrtc.org8ef65482014-01-31 10:05:0790 }
91
92 private:
pbos@webrtc.orgc476e642014-04-28 13:00:2193 const scoped_ptr<CriticalSectionWrapper> crit_;
pbos@webrtc.orgd3e3c9b2014-10-03 11:25:4594 LoadObserver* overuse_callback_ GUARDED_BY(crit_);
asapersson@webrtc.org8ef65482014-01-31 10:05:0795};
96
pbos@webrtc.org24e20892013-10-28 16:32:0197class 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.org24e20892013-10-28 16:32:01103
pbos@webrtc.org964d78e2013-11-20 10:40:25104 virtual VideoSendStream* CreateVideoSendStream(
pbos@webrtc.orgbdfcddf2014-06-06 10:49:19105 const VideoSendStream::Config& config,
pbos@webrtc.org58b51402014-09-19 12:30:25106 const VideoEncoderConfig& encoder_config) OVERRIDE;
pbos@webrtc.org24e20892013-10-28 16:32:01107
pbos@webrtc.org12a93e02013-11-21 13:49:43108 virtual void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream)
109 OVERRIDE;
pbos@webrtc.org24e20892013-10-28 16:32:01110
pbos@webrtc.org964d78e2013-11-20 10:40:25111 virtual VideoReceiveStream* CreateVideoReceiveStream(
pbos@webrtc.org24e20892013-10-28 16:32:01112 const VideoReceiveStream::Config& config) OVERRIDE;
113
pbos@webrtc.org12a93e02013-11-21 13:49:43114 virtual void DestroyVideoReceiveStream(
pbos@webrtc.org24e20892013-10-28 16:32:01115 webrtc::VideoReceiveStream* receive_stream) OVERRIDE;
116
stefan@webrtc.org52322672014-11-05 14:05:29117 virtual Stats GetStats() const OVERRIDE;
pbos@webrtc.org24e20892013-10-28 16:32:01118
pbos@webrtc.orgbc57e0f2014-05-14 13:57:12119 virtual DeliveryStatus DeliverPacket(const uint8_t* packet,
120 size_t length) OVERRIDE;
pbos@webrtc.org24e20892013-10-28 16:32:01121
pbos@webrtc.org5e3f6b42014-11-25 14:03:34122 virtual void SetBitrateConfig(
123 const webrtc::Call::Config::BitrateConfig& bitrate_config) OVERRIDE;
pbos@webrtc.org9b707ca2014-09-03 16:17:12124 virtual void SignalNetworkState(NetworkState state) OVERRIDE;
125
pbos@webrtc.org24e20892013-10-28 16:32:01126 private:
pbos@webrtc.orgbc57e0f2014-05-14 13:57:12127 DeliveryStatus DeliverRtcp(const uint8_t* packet, size_t length);
pbos@webrtc.orgf8be3d22014-07-08 07:38:12128 DeliveryStatus DeliverRtp(const uint8_t* packet, size_t length);
pbos@webrtc.org24e20892013-10-28 16:32:01129
130 Call::Config config_;
131
pbos@webrtc.org9b707ca2014-09-03 16:17:12132 // 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.org24e20892013-10-28 16:32:01137
pbos@webrtc.org9b707ca2014-09-03 16:17:12138 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.org24e20892013-10-28 16:32:01144
asapersson@webrtc.org8ef65482014-01-31 10:05:07145 scoped_ptr<CpuOveruseObserverProxy> overuse_observer_proxy_;
146
pbos@webrtc.org2fd91bd2014-07-07 13:06:48147 VideoSendStream::RtpStateMap suspended_send_ssrcs_;
148
mflodman@webrtc.orgdadfc9e2013-12-13 09:40:45149 VideoEngine* video_engine_;
pbos@webrtc.org24e20892013-10-28 16:32:01150 ViERTP_RTCP* rtp_rtcp_;
151 ViECodec* codec_;
mflodman@webrtc.orgdadfc9e2013-12-13 09:40:45152 ViEBase* base_;
153 int base_channel_id_;
pbos@webrtc.org24e20892013-10-28 16:32:01154
155 DISALLOW_COPY_AND_ASSIGN(Call);
156};
pbos@webrtc.orgd05597a2013-12-05 12:11:47157} // namespace internal
pbos@webrtc.orgc2014fd2013-08-14 13:52:52158
stefan@webrtc.org47f0c412013-12-04 10:24:26159Call* Call::Create(const Call::Config& config) {
pbos@webrtc.orgc71929d2014-01-24 09:30:53160 VideoEngine* video_engine = config.webrtc_config != NULL
161 ? VideoEngine::Create(*config.webrtc_config)
162 : VideoEngine::Create();
pbos@webrtc.orgc2014fd2013-08-14 13:52:52163 assert(video_engine != NULL);
164
pbos@webrtc.orgbf6d5722013-09-09 15:04:25165 return new internal::Call(video_engine, config);
pbos@webrtc.orgc2014fd2013-08-14 13:52:52166}
pbos@webrtc.orgc2014fd2013-08-14 13:52:52167
pbos@webrtc.org2a9108f2013-05-16 12:08:03168namespace internal {
169
pbos@webrtc.orgbf6d5722013-09-09 15:04:25170Call::Call(webrtc::VideoEngine* video_engine, const Call::Config& config)
mflodman@webrtc.orgbf76ae22013-07-23 11:35:00171 : config_(config),
pbos@webrtc.org9b707ca2014-09-03 16:17:12172 network_enabled_crit_(CriticalSectionWrapper::CreateCriticalSection()),
173 network_enabled_(true),
174 receive_crit_(RWLockWrapper::CreateRWLock()),
175 send_crit_(RWLockWrapper::CreateRWLock()),
mflodman@webrtc.orgdadfc9e2013-12-13 09:40:45176 video_engine_(video_engine),
177 base_channel_id_(-1) {
pbos@webrtc.org2a9108f2013-05-16 12:08:03178 assert(video_engine != NULL);
mflodman@webrtc.orgbf76ae22013-07-23 11:35:00179 assert(config.send_transport != NULL);
pbos@webrtc.org2a9108f2013-05-16 12:08:03180
pbos@webrtc.org5e3f6b42014-11-25 14:03:34181 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.org8ef65482014-01-31 10:05:07189 if (config.overuse_callback) {
190 overuse_observer_proxy_.reset(
191 new CpuOveruseObserverProxy(config.overuse_callback));
192 }
193
pbos@webrtc.org2a9108f2013-05-16 12:08:03194 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.orgdadfc9e2013-12-13 09:40:45199
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.org2a9108f2013-05-16 12:08:03207}
208
pbos@webrtc.orgbf6d5722013-09-09 15:04:25209Call::~Call() {
mflodman@webrtc.orgdadfc9e2013-12-13 09:40:45210 base_->DeleteChannel(base_channel_id_);
211 base_->Release();
pbos@webrtc.org2a9108f2013-05-16 12:08:03212 codec_->Release();
pbos@webrtc.orgc2014fd2013-08-14 13:52:52213 rtp_rtcp_->Release();
214 webrtc::VideoEngine::Delete(video_engine_);
pbos@webrtc.org2a9108f2013-05-16 12:08:03215}
216
pbos@webrtc.orgbf6d5722013-09-09 15:04:25217PacketReceiver* Call::Receiver() { return this; }
pbos@webrtc.org2a9108f2013-05-16 12:08:03218
pbos@webrtc.org964d78e2013-11-20 10:40:25219VideoSendStream* Call::CreateVideoSendStream(
pbos@webrtc.orgbdfcddf2014-06-06 10:49:19220 const VideoSendStream::Config& config,
pbos@webrtc.org58b51402014-09-19 12:30:25221 const VideoEncoderConfig& encoder_config) {
pbos@webrtc.org63988b22013-06-10 13:48:26222 assert(config.rtp.ssrcs.size() > 0);
pbos@webrtc.org63988b22013-06-10 13:48:26223
mflodman@webrtc.orgf89ce462014-06-16 08:57:39224 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
225 // the call has already started.
pbos@webrtc.org5e3f6b42014-11-25 14:03:34226 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.org63988b22013-06-10 13:48:26230
pbos@webrtc.org9b707ca2014-09-03 16:17:12231 // 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.org63988b22013-06-10 13:48:26235 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.org2a9108f2013-05-16 12:08:03238 }
pbos@webrtc.org9b707ca2014-09-03 16:17:12239 if (!network_enabled_)
240 send_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org2a9108f2013-05-16 12:08:03241 return send_stream;
242}
243
pbos@webrtc.org12a93e02013-11-21 13:49:43244void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org00208582013-09-05 12:38:54245 assert(send_stream != NULL);
246
pbos@webrtc.org2fd91bd2014-07-07 13:06:48247 send_stream->Stop();
248
pbos@webrtc.org00208582013-09-05 12:38:54249 VideoSendStream* send_stream_impl = NULL;
250 {
pbos@webrtc.org9b707ca2014-09-03 16:17:12251 WriteLockScoped write_lock(*send_crit_);
pbos@webrtc.org2fd91bd2014-07-07 13:06:48252 std::map<uint32_t, VideoSendStream*>::iterator it = send_ssrcs_.begin();
253 while (it != send_ssrcs_.end()) {
pbos@webrtc.org00208582013-09-05 12:38:54254 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
255 send_stream_impl = it->second;
pbos@webrtc.org2fd91bd2014-07-07 13:06:48256 send_ssrcs_.erase(it++);
257 } else {
258 ++it;
pbos@webrtc.org00208582013-09-05 12:38:54259 }
260 }
pbos@webrtc.org2a9108f2013-05-16 12:08:03261 }
pbos@webrtc.org00208582013-09-05 12:38:54262
pbos@webrtc.org2fd91bd2014-07-07 13:06:48263 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.org00208582013-09-05 12:38:54271 assert(send_stream_impl != NULL);
272 delete send_stream_impl;
pbos@webrtc.org2a9108f2013-05-16 12:08:03273}
274
pbos@webrtc.org964d78e2013-11-20 10:40:25275VideoReceiveStream* Call::CreateVideoReceiveStream(
pbos@webrtc.orgc1797062013-08-23 09:19:30276 const VideoReceiveStream::Config& config) {
mflodman@webrtc.orgdadfc9e2013-12-13 09:40:45277 VideoReceiveStream* receive_stream =
278 new VideoReceiveStream(video_engine_,
279 config,
280 config_.send_transport,
281 config_.voice_engine,
282 base_channel_id_);
pbos@webrtc.org2a9108f2013-05-16 12:08:03283
pbos@webrtc.org9b707ca2014-09-03 16:17:12284 // 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.org4b50db12013-12-03 10:13:04288 assert(receive_ssrcs_.find(config.rtp.remote_ssrc) == receive_ssrcs_.end());
289 receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc71929d2014-01-24 09:30:53290 // 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.org9b707ca2014-09-03 16:17:12296 if (!network_enabled_)
297 receive_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org2a9108f2013-05-16 12:08:03298 return receive_stream;
299}
300
pbos@webrtc.org12a93e02013-11-21 13:49:43301void Call::DestroyVideoReceiveStream(
302 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org00208582013-09-05 12:38:54303 assert(receive_stream != NULL);
304
305 VideoReceiveStream* receive_stream_impl = NULL;
306 {
pbos@webrtc.org9b707ca2014-09-03 16:17:12307 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc71929d2014-01-24 09:30:53308 // 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.org00208582013-09-05 12:38:54313 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
solenberg@webrtc.org2bb7ad52014-01-29 11:21:58314 assert(receive_stream_impl == NULL ||
315 receive_stream_impl == it->second);
pbos@webrtc.org00208582013-09-05 12:38:54316 receive_stream_impl = it->second;
pbos@webrtc.orgc71929d2014-01-24 09:30:53317 receive_ssrcs_.erase(it++);
318 } else {
319 ++it;
pbos@webrtc.org00208582013-09-05 12:38:54320 }
321 }
pbos@webrtc.org2a9108f2013-05-16 12:08:03322 }
pbos@webrtc.org00208582013-09-05 12:38:54323
324 assert(receive_stream_impl != NULL);
325 delete receive_stream_impl;
pbos@webrtc.org2a9108f2013-05-16 12:08:03326}
327
stefan@webrtc.org52322672014-11-05 14:05:29328Call::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.org2a9108f2013-05-16 12:08:03348}
349
pbos@webrtc.org5e3f6b42014-11-25 14:03:34350void 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.org9b707ca2014-09-03 16:17:12374void 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.orgf8be3d22014-07-08 07:38:12399PacketReceiver::DeliveryStatus Call::DeliverRtcp(const uint8_t* packet,
pbos@webrtc.orgbc57e0f2014-05-14 13:57:12400 size_t length) {
pbos@webrtc.org2a9108f2013-05-16 12:08:03401 // TODO(pbos): Figure out what channel needs it actually.
402 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgbc57e0f2014-05-14 13:57:12403 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
404 // there's no receiver of the packet.
pbos@webrtc.org2a9108f2013-05-16 12:08:03405 bool rtcp_delivered = false;
pbos@webrtc.org78ab5112013-08-05 12:49:22406 {
pbos@webrtc.org9b707ca2014-09-03 16:17:12407 ReadLockScoped read_lock(*receive_crit_);
pbos@webrtc.org78ab5112013-08-05 12:49:22408 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
409 receive_ssrcs_.begin();
410 it != receive_ssrcs_.end();
411 ++it) {
pbos@webrtc.org00112522013-09-20 11:56:26412 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org78ab5112013-08-05 12:49:22413 rtcp_delivered = true;
pbos@webrtc.orgce851092013-08-05 12:01:36414 }
415 }
416
pbos@webrtc.org78ab5112013-08-05 12:49:22417 {
pbos@webrtc.org9b707ca2014-09-03 16:17:12418 ReadLockScoped read_lock(*send_crit_);
pbos@webrtc.org78ab5112013-08-05 12:49:22419 for (std::map<uint32_t, VideoSendStream*>::iterator it =
420 send_ssrcs_.begin();
421 it != send_ssrcs_.end();
422 ++it) {
pbos@webrtc.org00112522013-09-20 11:56:26423 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org78ab5112013-08-05 12:49:22424 rtcp_delivered = true;
pbos@webrtc.org2a9108f2013-05-16 12:08:03425 }
426 }
pbos@webrtc.orgbc57e0f2014-05-14 13:57:12427 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org2a9108f2013-05-16 12:08:03428}
429
pbos@webrtc.orgf8be3d22014-07-08 07:38:12430PacketReceiver::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.org6c0337d2014-08-05 23:35:43437 uint32_t ssrc = ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
pbos@webrtc.orgf8be3d22014-07-08 07:38:12438
pbos@webrtc.org9b707ca2014-09-03 16:17:12439 ReadLockScoped read_lock(*receive_crit_);
solenberg@webrtc.org2bb7ad52014-01-29 11:21:58440 std::map<uint32_t, VideoReceiveStream*>::iterator it =
pbos@webrtc.orgf8be3d22014-07-08 07:38:12441 receive_ssrcs_.find(ssrc);
pbos@webrtc.orgbc57e0f2014-05-14 13:57:12442
443 if (it == receive_ssrcs_.end())
444 return DELIVERY_UNKNOWN_SSRC;
445
pbos@webrtc.orgf8be3d22014-07-08 07:38:12446 return it->second->DeliverRtp(packet, length) ? DELIVERY_OK
447 : DELIVERY_PACKET_ERROR;
pbos@webrtc.org2a9108f2013-05-16 12:08:03448}
449
pbos@webrtc.orgf8be3d22014-07-08 07:38:12450PacketReceiver::DeliveryStatus Call::DeliverPacket(const uint8_t* packet,
451 size_t length) {
pbos@webrtc.org6aae61c2014-07-08 12:10:51452 if (RtpHeaderParser::IsRtcp(packet, length))
pbos@webrtc.org78ab5112013-08-05 12:49:22453 return DeliverRtcp(packet, length);
pbos@webrtc.org2a9108f2013-05-16 12:08:03454
pbos@webrtc.orgf8be3d22014-07-08 07:38:12455 return DeliverRtp(packet, length);
pbos@webrtc.org2a9108f2013-05-16 12:08:03456}
457
458} // namespace internal
459} // namespace webrtc