blob: 69357ee6163e6c3c8c5ce5d96aca34dc21a34bad [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
sprang3911c262016-04-15 01:24:14 -07002* 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "api/optional.h"
16#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
Peter Boström4f5db112015-10-29 16:53:59 +010034VCMGenericEncoder::VCMGenericEncoder(
35 VideoEncoder* encoder,
Peter Boström4f5db112015-10-29 16:53:59 +010036 VCMEncodedFrameCallback* encoded_frame_callback,
sprang3911c262016-04-15 01:24:14 -070037 bool internal_source)
pbos@webrtc.org891d4832015-02-26 13:15:22 +000038 : encoder_(encoder),
Peter Boström4f5db112015-10-29 16:53:59 +010039 vcm_encoded_frame_callback_(encoded_frame_callback),
sprang3911c262016-04-15 01:24:14 -070040 internal_source_(internal_source),
Erik Språng566124a2018-04-23 12:32:22 +020041 encoder_params_({VideoBitrateAllocation(), 0, 0, 0}),
42 streams_or_svc_num_(0),
43 codec_type_(VideoCodecType::kVideoCodecUnknown) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000044
Peter Boström4f5db112015-10-29 16:53:59 +010045VCMGenericEncoder::~VCMGenericEncoder() {}
46
47int32_t VCMGenericEncoder::Release() {
Peter Boström02bafc62016-07-01 12:45:15 +020048 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Peter Boström4fd6cda2016-01-26 10:19:53 +010049 TRACE_EVENT0("webrtc", "VCMGenericEncoder::Release");
Peter Boström4f5db112015-10-29 16:53:59 +010050 return encoder_->Release();
niklase@google.com470e71d2011-07-07 08:21:25 +000051}
52
Peter Boström4f5db112015-10-29 16:53:59 +010053int32_t VCMGenericEncoder::InitEncode(const VideoCodec* settings,
sprang3911c262016-04-15 01:24:14 -070054 int32_t number_of_cores,
55 size_t max_payload_size) {
Peter Boström02bafc62016-07-01 12:45:15 +020056 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
pbosd9eec762015-11-17 06:03:43 -080057 TRACE_EVENT0("webrtc", "VCMGenericEncoder::InitEncode");
ilnik04f4d122017-06-19 07:18:55 -070058 streams_or_svc_num_ = settings->numberOfSimulcastStreams;
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +010059 codec_type_ = settings->codecType;
ilnik04f4d122017-06-19 07:18:55 -070060 if (settings->codecType == kVideoCodecVP9) {
61 streams_or_svc_num_ = settings->VP9().numberOfSpatialLayers;
62 }
63 if (streams_or_svc_num_ == 0)
64 streams_or_svc_num_ = 1;
65
66 vcm_encoded_frame_callback_->SetTimingFramesThresholds(
67 settings->timing_frame_thresholds);
68 vcm_encoded_frame_callback_->OnFrameRateChanged(settings->maxFramerate);
69
sprang3911c262016-04-15 01:24:14 -070070 if (encoder_->InitEncode(settings, number_of_cores, max_payload_size) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010071 RTC_LOG(LS_ERROR) << "Failed to initialize the encoder associated with "
Niels Möller2e1d7842018-02-23 15:41:13 +010072 "codec type: "
73 << CodecTypeToPayloadString(settings->codecType)
74 << " (" << settings->codecType <<")";
Peter Boström4f5db112015-10-29 16:53:59 +010075 return -1;
76 }
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020077 vcm_encoded_frame_callback_->Reset();
Peter Boström4f5db112015-10-29 16:53:59 +010078 encoder_->RegisterEncodeCompleteCallback(vcm_encoded_frame_callback_);
79 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000080}
81
sprang3911c262016-04-15 01:24:14 -070082int32_t VCMGenericEncoder::Encode(const VideoFrame& frame,
83 const CodecSpecificInfo* codec_specific,
84 const std::vector<FrameType>& frame_types) {
Peter Boström02bafc62016-07-01 12:45:15 +020085 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
pbosd9eec762015-11-17 06:03:43 -080086 TRACE_EVENT1("webrtc", "VCMGenericEncoder::Encode", "timestamp",
sprang3911c262016-04-15 01:24:14 -070087 frame.timestamp());
pbosd9eec762015-11-17 06:03:43 -080088
sprang3911c262016-04-15 01:24:14 -070089 for (FrameType frame_type : frame_types)
pbos22993e12015-10-19 02:39:06 -070090 RTC_DCHECK(frame_type == kVideoFrameKey || frame_type == kVideoFrameDelta);
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +000091
ilnik04f4d122017-06-19 07:18:55 -070092 for (size_t i = 0; i < streams_or_svc_num_; ++i)
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +010093 vcm_encoded_frame_callback_->OnEncodeStarted(frame.timestamp(),
94 frame.render_time_ms(), i);
Peter Boströmb7d9a972015-12-18 16:01:11 +010095
ilnike9973812017-09-12 10:24:46 -070096 return encoder_->Encode(frame, codec_specific, &frame_types);
niklase@google.com470e71d2011-07-07 08:21:25 +000097}
98
Peter Boström69ccb332015-10-29 16:30:23 +010099void VCMGenericEncoder::SetEncoderParameters(const EncoderParameters& params) {
Peter Boström02bafc62016-07-01 12:45:15 +0200100 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Peter Boström69ccb332015-10-29 16:30:23 +0100101 bool channel_parameters_have_changed;
102 bool rates_have_changed;
103 {
104 rtc::CritScope lock(&params_lock_);
105 channel_parameters_have_changed =
106 params.loss_rate != encoder_params_.loss_rate ||
107 params.rtt != encoder_params_.rtt;
108 rates_have_changed =
109 params.target_bitrate != encoder_params_.target_bitrate ||
110 params.input_frame_rate != encoder_params_.input_frame_rate;
111 encoder_params_ = params;
112 }
Erik Språng08127a92016-11-16 16:41:30 +0100113 if (channel_parameters_have_changed) {
114 int res = encoder_->SetChannelParameters(params.loss_rate, params.rtt);
115 if (res != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100116 RTC_LOG(LS_WARNING) << "Error set encoder parameters (loss = "
117 << params.loss_rate << ", rtt = " << params.rtt
118 << "): " << res;
Erik Språng08127a92016-11-16 16:41:30 +0100119 }
120 }
Peter Boström69ccb332015-10-29 16:30:23 +0100121 if (rates_have_changed) {
Erik Språng08127a92016-11-16 16:41:30 +0100122 int res = encoder_->SetRateAllocation(params.target_bitrate,
123 params.input_frame_rate);
124 if (res != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100125 RTC_LOG(LS_WARNING) << "Error set encoder rate (total bitrate bps = "
126 << params.target_bitrate.get_sum_bps()
127 << ", framerate = " << params.input_frame_rate
128 << "): " << res;
Erik Språng08127a92016-11-16 16:41:30 +0100129 }
ilnik04f4d122017-06-19 07:18:55 -0700130 vcm_encoded_frame_callback_->OnFrameRateChanged(params.input_frame_rate);
131 for (size_t i = 0; i < streams_or_svc_num_; ++i) {
ilnik04f4d122017-06-19 07:18:55 -0700132 vcm_encoded_frame_callback_->OnTargetBitrateChanged(
Sergey Silkin86684962018-03-28 19:32:37 +0200133 params.target_bitrate.GetSpatialLayerSum(i) / 8, i);
ilnik04f4d122017-06-19 07:18:55 -0700134 }
Peter Boström69ccb332015-10-29 16:30:23 +0100135 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000136}
137
Peter Boström69ccb332015-10-29 16:30:23 +0100138EncoderParameters VCMGenericEncoder::GetEncoderParameters() const {
139 rtc::CritScope lock(&params_lock_);
140 return encoder_params_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000141}
142
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000143int32_t VCMGenericEncoder::RequestFrame(
stefan@webrtc.orgcf216862012-10-25 11:29:51 +0000144 const std::vector<FrameType>& frame_types) {
Peter Boström02bafc62016-07-01 12:45:15 +0200145 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
nissedf2ceb82016-12-15 06:29:53 -0800146
147 // TODO(nisse): Used only with internal source. Delete as soon as
148 // that feature is removed. The only implementation I've been able
nisse01d5a0b2017-05-31 06:33:21 -0700149 // to find ignores what's in the frame. With one exception: It seems
150 // a few test cases, e.g.,
151 // VideoSendStreamTest.VideoSendStreamStopSetEncoderRateToZero, set
152 // internal_source to true and use FakeEncoder. And the latter will
153 // happily encode this 1x1 frame and pass it on down the pipeline.
nissedf2ceb82016-12-15 06:29:53 -0800154 return encoder_->Encode(VideoFrame(I420Buffer::Create(1, 1),
155 kVideoRotation_0, 0),
156 NULL, &frame_types);
157 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000158}
159
philipel9d3ab612015-12-21 04:12:39 -0800160bool VCMGenericEncoder::InternalSource() const {
161 return internal_source_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000162}
163
Peter Boströmeb66e802015-06-05 11:08:03 +0200164bool VCMGenericEncoder::SupportsNativeHandle() const {
Peter Boström02bafc62016-07-01 12:45:15 +0200165 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Peter Boströmeb66e802015-06-05 11:08:03 +0200166 return encoder_->SupportsNativeHandle();
167}
168
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000169VCMEncodedFrameCallback::VCMEncodedFrameCallback(
perkj376b1922016-05-02 11:35:24 -0700170 EncodedImageCallback* post_encode_callback,
171 media_optimization::MediaOptimization* media_opt)
172 : internal_source_(false),
173 post_encode_callback_(post_encode_callback),
ilnik04f4d122017-06-19 07:18:55 -0700174 media_opt_(media_opt),
175 framerate_(1),
176 last_timing_frame_time_ms_(-1),
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100177 timing_frames_thresholds_({-1, 0}),
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100178 incorrect_capture_time_logged_messages_(0),
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100179 reordered_frames_logged_messages_(0),
180 stalled_encoder_logged_messages_(0) {
Sebastian Janssoncabe3832018-01-12 10:54:18 +0100181 rtc::Optional<AlrExperimentSettings> experiment_settings =
182 AlrExperimentSettings::CreateFromFieldTrial(
183 AlrExperimentSettings::kStrictPacingAndProbingExperimentName);
ilnik6d5b4d62017-08-30 03:32:14 -0700184 if (experiment_settings) {
185 experiment_groups_[0] = experiment_settings->group_id + 1;
186 } else {
187 experiment_groups_[0] = 0;
188 }
Sebastian Janssoncabe3832018-01-12 10:54:18 +0100189 experiment_settings = AlrExperimentSettings::CreateFromFieldTrial(
190 AlrExperimentSettings::kScreenshareProbingBweExperimentName);
ilnik6d5b4d62017-08-30 03:32:14 -0700191 if (experiment_settings) {
192 experiment_groups_[1] = experiment_settings->group_id + 1;
193 } else {
194 experiment_groups_[1] = 0;
195 }
196}
niklase@google.com470e71d2011-07-07 08:21:25 +0000197
sprang3911c262016-04-15 01:24:14 -0700198VCMEncodedFrameCallback::~VCMEncodedFrameCallback() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
ilnik04f4d122017-06-19 07:18:55 -0700200void VCMEncodedFrameCallback::OnTargetBitrateChanged(
201 size_t bitrate_bytes_per_second,
202 size_t simulcast_svc_idx) {
203 rtc::CritScope crit(&timing_params_lock_);
204 if (timing_frames_info_.size() < simulcast_svc_idx + 1)
205 timing_frames_info_.resize(simulcast_svc_idx + 1);
206 timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec =
207 bitrate_bytes_per_second;
208}
209
210void VCMEncodedFrameCallback::OnFrameRateChanged(size_t framerate) {
211 rtc::CritScope crit(&timing_params_lock_);
212 framerate_ = framerate;
213}
214
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100215void VCMEncodedFrameCallback::OnEncodeStarted(uint32_t rtp_timestamp,
216 int64_t capture_time_ms,
ilnik04f4d122017-06-19 07:18:55 -0700217 size_t simulcast_svc_idx) {
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200218 if (internal_source_) {
219 return;
220 }
ilnik04f4d122017-06-19 07:18:55 -0700221 rtc::CritScope crit(&timing_params_lock_);
222 if (timing_frames_info_.size() < simulcast_svc_idx + 1)
223 timing_frames_info_.resize(simulcast_svc_idx + 1);
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200224 RTC_DCHECK(
225 timing_frames_info_[simulcast_svc_idx].encode_start_list.empty() ||
226 rtc::TimeDiff(capture_time_ms, timing_frames_info_[simulcast_svc_idx]
227 .encode_start_list.back()
228 .capture_time_ms) >= 0);
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +0100229 // If stream is disabled due to low bandwidth OnEncodeStarted still will be
230 // called and have to be ignored.
231 if (timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec == 0)
232 return;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200233 if (timing_frames_info_[simulcast_svc_idx].encode_start_list.size() ==
234 kMaxEncodeStartTimeListSize) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100235 ++stalled_encoder_logged_messages_;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100236 if (stalled_encoder_logged_messages_ <= kMessagesThrottlingThreshold ||
237 stalled_encoder_logged_messages_ % kThrottleRatio == 0) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100238 RTC_LOG(LS_WARNING) << "Too many frames in the encode_start_list."
239 " Did encoder stall?";
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100240 if (stalled_encoder_logged_messages_ == kMessagesThrottlingThreshold) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100241 RTC_LOG(LS_WARNING) << "Too many log messages. Further stalled encoder"
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100242 "warnings will be throttled.";
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100243 }
244 }
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200245 post_encode_callback_->OnDroppedFrame(DropReason::kDroppedByEncoder);
246 timing_frames_info_[simulcast_svc_idx].encode_start_list.pop_front();
247 }
248 timing_frames_info_[simulcast_svc_idx].encode_start_list.emplace_back(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100249 rtp_timestamp, capture_time_ms, rtc::TimeMillis());
ilnik04f4d122017-06-19 07:18:55 -0700250}
251
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200252rtc::Optional<int64_t> VCMEncodedFrameCallback::ExtractEncodeStartTime(
253 size_t simulcast_svc_idx,
254 EncodedImage* encoded_image) {
255 rtc::Optional<int64_t> result;
256 size_t num_simulcast_svc_streams = timing_frames_info_.size();
257 if (simulcast_svc_idx < num_simulcast_svc_streams) {
258 auto encode_start_list =
259 &timing_frames_info_[simulcast_svc_idx].encode_start_list;
260 // Skip frames for which there was OnEncodeStarted but no OnEncodedImage
261 // call. These are dropped by encoder internally.
262 // Because some hardware encoders don't preserve capture timestamp we
263 // use RTP timestamps here.
264 while (!encode_start_list->empty() &&
265 IsNewerTimestamp(encoded_image->_timeStamp,
266 encode_start_list->front().rtp_timestamp)) {
267 post_encode_callback_->OnDroppedFrame(DropReason::kDroppedByEncoder);
268 encode_start_list->pop_front();
269 }
270 if (encode_start_list->size() > 0 &&
271 encode_start_list->front().rtp_timestamp == encoded_image->_timeStamp) {
272 result.emplace(encode_start_list->front().encode_start_time_ms);
273 if (encoded_image->capture_time_ms_ !=
274 encode_start_list->front().capture_time_ms) {
275 // Force correct capture timestamp.
276 encoded_image->capture_time_ms_ =
277 encode_start_list->front().capture_time_ms;
278 ++incorrect_capture_time_logged_messages_;
279 if (incorrect_capture_time_logged_messages_ <=
280 kMessagesThrottlingThreshold ||
281 incorrect_capture_time_logged_messages_ % kThrottleRatio == 0) {
282 RTC_LOG(LS_WARNING)
283 << "Encoder is not preserving capture timestamps.";
284 if (incorrect_capture_time_logged_messages_ ==
285 kMessagesThrottlingThreshold) {
286 RTC_LOG(LS_WARNING) << "Too many log messages. Further incorrect "
287 "timestamps warnings will be throttled.";
288 }
289 }
290 }
291 encode_start_list->pop_front();
292 } else {
293 ++reordered_frames_logged_messages_;
294 if (reordered_frames_logged_messages_ <= kMessagesThrottlingThreshold ||
295 reordered_frames_logged_messages_ % kThrottleRatio == 0) {
296 RTC_LOG(LS_WARNING) << "Frame with no encode started time recordings. "
297 "Encoder may be reordering frames "
298 "or not preserving RTP timestamps.";
299 if (reordered_frames_logged_messages_ == kMessagesThrottlingThreshold) {
300 RTC_LOG(LS_WARNING) << "Too many log messages. Further frames "
301 "reordering warnings will be throttled.";
302 }
303 }
304 }
305 }
306 return result;
307}
308
309void VCMEncodedFrameCallback::FillTimingInfo(size_t simulcast_svc_idx,
310 EncodedImage* encoded_image) {
311 rtc::Optional<size_t> outlier_frame_size;
312 rtc::Optional<int64_t> encode_start_ms;
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200313 uint8_t timing_flags = VideoSendTiming::kNotTriggered;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200314 {
315 rtc::CritScope crit(&timing_params_lock_);
316
317 // Encoders with internal sources do not call OnEncodeStarted
318 // |timing_frames_info_| may be not filled here.
319 if (!internal_source_) {
320 encode_start_ms =
321 ExtractEncodeStartTime(simulcast_svc_idx, encoded_image);
322 }
323
324 if (timing_frames_info_.size() > simulcast_svc_idx) {
325 size_t target_bitrate =
326 timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec;
327 if (framerate_ > 0 && target_bitrate > 0) {
328 // framerate and target bitrate were reported by encoder.
329 size_t average_frame_size = target_bitrate / framerate_;
330 outlier_frame_size.emplace(
331 average_frame_size *
332 timing_frames_thresholds_.outlier_ratio_percent / 100);
333 }
334 }
335
336 // Outliers trigger timing frames, but do not affect scheduled timing
337 // frames.
338 if (outlier_frame_size && encoded_image->_length >= *outlier_frame_size) {
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200339 timing_flags |= VideoSendTiming::kTriggeredBySize;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200340 }
341
342 // Check if it's time to send a timing frame.
343 int64_t timing_frame_delay_ms =
344 encoded_image->capture_time_ms_ - last_timing_frame_time_ms_;
345 // Trigger threshold if it's a first frame, too long passed since the last
346 // timing frame, or we already sent timing frame on a different simulcast
347 // stream with the same capture time.
348 if (last_timing_frame_time_ms_ == -1 ||
349 timing_frame_delay_ms >= timing_frames_thresholds_.delay_ms ||
350 timing_frame_delay_ms == 0) {
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200351 timing_flags |= VideoSendTiming::kTriggeredByTimer;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200352 last_timing_frame_time_ms_ = encoded_image->capture_time_ms_;
353 }
354 } // rtc::CritScope crit(&timing_params_lock_);
355
356 int64_t now_ms = rtc::TimeMillis();
357 // Workaround for chromoting encoder: it passes encode start and finished
358 // timestamps in |timing_| field, but they (together with capture timestamp)
359 // are not in the WebRTC clock.
360 if (internal_source_ && encoded_image->timing_.encode_finish_ms > 0 &&
361 encoded_image->timing_.encode_start_ms > 0) {
362 int64_t clock_offset_ms = now_ms - encoded_image->timing_.encode_finish_ms;
363 // Translate capture timestamp to local WebRTC clock.
364 encoded_image->capture_time_ms_ += clock_offset_ms;
365 encoded_image->_timeStamp =
366 static_cast<uint32_t>(encoded_image->capture_time_ms_ * 90);
367 encode_start_ms.emplace(encoded_image->timing_.encode_start_ms +
368 clock_offset_ms);
369 }
370
371 // If encode start is not available that means that encoder uses internal
372 // source. In that case capture timestamp may be from a different clock with a
373 // drift relative to rtc::TimeMillis(). We can't use it for Timing frames,
374 // because to being sent in the network capture time required to be less than
375 // all the other timestamps.
376 if (encode_start_ms) {
377 encoded_image->SetEncodeTime(*encode_start_ms, now_ms);
378 encoded_image->timing_.flags = timing_flags;
379 } else {
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200380 encoded_image->timing_.flags = VideoSendTiming::kInvalid;
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200381 }
382}
383
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700384EncodedImageCallback::Result VCMEncodedFrameCallback::OnEncodedImage(
Peter Boströmb7d9a972015-12-18 16:01:11 +0100385 const EncodedImage& encoded_image,
sprang3911c262016-04-15 01:24:14 -0700386 const CodecSpecificInfo* codec_specific,
387 const RTPFragmentationHeader* fragmentation_header) {
pbosd9eec762015-11-17 06:03:43 -0800388 TRACE_EVENT_INSTANT1("webrtc", "VCMEncodedFrameCallback::Encoded",
Peter Boströmb7d9a972015-12-18 16:01:11 +0100389 "timestamp", encoded_image._timeStamp);
ilnik04f4d122017-06-19 07:18:55 -0700390 size_t simulcast_svc_idx = 0;
391 if (codec_specific->codecType == kVideoCodecVP9) {
392 if (codec_specific->codecSpecific.VP9.num_spatial_layers > 1)
393 simulcast_svc_idx = codec_specific->codecSpecific.VP9.spatial_idx;
394 } else if (codec_specific->codecType == kVideoCodecVP8) {
395 simulcast_svc_idx = codec_specific->codecSpecific.VP8.simulcastIdx;
396 } else if (codec_specific->codecType == kVideoCodecGeneric) {
397 simulcast_svc_idx = codec_specific->codecSpecific.generic.simulcast_idx;
398 } else if (codec_specific->codecType == kVideoCodecH264) {
399 // TODO(ilnik): When h264 simulcast is landed, extract simulcast idx here.
400 }
401
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100402 EncodedImage image_copy(encoded_image);
403
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +0200404 FillTimingInfo(simulcast_svc_idx, &image_copy);
ilnik04f4d122017-06-19 07:18:55 -0700405
ilnik6d5b4d62017-08-30 03:32:14 -0700406 // Piggyback ALR experiment group id and simulcast id into the content type.
407 uint8_t experiment_id =
408 experiment_groups_[videocontenttypehelpers::IsScreenshare(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100409 image_copy.content_type_)];
ilnik6d5b4d62017-08-30 03:32:14 -0700410
411 // TODO(ilnik): This will force content type extension to be present even
412 // for realtime video. At the expense of miniscule overhead we will get
413 // sliced receive statistics.
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100414 RTC_CHECK(videocontenttypehelpers::SetExperimentId(&image_copy.content_type_,
415 experiment_id));
ilnik6d5b4d62017-08-30 03:32:14 -0700416 // We count simulcast streams from 1 on the wire. That's why we set simulcast
417 // id in content type to +1 of that is actual simulcast index. This is because
418 // value 0 on the wire is reserved for 'no simulcast stream specified'.
419 RTC_CHECK(videocontenttypehelpers::SetSimulcastId(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100420 &image_copy.content_type_, static_cast<uint8_t>(simulcast_svc_idx + 1)));
ilnik6d5b4d62017-08-30 03:32:14 -0700421
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700422 Result result = post_encode_callback_->OnEncodedImage(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100423 image_copy, codec_specific, fragmentation_header);
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700424 if (result.error != Result::OK)
425 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000426
sprang3911c262016-04-15 01:24:14 -0700427 if (media_opt_) {
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100428 media_opt_->UpdateWithEncodedData(image_copy._length,
429 image_copy._frameType);
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700430 if (internal_source_) {
431 // Signal to encoder to drop next frame.
432 result.drop_next_frame = media_opt_->DropFrame();
433 }
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000434 }
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700435 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000436}
437
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000438} // namespace webrtc