blob: 731c4f8b2ba2ad24358e082cbdd137f1f6668c06 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
sprang3911c262016-04-15 01:24:14 -07002* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3*
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*/
niklase@google.com470e71d2011-07-07 08:21:25 +000010
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/generic_encoder.h"
philipel9d3ab612015-12-21 04:12:39 -080012
13#include <vector>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "api/optional.h"
16#include "api/video/i420_buffer.h"
17#include "modules/pacing/alr_detector.h"
18#include "modules/video_coding/encoded_frame.h"
19#include "modules/video_coding/media_optimization.h"
20#include "rtc_base/checks.h"
21#include "rtc_base/logging.h"
22#include "rtc_base/timeutils.h"
23#include "rtc_base/trace_event.h"
24#include "system_wrappers/include/field_trial.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000025
26namespace webrtc {
Sergey Ulanov525df3f2016-08-02 17:46:41 -070027
Peter Boström4f5db112015-10-29 16:53:59 +010028VCMGenericEncoder::VCMGenericEncoder(
29 VideoEncoder* encoder,
Peter Boström4f5db112015-10-29 16:53:59 +010030 VCMEncodedFrameCallback* encoded_frame_callback,
sprang3911c262016-04-15 01:24:14 -070031 bool internal_source)
pbos@webrtc.org891d4832015-02-26 13:15:22 +000032 : encoder_(encoder),
Peter Boström4f5db112015-10-29 16:53:59 +010033 vcm_encoded_frame_callback_(encoded_frame_callback),
sprang3911c262016-04-15 01:24:14 -070034 internal_source_(internal_source),
Erik Språng08127a92016-11-16 16:41:30 +010035 encoder_params_({BitrateAllocation(), 0, 0, 0}),
ilnik04f4d122017-06-19 07:18:55 -070036 streams_or_svc_num_(0) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000037
Peter Boström4f5db112015-10-29 16:53:59 +010038VCMGenericEncoder::~VCMGenericEncoder() {}
39
40int32_t VCMGenericEncoder::Release() {
Peter Boström02bafc62016-07-01 12:45:15 +020041 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Peter Boström4fd6cda2016-01-26 10:19:53 +010042 TRACE_EVENT0("webrtc", "VCMGenericEncoder::Release");
Peter Boström4f5db112015-10-29 16:53:59 +010043 return encoder_->Release();
niklase@google.com470e71d2011-07-07 08:21:25 +000044}
45
Peter Boström4f5db112015-10-29 16:53:59 +010046int32_t VCMGenericEncoder::InitEncode(const VideoCodec* settings,
sprang3911c262016-04-15 01:24:14 -070047 int32_t number_of_cores,
48 size_t max_payload_size) {
Peter Boström02bafc62016-07-01 12:45:15 +020049 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
pbosd9eec762015-11-17 06:03:43 -080050 TRACE_EVENT0("webrtc", "VCMGenericEncoder::InitEncode");
ilnik04f4d122017-06-19 07:18:55 -070051 streams_or_svc_num_ = settings->numberOfSimulcastStreams;
52 if (settings->codecType == kVideoCodecVP9) {
53 streams_or_svc_num_ = settings->VP9().numberOfSpatialLayers;
54 }
55 if (streams_or_svc_num_ == 0)
56 streams_or_svc_num_ = 1;
57
58 vcm_encoded_frame_callback_->SetTimingFramesThresholds(
59 settings->timing_frame_thresholds);
60 vcm_encoded_frame_callback_->OnFrameRateChanged(settings->maxFramerate);
61
sprang3911c262016-04-15 01:24:14 -070062 if (encoder_->InitEncode(settings, number_of_cores, max_payload_size) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010063 RTC_LOG(LS_ERROR) << "Failed to initialize the encoder associated with "
64 "payload name: "
65 << settings->plName;
Peter Boström4f5db112015-10-29 16:53:59 +010066 return -1;
67 }
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020068 vcm_encoded_frame_callback_->Reset();
Peter Boström4f5db112015-10-29 16:53:59 +010069 encoder_->RegisterEncodeCompleteCallback(vcm_encoded_frame_callback_);
70 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000071}
72
sprang3911c262016-04-15 01:24:14 -070073int32_t VCMGenericEncoder::Encode(const VideoFrame& frame,
74 const CodecSpecificInfo* codec_specific,
75 const std::vector<FrameType>& frame_types) {
Peter Boström02bafc62016-07-01 12:45:15 +020076 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
pbosd9eec762015-11-17 06:03:43 -080077 TRACE_EVENT1("webrtc", "VCMGenericEncoder::Encode", "timestamp",
sprang3911c262016-04-15 01:24:14 -070078 frame.timestamp());
pbosd9eec762015-11-17 06:03:43 -080079
sprang3911c262016-04-15 01:24:14 -070080 for (FrameType frame_type : frame_types)
pbos22993e12015-10-19 02:39:06 -070081 RTC_DCHECK(frame_type == kVideoFrameKey || frame_type == kVideoFrameDelta);
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +000082
ilnik04f4d122017-06-19 07:18:55 -070083 for (size_t i = 0; i < streams_or_svc_num_; ++i)
84 vcm_encoded_frame_callback_->OnEncodeStarted(frame.render_time_ms(), i);
Peter Boströmb7d9a972015-12-18 16:01:11 +010085
ilnike9973812017-09-12 10:24:46 -070086 return encoder_->Encode(frame, codec_specific, &frame_types);
niklase@google.com470e71d2011-07-07 08:21:25 +000087}
88
Peter Boström69ccb332015-10-29 16:30:23 +010089void VCMGenericEncoder::SetEncoderParameters(const EncoderParameters& params) {
Peter Boström02bafc62016-07-01 12:45:15 +020090 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Peter Boström69ccb332015-10-29 16:30:23 +010091 bool channel_parameters_have_changed;
92 bool rates_have_changed;
93 {
94 rtc::CritScope lock(&params_lock_);
95 channel_parameters_have_changed =
96 params.loss_rate != encoder_params_.loss_rate ||
97 params.rtt != encoder_params_.rtt;
98 rates_have_changed =
99 params.target_bitrate != encoder_params_.target_bitrate ||
100 params.input_frame_rate != encoder_params_.input_frame_rate;
101 encoder_params_ = params;
102 }
Erik Språng08127a92016-11-16 16:41:30 +0100103 if (channel_parameters_have_changed) {
104 int res = encoder_->SetChannelParameters(params.loss_rate, params.rtt);
105 if (res != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100106 RTC_LOG(LS_WARNING) << "Error set encoder parameters (loss = "
107 << params.loss_rate << ", rtt = " << params.rtt
108 << "): " << res;
Erik Språng08127a92016-11-16 16:41:30 +0100109 }
110 }
Peter Boström69ccb332015-10-29 16:30:23 +0100111 if (rates_have_changed) {
Erik Språng08127a92016-11-16 16:41:30 +0100112 int res = encoder_->SetRateAllocation(params.target_bitrate,
113 params.input_frame_rate);
114 if (res != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100115 RTC_LOG(LS_WARNING) << "Error set encoder rate (total bitrate bps = "
116 << params.target_bitrate.get_sum_bps()
117 << ", framerate = " << params.input_frame_rate
118 << "): " << res;
Erik Språng08127a92016-11-16 16:41:30 +0100119 }
ilnik04f4d122017-06-19 07:18:55 -0700120 vcm_encoded_frame_callback_->OnFrameRateChanged(params.input_frame_rate);
121 for (size_t i = 0; i < streams_or_svc_num_; ++i) {
122 size_t layer_bitrate_bytes_per_sec =
123 params.target_bitrate.GetSpatialLayerSum(i) / 8;
124 // VP9 rate control is not yet moved out of VP9Impl. Due to that rates
125 // are not split among spatial layers.
126 if (layer_bitrate_bytes_per_sec == 0)
127 layer_bitrate_bytes_per_sec = params.target_bitrate.get_sum_bps() / 8;
128 vcm_encoded_frame_callback_->OnTargetBitrateChanged(
129 layer_bitrate_bytes_per_sec, i);
130 }
Peter Boström69ccb332015-10-29 16:30:23 +0100131 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000132}
133
Peter Boström69ccb332015-10-29 16:30:23 +0100134EncoderParameters VCMGenericEncoder::GetEncoderParameters() const {
135 rtc::CritScope lock(&params_lock_);
136 return encoder_params_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000137}
138
philipel9d3ab612015-12-21 04:12:39 -0800139int32_t VCMGenericEncoder::SetPeriodicKeyFrames(bool enable) {
Peter Boström02bafc62016-07-01 12:45:15 +0200140 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
philipel9d3ab612015-12-21 04:12:39 -0800141 return encoder_->SetPeriodicKeyFrames(enable);
niklase@google.com470e71d2011-07-07 08:21:25 +0000142}
143
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000144int32_t VCMGenericEncoder::RequestFrame(
stefan@webrtc.orgcf216862012-10-25 11:29:51 +0000145 const std::vector<FrameType>& frame_types) {
Peter Boström02bafc62016-07-01 12:45:15 +0200146 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
nissedf2ceb82016-12-15 06:29:53 -0800147
148 // TODO(nisse): Used only with internal source. Delete as soon as
149 // that feature is removed. The only implementation I've been able
nisse01d5a0b2017-05-31 06:33:21 -0700150 // to find ignores what's in the frame. With one exception: It seems
151 // a few test cases, e.g.,
152 // VideoSendStreamTest.VideoSendStreamStopSetEncoderRateToZero, set
153 // internal_source to true and use FakeEncoder. And the latter will
154 // happily encode this 1x1 frame and pass it on down the pipeline.
nissedf2ceb82016-12-15 06:29:53 -0800155 return encoder_->Encode(VideoFrame(I420Buffer::Create(1, 1),
156 kVideoRotation_0, 0),
157 NULL, &frame_types);
158 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000159}
160
philipel9d3ab612015-12-21 04:12:39 -0800161bool VCMGenericEncoder::InternalSource() const {
162 return internal_source_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000163}
164
Peter Boströmeb66e802015-06-05 11:08:03 +0200165bool VCMGenericEncoder::SupportsNativeHandle() const {
Peter Boström02bafc62016-07-01 12:45:15 +0200166 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Peter Boströmeb66e802015-06-05 11:08:03 +0200167 return encoder_->SupportsNativeHandle();
168}
169
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000170VCMEncodedFrameCallback::VCMEncodedFrameCallback(
perkj376b1922016-05-02 11:35:24 -0700171 EncodedImageCallback* post_encode_callback,
172 media_optimization::MediaOptimization* media_opt)
173 : internal_source_(false),
174 post_encode_callback_(post_encode_callback),
ilnik04f4d122017-06-19 07:18:55 -0700175 media_opt_(media_opt),
176 framerate_(1),
177 last_timing_frame_time_ms_(-1),
ilnik6d5b4d62017-08-30 03:32:14 -0700178 timing_frames_thresholds_({-1, 0}) {
179 rtc::Optional<AlrDetector::AlrExperimentSettings> experiment_settings =
180 AlrDetector::ParseAlrSettingsFromFieldTrial(
181 AlrDetector::kStrictPacingAndProbingExperimentName);
182 if (experiment_settings) {
183 experiment_groups_[0] = experiment_settings->group_id + 1;
184 } else {
185 experiment_groups_[0] = 0;
186 }
187 experiment_settings = AlrDetector::ParseAlrSettingsFromFieldTrial(
188 AlrDetector::kScreenshareProbingBweExperimentName);
189 if (experiment_settings) {
190 experiment_groups_[1] = experiment_settings->group_id + 1;
191 } else {
192 experiment_groups_[1] = 0;
193 }
194}
niklase@google.com470e71d2011-07-07 08:21:25 +0000195
sprang3911c262016-04-15 01:24:14 -0700196VCMEncodedFrameCallback::~VCMEncodedFrameCallback() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000197
ilnik04f4d122017-06-19 07:18:55 -0700198void VCMEncodedFrameCallback::OnTargetBitrateChanged(
199 size_t bitrate_bytes_per_second,
200 size_t simulcast_svc_idx) {
201 rtc::CritScope crit(&timing_params_lock_);
202 if (timing_frames_info_.size() < simulcast_svc_idx + 1)
203 timing_frames_info_.resize(simulcast_svc_idx + 1);
204 timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec =
205 bitrate_bytes_per_second;
206}
207
208void VCMEncodedFrameCallback::OnFrameRateChanged(size_t framerate) {
209 rtc::CritScope crit(&timing_params_lock_);
210 framerate_ = framerate;
211}
212
213void VCMEncodedFrameCallback::OnEncodeStarted(int64_t capture_time_ms,
214 size_t simulcast_svc_idx) {
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200215 if (internal_source_) {
216 return;
217 }
ilnik04f4d122017-06-19 07:18:55 -0700218 rtc::CritScope crit(&timing_params_lock_);
219 if (timing_frames_info_.size() < simulcast_svc_idx + 1)
220 timing_frames_info_.resize(simulcast_svc_idx + 1);
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200221 RTC_DCHECK(
222 timing_frames_info_[simulcast_svc_idx].encode_start_list.empty() ||
223 rtc::TimeDiff(capture_time_ms, timing_frames_info_[simulcast_svc_idx]
224 .encode_start_list.back()
225 .capture_time_ms) >= 0);
226 if (timing_frames_info_[simulcast_svc_idx].encode_start_list.size() ==
227 kMaxEncodeStartTimeListSize) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100228 RTC_LOG(LS_WARNING) << "Too many frames in the encode_start_list."
229 " Did encoder stall?";
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200230 post_encode_callback_->OnDroppedFrame(DropReason::kDroppedByEncoder);
231 timing_frames_info_[simulcast_svc_idx].encode_start_list.pop_front();
232 }
233 timing_frames_info_[simulcast_svc_idx].encode_start_list.emplace_back(
234 capture_time_ms, rtc::TimeMillis());
ilnik04f4d122017-06-19 07:18:55 -0700235}
236
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700237EncodedImageCallback::Result VCMEncodedFrameCallback::OnEncodedImage(
Peter Boströmb7d9a972015-12-18 16:01:11 +0100238 const EncodedImage& encoded_image,
sprang3911c262016-04-15 01:24:14 -0700239 const CodecSpecificInfo* codec_specific,
240 const RTPFragmentationHeader* fragmentation_header) {
pbosd9eec762015-11-17 06:03:43 -0800241 TRACE_EVENT_INSTANT1("webrtc", "VCMEncodedFrameCallback::Encoded",
Peter Boströmb7d9a972015-12-18 16:01:11 +0100242 "timestamp", encoded_image._timeStamp);
ilnik04f4d122017-06-19 07:18:55 -0700243 size_t simulcast_svc_idx = 0;
244 if (codec_specific->codecType == kVideoCodecVP9) {
245 if (codec_specific->codecSpecific.VP9.num_spatial_layers > 1)
246 simulcast_svc_idx = codec_specific->codecSpecific.VP9.spatial_idx;
247 } else if (codec_specific->codecType == kVideoCodecVP8) {
248 simulcast_svc_idx = codec_specific->codecSpecific.VP8.simulcastIdx;
249 } else if (codec_specific->codecType == kVideoCodecGeneric) {
250 simulcast_svc_idx = codec_specific->codecSpecific.generic.simulcast_idx;
251 } else if (codec_specific->codecType == kVideoCodecH264) {
252 // TODO(ilnik): When h264 simulcast is landed, extract simulcast idx here.
253 }
254
ilnik29d08402017-07-11 08:08:12 -0700255 rtc::Optional<size_t> outlier_frame_size;
256 rtc::Optional<int64_t> encode_start_ms;
ilnik6d5b4d62017-08-30 03:32:14 -0700257 size_t num_simulcast_svc_streams = 1;
sprangba050a62017-08-18 02:51:12 -0700258 uint8_t timing_flags = TimingFrameFlags::kInvalid;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200259 if (!internal_source_) {
ilnik04f4d122017-06-19 07:18:55 -0700260 rtc::CritScope crit(&timing_params_lock_);
ilnik0b1e2f32017-07-10 12:25:29 -0700261
ilnik29d08402017-07-11 08:08:12 -0700262 // Encoders with internal sources do not call OnEncodeStarted and
263 // OnFrameRateChanged. |timing_frames_info_| may be not filled here.
ilnik6d5b4d62017-08-30 03:32:14 -0700264 num_simulcast_svc_streams = timing_frames_info_.size();
265 if (simulcast_svc_idx < num_simulcast_svc_streams) {
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200266 auto encode_start_list =
267 &timing_frames_info_[simulcast_svc_idx].encode_start_list;
268 // Skip frames for which there was OnEncodeStarted but no OnEncodedImage
269 // call. These are dropped by encoder internally.
270 while (!encode_start_list->empty() &&
271 encode_start_list->front().capture_time_ms <
272 encoded_image.capture_time_ms_) {
273 post_encode_callback_->OnDroppedFrame(DropReason::kDroppedByEncoder);
274 encode_start_list->pop_front();
275 }
276 if (encode_start_list->size() > 0 &&
277 encode_start_list->front().capture_time_ms ==
278 encoded_image.capture_time_ms_) {
279 encode_start_ms.emplace(
280 encode_start_list->front().encode_start_time_ms);
281 encode_start_list->pop_front();
ilnik3635f442017-06-28 03:53:19 -0700282 }
283
ilnik29d08402017-07-11 08:08:12 -0700284 size_t target_bitrate =
285 timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec;
286 if (framerate_ > 0 && target_bitrate > 0) {
287 // framerate and target bitrate were reported by encoder.
288 size_t average_frame_size = target_bitrate / framerate_;
289 outlier_frame_size.emplace(
290 average_frame_size *
291 timing_frames_thresholds_.outlier_ratio_percent / 100);
ilnik0b1e2f32017-07-10 12:25:29 -0700292 }
ilnik29d08402017-07-11 08:08:12 -0700293 }
294
295 // Check if it's time to send a timing frame.
296 int64_t timing_frame_delay_ms =
297 encoded_image.capture_time_ms_ - last_timing_frame_time_ms_;
298 // Trigger threshold if it's a first frame, too long passed since the last
299 // timing frame, or we already sent timing frame on a different simulcast
300 // stream with the same capture time.
301 if (last_timing_frame_time_ms_ == -1 ||
302 timing_frame_delay_ms >= timing_frames_thresholds_.delay_ms ||
303 timing_frame_delay_ms == 0) {
sprangba050a62017-08-18 02:51:12 -0700304 timing_flags = TimingFrameFlags::kTriggeredByTimer;
ilnik29d08402017-07-11 08:08:12 -0700305 last_timing_frame_time_ms_ = encoded_image.capture_time_ms_;
306 }
307
308 // Outliers trigger timing frames, but do not affect scheduled timing
309 // frames.
310 if (outlier_frame_size && encoded_image._length >= *outlier_frame_size) {
sprangba050a62017-08-18 02:51:12 -0700311 if (timing_flags == TimingFrameFlags::kInvalid)
312 timing_flags = 0;
313 timing_flags |= TimingFrameFlags::kTriggeredBySize;
ilnik04f4d122017-06-19 07:18:55 -0700314 }
ilnik04f4d122017-06-19 07:18:55 -0700315 }
316
ilnik29d08402017-07-11 08:08:12 -0700317 // If encode start is not available that means that encoder uses internal
318 // source. In that case capture timestamp may be from a different clock with a
319 // drift relative to rtc::TimeMillis(). We can't use it for Timing frames,
320 // because to being sent in the network capture time required to be less than
321 // all the other timestamps.
sprangba050a62017-08-18 02:51:12 -0700322 if (timing_flags != TimingFrameFlags::kInvalid && encode_start_ms) {
ilnik29d08402017-07-11 08:08:12 -0700323 encoded_image.SetEncodeTime(*encode_start_ms, rtc::TimeMillis());
sprangba050a62017-08-18 02:51:12 -0700324 encoded_image.timing_.flags = timing_flags;
325 } else {
326 encoded_image.timing_.flags = TimingFrameFlags::kInvalid;
ilnik04f4d122017-06-19 07:18:55 -0700327 }
328
ilnik6d5b4d62017-08-30 03:32:14 -0700329 // Piggyback ALR experiment group id and simulcast id into the content type.
330 uint8_t experiment_id =
331 experiment_groups_[videocontenttypehelpers::IsScreenshare(
332 encoded_image.content_type_)];
333
334 // TODO(ilnik): This will force content type extension to be present even
335 // for realtime video. At the expense of miniscule overhead we will get
336 // sliced receive statistics.
337 RTC_CHECK(videocontenttypehelpers::SetExperimentId(
338 &encoded_image.content_type_, experiment_id));
339 // We count simulcast streams from 1 on the wire. That's why we set simulcast
340 // id in content type to +1 of that is actual simulcast index. This is because
341 // value 0 on the wire is reserved for 'no simulcast stream specified'.
342 RTC_CHECK(videocontenttypehelpers::SetSimulcastId(
343 &encoded_image.content_type_,
344 static_cast<uint8_t>(simulcast_svc_idx + 1)));
345
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700346 Result result = post_encode_callback_->OnEncodedImage(
347 encoded_image, codec_specific, fragmentation_header);
348 if (result.error != Result::OK)
349 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000350
sprang3911c262016-04-15 01:24:14 -0700351 if (media_opt_) {
352 media_opt_->UpdateWithEncodedData(encoded_image);
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700353 if (internal_source_) {
354 // Signal to encoder to drop next frame.
355 result.drop_next_frame = media_opt_->DropFrame();
356 }
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000357 }
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700358 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000359}
360
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000361} // namespace webrtc