Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: webrtc/test/fake_encoder.cc

Issue 2248713003: Revert of Add task queue to Call. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@move_getpadding
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/test/call_test.cc ('k') | webrtc/video/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
16 #include "webrtc/modules/video_coding/include/video_codec_interface.h" 15 #include "webrtc/modules/video_coding/include/video_codec_interface.h"
17 #include "webrtc/system_wrappers/include/sleep.h" 16 #include "webrtc/system_wrappers/include/sleep.h"
18 17
19 namespace webrtc { 18 namespace webrtc {
20 namespace test { 19 namespace test {
21 20
22 FakeEncoder::FakeEncoder(Clock* clock) 21 FakeEncoder::FakeEncoder(Clock* clock)
23 : clock_(clock), 22 : clock_(clock),
24 callback_(NULL), 23 callback_(NULL),
25 target_bitrate_kbps_(0), 24 target_bitrate_kbps_(0),
26 max_target_bitrate_kbps_(-1), 25 max_target_bitrate_kbps_(-1),
27 last_encode_time_ms_(0) { 26 last_encode_time_ms_(0) {
28 // Generate some arbitrary not-all-zero data 27 // Generate some arbitrary not-all-zero data
29 for (size_t i = 0; i < sizeof(encoded_buffer_); ++i) { 28 for (size_t i = 0; i < sizeof(encoded_buffer_); ++i) {
30 encoded_buffer_[i] = static_cast<uint8_t>(i); 29 encoded_buffer_[i] = static_cast<uint8_t>(i);
31 } 30 }
32 } 31 }
33 32
34 FakeEncoder::~FakeEncoder() {} 33 FakeEncoder::~FakeEncoder() {}
35 34
36 void FakeEncoder::SetMaxBitrate(int max_kbps) { 35 void FakeEncoder::SetMaxBitrate(int max_kbps) {
37 RTC_DCHECK_GE(max_kbps, -1); // max_kbps == -1 disables it. 36 assert(max_kbps >= -1); // max_kbps == -1 disables it.
38 max_target_bitrate_kbps_ = max_kbps; 37 max_target_bitrate_kbps_ = max_kbps;
39 } 38 }
40 39
41 int32_t FakeEncoder::InitEncode(const VideoCodec* config, 40 int32_t FakeEncoder::InitEncode(const VideoCodec* config,
42 int32_t number_of_cores, 41 int32_t number_of_cores,
43 size_t max_payload_size) { 42 size_t max_payload_size) {
44 config_ = *config; 43 config_ = *config;
45 target_bitrate_kbps_ = config_.startBitrate; 44 target_bitrate_kbps_ = config_.startBitrate;
46 return 0; 45 return 0;
47 } 46 }
48 47
49 int32_t FakeEncoder::Encode(const VideoFrame& input_image, 48 int32_t FakeEncoder::Encode(const VideoFrame& input_image,
50 const CodecSpecificInfo* codec_specific_info, 49 const CodecSpecificInfo* codec_specific_info,
51 const std::vector<FrameType>* frame_types) { 50 const std::vector<FrameType>* frame_types) {
52 RTC_DCHECK_GT(config_.maxFramerate, 0); 51 assert(config_.maxFramerate > 0);
53 int64_t time_since_last_encode_ms = 1000 / config_.maxFramerate; 52 int64_t time_since_last_encode_ms = 1000 / config_.maxFramerate;
54 int64_t time_now_ms = clock_->TimeInMilliseconds(); 53 int64_t time_now_ms = clock_->TimeInMilliseconds();
55 const bool first_encode = last_encode_time_ms_ == 0; 54 const bool first_encode = last_encode_time_ms_ == 0;
56 if (!first_encode) { 55 if (!first_encode) {
57 // For all frames but the first we can estimate the display time by looking 56 // For all frames but the first we can estimate the display time by looking
58 // at the display time of the previous frame. 57 // at the display time of the previous frame.
59 time_since_last_encode_ms = time_now_ms - last_encode_time_ms_; 58 time_since_last_encode_ms = time_now_ms - last_encode_time_ms_;
60 } 59 }
61 if (time_since_last_encode_ms > 3 * 1000 / config_.maxFramerate) { 60 if (time_since_last_encode_ms > 3 * 1000 / config_.maxFramerate) {
62 // Rudimentary check to make sure we don't widely overshoot bitrate target 61 // Rudimentary check to make sure we don't widely overshoot bitrate target
63 // when resuming encoding after a suspension. 62 // when resuming encoding after a suspension.
64 time_since_last_encode_ms = 3 * 1000 / config_.maxFramerate; 63 time_since_last_encode_ms = 3 * 1000 / config_.maxFramerate;
65 } 64 }
66 65
67 size_t bits_available = 66 size_t bits_available =
68 static_cast<size_t>(target_bitrate_kbps_ * time_since_last_encode_ms); 67 static_cast<size_t>(target_bitrate_kbps_ * time_since_last_encode_ms);
69 size_t min_bits = static_cast<size_t>( 68 size_t min_bits = static_cast<size_t>(
70 config_.simulcastStream[0].minBitrate * time_since_last_encode_ms); 69 config_.simulcastStream[0].minBitrate * time_since_last_encode_ms);
71 if (bits_available < min_bits) 70 if (bits_available < min_bits)
72 bits_available = min_bits; 71 bits_available = min_bits;
73 size_t max_bits = 72 size_t max_bits =
74 static_cast<size_t>(max_target_bitrate_kbps_ * time_since_last_encode_ms); 73 static_cast<size_t>(max_target_bitrate_kbps_ * time_since_last_encode_ms);
75 if (max_bits > 0 && max_bits < bits_available) 74 if (max_bits > 0 && max_bits < bits_available)
76 bits_available = max_bits; 75 bits_available = max_bits;
77 last_encode_time_ms_ = time_now_ms; 76 last_encode_time_ms_ = time_now_ms;
78 77
79 RTC_DCHECK_GT(config_.numberOfSimulcastStreams, 0); 78 assert(config_.numberOfSimulcastStreams > 0);
80 for (unsigned char i = 0; i < config_.numberOfSimulcastStreams; ++i) { 79 for (unsigned char i = 0; i < config_.numberOfSimulcastStreams; ++i) {
81 CodecSpecificInfo specifics; 80 CodecSpecificInfo specifics;
82 memset(&specifics, 0, sizeof(specifics)); 81 memset(&specifics, 0, sizeof(specifics));
83 specifics.codecType = kVideoCodecGeneric; 82 specifics.codecType = kVideoCodecGeneric;
84 specifics.codecSpecific.generic.simulcast_idx = i; 83 specifics.codecSpecific.generic.simulcast_idx = i;
85 size_t min_stream_bits = static_cast<size_t>( 84 size_t min_stream_bits = static_cast<size_t>(
86 config_.simulcastStream[i].minBitrate * time_since_last_encode_ms); 85 config_.simulcastStream[i].minBitrate * time_since_last_encode_ms);
87 size_t max_stream_bits = static_cast<size_t>( 86 size_t max_stream_bits = static_cast<size_t>(
88 config_.simulcastStream[i].maxBitrate * time_since_last_encode_ms); 87 config_.simulcastStream[i].maxBitrate * time_since_last_encode_ms);
89 size_t stream_bits = (bits_available > max_stream_bits) ? max_stream_bits : 88 size_t stream_bits = (bits_available > max_stream_bits) ? max_stream_bits :
90 bits_available; 89 bits_available;
91 size_t stream_bytes = (stream_bits + 7) / 8; 90 size_t stream_bytes = (stream_bits + 7) / 8;
92 if (first_encode) { 91 if (first_encode) {
93 // The first frame is a key frame and should be larger. 92 // The first frame is a key frame and should be larger.
94 // TODO(holmer): The FakeEncoder should store the bits_available between 93 // TODO(holmer): The FakeEncoder should store the bits_available between
95 // encodes so that it can compensate for oversized frames. 94 // encodes so that it can compensate for oversized frames.
96 stream_bytes *= 10; 95 stream_bytes *= 10;
97 } 96 }
98 if (stream_bytes > sizeof(encoded_buffer_)) 97 if (stream_bytes > sizeof(encoded_buffer_))
99 stream_bytes = sizeof(encoded_buffer_); 98 stream_bytes = sizeof(encoded_buffer_);
100 99
101 // Always encode something on the first frame.
102 if (min_stream_bits > bits_available && i > 0)
103 continue;
104 EncodedImage encoded( 100 EncodedImage encoded(
105 encoded_buffer_, stream_bytes, sizeof(encoded_buffer_)); 101 encoded_buffer_, stream_bytes, sizeof(encoded_buffer_));
106 encoded._timeStamp = input_image.timestamp(); 102 encoded._timeStamp = input_image.timestamp();
107 encoded.capture_time_ms_ = input_image.render_time_ms(); 103 encoded.capture_time_ms_ = input_image.render_time_ms();
108 encoded._frameType = (*frame_types)[i]; 104 encoded._frameType = (*frame_types)[i];
109 encoded._encodedWidth = config_.simulcastStream[i].width; 105 encoded._encodedWidth = config_.simulcastStream[i].width;
110 encoded._encodedHeight = config_.simulcastStream[i].height; 106 encoded._encodedHeight = config_.simulcastStream[i].height;
111 RTC_DCHECK(callback_ != NULL); 107 // Always encode something on the first frame.
108 if (min_stream_bits > bits_available && i > 0)
109 continue;
110 assert(callback_ != NULL);
112 if (callback_->Encoded(encoded, &specifics, NULL) != 0) 111 if (callback_->Encoded(encoded, &specifics, NULL) != 0)
113 return -1; 112 return -1;
114 bits_available -= std::min(encoded._length * 8, bits_available); 113 bits_available -= std::min(encoded._length * 8, bits_available);
115 } 114 }
116 return 0; 115 return 0;
117 } 116 }
118 117
119 int32_t FakeEncoder::RegisterEncodeCompleteCallback( 118 int32_t FakeEncoder::RegisterEncodeCompleteCallback(
120 EncodedImageCallback* callback) { 119 EncodedImageCallback* callback) {
121 callback_ = callback; 120 callback_ = callback;
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 delay_ms_(delay_ms) {} 199 delay_ms_(delay_ms) {}
201 200
202 int32_t DelayedEncoder::Encode(const VideoFrame& input_image, 201 int32_t DelayedEncoder::Encode(const VideoFrame& input_image,
203 const CodecSpecificInfo* codec_specific_info, 202 const CodecSpecificInfo* codec_specific_info,
204 const std::vector<FrameType>* frame_types) { 203 const std::vector<FrameType>* frame_types) {
205 SleepMs(delay_ms_); 204 SleepMs(delay_ms_);
206 return FakeEncoder::Encode(input_image, codec_specific_info, frame_types); 205 return FakeEncoder::Encode(input_image, codec_specific_info, frame_types);
207 } 206 }
208 } // namespace test 207 } // namespace test
209 } // namespace webrtc 208 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/test/call_test.cc ('k') | webrtc/video/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698