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 FakeEncoder::FakeEncoder(Clock* clock) | 27 FakeEncoder::FakeEncoder(Clock* clock) |
28 : clock_(clock), | 28 : clock_(clock), |
29 callback_(nullptr), | 29 callback_(nullptr), |
30 configured_input_framerate_(-1), | |
30 max_target_bitrate_kbps_(-1), | 31 max_target_bitrate_kbps_(-1), |
31 last_encode_time_ms_(0) { | 32 pending_keyframe_(true) { |
32 // Generate some arbitrary not-all-zero data | 33 // Generate some arbitrary not-all-zero data |
33 for (size_t i = 0; i < sizeof(encoded_buffer_); ++i) { | 34 for (size_t i = 0; i < sizeof(encoded_buffer_); ++i) { |
34 encoded_buffer_[i] = static_cast<uint8_t>(i); | 35 encoded_buffer_[i] = static_cast<uint8_t>(i); |
35 } | 36 } |
36 } | 37 } |
37 | 38 |
38 void FakeEncoder::SetMaxBitrate(int max_kbps) { | 39 void FakeEncoder::SetMaxBitrate(int max_kbps) { |
39 RTC_DCHECK_GE(max_kbps, -1); // max_kbps == -1 disables it. | 40 RTC_DCHECK_GE(max_kbps, -1); // max_kbps == -1 disables it. |
40 rtc::CritScope cs(&crit_sect_); | 41 rtc::CritScope cs(&crit_sect_); |
41 max_target_bitrate_kbps_ = max_kbps; | 42 max_target_bitrate_kbps_ = max_kbps; |
42 } | 43 } |
43 | 44 |
44 int32_t FakeEncoder::InitEncode(const VideoCodec* config, | 45 int32_t FakeEncoder::InitEncode(const VideoCodec* config, |
45 int32_t number_of_cores, | 46 int32_t number_of_cores, |
46 size_t max_payload_size) { | 47 size_t max_payload_size) { |
47 rtc::CritScope cs(&crit_sect_); | 48 rtc::CritScope cs(&crit_sect_); |
48 config_ = *config; | 49 config_ = *config; |
49 target_bitrate_.SetBitrate(0, 0, config_.startBitrate * 1000); | 50 target_bitrate_.SetBitrate(0, 0, config_.startBitrate * 1000); |
51 configured_input_framerate_ = config_.maxFramerate; | |
52 pending_keyframe_ = true; | |
50 return 0; | 53 return 0; |
51 } | 54 } |
52 | 55 |
53 int32_t FakeEncoder::Encode(const VideoFrame& input_image, | 56 int32_t FakeEncoder::Encode(const VideoFrame& input_image, |
54 const CodecSpecificInfo* codec_specific_info, | 57 const CodecSpecificInfo* codec_specific_info, |
55 const std::vector<FrameType>* frame_types) { | 58 const std::vector<FrameType>* frame_types) { |
56 unsigned char max_framerate; | 59 unsigned char max_framerate; |
57 unsigned char num_simulcast_streams; | 60 unsigned char num_simulcast_streams; |
58 SimulcastStream simulcast_streams[kMaxSimulcastStreams]; | 61 SimulcastStream simulcast_streams[kMaxSimulcastStreams]; |
59 EncodedImageCallback* callback; | 62 EncodedImageCallback* callback; |
60 uint32_t target_bitrate_sum_kbps; | 63 uint32_t target_bitrate_sum_kbps; |
61 int max_target_bitrate_kbps; | 64 int max_target_bitrate_kbps; |
62 int64_t last_encode_time_ms; | |
63 size_t num_encoded_bytes; | 65 size_t num_encoded_bytes; |
66 int framerate; | |
64 VideoCodecMode mode; | 67 VideoCodecMode mode; |
68 bool keyframe; | |
65 { | 69 { |
66 rtc::CritScope cs(&crit_sect_); | 70 rtc::CritScope cs(&crit_sect_); |
67 max_framerate = config_.maxFramerate; | 71 max_framerate = config_.maxFramerate; |
68 num_simulcast_streams = config_.numberOfSimulcastStreams; | 72 num_simulcast_streams = config_.numberOfSimulcastStreams; |
69 for (int i = 0; i < num_simulcast_streams; ++i) { | 73 for (int i = 0; i < num_simulcast_streams; ++i) { |
70 simulcast_streams[i] = config_.simulcastStream[i]; | 74 simulcast_streams[i] = config_.simulcastStream[i]; |
71 } | 75 } |
72 callback = callback_; | 76 callback = callback_; |
73 target_bitrate_sum_kbps = target_bitrate_.get_sum_kbps(); | 77 target_bitrate_sum_kbps = target_bitrate_.get_sum_kbps(); |
74 max_target_bitrate_kbps = max_target_bitrate_kbps_; | 78 max_target_bitrate_kbps = max_target_bitrate_kbps_; |
75 last_encode_time_ms = last_encode_time_ms_; | |
76 num_encoded_bytes = sizeof(encoded_buffer_); | 79 num_encoded_bytes = sizeof(encoded_buffer_); |
77 mode = config_.mode; | 80 mode = config_.mode; |
81 if (configured_input_framerate_ > 0) { | |
82 framerate = configured_input_framerate_; | |
83 } else { | |
84 framerate = max_framerate; | |
85 } | |
86 keyframe = pending_keyframe_; | |
87 pending_keyframe_ = false; | |
78 } | 88 } |
79 | 89 |
80 int64_t time_now_ms = clock_->TimeInMilliseconds(); | 90 for (FrameType frame_type : *frame_types) { |
81 const bool first_encode = (last_encode_time_ms == 0); | 91 if (frame_type == kVideoFrameKey) { |
82 RTC_DCHECK_GT(max_framerate, 0); | 92 keyframe = true; |
83 int64_t time_since_last_encode_ms = 1000 / max_framerate; | 93 break; |
84 if (!first_encode) { | 94 } |
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 } | 95 } |
94 | 96 |
95 size_t bits_available = | 97 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 | 98 |
100 if (bits_available < min_bits) | 99 size_t bitrate = target_bitrate_sum_kbps; |
101 bits_available = min_bits; | 100 bitrate = |
102 size_t max_bits = | 101 std::max(bitrate, static_cast<size_t>(simulcast_streams[0].minBitrate)); |
103 static_cast<size_t>(max_target_bitrate_kbps * time_since_last_encode_ms); | 102 if (max_target_bitrate_kbps > 0) |
104 if (max_bits > 0 && max_bits < bits_available) | 103 bitrate = std::min(bitrate, static_cast<size_t>(max_target_bitrate_kbps)); |
105 bits_available = max_bits; | |
106 | 104 |
107 { | 105 size_t bits_available = target_bitrate_sum_kbps * 1000 / framerate; |
108 rtc::CritScope cs(&crit_sect_); | |
109 last_encode_time_ms_ = time_now_ms; | |
110 } | |
111 | 106 |
112 RTC_DCHECK_GT(num_simulcast_streams, 0); | 107 RTC_DCHECK_GT(num_simulcast_streams, 0); |
113 for (unsigned char i = 0; i < num_simulcast_streams; ++i) { | 108 for (unsigned char i = 0; i < num_simulcast_streams; ++i) { |
114 CodecSpecificInfo specifics; | 109 CodecSpecificInfo specifics; |
115 memset(&specifics, 0, sizeof(specifics)); | 110 memset(&specifics, 0, sizeof(specifics)); |
116 specifics.codecType = kVideoCodecGeneric; | 111 specifics.codecType = kVideoCodecGeneric; |
117 specifics.codecSpecific.generic.simulcast_idx = i; | 112 specifics.codecSpecific.generic.simulcast_idx = i; |
118 size_t min_stream_bits = static_cast<size_t>( | 113 size_t min_stream_bits = static_cast<size_t>( |
119 simulcast_streams[i].minBitrate * time_since_last_encode_ms); | 114 (simulcast_streams[i].minBitrate * 1000) / framerate); |
120 size_t max_stream_bits = static_cast<size_t>( | 115 size_t max_stream_bits = static_cast<size_t>( |
121 simulcast_streams[i].maxBitrate * time_since_last_encode_ms); | 116 (simulcast_streams[i].maxBitrate * 1000) / framerate); |
122 size_t stream_bits = (bits_available > max_stream_bits) ? max_stream_bits : | 117 size_t stream_bits = (bits_available > max_stream_bits) ? max_stream_bits : |
123 bits_available; | 118 bits_available; |
124 size_t stream_bytes = (stream_bits + 7) / 8; | 119 size_t stream_bytes = (stream_bits + 7) / 8; |
125 if (first_encode) { | 120 if (keyframe) { |
126 // The first frame is a key frame and should be larger. | 121 // The first frame is a key frame and should be larger. |
127 // TODO(holmer): The FakeEncoder should store the bits_available between | 122 // Store the overshoot bytes and distribute them over the coming frames, |
128 // encodes so that it can compensate for oversized frames. | 123 // so that we on average meet the bitrate target. |
129 stream_bytes *= 10; | 124 const int kKeyframeSizeFactor = 10; |
125 // Pay 1/2 of a frame debt factor every frame. | |
126 const int kNumDebtPayments = (kKeyframeSizeFactor - 1) * 2; | |
127 debt_bytes_.resize(kNumDebtPayments); | |
128 size_t total_debt_bytes = (kKeyframeSizeFactor - 1) * stream_bytes; | |
129 for (int i = 0; i < kNumDebtPayments; ++i) | |
130 debt_bytes_[i] += total_debt_bytes / kNumDebtPayments; | |
stefan-webrtc
2017/05/23 17:03:20
An option is to just keep track of a total debt an
sprang_webrtc
2017/05/24 09:07:07
Maybe, but then paying of the whole debt will take
holmer
2017/06/02 11:38:05
You could have a total_debt and a payed_debt and t
| |
131 stream_bytes *= kKeyframeSizeFactor; | |
132 } else { | |
133 if (!debt_bytes_.empty()) { | |
134 size_t debt_bytes = std::min(stream_bytes, debt_bytes_[0]); | |
135 debt_bytes_.pop_front(); | |
136 if (debt_bytes >= stream_bytes) { | |
stefan-webrtc
2017/05/23 17:03:20
This can only be true if == right? What is it that
sprang_webrtc
2017/05/24 09:07:07
Oops, the std::min above shouldn't be there.
I'll
| |
137 if (debt_bytes_.empty()) | |
138 debt_bytes_.resize(1); | |
139 debt_bytes_[0] += debt_bytes - stream_bytes + 1; | |
140 debt_bytes = stream_bytes - 1; | |
141 } | |
142 stream_bytes -= debt_bytes; | |
143 } | |
130 } | 144 } |
145 | |
131 if (stream_bytes > num_encoded_bytes) | 146 if (stream_bytes > num_encoded_bytes) |
132 stream_bytes = num_encoded_bytes; | 147 stream_bytes = num_encoded_bytes; |
133 | 148 |
134 // Always encode something on the first frame. | 149 // Always encode something on the first frame. |
135 if (min_stream_bits > bits_available && i > 0) | 150 if (min_stream_bits > bits_available && i > 0) |
136 continue; | 151 continue; |
137 | 152 |
138 std::unique_ptr<uint8_t[]> encoded_buffer(new uint8_t[num_encoded_bytes]); | 153 std::unique_ptr<uint8_t[]> encoded_buffer(new uint8_t[num_encoded_bytes]); |
139 memcpy(encoded_buffer.get(), encoded_buffer_, num_encoded_bytes); | 154 memcpy(encoded_buffer.get(), encoded_buffer_, num_encoded_bytes); |
140 EncodedImage encoded(encoded_buffer.get(), stream_bytes, num_encoded_bytes); | 155 EncodedImage encoded(encoded_buffer.get(), stream_bytes, num_encoded_bytes); |
(...skipping 27 matching lines...) Expand all Loading... | |
168 int32_t FakeEncoder::Release() { return 0; } | 183 int32_t FakeEncoder::Release() { return 0; } |
169 | 184 |
170 int32_t FakeEncoder::SetChannelParameters(uint32_t packet_loss, int64_t rtt) { | 185 int32_t FakeEncoder::SetChannelParameters(uint32_t packet_loss, int64_t rtt) { |
171 return 0; | 186 return 0; |
172 } | 187 } |
173 | 188 |
174 int32_t FakeEncoder::SetRateAllocation(const BitrateAllocation& rate_allocation, | 189 int32_t FakeEncoder::SetRateAllocation(const BitrateAllocation& rate_allocation, |
175 uint32_t framerate) { | 190 uint32_t framerate) { |
176 rtc::CritScope cs(&crit_sect_); | 191 rtc::CritScope cs(&crit_sect_); |
177 target_bitrate_ = rate_allocation; | 192 target_bitrate_ = rate_allocation; |
193 configured_input_framerate_ = framerate; | |
178 return 0; | 194 return 0; |
179 } | 195 } |
180 | 196 |
181 const char* FakeEncoder::kImplementationName = "fake_encoder"; | 197 const char* FakeEncoder::kImplementationName = "fake_encoder"; |
182 const char* FakeEncoder::ImplementationName() const { | 198 const char* FakeEncoder::ImplementationName() const { |
183 return kImplementationName; | 199 return kImplementationName; |
184 } | 200 } |
185 | 201 |
202 int FakeEncoder::GetConfiguredInputFramerate() { | |
203 rtc::CritScope cs(&crit_sect_); | |
204 return configured_input_framerate_; | |
205 } | |
206 | |
186 FakeH264Encoder::FakeH264Encoder(Clock* clock) | 207 FakeH264Encoder::FakeH264Encoder(Clock* clock) |
187 : FakeEncoder(clock), callback_(nullptr), idr_counter_(0) { | 208 : FakeEncoder(clock), callback_(nullptr), idr_counter_(0) { |
188 FakeEncoder::RegisterEncodeCompleteCallback(this); | 209 FakeEncoder::RegisterEncodeCompleteCallback(this); |
189 } | 210 } |
190 | 211 |
191 int32_t FakeH264Encoder::RegisterEncodeCompleteCallback( | 212 int32_t FakeH264Encoder::RegisterEncodeCompleteCallback( |
192 EncodedImageCallback* callback) { | 213 EncodedImageCallback* callback) { |
193 rtc::CritScope cs(&local_crit_sect_); | 214 rtc::CritScope cs(&local_crit_sect_); |
194 callback_ = callback; | 215 callback_ = callback; |
195 return 0; | 216 return 0; |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
355 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); | 376 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
356 | 377 |
357 queue1_.reset(); | 378 queue1_.reset(); |
358 queue2_.reset(); | 379 queue2_.reset(); |
359 | 380 |
360 return FakeH264Encoder::Release(); | 381 return FakeH264Encoder::Release(); |
361 } | 382 } |
362 | 383 |
363 } // namespace test | 384 } // namespace test |
364 } // namespace webrtc | 385 } // namespace webrtc |
OLD | NEW |