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..8b9b0cf7ac34f0c4801abc32c04c14fdebfc4850 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" |
| @@ -101,6 +102,7 @@ struct TestStream { |
| : num_frames(0), |
| aligned_buffer_size(0), |
| requested_bitrate(0), |
| + input_framerate(0), |
| requested_framerate(0), |
| requested_subsequent_bitrate(0), |
| requested_subsequent_framerate(0) {} |
| @@ -130,6 +132,7 @@ struct TestStream { |
| std::string out_filename; |
| media::VideoCodecProfile requested_profile; |
| unsigned int requested_bitrate; |
| + unsigned int input_framerate; |
|
Pawel Osciak
2014/12/18 04:54:10
Hm, now it becomes more visible that we have a dup
Owen Lin
2014/12/18 08:38:11
Done.
|
| unsigned int requested_framerate; |
| unsigned int requested_subsequent_bitrate; |
| unsigned int requested_subsequent_framerate; |
| @@ -294,15 +297,18 @@ static void ParseAndReadTestStreamData(const base::FilePath::StringType& data, |
| CHECK(base::StringToUint(fields[5], &test_stream->requested_bitrate)); |
| if (fields.size() >= 7 && !fields[6].empty()) |
| - CHECK(base::StringToUint(fields[6], &test_stream->requested_framerate)); |
| + CHECK(base::StringToUint(fields[6], &test_stream->input_framerate)); |
| - if (fields.size() >= 8 && !fields[7].empty()) { |
| - CHECK(base::StringToUint(fields[7], |
| - &test_stream->requested_subsequent_bitrate)); |
| - } |
| + if (fields.size() >= 8 && !fields[7].empty()) |
| + CHECK(base::StringToUint(fields[7], &test_stream->requested_framerate)); |
| if (fields.size() >= 9 && !fields[8].empty()) { |
| CHECK(base::StringToUint(fields[8], |
| + &test_stream->requested_subsequent_bitrate)); |
| + } |
| + |
| + if (fields.size() >= 10 && !fields[9].empty()) { |
| + CHECK(base::StringToUint(fields[9], |
| &test_stream->requested_subsequent_framerate)); |
| } |
| test_streams->push_back(test_stream); |
| @@ -497,9 +503,10 @@ 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 unless it has as many input buffers |
| + // as it asked for via RequireBitstreamBuffers(). Return false if no input |
| + // frame was fed. |
| + void FeedEncoderWithOneInput(); |
| // Provide the encoder with a new output buffer. |
| void FeedEncoderWithOutput(base::SharedMemory* shm); |
| @@ -528,10 +535,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 +624,12 @@ 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_; |
| + |
| + // The interval between two input frames. |
| + base::TimeDelta input_duration_; |
| }; |
| VEAClient::VEAClient(TestStream* test_stream, |
| @@ -656,6 +673,10 @@ VEAClient::VEAClient(TestStream* test_stream, |
| CHECK(validator_.get()); |
| + if (test_stream_->input_framerate > 0) |
| + input_duration_ = |
| + base::TimeDelta::FromSeconds(1) / test_stream->input_framerate; |
| + |
| if (save_to_file_) { |
| CHECK(!test_stream_->out_filename.empty()); |
| base::FilePath out_filename(test_stream_->out_filename); |
| @@ -706,6 +727,7 @@ void VEAClient::DestroyEncoder() { |
| if (!has_encoder()) |
| return; |
| encoder_.reset(); |
| + input_timer_.reset(); |
| } |
| void VEAClient::UpdateTestStreamData(bool mid_stream_bitrate_switch, |
| @@ -792,7 +814,16 @@ void VEAClient::RequireBitstreamBuffers(unsigned int input_count, |
| } |
| encode_start_time_ = base::TimeTicks::Now(); |
| - FeedEncoderWithInputs(); |
| + if (input_duration_ == base::TimeDelta()) { |
| + while (inputs_at_client_.size() < |
| + num_required_input_buffers_ + kNumExtraInputFrames) |
| + FeedEncoderWithOneInput(); |
| + } else { |
| + input_timer_.reset(new base::RepeatingTimer<VEAClient>()); |
| + input_timer_->Start( |
| + FROM_HERE, input_duration_, |
| + base::Bind(&VEAClient::OnInputTimer, base::Unretained(this))); |
| + } |
| } |
| void VEAClient::BitstreamBufferReady(int32 bitstream_buffer_id, |
| @@ -856,7 +887,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 (input_duration_ == base::TimeDelta()) |
| + FeedEncoderWithOneInput(); |
| } |
| scoped_refptr<media::VideoFrame> VEAClient::PrepareInputFrame(off_t position) { |
| @@ -895,39 +927,43 @@ 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) |
| + // Destructor will also stop the timer. |
| + 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) { |