blob: 1f24159e74a8844aaf7e62b581e89995982cfa22 [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
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <cstddef>
14#include <cstdint>
philipel9d3ab612015-12-21 04:12:39 -080015#include <vector>
16
Danil Chapovalov0040b662018-06-18 10:48:16 +020017#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/video/i420_buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "api/video/video_content_type.h"
20#include "api/video/video_frame_buffer.h"
21#include "api/video/video_rotation.h"
22#include "api/video/video_timing.h"
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +010023#include "modules/include/module_common_types_public.h"
Yves Gerey3e707812018-11-28 16:47:49 +010024#include "modules/video_coding/include/video_coding_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/video_coding/media_optimization.h"
26#include "rtc_base/checks.h"
Sebastian Janssoncabe3832018-01-12 10:54:18 +010027#include "rtc_base/experiments/alr_experiment.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/logging.h"
29#include "rtc_base/timeutils.h"
30#include "rtc_base/trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000031
32namespace webrtc {
Sergey Ulanov525df3f2016-08-02 17:46:41 -070033
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +010034namespace {
Ilya Nikolaevskiy269674f2018-01-16 10:48:27 +010035const int kMessagesThrottlingThreshold = 2;
36const int kThrottleRatio = 100000;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +010037} // namespace
38
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020039VCMEncodedFrameCallback::TimingFramesLayerInfo::TimingFramesLayerInfo() {}
40VCMEncodedFrameCallback::TimingFramesLayerInfo::~TimingFramesLayerInfo() {}
41
Peter Boström4f5db112015-10-29 16:53:59 +010042VCMGenericEncoder::VCMGenericEncoder(
43 VideoEncoder* encoder,
Peter Boström4f5db112015-10-29 16:53:59 +010044 VCMEncodedFrameCallback* encoded_frame_callback,
sprang3911c262016-04-15 01:24:14 -070045 bool internal_source)
pbos@webrtc.org891d4832015-02-26 13:15:22 +000046 : encoder_(encoder),
Peter Boström4f5db112015-10-29 16:53:59 +010047 vcm_encoded_frame_callback_(encoded_frame_callback),
sprang3911c262016-04-15 01:24:14 -070048 internal_source_(internal_source),
Erik Språng566124a2018-04-23 12:32:22 +020049 streams_or_svc_num_(0),
Kári Tristan Helgason84ccb2d2018-08-16 14:35:26 +020050 codec_type_(VideoCodecType::kVideoCodecGeneric) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000051
Peter Boström4f5db112015-10-29 16:53:59 +010052VCMGenericEncoder::~VCMGenericEncoder() {}
53
54int32_t VCMGenericEncoder::Release() {
Peter Boström02bafc62016-07-01 12:45:15 +020055 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Peter Boström4fd6cda2016-01-26 10:19:53 +010056 TRACE_EVENT0("webrtc", "VCMGenericEncoder::Release");
Peter Boström4f5db112015-10-29 16:53:59 +010057 return encoder_->Release();
niklase@google.com470e71d2011-07-07 08:21:25 +000058}
59
Peter Boström4f5db112015-10-29 16:53:59 +010060int32_t VCMGenericEncoder::InitEncode(const VideoCodec* settings,
sprang3911c262016-04-15 01:24:14 -070061 int32_t number_of_cores,
62 size_t max_payload_size) {
Peter Boström02bafc62016-07-01 12:45:15 +020063 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
pbosd9eec762015-11-17 06:03:43 -080064 TRACE_EVENT0("webrtc", "VCMGenericEncoder::InitEncode");
ilnik04f4d122017-06-19 07:18:55 -070065 streams_or_svc_num_ = settings->numberOfSimulcastStreams;
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +010066 codec_type_ = settings->codecType;
ilnik04f4d122017-06-19 07:18:55 -070067 if (settings->codecType == kVideoCodecVP9) {
68 streams_or_svc_num_ = settings->VP9().numberOfSpatialLayers;
69 }
70 if (streams_or_svc_num_ == 0)
71 streams_or_svc_num_ = 1;
72
73 vcm_encoded_frame_callback_->SetTimingFramesThresholds(
74 settings->timing_frame_thresholds);
75 vcm_encoded_frame_callback_->OnFrameRateChanged(settings->maxFramerate);
76
sprang3911c262016-04-15 01:24:14 -070077 if (encoder_->InitEncode(settings, number_of_cores, max_payload_size) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010078 RTC_LOG(LS_ERROR) << "Failed to initialize the encoder associated with "
Niels Möller2e1d7842018-02-23 15:41:13 +010079 "codec type: "
Yves Gerey665174f2018-06-19 15:03:05 +020080 << CodecTypeToPayloadString(settings->codecType) << " ("
81 << settings->codecType << ")";
Peter Boström4f5db112015-10-29 16:53:59 +010082 return -1;
83 }
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020084 vcm_encoded_frame_callback_->Reset();
Peter Boström4f5db112015-10-29 16:53:59 +010085 encoder_->RegisterEncodeCompleteCallback(vcm_encoded_frame_callback_);
86 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000087}
88
sprang3911c262016-04-15 01:24:14 -070089int32_t VCMGenericEncoder::Encode(const VideoFrame& frame,
90 const CodecSpecificInfo* codec_specific,
91 const std::vector<FrameType>& frame_types) {
Peter Boström02bafc62016-07-01 12:45:15 +020092 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
pbosd9eec762015-11-17 06:03:43 -080093 TRACE_EVENT1("webrtc", "VCMGenericEncoder::Encode", "timestamp",
sprang3911c262016-04-15 01:24:14 -070094 frame.timestamp());
pbosd9eec762015-11-17 06:03:43 -080095
sprang3911c262016-04-15 01:24:14 -070096 for (FrameType frame_type : frame_types)
pbos22993e12015-10-19 02:39:06 -070097 RTC_DCHECK(frame_type == kVideoFrameKey || frame_type == kVideoFrameDelta);
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +000098
ilnik04f4d122017-06-19 07:18:55 -070099 for (size_t i = 0; i < streams_or_svc_num_; ++i)
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100100 vcm_encoded_frame_callback_->OnEncodeStarted(frame.timestamp(),
101 frame.render_time_ms(), i);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100102
ilnike9973812017-09-12 10:24:46 -0700103 return encoder_->Encode(frame, codec_specific, &frame_types);
niklase@google.com470e71d2011-07-07 08:21:25 +0000104}
105
Peter Boström69ccb332015-10-29 16:30:23 +0100106void VCMGenericEncoder::SetEncoderParameters(const EncoderParameters& params) {
Peter Boström02bafc62016-07-01 12:45:15 +0200107 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Peter Boström69ccb332015-10-29 16:30:23 +0100108 bool rates_have_changed;
109 {
110 rtc::CritScope lock(&params_lock_);
Peter Boström69ccb332015-10-29 16:30:23 +0100111 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 }
Peter Boström69ccb332015-10-29 16:30:23 +0100116 if (rates_have_changed) {
Erik Språng08127a92016-11-16 16:41:30 +0100117 int res = encoder_->SetRateAllocation(params.target_bitrate,
118 params.input_frame_rate);
119 if (res != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100120 RTC_LOG(LS_WARNING) << "Error set encoder rate (total bitrate bps = "
121 << params.target_bitrate.get_sum_bps()
122 << ", framerate = " << params.input_frame_rate
123 << "): " << res;
Erik Språng08127a92016-11-16 16:41:30 +0100124 }
ilnik04f4d122017-06-19 07:18:55 -0700125 vcm_encoded_frame_callback_->OnFrameRateChanged(params.input_frame_rate);
126 for (size_t i = 0; i < streams_or_svc_num_; ++i) {
ilnik04f4d122017-06-19 07:18:55 -0700127 vcm_encoded_frame_callback_->OnTargetBitrateChanged(
Sergey Silkin86684962018-03-28 19:32:37 +0200128 params.target_bitrate.GetSpatialLayerSum(i) / 8, i);
ilnik04f4d122017-06-19 07:18:55 -0700129 }
Peter Boström69ccb332015-10-29 16:30:23 +0100130 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000131}
132
Peter Boström69ccb332015-10-29 16:30:23 +0100133EncoderParameters VCMGenericEncoder::GetEncoderParameters() const {
134 rtc::CritScope lock(&params_lock_);
135 return encoder_params_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000136}
137
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000138int32_t VCMGenericEncoder::RequestFrame(
stefan@webrtc.orgcf216862012-10-25 11:29:51 +0000139 const std::vector<FrameType>& frame_types) {
Peter Boström02bafc62016-07-01 12:45:15 +0200140 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
nissedf2ceb82016-12-15 06:29:53 -0800141
142 // TODO(nisse): Used only with internal source. Delete as soon as
143 // that feature is removed. The only implementation I've been able
nisse01d5a0b2017-05-31 06:33:21 -0700144 // to find ignores what's in the frame. With one exception: It seems
145 // a few test cases, e.g.,
146 // VideoSendStreamTest.VideoSendStreamStopSetEncoderRateToZero, set
147 // internal_source to true and use FakeEncoder. And the latter will
148 // happily encode this 1x1 frame and pass it on down the pipeline.
Yves Gerey665174f2018-06-19 15:03:05 +0200149 return encoder_->Encode(
150 VideoFrame(I420Buffer::Create(1, 1), kVideoRotation_0, 0), NULL,
151 &frame_types);
niklase@google.com470e71d2011-07-07 08:21:25 +0000152}
153
philipel9d3ab612015-12-21 04:12:39 -0800154bool VCMGenericEncoder::InternalSource() const {
155 return internal_source_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000156}
157
Erik Språngd3438aa2018-11-08 16:56:43 +0100158VideoEncoder::EncoderInfo VCMGenericEncoder::GetEncoderInfo() const {
Peter Boström02bafc62016-07-01 12:45:15 +0200159 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Erik Språngd3438aa2018-11-08 16:56:43 +0100160 return encoder_->GetEncoderInfo();
Peter Boströmeb66e802015-06-05 11:08:03 +0200161}
162
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000163VCMEncodedFrameCallback::VCMEncodedFrameCallback(
perkj376b1922016-05-02 11:35:24 -0700164 EncodedImageCallback* post_encode_callback,
165 media_optimization::MediaOptimization* media_opt)
166 : internal_source_(false),
167 post_encode_callback_(post_encode_callback),
ilnik04f4d122017-06-19 07:18:55 -0700168 media_opt_(media_opt),
169 framerate_(1),
170 last_timing_frame_time_ms_(-1),
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100171 timing_frames_thresholds_({-1, 0}),
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100172 incorrect_capture_time_logged_messages_(0),
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100173 reordered_frames_logged_messages_(0),
174 stalled_encoder_logged_messages_(0) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200175 absl::optional<AlrExperimentSettings> experiment_settings =
Sebastian Janssoncabe3832018-01-12 10:54:18 +0100176 AlrExperimentSettings::CreateFromFieldTrial(
177 AlrExperimentSettings::kStrictPacingAndProbingExperimentName);
ilnik6d5b4d62017-08-30 03:32:14 -0700178 if (experiment_settings) {
179 experiment_groups_[0] = experiment_settings->group_id + 1;
180 } else {
181 experiment_groups_[0] = 0;
182 }
Sebastian Janssoncabe3832018-01-12 10:54:18 +0100183 experiment_settings = AlrExperimentSettings::CreateFromFieldTrial(
184 AlrExperimentSettings::kScreenshareProbingBweExperimentName);
ilnik6d5b4d62017-08-30 03:32:14 -0700185 if (experiment_settings) {
186 experiment_groups_[1] = experiment_settings->group_id + 1;
187 } else {
188 experiment_groups_[1] = 0;
189 }
190}
niklase@google.com470e71d2011-07-07 08:21:25 +0000191
sprang3911c262016-04-15 01:24:14 -0700192VCMEncodedFrameCallback::~VCMEncodedFrameCallback() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000193
ilnik04f4d122017-06-19 07:18:55 -0700194void VCMEncodedFrameCallback::OnTargetBitrateChanged(
195 size_t bitrate_bytes_per_second,
196 size_t simulcast_svc_idx) {
197 rtc::CritScope crit(&timing_params_lock_);
198 if (timing_frames_info_.size() < simulcast_svc_idx + 1)
199 timing_frames_info_.resize(simulcast_svc_idx + 1);
200 timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec =
201 bitrate_bytes_per_second;
202}
203
204void VCMEncodedFrameCallback::OnFrameRateChanged(size_t framerate) {
205 rtc::CritScope crit(&timing_params_lock_);
206 framerate_ = framerate;
207}
208
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100209void VCMEncodedFrameCallback::OnEncodeStarted(uint32_t rtp_timestamp,
210 int64_t capture_time_ms,
ilnik04f4d122017-06-19 07:18:55 -0700211 size_t simulcast_svc_idx) {
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200212 if (internal_source_) {
213 return;
214 }
ilnik04f4d122017-06-19 07:18:55 -0700215 rtc::CritScope crit(&timing_params_lock_);
216 if (timing_frames_info_.size() < simulcast_svc_idx + 1)
217 timing_frames_info_.resize(simulcast_svc_idx + 1);
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200218 RTC_DCHECK(
219 timing_frames_info_[simulcast_svc_idx].encode_start_list.empty() ||
220 rtc::TimeDiff(capture_time_ms, timing_frames_info_[simulcast_svc_idx]
221 .encode_start_list.back()
222 .capture_time_ms) >= 0);
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +0100223 // If stream is disabled due to low bandwidth OnEncodeStarted still will be
224 // called and have to be ignored.
225 if (timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec == 0)
226 return;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200227 if (timing_frames_info_[simulcast_svc_idx].encode_start_list.size() ==
228 kMaxEncodeStartTimeListSize) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100229 ++stalled_encoder_logged_messages_;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100230 if (stalled_encoder_logged_messages_ <= kMessagesThrottlingThreshold ||
231 stalled_encoder_logged_messages_ % kThrottleRatio == 0) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100232 RTC_LOG(LS_WARNING) << "Too many frames in the encode_start_list."
233 " Did encoder stall?";
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100234 if (stalled_encoder_logged_messages_ == kMessagesThrottlingThreshold) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100235 RTC_LOG(LS_WARNING) << "Too many log messages. Further stalled encoder"
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100236 "warnings will be throttled.";
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100237 }
238 }
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200239 post_encode_callback_->OnDroppedFrame(DropReason::kDroppedByEncoder);
240 timing_frames_info_[simulcast_svc_idx].encode_start_list.pop_front();
241 }
242 timing_frames_info_[simulcast_svc_idx].encode_start_list.emplace_back(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100243 rtp_timestamp, capture_time_ms, rtc::TimeMillis());
ilnik04f4d122017-06-19 07:18:55 -0700244}
245
Danil Chapovalov0040b662018-06-18 10:48:16 +0200246absl::optional<int64_t> VCMEncodedFrameCallback::ExtractEncodeStartTime(
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200247 size_t simulcast_svc_idx,
248 EncodedImage* encoded_image) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200249 absl::optional<int64_t> result;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200250 size_t num_simulcast_svc_streams = timing_frames_info_.size();
251 if (simulcast_svc_idx < num_simulcast_svc_streams) {
252 auto encode_start_list =
253 &timing_frames_info_[simulcast_svc_idx].encode_start_list;
254 // Skip frames for which there was OnEncodeStarted but no OnEncodedImage
255 // call. These are dropped by encoder internally.
256 // Because some hardware encoders don't preserve capture timestamp we
257 // use RTP timestamps here.
258 while (!encode_start_list->empty() &&
Niels Möller23775882018-08-16 10:24:12 +0200259 IsNewerTimestamp(encoded_image->Timestamp(),
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200260 encode_start_list->front().rtp_timestamp)) {
261 post_encode_callback_->OnDroppedFrame(DropReason::kDroppedByEncoder);
262 encode_start_list->pop_front();
263 }
264 if (encode_start_list->size() > 0 &&
Niels Möller23775882018-08-16 10:24:12 +0200265 encode_start_list->front().rtp_timestamp ==
266 encoded_image->Timestamp()) {
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200267 result.emplace(encode_start_list->front().encode_start_time_ms);
268 if (encoded_image->capture_time_ms_ !=
269 encode_start_list->front().capture_time_ms) {
270 // Force correct capture timestamp.
271 encoded_image->capture_time_ms_ =
272 encode_start_list->front().capture_time_ms;
273 ++incorrect_capture_time_logged_messages_;
274 if (incorrect_capture_time_logged_messages_ <=
275 kMessagesThrottlingThreshold ||
276 incorrect_capture_time_logged_messages_ % kThrottleRatio == 0) {
277 RTC_LOG(LS_WARNING)
278 << "Encoder is not preserving capture timestamps.";
279 if (incorrect_capture_time_logged_messages_ ==
280 kMessagesThrottlingThreshold) {
281 RTC_LOG(LS_WARNING) << "Too many log messages. Further incorrect "
282 "timestamps warnings will be throttled.";
283 }
284 }
285 }
286 encode_start_list->pop_front();
287 } else {
288 ++reordered_frames_logged_messages_;
289 if (reordered_frames_logged_messages_ <= kMessagesThrottlingThreshold ||
290 reordered_frames_logged_messages_ % kThrottleRatio == 0) {
291 RTC_LOG(LS_WARNING) << "Frame with no encode started time recordings. "
292 "Encoder may be reordering frames "
293 "or not preserving RTP timestamps.";
294 if (reordered_frames_logged_messages_ == kMessagesThrottlingThreshold) {
295 RTC_LOG(LS_WARNING) << "Too many log messages. Further frames "
296 "reordering warnings will be throttled.";
297 }
298 }
299 }
300 }
301 return result;
302}
303
304void VCMEncodedFrameCallback::FillTimingInfo(size_t simulcast_svc_idx,
305 EncodedImage* encoded_image) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200306 absl::optional<size_t> outlier_frame_size;
307 absl::optional<int64_t> encode_start_ms;
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200308 uint8_t timing_flags = VideoSendTiming::kNotTriggered;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200309 {
310 rtc::CritScope crit(&timing_params_lock_);
311
312 // Encoders with internal sources do not call OnEncodeStarted
313 // |timing_frames_info_| may be not filled here.
314 if (!internal_source_) {
315 encode_start_ms =
316 ExtractEncodeStartTime(simulcast_svc_idx, encoded_image);
317 }
318
319 if (timing_frames_info_.size() > simulcast_svc_idx) {
320 size_t target_bitrate =
321 timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec;
322 if (framerate_ > 0 && target_bitrate > 0) {
323 // framerate and target bitrate were reported by encoder.
324 size_t average_frame_size = target_bitrate / framerate_;
325 outlier_frame_size.emplace(
326 average_frame_size *
327 timing_frames_thresholds_.outlier_ratio_percent / 100);
328 }
329 }
330
331 // Outliers trigger timing frames, but do not affect scheduled timing
332 // frames.
333 if (outlier_frame_size && encoded_image->_length >= *outlier_frame_size) {
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200334 timing_flags |= VideoSendTiming::kTriggeredBySize;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200335 }
336
337 // Check if it's time to send a timing frame.
338 int64_t timing_frame_delay_ms =
339 encoded_image->capture_time_ms_ - last_timing_frame_time_ms_;
340 // Trigger threshold if it's a first frame, too long passed since the last
341 // timing frame, or we already sent timing frame on a different simulcast
342 // stream with the same capture time.
343 if (last_timing_frame_time_ms_ == -1 ||
344 timing_frame_delay_ms >= timing_frames_thresholds_.delay_ms ||
345 timing_frame_delay_ms == 0) {
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200346 timing_flags |= VideoSendTiming::kTriggeredByTimer;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200347 last_timing_frame_time_ms_ = encoded_image->capture_time_ms_;
348 }
349 } // rtc::CritScope crit(&timing_params_lock_);
350
351 int64_t now_ms = rtc::TimeMillis();
352 // Workaround for chromoting encoder: it passes encode start and finished
353 // timestamps in |timing_| field, but they (together with capture timestamp)
354 // are not in the WebRTC clock.
355 if (internal_source_ && encoded_image->timing_.encode_finish_ms > 0 &&
356 encoded_image->timing_.encode_start_ms > 0) {
357 int64_t clock_offset_ms = now_ms - encoded_image->timing_.encode_finish_ms;
358 // Translate capture timestamp to local WebRTC clock.
359 encoded_image->capture_time_ms_ += clock_offset_ms;
Niels Möller23775882018-08-16 10:24:12 +0200360 encoded_image->SetTimestamp(
361 static_cast<uint32_t>(encoded_image->capture_time_ms_ * 90));
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200362 encode_start_ms.emplace(encoded_image->timing_.encode_start_ms +
363 clock_offset_ms);
364 }
365
366 // If encode start is not available that means that encoder uses internal
367 // source. In that case capture timestamp may be from a different clock with a
368 // drift relative to rtc::TimeMillis(). We can't use it for Timing frames,
369 // because to being sent in the network capture time required to be less than
370 // all the other timestamps.
371 if (encode_start_ms) {
372 encoded_image->SetEncodeTime(*encode_start_ms, now_ms);
373 encoded_image->timing_.flags = timing_flags;
374 } else {
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200375 encoded_image->timing_.flags = VideoSendTiming::kInvalid;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200376 }
377}
378
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700379EncodedImageCallback::Result VCMEncodedFrameCallback::OnEncodedImage(
Peter Boströmb7d9a972015-12-18 16:01:11 +0100380 const EncodedImage& encoded_image,
sprang3911c262016-04-15 01:24:14 -0700381 const CodecSpecificInfo* codec_specific,
382 const RTPFragmentationHeader* fragmentation_header) {
pbosd9eec762015-11-17 06:03:43 -0800383 TRACE_EVENT_INSTANT1("webrtc", "VCMEncodedFrameCallback::Encoded",
Niels Möller23775882018-08-16 10:24:12 +0200384 "timestamp", encoded_image.Timestamp());
Niels Möllerd3b8c632018-08-27 15:33:42 +0200385 const size_t spatial_idx = encoded_image.SpatialIndex().value_or(0);
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100386 EncodedImage image_copy(encoded_image);
387
Niels Möllerd3b8c632018-08-27 15:33:42 +0200388 FillTimingInfo(spatial_idx, &image_copy);
ilnik04f4d122017-06-19 07:18:55 -0700389
ilnik6d5b4d62017-08-30 03:32:14 -0700390 // Piggyback ALR experiment group id and simulcast id into the content type.
391 uint8_t experiment_id =
392 experiment_groups_[videocontenttypehelpers::IsScreenshare(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100393 image_copy.content_type_)];
ilnik6d5b4d62017-08-30 03:32:14 -0700394
395 // TODO(ilnik): This will force content type extension to be present even
396 // for realtime video. At the expense of miniscule overhead we will get
397 // sliced receive statistics.
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100398 RTC_CHECK(videocontenttypehelpers::SetExperimentId(&image_copy.content_type_,
399 experiment_id));
ilnik6d5b4d62017-08-30 03:32:14 -0700400 // We count simulcast streams from 1 on the wire. That's why we set simulcast
401 // id in content type to +1 of that is actual simulcast index. This is because
402 // value 0 on the wire is reserved for 'no simulcast stream specified'.
403 RTC_CHECK(videocontenttypehelpers::SetSimulcastId(
Niels Möllerd3b8c632018-08-27 15:33:42 +0200404 &image_copy.content_type_, static_cast<uint8_t>(spatial_idx + 1)));
ilnik6d5b4d62017-08-30 03:32:14 -0700405
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700406 Result result = post_encode_callback_->OnEncodedImage(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100407 image_copy, codec_specific, fragmentation_header);
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700408 if (result.error != Result::OK)
409 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000410
sprang3911c262016-04-15 01:24:14 -0700411 if (media_opt_) {
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100412 media_opt_->UpdateWithEncodedData(image_copy._length,
413 image_copy._frameType);
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700414 if (internal_source_) {
415 // Signal to encoder to drop next frame.
416 result.drop_next_frame = media_opt_->DropFrame();
417 }
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000418 }
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700419 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000420}
421
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000422} // namespace webrtc