blob: 034fe0330bd09f76ed5552cf9c436496deed88b0 [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 "
71 "payload name: "
72 << settings->plName;
Peter Boström4f5db112015-10-29 16:53:59 +010073 return -1;
74 }
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020075 vcm_encoded_frame_callback_->Reset();
Peter Boström4f5db112015-10-29 16:53:59 +010076 encoder_->RegisterEncodeCompleteCallback(vcm_encoded_frame_callback_);
77 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000078}
79
sprang3911c262016-04-15 01:24:14 -070080int32_t VCMGenericEncoder::Encode(const VideoFrame& frame,
81 const CodecSpecificInfo* codec_specific,
82 const std::vector<FrameType>& frame_types) {
Peter Boström02bafc62016-07-01 12:45:15 +020083 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
pbosd9eec762015-11-17 06:03:43 -080084 TRACE_EVENT1("webrtc", "VCMGenericEncoder::Encode", "timestamp",
sprang3911c262016-04-15 01:24:14 -070085 frame.timestamp());
pbosd9eec762015-11-17 06:03:43 -080086
sprang3911c262016-04-15 01:24:14 -070087 for (FrameType frame_type : frame_types)
pbos22993e12015-10-19 02:39:06 -070088 RTC_DCHECK(frame_type == kVideoFrameKey || frame_type == kVideoFrameDelta);
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +000089
ilnik04f4d122017-06-19 07:18:55 -070090 for (size_t i = 0; i < streams_or_svc_num_; ++i)
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +010091 vcm_encoded_frame_callback_->OnEncodeStarted(frame.timestamp(),
92 frame.render_time_ms(), i);
Peter Boströmb7d9a972015-12-18 16:01:11 +010093
ilnike9973812017-09-12 10:24:46 -070094 return encoder_->Encode(frame, codec_specific, &frame_types);
niklase@google.com470e71d2011-07-07 08:21:25 +000095}
96
Peter Boström69ccb332015-10-29 16:30:23 +010097void VCMGenericEncoder::SetEncoderParameters(const EncoderParameters& params) {
Peter Boström02bafc62016-07-01 12:45:15 +020098 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Peter Boström69ccb332015-10-29 16:30:23 +010099 bool channel_parameters_have_changed;
100 bool rates_have_changed;
101 {
102 rtc::CritScope lock(&params_lock_);
103 channel_parameters_have_changed =
104 params.loss_rate != encoder_params_.loss_rate ||
105 params.rtt != encoder_params_.rtt;
106 rates_have_changed =
107 params.target_bitrate != encoder_params_.target_bitrate ||
108 params.input_frame_rate != encoder_params_.input_frame_rate;
109 encoder_params_ = params;
110 }
Erik Språng08127a92016-11-16 16:41:30 +0100111 if (channel_parameters_have_changed) {
112 int res = encoder_->SetChannelParameters(params.loss_rate, params.rtt);
113 if (res != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100114 RTC_LOG(LS_WARNING) << "Error set encoder parameters (loss = "
115 << params.loss_rate << ", rtt = " << params.rtt
116 << "): " << res;
Erik Språng08127a92016-11-16 16:41:30 +0100117 }
118 }
Peter Boström69ccb332015-10-29 16:30:23 +0100119 if (rates_have_changed) {
Erik Språng08127a92016-11-16 16:41:30 +0100120 int res = encoder_->SetRateAllocation(params.target_bitrate,
121 params.input_frame_rate);
122 if (res != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100123 RTC_LOG(LS_WARNING) << "Error set encoder rate (total bitrate bps = "
124 << params.target_bitrate.get_sum_bps()
125 << ", framerate = " << params.input_frame_rate
126 << "): " << res;
Erik Språng08127a92016-11-16 16:41:30 +0100127 }
ilnik04f4d122017-06-19 07:18:55 -0700128 vcm_encoded_frame_callback_->OnFrameRateChanged(params.input_frame_rate);
129 for (size_t i = 0; i < streams_or_svc_num_; ++i) {
130 size_t layer_bitrate_bytes_per_sec =
131 params.target_bitrate.GetSpatialLayerSum(i) / 8;
132 // VP9 rate control is not yet moved out of VP9Impl. Due to that rates
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +0100133 // are not split among spatial layers. Use default 1:2:4 bitrate
134 // distribution.
135 // TODO(ilnik): move bitrate per spatial layer calculations out of
136 // vp9_impl.cc and drop the check below.
137 if (codec_type_ == kVideoCodecVP9) {
138 int scaling_factor_num = 1 << i; // 1, 2 or 4
139 int scaling_factor_den = (1 << streams_or_svc_num_) - 1; // 1 + 2 + 4
140 layer_bitrate_bytes_per_sec = params.target_bitrate.get_sum_bps() / 8 *
141 scaling_factor_num / scaling_factor_den;
142 }
ilnik04f4d122017-06-19 07:18:55 -0700143 vcm_encoded_frame_callback_->OnTargetBitrateChanged(
144 layer_bitrate_bytes_per_sec, i);
145 }
Peter Boström69ccb332015-10-29 16:30:23 +0100146 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000147}
148
Peter Boström69ccb332015-10-29 16:30:23 +0100149EncoderParameters VCMGenericEncoder::GetEncoderParameters() const {
150 rtc::CritScope lock(&params_lock_);
151 return encoder_params_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000152}
153
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000154int32_t VCMGenericEncoder::RequestFrame(
stefan@webrtc.orgcf216862012-10-25 11:29:51 +0000155 const std::vector<FrameType>& frame_types) {
Peter Boström02bafc62016-07-01 12:45:15 +0200156 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
nissedf2ceb82016-12-15 06:29:53 -0800157
158 // TODO(nisse): Used only with internal source. Delete as soon as
159 // that feature is removed. The only implementation I've been able
nisse01d5a0b2017-05-31 06:33:21 -0700160 // to find ignores what's in the frame. With one exception: It seems
161 // a few test cases, e.g.,
162 // VideoSendStreamTest.VideoSendStreamStopSetEncoderRateToZero, set
163 // internal_source to true and use FakeEncoder. And the latter will
164 // happily encode this 1x1 frame and pass it on down the pipeline.
nissedf2ceb82016-12-15 06:29:53 -0800165 return encoder_->Encode(VideoFrame(I420Buffer::Create(1, 1),
166 kVideoRotation_0, 0),
167 NULL, &frame_types);
168 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000169}
170
philipel9d3ab612015-12-21 04:12:39 -0800171bool VCMGenericEncoder::InternalSource() const {
172 return internal_source_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000173}
174
Peter Boströmeb66e802015-06-05 11:08:03 +0200175bool VCMGenericEncoder::SupportsNativeHandle() const {
Peter Boström02bafc62016-07-01 12:45:15 +0200176 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
Peter Boströmeb66e802015-06-05 11:08:03 +0200177 return encoder_->SupportsNativeHandle();
178}
179
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000180VCMEncodedFrameCallback::VCMEncodedFrameCallback(
perkj376b1922016-05-02 11:35:24 -0700181 EncodedImageCallback* post_encode_callback,
182 media_optimization::MediaOptimization* media_opt)
183 : internal_source_(false),
184 post_encode_callback_(post_encode_callback),
ilnik04f4d122017-06-19 07:18:55 -0700185 media_opt_(media_opt),
186 framerate_(1),
187 last_timing_frame_time_ms_(-1),
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100188 timing_frames_thresholds_({-1, 0}),
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100189 incorrect_capture_time_logged_messages_(0),
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100190 reordered_frames_logged_messages_(0),
191 stalled_encoder_logged_messages_(0) {
Sebastian Janssoncabe3832018-01-12 10:54:18 +0100192 rtc::Optional<AlrExperimentSettings> experiment_settings =
193 AlrExperimentSettings::CreateFromFieldTrial(
194 AlrExperimentSettings::kStrictPacingAndProbingExperimentName);
ilnik6d5b4d62017-08-30 03:32:14 -0700195 if (experiment_settings) {
196 experiment_groups_[0] = experiment_settings->group_id + 1;
197 } else {
198 experiment_groups_[0] = 0;
199 }
Sebastian Janssoncabe3832018-01-12 10:54:18 +0100200 experiment_settings = AlrExperimentSettings::CreateFromFieldTrial(
201 AlrExperimentSettings::kScreenshareProbingBweExperimentName);
ilnik6d5b4d62017-08-30 03:32:14 -0700202 if (experiment_settings) {
203 experiment_groups_[1] = experiment_settings->group_id + 1;
204 } else {
205 experiment_groups_[1] = 0;
206 }
207}
niklase@google.com470e71d2011-07-07 08:21:25 +0000208
sprang3911c262016-04-15 01:24:14 -0700209VCMEncodedFrameCallback::~VCMEncodedFrameCallback() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000210
ilnik04f4d122017-06-19 07:18:55 -0700211void VCMEncodedFrameCallback::OnTargetBitrateChanged(
212 size_t bitrate_bytes_per_second,
213 size_t simulcast_svc_idx) {
214 rtc::CritScope crit(&timing_params_lock_);
215 if (timing_frames_info_.size() < simulcast_svc_idx + 1)
216 timing_frames_info_.resize(simulcast_svc_idx + 1);
217 timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec =
218 bitrate_bytes_per_second;
219}
220
221void VCMEncodedFrameCallback::OnFrameRateChanged(size_t framerate) {
222 rtc::CritScope crit(&timing_params_lock_);
223 framerate_ = framerate;
224}
225
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100226void VCMEncodedFrameCallback::OnEncodeStarted(uint32_t rtp_timestamp,
227 int64_t capture_time_ms,
ilnik04f4d122017-06-19 07:18:55 -0700228 size_t simulcast_svc_idx) {
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200229 if (internal_source_) {
230 return;
231 }
ilnik04f4d122017-06-19 07:18:55 -0700232 rtc::CritScope crit(&timing_params_lock_);
233 if (timing_frames_info_.size() < simulcast_svc_idx + 1)
234 timing_frames_info_.resize(simulcast_svc_idx + 1);
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200235 RTC_DCHECK(
236 timing_frames_info_[simulcast_svc_idx].encode_start_list.empty() ||
237 rtc::TimeDiff(capture_time_ms, timing_frames_info_[simulcast_svc_idx]
238 .encode_start_list.back()
239 .capture_time_ms) >= 0);
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +0100240 // If stream is disabled due to low bandwidth OnEncodeStarted still will be
241 // called and have to be ignored.
242 if (timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec == 0)
243 return;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200244 if (timing_frames_info_[simulcast_svc_idx].encode_start_list.size() ==
245 kMaxEncodeStartTimeListSize) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100246 ++stalled_encoder_logged_messages_;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100247 if (stalled_encoder_logged_messages_ <= kMessagesThrottlingThreshold ||
248 stalled_encoder_logged_messages_ % kThrottleRatio == 0) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100249 RTC_LOG(LS_WARNING) << "Too many frames in the encode_start_list."
250 " Did encoder stall?";
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100251 if (stalled_encoder_logged_messages_ == kMessagesThrottlingThreshold) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100252 RTC_LOG(LS_WARNING) << "Too many log messages. Further stalled encoder"
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100253 "warnings will be throttled.";
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100254 }
255 }
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200256 post_encode_callback_->OnDroppedFrame(DropReason::kDroppedByEncoder);
257 timing_frames_info_[simulcast_svc_idx].encode_start_list.pop_front();
258 }
259 timing_frames_info_[simulcast_svc_idx].encode_start_list.emplace_back(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100260 rtp_timestamp, capture_time_ms, rtc::TimeMillis());
ilnik04f4d122017-06-19 07:18:55 -0700261}
262
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700263EncodedImageCallback::Result VCMEncodedFrameCallback::OnEncodedImage(
Peter Boströmb7d9a972015-12-18 16:01:11 +0100264 const EncodedImage& encoded_image,
sprang3911c262016-04-15 01:24:14 -0700265 const CodecSpecificInfo* codec_specific,
266 const RTPFragmentationHeader* fragmentation_header) {
pbosd9eec762015-11-17 06:03:43 -0800267 TRACE_EVENT_INSTANT1("webrtc", "VCMEncodedFrameCallback::Encoded",
Peter Boströmb7d9a972015-12-18 16:01:11 +0100268 "timestamp", encoded_image._timeStamp);
ilnik04f4d122017-06-19 07:18:55 -0700269 size_t simulcast_svc_idx = 0;
270 if (codec_specific->codecType == kVideoCodecVP9) {
271 if (codec_specific->codecSpecific.VP9.num_spatial_layers > 1)
272 simulcast_svc_idx = codec_specific->codecSpecific.VP9.spatial_idx;
273 } else if (codec_specific->codecType == kVideoCodecVP8) {
274 simulcast_svc_idx = codec_specific->codecSpecific.VP8.simulcastIdx;
275 } else if (codec_specific->codecType == kVideoCodecGeneric) {
276 simulcast_svc_idx = codec_specific->codecSpecific.generic.simulcast_idx;
277 } else if (codec_specific->codecType == kVideoCodecH264) {
278 // TODO(ilnik): When h264 simulcast is landed, extract simulcast idx here.
279 }
280
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100281 EncodedImage image_copy(encoded_image);
282
ilnik29d08402017-07-11 08:08:12 -0700283 rtc::Optional<size_t> outlier_frame_size;
284 rtc::Optional<int64_t> encode_start_ms;
ilnik6d5b4d62017-08-30 03:32:14 -0700285 size_t num_simulcast_svc_streams = 1;
Niels Möllerc241af92017-11-10 08:51:29 +0100286 uint8_t timing_flags = TimingFrameFlags::kNotTriggered;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200287 if (!internal_source_) {
ilnik04f4d122017-06-19 07:18:55 -0700288 rtc::CritScope crit(&timing_params_lock_);
ilnik0b1e2f32017-07-10 12:25:29 -0700289
ilnik29d08402017-07-11 08:08:12 -0700290 // Encoders with internal sources do not call OnEncodeStarted and
291 // OnFrameRateChanged. |timing_frames_info_| may be not filled here.
ilnik6d5b4d62017-08-30 03:32:14 -0700292 num_simulcast_svc_streams = timing_frames_info_.size();
293 if (simulcast_svc_idx < num_simulcast_svc_streams) {
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200294 auto encode_start_list =
295 &timing_frames_info_[simulcast_svc_idx].encode_start_list;
296 // Skip frames for which there was OnEncodeStarted but no OnEncodedImage
297 // call. These are dropped by encoder internally.
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100298 // Because some hardware encoders don't preserve capture timestamp we use
299 // RTP timestamps here.
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200300 while (!encode_start_list->empty() &&
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100301 IsNewerTimestamp(image_copy._timeStamp,
302 encode_start_list->front().rtp_timestamp)) {
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200303 post_encode_callback_->OnDroppedFrame(DropReason::kDroppedByEncoder);
304 encode_start_list->pop_front();
305 }
306 if (encode_start_list->size() > 0 &&
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100307 encode_start_list->front().rtp_timestamp == image_copy._timeStamp) {
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200308 encode_start_ms.emplace(
309 encode_start_list->front().encode_start_time_ms);
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100310 if (image_copy.capture_time_ms_ !=
311 encode_start_list->front().capture_time_ms) {
312 // Force correct capture timestamp.
313 image_copy.capture_time_ms_ =
314 encode_start_list->front().capture_time_ms;
315 ++incorrect_capture_time_logged_messages_;
316 if (incorrect_capture_time_logged_messages_ <=
317 kMessagesThrottlingThreshold ||
318 incorrect_capture_time_logged_messages_ % kThrottleRatio == 0) {
319 RTC_LOG(LS_WARNING)
320 << "Encoder is not preserving capture timestamps.";
321 if (incorrect_capture_time_logged_messages_ ==
322 kMessagesThrottlingThreshold) {
323 RTC_LOG(LS_WARNING) << "Too many log messages. Further incorrect "
324 "timestamps warnings will be throttled.";
325 }
326 }
327 }
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200328 encode_start_list->pop_front();
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +0100329 } else {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100330 ++reordered_frames_logged_messages_;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100331 if (reordered_frames_logged_messages_ <= kMessagesThrottlingThreshold ||
332 reordered_frames_logged_messages_ % kThrottleRatio == 0) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100333 RTC_LOG(LS_WARNING)
334 << "Frame with no encode started time recordings. "
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100335 "Encoder may be reordering frames "
336 "or not preserving RTP timestamps.";
337 if (reordered_frames_logged_messages_ ==
338 kMessagesThrottlingThreshold) {
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100339 RTC_LOG(LS_WARNING) << "Too many log messages. Further frames "
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100340 "reordering warnings will be throttled.";
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100341 }
342 }
ilnik3635f442017-06-28 03:53:19 -0700343 }
344
ilnik29d08402017-07-11 08:08:12 -0700345 size_t target_bitrate =
346 timing_frames_info_[simulcast_svc_idx].target_bitrate_bytes_per_sec;
347 if (framerate_ > 0 && target_bitrate > 0) {
348 // framerate and target bitrate were reported by encoder.
349 size_t average_frame_size = target_bitrate / framerate_;
350 outlier_frame_size.emplace(
351 average_frame_size *
352 timing_frames_thresholds_.outlier_ratio_percent / 100);
ilnik0b1e2f32017-07-10 12:25:29 -0700353 }
ilnik29d08402017-07-11 08:08:12 -0700354 }
355
356 // Check if it's time to send a timing frame.
357 int64_t timing_frame_delay_ms =
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100358 image_copy.capture_time_ms_ - last_timing_frame_time_ms_;
ilnik29d08402017-07-11 08:08:12 -0700359 // Trigger threshold if it's a first frame, too long passed since the last
360 // timing frame, or we already sent timing frame on a different simulcast
361 // stream with the same capture time.
362 if (last_timing_frame_time_ms_ == -1 ||
363 timing_frame_delay_ms >= timing_frames_thresholds_.delay_ms ||
364 timing_frame_delay_ms == 0) {
sprangba050a62017-08-18 02:51:12 -0700365 timing_flags = TimingFrameFlags::kTriggeredByTimer;
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100366 last_timing_frame_time_ms_ = image_copy.capture_time_ms_;
ilnik29d08402017-07-11 08:08:12 -0700367 }
368
369 // Outliers trigger timing frames, but do not affect scheduled timing
370 // frames.
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100371 if (outlier_frame_size && image_copy._length >= *outlier_frame_size) {
sprangba050a62017-08-18 02:51:12 -0700372 timing_flags |= TimingFrameFlags::kTriggeredBySize;
ilnik04f4d122017-06-19 07:18:55 -0700373 }
ilnik04f4d122017-06-19 07:18:55 -0700374 }
375
ilnik29d08402017-07-11 08:08:12 -0700376 // If encode start is not available that means that encoder uses internal
377 // source. In that case capture timestamp may be from a different clock with a
378 // drift relative to rtc::TimeMillis(). We can't use it for Timing frames,
379 // because to being sent in the network capture time required to be less than
380 // all the other timestamps.
Niels Möllerc241af92017-11-10 08:51:29 +0100381 if (encode_start_ms) {
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100382 image_copy.SetEncodeTime(*encode_start_ms, rtc::TimeMillis());
383 image_copy.timing_.flags = timing_flags;
sprangba050a62017-08-18 02:51:12 -0700384 } else {
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100385 image_copy.timing_.flags = TimingFrameFlags::kInvalid;
ilnik04f4d122017-06-19 07:18:55 -0700386 }
387
ilnik6d5b4d62017-08-30 03:32:14 -0700388 // Piggyback ALR experiment group id and simulcast id into the content type.
389 uint8_t experiment_id =
390 experiment_groups_[videocontenttypehelpers::IsScreenshare(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100391 image_copy.content_type_)];
ilnik6d5b4d62017-08-30 03:32:14 -0700392
393 // TODO(ilnik): This will force content type extension to be present even
394 // for realtime video. At the expense of miniscule overhead we will get
395 // sliced receive statistics.
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100396 RTC_CHECK(videocontenttypehelpers::SetExperimentId(&image_copy.content_type_,
397 experiment_id));
ilnik6d5b4d62017-08-30 03:32:14 -0700398 // We count simulcast streams from 1 on the wire. That's why we set simulcast
399 // id in content type to +1 of that is actual simulcast index. This is because
400 // value 0 on the wire is reserved for 'no simulcast stream specified'.
401 RTC_CHECK(videocontenttypehelpers::SetSimulcastId(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100402 &image_copy.content_type_, static_cast<uint8_t>(simulcast_svc_idx + 1)));
ilnik6d5b4d62017-08-30 03:32:14 -0700403
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700404 Result result = post_encode_callback_->OnEncodedImage(
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100405 image_copy, codec_specific, fragmentation_header);
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700406 if (result.error != Result::OK)
407 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000408
sprang3911c262016-04-15 01:24:14 -0700409 if (media_opt_) {
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100410 media_opt_->UpdateWithEncodedData(image_copy._length,
411 image_copy._frameType);
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700412 if (internal_source_) {
413 // Signal to encoder to drop next frame.
414 result.drop_next_frame = media_opt_->DropFrame();
415 }
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000416 }
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700417 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000418}
419
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000420} // namespace webrtc