blob: 2b1674d5f3e224be7ebebd84d2eb2a72318f5175 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:251/*
stefan@webrtc.org07b45a52012-02-02 08:37:482 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:253 *
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:4811#include "webrtc/video_engine/vie_encoder.h"
mflodman@webrtc.org84d17832011-12-01 17:02:2312
pbos@webrtc.org12dc1a32013-08-05 16:22:5313#include <assert.h>
14
stefan@webrtc.orgc3cc3752013-06-04 09:36:5615#include <algorithm>
niklase@google.com470e71d2011-07-07 08:21:2516
mflodman@webrtc.org02270cd2015-02-06 13:10:1917#include "webrtc/base/checks.h"
sprang@webrtc.org40709352013-11-26 11:41:5918#include "webrtc/common_video/interface/video_image.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:4819#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
pbos@webrtc.org273a4142014-12-01 15:23:2120#include "webrtc/frame_callback.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:4821#include "webrtc/modules/pacing/include/paced_sender.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:4822#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:5926#include "webrtc/modules/video_coding/main/source/encoded_frame.h"
stefan@webrtc.org88e0dda2014-07-04 09:20:4227#include "webrtc/system_wrappers/interface/clock.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:4828#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
29#include "webrtc/system_wrappers/interface/logging.h"
asapersson@webrtc.org96dc6852014-11-03 14:40:3830#include "webrtc/system_wrappers/interface/metrics.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:4831#include "webrtc/system_wrappers/interface/tick_util.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:4832#include "webrtc/system_wrappers/interface/trace_event.h"
pbos@webrtc.org273a4142014-12-01 15:23:2133#include "webrtc/video/send_statistics_proxy.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:4834#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:1936#include "webrtc/video_engine/payload_router.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:4837#include "webrtc/video_engine/vie_defines.h"
niklase@google.com470e71d2011-07-07 08:21:2538
niklase@google.com470e71d2011-07-07 08:21:2539namespace webrtc {
40
pwestin@webrtc.org52b4e882013-05-02 19:02:1741// 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:0845// 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:2948static const float kStopPaddingThresholdMs = 2000;
49
stefan@webrtc.orgb2c8a952013-09-06 13:58:0150std::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:3772class QMVideoSettingsCallback : public VCMQMSettingsCallback {
mflodman@webrtc.org84d17832011-12-01 17:02:2373 public:
marpan@webrtc.orgefd01fd2012-04-18 15:56:3474 explicit QMVideoSettingsCallback(VideoProcessingModule* vpm);
mflodman@webrtc.org6879c8a2013-07-23 11:35:0075
stefan@webrtc.org439be292012-02-16 14:45:3776 ~QMVideoSettingsCallback();
niklase@google.com470e71d2011-07-07 08:21:2577
mflodman@webrtc.org84d17832011-12-01 17:02:2378 // Update VPM with QM (quality modes: frame size & frame rate) settings.
pbos@webrtc.orgb238d122013-04-09 13:41:5179 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:2582
mflodman@webrtc.org84d17832011-12-01 17:02:2383 private:
84 VideoProcessingModule* vpm_;
mflodman@webrtc.org84d17832011-12-01 17:02:2385};
niklase@google.com470e71d2011-07-07 08:21:2586
pwestin@webrtc.org49888ce2012-04-27 05:25:5387class ViEBitrateObserver : public BitrateObserver {
88 public:
mflodman@webrtc.orgf5e99db2012-06-27 09:49:3789 explicit ViEBitrateObserver(ViEEncoder* owner)
pwestin@webrtc.org49888ce2012-04-27 05:25:5390 : owner_(owner) {
91 }
mflodman@webrtc.org6879c8a2013-07-23 11:35:0092 virtual ~ViEBitrateObserver() {}
pwestin@webrtc.org49888ce2012-04-27 05:25:5393 // Implements BitrateObserver.
stefan@webrtc.orgedeea912014-12-08 19:46:2394 virtual void OnNetworkChanged(uint32_t bitrate_bps,
95 uint8_t fraction_lost,
pkasting@chromium.org16825b12015-01-12 21:51:2196 int64_t rtt) {
pwestin@webrtc.org49888ce2012-04-27 05:25:5397 owner_->OnNetworkChanged(bitrate_bps, fraction_lost, rtt);
98 }
99 private:
100 ViEEncoder* owner_;
101};
niklase@google.com470e71d2011-07-07 08:21:25102
mflodman@webrtc.org9dd0ebc2015-02-26 12:57:47103ViEEncoder::ViEEncoder(int32_t channel_id,
pbos@webrtc.orgb238d122013-04-09 13:41:51104 uint32_t number_of_cores,
andresp@webrtc.org7707d062013-05-13 10:50:50105 const Config& config,
pwestin@webrtc.org49888ce2012-04-27 05:25:53106 ProcessThread& module_process_thread,
Stefan Holmere5904162015-03-26 10:11:06107 PacedSender* pacer,
stefan@webrtc.org792f1a12015-03-04 12:24:26108 BitrateAllocator* bitrate_allocator,
pbos@webrtc.org40367f92015-02-13 08:00:06109 BitrateController* bitrate_controller,
110 bool disable_default_encoder)
stefan@webrtc.org792f1a12015-03-04 12:24:26111 : channel_id_(channel_id),
112 number_of_cores_(number_of_cores),
113 disable_default_encoder_(disable_default_encoder),
mflodmanfcf54bd2015-04-14 19:28:08114 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:26119 send_payload_router_(NULL),
stefan@webrtc.org792f1a12015-03-04 12:24:26120 callback_cs_(CriticalSectionWrapper::CreateCriticalSection()),
121 data_cs_(CriticalSectionWrapper::CreateCriticalSection()),
Stefan Holmere5904162015-03-26 10:11:06122 pacer_(pacer),
stefan@webrtc.org792f1a12015-03-04 12:24:26123 bitrate_allocator_(bitrate_allocator),
124 bitrate_controller_(bitrate_controller),
Noah Richards099323e2015-04-15 16:14:12125 time_of_last_frame_activity_ms_(0),
stefan@webrtc.org792f1a12015-03-04 12:24:26126 send_padding_(false),
127 min_transmit_bitrate_kbps_(0),
pbos@webrtc.org143451d2015-03-18 14:40:03128 last_observed_bitrate_bps_(0),
stefan@webrtc.org792f1a12015-03-04 12:24:26129 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:26138 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:26142 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:53146 bitrate_observer_.reset(new ViEBitrateObserver(this));
wu@webrtc.org5d8c1022012-04-10 16:54:05147}
148
149bool ViEEncoder::Init() {
mflodmanfcf54bd2015-04-14 19:28:08150 if (vcm_->InitializeSender() != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05151 return false;
152 }
mflodmanfcf54bd2015-04-14 19:28:08153 vpm_->EnableTemporalDecimation(true);
mflodman@webrtc.org84d17832011-12-01 17:02:23154
155 // Enable/disable content analysis: off by default for now.
mflodmanfcf54bd2015-04-14 19:28:08156 vpm_->EnableContentAnalysis(false);
niklase@google.com470e71d2011-07-07 08:21:25157
pbos@webrtc.org40367f92015-02-13 08:00:06158 if (!disable_default_encoder_) {
niklase@google.com470e71d2011-07-07 08:21:25159#ifdef VIDEOCODEC_VP8
pbos@webrtc.org40367f92015-02-13 08:00:06160 VideoCodecType codec_type = webrtc::kVideoCodecVP8;
andresp@webrtc.orga84b0a62014-08-14 16:46:46161#else
pbos@webrtc.org40367f92015-02-13 08:00:06162 VideoCodecType codec_type = webrtc::kVideoCodecI420;
andresp@webrtc.orga84b0a62014-08-14 16:46:46163#endif
pbos@webrtc.org40367f92015-02-13 08:00:06164 VideoCodec video_codec;
mflodmanfcf54bd2015-04-14 19:28:08165 if (vcm_->Codec(codec_type, &video_codec) != VCM_OK) {
pbos@webrtc.org40367f92015-02-13 08:00:06166 return false;
167 }
168 {
169 CriticalSectionScoped cs(data_cs_.get());
170 send_padding_ = video_codec.numberOfSimulcastStreams > 1;
171 }
mflodmanfcf54bd2015-04-14 19:28:08172 if (vcm_->RegisterSendCodec(&video_codec, number_of_cores_,
173 PayloadRouter::DefaultMaxPayloadLength()) !=
174 0) {
pbos@webrtc.org40367f92015-02-13 08:00:06175 return false;
176 }
mflodman@webrtc.org84d17832011-12-01 17:02:23177 }
mflodmanfcf54bd2015-04-14 19:28:08178 if (vcm_->RegisterTransportCallback(this) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05179 return false;
mflodman@webrtc.org84d17832011-12-01 17:02:23180 }
mflodmanfcf54bd2015-04-14 19:28:08181 if (vcm_->RegisterSendStatisticsCallback(this) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05182 return false;
mflodman@webrtc.org84d17832011-12-01 17:02:23183 }
wu@webrtc.org5d8c1022012-04-10 16:54:05184 return true;
niklase@google.com470e71d2011-07-07 08:21:25185}
186
mflodman@webrtc.org96abda02015-02-25 13:50:10187void ViEEncoder::StartThreadsAndSetSharedMembers(
188 scoped_refptr<PayloadRouter> send_payload_router,
189 VCMProtectionCallback* vcm_protection_callback) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19190 DCHECK(send_payload_router_ == NULL);
mflodman@webrtc.org96abda02015-02-25 13:50:10191
mflodman@webrtc.org02270cd2015-02-06 13:10:19192 send_payload_router_ = send_payload_router;
mflodmanfcf54bd2015-04-14 19:28:08193 vcm_->RegisterProtectionCallback(vcm_protection_callback);
194 module_process_thread_.RegisterModule(vcm_.get());
mflodman@webrtc.org290cb562015-02-17 10:15:06195}
196
mflodman@webrtc.org96abda02015-02-25 13:50:10197void ViEEncoder::StopThreadsAndRemoveSharedMembers() {
mflodmanfcf54bd2015-04-14 19:28:08198 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:19202}
203
mflodman@webrtc.org84d17832011-12-01 17:02:23204ViEEncoder::~ViEEncoder() {
asapersson@webrtc.org96dc6852014-11-03 14:40:38205 UpdateHistograms();
niklase@google.com470e71d2011-07-07 08:21:25206}
207
asapersson@webrtc.org96dc6852014-11-03 14:40:38208void ViEEncoder::UpdateHistograms() {
asapersson@webrtc.org83b52002014-11-28 10:17:13209 int64_t elapsed_sec =
210 (Clock::GetRealTimeClock()->TimeInMilliseconds() - start_ms_) / 1000;
211 if (elapsed_sec < metrics::kMinRunTimeInSeconds) {
asapersson@webrtc.org96dc6852014-11-03 14:40:38212 return;
213 }
214 webrtc::VCMFrameCount frames;
mflodmanfcf54bd2015-04-14 19:28:08215 if (vcm_->SentFrameCount(frames) != VCM_OK) {
asapersson@webrtc.org96dc6852014-11-03 14:40:38216 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:41226int ViEEncoder::Owner() const {
227 return channel_id_;
228}
229
stefan@webrtc.orgbfacda62013-03-27 16:36:01230void ViEEncoder::SetNetworkTransmissionState(bool is_transmitting) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01231 {
232 CriticalSectionScoped cs(data_cs_.get());
233 network_is_transmitting_ = is_transmitting;
234 }
235 if (is_transmitting) {
Stefan Holmere5904162015-03-26 10:11:06236 pacer_->Resume();
stefan@webrtc.orgbfacda62013-03-27 16:36:01237 } else {
Stefan Holmere5904162015-03-26 10:11:06238 pacer_->Pause();
stefan@webrtc.orgbfacda62013-03-27 16:36:01239 }
240}
241
mflodman@webrtc.org84d17832011-12-01 17:02:23242void ViEEncoder::Pause() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53243 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.orgbfacda62013-03-27 16:36:01244 encoder_paused_ = true;
niklase@google.com470e71d2011-07-07 08:21:25245}
246
mflodman@webrtc.org84d17832011-12-01 17:02:23247void ViEEncoder::Restart() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53248 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.orgbfacda62013-03-27 16:36:01249 encoder_paused_ = false;
mflodman@webrtc.org84d17832011-12-01 17:02:23250}
251
pbos@webrtc.orgb238d122013-04-09 13:41:51252uint8_t ViEEncoder::NumberOfCodecs() {
mflodmanfcf54bd2015-04-14 19:28:08253 return vcm_->NumberOfCodecs();
mflodman@webrtc.org84d17832011-12-01 17:02:23254}
255
pbos@webrtc.orgb238d122013-04-09 13:41:51256int32_t ViEEncoder::GetCodec(uint8_t list_index, VideoCodec* video_codec) {
mflodmanfcf54bd2015-04-14 19:28:08257 if (vcm_->Codec(list_index, video_codec) != 0) {
mflodman@webrtc.org84d17832011-12-01 17:02:23258 return -1;
259 }
260 return 0;
261}
262
pbos@webrtc.orgb238d122013-04-09 13:41:51263int32_t ViEEncoder::RegisterExternalEncoder(webrtc::VideoEncoder* encoder,
264 uint8_t pl_type,
265 bool internal_source) {
mflodman@webrtc.org84d17832011-12-01 17:02:23266 if (encoder == NULL)
267 return -1;
268
mflodmanfcf54bd2015-04-14 19:28:08269 if (vcm_->RegisterExternalEncoder(encoder, pl_type, internal_source) !=
mflodman@webrtc.org5574dac2014-04-07 10:56:31270 VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23271 return -1;
272 }
273 return 0;
274}
275
pbos@webrtc.orgb238d122013-04-09 13:41:51276int32_t ViEEncoder::DeRegisterExternalEncoder(uint8_t pl_type) {
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18277 DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23278 webrtc::VideoCodec current_send_codec;
mflodmanfcf54bd2015-04-14 19:28:08279 if (vcm_->SendCodec(&current_send_codec) == VCM_OK) {
stefan@webrtc.org3d0b0d62013-03-19 10:04:57280 uint32_t current_bitrate_bps = 0;
mflodmanfcf54bd2015-04-14 19:28:08281 if (vcm_->Bitrate(&current_bitrate_bps) != 0) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31282 LOG(LS_WARNING) << "Failed to get the current encoder target bitrate.";
stefan@webrtc.org439be292012-02-16 14:45:37283 }
stefan@webrtc.org3d0b0d62013-03-19 10:04:57284 current_send_codec.startBitrate = (current_bitrate_bps + 500) / 1000;
mflodman@webrtc.org84d17832011-12-01 17:02:23285 }
286
mflodmanfcf54bd2015-04-14 19:28:08287 if (vcm_->RegisterExternalEncoder(NULL, pl_type) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23288 return -1;
289 }
290
pbos@webrtc.org40367f92015-02-13 08:00:06291 if (disable_default_encoder_)
292 return 0;
293
stefan@webrtc.org508a84b2013-06-17 12:53:37294 // If the external encoder is the current send codec, use vcm internal
mflodman@webrtc.org84d17832011-12-01 17:02:23295 // encoder.
296 if (current_send_codec.plType == pl_type) {
stefan@webrtc.orgae2563a2014-02-13 13:48:38297 {
298 CriticalSectionScoped cs(data_cs_.get());
299 send_padding_ = current_send_codec.numberOfSimulcastStreams > 1;
300 }
fischman@webrtc.org64e04052014-03-07 18:00:05301 // 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:18306 size_t max_data_payload_length = send_payload_router_->MaxPayloadLength();
mflodmanfcf54bd2015-04-14 19:28:08307 if (vcm_->RegisterSendCodec(&current_send_codec, number_of_cores_,
308 max_data_payload_length) != VCM_OK) {
stefan@webrtc.org4070b1d2014-07-16 11:20:40309 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:23313 }
314 }
315 return 0;
316}
317
pbos@webrtc.orgb238d122013-04-09 13:41:51318int32_t ViEEncoder::SetEncoder(const webrtc::VideoCodec& video_codec) {
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18319 DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23320 // Setting target width and height for VPM.
mflodmanfcf54bd2015-04-14 19:28:08321 if (vpm_->SetTargetResolution(video_codec.width, video_codec.height,
322 video_codec.maxFramerate) != VPM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23323 return -1;
324 }
325
stefan@webrtc.org9075d512014-02-14 09:45:58326 {
327 CriticalSectionScoped cs(data_cs_.get());
328 send_padding_ = video_codec.numberOfSimulcastStreams > 1;
329 }
Stefan Holmere5904162015-03-26 10:11:06330
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:18361 size_t max_data_payload_length = send_payload_router_->MaxPayloadLength();
mflodmanfcf54bd2015-04-14 19:28:08362 if (vcm_->RegisterSendCodec(&modified_video_codec, number_of_cores_,
363 max_data_payload_length) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23364 return -1;
365 }
mflodman@webrtc.org84d17832011-12-01 17:02:23366 return 0;
367}
368
pbos@webrtc.orgb238d122013-04-09 13:41:51369int32_t ViEEncoder::GetEncoder(VideoCodec* video_codec) {
mflodmanfcf54bd2015-04-14 19:28:08370 *video_codec = vcm_->GetSendCodec();
mflodman@webrtc.org84d17832011-12-01 17:02:23371 return 0;
372}
niklase@google.com470e71d2011-07-07 08:21:25373
pbos@webrtc.orgb238d122013-04-09 13:41:51374int32_t ViEEncoder::GetCodecConfigParameters(
mflodman@webrtc.org84d17832011-12-01 17:02:23375 unsigned char config_parameters[kConfigParameterSize],
376 unsigned char& config_parameters_size) {
pbos@webrtc.orgb238d122013-04-09 13:41:51377 int32_t num_parameters =
mflodmanfcf54bd2015-04-14 19:28:08378 vcm_->CodecConfigParameters(config_parameters, kConfigParameterSize);
mflodman@webrtc.org84d17832011-12-01 17:02:23379 if (num_parameters <= 0) {
380 config_parameters_size = 0;
mflodman@webrtc.org84d17832011-12-01 17:02:23381 return -1;
382 }
383 config_parameters_size = static_cast<unsigned char>(num_parameters);
384 return 0;
niklase@google.com470e71d2011-07-07 08:21:25385}
386
pbos@webrtc.orgb238d122013-04-09 13:41:51387int32_t ViEEncoder::ScaleInputImage(bool enable) {
mflodman@webrtc.org84d17832011-12-01 17:02:23388 VideoFrameResampling resampling_mode = kFastRescaling;
mflodman@webrtc.org5574dac2014-04-07 10:56:31389 // TODO(mflodman) What?
390 if (enable) {
mflodman@webrtc.org84d17832011-12-01 17:02:23391 // kInterpolation is currently not supported.
mflodman@webrtc.org5574dac2014-04-07 10:56:31392 LOG_F(LS_ERROR) << "Not supported.";
mflodman@webrtc.org84d17832011-12-01 17:02:23393 return -1;
394 }
mflodmanfcf54bd2015-04-14 19:28:08395 vpm_->SetInputFrameResampleMode(resampling_mode);
niklase@google.com470e71d2011-07-07 08:21:25396
mflodman@webrtc.org84d17832011-12-01 17:02:23397 return 0;
niklase@google.com470e71d2011-07-07 08:21:25398}
399
stefan@webrtc.org792f1a12015-03-04 12:24:26400int ViEEncoder::GetPaddingNeededBps(int bitrate_bps) const {
Noah Richards099323e2015-04-15 16:14:12401 int64_t time_of_last_frame_activity_ms;
stefan@webrtc.org792f1a12015-03-04 12:24:26402 int min_transmit_bitrate_bps;
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40403 {
404 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.org792f1a12015-03-04 12:24:26405 bool send_padding =
pbos@webrtc.org3349ae02014-03-13 12:52:27406 send_padding_ || video_suspended_ || min_transmit_bitrate_kbps_ > 0;
stefan@webrtc.org792f1a12015-03-04 12:24:26407 if (!send_padding)
408 return 0;
Noah Richards099323e2015-04-15 16:14:12409 time_of_last_frame_activity_ms = time_of_last_frame_activity_ms_;
stefan@webrtc.org792f1a12015-03-04 12:24:26410 min_transmit_bitrate_bps = 1000 * min_transmit_bitrate_kbps_;
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40411 }
stefan@webrtc.org792f1a12015-03-04 12:24:26412
413 VideoCodec send_codec;
mflodmanfcf54bd2015-04-14 19:28:08414 if (vcm_->SendCodec(&send_codec) != 0)
stefan@webrtc.org792f1a12015-03-04 12:24:26415 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 19:28:08421 bool video_is_suspended = vcm_->VideoSuspended();
stefan@webrtc.org792f1a12015-03-04 12:24:26422
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:37436 }
stefan@webrtc.org792f1a12015-03-04 12:24:26437
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
Noah Richards099323e2015-04-15 16:14:12444 // captured/encoded unless a min-transmit bitrate is used.
stefan@webrtc.org792f1a12015-03-04 12:24:26445 int64_t now_ms = TickTime::MillisecondTimestamp();
Noah Richards099323e2015-04-15 16:14:12446 if (now_ms - time_of_last_frame_activity_ms > kStopPaddingThresholdMs)
stefan@webrtc.org792f1a12015-03-04 12:24:26447 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:37458}
459
stefan@webrtc.orgbfacda62013-03-27 16:36:01460bool ViEEncoder::EncoderPaused() const {
pwestin@webrtc.org91563e42013-04-25 22:20:08461 // 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 10:11:06470 return pacer_->QueueInMs() >=
471 std::max(
472 static_cast<int>(target_delay_ms_ * kEncoderPausePacerMargin),
473 kMinPacingDelayMs);
pwestin@webrtc.org91563e42013-04-25 22:20:08474 }
Stefan Holmere5904162015-03-26 10:11:06475 if (pacer_->ExpectedQueueTimeMs() > PacedSender::kDefaultMaxQueueLengthMs) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16476 // Too much data in pacer queue, drop frame.
477 return true;
478 }
pwestin@webrtc.org91563e42013-04-25 22:20:08479 return !network_is_transmitting_;
stefan@webrtc.orgbfacda62013-03-27 16:36:01480}
481
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16482void 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:50499void ViEEncoder::DeliverFrame(int id,
Magnus Jedvert26679d62015-04-07 12:07:41500 const I420VideoFrame& video_frame,
pbos@webrtc.org9334ac22014-11-24 08:25:50501 const std::vector<uint32_t>& csrcs) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19502 DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.orga98e7962015-02-11 15:45:56503 DCHECK(csrcs.empty());
mflodman@webrtc.org47d657b2015-02-19 10:29:32504 if (!send_payload_router_->active()) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19505 // We've paused or we have no channels attached, don't waste resources on
506 // encoding.
wuchengli@chromium.orgac4b87c2014-03-19 03:44:20507 return;
508 }
mflodman@webrtc.org84d17832011-12-01 17:02:23509 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53510 CriticalSectionScoped cs(data_cs_.get());
Noah Richards099323e2015-04-15 16:14:12511 time_of_last_frame_activity_ms_ = TickTime::MillisecondTimestamp();
pwestin@webrtc.org52b4e882013-05-02 19:02:17512 if (EncoderPaused()) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16513 TraceFrameDropStart();
pwestin@webrtc.org52b4e882013-05-02 19:02:17514 return;
515 }
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16516 TraceFrameDropEnd();
mflodman@webrtc.org84d17832011-12-01 17:02:23517 }
niklase@google.com470e71d2011-07-07 08:21:25518
Magnus Jedvert26679d62015-04-07 12:07:41519 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(),
hclam@chromium.org1a7b9b92013-07-08 21:31:18520 "Encode");
pwestin@webrtc.org2f476ed2012-10-30 16:21:52521 I420VideoFrame* decimated_frame = NULL;
wuchengli@chromium.orgf425b552014-06-20 12:04:05522 // TODO(wuchengli): support texture frames.
Magnus Jedvert26679d62015-04-07 12:07:41523 if (video_frame.native_handle() == NULL) {
wuchengli@chromium.orgf425b552014-06-20 12:04:05524 {
525 CriticalSectionScoped cs(callback_cs_.get());
526 if (effect_filter_) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14527 size_t length =
Magnus Jedvert26679d62015-04-07 12:07:41528 CalcBufferSize(kI420, video_frame.width(), video_frame.height());
mflodman@webrtc.org948d6172015-02-10 08:58:16529 rtc::scoped_ptr<uint8_t[]> video_buffer(new uint8_t[length]);
Magnus Jedvert26679d62015-04-07 12:07:41530 ExtractBuffer(video_frame, length, video_buffer.get());
wuchengli@chromium.orgf425b552014-06-20 12:04:05531 effect_filter_->Transform(length,
532 video_buffer.get(),
Magnus Jedvert26679d62015-04-07 12:07:41533 video_frame.ntp_time_ms(),
534 video_frame.timestamp(),
535 video_frame.width(),
536 video_frame.height());
wuchengli@chromium.orgf425b552014-06-20 12:04:05537 }
538 }
539
540 // Pass frame via preprocessor.
mflodmanfcf54bd2015-04-14 19:28:08541 const int ret = vpm_->PreprocessFrame(video_frame, &decimated_frame);
wuchengli@chromium.orgf425b552014-06-20 12:04:05542 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:52549 }
pbos@webrtc.orgfe1ef932013-10-21 10:34:43550
Magnus Jedvert26679d62015-04-07 12:07:41551 // 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:43554 {
555 CriticalSectionScoped cs(callback_cs_.get());
Magnus Jedvert26679d62015-04-07 12:07:41556 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:43562 pre_encode_callback_->FrameCallback(decimated_frame);
Magnus Jedvert26679d62015-04-07 12:07:41563 }
pbos@webrtc.orgfe1ef932013-10-21 10:34:43564 }
565
Magnus Jedvert26679d62015-04-07 12:07:41566 // 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:05572 // TODO(wuchengli): add texture support. http://crbug.com/362437
573 return;
574 }
575
niklase@google.com470e71d2011-07-07 08:21:25576#ifdef VIDEOCODEC_VP8
mflodmanfcf54bd2015-04-14 19:28:08577 if (vcm_->SendCodec() == webrtc::kVideoCodecVP8) {
mflodman@webrtc.org84d17832011-12-01 17:02:23578 webrtc::CodecSpecificInfo codec_specific_info;
579 codec_specific_info.codecType = webrtc::kVideoCodecVP8;
stefan@webrtc.org7af12be2014-07-09 14:46:31580 {
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:23593
mflodmanfcf54bd2015-04-14 19:28:08594 vcm_->AddVideoFrame(*output_frame, vpm_->ContentMetrics(),
595 &codec_specific_info);
mflodman@webrtc.org84d17832011-12-01 17:02:23596 return;
597 }
niklase@google.com470e71d2011-07-07 08:21:25598#endif
mflodmanfcf54bd2015-04-14 19:28:08599 vcm_->AddVideoFrame(*output_frame);
niklase@google.com470e71d2011-07-07 08:21:25600}
niklase@google.com470e71d2011-07-07 08:21:25601
mflodman@webrtc.org84d17832011-12-01 17:02:23602void ViEEncoder::DelayChanged(int id, int frame_delay) {
niklase@google.com470e71d2011-07-07 08:21:25603}
niklase@google.com470e71d2011-07-07 08:21:25604
mflodman@webrtc.org8baed512012-06-21 12:11:50605int ViEEncoder::GetPreferedFrameSettings(int* width,
606 int* height,
607 int* frame_rate) {
mflodman@webrtc.org84d17832011-12-01 17:02:23608 webrtc::VideoCodec video_codec;
609 memset(&video_codec, 0, sizeof(video_codec));
mflodmanfcf54bd2015-04-14 19:28:08610 if (vcm_->SendCodec(&video_codec) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23611 return -1;
612 }
613
mflodman@webrtc.org8baed512012-06-21 12:11:50614 *width = video_codec.width;
615 *height = video_codec.height;
616 *frame_rate = video_codec.maxFramerate;
mflodman@webrtc.org84d17832011-12-01 17:02:23617 return 0;
618}
619
pwestin@webrtc.orgce330352012-04-12 06:59:14620int ViEEncoder::SendKeyFrame() {
mflodmanfcf54bd2015-04-14 19:28:08621 return vcm_->IntraFrameRequest(0);
mflodman@webrtc.org84d17832011-12-01 17:02:23622}
623
pbos@webrtc.orgb238d122013-04-09 13:41:51624int32_t ViEEncoder::SendCodecStatistics(
625 uint32_t* num_key_frames, uint32_t* num_delta_frames) {
mflodman@webrtc.org84d17832011-12-01 17:02:23626 webrtc::VCMFrameCount sent_frames;
mflodmanfcf54bd2015-04-14 19:28:08627 if (vcm_->SentFrameCount(sent_frames) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23628 return -1;
629 }
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37630 *num_key_frames = sent_frames.numKeyFrames;
631 *num_delta_frames = sent_frames.numDeltaFrames;
mflodman@webrtc.org84d17832011-12-01 17:02:23632 return 0;
633}
634
pbos@webrtc.org143451d2015-03-18 14:40:03635uint32_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:51640int ViEEncoder::CodecTargetBitrate(uint32_t* bitrate) const {
mflodmanfcf54bd2015-04-14 19:28:08641 if (vcm_->Bitrate(bitrate) != 0)
stefan@webrtc.org439be292012-02-16 14:45:37642 return -1;
643 return 0;
stefan@webrtc.org07b45a52012-02-02 08:37:48644}
645
mflodman@webrtc.org96abda02015-02-25 13:50:10646int32_t ViEEncoder::UpdateProtectionMethod(bool nack, bool fec) {
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18647 DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23648
mflodman@webrtc.org96abda02015-02-25 13:50:10649 if (fec_enabled_ == fec && nack_enabled_ == nack) {
mflodman@webrtc.org84d17832011-12-01 17:02:23650 // No change needed, we're already in correct state.
651 return 0;
652 }
mflodman@webrtc.org96abda02015-02-25 13:50:10653 fec_enabled_ = fec;
654 nack_enabled_ = nack;
mflodman@webrtc.org84d17832011-12-01 17:02:23655
656 // Set Video Protection for VCM.
mflodman@webrtc.org96abda02015-02-25 13:50:10657 if (fec_enabled_ && nack_enabled_) {
mflodmanfcf54bd2015-04-14 19:28:08658 vcm_->SetVideoProtection(webrtc::kProtectionNackFEC, true);
mflodman@webrtc.org84d17832011-12-01 17:02:23659 } else {
mflodmanfcf54bd2015-04-14 19:28:08660 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:23663 }
664
wu@webrtc.org822fbd82013-08-15 23:38:54665 if (fec_enabled_ || nack_enabled_) {
mflodman@webrtc.org84d17832011-12-01 17:02:23666 // The send codec must be registered to set correct MTU.
667 webrtc::VideoCodec codec;
mflodmanfcf54bd2015-04-14 19:28:08668 if (vcm_->SendCodec(&codec) == 0) {
stefan@webrtc.org3d0b0d62013-03-19 10:04:57669 uint32_t current_bitrate_bps = 0;
mflodmanfcf54bd2015-04-14 19:28:08670 if (vcm_->Bitrate(&current_bitrate_bps) != 0) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31671 LOG_F(LS_WARNING) <<
672 "Failed to get the current encoder target bitrate.";
stefan@webrtc.org439be292012-02-16 14:45:37673 }
stefan@webrtc.org3d0b0d62013-03-19 10:04:57674 // Convert to start bitrate in kbps.
675 codec.startBitrate = (current_bitrate_bps + 500) / 1000;
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18676 size_t max_payload_length = send_payload_router_->MaxPayloadLength();
mflodmanfcf54bd2015-04-14 19:28:08677 if (vcm_->RegisterSendCodec(&codec, number_of_cores_,
678 max_payload_length) != 0) {
niklase@google.com470e71d2011-07-07 08:21:25679 return -1;
mflodman@webrtc.org84d17832011-12-01 17:02:23680 }
niklase@google.com470e71d2011-07-07 08:21:25681 }
mflodman@webrtc.org84d17832011-12-01 17:02:23682 }
683 return 0;
niklase@google.com470e71d2011-07-07 08:21:25684}
685
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18686void ViEEncoder::SetSenderBufferingMode(int target_delay_ms) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01687 {
688 CriticalSectionScoped cs(data_cs_.get());
689 target_delay_ms_ = target_delay_ms;
690 }
mikhal@webrtc.org3d305c62013-02-10 18:42:55691 if (target_delay_ms > 0) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01692 // Disable external frame-droppers.
mflodmanfcf54bd2015-04-14 19:28:08693 vcm_->EnableFrameDropper(false);
694 vpm_->EnableTemporalDecimation(false);
mikhal@webrtc.org3d305c62013-02-10 18:42:55695 } else {
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18696 // Real-time mode - enable frame droppers.
mflodmanfcf54bd2015-04-14 19:28:08697 vpm_->EnableTemporalDecimation(true);
698 vcm_->EnableFrameDropper(true);
mikhal@webrtc.org3d305c62013-02-10 18:42:55699 }
700}
701
pbos@webrtc.org891d4832015-02-26 13:15:22702void 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:51708int32_t ViEEncoder::SendData(
pbos@webrtc.orgb238d122013-04-09 13:41:51709 const uint8_t payload_type,
pbos@webrtc.org273a4142014-12-01 15:23:21710 const EncodedImage& encoded_image,
mflodman@webrtc.org84d17832011-12-01 17:02:23711 const webrtc::RTPFragmentationHeader& fragmentation_header,
712 const RTPVideoHeader* rtp_video_hdr) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19713 DCHECK(send_payload_router_ != NULL);
714
pbos@webrtc.org891d4832015-02-26 13:15:22715 {
Noah Richards099323e2015-04-15 16:14:12716 CriticalSectionScoped cs(data_cs_.get());
717 time_of_last_frame_activity_ms_ = TickTime::MillisecondTimestamp();
718 }
719
720 {
pbos@webrtc.org891d4832015-02-26 13:15:22721 CriticalSectionScoped cs(callback_cs_.get());
722 if (send_statistics_proxy_ != NULL)
723 send_statistics_proxy_->OnSendEncodedImage(encoded_image, rtp_video_hdr);
pbos@webrtc.org273a4142014-12-01 15:23:21724 }
mflodman@webrtc.org02270cd2015-02-06 13:10:19725
726 return send_payload_router_->RoutePayload(
pbos@webrtc.org273a4142014-12-01 15:23:21727 VCMEncodedFrame::ConvertFrameType(encoded_image._frameType), payload_type,
728 encoded_image._timeStamp, encoded_image.capture_time_ms_,
729 encoded_image._buffer, encoded_image._length, &fragmentation_header,
mflodman@webrtc.org02270cd2015-02-06 13:10:19730 rtp_video_hdr) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25731}
732
pbos@webrtc.orgb238d122013-04-09 13:41:51733int32_t ViEEncoder::SendStatistics(const uint32_t bit_rate,
734 const uint32_t frame_rate) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53735 CriticalSectionScoped cs(callback_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23736 if (codec_observer_) {
mflodman@webrtc.org84d17832011-12-01 17:02:23737 codec_observer_->OutgoingRate(channel_id_, frame_rate, bit_rate);
738 }
739 return 0;
niklase@google.com470e71d2011-07-07 08:21:25740}
741
pbos@webrtc.orgb238d122013-04-09 13:41:51742int32_t ViEEncoder::RegisterCodecObserver(ViEEncoderObserver* observer) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53743 CriticalSectionScoped cs(callback_cs_.get());
mflodman@webrtc.org5574dac2014-04-07 10:56:31744 if (observer && codec_observer_) {
745 LOG_F(LS_ERROR) << "Observer already set.";
746 return -1;
mflodman@webrtc.org84d17832011-12-01 17:02:23747 }
mflodman@webrtc.org5574dac2014-04-07 10:56:31748 codec_observer_ = observer;
mflodman@webrtc.org84d17832011-12-01 17:02:23749 return 0;
niklase@google.com470e71d2011-07-07 08:21:25750}
751
andrew@webrtc.org96636862012-09-20 23:33:17752void ViEEncoder::OnReceivedSLI(uint32_t /*ssrc*/,
753 uint8_t picture_id) {
stefan@webrtc.org7af12be2014-07-09 14:46:31754 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23755 picture_id_sli_ = picture_id;
756 has_received_sli_ = true;
niklase@google.com470e71d2011-07-07 08:21:25757}
758
andrew@webrtc.org96636862012-09-20 23:33:17759void ViEEncoder::OnReceivedRPSI(uint32_t /*ssrc*/,
760 uint64_t picture_id) {
stefan@webrtc.org7af12be2014-07-09 14:46:31761 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23762 picture_id_rpsi_ = picture_id;
763 has_received_rpsi_ = true;
niklase@google.com470e71d2011-07-07 08:21:25764}
765
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29766void ViEEncoder::OnReceivedIntraFrameRequest(uint32_t ssrc) {
mflodman@webrtc.org84d17832011-12-01 17:02:23767 // Key frame request from remote side, signal to VCM.
justinlin@chromium.org7bfb3a32013-05-13 22:59:00768 TRACE_EVENT0("webrtc", "OnKeyFrameRequest");
pwestin@webrtc.org1da1ce02011-10-13 15:19:55769
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29770 int idx = 0;
771 {
772 CriticalSectionScoped cs(data_cs_.get());
773 std::map<unsigned int, int>::iterator stream_it = ssrc_streams_.find(ssrc);
774 if (stream_it == ssrc_streams_.end()) {
mflodman@webrtc.orgd73527c2012-12-20 09:26:17775 LOG_F(LS_WARNING) << "ssrc not found: " << ssrc << ", map size "
776 << ssrc_streams_.size();
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29777 return;
778 }
pbos@webrtc.orgb238d122013-04-09 13:41:51779 std::map<unsigned int, int64_t>::iterator time_it =
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29780 time_last_intra_request_ms_.find(ssrc);
781 if (time_it == time_last_intra_request_ms_.end()) {
782 time_last_intra_request_ms_[ssrc] = 0;
783 }
784
pbos@webrtc.orgb238d122013-04-09 13:41:51785 int64_t now = TickTime::MillisecondTimestamp();
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29786 if (time_last_intra_request_ms_[ssrc] + kViEMinKeyRequestIntervalMs > now) {
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29787 return;
788 }
789 time_last_intra_request_ms_[ssrc] = now;
790 idx = stream_it->second;
mflodman@webrtc.org84d17832011-12-01 17:02:23791 }
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29792 // Release the critsect before triggering key frame.
mflodmanfcf54bd2015-04-14 19:28:08793 vcm_->IntraFrameRequest(idx);
niklase@google.com470e71d2011-07-07 08:21:25794}
795
mflodman@webrtc.orgaca26292012-10-05 16:17:41796void ViEEncoder::OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc) {
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29797 CriticalSectionScoped cs(data_cs_.get());
798 std::map<unsigned int, int>::iterator it = ssrc_streams_.find(old_ssrc);
799 if (it == ssrc_streams_.end()) {
800 return;
801 }
802
803 ssrc_streams_[new_ssrc] = it->second;
804 ssrc_streams_.erase(it);
805
806 std::map<unsigned int, int64_t>::iterator time_it =
807 time_last_intra_request_ms_.find(old_ssrc);
808 int64_t last_intra_request_ms = 0;
809 if (time_it != time_last_intra_request_ms_.end()) {
810 last_intra_request_ms = time_it->second;
811 time_last_intra_request_ms_.erase(time_it);
812 }
813 time_last_intra_request_ms_[new_ssrc] = last_intra_request_ms;
814}
815
816bool ViEEncoder::SetSsrcs(const std::list<unsigned int>& ssrcs) {
817 VideoCodec codec;
mflodmanfcf54bd2015-04-14 19:28:08818 if (vcm_->SendCodec(&codec) != 0)
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29819 return false;
820
821 if (codec.numberOfSimulcastStreams > 0 &&
822 ssrcs.size() != codec.numberOfSimulcastStreams) {
823 return false;
824 }
825
826 CriticalSectionScoped cs(data_cs_.get());
827 ssrc_streams_.clear();
828 time_last_intra_request_ms_.clear();
829 int idx = 0;
830 for (std::list<unsigned int>::const_iterator it = ssrcs.begin();
831 it != ssrcs.end(); ++it, ++idx) {
832 unsigned int ssrc = *it;
833 ssrc_streams_[ssrc] = idx;
834 }
835 return true;
mflodman@webrtc.orgaca26292012-10-05 16:17:41836}
837
pbos@webrtc.org3349ae02014-03-13 12:52:27838void ViEEncoder::SetMinTransmitBitrate(int min_transmit_bitrate_kbps) {
839 assert(min_transmit_bitrate_kbps >= 0);
840 CriticalSectionScoped crit(data_cs_.get());
841 min_transmit_bitrate_kbps_ = min_transmit_bitrate_kbps;
842}
843
pwestin@webrtc.org49888ce2012-04-27 05:25:53844// Called from ViEBitrateObserver.
stefan@webrtc.orgedeea912014-12-08 19:46:23845void ViEEncoder::OnNetworkChanged(uint32_t bitrate_bps,
846 uint8_t fraction_lost,
pkasting@chromium.org16825b12015-01-12 21:51:21847 int64_t round_trip_time_ms) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31848 LOG(LS_VERBOSE) << "OnNetworkChanged, bitrate" << bitrate_bps
849 << " packet loss " << fraction_lost
850 << " rtt " << round_trip_time_ms;
mflodman@webrtc.org50e28162015-02-23 07:45:11851 DCHECK(send_payload_router_ != NULL);
mflodmanfcf54bd2015-04-14 19:28:08852 vcm_->SetChannelParameters(bitrate_bps, fraction_lost, round_trip_time_ms);
853 bool video_is_suspended = vcm_->VideoSuspended();
stefan@webrtc.org792f1a12015-03-04 12:24:26854
stefan@webrtc.org508a84b2013-06-17 12:53:37855 VideoCodec send_codec;
mflodmanfcf54bd2015-04-14 19:28:08856 if (vcm_->SendCodec(&send_codec) != 0) {
stefan@webrtc.org508a84b2013-06-17 12:53:37857 return;
858 }
stefan@webrtc.orgb2c8a952013-09-06 13:58:01859 SimulcastStream* stream_configs = send_codec.simulcastStream;
860 // Allocate the bandwidth between the streams.
861 std::vector<uint32_t> stream_bitrates = AllocateStreamBitrates(
stefan@webrtc.org792f1a12015-03-04 12:24:26862 bitrate_bps, stream_configs, send_codec.numberOfSimulcastStreams);
863 send_payload_router_->SetTargetSendBitrates(stream_bitrates);
pbos@webrtc.org709e2972014-03-19 10:59:52864
stefan@webrtc.org3e005052013-10-18 15:05:29865 {
stefan@webrtc.org3e005052013-10-18 15:05:29866 CriticalSectionScoped cs(data_cs_.get());
pbos@webrtc.org143451d2015-03-18 14:40:03867 last_observed_bitrate_bps_ = bitrate_bps;
pbos@webrtc.org484ee962013-11-21 18:44:23868 if (video_suspended_ == video_is_suspended)
869 return;
870 video_suspended_ = video_is_suspended;
871 }
pbos@webrtc.org709e2972014-03-19 10:59:52872 // Video suspend-state changed, inform codec observer.
pbos@webrtc.org3349ae02014-03-13 12:52:27873 CriticalSectionScoped crit(callback_cs_.get());
pbos@webrtc.org484ee962013-11-21 18:44:23874 if (codec_observer_) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31875 LOG(LS_INFO) << "Video suspended " << video_is_suspended
876 << " for channel " << channel_id_;
henrik.lundin@webrtc.org9fe36032013-11-21 23:00:40877 codec_observer_->SuspendChange(channel_id_, video_is_suspended);
henrik.lundin@webrtc.org7ea4f242013-10-02 13:34:26878 }
niklase@google.com470e71d2011-07-07 08:21:25879}
880
pbos@webrtc.orgb238d122013-04-09 13:41:51881int32_t ViEEncoder::RegisterEffectFilter(ViEEffectFilter* effect_filter) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53882 CriticalSectionScoped cs(callback_cs_.get());
mflodman@webrtc.org5574dac2014-04-07 10:56:31883 if (effect_filter != NULL && effect_filter_ != NULL) {
884 LOG_F(LS_ERROR) << "Filter already set.";
885 return -1;
mflodman@webrtc.org84d17832011-12-01 17:02:23886 }
887 effect_filter_ = effect_filter;
888 return 0;
niklase@google.com470e71d2011-07-07 08:21:25889}
890
mikhal@webrtc.orge41bbdf2012-08-28 16:15:16891int ViEEncoder::StartDebugRecording(const char* fileNameUTF8) {
mflodmanfcf54bd2015-04-14 19:28:08892 return vcm_->StartDebugRecording(fileNameUTF8);
mikhal@webrtc.orge41bbdf2012-08-28 16:15:16893}
894
895int ViEEncoder::StopDebugRecording() {
mflodmanfcf54bd2015-04-14 19:28:08896 return vcm_->StopDebugRecording();
mikhal@webrtc.orge41bbdf2012-08-28 16:15:16897}
898
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43899void ViEEncoder::SuspendBelowMinBitrate() {
mflodmanfcf54bd2015-04-14 19:28:08900 vcm_->SuspendBelowMinBitrate();
stefan@webrtc.org792f1a12015-03-04 12:24:26901 bitrate_allocator_->EnforceMinBitrate(false);
henrik.lundin@webrtc.org7ea4f242013-10-02 13:34:26902}
903
pbos@webrtc.orgfe1ef932013-10-21 10:34:43904void ViEEncoder::RegisterPreEncodeCallback(
905 I420FrameCallback* pre_encode_callback) {
906 CriticalSectionScoped cs(callback_cs_.get());
907 pre_encode_callback_ = pre_encode_callback;
908}
909
910void ViEEncoder::DeRegisterPreEncodeCallback() {
911 CriticalSectionScoped cs(callback_cs_.get());
912 pre_encode_callback_ = NULL;
913}
914
sprang@webrtc.org40709352013-11-26 11:41:59915void ViEEncoder::RegisterPostEncodeImageCallback(
916 EncodedImageCallback* post_encode_callback) {
mflodmanfcf54bd2015-04-14 19:28:08917 vcm_->RegisterPostEncodeImageCallback(post_encode_callback);
sprang@webrtc.org40709352013-11-26 11:41:59918}
919
920void ViEEncoder::DeRegisterPostEncodeImageCallback() {
mflodmanfcf54bd2015-04-14 19:28:08921 vcm_->RegisterPostEncodeImageCallback(NULL);
sprang@webrtc.org40709352013-11-26 11:41:59922}
923
pbos@webrtc.org273a4142014-12-01 15:23:21924void ViEEncoder::RegisterSendStatisticsProxy(
925 SendStatisticsProxy* send_statistics_proxy) {
pbos@webrtc.org891d4832015-02-26 13:15:22926 CriticalSectionScoped cs(callback_cs_.get());
pbos@webrtc.org273a4142014-12-01 15:23:21927 send_statistics_proxy_ = send_statistics_proxy;
928}
929
marpan@webrtc.orgefd01fd2012-04-18 15:56:34930QMVideoSettingsCallback::QMVideoSettingsCallback(VideoProcessingModule* vpm)
931 : vpm_(vpm) {
mflodman@webrtc.org84d17832011-12-01 17:02:23932}
niklase@google.com470e71d2011-07-07 08:21:25933
stefan@webrtc.org439be292012-02-16 14:45:37934QMVideoSettingsCallback::~QMVideoSettingsCallback() {
mflodman@webrtc.org84d17832011-12-01 17:02:23935}
936
pbos@webrtc.orgb238d122013-04-09 13:41:51937int32_t QMVideoSettingsCallback::SetVideoQMSettings(
938 const uint32_t frame_rate,
939 const uint32_t width,
940 const uint32_t height) {
marpan@webrtc.orgcf706c22012-03-27 21:04:13941 return vpm_->SetTargetResolution(width, height, frame_rate);
mflodman@webrtc.org84d17832011-12-01 17:02:23942}
943
mflodman@webrtc.org84d17832011-12-01 17:02:23944} // namespace webrtc