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 52be56b5c3541aaacf2a9b6f103c7a48ea0eecef..6c61b95506e70f240c12bfcf3199d9417ab4e277 100644 |
--- a/content/common/gpu/media/video_encode_accelerator_unittest.cc |
+++ b/content/common/gpu/media/video_encode_accelerator_unittest.cc |
@@ -2,6 +2,8 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+#include <algorithm> |
+ |
#include "base/at_exit.h" |
#include "base/bind.h" |
#include "base/command_line.h" |
@@ -568,9 +570,12 @@ class VEAClient : public VideoEncodeAccelerator::Client { |
void CreateEncoder(); |
void DestroyEncoder(); |
- // Return the number of encoded frames per second. |
+ // Returns the number of encoded frames per second. |
double frames_per_second(); |
+ // Returns the median encode delay for all the encoded frames. |
+ base::TimeDelta median_encode_latency(); |
+ |
// VideoDecodeAccelerator::Client implementation. |
void RequireBitstreamBuffers(unsigned int input_count, |
const gfx::Size& input_coded_size, |
@@ -622,9 +627,11 @@ class VEAClient : public VideoEncodeAccelerator::Client { |
// Write an IVF frame header to test_stream_->out_filename. |
void WriteIvfFrameHeader(int frame_index, size_t frame_size); |
- // Prepare and return a frame wrapping the data at |position| bytes in |
- // the input stream, ready to be sent to encoder. |
- scoped_refptr<media::VideoFrame> PrepareInputFrame(off_t position); |
+ // Prepare and return a frame wrapping the data at |position| bytes in the |
+ // input stream, ready to be sent to encoder. Returns a pair of video frame |
+ // and the input frame id. |
+ std::pair<scoped_refptr<media::VideoFrame>, int32> PrepareInputFrame( |
+ off_t position); |
// Update the parameters according to |mid_stream_bitrate_switch| and |
// |mid_stream_framerate_switch|. |
@@ -642,11 +649,16 @@ class VEAClient : public VideoEncodeAccelerator::Client { |
// Used to notify another thread about the state. VEAClient does not own this. |
ClientStateNotification<ClientState>* note_; |
- // Ids assigned to VideoFrames (start at 1 for easy comparison with |
- // num_encoded_frames_). |
Justin Chuang
2015/04/30 10:36:56
The original comments is wrong. So remove it. (It
|
+ // Ids assigned to VideoFrames. |
std::set<int32> inputs_at_client_; |
int32 next_input_id_; |
+ typedef std::map<int32, base::TimeTicks> IdToTicks; |
+ // A map from input frame id to the encode start time of the frame. |
+ IdToTicks encode_start_time_per_frame_; |
+ // The encode latencies of all encoded frames. |
+ std::vector<base::TimeDelta> encode_latencies_; |
+ |
// Ids for output BitstreamBuffers. |
typedef std::map<int32, base::SharedMemory*> IdToSHM; |
ScopedVector<base::SharedMemory> output_shms_; |
@@ -915,6 +927,16 @@ double VEAClient::frames_per_second() { |
return num_encoded_frames_ / duration.InSecondsF(); |
} |
+base::TimeDelta VEAClient::median_encode_latency() { |
+ if (encode_latencies_.size() == 0) |
+ return base::TimeDelta(); |
+ std::sort(encode_latencies_.begin(), encode_latencies_.end()); |
+ int index = encode_latencies_.size() / 2; |
+ if (encode_latencies_.size() % 2 != 0) |
+ return encode_latencies_[index]; |
+ return (encode_latencies_[index] + encode_latencies_[index - 1]) / 2; |
+} |
+ |
void VEAClient::RequireBitstreamBuffers(unsigned int input_count, |
const gfx::Size& input_coded_size, |
size_t output_size) { |
@@ -1038,7 +1060,8 @@ void VEAClient::InputNoLongerNeededCallback(int32 input_id) { |
FeedEncoderWithOneInput(); |
} |
-scoped_refptr<media::VideoFrame> VEAClient::PrepareInputFrame(off_t position) { |
+std::pair<scoped_refptr<media::VideoFrame>, int32> VEAClient::PrepareInputFrame( |
+ off_t position) { |
CHECK_LE(position + test_stream_->aligned_buffer_size, |
test_stream_->mapped_aligned_in_file.length()); |
@@ -1069,9 +1092,8 @@ scoped_refptr<media::VideoFrame> VEAClient::PrepareInputFrame(off_t position) { |
next_input_id_))); |
CHECK(inputs_at_client_.insert(next_input_id_).second); |
- ++next_input_id_; |
- return frame; |
+ return std::make_pair(frame, next_input_id_++); |
} |
void VEAClient::OnInputTimer() { |
@@ -1105,11 +1127,12 @@ void VEAClient::FeedEncoderWithOneInput() { |
++num_keyframes_requested_; |
} |
- scoped_refptr<media::VideoFrame> video_frame = |
- PrepareInputFrame(pos_in_input_stream_); |
+ auto ret = PrepareInputFrame(pos_in_input_stream_); |
pos_in_input_stream_ += test_stream_->aligned_buffer_size; |
- encoder_->Encode(video_frame, force_keyframe); |
+ CHECK(encode_start_time_per_frame_.insert( |
+ std::make_pair(ret.second, base::TimeTicks::Now())).second); |
+ encoder_->Encode(ret.first, force_keyframe); |
} |
void VEAClient::FeedEncoderWithOutput(base::SharedMemory* shm) { |
@@ -1134,11 +1157,18 @@ bool VEAClient::HandleEncodedFrame(bool keyframe) { |
// return value from this method. |
CHECK_LE(num_encoded_frames_, num_frames_to_encode_); |
+ last_frame_ready_time_ = base::TimeTicks::Now(); |
+ |
+ // Record encode latency of this frame. |
+ IdToTicks::iterator it = |
+ encode_start_time_per_frame_.find(num_encoded_frames_); |
+ CHECK_NE(it, encode_start_time_per_frame_.end()); |
+ encode_latencies_.push_back(last_frame_ready_time_ - it->second); |
+ encode_start_time_per_frame_.erase(it); |
+ |
++num_encoded_frames_; |
++num_frames_since_last_check_; |
- last_frame_ready_time_ = base::TimeTicks::Now(); |
- |
// Because the keyframe behavior requirements are loose, we give |
// the encoder more freedom here. It could either deliver a keyframe |
// immediately after we requested it, which could be for a frame number |
@@ -1189,6 +1219,13 @@ void VEAClient::VerifyPerf() { |
base::StringPrintf("%.3f", measured_fps)); |
if (test_perf_) |
EXPECT_GE(measured_fps, kMinPerfFPS); |
+ |
+ base::TimeDelta median_latency = median_encode_latency(); |
+ LOG(INFO) << "Median encode latency: " << median_latency.InMicroseconds() |
+ << " us"; |
+ g_env->LogToFile( |
+ "Median encode latency", |
+ base::StringPrintf("%" PRId64 " us", median_latency.InMicroseconds())); |
} |
void VEAClient::VerifyStreamProperties() { |