stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "test/fake_encoder.h" |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 12 | |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 13 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame^] | 14 | |
palmkvist | e75f204 | 2016-09-28 06:19:48 -0700 | [diff] [blame] | 15 | #include <algorithm> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 16 | #include <cstdint> |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 17 | #include <memory> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 18 | #include <string> |
palmkvist | e75f204 | 2016-09-28 06:19:48 -0700 | [diff] [blame] | 19 | |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 20 | #include "absl/memory/memory.h" |
Danil Chapovalov | 471783f | 2019-03-11 14:26:02 +0100 | [diff] [blame] | 21 | #include "api/task_queue/queued_task.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 22 | #include "api/video/video_content_type.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 23 | #include "modules/video_coding/codecs/h264/include/h264_globals.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "modules/video_coding/include/video_codec_interface.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 25 | #include "modules/video_coding/include/video_error_codes.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "rtc_base/checks.h" |
| 27 | #include "system_wrappers/include/sleep.h" |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 28 | |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 29 | namespace webrtc { |
pbos@webrtc.org | cb5118c | 2013-09-03 09:10:37 +0000 | [diff] [blame] | 30 | namespace test { |
Per Kjellander | 17fc7e2 | 2018-11-06 11:01:06 +0100 | [diff] [blame] | 31 | namespace { |
Ilya Nikolaevskiy | 6003e7a | 2018-10-15 10:47:23 +0200 | [diff] [blame] | 32 | const int kKeyframeSizeFactor = 5; |
| 33 | |
| 34 | // Inverse of proportion of frames assigned to each temporal layer for all |
| 35 | // possible temporal layers numbers. |
| 36 | const int kTemporalLayerRateFactor[4][4] = { |
| 37 | {1, 0, 0, 0}, // 1/1 |
| 38 | {2, 2, 0, 0}, // 1/2 + 1/2 |
| 39 | {4, 4, 2, 0}, // 1/4 + 1/4 + 1/2 |
| 40 | {8, 8, 4, 2}, // 1/8 + 1/8 + 1/4 + 1/2 |
| 41 | }; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 42 | |
Per Kjellander | 17fc7e2 | 2018-11-06 11:01:06 +0100 | [diff] [blame] | 43 | void WriteCounter(unsigned char* payload, uint32_t counter) { |
| 44 | payload[0] = (counter & 0x00FF); |
| 45 | payload[1] = (counter & 0xFF00) >> 8; |
| 46 | payload[2] = (counter & 0xFF0000) >> 16; |
| 47 | payload[3] = (counter & 0xFF000000) >> 24; |
| 48 | } |
| 49 | |
Nico Weber | 22f9925 | 2019-02-20 10:13:16 -0500 | [diff] [blame] | 50 | } // namespace |
Per Kjellander | 17fc7e2 | 2018-11-06 11:01:06 +0100 | [diff] [blame] | 51 | |
Per Kjellander | 65cc52e | 2019-02-01 11:27:43 +0100 | [diff] [blame] | 52 | FakeEncoder::FakeEncoder(Clock* clock) |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 53 | : clock_(clock), |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 54 | callback_(nullptr), |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 55 | max_target_bitrate_kbps_(-1), |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 56 | pending_keyframe_(true), |
Per Kjellander | 17fc7e2 | 2018-11-06 11:01:06 +0100 | [diff] [blame] | 57 | counter_(0), |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 58 | debt_bytes_(0) { |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 59 | for (bool& used : used_layers_) { |
| 60 | used = false; |
| 61 | } |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Elad Alon | 8f01c4e | 2019-06-28 15:19:43 +0200 | [diff] [blame] | 64 | void FakeEncoder::SetFecControllerOverride( |
| 65 | FecControllerOverride* fec_controller_override) { |
| 66 | // Ignored. |
| 67 | } |
| 68 | |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 69 | void FakeEncoder::SetMaxBitrate(int max_kbps) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 70 | RTC_DCHECK_GE(max_kbps, -1); // max_kbps == -1 disables it. |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 71 | rtc::CritScope cs(&crit_sect_); |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 72 | max_target_bitrate_kbps_ = max_kbps; |
Erik Språng | 16cb8f5 | 2019-04-12 13:59:09 +0200 | [diff] [blame] | 73 | SetRates(current_rate_settings_); |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 74 | } |
| 75 | |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 76 | int32_t FakeEncoder::InitEncode(const VideoCodec* config, |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 77 | const Settings& settings) { |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 78 | rtc::CritScope cs(&crit_sect_); |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 79 | config_ = *config; |
Erik Språng | 16cb8f5 | 2019-04-12 13:59:09 +0200 | [diff] [blame] | 80 | current_rate_settings_.bitrate.SetBitrate(0, 0, config_.startBitrate * 1000); |
| 81 | current_rate_settings_.framerate_fps = config_.maxFramerate; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 82 | pending_keyframe_ = true; |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 83 | last_frame_info_ = FrameInfo(); |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 87 | int32_t FakeEncoder::Encode(const VideoFrame& input_image, |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 88 | const std::vector<VideoFrameType>* frame_types) { |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 89 | unsigned char max_framerate; |
| 90 | unsigned char num_simulcast_streams; |
| 91 | SimulcastStream simulcast_streams[kMaxSimulcastStreams]; |
| 92 | EncodedImageCallback* callback; |
Erik Språng | 16cb8f5 | 2019-04-12 13:59:09 +0200 | [diff] [blame] | 93 | RateControlParameters rates; |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 94 | VideoCodecMode mode; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 95 | bool keyframe; |
Per Kjellander | 17fc7e2 | 2018-11-06 11:01:06 +0100 | [diff] [blame] | 96 | uint32_t counter; |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 97 | { |
| 98 | rtc::CritScope cs(&crit_sect_); |
| 99 | max_framerate = config_.maxFramerate; |
| 100 | num_simulcast_streams = config_.numberOfSimulcastStreams; |
| 101 | for (int i = 0; i < num_simulcast_streams; ++i) { |
| 102 | simulcast_streams[i] = config_.simulcastStream[i]; |
| 103 | } |
| 104 | callback = callback_; |
Erik Språng | 16cb8f5 | 2019-04-12 13:59:09 +0200 | [diff] [blame] | 105 | rates = current_rate_settings_; |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 106 | mode = config_.mode; |
Erik Språng | 16cb8f5 | 2019-04-12 13:59:09 +0200 | [diff] [blame] | 107 | if (rates.framerate_fps <= 0.0) { |
| 108 | rates.framerate_fps = max_framerate; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 109 | } |
| 110 | keyframe = pending_keyframe_; |
| 111 | pending_keyframe_ = false; |
Per Kjellander | 17fc7e2 | 2018-11-06 11:01:06 +0100 | [diff] [blame] | 112 | counter = counter_++; |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 115 | FrameInfo frame_info = |
Erik Språng | 16cb8f5 | 2019-04-12 13:59:09 +0200 | [diff] [blame] | 116 | NextFrame(frame_types, keyframe, num_simulcast_streams, rates.bitrate, |
| 117 | simulcast_streams, static_cast<int>(rates.framerate_fps + 0.5)); |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 118 | for (uint8_t i = 0; i < frame_info.layers.size(); ++i) { |
Per Kjellander | 17fc7e2 | 2018-11-06 11:01:06 +0100 | [diff] [blame] | 119 | constexpr int kMinPayLoadLength = 14; |
Per Kjellander | 841c912 | 2018-10-04 18:40:28 +0200 | [diff] [blame] | 120 | if (frame_info.layers[i].size < kMinPayLoadLength) { |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 121 | // Drop this temporal layer. |
| 122 | continue; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 123 | } |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 124 | |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 125 | EncodedImage encoded; |
Niels Möller | 4d504c7 | 2019-06-18 15:56:56 +0200 | [diff] [blame] | 126 | encoded.SetEncodedData( |
| 127 | EncodedImageBuffer::Create(frame_info.layers[i].size)); |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 128 | |
Per Kjellander | 65cc52e | 2019-02-01 11:27:43 +0100 | [diff] [blame] | 129 | // Fill the buffer with arbitrary data. Write someting to make Asan happy. |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 130 | memset(encoded.data(), 9, frame_info.layers[i].size); |
Per Kjellander | 17fc7e2 | 2018-11-06 11:01:06 +0100 | [diff] [blame] | 131 | // Write a counter to the image to make each frame unique. |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 132 | WriteCounter(encoded.data() + frame_info.layers[i].size - 4, counter); |
Niels Möller | 72bc8d6 | 2018-09-12 10:03:51 +0200 | [diff] [blame] | 133 | encoded.SetTimestamp(input_image.timestamp()); |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 134 | encoded._frameType = frame_info.keyframe ? VideoFrameType::kVideoFrameKey |
| 135 | : VideoFrameType::kVideoFrameDelta; |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 136 | encoded._encodedWidth = simulcast_streams[i].width; |
| 137 | encoded._encodedHeight = simulcast_streams[i].height; |
Niels Möller | d3b8c63 | 2018-08-27 15:33:42 +0200 | [diff] [blame] | 138 | encoded.SetSpatialIndex(i); |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 139 | CodecSpecificInfo codec_specific; |
| 140 | std::unique_ptr<RTPFragmentationHeader> fragmentation = |
| 141 | EncodeHook(&encoded, &codec_specific); |
| 142 | |
| 143 | if (callback->OnEncodedImage(encoded, &codec_specific, fragmentation.get()) |
| 144 | .error != EncodedImageCallback::Result::OK) { |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 145 | return -1; |
sergeyu | 2cb155a | 2016-11-04 11:39:29 -0700 | [diff] [blame] | 146 | } |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 147 | } |
| 148 | return 0; |
| 149 | } |
| 150 | |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 151 | std::unique_ptr<RTPFragmentationHeader> FakeEncoder::EncodeHook( |
| 152 | EncodedImage* encoded_image, |
| 153 | CodecSpecificInfo* codec_specific) { |
| 154 | codec_specific->codecType = kVideoCodecGeneric; |
| 155 | return nullptr; |
| 156 | } |
| 157 | |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 158 | FakeEncoder::FrameInfo FakeEncoder::NextFrame( |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 159 | const std::vector<VideoFrameType>* frame_types, |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 160 | bool keyframe, |
| 161 | uint8_t num_simulcast_streams, |
| 162 | const VideoBitrateAllocation& target_bitrate, |
| 163 | SimulcastStream simulcast_streams[kMaxSimulcastStreams], |
| 164 | int framerate) { |
| 165 | FrameInfo frame_info; |
| 166 | frame_info.keyframe = keyframe; |
| 167 | |
| 168 | if (frame_types) { |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 169 | for (VideoFrameType frame_type : *frame_types) { |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 170 | if (frame_type == VideoFrameType::kVideoFrameKey) { |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 171 | frame_info.keyframe = true; |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
Yves Gerey | 0414040 | 2018-10-08 11:39:18 +0200 | [diff] [blame] | 177 | rtc::CritScope cs(&crit_sect_); |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 178 | for (uint8_t i = 0; i < num_simulcast_streams; ++i) { |
| 179 | if (target_bitrate.GetBitrate(i, 0) > 0) { |
| 180 | int temporal_id = last_frame_info_.layers.size() > i |
| 181 | ? ++last_frame_info_.layers[i].temporal_id % |
| 182 | simulcast_streams[i].numberOfTemporalLayers |
| 183 | : 0; |
| 184 | frame_info.layers.emplace_back(0, temporal_id); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if (last_frame_info_.layers.size() < frame_info.layers.size()) { |
| 189 | // A new keyframe is needed since a new layer will be added. |
| 190 | frame_info.keyframe = true; |
| 191 | } |
| 192 | |
| 193 | for (uint8_t i = 0; i < frame_info.layers.size(); ++i) { |
| 194 | FrameInfo::SpatialLayer& layer_info = frame_info.layers[i]; |
| 195 | if (frame_info.keyframe) { |
| 196 | layer_info.temporal_id = 0; |
| 197 | size_t avg_frame_size = |
Ilya Nikolaevskiy | 6003e7a | 2018-10-15 10:47:23 +0200 | [diff] [blame] | 198 | (target_bitrate.GetBitrate(i, 0) + 7) * |
| 199 | kTemporalLayerRateFactor[frame_info.layers.size() - 1][i] / |
| 200 | (8 * framerate); |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 201 | |
| 202 | // The first frame is a key frame and should be larger. |
| 203 | // Store the overshoot bytes and distribute them over the coming frames, |
| 204 | // so that we on average meet the bitrate target. |
| 205 | debt_bytes_ += (kKeyframeSizeFactor - 1) * avg_frame_size; |
| 206 | layer_info.size = kKeyframeSizeFactor * avg_frame_size; |
| 207 | } else { |
| 208 | size_t avg_frame_size = |
Ilya Nikolaevskiy | 6003e7a | 2018-10-15 10:47:23 +0200 | [diff] [blame] | 209 | (target_bitrate.GetBitrate(i, layer_info.temporal_id) + 7) * |
| 210 | kTemporalLayerRateFactor[frame_info.layers.size() - 1][i] / |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 211 | (8 * framerate); |
| 212 | layer_info.size = avg_frame_size; |
| 213 | if (debt_bytes_ > 0) { |
| 214 | // Pay at most half of the frame size for old debts. |
| 215 | size_t payment_size = std::min(avg_frame_size / 2, debt_bytes_); |
| 216 | debt_bytes_ -= payment_size; |
| 217 | layer_info.size -= payment_size; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | last_frame_info_ = frame_info; |
| 222 | return frame_info; |
| 223 | } |
| 224 | |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 225 | int32_t FakeEncoder::RegisterEncodeCompleteCallback( |
| 226 | EncodedImageCallback* callback) { |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 227 | rtc::CritScope cs(&crit_sect_); |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 228 | callback_ = callback; |
| 229 | return 0; |
| 230 | } |
| 231 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 232 | int32_t FakeEncoder::Release() { |
| 233 | return 0; |
| 234 | } |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 235 | |
Erik Språng | 16cb8f5 | 2019-04-12 13:59:09 +0200 | [diff] [blame] | 236 | void FakeEncoder::SetRates(const RateControlParameters& parameters) { |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 237 | rtc::CritScope cs(&crit_sect_); |
Erik Språng | 16cb8f5 | 2019-04-12 13:59:09 +0200 | [diff] [blame] | 238 | current_rate_settings_ = parameters; |
| 239 | int allocated_bitrate_kbps = parameters.bitrate.get_sum_kbps(); |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 240 | |
| 241 | // Scale bitrate allocation to not exceed the given max target bitrate. |
| 242 | if (max_target_bitrate_kbps_ > 0 && |
| 243 | allocated_bitrate_kbps > max_target_bitrate_kbps_) { |
| 244 | for (uint8_t spatial_idx = 0; spatial_idx < kMaxSpatialLayers; |
| 245 | ++spatial_idx) { |
| 246 | for (uint8_t temporal_idx = 0; temporal_idx < kMaxTemporalStreams; |
| 247 | ++temporal_idx) { |
Erik Språng | 16cb8f5 | 2019-04-12 13:59:09 +0200 | [diff] [blame] | 248 | if (current_rate_settings_.bitrate.HasBitrate(spatial_idx, |
| 249 | temporal_idx)) { |
| 250 | uint32_t bitrate = current_rate_settings_.bitrate.GetBitrate( |
| 251 | spatial_idx, temporal_idx); |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 252 | bitrate = static_cast<uint32_t>( |
| 253 | (bitrate * int64_t{max_target_bitrate_kbps_}) / |
| 254 | allocated_bitrate_kbps); |
Erik Språng | 16cb8f5 | 2019-04-12 13:59:09 +0200 | [diff] [blame] | 255 | current_rate_settings_.bitrate.SetBitrate(spatial_idx, temporal_idx, |
| 256 | bitrate); |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 261 | } |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 262 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 263 | const char* FakeEncoder::kImplementationName = "fake_encoder"; |
Erik Språng | df351f4 | 2018-11-06 11:33:15 +0100 | [diff] [blame] | 264 | VideoEncoder::EncoderInfo FakeEncoder::GetEncoderInfo() const { |
| 265 | EncoderInfo info; |
| 266 | info.implementation_name = kImplementationName; |
| 267 | return info; |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 268 | } |
| 269 | |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 270 | int FakeEncoder::GetConfiguredInputFramerate() const { |
| 271 | rtc::CritScope cs(&crit_sect_); |
Erik Språng | 16cb8f5 | 2019-04-12 13:59:09 +0200 | [diff] [blame] | 272 | return static_cast<int>(current_rate_settings_.framerate_fps + 0.5); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 273 | } |
| 274 | |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 275 | FakeH264Encoder::FakeH264Encoder(Clock* clock) |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 276 | : FakeEncoder(clock), idr_counter_(0) {} |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 277 | |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 278 | std::unique_ptr<RTPFragmentationHeader> FakeH264Encoder::EncodeHook( |
| 279 | EncodedImage* encoded_image, |
| 280 | CodecSpecificInfo* codec_specific) { |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 281 | const size_t kSpsSize = 8; |
| 282 | const size_t kPpsSize = 11; |
| 283 | const int kIdrFrequency = 10; |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 284 | int current_idr_counter; |
| 285 | { |
| 286 | rtc::CritScope cs(&local_crit_sect_); |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 287 | current_idr_counter = idr_counter_; |
| 288 | ++idr_counter_; |
| 289 | } |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 290 | auto fragmentation = absl::make_unique<RTPFragmentationHeader>(); |
| 291 | |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 292 | if (current_idr_counter % kIdrFrequency == 0 && |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 293 | encoded_image->size() > kSpsSize + kPpsSize + 1) { |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 294 | const size_t kNumSlices = 3; |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 295 | fragmentation->VerifyAndAllocateFragmentationHeader(kNumSlices); |
| 296 | fragmentation->fragmentationOffset[0] = 0; |
| 297 | fragmentation->fragmentationLength[0] = kSpsSize; |
| 298 | fragmentation->fragmentationOffset[1] = kSpsSize; |
| 299 | fragmentation->fragmentationLength[1] = kPpsSize; |
| 300 | fragmentation->fragmentationOffset[2] = kSpsSize + kPpsSize; |
| 301 | fragmentation->fragmentationLength[2] = |
| 302 | encoded_image->size() - (kSpsSize + kPpsSize); |
glaznev@webrtc.org | e69220c | 2015-02-05 17:56:15 +0000 | [diff] [blame] | 303 | const size_t kSpsNalHeader = 0x67; |
| 304 | const size_t kPpsNalHeader = 0x68; |
| 305 | const size_t kIdrNalHeader = 0x65; |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 306 | encoded_image->data()[fragmentation->fragmentationOffset[0]] = |
Niels Möller | 938dd9f | 2019-02-07 00:02:17 +0100 | [diff] [blame] | 307 | kSpsNalHeader; |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 308 | encoded_image->data()[fragmentation->fragmentationOffset[1]] = |
Niels Möller | 938dd9f | 2019-02-07 00:02:17 +0100 | [diff] [blame] | 309 | kPpsNalHeader; |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 310 | encoded_image->data()[fragmentation->fragmentationOffset[2]] = |
Niels Möller | 938dd9f | 2019-02-07 00:02:17 +0100 | [diff] [blame] | 311 | kIdrNalHeader; |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 312 | } else { |
| 313 | const size_t kNumSlices = 1; |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 314 | fragmentation->VerifyAndAllocateFragmentationHeader(kNumSlices); |
| 315 | fragmentation->fragmentationOffset[0] = 0; |
| 316 | fragmentation->fragmentationLength[0] = encoded_image->size(); |
glaznev@webrtc.org | e69220c | 2015-02-05 17:56:15 +0000 | [diff] [blame] | 317 | const size_t kNalHeader = 0x41; |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 318 | encoded_image->data()[fragmentation->fragmentationOffset[0]] = kNalHeader; |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 319 | } |
| 320 | uint8_t value = 0; |
| 321 | int fragment_counter = 0; |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 322 | for (size_t i = 0; i < encoded_image->size(); ++i) { |
| 323 | if (fragment_counter == fragmentation->fragmentationVectorSize || |
| 324 | i != fragmentation->fragmentationOffset[fragment_counter]) { |
| 325 | encoded_image->data()[i] = value++; |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 326 | } else { |
| 327 | ++fragment_counter; |
| 328 | } |
| 329 | } |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 330 | codec_specific->codecType = kVideoCodecH264; |
| 331 | codec_specific->codecSpecific.H264.packetization_mode = |
hta | 9aa9688 | 2016-12-06 05:36:03 -0800 | [diff] [blame] | 332 | H264PacketizationMode::NonInterleaved; |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 333 | |
| 334 | return fragmentation; |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 335 | } |
asapersson@webrtc.org | 049e4ec | 2014-11-20 10:19:46 +0000 | [diff] [blame] | 336 | |
| 337 | DelayedEncoder::DelayedEncoder(Clock* clock, int delay_ms) |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 338 | : test::FakeEncoder(clock), delay_ms_(delay_ms) { |
| 339 | // The encoder could be created on a different thread than |
| 340 | // it is being used on. |
| 341 | sequence_checker_.Detach(); |
| 342 | } |
asapersson@webrtc.org | 049e4ec | 2014-11-20 10:19:46 +0000 | [diff] [blame] | 343 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 344 | void DelayedEncoder::SetDelay(int delay_ms) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 345 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 346 | delay_ms_ = delay_ms; |
| 347 | } |
| 348 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 349 | int32_t DelayedEncoder::Encode(const VideoFrame& input_image, |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 350 | const std::vector<VideoFrameType>* frame_types) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 351 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 352 | |
| 353 | SleepMs(delay_ms_); |
| 354 | |
Niels Möller | b859b32 | 2019-03-07 12:40:01 +0100 | [diff] [blame] | 355 | return FakeEncoder::Encode(input_image, frame_types); |
asapersson@webrtc.org | 049e4ec | 2014-11-20 10:19:46 +0000 | [diff] [blame] | 356 | } |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 357 | |
Danil Chapovalov | 22ed366 | 2019-03-19 19:39:49 +0100 | [diff] [blame] | 358 | MultithreadedFakeH264Encoder::MultithreadedFakeH264Encoder( |
| 359 | Clock* clock, |
| 360 | TaskQueueFactory* task_queue_factory) |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 361 | : test::FakeH264Encoder(clock), |
Danil Chapovalov | 22ed366 | 2019-03-19 19:39:49 +0100 | [diff] [blame] | 362 | task_queue_factory_(task_queue_factory), |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 363 | current_queue_(0), |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 364 | queue1_(nullptr), |
| 365 | queue2_(nullptr) { |
| 366 | // The encoder could be created on a different thread than |
| 367 | // it is being used on. |
| 368 | sequence_checker_.Detach(); |
| 369 | } |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 370 | |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 371 | int32_t MultithreadedFakeH264Encoder::InitEncode(const VideoCodec* config, |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 372 | const Settings& settings) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 373 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 374 | |
Danil Chapovalov | 22ed366 | 2019-03-19 19:39:49 +0100 | [diff] [blame] | 375 | queue1_ = task_queue_factory_->CreateTaskQueue( |
| 376 | "Queue 1", TaskQueueFactory::Priority::NORMAL); |
| 377 | queue2_ = task_queue_factory_->CreateTaskQueue( |
| 378 | "Queue 2", TaskQueueFactory::Priority::NORMAL); |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 379 | |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 380 | return FakeH264Encoder::InitEncode(config, settings); |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 381 | } |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 382 | |
Danil Chapovalov | 471783f | 2019-03-11 14:26:02 +0100 | [diff] [blame] | 383 | class MultithreadedFakeH264Encoder::EncodeTask : public QueuedTask { |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 384 | public: |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 385 | EncodeTask(MultithreadedFakeH264Encoder* encoder, |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 386 | const VideoFrame& input_image, |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 387 | const std::vector<VideoFrameType>* frame_types) |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 388 | : encoder_(encoder), |
| 389 | input_image_(input_image), |
Niels Möller | b859b32 | 2019-03-07 12:40:01 +0100 | [diff] [blame] | 390 | frame_types_(*frame_types) {} |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 391 | |
| 392 | private: |
| 393 | bool Run() override { |
Niels Möller | b859b32 | 2019-03-07 12:40:01 +0100 | [diff] [blame] | 394 | encoder_->EncodeCallback(input_image_, &frame_types_); |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 395 | return true; |
| 396 | } |
| 397 | |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 398 | MultithreadedFakeH264Encoder* const encoder_; |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 399 | VideoFrame input_image_; |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 400 | std::vector<VideoFrameType> frame_types_; |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 401 | }; |
| 402 | |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 403 | int32_t MultithreadedFakeH264Encoder::Encode( |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 404 | const VideoFrame& input_image, |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 405 | const std::vector<VideoFrameType>* frame_types) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 406 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 407 | |
Danil Chapovalov | 22ed366 | 2019-03-19 19:39:49 +0100 | [diff] [blame] | 408 | TaskQueueBase* queue = |
| 409 | (current_queue_++ % 2 == 0) ? queue1_.get() : queue2_.get(); |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 410 | |
| 411 | if (!queue) { |
| 412 | return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 413 | } |
| 414 | |
Danil Chapovalov | 471783f | 2019-03-11 14:26:02 +0100 | [diff] [blame] | 415 | queue->PostTask( |
| 416 | absl::make_unique<EncodeTask>(this, input_image, frame_types)); |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 417 | |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 418 | return WEBRTC_VIDEO_CODEC_OK; |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 419 | } |
| 420 | |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 421 | int32_t MultithreadedFakeH264Encoder::EncodeCallback( |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 422 | const VideoFrame& input_image, |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 423 | const std::vector<VideoFrameType>* frame_types) { |
Niels Möller | b859b32 | 2019-03-07 12:40:01 +0100 | [diff] [blame] | 424 | return FakeH264Encoder::Encode(input_image, frame_types); |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 425 | } |
| 426 | |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 427 | int32_t MultithreadedFakeH264Encoder::Release() { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 428 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 429 | |
| 430 | queue1_.reset(); |
| 431 | queue2_.reset(); |
| 432 | |
| 433 | return FakeH264Encoder::Release(); |
| 434 | } |
| 435 | |
pbos@webrtc.org | cb5118c | 2013-09-03 09:10:37 +0000 | [diff] [blame] | 436 | } // namespace test |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 437 | } // namespace webrtc |