blob: c455ba8ee63d7c539d3ba91f9524df10e0868346 [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_;
Stefan Holmere5904162015-03-26 11:11:06 +0100158 ViENetwork* network_;
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000159 int base_channel_id_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000160
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000161 rtc::scoped_ptr<VideoRender> external_render_;
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000162
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000163 DISALLOW_COPY_AND_ASSIGN(Call);
164};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000165} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000166
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000167Call* Call::Create(const Call::Config& config) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000168 VideoEngine* video_engine = config.webrtc_config != nullptr
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000169 ? VideoEngine::Create(*config.webrtc_config)
170 : VideoEngine::Create();
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000171 DCHECK(video_engine != nullptr);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000172
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000173 return new internal::Call(video_engine, config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000174}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000175
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000176namespace internal {
177
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000178Call::Call(webrtc::VideoEngine* video_engine, const Call::Config& config)
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000179 : config_(config),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000180 network_enabled_crit_(CriticalSectionWrapper::CreateCriticalSection()),
181 network_enabled_(true),
182 receive_crit_(RWLockWrapper::CreateRWLock()),
183 send_crit_(RWLockWrapper::CreateRWLock()),
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000184 video_engine_(video_engine),
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000185 base_channel_id_(-1),
186 external_render_(
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000187 VideoRender::CreateVideoRender(42, nullptr, false, kRenderExternal)) {
188 DCHECK(video_engine != nullptr);
189 DCHECK(config.send_transport != nullptr);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000190
Stefan Holmere5904162015-03-26 11:11:06 +0100191 DCHECK_GE(config.bitrate_config.min_bitrate_bps, 0);
192 DCHECK_GE(config.bitrate_config.start_bitrate_bps,
193 config.bitrate_config.min_bitrate_bps);
194 if (config.bitrate_config.max_bitrate_bps != -1) {
195 DCHECK_GE(config.bitrate_config.max_bitrate_bps,
196 config.bitrate_config.start_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000197 }
198
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000199 if (config.overuse_callback) {
200 overuse_observer_proxy_.reset(
201 new CpuOveruseObserverProxy(config.overuse_callback));
202 }
203
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000204 render_ = ViERender::GetInterface(video_engine_);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000205 DCHECK(render_ != nullptr);
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000206
207 render_->RegisterVideoRenderModule(*external_render_.get());
208
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000209 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000210 DCHECK(rtp_rtcp_ != nullptr);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000211
212 codec_ = ViECodec::GetInterface(video_engine_);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000213 DCHECK(codec_ != nullptr);
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000214
Stefan Holmere5904162015-03-26 11:11:06 +0100215 network_ = ViENetwork::GetInterface(video_engine_);
216
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000217 // As a workaround for non-existing calls in the old API, create a base
218 // channel used as default channel when creating send and receive streams.
219 base_ = ViEBase::GetInterface(video_engine_);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000220 DCHECK(base_ != nullptr);
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000221
222 base_->CreateChannel(base_channel_id_);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000223 DCHECK(base_channel_id_ != -1);
Stefan Holmere5904162015-03-26 11:11:06 +0100224
225 network_->SetBitrateConfig(base_channel_id_,
226 config_.bitrate_config.min_bitrate_bps,
227 config_.bitrate_config.start_bitrate_bps,
228 config_.bitrate_config.max_bitrate_bps);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000229}
230
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000231Call::~Call() {
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000232 base_->DeleteChannel(base_channel_id_);
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000233
234 render_->DeRegisterVideoRenderModule(*external_render_.get());
235
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000236 base_->Release();
Stefan Holmere5904162015-03-26 11:11:06 +0100237 network_->Release();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000238 codec_->Release();
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000239 render_->Release();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000240 rtp_rtcp_->Release();
241 webrtc::VideoEngine::Delete(video_engine_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000242}
243
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000244PacketReceiver* Call::Receiver() { return this; }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000245
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000246VideoSendStream* Call::CreateVideoSendStream(
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000247 const VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000248 const VideoEncoderConfig& encoder_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000249 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
pbos@webrtc.org32e85282015-01-15 10:09:39 +0000250 LOG(LS_INFO) << "CreateVideoSendStream: " << config.ToString();
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000251 DCHECK(!config.rtp.ssrcs.empty());
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000252
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000253 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
254 // the call has already started.
pbos@webrtc.org00873182014-11-25 14:03:34 +0000255 VideoSendStream* send_stream = new VideoSendStream(
256 config_.send_transport, overuse_observer_proxy_.get(), video_engine_,
Stefan Holmere5904162015-03-26 11:11:06 +0100257 config, encoder_config, suspended_send_ssrcs_, base_channel_id_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000258
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000259 // This needs to be taken before send_crit_ as both locks need to be held
260 // while changing network state.
261 CriticalSectionScoped lock(network_enabled_crit_.get());
262 WriteLockScoped write_lock(*send_crit_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000263 for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000264 DCHECK(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end());
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000265 send_ssrcs_[config.rtp.ssrcs[i]] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000266 }
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000267 if (!network_enabled_)
268 send_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000269 return send_stream;
270}
271
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000272void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000273 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000274 DCHECK(send_stream != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000275
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000276 send_stream->Stop();
277
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000278 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000279 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000280 WriteLockScoped write_lock(*send_crit_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000281 std::map<uint32_t, VideoSendStream*>::iterator it = send_ssrcs_.begin();
282 while (it != send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000283 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
284 send_stream_impl = it->second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000285 send_ssrcs_.erase(it++);
286 } else {
287 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000288 }
289 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000290 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000291
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000292 VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates();
293
294 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
295 it != rtp_state.end();
296 ++it) {
297 suspended_send_ssrcs_[it->first] = it->second;
298 }
299
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000300 DCHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000301 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000302}
303
pbos@webrtc.org5a636552013-11-20 10:40:25 +0000304VideoReceiveStream* Call::CreateVideoReceiveStream(
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000305 const VideoReceiveStream::Config& config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000306 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
pbos@webrtc.org32e85282015-01-15 10:09:39 +0000307 LOG(LS_INFO) << "CreateVideoReceiveStream: " << config.ToString();
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000308 VideoReceiveStream* receive_stream =
309 new VideoReceiveStream(video_engine_,
310 config,
311 config_.send_transport,
312 config_.voice_engine,
313 base_channel_id_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000314
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000315 // This needs to be taken before receive_crit_ as both locks need to be held
316 // while changing network state.
317 CriticalSectionScoped lock(network_enabled_crit_.get());
318 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000319 DCHECK(receive_ssrcs_.find(config.rtp.remote_ssrc) == receive_ssrcs_.end());
pbos@webrtc.orgb613b5a2013-12-03 10:13:04 +0000320 receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000321 // TODO(pbos): Configure different RTX payloads per receive payload.
322 VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
323 config.rtp.rtx.begin();
324 if (it != config.rtp.rtx.end())
325 receive_ssrcs_[it->second.ssrc] = receive_stream;
326
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000327 if (!network_enabled_)
328 receive_stream->SignalNetworkState(kNetworkDown);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000329 return receive_stream;
330}
331
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000332void Call::DestroyVideoReceiveStream(
333 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000334 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000335 DCHECK(receive_stream != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000336
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000337 VideoReceiveStream* receive_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000338 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000339 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000340 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
341 // separate SSRC there can be either one or two.
342 std::map<uint32_t, VideoReceiveStream*>::iterator it =
343 receive_ssrcs_.begin();
344 while (it != receive_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000345 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000346 if (receive_stream_impl != nullptr)
347 DCHECK(receive_stream_impl == it->second);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000348 receive_stream_impl = it->second;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000349 receive_ssrcs_.erase(it++);
350 } else {
351 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000352 }
353 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000354 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000355
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000356 DCHECK(receive_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000357 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000358}
359
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000360Call::Stats Call::GetStats() const {
361 Stats stats;
362 // Ignoring return values.
363 uint32_t send_bandwidth = 0;
364 rtp_rtcp_->GetEstimatedSendBandwidth(base_channel_id_, &send_bandwidth);
365 stats.send_bandwidth_bps = send_bandwidth;
366 uint32_t recv_bandwidth = 0;
367 rtp_rtcp_->GetEstimatedReceiveBandwidth(base_channel_id_, &recv_bandwidth);
368 stats.recv_bandwidth_bps = recv_bandwidth;
369 {
370 ReadLockScoped read_lock(*send_crit_);
371 for (std::map<uint32_t, VideoSendStream*>::const_iterator it =
372 send_ssrcs_.begin();
373 it != send_ssrcs_.end();
374 ++it) {
375 stats.pacer_delay_ms =
376 std::max(it->second->GetPacerQueuingDelayMs(), stats.pacer_delay_ms);
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000377 int rtt_ms = it->second->GetRtt();
378 if (rtt_ms > 0)
379 stats.rtt_ms = rtt_ms;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000380 }
381 }
382 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000383}
384
pbos@webrtc.org00873182014-11-25 14:03:34 +0000385void Call::SetBitrateConfig(
386 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000387 TRACE_EVENT0("webrtc", "Call::SetBitrateConfig");
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000388 DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
389 if (bitrate_config.max_bitrate_bps != -1)
390 DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
Stefan Holmere5904162015-03-26 11:11:06 +0100391 if (config_.bitrate_config.min_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000392 bitrate_config.min_bitrate_bps &&
393 (bitrate_config.start_bitrate_bps <= 0 ||
Stefan Holmere5904162015-03-26 11:11:06 +0100394 config_.bitrate_config.start_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000395 bitrate_config.start_bitrate_bps) &&
Stefan Holmere5904162015-03-26 11:11:06 +0100396 config_.bitrate_config.max_bitrate_bps ==
pbos@webrtc.org00873182014-11-25 14:03:34 +0000397 bitrate_config.max_bitrate_bps) {
398 // Nothing new to set, early abort to avoid encoder reconfigurations.
399 return;
400 }
Stefan Holmere5904162015-03-26 11:11:06 +0100401 config_.bitrate_config = bitrate_config;
402 network_->SetBitrateConfig(base_channel_id_, bitrate_config.min_bitrate_bps,
403 bitrate_config.start_bitrate_bps,
404 bitrate_config.max_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000405}
406
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000407void Call::SignalNetworkState(NetworkState state) {
408 // Take crit for entire function, it needs to be held while updating streams
409 // to guarantee a consistent state across streams.
410 CriticalSectionScoped lock(network_enabled_crit_.get());
411 network_enabled_ = state == kNetworkUp;
412 {
413 ReadLockScoped write_lock(*send_crit_);
414 for (std::map<uint32_t, VideoSendStream*>::iterator it =
415 send_ssrcs_.begin();
416 it != send_ssrcs_.end();
417 ++it) {
418 it->second->SignalNetworkState(state);
419 }
420 }
421 {
422 ReadLockScoped write_lock(*receive_crit_);
423 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
424 receive_ssrcs_.begin();
425 it != receive_ssrcs_.end();
426 ++it) {
427 it->second->SignalNetworkState(state);
428 }
429 }
430}
431
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000432PacketReceiver::DeliveryStatus Call::DeliverRtcp(const uint8_t* packet,
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000433 size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000434 // TODO(pbos): Figure out what channel needs it actually.
435 // Do NOT broadcast! Also make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000436 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
437 // there's no receiver of the packet.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000438 bool rtcp_delivered = false;
pbos@webrtc.org40523702013-08-05 12:49:22 +0000439 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000440 ReadLockScoped read_lock(*receive_crit_);
pbos@webrtc.org40523702013-08-05 12:49:22 +0000441 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
442 receive_ssrcs_.begin();
443 it != receive_ssrcs_.end();
444 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000445 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000446 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000447 }
448 }
449
pbos@webrtc.org40523702013-08-05 12:49:22 +0000450 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000451 ReadLockScoped read_lock(*send_crit_);
pbos@webrtc.org40523702013-08-05 12:49:22 +0000452 for (std::map<uint32_t, VideoSendStream*>::iterator it =
453 send_ssrcs_.begin();
454 it != send_ssrcs_.end();
455 ++it) {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000456 if (it->second->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000457 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000458 }
459 }
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000460 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000461}
462
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000463PacketReceiver::DeliveryStatus Call::DeliverRtp(const uint8_t* packet,
464 size_t length) {
465 // Minimum RTP header size.
466 if (length < 12)
467 return DELIVERY_PACKET_ERROR;
468
sprang@webrtc.org2a6558c2015-01-28 12:37:36 +0000469 uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(&packet[8]);
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000470
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000471 ReadLockScoped read_lock(*receive_crit_);
solenberg@webrtc.org094ac392014-01-29 11:21:58 +0000472 std::map<uint32_t, VideoReceiveStream*>::iterator it =
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000473 receive_ssrcs_.find(ssrc);
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000474
475 if (it == receive_ssrcs_.end())
476 return DELIVERY_UNKNOWN_SSRC;
477
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000478 return it->second->DeliverRtp(packet, length) ? DELIVERY_OK
479 : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000480}
481
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000482PacketReceiver::DeliveryStatus Call::DeliverPacket(const uint8_t* packet,
483 size_t length) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000484 if (RtpHeaderParser::IsRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +0000485 return DeliverRtcp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000486
pbos@webrtc.orgaf38f4e2014-07-08 07:38:12 +0000487 return DeliverRtp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000488}
489
490} // namespace internal
491} // namespace webrtc