blob: b03b6c91a2f1e753fec5836c97a6f4e1dd761004 [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
117 virtual uint32_t SendBitrateEstimate() OVERRIDE;
118 virtual uint32_t ReceiveBitrateEstimate() OVERRIDE;
119
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.org26c0c412014-09-03 16:17:12 +0000123 virtual void SignalNetworkState(NetworkState state) OVERRIDE;
124
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000125 private:
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000126 DeliveryStatus DeliverRtcp(const uint8_t* packet, size_t length);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000127 DeliveryStatus DeliverRtp(const uint8_t* packet, size_t length);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000128
129 Call::Config config_;
130
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000131 // Needs to be held while write-locking |receive_crit_| or |send_crit_|. This
132 // ensures that we have a consistent network state signalled to all senders
133 // and receivers.
134 scoped_ptr<CriticalSectionWrapper> network_enabled_crit_;
135 bool network_enabled_ GUARDED_BY(network_enabled_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000136
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000137 scoped_ptr<RWLockWrapper> receive_crit_;
138 std::map<uint32_t, VideoReceiveStream*> receive_ssrcs_
139 GUARDED_BY(receive_crit_);
140
141 scoped_ptr<RWLockWrapper> send_crit_;
142 std::map<uint32_t, VideoSendStream*> send_ssrcs_ GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000143
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000144 scoped_ptr<CpuOveruseObserverProxy> overuse_observer_proxy_;
145
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000146 VideoSendStream::RtpStateMap suspended_send_ssrcs_;
147
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000148 VideoEngine* video_engine_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000149 ViERTP_RTCP* rtp_rtcp_;
150 ViECodec* codec_;
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000151 ViEBase* base_;
152 int base_channel_id_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000153
154 DISALLOW_COPY_AND_ASSIGN(Call);
155};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000156} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000157
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000158Call* Call::Create(const Call::Config& config) {
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000159 VideoEngine* video_engine = config.webrtc_config != NULL
160 ? VideoEngine::Create(*config.webrtc_config)
161 : VideoEngine::Create();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000162 assert(video_engine != NULL);
163
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000164 return new internal::Call(video_engine, config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000165}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000166
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000167namespace internal {
168
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000169Call::Call(webrtc::VideoEngine* video_engine, const Call::Config& config)
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000170 : config_(config),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000171 network_enabled_crit_(CriticalSectionWrapper::CreateCriticalSection()),
172 network_enabled_(true),
173 receive_crit_(RWLockWrapper::CreateRWLock()),
174 send_crit_(RWLockWrapper::CreateRWLock()),
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000175 video_engine_(video_engine),
176 base_channel_id_(-1) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000177 assert(video_engine != NULL);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000178 assert(config.send_transport != NULL);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000179
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000180 if (config.overuse_callback) {
181 overuse_observer_proxy_.reset(
182 new CpuOveruseObserverProxy(config.overuse_callback));
183 }
184
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000185 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
186 assert(rtp_rtcp_ != NULL);
187
188 codec_ = ViECodec::GetInterface(video_engine_);
189 assert(codec_ != NULL);
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000190
191 // As a workaround for non-existing calls in the old API, create a base
192 // channel used as default channel when creating send and receive streams.
193 base_ = ViEBase::GetInterface(video_engine_);
194 assert(base_ != NULL);
195
196 base_->CreateChannel(base_channel_id_);
197 assert(base_channel_id_ != -1);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000198}
199
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000200Call::~Call() {
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000201 base_->DeleteChannel(base_channel_id_);
202 base_->Release();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000203 codec_->Release();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000204 rtp_rtcp_->Release();
205 webrtc::VideoEngine::Delete(video_engine_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000206}
207
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000208PacketReceiver* Call::Receiver() { return this; }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000209
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000210VideoSendStream* Call::CreateVideoSendStream(
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000211 const VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000212 const VideoEncoderConfig& encoder_config) {
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000213 assert(config.rtp.ssrcs.size() > 0);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000214
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000215 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
216 // the call has already started.
pbos@webrtc.orga73a6782014-10-14 11:52:10 +0000217 VideoSendStream* send_stream =
218 new VideoSendStream(config_.send_transport,
219 overuse_observer_proxy_.get(),
220 video_engine_,
221 config,
222 encoder_config,
223 suspended_send_ssrcs_,
224 base_channel_id_,
225 config_.stream_start_bitrate_bps);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000226
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000227 // This needs to be taken before send_crit_ as both locks need to be held
228 // while changing network state.
229 CriticalSectionScoped lock(network_enabled_crit_.get());
230 WriteLockScoped write_lock(*send_crit_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000231 for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) {
232 assert(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end());
233 send_ssrcs_[config.rtp.ssrcs[i]] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000234 }
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000235 if (!network_enabled_)
236 send_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000237 return send_stream;
238}
239
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000240void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000241 assert(send_stream != NULL);
242
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000243 send_stream->Stop();
244
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000245 VideoSendStream* send_stream_impl = NULL;
246 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000247 WriteLockScoped write_lock(*send_crit_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000248 std::map<uint32_t, VideoSendStream*>::iterator it = send_ssrcs_.begin();
249 while (it != send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000250 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
251 send_stream_impl = it->second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000252 send_ssrcs_.erase(it++);
253 } else {
254 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000255 }
256 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000257 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000258
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000259 VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates();
260
261 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
262 it != rtp_state.end();
263 ++it) {
264 suspended_send_ssrcs_[it->first] = it->second;
265 }
266
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000267 assert(send_stream_impl != NULL);
268 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000269}
270
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000271VideoReceiveStream* Call::CreateVideoReceiveStream(
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000272 const VideoReceiveStream::Config& config) {
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000273 VideoReceiveStream* receive_stream =
274 new VideoReceiveStream(video_engine_,
275 config,
276 config_.send_transport,
277 config_.voice_engine,
278 base_channel_id_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000279
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000280 // This needs to be taken before receive_crit_ as both locks need to be held
281 // while changing network state.
282 CriticalSectionScoped lock(network_enabled_crit_.get());
283 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgb613b5a2013-12-03 10:13:04 +0000284 assert(receive_ssrcs_.find(config.rtp.remote_ssrc) == receive_ssrcs_.end());
285 receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000286 // TODO(pbos): Configure different RTX payloads per receive payload.
287 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
288 config.rtp.rtx.begin();
289 if (it != config.rtp.rtx.end())
290 receive_ssrcs_[it->second.ssrc] = receive_stream;
291
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000292 if (!network_enabled_)
293 receive_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000294 return receive_stream;
295}
296
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000297void Call::DestroyVideoReceiveStream(
298 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000299 assert(receive_stream != NULL);
300
301 VideoReceiveStream* receive_stream_impl = NULL;
302 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000303 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000304 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
305 // separate SSRC there can be either one or two.
306 std::map<uint32_t, VideoReceiveStream*>::iterator it =
307 receive_ssrcs_.begin();
308 while (it != receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000309 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
solenberg@webrtc.org094ac392014-01-29 11:21:58 +0000310 assert(receive_stream_impl == NULL ||
311 receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000312 receive_stream_impl = it->second;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000313 receive_ssrcs_.erase(it++);
314 } else {
315 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000316 }
317 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000318 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000319
320 assert(receive_stream_impl != NULL);
321 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000322}
323
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000324uint32_t Call::SendBitrateEstimate() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000325 // TODO(pbos): Return send-bitrate estimate
326 return 0;
327}
328
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000329uint32_t Call::ReceiveBitrateEstimate() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000330 // TODO(pbos): Return receive-bitrate estimate
331 return 0;
332}
333
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000334void Call::SignalNetworkState(NetworkState state) {
335 // Take crit for entire function, it needs to be held while updating streams
336 // to guarantee a consistent state across streams.
337 CriticalSectionScoped lock(network_enabled_crit_.get());
338 network_enabled_ = state == kNetworkUp;
339 {
340 ReadLockScoped write_lock(*send_crit_);
341 for (std::map<uint32_t, VideoSendStream*>::iterator it =
342 send_ssrcs_.begin();
343 it != send_ssrcs_.end();
344 ++it) {
345 it->second->SignalNetworkState(state);
346 }
347 }
348 {
349 ReadLockScoped write_lock(*receive_crit_);
350 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
351 receive_ssrcs_.begin();
352 it != receive_ssrcs_.end();
353 ++it) {
354 it->second->SignalNetworkState(state);
355 }
356 }
357}
358
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000359PacketReceiver::DeliveryStatus Call::DeliverRtcp(const uint8_t* packet,
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000360 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000361 // TODO(pbos): Figure out what channel needs it actually.
362 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000363 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
364 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000365 bool rtcp_delivered = false;
pbos@webrtc.org40523702013-08-05 12:49:22 +0000366 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000367 ReadLockScoped read_lock(*receive_crit_);
pbos@webrtc.org40523702013-08-05 12:49:22 +0000368 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
369 receive_ssrcs_.begin();
370 it != receive_ssrcs_.end();
371 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000372 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000373 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000374 }
375 }
376
pbos@webrtc.org40523702013-08-05 12:49:22 +0000377 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000378 ReadLockScoped read_lock(*send_crit_);
pbos@webrtc.org40523702013-08-05 12:49:22 +0000379 for (std::map<uint32_t, VideoSendStream*>::iterator it =
380 send_ssrcs_.begin();
381 it != send_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.org29d58392013-05-16 12:08:03 +0000385 }
386 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000387 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000388}
389
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000390PacketReceiver::DeliveryStatus Call::DeliverRtp(const uint8_t* packet,
391 size_t length) {
392 // Minimum RTP header size.
393 if (length < 12)
394 return DELIVERY_PACKET_ERROR;
395
396 const uint8_t* ptr = &packet[8];
pbos@webrtc.orgdde16f12014-08-05 23:35:43 +0000397 uint32_t ssrc = ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000398
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000399 ReadLockScoped read_lock(*receive_crit_);
solenberg@webrtc.org094ac392014-01-29 11:21:58 +0000400 std::map<uint32_t, VideoReceiveStream*>::iterator it =
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000401 receive_ssrcs_.find(ssrc);
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000402
403 if (it == receive_ssrcs_.end())
404 return DELIVERY_UNKNOWN_SSRC;
405
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000406 return it->second->DeliverRtp(packet, length) ? DELIVERY_OK
407 : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000408}
409
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000410PacketReceiver::DeliveryStatus Call::DeliverPacket(const uint8_t* packet,
411 size_t length) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000412 if (RtpHeaderParser::IsRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000413 return DeliverRtcp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000414
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000415 return DeliverRtp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000416}
417
418} // namespace internal
419} // namespace webrtc