blob: d54e22bcc9102b18bdb57e93bfe2f23de8e533f3 [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),
mflodmanfcf54bd2015-04-14 21:28:08 +0200114 vpm_(VideoProcessingModule::Create(ViEModuleId(-1, channel_id))),
115 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 callback_cs_(CriticalSectionWrapper::CreateCriticalSection()),
121 data_cs_(CriticalSectionWrapper::CreateCriticalSection()),
Stefan Holmere5904162015-03-26 11:11:06 +0100122 pacer_(pacer),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000123 bitrate_allocator_(bitrate_allocator),
124 bitrate_controller_(bitrate_controller),
125 time_of_last_incoming_frame_ms_(0),
126 send_padding_(false),
127 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),
135 codec_observer_(NULL),
136 effect_filter_(NULL),
137 module_process_thread_(module_process_thread),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000138 has_received_sli_(false),
139 picture_id_sli_(0),
140 has_received_rpsi_(false),
141 picture_id_rpsi_(0),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000142 video_suspended_(false),
143 pre_encode_callback_(NULL),
144 start_ms_(Clock::GetRealTimeClock()->TimeInMilliseconds()),
145 send_statistics_proxy_(NULL) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000146 bitrate_observer_.reset(new ViEBitrateObserver(this));
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000147}
148
149bool ViEEncoder::Init() {
mflodmanfcf54bd2015-04-14 21:28:08 +0200150 if (vcm_->InitializeSender() != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000151 return false;
152 }
mflodmanfcf54bd2015-04-14 21:28:08 +0200153 vpm_->EnableTemporalDecimation(true);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000154
155 // Enable/disable content analysis: off by default for now.
mflodmanfcf54bd2015-04-14 21:28:08 +0200156 vpm_->EnableContentAnalysis(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000157
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000158 if (!disable_default_encoder_) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000159#ifdef VIDEOCODEC_VP8
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000160 VideoCodecType codec_type = webrtc::kVideoCodecVP8;
andresp@webrtc.orga84b0a62014-08-14 16:46:46 +0000161#else
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000162 VideoCodecType codec_type = webrtc::kVideoCodecI420;
andresp@webrtc.orga84b0a62014-08-14 16:46:46 +0000163#endif
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000164 VideoCodec video_codec;
mflodmanfcf54bd2015-04-14 21:28:08 +0200165 if (vcm_->Codec(codec_type, &video_codec) != VCM_OK) {
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000166 return false;
167 }
168 {
169 CriticalSectionScoped cs(data_cs_.get());
170 send_padding_ = video_codec.numberOfSimulcastStreams > 1;
171 }
mflodmanfcf54bd2015-04-14 21:28:08 +0200172 if (vcm_->RegisterSendCodec(&video_codec, number_of_cores_,
173 PayloadRouter::DefaultMaxPayloadLength()) !=
174 0) {
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000175 return false;
176 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000177 }
mflodmanfcf54bd2015-04-14 21:28:08 +0200178 if (vcm_->RegisterTransportCallback(this) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000179 return false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000180 }
mflodmanfcf54bd2015-04-14 21:28:08 +0200181 if (vcm_->RegisterSendStatisticsCallback(this) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000182 return false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000183 }
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000184 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000185}
186
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000187void ViEEncoder::StartThreadsAndSetSharedMembers(
188 scoped_refptr<PayloadRouter> send_payload_router,
189 VCMProtectionCallback* vcm_protection_callback) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000190 DCHECK(send_payload_router_ == NULL);
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000191
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000192 send_payload_router_ = send_payload_router;
mflodmanfcf54bd2015-04-14 21:28:08 +0200193 vcm_->RegisterProtectionCallback(vcm_protection_callback);
194 module_process_thread_.RegisterModule(vcm_.get());
mflodman@webrtc.org290cb562015-02-17 10:15:06 +0000195}
196
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000197void ViEEncoder::StopThreadsAndRemoveSharedMembers() {
mflodmanfcf54bd2015-04-14 21:28:08 +0200198 if (bitrate_allocator_)
199 bitrate_allocator_->RemoveBitrateObserver(bitrate_observer_.get());
200 module_process_thread_.DeRegisterModule(vcm_.get());
201 module_process_thread_.DeRegisterModule(vpm_.get());
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000202}
203
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000204ViEEncoder::~ViEEncoder() {
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000205 UpdateHistograms();
niklase@google.com470e71d2011-07-07 08:21:25 +0000206}
207
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000208void ViEEncoder::UpdateHistograms() {
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000209 int64_t elapsed_sec =
210 (Clock::GetRealTimeClock()->TimeInMilliseconds() - start_ms_) / 1000;
211 if (elapsed_sec < metrics::kMinRunTimeInSeconds) {
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000212 return;
213 }
214 webrtc::VCMFrameCount frames;
mflodmanfcf54bd2015-04-14 21:28:08 +0200215 if (vcm_->SentFrameCount(frames) != VCM_OK) {
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000216 return;
217 }
218 uint32_t total_frames = frames.numKeyFrames + frames.numDeltaFrames;
219 if (total_frames > 0) {
220 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.KeyFramesSentInPermille",
221 static_cast<int>(
222 (frames.numKeyFrames * 1000.0f / total_frames) + 0.5f));
223 }
224}
225
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000226int ViEEncoder::Owner() const {
227 return channel_id_;
228}
229
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000230void ViEEncoder::SetNetworkTransmissionState(bool is_transmitting) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000231 {
232 CriticalSectionScoped cs(data_cs_.get());
233 network_is_transmitting_ = is_transmitting;
234 }
235 if (is_transmitting) {
Stefan Holmere5904162015-03-26 11:11:06 +0100236 pacer_->Resume();
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000237 } else {
Stefan Holmere5904162015-03-26 11:11:06 +0100238 pacer_->Pause();
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000239 }
240}
241
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000242void ViEEncoder::Pause() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000243 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000244 encoder_paused_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000245}
246
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000247void ViEEncoder::Restart() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000248 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000249 encoder_paused_ = false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000250}
251
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000252uint8_t ViEEncoder::NumberOfCodecs() {
mflodmanfcf54bd2015-04-14 21:28:08 +0200253 return vcm_->NumberOfCodecs();
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000254}
255
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000256int32_t ViEEncoder::GetCodec(uint8_t list_index, VideoCodec* video_codec) {
mflodmanfcf54bd2015-04-14 21:28:08 +0200257 if (vcm_->Codec(list_index, video_codec) != 0) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000258 return -1;
259 }
260 return 0;
261}
262
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000263int32_t ViEEncoder::RegisterExternalEncoder(webrtc::VideoEncoder* encoder,
264 uint8_t pl_type,
265 bool internal_source) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000266 if (encoder == NULL)
267 return -1;
268
mflodmanfcf54bd2015-04-14 21:28:08 +0200269 if (vcm_->RegisterExternalEncoder(encoder, pl_type, internal_source) !=
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000270 VCM_OK) {
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::DeRegisterExternalEncoder(uint8_t pl_type) {
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000277 DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000278 webrtc::VideoCodec current_send_codec;
mflodmanfcf54bd2015-04-14 21:28:08 +0200279 if (vcm_->SendCodec(&current_send_codec) == VCM_OK) {
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000280 uint32_t current_bitrate_bps = 0;
mflodmanfcf54bd2015-04-14 21:28:08 +0200281 if (vcm_->Bitrate(&current_bitrate_bps) != 0) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000282 LOG(LS_WARNING) << "Failed to get the current encoder target bitrate.";
stefan@webrtc.org439be292012-02-16 14:45:37 +0000283 }
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000284 current_send_codec.startBitrate = (current_bitrate_bps + 500) / 1000;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000285 }
286
mflodmanfcf54bd2015-04-14 21:28:08 +0200287 if (vcm_->RegisterExternalEncoder(NULL, pl_type) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000288 return -1;
289 }
290
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000291 if (disable_default_encoder_)
292 return 0;
293
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000294 // If the external encoder is the current send codec, use vcm internal
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000295 // encoder.
296 if (current_send_codec.plType == pl_type) {
stefan@webrtc.orgae2563a2014-02-13 13:48:38 +0000297 {
298 CriticalSectionScoped cs(data_cs_.get());
299 send_padding_ = current_send_codec.numberOfSimulcastStreams > 1;
300 }
fischman@webrtc.org64e04052014-03-07 18:00:05 +0000301 // TODO(mflodman): Unfortunately the VideoCodec that VCM has cached a
302 // raw pointer to an |extra_options| that's long gone. Clearing it here is
303 // a hack to prevent the following code from crashing. This should be fixed
304 // for realz. https://code.google.com/p/chromium/issues/detail?id=348222
305 current_send_codec.extra_options = NULL;
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000306 size_t max_data_payload_length = send_payload_router_->MaxPayloadLength();
mflodmanfcf54bd2015-04-14 21:28:08 +0200307 if (vcm_->RegisterSendCodec(&current_send_codec, number_of_cores_,
308 max_data_payload_length) != VCM_OK) {
stefan@webrtc.org4070b1d2014-07-16 11:20:40 +0000309 LOG(LS_INFO) << "De-registered the currently used external encoder ("
310 << static_cast<int>(pl_type) << ") and therefore tried to "
311 << "register the corresponding internal encoder, but none "
312 << "was supported.";
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000313 }
314 }
315 return 0;
316}
317
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000318int32_t ViEEncoder::SetEncoder(const webrtc::VideoCodec& video_codec) {
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000319 DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000320 // Setting target width and height for VPM.
mflodmanfcf54bd2015-04-14 21:28:08 +0200321 if (vpm_->SetTargetResolution(video_codec.width, video_codec.height,
322 video_codec.maxFramerate) != VPM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000323 return -1;
324 }
325
stefan@webrtc.org9075d512014-02-14 09:45:58 +0000326 {
327 CriticalSectionScoped cs(data_cs_.get());
328 send_padding_ = video_codec.numberOfSimulcastStreams > 1;
329 }
Stefan Holmere5904162015-03-26 11:11:06 +0100330
331 // Add a bitrate observer to the allocator and update the start, max and
332 // min bitrates of the bitrate controller as needed.
333 int allocated_bitrate_bps;
334 int new_bwe_candidate_bps = bitrate_allocator_->AddBitrateObserver(
335 bitrate_observer_.get(), video_codec.startBitrate * 1000,
336 video_codec.minBitrate * 1000, video_codec.maxBitrate * 1000,
337 &allocated_bitrate_bps);
338
339 // Only set the start/min/max bitrate of the bitrate controller if the start
340 // bitrate is greater than zero. The new API sets these via the channel group
341 // and passes a zero start bitrate to SetSendCodec.
342 // TODO(holmer): Remove this when the new API has been launched.
343 if (video_codec.startBitrate > 0) {
344 if (new_bwe_candidate_bps > 0) {
345 uint32_t current_bwe_bps = 0;
346 bitrate_controller_->AvailableBandwidth(&current_bwe_bps);
347 bitrate_controller_->SetStartBitrate(std::max(
348 static_cast<uint32_t>(new_bwe_candidate_bps), current_bwe_bps));
349 }
350
351 int new_bwe_min_bps = 0;
352 int new_bwe_max_bps = 0;
353 bitrate_allocator_->GetMinMaxBitrateSumBps(&new_bwe_min_bps,
354 &new_bwe_max_bps);
355 bitrate_controller_->SetMinMaxBitrate(new_bwe_min_bps, new_bwe_max_bps);
356 }
357
358 webrtc::VideoCodec modified_video_codec = video_codec;
359 modified_video_codec.startBitrate = allocated_bitrate_bps / 1000;
360
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000361 size_t max_data_payload_length = send_payload_router_->MaxPayloadLength();
mflodmanfcf54bd2015-04-14 21:28:08 +0200362 if (vcm_->RegisterSendCodec(&modified_video_codec, number_of_cores_,
363 max_data_payload_length) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000364 return -1;
365 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000366 return 0;
367}
368
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000369int32_t ViEEncoder::GetEncoder(VideoCodec* video_codec) {
mflodmanfcf54bd2015-04-14 21:28:08 +0200370 *video_codec = vcm_->GetSendCodec();
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000371 return 0;
372}
niklase@google.com470e71d2011-07-07 08:21:25 +0000373
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000374int32_t ViEEncoder::GetCodecConfigParameters(
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000375 unsigned char config_parameters[kConfigParameterSize],
376 unsigned char& config_parameters_size) {
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000377 int32_t num_parameters =
mflodmanfcf54bd2015-04-14 21:28:08 +0200378 vcm_->CodecConfigParameters(config_parameters, kConfigParameterSize);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000379 if (num_parameters <= 0) {
380 config_parameters_size = 0;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000381 return -1;
382 }
383 config_parameters_size = static_cast<unsigned char>(num_parameters);
384 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000385}
386
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000387int32_t ViEEncoder::ScaleInputImage(bool enable) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000388 VideoFrameResampling resampling_mode = kFastRescaling;
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000389 // TODO(mflodman) What?
390 if (enable) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000391 // kInterpolation is currently not supported.
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000392 LOG_F(LS_ERROR) << "Not supported.";
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000393 return -1;
394 }
mflodmanfcf54bd2015-04-14 21:28:08 +0200395 vpm_->SetInputFrameResampleMode(resampling_mode);
niklase@google.com470e71d2011-07-07 08:21:25 +0000396
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000397 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000398}
399
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000400int ViEEncoder::GetPaddingNeededBps(int bitrate_bps) const {
401 int64_t time_of_last_incoming_frame_ms;
402 int min_transmit_bitrate_bps;
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +0000403 {
404 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000405 bool send_padding =
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000406 send_padding_ || video_suspended_ || min_transmit_bitrate_kbps_ > 0;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000407 if (!send_padding)
408 return 0;
409 time_of_last_incoming_frame_ms = time_of_last_incoming_frame_ms_;
410 min_transmit_bitrate_bps = 1000 * min_transmit_bitrate_kbps_;
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +0000411 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000412
413 VideoCodec send_codec;
mflodmanfcf54bd2015-04-14 21:28:08 +0200414 if (vcm_->SendCodec(&send_codec) != 0)
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000415 return 0;
416 SimulcastStream* stream_configs = send_codec.simulcastStream;
417 // Allocate the bandwidth between the streams.
418 std::vector<uint32_t> stream_bitrates = AllocateStreamBitrates(
419 bitrate_bps, stream_configs, send_codec.numberOfSimulcastStreams);
420
mflodmanfcf54bd2015-04-14 21:28:08 +0200421 bool video_is_suspended = vcm_->VideoSuspended();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000422
423 // Find the max amount of padding we can allow ourselves to send at this
424 // point, based on which streams are currently active and what our current
425 // available bandwidth is.
426 int pad_up_to_bitrate_bps = 0;
427 if (send_codec.numberOfSimulcastStreams == 0) {
428 pad_up_to_bitrate_bps = send_codec.minBitrate * 1000;
429 } else {
430 pad_up_to_bitrate_bps =
431 stream_configs[send_codec.numberOfSimulcastStreams - 1].minBitrate *
432 1000;
433 for (int i = 0; i < send_codec.numberOfSimulcastStreams - 1; ++i) {
434 pad_up_to_bitrate_bps += stream_configs[i].targetBitrate * 1000;
435 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000436 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000437
438 // Disable padding if only sending one stream and video isn't suspended and
439 // min-transmit bitrate isn't used (applied later).
440 if (!video_is_suspended && send_codec.numberOfSimulcastStreams <= 1)
441 pad_up_to_bitrate_bps = 0;
442
443 // The amount of padding should decay to zero if no frames are being
444 // captured unless a min-transmit bitrate is used.
445 int64_t now_ms = TickTime::MillisecondTimestamp();
446 if (now_ms - time_of_last_incoming_frame_ms > kStopPaddingThresholdMs)
447 pad_up_to_bitrate_bps = 0;
448
449 // Pad up to min bitrate.
450 if (pad_up_to_bitrate_bps < min_transmit_bitrate_bps)
451 pad_up_to_bitrate_bps = min_transmit_bitrate_bps;
452
453 // Padding may never exceed bitrate estimate.
454 if (pad_up_to_bitrate_bps > bitrate_bps)
455 pad_up_to_bitrate_bps = bitrate_bps;
456
457 return pad_up_to_bitrate_bps;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000458}
459
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000460bool ViEEncoder::EncoderPaused() const {
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000461 // Pause video if paused by caller or as long as the network is down or the
462 // pacer queue has grown too large in buffered mode.
463 if (encoder_paused_) {
464 return true;
465 }
466 if (target_delay_ms_ > 0) {
467 // Buffered mode.
468 // TODO(pwestin): Workaround until nack is configured as a time and not
469 // number of packets.
Stefan Holmere5904162015-03-26 11:11:06 +0100470 return pacer_->QueueInMs() >=
471 std::max(
472 static_cast<int>(target_delay_ms_ * kEncoderPausePacerMargin),
473 kMinPacingDelayMs);
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000474 }
Stefan Holmere5904162015-03-26 11:11:06 +0100475 if (pacer_->ExpectedQueueTimeMs() > PacedSender::kDefaultMaxQueueLengthMs) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000476 // Too much data in pacer queue, drop frame.
477 return true;
478 }
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000479 return !network_is_transmitting_;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000480}
481
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000482void ViEEncoder::TraceFrameDropStart() {
483 // Start trace event only on the first frame after encoder is paused.
484 if (!encoder_paused_and_dropped_frame_) {
485 TRACE_EVENT_ASYNC_BEGIN0("webrtc", "EncoderPaused", this);
486 }
487 encoder_paused_and_dropped_frame_ = true;
488 return;
489}
490
491void ViEEncoder::TraceFrameDropEnd() {
492 // End trace event on first frame after encoder resumes, if frame was dropped.
493 if (encoder_paused_and_dropped_frame_) {
494 TRACE_EVENT_ASYNC_END0("webrtc", "EncoderPaused", this);
495 }
496 encoder_paused_and_dropped_frame_ = false;
497}
498
mflodman@webrtc.org8baed512012-06-21 12:11:50 +0000499void ViEEncoder::DeliverFrame(int id,
Magnus Jedvert26679d62015-04-07 14:07:41 +0200500 const I420VideoFrame& video_frame,
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000501 const std::vector<uint32_t>& csrcs) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000502 DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.orga98e7962015-02-11 15:45:56 +0000503 DCHECK(csrcs.empty());
mflodman@webrtc.org47d657b2015-02-19 10:29:32 +0000504 if (!send_payload_router_->active()) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000505 // We've paused or we have no channels attached, don't waste resources on
506 // encoding.
wuchengli@chromium.orgac4b87c2014-03-19 03:44:20 +0000507 return;
508 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000509 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000510 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000511 time_of_last_incoming_frame_ms_ = TickTime::MillisecondTimestamp();
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000512 if (EncoderPaused()) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000513 TraceFrameDropStart();
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000514 return;
515 }
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000516 TraceFrameDropEnd();
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000517 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000518
Magnus Jedvert26679d62015-04-07 14:07:41 +0200519 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(),
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000520 "Encode");
pwestin@webrtc.org2f476ed2012-10-30 16:21:52 +0000521 I420VideoFrame* decimated_frame = NULL;
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000522 // TODO(wuchengli): support texture frames.
Magnus Jedvert26679d62015-04-07 14:07:41 +0200523 if (video_frame.native_handle() == NULL) {
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000524 {
525 CriticalSectionScoped cs(callback_cs_.get());
526 if (effect_filter_) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000527 size_t length =
Magnus Jedvert26679d62015-04-07 14:07:41 +0200528 CalcBufferSize(kI420, video_frame.width(), video_frame.height());
mflodman@webrtc.org948d6172015-02-10 08:58:16 +0000529 rtc::scoped_ptr<uint8_t[]> video_buffer(new uint8_t[length]);
Magnus Jedvert26679d62015-04-07 14:07:41 +0200530 ExtractBuffer(video_frame, length, video_buffer.get());
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000531 effect_filter_->Transform(length,
532 video_buffer.get(),
Magnus Jedvert26679d62015-04-07 14:07:41 +0200533 video_frame.ntp_time_ms(),
534 video_frame.timestamp(),
535 video_frame.width(),
536 video_frame.height());
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000537 }
538 }
539
540 // Pass frame via preprocessor.
mflodmanfcf54bd2015-04-14 21:28:08 +0200541 const int ret = vpm_->PreprocessFrame(video_frame, &decimated_frame);
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000542 if (ret == 1) {
543 // Drop this frame.
544 return;
545 }
546 if (ret != VPM_OK) {
547 return;
548 }
pwestin@webrtc.org2f476ed2012-10-30 16:21:52 +0000549 }
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000550
Magnus Jedvert26679d62015-04-07 14:07:41 +0200551 // If we haven't resampled the frame and we have a FrameCallback, we need to
552 // make a deep copy of |video_frame|.
553 I420VideoFrame copied_frame;
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000554 {
555 CriticalSectionScoped cs(callback_cs_.get());
Magnus Jedvert26679d62015-04-07 14:07:41 +0200556 if (pre_encode_callback_) {
557 // If the frame was not resampled or scaled => use copy of original.
558 if (decimated_frame == NULL) {
559 copied_frame.CopyFrame(video_frame);
560 decimated_frame = &copied_frame;
561 }
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000562 pre_encode_callback_->FrameCallback(decimated_frame);
Magnus Jedvert26679d62015-04-07 14:07:41 +0200563 }
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000564 }
565
Magnus Jedvert26679d62015-04-07 14:07:41 +0200566 // If the frame was not resampled, scaled, or touched by FrameCallback => use
567 // original. The frame is const from here.
568 const I420VideoFrame* output_frame =
569 (decimated_frame != NULL) ? decimated_frame : &video_frame;
570
571 if (video_frame.native_handle() != NULL) {
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000572 // TODO(wuchengli): add texture support. http://crbug.com/362437
573 return;
574 }
575
niklase@google.com470e71d2011-07-07 08:21:25 +0000576#ifdef VIDEOCODEC_VP8
mflodmanfcf54bd2015-04-14 21:28:08 +0200577 if (vcm_->SendCodec() == webrtc::kVideoCodecVP8) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000578 webrtc::CodecSpecificInfo codec_specific_info;
579 codec_specific_info.codecType = webrtc::kVideoCodecVP8;
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000580 {
581 CriticalSectionScoped cs(data_cs_.get());
582 codec_specific_info.codecSpecific.VP8.hasReceivedRPSI =
583 has_received_rpsi_;
584 codec_specific_info.codecSpecific.VP8.hasReceivedSLI =
585 has_received_sli_;
586 codec_specific_info.codecSpecific.VP8.pictureIdRPSI =
587 picture_id_rpsi_;
588 codec_specific_info.codecSpecific.VP8.pictureIdSLI =
589 picture_id_sli_;
590 has_received_sli_ = false;
591 has_received_rpsi_ = false;
592 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000593
mflodmanfcf54bd2015-04-14 21:28:08 +0200594 vcm_->AddVideoFrame(*output_frame, vpm_->ContentMetrics(),
595 &codec_specific_info);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000596 return;
597 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000598#endif
mflodmanfcf54bd2015-04-14 21:28:08 +0200599 vcm_->AddVideoFrame(*output_frame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000600}
niklase@google.com470e71d2011-07-07 08:21:25 +0000601
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000602void ViEEncoder::DelayChanged(int id, int frame_delay) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000603}
niklase@google.com470e71d2011-07-07 08:21:25 +0000604
mflodman@webrtc.org8baed512012-06-21 12:11:50 +0000605int ViEEncoder::GetPreferedFrameSettings(int* width,
606 int* height,
607 int* frame_rate) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000608 webrtc::VideoCodec video_codec;
609 memset(&video_codec, 0, sizeof(video_codec));
mflodmanfcf54bd2015-04-14 21:28:08 +0200610 if (vcm_->SendCodec(&video_codec) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000611 return -1;
612 }
613
mflodman@webrtc.org8baed512012-06-21 12:11:50 +0000614 *width = video_codec.width;
615 *height = video_codec.height;
616 *frame_rate = video_codec.maxFramerate;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000617 return 0;
618}
619
pwestin@webrtc.orgce330352012-04-12 06:59:14 +0000620int ViEEncoder::SendKeyFrame() {
mflodmanfcf54bd2015-04-14 21:28:08 +0200621 return vcm_->IntraFrameRequest(0);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000622}
623
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000624int32_t ViEEncoder::SendCodecStatistics(
625 uint32_t* num_key_frames, uint32_t* num_delta_frames) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000626 webrtc::VCMFrameCount sent_frames;
mflodmanfcf54bd2015-04-14 21:28:08 +0200627 if (vcm_->SentFrameCount(sent_frames) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000628 return -1;
629 }
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +0000630 *num_key_frames = sent_frames.numKeyFrames;
631 *num_delta_frames = sent_frames.numDeltaFrames;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000632 return 0;
633}
634
pbos@webrtc.org143451d2015-03-18 14:40:03 +0000635uint32_t ViEEncoder::LastObservedBitrateBps() const {
636 CriticalSectionScoped cs(data_cs_.get());
637 return last_observed_bitrate_bps_;
638}
639
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000640int ViEEncoder::CodecTargetBitrate(uint32_t* bitrate) const {
mflodmanfcf54bd2015-04-14 21:28:08 +0200641 if (vcm_->Bitrate(bitrate) != 0)
stefan@webrtc.org439be292012-02-16 14:45:37 +0000642 return -1;
643 return 0;
stefan@webrtc.org07b45a52012-02-02 08:37:48 +0000644}
645
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000646int32_t ViEEncoder::UpdateProtectionMethod(bool nack, bool fec) {
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000647 DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000648
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000649 if (fec_enabled_ == fec && nack_enabled_ == nack) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000650 // No change needed, we're already in correct state.
651 return 0;
652 }
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000653 fec_enabled_ = fec;
654 nack_enabled_ = nack;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000655
656 // Set Video Protection for VCM.
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000657 if (fec_enabled_ && nack_enabled_) {
mflodmanfcf54bd2015-04-14 21:28:08 +0200658 vcm_->SetVideoProtection(webrtc::kProtectionNackFEC, true);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000659 } else {
mflodmanfcf54bd2015-04-14 21:28:08 +0200660 vcm_->SetVideoProtection(webrtc::kProtectionFEC, fec_enabled_);
661 vcm_->SetVideoProtection(webrtc::kProtectionNackSender, nack_enabled_);
662 vcm_->SetVideoProtection(webrtc::kProtectionNackFEC, false);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000663 }
664
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000665 if (fec_enabled_ || nack_enabled_) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000666 // The send codec must be registered to set correct MTU.
667 webrtc::VideoCodec codec;
mflodmanfcf54bd2015-04-14 21:28:08 +0200668 if (vcm_->SendCodec(&codec) == 0) {
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000669 uint32_t current_bitrate_bps = 0;
mflodmanfcf54bd2015-04-14 21:28:08 +0200670 if (vcm_->Bitrate(&current_bitrate_bps) != 0) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000671 LOG_F(LS_WARNING) <<
672 "Failed to get the current encoder target bitrate.";
stefan@webrtc.org439be292012-02-16 14:45:37 +0000673 }
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000674 // Convert to start bitrate in kbps.
675 codec.startBitrate = (current_bitrate_bps + 500) / 1000;
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000676 size_t max_payload_length = send_payload_router_->MaxPayloadLength();
mflodmanfcf54bd2015-04-14 21:28:08 +0200677 if (vcm_->RegisterSendCodec(&codec, number_of_cores_,
678 max_payload_length) != 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000679 return -1;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000680 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000681 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000682 }
683 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000684}
685
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000686void ViEEncoder::SetSenderBufferingMode(int target_delay_ms) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000687 {
688 CriticalSectionScoped cs(data_cs_.get());
689 target_delay_ms_ = target_delay_ms;
690 }
mikhal@webrtc.org3d305c62013-02-10 18:42:55 +0000691 if (target_delay_ms > 0) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000692 // Disable external frame-droppers.
mflodmanfcf54bd2015-04-14 21:28:08 +0200693 vcm_->EnableFrameDropper(false);
694 vpm_->EnableTemporalDecimation(false);
mikhal@webrtc.org3d305c62013-02-10 18:42:55 +0000695 } else {
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000696 // Real-time mode - enable frame droppers.
mflodmanfcf54bd2015-04-14 21:28:08 +0200697 vpm_->EnableTemporalDecimation(true);
698 vcm_->EnableFrameDropper(true);
mikhal@webrtc.org3d305c62013-02-10 18:42:55 +0000699 }
700}
701
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000702void ViEEncoder::OnSetRates(uint32_t bitrate_bps, int framerate) {
703 CriticalSectionScoped cs(callback_cs_.get());
704 if (send_statistics_proxy_ != nullptr)
705 send_statistics_proxy_->OnSetRates(bitrate_bps, framerate);
706}
707
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000708int32_t ViEEncoder::SendData(
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000709 const uint8_t payload_type,
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000710 const EncodedImage& encoded_image,
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000711 const webrtc::RTPFragmentationHeader& fragmentation_header,
712 const RTPVideoHeader* rtp_video_hdr) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000713 DCHECK(send_payload_router_ != NULL);
714
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000715 {
716 CriticalSectionScoped cs(callback_cs_.get());
717 if (send_statistics_proxy_ != NULL)
718 send_statistics_proxy_->OnSendEncodedImage(encoded_image, rtp_video_hdr);
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000719 }
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000720
721 return send_payload_router_->RoutePayload(
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000722 VCMEncodedFrame::ConvertFrameType(encoded_image._frameType), payload_type,
723 encoded_image._timeStamp, encoded_image.capture_time_ms_,
724 encoded_image._buffer, encoded_image._length, &fragmentation_header,
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000725 rtp_video_hdr) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000726}
727
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000728int32_t ViEEncoder::SendStatistics(const uint32_t bit_rate,
729 const uint32_t frame_rate) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000730 CriticalSectionScoped cs(callback_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000731 if (codec_observer_) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000732 codec_observer_->OutgoingRate(channel_id_, frame_rate, bit_rate);
733 }
734 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000735}
736
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000737int32_t ViEEncoder::RegisterCodecObserver(ViEEncoderObserver* observer) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000738 CriticalSectionScoped cs(callback_cs_.get());
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000739 if (observer && codec_observer_) {
740 LOG_F(LS_ERROR) << "Observer already set.";
741 return -1;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000742 }
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000743 codec_observer_ = observer;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000744 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000745}
746
andrew@webrtc.org96636862012-09-20 23:33:17 +0000747void ViEEncoder::OnReceivedSLI(uint32_t /*ssrc*/,
748 uint8_t picture_id) {
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000749 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000750 picture_id_sli_ = picture_id;
751 has_received_sli_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000752}
753
andrew@webrtc.org96636862012-09-20 23:33:17 +0000754void ViEEncoder::OnReceivedRPSI(uint32_t /*ssrc*/,
755 uint64_t picture_id) {
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000756 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000757 picture_id_rpsi_ = picture_id;
758 has_received_rpsi_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000759}
760
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000761void ViEEncoder::OnReceivedIntraFrameRequest(uint32_t ssrc) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000762 // Key frame request from remote side, signal to VCM.
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000763 TRACE_EVENT0("webrtc", "OnKeyFrameRequest");
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000764
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000765 int idx = 0;
766 {
767 CriticalSectionScoped cs(data_cs_.get());
768 std::map<unsigned int, int>::iterator stream_it = ssrc_streams_.find(ssrc);
769 if (stream_it == ssrc_streams_.end()) {
mflodman@webrtc.orgd73527c2012-12-20 09:26:17 +0000770 LOG_F(LS_WARNING) << "ssrc not found: " << ssrc << ", map size "
771 << ssrc_streams_.size();
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000772 return;
773 }
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000774 std::map<unsigned int, int64_t>::iterator time_it =
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000775 time_last_intra_request_ms_.find(ssrc);
776 if (time_it == time_last_intra_request_ms_.end()) {
777 time_last_intra_request_ms_[ssrc] = 0;
778 }
779
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000780 int64_t now = TickTime::MillisecondTimestamp();
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000781 if (time_last_intra_request_ms_[ssrc] + kViEMinKeyRequestIntervalMs > now) {
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000782 return;
783 }
784 time_last_intra_request_ms_[ssrc] = now;
785 idx = stream_it->second;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000786 }
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000787 // Release the critsect before triggering key frame.
mflodmanfcf54bd2015-04-14 21:28:08 +0200788 vcm_->IntraFrameRequest(idx);
niklase@google.com470e71d2011-07-07 08:21:25 +0000789}
790
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000791void ViEEncoder::OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc) {
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000792 CriticalSectionScoped cs(data_cs_.get());
793 std::map<unsigned int, int>::iterator it = ssrc_streams_.find(old_ssrc);
794 if (it == ssrc_streams_.end()) {
795 return;
796 }
797
798 ssrc_streams_[new_ssrc] = it->second;
799 ssrc_streams_.erase(it);
800
801 std::map<unsigned int, int64_t>::iterator time_it =
802 time_last_intra_request_ms_.find(old_ssrc);
803 int64_t last_intra_request_ms = 0;
804 if (time_it != time_last_intra_request_ms_.end()) {
805 last_intra_request_ms = time_it->second;
806 time_last_intra_request_ms_.erase(time_it);
807 }
808 time_last_intra_request_ms_[new_ssrc] = last_intra_request_ms;
809}
810
811bool ViEEncoder::SetSsrcs(const std::list<unsigned int>& ssrcs) {
812 VideoCodec codec;
mflodmanfcf54bd2015-04-14 21:28:08 +0200813 if (vcm_->SendCodec(&codec) != 0)
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000814 return false;
815
816 if (codec.numberOfSimulcastStreams > 0 &&
817 ssrcs.size() != codec.numberOfSimulcastStreams) {
818 return false;
819 }
820
821 CriticalSectionScoped cs(data_cs_.get());
822 ssrc_streams_.clear();
823 time_last_intra_request_ms_.clear();
824 int idx = 0;
825 for (std::list<unsigned int>::const_iterator it = ssrcs.begin();
826 it != ssrcs.end(); ++it, ++idx) {
827 unsigned int ssrc = *it;
828 ssrc_streams_[ssrc] = idx;
829 }
830 return true;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000831}
832
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000833void ViEEncoder::SetMinTransmitBitrate(int min_transmit_bitrate_kbps) {
834 assert(min_transmit_bitrate_kbps >= 0);
835 CriticalSectionScoped crit(data_cs_.get());
836 min_transmit_bitrate_kbps_ = min_transmit_bitrate_kbps;
837}
838
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000839// Called from ViEBitrateObserver.
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000840void ViEEncoder::OnNetworkChanged(uint32_t bitrate_bps,
841 uint8_t fraction_lost,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000842 int64_t round_trip_time_ms) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000843 LOG(LS_VERBOSE) << "OnNetworkChanged, bitrate" << bitrate_bps
844 << " packet loss " << fraction_lost
845 << " rtt " << round_trip_time_ms;
mflodman@webrtc.org50e28162015-02-23 07:45:11 +0000846 DCHECK(send_payload_router_ != NULL);
mflodmanfcf54bd2015-04-14 21:28:08 +0200847 vcm_->SetChannelParameters(bitrate_bps, fraction_lost, round_trip_time_ms);
848 bool video_is_suspended = vcm_->VideoSuspended();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000849
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000850 VideoCodec send_codec;
mflodmanfcf54bd2015-04-14 21:28:08 +0200851 if (vcm_->SendCodec(&send_codec) != 0) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000852 return;
853 }
stefan@webrtc.orgb2c8a952013-09-06 13:58:01 +0000854 SimulcastStream* stream_configs = send_codec.simulcastStream;
855 // Allocate the bandwidth between the streams.
856 std::vector<uint32_t> stream_bitrates = AllocateStreamBitrates(
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000857 bitrate_bps, stream_configs, send_codec.numberOfSimulcastStreams);
858 send_payload_router_->SetTargetSendBitrates(stream_bitrates);
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000859
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000860 {
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000861 CriticalSectionScoped cs(data_cs_.get());
pbos@webrtc.org143451d2015-03-18 14:40:03 +0000862 last_observed_bitrate_bps_ = bitrate_bps;
pbos@webrtc.org484ee962013-11-21 18:44:23 +0000863 if (video_suspended_ == video_is_suspended)
864 return;
865 video_suspended_ = video_is_suspended;
866 }
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000867 // Video suspend-state changed, inform codec observer.
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000868 CriticalSectionScoped crit(callback_cs_.get());
pbos@webrtc.org484ee962013-11-21 18:44:23 +0000869 if (codec_observer_) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000870 LOG(LS_INFO) << "Video suspended " << video_is_suspended
871 << " for channel " << channel_id_;
henrik.lundin@webrtc.org9fe36032013-11-21 23:00:40 +0000872 codec_observer_->SuspendChange(channel_id_, video_is_suspended);
henrik.lundin@webrtc.org7ea4f242013-10-02 13:34:26 +0000873 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000874}
875
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000876int32_t ViEEncoder::RegisterEffectFilter(ViEEffectFilter* effect_filter) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000877 CriticalSectionScoped cs(callback_cs_.get());
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000878 if (effect_filter != NULL && effect_filter_ != NULL) {
879 LOG_F(LS_ERROR) << "Filter already set.";
880 return -1;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000881 }
882 effect_filter_ = effect_filter;
883 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000884}
885
mikhal@webrtc.orge41bbdf2012-08-28 16:15:16 +0000886int ViEEncoder::StartDebugRecording(const char* fileNameUTF8) {
mflodmanfcf54bd2015-04-14 21:28:08 +0200887 return vcm_->StartDebugRecording(fileNameUTF8);
mikhal@webrtc.orge41bbdf2012-08-28 16:15:16 +0000888}
889
890int ViEEncoder::StopDebugRecording() {
mflodmanfcf54bd2015-04-14 21:28:08 +0200891 return vcm_->StopDebugRecording();
mikhal@webrtc.orge41bbdf2012-08-28 16:15:16 +0000892}
893
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000894void ViEEncoder::SuspendBelowMinBitrate() {
mflodmanfcf54bd2015-04-14 21:28:08 +0200895 vcm_->SuspendBelowMinBitrate();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000896 bitrate_allocator_->EnforceMinBitrate(false);
henrik.lundin@webrtc.org7ea4f242013-10-02 13:34:26 +0000897}
898
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000899void ViEEncoder::RegisterPreEncodeCallback(
900 I420FrameCallback* pre_encode_callback) {
901 CriticalSectionScoped cs(callback_cs_.get());
902 pre_encode_callback_ = pre_encode_callback;
903}
904
905void ViEEncoder::DeRegisterPreEncodeCallback() {
906 CriticalSectionScoped cs(callback_cs_.get());
907 pre_encode_callback_ = NULL;
908}
909
sprang@webrtc.org40709352013-11-26 11:41:59 +0000910void ViEEncoder::RegisterPostEncodeImageCallback(
911 EncodedImageCallback* post_encode_callback) {
mflodmanfcf54bd2015-04-14 21:28:08 +0200912 vcm_->RegisterPostEncodeImageCallback(post_encode_callback);
sprang@webrtc.org40709352013-11-26 11:41:59 +0000913}
914
915void ViEEncoder::DeRegisterPostEncodeImageCallback() {
mflodmanfcf54bd2015-04-14 21:28:08 +0200916 vcm_->RegisterPostEncodeImageCallback(NULL);
sprang@webrtc.org40709352013-11-26 11:41:59 +0000917}
918
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000919void ViEEncoder::RegisterSendStatisticsProxy(
920 SendStatisticsProxy* send_statistics_proxy) {
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000921 CriticalSectionScoped cs(callback_cs_.get());
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000922 send_statistics_proxy_ = send_statistics_proxy;
923}
924
marpan@webrtc.orgefd01fd2012-04-18 15:56:34 +0000925QMVideoSettingsCallback::QMVideoSettingsCallback(VideoProcessingModule* vpm)
926 : vpm_(vpm) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000927}
niklase@google.com470e71d2011-07-07 08:21:25 +0000928
stefan@webrtc.org439be292012-02-16 14:45:37 +0000929QMVideoSettingsCallback::~QMVideoSettingsCallback() {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000930}
931
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000932int32_t QMVideoSettingsCallback::SetVideoQMSettings(
933 const uint32_t frame_rate,
934 const uint32_t width,
935 const uint32_t height) {
marpan@webrtc.orgcf706c22012-03-27 21:04:13 +0000936 return vpm_->SetTargetResolution(width, height, frame_rate);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000937}
938
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000939} // namespace webrtc