blob: 1d6b81611da0310a335f5a85ca5734d9539ba31f [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
sprang@webrtc.org40709352013-11-26 11:41:59 +000017#include "webrtc/common_video/interface/video_image.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000018#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
19#include "webrtc/modules/pacing/include/paced_sender.h"
20#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
21#include "webrtc/modules/utility/interface/process_thread.h"
22#include "webrtc/modules/video_coding/codecs/interface/video_codec_interface.h"
23#include "webrtc/modules/video_coding/main/interface/video_coding.h"
24#include "webrtc/modules/video_coding/main/interface/video_coding_defines.h"
sprang@webrtc.org40709352013-11-26 11:41:59 +000025#include "webrtc/modules/video_coding/main/source/encoded_frame.h"
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000026#include "webrtc/system_wrappers/interface/clock.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000027#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
28#include "webrtc/system_wrappers/interface/logging.h"
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +000029#include "webrtc/system_wrappers/interface/metrics.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000030#include "webrtc/system_wrappers/interface/tick_util.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000031#include "webrtc/system_wrappers/interface/trace_event.h"
32#include "webrtc/video_engine/include/vie_codec.h"
33#include "webrtc/video_engine/include/vie_image_process.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000034#include "webrtc/frame_callback.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.orgbfacda62013-03-27 16:36:01 +000046// Allow packets to be transmitted in up to 2 times max video bitrate if the
47// bandwidth estimate allows it.
48// TODO(holmer): Expose transmission start, min and max bitrates in the
49// VideoEngine API and remove the kTransmissionMaxBitrateMultiplier.
50static const int kTransmissionMaxBitrateMultiplier = 2;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000051
stefan@webrtc.org3e005052013-10-18 15:05:29 +000052static const float kStopPaddingThresholdMs = 2000;
53
stefan@webrtc.orgb2c8a952013-09-06 13:58:01 +000054std::vector<uint32_t> AllocateStreamBitrates(
55 uint32_t total_bitrate,
56 const SimulcastStream* stream_configs,
57 size_t number_of_streams) {
58 if (number_of_streams == 0) {
59 std::vector<uint32_t> stream_bitrates(1, 0);
60 stream_bitrates[0] = total_bitrate;
61 return stream_bitrates;
62 }
63 std::vector<uint32_t> stream_bitrates(number_of_streams, 0);
64 uint32_t bitrate_remainder = total_bitrate;
65 for (size_t i = 0; i < stream_bitrates.size() && bitrate_remainder > 0; ++i) {
66 if (stream_configs[i].maxBitrate * 1000 > bitrate_remainder) {
67 stream_bitrates[i] = bitrate_remainder;
68 } else {
69 stream_bitrates[i] = stream_configs[i].maxBitrate * 1000;
70 }
71 bitrate_remainder -= stream_bitrates[i];
72 }
73 return stream_bitrates;
74}
75
stefan@webrtc.org439be292012-02-16 14:45:37 +000076class QMVideoSettingsCallback : public VCMQMSettingsCallback {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000077 public:
marpan@webrtc.orgefd01fd2012-04-18 15:56:34 +000078 explicit QMVideoSettingsCallback(VideoProcessingModule* vpm);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000079
stefan@webrtc.org439be292012-02-16 14:45:37 +000080 ~QMVideoSettingsCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +000081
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000082 // Update VPM with QM (quality modes: frame size & frame rate) settings.
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000083 int32_t SetVideoQMSettings(const uint32_t frame_rate,
84 const uint32_t width,
85 const uint32_t height);
niklase@google.com470e71d2011-07-07 08:21:25 +000086
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000087 private:
88 VideoProcessingModule* vpm_;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000089};
niklase@google.com470e71d2011-07-07 08:21:25 +000090
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000091class ViEBitrateObserver : public BitrateObserver {
92 public:
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +000093 explicit ViEBitrateObserver(ViEEncoder* owner)
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000094 : owner_(owner) {
95 }
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000096 virtual ~ViEBitrateObserver() {}
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000097 // Implements BitrateObserver.
98 virtual void OnNetworkChanged(const uint32_t bitrate_bps,
99 const uint8_t fraction_lost,
100 const uint32_t rtt) {
101 owner_->OnNetworkChanged(bitrate_bps, fraction_lost, rtt);
102 }
103 private:
104 ViEEncoder* owner_;
105};
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000107class ViEPacedSenderCallback : public PacedSender::Callback {
108 public:
109 explicit ViEPacedSenderCallback(ViEEncoder* owner)
110 : owner_(owner) {
111 }
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000112 virtual ~ViEPacedSenderCallback() {}
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000113 virtual bool TimeToSendPacket(uint32_t ssrc,
114 uint16_t sequence_number,
115 int64_t capture_time_ms,
116 bool retransmission) {
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000117 return owner_->TimeToSendPacket(ssrc, sequence_number, capture_time_ms,
118 retransmission);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000119 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000120 virtual size_t TimeToSendPadding(size_t bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000121 return owner_->TimeToSendPadding(bytes);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000122 }
123 private:
124 ViEEncoder* owner_;
125};
126
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000127ViEEncoder::ViEEncoder(int32_t engine_id,
128 int32_t channel_id,
129 uint32_t number_of_cores,
andresp@webrtc.org7707d062013-05-13 10:50:50 +0000130 const Config& config,
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000131 ProcessThread& module_process_thread,
132 BitrateController* bitrate_controller)
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000133 : engine_id_(engine_id),
134 channel_id_(channel_id),
135 number_of_cores_(number_of_cores),
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000136 vcm_(*webrtc::VideoCodingModule::Create()),
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000137 vpm_(*webrtc::VideoProcessingModule::Create(ViEModuleId(engine_id,
138 channel_id))),
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000139 callback_cs_(CriticalSectionWrapper::CreateCriticalSection()),
140 data_cs_(CriticalSectionWrapper::CreateCriticalSection()),
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000141 bitrate_controller_(bitrate_controller),
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000142 time_of_last_incoming_frame_ms_(0),
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000143 send_padding_(false),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000144 min_transmit_bitrate_kbps_(0),
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000145 target_delay_ms_(0),
146 network_is_transmitting_(true),
147 encoder_paused_(false),
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000148 encoder_paused_and_dropped_frame_(false),
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000149 fec_enabled_(false),
150 nack_enabled_(false),
151 codec_observer_(NULL),
152 effect_filter_(NULL),
153 module_process_thread_(module_process_thread),
154 has_received_sli_(false),
155 picture_id_sli_(0),
156 has_received_rpsi_(false),
157 picture_id_rpsi_(0),
henrik.lundin@webrtc.org7ea4f242013-10-02 13:34:26 +0000158 qm_callback_(NULL),
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000159 video_suspended_(false),
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000160 pre_encode_callback_(NULL),
161 start_ms_(Clock::GetRealTimeClock()->TimeInMilliseconds()) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000162 RtpRtcp::Configuration configuration;
163 configuration.id = ViEModuleId(engine_id_, channel_id_);
164 configuration.audio = false; // Video.
165
166 default_rtp_rtcp_.reset(RtpRtcp::CreateRtpRtcp(configuration));
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000167 bitrate_observer_.reset(new ViEBitrateObserver(this));
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000168 pacing_callback_.reset(new ViEPacedSenderCallback(this));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000169 paced_sender_.reset(new PacedSender(
170 Clock::GetRealTimeClock(),
171 pacing_callback_.get(),
172 kDefaultStartBitrateKbps,
173 PacedSender::kDefaultPaceMultiplier * kDefaultStartBitrateKbps,
174 0));
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000175}
176
177bool ViEEncoder::Init() {
178 if (vcm_.InitializeSender() != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000179 return false;
180 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000181 vpm_.EnableTemporalDecimation(true);
182
183 // Enable/disable content analysis: off by default for now.
184 vpm_.EnableContentAnalysis(false);
185
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000186 if (module_process_thread_.RegisterModule(&vcm_) != 0 ||
187 module_process_thread_.RegisterModule(default_rtp_rtcp_.get()) != 0 ||
188 module_process_thread_.RegisterModule(paced_sender_.get()) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000189 return false;
190 }
stefan@webrtc.org97845122012-04-13 07:47:05 +0000191 if (qm_callback_) {
192 delete qm_callback_;
193 }
marpan@webrtc.orgefd01fd2012-04-18 15:56:34 +0000194 qm_callback_ = new QMVideoSettingsCallback(&vpm_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000195
196#ifdef VIDEOCODEC_VP8
andresp@webrtc.orga84b0a62014-08-14 16:46:46 +0000197 VideoCodecType codec_type = webrtc::kVideoCodecVP8;
198#else
199 VideoCodecType codec_type = webrtc::kVideoCodecI420;
200#endif
201
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000202 VideoCodec video_codec;
andresp@webrtc.orga84b0a62014-08-14 16:46:46 +0000203 if (vcm_.Codec(codec_type, &video_codec) != VCM_OK) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000204 return false;
205 }
stefan@webrtc.orgae2563a2014-02-13 13:48:38 +0000206 {
207 CriticalSectionScoped cs(data_cs_.get());
208 send_padding_ = video_codec.numberOfSimulcastStreams > 1;
209 }
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000210 if (vcm_.RegisterSendCodec(&video_codec, number_of_cores_,
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000211 default_rtp_rtcp_->MaxDataPayloadLength()) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000212 return false;
213 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000214 if (default_rtp_rtcp_->RegisterSendPayload(video_codec) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000215 return false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000216 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000217 if (vcm_.RegisterTransportCallback(this) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000218 return false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000219 }
220 if (vcm_.RegisterSendStatisticsCallback(this) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000221 return false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000222 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000223 if (vcm_.RegisterVideoQMCallback(qm_callback_) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000224 return false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000225 }
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000226 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000227}
228
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000229ViEEncoder::~ViEEncoder() {
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000230 UpdateHistograms();
stefan@webrtc.orgbf415082012-11-29 09:18:53 +0000231 if (bitrate_controller_) {
232 bitrate_controller_->RemoveBitrateObserver(bitrate_observer_.get());
233 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000234 module_process_thread_.DeRegisterModule(&vcm_);
235 module_process_thread_.DeRegisterModule(&vpm_);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000236 module_process_thread_.DeRegisterModule(default_rtp_rtcp_.get());
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000237 module_process_thread_.DeRegisterModule(paced_sender_.get());
mflodman@webrtc.org66480932013-03-01 14:51:23 +0000238 VideoCodingModule::Destroy(&vcm_);
239 VideoProcessingModule::Destroy(&vpm_);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000240 delete qm_callback_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000241}
242
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000243void ViEEncoder::UpdateHistograms() {
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000244 int64_t elapsed_sec =
245 (Clock::GetRealTimeClock()->TimeInMilliseconds() - start_ms_) / 1000;
246 if (elapsed_sec < metrics::kMinRunTimeInSeconds) {
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000247 return;
248 }
249 webrtc::VCMFrameCount frames;
250 if (vcm_.SentFrameCount(frames) != VCM_OK) {
251 return;
252 }
253 uint32_t total_frames = frames.numKeyFrames + frames.numDeltaFrames;
254 if (total_frames > 0) {
255 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.KeyFramesSentInPermille",
256 static_cast<int>(
257 (frames.numKeyFrames * 1000.0f / total_frames) + 0.5f));
258 }
259}
260
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000261int ViEEncoder::Owner() const {
262 return channel_id_;
263}
264
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000265void ViEEncoder::SetNetworkTransmissionState(bool is_transmitting) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000266 {
267 CriticalSectionScoped cs(data_cs_.get());
268 network_is_transmitting_ = is_transmitting;
269 }
270 if (is_transmitting) {
271 paced_sender_->Resume();
272 } else {
273 paced_sender_->Pause();
274 }
275}
276
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000277void ViEEncoder::Pause() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000278 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000279 encoder_paused_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000280}
281
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000282void ViEEncoder::Restart() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000283 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000284 encoder_paused_ = false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000285}
286
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000287uint8_t ViEEncoder::NumberOfCodecs() {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000288 return vcm_.NumberOfCodecs();
289}
290
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000291int32_t ViEEncoder::GetCodec(uint8_t list_index, VideoCodec* video_codec) {
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +0000292 if (vcm_.Codec(list_index, video_codec) != 0) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000293 return -1;
294 }
295 return 0;
296}
297
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000298int32_t ViEEncoder::RegisterExternalEncoder(webrtc::VideoEncoder* encoder,
299 uint8_t pl_type,
300 bool internal_source) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000301 if (encoder == NULL)
302 return -1;
303
stefan@webrtc.orgfcd85852013-01-09 08:35:40 +0000304 if (vcm_.RegisterExternalEncoder(encoder, pl_type, internal_source) !=
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000305 VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000306 return -1;
307 }
308 return 0;
309}
310
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000311int32_t ViEEncoder::DeRegisterExternalEncoder(uint8_t pl_type) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000312 webrtc::VideoCodec current_send_codec;
313 if (vcm_.SendCodec(&current_send_codec) == VCM_OK) {
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000314 uint32_t current_bitrate_bps = 0;
315 if (vcm_.Bitrate(&current_bitrate_bps) != 0) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000316 LOG(LS_WARNING) << "Failed to get the current encoder target bitrate.";
stefan@webrtc.org439be292012-02-16 14:45:37 +0000317 }
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000318 current_send_codec.startBitrate = (current_bitrate_bps + 500) / 1000;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000319 }
320
321 if (vcm_.RegisterExternalEncoder(NULL, pl_type) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000322 return -1;
323 }
324
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000325 // If the external encoder is the current send codec, use vcm internal
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000326 // encoder.
327 if (current_send_codec.plType == pl_type) {
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000328 uint16_t max_data_payload_length =
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000329 default_rtp_rtcp_->MaxDataPayloadLength();
stefan@webrtc.orgae2563a2014-02-13 13:48:38 +0000330 {
331 CriticalSectionScoped cs(data_cs_.get());
332 send_padding_ = current_send_codec.numberOfSimulcastStreams > 1;
333 }
fischman@webrtc.org64e04052014-03-07 18:00:05 +0000334 // TODO(mflodman): Unfortunately the VideoCodec that VCM has cached a
335 // raw pointer to an |extra_options| that's long gone. Clearing it here is
336 // a hack to prevent the following code from crashing. This should be fixed
337 // for realz. https://code.google.com/p/chromium/issues/detail?id=348222
338 current_send_codec.extra_options = NULL;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000339 if (vcm_.RegisterSendCodec(&current_send_codec, number_of_cores_,
340 max_data_payload_length) != VCM_OK) {
stefan@webrtc.org4070b1d2014-07-16 11:20:40 +0000341 LOG(LS_INFO) << "De-registered the currently used external encoder ("
342 << static_cast<int>(pl_type) << ") and therefore tried to "
343 << "register the corresponding internal encoder, but none "
344 << "was supported.";
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000345 }
346 }
347 return 0;
348}
349
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000350int32_t ViEEncoder::SetEncoder(const webrtc::VideoCodec& video_codec) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000351 // Setting target width and height for VPM.
352 if (vpm_.SetTargetResolution(video_codec.width, video_codec.height,
353 video_codec.maxFramerate) != VPM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000354 return -1;
355 }
356
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000357 if (default_rtp_rtcp_->RegisterSendPayload(video_codec) != 0) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000358 return -1;
359 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000360 // Convert from kbps to bps.
stefan@webrtc.orgb2c8a952013-09-06 13:58:01 +0000361 std::vector<uint32_t> stream_bitrates = AllocateStreamBitrates(
362 video_codec.startBitrate * 1000,
363 video_codec.simulcastStream,
364 video_codec.numberOfSimulcastStreams);
365 default_rtp_rtcp_->SetTargetSendBitrate(stream_bitrates);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000366
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000367 uint16_t max_data_payload_length =
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000368 default_rtp_rtcp_->MaxDataPayloadLength();
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000369
stefan@webrtc.org9075d512014-02-14 09:45:58 +0000370 {
371 CriticalSectionScoped cs(data_cs_.get());
372 send_padding_ = video_codec.numberOfSimulcastStreams > 1;
373 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000374 if (vcm_.RegisterSendCodec(&video_codec, number_of_cores_,
375 max_data_payload_length) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000376 return -1;
377 }
378
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000379 // Set this module as sending right away, let the slave module in the channel
380 // start and stop sending.
andresp@webrtc.orga84b0a62014-08-14 16:46:46 +0000381 if (default_rtp_rtcp_->SetSendingStatus(true) != 0) {
382 return -1;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000383 }
andresp@webrtc.orga84b0a62014-08-14 16:46:46 +0000384
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000385 bitrate_controller_->SetBitrateObserver(bitrate_observer_.get(),
386 video_codec.startBitrate * 1000,
387 video_codec.minBitrate * 1000,
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000388 kTransmissionMaxBitrateMultiplier *
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000389 video_codec.maxBitrate * 1000);
390
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000391 CriticalSectionScoped crit(data_cs_.get());
392 int pad_up_to_bitrate_kbps = video_codec.startBitrate;
393 if (pad_up_to_bitrate_kbps < min_transmit_bitrate_kbps_)
394 pad_up_to_bitrate_kbps = min_transmit_bitrate_kbps_;
395
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000396 paced_sender_->UpdateBitrate(
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000397 video_codec.startBitrate,
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000398 PacedSender::kDefaultPaceMultiplier * video_codec.startBitrate,
399 pad_up_to_bitrate_kbps);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000400
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000401 return 0;
402}
403
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000404int32_t ViEEncoder::GetEncoder(VideoCodec* video_codec) {
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +0000405 if (vcm_.SendCodec(video_codec) != 0) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000406 return -1;
407 }
408 return 0;
409}
niklase@google.com470e71d2011-07-07 08:21:25 +0000410
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000411int32_t ViEEncoder::GetCodecConfigParameters(
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000412 unsigned char config_parameters[kConfigParameterSize],
413 unsigned char& config_parameters_size) {
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000414 int32_t num_parameters =
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000415 vcm_.CodecConfigParameters(config_parameters, kConfigParameterSize);
416 if (num_parameters <= 0) {
417 config_parameters_size = 0;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000418 return -1;
419 }
420 config_parameters_size = static_cast<unsigned char>(num_parameters);
421 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000422}
423
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000424int32_t ViEEncoder::ScaleInputImage(bool enable) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000425 VideoFrameResampling resampling_mode = kFastRescaling;
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000426 // TODO(mflodman) What?
427 if (enable) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000428 // kInterpolation is currently not supported.
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000429 LOG_F(LS_ERROR) << "Not supported.";
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000430 return -1;
431 }
432 vpm_.SetInputFrameResampleMode(resampling_mode);
niklase@google.com470e71d2011-07-07 08:21:25 +0000433
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000434 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000435}
436
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000437bool ViEEncoder::TimeToSendPacket(uint32_t ssrc,
438 uint16_t sequence_number,
439 int64_t capture_time_ms,
440 bool retransmission) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000441 return default_rtp_rtcp_->TimeToSendPacket(ssrc, sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000442 capture_time_ms, retransmission);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000443}
444
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000445size_t ViEEncoder::TimeToSendPadding(size_t bytes) {
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +0000446 bool send_padding;
447 {
448 CriticalSectionScoped cs(data_cs_.get());
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000449 send_padding =
450 send_padding_ || video_suspended_ || min_transmit_bitrate_kbps_ > 0;
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +0000451 }
452 if (send_padding) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000453 return default_rtp_rtcp_->TimeToSendPadding(bytes);
454 }
455 return 0;
456}
457
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000458bool ViEEncoder::EncoderPaused() const {
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000459 // Pause video if paused by caller or as long as the network is down or the
460 // pacer queue has grown too large in buffered mode.
461 if (encoder_paused_) {
462 return true;
463 }
464 if (target_delay_ms_ > 0) {
465 // Buffered mode.
466 // TODO(pwestin): Workaround until nack is configured as a time and not
467 // number of packets.
468 return paced_sender_->QueueInMs() >=
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000469 std::max(static_cast<int>(target_delay_ms_ * kEncoderPausePacerMargin),
470 kMinPacingDelayMs);
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000471 }
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000472 if (paced_sender_->ExpectedQueueTimeMs() >
473 PacedSender::kDefaultMaxQueueLengthMs) {
474 // Too much data in pacer queue, drop frame.
475 return true;
476 }
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000477 return !network_is_transmitting_;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000478}
479
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000480void ViEEncoder::TraceFrameDropStart() {
481 // Start trace event only on the first frame after encoder is paused.
482 if (!encoder_paused_and_dropped_frame_) {
483 TRACE_EVENT_ASYNC_BEGIN0("webrtc", "EncoderPaused", this);
484 }
485 encoder_paused_and_dropped_frame_ = true;
486 return;
487}
488
489void ViEEncoder::TraceFrameDropEnd() {
490 // End trace event on first frame after encoder resumes, if frame was dropped.
491 if (encoder_paused_and_dropped_frame_) {
492 TRACE_EVENT_ASYNC_END0("webrtc", "EncoderPaused", this);
493 }
494 encoder_paused_and_dropped_frame_ = false;
495}
496
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000497RtpRtcp* ViEEncoder::SendRtpRtcpModule() {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000498 return default_rtp_rtcp_.get();
niklase@google.com470e71d2011-07-07 08:21:25 +0000499}
500
mflodman@webrtc.org8baed512012-06-21 12:11:50 +0000501void ViEEncoder::DeliverFrame(int id,
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000502 I420VideoFrame* video_frame,
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000503 const std::vector<uint32_t>& csrcs) {
wuchengli@chromium.orgac4b87c2014-03-19 03:44:20 +0000504 if (default_rtp_rtcp_->SendingMedia() == false) {
505 // We've paused or we have no channels attached, don't encode.
506 return;
507 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000508 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000509 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000510 time_of_last_incoming_frame_ms_ = TickTime::MillisecondTimestamp();
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000511 if (EncoderPaused()) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000512 TraceFrameDropStart();
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000513 return;
514 }
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000515 TraceFrameDropEnd();
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000516 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000517
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000518 // Convert render time, in ms, to RTP timestamp.
mflodman@webrtc.org8baed512012-06-21 12:11:50 +0000519 const int kMsToRtpTimestamp = 90;
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000520 const uint32_t time_stamp =
mflodman@webrtc.org8baed512012-06-21 12:11:50 +0000521 kMsToRtpTimestamp *
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000522 static_cast<uint32_t>(video_frame->render_time_ms());
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000523
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000524 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame->render_time_ms(),
525 "Encode");
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000526 video_frame->set_timestamp(time_stamp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000527
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000528 // Make sure the CSRC list is correct.
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000529 if (csrcs.size() > 0) {
530 std::vector<uint32_t> temp_csrcs(csrcs.size());
531 for (size_t i = 0; i < csrcs.size(); i++) {
532 if (csrcs[i] == 1) {
533 temp_csrcs[i] = default_rtp_rtcp_->SSRC();
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000534 } else {
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000535 temp_csrcs[i] = csrcs[i];
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000536 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000537 }
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000538 default_rtp_rtcp_->SetCsrcs(temp_csrcs);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000539 }
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000540
pwestin@webrtc.org2f476ed2012-10-30 16:21:52 +0000541 I420VideoFrame* decimated_frame = NULL;
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000542 // TODO(wuchengli): support texture frames.
543 if (video_frame->native_handle() == NULL) {
544 {
545 CriticalSectionScoped cs(callback_cs_.get());
546 if (effect_filter_) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000547 size_t length =
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000548 CalcBufferSize(kI420, video_frame->width(), video_frame->height());
549 scoped_ptr<uint8_t[]> video_buffer(new uint8_t[length]);
550 ExtractBuffer(*video_frame, length, video_buffer.get());
551 effect_filter_->Transform(length,
552 video_buffer.get(),
553 video_frame->ntp_time_ms(),
554 video_frame->timestamp(),
555 video_frame->width(),
556 video_frame->height());
557 }
558 }
559
560 // Pass frame via preprocessor.
561 const int ret = vpm_.PreprocessFrame(*video_frame, &decimated_frame);
562 if (ret == 1) {
563 // Drop this frame.
564 return;
565 }
566 if (ret != VPM_OK) {
567 return;
568 }
pwestin@webrtc.org2f476ed2012-10-30 16:21:52 +0000569 }
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000570 // If the frame was not resampled or scaled => use original.
pwestin@webrtc.org2f476ed2012-10-30 16:21:52 +0000571 if (decimated_frame == NULL) {
572 decimated_frame = video_frame;
573 }
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000574
575 {
576 CriticalSectionScoped cs(callback_cs_.get());
577 if (pre_encode_callback_)
578 pre_encode_callback_->FrameCallback(decimated_frame);
579 }
580
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000581 if (video_frame->native_handle() != NULL) {
582 // TODO(wuchengli): add texture support. http://crbug.com/362437
583 return;
584 }
585
niklase@google.com470e71d2011-07-07 08:21:25 +0000586#ifdef VIDEOCODEC_VP8
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000587 if (vcm_.SendCodec() == webrtc::kVideoCodecVP8) {
588 webrtc::CodecSpecificInfo codec_specific_info;
589 codec_specific_info.codecType = webrtc::kVideoCodecVP8;
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000590 {
591 CriticalSectionScoped cs(data_cs_.get());
592 codec_specific_info.codecSpecific.VP8.hasReceivedRPSI =
593 has_received_rpsi_;
594 codec_specific_info.codecSpecific.VP8.hasReceivedSLI =
595 has_received_sli_;
596 codec_specific_info.codecSpecific.VP8.pictureIdRPSI =
597 picture_id_rpsi_;
598 codec_specific_info.codecSpecific.VP8.pictureIdSLI =
599 picture_id_sli_;
600 has_received_sli_ = false;
601 has_received_rpsi_ = false;
602 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000603
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000604 vcm_.AddVideoFrame(*decimated_frame, vpm_.ContentMetrics(),
605 &codec_specific_info);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000606 return;
607 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000608#endif
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000609 vcm_.AddVideoFrame(*decimated_frame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000610}
niklase@google.com470e71d2011-07-07 08:21:25 +0000611
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000612void ViEEncoder::DelayChanged(int id, int frame_delay) {
stefan@webrtc.org7da34592013-04-09 14:56:29 +0000613 default_rtp_rtcp_->SetCameraDelay(frame_delay);
niklase@google.com470e71d2011-07-07 08:21:25 +0000614}
niklase@google.com470e71d2011-07-07 08:21:25 +0000615
mflodman@webrtc.org8baed512012-06-21 12:11:50 +0000616int ViEEncoder::GetPreferedFrameSettings(int* width,
617 int* height,
618 int* frame_rate) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000619 webrtc::VideoCodec video_codec;
620 memset(&video_codec, 0, sizeof(video_codec));
621 if (vcm_.SendCodec(&video_codec) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000622 return -1;
623 }
624
mflodman@webrtc.org8baed512012-06-21 12:11:50 +0000625 *width = video_codec.width;
626 *height = video_codec.height;
627 *frame_rate = video_codec.maxFramerate;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000628 return 0;
629}
630
pwestin@webrtc.orgce330352012-04-12 06:59:14 +0000631int ViEEncoder::SendKeyFrame() {
stefan@webrtc.orgc5300432012-10-08 07:06:53 +0000632 return vcm_.IntraFrameRequest(0);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000633}
634
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000635int32_t ViEEncoder::SendCodecStatistics(
636 uint32_t* num_key_frames, uint32_t* num_delta_frames) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000637 webrtc::VCMFrameCount sent_frames;
638 if (vcm_.SentFrameCount(sent_frames) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000639 return -1;
640 }
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +0000641 *num_key_frames = sent_frames.numKeyFrames;
642 *num_delta_frames = sent_frames.numDeltaFrames;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000643 return 0;
644}
645
jiayl@webrtc.org9fd8d872014-02-27 22:32:40 +0000646int32_t ViEEncoder::PacerQueuingDelayMs() const {
647 return paced_sender_->QueueInMs();
648}
649
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000650int ViEEncoder::CodecTargetBitrate(uint32_t* bitrate) const {
stefan@webrtc.org439be292012-02-16 14:45:37 +0000651 if (vcm_.Bitrate(bitrate) != 0)
652 return -1;
653 return 0;
stefan@webrtc.org07b45a52012-02-02 08:37:48 +0000654}
655
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000656int32_t ViEEncoder::UpdateProtectionMethod(bool enable_nack) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000657 bool fec_enabled = false;
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000658 uint8_t dummy_ptype_red = 0;
659 uint8_t dummy_ptypeFEC = 0;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000660
661 // Updated protection method to VCM to get correct packetization sizes.
662 // FEC has larger overhead than NACK -> set FEC if used.
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000663 int32_t error = default_rtp_rtcp_->GenericFECStatus(fec_enabled,
664 dummy_ptype_red,
665 dummy_ptypeFEC);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000666 if (error) {
667 return -1;
668 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000669 if (fec_enabled_ == fec_enabled && nack_enabled_ == enable_nack) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000670 // No change needed, we're already in correct state.
671 return 0;
672 }
673 fec_enabled_ = fec_enabled;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000674 nack_enabled_ = enable_nack;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000675
676 // Set Video Protection for VCM.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000677 if (fec_enabled && nack_enabled_) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000678 vcm_.SetVideoProtection(webrtc::kProtectionNackFEC, true);
679 } else {
680 vcm_.SetVideoProtection(webrtc::kProtectionFEC, fec_enabled_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000681 vcm_.SetVideoProtection(webrtc::kProtectionNackSender, nack_enabled_);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000682 vcm_.SetVideoProtection(webrtc::kProtectionNackFEC, false);
683 }
684
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000685 if (fec_enabled_ || nack_enabled_) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000686 vcm_.RegisterProtectionCallback(this);
687 // The send codec must be registered to set correct MTU.
688 webrtc::VideoCodec codec;
689 if (vcm_.SendCodec(&codec) == 0) {
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000690 uint16_t max_pay_load = default_rtp_rtcp_->MaxDataPayloadLength();
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000691 uint32_t current_bitrate_bps = 0;
692 if (vcm_.Bitrate(&current_bitrate_bps) != 0) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000693 LOG_F(LS_WARNING) <<
694 "Failed to get the current encoder target bitrate.";
stefan@webrtc.org439be292012-02-16 14:45:37 +0000695 }
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000696 // Convert to start bitrate in kbps.
697 codec.startBitrate = (current_bitrate_bps + 500) / 1000;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000698 if (vcm_.RegisterSendCodec(&codec, number_of_cores_, max_pay_load) != 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000699 return -1;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000700 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000701 }
702 return 0;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000703 } else {
704 // FEC and NACK are disabled.
705 vcm_.RegisterProtectionCallback(NULL);
706 }
707 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000708}
709
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000710void ViEEncoder::SetSenderBufferingMode(int target_delay_ms) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000711 {
712 CriticalSectionScoped cs(data_cs_.get());
713 target_delay_ms_ = target_delay_ms;
714 }
mikhal@webrtc.org3d305c62013-02-10 18:42:55 +0000715 if (target_delay_ms > 0) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000716 // Disable external frame-droppers.
717 vcm_.EnableFrameDropper(false);
718 vpm_.EnableTemporalDecimation(false);
mikhal@webrtc.org3d305c62013-02-10 18:42:55 +0000719 } else {
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000720 // Real-time mode - enable frame droppers.
mikhal@webrtc.org3d305c62013-02-10 18:42:55 +0000721 vpm_.EnableTemporalDecimation(true);
722 vcm_.EnableFrameDropper(true);
723 }
724}
725
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000726int32_t ViEEncoder::SendData(
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000727 const FrameType frame_type,
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000728 const uint8_t payload_type,
729 const uint32_t time_stamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000730 int64_t capture_time_ms,
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000731 const uint8_t* payload_data,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000732 const size_t payload_size,
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000733 const webrtc::RTPFragmentationHeader& fragmentation_header,
734 const RTPVideoHeader* rtp_video_hdr) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000735 // New encoded data, hand over to the rtp module.
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000736 return default_rtp_rtcp_->SendOutgoingData(frame_type,
737 payload_type,
738 time_stamp,
739 capture_time_ms,
740 payload_data,
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000741 payload_size,
742 &fragmentation_header,
743 rtp_video_hdr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000744}
745
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000746int32_t ViEEncoder::ProtectionRequest(
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000747 const FecProtectionParams* delta_fec_params,
748 const FecProtectionParams* key_fec_params,
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000749 uint32_t* sent_video_rate_bps,
750 uint32_t* sent_nack_rate_bps,
751 uint32_t* sent_fec_rate_bps) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000752 default_rtp_rtcp_->SetFecParameters(delta_fec_params, key_fec_params);
753 default_rtp_rtcp_->BitrateSent(NULL, sent_video_rate_bps, sent_fec_rate_bps,
stefan@webrtc.orgf4c82862011-12-13 15:38:14 +0000754 sent_nack_rate_bps);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000755 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000756}
757
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000758int32_t ViEEncoder::SendStatistics(const uint32_t bit_rate,
759 const uint32_t frame_rate) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000760 CriticalSectionScoped cs(callback_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000761 if (codec_observer_) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000762 codec_observer_->OutgoingRate(channel_id_, frame_rate, bit_rate);
763 }
764 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000765}
766
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000767int32_t ViEEncoder::RegisterCodecObserver(ViEEncoderObserver* observer) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000768 CriticalSectionScoped cs(callback_cs_.get());
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000769 if (observer && codec_observer_) {
770 LOG_F(LS_ERROR) << "Observer already set.";
771 return -1;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000772 }
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000773 codec_observer_ = observer;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000774 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000775}
776
andrew@webrtc.org96636862012-09-20 23:33:17 +0000777void ViEEncoder::OnReceivedSLI(uint32_t /*ssrc*/,
778 uint8_t picture_id) {
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000779 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000780 picture_id_sli_ = picture_id;
781 has_received_sli_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000782}
783
andrew@webrtc.org96636862012-09-20 23:33:17 +0000784void ViEEncoder::OnReceivedRPSI(uint32_t /*ssrc*/,
785 uint64_t picture_id) {
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000786 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000787 picture_id_rpsi_ = picture_id;
788 has_received_rpsi_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000789}
790
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000791void ViEEncoder::OnReceivedIntraFrameRequest(uint32_t ssrc) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000792 // Key frame request from remote side, signal to VCM.
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000793 TRACE_EVENT0("webrtc", "OnKeyFrameRequest");
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000794
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000795 int idx = 0;
796 {
797 CriticalSectionScoped cs(data_cs_.get());
798 std::map<unsigned int, int>::iterator stream_it = ssrc_streams_.find(ssrc);
799 if (stream_it == ssrc_streams_.end()) {
mflodman@webrtc.orgd73527c2012-12-20 09:26:17 +0000800 LOG_F(LS_WARNING) << "ssrc not found: " << ssrc << ", map size "
801 << ssrc_streams_.size();
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000802 return;
803 }
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000804 std::map<unsigned int, int64_t>::iterator time_it =
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000805 time_last_intra_request_ms_.find(ssrc);
806 if (time_it == time_last_intra_request_ms_.end()) {
807 time_last_intra_request_ms_[ssrc] = 0;
808 }
809
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000810 int64_t now = TickTime::MillisecondTimestamp();
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000811 if (time_last_intra_request_ms_[ssrc] + kViEMinKeyRequestIntervalMs > now) {
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000812 return;
813 }
814 time_last_intra_request_ms_[ssrc] = now;
815 idx = stream_it->second;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000816 }
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000817 // Release the critsect before triggering key frame.
818 vcm_.IntraFrameRequest(idx);
niklase@google.com470e71d2011-07-07 08:21:25 +0000819}
820
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000821void ViEEncoder::OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc) {
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000822 CriticalSectionScoped cs(data_cs_.get());
823 std::map<unsigned int, int>::iterator it = ssrc_streams_.find(old_ssrc);
824 if (it == ssrc_streams_.end()) {
825 return;
826 }
827
828 ssrc_streams_[new_ssrc] = it->second;
829 ssrc_streams_.erase(it);
830
831 std::map<unsigned int, int64_t>::iterator time_it =
832 time_last_intra_request_ms_.find(old_ssrc);
833 int64_t last_intra_request_ms = 0;
834 if (time_it != time_last_intra_request_ms_.end()) {
835 last_intra_request_ms = time_it->second;
836 time_last_intra_request_ms_.erase(time_it);
837 }
838 time_last_intra_request_ms_[new_ssrc] = last_intra_request_ms;
839}
840
841bool ViEEncoder::SetSsrcs(const std::list<unsigned int>& ssrcs) {
842 VideoCodec codec;
843 if (vcm_.SendCodec(&codec) != 0)
844 return false;
845
846 if (codec.numberOfSimulcastStreams > 0 &&
847 ssrcs.size() != codec.numberOfSimulcastStreams) {
848 return false;
849 }
850
851 CriticalSectionScoped cs(data_cs_.get());
852 ssrc_streams_.clear();
853 time_last_intra_request_ms_.clear();
854 int idx = 0;
855 for (std::list<unsigned int>::const_iterator it = ssrcs.begin();
856 it != ssrcs.end(); ++it, ++idx) {
857 unsigned int ssrc = *it;
858 ssrc_streams_[ssrc] = idx;
859 }
860 return true;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000861}
862
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000863void ViEEncoder::SetMinTransmitBitrate(int min_transmit_bitrate_kbps) {
864 assert(min_transmit_bitrate_kbps >= 0);
865 CriticalSectionScoped crit(data_cs_.get());
866 min_transmit_bitrate_kbps_ = min_transmit_bitrate_kbps;
867}
868
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000869// Called from ViEBitrateObserver.
870void ViEEncoder::OnNetworkChanged(const uint32_t bitrate_bps,
871 const uint8_t fraction_lost,
872 const uint32_t round_trip_time_ms) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000873 LOG(LS_VERBOSE) << "OnNetworkChanged, bitrate" << bitrate_bps
874 << " packet loss " << fraction_lost
875 << " rtt " << round_trip_time_ms;
stefan@webrtc.orgabc9d5b2013-03-18 17:00:51 +0000876 vcm_.SetChannelParameters(bitrate_bps, fraction_lost, round_trip_time_ms);
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000877 bool video_is_suspended = vcm_.VideoSuspended();
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000878 int bitrate_kbps = bitrate_bps / 1000;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000879 VideoCodec send_codec;
880 if (vcm_.SendCodec(&send_codec) != 0) {
881 return;
882 }
stefan@webrtc.orgb2c8a952013-09-06 13:58:01 +0000883 SimulcastStream* stream_configs = send_codec.simulcastStream;
884 // Allocate the bandwidth between the streams.
885 std::vector<uint32_t> stream_bitrates = AllocateStreamBitrates(
886 bitrate_bps,
887 stream_configs,
888 send_codec.numberOfSimulcastStreams);
889 // Find the max amount of padding we can allow ourselves to send at this
890 // point, based on which streams are currently active and what our current
891 // available bandwidth is.
stefan@webrtc.orgb400aa72013-10-16 13:03:10 +0000892 int pad_up_to_bitrate_kbps = 0;
893 if (send_codec.numberOfSimulcastStreams == 0) {
stefan@webrtc.orgb400aa72013-10-16 13:03:10 +0000894 pad_up_to_bitrate_kbps = send_codec.minBitrate;
895 } else {
stefan@webrtc.orgb400aa72013-10-16 13:03:10 +0000896 pad_up_to_bitrate_kbps =
897 stream_configs[send_codec.numberOfSimulcastStreams - 1].minBitrate;
898 for (int i = 0; i < send_codec.numberOfSimulcastStreams - 1; ++i) {
899 pad_up_to_bitrate_kbps += stream_configs[i].targetBitrate;
900 }
stefan@webrtc.orgb2c8a952013-09-06 13:58:01 +0000901 }
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000902
903 // Disable padding if only sending one stream and video isn't suspended and
904 // min-transmit bitrate isn't used (applied later).
905 if (!video_is_suspended && send_codec.numberOfSimulcastStreams <= 1)
stefan@webrtc.orgb400aa72013-10-16 13:03:10 +0000906 pad_up_to_bitrate_kbps = 0;
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000907
908 {
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000909 CriticalSectionScoped cs(data_cs_.get());
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000910 // The amount of padding should decay to zero if no frames are being
911 // captured unless a min-transmit bitrate is used.
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000912 int64_t now_ms = TickTime::MillisecondTimestamp();
913 if (now_ms - time_of_last_incoming_frame_ms_ > kStopPaddingThresholdMs)
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000914 pad_up_to_bitrate_kbps = 0;
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000915
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000916 // Pad up to min bitrate.
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000917 if (pad_up_to_bitrate_kbps < min_transmit_bitrate_kbps_)
918 pad_up_to_bitrate_kbps = min_transmit_bitrate_kbps_;
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000919
920 // Padding may never exceed bitrate estimate.
921 if (pad_up_to_bitrate_kbps > bitrate_kbps)
922 pad_up_to_bitrate_kbps = bitrate_kbps;
923
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000924 paced_sender_->UpdateBitrate(
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000925 bitrate_kbps,
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000926 PacedSender::kDefaultPaceMultiplier * bitrate_kbps,
927 pad_up_to_bitrate_kbps);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000928 default_rtp_rtcp_->SetTargetSendBitrate(stream_bitrates);
pbos@webrtc.org484ee962013-11-21 18:44:23 +0000929 if (video_suspended_ == video_is_suspended)
930 return;
931 video_suspended_ = video_is_suspended;
932 }
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000933
934 // Video suspend-state changed, inform codec observer.
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000935 CriticalSectionScoped crit(callback_cs_.get());
pbos@webrtc.org484ee962013-11-21 18:44:23 +0000936 if (codec_observer_) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000937 LOG(LS_INFO) << "Video suspended " << video_is_suspended
938 << " for channel " << channel_id_;
henrik.lundin@webrtc.org9fe36032013-11-21 23:00:40 +0000939 codec_observer_->SuspendChange(channel_id_, video_is_suspended);
henrik.lundin@webrtc.org7ea4f242013-10-02 13:34:26 +0000940 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000941}
942
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000943PacedSender* ViEEncoder::GetPacedSender() {
944 return paced_sender_.get();
945}
946
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000947int32_t ViEEncoder::RegisterEffectFilter(ViEEffectFilter* effect_filter) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000948 CriticalSectionScoped cs(callback_cs_.get());
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000949 if (effect_filter != NULL && effect_filter_ != NULL) {
950 LOG_F(LS_ERROR) << "Filter already set.";
951 return -1;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000952 }
953 effect_filter_ = effect_filter;
954 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000955}
956
mikhal@webrtc.orge41bbdf2012-08-28 16:15:16 +0000957int ViEEncoder::StartDebugRecording(const char* fileNameUTF8) {
958 return vcm_.StartDebugRecording(fileNameUTF8);
959}
960
961int ViEEncoder::StopDebugRecording() {
962 return vcm_.StopDebugRecording();
963}
964
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000965void ViEEncoder::SuspendBelowMinBitrate() {
966 vcm_.SuspendBelowMinBitrate();
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +0000967 bitrate_controller_->EnforceMinBitrate(false);
henrik.lundin@webrtc.org7ea4f242013-10-02 13:34:26 +0000968}
969
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000970void ViEEncoder::RegisterPreEncodeCallback(
971 I420FrameCallback* pre_encode_callback) {
972 CriticalSectionScoped cs(callback_cs_.get());
973 pre_encode_callback_ = pre_encode_callback;
974}
975
976void ViEEncoder::DeRegisterPreEncodeCallback() {
977 CriticalSectionScoped cs(callback_cs_.get());
978 pre_encode_callback_ = NULL;
979}
980
sprang@webrtc.org40709352013-11-26 11:41:59 +0000981void ViEEncoder::RegisterPostEncodeImageCallback(
982 EncodedImageCallback* post_encode_callback) {
983 vcm_.RegisterPostEncodeImageCallback(post_encode_callback);
984}
985
986void ViEEncoder::DeRegisterPostEncodeImageCallback() {
987 vcm_.RegisterPostEncodeImageCallback(NULL);
988}
989
marpan@webrtc.orgefd01fd2012-04-18 15:56:34 +0000990QMVideoSettingsCallback::QMVideoSettingsCallback(VideoProcessingModule* vpm)
991 : vpm_(vpm) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000992}
niklase@google.com470e71d2011-07-07 08:21:25 +0000993
stefan@webrtc.org439be292012-02-16 14:45:37 +0000994QMVideoSettingsCallback::~QMVideoSettingsCallback() {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000995}
996
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000997int32_t QMVideoSettingsCallback::SetVideoQMSettings(
998 const uint32_t frame_rate,
999 const uint32_t width,
1000 const uint32_t height) {
marpan@webrtc.orgcf706c22012-03-27 21:04:13 +00001001 return vpm_->SetTargetResolution(width, height, frame_rate);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +00001002}
1003
mflodman@webrtc.org84d17832011-12-01 17:02:23 +00001004} // namespace webrtc