Chromium Code Reviews| Index: content/common/gpu/media/video_encode_accelerator_unittest.cc |
| diff --git a/content/common/gpu/media/video_encode_accelerator_unittest.cc b/content/common/gpu/media/video_encode_accelerator_unittest.cc |
| index b1fb59226668a32f193cab0b3021bc9ac5b04df7..17f2245cb6ffd6298b3b2ff5e59b4a479980b1bb 100644 |
| --- a/content/common/gpu/media/video_encode_accelerator_unittest.cc |
| +++ b/content/common/gpu/media/video_encode_accelerator_unittest.cc |
| @@ -13,6 +13,7 @@ |
| #include "base/strings/string_number_conversions.h" |
| #include "base/strings/string_split.h" |
| #include "base/time/time.h" |
| +#include "base/timer/timer.h" |
| #include "content/common/gpu/media/video_accelerator_unittest_helpers.h" |
| #include "media/base/bind_to_current_loop.h" |
| #include "media/base/bitstream_buffer.h" |
| @@ -469,7 +470,8 @@ class VEAClient : public VideoEncodeAccelerator::Client { |
| bool force_bitrate, |
| bool test_perf, |
| bool mid_stream_bitrate_switch, |
| - bool mid_stream_framerate_switch); |
| + bool mid_stream_framerate_switch, |
| + bool real_time_clock); |
| virtual ~VEAClient(); |
| void CreateEncoder(); |
| void DestroyEncoder(); |
| @@ -497,9 +499,8 @@ class VEAClient : public VideoEncodeAccelerator::Client { |
| // Called when encoder is done with a VideoFrame. |
| void InputNoLongerNeededCallback(int32 input_id); |
| - // Ensure encoder has at least as many inputs as it asked for |
| - // via RequireBitstreamBuffers(). |
| - void FeedEncoderWithInputs(); |
| + // Feed the encoder with one input frame. |
| + void FeedEncoderWithOneInput(); |
| // Provide the encoder with a new output buffer. |
| void FeedEncoderWithOutput(base::SharedMemory* shm); |
| @@ -528,10 +529,14 @@ class VEAClient : public VideoEncodeAccelerator::Client { |
| void UpdateTestStreamData(bool mid_stream_bitrate_switch, |
| bool mid_stream_framerate_switch); |
| + // Callback function of the |input_timer_|. |
| + void OnInputTimer(); |
| + |
| ClientState state_; |
| scoped_ptr<VideoEncodeAccelerator> encoder_; |
| TestStream* test_stream_; |
| + |
| // Used to notify another thread about the state. VEAClient does not own this. |
| ClientStateNotification<ClientState>* note_; |
| @@ -613,6 +618,14 @@ class VEAClient : public VideoEncodeAccelerator::Client { |
| // Framerate to switch to in the middle of the stream. |
| unsigned int requested_subsequent_framerate_; |
| + |
| + // The timer used to feed the encoder with the input frames. |
| + scoped_ptr<base::RepeatingTimer<VEAClient>> input_timer_; |
| + |
| + // Feed the encoder with the input buffers at the |requested_framerate_|. If |
| + // false, feed as fast as possible. This is set by the command line switch |
| + // "--real_time_clock". |
| + bool real_time_clock_; |
| }; |
| VEAClient::VEAClient(TestStream* test_stream, |
| @@ -622,7 +635,8 @@ VEAClient::VEAClient(TestStream* test_stream, |
| bool force_bitrate, |
| bool test_perf, |
| bool mid_stream_bitrate_switch, |
| - bool mid_stream_framerate_switch) |
| + bool mid_stream_framerate_switch, |
| + bool real_time_clock) |
| : state_(CS_CREATED), |
| test_stream_(test_stream), |
| note_(note), |
| @@ -646,7 +660,8 @@ VEAClient::VEAClient(TestStream* test_stream, |
| requested_bitrate_(0), |
| requested_framerate_(0), |
| requested_subsequent_bitrate_(0), |
| - requested_subsequent_framerate_(0) { |
| + requested_subsequent_framerate_(0), |
| + real_time_clock_(real_time_clock) { |
| if (keyframe_period_) |
| CHECK_LT(kMaxKeyframeDelay, keyframe_period_); |
| @@ -706,6 +721,7 @@ void VEAClient::DestroyEncoder() { |
| if (!has_encoder()) |
| return; |
| encoder_.reset(); |
| + input_timer_.reset(); |
| } |
| void VEAClient::UpdateTestStreamData(bool mid_stream_bitrate_switch, |
| @@ -792,7 +808,16 @@ void VEAClient::RequireBitstreamBuffers(unsigned int input_count, |
| } |
| encode_start_time_ = base::TimeTicks::Now(); |
| - FeedEncoderWithInputs(); |
| + if (real_time_clock_) { |
| + input_timer_.reset(new base::RepeatingTimer<VEAClient>()); |
| + input_timer_->Start( |
| + FROM_HERE, base::TimeDelta::FromSeconds(1) / current_framerate_, |
| + base::Bind(&VEAClient::OnInputTimer, base::Unretained(this))); |
| + } else { |
| + while (inputs_at_client_.size() < |
| + num_required_input_buffers_ + kNumExtraInputFrames) |
| + FeedEncoderWithOneInput(); |
| + } |
| } |
| void VEAClient::BitstreamBufferReady(int32 bitstream_buffer_id, |
| @@ -856,7 +881,8 @@ void VEAClient::InputNoLongerNeededCallback(int32 input_id) { |
| std::set<int32>::iterator it = inputs_at_client_.find(input_id); |
| ASSERT_NE(it, inputs_at_client_.end()); |
| inputs_at_client_.erase(it); |
| - FeedEncoderWithInputs(); |
| + if (!real_time_clock_) |
| + FeedEncoderWithOneInput(); |
| } |
| scoped_refptr<media::VideoFrame> VEAClient::PrepareInputFrame(off_t position) { |
| @@ -895,39 +921,42 @@ scoped_refptr<media::VideoFrame> VEAClient::PrepareInputFrame(off_t position) { |
| return frame; |
| } |
| -void VEAClient::FeedEncoderWithInputs() { |
| - if (!has_encoder()) |
| - return; |
| +void VEAClient::OnInputTimer() { |
| + if (!has_encoder() || state_ != CS_ENCODING) |
| + input_timer_.reset(); |
| + else if (inputs_at_client_.size() < |
| + num_required_input_buffers_ + kNumExtraInputFrames) |
| + FeedEncoderWithOneInput(); |
| + else |
| + DVLOG(1) << "Dropping input frame"; |
| +} |
| - if (state_ != CS_ENCODING) |
| +void VEAClient::FeedEncoderWithOneInput() { |
| + if (!has_encoder() || state_ != CS_ENCODING) |
| return; |
| - while (inputs_at_client_.size() < |
| - num_required_input_buffers_ + kNumExtraInputFrames) { |
| - size_t bytes_left = |
| - test_stream_->mapped_aligned_in_file.length() - pos_in_input_stream_; |
| - if (bytes_left < test_stream_->aligned_buffer_size) { |
| - DCHECK_EQ(bytes_left, 0UL); |
| - // Rewind if at the end of stream and we are still encoding. |
| - // This is to flush the encoder with additional frames from the beginning |
| - // of the stream, or if the stream is shorter that the number of frames |
| - // we require for bitrate tests. |
| - pos_in_input_stream_ = 0; |
| - continue; |
| - } |
| + size_t bytes_left = |
| + test_stream_->mapped_aligned_in_file.length() - pos_in_input_stream_; |
| + if (bytes_left < test_stream_->aligned_buffer_size) { |
| + DCHECK_EQ(bytes_left, 0UL); |
| + // Rewind if at the end of stream and we are still encoding. |
| + // This is to flush the encoder with additional frames from the beginning |
| + // of the stream, or if the stream is shorter that the number of frames |
| + // we require for bitrate tests. |
| + pos_in_input_stream_ = 0; |
| + } |
| - bool force_keyframe = false; |
| - if (keyframe_period_ && next_input_id_ % keyframe_period_ == 0) { |
| - keyframe_requested_at_ = next_input_id_; |
| - force_keyframe = true; |
| - } |
| + bool force_keyframe = false; |
| + if (keyframe_period_ && next_input_id_ % keyframe_period_ == 0) { |
| + keyframe_requested_at_ = next_input_id_; |
| + force_keyframe = true; |
| + } |
| - scoped_refptr<media::VideoFrame> video_frame = |
| - PrepareInputFrame(pos_in_input_stream_); |
| - pos_in_input_stream_ += test_stream_->aligned_buffer_size; |
| + scoped_refptr<media::VideoFrame> video_frame = |
| + PrepareInputFrame(pos_in_input_stream_); |
| + pos_in_input_stream_ += test_stream_->aligned_buffer_size; |
| - encoder_->Encode(video_frame, force_keyframe); |
| - } |
| + encoder_->Encode(video_frame, force_keyframe); |
| } |
| void VEAClient::FeedEncoderWithOutput(base::SharedMemory* shm) { |
| @@ -981,6 +1010,10 @@ bool VEAClient::HandleEncodedFrame(bool keyframe) { |
| requested_subsequent_framerate_ != current_framerate_) { |
| SetStreamParameters(requested_subsequent_bitrate_, |
| requested_subsequent_framerate_); |
| + if (real_time_clock_ && input_timer_) |
| + input_timer_->Start( |
| + FROM_HERE, base::TimeDelta::FromSeconds(1) / current_framerate_, |
| + base::Bind(&VEAClient::OnInputTimer, base::Unretained(this))); |
| } |
| } else if (num_encoded_frames_ == num_frames_to_encode_) { |
| VerifyPerf(); |
| @@ -1024,8 +1057,10 @@ void VEAClient::VerifyStreamProperties() { |
| class VideoEncodeAcceleratorTestEnvironment : public ::testing::Environment { |
| public: |
| VideoEncodeAcceleratorTestEnvironment( |
| - scoped_ptr<base::FilePath::StringType> data) { |
| + scoped_ptr<base::FilePath::StringType> data, |
| + bool real_time_clock) { |
| test_stream_data_ = data.Pass(); |
| + real_time_clock_ = real_time_clock; |
| } |
| virtual void SetUp() { |
| @@ -1039,6 +1074,7 @@ class VideoEncodeAcceleratorTestEnvironment : public ::testing::Environment { |
| } |
| ScopedVector<TestStream> test_streams_; |
| + bool real_time_clock_; |
| private: |
| scoped_ptr<base::FilePath::StringType> test_stream_data_; |
| @@ -1080,14 +1116,11 @@ TEST_P(VideoEncodeAcceleratorTest, TestSimpleEncode) { |
| !g_env->test_streams_[test_stream_index]->out_filename.empty()); |
| notes.push_back(new ClientStateNotification<ClientState>()); |
| - clients.push_back(new VEAClient(g_env->test_streams_[test_stream_index], |
| - notes.back(), |
| - encoder_save_to_file, |
| - keyframe_period, |
| - force_bitrate, |
| - test_perf, |
| - mid_stream_bitrate_switch, |
| - mid_stream_framerate_switch)); |
| + clients.push_back( |
| + new VEAClient(g_env->test_streams_[test_stream_index], notes.back(), |
| + encoder_save_to_file, keyframe_period, force_bitrate, |
| + test_perf, mid_stream_bitrate_switch, |
| + mid_stream_framerate_switch, g_env->real_time_clock_)); |
| encoder_thread.message_loop()->PostTask( |
| FROM_HERE, |
| @@ -1184,6 +1217,7 @@ int main(int argc, char** argv) { |
| const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
| DCHECK(cmd_line); |
| + bool real_time_clock = false; |
| base::CommandLine::SwitchMap switches = cmd_line->GetSwitches(); |
| for (base::CommandLine::SwitchMap::const_iterator it = switches.begin(); |
| it != switches.end(); |
| @@ -1192,6 +1226,10 @@ int main(int argc, char** argv) { |
| test_stream_data->assign(it->second.c_str()); |
| continue; |
| } |
| + if (it->first == "real_time_clock") { |
|
Pawel Osciak
2014/12/21 08:07:05
Hm, I know I proposed RealTime, but this doesn't t
|
| + real_time_clock = true; |
| + continue; |
| + } |
| if (it->first == "v" || it->first == "vmodule") |
| continue; |
| LOG(FATAL) << "Unexpected switch: " << it->first << ":" << it->second; |
| @@ -1201,7 +1239,7 @@ int main(int argc, char** argv) { |
| reinterpret_cast<content::VideoEncodeAcceleratorTestEnvironment*>( |
| testing::AddGlobalTestEnvironment( |
| new content::VideoEncodeAcceleratorTestEnvironment( |
| - test_stream_data.Pass()))); |
| + test_stream_data.Pass(), real_time_clock))); |
| return RUN_ALL_TESTS(); |
| } |