blob: 24a4678404125aedb3d8421b5fc1369f57a72824 [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 encoder_params_({VideoBitrateAllocation(), 0, 0, 0}),
50 streams_or_svc_num_(0),
Kári Tristan Helgason84ccb2d2018-08-16 14:35:26 +020051 codec_type_(VideoCodecType::kVideoCodecGeneric) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000052
Peter Boström4f5db112015-10-29 16:53:59 +010053VCMGenericEncoder::~VCMGenericEncoder() {}
54
55int32_t VCMGenericEncoder::Release() {
Peter Boström02bafc62016-07-01 12:45:15 +020056 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Peter Boström4fd6cda2016-01-26 10:19:53 +010057 TRACE_EVENT0("webrtc", "VCMGenericEncoder::Release");
Peter Boström4f5db112015-10-29 16:53:59 +010058 return encoder_->Release();
niklase@google.com470e71d2011-07-07 08:21:25 +000059}
60
Peter Boström4f5db112015-10-29 16:53:59 +010061int32_t VCMGenericEncoder::InitEncode(const VideoCodec* settings,
sprang3911c262016-04-15 01:24:14 -070062 int32_t number_of_cores,
63 size_t max_payload_size) {
Peter Boström02bafc62016-07-01 12:45:15 +020064 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
pbosd9eec762015-11-17 06:03:43 -080065 TRACE_EVENT0("webrtc", "VCMGenericEncoder::InitEncode");
ilnik04f4d122017-06-19 07:18:55 -070066 streams_or_svc_num_ = settings->numberOfSimulcastStreams;
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +010067 codec_type_ = settings->codecType;
ilnik04f4d122017-06-19 07:18:55 -070068 if (settings->codecType == kVideoCodecVP9) {
69 streams_or_svc_num_ = settings->VP9().numberOfSpatialLayers;
70 }
71 if (streams_or_svc_num_ == 0)
72 streams_or_svc_num_ = 1;
73
74 vcm_encoded_frame_callback_->SetTimingFramesThresholds(
75 settings->timing_frame_thresholds);
76 vcm_encoded_frame_callback_->OnFrameRateChanged(settings->maxFramerate);
77
sprang3911c262016-04-15 01:24:14 -070078 if (encoder_->InitEncode(settings, number_of_cores, max_payload_size) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010079 RTC_LOG(LS_ERROR) << "Failed to initialize the encoder associated with "
Niels Möller2e1d7842018-02-23 15:41:13 +010080 "codec type: "
Yves Gerey665174f2018-06-19 15:03:05 +020081 << CodecTypeToPayloadString(settings->codecType) << " ("
82 << settings->codecType << ")";
Peter Boström4f5db112015-10-29 16:53:59 +010083 return -1;
84 }
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020085 vcm_encoded_frame_callback_->Reset();
Peter Boström4f5db112015-10-29 16:53:59 +010086 encoder_->RegisterEncodeCompleteCallback(vcm_encoded_frame_callback_);
87 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000088}
89
sprang3911c262016-04-15 01:24:14 -070090int32_t VCMGenericEncoder::Encode(const VideoFrame& frame,
91 const CodecSpecificInfo* codec_specific,
92 const std::vector<FrameType>& frame_types) {
Peter Boström02bafc62016-07-01 12:45:15 +020093 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
pbosd9eec762015-11-17 06:03:43 -080094 TRACE_EVENT1("webrtc", "VCMGenericEncoder::Encode", "timestamp",
sprang3911c262016-04-15 01:24:14 -070095 frame.timestamp());
pbosd9eec762015-11-17 06:03:43 -080096
sprang3911c262016-04-15 01:24:14 -070097 for (FrameType frame_type : frame_types)
pbos22993e12015-10-19 02:39:06 -070098 RTC_DCHECK(frame_type == kVideoFrameKey || frame_type == kVideoFrameDelta);
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +000099
ilnik04f4d122017-06-19 07:18:55 -0700100 for (size_t i = 0; i < streams_or_svc_num_; ++i)
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100101 vcm_encoded_frame_callback_->OnEncodeStarted(frame.timestamp(),
102 frame.render_time_ms(), i);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100103
ilnike9973812017-09-12 10:24:46 -0700104 return encoder_->Encode(frame, codec_specific, &frame_types);
niklase@google.com470e71d2011-07-07 08:21:25 +0000105}
106
Peter Boström69ccb332015-10-29 16:30:23 +0100107void VCMGenericEncoder::SetEncoderParameters(const EncoderParameters& params) {
Peter Boström02bafc62016-07-01 12:45:15 +0200108 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Peter Boström69ccb332015-10-29 16:30:23 +0100109 bool rates_have_changed;
110 {
111 rtc::CritScope lock(&params_lock_);
Peter Boström69ccb332015-10-29 16:30:23 +0100112 rates_have_changed =
113 params.target_bitrate != encoder_params_.target_bitrate ||
114 params.input_frame_rate != encoder_params_.input_frame_rate;
115 encoder_params_ = params;
116 }
Peter Boström69ccb332015-10-29 16:30:23 +0100117 if (rates_have_changed) {
Erik Språng08127a92016-11-16 16:41:30 +0100118 int res = encoder_->SetRateAllocation(params.target_bitrate,
119 params.input_frame_rate);
120 if (res != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100121 RTC_LOG(LS_WARNING) << "Error set encoder rate (total bitrate bps = "
122 << params.target_bitrate.get_sum_bps()
123 << ", framerate = " << params.input_frame_rate
124 << "): " << res;
Erik Språng08127a92016-11-16 16:41:30 +0100125 }
ilnik04f4d122017-06-19 07:18:55 -0700126 vcm_encoded_frame_callback_->OnFrameRateChanged(params.input_frame_rate);
127 for (size_t i = 0; i < streams_or_svc_num_; ++i) {
ilnik04f4d122017-06-19 07:18:55 -0700128 vcm_encoded_frame_callback_->OnTargetBitrateChanged(
Sergey Silkin86684962018-03-28 19:32:37 +0200129 params.target_bitrate.GetSpatialLayerSum(i) / 8, i);
ilnik04f4d122017-06-19 07:18:55 -0700130 }
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
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000139int32_t VCMGenericEncoder::RequestFrame(
stefan@webrtc.orgcf216862012-10-25 11:29:51 +0000140 const std::vector<FrameType>& frame_types) {
Peter Boström02bafc62016-07-01 12:45:15 +0200141 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
nissedf2ceb82016-12-15 06:29:53 -0800142
143 // TODO(nisse): Used only with internal source. Delete as soon as
144 // that feature is removed. The only implementation I've been able
nisse01d5a0b2017-05-31 06:33:21 -0700145 // to find ignores what's in the frame. With one exception: It seems
146 // a few test cases, e.g.,
147 // VideoSendStreamTest.VideoSendStreamStopSetEncoderRateToZero, set
148 // internal_source to true and use FakeEncoder. And the latter will
149 // happily encode this 1x1 frame and pass it on down the pipeline.
Yves Gerey665174f2018-06-19 15:03:05 +0200150 return encoder_->Encode(
151 VideoFrame(I420Buffer::Create(1, 1), kVideoRotation_0, 0), NULL,
152 &frame_types);
nissedf2ceb82016-12-15 06:29:53 -0800153 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000154}
155
philipel9d3ab612015-12-21 04:12:39 -0800156bool VCMGenericEncoder::InternalSource() const {
157 return internal_source_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000158}
159
Erik Språngd3438aa2018-11-08 16:56:43 +0100160VideoEncoder::EncoderInfo VCMGenericEncoder::GetEncoderInfo() const {
Peter Boström02bafc62016-07-01 12:45:15 +0200161 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Erik Språngd3438aa2018-11-08 16:56:43 +0100162 return encoder_->GetEncoderInfo();
Peter Boströmeb66e802015-06-05 11:08:03 +0200163}
164
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000165VCMEncodedFrameCallback::VCMEncodedFrameCallback(
perkj376b1922016-05-02 11:35:24 -0700166 EncodedImageCallback* post_encode_callback,
167 media_optimization::MediaOptimization* media_opt)
168 : internal_source_(false),
169 post_encode_callback_(post_encode_callback),
ilnik04f4d122017-06-19 07:18:55 -0700170 media_opt_(media_opt),
171 framerate_(1),
172 last_timing_frame_time_ms_(-1),
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100173 timing_frames_thresholds_({-1, 0}),
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100174 incorrect_capture_time_logged_messages_(0),
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100175 reordered_frames_logged_messages_(0),
176 stalled_encoder_logged_messages_(0) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200177 absl::optional<AlrExperimentSettings> experiment_settings =
Sebastian Janssoncabe3832018-01-12 10:54:18 +0100178 AlrExperimentSettings::CreateFromFieldTrial(
179 AlrExperimentSettings::kStrictPacingAndProbingExperimentName);
ilnik6d5b4d62017-08-30 03:32:14 -0700180 if (experiment_settings) {
181 experiment_groups_[0] = experiment_settings->group_id + 1;
182 } else {
183 experiment_groups_[0] = 0;
184 }
Sebastian Janssoncabe3832018-01-12 10:54:18 +0100185 experiment_settings = AlrExperimentSettings::CreateFromFieldTrial(
186 AlrExperimentSettings::kScreenshareProbingBweExperimentName);
ilnik6d5b4d62017-08-30 03:32:14 -0700187 if (experiment_settings) {
188 experiment_groups_[1] = experiment_settings->group_id + 1;
189 } else {
190 experiment_groups_[1] = 0;
191 }
192}
niklase@google.com470e71d2011-07-07 08:21:25 +0000193
sprang3911c262016-04-15 01:24:14 -0700194VCMEncodedFrameCallback::~VCMEncodedFrameCallback() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000195
ilnik04f4d122017-06-19 07:18:55 -0700196void VCMEncodedFrameCallback::OnTargetBitrateChanged(
197 size_t bitrate_bytes_per_second,
198 size_t simulcast_svc_idx) {
199 rtc::CritScope crit(&timing_params_lock_);
200 if (timing_frames_info_.size() < simulcast_svc_idx + 1)
201 timing_frames_info_.resize(simulcast_svc_idx + 1);
202 timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec =
203 bitrate_bytes_per_second;
204}
205
206void VCMEncodedFrameCallback::OnFrameRateChanged(size_t framerate) {
207 rtc::CritScope crit(&timing_params_lock_);
208 framerate_ = framerate;
209}
210
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100211void VCMEncodedFrameCallback::OnEncodeStarted(uint32_t rtp_timestamp,
212 int64_t capture_time_ms,
ilnik04f4d122017-06-19 07:18:55 -0700213 size_t simulcast_svc_idx) {
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200214 if (internal_source_) {
215 return;
216 }
ilnik04f4d122017-06-19 07:18:55 -0700217 rtc::CritScope crit(&timing_params_lock_);
218 if (timing_frames_info_.size() < simulcast_svc_idx + 1)
219 timing_frames_info_.resize(simulcast_svc_idx + 1);
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200220 RTC_DCHECK(
221 timing_frames_info_[simulcast_svc_idx].encode_start_list.empty() ||
222 rtc::TimeDiff(capture_time_ms, timing_frames_info_[simulcast_svc_idx]
223 .encode_start_list.back()
224 .capture_time_ms) >= 0);
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +0100225 // If stream is disabled due to low bandwidth OnEncodeStarted still will be
226 // called and have to be ignored.
227 if (timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec == 0)
228 return;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200229 if (timing_frames_info_[simulcast_svc_idx].encode_start_list.size() ==
230 kMaxEncodeStartTimeListSize) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100231 ++stalled_encoder_logged_messages_;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100232 if (stalled_encoder_logged_messages_ <= kMessagesThrottlingThreshold ||
233 stalled_encoder_logged_messages_ % kThrottleRatio == 0) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100234 RTC_LOG(LS_WARNING) << "Too many frames in the encode_start_list."
235 " Did encoder stall?";
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100236 if (stalled_encoder_logged_messages_ == kMessagesThrottlingThreshold) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100237 RTC_LOG(LS_WARNING) << "Too many log messages. Further stalled encoder"
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100238 "warnings will be throttled.";
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100239 }
240 }
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200241 post_encode_callback_->OnDroppedFrame(DropReason::kDroppedByEncoder);
242 timing_frames_info_[simulcast_svc_idx].encode_start_list.pop_front();
243 }
244 timing_frames_info_[simulcast_svc_idx].encode_start_list.emplace_back(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100245 rtp_timestamp, capture_time_ms, rtc::TimeMillis());
ilnik04f4d122017-06-19 07:18:55 -0700246}
247
Danil Chapovalov0040b662018-06-18 10:48:16 +0200248absl::optional<int64_t> VCMEncodedFrameCallback::ExtractEncodeStartTime(
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200249 size_t simulcast_svc_idx,
250 EncodedImage* encoded_image) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200251 absl::optional<int64_t> result;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200252 size_t num_simulcast_svc_streams = timing_frames_info_.size();
253 if (simulcast_svc_idx < num_simulcast_svc_streams) {
254 auto encode_start_list =
255 &timing_frames_info_[simulcast_svc_idx].encode_start_list;
256 // Skip frames for which there was OnEncodeStarted but no OnEncodedImage
257 // call. These are dropped by encoder internally.
258 // Because some hardware encoders don't preserve capture timestamp we
259 // use RTP timestamps here.
260 while (!encode_start_list->empty() &&
Niels Möller23775882018-08-16 10:24:12 +0200261 IsNewerTimestamp(encoded_image->Timestamp(),
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200262 encode_start_list->front().rtp_timestamp)) {
263 post_encode_callback_->OnDroppedFrame(DropReason::kDroppedByEncoder);
264 encode_start_list->pop_front();
265 }
266 if (encode_start_list->size() > 0 &&
Niels Möller23775882018-08-16 10:24:12 +0200267 encode_start_list->front().rtp_timestamp ==
268 encoded_image->Timestamp()) {
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200269 result.emplace(encode_start_list->front().encode_start_time_ms);
270 if (encoded_image->capture_time_ms_ !=
271 encode_start_list->front().capture_time_ms) {
272 // Force correct capture timestamp.
273 encoded_image->capture_time_ms_ =
274 encode_start_list->front().capture_time_ms;
275 ++incorrect_capture_time_logged_messages_;
276 if (incorrect_capture_time_logged_messages_ <=
277 kMessagesThrottlingThreshold ||
278 incorrect_capture_time_logged_messages_ % kThrottleRatio == 0) {
279 RTC_LOG(LS_WARNING)
280 << "Encoder is not preserving capture timestamps.";
281 if (incorrect_capture_time_logged_messages_ ==
282 kMessagesThrottlingThreshold) {
283 RTC_LOG(LS_WARNING) << "Too many log messages. Further incorrect "
284 "timestamps warnings will be throttled.";
285 }
286 }
287 }
288 encode_start_list->pop_front();
289 } else {
290 ++reordered_frames_logged_messages_;
291 if (reordered_frames_logged_messages_ <= kMessagesThrottlingThreshold ||
292 reordered_frames_logged_messages_ % kThrottleRatio == 0) {
293 RTC_LOG(LS_WARNING) << "Frame with no encode started time recordings. "
294 "Encoder may be reordering frames "
295 "or not preserving RTP timestamps.";
296 if (reordered_frames_logged_messages_ == kMessagesThrottlingThreshold) {
297 RTC_LOG(LS_WARNING) << "Too many log messages. Further frames "
298 "reordering warnings will be throttled.";
299 }
300 }
301 }
302 }
303 return result;
304}
305
306void VCMEncodedFrameCallback::FillTimingInfo(size_t simulcast_svc_idx,
307 EncodedImage* encoded_image) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200308 absl::optional<size_t> outlier_frame_size;
309 absl::optional<int64_t> encode_start_ms;
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200310 uint8_t timing_flags = VideoSendTiming::kNotTriggered;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200311 {
312 rtc::CritScope crit(&timing_params_lock_);
313
314 // Encoders with internal sources do not call OnEncodeStarted
315 // |timing_frames_info_| may be not filled here.
316 if (!internal_source_) {
317 encode_start_ms =
318 ExtractEncodeStartTime(simulcast_svc_idx, encoded_image);
319 }
320
321 if (timing_frames_info_.size() > simulcast_svc_idx) {
322 size_t target_bitrate =
323 timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec;
324 if (framerate_ > 0 && target_bitrate > 0) {
325 // framerate and target bitrate were reported by encoder.
326 size_t average_frame_size = target_bitrate / framerate_;
327 outlier_frame_size.emplace(
328 average_frame_size *
329 timing_frames_thresholds_.outlier_ratio_percent / 100);
330 }
331 }
332
333 // Outliers trigger timing frames, but do not affect scheduled timing
334 // frames.
335 if (outlier_frame_size && encoded_image->_length >= *outlier_frame_size) {
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200336 timing_flags |= VideoSendTiming::kTriggeredBySize;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200337 }
338
339 // Check if it's time to send a timing frame.
340 int64_t timing_frame_delay_ms =
341 encoded_image->capture_time_ms_ - last_timing_frame_time_ms_;
342 // Trigger threshold if it's a first frame, too long passed since the last
343 // timing frame, or we already sent timing frame on a different simulcast
344 // stream with the same capture time.
345 if (last_timing_frame_time_ms_ == -1 ||
346 timing_frame_delay_ms >= timing_frames_thresholds_.delay_ms ||
347 timing_frame_delay_ms == 0) {
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200348 timing_flags |= VideoSendTiming::kTriggeredByTimer;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200349 last_timing_frame_time_ms_ = encoded_image->capture_time_ms_;
350 }
351 } // rtc::CritScope crit(&timing_params_lock_);
352
353 int64_t now_ms = rtc::TimeMillis();
354 // Workaround for chromoting encoder: it passes encode start and finished
355 // timestamps in |timing_| field, but they (together with capture timestamp)
356 // are not in the WebRTC clock.
357 if (internal_source_ && encoded_image->timing_.encode_finish_ms > 0 &&
358 encoded_image->timing_.encode_start_ms > 0) {
359 int64_t clock_offset_ms = now_ms - encoded_image->timing_.encode_finish_ms;
360 // Translate capture timestamp to local WebRTC clock.
361 encoded_image->capture_time_ms_ += clock_offset_ms;
Niels Möller23775882018-08-16 10:24:12 +0200362 encoded_image->SetTimestamp(
363 static_cast<uint32_t>(encoded_image->capture_time_ms_ * 90));
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200364 encode_start_ms.emplace(encoded_image->timing_.encode_start_ms +
365 clock_offset_ms);
366 }
367
368 // If encode start is not available that means that encoder uses internal
369 // source. In that case capture timestamp may be from a different clock with a
370 // drift relative to rtc::TimeMillis(). We can't use it for Timing frames,
371 // because to being sent in the network capture time required to be less than
372 // all the other timestamps.
373 if (encode_start_ms) {
374 encoded_image->SetEncodeTime(*encode_start_ms, now_ms);
375 encoded_image->timing_.flags = timing_flags;
376 } else {
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200377 encoded_image->timing_.flags = VideoSendTiming::kInvalid;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200378 }
379}
380
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700381EncodedImageCallback::Result VCMEncodedFrameCallback::OnEncodedImage(
Peter Boströmb7d9a972015-12-18 16:01:11 +0100382 const EncodedImage& encoded_image,
sprang3911c262016-04-15 01:24:14 -0700383 const CodecSpecificInfo* codec_specific,
384 const RTPFragmentationHeader* fragmentation_header) {
pbosd9eec762015-11-17 06:03:43 -0800385 TRACE_EVENT_INSTANT1("webrtc", "VCMEncodedFrameCallback::Encoded",
Niels Möller23775882018-08-16 10:24:12 +0200386 "timestamp", encoded_image.Timestamp());
Niels Möllerd3b8c632018-08-27 15:33:42 +0200387 const size_t spatial_idx = encoded_image.SpatialIndex().value_or(0);
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100388 EncodedImage image_copy(encoded_image);
389
Niels Möllerd3b8c632018-08-27 15:33:42 +0200390 FillTimingInfo(spatial_idx, &image_copy);
ilnik04f4d122017-06-19 07:18:55 -0700391
ilnik6d5b4d62017-08-30 03:32:14 -0700392 // Piggyback ALR experiment group id and simulcast id into the content type.
393 uint8_t experiment_id =
394 experiment_groups_[videocontenttypehelpers::IsScreenshare(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100395 image_copy.content_type_)];
ilnik6d5b4d62017-08-30 03:32:14 -0700396
397 // TODO(ilnik): This will force content type extension to be present even
398 // for realtime video. At the expense of miniscule overhead we will get
399 // sliced receive statistics.
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100400 RTC_CHECK(videocontenttypehelpers::SetExperimentId(&image_copy.content_type_,
401 experiment_id));
ilnik6d5b4d62017-08-30 03:32:14 -0700402 // We count simulcast streams from 1 on the wire. That's why we set simulcast
403 // id in content type to +1 of that is actual simulcast index. This is because
404 // value 0 on the wire is reserved for 'no simulcast stream specified'.
405 RTC_CHECK(videocontenttypehelpers::SetSimulcastId(
Niels Möllerd3b8c632018-08-27 15:33:42 +0200406 &image_copy.content_type_, static_cast<uint8_t>(spatial_idx + 1)));
ilnik6d5b4d62017-08-30 03:32:14 -0700407
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700408 Result result = post_encode_callback_->OnEncodedImage(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100409 image_copy, codec_specific, fragmentation_header);
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700410 if (result.error != Result::OK)
411 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000412
sprang3911c262016-04-15 01:24:14 -0700413 if (media_opt_) {
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100414 media_opt_->UpdateWithEncodedData(image_copy._length,
415 image_copy._frameType);
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700416 if (internal_source_) {
417 // Signal to encoder to drop next frame.
418 result.drop_next_frame = media_opt_->DropFrame();
419 }
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000420 }
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700421 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000422}
423
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000424} // namespace webrtc