blob: 2b4f76f4c5399aa077a7a421d1e2ce8bef064e3a [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.org26c0c412014-09-03 16:17:12 +0000122 virtual void SignalNetworkState(NetworkState state) OVERRIDE;
123
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000124 private:
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000125 DeliveryStatus DeliverRtcp(const uint8_t* packet, size_t length);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000126 DeliveryStatus DeliverRtp(const uint8_t* packet, size_t length);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000127
128 Call::Config config_;
129
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000130 // Needs to be held while write-locking |receive_crit_| or |send_crit_|. This
131 // ensures that we have a consistent network state signalled to all senders
132 // and receivers.
133 scoped_ptr<CriticalSectionWrapper> network_enabled_crit_;
134 bool network_enabled_ GUARDED_BY(network_enabled_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000135
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000136 scoped_ptr<RWLockWrapper> receive_crit_;
137 std::map<uint32_t, VideoReceiveStream*> receive_ssrcs_
138 GUARDED_BY(receive_crit_);
139
140 scoped_ptr<RWLockWrapper> send_crit_;
141 std::map<uint32_t, VideoSendStream*> send_ssrcs_ GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000142
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000143 scoped_ptr<CpuOveruseObserverProxy> overuse_observer_proxy_;
144
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000145 VideoSendStream::RtpStateMap suspended_send_ssrcs_;
146
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000147 VideoEngine* video_engine_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000148 ViERTP_RTCP* rtp_rtcp_;
149 ViECodec* codec_;
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000150 ViEBase* base_;
151 int base_channel_id_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000152
153 DISALLOW_COPY_AND_ASSIGN(Call);
154};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000155} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000156
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000157Call* Call::Create(const Call::Config& config) {
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000158 VideoEngine* video_engine = config.webrtc_config != NULL
159 ? VideoEngine::Create(*config.webrtc_config)
160 : VideoEngine::Create();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000161 assert(video_engine != NULL);
162
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000163 return new internal::Call(video_engine, config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000164}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000165
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000166namespace internal {
167
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000168Call::Call(webrtc::VideoEngine* video_engine, const Call::Config& config)
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000169 : config_(config),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000170 network_enabled_crit_(CriticalSectionWrapper::CreateCriticalSection()),
171 network_enabled_(true),
172 receive_crit_(RWLockWrapper::CreateRWLock()),
173 send_crit_(RWLockWrapper::CreateRWLock()),
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000174 video_engine_(video_engine),
175 base_channel_id_(-1) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000176 assert(video_engine != NULL);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000177 assert(config.send_transport != NULL);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000178
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000179 if (config.overuse_callback) {
180 overuse_observer_proxy_.reset(
181 new CpuOveruseObserverProxy(config.overuse_callback));
182 }
183
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000184 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
185 assert(rtp_rtcp_ != NULL);
186
187 codec_ = ViECodec::GetInterface(video_engine_);
188 assert(codec_ != NULL);
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000189
190 // As a workaround for non-existing calls in the old API, create a base
191 // channel used as default channel when creating send and receive streams.
192 base_ = ViEBase::GetInterface(video_engine_);
193 assert(base_ != NULL);
194
195 base_->CreateChannel(base_channel_id_);
196 assert(base_channel_id_ != -1);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000197}
198
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000199Call::~Call() {
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000200 base_->DeleteChannel(base_channel_id_);
201 base_->Release();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000202 codec_->Release();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000203 rtp_rtcp_->Release();
204 webrtc::VideoEngine::Delete(video_engine_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000205}
206
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000207PacketReceiver* Call::Receiver() { return this; }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000208
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000209VideoSendStream* Call::CreateVideoSendStream(
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000210 const VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000211 const VideoEncoderConfig& encoder_config) {
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000212 assert(config.rtp.ssrcs.size() > 0);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000213
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000214 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
215 // the call has already started.
pbos@webrtc.orga73a6782014-10-14 11:52:10 +0000216 VideoSendStream* send_stream =
217 new VideoSendStream(config_.send_transport,
218 overuse_observer_proxy_.get(),
219 video_engine_,
220 config,
221 encoder_config,
222 suspended_send_ssrcs_,
223 base_channel_id_,
224 config_.stream_start_bitrate_bps);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000225
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000226 // This needs to be taken before send_crit_ as both locks need to be held
227 // while changing network state.
228 CriticalSectionScoped lock(network_enabled_crit_.get());
229 WriteLockScoped write_lock(*send_crit_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000230 for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) {
231 assert(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end());
232 send_ssrcs_[config.rtp.ssrcs[i]] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000233 }
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000234 if (!network_enabled_)
235 send_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000236 return send_stream;
237}
238
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000239void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000240 assert(send_stream != NULL);
241
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000242 send_stream->Stop();
243
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000244 VideoSendStream* send_stream_impl = NULL;
245 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000246 WriteLockScoped write_lock(*send_crit_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000247 std::map<uint32_t, VideoSendStream*>::iterator it = send_ssrcs_.begin();
248 while (it != send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000249 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
250 send_stream_impl = it->second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000251 send_ssrcs_.erase(it++);
252 } else {
253 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000254 }
255 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000256 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000257
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000258 VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates();
259
260 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
261 it != rtp_state.end();
262 ++it) {
263 suspended_send_ssrcs_[it->first] = it->second;
264 }
265
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000266 assert(send_stream_impl != NULL);
267 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000268}
269
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000270VideoReceiveStream* Call::CreateVideoReceiveStream(
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000271 const VideoReceiveStream::Config& config) {
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000272 VideoReceiveStream* receive_stream =
273 new VideoReceiveStream(video_engine_,
274 config,
275 config_.send_transport,
276 config_.voice_engine,
277 base_channel_id_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000278
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000279 // This needs to be taken before receive_crit_ as both locks need to be held
280 // while changing network state.
281 CriticalSectionScoped lock(network_enabled_crit_.get());
282 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgb613b5a2013-12-03 10:13:04 +0000283 assert(receive_ssrcs_.find(config.rtp.remote_ssrc) == receive_ssrcs_.end());
284 receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000285 // TODO(pbos): Configure different RTX payloads per receive payload.
286 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
287 config.rtp.rtx.begin();
288 if (it != config.rtp.rtx.end())
289 receive_ssrcs_[it->second.ssrc] = receive_stream;
290
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000291 if (!network_enabled_)
292 receive_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000293 return receive_stream;
294}
295
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000296void Call::DestroyVideoReceiveStream(
297 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000298 assert(receive_stream != NULL);
299
300 VideoReceiveStream* receive_stream_impl = NULL;
301 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000302 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000303 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
304 // separate SSRC there can be either one or two.
305 std::map<uint32_t, VideoReceiveStream*>::iterator it =
306 receive_ssrcs_.begin();
307 while (it != receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000308 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
solenberg@webrtc.org094ac392014-01-29 11:21:58 +0000309 assert(receive_stream_impl == NULL ||
310 receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000311 receive_stream_impl = it->second;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000312 receive_ssrcs_.erase(it++);
313 } else {
314 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000315 }
316 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000317 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000318
319 assert(receive_stream_impl != NULL);
320 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000321}
322
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000323Call::Stats Call::GetStats() const {
324 Stats stats;
325 // Ignoring return values.
326 uint32_t send_bandwidth = 0;
327 rtp_rtcp_->GetEstimatedSendBandwidth(base_channel_id_, &send_bandwidth);
328 stats.send_bandwidth_bps = send_bandwidth;
329 uint32_t recv_bandwidth = 0;
330 rtp_rtcp_->GetEstimatedReceiveBandwidth(base_channel_id_, &recv_bandwidth);
331 stats.recv_bandwidth_bps = recv_bandwidth;
332 {
333 ReadLockScoped read_lock(*send_crit_);
334 for (std::map<uint32_t, VideoSendStream*>::const_iterator it =
335 send_ssrcs_.begin();
336 it != send_ssrcs_.end();
337 ++it) {
338 stats.pacer_delay_ms =
339 std::max(it->second->GetPacerQueuingDelayMs(), stats.pacer_delay_ms);
340 }
341 }
342 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000343}
344
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000345void Call::SignalNetworkState(NetworkState state) {
346 // Take crit for entire function, it needs to be held while updating streams
347 // to guarantee a consistent state across streams.
348 CriticalSectionScoped lock(network_enabled_crit_.get());
349 network_enabled_ = state == kNetworkUp;
350 {
351 ReadLockScoped write_lock(*send_crit_);
352 for (std::map<uint32_t, VideoSendStream*>::iterator it =
353 send_ssrcs_.begin();
354 it != send_ssrcs_.end();
355 ++it) {
356 it->second->SignalNetworkState(state);
357 }
358 }
359 {
360 ReadLockScoped write_lock(*receive_crit_);
361 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
362 receive_ssrcs_.begin();
363 it != receive_ssrcs_.end();
364 ++it) {
365 it->second->SignalNetworkState(state);
366 }
367 }
368}
369
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000370PacketReceiver::DeliveryStatus Call::DeliverRtcp(const uint8_t* packet,
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000371 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000372 // TODO(pbos): Figure out what channel needs it actually.
373 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000374 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
375 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000376 bool rtcp_delivered = false;
pbos@webrtc.org40523702013-08-05 12:49:22 +0000377 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000378 ReadLockScoped read_lock(*receive_crit_);
pbos@webrtc.org40523702013-08-05 12:49:22 +0000379 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
380 receive_ssrcs_.begin();
381 it != receive_ssrcs_.end();
382 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000383 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000384 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000385 }
386 }
387
pbos@webrtc.org40523702013-08-05 12:49:22 +0000388 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000389 ReadLockScoped read_lock(*send_crit_);
pbos@webrtc.org40523702013-08-05 12:49:22 +0000390 for (std::map<uint32_t, VideoSendStream*>::iterator it =
391 send_ssrcs_.begin();
392 it != send_ssrcs_.end();
393 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000394 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000395 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000396 }
397 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000398 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000399}
400
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000401PacketReceiver::DeliveryStatus Call::DeliverRtp(const uint8_t* packet,
402 size_t length) {
403 // Minimum RTP header size.
404 if (length < 12)
405 return DELIVERY_PACKET_ERROR;
406
407 const uint8_t* ptr = &packet[8];
pbos@webrtc.orgdde16f12014-08-05 23:35:43 +0000408 uint32_t ssrc = ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000409
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000410 ReadLockScoped read_lock(*receive_crit_);
solenberg@webrtc.org094ac392014-01-29 11:21:58 +0000411 std::map<uint32_t, VideoReceiveStream*>::iterator it =
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000412 receive_ssrcs_.find(ssrc);
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000413
414 if (it == receive_ssrcs_.end())
415 return DELIVERY_UNKNOWN_SSRC;
416
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000417 return it->second->DeliverRtp(packet, length) ? DELIVERY_OK
418 : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000419}
420
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000421PacketReceiver::DeliveryStatus Call::DeliverPacket(const uint8_t* packet,
422 size_t length) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000423 if (RtpHeaderParser::IsRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000424 return DeliverRtcp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000425
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000426 return DeliverRtp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000427}
428
429} // namespace internal
430} // namespace webrtc