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 <algorithm> | 13 #include <algorithm> |
14 | 14 |
15 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
16 #include "webrtc/modules/video_coding/include/video_codec_interface.h" | 16 #include "webrtc/modules/video_coding/include/video_codec_interface.h" |
17 #include "webrtc/system_wrappers/include/sleep.h" | 17 #include "webrtc/system_wrappers/include/sleep.h" |
18 #include "webrtc/test/gtest.h" | 18 #include "webrtc/test/gtest.h" |
19 | 19 |
20 namespace webrtc { | 20 namespace webrtc { |
21 namespace test { | 21 namespace test { |
22 | 22 |
23 FakeEncoder::FakeEncoder(Clock* clock) | 23 FakeEncoder::FakeEncoder(Clock* clock) |
24 : clock_(clock), | 24 : clock_(clock), |
25 callback_(NULL), | 25 callback_(NULL), |
26 target_bitrate_kbps_(0), | |
27 max_target_bitrate_kbps_(-1), | 26 max_target_bitrate_kbps_(-1), |
28 last_encode_time_ms_(0) { | 27 last_encode_time_ms_(0) { |
29 // Generate some arbitrary not-all-zero data | 28 // Generate some arbitrary not-all-zero data |
30 for (size_t i = 0; i < sizeof(encoded_buffer_); ++i) { | 29 for (size_t i = 0; i < sizeof(encoded_buffer_); ++i) { |
31 encoded_buffer_[i] = static_cast<uint8_t>(i); | 30 encoded_buffer_[i] = static_cast<uint8_t>(i); |
32 } | 31 } |
33 } | 32 } |
34 | 33 |
35 FakeEncoder::~FakeEncoder() {} | 34 FakeEncoder::~FakeEncoder() {} |
36 | 35 |
37 void FakeEncoder::SetMaxBitrate(int max_kbps) { | 36 void FakeEncoder::SetMaxBitrate(int max_kbps) { |
38 RTC_DCHECK_GE(max_kbps, -1); // max_kbps == -1 disables it. | 37 RTC_DCHECK_GE(max_kbps, -1); // max_kbps == -1 disables it. |
39 max_target_bitrate_kbps_ = max_kbps; | 38 max_target_bitrate_kbps_ = max_kbps; |
40 } | 39 } |
41 | 40 |
42 int32_t FakeEncoder::InitEncode(const VideoCodec* config, | 41 int32_t FakeEncoder::InitEncode(const VideoCodec* config, |
43 int32_t number_of_cores, | 42 int32_t number_of_cores, |
44 size_t max_payload_size) { | 43 size_t max_payload_size) { |
45 config_ = *config; | 44 config_ = *config; |
46 target_bitrate_kbps_ = config_.startBitrate; | 45 target_bitrate_.SetBitrate(0, 0, config_.startBitrate * 1000); |
47 return 0; | 46 return 0; |
48 } | 47 } |
49 | 48 |
50 int32_t FakeEncoder::Encode(const VideoFrame& input_image, | 49 int32_t FakeEncoder::Encode(const VideoFrame& input_image, |
51 const CodecSpecificInfo* codec_specific_info, | 50 const CodecSpecificInfo* codec_specific_info, |
52 const std::vector<FrameType>* frame_types) { | 51 const std::vector<FrameType>* frame_types) { |
53 RTC_DCHECK_GT(config_.maxFramerate, 0); | 52 RTC_DCHECK_GT(config_.maxFramerate, 0); |
54 int64_t time_since_last_encode_ms = 1000 / config_.maxFramerate; | 53 int64_t time_since_last_encode_ms = 1000 / config_.maxFramerate; |
55 int64_t time_now_ms = clock_->TimeInMilliseconds(); | 54 int64_t time_now_ms = clock_->TimeInMilliseconds(); |
56 const bool first_encode = last_encode_time_ms_ == 0; | 55 const bool first_encode = last_encode_time_ms_ == 0; |
57 if (!first_encode) { | 56 if (!first_encode) { |
58 // For all frames but the first we can estimate the display time by looking | 57 // For all frames but the first we can estimate the display time by looking |
59 // at the display time of the previous frame. | 58 // at the display time of the previous frame. |
60 time_since_last_encode_ms = time_now_ms - last_encode_time_ms_; | 59 time_since_last_encode_ms = time_now_ms - last_encode_time_ms_; |
61 } | 60 } |
62 if (time_since_last_encode_ms > 3 * 1000 / config_.maxFramerate) { | 61 if (time_since_last_encode_ms > 3 * 1000 / config_.maxFramerate) { |
63 // Rudimentary check to make sure we don't widely overshoot bitrate target | 62 // Rudimentary check to make sure we don't widely overshoot bitrate target |
64 // when resuming encoding after a suspension. | 63 // when resuming encoding after a suspension. |
65 time_since_last_encode_ms = 3 * 1000 / config_.maxFramerate; | 64 time_since_last_encode_ms = 3 * 1000 / config_.maxFramerate; |
66 } | 65 } |
67 | 66 |
68 size_t bits_available = | 67 size_t bits_available = static_cast<size_t>(target_bitrate_.get_sum_kbps() * |
69 static_cast<size_t>(target_bitrate_kbps_ * time_since_last_encode_ms); | 68 time_since_last_encode_ms); |
70 size_t min_bits = static_cast<size_t>( | 69 size_t min_bits = static_cast<size_t>( |
71 config_.simulcastStream[0].minBitrate * time_since_last_encode_ms); | 70 config_.simulcastStream[0].minBitrate * time_since_last_encode_ms); |
72 if (bits_available < min_bits) | 71 if (bits_available < min_bits) |
73 bits_available = min_bits; | 72 bits_available = min_bits; |
74 size_t max_bits = | 73 size_t max_bits = |
75 static_cast<size_t>(max_target_bitrate_kbps_ * time_since_last_encode_ms); | 74 static_cast<size_t>(max_target_bitrate_kbps_ * time_since_last_encode_ms); |
76 if (max_bits > 0 && max_bits < bits_available) | 75 if (max_bits > 0 && max_bits < bits_available) |
77 bits_available = max_bits; | 76 bits_available = max_bits; |
78 last_encode_time_ms_ = time_now_ms; | 77 last_encode_time_ms_ = time_now_ms; |
79 | 78 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 callback_ = callback; | 125 callback_ = callback; |
127 return 0; | 126 return 0; |
128 } | 127 } |
129 | 128 |
130 int32_t FakeEncoder::Release() { return 0; } | 129 int32_t FakeEncoder::Release() { return 0; } |
131 | 130 |
132 int32_t FakeEncoder::SetChannelParameters(uint32_t packet_loss, int64_t rtt) { | 131 int32_t FakeEncoder::SetChannelParameters(uint32_t packet_loss, int64_t rtt) { |
133 return 0; | 132 return 0; |
134 } | 133 } |
135 | 134 |
136 int32_t FakeEncoder::SetRates(uint32_t new_target_bitrate, uint32_t framerate) { | 135 int32_t FakeEncoder::SetRateAllocation(const BitrateAllocation& rate_allocation, |
137 target_bitrate_kbps_ = new_target_bitrate; | 136 uint32_t framerate) { |
| 137 target_bitrate_ = rate_allocation; |
138 return 0; | 138 return 0; |
139 } | 139 } |
140 | 140 |
141 const char* FakeEncoder::kImplementationName = "fake_encoder"; | 141 const char* FakeEncoder::kImplementationName = "fake_encoder"; |
142 const char* FakeEncoder::ImplementationName() const { | 142 const char* FakeEncoder::ImplementationName() const { |
143 return kImplementationName; | 143 return kImplementationName; |
144 } | 144 } |
145 | 145 |
146 FakeH264Encoder::FakeH264Encoder(Clock* clock) | 146 FakeH264Encoder::FakeH264Encoder(Clock* clock) |
147 : FakeEncoder(clock), callback_(NULL), idr_counter_(0) { | 147 : FakeEncoder(clock), callback_(NULL), idr_counter_(0) { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 int delay_ms = 0; | 218 int delay_ms = 0; |
219 { | 219 { |
220 rtc::CritScope lock(&lock_); | 220 rtc::CritScope lock(&lock_); |
221 delay_ms = delay_ms_; | 221 delay_ms = delay_ms_; |
222 } | 222 } |
223 SleepMs(delay_ms); | 223 SleepMs(delay_ms); |
224 return FakeEncoder::Encode(input_image, codec_specific_info, frame_types); | 224 return FakeEncoder::Encode(input_image, codec_specific_info, frame_types); |
225 } | 225 } |
226 } // namespace test | 226 } // namespace test |
227 } // namespace webrtc | 227 } // namespace webrtc |
OLD | NEW |