blob: 5cea0a5a6523a5b407631966649a3998cc3c2322 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
Yves Gerey665174f2018-06-19 15:03:05 +02002 * 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
Danil Chapovalov0040b662018-06-18 10:48:16 +020015#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/video/i420_buffer.h"
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +010017#include "modules/include/module_common_types_public.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/video_coding/encoded_frame.h"
19#include "modules/video_coding/media_optimization.h"
20#include "rtc_base/checks.h"
Sebastian Janssoncabe3832018-01-12 10:54:18 +010021#include "rtc_base/experiments/alr_experiment.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/logging.h"
23#include "rtc_base/timeutils.h"
24#include "rtc_base/trace_event.h"
25#include "system_wrappers/include/field_trial.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000026
27namespace webrtc {
Sergey Ulanov525df3f2016-08-02 17:46:41 -070028
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +010029namespace {
Ilya Nikolaevskiy269674f2018-01-16 10:48:27 +010030const int kMessagesThrottlingThreshold = 2;
31const int kThrottleRatio = 100000;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +010032} // namespace
33
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020034VCMEncodedFrameCallback::TimingFramesLayerInfo::TimingFramesLayerInfo() {}
35VCMEncodedFrameCallback::TimingFramesLayerInfo::~TimingFramesLayerInfo() {}
36
Peter Boström4f5db112015-10-29 16:53:59 +010037VCMGenericEncoder::VCMGenericEncoder(
38 VideoEncoder* encoder,
Peter Boström4f5db112015-10-29 16:53:59 +010039 VCMEncodedFrameCallback* encoded_frame_callback,
sprang3911c262016-04-15 01:24:14 -070040 bool internal_source)
pbos@webrtc.org891d4832015-02-26 13:15:22 +000041 : encoder_(encoder),
Peter Boström4f5db112015-10-29 16:53:59 +010042 vcm_encoded_frame_callback_(encoded_frame_callback),
sprang3911c262016-04-15 01:24:14 -070043 internal_source_(internal_source),
Erik Språng566124a2018-04-23 12:32:22 +020044 encoder_params_({VideoBitrateAllocation(), 0, 0, 0}),
45 streams_or_svc_num_(0),
Kári Tristan Helgason84ccb2d2018-08-16 14:35:26 +020046 codec_type_(VideoCodecType::kVideoCodecGeneric) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000047
Peter Boström4f5db112015-10-29 16:53:59 +010048VCMGenericEncoder::~VCMGenericEncoder() {}
49
50int32_t VCMGenericEncoder::Release() {
Peter Boström02bafc62016-07-01 12:45:15 +020051 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Peter Boström4fd6cda2016-01-26 10:19:53 +010052 TRACE_EVENT0("webrtc", "VCMGenericEncoder::Release");
Peter Boström4f5db112015-10-29 16:53:59 +010053 return encoder_->Release();
niklase@google.com470e71d2011-07-07 08:21:25 +000054}
55
Peter Boström4f5db112015-10-29 16:53:59 +010056int32_t VCMGenericEncoder::InitEncode(const VideoCodec* settings,
sprang3911c262016-04-15 01:24:14 -070057 int32_t number_of_cores,
58 size_t max_payload_size) {
Peter Boström02bafc62016-07-01 12:45:15 +020059 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
pbosd9eec762015-11-17 06:03:43 -080060 TRACE_EVENT0("webrtc", "VCMGenericEncoder::InitEncode");
ilnik04f4d122017-06-19 07:18:55 -070061 streams_or_svc_num_ = settings->numberOfSimulcastStreams;
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +010062 codec_type_ = settings->codecType;
ilnik04f4d122017-06-19 07:18:55 -070063 if (settings->codecType == kVideoCodecVP9) {
64 streams_or_svc_num_ = settings->VP9().numberOfSpatialLayers;
65 }
66 if (streams_or_svc_num_ == 0)
67 streams_or_svc_num_ = 1;
68
69 vcm_encoded_frame_callback_->SetTimingFramesThresholds(
70 settings->timing_frame_thresholds);
71 vcm_encoded_frame_callback_->OnFrameRateChanged(settings->maxFramerate);
72
sprang3911c262016-04-15 01:24:14 -070073 if (encoder_->InitEncode(settings, number_of_cores, max_payload_size) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010074 RTC_LOG(LS_ERROR) << "Failed to initialize the encoder associated with "
Niels Möller2e1d7842018-02-23 15:41:13 +010075 "codec type: "
Yves Gerey665174f2018-06-19 15:03:05 +020076 << CodecTypeToPayloadString(settings->codecType) << " ("
77 << settings->codecType << ")";
Peter Boström4f5db112015-10-29 16:53:59 +010078 return -1;
79 }
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020080 vcm_encoded_frame_callback_->Reset();
Peter Boström4f5db112015-10-29 16:53:59 +010081 encoder_->RegisterEncodeCompleteCallback(vcm_encoded_frame_callback_);
82 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000083}
84
sprang3911c262016-04-15 01:24:14 -070085int32_t VCMGenericEncoder::Encode(const VideoFrame& frame,
86 const CodecSpecificInfo* codec_specific,
87 const std::vector<FrameType>& frame_types) {
Peter Boström02bafc62016-07-01 12:45:15 +020088 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
pbosd9eec762015-11-17 06:03:43 -080089 TRACE_EVENT1("webrtc", "VCMGenericEncoder::Encode", "timestamp",
sprang3911c262016-04-15 01:24:14 -070090 frame.timestamp());
pbosd9eec762015-11-17 06:03:43 -080091
sprang3911c262016-04-15 01:24:14 -070092 for (FrameType frame_type : frame_types)
pbos22993e12015-10-19 02:39:06 -070093 RTC_DCHECK(frame_type == kVideoFrameKey || frame_type == kVideoFrameDelta);
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +000094
ilnik04f4d122017-06-19 07:18:55 -070095 for (size_t i = 0; i < streams_or_svc_num_; ++i)
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +010096 vcm_encoded_frame_callback_->OnEncodeStarted(frame.timestamp(),
97 frame.render_time_ms(), i);
Peter Boströmb7d9a972015-12-18 16:01:11 +010098
ilnike9973812017-09-12 10:24:46 -070099 return encoder_->Encode(frame, codec_specific, &frame_types);
niklase@google.com470e71d2011-07-07 08:21:25 +0000100}
101
Peter Boström69ccb332015-10-29 16:30:23 +0100102void VCMGenericEncoder::SetEncoderParameters(const EncoderParameters& params) {
Peter Boström02bafc62016-07-01 12:45:15 +0200103 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Peter Boström69ccb332015-10-29 16:30:23 +0100104 bool channel_parameters_have_changed;
105 bool rates_have_changed;
106 {
107 rtc::CritScope lock(&params_lock_);
108 channel_parameters_have_changed =
109 params.loss_rate != encoder_params_.loss_rate ||
110 params.rtt != encoder_params_.rtt;
111 rates_have_changed =
112 params.target_bitrate != encoder_params_.target_bitrate ||
113 params.input_frame_rate != encoder_params_.input_frame_rate;
114 encoder_params_ = params;
115 }
Erik Språng08127a92016-11-16 16:41:30 +0100116 if (channel_parameters_have_changed) {
117 int res = encoder_->SetChannelParameters(params.loss_rate, params.rtt);
118 if (res != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100119 RTC_LOG(LS_WARNING) << "Error set encoder parameters (loss = "
120 << params.loss_rate << ", rtt = " << params.rtt
121 << "): " << res;
Erik Språng08127a92016-11-16 16:41:30 +0100122 }
123 }
Peter Boström69ccb332015-10-29 16:30:23 +0100124 if (rates_have_changed) {
Erik Språng08127a92016-11-16 16:41:30 +0100125 int res = encoder_->SetRateAllocation(params.target_bitrate,
126 params.input_frame_rate);
127 if (res != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100128 RTC_LOG(LS_WARNING) << "Error set encoder rate (total bitrate bps = "
129 << params.target_bitrate.get_sum_bps()
130 << ", framerate = " << params.input_frame_rate
131 << "): " << res;
Erik Språng08127a92016-11-16 16:41:30 +0100132 }
ilnik04f4d122017-06-19 07:18:55 -0700133 vcm_encoded_frame_callback_->OnFrameRateChanged(params.input_frame_rate);
134 for (size_t i = 0; i < streams_or_svc_num_; ++i) {
ilnik04f4d122017-06-19 07:18:55 -0700135 vcm_encoded_frame_callback_->OnTargetBitrateChanged(
Sergey Silkin86684962018-03-28 19:32:37 +0200136 params.target_bitrate.GetSpatialLayerSum(i) / 8, i);
ilnik04f4d122017-06-19 07:18:55 -0700137 }
Peter Boström69ccb332015-10-29 16:30:23 +0100138 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000139}
140
Peter Boström69ccb332015-10-29 16:30:23 +0100141EncoderParameters VCMGenericEncoder::GetEncoderParameters() const {
142 rtc::CritScope lock(&params_lock_);
143 return encoder_params_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000144}
145
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000146int32_t VCMGenericEncoder::RequestFrame(
stefan@webrtc.orgcf216862012-10-25 11:29:51 +0000147 const std::vector<FrameType>& frame_types) {
Peter Boström02bafc62016-07-01 12:45:15 +0200148 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
nissedf2ceb82016-12-15 06:29:53 -0800149
150 // TODO(nisse): Used only with internal source. Delete as soon as
151 // that feature is removed. The only implementation I've been able
nisse01d5a0b2017-05-31 06:33:21 -0700152 // to find ignores what's in the frame. With one exception: It seems
153 // a few test cases, e.g.,
154 // VideoSendStreamTest.VideoSendStreamStopSetEncoderRateToZero, set
155 // internal_source to true and use FakeEncoder. And the latter will
156 // happily encode this 1x1 frame and pass it on down the pipeline.
Yves Gerey665174f2018-06-19 15:03:05 +0200157 return encoder_->Encode(
158 VideoFrame(I420Buffer::Create(1, 1), kVideoRotation_0, 0), NULL,
159 &frame_types);
nissedf2ceb82016-12-15 06:29:53 -0800160 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000161}
162
philipel9d3ab612015-12-21 04:12:39 -0800163bool VCMGenericEncoder::InternalSource() const {
164 return internal_source_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000165}
166
Peter Boströmeb66e802015-06-05 11:08:03 +0200167bool VCMGenericEncoder::SupportsNativeHandle() const {
Peter Boström02bafc62016-07-01 12:45:15 +0200168 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Peter Boströmeb66e802015-06-05 11:08:03 +0200169 return encoder_->SupportsNativeHandle();
170}
171
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000172VCMEncodedFrameCallback::VCMEncodedFrameCallback(
perkj376b1922016-05-02 11:35:24 -0700173 EncodedImageCallback* post_encode_callback,
174 media_optimization::MediaOptimization* media_opt)
175 : internal_source_(false),
176 post_encode_callback_(post_encode_callback),
ilnik04f4d122017-06-19 07:18:55 -0700177 media_opt_(media_opt),
178 framerate_(1),
179 last_timing_frame_time_ms_(-1),
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100180 timing_frames_thresholds_({-1, 0}),
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100181 incorrect_capture_time_logged_messages_(0),
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100182 reordered_frames_logged_messages_(0),
183 stalled_encoder_logged_messages_(0) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200184 absl::optional<AlrExperimentSettings> experiment_settings =
Sebastian Janssoncabe3832018-01-12 10:54:18 +0100185 AlrExperimentSettings::CreateFromFieldTrial(
186 AlrExperimentSettings::kStrictPacingAndProbingExperimentName);
ilnik6d5b4d62017-08-30 03:32:14 -0700187 if (experiment_settings) {
188 experiment_groups_[0] = experiment_settings->group_id + 1;
189 } else {
190 experiment_groups_[0] = 0;
191 }
Sebastian Janssoncabe3832018-01-12 10:54:18 +0100192 experiment_settings = AlrExperimentSettings::CreateFromFieldTrial(
193 AlrExperimentSettings::kScreenshareProbingBweExperimentName);
ilnik6d5b4d62017-08-30 03:32:14 -0700194 if (experiment_settings) {
195 experiment_groups_[1] = experiment_settings->group_id + 1;
196 } else {
197 experiment_groups_[1] = 0;
198 }
199}
niklase@google.com470e71d2011-07-07 08:21:25 +0000200
sprang3911c262016-04-15 01:24:14 -0700201VCMEncodedFrameCallback::~VCMEncodedFrameCallback() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000202
ilnik04f4d122017-06-19 07:18:55 -0700203void VCMEncodedFrameCallback::OnTargetBitrateChanged(
204 size_t bitrate_bytes_per_second,
205 size_t simulcast_svc_idx) {
206 rtc::CritScope crit(&timing_params_lock_);
207 if (timing_frames_info_.size() < simulcast_svc_idx + 1)
208 timing_frames_info_.resize(simulcast_svc_idx + 1);
209 timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec =
210 bitrate_bytes_per_second;
211}
212
213void VCMEncodedFrameCallback::OnFrameRateChanged(size_t framerate) {
214 rtc::CritScope crit(&timing_params_lock_);
215 framerate_ = framerate;
216}
217
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100218void VCMEncodedFrameCallback::OnEncodeStarted(uint32_t rtp_timestamp,
219 int64_t capture_time_ms,
ilnik04f4d122017-06-19 07:18:55 -0700220 size_t simulcast_svc_idx) {
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200221 if (internal_source_) {
222 return;
223 }
ilnik04f4d122017-06-19 07:18:55 -0700224 rtc::CritScope crit(&timing_params_lock_);
225 if (timing_frames_info_.size() < simulcast_svc_idx + 1)
226 timing_frames_info_.resize(simulcast_svc_idx + 1);
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200227 RTC_DCHECK(
228 timing_frames_info_[simulcast_svc_idx].encode_start_list.empty() ||
229 rtc::TimeDiff(capture_time_ms, timing_frames_info_[simulcast_svc_idx]
230 .encode_start_list.back()
231 .capture_time_ms) >= 0);
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +0100232 // If stream is disabled due to low bandwidth OnEncodeStarted still will be
233 // called and have to be ignored.
234 if (timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec == 0)
235 return;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200236 if (timing_frames_info_[simulcast_svc_idx].encode_start_list.size() ==
237 kMaxEncodeStartTimeListSize) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100238 ++stalled_encoder_logged_messages_;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100239 if (stalled_encoder_logged_messages_ <= kMessagesThrottlingThreshold ||
240 stalled_encoder_logged_messages_ % kThrottleRatio == 0) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100241 RTC_LOG(LS_WARNING) << "Too many frames in the encode_start_list."
242 " Did encoder stall?";
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100243 if (stalled_encoder_logged_messages_ == kMessagesThrottlingThreshold) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100244 RTC_LOG(LS_WARNING) << "Too many log messages. Further stalled encoder"
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100245 "warnings will be throttled.";
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100246 }
247 }
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200248 post_encode_callback_->OnDroppedFrame(DropReason::kDroppedByEncoder);
249 timing_frames_info_[simulcast_svc_idx].encode_start_list.pop_front();
250 }
251 timing_frames_info_[simulcast_svc_idx].encode_start_list.emplace_back(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100252 rtp_timestamp, capture_time_ms, rtc::TimeMillis());
ilnik04f4d122017-06-19 07:18:55 -0700253}
254
Danil Chapovalov0040b662018-06-18 10:48:16 +0200255absl::optional<int64_t> VCMEncodedFrameCallback::ExtractEncodeStartTime(
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200256 size_t simulcast_svc_idx,
257 EncodedImage* encoded_image) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200258 absl::optional<int64_t> result;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200259 size_t num_simulcast_svc_streams = timing_frames_info_.size();
260 if (simulcast_svc_idx < num_simulcast_svc_streams) {
261 auto encode_start_list =
262 &timing_frames_info_[simulcast_svc_idx].encode_start_list;
263 // Skip frames for which there was OnEncodeStarted but no OnEncodedImage
264 // call. These are dropped by encoder internally.
265 // Because some hardware encoders don't preserve capture timestamp we
266 // use RTP timestamps here.
267 while (!encode_start_list->empty() &&
268 IsNewerTimestamp(encoded_image->_timeStamp,
269 encode_start_list->front().rtp_timestamp)) {
270 post_encode_callback_->OnDroppedFrame(DropReason::kDroppedByEncoder);
271 encode_start_list->pop_front();
272 }
273 if (encode_start_list->size() > 0 &&
274 encode_start_list->front().rtp_timestamp == encoded_image->_timeStamp) {
275 result.emplace(encode_start_list->front().encode_start_time_ms);
276 if (encoded_image->capture_time_ms_ !=
277 encode_start_list->front().capture_time_ms) {
278 // Force correct capture timestamp.
279 encoded_image->capture_time_ms_ =
280 encode_start_list->front().capture_time_ms;
281 ++incorrect_capture_time_logged_messages_;
282 if (incorrect_capture_time_logged_messages_ <=
283 kMessagesThrottlingThreshold ||
284 incorrect_capture_time_logged_messages_ % kThrottleRatio == 0) {
285 RTC_LOG(LS_WARNING)
286 << "Encoder is not preserving capture timestamps.";
287 if (incorrect_capture_time_logged_messages_ ==
288 kMessagesThrottlingThreshold) {
289 RTC_LOG(LS_WARNING) << "Too many log messages. Further incorrect "
290 "timestamps warnings will be throttled.";
291 }
292 }
293 }
294 encode_start_list->pop_front();
295 } else {
296 ++reordered_frames_logged_messages_;
297 if (reordered_frames_logged_messages_ <= kMessagesThrottlingThreshold ||
298 reordered_frames_logged_messages_ % kThrottleRatio == 0) {
299 RTC_LOG(LS_WARNING) << "Frame with no encode started time recordings. "
300 "Encoder may be reordering frames "
301 "or not preserving RTP timestamps.";
302 if (reordered_frames_logged_messages_ == kMessagesThrottlingThreshold) {
303 RTC_LOG(LS_WARNING) << "Too many log messages. Further frames "
304 "reordering warnings will be throttled.";
305 }
306 }
307 }
308 }
309 return result;
310}
311
312void VCMEncodedFrameCallback::FillTimingInfo(size_t simulcast_svc_idx,
313 EncodedImage* encoded_image) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200314 absl::optional<size_t> outlier_frame_size;
315 absl::optional<int64_t> encode_start_ms;
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200316 uint8_t timing_flags = VideoSendTiming::kNotTriggered;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200317 {
318 rtc::CritScope crit(&timing_params_lock_);
319
320 // Encoders with internal sources do not call OnEncodeStarted
321 // |timing_frames_info_| may be not filled here.
322 if (!internal_source_) {
323 encode_start_ms =
324 ExtractEncodeStartTime(simulcast_svc_idx, encoded_image);
325 }
326
327 if (timing_frames_info_.size() > simulcast_svc_idx) {
328 size_t target_bitrate =
329 timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec;
330 if (framerate_ > 0 && target_bitrate > 0) {
331 // framerate and target bitrate were reported by encoder.
332 size_t average_frame_size = target_bitrate / framerate_;
333 outlier_frame_size.emplace(
334 average_frame_size *
335 timing_frames_thresholds_.outlier_ratio_percent / 100);
336 }
337 }
338
339 // Outliers trigger timing frames, but do not affect scheduled timing
340 // frames.
341 if (outlier_frame_size && encoded_image->_length >= *outlier_frame_size) {
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200342 timing_flags |= VideoSendTiming::kTriggeredBySize;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200343 }
344
345 // Check if it's time to send a timing frame.
346 int64_t timing_frame_delay_ms =
347 encoded_image->capture_time_ms_ - last_timing_frame_time_ms_;
348 // Trigger threshold if it's a first frame, too long passed since the last
349 // timing frame, or we already sent timing frame on a different simulcast
350 // stream with the same capture time.
351 if (last_timing_frame_time_ms_ == -1 ||
352 timing_frame_delay_ms >= timing_frames_thresholds_.delay_ms ||
353 timing_frame_delay_ms == 0) {
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200354 timing_flags |= VideoSendTiming::kTriggeredByTimer;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200355 last_timing_frame_time_ms_ = encoded_image->capture_time_ms_;
356 }
357 } // rtc::CritScope crit(&timing_params_lock_);
358
359 int64_t now_ms = rtc::TimeMillis();
360 // Workaround for chromoting encoder: it passes encode start and finished
361 // timestamps in |timing_| field, but they (together with capture timestamp)
362 // are not in the WebRTC clock.
363 if (internal_source_ && encoded_image->timing_.encode_finish_ms > 0 &&
364 encoded_image->timing_.encode_start_ms > 0) {
365 int64_t clock_offset_ms = now_ms - encoded_image->timing_.encode_finish_ms;
366 // Translate capture timestamp to local WebRTC clock.
367 encoded_image->capture_time_ms_ += clock_offset_ms;
368 encoded_image->_timeStamp =
369 static_cast<uint32_t>(encoded_image->capture_time_ms_ * 90);
370 encode_start_ms.emplace(encoded_image->timing_.encode_start_ms +
371 clock_offset_ms);
372 }
373
374 // If encode start is not available that means that encoder uses internal
375 // source. In that case capture timestamp may be from a different clock with a
376 // drift relative to rtc::TimeMillis(). We can't use it for Timing frames,
377 // because to being sent in the network capture time required to be less than
378 // all the other timestamps.
379 if (encode_start_ms) {
380 encoded_image->SetEncodeTime(*encode_start_ms, now_ms);
381 encoded_image->timing_.flags = timing_flags;
382 } else {
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200383 encoded_image->timing_.flags = VideoSendTiming::kInvalid;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200384 }
385}
386
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700387EncodedImageCallback::Result VCMEncodedFrameCallback::OnEncodedImage(
Peter Boströmb7d9a972015-12-18 16:01:11 +0100388 const EncodedImage& encoded_image,
sprang3911c262016-04-15 01:24:14 -0700389 const CodecSpecificInfo* codec_specific,
390 const RTPFragmentationHeader* fragmentation_header) {
pbosd9eec762015-11-17 06:03:43 -0800391 TRACE_EVENT_INSTANT1("webrtc", "VCMEncodedFrameCallback::Encoded",
Peter Boströmb7d9a972015-12-18 16:01:11 +0100392 "timestamp", encoded_image._timeStamp);
ilnik04f4d122017-06-19 07:18:55 -0700393 size_t simulcast_svc_idx = 0;
394 if (codec_specific->codecType == kVideoCodecVP9) {
395 if (codec_specific->codecSpecific.VP9.num_spatial_layers > 1)
396 simulcast_svc_idx = codec_specific->codecSpecific.VP9.spatial_idx;
397 } else if (codec_specific->codecType == kVideoCodecVP8) {
398 simulcast_svc_idx = codec_specific->codecSpecific.VP8.simulcastIdx;
399 } else if (codec_specific->codecType == kVideoCodecGeneric) {
400 simulcast_svc_idx = codec_specific->codecSpecific.generic.simulcast_idx;
401 } else if (codec_specific->codecType == kVideoCodecH264) {
402 // TODO(ilnik): When h264 simulcast is landed, extract simulcast idx here.
403 }
404
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100405 EncodedImage image_copy(encoded_image);
406
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200407 FillTimingInfo(simulcast_svc_idx, &image_copy);
ilnik04f4d122017-06-19 07:18:55 -0700408
ilnik6d5b4d62017-08-30 03:32:14 -0700409 // Piggyback ALR experiment group id and simulcast id into the content type.
410 uint8_t experiment_id =
411 experiment_groups_[videocontenttypehelpers::IsScreenshare(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100412 image_copy.content_type_)];
ilnik6d5b4d62017-08-30 03:32:14 -0700413
414 // TODO(ilnik): This will force content type extension to be present even
415 // for realtime video. At the expense of miniscule overhead we will get
416 // sliced receive statistics.
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100417 RTC_CHECK(videocontenttypehelpers::SetExperimentId(&image_copy.content_type_,
418 experiment_id));
ilnik6d5b4d62017-08-30 03:32:14 -0700419 // We count simulcast streams from 1 on the wire. That's why we set simulcast
420 // id in content type to +1 of that is actual simulcast index. This is because
421 // value 0 on the wire is reserved for 'no simulcast stream specified'.
422 RTC_CHECK(videocontenttypehelpers::SetSimulcastId(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100423 &image_copy.content_type_, static_cast<uint8_t>(simulcast_svc_idx + 1)));
ilnik6d5b4d62017-08-30 03:32:14 -0700424
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700425 Result result = post_encode_callback_->OnEncodedImage(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100426 image_copy, codec_specific, fragmentation_header);
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700427 if (result.error != Result::OK)
428 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000429
sprang3911c262016-04-15 01:24:14 -0700430 if (media_opt_) {
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100431 media_opt_->UpdateWithEncodedData(image_copy._length,
432 image_copy._frameType);
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700433 if (internal_source_) {
434 // Signal to encoder to drop next frame.
435 result.drop_next_frame = media_opt_->DropFrame();
436 }
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000437 }
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700438 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000439}
440
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000441} // namespace webrtc