blob: 391766cb863074807c0db8600f431474bf7d1c75 [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"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000021#include "webrtc/modules/pacing/include/paced_sender.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000022#include "webrtc/modules/utility/interface/process_thread.h"
23#include "webrtc/modules/video_coding/codecs/interface/video_codec_interface.h"
24#include "webrtc/modules/video_coding/main/interface/video_coding.h"
25#include "webrtc/modules/video_coding/main/interface/video_coding_defines.h"
sprang@webrtc.org40709352013-11-26 11:41:59 +000026#include "webrtc/modules/video_coding/main/source/encoded_frame.h"
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000027#include "webrtc/system_wrappers/interface/clock.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000028#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
29#include "webrtc/system_wrappers/interface/logging.h"
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +000030#include "webrtc/system_wrappers/interface/metrics.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000031#include "webrtc/system_wrappers/interface/tick_util.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000032#include "webrtc/system_wrappers/interface/trace_event.h"
pbos@webrtc.org273a4142014-12-01 15:23:21 +000033#include "webrtc/video/send_statistics_proxy.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000034#include "webrtc/video_engine/include/vie_codec.h"
35#include "webrtc/video_engine/include/vie_image_process.h"
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000036#include "webrtc/video_engine/payload_router.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000037#include "webrtc/video_engine/vie_defines.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000038
niklase@google.com470e71d2011-07-07 08:21:25 +000039namespace webrtc {
40
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +000041// Margin on when we pause the encoder when the pacing buffer overflows relative
42// to the configured buffer delay.
43static const float kEncoderPausePacerMargin = 2.0f;
44
pwestin@webrtc.org91563e42013-04-25 22:20:08 +000045// Don't stop the encoder unless the delay is above this configured value.
46static const int kMinPacingDelayMs = 200;
47
stefan@webrtc.org3e005052013-10-18 15:05:29 +000048static const float kStopPaddingThresholdMs = 2000;
49
stefan@webrtc.orgb2c8a952013-09-06 13:58:01 +000050std::vector<uint32_t> AllocateStreamBitrates(
51 uint32_t total_bitrate,
52 const SimulcastStream* stream_configs,
53 size_t number_of_streams) {
54 if (number_of_streams == 0) {
55 std::vector<uint32_t> stream_bitrates(1, 0);
56 stream_bitrates[0] = total_bitrate;
57 return stream_bitrates;
58 }
59 std::vector<uint32_t> stream_bitrates(number_of_streams, 0);
60 uint32_t bitrate_remainder = total_bitrate;
61 for (size_t i = 0; i < stream_bitrates.size() && bitrate_remainder > 0; ++i) {
62 if (stream_configs[i].maxBitrate * 1000 > bitrate_remainder) {
63 stream_bitrates[i] = bitrate_remainder;
64 } else {
65 stream_bitrates[i] = stream_configs[i].maxBitrate * 1000;
66 }
67 bitrate_remainder -= stream_bitrates[i];
68 }
69 return stream_bitrates;
70}
71
stefan@webrtc.org439be292012-02-16 14:45:37 +000072class QMVideoSettingsCallback : public VCMQMSettingsCallback {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000073 public:
marpan@webrtc.orgefd01fd2012-04-18 15:56:34 +000074 explicit QMVideoSettingsCallback(VideoProcessingModule* vpm);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000075
stefan@webrtc.org439be292012-02-16 14:45:37 +000076 ~QMVideoSettingsCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +000077
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000078 // Update VPM with QM (quality modes: frame size & frame rate) settings.
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000079 int32_t SetVideoQMSettings(const uint32_t frame_rate,
80 const uint32_t width,
81 const uint32_t height);
niklase@google.com470e71d2011-07-07 08:21:25 +000082
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000083 private:
84 VideoProcessingModule* vpm_;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000085};
niklase@google.com470e71d2011-07-07 08:21:25 +000086
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000087class ViEBitrateObserver : public BitrateObserver {
88 public:
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +000089 explicit ViEBitrateObserver(ViEEncoder* owner)
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000090 : owner_(owner) {
91 }
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000092 virtual ~ViEBitrateObserver() {}
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000093 // Implements BitrateObserver.
stefan@webrtc.orgedeea912014-12-08 19:46:23 +000094 virtual void OnNetworkChanged(uint32_t bitrate_bps,
95 uint8_t fraction_lost,
pkasting@chromium.org16825b12015-01-12 21:51:21 +000096 int64_t rtt) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000097 owner_->OnNetworkChanged(bitrate_bps, fraction_lost, rtt);
98 }
99 private:
100 ViEEncoder* owner_;
101};
niklase@google.com470e71d2011-07-07 08:21:25 +0000102
mflodman@webrtc.org9dd0ebc2015-02-26 12:57:47 +0000103ViEEncoder::ViEEncoder(int32_t channel_id,
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000104 uint32_t number_of_cores,
andresp@webrtc.org7707d062013-05-13 10:50:50 +0000105 const Config& config,
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000106 ProcessThread& module_process_thread,
Stefan Holmere5904162015-03-26 11:11:06 +0100107 PacedSender* pacer,
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000108 BitrateAllocator* bitrate_allocator,
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000109 BitrateController* bitrate_controller,
110 bool disable_default_encoder)
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000111 : channel_id_(channel_id),
112 number_of_cores_(number_of_cores),
113 disable_default_encoder_(disable_default_encoder),
mflodman0828a0c2015-03-31 15:29:23 +0200114 vcm_(*webrtc::VideoCodingModule::Create(this)),
115 vpm_(*webrtc::VideoProcessingModule::Create(ViEModuleId(-1, channel_id))),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000116 send_payload_router_(NULL),
mflodman0828a0c2015-03-31 15:29:23 +0200117 vcm_protection_callback_(NULL),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000118 callback_cs_(CriticalSectionWrapper::CreateCriticalSection()),
119 data_cs_(CriticalSectionWrapper::CreateCriticalSection()),
Stefan Holmere5904162015-03-26 11:11:06 +0100120 pacer_(pacer),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000121 bitrate_allocator_(bitrate_allocator),
122 bitrate_controller_(bitrate_controller),
123 time_of_last_incoming_frame_ms_(0),
124 send_padding_(false),
125 min_transmit_bitrate_kbps_(0),
pbos@webrtc.org143451d2015-03-18 14:40:03 +0000126 last_observed_bitrate_bps_(0),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000127 target_delay_ms_(0),
128 network_is_transmitting_(true),
129 encoder_paused_(false),
130 encoder_paused_and_dropped_frame_(false),
131 fec_enabled_(false),
132 nack_enabled_(false),
133 codec_observer_(NULL),
134 effect_filter_(NULL),
135 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),
mflodman0828a0c2015-03-31 15:29:23 +0200140 qm_callback_(NULL),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000141 video_suspended_(false),
142 pre_encode_callback_(NULL),
143 start_ms_(Clock::GetRealTimeClock()->TimeInMilliseconds()),
144 send_statistics_proxy_(NULL) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000145 bitrate_observer_.reset(new ViEBitrateObserver(this));
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000146}
147
148bool ViEEncoder::Init() {
mflodman0828a0c2015-03-31 15:29:23 +0200149 if (vcm_.InitializeSender() != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000150 return false;
151 }
mflodman0828a0c2015-03-31 15:29:23 +0200152 vpm_.EnableTemporalDecimation(true);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000153
154 // Enable/disable content analysis: off by default for now.
mflodman0828a0c2015-03-31 15:29:23 +0200155 vpm_.EnableContentAnalysis(false);
156
157 if (qm_callback_) {
158 delete qm_callback_;
159 }
160 qm_callback_ = new QMVideoSettingsCallback(&vpm_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000161
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000162 if (!disable_default_encoder_) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000163#ifdef VIDEOCODEC_VP8
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000164 VideoCodecType codec_type = webrtc::kVideoCodecVP8;
andresp@webrtc.orga84b0a62014-08-14 16:46:46 +0000165#else
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000166 VideoCodecType codec_type = webrtc::kVideoCodecI420;
andresp@webrtc.orga84b0a62014-08-14 16:46:46 +0000167#endif
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000168 VideoCodec video_codec;
mflodman0828a0c2015-03-31 15:29:23 +0200169 if (vcm_.Codec(codec_type, &video_codec) != VCM_OK) {
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000170 return false;
171 }
172 {
173 CriticalSectionScoped cs(data_cs_.get());
174 send_padding_ = video_codec.numberOfSimulcastStreams > 1;
175 }
mflodman0828a0c2015-03-31 15:29:23 +0200176 if (vcm_.RegisterSendCodec(&video_codec, number_of_cores_,
177 PayloadRouter::DefaultMaxPayloadLength()) != 0) {
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000178 return false;
179 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000180 }
mflodman0828a0c2015-03-31 15:29:23 +0200181 if (vcm_.RegisterTransportCallback(this) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000182 return false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000183 }
mflodman0828a0c2015-03-31 15:29:23 +0200184 if (vcm_.RegisterSendStatisticsCallback(this) != 0) {
185 return false;
186 }
187 if (vcm_.RegisterVideoQMCallback(qm_callback_) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000188 return false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000189 }
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000190 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000191}
192
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000193void ViEEncoder::StartThreadsAndSetSharedMembers(
194 scoped_refptr<PayloadRouter> send_payload_router,
195 VCMProtectionCallback* vcm_protection_callback) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000196 DCHECK(send_payload_router_ == NULL);
mflodman0828a0c2015-03-31 15:29:23 +0200197 DCHECK(vcm_protection_callback_ == NULL);
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000198
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000199 send_payload_router_ = send_payload_router;
mflodman0828a0c2015-03-31 15:29:23 +0200200 vcm_protection_callback_ = vcm_protection_callback;
201
202 module_process_thread_.RegisterModule(&vcm_);
mflodman@webrtc.org290cb562015-02-17 10:15:06 +0000203}
204
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000205void ViEEncoder::StopThreadsAndRemoveSharedMembers() {
mflodman0828a0c2015-03-31 15:29:23 +0200206 vcm_.RegisterProtectionCallback(NULL);
207 vcm_protection_callback_ = NULL;
208 module_process_thread_.DeRegisterModule(&vcm_);
209 module_process_thread_.DeRegisterModule(&vpm_);
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000210}
211
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000212ViEEncoder::~ViEEncoder() {
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000213 UpdateHistograms();
mflodman0828a0c2015-03-31 15:29:23 +0200214 if (bitrate_allocator_)
215 bitrate_allocator_->RemoveBitrateObserver(bitrate_observer_.get());
216 VideoCodingModule::Destroy(&vcm_);
217 VideoProcessingModule::Destroy(&vpm_);
218 delete qm_callback_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000219}
220
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000221void ViEEncoder::UpdateHistograms() {
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000222 int64_t elapsed_sec =
223 (Clock::GetRealTimeClock()->TimeInMilliseconds() - start_ms_) / 1000;
224 if (elapsed_sec < metrics::kMinRunTimeInSeconds) {
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000225 return;
226 }
227 webrtc::VCMFrameCount frames;
mflodman0828a0c2015-03-31 15:29:23 +0200228 if (vcm_.SentFrameCount(frames) != VCM_OK) {
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000229 return;
230 }
231 uint32_t total_frames = frames.numKeyFrames + frames.numDeltaFrames;
232 if (total_frames > 0) {
233 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.KeyFramesSentInPermille",
234 static_cast<int>(
235 (frames.numKeyFrames * 1000.0f / total_frames) + 0.5f));
236 }
237}
238
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000239int ViEEncoder::Owner() const {
240 return channel_id_;
241}
242
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000243void ViEEncoder::SetNetworkTransmissionState(bool is_transmitting) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000244 {
245 CriticalSectionScoped cs(data_cs_.get());
246 network_is_transmitting_ = is_transmitting;
247 }
248 if (is_transmitting) {
Stefan Holmere5904162015-03-26 11:11:06 +0100249 pacer_->Resume();
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000250 } else {
Stefan Holmere5904162015-03-26 11:11:06 +0100251 pacer_->Pause();
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000252 }
253}
254
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000255void ViEEncoder::Pause() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000256 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000257 encoder_paused_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000258}
259
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000260void ViEEncoder::Restart() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000261 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000262 encoder_paused_ = false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000263}
264
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000265uint8_t ViEEncoder::NumberOfCodecs() {
mflodman0828a0c2015-03-31 15:29:23 +0200266 return vcm_.NumberOfCodecs();
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000267}
268
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000269int32_t ViEEncoder::GetCodec(uint8_t list_index, VideoCodec* video_codec) {
mflodman0828a0c2015-03-31 15:29:23 +0200270 if (vcm_.Codec(list_index, video_codec) != 0) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000271 return -1;
272 }
273 return 0;
274}
275
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000276int32_t ViEEncoder::RegisterExternalEncoder(webrtc::VideoEncoder* encoder,
277 uint8_t pl_type,
278 bool internal_source) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000279 if (encoder == NULL)
280 return -1;
281
mflodman0828a0c2015-03-31 15:29:23 +0200282 if (vcm_.RegisterExternalEncoder(encoder, pl_type, internal_source) !=
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000283 VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000284 return -1;
285 }
286 return 0;
287}
288
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000289int32_t ViEEncoder::DeRegisterExternalEncoder(uint8_t pl_type) {
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000290 DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000291 webrtc::VideoCodec current_send_codec;
mflodman0828a0c2015-03-31 15:29:23 +0200292 if (vcm_.SendCodec(&current_send_codec) == VCM_OK) {
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000293 uint32_t current_bitrate_bps = 0;
mflodman0828a0c2015-03-31 15:29:23 +0200294 if (vcm_.Bitrate(&current_bitrate_bps) != 0) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000295 LOG(LS_WARNING) << "Failed to get the current encoder target bitrate.";
stefan@webrtc.org439be292012-02-16 14:45:37 +0000296 }
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000297 current_send_codec.startBitrate = (current_bitrate_bps + 500) / 1000;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000298 }
299
mflodman0828a0c2015-03-31 15:29:23 +0200300 if (vcm_.RegisterExternalEncoder(NULL, pl_type) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000301 return -1;
302 }
303
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000304 if (disable_default_encoder_)
305 return 0;
306
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000307 // If the external encoder is the current send codec, use vcm internal
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000308 // encoder.
309 if (current_send_codec.plType == pl_type) {
stefan@webrtc.orgae2563a2014-02-13 13:48:38 +0000310 {
311 CriticalSectionScoped cs(data_cs_.get());
312 send_padding_ = current_send_codec.numberOfSimulcastStreams > 1;
313 }
fischman@webrtc.org64e04052014-03-07 18:00:05 +0000314 // TODO(mflodman): Unfortunately the VideoCodec that VCM has cached a
315 // raw pointer to an |extra_options| that's long gone. Clearing it here is
316 // a hack to prevent the following code from crashing. This should be fixed
317 // for realz. https://code.google.com/p/chromium/issues/detail?id=348222
318 current_send_codec.extra_options = NULL;
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000319 size_t max_data_payload_length = send_payload_router_->MaxPayloadLength();
mflodman0828a0c2015-03-31 15:29:23 +0200320 if (vcm_.RegisterSendCodec(&current_send_codec, number_of_cores_,
321 max_data_payload_length) != VCM_OK) {
stefan@webrtc.org4070b1d2014-07-16 11:20:40 +0000322 LOG(LS_INFO) << "De-registered the currently used external encoder ("
323 << static_cast<int>(pl_type) << ") and therefore tried to "
324 << "register the corresponding internal encoder, but none "
325 << "was supported.";
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000326 }
327 }
328 return 0;
329}
330
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000331int32_t ViEEncoder::SetEncoder(const webrtc::VideoCodec& video_codec) {
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000332 DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000333 // Setting target width and height for VPM.
mflodman0828a0c2015-03-31 15:29:23 +0200334 if (vpm_.SetTargetResolution(video_codec.width, video_codec.height,
335 video_codec.maxFramerate) != VPM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000336 return -1;
337 }
338
stefan@webrtc.org9075d512014-02-14 09:45:58 +0000339 {
340 CriticalSectionScoped cs(data_cs_.get());
341 send_padding_ = video_codec.numberOfSimulcastStreams > 1;
342 }
Stefan Holmere5904162015-03-26 11:11:06 +0100343
344 // Add a bitrate observer to the allocator and update the start, max and
345 // min bitrates of the bitrate controller as needed.
346 int allocated_bitrate_bps;
347 int new_bwe_candidate_bps = bitrate_allocator_->AddBitrateObserver(
348 bitrate_observer_.get(), video_codec.startBitrate * 1000,
349 video_codec.minBitrate * 1000, video_codec.maxBitrate * 1000,
350 &allocated_bitrate_bps);
351
352 // Only set the start/min/max bitrate of the bitrate controller if the start
353 // bitrate is greater than zero. The new API sets these via the channel group
354 // and passes a zero start bitrate to SetSendCodec.
355 // TODO(holmer): Remove this when the new API has been launched.
356 if (video_codec.startBitrate > 0) {
357 if (new_bwe_candidate_bps > 0) {
358 uint32_t current_bwe_bps = 0;
359 bitrate_controller_->AvailableBandwidth(&current_bwe_bps);
360 bitrate_controller_->SetStartBitrate(std::max(
361 static_cast<uint32_t>(new_bwe_candidate_bps), current_bwe_bps));
362 }
363
364 int new_bwe_min_bps = 0;
365 int new_bwe_max_bps = 0;
366 bitrate_allocator_->GetMinMaxBitrateSumBps(&new_bwe_min_bps,
367 &new_bwe_max_bps);
368 bitrate_controller_->SetMinMaxBitrate(new_bwe_min_bps, new_bwe_max_bps);
369 }
370
371 webrtc::VideoCodec modified_video_codec = video_codec;
372 modified_video_codec.startBitrate = allocated_bitrate_bps / 1000;
373
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000374 size_t max_data_payload_length = send_payload_router_->MaxPayloadLength();
mflodman0828a0c2015-03-31 15:29:23 +0200375 if (vcm_.RegisterSendCodec(&modified_video_codec, number_of_cores_,
376 max_data_payload_length) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000377 return -1;
378 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000379 return 0;
380}
381
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000382int32_t ViEEncoder::GetEncoder(VideoCodec* video_codec) {
mflodman0828a0c2015-03-31 15:29:23 +0200383 *video_codec = vcm_.GetSendCodec();
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000384 return 0;
385}
niklase@google.com470e71d2011-07-07 08:21:25 +0000386
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000387int32_t ViEEncoder::GetCodecConfigParameters(
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000388 unsigned char config_parameters[kConfigParameterSize],
389 unsigned char& config_parameters_size) {
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000390 int32_t num_parameters =
mflodman0828a0c2015-03-31 15:29:23 +0200391 vcm_.CodecConfigParameters(config_parameters, kConfigParameterSize);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000392 if (num_parameters <= 0) {
393 config_parameters_size = 0;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000394 return -1;
395 }
396 config_parameters_size = static_cast<unsigned char>(num_parameters);
397 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000398}
399
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000400int32_t ViEEncoder::ScaleInputImage(bool enable) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000401 VideoFrameResampling resampling_mode = kFastRescaling;
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000402 // TODO(mflodman) What?
403 if (enable) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000404 // kInterpolation is currently not supported.
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000405 LOG_F(LS_ERROR) << "Not supported.";
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000406 return -1;
407 }
mflodman0828a0c2015-03-31 15:29:23 +0200408 vpm_.SetInputFrameResampleMode(resampling_mode);
niklase@google.com470e71d2011-07-07 08:21:25 +0000409
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000410 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000411}
412
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000413int ViEEncoder::GetPaddingNeededBps(int bitrate_bps) const {
414 int64_t time_of_last_incoming_frame_ms;
415 int min_transmit_bitrate_bps;
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +0000416 {
417 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000418 bool send_padding =
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000419 send_padding_ || video_suspended_ || min_transmit_bitrate_kbps_ > 0;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000420 if (!send_padding)
421 return 0;
422 time_of_last_incoming_frame_ms = time_of_last_incoming_frame_ms_;
423 min_transmit_bitrate_bps = 1000 * min_transmit_bitrate_kbps_;
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +0000424 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000425
426 VideoCodec send_codec;
mflodman0828a0c2015-03-31 15:29:23 +0200427 if (vcm_.SendCodec(&send_codec) != 0)
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000428 return 0;
429 SimulcastStream* stream_configs = send_codec.simulcastStream;
430 // Allocate the bandwidth between the streams.
431 std::vector<uint32_t> stream_bitrates = AllocateStreamBitrates(
432 bitrate_bps, stream_configs, send_codec.numberOfSimulcastStreams);
433
mflodman0828a0c2015-03-31 15:29:23 +0200434 bool video_is_suspended = vcm_.VideoSuspended();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000435
436 // Find the max amount of padding we can allow ourselves to send at this
437 // point, based on which streams are currently active and what our current
438 // available bandwidth is.
439 int pad_up_to_bitrate_bps = 0;
440 if (send_codec.numberOfSimulcastStreams == 0) {
441 pad_up_to_bitrate_bps = send_codec.minBitrate * 1000;
442 } else {
443 pad_up_to_bitrate_bps =
444 stream_configs[send_codec.numberOfSimulcastStreams - 1].minBitrate *
445 1000;
446 for (int i = 0; i < send_codec.numberOfSimulcastStreams - 1; ++i) {
447 pad_up_to_bitrate_bps += stream_configs[i].targetBitrate * 1000;
448 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000449 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000450
451 // Disable padding if only sending one stream and video isn't suspended and
452 // min-transmit bitrate isn't used (applied later).
453 if (!video_is_suspended && send_codec.numberOfSimulcastStreams <= 1)
454 pad_up_to_bitrate_bps = 0;
455
456 // The amount of padding should decay to zero if no frames are being
457 // captured unless a min-transmit bitrate is used.
458 int64_t now_ms = TickTime::MillisecondTimestamp();
459 if (now_ms - time_of_last_incoming_frame_ms > kStopPaddingThresholdMs)
460 pad_up_to_bitrate_bps = 0;
461
462 // Pad up to min bitrate.
463 if (pad_up_to_bitrate_bps < min_transmit_bitrate_bps)
464 pad_up_to_bitrate_bps = min_transmit_bitrate_bps;
465
466 // Padding may never exceed bitrate estimate.
467 if (pad_up_to_bitrate_bps > bitrate_bps)
468 pad_up_to_bitrate_bps = bitrate_bps;
469
470 return pad_up_to_bitrate_bps;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000471}
472
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000473bool ViEEncoder::EncoderPaused() const {
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000474 // Pause video if paused by caller or as long as the network is down or the
475 // pacer queue has grown too large in buffered mode.
476 if (encoder_paused_) {
477 return true;
478 }
479 if (target_delay_ms_ > 0) {
480 // Buffered mode.
481 // TODO(pwestin): Workaround until nack is configured as a time and not
482 // number of packets.
Stefan Holmere5904162015-03-26 11:11:06 +0100483 return pacer_->QueueInMs() >=
484 std::max(
485 static_cast<int>(target_delay_ms_ * kEncoderPausePacerMargin),
486 kMinPacingDelayMs);
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000487 }
Stefan Holmere5904162015-03-26 11:11:06 +0100488 if (pacer_->ExpectedQueueTimeMs() > PacedSender::kDefaultMaxQueueLengthMs) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000489 // Too much data in pacer queue, drop frame.
490 return true;
491 }
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000492 return !network_is_transmitting_;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000493}
494
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000495void ViEEncoder::TraceFrameDropStart() {
496 // Start trace event only on the first frame after encoder is paused.
497 if (!encoder_paused_and_dropped_frame_) {
498 TRACE_EVENT_ASYNC_BEGIN0("webrtc", "EncoderPaused", this);
499 }
500 encoder_paused_and_dropped_frame_ = true;
501 return;
502}
503
504void ViEEncoder::TraceFrameDropEnd() {
505 // End trace event on first frame after encoder resumes, if frame was dropped.
506 if (encoder_paused_and_dropped_frame_) {
507 TRACE_EVENT_ASYNC_END0("webrtc", "EncoderPaused", this);
508 }
509 encoder_paused_and_dropped_frame_ = false;
510}
511
mflodman@webrtc.org8baed512012-06-21 12:11:50 +0000512void ViEEncoder::DeliverFrame(int id,
Magnus Jedvert26679d62015-04-07 14:07:41 +0200513 const I420VideoFrame& video_frame,
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000514 const std::vector<uint32_t>& csrcs) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000515 DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.orga98e7962015-02-11 15:45:56 +0000516 DCHECK(csrcs.empty());
mflodman@webrtc.org47d657b2015-02-19 10:29:32 +0000517 if (!send_payload_router_->active()) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000518 // We've paused or we have no channels attached, don't waste resources on
519 // encoding.
wuchengli@chromium.orgac4b87c2014-03-19 03:44:20 +0000520 return;
521 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000522 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000523 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000524 time_of_last_incoming_frame_ms_ = TickTime::MillisecondTimestamp();
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000525 if (EncoderPaused()) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000526 TraceFrameDropStart();
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000527 return;
528 }
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000529 TraceFrameDropEnd();
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000530 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000531
Magnus Jedvert26679d62015-04-07 14:07:41 +0200532 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(),
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000533 "Encode");
pwestin@webrtc.org2f476ed2012-10-30 16:21:52 +0000534 I420VideoFrame* decimated_frame = NULL;
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000535 // TODO(wuchengli): support texture frames.
Magnus Jedvert26679d62015-04-07 14:07:41 +0200536 if (video_frame.native_handle() == NULL) {
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000537 {
538 CriticalSectionScoped cs(callback_cs_.get());
539 if (effect_filter_) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000540 size_t length =
Magnus Jedvert26679d62015-04-07 14:07:41 +0200541 CalcBufferSize(kI420, video_frame.width(), video_frame.height());
mflodman@webrtc.org948d6172015-02-10 08:58:16 +0000542 rtc::scoped_ptr<uint8_t[]> video_buffer(new uint8_t[length]);
Magnus Jedvert26679d62015-04-07 14:07:41 +0200543 ExtractBuffer(video_frame, length, video_buffer.get());
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000544 effect_filter_->Transform(length,
545 video_buffer.get(),
Magnus Jedvert26679d62015-04-07 14:07:41 +0200546 video_frame.ntp_time_ms(),
547 video_frame.timestamp(),
548 video_frame.width(),
549 video_frame.height());
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000550 }
551 }
552
553 // Pass frame via preprocessor.
Magnus Jedvert26679d62015-04-07 14:07:41 +0200554 const int ret = vpm_.PreprocessFrame(video_frame, &decimated_frame);
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000555 if (ret == 1) {
556 // Drop this frame.
557 return;
558 }
559 if (ret != VPM_OK) {
560 return;
561 }
pwestin@webrtc.org2f476ed2012-10-30 16:21:52 +0000562 }
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000563
Magnus Jedvert26679d62015-04-07 14:07:41 +0200564 // If we haven't resampled the frame and we have a FrameCallback, we need to
565 // make a deep copy of |video_frame|.
566 I420VideoFrame copied_frame;
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000567 {
568 CriticalSectionScoped cs(callback_cs_.get());
Magnus Jedvert26679d62015-04-07 14:07:41 +0200569 if (pre_encode_callback_) {
570 // If the frame was not resampled or scaled => use copy of original.
571 if (decimated_frame == NULL) {
572 copied_frame.CopyFrame(video_frame);
573 decimated_frame = &copied_frame;
574 }
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000575 pre_encode_callback_->FrameCallback(decimated_frame);
Magnus Jedvert26679d62015-04-07 14:07:41 +0200576 }
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000577 }
578
Magnus Jedvert26679d62015-04-07 14:07:41 +0200579 // If the frame was not resampled, scaled, or touched by FrameCallback => use
580 // original. The frame is const from here.
581 const I420VideoFrame* output_frame =
582 (decimated_frame != NULL) ? decimated_frame : &video_frame;
583
584 if (video_frame.native_handle() != NULL) {
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000585 // TODO(wuchengli): add texture support. http://crbug.com/362437
586 return;
587 }
588
niklase@google.com470e71d2011-07-07 08:21:25 +0000589#ifdef VIDEOCODEC_VP8
mflodman0828a0c2015-03-31 15:29:23 +0200590 if (vcm_.SendCodec() == webrtc::kVideoCodecVP8) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000591 webrtc::CodecSpecificInfo codec_specific_info;
592 codec_specific_info.codecType = webrtc::kVideoCodecVP8;
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000593 {
594 CriticalSectionScoped cs(data_cs_.get());
595 codec_specific_info.codecSpecific.VP8.hasReceivedRPSI =
596 has_received_rpsi_;
597 codec_specific_info.codecSpecific.VP8.hasReceivedSLI =
598 has_received_sli_;
599 codec_specific_info.codecSpecific.VP8.pictureIdRPSI =
600 picture_id_rpsi_;
601 codec_specific_info.codecSpecific.VP8.pictureIdSLI =
602 picture_id_sli_;
603 has_received_sli_ = false;
604 has_received_rpsi_ = false;
605 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000606
Magnus Jedvert26679d62015-04-07 14:07:41 +0200607 vcm_.AddVideoFrame(*output_frame, vpm_.ContentMetrics(),
mflodman0828a0c2015-03-31 15:29:23 +0200608 &codec_specific_info);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000609 return;
610 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000611#endif
Magnus Jedvert26679d62015-04-07 14:07:41 +0200612 vcm_.AddVideoFrame(*output_frame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000613}
niklase@google.com470e71d2011-07-07 08:21:25 +0000614
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000615void ViEEncoder::DelayChanged(int id, int frame_delay) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000616}
niklase@google.com470e71d2011-07-07 08:21:25 +0000617
mflodman@webrtc.org8baed512012-06-21 12:11:50 +0000618int ViEEncoder::GetPreferedFrameSettings(int* width,
619 int* height,
620 int* frame_rate) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000621 webrtc::VideoCodec video_codec;
622 memset(&video_codec, 0, sizeof(video_codec));
mflodman0828a0c2015-03-31 15:29:23 +0200623 if (vcm_.SendCodec(&video_codec) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000624 return -1;
625 }
626
mflodman@webrtc.org8baed512012-06-21 12:11:50 +0000627 *width = video_codec.width;
628 *height = video_codec.height;
629 *frame_rate = video_codec.maxFramerate;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000630 return 0;
631}
632
pwestin@webrtc.orgce330352012-04-12 06:59:14 +0000633int ViEEncoder::SendKeyFrame() {
mflodman0828a0c2015-03-31 15:29:23 +0200634 return vcm_.IntraFrameRequest(0);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000635}
636
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000637int32_t ViEEncoder::SendCodecStatistics(
638 uint32_t* num_key_frames, uint32_t* num_delta_frames) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000639 webrtc::VCMFrameCount sent_frames;
mflodman0828a0c2015-03-31 15:29:23 +0200640 if (vcm_.SentFrameCount(sent_frames) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000641 return -1;
642 }
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +0000643 *num_key_frames = sent_frames.numKeyFrames;
644 *num_delta_frames = sent_frames.numDeltaFrames;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000645 return 0;
646}
647
pbos@webrtc.org143451d2015-03-18 14:40:03 +0000648uint32_t ViEEncoder::LastObservedBitrateBps() const {
649 CriticalSectionScoped cs(data_cs_.get());
650 return last_observed_bitrate_bps_;
651}
652
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000653int ViEEncoder::CodecTargetBitrate(uint32_t* bitrate) const {
mflodman0828a0c2015-03-31 15:29:23 +0200654 if (vcm_.Bitrate(bitrate) != 0)
stefan@webrtc.org439be292012-02-16 14:45:37 +0000655 return -1;
656 return 0;
stefan@webrtc.org07b45a52012-02-02 08:37:48 +0000657}
658
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000659int32_t ViEEncoder::UpdateProtectionMethod(bool nack, bool fec) {
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000660 DCHECK(send_payload_router_ != NULL);
mflodman0828a0c2015-03-31 15:29:23 +0200661 DCHECK(vcm_protection_callback_ != NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000662
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000663 if (fec_enabled_ == fec && nack_enabled_ == nack) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000664 // No change needed, we're already in correct state.
665 return 0;
666 }
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000667 fec_enabled_ = fec;
668 nack_enabled_ = nack;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000669
670 // Set Video Protection for VCM.
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000671 if (fec_enabled_ && nack_enabled_) {
mflodman0828a0c2015-03-31 15:29:23 +0200672 vcm_.SetVideoProtection(webrtc::kProtectionNackFEC, true);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000673 } else {
mflodman0828a0c2015-03-31 15:29:23 +0200674 vcm_.SetVideoProtection(webrtc::kProtectionFEC, fec_enabled_);
675 vcm_.SetVideoProtection(webrtc::kProtectionNackSender, nack_enabled_);
676 vcm_.SetVideoProtection(webrtc::kProtectionNackFEC, false);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000677 }
678
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000679 if (fec_enabled_ || nack_enabled_) {
mflodman0828a0c2015-03-31 15:29:23 +0200680 vcm_.RegisterProtectionCallback(vcm_protection_callback_);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000681 // The send codec must be registered to set correct MTU.
682 webrtc::VideoCodec codec;
mflodman0828a0c2015-03-31 15:29:23 +0200683 if (vcm_.SendCodec(&codec) == 0) {
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000684 uint32_t current_bitrate_bps = 0;
mflodman0828a0c2015-03-31 15:29:23 +0200685 if (vcm_.Bitrate(&current_bitrate_bps) != 0) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000686 LOG_F(LS_WARNING) <<
687 "Failed to get the current encoder target bitrate.";
stefan@webrtc.org439be292012-02-16 14:45:37 +0000688 }
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000689 // Convert to start bitrate in kbps.
690 codec.startBitrate = (current_bitrate_bps + 500) / 1000;
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000691 size_t max_payload_length = send_payload_router_->MaxPayloadLength();
mflodman0828a0c2015-03-31 15:29:23 +0200692 if (vcm_.RegisterSendCodec(&codec, number_of_cores_,
693 max_payload_length) != 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000694 return -1;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000695 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000696 }
mflodman0828a0c2015-03-31 15:29:23 +0200697 return 0;
698 } else {
699 // FEC and NACK are disabled.
700 vcm_.RegisterProtectionCallback(NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000701 }
702 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000703}
704
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000705void ViEEncoder::SetSenderBufferingMode(int target_delay_ms) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000706 {
707 CriticalSectionScoped cs(data_cs_.get());
708 target_delay_ms_ = target_delay_ms;
709 }
mikhal@webrtc.org3d305c62013-02-10 18:42:55 +0000710 if (target_delay_ms > 0) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000711 // Disable external frame-droppers.
mflodman0828a0c2015-03-31 15:29:23 +0200712 vcm_.EnableFrameDropper(false);
713 vpm_.EnableTemporalDecimation(false);
mikhal@webrtc.org3d305c62013-02-10 18:42:55 +0000714 } else {
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000715 // Real-time mode - enable frame droppers.
mflodman0828a0c2015-03-31 15:29:23 +0200716 vpm_.EnableTemporalDecimation(true);
717 vcm_.EnableFrameDropper(true);
mikhal@webrtc.org3d305c62013-02-10 18:42:55 +0000718 }
719}
720
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000721void ViEEncoder::OnSetRates(uint32_t bitrate_bps, int framerate) {
722 CriticalSectionScoped cs(callback_cs_.get());
723 if (send_statistics_proxy_ != nullptr)
724 send_statistics_proxy_->OnSetRates(bitrate_bps, framerate);
725}
726
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000727int32_t ViEEncoder::SendData(
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000728 const uint8_t payload_type,
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000729 const EncodedImage& encoded_image,
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000730 const webrtc::RTPFragmentationHeader& fragmentation_header,
731 const RTPVideoHeader* rtp_video_hdr) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000732 DCHECK(send_payload_router_ != NULL);
733
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000734 {
735 CriticalSectionScoped cs(callback_cs_.get());
736 if (send_statistics_proxy_ != NULL)
737 send_statistics_proxy_->OnSendEncodedImage(encoded_image, rtp_video_hdr);
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000738 }
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000739
740 return send_payload_router_->RoutePayload(
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000741 VCMEncodedFrame::ConvertFrameType(encoded_image._frameType), payload_type,
742 encoded_image._timeStamp, encoded_image.capture_time_ms_,
743 encoded_image._buffer, encoded_image._length, &fragmentation_header,
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000744 rtp_video_hdr) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000745}
746
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000747int32_t ViEEncoder::SendStatistics(const uint32_t bit_rate,
748 const uint32_t frame_rate) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000749 CriticalSectionScoped cs(callback_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000750 if (codec_observer_) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000751 codec_observer_->OutgoingRate(channel_id_, frame_rate, bit_rate);
752 }
753 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000754}
755
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000756int32_t ViEEncoder::RegisterCodecObserver(ViEEncoderObserver* observer) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000757 CriticalSectionScoped cs(callback_cs_.get());
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000758 if (observer && codec_observer_) {
759 LOG_F(LS_ERROR) << "Observer already set.";
760 return -1;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000761 }
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000762 codec_observer_ = observer;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000763 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000764}
765
andrew@webrtc.org96636862012-09-20 23:33:17 +0000766void ViEEncoder::OnReceivedSLI(uint32_t /*ssrc*/,
767 uint8_t picture_id) {
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000768 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000769 picture_id_sli_ = picture_id;
770 has_received_sli_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000771}
772
andrew@webrtc.org96636862012-09-20 23:33:17 +0000773void ViEEncoder::OnReceivedRPSI(uint32_t /*ssrc*/,
774 uint64_t picture_id) {
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000775 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000776 picture_id_rpsi_ = picture_id;
777 has_received_rpsi_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000778}
779
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000780void ViEEncoder::OnReceivedIntraFrameRequest(uint32_t ssrc) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000781 // Key frame request from remote side, signal to VCM.
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000782 TRACE_EVENT0("webrtc", "OnKeyFrameRequest");
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000783
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000784 int idx = 0;
785 {
786 CriticalSectionScoped cs(data_cs_.get());
787 std::map<unsigned int, int>::iterator stream_it = ssrc_streams_.find(ssrc);
788 if (stream_it == ssrc_streams_.end()) {
mflodman@webrtc.orgd73527c2012-12-20 09:26:17 +0000789 LOG_F(LS_WARNING) << "ssrc not found: " << ssrc << ", map size "
790 << ssrc_streams_.size();
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000791 return;
792 }
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000793 std::map<unsigned int, int64_t>::iterator time_it =
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000794 time_last_intra_request_ms_.find(ssrc);
795 if (time_it == time_last_intra_request_ms_.end()) {
796 time_last_intra_request_ms_[ssrc] = 0;
797 }
798
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000799 int64_t now = TickTime::MillisecondTimestamp();
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000800 if (time_last_intra_request_ms_[ssrc] + kViEMinKeyRequestIntervalMs > now) {
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000801 return;
802 }
803 time_last_intra_request_ms_[ssrc] = now;
804 idx = stream_it->second;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000805 }
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000806 // Release the critsect before triggering key frame.
mflodman0828a0c2015-03-31 15:29:23 +0200807 vcm_.IntraFrameRequest(idx);
niklase@google.com470e71d2011-07-07 08:21:25 +0000808}
809
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000810void ViEEncoder::OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc) {
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000811 CriticalSectionScoped cs(data_cs_.get());
812 std::map<unsigned int, int>::iterator it = ssrc_streams_.find(old_ssrc);
813 if (it == ssrc_streams_.end()) {
814 return;
815 }
816
817 ssrc_streams_[new_ssrc] = it->second;
818 ssrc_streams_.erase(it);
819
820 std::map<unsigned int, int64_t>::iterator time_it =
821 time_last_intra_request_ms_.find(old_ssrc);
822 int64_t last_intra_request_ms = 0;
823 if (time_it != time_last_intra_request_ms_.end()) {
824 last_intra_request_ms = time_it->second;
825 time_last_intra_request_ms_.erase(time_it);
826 }
827 time_last_intra_request_ms_[new_ssrc] = last_intra_request_ms;
828}
829
830bool ViEEncoder::SetSsrcs(const std::list<unsigned int>& ssrcs) {
831 VideoCodec codec;
mflodman0828a0c2015-03-31 15:29:23 +0200832 if (vcm_.SendCodec(&codec) != 0)
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000833 return false;
834
835 if (codec.numberOfSimulcastStreams > 0 &&
836 ssrcs.size() != codec.numberOfSimulcastStreams) {
837 return false;
838 }
839
840 CriticalSectionScoped cs(data_cs_.get());
841 ssrc_streams_.clear();
842 time_last_intra_request_ms_.clear();
843 int idx = 0;
844 for (std::list<unsigned int>::const_iterator it = ssrcs.begin();
845 it != ssrcs.end(); ++it, ++idx) {
846 unsigned int ssrc = *it;
847 ssrc_streams_[ssrc] = idx;
848 }
849 return true;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000850}
851
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000852void ViEEncoder::SetMinTransmitBitrate(int min_transmit_bitrate_kbps) {
853 assert(min_transmit_bitrate_kbps >= 0);
854 CriticalSectionScoped crit(data_cs_.get());
855 min_transmit_bitrate_kbps_ = min_transmit_bitrate_kbps;
856}
857
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000858// Called from ViEBitrateObserver.
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000859void ViEEncoder::OnNetworkChanged(uint32_t bitrate_bps,
860 uint8_t fraction_lost,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000861 int64_t round_trip_time_ms) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000862 LOG(LS_VERBOSE) << "OnNetworkChanged, bitrate" << bitrate_bps
863 << " packet loss " << fraction_lost
864 << " rtt " << round_trip_time_ms;
mflodman@webrtc.org50e28162015-02-23 07:45:11 +0000865 DCHECK(send_payload_router_ != NULL);
mflodman0828a0c2015-03-31 15:29:23 +0200866 vcm_.SetChannelParameters(bitrate_bps, fraction_lost, round_trip_time_ms);
867 bool video_is_suspended = vcm_.VideoSuspended();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000868
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000869 VideoCodec send_codec;
mflodman0828a0c2015-03-31 15:29:23 +0200870 if (vcm_.SendCodec(&send_codec) != 0) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000871 return;
872 }
stefan@webrtc.orgb2c8a952013-09-06 13:58:01 +0000873 SimulcastStream* stream_configs = send_codec.simulcastStream;
874 // Allocate the bandwidth between the streams.
875 std::vector<uint32_t> stream_bitrates = AllocateStreamBitrates(
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000876 bitrate_bps, stream_configs, send_codec.numberOfSimulcastStreams);
877 send_payload_router_->SetTargetSendBitrates(stream_bitrates);
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000878
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000879 {
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000880 CriticalSectionScoped cs(data_cs_.get());
pbos@webrtc.org143451d2015-03-18 14:40:03 +0000881 last_observed_bitrate_bps_ = bitrate_bps;
pbos@webrtc.org484ee962013-11-21 18:44:23 +0000882 if (video_suspended_ == video_is_suspended)
883 return;
884 video_suspended_ = video_is_suspended;
885 }
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000886 // Video suspend-state changed, inform codec observer.
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000887 CriticalSectionScoped crit(callback_cs_.get());
pbos@webrtc.org484ee962013-11-21 18:44:23 +0000888 if (codec_observer_) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000889 LOG(LS_INFO) << "Video suspended " << video_is_suspended
890 << " for channel " << channel_id_;
henrik.lundin@webrtc.org9fe36032013-11-21 23:00:40 +0000891 codec_observer_->SuspendChange(channel_id_, video_is_suspended);
henrik.lundin@webrtc.org7ea4f242013-10-02 13:34:26 +0000892 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000893}
894
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000895int32_t ViEEncoder::RegisterEffectFilter(ViEEffectFilter* effect_filter) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000896 CriticalSectionScoped cs(callback_cs_.get());
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000897 if (effect_filter != NULL && effect_filter_ != NULL) {
898 LOG_F(LS_ERROR) << "Filter already set.";
899 return -1;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000900 }
901 effect_filter_ = effect_filter;
902 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000903}
904
mikhal@webrtc.orge41bbdf2012-08-28 16:15:16 +0000905int ViEEncoder::StartDebugRecording(const char* fileNameUTF8) {
mflodman0828a0c2015-03-31 15:29:23 +0200906 return vcm_.StartDebugRecording(fileNameUTF8);
mikhal@webrtc.orge41bbdf2012-08-28 16:15:16 +0000907}
908
909int ViEEncoder::StopDebugRecording() {
mflodman0828a0c2015-03-31 15:29:23 +0200910 return vcm_.StopDebugRecording();
mikhal@webrtc.orge41bbdf2012-08-28 16:15:16 +0000911}
912
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000913void ViEEncoder::SuspendBelowMinBitrate() {
mflodman0828a0c2015-03-31 15:29:23 +0200914 vcm_.SuspendBelowMinBitrate();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000915 bitrate_allocator_->EnforceMinBitrate(false);
henrik.lundin@webrtc.org7ea4f242013-10-02 13:34:26 +0000916}
917
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000918void ViEEncoder::RegisterPreEncodeCallback(
919 I420FrameCallback* pre_encode_callback) {
920 CriticalSectionScoped cs(callback_cs_.get());
921 pre_encode_callback_ = pre_encode_callback;
922}
923
924void ViEEncoder::DeRegisterPreEncodeCallback() {
925 CriticalSectionScoped cs(callback_cs_.get());
926 pre_encode_callback_ = NULL;
927}
928
sprang@webrtc.org40709352013-11-26 11:41:59 +0000929void ViEEncoder::RegisterPostEncodeImageCallback(
930 EncodedImageCallback* post_encode_callback) {
mflodman0828a0c2015-03-31 15:29:23 +0200931 vcm_.RegisterPostEncodeImageCallback(post_encode_callback);
sprang@webrtc.org40709352013-11-26 11:41:59 +0000932}
933
934void ViEEncoder::DeRegisterPostEncodeImageCallback() {
mflodman0828a0c2015-03-31 15:29:23 +0200935 vcm_.RegisterPostEncodeImageCallback(NULL);
sprang@webrtc.org40709352013-11-26 11:41:59 +0000936}
937
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000938void ViEEncoder::RegisterSendStatisticsProxy(
939 SendStatisticsProxy* send_statistics_proxy) {
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000940 CriticalSectionScoped cs(callback_cs_.get());
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000941 send_statistics_proxy_ = send_statistics_proxy;
942}
943
marpan@webrtc.orgefd01fd2012-04-18 15:56:34 +0000944QMVideoSettingsCallback::QMVideoSettingsCallback(VideoProcessingModule* vpm)
945 : vpm_(vpm) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000946}
niklase@google.com470e71d2011-07-07 08:21:25 +0000947
stefan@webrtc.org439be292012-02-16 14:45:37 +0000948QMVideoSettingsCallback::~QMVideoSettingsCallback() {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000949}
950
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000951int32_t QMVideoSettingsCallback::SetVideoQMSettings(
952 const uint32_t frame_rate,
953 const uint32_t width,
954 const uint32_t height) {
marpan@webrtc.orgcf706c22012-03-27 21:04:13 +0000955 return vpm_->SetTargetResolution(width, height, frame_rate);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000956}
957
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000958} // namespace webrtc