blob: f8a9fcca6ae85efa90dca769319dcfa98fa7cc58 [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ång08127a92016-11-16 16:41:30 +010041 encoder_params_({BitrateAllocation(), 0, 0, 0}),
ilnik04f4d122017-06-19 07:18:55 -070042 streams_or_svc_num_(0) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000043
Peter Boström4f5db112015-10-29 16:53:59 +010044VCMGenericEncoder::~VCMGenericEncoder() {}
45
46int32_t VCMGenericEncoder::Release() {
Peter Boström02bafc62016-07-01 12:45:15 +020047 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Peter Boström4fd6cda2016-01-26 10:19:53 +010048 TRACE_EVENT0("webrtc", "VCMGenericEncoder::Release");
Peter Boström4f5db112015-10-29 16:53:59 +010049 return encoder_->Release();
niklase@google.com470e71d2011-07-07 08:21:25 +000050}
51
Peter Boström4f5db112015-10-29 16:53:59 +010052int32_t VCMGenericEncoder::InitEncode(const VideoCodec* settings,
sprang3911c262016-04-15 01:24:14 -070053 int32_t number_of_cores,
54 size_t max_payload_size) {
Peter Boström02bafc62016-07-01 12:45:15 +020055 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
pbosd9eec762015-11-17 06:03:43 -080056 TRACE_EVENT0("webrtc", "VCMGenericEncoder::InitEncode");
ilnik04f4d122017-06-19 07:18:55 -070057 streams_or_svc_num_ = settings->numberOfSimulcastStreams;
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +010058 codec_type_ = settings->codecType;
ilnik04f4d122017-06-19 07:18:55 -070059 if (settings->codecType == kVideoCodecVP9) {
60 streams_or_svc_num_ = settings->VP9().numberOfSpatialLayers;
61 }
62 if (streams_or_svc_num_ == 0)
63 streams_or_svc_num_ = 1;
64
65 vcm_encoded_frame_callback_->SetTimingFramesThresholds(
66 settings->timing_frame_thresholds);
67 vcm_encoded_frame_callback_->OnFrameRateChanged(settings->maxFramerate);
68
sprang3911c262016-04-15 01:24:14 -070069 if (encoder_->InitEncode(settings, number_of_cores, max_payload_size) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010070 RTC_LOG(LS_ERROR) << "Failed to initialize the encoder associated with "
Niels Möller2e1d7842018-02-23 15:41:13 +010071 "codec type: "
72 << CodecTypeToPayloadString(settings->codecType)
73 << " (" << settings->codecType <<")";
Peter Boström4f5db112015-10-29 16:53:59 +010074 return -1;
75 }
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020076 vcm_encoded_frame_callback_->Reset();
Peter Boström4f5db112015-10-29 16:53:59 +010077 encoder_->RegisterEncodeCompleteCallback(vcm_encoded_frame_callback_);
78 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000079}
80
sprang3911c262016-04-15 01:24:14 -070081int32_t VCMGenericEncoder::Encode(const VideoFrame& frame,
82 const CodecSpecificInfo* codec_specific,
83 const std::vector<FrameType>& frame_types) {
Peter Boström02bafc62016-07-01 12:45:15 +020084 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
pbosd9eec762015-11-17 06:03:43 -080085 TRACE_EVENT1("webrtc", "VCMGenericEncoder::Encode", "timestamp",
sprang3911c262016-04-15 01:24:14 -070086 frame.timestamp());
pbosd9eec762015-11-17 06:03:43 -080087
sprang3911c262016-04-15 01:24:14 -070088 for (FrameType frame_type : frame_types)
pbos22993e12015-10-19 02:39:06 -070089 RTC_DCHECK(frame_type == kVideoFrameKey || frame_type == kVideoFrameDelta);
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +000090
ilnik04f4d122017-06-19 07:18:55 -070091 for (size_t i = 0; i < streams_or_svc_num_; ++i)
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +010092 vcm_encoded_frame_callback_->OnEncodeStarted(frame.timestamp(),
93 frame.render_time_ms(), i);
Peter Boströmb7d9a972015-12-18 16:01:11 +010094
ilnike9973812017-09-12 10:24:46 -070095 return encoder_->Encode(frame, codec_specific, &frame_types);
niklase@google.com470e71d2011-07-07 08:21:25 +000096}
97
Peter Boström69ccb332015-10-29 16:30:23 +010098void VCMGenericEncoder::SetEncoderParameters(const EncoderParameters& params) {
Peter Boström02bafc62016-07-01 12:45:15 +020099 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Peter Boström69ccb332015-10-29 16:30:23 +0100100 bool channel_parameters_have_changed;
101 bool rates_have_changed;
102 {
103 rtc::CritScope lock(&params_lock_);
104 channel_parameters_have_changed =
105 params.loss_rate != encoder_params_.loss_rate ||
106 params.rtt != encoder_params_.rtt;
107 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 }
Erik Språng08127a92016-11-16 16:41:30 +0100112 if (channel_parameters_have_changed) {
113 int res = encoder_->SetChannelParameters(params.loss_rate, params.rtt);
114 if (res != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100115 RTC_LOG(LS_WARNING) << "Error set encoder parameters (loss = "
116 << params.loss_rate << ", rtt = " << params.rtt
117 << "): " << res;
Erik Språng08127a92016-11-16 16:41:30 +0100118 }
119 }
Peter Boström69ccb332015-10-29 16:30:23 +0100120 if (rates_have_changed) {
Erik Språng08127a92016-11-16 16:41:30 +0100121 int res = encoder_->SetRateAllocation(params.target_bitrate,
122 params.input_frame_rate);
123 if (res != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100124 RTC_LOG(LS_WARNING) << "Error set encoder rate (total bitrate bps = "
125 << params.target_bitrate.get_sum_bps()
126 << ", framerate = " << params.input_frame_rate
127 << "): " << res;
Erik Språng08127a92016-11-16 16:41:30 +0100128 }
ilnik04f4d122017-06-19 07:18:55 -0700129 vcm_encoded_frame_callback_->OnFrameRateChanged(params.input_frame_rate);
130 for (size_t i = 0; i < streams_or_svc_num_; ++i) {
ilnik04f4d122017-06-19 07:18:55 -0700131 vcm_encoded_frame_callback_->OnTargetBitrateChanged(
Sergey Silkin86684962018-03-28 19:32:37 +0200132 params.target_bitrate.GetSpatialLayerSum(i) / 8, i);
ilnik04f4d122017-06-19 07:18:55 -0700133 }
Peter Boström69ccb332015-10-29 16:30:23 +0100134 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000135}
136
Peter Boström69ccb332015-10-29 16:30:23 +0100137EncoderParameters VCMGenericEncoder::GetEncoderParameters() const {
138 rtc::CritScope lock(&params_lock_);
139 return encoder_params_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000140}
141
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000142int32_t VCMGenericEncoder::RequestFrame(
stefan@webrtc.orgcf216862012-10-25 11:29:51 +0000143 const std::vector<FrameType>& frame_types) {
Peter Boström02bafc62016-07-01 12:45:15 +0200144 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
nissedf2ceb82016-12-15 06:29:53 -0800145
146 // TODO(nisse): Used only with internal source. Delete as soon as
147 // that feature is removed. The only implementation I've been able
nisse01d5a0b2017-05-31 06:33:21 -0700148 // to find ignores what's in the frame. With one exception: It seems
149 // a few test cases, e.g.,
150 // VideoSendStreamTest.VideoSendStreamStopSetEncoderRateToZero, set
151 // internal_source to true and use FakeEncoder. And the latter will
152 // happily encode this 1x1 frame and pass it on down the pipeline.
nissedf2ceb82016-12-15 06:29:53 -0800153 return encoder_->Encode(VideoFrame(I420Buffer::Create(1, 1),
154 kVideoRotation_0, 0),
155 NULL, &frame_types);
156 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000157}
158
philipel9d3ab612015-12-21 04:12:39 -0800159bool VCMGenericEncoder::InternalSource() const {
160 return internal_source_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000161}
162
Peter Boströmeb66e802015-06-05 11:08:03 +0200163bool VCMGenericEncoder::SupportsNativeHandle() const {
Peter Boström02bafc62016-07-01 12:45:15 +0200164 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Peter Boströmeb66e802015-06-05 11:08:03 +0200165 return encoder_->SupportsNativeHandle();
166}
167
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000168VCMEncodedFrameCallback::VCMEncodedFrameCallback(
perkj376b1922016-05-02 11:35:24 -0700169 EncodedImageCallback* post_encode_callback,
170 media_optimization::MediaOptimization* media_opt)
171 : internal_source_(false),
172 post_encode_callback_(post_encode_callback),
ilnik04f4d122017-06-19 07:18:55 -0700173 media_opt_(media_opt),
174 framerate_(1),
175 last_timing_frame_time_ms_(-1),
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100176 timing_frames_thresholds_({-1, 0}),
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100177 incorrect_capture_time_logged_messages_(0),
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100178 reordered_frames_logged_messages_(0),
179 stalled_encoder_logged_messages_(0) {
Sebastian Janssoncabe3832018-01-12 10:54:18 +0100180 rtc::Optional<AlrExperimentSettings> experiment_settings =
181 AlrExperimentSettings::CreateFromFieldTrial(
182 AlrExperimentSettings::kStrictPacingAndProbingExperimentName);
ilnik6d5b4d62017-08-30 03:32:14 -0700183 if (experiment_settings) {
184 experiment_groups_[0] = experiment_settings->group_id + 1;
185 } else {
186 experiment_groups_[0] = 0;
187 }
Sebastian Janssoncabe3832018-01-12 10:54:18 +0100188 experiment_settings = AlrExperimentSettings::CreateFromFieldTrial(
189 AlrExperimentSettings::kScreenshareProbingBweExperimentName);
ilnik6d5b4d62017-08-30 03:32:14 -0700190 if (experiment_settings) {
191 experiment_groups_[1] = experiment_settings->group_id + 1;
192 } else {
193 experiment_groups_[1] = 0;
194 }
195}
niklase@google.com470e71d2011-07-07 08:21:25 +0000196
sprang3911c262016-04-15 01:24:14 -0700197VCMEncodedFrameCallback::~VCMEncodedFrameCallback() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000198
ilnik04f4d122017-06-19 07:18:55 -0700199void VCMEncodedFrameCallback::OnTargetBitrateChanged(
200 size_t bitrate_bytes_per_second,
201 size_t simulcast_svc_idx) {
202 rtc::CritScope crit(&timing_params_lock_);
203 if (timing_frames_info_.size() < simulcast_svc_idx + 1)
204 timing_frames_info_.resize(simulcast_svc_idx + 1);
205 timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec =
206 bitrate_bytes_per_second;
207}
208
209void VCMEncodedFrameCallback::OnFrameRateChanged(size_t framerate) {
210 rtc::CritScope crit(&timing_params_lock_);
211 framerate_ = framerate;
212}
213
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100214void VCMEncodedFrameCallback::OnEncodeStarted(uint32_t rtp_timestamp,
215 int64_t capture_time_ms,
ilnik04f4d122017-06-19 07:18:55 -0700216 size_t simulcast_svc_idx) {
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200217 if (internal_source_) {
218 return;
219 }
ilnik04f4d122017-06-19 07:18:55 -0700220 rtc::CritScope crit(&timing_params_lock_);
221 if (timing_frames_info_.size() < simulcast_svc_idx + 1)
222 timing_frames_info_.resize(simulcast_svc_idx + 1);
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200223 RTC_DCHECK(
224 timing_frames_info_[simulcast_svc_idx].encode_start_list.empty() ||
225 rtc::TimeDiff(capture_time_ms, timing_frames_info_[simulcast_svc_idx]
226 .encode_start_list.back()
227 .capture_time_ms) >= 0);
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +0100228 // If stream is disabled due to low bandwidth OnEncodeStarted still will be
229 // called and have to be ignored.
230 if (timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec == 0)
231 return;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200232 if (timing_frames_info_[simulcast_svc_idx].encode_start_list.size() ==
233 kMaxEncodeStartTimeListSize) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100234 ++stalled_encoder_logged_messages_;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100235 if (stalled_encoder_logged_messages_ <= kMessagesThrottlingThreshold ||
236 stalled_encoder_logged_messages_ % kThrottleRatio == 0) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100237 RTC_LOG(LS_WARNING) << "Too many frames in the encode_start_list."
238 " Did encoder stall?";
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100239 if (stalled_encoder_logged_messages_ == kMessagesThrottlingThreshold) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100240 RTC_LOG(LS_WARNING) << "Too many log messages. Further stalled encoder"
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100241 "warnings will be throttled.";
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100242 }
243 }
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200244 post_encode_callback_->OnDroppedFrame(DropReason::kDroppedByEncoder);
245 timing_frames_info_[simulcast_svc_idx].encode_start_list.pop_front();
246 }
247 timing_frames_info_[simulcast_svc_idx].encode_start_list.emplace_back(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100248 rtp_timestamp, capture_time_ms, rtc::TimeMillis());
ilnik04f4d122017-06-19 07:18:55 -0700249}
250
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700251EncodedImageCallback::Result VCMEncodedFrameCallback::OnEncodedImage(
Peter Boströmb7d9a972015-12-18 16:01:11 +0100252 const EncodedImage& encoded_image,
sprang3911c262016-04-15 01:24:14 -0700253 const CodecSpecificInfo* codec_specific,
254 const RTPFragmentationHeader* fragmentation_header) {
pbosd9eec762015-11-17 06:03:43 -0800255 TRACE_EVENT_INSTANT1("webrtc", "VCMEncodedFrameCallback::Encoded",
Peter Boströmb7d9a972015-12-18 16:01:11 +0100256 "timestamp", encoded_image._timeStamp);
ilnik04f4d122017-06-19 07:18:55 -0700257 size_t simulcast_svc_idx = 0;
258 if (codec_specific->codecType == kVideoCodecVP9) {
259 if (codec_specific->codecSpecific.VP9.num_spatial_layers > 1)
260 simulcast_svc_idx = codec_specific->codecSpecific.VP9.spatial_idx;
261 } else if (codec_specific->codecType == kVideoCodecVP8) {
262 simulcast_svc_idx = codec_specific->codecSpecific.VP8.simulcastIdx;
263 } else if (codec_specific->codecType == kVideoCodecGeneric) {
264 simulcast_svc_idx = codec_specific->codecSpecific.generic.simulcast_idx;
265 } else if (codec_specific->codecType == kVideoCodecH264) {
266 // TODO(ilnik): When h264 simulcast is landed, extract simulcast idx here.
267 }
268
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100269 EncodedImage image_copy(encoded_image);
270
Ilya Nikolaevskiy41579362018-03-30 06:54:39 +0000271 rtc::Optional<size_t> outlier_frame_size;
272 rtc::Optional<int64_t> encode_start_ms;
273 size_t num_simulcast_svc_streams = 1;
274 uint8_t timing_flags = TimingFrameFlags::kNotTriggered;
275 if (!internal_source_) {
276 rtc::CritScope crit(&timing_params_lock_);
277
278 // Encoders with internal sources do not call OnEncodeStarted and
279 // OnFrameRateChanged. |timing_frames_info_| may be not filled here.
280 num_simulcast_svc_streams = timing_frames_info_.size();
281 if (simulcast_svc_idx < num_simulcast_svc_streams) {
282 auto encode_start_list =
283 &timing_frames_info_[simulcast_svc_idx].encode_start_list;
284 // Skip frames for which there was OnEncodeStarted but no OnEncodedImage
285 // call. These are dropped by encoder internally.
286 // Because some hardware encoders don't preserve capture timestamp we use
287 // RTP timestamps here.
288 while (!encode_start_list->empty() &&
289 IsNewerTimestamp(image_copy._timeStamp,
290 encode_start_list->front().rtp_timestamp)) {
291 post_encode_callback_->OnDroppedFrame(DropReason::kDroppedByEncoder);
292 encode_start_list->pop_front();
293 }
294 if (encode_start_list->size() > 0 &&
295 encode_start_list->front().rtp_timestamp == image_copy._timeStamp) {
296 encode_start_ms.emplace(
297 encode_start_list->front().encode_start_time_ms);
298 if (image_copy.capture_time_ms_ !=
299 encode_start_list->front().capture_time_ms) {
300 // Force correct capture timestamp.
301 image_copy.capture_time_ms_ =
302 encode_start_list->front().capture_time_ms;
303 ++incorrect_capture_time_logged_messages_;
304 if (incorrect_capture_time_logged_messages_ <=
305 kMessagesThrottlingThreshold ||
306 incorrect_capture_time_logged_messages_ % kThrottleRatio == 0) {
307 RTC_LOG(LS_WARNING)
308 << "Encoder is not preserving capture timestamps.";
309 if (incorrect_capture_time_logged_messages_ ==
310 kMessagesThrottlingThreshold) {
311 RTC_LOG(LS_WARNING) << "Too many log messages. Further incorrect "
312 "timestamps warnings will be throttled.";
313 }
314 }
315 }
316 encode_start_list->pop_front();
317 } else {
318 ++reordered_frames_logged_messages_;
319 if (reordered_frames_logged_messages_ <= kMessagesThrottlingThreshold ||
320 reordered_frames_logged_messages_ % kThrottleRatio == 0) {
321 RTC_LOG(LS_WARNING)
322 << "Frame with no encode started time recordings. "
323 "Encoder may be reordering frames "
324 "or not preserving RTP timestamps.";
325 if (reordered_frames_logged_messages_ ==
326 kMessagesThrottlingThreshold) {
327 RTC_LOG(LS_WARNING) << "Too many log messages. Further frames "
328 "reordering warnings will be throttled.";
329 }
330 }
331 }
332
333 size_t target_bitrate =
334 timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec;
335 if (framerate_ > 0 && target_bitrate > 0) {
336 // framerate and target bitrate were reported by encoder.
337 size_t average_frame_size = target_bitrate / framerate_;
338 outlier_frame_size.emplace(
339 average_frame_size *
340 timing_frames_thresholds_.outlier_ratio_percent / 100);
341 }
342 }
343
344 // Check if it's time to send a timing frame.
345 int64_t timing_frame_delay_ms =
346 image_copy.capture_time_ms_ - last_timing_frame_time_ms_;
347 // Trigger threshold if it's a first frame, too long passed since the last
348 // timing frame, or we already sent timing frame on a different simulcast
349 // stream with the same capture time.
350 if (last_timing_frame_time_ms_ == -1 ||
351 timing_frame_delay_ms >= timing_frames_thresholds_.delay_ms ||
352 timing_frame_delay_ms == 0) {
353 timing_flags = TimingFrameFlags::kTriggeredByTimer;
354 last_timing_frame_time_ms_ = image_copy.capture_time_ms_;
355 }
356
357 // Outliers trigger timing frames, but do not affect scheduled timing
358 // frames.
359 if (outlier_frame_size && image_copy._length >= *outlier_frame_size) {
360 timing_flags |= TimingFrameFlags::kTriggeredBySize;
361 }
362 }
363
364 // If encode start is not available that means that encoder uses internal
365 // source. In that case capture timestamp may be from a different clock with a
366 // drift relative to rtc::TimeMillis(). We can't use it for Timing frames,
367 // because to being sent in the network capture time required to be less than
368 // all the other timestamps.
369 if (encode_start_ms) {
370 image_copy.SetEncodeTime(*encode_start_ms, rtc::TimeMillis());
371 image_copy.timing_.flags = timing_flags;
372 } else {
373 image_copy.timing_.flags = TimingFrameFlags::kInvalid;
374 }
ilnik04f4d122017-06-19 07:18:55 -0700375
ilnik6d5b4d62017-08-30 03:32:14 -0700376 // Piggyback ALR experiment group id and simulcast id into the content type.
377 uint8_t experiment_id =
378 experiment_groups_[videocontenttypehelpers::IsScreenshare(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100379 image_copy.content_type_)];
ilnik6d5b4d62017-08-30 03:32:14 -0700380
381 // TODO(ilnik): This will force content type extension to be present even
382 // for realtime video. At the expense of miniscule overhead we will get
383 // sliced receive statistics.
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100384 RTC_CHECK(videocontenttypehelpers::SetExperimentId(&image_copy.content_type_,
385 experiment_id));
ilnik6d5b4d62017-08-30 03:32:14 -0700386 // We count simulcast streams from 1 on the wire. That's why we set simulcast
387 // id in content type to +1 of that is actual simulcast index. This is because
388 // value 0 on the wire is reserved for 'no simulcast stream specified'.
389 RTC_CHECK(videocontenttypehelpers::SetSimulcastId(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100390 &image_copy.content_type_, static_cast<uint8_t>(simulcast_svc_idx + 1)));
ilnik6d5b4d62017-08-30 03:32:14 -0700391
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700392 Result result = post_encode_callback_->OnEncodedImage(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100393 image_copy, codec_specific, fragmentation_header);
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700394 if (result.error != Result::OK)
395 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000396
sprang3911c262016-04-15 01:24:14 -0700397 if (media_opt_) {
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100398 media_opt_->UpdateWithEncodedData(image_copy._length,
399 image_copy._frameType);
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700400 if (internal_source_) {
401 // Signal to encoder to drop next frame.
402 result.drop_next_frame = media_opt_->DropFrame();
403 }
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000404 }
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700405 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000406}
407
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000408} // namespace webrtc