blob: ed645bc4fcb68a3e3496dbd981d0c4bf02c9f275 [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
Peter Boström7623ce42015-12-09 11:13:3011#include "webrtc/video/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"
Peter Boström415d2cd2015-10-26 10:35:1718#include "webrtc/base/logging.h"
tommie4f96502015-10-21 06:00:4819#include "webrtc/base/trace_event.h"
mflodman0e7e2592015-11-13 05:02:4220#include "webrtc/call/bitrate_allocator.h"
kjellander6f8ce062015-11-16 21:52:2421#include "webrtc/common_video/include/video_image.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:4822#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
pbos@webrtc.org273a4142014-12-01 15:23:2123#include "webrtc/frame_callback.h"
Peter Boström8e4e8b02015-09-15 13:08:0324#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
Henrik Kjellander0b9e29c2015-11-16 10:12:2425#include "webrtc/modules/pacing/paced_sender.h"
Henrik Kjellanderff761fb2015-11-04 07:31:5226#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander2557b862015-11-18 21:00:2127#include "webrtc/modules/video_coding/include/video_codec_interface.h"
28#include "webrtc/modules/video_coding/include/video_coding.h"
29#include "webrtc/modules/video_coding/include/video_coding_defines.h"
30#include "webrtc/modules/video_coding/encoded_frame.h"
Henrik Kjellander98f53512015-10-28 17:17:4031#include "webrtc/system_wrappers/include/clock.h"
32#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
33#include "webrtc/system_wrappers/include/metrics.h"
34#include "webrtc/system_wrappers/include/tick_util.h"
Peter Boström7623ce42015-12-09 11:13:3035#include "webrtc/video/payload_router.h"
pbos@webrtc.org273a4142014-12-01 15:23:2136#include "webrtc/video/send_statistics_proxy.h"
niklase@google.com470e71d2011-07-07 08:21:2537
niklase@google.com470e71d2011-07-07 08:21:2538namespace webrtc {
39
pwestin@webrtc.org52b4e882013-05-02 19:02:1740// Margin on when we pause the encoder when the pacing buffer overflows relative
41// to the configured buffer delay.
42static const float kEncoderPausePacerMargin = 2.0f;
43
pwestin@webrtc.org91563e42013-04-25 22:20:0844// Don't stop the encoder unless the delay is above this configured value.
45static const int kMinPacingDelayMs = 200;
46
stefan@webrtc.org3e005052013-10-18 15:05:2947static const float kStopPaddingThresholdMs = 2000;
48
mflodmanc4a1c372015-11-06 12:33:5149static const int kMinKeyFrameRequestIntervalMs = 300;
50
stefan@webrtc.orgb2c8a952013-09-06 13:58:0151std::vector<uint32_t> AllocateStreamBitrates(
52 uint32_t total_bitrate,
53 const SimulcastStream* stream_configs,
54 size_t number_of_streams) {
55 if (number_of_streams == 0) {
56 std::vector<uint32_t> stream_bitrates(1, 0);
57 stream_bitrates[0] = total_bitrate;
58 return stream_bitrates;
59 }
60 std::vector<uint32_t> stream_bitrates(number_of_streams, 0);
61 uint32_t bitrate_remainder = total_bitrate;
62 for (size_t i = 0; i < stream_bitrates.size() && bitrate_remainder > 0; ++i) {
63 if (stream_configs[i].maxBitrate * 1000 > bitrate_remainder) {
64 stream_bitrates[i] = bitrate_remainder;
65 } else {
66 stream_bitrates[i] = stream_configs[i].maxBitrate * 1000;
67 }
68 bitrate_remainder -= stream_bitrates[i];
69 }
70 return stream_bitrates;
71}
72
stefan@webrtc.org439be292012-02-16 14:45:3773class QMVideoSettingsCallback : public VCMQMSettingsCallback {
mflodman@webrtc.org84d17832011-12-01 17:02:2374 public:
mflodmana8565422015-12-07 09:09:5275 explicit QMVideoSettingsCallback(VideoProcessing* vpm);
mflodman@webrtc.org6879c8a2013-07-23 11:35:0076
stefan@webrtc.org439be292012-02-16 14:45:3777 ~QMVideoSettingsCallback();
niklase@google.com470e71d2011-07-07 08:21:2578
mflodman@webrtc.org84d17832011-12-01 17:02:2379 // Update VPM with QM (quality modes: frame size & frame rate) settings.
pbos@webrtc.orgb238d122013-04-09 13:41:5180 int32_t SetVideoQMSettings(const uint32_t frame_rate,
81 const uint32_t width,
82 const uint32_t height);
niklase@google.com470e71d2011-07-07 08:21:2583
jackychen6e2ce6e2015-07-13 23:26:3384 // Update target frame rate.
85 void SetTargetFramerate(int frame_rate);
86
mflodman@webrtc.org84d17832011-12-01 17:02:2387 private:
mflodmana8565422015-12-07 09:09:5288 VideoProcessing* vp_;
mflodman@webrtc.org84d17832011-12-01 17:02:2389};
niklase@google.com470e71d2011-07-07 08:21:2590
pwestin@webrtc.org49888ce2012-04-27 05:25:5391class ViEBitrateObserver : public BitrateObserver {
92 public:
mflodman@webrtc.orgf5e99db2012-06-27 09:49:3793 explicit ViEBitrateObserver(ViEEncoder* owner)
pwestin@webrtc.org49888ce2012-04-27 05:25:5394 : owner_(owner) {
95 }
mflodman@webrtc.org6879c8a2013-07-23 11:35:0096 virtual ~ViEBitrateObserver() {}
pwestin@webrtc.org49888ce2012-04-27 05:25:5397 // Implements BitrateObserver.
stefan@webrtc.orgedeea912014-12-08 19:46:2398 virtual void OnNetworkChanged(uint32_t bitrate_bps,
99 uint8_t fraction_lost,
pkasting@chromium.org16825b12015-01-12 21:51:21100 int64_t rtt) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53101 owner_->OnNetworkChanged(bitrate_bps, fraction_lost, rtt);
102 }
103 private:
104 ViEEncoder* owner_;
105};
niklase@google.com470e71d2011-07-07 08:21:25106
mflodman0dbf0092015-10-19 15:12:12107ViEEncoder::ViEEncoder(uint32_t number_of_cores,
Peter Boström7083e112015-09-22 14:28:51108 ProcessThread* module_process_thread,
109 SendStatisticsProxy* stats_proxy,
110 I420FrameCallback* pre_encode_callback,
Stefan Holmere5904162015-03-26 10:11:06111 PacedSender* pacer,
Peter Boström8e4e8b02015-09-15 13:08:03112 BitrateAllocator* bitrate_allocator)
mflodman0dbf0092015-10-19 15:12:12113 : number_of_cores_(number_of_cores),
mflodmana8565422015-12-07 09:09:52114 vp_(VideoProcessing::Create()),
115 qm_callback_(new QMVideoSettingsCallback(vp_.get())),
mflodmanfcf54bd2015-04-14 19:28:08116 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 data_cs_(CriticalSectionWrapper::CreateCriticalSection()),
Peter Boström7083e112015-09-22 14:28:51121 stats_proxy_(stats_proxy),
122 pre_encode_callback_(pre_encode_callback),
Stefan Holmere5904162015-03-26 10:11:06123 pacer_(pacer),
stefan@webrtc.org792f1a12015-03-04 12:24:26124 bitrate_allocator_(bitrate_allocator),
Noah Richards099323e2015-04-15 16:14:12125 time_of_last_frame_activity_ms_(0),
Peter Boströmd153a372015-11-10 15:27:12126 encoder_config_(),
stefan@webrtc.org792f1a12015-03-04 12:24:26127 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),
stefan@webrtc.org792f1a12015-03-04 12:24:26133 module_process_thread_(module_process_thread),
stefan@webrtc.org792f1a12015-03-04 12:24:26134 has_received_sli_(false),
135 picture_id_sli_(0),
136 has_received_rpsi_(false),
137 picture_id_rpsi_(0),
asaperssondec5ebf2015-10-05 09:36:17138 video_suspended_(false) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53139 bitrate_observer_.reset(new ViEBitrateObserver(this));
wu@webrtc.org5d8c1022012-04-10 16:54:05140}
141
142bool ViEEncoder::Init() {
mflodmana8565422015-12-07 09:09:52143 vp_->EnableTemporalDecimation(true);
mflodman@webrtc.org84d17832011-12-01 17:02:23144
145 // Enable/disable content analysis: off by default for now.
mflodmana8565422015-12-07 09:09:52146 vp_->EnableContentAnalysis(false);
niklase@google.com470e71d2011-07-07 08:21:25147
mflodmanfcf54bd2015-04-14 19:28:08148 if (vcm_->RegisterTransportCallback(this) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05149 return false;
mflodman@webrtc.org84d17832011-12-01 17:02:23150 }
mflodmanfcf54bd2015-04-14 19:28:08151 if (vcm_->RegisterSendStatisticsCallback(this) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05152 return false;
mflodman@webrtc.org84d17832011-12-01 17:02:23153 }
wu@webrtc.org5d8c1022012-04-10 16:54:05154 return true;
niklase@google.com470e71d2011-07-07 08:21:25155}
156
mflodman@webrtc.org96abda02015-02-25 13:50:10157void ViEEncoder::StartThreadsAndSetSharedMembers(
Peter Boström26b08602015-06-04 13:18:17158 rtc::scoped_refptr<PayloadRouter> send_payload_router,
mflodman@webrtc.org96abda02015-02-25 13:50:10159 VCMProtectionCallback* vcm_protection_callback) {
henrikg91d6ede2015-09-17 07:24:34160 RTC_DCHECK(send_payload_router_ == NULL);
mflodman@webrtc.org96abda02015-02-25 13:50:10161
mflodman@webrtc.org02270cd2015-02-06 13:10:19162 send_payload_router_ = send_payload_router;
mflodmanfcf54bd2015-04-14 19:28:08163 vcm_->RegisterProtectionCallback(vcm_protection_callback);
Peter Boström7083e112015-09-22 14:28:51164 module_process_thread_->RegisterModule(vcm_.get());
mflodman@webrtc.org290cb562015-02-17 10:15:06165}
166
mflodman@webrtc.org96abda02015-02-25 13:50:10167void ViEEncoder::StopThreadsAndRemoveSharedMembers() {
mflodmanfcf54bd2015-04-14 19:28:08168 if (bitrate_allocator_)
169 bitrate_allocator_->RemoveBitrateObserver(bitrate_observer_.get());
Peter Boström7083e112015-09-22 14:28:51170 module_process_thread_->DeRegisterModule(vcm_.get());
mflodman@webrtc.org02270cd2015-02-06 13:10:19171}
172
mflodman@webrtc.org84d17832011-12-01 17:02:23173ViEEncoder::~ViEEncoder() {
asapersson@webrtc.org96dc6852014-11-03 14:40:38174}
175
stefan@webrtc.orgbfacda62013-03-27 16:36:01176void ViEEncoder::SetNetworkTransmissionState(bool is_transmitting) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01177 {
178 CriticalSectionScoped cs(data_cs_.get());
179 network_is_transmitting_ = is_transmitting;
180 }
stefan@webrtc.orgbfacda62013-03-27 16:36:01181}
182
mflodman@webrtc.org84d17832011-12-01 17:02:23183void ViEEncoder::Pause() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53184 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.orgbfacda62013-03-27 16:36:01185 encoder_paused_ = true;
niklase@google.com470e71d2011-07-07 08:21:25186}
187
mflodman@webrtc.org84d17832011-12-01 17:02:23188void ViEEncoder::Restart() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53189 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.orgbfacda62013-03-27 16:36:01190 encoder_paused_ = false;
mflodman@webrtc.org84d17832011-12-01 17:02:23191}
192
pbos@webrtc.orgb238d122013-04-09 13:41:51193int32_t ViEEncoder::RegisterExternalEncoder(webrtc::VideoEncoder* encoder,
194 uint8_t pl_type,
195 bool internal_source) {
mflodmanfcf54bd2015-04-14 19:28:08196 if (vcm_->RegisterExternalEncoder(encoder, pl_type, internal_source) !=
mflodman@webrtc.org5574dac2014-04-07 10:56:31197 VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23198 return -1;
199 }
200 return 0;
201}
202
pbos@webrtc.orgb238d122013-04-09 13:41:51203int32_t ViEEncoder::DeRegisterExternalEncoder(uint8_t pl_type) {
mflodmanfcf54bd2015-04-14 19:28:08204 if (vcm_->RegisterExternalEncoder(NULL, pl_type) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23205 return -1;
206 }
mflodman@webrtc.org84d17832011-12-01 17:02:23207 return 0;
208}
209
pbos@webrtc.orgb238d122013-04-09 13:41:51210int32_t ViEEncoder::SetEncoder(const webrtc::VideoCodec& video_codec) {
henrikg91d6ede2015-09-17 07:24:34211 RTC_DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23212 // Setting target width and height for VPM.
mflodmana8565422015-12-07 09:09:52213 if (vp_->SetTargetResolution(video_codec.width, video_codec.height,
214 video_codec.maxFramerate) != VPM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23215 return -1;
216 }
217
Peter Boströmd153a372015-11-10 15:27:12218 // Cache codec before calling AddBitrateObserver (which calls OnNetworkChanged
219 // that makes use of the number of simulcast streams configured).
stefan@webrtc.org9075d512014-02-14 09:45:58220 {
221 CriticalSectionScoped cs(data_cs_.get());
Peter Boströmd153a372015-11-10 15:27:12222 encoder_config_ = video_codec;
stefan@webrtc.org9075d512014-02-14 09:45:58223 }
Stefan Holmere5904162015-03-26 10:11:06224
225 // Add a bitrate observer to the allocator and update the start, max and
226 // min bitrates of the bitrate controller as needed.
Peter Boström8e4e8b02015-09-15 13:08:03227 int allocated_bitrate_bps = bitrate_allocator_->AddBitrateObserver(
228 bitrate_observer_.get(), video_codec.minBitrate * 1000,
229 video_codec.maxBitrate * 1000);
Stefan Holmere5904162015-03-26 10:11:06230
231 webrtc::VideoCodec modified_video_codec = video_codec;
232 modified_video_codec.startBitrate = allocated_bitrate_bps / 1000;
233
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18234 size_t max_data_payload_length = send_payload_router_->MaxPayloadLength();
mflodmanfcf54bd2015-04-14 19:28:08235 if (vcm_->RegisterSendCodec(&modified_video_codec, number_of_cores_,
Peter Boströmae37abb2015-06-18 17:00:34236 static_cast<uint32_t>(max_data_payload_length)) !=
237 VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23238 return -1;
239 }
mflodman@webrtc.org84d17832011-12-01 17:02:23240 return 0;
241}
242
stefana4a8d4a2015-07-15 11:39:22243int ViEEncoder::GetPaddingNeededBps() const {
Noah Richards099323e2015-04-15 16:14:12244 int64_t time_of_last_frame_activity_ms;
stefan@webrtc.org792f1a12015-03-04 12:24:26245 int min_transmit_bitrate_bps;
stefana4a8d4a2015-07-15 11:39:22246 int bitrate_bps;
Peter Boströmd153a372015-11-10 15:27:12247 VideoCodec send_codec;
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40248 {
249 CriticalSectionScoped cs(data_cs_.get());
Peter Boströmd153a372015-11-10 15:27:12250 bool send_padding = encoder_config_.numberOfSimulcastStreams > 1 ||
251 video_suspended_ || min_transmit_bitrate_kbps_ > 0;
stefan@webrtc.org792f1a12015-03-04 12:24:26252 if (!send_padding)
253 return 0;
Noah Richards099323e2015-04-15 16:14:12254 time_of_last_frame_activity_ms = time_of_last_frame_activity_ms_;
stefan@webrtc.org792f1a12015-03-04 12:24:26255 min_transmit_bitrate_bps = 1000 * min_transmit_bitrate_kbps_;
stefana4a8d4a2015-07-15 11:39:22256 bitrate_bps = last_observed_bitrate_bps_;
Peter Boströmd153a372015-11-10 15:27:12257 send_codec = encoder_config_;
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40258 }
stefan@webrtc.org792f1a12015-03-04 12:24:26259
mflodmanfcf54bd2015-04-14 19:28:08260 bool video_is_suspended = vcm_->VideoSuspended();
stefan@webrtc.org792f1a12015-03-04 12:24:26261
262 // Find the max amount of padding we can allow ourselves to send at this
263 // point, based on which streams are currently active and what our current
264 // available bandwidth is.
265 int pad_up_to_bitrate_bps = 0;
266 if (send_codec.numberOfSimulcastStreams == 0) {
267 pad_up_to_bitrate_bps = send_codec.minBitrate * 1000;
268 } else {
stefana4a8d4a2015-07-15 11:39:22269 SimulcastStream* stream_configs = send_codec.simulcastStream;
stefan@webrtc.org792f1a12015-03-04 12:24:26270 pad_up_to_bitrate_bps =
271 stream_configs[send_codec.numberOfSimulcastStreams - 1].minBitrate *
272 1000;
273 for (int i = 0; i < send_codec.numberOfSimulcastStreams - 1; ++i) {
274 pad_up_to_bitrate_bps += stream_configs[i].targetBitrate * 1000;
275 }
stefan@webrtc.org508a84b2013-06-17 12:53:37276 }
stefan@webrtc.org792f1a12015-03-04 12:24:26277
278 // Disable padding if only sending one stream and video isn't suspended and
279 // min-transmit bitrate isn't used (applied later).
280 if (!video_is_suspended && send_codec.numberOfSimulcastStreams <= 1)
281 pad_up_to_bitrate_bps = 0;
282
283 // The amount of padding should decay to zero if no frames are being
Noah Richards099323e2015-04-15 16:14:12284 // captured/encoded unless a min-transmit bitrate is used.
stefan@webrtc.org792f1a12015-03-04 12:24:26285 int64_t now_ms = TickTime::MillisecondTimestamp();
Noah Richards099323e2015-04-15 16:14:12286 if (now_ms - time_of_last_frame_activity_ms > kStopPaddingThresholdMs)
stefan@webrtc.org792f1a12015-03-04 12:24:26287 pad_up_to_bitrate_bps = 0;
288
289 // Pad up to min bitrate.
290 if (pad_up_to_bitrate_bps < min_transmit_bitrate_bps)
291 pad_up_to_bitrate_bps = min_transmit_bitrate_bps;
292
293 // Padding may never exceed bitrate estimate.
294 if (pad_up_to_bitrate_bps > bitrate_bps)
295 pad_up_to_bitrate_bps = bitrate_bps;
296
297 return pad_up_to_bitrate_bps;
stefan@webrtc.org508a84b2013-06-17 12:53:37298}
299
stefan@webrtc.orgbfacda62013-03-27 16:36:01300bool ViEEncoder::EncoderPaused() const {
pwestin@webrtc.org91563e42013-04-25 22:20:08301 // Pause video if paused by caller or as long as the network is down or the
302 // pacer queue has grown too large in buffered mode.
303 if (encoder_paused_) {
304 return true;
305 }
306 if (target_delay_ms_ > 0) {
307 // Buffered mode.
308 // TODO(pwestin): Workaround until nack is configured as a time and not
309 // number of packets.
Stefan Holmere5904162015-03-26 10:11:06310 return pacer_->QueueInMs() >=
311 std::max(
312 static_cast<int>(target_delay_ms_ * kEncoderPausePacerMargin),
313 kMinPacingDelayMs);
pwestin@webrtc.org91563e42013-04-25 22:20:08314 }
sprang0a43fef2015-11-20 17:00:37315 if (pacer_->ExpectedQueueTimeMs() > PacedSender::kMaxQueueLengthMs) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16316 // Too much data in pacer queue, drop frame.
317 return true;
318 }
pwestin@webrtc.org91563e42013-04-25 22:20:08319 return !network_is_transmitting_;
stefan@webrtc.orgbfacda62013-03-27 16:36:01320}
321
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16322void ViEEncoder::TraceFrameDropStart() {
323 // Start trace event only on the first frame after encoder is paused.
324 if (!encoder_paused_and_dropped_frame_) {
325 TRACE_EVENT_ASYNC_BEGIN0("webrtc", "EncoderPaused", this);
326 }
327 encoder_paused_and_dropped_frame_ = true;
328 return;
329}
330
331void ViEEncoder::TraceFrameDropEnd() {
332 // End trace event on first frame after encoder resumes, if frame was dropped.
333 if (encoder_paused_and_dropped_frame_) {
334 TRACE_EVENT_ASYNC_END0("webrtc", "EncoderPaused", this);
335 }
336 encoder_paused_and_dropped_frame_ = false;
337}
338
Miguel Casas-Sanchez47650702015-05-30 00:21:40339void ViEEncoder::DeliverFrame(VideoFrame video_frame) {
henrikg91d6ede2015-09-17 07:24:34340 RTC_DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org47d657b2015-02-19 10:29:32341 if (!send_payload_router_->active()) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19342 // We've paused or we have no channels attached, don't waste resources on
343 // encoding.
wuchengli@chromium.orgac4b87c2014-03-19 03:44:20344 return;
345 }
Peter Boströmd153a372015-11-10 15:27:12346 VideoCodecType codec_type;
mflodman@webrtc.org84d17832011-12-01 17:02:23347 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53348 CriticalSectionScoped cs(data_cs_.get());
Noah Richards099323e2015-04-15 16:14:12349 time_of_last_frame_activity_ms_ = TickTime::MillisecondTimestamp();
pwestin@webrtc.org52b4e882013-05-02 19:02:17350 if (EncoderPaused()) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16351 TraceFrameDropStart();
pwestin@webrtc.org52b4e882013-05-02 19:02:17352 return;
353 }
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16354 TraceFrameDropEnd();
Peter Boströmd153a372015-11-10 15:27:12355 codec_type = encoder_config_.codecType;
mflodman@webrtc.org84d17832011-12-01 17:02:23356 }
niklase@google.com470e71d2011-07-07 08:21:25357
Magnus Jedvert26679d62015-04-07 12:07:41358 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(),
hclam@chromium.org1a7b9b92013-07-08 21:31:18359 "Encode");
mflodmana8565422015-12-07 09:09:52360 const VideoFrame* frame_to_send = &video_frame;
wuchengli@chromium.orgf425b552014-06-20 12:04:05361 // TODO(wuchengli): support texture frames.
Magnus Jedvert26679d62015-04-07 12:07:41362 if (video_frame.native_handle() == NULL) {
wuchengli@chromium.orgf425b552014-06-20 12:04:05363 // Pass frame via preprocessor.
mflodmana8565422015-12-07 09:09:52364 frame_to_send = vp_->PreprocessFrame(video_frame);
365 if (!frame_to_send) {
366 // Drop this frame, or there was an error processing it.
wuchengli@chromium.orgf425b552014-06-20 12:04:05367 return;
368 }
pwestin@webrtc.org2f476ed2012-10-30 16:21:52369 }
pbos@webrtc.orgfe1ef932013-10-21 10:34:43370
Magnus Jedvert26679d62015-04-07 12:07:41371 // If we haven't resampled the frame and we have a FrameCallback, we need to
372 // make a deep copy of |video_frame|.
Miguel Casas-Sanchez47650702015-05-30 00:21:40373 VideoFrame copied_frame;
Peter Boström7083e112015-09-22 14:28:51374 if (pre_encode_callback_) {
mflodmana8565422015-12-07 09:09:52375 copied_frame.CopyFrame(*frame_to_send);
376 pre_encode_callback_->FrameCallback(&copied_frame);
377 frame_to_send = &copied_frame;
pbos@webrtc.orgfe1ef932013-10-21 10:34:43378 }
379
Peter Boströmd153a372015-11-10 15:27:12380 if (codec_type == webrtc::kVideoCodecVP8) {
mflodman@webrtc.org84d17832011-12-01 17:02:23381 webrtc::CodecSpecificInfo codec_specific_info;
382 codec_specific_info.codecType = webrtc::kVideoCodecVP8;
stefan@webrtc.org7af12be2014-07-09 14:46:31383 {
384 CriticalSectionScoped cs(data_cs_.get());
385 codec_specific_info.codecSpecific.VP8.hasReceivedRPSI =
386 has_received_rpsi_;
387 codec_specific_info.codecSpecific.VP8.hasReceivedSLI =
388 has_received_sli_;
389 codec_specific_info.codecSpecific.VP8.pictureIdRPSI =
390 picture_id_rpsi_;
391 codec_specific_info.codecSpecific.VP8.pictureIdSLI =
392 picture_id_sli_;
393 has_received_sli_ = false;
394 has_received_rpsi_ = false;
395 }
mflodman@webrtc.org84d17832011-12-01 17:02:23396
mflodmana8565422015-12-07 09:09:52397 vcm_->AddVideoFrame(*frame_to_send, vp_->GetContentMetrics(),
mflodmanfcf54bd2015-04-14 19:28:08398 &codec_specific_info);
mflodman@webrtc.org84d17832011-12-01 17:02:23399 return;
400 }
mflodmana8565422015-12-07 09:09:52401 vcm_->AddVideoFrame(*frame_to_send);
niklase@google.com470e71d2011-07-07 08:21:25402}
niklase@google.com470e71d2011-07-07 08:21:25403
Peter Boström233bfd22016-01-18 19:23:40404void ViEEncoder::SendKeyFrame() {
405 vcm_->IntraFrameRequest(0);
mflodman@webrtc.org84d17832011-12-01 17:02:23406}
407
pbos@webrtc.org143451d2015-03-18 14:40:03408uint32_t ViEEncoder::LastObservedBitrateBps() const {
409 CriticalSectionScoped cs(data_cs_.get());
410 return last_observed_bitrate_bps_;
411}
412
pbos@webrtc.orgb238d122013-04-09 13:41:51413int ViEEncoder::CodecTargetBitrate(uint32_t* bitrate) const {
mflodmanfcf54bd2015-04-14 19:28:08414 if (vcm_->Bitrate(bitrate) != 0)
stefan@webrtc.org439be292012-02-16 14:45:37415 return -1;
416 return 0;
stefan@webrtc.org07b45a52012-02-02 08:37:48417}
418
Peter Boströmd153a372015-11-10 15:27:12419void ViEEncoder::SetProtectionMethod(bool nack, bool fec) {
mflodman@webrtc.org84d17832011-12-01 17:02:23420 // Set Video Protection for VCM.
pbosba8c15b2015-07-14 16:36:34421 VCMVideoProtection protection_mode;
Peter Boströmd153a372015-11-10 15:27:12422 if (fec) {
pbosba8c15b2015-07-14 16:36:34423 protection_mode =
Peter Boströmd153a372015-11-10 15:27:12424 nack ? webrtc::kProtectionNackFEC : kProtectionFEC;
mflodman@webrtc.org84d17832011-12-01 17:02:23425 } else {
Peter Boströmd153a372015-11-10 15:27:12426 protection_mode = nack ? kProtectionNack : kProtectionNone;
mflodman@webrtc.org84d17832011-12-01 17:02:23427 }
pbosba8c15b2015-07-14 16:36:34428 vcm_->SetVideoProtection(protection_mode, true);
niklase@google.com470e71d2011-07-07 08:21:25429}
430
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18431void ViEEncoder::SetSenderBufferingMode(int target_delay_ms) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01432 {
433 CriticalSectionScoped cs(data_cs_.get());
434 target_delay_ms_ = target_delay_ms;
435 }
mikhal@webrtc.org3d305c62013-02-10 18:42:55436 if (target_delay_ms > 0) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01437 // Disable external frame-droppers.
mflodmanfcf54bd2015-04-14 19:28:08438 vcm_->EnableFrameDropper(false);
mflodmana8565422015-12-07 09:09:52439 vp_->EnableTemporalDecimation(false);
mikhal@webrtc.org3d305c62013-02-10 18:42:55440 } else {
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18441 // Real-time mode - enable frame droppers.
mflodmana8565422015-12-07 09:09:52442 vp_->EnableTemporalDecimation(true);
mflodmanfcf54bd2015-04-14 19:28:08443 vcm_->EnableFrameDropper(true);
mikhal@webrtc.org3d305c62013-02-10 18:42:55444 }
445}
446
pbos@webrtc.org891d4832015-02-26 13:15:22447void ViEEncoder::OnSetRates(uint32_t bitrate_bps, int framerate) {
Peter Boström7083e112015-09-22 14:28:51448 if (stats_proxy_)
449 stats_proxy_->OnSetRates(bitrate_bps, framerate);
pbos@webrtc.org891d4832015-02-26 13:15:22450}
451
pbos@webrtc.orgb238d122013-04-09 13:41:51452int32_t ViEEncoder::SendData(
pbos@webrtc.orgb238d122013-04-09 13:41:51453 const uint8_t payload_type,
pbos@webrtc.org273a4142014-12-01 15:23:21454 const EncodedImage& encoded_image,
mflodman@webrtc.org84d17832011-12-01 17:02:23455 const webrtc::RTPFragmentationHeader& fragmentation_header,
456 const RTPVideoHeader* rtp_video_hdr) {
henrikg91d6ede2015-09-17 07:24:34457 RTC_DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org02270cd2015-02-06 13:10:19458
pbos@webrtc.org891d4832015-02-26 13:15:22459 {
Noah Richards099323e2015-04-15 16:14:12460 CriticalSectionScoped cs(data_cs_.get());
461 time_of_last_frame_activity_ms_ = TickTime::MillisecondTimestamp();
462 }
463
Peter Boström7083e112015-09-22 14:28:51464 if (stats_proxy_ != NULL)
465 stats_proxy_->OnSendEncodedImage(encoded_image, rtp_video_hdr);
mflodman@webrtc.org02270cd2015-02-06 13:10:19466
467 return send_payload_router_->RoutePayload(
pbos22993e12015-10-19 09:39:06468 encoded_image._frameType, payload_type, encoded_image._timeStamp,
469 encoded_image.capture_time_ms_, encoded_image._buffer,
470 encoded_image._length, &fragmentation_header, rtp_video_hdr)
471 ? 0
472 : -1;
niklase@google.com470e71d2011-07-07 08:21:25473}
474
Peter Boströmb7d9a972015-12-18 15:01:11475void ViEEncoder::OnEncoderImplementationName(
476 const char* implementation_name) {
477 if (stats_proxy_)
478 stats_proxy_->OnEncoderImplementationName(implementation_name);
479}
480
pbos@webrtc.orgb238d122013-04-09 13:41:51481int32_t ViEEncoder::SendStatistics(const uint32_t bit_rate,
482 const uint32_t frame_rate) {
Peter Boström7083e112015-09-22 14:28:51483 if (stats_proxy_)
484 stats_proxy_->OnOutgoingRate(frame_rate, bit_rate);
mflodman@webrtc.org84d17832011-12-01 17:02:23485 return 0;
niklase@google.com470e71d2011-07-07 08:21:25486}
487
andrew@webrtc.org96636862012-09-20 23:33:17488void ViEEncoder::OnReceivedSLI(uint32_t /*ssrc*/,
489 uint8_t picture_id) {
stefan@webrtc.org7af12be2014-07-09 14:46:31490 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23491 picture_id_sli_ = picture_id;
492 has_received_sli_ = true;
niklase@google.com470e71d2011-07-07 08:21:25493}
494
andrew@webrtc.org96636862012-09-20 23:33:17495void ViEEncoder::OnReceivedRPSI(uint32_t /*ssrc*/,
496 uint64_t picture_id) {
stefan@webrtc.org7af12be2014-07-09 14:46:31497 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23498 picture_id_rpsi_ = picture_id;
499 has_received_rpsi_ = true;
niklase@google.com470e71d2011-07-07 08:21:25500}
501
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29502void ViEEncoder::OnReceivedIntraFrameRequest(uint32_t ssrc) {
mflodman@webrtc.org84d17832011-12-01 17:02:23503 // Key frame request from remote side, signal to VCM.
justinlin@chromium.org7bfb3a32013-05-13 22:59:00504 TRACE_EVENT0("webrtc", "OnKeyFrameRequest");
pwestin@webrtc.org1da1ce02011-10-13 15:19:55505
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29506 int idx = 0;
507 {
508 CriticalSectionScoped cs(data_cs_.get());
Peter Boström5cb9ce42015-05-05 13:16:30509 auto stream_it = ssrc_streams_.find(ssrc);
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29510 if (stream_it == ssrc_streams_.end()) {
mflodman@webrtc.orgd73527c2012-12-20 09:26:17511 LOG_F(LS_WARNING) << "ssrc not found: " << ssrc << ", map size "
512 << ssrc_streams_.size();
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29513 return;
514 }
pbos@webrtc.orgb238d122013-04-09 13:41:51515 std::map<unsigned int, int64_t>::iterator time_it =
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29516 time_last_intra_request_ms_.find(ssrc);
517 if (time_it == time_last_intra_request_ms_.end()) {
518 time_last_intra_request_ms_[ssrc] = 0;
519 }
520
pbos@webrtc.orgb238d122013-04-09 13:41:51521 int64_t now = TickTime::MillisecondTimestamp();
mflodmanc4a1c372015-11-06 12:33:51522 if (time_last_intra_request_ms_[ssrc] + kMinKeyFrameRequestIntervalMs
523 > now) {
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29524 return;
525 }
526 time_last_intra_request_ms_[ssrc] = now;
527 idx = stream_it->second;
mflodman@webrtc.org84d17832011-12-01 17:02:23528 }
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29529 // Release the critsect before triggering key frame.
mflodmanfcf54bd2015-04-14 19:28:08530 vcm_->IntraFrameRequest(idx);
niklase@google.com470e71d2011-07-07 08:21:25531}
532
mflodman@webrtc.orgaca26292012-10-05 16:17:41533void ViEEncoder::OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc) {
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29534 CriticalSectionScoped cs(data_cs_.get());
535 std::map<unsigned int, int>::iterator it = ssrc_streams_.find(old_ssrc);
536 if (it == ssrc_streams_.end()) {
537 return;
538 }
539
540 ssrc_streams_[new_ssrc] = it->second;
541 ssrc_streams_.erase(it);
542
543 std::map<unsigned int, int64_t>::iterator time_it =
544 time_last_intra_request_ms_.find(old_ssrc);
545 int64_t last_intra_request_ms = 0;
546 if (time_it != time_last_intra_request_ms_.end()) {
547 last_intra_request_ms = time_it->second;
548 time_last_intra_request_ms_.erase(time_it);
549 }
550 time_last_intra_request_ms_[new_ssrc] = last_intra_request_ms;
551}
552
Peter Boströmd153a372015-11-10 15:27:12553void ViEEncoder::SetSsrcs(const std::vector<uint32_t>& ssrcs) {
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29554 CriticalSectionScoped cs(data_cs_.get());
555 ssrc_streams_.clear();
556 time_last_intra_request_ms_.clear();
557 int idx = 0;
Peter Boström5cb9ce42015-05-05 13:16:30558 for (uint32_t ssrc : ssrcs) {
559 ssrc_streams_[ssrc] = idx++;
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29560 }
mflodman@webrtc.orgaca26292012-10-05 16:17:41561}
562
pbos@webrtc.org3349ae02014-03-13 12:52:27563void ViEEncoder::SetMinTransmitBitrate(int min_transmit_bitrate_kbps) {
564 assert(min_transmit_bitrate_kbps >= 0);
565 CriticalSectionScoped crit(data_cs_.get());
566 min_transmit_bitrate_kbps_ = min_transmit_bitrate_kbps;
567}
568
pwestin@webrtc.org49888ce2012-04-27 05:25:53569// Called from ViEBitrateObserver.
stefan@webrtc.orgedeea912014-12-08 19:46:23570void ViEEncoder::OnNetworkChanged(uint32_t bitrate_bps,
571 uint8_t fraction_lost,
pkasting@chromium.org16825b12015-01-12 21:51:21572 int64_t round_trip_time_ms) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31573 LOG(LS_VERBOSE) << "OnNetworkChanged, bitrate" << bitrate_bps
mflodman8602a3d2015-05-20 22:54:42574 << " packet loss " << static_cast<int>(fraction_lost)
mflodman@webrtc.org5574dac2014-04-07 10:56:31575 << " rtt " << round_trip_time_ms;
henrikg91d6ede2015-09-17 07:24:34576 RTC_DCHECK(send_payload_router_ != NULL);
mflodmanfcf54bd2015-04-14 19:28:08577 vcm_->SetChannelParameters(bitrate_bps, fraction_lost, round_trip_time_ms);
578 bool video_is_suspended = vcm_->VideoSuspended();
Peter Boströmd153a372015-11-10 15:27:12579 bool video_suspension_changed;
stefan@webrtc.org508a84b2013-06-17 12:53:37580 VideoCodec send_codec;
Peter Boströmd153a372015-11-10 15:27:12581 uint32_t first_ssrc;
582 {
583 CriticalSectionScoped cs(data_cs_.get());
584 last_observed_bitrate_bps_ = bitrate_bps;
585 video_suspension_changed = video_suspended_ != video_is_suspended;
586 video_suspended_ = video_is_suspended;
587 send_codec = encoder_config_;
588 first_ssrc = ssrc_streams_.begin()->first;
stefan@webrtc.org508a84b2013-06-17 12:53:37589 }
Peter Boströmd153a372015-11-10 15:27:12590
stefan@webrtc.orgb2c8a952013-09-06 13:58:01591 SimulcastStream* stream_configs = send_codec.simulcastStream;
592 // Allocate the bandwidth between the streams.
593 std::vector<uint32_t> stream_bitrates = AllocateStreamBitrates(
stefan@webrtc.org792f1a12015-03-04 12:24:26594 bitrate_bps, stream_configs, send_codec.numberOfSimulcastStreams);
595 send_payload_router_->SetTargetSendBitrates(stream_bitrates);
pbos@webrtc.org709e2972014-03-19 10:59:52596
Peter Boströmd153a372015-11-10 15:27:12597 if (!video_suspension_changed)
598 return;
Peter Boström7083e112015-09-22 14:28:51599 // Video suspend-state changed, inform codec observer.
Peter Boströmd153a372015-11-10 15:27:12600 LOG(LS_INFO) << "Video suspend state changed " << video_is_suspended
601 << " for ssrc " << first_ssrc;
Peter Boström7083e112015-09-22 14:28:51602 if (stats_proxy_)
603 stats_proxy_->OnSuspendChange(video_is_suspended);
niklase@google.com470e71d2011-07-07 08:21:25604}
605
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43606void ViEEncoder::SuspendBelowMinBitrate() {
mflodmanfcf54bd2015-04-14 19:28:08607 vcm_->SuspendBelowMinBitrate();
stefan@webrtc.org792f1a12015-03-04 12:24:26608 bitrate_allocator_->EnforceMinBitrate(false);
henrik.lundin@webrtc.org7ea4f242013-10-02 13:34:26609}
610
sprang@webrtc.org40709352013-11-26 11:41:59611void ViEEncoder::RegisterPostEncodeImageCallback(
612 EncodedImageCallback* post_encode_callback) {
mflodmanfcf54bd2015-04-14 19:28:08613 vcm_->RegisterPostEncodeImageCallback(post_encode_callback);
sprang@webrtc.org40709352013-11-26 11:41:59614}
615
mflodmana8565422015-12-07 09:09:52616QMVideoSettingsCallback::QMVideoSettingsCallback(VideoProcessing* vpm)
617 : vp_(vpm) {
mflodman@webrtc.org84d17832011-12-01 17:02:23618}
niklase@google.com470e71d2011-07-07 08:21:25619
stefan@webrtc.org439be292012-02-16 14:45:37620QMVideoSettingsCallback::~QMVideoSettingsCallback() {
mflodman@webrtc.org84d17832011-12-01 17:02:23621}
622
pbos@webrtc.orgb238d122013-04-09 13:41:51623int32_t QMVideoSettingsCallback::SetVideoQMSettings(
624 const uint32_t frame_rate,
625 const uint32_t width,
626 const uint32_t height) {
mflodmana8565422015-12-07 09:09:52627 return vp_->SetTargetResolution(width, height, frame_rate);
mflodman@webrtc.org84d17832011-12-01 17:02:23628}
629
jackychen6e2ce6e2015-07-13 23:26:33630void QMVideoSettingsCallback::SetTargetFramerate(int frame_rate) {
mflodmana8565422015-12-07 09:09:52631 vp_->SetTargetFramerate(frame_rate);
jackychen6e2ce6e2015-07-13 23:26:33632}
633
mflodman@webrtc.org84d17832011-12-01 17:02:23634} // namespace webrtc