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), |
26 max_target_bitrate_kbps_(-1), | 27 max_target_bitrate_kbps_(-1), |
27 last_encode_time_ms_(0) { | 28 last_encode_time_ms_(0) { |
28 // Generate some arbitrary not-all-zero data | 29 // Generate some arbitrary not-all-zero data |
29 for (size_t i = 0; i < sizeof(encoded_buffer_); ++i) { | 30 for (size_t i = 0; i < sizeof(encoded_buffer_); ++i) { |
30 encoded_buffer_[i] = static_cast<uint8_t>(i); | 31 encoded_buffer_[i] = static_cast<uint8_t>(i); |
31 } | 32 } |
32 } | 33 } |
33 | 34 |
34 FakeEncoder::~FakeEncoder() {} | 35 FakeEncoder::~FakeEncoder() {} |
35 | 36 |
36 void FakeEncoder::SetMaxBitrate(int max_kbps) { | 37 void FakeEncoder::SetMaxBitrate(int max_kbps) { |
37 RTC_DCHECK_GE(max_kbps, -1); // max_kbps == -1 disables it. | 38 RTC_DCHECK_GE(max_kbps, -1); // max_kbps == -1 disables it. |
38 max_target_bitrate_kbps_ = max_kbps; | 39 max_target_bitrate_kbps_ = max_kbps; |
39 } | 40 } |
40 | 41 |
41 int32_t FakeEncoder::InitEncode(const VideoCodec* config, | 42 int32_t FakeEncoder::InitEncode(const VideoCodec* config, |
42 int32_t number_of_cores, | 43 int32_t number_of_cores, |
43 size_t max_payload_size) { | 44 size_t max_payload_size) { |
44 config_ = *config; | 45 config_ = *config; |
45 target_bitrate_.SetBitrate(0, 0, config_.startBitrate * 1000); | 46 target_bitrate_kbps_ = config_.startBitrate; |
46 return 0; | 47 return 0; |
47 } | 48 } |
48 | 49 |
49 int32_t FakeEncoder::Encode(const VideoFrame& input_image, | 50 int32_t FakeEncoder::Encode(const VideoFrame& input_image, |
50 const CodecSpecificInfo* codec_specific_info, | 51 const CodecSpecificInfo* codec_specific_info, |
51 const std::vector<FrameType>* frame_types) { | 52 const std::vector<FrameType>* frame_types) { |
52 RTC_DCHECK_GT(config_.maxFramerate, 0); | 53 RTC_DCHECK_GT(config_.maxFramerate, 0); |
53 int64_t time_since_last_encode_ms = 1000 / config_.maxFramerate; | 54 int64_t time_since_last_encode_ms = 1000 / config_.maxFramerate; |
54 int64_t time_now_ms = clock_->TimeInMilliseconds(); | 55 int64_t time_now_ms = clock_->TimeInMilliseconds(); |
55 const bool first_encode = last_encode_time_ms_ == 0; | 56 const bool first_encode = last_encode_time_ms_ == 0; |
56 if (!first_encode) { | 57 if (!first_encode) { |
57 // For all frames but the first we can estimate the display time by looking | 58 // For all frames but the first we can estimate the display time by looking |
58 // at the display time of the previous frame. | 59 // at the display time of the previous frame. |
59 time_since_last_encode_ms = time_now_ms - last_encode_time_ms_; | 60 time_since_last_encode_ms = time_now_ms - last_encode_time_ms_; |
60 } | 61 } |
61 if (time_since_last_encode_ms > 3 * 1000 / config_.maxFramerate) { | 62 if (time_since_last_encode_ms > 3 * 1000 / config_.maxFramerate) { |
62 // Rudimentary check to make sure we don't widely overshoot bitrate target | 63 // Rudimentary check to make sure we don't widely overshoot bitrate target |
63 // when resuming encoding after a suspension. | 64 // when resuming encoding after a suspension. |
64 time_since_last_encode_ms = 3 * 1000 / config_.maxFramerate; | 65 time_since_last_encode_ms = 3 * 1000 / config_.maxFramerate; |
65 } | 66 } |
66 | 67 |
67 size_t bits_available = static_cast<size_t>(target_bitrate_.get_sum_kbps() * | 68 size_t bits_available = |
68 time_since_last_encode_ms); | 69 static_cast<size_t>(target_bitrate_kbps_ * time_since_last_encode_ms); |
69 size_t min_bits = static_cast<size_t>( | 70 size_t min_bits = static_cast<size_t>( |
70 config_.simulcastStream[0].minBitrate * time_since_last_encode_ms); | 71 config_.simulcastStream[0].minBitrate * time_since_last_encode_ms); |
71 if (bits_available < min_bits) | 72 if (bits_available < min_bits) |
72 bits_available = min_bits; | 73 bits_available = min_bits; |
73 size_t max_bits = | 74 size_t max_bits = |
74 static_cast<size_t>(max_target_bitrate_kbps_ * time_since_last_encode_ms); | 75 static_cast<size_t>(max_target_bitrate_kbps_ * time_since_last_encode_ms); |
75 if (max_bits > 0 && max_bits < bits_available) | 76 if (max_bits > 0 && max_bits < bits_available) |
76 bits_available = max_bits; | 77 bits_available = max_bits; |
77 last_encode_time_ms_ = time_now_ms; | 78 last_encode_time_ms_ = time_now_ms; |
78 | 79 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 callback_ = callback; | 126 callback_ = callback; |
126 return 0; | 127 return 0; |
127 } | 128 } |
128 | 129 |
129 int32_t FakeEncoder::Release() { return 0; } | 130 int32_t FakeEncoder::Release() { return 0; } |
130 | 131 |
131 int32_t FakeEncoder::SetChannelParameters(uint32_t packet_loss, int64_t rtt) { | 132 int32_t FakeEncoder::SetChannelParameters(uint32_t packet_loss, int64_t rtt) { |
132 return 0; | 133 return 0; |
133 } | 134 } |
134 | 135 |
135 int32_t FakeEncoder::SetRateAllocation(const BitrateAllocation& rate_allocation, | 136 int32_t FakeEncoder::SetRates(uint32_t new_target_bitrate, uint32_t framerate) { |
136 uint32_t framerate) { | 137 target_bitrate_kbps_ = new_target_bitrate; |
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 |