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 "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
| 15 #include "webrtc/base/checks.h" |
15 #include "webrtc/modules/video_coding/include/video_codec_interface.h" | 16 #include "webrtc/modules/video_coding/include/video_codec_interface.h" |
16 #include "webrtc/system_wrappers/include/sleep.h" | 17 #include "webrtc/system_wrappers/include/sleep.h" |
17 | 18 |
18 namespace webrtc { | 19 namespace webrtc { |
19 namespace test { | 20 namespace test { |
20 | 21 |
21 FakeEncoder::FakeEncoder(Clock* clock) | 22 FakeEncoder::FakeEncoder(Clock* clock) |
22 : clock_(clock), | 23 : clock_(clock), |
23 callback_(NULL), | 24 callback_(NULL), |
24 target_bitrate_kbps_(0), | 25 target_bitrate_kbps_(0), |
25 max_target_bitrate_kbps_(-1), | 26 max_target_bitrate_kbps_(-1), |
26 last_encode_time_ms_(0) { | 27 last_encode_time_ms_(0) { |
27 // Generate some arbitrary not-all-zero data | 28 // Generate some arbitrary not-all-zero data |
28 for (size_t i = 0; i < sizeof(encoded_buffer_); ++i) { | 29 for (size_t i = 0; i < sizeof(encoded_buffer_); ++i) { |
29 encoded_buffer_[i] = static_cast<uint8_t>(i); | 30 encoded_buffer_[i] = static_cast<uint8_t>(i); |
30 } | 31 } |
31 } | 32 } |
32 | 33 |
33 FakeEncoder::~FakeEncoder() {} | 34 FakeEncoder::~FakeEncoder() {} |
34 | 35 |
35 void FakeEncoder::SetMaxBitrate(int max_kbps) { | 36 void FakeEncoder::SetMaxBitrate(int max_kbps) { |
36 assert(max_kbps >= -1); // max_kbps == -1 disables it. | 37 RTC_DCHECK_GE(max_kbps, -1); // max_kbps == -1 disables it. |
37 max_target_bitrate_kbps_ = max_kbps; | 38 max_target_bitrate_kbps_ = max_kbps; |
38 } | 39 } |
39 | 40 |
40 int32_t FakeEncoder::InitEncode(const VideoCodec* config, | 41 int32_t FakeEncoder::InitEncode(const VideoCodec* config, |
41 int32_t number_of_cores, | 42 int32_t number_of_cores, |
42 size_t max_payload_size) { | 43 size_t max_payload_size) { |
43 config_ = *config; | 44 config_ = *config; |
44 target_bitrate_kbps_ = config_.startBitrate; | 45 target_bitrate_kbps_ = config_.startBitrate; |
45 return 0; | 46 return 0; |
46 } | 47 } |
47 | 48 |
48 int32_t FakeEncoder::Encode(const VideoFrame& input_image, | 49 int32_t FakeEncoder::Encode(const VideoFrame& input_image, |
49 const CodecSpecificInfo* codec_specific_info, | 50 const CodecSpecificInfo* codec_specific_info, |
50 const std::vector<FrameType>* frame_types) { | 51 const std::vector<FrameType>* frame_types) { |
51 assert(config_.maxFramerate > 0); | 52 RTC_DCHECK_GT(config_.maxFramerate, 0); |
52 int64_t time_since_last_encode_ms = 1000 / config_.maxFramerate; | 53 int64_t time_since_last_encode_ms = 1000 / config_.maxFramerate; |
53 int64_t time_now_ms = clock_->TimeInMilliseconds(); | 54 int64_t time_now_ms = clock_->TimeInMilliseconds(); |
54 const bool first_encode = last_encode_time_ms_ == 0; | 55 const bool first_encode = last_encode_time_ms_ == 0; |
55 if (!first_encode) { | 56 if (!first_encode) { |
56 // 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 |
57 // at the display time of the previous frame. | 58 // at the display time of the previous frame. |
58 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_; |
59 } | 60 } |
60 if (time_since_last_encode_ms > 3 * 1000 / config_.maxFramerate) { | 61 if (time_since_last_encode_ms > 3 * 1000 / config_.maxFramerate) { |
61 // 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 |
62 // when resuming encoding after a suspension. | 63 // when resuming encoding after a suspension. |
63 time_since_last_encode_ms = 3 * 1000 / config_.maxFramerate; | 64 time_since_last_encode_ms = 3 * 1000 / config_.maxFramerate; |
64 } | 65 } |
65 | 66 |
66 size_t bits_available = | 67 size_t bits_available = |
67 static_cast<size_t>(target_bitrate_kbps_ * time_since_last_encode_ms); | 68 static_cast<size_t>(target_bitrate_kbps_ * time_since_last_encode_ms); |
68 size_t min_bits = static_cast<size_t>( | 69 size_t min_bits = static_cast<size_t>( |
69 config_.simulcastStream[0].minBitrate * time_since_last_encode_ms); | 70 config_.simulcastStream[0].minBitrate * time_since_last_encode_ms); |
70 if (bits_available < min_bits) | 71 if (bits_available < min_bits) |
71 bits_available = min_bits; | 72 bits_available = min_bits; |
72 size_t max_bits = | 73 size_t max_bits = |
73 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); |
74 if (max_bits > 0 && max_bits < bits_available) | 75 if (max_bits > 0 && max_bits < bits_available) |
75 bits_available = max_bits; | 76 bits_available = max_bits; |
76 last_encode_time_ms_ = time_now_ms; | 77 last_encode_time_ms_ = time_now_ms; |
77 | 78 |
78 assert(config_.numberOfSimulcastStreams > 0); | 79 RTC_DCHECK_GT(config_.numberOfSimulcastStreams, 0); |
79 for (unsigned char i = 0; i < config_.numberOfSimulcastStreams; ++i) { | 80 for (unsigned char i = 0; i < config_.numberOfSimulcastStreams; ++i) { |
80 CodecSpecificInfo specifics; | 81 CodecSpecificInfo specifics; |
81 memset(&specifics, 0, sizeof(specifics)); | 82 memset(&specifics, 0, sizeof(specifics)); |
82 specifics.codecType = kVideoCodecGeneric; | 83 specifics.codecType = kVideoCodecGeneric; |
83 specifics.codecSpecific.generic.simulcast_idx = i; | 84 specifics.codecSpecific.generic.simulcast_idx = i; |
84 size_t min_stream_bits = static_cast<size_t>( | 85 size_t min_stream_bits = static_cast<size_t>( |
85 config_.simulcastStream[i].minBitrate * time_since_last_encode_ms); | 86 config_.simulcastStream[i].minBitrate * time_since_last_encode_ms); |
86 size_t max_stream_bits = static_cast<size_t>( | 87 size_t max_stream_bits = static_cast<size_t>( |
87 config_.simulcastStream[i].maxBitrate * time_since_last_encode_ms); | 88 config_.simulcastStream[i].maxBitrate * time_since_last_encode_ms); |
88 size_t stream_bits = (bits_available > max_stream_bits) ? max_stream_bits : | 89 size_t stream_bits = (bits_available > max_stream_bits) ? max_stream_bits : |
89 bits_available; | 90 bits_available; |
90 size_t stream_bytes = (stream_bits + 7) / 8; | 91 size_t stream_bytes = (stream_bits + 7) / 8; |
91 if (first_encode) { | 92 if (first_encode) { |
92 // The first frame is a key frame and should be larger. | 93 // The first frame is a key frame and should be larger. |
93 // TODO(holmer): The FakeEncoder should store the bits_available between | 94 // TODO(holmer): The FakeEncoder should store the bits_available between |
94 // encodes so that it can compensate for oversized frames. | 95 // encodes so that it can compensate for oversized frames. |
95 stream_bytes *= 10; | 96 stream_bytes *= 10; |
96 } | 97 } |
97 if (stream_bytes > sizeof(encoded_buffer_)) | 98 if (stream_bytes > sizeof(encoded_buffer_)) |
98 stream_bytes = sizeof(encoded_buffer_); | 99 stream_bytes = sizeof(encoded_buffer_); |
99 | 100 |
| 101 // Always encode something on the first frame. |
| 102 if (min_stream_bits > bits_available && i > 0) |
| 103 continue; |
100 EncodedImage encoded( | 104 EncodedImage encoded( |
101 encoded_buffer_, stream_bytes, sizeof(encoded_buffer_)); | 105 encoded_buffer_, stream_bytes, sizeof(encoded_buffer_)); |
102 encoded._timeStamp = input_image.timestamp(); | 106 encoded._timeStamp = input_image.timestamp(); |
103 encoded.capture_time_ms_ = input_image.render_time_ms(); | 107 encoded.capture_time_ms_ = input_image.render_time_ms(); |
104 encoded._frameType = (*frame_types)[i]; | 108 encoded._frameType = (*frame_types)[i]; |
105 encoded._encodedWidth = config_.simulcastStream[i].width; | 109 encoded._encodedWidth = config_.simulcastStream[i].width; |
106 encoded._encodedHeight = config_.simulcastStream[i].height; | 110 encoded._encodedHeight = config_.simulcastStream[i].height; |
107 // Always encode something on the first frame. | 111 RTC_DCHECK(callback_ != NULL); |
108 if (min_stream_bits > bits_available && i > 0) | |
109 continue; | |
110 assert(callback_ != NULL); | |
111 specifics.codec_name = ImplementationName(); | 112 specifics.codec_name = ImplementationName(); |
112 if (callback_->Encoded(encoded, &specifics, NULL) != 0) | 113 if (callback_->Encoded(encoded, &specifics, NULL) != 0) |
113 return -1; | 114 return -1; |
114 bits_available -= std::min(encoded._length * 8, bits_available); | 115 bits_available -= std::min(encoded._length * 8, bits_available); |
115 } | 116 } |
116 return 0; | 117 return 0; |
117 } | 118 } |
118 | 119 |
119 int32_t FakeEncoder::RegisterEncodeCompleteCallback( | 120 int32_t FakeEncoder::RegisterEncodeCompleteCallback( |
120 EncodedImageCallback* callback) { | 121 EncodedImageCallback* callback) { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 delay_ms_(delay_ms) {} | 201 delay_ms_(delay_ms) {} |
201 | 202 |
202 int32_t DelayedEncoder::Encode(const VideoFrame& input_image, | 203 int32_t DelayedEncoder::Encode(const VideoFrame& input_image, |
203 const CodecSpecificInfo* codec_specific_info, | 204 const CodecSpecificInfo* codec_specific_info, |
204 const std::vector<FrameType>* frame_types) { | 205 const std::vector<FrameType>* frame_types) { |
205 SleepMs(delay_ms_); | 206 SleepMs(delay_ms_); |
206 return FakeEncoder::Encode(input_image, codec_specific_info, frame_types); | 207 return FakeEncoder::Encode(input_image, codec_specific_info, frame_types); |
207 } | 208 } |
208 } // namespace test | 209 } // namespace test |
209 } // namespace webrtc | 210 } // namespace webrtc |
OLD | NEW |