blob: 9dd07bc1303065f10adc75b4dadfc2db554a766c [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"
Niels Möller6613f8e2019-01-10 10:30:21 +010025#include "modules/video_coding/media_optimization.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#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
Niels Möller6613f8e2019-01-10 10:30:21 +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_);
Niels Möller6613f8e2019-01-10 10:30:21 +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;
Peter Boström69ccb332015-10-29 16:30:23 +0100115 }
Peter Boström69ccb332015-10-29 16:30:23 +0100116 if (rates_have_changed) {
Niels Möller6613f8e2019-01-10 10:30:21 +0100117 int res = encoder_->SetRateAllocation(params.target_bitrate,
118 params.input_frame_rate);
Erik Språng08127a92016-11-16 16:41:30 +0100119 if (res != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100120 RTC_LOG(LS_WARNING) << "Error set encoder rate (total bitrate bps = "
Niels Möller6613f8e2019-01-10 10:30:21 +0100121 << params.target_bitrate.get_sum_bps()
122 << ", framerate = " << params.input_frame_rate
Mirko Bonadei675513b2017-11-09 11:09:25 +0100123 << "): " << res;
Erik Språng08127a92016-11-16 16:41:30 +0100124 }
Niels Möller6613f8e2019-01-10 10:30:21 +0100125 vcm_encoded_frame_callback_->OnFrameRateChanged(params.input_frame_rate);
ilnik04f4d122017-06-19 07:18:55 -0700126 for (size_t i = 0; i < streams_or_svc_num_; ++i) {
ilnik04f4d122017-06-19 07:18:55 -0700127 vcm_encoded_frame_callback_->OnTargetBitrateChanged(
Niels Möller6613f8e2019-01-10 10:30:21 +0100128 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
Niels Möller6613f8e2019-01-10 10:30:21 +0100133EncoderParameters VCMGenericEncoder::GetEncoderParameters() const {
134 rtc::CritScope lock(&params_lock_);
135 return encoder_params_;
136}
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.
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100149 return encoder_->Encode(VideoFrame::Builder()
150 .set_video_frame_buffer(I420Buffer::Create(1, 1))
151 .set_rotation(kVideoRotation_0)
152 .set_timestamp_us(0)
153 .build(),
154 NULL, &frame_types);
niklase@google.com470e71d2011-07-07 08:21:25 +0000155}
156
philipel9d3ab612015-12-21 04:12:39 -0800157bool VCMGenericEncoder::InternalSource() const {
158 return internal_source_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000159}
160
Erik Språngd3438aa2018-11-08 16:56:43 +0100161VideoEncoder::EncoderInfo VCMGenericEncoder::GetEncoderInfo() const {
Peter Boström02bafc62016-07-01 12:45:15 +0200162 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Erik Språngd3438aa2018-11-08 16:56:43 +0100163 return encoder_->GetEncoderInfo();
Peter Boströmeb66e802015-06-05 11:08:03 +0200164}
165
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000166VCMEncodedFrameCallback::VCMEncodedFrameCallback(
Niels Möller6613f8e2019-01-10 10:30:21 +0100167 EncodedImageCallback* post_encode_callback,
168 media_optimization::MediaOptimization* media_opt)
perkj376b1922016-05-02 11:35:24 -0700169 : internal_source_(false),
170 post_encode_callback_(post_encode_callback),
Niels Möller6613f8e2019-01-10 10:30:21 +0100171 media_opt_(media_opt),
ilnik04f4d122017-06-19 07:18:55 -0700172 framerate_(1),
173 last_timing_frame_time_ms_(-1),
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100174 timing_frames_thresholds_({-1, 0}),
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100175 incorrect_capture_time_logged_messages_(0),
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100176 reordered_frames_logged_messages_(0),
177 stalled_encoder_logged_messages_(0) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200178 absl::optional<AlrExperimentSettings> experiment_settings =
Sebastian Janssoncabe3832018-01-12 10:54:18 +0100179 AlrExperimentSettings::CreateFromFieldTrial(
180 AlrExperimentSettings::kStrictPacingAndProbingExperimentName);
ilnik6d5b4d62017-08-30 03:32:14 -0700181 if (experiment_settings) {
182 experiment_groups_[0] = experiment_settings->group_id + 1;
183 } else {
184 experiment_groups_[0] = 0;
185 }
Sebastian Janssoncabe3832018-01-12 10:54:18 +0100186 experiment_settings = AlrExperimentSettings::CreateFromFieldTrial(
187 AlrExperimentSettings::kScreenshareProbingBweExperimentName);
ilnik6d5b4d62017-08-30 03:32:14 -0700188 if (experiment_settings) {
189 experiment_groups_[1] = experiment_settings->group_id + 1;
190 } else {
191 experiment_groups_[1] = 0;
192 }
193}
niklase@google.com470e71d2011-07-07 08:21:25 +0000194
sprang3911c262016-04-15 01:24:14 -0700195VCMEncodedFrameCallback::~VCMEncodedFrameCallback() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000196
ilnik04f4d122017-06-19 07:18:55 -0700197void VCMEncodedFrameCallback::OnTargetBitrateChanged(
198 size_t bitrate_bytes_per_second,
199 size_t simulcast_svc_idx) {
200 rtc::CritScope crit(&timing_params_lock_);
201 if (timing_frames_info_.size() < simulcast_svc_idx + 1)
202 timing_frames_info_.resize(simulcast_svc_idx + 1);
203 timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec =
204 bitrate_bytes_per_second;
205}
206
207void VCMEncodedFrameCallback::OnFrameRateChanged(size_t framerate) {
208 rtc::CritScope crit(&timing_params_lock_);
209 framerate_ = framerate;
210}
211
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100212void VCMEncodedFrameCallback::OnEncodeStarted(uint32_t rtp_timestamp,
213 int64_t capture_time_ms,
ilnik04f4d122017-06-19 07:18:55 -0700214 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);
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +0100226 // If stream is disabled due to low bandwidth OnEncodeStarted still will be
227 // called and have to be ignored.
228 if (timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec == 0)
229 return;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200230 if (timing_frames_info_[simulcast_svc_idx].encode_start_list.size() ==
231 kMaxEncodeStartTimeListSize) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100232 ++stalled_encoder_logged_messages_;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100233 if (stalled_encoder_logged_messages_ <= kMessagesThrottlingThreshold ||
234 stalled_encoder_logged_messages_ % kThrottleRatio == 0) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100235 RTC_LOG(LS_WARNING) << "Too many frames in the encode_start_list."
236 " Did encoder stall?";
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100237 if (stalled_encoder_logged_messages_ == kMessagesThrottlingThreshold) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100238 RTC_LOG(LS_WARNING) << "Too many log messages. Further stalled encoder"
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100239 "warnings will be throttled.";
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100240 }
241 }
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200242 post_encode_callback_->OnDroppedFrame(DropReason::kDroppedByEncoder);
243 timing_frames_info_[simulcast_svc_idx].encode_start_list.pop_front();
244 }
245 timing_frames_info_[simulcast_svc_idx].encode_start_list.emplace_back(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100246 rtp_timestamp, capture_time_ms, rtc::TimeMillis());
ilnik04f4d122017-06-19 07:18:55 -0700247}
248
Danil Chapovalov0040b662018-06-18 10:48:16 +0200249absl::optional<int64_t> VCMEncodedFrameCallback::ExtractEncodeStartTime(
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200250 size_t simulcast_svc_idx,
251 EncodedImage* encoded_image) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200252 absl::optional<int64_t> result;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200253 size_t num_simulcast_svc_streams = timing_frames_info_.size();
254 if (simulcast_svc_idx < num_simulcast_svc_streams) {
255 auto encode_start_list =
256 &timing_frames_info_[simulcast_svc_idx].encode_start_list;
257 // Skip frames for which there was OnEncodeStarted but no OnEncodedImage
258 // call. These are dropped by encoder internally.
259 // Because some hardware encoders don't preserve capture timestamp we
260 // use RTP timestamps here.
261 while (!encode_start_list->empty() &&
Niels Möller23775882018-08-16 10:24:12 +0200262 IsNewerTimestamp(encoded_image->Timestamp(),
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200263 encode_start_list->front().rtp_timestamp)) {
264 post_encode_callback_->OnDroppedFrame(DropReason::kDroppedByEncoder);
265 encode_start_list->pop_front();
266 }
267 if (encode_start_list->size() > 0 &&
Niels Möller23775882018-08-16 10:24:12 +0200268 encode_start_list->front().rtp_timestamp ==
269 encoded_image->Timestamp()) {
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200270 result.emplace(encode_start_list->front().encode_start_time_ms);
271 if (encoded_image->capture_time_ms_ !=
272 encode_start_list->front().capture_time_ms) {
273 // Force correct capture timestamp.
274 encoded_image->capture_time_ms_ =
275 encode_start_list->front().capture_time_ms;
276 ++incorrect_capture_time_logged_messages_;
277 if (incorrect_capture_time_logged_messages_ <=
278 kMessagesThrottlingThreshold ||
279 incorrect_capture_time_logged_messages_ % kThrottleRatio == 0) {
280 RTC_LOG(LS_WARNING)
281 << "Encoder is not preserving capture timestamps.";
282 if (incorrect_capture_time_logged_messages_ ==
283 kMessagesThrottlingThreshold) {
284 RTC_LOG(LS_WARNING) << "Too many log messages. Further incorrect "
285 "timestamps warnings will be throttled.";
286 }
287 }
288 }
289 encode_start_list->pop_front();
290 } else {
291 ++reordered_frames_logged_messages_;
292 if (reordered_frames_logged_messages_ <= kMessagesThrottlingThreshold ||
293 reordered_frames_logged_messages_ % kThrottleRatio == 0) {
294 RTC_LOG(LS_WARNING) << "Frame with no encode started time recordings. "
295 "Encoder may be reordering frames "
296 "or not preserving RTP timestamps.";
297 if (reordered_frames_logged_messages_ == kMessagesThrottlingThreshold) {
298 RTC_LOG(LS_WARNING) << "Too many log messages. Further frames "
299 "reordering warnings will be throttled.";
300 }
301 }
302 }
303 }
304 return result;
305}
306
307void VCMEncodedFrameCallback::FillTimingInfo(size_t simulcast_svc_idx,
308 EncodedImage* encoded_image) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200309 absl::optional<size_t> outlier_frame_size;
310 absl::optional<int64_t> encode_start_ms;
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200311 uint8_t timing_flags = VideoSendTiming::kNotTriggered;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200312 {
313 rtc::CritScope crit(&timing_params_lock_);
314
315 // Encoders with internal sources do not call OnEncodeStarted
316 // |timing_frames_info_| may be not filled here.
317 if (!internal_source_) {
318 encode_start_ms =
319 ExtractEncodeStartTime(simulcast_svc_idx, encoded_image);
320 }
321
322 if (timing_frames_info_.size() > simulcast_svc_idx) {
323 size_t target_bitrate =
324 timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec;
325 if (framerate_ > 0 && target_bitrate > 0) {
326 // framerate and target bitrate were reported by encoder.
327 size_t average_frame_size = target_bitrate / framerate_;
328 outlier_frame_size.emplace(
329 average_frame_size *
330 timing_frames_thresholds_.outlier_ratio_percent / 100);
331 }
332 }
333
334 // Outliers trigger timing frames, but do not affect scheduled timing
335 // frames.
336 if (outlier_frame_size && encoded_image->_length >= *outlier_frame_size) {
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200337 timing_flags |= VideoSendTiming::kTriggeredBySize;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200338 }
339
340 // Check if it's time to send a timing frame.
341 int64_t timing_frame_delay_ms =
342 encoded_image->capture_time_ms_ - last_timing_frame_time_ms_;
343 // Trigger threshold if it's a first frame, too long passed since the last
344 // timing frame, or we already sent timing frame on a different simulcast
345 // stream with the same capture time.
346 if (last_timing_frame_time_ms_ == -1 ||
347 timing_frame_delay_ms >= timing_frames_thresholds_.delay_ms ||
348 timing_frame_delay_ms == 0) {
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200349 timing_flags |= VideoSendTiming::kTriggeredByTimer;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200350 last_timing_frame_time_ms_ = encoded_image->capture_time_ms_;
351 }
352 } // rtc::CritScope crit(&timing_params_lock_);
353
354 int64_t now_ms = rtc::TimeMillis();
355 // Workaround for chromoting encoder: it passes encode start and finished
356 // timestamps in |timing_| field, but they (together with capture timestamp)
357 // are not in the WebRTC clock.
358 if (internal_source_ && encoded_image->timing_.encode_finish_ms > 0 &&
359 encoded_image->timing_.encode_start_ms > 0) {
360 int64_t clock_offset_ms = now_ms - encoded_image->timing_.encode_finish_ms;
361 // Translate capture timestamp to local WebRTC clock.
362 encoded_image->capture_time_ms_ += clock_offset_ms;
Niels Möller23775882018-08-16 10:24:12 +0200363 encoded_image->SetTimestamp(
364 static_cast<uint32_t>(encoded_image->capture_time_ms_ * 90));
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200365 encode_start_ms.emplace(encoded_image->timing_.encode_start_ms +
366 clock_offset_ms);
367 }
368
369 // If encode start is not available that means that encoder uses internal
370 // source. In that case capture timestamp may be from a different clock with a
371 // drift relative to rtc::TimeMillis(). We can't use it for Timing frames,
372 // because to being sent in the network capture time required to be less than
373 // all the other timestamps.
374 if (encode_start_ms) {
375 encoded_image->SetEncodeTime(*encode_start_ms, now_ms);
376 encoded_image->timing_.flags = timing_flags;
377 } else {
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200378 encoded_image->timing_.flags = VideoSendTiming::kInvalid;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200379 }
380}
381
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700382EncodedImageCallback::Result VCMEncodedFrameCallback::OnEncodedImage(
Peter Boströmb7d9a972015-12-18 16:01:11 +0100383 const EncodedImage& encoded_image,
sprang3911c262016-04-15 01:24:14 -0700384 const CodecSpecificInfo* codec_specific,
385 const RTPFragmentationHeader* fragmentation_header) {
pbosd9eec762015-11-17 06:03:43 -0800386 TRACE_EVENT_INSTANT1("webrtc", "VCMEncodedFrameCallback::Encoded",
Niels Möller23775882018-08-16 10:24:12 +0200387 "timestamp", encoded_image.Timestamp());
Niels Möllerd3b8c632018-08-27 15:33:42 +0200388 const size_t spatial_idx = encoded_image.SpatialIndex().value_or(0);
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100389 EncodedImage image_copy(encoded_image);
390
Niels Möllerd3b8c632018-08-27 15:33:42 +0200391 FillTimingInfo(spatial_idx, &image_copy);
ilnik04f4d122017-06-19 07:18:55 -0700392
ilnik6d5b4d62017-08-30 03:32:14 -0700393 // Piggyback ALR experiment group id and simulcast id into the content type.
394 uint8_t experiment_id =
395 experiment_groups_[videocontenttypehelpers::IsScreenshare(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100396 image_copy.content_type_)];
ilnik6d5b4d62017-08-30 03:32:14 -0700397
398 // TODO(ilnik): This will force content type extension to be present even
399 // for realtime video. At the expense of miniscule overhead we will get
400 // sliced receive statistics.
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100401 RTC_CHECK(videocontenttypehelpers::SetExperimentId(&image_copy.content_type_,
402 experiment_id));
ilnik6d5b4d62017-08-30 03:32:14 -0700403 // We count simulcast streams from 1 on the wire. That's why we set simulcast
404 // id in content type to +1 of that is actual simulcast index. This is because
405 // value 0 on the wire is reserved for 'no simulcast stream specified'.
406 RTC_CHECK(videocontenttypehelpers::SetSimulcastId(
Niels Möllerd3b8c632018-08-27 15:33:42 +0200407 &image_copy.content_type_, static_cast<uint8_t>(spatial_idx + 1)));
ilnik6d5b4d62017-08-30 03:32:14 -0700408
Niels Möller6613f8e2019-01-10 10:30:21 +0100409 Result result = post_encode_callback_->OnEncodedImage(
410 image_copy, codec_specific, fragmentation_header);
411 if (result.error != Result::OK)
412 return result;
413
414 if (media_opt_) {
415 media_opt_->UpdateWithEncodedData(image_copy._length,
416 image_copy._frameType);
417 if (internal_source_) {
418 // Signal to encoder to drop next frame.
419 result.drop_next_frame = media_opt_->DropFrame();
420 }
421 }
422 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000423}
424
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000425} // namespace webrtc