blob: d5cc0a8ba068dcfdfef5eaf24d78911156ebb9d3 [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 rates_have_changed;
105 {
106 rtc::CritScope lock(&params_lock_);
Peter Boström69ccb332015-10-29 16:30:23 +0100107 rates_have_changed =
108 params.target_bitrate != encoder_params_.target_bitrate ||
109 params.input_frame_rate != encoder_params_.input_frame_rate;
110 encoder_params_ = params;
111 }
Peter Boström69ccb332015-10-29 16:30:23 +0100112 if (rates_have_changed) {
Erik Språng08127a92016-11-16 16:41:30 +0100113 int res = encoder_->SetRateAllocation(params.target_bitrate,
114 params.input_frame_rate);
115 if (res != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100116 RTC_LOG(LS_WARNING) << "Error set encoder rate (total bitrate bps = "
117 << params.target_bitrate.get_sum_bps()
118 << ", framerate = " << params.input_frame_rate
119 << "): " << res;
Erik Språng08127a92016-11-16 16:41:30 +0100120 }
ilnik04f4d122017-06-19 07:18:55 -0700121 vcm_encoded_frame_callback_->OnFrameRateChanged(params.input_frame_rate);
122 for (size_t i = 0; i < streams_or_svc_num_; ++i) {
ilnik04f4d122017-06-19 07:18:55 -0700123 vcm_encoded_frame_callback_->OnTargetBitrateChanged(
Sergey Silkin86684962018-03-28 19:32:37 +0200124 params.target_bitrate.GetSpatialLayerSum(i) / 8, i);
ilnik04f4d122017-06-19 07:18:55 -0700125 }
Peter Boström69ccb332015-10-29 16:30:23 +0100126 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000127}
128
Peter Boström69ccb332015-10-29 16:30:23 +0100129EncoderParameters VCMGenericEncoder::GetEncoderParameters() const {
130 rtc::CritScope lock(&params_lock_);
131 return encoder_params_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000132}
133
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000134int32_t VCMGenericEncoder::RequestFrame(
stefan@webrtc.orgcf216862012-10-25 11:29:51 +0000135 const std::vector<FrameType>& frame_types) {
Peter Boström02bafc62016-07-01 12:45:15 +0200136 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
nissedf2ceb82016-12-15 06:29:53 -0800137
138 // TODO(nisse): Used only with internal source. Delete as soon as
139 // that feature is removed. The only implementation I've been able
nisse01d5a0b2017-05-31 06:33:21 -0700140 // to find ignores what's in the frame. With one exception: It seems
141 // a few test cases, e.g.,
142 // VideoSendStreamTest.VideoSendStreamStopSetEncoderRateToZero, set
143 // internal_source to true and use FakeEncoder. And the latter will
144 // happily encode this 1x1 frame and pass it on down the pipeline.
Yves Gerey665174f2018-06-19 15:03:05 +0200145 return encoder_->Encode(
146 VideoFrame(I420Buffer::Create(1, 1), kVideoRotation_0, 0), NULL,
147 &frame_types);
nissedf2ceb82016-12-15 06:29:53 -0800148 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000149}
150
philipel9d3ab612015-12-21 04:12:39 -0800151bool VCMGenericEncoder::InternalSource() const {
152 return internal_source_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000153}
154
Erik Språngd3438aa2018-11-08 16:56:43 +0100155VideoEncoder::EncoderInfo VCMGenericEncoder::GetEncoderInfo() const {
Peter Boström02bafc62016-07-01 12:45:15 +0200156 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Erik Språngd3438aa2018-11-08 16:56:43 +0100157 return encoder_->GetEncoderInfo();
Peter Boströmeb66e802015-06-05 11:08:03 +0200158}
159
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000160VCMEncodedFrameCallback::VCMEncodedFrameCallback(
perkj376b1922016-05-02 11:35:24 -0700161 EncodedImageCallback* post_encode_callback,
162 media_optimization::MediaOptimization* media_opt)
163 : internal_source_(false),
164 post_encode_callback_(post_encode_callback),
ilnik04f4d122017-06-19 07:18:55 -0700165 media_opt_(media_opt),
166 framerate_(1),
167 last_timing_frame_time_ms_(-1),
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100168 timing_frames_thresholds_({-1, 0}),
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100169 incorrect_capture_time_logged_messages_(0),
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100170 reordered_frames_logged_messages_(0),
171 stalled_encoder_logged_messages_(0) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200172 absl::optional<AlrExperimentSettings> experiment_settings =
Sebastian Janssoncabe3832018-01-12 10:54:18 +0100173 AlrExperimentSettings::CreateFromFieldTrial(
174 AlrExperimentSettings::kStrictPacingAndProbingExperimentName);
ilnik6d5b4d62017-08-30 03:32:14 -0700175 if (experiment_settings) {
176 experiment_groups_[0] = experiment_settings->group_id + 1;
177 } else {
178 experiment_groups_[0] = 0;
179 }
Sebastian Janssoncabe3832018-01-12 10:54:18 +0100180 experiment_settings = AlrExperimentSettings::CreateFromFieldTrial(
181 AlrExperimentSettings::kScreenshareProbingBweExperimentName);
ilnik6d5b4d62017-08-30 03:32:14 -0700182 if (experiment_settings) {
183 experiment_groups_[1] = experiment_settings->group_id + 1;
184 } else {
185 experiment_groups_[1] = 0;
186 }
187}
niklase@google.com470e71d2011-07-07 08:21:25 +0000188
sprang3911c262016-04-15 01:24:14 -0700189VCMEncodedFrameCallback::~VCMEncodedFrameCallback() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000190
ilnik04f4d122017-06-19 07:18:55 -0700191void VCMEncodedFrameCallback::OnTargetBitrateChanged(
192 size_t bitrate_bytes_per_second,
193 size_t simulcast_svc_idx) {
194 rtc::CritScope crit(&timing_params_lock_);
195 if (timing_frames_info_.size() < simulcast_svc_idx + 1)
196 timing_frames_info_.resize(simulcast_svc_idx + 1);
197 timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec =
198 bitrate_bytes_per_second;
199}
200
201void VCMEncodedFrameCallback::OnFrameRateChanged(size_t framerate) {
202 rtc::CritScope crit(&timing_params_lock_);
203 framerate_ = framerate;
204}
205
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100206void VCMEncodedFrameCallback::OnEncodeStarted(uint32_t rtp_timestamp,
207 int64_t capture_time_ms,
ilnik04f4d122017-06-19 07:18:55 -0700208 size_t simulcast_svc_idx) {
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200209 if (internal_source_) {
210 return;
211 }
ilnik04f4d122017-06-19 07:18:55 -0700212 rtc::CritScope crit(&timing_params_lock_);
213 if (timing_frames_info_.size() < simulcast_svc_idx + 1)
214 timing_frames_info_.resize(simulcast_svc_idx + 1);
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200215 RTC_DCHECK(
216 timing_frames_info_[simulcast_svc_idx].encode_start_list.empty() ||
217 rtc::TimeDiff(capture_time_ms, timing_frames_info_[simulcast_svc_idx]
218 .encode_start_list.back()
219 .capture_time_ms) >= 0);
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +0100220 // If stream is disabled due to low bandwidth OnEncodeStarted still will be
221 // called and have to be ignored.
222 if (timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec == 0)
223 return;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200224 if (timing_frames_info_[simulcast_svc_idx].encode_start_list.size() ==
225 kMaxEncodeStartTimeListSize) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100226 ++stalled_encoder_logged_messages_;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100227 if (stalled_encoder_logged_messages_ <= kMessagesThrottlingThreshold ||
228 stalled_encoder_logged_messages_ % kThrottleRatio == 0) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100229 RTC_LOG(LS_WARNING) << "Too many frames in the encode_start_list."
230 " Did encoder stall?";
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100231 if (stalled_encoder_logged_messages_ == kMessagesThrottlingThreshold) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100232 RTC_LOG(LS_WARNING) << "Too many log messages. Further stalled encoder"
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100233 "warnings will be throttled.";
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100234 }
235 }
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200236 post_encode_callback_->OnDroppedFrame(DropReason::kDroppedByEncoder);
237 timing_frames_info_[simulcast_svc_idx].encode_start_list.pop_front();
238 }
239 timing_frames_info_[simulcast_svc_idx].encode_start_list.emplace_back(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100240 rtp_timestamp, capture_time_ms, rtc::TimeMillis());
ilnik04f4d122017-06-19 07:18:55 -0700241}
242
Danil Chapovalov0040b662018-06-18 10:48:16 +0200243absl::optional<int64_t> VCMEncodedFrameCallback::ExtractEncodeStartTime(
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200244 size_t simulcast_svc_idx,
245 EncodedImage* encoded_image) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200246 absl::optional<int64_t> result;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200247 size_t num_simulcast_svc_streams = timing_frames_info_.size();
248 if (simulcast_svc_idx < num_simulcast_svc_streams) {
249 auto encode_start_list =
250 &timing_frames_info_[simulcast_svc_idx].encode_start_list;
251 // Skip frames for which there was OnEncodeStarted but no OnEncodedImage
252 // call. These are dropped by encoder internally.
253 // Because some hardware encoders don't preserve capture timestamp we
254 // use RTP timestamps here.
255 while (!encode_start_list->empty() &&
Niels Möller23775882018-08-16 10:24:12 +0200256 IsNewerTimestamp(encoded_image->Timestamp(),
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200257 encode_start_list->front().rtp_timestamp)) {
258 post_encode_callback_->OnDroppedFrame(DropReason::kDroppedByEncoder);
259 encode_start_list->pop_front();
260 }
261 if (encode_start_list->size() > 0 &&
Niels Möller23775882018-08-16 10:24:12 +0200262 encode_start_list->front().rtp_timestamp ==
263 encoded_image->Timestamp()) {
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200264 result.emplace(encode_start_list->front().encode_start_time_ms);
265 if (encoded_image->capture_time_ms_ !=
266 encode_start_list->front().capture_time_ms) {
267 // Force correct capture timestamp.
268 encoded_image->capture_time_ms_ =
269 encode_start_list->front().capture_time_ms;
270 ++incorrect_capture_time_logged_messages_;
271 if (incorrect_capture_time_logged_messages_ <=
272 kMessagesThrottlingThreshold ||
273 incorrect_capture_time_logged_messages_ % kThrottleRatio == 0) {
274 RTC_LOG(LS_WARNING)
275 << "Encoder is not preserving capture timestamps.";
276 if (incorrect_capture_time_logged_messages_ ==
277 kMessagesThrottlingThreshold) {
278 RTC_LOG(LS_WARNING) << "Too many log messages. Further incorrect "
279 "timestamps warnings will be throttled.";
280 }
281 }
282 }
283 encode_start_list->pop_front();
284 } else {
285 ++reordered_frames_logged_messages_;
286 if (reordered_frames_logged_messages_ <= kMessagesThrottlingThreshold ||
287 reordered_frames_logged_messages_ % kThrottleRatio == 0) {
288 RTC_LOG(LS_WARNING) << "Frame with no encode started time recordings. "
289 "Encoder may be reordering frames "
290 "or not preserving RTP timestamps.";
291 if (reordered_frames_logged_messages_ == kMessagesThrottlingThreshold) {
292 RTC_LOG(LS_WARNING) << "Too many log messages. Further frames "
293 "reordering warnings will be throttled.";
294 }
295 }
296 }
297 }
298 return result;
299}
300
301void VCMEncodedFrameCallback::FillTimingInfo(size_t simulcast_svc_idx,
302 EncodedImage* encoded_image) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200303 absl::optional<size_t> outlier_frame_size;
304 absl::optional<int64_t> encode_start_ms;
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200305 uint8_t timing_flags = VideoSendTiming::kNotTriggered;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200306 {
307 rtc::CritScope crit(&timing_params_lock_);
308
309 // Encoders with internal sources do not call OnEncodeStarted
310 // |timing_frames_info_| may be not filled here.
311 if (!internal_source_) {
312 encode_start_ms =
313 ExtractEncodeStartTime(simulcast_svc_idx, encoded_image);
314 }
315
316 if (timing_frames_info_.size() > simulcast_svc_idx) {
317 size_t target_bitrate =
318 timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec;
319 if (framerate_ > 0 && target_bitrate > 0) {
320 // framerate and target bitrate were reported by encoder.
321 size_t average_frame_size = target_bitrate / framerate_;
322 outlier_frame_size.emplace(
323 average_frame_size *
324 timing_frames_thresholds_.outlier_ratio_percent / 100);
325 }
326 }
327
328 // Outliers trigger timing frames, but do not affect scheduled timing
329 // frames.
330 if (outlier_frame_size && encoded_image->_length >= *outlier_frame_size) {
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200331 timing_flags |= VideoSendTiming::kTriggeredBySize;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200332 }
333
334 // Check if it's time to send a timing frame.
335 int64_t timing_frame_delay_ms =
336 encoded_image->capture_time_ms_ - last_timing_frame_time_ms_;
337 // Trigger threshold if it's a first frame, too long passed since the last
338 // timing frame, or we already sent timing frame on a different simulcast
339 // stream with the same capture time.
340 if (last_timing_frame_time_ms_ == -1 ||
341 timing_frame_delay_ms >= timing_frames_thresholds_.delay_ms ||
342 timing_frame_delay_ms == 0) {
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200343 timing_flags |= VideoSendTiming::kTriggeredByTimer;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200344 last_timing_frame_time_ms_ = encoded_image->capture_time_ms_;
345 }
346 } // rtc::CritScope crit(&timing_params_lock_);
347
348 int64_t now_ms = rtc::TimeMillis();
349 // Workaround for chromoting encoder: it passes encode start and finished
350 // timestamps in |timing_| field, but they (together with capture timestamp)
351 // are not in the WebRTC clock.
352 if (internal_source_ && encoded_image->timing_.encode_finish_ms > 0 &&
353 encoded_image->timing_.encode_start_ms > 0) {
354 int64_t clock_offset_ms = now_ms - encoded_image->timing_.encode_finish_ms;
355 // Translate capture timestamp to local WebRTC clock.
356 encoded_image->capture_time_ms_ += clock_offset_ms;
Niels Möller23775882018-08-16 10:24:12 +0200357 encoded_image->SetTimestamp(
358 static_cast<uint32_t>(encoded_image->capture_time_ms_ * 90));
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200359 encode_start_ms.emplace(encoded_image->timing_.encode_start_ms +
360 clock_offset_ms);
361 }
362
363 // If encode start is not available that means that encoder uses internal
364 // source. In that case capture timestamp may be from a different clock with a
365 // drift relative to rtc::TimeMillis(). We can't use it for Timing frames,
366 // because to being sent in the network capture time required to be less than
367 // all the other timestamps.
368 if (encode_start_ms) {
369 encoded_image->SetEncodeTime(*encode_start_ms, now_ms);
370 encoded_image->timing_.flags = timing_flags;
371 } else {
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200372 encoded_image->timing_.flags = VideoSendTiming::kInvalid;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200373 }
374}
375
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700376EncodedImageCallback::Result VCMEncodedFrameCallback::OnEncodedImage(
Peter Boströmb7d9a972015-12-18 16:01:11 +0100377 const EncodedImage& encoded_image,
sprang3911c262016-04-15 01:24:14 -0700378 const CodecSpecificInfo* codec_specific,
379 const RTPFragmentationHeader* fragmentation_header) {
pbosd9eec762015-11-17 06:03:43 -0800380 TRACE_EVENT_INSTANT1("webrtc", "VCMEncodedFrameCallback::Encoded",
Niels Möller23775882018-08-16 10:24:12 +0200381 "timestamp", encoded_image.Timestamp());
Niels Möllerd3b8c632018-08-27 15:33:42 +0200382 const size_t spatial_idx = encoded_image.SpatialIndex().value_or(0);
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100383 EncodedImage image_copy(encoded_image);
384
Niels Möllerd3b8c632018-08-27 15:33:42 +0200385 FillTimingInfo(spatial_idx, &image_copy);
ilnik04f4d122017-06-19 07:18:55 -0700386
ilnik6d5b4d62017-08-30 03:32:14 -0700387 // Piggyback ALR experiment group id and simulcast id into the content type.
388 uint8_t experiment_id =
389 experiment_groups_[videocontenttypehelpers::IsScreenshare(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100390 image_copy.content_type_)];
ilnik6d5b4d62017-08-30 03:32:14 -0700391
392 // TODO(ilnik): This will force content type extension to be present even
393 // for realtime video. At the expense of miniscule overhead we will get
394 // sliced receive statistics.
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100395 RTC_CHECK(videocontenttypehelpers::SetExperimentId(&image_copy.content_type_,
396 experiment_id));
ilnik6d5b4d62017-08-30 03:32:14 -0700397 // We count simulcast streams from 1 on the wire. That's why we set simulcast
398 // id in content type to +1 of that is actual simulcast index. This is because
399 // value 0 on the wire is reserved for 'no simulcast stream specified'.
400 RTC_CHECK(videocontenttypehelpers::SetSimulcastId(
Niels Möllerd3b8c632018-08-27 15:33:42 +0200401 &image_copy.content_type_, static_cast<uint8_t>(spatial_idx + 1)));
ilnik6d5b4d62017-08-30 03:32:14 -0700402
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700403 Result result = post_encode_callback_->OnEncodedImage(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100404 image_copy, codec_specific, fragmentation_header);
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700405 if (result.error != Result::OK)
406 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000407
sprang3911c262016-04-15 01:24:14 -0700408 if (media_opt_) {
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100409 media_opt_->UpdateWithEncodedData(image_copy._length,
410 image_copy._frameType);
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700411 if (internal_source_) {
412 // Signal to encoder to drop next frame.
413 result.drop_next_frame = media_opt_->DropFrame();
414 }
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000415 }
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700416 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000417}
418
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000419} // namespace webrtc