blob: 4d41e0e1ae8247ead2388a3b28cd14643df4699f [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
stefan@webrtc.org07b45a52012-02-02 08:37:48 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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.orgf5d4cb12013-05-17 13:44:48 +000011#include "webrtc/video_engine/vie_encoder.h"
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h>
14
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +000015#include <algorithm>
niklase@google.com470e71d2011-07-07 08:21:25 +000016
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000017#include "webrtc/base/checks.h"
sprang@webrtc.org40709352013-11-26 11:41:59 +000018#include "webrtc/common_video/interface/video_image.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000019#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
pbos@webrtc.org273a4142014-12-01 15:23:21 +000020#include "webrtc/frame_callback.h"
Peter Boström8e4e8b02015-09-15 15:08:03 +020021#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000022#include "webrtc/modules/pacing/include/paced_sender.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000023#include "webrtc/modules/utility/interface/process_thread.h"
24#include "webrtc/modules/video_coding/codecs/interface/video_codec_interface.h"
25#include "webrtc/modules/video_coding/main/interface/video_coding.h"
26#include "webrtc/modules/video_coding/main/interface/video_coding_defines.h"
sprang@webrtc.org40709352013-11-26 11:41:59 +000027#include "webrtc/modules/video_coding/main/source/encoded_frame.h"
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000028#include "webrtc/system_wrappers/interface/clock.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000029#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
30#include "webrtc/system_wrappers/interface/logging.h"
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +000031#include "webrtc/system_wrappers/interface/metrics.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000032#include "webrtc/system_wrappers/interface/tick_util.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000033#include "webrtc/system_wrappers/interface/trace_event.h"
pbos@webrtc.org273a4142014-12-01 15:23:21 +000034#include "webrtc/video/send_statistics_proxy.h"
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000035#include "webrtc/video_engine/payload_router.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000036#include "webrtc/video_engine/vie_defines.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000037
niklase@google.com470e71d2011-07-07 08:21:25 +000038namespace webrtc {
39
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +000040// Margin on when we pause the encoder when the pacing buffer overflows relative
41// to the configured buffer delay.
42static const float kEncoderPausePacerMargin = 2.0f;
43
pwestin@webrtc.org91563e42013-04-25 22:20:08 +000044// Don't stop the encoder unless the delay is above this configured value.
45static const int kMinPacingDelayMs = 200;
46
stefan@webrtc.org3e005052013-10-18 15:05:29 +000047static const float kStopPaddingThresholdMs = 2000;
48
stefan@webrtc.orgb2c8a952013-09-06 13:58:01 +000049std::vector<uint32_t> AllocateStreamBitrates(
50 uint32_t total_bitrate,
51 const SimulcastStream* stream_configs,
52 size_t number_of_streams) {
53 if (number_of_streams == 0) {
54 std::vector<uint32_t> stream_bitrates(1, 0);
55 stream_bitrates[0] = total_bitrate;
56 return stream_bitrates;
57 }
58 std::vector<uint32_t> stream_bitrates(number_of_streams, 0);
59 uint32_t bitrate_remainder = total_bitrate;
60 for (size_t i = 0; i < stream_bitrates.size() && bitrate_remainder > 0; ++i) {
61 if (stream_configs[i].maxBitrate * 1000 > bitrate_remainder) {
62 stream_bitrates[i] = bitrate_remainder;
63 } else {
64 stream_bitrates[i] = stream_configs[i].maxBitrate * 1000;
65 }
66 bitrate_remainder -= stream_bitrates[i];
67 }
68 return stream_bitrates;
69}
70
stefan@webrtc.org439be292012-02-16 14:45:37 +000071class QMVideoSettingsCallback : public VCMQMSettingsCallback {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000072 public:
marpan@webrtc.orgefd01fd2012-04-18 15:56:34 +000073 explicit QMVideoSettingsCallback(VideoProcessingModule* vpm);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000074
stefan@webrtc.org439be292012-02-16 14:45:37 +000075 ~QMVideoSettingsCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +000076
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000077 // Update VPM with QM (quality modes: frame size & frame rate) settings.
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000078 int32_t SetVideoQMSettings(const uint32_t frame_rate,
79 const uint32_t width,
80 const uint32_t height);
niklase@google.com470e71d2011-07-07 08:21:25 +000081
jackychen6e2ce6e2015-07-13 16:26:33 -070082 // Update target frame rate.
83 void SetTargetFramerate(int frame_rate);
84
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000085 private:
86 VideoProcessingModule* vpm_;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000087};
niklase@google.com470e71d2011-07-07 08:21:25 +000088
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000089class ViEBitrateObserver : public BitrateObserver {
90 public:
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +000091 explicit ViEBitrateObserver(ViEEncoder* owner)
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000092 : owner_(owner) {
93 }
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000094 virtual ~ViEBitrateObserver() {}
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000095 // Implements BitrateObserver.
stefan@webrtc.orgedeea912014-12-08 19:46:23 +000096 virtual void OnNetworkChanged(uint32_t bitrate_bps,
97 uint8_t fraction_lost,
pkasting@chromium.org16825b12015-01-12 21:51:21 +000098 int64_t rtt) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000099 owner_->OnNetworkChanged(bitrate_bps, fraction_lost, rtt);
100 }
101 private:
102 ViEEncoder* owner_;
103};
niklase@google.com470e71d2011-07-07 08:21:25 +0000104
mflodman@webrtc.org9dd0ebc2015-02-26 12:57:47 +0000105ViEEncoder::ViEEncoder(int32_t channel_id,
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000106 uint32_t number_of_cores,
Peter Boström7083e112015-09-22 16:28:51 +0200107 ProcessThread* module_process_thread,
108 SendStatisticsProxy* stats_proxy,
109 I420FrameCallback* pre_encode_callback,
Stefan Holmere5904162015-03-26 11:11:06 +0100110 PacedSender* pacer,
Peter Boström8e4e8b02015-09-15 15:08:03 +0200111 BitrateAllocator* bitrate_allocator)
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000112 : channel_id_(channel_id),
113 number_of_cores_(number_of_cores),
Peter Boströmf4aa4c22015-09-18 12:24:25 +0200114 vpm_(VideoProcessingModule::Create()),
mflodmanfcf54bd2015-04-14 21:28:08 +0200115 qm_callback_(new QMVideoSettingsCallback(vpm_.get())),
116 vcm_(VideoCodingModule::Create(Clock::GetRealTimeClock(),
117 this,
118 qm_callback_.get())),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000119 send_payload_router_(NULL),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000120 data_cs_(CriticalSectionWrapper::CreateCriticalSection()),
Peter Boström7083e112015-09-22 16:28:51 +0200121 stats_proxy_(stats_proxy),
122 pre_encode_callback_(pre_encode_callback),
Stefan Holmere5904162015-03-26 11:11:06 +0100123 pacer_(pacer),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000124 bitrate_allocator_(bitrate_allocator),
Noah Richards099323e2015-04-15 09:14:12 -0700125 time_of_last_frame_activity_ms_(0),
Peter Boströma7531772015-09-15 13:12:24 +0200126 simulcast_enabled_(false),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000127 min_transmit_bitrate_kbps_(0),
pbos@webrtc.org143451d2015-03-18 14:40:03 +0000128 last_observed_bitrate_bps_(0),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000129 target_delay_ms_(0),
130 network_is_transmitting_(true),
131 encoder_paused_(false),
132 encoder_paused_and_dropped_frame_(false),
133 fec_enabled_(false),
134 nack_enabled_(false),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000135 module_process_thread_(module_process_thread),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000136 has_received_sli_(false),
137 picture_id_sli_(0),
138 has_received_rpsi_(false),
139 picture_id_rpsi_(0),
asaperssondec5ebf2015-10-05 02:36:17 -0700140 video_suspended_(false) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000141 bitrate_observer_.reset(new ViEBitrateObserver(this));
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000142}
143
144bool ViEEncoder::Init() {
mflodmanfcf54bd2015-04-14 21:28:08 +0200145 vpm_->EnableTemporalDecimation(true);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000146
147 // Enable/disable content analysis: off by default for now.
mflodmanfcf54bd2015-04-14 21:28:08 +0200148 vpm_->EnableContentAnalysis(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000149
mflodmanfcf54bd2015-04-14 21:28:08 +0200150 if (vcm_->RegisterTransportCallback(this) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000151 return false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000152 }
mflodmanfcf54bd2015-04-14 21:28:08 +0200153 if (vcm_->RegisterSendStatisticsCallback(this) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000154 return false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000155 }
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000156 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000157}
158
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000159void ViEEncoder::StartThreadsAndSetSharedMembers(
Peter Boström26b08602015-06-04 15:18:17 +0200160 rtc::scoped_refptr<PayloadRouter> send_payload_router,
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000161 VCMProtectionCallback* vcm_protection_callback) {
henrikg91d6ede2015-09-17 00:24:34 -0700162 RTC_DCHECK(send_payload_router_ == NULL);
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000163
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000164 send_payload_router_ = send_payload_router;
mflodmanfcf54bd2015-04-14 21:28:08 +0200165 vcm_->RegisterProtectionCallback(vcm_protection_callback);
Peter Boström7083e112015-09-22 16:28:51 +0200166 module_process_thread_->RegisterModule(vcm_.get());
mflodman@webrtc.org290cb562015-02-17 10:15:06 +0000167}
168
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000169void ViEEncoder::StopThreadsAndRemoveSharedMembers() {
mflodmanfcf54bd2015-04-14 21:28:08 +0200170 if (bitrate_allocator_)
171 bitrate_allocator_->RemoveBitrateObserver(bitrate_observer_.get());
Peter Boström7083e112015-09-22 16:28:51 +0200172 module_process_thread_->DeRegisterModule(vcm_.get());
173 module_process_thread_->DeRegisterModule(vpm_.get());
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000174}
175
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000176ViEEncoder::~ViEEncoder() {
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000177}
178
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000179int ViEEncoder::Owner() const {
180 return channel_id_;
181}
182
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000183void ViEEncoder::SetNetworkTransmissionState(bool is_transmitting) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000184 {
185 CriticalSectionScoped cs(data_cs_.get());
186 network_is_transmitting_ = is_transmitting;
187 }
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000188}
189
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000190void ViEEncoder::Pause() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000191 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000192 encoder_paused_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000193}
194
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000195void ViEEncoder::Restart() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000196 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000197 encoder_paused_ = false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000198}
199
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000200uint8_t ViEEncoder::NumberOfCodecs() {
mflodmanfcf54bd2015-04-14 21:28:08 +0200201 return vcm_->NumberOfCodecs();
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000202}
203
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000204int32_t ViEEncoder::GetCodec(uint8_t list_index, VideoCodec* video_codec) {
mflodmanfcf54bd2015-04-14 21:28:08 +0200205 if (vcm_->Codec(list_index, video_codec) != 0) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000206 return -1;
207 }
208 return 0;
209}
210
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000211int32_t ViEEncoder::RegisterExternalEncoder(webrtc::VideoEncoder* encoder,
212 uint8_t pl_type,
213 bool internal_source) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000214 if (encoder == NULL)
215 return -1;
216
mflodmanfcf54bd2015-04-14 21:28:08 +0200217 if (vcm_->RegisterExternalEncoder(encoder, pl_type, internal_source) !=
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000218 VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000219 return -1;
220 }
221 return 0;
222}
223
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000224int32_t ViEEncoder::DeRegisterExternalEncoder(uint8_t pl_type) {
mflodmanfcf54bd2015-04-14 21:28:08 +0200225 if (vcm_->RegisterExternalEncoder(NULL, pl_type) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000226 return -1;
227 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000228 return 0;
229}
230
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000231int32_t ViEEncoder::SetEncoder(const webrtc::VideoCodec& video_codec) {
henrikg91d6ede2015-09-17 00:24:34 -0700232 RTC_DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000233 // Setting target width and height for VPM.
mflodmanfcf54bd2015-04-14 21:28:08 +0200234 if (vpm_->SetTargetResolution(video_codec.width, video_codec.height,
235 video_codec.maxFramerate) != VPM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000236 return -1;
237 }
238
stefan@webrtc.org9075d512014-02-14 09:45:58 +0000239 {
240 CriticalSectionScoped cs(data_cs_.get());
Peter Boströma7531772015-09-15 13:12:24 +0200241 simulcast_enabled_ = video_codec.numberOfSimulcastStreams > 1;
stefan@webrtc.org9075d512014-02-14 09:45:58 +0000242 }
Stefan Holmere5904162015-03-26 11:11:06 +0100243
244 // Add a bitrate observer to the allocator and update the start, max and
245 // min bitrates of the bitrate controller as needed.
Peter Boström8e4e8b02015-09-15 15:08:03 +0200246 int allocated_bitrate_bps = bitrate_allocator_->AddBitrateObserver(
247 bitrate_observer_.get(), video_codec.minBitrate * 1000,
248 video_codec.maxBitrate * 1000);
Stefan Holmere5904162015-03-26 11:11:06 +0100249
250 webrtc::VideoCodec modified_video_codec = video_codec;
251 modified_video_codec.startBitrate = allocated_bitrate_bps / 1000;
252
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000253 size_t max_data_payload_length = send_payload_router_->MaxPayloadLength();
mflodmanfcf54bd2015-04-14 21:28:08 +0200254 if (vcm_->RegisterSendCodec(&modified_video_codec, number_of_cores_,
Peter Boströmae37abb2015-06-18 19:00:34 +0200255 static_cast<uint32_t>(max_data_payload_length)) !=
256 VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000257 return -1;
258 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000259 return 0;
260}
261
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000262int32_t ViEEncoder::GetEncoder(VideoCodec* video_codec) {
mflodmanfcf54bd2015-04-14 21:28:08 +0200263 *video_codec = vcm_->GetSendCodec();
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000264 return 0;
265}
niklase@google.com470e71d2011-07-07 08:21:25 +0000266
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000267int32_t ViEEncoder::GetCodecConfigParameters(
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000268 unsigned char config_parameters[kConfigParameterSize],
269 unsigned char& config_parameters_size) {
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000270 int32_t num_parameters =
mflodmanfcf54bd2015-04-14 21:28:08 +0200271 vcm_->CodecConfigParameters(config_parameters, kConfigParameterSize);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000272 if (num_parameters <= 0) {
273 config_parameters_size = 0;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000274 return -1;
275 }
276 config_parameters_size = static_cast<unsigned char>(num_parameters);
277 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000278}
279
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000280int32_t ViEEncoder::ScaleInputImage(bool enable) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000281 VideoFrameResampling resampling_mode = kFastRescaling;
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000282 // TODO(mflodman) What?
283 if (enable) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000284 // kInterpolation is currently not supported.
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000285 LOG_F(LS_ERROR) << "Not supported.";
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000286 return -1;
287 }
mflodmanfcf54bd2015-04-14 21:28:08 +0200288 vpm_->SetInputFrameResampleMode(resampling_mode);
niklase@google.com470e71d2011-07-07 08:21:25 +0000289
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000290 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000291}
292
stefana4a8d4a2015-07-15 04:39:22 -0700293int ViEEncoder::GetPaddingNeededBps() const {
Noah Richards099323e2015-04-15 09:14:12 -0700294 int64_t time_of_last_frame_activity_ms;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000295 int min_transmit_bitrate_bps;
stefana4a8d4a2015-07-15 04:39:22 -0700296 int bitrate_bps;
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +0000297 {
298 CriticalSectionScoped cs(data_cs_.get());
Peter Boströma7531772015-09-15 13:12:24 +0200299 bool send_padding = simulcast_enabled_ || video_suspended_ ||
300 min_transmit_bitrate_kbps_ > 0;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000301 if (!send_padding)
302 return 0;
Noah Richards099323e2015-04-15 09:14:12 -0700303 time_of_last_frame_activity_ms = time_of_last_frame_activity_ms_;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000304 min_transmit_bitrate_bps = 1000 * min_transmit_bitrate_kbps_;
stefana4a8d4a2015-07-15 04:39:22 -0700305 bitrate_bps = last_observed_bitrate_bps_;
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +0000306 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000307
308 VideoCodec send_codec;
mflodmanfcf54bd2015-04-14 21:28:08 +0200309 if (vcm_->SendCodec(&send_codec) != 0)
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000310 return 0;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000311
mflodmanfcf54bd2015-04-14 21:28:08 +0200312 bool video_is_suspended = vcm_->VideoSuspended();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000313
314 // Find the max amount of padding we can allow ourselves to send at this
315 // point, based on which streams are currently active and what our current
316 // available bandwidth is.
317 int pad_up_to_bitrate_bps = 0;
318 if (send_codec.numberOfSimulcastStreams == 0) {
319 pad_up_to_bitrate_bps = send_codec.minBitrate * 1000;
320 } else {
stefana4a8d4a2015-07-15 04:39:22 -0700321 SimulcastStream* stream_configs = send_codec.simulcastStream;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000322 pad_up_to_bitrate_bps =
323 stream_configs[send_codec.numberOfSimulcastStreams - 1].minBitrate *
324 1000;
325 for (int i = 0; i < send_codec.numberOfSimulcastStreams - 1; ++i) {
326 pad_up_to_bitrate_bps += stream_configs[i].targetBitrate * 1000;
327 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000328 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000329
330 // Disable padding if only sending one stream and video isn't suspended and
331 // min-transmit bitrate isn't used (applied later).
332 if (!video_is_suspended && send_codec.numberOfSimulcastStreams <= 1)
333 pad_up_to_bitrate_bps = 0;
334
335 // The amount of padding should decay to zero if no frames are being
Noah Richards099323e2015-04-15 09:14:12 -0700336 // captured/encoded unless a min-transmit bitrate is used.
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000337 int64_t now_ms = TickTime::MillisecondTimestamp();
Noah Richards099323e2015-04-15 09:14:12 -0700338 if (now_ms - time_of_last_frame_activity_ms > kStopPaddingThresholdMs)
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000339 pad_up_to_bitrate_bps = 0;
340
341 // Pad up to min bitrate.
342 if (pad_up_to_bitrate_bps < min_transmit_bitrate_bps)
343 pad_up_to_bitrate_bps = min_transmit_bitrate_bps;
344
345 // Padding may never exceed bitrate estimate.
346 if (pad_up_to_bitrate_bps > bitrate_bps)
347 pad_up_to_bitrate_bps = bitrate_bps;
348
349 return pad_up_to_bitrate_bps;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000350}
351
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000352bool ViEEncoder::EncoderPaused() const {
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000353 // Pause video if paused by caller or as long as the network is down or the
354 // pacer queue has grown too large in buffered mode.
355 if (encoder_paused_) {
356 return true;
357 }
358 if (target_delay_ms_ > 0) {
359 // Buffered mode.
360 // TODO(pwestin): Workaround until nack is configured as a time and not
361 // number of packets.
Stefan Holmere5904162015-03-26 11:11:06 +0100362 return pacer_->QueueInMs() >=
363 std::max(
364 static_cast<int>(target_delay_ms_ * kEncoderPausePacerMargin),
365 kMinPacingDelayMs);
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000366 }
Stefan Holmere5904162015-03-26 11:11:06 +0100367 if (pacer_->ExpectedQueueTimeMs() > PacedSender::kDefaultMaxQueueLengthMs) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000368 // Too much data in pacer queue, drop frame.
369 return true;
370 }
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000371 return !network_is_transmitting_;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000372}
373
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000374void ViEEncoder::TraceFrameDropStart() {
375 // Start trace event only on the first frame after encoder is paused.
376 if (!encoder_paused_and_dropped_frame_) {
377 TRACE_EVENT_ASYNC_BEGIN0("webrtc", "EncoderPaused", this);
378 }
379 encoder_paused_and_dropped_frame_ = true;
380 return;
381}
382
383void ViEEncoder::TraceFrameDropEnd() {
384 // End trace event on first frame after encoder resumes, if frame was dropped.
385 if (encoder_paused_and_dropped_frame_) {
386 TRACE_EVENT_ASYNC_END0("webrtc", "EncoderPaused", this);
387 }
388 encoder_paused_and_dropped_frame_ = false;
389}
390
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700391void ViEEncoder::DeliverFrame(VideoFrame video_frame) {
henrikg91d6ede2015-09-17 00:24:34 -0700392 RTC_DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org47d657b2015-02-19 10:29:32 +0000393 if (!send_payload_router_->active()) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000394 // We've paused or we have no channels attached, don't waste resources on
395 // encoding.
wuchengli@chromium.orgac4b87c2014-03-19 03:44:20 +0000396 return;
397 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000398 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000399 CriticalSectionScoped cs(data_cs_.get());
Noah Richards099323e2015-04-15 09:14:12 -0700400 time_of_last_frame_activity_ms_ = TickTime::MillisecondTimestamp();
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000401 if (EncoderPaused()) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000402 TraceFrameDropStart();
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000403 return;
404 }
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000405 TraceFrameDropEnd();
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000406 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000407
Magnus Jedvert26679d62015-04-07 14:07:41 +0200408 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(),
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000409 "Encode");
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700410 VideoFrame* decimated_frame = NULL;
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000411 // TODO(wuchengli): support texture frames.
Magnus Jedvert26679d62015-04-07 14:07:41 +0200412 if (video_frame.native_handle() == NULL) {
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000413 // Pass frame via preprocessor.
mflodmanfcf54bd2015-04-14 21:28:08 +0200414 const int ret = vpm_->PreprocessFrame(video_frame, &decimated_frame);
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000415 if (ret == 1) {
416 // Drop this frame.
417 return;
418 }
419 if (ret != VPM_OK) {
420 return;
421 }
pwestin@webrtc.org2f476ed2012-10-30 16:21:52 +0000422 }
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000423
Magnus Jedvert26679d62015-04-07 14:07:41 +0200424 // If we haven't resampled the frame and we have a FrameCallback, we need to
425 // make a deep copy of |video_frame|.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700426 VideoFrame copied_frame;
Peter Boström7083e112015-09-22 16:28:51 +0200427 if (pre_encode_callback_) {
428 // If the frame was not resampled or scaled => use copy of original.
429 if (decimated_frame == NULL) {
430 copied_frame.CopyFrame(video_frame);
431 decimated_frame = &copied_frame;
Magnus Jedvert26679d62015-04-07 14:07:41 +0200432 }
Peter Boström7083e112015-09-22 16:28:51 +0200433 pre_encode_callback_->FrameCallback(decimated_frame);
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000434 }
435
Magnus Jedvert26679d62015-04-07 14:07:41 +0200436 // If the frame was not resampled, scaled, or touched by FrameCallback => use
437 // original. The frame is const from here.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700438 const VideoFrame* output_frame =
Magnus Jedvert26679d62015-04-07 14:07:41 +0200439 (decimated_frame != NULL) ? decimated_frame : &video_frame;
440
niklase@google.com470e71d2011-07-07 08:21:25 +0000441#ifdef VIDEOCODEC_VP8
mflodmanfcf54bd2015-04-14 21:28:08 +0200442 if (vcm_->SendCodec() == webrtc::kVideoCodecVP8) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000443 webrtc::CodecSpecificInfo codec_specific_info;
444 codec_specific_info.codecType = webrtc::kVideoCodecVP8;
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000445 {
446 CriticalSectionScoped cs(data_cs_.get());
447 codec_specific_info.codecSpecific.VP8.hasReceivedRPSI =
448 has_received_rpsi_;
449 codec_specific_info.codecSpecific.VP8.hasReceivedSLI =
450 has_received_sli_;
451 codec_specific_info.codecSpecific.VP8.pictureIdRPSI =
452 picture_id_rpsi_;
453 codec_specific_info.codecSpecific.VP8.pictureIdSLI =
454 picture_id_sli_;
455 has_received_sli_ = false;
456 has_received_rpsi_ = false;
457 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000458
mflodmanfcf54bd2015-04-14 21:28:08 +0200459 vcm_->AddVideoFrame(*output_frame, vpm_->ContentMetrics(),
460 &codec_specific_info);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000461 return;
462 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000463#endif
mflodmanfcf54bd2015-04-14 21:28:08 +0200464 vcm_->AddVideoFrame(*output_frame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000465}
niklase@google.com470e71d2011-07-07 08:21:25 +0000466
pwestin@webrtc.orgce330352012-04-12 06:59:14 +0000467int ViEEncoder::SendKeyFrame() {
mflodmanfcf54bd2015-04-14 21:28:08 +0200468 return vcm_->IntraFrameRequest(0);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000469}
470
pbos@webrtc.org143451d2015-03-18 14:40:03 +0000471uint32_t ViEEncoder::LastObservedBitrateBps() const {
472 CriticalSectionScoped cs(data_cs_.get());
473 return last_observed_bitrate_bps_;
474}
475
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000476int ViEEncoder::CodecTargetBitrate(uint32_t* bitrate) const {
mflodmanfcf54bd2015-04-14 21:28:08 +0200477 if (vcm_->Bitrate(bitrate) != 0)
stefan@webrtc.org439be292012-02-16 14:45:37 +0000478 return -1;
479 return 0;
stefan@webrtc.org07b45a52012-02-02 08:37:48 +0000480}
481
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000482int32_t ViEEncoder::UpdateProtectionMethod(bool nack, bool fec) {
henrikg91d6ede2015-09-17 00:24:34 -0700483 RTC_DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000484
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000485 if (fec_enabled_ == fec && nack_enabled_ == nack) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000486 // No change needed, we're already in correct state.
487 return 0;
488 }
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000489 fec_enabled_ = fec;
490 nack_enabled_ = nack;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000491
492 // Set Video Protection for VCM.
pbosba8c15b2015-07-14 09:36:34 -0700493 VCMVideoProtection protection_mode;
494 if (fec_enabled_) {
495 protection_mode =
496 nack_enabled_ ? webrtc::kProtectionNackFEC : kProtectionFEC;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000497 } else {
pbosba8c15b2015-07-14 09:36:34 -0700498 protection_mode = nack_enabled_ ? kProtectionNack : kProtectionNone;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000499 }
pbosba8c15b2015-07-14 09:36:34 -0700500 vcm_->SetVideoProtection(protection_mode, true);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000501
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000502 if (fec_enabled_ || nack_enabled_) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000503 // The send codec must be registered to set correct MTU.
504 webrtc::VideoCodec codec;
mflodmanfcf54bd2015-04-14 21:28:08 +0200505 if (vcm_->SendCodec(&codec) == 0) {
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000506 uint32_t current_bitrate_bps = 0;
mflodmanfcf54bd2015-04-14 21:28:08 +0200507 if (vcm_->Bitrate(&current_bitrate_bps) != 0) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000508 LOG_F(LS_WARNING) <<
509 "Failed to get the current encoder target bitrate.";
stefan@webrtc.org439be292012-02-16 14:45:37 +0000510 }
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000511 // Convert to start bitrate in kbps.
512 codec.startBitrate = (current_bitrate_bps + 500) / 1000;
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000513 size_t max_payload_length = send_payload_router_->MaxPayloadLength();
mflodmanfcf54bd2015-04-14 21:28:08 +0200514 if (vcm_->RegisterSendCodec(&codec, number_of_cores_,
Peter Boströmae37abb2015-06-18 19:00:34 +0200515 static_cast<uint32_t>(max_payload_length)) !=
516 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000517 return -1;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000518 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000519 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000520 }
521 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000522}
523
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000524void ViEEncoder::SetSenderBufferingMode(int target_delay_ms) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000525 {
526 CriticalSectionScoped cs(data_cs_.get());
527 target_delay_ms_ = target_delay_ms;
528 }
mikhal@webrtc.org3d305c62013-02-10 18:42:55 +0000529 if (target_delay_ms > 0) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000530 // Disable external frame-droppers.
mflodmanfcf54bd2015-04-14 21:28:08 +0200531 vcm_->EnableFrameDropper(false);
532 vpm_->EnableTemporalDecimation(false);
mikhal@webrtc.org3d305c62013-02-10 18:42:55 +0000533 } else {
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000534 // Real-time mode - enable frame droppers.
mflodmanfcf54bd2015-04-14 21:28:08 +0200535 vpm_->EnableTemporalDecimation(true);
536 vcm_->EnableFrameDropper(true);
mikhal@webrtc.org3d305c62013-02-10 18:42:55 +0000537 }
538}
539
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000540void ViEEncoder::OnSetRates(uint32_t bitrate_bps, int framerate) {
Peter Boström7083e112015-09-22 16:28:51 +0200541 if (stats_proxy_)
542 stats_proxy_->OnSetRates(bitrate_bps, framerate);
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000543}
544
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000545int32_t ViEEncoder::SendData(
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000546 const uint8_t payload_type,
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000547 const EncodedImage& encoded_image,
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000548 const webrtc::RTPFragmentationHeader& fragmentation_header,
549 const RTPVideoHeader* rtp_video_hdr) {
henrikg91d6ede2015-09-17 00:24:34 -0700550 RTC_DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000551
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000552 {
Noah Richards099323e2015-04-15 09:14:12 -0700553 CriticalSectionScoped cs(data_cs_.get());
554 time_of_last_frame_activity_ms_ = TickTime::MillisecondTimestamp();
555 }
556
Peter Boström7083e112015-09-22 16:28:51 +0200557 if (stats_proxy_ != NULL)
558 stats_proxy_->OnSendEncodedImage(encoded_image, rtp_video_hdr);
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000559
560 return send_payload_router_->RoutePayload(
pbos22993e12015-10-19 02:39:06 -0700561 encoded_image._frameType, payload_type, encoded_image._timeStamp,
562 encoded_image.capture_time_ms_, encoded_image._buffer,
563 encoded_image._length, &fragmentation_header, rtp_video_hdr)
564 ? 0
565 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000566}
567
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000568int32_t ViEEncoder::SendStatistics(const uint32_t bit_rate,
569 const uint32_t frame_rate) {
Peter Boström7083e112015-09-22 16:28:51 +0200570 if (stats_proxy_)
571 stats_proxy_->OnOutgoingRate(frame_rate, bit_rate);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000572 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000573}
574
andrew@webrtc.org96636862012-09-20 23:33:17 +0000575void ViEEncoder::OnReceivedSLI(uint32_t /*ssrc*/,
576 uint8_t picture_id) {
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000577 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000578 picture_id_sli_ = picture_id;
579 has_received_sli_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000580}
581
andrew@webrtc.org96636862012-09-20 23:33:17 +0000582void ViEEncoder::OnReceivedRPSI(uint32_t /*ssrc*/,
583 uint64_t picture_id) {
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000584 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000585 picture_id_rpsi_ = picture_id;
586 has_received_rpsi_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000587}
588
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000589void ViEEncoder::OnReceivedIntraFrameRequest(uint32_t ssrc) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000590 // Key frame request from remote side, signal to VCM.
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000591 TRACE_EVENT0("webrtc", "OnKeyFrameRequest");
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000592
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000593 int idx = 0;
594 {
595 CriticalSectionScoped cs(data_cs_.get());
Peter Boström5cb9ce42015-05-05 15:16:30 +0200596 auto stream_it = ssrc_streams_.find(ssrc);
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000597 if (stream_it == ssrc_streams_.end()) {
mflodman@webrtc.orgd73527c2012-12-20 09:26:17 +0000598 LOG_F(LS_WARNING) << "ssrc not found: " << ssrc << ", map size "
599 << ssrc_streams_.size();
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000600 return;
601 }
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000602 std::map<unsigned int, int64_t>::iterator time_it =
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000603 time_last_intra_request_ms_.find(ssrc);
604 if (time_it == time_last_intra_request_ms_.end()) {
605 time_last_intra_request_ms_[ssrc] = 0;
606 }
607
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000608 int64_t now = TickTime::MillisecondTimestamp();
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000609 if (time_last_intra_request_ms_[ssrc] + kViEMinKeyRequestIntervalMs > now) {
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000610 return;
611 }
612 time_last_intra_request_ms_[ssrc] = now;
613 idx = stream_it->second;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000614 }
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000615 // Release the critsect before triggering key frame.
mflodmanfcf54bd2015-04-14 21:28:08 +0200616 vcm_->IntraFrameRequest(idx);
niklase@google.com470e71d2011-07-07 08:21:25 +0000617}
618
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000619void ViEEncoder::OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc) {
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000620 CriticalSectionScoped cs(data_cs_.get());
621 std::map<unsigned int, int>::iterator it = ssrc_streams_.find(old_ssrc);
622 if (it == ssrc_streams_.end()) {
623 return;
624 }
625
626 ssrc_streams_[new_ssrc] = it->second;
627 ssrc_streams_.erase(it);
628
629 std::map<unsigned int, int64_t>::iterator time_it =
630 time_last_intra_request_ms_.find(old_ssrc);
631 int64_t last_intra_request_ms = 0;
632 if (time_it != time_last_intra_request_ms_.end()) {
633 last_intra_request_ms = time_it->second;
634 time_last_intra_request_ms_.erase(time_it);
635 }
636 time_last_intra_request_ms_[new_ssrc] = last_intra_request_ms;
637}
638
Peter Boström5cb9ce42015-05-05 15:16:30 +0200639bool ViEEncoder::SetSsrcs(const std::vector<uint32_t>& ssrcs) {
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000640 VideoCodec codec;
mflodmanfcf54bd2015-04-14 21:28:08 +0200641 if (vcm_->SendCodec(&codec) != 0)
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000642 return false;
643
644 if (codec.numberOfSimulcastStreams > 0 &&
645 ssrcs.size() != codec.numberOfSimulcastStreams) {
646 return false;
647 }
648
649 CriticalSectionScoped cs(data_cs_.get());
650 ssrc_streams_.clear();
651 time_last_intra_request_ms_.clear();
652 int idx = 0;
Peter Boström5cb9ce42015-05-05 15:16:30 +0200653 for (uint32_t ssrc : ssrcs) {
654 ssrc_streams_[ssrc] = idx++;
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000655 }
656 return true;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000657}
658
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000659void ViEEncoder::SetMinTransmitBitrate(int min_transmit_bitrate_kbps) {
660 assert(min_transmit_bitrate_kbps >= 0);
661 CriticalSectionScoped crit(data_cs_.get());
662 min_transmit_bitrate_kbps_ = min_transmit_bitrate_kbps;
663}
664
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000665// Called from ViEBitrateObserver.
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000666void ViEEncoder::OnNetworkChanged(uint32_t bitrate_bps,
667 uint8_t fraction_lost,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000668 int64_t round_trip_time_ms) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000669 LOG(LS_VERBOSE) << "OnNetworkChanged, bitrate" << bitrate_bps
mflodman8602a3d2015-05-20 15:54:42 -0700670 << " packet loss " << static_cast<int>(fraction_lost)
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000671 << " rtt " << round_trip_time_ms;
henrikg91d6ede2015-09-17 00:24:34 -0700672 RTC_DCHECK(send_payload_router_ != NULL);
mflodmanfcf54bd2015-04-14 21:28:08 +0200673 vcm_->SetChannelParameters(bitrate_bps, fraction_lost, round_trip_time_ms);
674 bool video_is_suspended = vcm_->VideoSuspended();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000675
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000676 VideoCodec send_codec;
mflodmanfcf54bd2015-04-14 21:28:08 +0200677 if (vcm_->SendCodec(&send_codec) != 0) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000678 return;
679 }
stefan@webrtc.orgb2c8a952013-09-06 13:58:01 +0000680 SimulcastStream* stream_configs = send_codec.simulcastStream;
681 // Allocate the bandwidth between the streams.
682 std::vector<uint32_t> stream_bitrates = AllocateStreamBitrates(
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000683 bitrate_bps, stream_configs, send_codec.numberOfSimulcastStreams);
684 send_payload_router_->SetTargetSendBitrates(stream_bitrates);
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000685
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000686 {
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000687 CriticalSectionScoped cs(data_cs_.get());
pbos@webrtc.org143451d2015-03-18 14:40:03 +0000688 last_observed_bitrate_bps_ = bitrate_bps;
pbos@webrtc.org484ee962013-11-21 18:44:23 +0000689 if (video_suspended_ == video_is_suspended)
690 return;
691 video_suspended_ = video_is_suspended;
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000692 LOG(LS_INFO) << "Video suspended " << video_is_suspended
693 << " for channel " << channel_id_;
henrik.lundin@webrtc.org7ea4f242013-10-02 13:34:26 +0000694 }
Peter Boström7083e112015-09-22 16:28:51 +0200695 // Video suspend-state changed, inform codec observer.
696 if (stats_proxy_)
697 stats_proxy_->OnSuspendChange(video_is_suspended);
niklase@google.com470e71d2011-07-07 08:21:25 +0000698}
699
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000700void ViEEncoder::SuspendBelowMinBitrate() {
mflodmanfcf54bd2015-04-14 21:28:08 +0200701 vcm_->SuspendBelowMinBitrate();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000702 bitrate_allocator_->EnforceMinBitrate(false);
henrik.lundin@webrtc.org7ea4f242013-10-02 13:34:26 +0000703}
704
sprang@webrtc.org40709352013-11-26 11:41:59 +0000705void ViEEncoder::RegisterPostEncodeImageCallback(
706 EncodedImageCallback* post_encode_callback) {
mflodmanfcf54bd2015-04-14 21:28:08 +0200707 vcm_->RegisterPostEncodeImageCallback(post_encode_callback);
sprang@webrtc.org40709352013-11-26 11:41:59 +0000708}
709
marpan@webrtc.orgefd01fd2012-04-18 15:56:34 +0000710QMVideoSettingsCallback::QMVideoSettingsCallback(VideoProcessingModule* vpm)
711 : vpm_(vpm) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000712}
niklase@google.com470e71d2011-07-07 08:21:25 +0000713
stefan@webrtc.org439be292012-02-16 14:45:37 +0000714QMVideoSettingsCallback::~QMVideoSettingsCallback() {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000715}
716
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000717int32_t QMVideoSettingsCallback::SetVideoQMSettings(
718 const uint32_t frame_rate,
719 const uint32_t width,
720 const uint32_t height) {
marpan@webrtc.orgcf706c22012-03-27 21:04:13 +0000721 return vpm_->SetTargetResolution(width, height, frame_rate);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000722}
723
jackychen6e2ce6e2015-07-13 16:26:33 -0700724void QMVideoSettingsCallback::SetTargetFramerate(int frame_rate) {
725 vpm_->SetTargetFramerate(frame_rate);
726}
727
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000728} // namespace webrtc