blob: e5f6b96d0f5ed455d7a8e32542d16eacdf2653c0 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
stefan@webrtc.org07b45a52012-02-02 08:37:48 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000011#include "webrtc/video_engine/vie_encoder.h"
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h>
14
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +000015#include <algorithm>
niklase@google.com470e71d2011-07-07 08:21:25 +000016
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000017#include "webrtc/base/checks.h"
sprang@webrtc.org40709352013-11-26 11:41:59 +000018#include "webrtc/common_video/interface/video_image.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000019#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
pbos@webrtc.org273a4142014-12-01 15:23:21 +000020#include "webrtc/frame_callback.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000021#include "webrtc/modules/pacing/include/paced_sender.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000022#include "webrtc/modules/utility/interface/process_thread.h"
23#include "webrtc/modules/video_coding/codecs/interface/video_codec_interface.h"
24#include "webrtc/modules/video_coding/main/interface/video_coding.h"
25#include "webrtc/modules/video_coding/main/interface/video_coding_defines.h"
sprang@webrtc.org40709352013-11-26 11:41:59 +000026#include "webrtc/modules/video_coding/main/source/encoded_frame.h"
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000027#include "webrtc/system_wrappers/interface/clock.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000028#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
29#include "webrtc/system_wrappers/interface/logging.h"
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +000030#include "webrtc/system_wrappers/interface/metrics.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000031#include "webrtc/system_wrappers/interface/tick_util.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000032#include "webrtc/system_wrappers/interface/trace_event.h"
pbos@webrtc.org273a4142014-12-01 15:23:21 +000033#include "webrtc/video/send_statistics_proxy.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000034#include "webrtc/video_engine/include/vie_codec.h"
35#include "webrtc/video_engine/include/vie_image_process.h"
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000036#include "webrtc/video_engine/payload_router.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000037#include "webrtc/video_engine/vie_defines.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000038
niklase@google.com470e71d2011-07-07 08:21:25 +000039namespace webrtc {
40
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +000041// Margin on when we pause the encoder when the pacing buffer overflows relative
42// to the configured buffer delay.
43static const float kEncoderPausePacerMargin = 2.0f;
44
pwestin@webrtc.org91563e42013-04-25 22:20:08 +000045// Don't stop the encoder unless the delay is above this configured value.
46static const int kMinPacingDelayMs = 200;
47
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +000048// Allow packets to be transmitted in up to 2 times max video bitrate if the
49// bandwidth estimate allows it.
50// TODO(holmer): Expose transmission start, min and max bitrates in the
51// VideoEngine API and remove the kTransmissionMaxBitrateMultiplier.
52static const int kTransmissionMaxBitrateMultiplier = 2;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000053
stefan@webrtc.org3e005052013-10-18 15:05:29 +000054static const float kStopPaddingThresholdMs = 2000;
55
stefan@webrtc.orgb2c8a952013-09-06 13:58:01 +000056std::vector<uint32_t> AllocateStreamBitrates(
57 uint32_t total_bitrate,
58 const SimulcastStream* stream_configs,
59 size_t number_of_streams) {
60 if (number_of_streams == 0) {
61 std::vector<uint32_t> stream_bitrates(1, 0);
62 stream_bitrates[0] = total_bitrate;
63 return stream_bitrates;
64 }
65 std::vector<uint32_t> stream_bitrates(number_of_streams, 0);
66 uint32_t bitrate_remainder = total_bitrate;
67 for (size_t i = 0; i < stream_bitrates.size() && bitrate_remainder > 0; ++i) {
68 if (stream_configs[i].maxBitrate * 1000 > bitrate_remainder) {
69 stream_bitrates[i] = bitrate_remainder;
70 } else {
71 stream_bitrates[i] = stream_configs[i].maxBitrate * 1000;
72 }
73 bitrate_remainder -= stream_bitrates[i];
74 }
75 return stream_bitrates;
76}
77
stefan@webrtc.org439be292012-02-16 14:45:37 +000078class QMVideoSettingsCallback : public VCMQMSettingsCallback {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000079 public:
marpan@webrtc.orgefd01fd2012-04-18 15:56:34 +000080 explicit QMVideoSettingsCallback(VideoProcessingModule* vpm);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000081
stefan@webrtc.org439be292012-02-16 14:45:37 +000082 ~QMVideoSettingsCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +000083
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000084 // Update VPM with QM (quality modes: frame size & frame rate) settings.
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000085 int32_t SetVideoQMSettings(const uint32_t frame_rate,
86 const uint32_t width,
87 const uint32_t height);
niklase@google.com470e71d2011-07-07 08:21:25 +000088
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000089 private:
90 VideoProcessingModule* vpm_;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000091};
niklase@google.com470e71d2011-07-07 08:21:25 +000092
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000093class ViEBitrateObserver : public BitrateObserver {
94 public:
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +000095 explicit ViEBitrateObserver(ViEEncoder* owner)
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000096 : owner_(owner) {
97 }
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000098 virtual ~ViEBitrateObserver() {}
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000099 // Implements BitrateObserver.
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000100 virtual void OnNetworkChanged(uint32_t bitrate_bps,
101 uint8_t fraction_lost,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000102 int64_t rtt) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000103 owner_->OnNetworkChanged(bitrate_bps, fraction_lost, rtt);
104 }
105 private:
106 ViEEncoder* owner_;
107};
niklase@google.com470e71d2011-07-07 08:21:25 +0000108
mflodman@webrtc.org290cb562015-02-17 10:15:06 +0000109// TODO(mflodman): Move this observer to PayloadRouter class.
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000110class ViEPacedSenderCallback : public PacedSender::Callback {
111 public:
112 explicit ViEPacedSenderCallback(ViEEncoder* owner)
113 : owner_(owner) {
114 }
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000115 virtual ~ViEPacedSenderCallback() {}
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000116 virtual bool TimeToSendPacket(uint32_t ssrc,
117 uint16_t sequence_number,
118 int64_t capture_time_ms,
119 bool retransmission) {
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000120 return owner_->TimeToSendPacket(ssrc, sequence_number, capture_time_ms,
121 retransmission);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000122 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000123 virtual size_t TimeToSendPadding(size_t bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000124 return owner_->TimeToSendPadding(bytes);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000125 }
126 private:
127 ViEEncoder* owner_;
128};
129
mflodman@webrtc.org9dd0ebc2015-02-26 12:57:47 +0000130ViEEncoder::ViEEncoder(int32_t channel_id,
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000131 uint32_t number_of_cores,
andresp@webrtc.org7707d062013-05-13 10:50:50 +0000132 const Config& config,
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000133 ProcessThread& module_process_thread,
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000134 BitrateAllocator* bitrate_allocator,
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000135 BitrateController* bitrate_controller,
136 bool disable_default_encoder)
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000137 : channel_id_(channel_id),
138 number_of_cores_(number_of_cores),
139 disable_default_encoder_(disable_default_encoder),
140 vcm_(*webrtc::VideoCodingModule::Create(this)),
141 vpm_(*webrtc::VideoProcessingModule::Create(ViEModuleId(-1, channel_id))),
142 send_payload_router_(NULL),
143 vcm_protection_callback_(NULL),
144 callback_cs_(CriticalSectionWrapper::CreateCriticalSection()),
145 data_cs_(CriticalSectionWrapper::CreateCriticalSection()),
146 bitrate_allocator_(bitrate_allocator),
147 bitrate_controller_(bitrate_controller),
148 time_of_last_incoming_frame_ms_(0),
149 send_padding_(false),
150 min_transmit_bitrate_kbps_(0),
151 target_delay_ms_(0),
152 network_is_transmitting_(true),
153 encoder_paused_(false),
154 encoder_paused_and_dropped_frame_(false),
155 fec_enabled_(false),
156 nack_enabled_(false),
157 codec_observer_(NULL),
158 effect_filter_(NULL),
159 module_process_thread_(module_process_thread),
160 pacer_thread_(ProcessThread::Create()),
161 has_received_sli_(false),
162 picture_id_sli_(0),
163 has_received_rpsi_(false),
164 picture_id_rpsi_(0),
165 qm_callback_(NULL),
166 video_suspended_(false),
167 pre_encode_callback_(NULL),
168 start_ms_(Clock::GetRealTimeClock()->TimeInMilliseconds()),
169 send_statistics_proxy_(NULL) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000170 bitrate_observer_.reset(new ViEBitrateObserver(this));
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000171 pacing_callback_.reset(new ViEPacedSenderCallback(this));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000172 paced_sender_.reset(new PacedSender(
173 Clock::GetRealTimeClock(),
174 pacing_callback_.get(),
175 kDefaultStartBitrateKbps,
176 PacedSender::kDefaultPaceMultiplier * kDefaultStartBitrateKbps,
177 0));
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000178}
179
180bool ViEEncoder::Init() {
181 if (vcm_.InitializeSender() != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000182 return false;
183 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000184 vpm_.EnableTemporalDecimation(true);
185
186 // Enable/disable content analysis: off by default for now.
187 vpm_.EnableContentAnalysis(false);
188
stefan@webrtc.org97845122012-04-13 07:47:05 +0000189 if (qm_callback_) {
190 delete qm_callback_;
191 }
marpan@webrtc.orgefd01fd2012-04-18 15:56:34 +0000192 qm_callback_ = new QMVideoSettingsCallback(&vpm_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000193
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000194 if (!disable_default_encoder_) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000195#ifdef VIDEOCODEC_VP8
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000196 VideoCodecType codec_type = webrtc::kVideoCodecVP8;
andresp@webrtc.orga84b0a62014-08-14 16:46:46 +0000197#else
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000198 VideoCodecType codec_type = webrtc::kVideoCodecI420;
andresp@webrtc.orga84b0a62014-08-14 16:46:46 +0000199#endif
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000200 VideoCodec video_codec;
201 if (vcm_.Codec(codec_type, &video_codec) != VCM_OK) {
202 return false;
203 }
204 {
205 CriticalSectionScoped cs(data_cs_.get());
206 send_padding_ = video_codec.numberOfSimulcastStreams > 1;
207 }
208 if (vcm_.RegisterSendCodec(&video_codec, number_of_cores_,
mflodman@webrtc.org2bd299a2015-02-13 09:52:01 +0000209 PayloadRouter::DefaultMaxPayloadLength()) != 0) {
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000210 return false;
211 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000212 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000213 if (vcm_.RegisterTransportCallback(this) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000214 return false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000215 }
216 if (vcm_.RegisterSendStatisticsCallback(this) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000217 return false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000218 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000219 if (vcm_.RegisterVideoQMCallback(qm_callback_) != 0) {
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000220 return false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000221 }
wu@webrtc.org5d8c1022012-04-10 16:54:05 +0000222 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000223}
224
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000225void ViEEncoder::StartThreadsAndSetSharedMembers(
226 scoped_refptr<PayloadRouter> send_payload_router,
227 VCMProtectionCallback* vcm_protection_callback) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000228 DCHECK(send_payload_router_ == NULL);
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000229 DCHECK(vcm_protection_callback_ == NULL);
230
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000231 send_payload_router_ = send_payload_router;
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000232 vcm_protection_callback_ = vcm_protection_callback;
mflodman@webrtc.org290cb562015-02-17 10:15:06 +0000233
234 module_process_thread_.RegisterModule(&vcm_);
mflodman@webrtc.org290cb562015-02-17 10:15:06 +0000235 pacer_thread_->RegisterModule(paced_sender_.get());
236 pacer_thread_->Start();
237}
238
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000239void ViEEncoder::StopThreadsAndRemoveSharedMembers() {
240 vcm_.RegisterProtectionCallback(NULL);
241 vcm_protection_callback_ = NULL;
mflodman@webrtc.org290cb562015-02-17 10:15:06 +0000242 pacer_thread_->Stop();
243 pacer_thread_->DeRegisterModule(paced_sender_.get());
244 module_process_thread_.DeRegisterModule(&vcm_);
245 module_process_thread_.DeRegisterModule(&vpm_);
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000246}
247
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000248ViEEncoder::~ViEEncoder() {
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000249 UpdateHistograms();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000250 if (bitrate_allocator_)
251 bitrate_allocator_->RemoveBitrateObserver(bitrate_observer_.get());
mflodman@webrtc.org66480932013-03-01 14:51:23 +0000252 VideoCodingModule::Destroy(&vcm_);
253 VideoProcessingModule::Destroy(&vpm_);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000254 delete qm_callback_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000255}
256
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000257void ViEEncoder::UpdateHistograms() {
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000258 int64_t elapsed_sec =
259 (Clock::GetRealTimeClock()->TimeInMilliseconds() - start_ms_) / 1000;
260 if (elapsed_sec < metrics::kMinRunTimeInSeconds) {
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000261 return;
262 }
263 webrtc::VCMFrameCount frames;
264 if (vcm_.SentFrameCount(frames) != VCM_OK) {
265 return;
266 }
267 uint32_t total_frames = frames.numKeyFrames + frames.numDeltaFrames;
268 if (total_frames > 0) {
269 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.KeyFramesSentInPermille",
270 static_cast<int>(
271 (frames.numKeyFrames * 1000.0f / total_frames) + 0.5f));
272 }
273}
274
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000275int ViEEncoder::Owner() const {
276 return channel_id_;
277}
278
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000279void ViEEncoder::SetNetworkTransmissionState(bool is_transmitting) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000280 {
281 CriticalSectionScoped cs(data_cs_.get());
282 network_is_transmitting_ = is_transmitting;
283 }
284 if (is_transmitting) {
285 paced_sender_->Resume();
286 } else {
287 paced_sender_->Pause();
288 }
289}
290
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000291void ViEEncoder::Pause() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000292 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000293 encoder_paused_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000294}
295
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000296void ViEEncoder::Restart() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000297 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000298 encoder_paused_ = false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000299}
300
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000301uint8_t ViEEncoder::NumberOfCodecs() {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000302 return vcm_.NumberOfCodecs();
303}
304
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000305int32_t ViEEncoder::GetCodec(uint8_t list_index, VideoCodec* video_codec) {
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +0000306 if (vcm_.Codec(list_index, video_codec) != 0) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000307 return -1;
308 }
309 return 0;
310}
311
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000312int32_t ViEEncoder::RegisterExternalEncoder(webrtc::VideoEncoder* encoder,
313 uint8_t pl_type,
314 bool internal_source) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000315 if (encoder == NULL)
316 return -1;
317
stefan@webrtc.orgfcd85852013-01-09 08:35:40 +0000318 if (vcm_.RegisterExternalEncoder(encoder, pl_type, internal_source) !=
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000319 VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000320 return -1;
321 }
322 return 0;
323}
324
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000325int32_t ViEEncoder::DeRegisterExternalEncoder(uint8_t pl_type) {
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000326 DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000327 webrtc::VideoCodec current_send_codec;
328 if (vcm_.SendCodec(&current_send_codec) == VCM_OK) {
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000329 uint32_t current_bitrate_bps = 0;
330 if (vcm_.Bitrate(&current_bitrate_bps) != 0) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000331 LOG(LS_WARNING) << "Failed to get the current encoder target bitrate.";
stefan@webrtc.org439be292012-02-16 14:45:37 +0000332 }
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000333 current_send_codec.startBitrate = (current_bitrate_bps + 500) / 1000;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000334 }
335
336 if (vcm_.RegisterExternalEncoder(NULL, pl_type) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000337 return -1;
338 }
339
pbos@webrtc.org40367f92015-02-13 08:00:06 +0000340 if (disable_default_encoder_)
341 return 0;
342
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000343 // If the external encoder is the current send codec, use vcm internal
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000344 // encoder.
345 if (current_send_codec.plType == pl_type) {
stefan@webrtc.orgae2563a2014-02-13 13:48:38 +0000346 {
347 CriticalSectionScoped cs(data_cs_.get());
348 send_padding_ = current_send_codec.numberOfSimulcastStreams > 1;
349 }
fischman@webrtc.org64e04052014-03-07 18:00:05 +0000350 // TODO(mflodman): Unfortunately the VideoCodec that VCM has cached a
351 // raw pointer to an |extra_options| that's long gone. Clearing it here is
352 // a hack to prevent the following code from crashing. This should be fixed
353 // for realz. https://code.google.com/p/chromium/issues/detail?id=348222
354 current_send_codec.extra_options = NULL;
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000355 size_t max_data_payload_length = send_payload_router_->MaxPayloadLength();
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000356 if (vcm_.RegisterSendCodec(&current_send_codec, number_of_cores_,
357 max_data_payload_length) != VCM_OK) {
stefan@webrtc.org4070b1d2014-07-16 11:20:40 +0000358 LOG(LS_INFO) << "De-registered the currently used external encoder ("
359 << static_cast<int>(pl_type) << ") and therefore tried to "
360 << "register the corresponding internal encoder, but none "
361 << "was supported.";
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000362 }
363 }
364 return 0;
365}
366
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000367int32_t ViEEncoder::SetEncoder(const webrtc::VideoCodec& video_codec) {
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000368 DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000369 // Setting target width and height for VPM.
370 if (vpm_.SetTargetResolution(video_codec.width, video_codec.height,
371 video_codec.maxFramerate) != VPM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000372 return -1;
373 }
374
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000375 // Convert from kbps to bps.
stefan@webrtc.orgb2c8a952013-09-06 13:58:01 +0000376 std::vector<uint32_t> stream_bitrates = AllocateStreamBitrates(
377 video_codec.startBitrate * 1000,
378 video_codec.simulcastStream,
379 video_codec.numberOfSimulcastStreams);
mflodman@webrtc.org50e28162015-02-23 07:45:11 +0000380 send_payload_router_->SetTargetSendBitrates(stream_bitrates);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000381
stefan@webrtc.org9075d512014-02-14 09:45:58 +0000382 {
383 CriticalSectionScoped cs(data_cs_.get());
384 send_padding_ = video_codec.numberOfSimulcastStreams > 1;
385 }
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000386 size_t max_data_payload_length = send_payload_router_->MaxPayloadLength();
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000387 if (vcm_.RegisterSendCodec(&video_codec, number_of_cores_,
388 max_data_payload_length) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000389 return -1;
390 }
391
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000392 // Add the a bitrate observer to the allocator and update the start, max and
393 // min bitrates of the bitrate controller as needed.
394 int new_bwe_candidate_bps = bitrate_allocator_->AddBitrateObserver(
395 bitrate_observer_.get(), video_codec.startBitrate * 1000,
396 video_codec.minBitrate * 1000,
397 kTransmissionMaxBitrateMultiplier * video_codec.maxBitrate * 1000);
398 if (new_bwe_candidate_bps > 0) {
399 uint32_t current_bwe_bps = 0;
400 bitrate_controller_->AvailableBandwidth(&current_bwe_bps);
401 bitrate_controller_->SetStartBitrate(std::max(
402 static_cast<uint32_t>(new_bwe_candidate_bps), current_bwe_bps));
403 }
404
405 int new_bwe_min_bps = 0;
406 int new_bwe_max_bps = 0;
407 bitrate_allocator_->GetMinMaxBitrateSumBps(&new_bwe_min_bps,
408 &new_bwe_max_bps);
409 bitrate_controller_->SetMinMaxBitrate(new_bwe_min_bps, new_bwe_max_bps);
410
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000411 int pad_up_to_bitrate_bps =
412 GetPaddingNeededBps(1000 * video_codec.startBitrate);
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000413 paced_sender_->UpdateBitrate(
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000414 video_codec.startBitrate,
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000415 PacedSender::kDefaultPaceMultiplier * video_codec.startBitrate,
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000416 pad_up_to_bitrate_bps / 1000);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000417
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000418 return 0;
419}
420
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000421int32_t ViEEncoder::GetEncoder(VideoCodec* video_codec) {
tommi@webrtc.orge07710c2015-02-19 17:43:25 +0000422 *video_codec = vcm_.GetSendCodec();
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000423 return 0;
424}
niklase@google.com470e71d2011-07-07 08:21:25 +0000425
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000426int32_t ViEEncoder::GetCodecConfigParameters(
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000427 unsigned char config_parameters[kConfigParameterSize],
428 unsigned char& config_parameters_size) {
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000429 int32_t num_parameters =
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000430 vcm_.CodecConfigParameters(config_parameters, kConfigParameterSize);
431 if (num_parameters <= 0) {
432 config_parameters_size = 0;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000433 return -1;
434 }
435 config_parameters_size = static_cast<unsigned char>(num_parameters);
436 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000437}
438
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000439int32_t ViEEncoder::ScaleInputImage(bool enable) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000440 VideoFrameResampling resampling_mode = kFastRescaling;
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000441 // TODO(mflodman) What?
442 if (enable) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000443 // kInterpolation is currently not supported.
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000444 LOG_F(LS_ERROR) << "Not supported.";
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000445 return -1;
446 }
447 vpm_.SetInputFrameResampleMode(resampling_mode);
niklase@google.com470e71d2011-07-07 08:21:25 +0000448
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000449 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000450}
451
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000452bool ViEEncoder::TimeToSendPacket(uint32_t ssrc,
453 uint16_t sequence_number,
454 int64_t capture_time_ms,
455 bool retransmission) {
mflodman@webrtc.org290cb562015-02-17 10:15:06 +0000456 return send_payload_router_->TimeToSendPacket(
457 ssrc, sequence_number, capture_time_ms, retransmission);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000458}
459
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000460size_t ViEEncoder::TimeToSendPadding(size_t bytes) {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000461 return send_payload_router_->TimeToSendPadding(bytes);
462}
463
464int ViEEncoder::GetPaddingNeededBps(int bitrate_bps) const {
465 int64_t time_of_last_incoming_frame_ms;
466 int min_transmit_bitrate_bps;
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +0000467 {
468 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000469 bool send_padding =
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000470 send_padding_ || video_suspended_ || min_transmit_bitrate_kbps_ > 0;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000471 if (!send_padding)
472 return 0;
473 time_of_last_incoming_frame_ms = time_of_last_incoming_frame_ms_;
474 min_transmit_bitrate_bps = 1000 * min_transmit_bitrate_kbps_;
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +0000475 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000476
477 VideoCodec send_codec;
478 if (vcm_.SendCodec(&send_codec) != 0)
479 return 0;
480 SimulcastStream* stream_configs = send_codec.simulcastStream;
481 // Allocate the bandwidth between the streams.
482 std::vector<uint32_t> stream_bitrates = AllocateStreamBitrates(
483 bitrate_bps, stream_configs, send_codec.numberOfSimulcastStreams);
484
485 bool video_is_suspended = vcm_.VideoSuspended();
486
487 // Find the max amount of padding we can allow ourselves to send at this
488 // point, based on which streams are currently active and what our current
489 // available bandwidth is.
490 int pad_up_to_bitrate_bps = 0;
491 if (send_codec.numberOfSimulcastStreams == 0) {
492 pad_up_to_bitrate_bps = send_codec.minBitrate * 1000;
493 } else {
494 pad_up_to_bitrate_bps =
495 stream_configs[send_codec.numberOfSimulcastStreams - 1].minBitrate *
496 1000;
497 for (int i = 0; i < send_codec.numberOfSimulcastStreams - 1; ++i) {
498 pad_up_to_bitrate_bps += stream_configs[i].targetBitrate * 1000;
499 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000500 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000501
502 // Disable padding if only sending one stream and video isn't suspended and
503 // min-transmit bitrate isn't used (applied later).
504 if (!video_is_suspended && send_codec.numberOfSimulcastStreams <= 1)
505 pad_up_to_bitrate_bps = 0;
506
507 // The amount of padding should decay to zero if no frames are being
508 // captured unless a min-transmit bitrate is used.
509 int64_t now_ms = TickTime::MillisecondTimestamp();
510 if (now_ms - time_of_last_incoming_frame_ms > kStopPaddingThresholdMs)
511 pad_up_to_bitrate_bps = 0;
512
513 // Pad up to min bitrate.
514 if (pad_up_to_bitrate_bps < min_transmit_bitrate_bps)
515 pad_up_to_bitrate_bps = min_transmit_bitrate_bps;
516
517 // Padding may never exceed bitrate estimate.
518 if (pad_up_to_bitrate_bps > bitrate_bps)
519 pad_up_to_bitrate_bps = bitrate_bps;
520
521 return pad_up_to_bitrate_bps;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000522}
523
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000524bool ViEEncoder::EncoderPaused() const {
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000525 // Pause video if paused by caller or as long as the network is down or the
526 // pacer queue has grown too large in buffered mode.
527 if (encoder_paused_) {
528 return true;
529 }
530 if (target_delay_ms_ > 0) {
531 // Buffered mode.
532 // TODO(pwestin): Workaround until nack is configured as a time and not
533 // number of packets.
534 return paced_sender_->QueueInMs() >=
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000535 std::max(static_cast<int>(target_delay_ms_ * kEncoderPausePacerMargin),
536 kMinPacingDelayMs);
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000537 }
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000538 if (paced_sender_->ExpectedQueueTimeMs() >
539 PacedSender::kDefaultMaxQueueLengthMs) {
540 // Too much data in pacer queue, drop frame.
541 return true;
542 }
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000543 return !network_is_transmitting_;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000544}
545
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000546void ViEEncoder::TraceFrameDropStart() {
547 // Start trace event only on the first frame after encoder is paused.
548 if (!encoder_paused_and_dropped_frame_) {
549 TRACE_EVENT_ASYNC_BEGIN0("webrtc", "EncoderPaused", this);
550 }
551 encoder_paused_and_dropped_frame_ = true;
552 return;
553}
554
555void ViEEncoder::TraceFrameDropEnd() {
556 // End trace event on first frame after encoder resumes, if frame was dropped.
557 if (encoder_paused_and_dropped_frame_) {
558 TRACE_EVENT_ASYNC_END0("webrtc", "EncoderPaused", this);
559 }
560 encoder_paused_and_dropped_frame_ = false;
561}
562
mflodman@webrtc.org8baed512012-06-21 12:11:50 +0000563void ViEEncoder::DeliverFrame(int id,
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000564 I420VideoFrame* video_frame,
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000565 const std::vector<uint32_t>& csrcs) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000566 DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.orga98e7962015-02-11 15:45:56 +0000567 DCHECK(csrcs.empty());
mflodman@webrtc.org47d657b2015-02-19 10:29:32 +0000568 if (!send_payload_router_->active()) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000569 // We've paused or we have no channels attached, don't waste resources on
570 // encoding.
wuchengli@chromium.orgac4b87c2014-03-19 03:44:20 +0000571 return;
572 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000573 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000574 CriticalSectionScoped cs(data_cs_.get());
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000575 time_of_last_incoming_frame_ms_ = TickTime::MillisecondTimestamp();
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000576 if (EncoderPaused()) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000577 TraceFrameDropStart();
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000578 return;
579 }
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000580 TraceFrameDropEnd();
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000581 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000582
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000583 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame->render_time_ms(),
584 "Encode");
pwestin@webrtc.org2f476ed2012-10-30 16:21:52 +0000585 I420VideoFrame* decimated_frame = NULL;
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000586 // TODO(wuchengli): support texture frames.
587 if (video_frame->native_handle() == NULL) {
588 {
589 CriticalSectionScoped cs(callback_cs_.get());
590 if (effect_filter_) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000591 size_t length =
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000592 CalcBufferSize(kI420, video_frame->width(), video_frame->height());
mflodman@webrtc.org948d6172015-02-10 08:58:16 +0000593 rtc::scoped_ptr<uint8_t[]> video_buffer(new uint8_t[length]);
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000594 ExtractBuffer(*video_frame, length, video_buffer.get());
595 effect_filter_->Transform(length,
596 video_buffer.get(),
597 video_frame->ntp_time_ms(),
598 video_frame->timestamp(),
599 video_frame->width(),
600 video_frame->height());
601 }
602 }
603
604 // Pass frame via preprocessor.
605 const int ret = vpm_.PreprocessFrame(*video_frame, &decimated_frame);
606 if (ret == 1) {
607 // Drop this frame.
608 return;
609 }
610 if (ret != VPM_OK) {
611 return;
612 }
pwestin@webrtc.org2f476ed2012-10-30 16:21:52 +0000613 }
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000614 // If the frame was not resampled or scaled => use original.
pwestin@webrtc.org2f476ed2012-10-30 16:21:52 +0000615 if (decimated_frame == NULL) {
616 decimated_frame = video_frame;
617 }
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000618
619 {
620 CriticalSectionScoped cs(callback_cs_.get());
621 if (pre_encode_callback_)
622 pre_encode_callback_->FrameCallback(decimated_frame);
623 }
624
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000625 if (video_frame->native_handle() != NULL) {
626 // TODO(wuchengli): add texture support. http://crbug.com/362437
627 return;
628 }
629
niklase@google.com470e71d2011-07-07 08:21:25 +0000630#ifdef VIDEOCODEC_VP8
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000631 if (vcm_.SendCodec() == webrtc::kVideoCodecVP8) {
632 webrtc::CodecSpecificInfo codec_specific_info;
633 codec_specific_info.codecType = webrtc::kVideoCodecVP8;
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000634 {
635 CriticalSectionScoped cs(data_cs_.get());
636 codec_specific_info.codecSpecific.VP8.hasReceivedRPSI =
637 has_received_rpsi_;
638 codec_specific_info.codecSpecific.VP8.hasReceivedSLI =
639 has_received_sli_;
640 codec_specific_info.codecSpecific.VP8.pictureIdRPSI =
641 picture_id_rpsi_;
642 codec_specific_info.codecSpecific.VP8.pictureIdSLI =
643 picture_id_sli_;
644 has_received_sli_ = false;
645 has_received_rpsi_ = false;
646 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000647
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000648 vcm_.AddVideoFrame(*decimated_frame, vpm_.ContentMetrics(),
649 &codec_specific_info);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000650 return;
651 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000652#endif
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000653 vcm_.AddVideoFrame(*decimated_frame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000654}
niklase@google.com470e71d2011-07-07 08:21:25 +0000655
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000656void ViEEncoder::DelayChanged(int id, int frame_delay) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000657}
niklase@google.com470e71d2011-07-07 08:21:25 +0000658
mflodman@webrtc.org8baed512012-06-21 12:11:50 +0000659int ViEEncoder::GetPreferedFrameSettings(int* width,
660 int* height,
661 int* frame_rate) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000662 webrtc::VideoCodec video_codec;
663 memset(&video_codec, 0, sizeof(video_codec));
664 if (vcm_.SendCodec(&video_codec) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000665 return -1;
666 }
667
mflodman@webrtc.org8baed512012-06-21 12:11:50 +0000668 *width = video_codec.width;
669 *height = video_codec.height;
670 *frame_rate = video_codec.maxFramerate;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000671 return 0;
672}
673
pwestin@webrtc.orgce330352012-04-12 06:59:14 +0000674int ViEEncoder::SendKeyFrame() {
stefan@webrtc.orgc5300432012-10-08 07:06:53 +0000675 return vcm_.IntraFrameRequest(0);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000676}
677
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000678int32_t ViEEncoder::SendCodecStatistics(
679 uint32_t* num_key_frames, uint32_t* num_delta_frames) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000680 webrtc::VCMFrameCount sent_frames;
681 if (vcm_.SentFrameCount(sent_frames) != VCM_OK) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000682 return -1;
683 }
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +0000684 *num_key_frames = sent_frames.numKeyFrames;
685 *num_delta_frames = sent_frames.numDeltaFrames;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000686 return 0;
687}
688
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000689int64_t ViEEncoder::PacerQueuingDelayMs() const {
jiayl@webrtc.org9fd8d872014-02-27 22:32:40 +0000690 return paced_sender_->QueueInMs();
691}
692
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000693int ViEEncoder::CodecTargetBitrate(uint32_t* bitrate) const {
stefan@webrtc.org439be292012-02-16 14:45:37 +0000694 if (vcm_.Bitrate(bitrate) != 0)
695 return -1;
696 return 0;
stefan@webrtc.org07b45a52012-02-02 08:37:48 +0000697}
698
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000699int32_t ViEEncoder::UpdateProtectionMethod(bool nack, bool fec) {
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000700 DCHECK(send_payload_router_ != NULL);
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000701 DCHECK(vcm_protection_callback_ != NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000702
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000703 if (fec_enabled_ == fec && nack_enabled_ == nack) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000704 // No change needed, we're already in correct state.
705 return 0;
706 }
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000707 fec_enabled_ = fec;
708 nack_enabled_ = nack;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000709
710 // Set Video Protection for VCM.
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000711 if (fec_enabled_ && nack_enabled_) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000712 vcm_.SetVideoProtection(webrtc::kProtectionNackFEC, true);
713 } else {
714 vcm_.SetVideoProtection(webrtc::kProtectionFEC, fec_enabled_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000715 vcm_.SetVideoProtection(webrtc::kProtectionNackSender, nack_enabled_);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000716 vcm_.SetVideoProtection(webrtc::kProtectionNackFEC, false);
717 }
718
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000719 if (fec_enabled_ || nack_enabled_) {
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000720 vcm_.RegisterProtectionCallback(vcm_protection_callback_);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000721 // The send codec must be registered to set correct MTU.
722 webrtc::VideoCodec codec;
723 if (vcm_.SendCodec(&codec) == 0) {
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000724 uint32_t current_bitrate_bps = 0;
725 if (vcm_.Bitrate(&current_bitrate_bps) != 0) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000726 LOG_F(LS_WARNING) <<
727 "Failed to get the current encoder target bitrate.";
stefan@webrtc.org439be292012-02-16 14:45:37 +0000728 }
stefan@webrtc.org3d0b0d62013-03-19 10:04:57 +0000729 // Convert to start bitrate in kbps.
730 codec.startBitrate = (current_bitrate_bps + 500) / 1000;
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000731 size_t max_payload_length = send_payload_router_->MaxPayloadLength();
732 if (vcm_.RegisterSendCodec(&codec, number_of_cores_,
733 max_payload_length) != 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000734 return -1;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000735 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000736 }
737 return 0;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000738 } else {
739 // FEC and NACK are disabled.
740 vcm_.RegisterProtectionCallback(NULL);
741 }
742 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000743}
744
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000745void ViEEncoder::SetSenderBufferingMode(int target_delay_ms) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000746 {
747 CriticalSectionScoped cs(data_cs_.get());
748 target_delay_ms_ = target_delay_ms;
749 }
mikhal@webrtc.org3d305c62013-02-10 18:42:55 +0000750 if (target_delay_ms > 0) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000751 // Disable external frame-droppers.
752 vcm_.EnableFrameDropper(false);
753 vpm_.EnableTemporalDecimation(false);
mikhal@webrtc.org3d305c62013-02-10 18:42:55 +0000754 } else {
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000755 // Real-time mode - enable frame droppers.
mikhal@webrtc.org3d305c62013-02-10 18:42:55 +0000756 vpm_.EnableTemporalDecimation(true);
757 vcm_.EnableFrameDropper(true);
758 }
759}
760
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000761void ViEEncoder::OnSetRates(uint32_t bitrate_bps, int framerate) {
762 CriticalSectionScoped cs(callback_cs_.get());
763 if (send_statistics_proxy_ != nullptr)
764 send_statistics_proxy_->OnSetRates(bitrate_bps, framerate);
765}
766
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000767int32_t ViEEncoder::SendData(
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000768 const uint8_t payload_type,
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000769 const EncodedImage& encoded_image,
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000770 const webrtc::RTPFragmentationHeader& fragmentation_header,
771 const RTPVideoHeader* rtp_video_hdr) {
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000772 DCHECK(send_payload_router_ != NULL);
773
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000774 {
775 CriticalSectionScoped cs(callback_cs_.get());
776 if (send_statistics_proxy_ != NULL)
777 send_statistics_proxy_->OnSendEncodedImage(encoded_image, rtp_video_hdr);
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000778 }
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000779
780 return send_payload_router_->RoutePayload(
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000781 VCMEncodedFrame::ConvertFrameType(encoded_image._frameType), payload_type,
782 encoded_image._timeStamp, encoded_image.capture_time_ms_,
783 encoded_image._buffer, encoded_image._length, &fragmentation_header,
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000784 rtp_video_hdr) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000785}
786
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000787int32_t ViEEncoder::SendStatistics(const uint32_t bit_rate,
788 const uint32_t frame_rate) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000789 CriticalSectionScoped cs(callback_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000790 if (codec_observer_) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000791 codec_observer_->OutgoingRate(channel_id_, frame_rate, bit_rate);
792 }
793 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000794}
795
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000796int32_t ViEEncoder::RegisterCodecObserver(ViEEncoderObserver* observer) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000797 CriticalSectionScoped cs(callback_cs_.get());
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000798 if (observer && codec_observer_) {
799 LOG_F(LS_ERROR) << "Observer already set.";
800 return -1;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000801 }
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000802 codec_observer_ = observer;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000803 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000804}
805
andrew@webrtc.org96636862012-09-20 23:33:17 +0000806void ViEEncoder::OnReceivedSLI(uint32_t /*ssrc*/,
807 uint8_t picture_id) {
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000808 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000809 picture_id_sli_ = picture_id;
810 has_received_sli_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000811}
812
andrew@webrtc.org96636862012-09-20 23:33:17 +0000813void ViEEncoder::OnReceivedRPSI(uint32_t /*ssrc*/,
814 uint64_t picture_id) {
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000815 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000816 picture_id_rpsi_ = picture_id;
817 has_received_rpsi_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000818}
819
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000820void ViEEncoder::OnReceivedIntraFrameRequest(uint32_t ssrc) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000821 // Key frame request from remote side, signal to VCM.
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000822 TRACE_EVENT0("webrtc", "OnKeyFrameRequest");
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000823
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000824 int idx = 0;
825 {
826 CriticalSectionScoped cs(data_cs_.get());
827 std::map<unsigned int, int>::iterator stream_it = ssrc_streams_.find(ssrc);
828 if (stream_it == ssrc_streams_.end()) {
mflodman@webrtc.orgd73527c2012-12-20 09:26:17 +0000829 LOG_F(LS_WARNING) << "ssrc not found: " << ssrc << ", map size "
830 << ssrc_streams_.size();
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000831 return;
832 }
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000833 std::map<unsigned int, int64_t>::iterator time_it =
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000834 time_last_intra_request_ms_.find(ssrc);
835 if (time_it == time_last_intra_request_ms_.end()) {
836 time_last_intra_request_ms_[ssrc] = 0;
837 }
838
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000839 int64_t now = TickTime::MillisecondTimestamp();
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000840 if (time_last_intra_request_ms_[ssrc] + kViEMinKeyRequestIntervalMs > now) {
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000841 return;
842 }
843 time_last_intra_request_ms_[ssrc] = now;
844 idx = stream_it->second;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000845 }
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000846 // Release the critsect before triggering key frame.
847 vcm_.IntraFrameRequest(idx);
niklase@google.com470e71d2011-07-07 08:21:25 +0000848}
849
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000850void ViEEncoder::OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc) {
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000851 CriticalSectionScoped cs(data_cs_.get());
852 std::map<unsigned int, int>::iterator it = ssrc_streams_.find(old_ssrc);
853 if (it == ssrc_streams_.end()) {
854 return;
855 }
856
857 ssrc_streams_[new_ssrc] = it->second;
858 ssrc_streams_.erase(it);
859
860 std::map<unsigned int, int64_t>::iterator time_it =
861 time_last_intra_request_ms_.find(old_ssrc);
862 int64_t last_intra_request_ms = 0;
863 if (time_it != time_last_intra_request_ms_.end()) {
864 last_intra_request_ms = time_it->second;
865 time_last_intra_request_ms_.erase(time_it);
866 }
867 time_last_intra_request_ms_[new_ssrc] = last_intra_request_ms;
868}
869
870bool ViEEncoder::SetSsrcs(const std::list<unsigned int>& ssrcs) {
871 VideoCodec codec;
872 if (vcm_.SendCodec(&codec) != 0)
873 return false;
874
875 if (codec.numberOfSimulcastStreams > 0 &&
876 ssrcs.size() != codec.numberOfSimulcastStreams) {
877 return false;
878 }
879
880 CriticalSectionScoped cs(data_cs_.get());
881 ssrc_streams_.clear();
882 time_last_intra_request_ms_.clear();
883 int idx = 0;
884 for (std::list<unsigned int>::const_iterator it = ssrcs.begin();
885 it != ssrcs.end(); ++it, ++idx) {
886 unsigned int ssrc = *it;
887 ssrc_streams_[ssrc] = idx;
888 }
889 return true;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000890}
891
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000892void ViEEncoder::SetMinTransmitBitrate(int min_transmit_bitrate_kbps) {
893 assert(min_transmit_bitrate_kbps >= 0);
894 CriticalSectionScoped crit(data_cs_.get());
895 min_transmit_bitrate_kbps_ = min_transmit_bitrate_kbps;
896}
897
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000898// Called from ViEBitrateObserver.
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000899void ViEEncoder::OnNetworkChanged(uint32_t bitrate_bps,
900 uint8_t fraction_lost,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000901 int64_t round_trip_time_ms) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000902 LOG(LS_VERBOSE) << "OnNetworkChanged, bitrate" << bitrate_bps
903 << " packet loss " << fraction_lost
904 << " rtt " << round_trip_time_ms;
mflodman@webrtc.org50e28162015-02-23 07:45:11 +0000905 DCHECK(send_payload_router_ != NULL);
stefan@webrtc.orgabc9d5b2013-03-18 17:00:51 +0000906 vcm_.SetChannelParameters(bitrate_bps, fraction_lost, round_trip_time_ms);
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000907 bool video_is_suspended = vcm_.VideoSuspended();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000908
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000909 VideoCodec send_codec;
910 if (vcm_.SendCodec(&send_codec) != 0) {
911 return;
912 }
stefan@webrtc.orgb2c8a952013-09-06 13:58:01 +0000913 SimulcastStream* stream_configs = send_codec.simulcastStream;
914 // Allocate the bandwidth between the streams.
915 std::vector<uint32_t> stream_bitrates = AllocateStreamBitrates(
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000916 bitrate_bps, stream_configs, send_codec.numberOfSimulcastStreams);
917 send_payload_router_->SetTargetSendBitrates(stream_bitrates);
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000918
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000919 int pad_up_to_bitrate_bps = GetPaddingNeededBps(bitrate_bps);
920 paced_sender_->UpdateBitrate(
921 bitrate_bps / 1000,
922 PacedSender::kDefaultPaceMultiplier * bitrate_bps / 1000,
923 pad_up_to_bitrate_bps / 1000);
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000924
925 {
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000926 CriticalSectionScoped cs(data_cs_.get());
pbos@webrtc.org484ee962013-11-21 18:44:23 +0000927 if (video_suspended_ == video_is_suspended)
928 return;
929 video_suspended_ = video_is_suspended;
930 }
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000931 // Video suspend-state changed, inform codec observer.
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000932 CriticalSectionScoped crit(callback_cs_.get());
pbos@webrtc.org484ee962013-11-21 18:44:23 +0000933 if (codec_observer_) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000934 LOG(LS_INFO) << "Video suspended " << video_is_suspended
935 << " for channel " << channel_id_;
henrik.lundin@webrtc.org9fe36032013-11-21 23:00:40 +0000936 codec_observer_->SuspendChange(channel_id_, video_is_suspended);
henrik.lundin@webrtc.org7ea4f242013-10-02 13:34:26 +0000937 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000938}
939
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000940PacedSender* ViEEncoder::GetPacedSender() {
941 return paced_sender_.get();
942}
943
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000944int32_t ViEEncoder::RegisterEffectFilter(ViEEffectFilter* effect_filter) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000945 CriticalSectionScoped cs(callback_cs_.get());
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000946 if (effect_filter != NULL && effect_filter_ != NULL) {
947 LOG_F(LS_ERROR) << "Filter already set.";
948 return -1;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000949 }
950 effect_filter_ = effect_filter;
951 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000952}
953
mikhal@webrtc.orge41bbdf2012-08-28 16:15:16 +0000954int ViEEncoder::StartDebugRecording(const char* fileNameUTF8) {
955 return vcm_.StartDebugRecording(fileNameUTF8);
956}
957
958int ViEEncoder::StopDebugRecording() {
959 return vcm_.StopDebugRecording();
960}
961
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000962void ViEEncoder::SuspendBelowMinBitrate() {
963 vcm_.SuspendBelowMinBitrate();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000964 bitrate_allocator_->EnforceMinBitrate(false);
965 int min_bitrate_sum_bps;
966 int max_bitrate_sum_bps;
967 bitrate_allocator_->GetMinMaxBitrateSumBps(&min_bitrate_sum_bps,
968 &max_bitrate_sum_bps);
969 bitrate_controller_->SetMinMaxBitrate(min_bitrate_sum_bps,
970 max_bitrate_sum_bps);
henrik.lundin@webrtc.org7ea4f242013-10-02 13:34:26 +0000971}
972
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000973void ViEEncoder::RegisterPreEncodeCallback(
974 I420FrameCallback* pre_encode_callback) {
975 CriticalSectionScoped cs(callback_cs_.get());
976 pre_encode_callback_ = pre_encode_callback;
977}
978
979void ViEEncoder::DeRegisterPreEncodeCallback() {
980 CriticalSectionScoped cs(callback_cs_.get());
981 pre_encode_callback_ = NULL;
982}
983
sprang@webrtc.org40709352013-11-26 11:41:59 +0000984void ViEEncoder::RegisterPostEncodeImageCallback(
985 EncodedImageCallback* post_encode_callback) {
986 vcm_.RegisterPostEncodeImageCallback(post_encode_callback);
987}
988
989void ViEEncoder::DeRegisterPostEncodeImageCallback() {
990 vcm_.RegisterPostEncodeImageCallback(NULL);
991}
992
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000993void ViEEncoder::RegisterSendStatisticsProxy(
994 SendStatisticsProxy* send_statistics_proxy) {
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000995 CriticalSectionScoped cs(callback_cs_.get());
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000996 send_statistics_proxy_ = send_statistics_proxy;
997}
998
marpan@webrtc.orgefd01fd2012-04-18 15:56:34 +0000999QMVideoSettingsCallback::QMVideoSettingsCallback(VideoProcessingModule* vpm)
1000 : vpm_(vpm) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +00001001}
niklase@google.com470e71d2011-07-07 08:21:25 +00001002
stefan@webrtc.org439be292012-02-16 14:45:37 +00001003QMVideoSettingsCallback::~QMVideoSettingsCallback() {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +00001004}
1005
pbos@webrtc.orgb238d122013-04-09 13:41:51 +00001006int32_t QMVideoSettingsCallback::SetVideoQMSettings(
1007 const uint32_t frame_rate,
1008 const uint32_t width,
1009 const uint32_t height) {
marpan@webrtc.orgcf706c22012-03-27 21:04:13 +00001010 return vpm_->SetTargetResolution(width, height, frame_rate);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +00001011}
1012
mflodman@webrtc.org84d17832011-12-01 17:02:23 +00001013} // namespace webrtc