blob: 7ed207fd548482e51243fb08a6076bb43616dff9 [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"
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000034#include "webrtc/video_engine/payload_router.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000035#include "webrtc/video_engine/vie_defines.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000036
niklase@google.com470e71d2011-07-07 08:21:25 +000037namespace webrtc {
38
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +000039// Margin on when we pause the encoder when the pacing buffer overflows relative
40// to the configured buffer delay.
41static const float kEncoderPausePacerMargin = 2.0f;
42
pwestin@webrtc.org91563e42013-04-25 22:20:08 +000043// Don't stop the encoder unless the delay is above this configured value.
44static const int kMinPacingDelayMs = 200;
45
stefan@webrtc.org3e005052013-10-18 15:05:29 +000046static const float kStopPaddingThresholdMs = 2000;
47
stefan@webrtc.orgb2c8a952013-09-06 13:58:01 +000048std::vector<uint32_t> AllocateStreamBitrates(
49 uint32_t total_bitrate,
50 const SimulcastStream* stream_configs,
51 size_t number_of_streams) {
52 if (number_of_streams == 0) {
53 std::vector<uint32_t> stream_bitrates(1, 0);
54 stream_bitrates[0] = total_bitrate;
55 return stream_bitrates;
56 }
57 std::vector<uint32_t> stream_bitrates(number_of_streams, 0);
58 uint32_t bitrate_remainder = total_bitrate;
59 for (size_t i = 0; i < stream_bitrates.size() && bitrate_remainder > 0; ++i) {
60 if (stream_configs[i].maxBitrate * 1000 > bitrate_remainder) {
61 stream_bitrates[i] = bitrate_remainder;
62 } else {
63 stream_bitrates[i] = stream_configs[i].maxBitrate * 1000;
64 }
65 bitrate_remainder -= stream_bitrates[i];
66 }
67 return stream_bitrates;
68}
69
stefan@webrtc.org439be292012-02-16 14:45:37 +000070class QMVideoSettingsCallback : public VCMQMSettingsCallback {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000071 public:
marpan@webrtc.orgefd01fd2012-04-18 15:56:34 +000072 explicit QMVideoSettingsCallback(VideoProcessingModule* vpm);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000073
stefan@webrtc.org439be292012-02-16 14:45:37 +000074 ~QMVideoSettingsCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +000075
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000076 // Update VPM with QM (quality modes: frame size & frame rate) settings.
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000077 int32_t SetVideoQMSettings(const uint32_t frame_rate,
78 const uint32_t width,
79 const uint32_t height);
niklase@google.com470e71d2011-07-07 08:21:25 +000080
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000081 private:
82 VideoProcessingModule* vpm_;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000083};
niklase@google.com470e71d2011-07-07 08:21:25 +000084
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000085class ViEBitrateObserver : public BitrateObserver {
86 public:
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +000087 explicit ViEBitrateObserver(ViEEncoder* owner)
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000088 : owner_(owner) {
89 }
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000090 virtual ~ViEBitrateObserver() {}
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000091 // Implements BitrateObserver.
stefan@webrtc.orgedeea912014-12-08 19:46:23 +000092 virtual void OnNetworkChanged(uint32_t bitrate_bps,
93 uint8_t fraction_lost,
pkasting@chromium.org16825b12015-01-12 21:51:21 +000094 int64_t rtt) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000095 owner_->OnNetworkChanged(bitrate_bps, fraction_lost, rtt);
96 }
97 private:
98 ViEEncoder* owner_;
99};
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
mflodman@webrtc.org9dd0ebc2015-02-26 12:57:47 +0000101ViEEncoder::ViEEncoder(int32_t channel_id,
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000102 uint32_t number_of_cores,
andresp@webrtc.org7707d062013-05-13 10:50:50 +0000103 const Config& config,
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000104 ProcessThread& module_process_thread,
Stefan Holmere5904162015-03-26 11:11:06 +0100105 PacedSender* pacer,
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000106 BitrateAllocator* bitrate_allocator,
Peter Boström5a3ebd72015-05-26 11:44:05 +0200107 BitrateController* bitrate_controller,
108 bool disable_default_encoder)
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000109 : channel_id_(channel_id),
110 number_of_cores_(number_of_cores),
Peter Boström5a3ebd72015-05-26 11:44:05 +0200111 disable_default_encoder_(disable_default_encoder),
mflodmanfcf54bd2015-04-14 21:28:08 +0200112 vpm_(VideoProcessingModule::Create(ViEModuleId(-1, channel_id))),
113 qm_callback_(new QMVideoSettingsCallback(vpm_.get())),
114 vcm_(VideoCodingModule::Create(Clock::GetRealTimeClock(),
115 this,
116 qm_callback_.get())),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000117 send_payload_router_(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),
Noah Richards099323e2015-04-15 09:14:12 -0700123 time_of_last_frame_activity_ms_(0),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000124 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),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000134 module_process_thread_(module_process_thread),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000135 has_received_sli_(false),
136 picture_id_sli_(0),
137 has_received_rpsi_(false),
138 picture_id_rpsi_(0),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000139 video_suspended_(false),
140 pre_encode_callback_(NULL),
141 start_ms_(Clock::GetRealTimeClock()->TimeInMilliseconds()),
142 send_statistics_proxy_(NULL) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000143 bitrate_observer_.reset(new ViEBitrateObserver(this));
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000144}
145
146bool ViEEncoder::Init() {
mflodmanfcf54bd2015-04-14 21:28:08 +0200147 vpm_->EnableTemporalDecimation(true);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000148
149 // Enable/disable content analysis: off by default for now.
mflodmanfcf54bd2015-04-14 21:28:08 +0200150 vpm_->EnableContentAnalysis(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000151
Peter Boström5a3ebd72015-05-26 11:44:05 +0200152 if (!disable_default_encoder_) {
153#ifdef VIDEOCODEC_VP8
154 VideoCodecType codec_type = webrtc::kVideoCodecVP8;
155#else
156 VideoCodecType codec_type = webrtc::kVideoCodecI420;
157#endif
158 VideoCodec video_codec;
159 if (vcm_->Codec(codec_type, &video_codec) != VCM_OK) {
160 return false;
161 }
162 {
163 CriticalSectionScoped cs(data_cs_.get());
164 send_padding_ = video_codec.numberOfSimulcastStreams > 1;
165 }
Peter Boströmae37abb2015-06-18 19:00:34 +0200166 if (vcm_->RegisterSendCodec(
167 &video_codec, number_of_cores_,
168 static_cast<uint32_t>(PayloadRouter::DefaultMaxPayloadLength())) !=
Peter Boström5a3ebd72015-05-26 11:44:05 +0200169 0) {
170 return false;
171 }
172 }
mflodmanfcf54bd2015-04-14 21:28:08 +0200173 if (vcm_->RegisterTransportCallback(this) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000174 return false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000175 }
mflodmanfcf54bd2015-04-14 21:28:08 +0200176 if (vcm_->RegisterSendStatisticsCallback(this) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000177 return false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000178 }
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000179 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000180}
181
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000182void ViEEncoder::StartThreadsAndSetSharedMembers(
Peter Boström26b08602015-06-04 15:18:17 +0200183 rtc::scoped_refptr<PayloadRouter> send_payload_router,
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000184 VCMProtectionCallback* vcm_protection_callback) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000185 DCHECK(send_payload_router_ == NULL);
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000186
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000187 send_payload_router_ = send_payload_router;
mflodmanfcf54bd2015-04-14 21:28:08 +0200188 vcm_->RegisterProtectionCallback(vcm_protection_callback);
189 module_process_thread_.RegisterModule(vcm_.get());
mflodman@webrtc.org290cb562015-02-17 10:15:06 +0000190}
191
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000192void ViEEncoder::StopThreadsAndRemoveSharedMembers() {
mflodmanfcf54bd2015-04-14 21:28:08 +0200193 if (bitrate_allocator_)
194 bitrate_allocator_->RemoveBitrateObserver(bitrate_observer_.get());
195 module_process_thread_.DeRegisterModule(vcm_.get());
196 module_process_thread_.DeRegisterModule(vpm_.get());
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000197}
198
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000199ViEEncoder::~ViEEncoder() {
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000200 UpdateHistograms();
niklase@google.com470e71d2011-07-07 08:21:25 +0000201}
202
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000203void ViEEncoder::UpdateHistograms() {
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000204 int64_t elapsed_sec =
205 (Clock::GetRealTimeClock()->TimeInMilliseconds() - start_ms_) / 1000;
206 if (elapsed_sec < metrics::kMinRunTimeInSeconds) {
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000207 return;
208 }
209 webrtc::VCMFrameCount frames;
mflodmanfcf54bd2015-04-14 21:28:08 +0200210 if (vcm_->SentFrameCount(frames) != VCM_OK) {
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000211 return;
212 }
213 uint32_t total_frames = frames.numKeyFrames + frames.numDeltaFrames;
214 if (total_frames > 0) {
215 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.KeyFramesSentInPermille",
216 static_cast<int>(
217 (frames.numKeyFrames * 1000.0f / total_frames) + 0.5f));
218 }
219}
220
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000221int ViEEncoder::Owner() const {
222 return channel_id_;
223}
224
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000225void ViEEncoder::SetNetworkTransmissionState(bool is_transmitting) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000226 {
227 CriticalSectionScoped cs(data_cs_.get());
228 network_is_transmitting_ = is_transmitting;
229 }
230 if (is_transmitting) {
Stefan Holmere5904162015-03-26 11:11:06 +0100231 pacer_->Resume();
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000232 } else {
Stefan Holmere5904162015-03-26 11:11:06 +0100233 pacer_->Pause();
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000234 }
235}
236
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000237void ViEEncoder::Pause() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000238 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000239 encoder_paused_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000240}
241
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000242void ViEEncoder::Restart() {
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_ = false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000245}
246
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000247uint8_t ViEEncoder::NumberOfCodecs() {
mflodmanfcf54bd2015-04-14 21:28:08 +0200248 return vcm_->NumberOfCodecs();
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000249}
250
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000251int32_t ViEEncoder::GetCodec(uint8_t list_index, VideoCodec* video_codec) {
mflodmanfcf54bd2015-04-14 21:28:08 +0200252 if (vcm_->Codec(list_index, video_codec) != 0) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000253 return -1;
254 }
255 return 0;
256}
257
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000258int32_t ViEEncoder::RegisterExternalEncoder(webrtc::VideoEncoder* encoder,
259 uint8_t pl_type,
260 bool internal_source) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000261 if (encoder == NULL)
262 return -1;
263
mflodmanfcf54bd2015-04-14 21:28:08 +0200264 if (vcm_->RegisterExternalEncoder(encoder, pl_type, internal_source) !=
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000265 VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000266 return -1;
267 }
268 return 0;
269}
270
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000271int32_t ViEEncoder::DeRegisterExternalEncoder(uint8_t pl_type) {
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000272 DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000273 webrtc::VideoCodec current_send_codec;
mflodmanfcf54bd2015-04-14 21:28:08 +0200274 if (vcm_->SendCodec(&current_send_codec) == VCM_OK) {
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000275 uint32_t current_bitrate_bps = 0;
mflodmanfcf54bd2015-04-14 21:28:08 +0200276 if (vcm_->Bitrate(&current_bitrate_bps) != 0) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000277 LOG(LS_WARNING) << "Failed to get the current encoder target bitrate.";
stefan@webrtc.org439be292012-02-16 14:45:37 +0000278 }
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000279 current_send_codec.startBitrate = (current_bitrate_bps + 500) / 1000;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000280 }
281
mflodmanfcf54bd2015-04-14 21:28:08 +0200282 if (vcm_->RegisterExternalEncoder(NULL, pl_type) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000283 return -1;
284 }
285
Peter Boström5a3ebd72015-05-26 11:44:05 +0200286 if (disable_default_encoder_)
287 return 0;
288
289 // If the external encoder is the current send codec, use vcm internal
290 // encoder.
291 if (current_send_codec.plType == pl_type) {
292 {
293 CriticalSectionScoped cs(data_cs_.get());
294 send_padding_ = current_send_codec.numberOfSimulcastStreams > 1;
295 }
296 // TODO(mflodman): Unfortunately the VideoCodec that VCM has cached a
297 // raw pointer to an |extra_options| that's long gone. Clearing it here is
298 // a hack to prevent the following code from crashing. This should be fixed
299 // for realz. https://code.google.com/p/chromium/issues/detail?id=348222
300 current_send_codec.extra_options = NULL;
301 size_t max_data_payload_length = send_payload_router_->MaxPayloadLength();
Peter Boströmae37abb2015-06-18 19:00:34 +0200302 if (vcm_->RegisterSendCodec(
303 &current_send_codec, number_of_cores_,
304 static_cast<uint32_t>(max_data_payload_length)) != VCM_OK) {
Peter Boström5a3ebd72015-05-26 11:44:05 +0200305 LOG(LS_INFO) << "De-registered the currently used external encoder ("
306 << static_cast<int>(pl_type) << ") and therefore tried to "
307 << "register the corresponding internal encoder, but none "
308 << "was supported.";
309 }
310 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000311 return 0;
312}
313
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000314int32_t ViEEncoder::SetEncoder(const webrtc::VideoCodec& video_codec) {
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000315 DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000316 // Setting target width and height for VPM.
mflodmanfcf54bd2015-04-14 21:28:08 +0200317 if (vpm_->SetTargetResolution(video_codec.width, video_codec.height,
318 video_codec.maxFramerate) != VPM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000319 return -1;
320 }
321
stefan@webrtc.org9075d512014-02-14 09:45:58 +0000322 {
323 CriticalSectionScoped cs(data_cs_.get());
324 send_padding_ = video_codec.numberOfSimulcastStreams > 1;
325 }
Stefan Holmere5904162015-03-26 11:11:06 +0100326
327 // Add a bitrate observer to the allocator and update the start, max and
328 // min bitrates of the bitrate controller as needed.
329 int allocated_bitrate_bps;
330 int new_bwe_candidate_bps = bitrate_allocator_->AddBitrateObserver(
331 bitrate_observer_.get(), video_codec.startBitrate * 1000,
332 video_codec.minBitrate * 1000, video_codec.maxBitrate * 1000,
333 &allocated_bitrate_bps);
334
335 // Only set the start/min/max bitrate of the bitrate controller if the start
336 // bitrate is greater than zero. The new API sets these via the channel group
337 // and passes a zero start bitrate to SetSendCodec.
338 // TODO(holmer): Remove this when the new API has been launched.
339 if (video_codec.startBitrate > 0) {
340 if (new_bwe_candidate_bps > 0) {
341 uint32_t current_bwe_bps = 0;
342 bitrate_controller_->AvailableBandwidth(&current_bwe_bps);
343 bitrate_controller_->SetStartBitrate(std::max(
344 static_cast<uint32_t>(new_bwe_candidate_bps), current_bwe_bps));
345 }
346
347 int new_bwe_min_bps = 0;
348 int new_bwe_max_bps = 0;
349 bitrate_allocator_->GetMinMaxBitrateSumBps(&new_bwe_min_bps,
350 &new_bwe_max_bps);
351 bitrate_controller_->SetMinMaxBitrate(new_bwe_min_bps, new_bwe_max_bps);
352 }
353
354 webrtc::VideoCodec modified_video_codec = video_codec;
355 modified_video_codec.startBitrate = allocated_bitrate_bps / 1000;
356
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000357 size_t max_data_payload_length = send_payload_router_->MaxPayloadLength();
mflodmanfcf54bd2015-04-14 21:28:08 +0200358 if (vcm_->RegisterSendCodec(&modified_video_codec, number_of_cores_,
Peter Boströmae37abb2015-06-18 19:00:34 +0200359 static_cast<uint32_t>(max_data_payload_length)) !=
360 VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000361 return -1;
362 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000363 return 0;
364}
365
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000366int32_t ViEEncoder::GetEncoder(VideoCodec* video_codec) {
mflodmanfcf54bd2015-04-14 21:28:08 +0200367 *video_codec = vcm_->GetSendCodec();
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000368 return 0;
369}
niklase@google.com470e71d2011-07-07 08:21:25 +0000370
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000371int32_t ViEEncoder::GetCodecConfigParameters(
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000372 unsigned char config_parameters[kConfigParameterSize],
373 unsigned char& config_parameters_size) {
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000374 int32_t num_parameters =
mflodmanfcf54bd2015-04-14 21:28:08 +0200375 vcm_->CodecConfigParameters(config_parameters, kConfigParameterSize);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000376 if (num_parameters <= 0) {
377 config_parameters_size = 0;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000378 return -1;
379 }
380 config_parameters_size = static_cast<unsigned char>(num_parameters);
381 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000382}
383
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000384int32_t ViEEncoder::ScaleInputImage(bool enable) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000385 VideoFrameResampling resampling_mode = kFastRescaling;
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000386 // TODO(mflodman) What?
387 if (enable) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000388 // kInterpolation is currently not supported.
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000389 LOG_F(LS_ERROR) << "Not supported.";
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000390 return -1;
391 }
mflodmanfcf54bd2015-04-14 21:28:08 +0200392 vpm_->SetInputFrameResampleMode(resampling_mode);
niklase@google.com470e71d2011-07-07 08:21:25 +0000393
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000394 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000395}
396
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000397int ViEEncoder::GetPaddingNeededBps(int bitrate_bps) const {
Noah Richards099323e2015-04-15 09:14:12 -0700398 int64_t time_of_last_frame_activity_ms;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000399 int min_transmit_bitrate_bps;
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +0000400 {
401 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000402 bool send_padding =
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000403 send_padding_ || video_suspended_ || min_transmit_bitrate_kbps_ > 0;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000404 if (!send_padding)
405 return 0;
Noah Richards099323e2015-04-15 09:14:12 -0700406 time_of_last_frame_activity_ms = time_of_last_frame_activity_ms_;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000407 min_transmit_bitrate_bps = 1000 * min_transmit_bitrate_kbps_;
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +0000408 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000409
410 VideoCodec send_codec;
mflodmanfcf54bd2015-04-14 21:28:08 +0200411 if (vcm_->SendCodec(&send_codec) != 0)
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000412 return 0;
413 SimulcastStream* stream_configs = send_codec.simulcastStream;
414 // Allocate the bandwidth between the streams.
415 std::vector<uint32_t> stream_bitrates = AllocateStreamBitrates(
416 bitrate_bps, stream_configs, send_codec.numberOfSimulcastStreams);
417
mflodmanfcf54bd2015-04-14 21:28:08 +0200418 bool video_is_suspended = vcm_->VideoSuspended();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000419
420 // Find the max amount of padding we can allow ourselves to send at this
421 // point, based on which streams are currently active and what our current
422 // available bandwidth is.
423 int pad_up_to_bitrate_bps = 0;
424 if (send_codec.numberOfSimulcastStreams == 0) {
425 pad_up_to_bitrate_bps = send_codec.minBitrate * 1000;
426 } else {
427 pad_up_to_bitrate_bps =
428 stream_configs[send_codec.numberOfSimulcastStreams - 1].minBitrate *
429 1000;
430 for (int i = 0; i < send_codec.numberOfSimulcastStreams - 1; ++i) {
431 pad_up_to_bitrate_bps += stream_configs[i].targetBitrate * 1000;
432 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000433 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000434
435 // Disable padding if only sending one stream and video isn't suspended and
436 // min-transmit bitrate isn't used (applied later).
437 if (!video_is_suspended && send_codec.numberOfSimulcastStreams <= 1)
438 pad_up_to_bitrate_bps = 0;
439
440 // The amount of padding should decay to zero if no frames are being
Noah Richards099323e2015-04-15 09:14:12 -0700441 // captured/encoded unless a min-transmit bitrate is used.
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000442 int64_t now_ms = TickTime::MillisecondTimestamp();
Noah Richards099323e2015-04-15 09:14:12 -0700443 if (now_ms - time_of_last_frame_activity_ms > kStopPaddingThresholdMs)
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000444 pad_up_to_bitrate_bps = 0;
445
446 // Pad up to min bitrate.
447 if (pad_up_to_bitrate_bps < min_transmit_bitrate_bps)
448 pad_up_to_bitrate_bps = min_transmit_bitrate_bps;
449
450 // Padding may never exceed bitrate estimate.
451 if (pad_up_to_bitrate_bps > bitrate_bps)
452 pad_up_to_bitrate_bps = bitrate_bps;
453
454 return pad_up_to_bitrate_bps;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000455}
456
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000457bool ViEEncoder::EncoderPaused() const {
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000458 // Pause video if paused by caller or as long as the network is down or the
459 // pacer queue has grown too large in buffered mode.
460 if (encoder_paused_) {
461 return true;
462 }
463 if (target_delay_ms_ > 0) {
464 // Buffered mode.
465 // TODO(pwestin): Workaround until nack is configured as a time and not
466 // number of packets.
Stefan Holmere5904162015-03-26 11:11:06 +0100467 return pacer_->QueueInMs() >=
468 std::max(
469 static_cast<int>(target_delay_ms_ * kEncoderPausePacerMargin),
470 kMinPacingDelayMs);
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000471 }
Stefan Holmere5904162015-03-26 11:11:06 +0100472 if (pacer_->ExpectedQueueTimeMs() > PacedSender::kDefaultMaxQueueLengthMs) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000473 // Too much data in pacer queue, drop frame.
474 return true;
475 }
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000476 return !network_is_transmitting_;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000477}
478
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000479void ViEEncoder::TraceFrameDropStart() {
480 // Start trace event only on the first frame after encoder is paused.
481 if (!encoder_paused_and_dropped_frame_) {
482 TRACE_EVENT_ASYNC_BEGIN0("webrtc", "EncoderPaused", this);
483 }
484 encoder_paused_and_dropped_frame_ = true;
485 return;
486}
487
488void ViEEncoder::TraceFrameDropEnd() {
489 // End trace event on first frame after encoder resumes, if frame was dropped.
490 if (encoder_paused_and_dropped_frame_) {
491 TRACE_EVENT_ASYNC_END0("webrtc", "EncoderPaused", this);
492 }
493 encoder_paused_and_dropped_frame_ = false;
494}
495
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700496void ViEEncoder::DeliverFrame(VideoFrame video_frame) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000497 DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org47d657b2015-02-19 10:29:32 +0000498 if (!send_payload_router_->active()) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000499 // We've paused or we have no channels attached, don't waste resources on
500 // encoding.
wuchengli@chromium.orgac4b87c2014-03-19 03:44:20 +0000501 return;
502 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000503 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000504 CriticalSectionScoped cs(data_cs_.get());
Noah Richards099323e2015-04-15 09:14:12 -0700505 time_of_last_frame_activity_ms_ = TickTime::MillisecondTimestamp();
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000506 if (EncoderPaused()) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000507 TraceFrameDropStart();
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000508 return;
509 }
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000510 TraceFrameDropEnd();
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000511 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000512
Magnus Jedvert26679d62015-04-07 14:07:41 +0200513 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(),
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000514 "Encode");
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700515 VideoFrame* decimated_frame = NULL;
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000516 // TODO(wuchengli): support texture frames.
Magnus Jedvert26679d62015-04-07 14:07:41 +0200517 if (video_frame.native_handle() == NULL) {
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000518 // Pass frame via preprocessor.
mflodmanfcf54bd2015-04-14 21:28:08 +0200519 const int ret = vpm_->PreprocessFrame(video_frame, &decimated_frame);
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000520 if (ret == 1) {
521 // Drop this frame.
522 return;
523 }
524 if (ret != VPM_OK) {
525 return;
526 }
pwestin@webrtc.org2f476ed2012-10-30 16:21:52 +0000527 }
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000528
Magnus Jedvert26679d62015-04-07 14:07:41 +0200529 // If we haven't resampled the frame and we have a FrameCallback, we need to
530 // make a deep copy of |video_frame|.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700531 VideoFrame copied_frame;
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000532 {
533 CriticalSectionScoped cs(callback_cs_.get());
Magnus Jedvert26679d62015-04-07 14:07:41 +0200534 if (pre_encode_callback_) {
535 // If the frame was not resampled or scaled => use copy of original.
536 if (decimated_frame == NULL) {
537 copied_frame.CopyFrame(video_frame);
538 decimated_frame = &copied_frame;
539 }
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000540 pre_encode_callback_->FrameCallback(decimated_frame);
Magnus Jedvert26679d62015-04-07 14:07:41 +0200541 }
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000542 }
543
Magnus Jedvert26679d62015-04-07 14:07:41 +0200544 // If the frame was not resampled, scaled, or touched by FrameCallback => use
545 // original. The frame is const from here.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700546 const VideoFrame* output_frame =
Magnus Jedvert26679d62015-04-07 14:07:41 +0200547 (decimated_frame != NULL) ? decimated_frame : &video_frame;
548
niklase@google.com470e71d2011-07-07 08:21:25 +0000549#ifdef VIDEOCODEC_VP8
mflodmanfcf54bd2015-04-14 21:28:08 +0200550 if (vcm_->SendCodec() == webrtc::kVideoCodecVP8) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000551 webrtc::CodecSpecificInfo codec_specific_info;
552 codec_specific_info.codecType = webrtc::kVideoCodecVP8;
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000553 {
554 CriticalSectionScoped cs(data_cs_.get());
555 codec_specific_info.codecSpecific.VP8.hasReceivedRPSI =
556 has_received_rpsi_;
557 codec_specific_info.codecSpecific.VP8.hasReceivedSLI =
558 has_received_sli_;
559 codec_specific_info.codecSpecific.VP8.pictureIdRPSI =
560 picture_id_rpsi_;
561 codec_specific_info.codecSpecific.VP8.pictureIdSLI =
562 picture_id_sli_;
563 has_received_sli_ = false;
564 has_received_rpsi_ = false;
565 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000566
mflodmanfcf54bd2015-04-14 21:28:08 +0200567 vcm_->AddVideoFrame(*output_frame, vpm_->ContentMetrics(),
568 &codec_specific_info);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000569 return;
570 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000571#endif
mflodmanfcf54bd2015-04-14 21:28:08 +0200572 vcm_->AddVideoFrame(*output_frame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000573}
niklase@google.com470e71d2011-07-07 08:21:25 +0000574
pwestin@webrtc.orgce330352012-04-12 06:59:14 +0000575int ViEEncoder::SendKeyFrame() {
mflodmanfcf54bd2015-04-14 21:28:08 +0200576 return vcm_->IntraFrameRequest(0);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000577}
578
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000579int32_t ViEEncoder::SendCodecStatistics(
580 uint32_t* num_key_frames, uint32_t* num_delta_frames) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000581 webrtc::VCMFrameCount sent_frames;
mflodmanfcf54bd2015-04-14 21:28:08 +0200582 if (vcm_->SentFrameCount(sent_frames) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000583 return -1;
584 }
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +0000585 *num_key_frames = sent_frames.numKeyFrames;
586 *num_delta_frames = sent_frames.numDeltaFrames;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000587 return 0;
588}
589
pbos@webrtc.org143451d2015-03-18 14:40:03 +0000590uint32_t ViEEncoder::LastObservedBitrateBps() const {
591 CriticalSectionScoped cs(data_cs_.get());
592 return last_observed_bitrate_bps_;
593}
594
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000595int ViEEncoder::CodecTargetBitrate(uint32_t* bitrate) const {
mflodmanfcf54bd2015-04-14 21:28:08 +0200596 if (vcm_->Bitrate(bitrate) != 0)
stefan@webrtc.org439be292012-02-16 14:45:37 +0000597 return -1;
598 return 0;
stefan@webrtc.org07b45a52012-02-02 08:37:48 +0000599}
600
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000601int32_t ViEEncoder::UpdateProtectionMethod(bool nack, bool fec) {
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000602 DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000603
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000604 if (fec_enabled_ == fec && nack_enabled_ == nack) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000605 // No change needed, we're already in correct state.
606 return 0;
607 }
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000608 fec_enabled_ = fec;
609 nack_enabled_ = nack;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000610
611 // Set Video Protection for VCM.
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000612 if (fec_enabled_ && nack_enabled_) {
mflodmanfcf54bd2015-04-14 21:28:08 +0200613 vcm_->SetVideoProtection(webrtc::kProtectionNackFEC, true);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000614 } else {
mflodmanfcf54bd2015-04-14 21:28:08 +0200615 vcm_->SetVideoProtection(webrtc::kProtectionFEC, fec_enabled_);
616 vcm_->SetVideoProtection(webrtc::kProtectionNackSender, nack_enabled_);
617 vcm_->SetVideoProtection(webrtc::kProtectionNackFEC, false);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000618 }
619
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000620 if (fec_enabled_ || nack_enabled_) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000621 // The send codec must be registered to set correct MTU.
622 webrtc::VideoCodec codec;
mflodmanfcf54bd2015-04-14 21:28:08 +0200623 if (vcm_->SendCodec(&codec) == 0) {
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000624 uint32_t current_bitrate_bps = 0;
mflodmanfcf54bd2015-04-14 21:28:08 +0200625 if (vcm_->Bitrate(&current_bitrate_bps) != 0) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000626 LOG_F(LS_WARNING) <<
627 "Failed to get the current encoder target bitrate.";
stefan@webrtc.org439be292012-02-16 14:45:37 +0000628 }
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000629 // Convert to start bitrate in kbps.
630 codec.startBitrate = (current_bitrate_bps + 500) / 1000;
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000631 size_t max_payload_length = send_payload_router_->MaxPayloadLength();
mflodmanfcf54bd2015-04-14 21:28:08 +0200632 if (vcm_->RegisterSendCodec(&codec, number_of_cores_,
Peter Boströmae37abb2015-06-18 19:00:34 +0200633 static_cast<uint32_t>(max_payload_length)) !=
634 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000635 return -1;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000636 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000637 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000638 }
639 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000640}
641
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000642void ViEEncoder::SetSenderBufferingMode(int target_delay_ms) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000643 {
644 CriticalSectionScoped cs(data_cs_.get());
645 target_delay_ms_ = target_delay_ms;
646 }
mikhal@webrtc.org3d305c62013-02-10 18:42:55 +0000647 if (target_delay_ms > 0) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000648 // Disable external frame-droppers.
mflodmanfcf54bd2015-04-14 21:28:08 +0200649 vcm_->EnableFrameDropper(false);
650 vpm_->EnableTemporalDecimation(false);
mikhal@webrtc.org3d305c62013-02-10 18:42:55 +0000651 } else {
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000652 // Real-time mode - enable frame droppers.
mflodmanfcf54bd2015-04-14 21:28:08 +0200653 vpm_->EnableTemporalDecimation(true);
654 vcm_->EnableFrameDropper(true);
mikhal@webrtc.org3d305c62013-02-10 18:42:55 +0000655 }
656}
657
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000658void ViEEncoder::OnSetRates(uint32_t bitrate_bps, int framerate) {
659 CriticalSectionScoped cs(callback_cs_.get());
660 if (send_statistics_proxy_ != nullptr)
661 send_statistics_proxy_->OnSetRates(bitrate_bps, framerate);
662}
663
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000664int32_t ViEEncoder::SendData(
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000665 const uint8_t payload_type,
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000666 const EncodedImage& encoded_image,
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000667 const webrtc::RTPFragmentationHeader& fragmentation_header,
668 const RTPVideoHeader* rtp_video_hdr) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000669 DCHECK(send_payload_router_ != NULL);
670
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000671 {
Noah Richards099323e2015-04-15 09:14:12 -0700672 CriticalSectionScoped cs(data_cs_.get());
673 time_of_last_frame_activity_ms_ = TickTime::MillisecondTimestamp();
674 }
675
676 {
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000677 CriticalSectionScoped cs(callback_cs_.get());
678 if (send_statistics_proxy_ != NULL)
679 send_statistics_proxy_->OnSendEncodedImage(encoded_image, rtp_video_hdr);
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000680 }
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000681
682 return send_payload_router_->RoutePayload(
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000683 VCMEncodedFrame::ConvertFrameType(encoded_image._frameType), payload_type,
684 encoded_image._timeStamp, encoded_image.capture_time_ms_,
685 encoded_image._buffer, encoded_image._length, &fragmentation_header,
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000686 rtp_video_hdr) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000687}
688
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000689int32_t ViEEncoder::SendStatistics(const uint32_t bit_rate,
690 const uint32_t frame_rate) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000691 CriticalSectionScoped cs(callback_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000692 if (codec_observer_) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000693 codec_observer_->OutgoingRate(channel_id_, frame_rate, bit_rate);
694 }
695 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000696}
697
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000698int32_t ViEEncoder::RegisterCodecObserver(ViEEncoderObserver* observer) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000699 CriticalSectionScoped cs(callback_cs_.get());
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000700 if (observer && codec_observer_) {
701 LOG_F(LS_ERROR) << "Observer already set.";
702 return -1;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000703 }
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000704 codec_observer_ = observer;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000705 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000706}
707
andrew@webrtc.org96636862012-09-20 23:33:17 +0000708void ViEEncoder::OnReceivedSLI(uint32_t /*ssrc*/,
709 uint8_t picture_id) {
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000710 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000711 picture_id_sli_ = picture_id;
712 has_received_sli_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000713}
714
andrew@webrtc.org96636862012-09-20 23:33:17 +0000715void ViEEncoder::OnReceivedRPSI(uint32_t /*ssrc*/,
716 uint64_t picture_id) {
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000717 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000718 picture_id_rpsi_ = picture_id;
719 has_received_rpsi_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000720}
721
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000722void ViEEncoder::OnReceivedIntraFrameRequest(uint32_t ssrc) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000723 // Key frame request from remote side, signal to VCM.
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000724 TRACE_EVENT0("webrtc", "OnKeyFrameRequest");
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000725
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000726 int idx = 0;
727 {
728 CriticalSectionScoped cs(data_cs_.get());
Peter Boström5cb9ce42015-05-05 15:16:30 +0200729 auto stream_it = ssrc_streams_.find(ssrc);
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000730 if (stream_it == ssrc_streams_.end()) {
mflodman@webrtc.orgd73527c2012-12-20 09:26:17 +0000731 LOG_F(LS_WARNING) << "ssrc not found: " << ssrc << ", map size "
732 << ssrc_streams_.size();
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000733 return;
734 }
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000735 std::map<unsigned int, int64_t>::iterator time_it =
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000736 time_last_intra_request_ms_.find(ssrc);
737 if (time_it == time_last_intra_request_ms_.end()) {
738 time_last_intra_request_ms_[ssrc] = 0;
739 }
740
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000741 int64_t now = TickTime::MillisecondTimestamp();
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000742 if (time_last_intra_request_ms_[ssrc] + kViEMinKeyRequestIntervalMs > now) {
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000743 return;
744 }
745 time_last_intra_request_ms_[ssrc] = now;
746 idx = stream_it->second;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000747 }
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000748 // Release the critsect before triggering key frame.
mflodmanfcf54bd2015-04-14 21:28:08 +0200749 vcm_->IntraFrameRequest(idx);
niklase@google.com470e71d2011-07-07 08:21:25 +0000750}
751
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000752void ViEEncoder::OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc) {
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000753 CriticalSectionScoped cs(data_cs_.get());
754 std::map<unsigned int, int>::iterator it = ssrc_streams_.find(old_ssrc);
755 if (it == ssrc_streams_.end()) {
756 return;
757 }
758
759 ssrc_streams_[new_ssrc] = it->second;
760 ssrc_streams_.erase(it);
761
762 std::map<unsigned int, int64_t>::iterator time_it =
763 time_last_intra_request_ms_.find(old_ssrc);
764 int64_t last_intra_request_ms = 0;
765 if (time_it != time_last_intra_request_ms_.end()) {
766 last_intra_request_ms = time_it->second;
767 time_last_intra_request_ms_.erase(time_it);
768 }
769 time_last_intra_request_ms_[new_ssrc] = last_intra_request_ms;
770}
771
Peter Boström5cb9ce42015-05-05 15:16:30 +0200772bool ViEEncoder::SetSsrcs(const std::vector<uint32_t>& ssrcs) {
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000773 VideoCodec codec;
mflodmanfcf54bd2015-04-14 21:28:08 +0200774 if (vcm_->SendCodec(&codec) != 0)
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000775 return false;
776
777 if (codec.numberOfSimulcastStreams > 0 &&
778 ssrcs.size() != codec.numberOfSimulcastStreams) {
779 return false;
780 }
781
782 CriticalSectionScoped cs(data_cs_.get());
783 ssrc_streams_.clear();
784 time_last_intra_request_ms_.clear();
785 int idx = 0;
Peter Boström5cb9ce42015-05-05 15:16:30 +0200786 for (uint32_t ssrc : ssrcs) {
787 ssrc_streams_[ssrc] = idx++;
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000788 }
789 return true;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000790}
791
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000792void ViEEncoder::SetMinTransmitBitrate(int min_transmit_bitrate_kbps) {
793 assert(min_transmit_bitrate_kbps >= 0);
794 CriticalSectionScoped crit(data_cs_.get());
795 min_transmit_bitrate_kbps_ = min_transmit_bitrate_kbps;
796}
797
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000798// Called from ViEBitrateObserver.
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000799void ViEEncoder::OnNetworkChanged(uint32_t bitrate_bps,
800 uint8_t fraction_lost,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000801 int64_t round_trip_time_ms) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000802 LOG(LS_VERBOSE) << "OnNetworkChanged, bitrate" << bitrate_bps
mflodman8602a3d2015-05-20 15:54:42 -0700803 << " packet loss " << static_cast<int>(fraction_lost)
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000804 << " rtt " << round_trip_time_ms;
mflodman@webrtc.org50e28162015-02-23 07:45:11 +0000805 DCHECK(send_payload_router_ != NULL);
mflodmanfcf54bd2015-04-14 21:28:08 +0200806 vcm_->SetChannelParameters(bitrate_bps, fraction_lost, round_trip_time_ms);
807 bool video_is_suspended = vcm_->VideoSuspended();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000808
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000809 VideoCodec send_codec;
mflodmanfcf54bd2015-04-14 21:28:08 +0200810 if (vcm_->SendCodec(&send_codec) != 0) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000811 return;
812 }
stefan@webrtc.orgb2c8a952013-09-06 13:58:01 +0000813 SimulcastStream* stream_configs = send_codec.simulcastStream;
814 // Allocate the bandwidth between the streams.
815 std::vector<uint32_t> stream_bitrates = AllocateStreamBitrates(
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000816 bitrate_bps, stream_configs, send_codec.numberOfSimulcastStreams);
817 send_payload_router_->SetTargetSendBitrates(stream_bitrates);
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000818
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000819 {
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000820 CriticalSectionScoped cs(data_cs_.get());
pbos@webrtc.org143451d2015-03-18 14:40:03 +0000821 last_observed_bitrate_bps_ = bitrate_bps;
pbos@webrtc.org484ee962013-11-21 18:44:23 +0000822 if (video_suspended_ == video_is_suspended)
823 return;
824 video_suspended_ = video_is_suspended;
825 }
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000826 // Video suspend-state changed, inform codec observer.
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000827 CriticalSectionScoped crit(callback_cs_.get());
pbos@webrtc.org484ee962013-11-21 18:44:23 +0000828 if (codec_observer_) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000829 LOG(LS_INFO) << "Video suspended " << video_is_suspended
830 << " for channel " << channel_id_;
henrik.lundin@webrtc.org9fe36032013-11-21 23:00:40 +0000831 codec_observer_->SuspendChange(channel_id_, video_is_suspended);
henrik.lundin@webrtc.org7ea4f242013-10-02 13:34:26 +0000832 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000833}
834
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000835void ViEEncoder::SuspendBelowMinBitrate() {
mflodmanfcf54bd2015-04-14 21:28:08 +0200836 vcm_->SuspendBelowMinBitrate();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000837 bitrate_allocator_->EnforceMinBitrate(false);
henrik.lundin@webrtc.org7ea4f242013-10-02 13:34:26 +0000838}
839
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000840void ViEEncoder::RegisterPreEncodeCallback(
841 I420FrameCallback* pre_encode_callback) {
842 CriticalSectionScoped cs(callback_cs_.get());
843 pre_encode_callback_ = pre_encode_callback;
844}
845
sprang@webrtc.org40709352013-11-26 11:41:59 +0000846void ViEEncoder::RegisterPostEncodeImageCallback(
847 EncodedImageCallback* post_encode_callback) {
mflodmanfcf54bd2015-04-14 21:28:08 +0200848 vcm_->RegisterPostEncodeImageCallback(post_encode_callback);
sprang@webrtc.org40709352013-11-26 11:41:59 +0000849}
850
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000851void ViEEncoder::RegisterSendStatisticsProxy(
852 SendStatisticsProxy* send_statistics_proxy) {
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000853 CriticalSectionScoped cs(callback_cs_.get());
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000854 send_statistics_proxy_ = send_statistics_proxy;
855}
856
marpan@webrtc.orgefd01fd2012-04-18 15:56:34 +0000857QMVideoSettingsCallback::QMVideoSettingsCallback(VideoProcessingModule* vpm)
858 : vpm_(vpm) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000859}
niklase@google.com470e71d2011-07-07 08:21:25 +0000860
stefan@webrtc.org439be292012-02-16 14:45:37 +0000861QMVideoSettingsCallback::~QMVideoSettingsCallback() {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000862}
863
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000864int32_t QMVideoSettingsCallback::SetVideoQMSettings(
865 const uint32_t frame_rate,
866 const uint32_t width,
867 const uint32_t height) {
marpan@webrtc.orgcf706c22012-03-27 21:04:13 +0000868 return vpm_->SetTargetResolution(width, height, frame_rate);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000869}
870
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000871} // namespace webrtc