| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/test/fake_encoder.h" | 11 #include "webrtc/test/fake_encoder.h" |
| 12 | 12 |
| 13 #include <string.h> | 13 #include <string.h> |
| 14 | 14 |
| 15 #include <algorithm> | 15 #include <algorithm> |
| 16 #include <memory> | 16 #include <memory> |
| 17 | 17 |
| 18 #include "webrtc/base/checks.h" | 18 #include "webrtc/base/checks.h" |
| 19 #include "webrtc/common_types.h" | 19 #include "webrtc/common_types.h" |
| 20 #include "webrtc/modules/video_coding/include/video_codec_interface.h" | 20 #include "webrtc/modules/video_coding/include/video_codec_interface.h" |
| 21 #include "webrtc/system_wrappers/include/sleep.h" | 21 #include "webrtc/system_wrappers/include/sleep.h" |
| 22 #include "webrtc/test/gtest.h" | 22 #include "webrtc/test/gtest.h" |
| 23 | 23 |
| 24 namespace webrtc { | 24 namespace webrtc { |
| 25 namespace test { | 25 namespace test { |
| 26 | 26 |
| 27 const int kKeyframeSizeFactor = 10; |
| 28 |
| 27 FakeEncoder::FakeEncoder(Clock* clock) | 29 FakeEncoder::FakeEncoder(Clock* clock) |
| 28 : clock_(clock), | 30 : clock_(clock), |
| 29 callback_(nullptr), | 31 callback_(nullptr), |
| 32 configured_input_framerate_(-1), |
| 30 max_target_bitrate_kbps_(-1), | 33 max_target_bitrate_kbps_(-1), |
| 31 last_encode_time_ms_(0) { | 34 pending_keyframe_(true), |
| 35 debt_bytes_(0) { |
| 32 // Generate some arbitrary not-all-zero data | 36 // Generate some arbitrary not-all-zero data |
| 33 for (size_t i = 0; i < sizeof(encoded_buffer_); ++i) { | 37 for (size_t i = 0; i < sizeof(encoded_buffer_); ++i) { |
| 34 encoded_buffer_[i] = static_cast<uint8_t>(i); | 38 encoded_buffer_[i] = static_cast<uint8_t>(i); |
| 35 } | 39 } |
| 36 } | 40 } |
| 37 | 41 |
| 38 void FakeEncoder::SetMaxBitrate(int max_kbps) { | 42 void FakeEncoder::SetMaxBitrate(int max_kbps) { |
| 39 RTC_DCHECK_GE(max_kbps, -1); // max_kbps == -1 disables it. | 43 RTC_DCHECK_GE(max_kbps, -1); // max_kbps == -1 disables it. |
| 40 rtc::CritScope cs(&crit_sect_); | 44 rtc::CritScope cs(&crit_sect_); |
| 41 max_target_bitrate_kbps_ = max_kbps; | 45 max_target_bitrate_kbps_ = max_kbps; |
| 42 } | 46 } |
| 43 | 47 |
| 44 int32_t FakeEncoder::InitEncode(const VideoCodec* config, | 48 int32_t FakeEncoder::InitEncode(const VideoCodec* config, |
| 45 int32_t number_of_cores, | 49 int32_t number_of_cores, |
| 46 size_t max_payload_size) { | 50 size_t max_payload_size) { |
| 47 rtc::CritScope cs(&crit_sect_); | 51 rtc::CritScope cs(&crit_sect_); |
| 48 config_ = *config; | 52 config_ = *config; |
| 49 target_bitrate_.SetBitrate(0, 0, config_.startBitrate * 1000); | 53 target_bitrate_.SetBitrate(0, 0, config_.startBitrate * 1000); |
| 54 configured_input_framerate_ = config_.maxFramerate; |
| 55 pending_keyframe_ = true; |
| 50 return 0; | 56 return 0; |
| 51 } | 57 } |
| 52 | 58 |
| 53 int32_t FakeEncoder::Encode(const VideoFrame& input_image, | 59 int32_t FakeEncoder::Encode(const VideoFrame& input_image, |
| 54 const CodecSpecificInfo* codec_specific_info, | 60 const CodecSpecificInfo* codec_specific_info, |
| 55 const std::vector<FrameType>* frame_types) { | 61 const std::vector<FrameType>* frame_types) { |
| 56 unsigned char max_framerate; | 62 unsigned char max_framerate; |
| 57 unsigned char num_simulcast_streams; | 63 unsigned char num_simulcast_streams; |
| 58 SimulcastStream simulcast_streams[kMaxSimulcastStreams]; | 64 SimulcastStream simulcast_streams[kMaxSimulcastStreams]; |
| 59 EncodedImageCallback* callback; | 65 EncodedImageCallback* callback; |
| 60 uint32_t target_bitrate_sum_kbps; | 66 uint32_t target_bitrate_sum_kbps; |
| 61 int max_target_bitrate_kbps; | 67 int max_target_bitrate_kbps; |
| 62 int64_t last_encode_time_ms; | |
| 63 size_t num_encoded_bytes; | 68 size_t num_encoded_bytes; |
| 69 int framerate; |
| 64 VideoCodecMode mode; | 70 VideoCodecMode mode; |
| 71 bool keyframe; |
| 65 { | 72 { |
| 66 rtc::CritScope cs(&crit_sect_); | 73 rtc::CritScope cs(&crit_sect_); |
| 67 max_framerate = config_.maxFramerate; | 74 max_framerate = config_.maxFramerate; |
| 68 num_simulcast_streams = config_.numberOfSimulcastStreams; | 75 num_simulcast_streams = config_.numberOfSimulcastStreams; |
| 69 for (int i = 0; i < num_simulcast_streams; ++i) { | 76 for (int i = 0; i < num_simulcast_streams; ++i) { |
| 70 simulcast_streams[i] = config_.simulcastStream[i]; | 77 simulcast_streams[i] = config_.simulcastStream[i]; |
| 71 } | 78 } |
| 72 callback = callback_; | 79 callback = callback_; |
| 73 target_bitrate_sum_kbps = target_bitrate_.get_sum_kbps(); | 80 target_bitrate_sum_kbps = target_bitrate_.get_sum_kbps(); |
| 74 max_target_bitrate_kbps = max_target_bitrate_kbps_; | 81 max_target_bitrate_kbps = max_target_bitrate_kbps_; |
| 75 last_encode_time_ms = last_encode_time_ms_; | |
| 76 num_encoded_bytes = sizeof(encoded_buffer_); | 82 num_encoded_bytes = sizeof(encoded_buffer_); |
| 77 mode = config_.mode; | 83 mode = config_.mode; |
| 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; |
| 78 } | 91 } |
| 79 | 92 |
| 80 int64_t time_now_ms = clock_->TimeInMilliseconds(); | 93 for (FrameType frame_type : *frame_types) { |
| 81 const bool first_encode = (last_encode_time_ms == 0); | 94 if (frame_type == kVideoFrameKey) { |
| 82 RTC_DCHECK_GT(max_framerate, 0); | 95 keyframe = true; |
| 83 int64_t time_since_last_encode_ms = 1000 / max_framerate; | 96 break; |
| 84 if (!first_encode) { | 97 } |
| 85 // For all frames but the first we can estimate the display time by looking | |
| 86 // at the display time of the previous frame. | |
| 87 time_since_last_encode_ms = time_now_ms - last_encode_time_ms; | |
| 88 } | |
| 89 if (time_since_last_encode_ms > 3 * 1000 / max_framerate) { | |
| 90 // Rudimentary check to make sure we don't widely overshoot bitrate target | |
| 91 // when resuming encoding after a suspension. | |
| 92 time_since_last_encode_ms = 3 * 1000 / max_framerate; | |
| 93 } | 98 } |
| 94 | 99 |
| 95 size_t bits_available = | 100 RTC_DCHECK_GT(max_framerate, 0); |
| 96 static_cast<size_t>(target_bitrate_sum_kbps * time_since_last_encode_ms); | |
| 97 size_t min_bits = static_cast<size_t>(simulcast_streams[0].minBitrate * | |
| 98 time_since_last_encode_ms); | |
| 99 | 101 |
| 100 if (bits_available < min_bits) | 102 size_t bitrate = |
| 101 bits_available = min_bits; | 103 std::max(target_bitrate_sum_kbps, simulcast_streams[0].minBitrate); |
| 102 size_t max_bits = | 104 if (max_target_bitrate_kbps > 0) |
| 103 static_cast<size_t>(max_target_bitrate_kbps * time_since_last_encode_ms); | 105 bitrate = std::min(bitrate, static_cast<size_t>(max_target_bitrate_kbps)); |
| 104 if (max_bits > 0 && max_bits < bits_available) | |
| 105 bits_available = max_bits; | |
| 106 | 106 |
| 107 { | 107 size_t bits_available = bitrate * 1000 / framerate; |
| 108 rtc::CritScope cs(&crit_sect_); | |
| 109 last_encode_time_ms_ = time_now_ms; | |
| 110 } | |
| 111 | 108 |
| 112 RTC_DCHECK_GT(num_simulcast_streams, 0); | 109 RTC_DCHECK_GT(num_simulcast_streams, 0); |
| 113 for (unsigned char i = 0; i < num_simulcast_streams; ++i) { | 110 for (unsigned char i = 0; i < num_simulcast_streams; ++i) { |
| 114 CodecSpecificInfo specifics; | 111 CodecSpecificInfo specifics; |
| 115 memset(&specifics, 0, sizeof(specifics)); | 112 memset(&specifics, 0, sizeof(specifics)); |
| 116 specifics.codecType = kVideoCodecGeneric; | 113 specifics.codecType = kVideoCodecGeneric; |
| 117 specifics.codecSpecific.generic.simulcast_idx = i; | 114 specifics.codecSpecific.generic.simulcast_idx = i; |
| 118 size_t min_stream_bits = static_cast<size_t>( | 115 size_t min_stream_bits = static_cast<size_t>( |
| 119 simulcast_streams[i].minBitrate * time_since_last_encode_ms); | 116 (simulcast_streams[i].minBitrate * 1000) / framerate); |
| 120 size_t max_stream_bits = static_cast<size_t>( | 117 size_t max_stream_bits = static_cast<size_t>( |
| 121 simulcast_streams[i].maxBitrate * time_since_last_encode_ms); | 118 (simulcast_streams[i].maxBitrate * 1000) / framerate); |
| 122 size_t stream_bits = (bits_available > max_stream_bits) ? max_stream_bits : | 119 size_t stream_bits = (bits_available > max_stream_bits) ? max_stream_bits : |
| 123 bits_available; | 120 bits_available; |
| 124 size_t stream_bytes = (stream_bits + 7) / 8; | 121 size_t stream_bytes = (stream_bits + 7) / 8; |
| 125 if (first_encode) { | 122 if (keyframe) { |
| 126 // The first frame is a key frame and should be larger. | 123 // The first frame is a key frame and should be larger. |
| 127 // TODO(holmer): The FakeEncoder should store the bits_available between | 124 // Store the overshoot bytes and distribute them over the coming frames, |
| 128 // encodes so that it can compensate for oversized frames. | 125 // so that we on average meet the bitrate target. |
| 129 stream_bytes *= 10; | 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 } |
| 130 } | 135 } |
| 136 |
| 131 if (stream_bytes > num_encoded_bytes) | 137 if (stream_bytes > num_encoded_bytes) |
| 132 stream_bytes = num_encoded_bytes; | 138 stream_bytes = num_encoded_bytes; |
| 133 | 139 |
| 134 // Always encode something on the first frame. | 140 // Always encode something on the first frame. |
| 135 if (min_stream_bits > bits_available && i > 0) | 141 if (min_stream_bits > bits_available && i > 0) |
| 136 continue; | 142 continue; |
| 137 | 143 |
| 138 std::unique_ptr<uint8_t[]> encoded_buffer(new uint8_t[num_encoded_bytes]); | 144 std::unique_ptr<uint8_t[]> encoded_buffer(new uint8_t[num_encoded_bytes]); |
| 139 memcpy(encoded_buffer.get(), encoded_buffer_, num_encoded_bytes); | 145 memcpy(encoded_buffer.get(), encoded_buffer_, num_encoded_bytes); |
| 140 EncodedImage encoded(encoded_buffer.get(), stream_bytes, num_encoded_bytes); | 146 EncodedImage encoded(encoded_buffer.get(), stream_bytes, num_encoded_bytes); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 169 int32_t FakeEncoder::Release() { return 0; } | 175 int32_t FakeEncoder::Release() { return 0; } |
| 170 | 176 |
| 171 int32_t FakeEncoder::SetChannelParameters(uint32_t packet_loss, int64_t rtt) { | 177 int32_t FakeEncoder::SetChannelParameters(uint32_t packet_loss, int64_t rtt) { |
| 172 return 0; | 178 return 0; |
| 173 } | 179 } |
| 174 | 180 |
| 175 int32_t FakeEncoder::SetRateAllocation(const BitrateAllocation& rate_allocation, | 181 int32_t FakeEncoder::SetRateAllocation(const BitrateAllocation& rate_allocation, |
| 176 uint32_t framerate) { | 182 uint32_t framerate) { |
| 177 rtc::CritScope cs(&crit_sect_); | 183 rtc::CritScope cs(&crit_sect_); |
| 178 target_bitrate_ = rate_allocation; | 184 target_bitrate_ = rate_allocation; |
| 185 configured_input_framerate_ = framerate; |
| 179 return 0; | 186 return 0; |
| 180 } | 187 } |
| 181 | 188 |
| 182 const char* FakeEncoder::kImplementationName = "fake_encoder"; | 189 const char* FakeEncoder::kImplementationName = "fake_encoder"; |
| 183 const char* FakeEncoder::ImplementationName() const { | 190 const char* FakeEncoder::ImplementationName() const { |
| 184 return kImplementationName; | 191 return kImplementationName; |
| 185 } | 192 } |
| 186 | 193 |
| 194 int FakeEncoder::GetConfiguredInputFramerate() const { |
| 195 rtc::CritScope cs(&crit_sect_); |
| 196 return configured_input_framerate_; |
| 197 } |
| 198 |
| 187 FakeH264Encoder::FakeH264Encoder(Clock* clock) | 199 FakeH264Encoder::FakeH264Encoder(Clock* clock) |
| 188 : FakeEncoder(clock), callback_(nullptr), idr_counter_(0) { | 200 : FakeEncoder(clock), callback_(nullptr), idr_counter_(0) { |
| 189 FakeEncoder::RegisterEncodeCompleteCallback(this); | 201 FakeEncoder::RegisterEncodeCompleteCallback(this); |
| 190 } | 202 } |
| 191 | 203 |
| 192 int32_t FakeH264Encoder::RegisterEncodeCompleteCallback( | 204 int32_t FakeH264Encoder::RegisterEncodeCompleteCallback( |
| 193 EncodedImageCallback* callback) { | 205 EncodedImageCallback* callback) { |
| 194 rtc::CritScope cs(&local_crit_sect_); | 206 rtc::CritScope cs(&local_crit_sect_); |
| 195 callback_ = callback; | 207 callback_ = callback; |
| 196 return 0; | 208 return 0; |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); | 368 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 357 | 369 |
| 358 queue1_.reset(); | 370 queue1_.reset(); |
| 359 queue2_.reset(); | 371 queue2_.reset(); |
| 360 | 372 |
| 361 return FakeH264Encoder::Release(); | 373 return FakeH264Encoder::Release(); |
| 362 } | 374 } |
| 363 | 375 |
| 364 } // namespace test | 376 } // namespace test |
| 365 } // namespace webrtc | 377 } // namespace webrtc |
| OLD | NEW |