blob: 8d579d326269cf4b5c7c907f1dfe13a1b674bda0 [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 <string.h>
12
pbos@webrtc.org29d58392013-05-16 12:08:03 +000013#include <map>
14#include <vector>
15
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +000016#include "webrtc/base/checks.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000017#include "webrtc/base/scoped_ptr.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000018#include "webrtc/base/thread_annotations.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000019#include "webrtc/call.h"
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000020#include "webrtc/common.h"
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +000021#include "webrtc/config.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000022#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +000023#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000024#include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
marpan@webrtc.org5b883172014-11-01 06:10:48 +000025#include "webrtc/modules/video_coding/codecs/vp9/include/vp9.h"
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +000026#include "webrtc/modules/video_render/include/video_render.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000027#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
pbos@webrtc.org32e85282015-01-15 10:09:39 +000028#include "webrtc/system_wrappers/interface/logging.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000029#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
pbos@webrtc.orgde74b642013-10-02 13:36:09 +000030#include "webrtc/system_wrappers/interface/trace.h"
pbos@webrtc.org50fe3592015-01-29 12:33:07 +000031#include "webrtc/system_wrappers/interface/trace_event.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000032#include "webrtc/video/video_receive_stream.h"
33#include "webrtc/video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000034#include "webrtc/video_engine/include/vie_base.h"
35#include "webrtc/video_engine/include/vie_codec.h"
36#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
pbos@webrtc.org26c0c412014-09-03 16:17:12 +000037#include "webrtc/video_engine/include/vie_network.h"
38#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000039
40namespace webrtc {
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +000041const char* RtpExtension::kTOffset = "urn:ietf:params:rtp-hdrext:toffset";
42const char* RtpExtension::kAbsSendTime =
43 "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time";
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +000044const char* RtpExtension::kVideoRotation = "urn:3gpp:video-orientation";
pbos@webrtc.org3c107582014-07-20 15:27:35 +000045
46bool RtpExtension::IsSupported(const std::string& name) {
47 return name == webrtc::RtpExtension::kTOffset ||
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +000048 name == webrtc::RtpExtension::kAbsSendTime ||
49 name == webrtc::RtpExtension::kVideoRotation;
pbos@webrtc.org3c107582014-07-20 15:27:35 +000050}
51
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000052VideoEncoder* VideoEncoder::Create(VideoEncoder::EncoderType codec_type) {
53 switch (codec_type) {
54 case kVp8:
55 return VP8Encoder::Create();
marpan@webrtc.org5b883172014-11-01 06:10:48 +000056 case kVp9:
57 return VP9Encoder::Create();
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000058 }
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +000059 RTC_NOTREACHED();
60 return nullptr;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000061}
62
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000063VideoDecoder* VideoDecoder::Create(VideoDecoder::DecoderType codec_type) {
64 switch (codec_type) {
65 case kVp8:
66 return VP8Decoder::Create();
stefan@webrtc.org7c29e8c2014-11-04 19:41:15 +000067 case kVp9:
68 return VP9Decoder::Create();
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000069 }
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +000070 RTC_NOTREACHED();
71 return nullptr;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000072}
73
pbos@webrtc.orga73a6782014-10-14 11:52:10 +000074const int Call::Config::kDefaultStartBitrateBps = 300000;
75
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000076namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000077
78class CpuOveruseObserverProxy : public webrtc::CpuOveruseObserver {
79 public:
pbos@webrtc.org42684be2014-10-03 11:25:45 +000080 explicit CpuOveruseObserverProxy(LoadObserver* overuse_callback)
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000081 : crit_(CriticalSectionWrapper::CreateCriticalSection()),
82 overuse_callback_(overuse_callback) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +000083 DCHECK(overuse_callback != nullptr);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000084 }
85
86 virtual ~CpuOveruseObserverProxy() {}
87
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000088 void OveruseDetected() 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::kOveruse);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000091 }
92
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000093 void NormalUsage() override {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000094 CriticalSectionScoped lock(crit_.get());
pbos@webrtc.org42684be2014-10-03 11:25:45 +000095 overuse_callback_->OnLoadUpdate(LoadObserver::kUnderuse);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000096 }
97
98 private:
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000099 const rtc::scoped_ptr<CriticalSectionWrapper> crit_;
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000100 LoadObserver* overuse_callback_ GUARDED_BY(crit_);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000101};
102
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000103class Call : public webrtc::Call, public PacketReceiver {
104 public:
105 Call(webrtc::VideoEngine* video_engine, const Call::Config& config);
106 virtual ~Call();
107
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000108 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000109
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000110 VideoSendStream* CreateVideoSendStream(
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000111 const VideoSendStream::Config& config,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000112 const VideoEncoderConfig& encoder_config) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000113
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000114 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000115
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000116 VideoReceiveStream* CreateVideoReceiveStream(
117 const VideoReceiveStream::Config& config) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000118
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000119 void DestroyVideoReceiveStream(
120 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000121
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000122 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000123
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000124 DeliveryStatus DeliverPacket(const uint8_t* packet, size_t length) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000125
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000126 void SetBitrateConfig(
127 const webrtc::Call::Config::BitrateConfig& bitrate_config) override;
128 void SignalNetworkState(NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000129
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000130 private:
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000131 DeliveryStatus DeliverRtcp(const uint8_t* packet, size_t length);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000132 DeliveryStatus DeliverRtp(const uint8_t* packet, size_t length);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000133
134 Call::Config config_;
135
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000136 // Needs to be held while write-locking |receive_crit_| or |send_crit_|. This
137 // ensures that we have a consistent network state signalled to all senders
138 // and receivers.
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000139 rtc::scoped_ptr<CriticalSectionWrapper> network_enabled_crit_;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000140 bool network_enabled_ GUARDED_BY(network_enabled_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000141
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000142 rtc::scoped_ptr<RWLockWrapper> receive_crit_;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000143 std::map<uint32_t, VideoReceiveStream*> receive_ssrcs_
144 GUARDED_BY(receive_crit_);
145
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000146 rtc::scoped_ptr<RWLockWrapper> send_crit_;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000147 std::map<uint32_t, VideoSendStream*> send_ssrcs_ GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000148
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000149 rtc::scoped_ptr<CpuOveruseObserverProxy> overuse_observer_proxy_;
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000150
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000151 VideoSendStream::RtpStateMap suspended_send_ssrcs_;
152
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000153 VideoEngine* video_engine_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000154 ViERTP_RTCP* rtp_rtcp_;
155 ViECodec* codec_;
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000156 ViERender* render_;
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000157 ViEBase* base_;
158 int base_channel_id_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000159
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000160 rtc::scoped_ptr<VideoRender> external_render_;
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000161
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000162 DISALLOW_COPY_AND_ASSIGN(Call);
163};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000164} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000165
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000166Call* Call::Create(const Call::Config& config) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000167 VideoEngine* video_engine = config.webrtc_config != nullptr
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000168 ? VideoEngine::Create(*config.webrtc_config)
169 : VideoEngine::Create();
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000170 DCHECK(video_engine != nullptr);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000171
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000172 return new internal::Call(video_engine, config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000173}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000174
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000175namespace internal {
176
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000177Call::Call(webrtc::VideoEngine* video_engine, const Call::Config& config)
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000178 : config_(config),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000179 network_enabled_crit_(CriticalSectionWrapper::CreateCriticalSection()),
180 network_enabled_(true),
181 receive_crit_(RWLockWrapper::CreateRWLock()),
182 send_crit_(RWLockWrapper::CreateRWLock()),
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000183 video_engine_(video_engine),
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000184 base_channel_id_(-1),
185 external_render_(
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000186 VideoRender::CreateVideoRender(42, nullptr, false, kRenderExternal)) {
187 DCHECK(video_engine != nullptr);
188 DCHECK(config.send_transport != nullptr);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000189
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000190 DCHECK_GE(config.stream_bitrates.min_bitrate_bps, 0);
191 DCHECK_GE(config.stream_bitrates.start_bitrate_bps,
192 config.stream_bitrates.min_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000193 if (config.stream_bitrates.max_bitrate_bps != -1) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000194 DCHECK_GE(config.stream_bitrates.max_bitrate_bps,
195 config.stream_bitrates.start_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000196 }
197
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000198 if (config.overuse_callback) {
199 overuse_observer_proxy_.reset(
200 new CpuOveruseObserverProxy(config.overuse_callback));
201 }
202
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000203 render_ = ViERender::GetInterface(video_engine_);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000204 DCHECK(render_ != nullptr);
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000205
206 render_->RegisterVideoRenderModule(*external_render_.get());
207
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000208 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000209 DCHECK(rtp_rtcp_ != nullptr);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000210
211 codec_ = ViECodec::GetInterface(video_engine_);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000212 DCHECK(codec_ != nullptr);
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000213
214 // As a workaround for non-existing calls in the old API, create a base
215 // channel used as default channel when creating send and receive streams.
216 base_ = ViEBase::GetInterface(video_engine_);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000217 DCHECK(base_ != nullptr);
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000218
219 base_->CreateChannel(base_channel_id_);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000220 DCHECK(base_channel_id_ != -1);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000221}
222
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000223Call::~Call() {
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000224 base_->DeleteChannel(base_channel_id_);
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000225
226 render_->DeRegisterVideoRenderModule(*external_render_.get());
227
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000228 base_->Release();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000229 codec_->Release();
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000230 render_->Release();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000231 rtp_rtcp_->Release();
232 webrtc::VideoEngine::Delete(video_engine_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000233}
234
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000235PacketReceiver* Call::Receiver() { return this; }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000236
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000237VideoSendStream* Call::CreateVideoSendStream(
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000238 const VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000239 const VideoEncoderConfig& encoder_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000240 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
pbos@webrtc.org32e85282015-01-15 10:09:39 +0000241 LOG(LS_INFO) << "CreateVideoSendStream: " << config.ToString();
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000242 DCHECK(!config.rtp.ssrcs.empty());
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000243
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000244 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
245 // the call has already started.
pbos@webrtc.org00873182014-11-25 14:03:34 +0000246 VideoSendStream* send_stream = new VideoSendStream(
247 config_.send_transport, overuse_observer_proxy_.get(), video_engine_,
248 config, encoder_config, suspended_send_ssrcs_, base_channel_id_,
249 config_.stream_bitrates);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000250
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000251 // This needs to be taken before send_crit_ as both locks need to be held
252 // while changing network state.
253 CriticalSectionScoped lock(network_enabled_crit_.get());
254 WriteLockScoped write_lock(*send_crit_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000255 for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000256 DCHECK(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end());
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000257 send_ssrcs_[config.rtp.ssrcs[i]] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000258 }
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000259 if (!network_enabled_)
260 send_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000261 return send_stream;
262}
263
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000264void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000265 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000266 DCHECK(send_stream != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000267
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000268 send_stream->Stop();
269
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000270 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000271 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000272 WriteLockScoped write_lock(*send_crit_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000273 std::map<uint32_t, VideoSendStream*>::iterator it = send_ssrcs_.begin();
274 while (it != send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000275 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
276 send_stream_impl = it->second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000277 send_ssrcs_.erase(it++);
278 } else {
279 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000280 }
281 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000282 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000283
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000284 VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates();
285
286 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
287 it != rtp_state.end();
288 ++it) {
289 suspended_send_ssrcs_[it->first] = it->second;
290 }
291
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000292 DCHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000293 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000294}
295
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000296VideoReceiveStream* Call::CreateVideoReceiveStream(
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000297 const VideoReceiveStream::Config& config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000298 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
pbos@webrtc.org32e85282015-01-15 10:09:39 +0000299 LOG(LS_INFO) << "CreateVideoReceiveStream: " << config.ToString();
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000300 VideoReceiveStream* receive_stream =
301 new VideoReceiveStream(video_engine_,
302 config,
303 config_.send_transport,
304 config_.voice_engine,
305 base_channel_id_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000306
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000307 // This needs to be taken before receive_crit_ as both locks need to be held
308 // while changing network state.
309 CriticalSectionScoped lock(network_enabled_crit_.get());
310 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000311 DCHECK(receive_ssrcs_.find(config.rtp.remote_ssrc) == receive_ssrcs_.end());
pbos@webrtc.orgb613b5a2013-12-03 10:13:04 +0000312 receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000313 // TODO(pbos): Configure different RTX payloads per receive payload.
314 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
315 config.rtp.rtx.begin();
316 if (it != config.rtp.rtx.end())
317 receive_ssrcs_[it->second.ssrc] = receive_stream;
318
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000319 if (!network_enabled_)
320 receive_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000321 return receive_stream;
322}
323
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000324void Call::DestroyVideoReceiveStream(
325 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000326 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000327 DCHECK(receive_stream != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000328
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000329 VideoReceiveStream* receive_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000330 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000331 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000332 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
333 // separate SSRC there can be either one or two.
334 std::map<uint32_t, VideoReceiveStream*>::iterator it =
335 receive_ssrcs_.begin();
336 while (it != receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000337 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000338 if (receive_stream_impl != nullptr)
339 DCHECK(receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000340 receive_stream_impl = it->second;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000341 receive_ssrcs_.erase(it++);
342 } else {
343 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000344 }
345 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000346 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000347
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000348 DCHECK(receive_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000349 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000350}
351
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000352Call::Stats Call::GetStats() const {
353 Stats stats;
354 // Ignoring return values.
355 uint32_t send_bandwidth = 0;
356 rtp_rtcp_->GetEstimatedSendBandwidth(base_channel_id_, &send_bandwidth);
357 stats.send_bandwidth_bps = send_bandwidth;
358 uint32_t recv_bandwidth = 0;
359 rtp_rtcp_->GetEstimatedReceiveBandwidth(base_channel_id_, &recv_bandwidth);
360 stats.recv_bandwidth_bps = recv_bandwidth;
361 {
362 ReadLockScoped read_lock(*send_crit_);
363 for (std::map<uint32_t, VideoSendStream*>::const_iterator it =
364 send_ssrcs_.begin();
365 it != send_ssrcs_.end();
366 ++it) {
367 stats.pacer_delay_ms =
368 std::max(it->second->GetPacerQueuingDelayMs(), stats.pacer_delay_ms);
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000369 int rtt_ms = it->second->GetRtt();
370 if (rtt_ms > 0)
371 stats.rtt_ms = rtt_ms;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000372 }
373 }
374 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000375}
376
pbos@webrtc.org00873182014-11-25 14:03:34 +0000377void Call::SetBitrateConfig(
378 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000379 TRACE_EVENT0("webrtc", "Call::SetBitrateConfig");
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000380 DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
381 if (bitrate_config.max_bitrate_bps != -1)
382 DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000383 if (config_.stream_bitrates.min_bitrate_bps ==
384 bitrate_config.min_bitrate_bps &&
385 (bitrate_config.start_bitrate_bps <= 0 ||
386 config_.stream_bitrates.start_bitrate_bps ==
387 bitrate_config.start_bitrate_bps) &&
388 config_.stream_bitrates.max_bitrate_bps ==
389 bitrate_config.max_bitrate_bps) {
390 // Nothing new to set, early abort to avoid encoder reconfigurations.
391 return;
392 }
393 config_.stream_bitrates = bitrate_config;
394 ReadLockScoped read_lock(*send_crit_);
395 for (std::map<uint32_t, VideoSendStream*>::const_iterator it =
396 send_ssrcs_.begin();
397 it != send_ssrcs_.end(); ++it) {
398 it->second->SetBitrateConfig(bitrate_config);
399 }
400}
401
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000402void Call::SignalNetworkState(NetworkState state) {
403 // Take crit for entire function, it needs to be held while updating streams
404 // to guarantee a consistent state across streams.
405 CriticalSectionScoped lock(network_enabled_crit_.get());
406 network_enabled_ = state == kNetworkUp;
407 {
408 ReadLockScoped write_lock(*send_crit_);
409 for (std::map<uint32_t, VideoSendStream*>::iterator it =
410 send_ssrcs_.begin();
411 it != send_ssrcs_.end();
412 ++it) {
413 it->second->SignalNetworkState(state);
414 }
415 }
416 {
417 ReadLockScoped write_lock(*receive_crit_);
418 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
419 receive_ssrcs_.begin();
420 it != receive_ssrcs_.end();
421 ++it) {
422 it->second->SignalNetworkState(state);
423 }
424 }
425}
426
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000427PacketReceiver::DeliveryStatus Call::DeliverRtcp(const uint8_t* packet,
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000428 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000429 // TODO(pbos): Figure out what channel needs it actually.
430 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000431 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
432 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000433 bool rtcp_delivered = false;
pbos@webrtc.org40523702013-08-05 12:49:22 +0000434 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000435 ReadLockScoped read_lock(*receive_crit_);
pbos@webrtc.org40523702013-08-05 12:49:22 +0000436 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
437 receive_ssrcs_.begin();
438 it != receive_ssrcs_.end();
439 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000440 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000441 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000442 }
443 }
444
pbos@webrtc.org40523702013-08-05 12:49:22 +0000445 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000446 ReadLockScoped read_lock(*send_crit_);
pbos@webrtc.org40523702013-08-05 12:49:22 +0000447 for (std::map<uint32_t, VideoSendStream*>::iterator it =
448 send_ssrcs_.begin();
449 it != send_ssrcs_.end();
450 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000451 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000452 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000453 }
454 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000455 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000456}
457
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000458PacketReceiver::DeliveryStatus Call::DeliverRtp(const uint8_t* packet,
459 size_t length) {
460 // Minimum RTP header size.
461 if (length < 12)
462 return DELIVERY_PACKET_ERROR;
463
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +0000464 uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(&packet[8]);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000465
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000466 ReadLockScoped read_lock(*receive_crit_);
solenberg@webrtc.org094ac392014-01-29 11:21:58 +0000467 std::map<uint32_t, VideoReceiveStream*>::iterator it =
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000468 receive_ssrcs_.find(ssrc);
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000469
470 if (it == receive_ssrcs_.end())
471 return DELIVERY_UNKNOWN_SSRC;
472
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000473 return it->second->DeliverRtp(packet, length) ? DELIVERY_OK
474 : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000475}
476
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000477PacketReceiver::DeliveryStatus Call::DeliverPacket(const uint8_t* packet,
478 size_t length) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000479 if (RtpHeaderParser::IsRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000480 return DeliverRtcp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000481
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000482 return DeliverRtp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000483}
484
485} // namespace internal
486} // namespace webrtc