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> |
| 14 | |
palmkvist | e75f204 | 2016-09-28 06:19:48 -0700 | [diff] [blame] | 15 | #include <algorithm> |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 16 | #include <memory> |
palmkvist | e75f204 | 2016-09-28 06:19:48 -0700 | [diff] [blame] | 17 | |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 18 | #include "common_types.h" // NOLINT(build/include) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "modules/video_coding/include/video_codec_interface.h" |
| 20 | #include "rtc_base/checks.h" |
| 21 | #include "system_wrappers/include/sleep.h" |
| 22 | #include "test/gtest.h" |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 23 | |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 24 | namespace webrtc { |
pbos@webrtc.org | cb5118c | 2013-09-03 09:10:37 +0000 | [diff] [blame] | 25 | namespace test { |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 26 | |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 27 | const int kKeyframeSizeFactor = 10; |
| 28 | |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 29 | FakeEncoder::FakeEncoder(Clock* clock) |
| 30 | : clock_(clock), |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 31 | callback_(nullptr), |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 32 | configured_input_framerate_(-1), |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 33 | max_target_bitrate_kbps_(-1), |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 34 | pending_keyframe_(true), |
| 35 | debt_bytes_(0) { |
sprang@webrtc.org | 4070935 | 2013-11-26 11:41:59 +0000 | [diff] [blame] | 36 | // Generate some arbitrary not-all-zero data |
| 37 | for (size_t i = 0; i < sizeof(encoded_buffer_); ++i) { |
| 38 | encoded_buffer_[i] = static_cast<uint8_t>(i); |
| 39 | } |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 40 | } |
| 41 | |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 42 | void FakeEncoder::SetMaxBitrate(int max_kbps) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 43 | RTC_DCHECK_GE(max_kbps, -1); // max_kbps == -1 disables it. |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 44 | rtc::CritScope cs(&crit_sect_); |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 45 | max_target_bitrate_kbps_ = max_kbps; |
| 46 | } |
| 47 | |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 48 | int32_t FakeEncoder::InitEncode(const VideoCodec* config, |
| 49 | int32_t number_of_cores, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 50 | size_t max_payload_size) { |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 51 | rtc::CritScope cs(&crit_sect_); |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 52 | config_ = *config; |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 53 | target_bitrate_.SetBitrate(0, 0, config_.startBitrate * 1000); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 54 | configured_input_framerate_ = config_.maxFramerate; |
| 55 | pending_keyframe_ = true; |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 59 | int32_t FakeEncoder::Encode(const VideoFrame& input_image, |
| 60 | const CodecSpecificInfo* codec_specific_info, |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 61 | const std::vector<FrameType>* frame_types) { |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 62 | unsigned char max_framerate; |
| 63 | unsigned char num_simulcast_streams; |
| 64 | SimulcastStream simulcast_streams[kMaxSimulcastStreams]; |
| 65 | EncodedImageCallback* callback; |
| 66 | uint32_t target_bitrate_sum_kbps; |
| 67 | int max_target_bitrate_kbps; |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 68 | size_t num_encoded_bytes; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 69 | int framerate; |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 70 | VideoCodecMode mode; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 71 | bool keyframe; |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 72 | { |
| 73 | rtc::CritScope cs(&crit_sect_); |
| 74 | max_framerate = config_.maxFramerate; |
| 75 | num_simulcast_streams = config_.numberOfSimulcastStreams; |
| 76 | for (int i = 0; i < num_simulcast_streams; ++i) { |
| 77 | simulcast_streams[i] = config_.simulcastStream[i]; |
| 78 | } |
| 79 | callback = callback_; |
| 80 | target_bitrate_sum_kbps = target_bitrate_.get_sum_kbps(); |
| 81 | max_target_bitrate_kbps = max_target_bitrate_kbps_; |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 82 | num_encoded_bytes = sizeof(encoded_buffer_); |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 83 | mode = config_.mode; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 84 | if (configured_input_framerate_ > 0) { |
| 85 | framerate = configured_input_framerate_; |
| 86 | } else { |
| 87 | framerate = max_framerate; |
| 88 | } |
| 89 | keyframe = pending_keyframe_; |
| 90 | pending_keyframe_ = false; |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 91 | } |
| 92 | |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 93 | for (FrameType frame_type : *frame_types) { |
| 94 | if (frame_type == kVideoFrameKey) { |
| 95 | keyframe = true; |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 100 | RTC_DCHECK_GT(max_framerate, 0); |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 101 | |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 102 | size_t bitrate = |
| 103 | std::max(target_bitrate_sum_kbps, simulcast_streams[0].minBitrate); |
| 104 | if (max_target_bitrate_kbps > 0) |
| 105 | bitrate = std::min(bitrate, static_cast<size_t>(max_target_bitrate_kbps)); |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 106 | |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 107 | size_t bits_available = bitrate * 1000 / framerate; |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 108 | |
| 109 | RTC_DCHECK_GT(num_simulcast_streams, 0); |
| 110 | for (unsigned char i = 0; i < num_simulcast_streams; ++i) { |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 111 | CodecSpecificInfo specifics; |
| 112 | memset(&specifics, 0, sizeof(specifics)); |
pbos@webrtc.org | 0181b5f | 2013-09-09 08:26:30 +0000 | [diff] [blame] | 113 | specifics.codecType = kVideoCodecGeneric; |
| 114 | specifics.codecSpecific.generic.simulcast_idx = i; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 115 | size_t min_stream_bits = static_cast<size_t>( |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 116 | (simulcast_streams[i].minBitrate * 1000) / framerate); |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 117 | size_t max_stream_bits = static_cast<size_t>( |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 118 | (simulcast_streams[i].maxBitrate * 1000) / framerate); |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 119 | size_t stream_bits = (bits_available > max_stream_bits) ? max_stream_bits : |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 120 | bits_available; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 121 | size_t stream_bytes = (stream_bits + 7) / 8; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 122 | if (keyframe) { |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 123 | // The first frame is a key frame and should be larger. |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 124 | // Store the overshoot bytes and distribute them over the coming frames, |
| 125 | // so that we on average meet the bitrate target. |
| 126 | debt_bytes_ += (kKeyframeSizeFactor - 1) * stream_bytes; |
| 127 | stream_bytes *= kKeyframeSizeFactor; |
| 128 | } else { |
| 129 | if (debt_bytes_ > 0) { |
| 130 | // Pay at most half of the frame size for old debts. |
| 131 | size_t payment_size = std::min(stream_bytes / 2, debt_bytes_); |
| 132 | debt_bytes_ -= payment_size; |
| 133 | stream_bytes -= payment_size; |
| 134 | } |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 135 | } |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 136 | |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 137 | if (stream_bytes > num_encoded_bytes) |
| 138 | stream_bytes = num_encoded_bytes; |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 139 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 140 | // Always encode something on the first frame. |
| 141 | if (min_stream_bits > bits_available && i > 0) |
| 142 | continue; |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 143 | |
| 144 | std::unique_ptr<uint8_t[]> encoded_buffer(new uint8_t[num_encoded_bytes]); |
| 145 | memcpy(encoded_buffer.get(), encoded_buffer_, num_encoded_bytes); |
| 146 | EncodedImage encoded(encoded_buffer.get(), stream_bytes, num_encoded_bytes); |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 147 | encoded._timeStamp = input_image.timestamp(); |
| 148 | encoded.capture_time_ms_ = input_image.render_time_ms(); |
pbos@webrtc.org | 0181b5f | 2013-09-09 08:26:30 +0000 | [diff] [blame] | 149 | encoded._frameType = (*frame_types)[i]; |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 150 | encoded._encodedWidth = simulcast_streams[i].width; |
| 151 | encoded._encodedHeight = simulcast_streams[i].height; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 152 | encoded.rotation_ = input_image.rotation(); |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 153 | encoded.content_type_ = (mode == kScreensharing) |
| 154 | ? VideoContentType::SCREENSHARE |
| 155 | : VideoContentType::UNSPECIFIED; |
perkj | 275afc5 | 2016-09-01 00:21:16 -0700 | [diff] [blame] | 156 | specifics.codec_name = ImplementationName(); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 157 | specifics.codecSpecific.generic.simulcast_idx = i; |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 158 | RTC_DCHECK(callback); |
| 159 | if (callback->OnEncodedImage(encoded, &specifics, nullptr).error != |
sergeyu | 2cb155a | 2016-11-04 11:39:29 -0700 | [diff] [blame] | 160 | EncodedImageCallback::Result::OK) { |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 161 | return -1; |
sergeyu | 2cb155a | 2016-11-04 11:39:29 -0700 | [diff] [blame] | 162 | } |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 163 | bits_available -= std::min(encoded._length * 8, bits_available); |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 164 | } |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | int32_t FakeEncoder::RegisterEncodeCompleteCallback( |
| 169 | EncodedImageCallback* callback) { |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 170 | rtc::CritScope cs(&crit_sect_); |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 171 | callback_ = callback; |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | int32_t FakeEncoder::Release() { return 0; } |
| 176 | |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 177 | int32_t FakeEncoder::SetChannelParameters(uint32_t packet_loss, int64_t rtt) { |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
Erik Språng | 566124a | 2018-04-23 12:32:22 +0200 | [diff] [blame] | 181 | int32_t FakeEncoder::SetRateAllocation( |
| 182 | const VideoBitrateAllocation& rate_allocation, |
| 183 | uint32_t framerate) { |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 184 | rtc::CritScope cs(&crit_sect_); |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 185 | target_bitrate_ = rate_allocation; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 186 | configured_input_framerate_ = framerate; |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 187 | return 0; |
| 188 | } |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 189 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 190 | const char* FakeEncoder::kImplementationName = "fake_encoder"; |
| 191 | const char* FakeEncoder::ImplementationName() const { |
| 192 | return kImplementationName; |
| 193 | } |
| 194 | |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 195 | int FakeEncoder::GetConfiguredInputFramerate() const { |
| 196 | rtc::CritScope cs(&crit_sect_); |
| 197 | return configured_input_framerate_; |
| 198 | } |
| 199 | |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 200 | FakeH264Encoder::FakeH264Encoder(Clock* clock) |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 201 | : FakeEncoder(clock), callback_(nullptr), idr_counter_(0) { |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 202 | FakeEncoder::RegisterEncodeCompleteCallback(this); |
| 203 | } |
| 204 | |
| 205 | int32_t FakeH264Encoder::RegisterEncodeCompleteCallback( |
| 206 | EncodedImageCallback* callback) { |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 207 | rtc::CritScope cs(&local_crit_sect_); |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 208 | callback_ = callback; |
| 209 | return 0; |
| 210 | } |
| 211 | |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 212 | EncodedImageCallback::Result FakeH264Encoder::OnEncodedImage( |
| 213 | const EncodedImage& encoded_image, |
| 214 | const CodecSpecificInfo* codec_specific_info, |
| 215 | const RTPFragmentationHeader* fragments) { |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 216 | const size_t kSpsSize = 8; |
| 217 | const size_t kPpsSize = 11; |
| 218 | const int kIdrFrequency = 10; |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 219 | EncodedImageCallback* callback; |
| 220 | int current_idr_counter; |
| 221 | { |
| 222 | rtc::CritScope cs(&local_crit_sect_); |
| 223 | callback = callback_; |
| 224 | current_idr_counter = idr_counter_; |
| 225 | ++idr_counter_; |
| 226 | } |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 227 | RTPFragmentationHeader fragmentation; |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 228 | if (current_idr_counter % kIdrFrequency == 0 && |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 229 | encoded_image._length > kSpsSize + kPpsSize + 1) { |
| 230 | const size_t kNumSlices = 3; |
| 231 | fragmentation.VerifyAndAllocateFragmentationHeader(kNumSlices); |
| 232 | fragmentation.fragmentationOffset[0] = 0; |
| 233 | fragmentation.fragmentationLength[0] = kSpsSize; |
| 234 | fragmentation.fragmentationOffset[1] = kSpsSize; |
| 235 | fragmentation.fragmentationLength[1] = kPpsSize; |
| 236 | fragmentation.fragmentationOffset[2] = kSpsSize + kPpsSize; |
| 237 | fragmentation.fragmentationLength[2] = |
| 238 | encoded_image._length - (kSpsSize + kPpsSize); |
glaznev@webrtc.org | e69220c | 2015-02-05 17:56:15 +0000 | [diff] [blame] | 239 | const size_t kSpsNalHeader = 0x67; |
| 240 | const size_t kPpsNalHeader = 0x68; |
| 241 | const size_t kIdrNalHeader = 0x65; |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 242 | encoded_image._buffer[fragmentation.fragmentationOffset[0]] = kSpsNalHeader; |
| 243 | encoded_image._buffer[fragmentation.fragmentationOffset[1]] = kPpsNalHeader; |
| 244 | encoded_image._buffer[fragmentation.fragmentationOffset[2]] = kIdrNalHeader; |
| 245 | } else { |
| 246 | const size_t kNumSlices = 1; |
| 247 | fragmentation.VerifyAndAllocateFragmentationHeader(kNumSlices); |
| 248 | fragmentation.fragmentationOffset[0] = 0; |
| 249 | fragmentation.fragmentationLength[0] = encoded_image._length; |
glaznev@webrtc.org | e69220c | 2015-02-05 17:56:15 +0000 | [diff] [blame] | 250 | const size_t kNalHeader = 0x41; |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 251 | encoded_image._buffer[fragmentation.fragmentationOffset[0]] = kNalHeader; |
| 252 | } |
| 253 | uint8_t value = 0; |
| 254 | int fragment_counter = 0; |
| 255 | for (size_t i = 0; i < encoded_image._length; ++i) { |
| 256 | if (fragment_counter == fragmentation.fragmentationVectorSize || |
| 257 | i != fragmentation.fragmentationOffset[fragment_counter]) { |
| 258 | encoded_image._buffer[i] = value++; |
| 259 | } else { |
| 260 | ++fragment_counter; |
| 261 | } |
| 262 | } |
palmkvist | e75f204 | 2016-09-28 06:19:48 -0700 | [diff] [blame] | 263 | CodecSpecificInfo specifics; |
| 264 | memset(&specifics, 0, sizeof(specifics)); |
| 265 | specifics.codecType = kVideoCodecH264; |
hta | 9aa9688 | 2016-12-06 05:36:03 -0800 | [diff] [blame] | 266 | specifics.codecSpecific.H264.packetization_mode = |
| 267 | H264PacketizationMode::NonInterleaved; |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 268 | RTC_DCHECK(callback); |
| 269 | return callback->OnEncodedImage(encoded_image, &specifics, &fragmentation); |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 270 | } |
asapersson@webrtc.org | 049e4ec | 2014-11-20 10:19:46 +0000 | [diff] [blame] | 271 | |
| 272 | DelayedEncoder::DelayedEncoder(Clock* clock, int delay_ms) |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 273 | : test::FakeEncoder(clock), delay_ms_(delay_ms) { |
| 274 | // The encoder could be created on a different thread than |
| 275 | // it is being used on. |
| 276 | sequence_checker_.Detach(); |
| 277 | } |
asapersson@webrtc.org | 049e4ec | 2014-11-20 10:19:46 +0000 | [diff] [blame] | 278 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 279 | void DelayedEncoder::SetDelay(int delay_ms) { |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 280 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 281 | delay_ms_ = delay_ms; |
| 282 | } |
| 283 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 284 | int32_t DelayedEncoder::Encode(const VideoFrame& input_image, |
asapersson@webrtc.org | 049e4ec | 2014-11-20 10:19:46 +0000 | [diff] [blame] | 285 | const CodecSpecificInfo* codec_specific_info, |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 286 | const std::vector<FrameType>* frame_types) { |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 287 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 288 | |
| 289 | SleepMs(delay_ms_); |
| 290 | |
asapersson@webrtc.org | 049e4ec | 2014-11-20 10:19:46 +0000 | [diff] [blame] | 291 | return FakeEncoder::Encode(input_image, codec_specific_info, frame_types); |
| 292 | } |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 293 | |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 294 | MultithreadedFakeH264Encoder::MultithreadedFakeH264Encoder(Clock* clock) |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 295 | : test::FakeH264Encoder(clock), |
| 296 | current_queue_(0), |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 297 | queue1_(nullptr), |
| 298 | queue2_(nullptr) { |
| 299 | // The encoder could be created on a different thread than |
| 300 | // it is being used on. |
| 301 | sequence_checker_.Detach(); |
| 302 | } |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 303 | |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 304 | int32_t MultithreadedFakeH264Encoder::InitEncode(const VideoCodec* config, |
| 305 | int32_t number_of_cores, |
| 306 | size_t max_payload_size) { |
| 307 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 308 | |
| 309 | queue1_.reset(new rtc::TaskQueue("Queue 1")); |
| 310 | queue2_.reset(new rtc::TaskQueue("Queue 2")); |
| 311 | |
| 312 | return FakeH264Encoder::InitEncode(config, number_of_cores, max_payload_size); |
| 313 | } |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 314 | |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 315 | class MultithreadedFakeH264Encoder::EncodeTask : public rtc::QueuedTask { |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 316 | public: |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 317 | EncodeTask(MultithreadedFakeH264Encoder* encoder, |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 318 | const VideoFrame& input_image, |
| 319 | const CodecSpecificInfo* codec_specific_info, |
| 320 | const std::vector<FrameType>* frame_types) |
| 321 | : encoder_(encoder), |
| 322 | input_image_(input_image), |
| 323 | codec_specific_info_(), |
| 324 | frame_types_(*frame_types) { |
| 325 | if (codec_specific_info) |
| 326 | codec_specific_info_ = *codec_specific_info; |
| 327 | } |
| 328 | |
| 329 | private: |
| 330 | bool Run() override { |
| 331 | encoder_->EncodeCallback(input_image_, &codec_specific_info_, |
| 332 | &frame_types_); |
| 333 | return true; |
| 334 | } |
| 335 | |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 336 | MultithreadedFakeH264Encoder* const encoder_; |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 337 | VideoFrame input_image_; |
| 338 | CodecSpecificInfo codec_specific_info_; |
| 339 | std::vector<FrameType> frame_types_; |
| 340 | }; |
| 341 | |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 342 | int32_t MultithreadedFakeH264Encoder::Encode( |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 343 | const VideoFrame& input_image, |
| 344 | const CodecSpecificInfo* codec_specific_info, |
| 345 | const std::vector<FrameType>* frame_types) { |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 346 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 347 | |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 348 | std::unique_ptr<rtc::TaskQueue>& queue = |
| 349 | (current_queue_++ % 2 == 0) ? queue1_ : queue2_; |
| 350 | |
| 351 | if (!queue) { |
| 352 | return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 353 | } |
| 354 | |
| 355 | queue->PostTask(std::unique_ptr<rtc::QueuedTask>( |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 356 | new EncodeTask(this, input_image, codec_specific_info, frame_types))); |
| 357 | |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 358 | return WEBRTC_VIDEO_CODEC_OK; |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 359 | } |
| 360 | |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 361 | int32_t MultithreadedFakeH264Encoder::EncodeCallback( |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 362 | const VideoFrame& input_image, |
| 363 | const CodecSpecificInfo* codec_specific_info, |
| 364 | const std::vector<FrameType>* frame_types) { |
| 365 | return FakeH264Encoder::Encode(input_image, codec_specific_info, frame_types); |
| 366 | } |
| 367 | |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 368 | int32_t MultithreadedFakeH264Encoder::Release() { |
| 369 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 370 | |
| 371 | queue1_.reset(); |
| 372 | queue2_.reset(); |
| 373 | |
| 374 | return FakeH264Encoder::Release(); |
| 375 | } |
| 376 | |
pbos@webrtc.org | cb5118c | 2013-09-03 09:10:37 +0000 | [diff] [blame] | 377 | } // namespace test |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 378 | } // namespace webrtc |