Index: content/renderer/media/gpu/rtc_video_encoder_unittest.cc |
diff --git a/content/renderer/media/gpu/rtc_video_encoder_unittest.cc b/content/renderer/media/gpu/rtc_video_encoder_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..63798745cb2e57487bbbb8a5faf87511080ce7e4 |
--- /dev/null |
+++ b/content/renderer/media/gpu/rtc_video_encoder_unittest.cc |
@@ -0,0 +1,156 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <stdint.h> |
+#include <string.h> |
Pawel Osciak
2016/07/28 01:48:08
Is string.h needed?
emircan
2016/07/28 19:35:45
Removed.
|
+ |
+#include "base/bind.h" |
+#include "base/memory/ptr_util.h" |
+#include "base/single_thread_task_runner.h" |
+#include "base/synchronization/waitable_event.h" |
+#include "base/threading/thread.h" |
+#include "content/renderer/media/gpu/rtc_video_encoder.h" |
+#include "media/renderers/mock_gpu_video_accelerator_factories.h" |
+#include "media/video/mock_video_encode_accelerator.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "third_party/webrtc/modules/video_coding/include/video_codec_interface.h" |
+ |
+using ::testing::_; |
+using ::testing::Invoke; |
+using ::testing::Return; |
+using ::testing::SaveArg; |
+using ::testing::Values; |
+using ::testing::WithArgs; |
+ |
+namespace content { |
+ |
+namespace { |
+ |
+const unsigned short kInputFrameWidth = 345; |
+const unsigned short kInputFrameHeight = 234; |
+ |
+} // anonymous namespace |
+ |
+class RTCVideoEncoderTest |
+ : public ::testing::TestWithParam<webrtc::VideoCodecType> { |
+ public: |
+ RTCVideoEncoderTest() |
+ : mock_gpu_factories_( |
+ new media::MockGpuVideoAcceleratorFactories(nullptr)), |
+ vea_thread_("vea_thread"), |
+ idle_waiter_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
+ base::WaitableEvent::InitialState::NOT_SIGNALED) {} |
+ |
+ void SetUp() override { |
+ DVLOG(3) << __FUNCTION__; |
+ ASSERT_TRUE(vea_thread_.Start()); |
+ vea_task_runner_ = vea_thread_.task_runner(); |
+ mock_vea_ = new media::MockVideoEncodeAccelerator(); |
+ |
+ EXPECT_CALL(*mock_gpu_factories_.get(), GetTaskRunner()) |
+ .WillRepeatedly(Return(vea_task_runner_)); |
+ EXPECT_CALL(*mock_gpu_factories_.get(), DoCreateVideoEncodeAccelerator()) |
+ .WillRepeatedly(Return(mock_vea_)); |
+ EXPECT_CALL(*mock_vea_, Initialize(_, _, _, _, _)) |
+ .WillOnce(Invoke(this, &RTCVideoEncoderTest::Initialize)); |
+ EXPECT_CALL(*mock_vea_, UseOutputBitstreamBuffer(_)).Times(3); |
+ EXPECT_CALL(*mock_vea_, Destroy()).Times(1); |
+ } |
+ |
+ void TearDown() override { |
+ DVLOG(3) << __FUNCTION__; |
+ EXPECT_TRUE(vea_thread_.IsRunning()); |
+ RunUntilIdle(); |
+ rtc_encoder_->Release(); |
+ RunUntilIdle(); |
+ vea_thread_.Stop(); |
+ } |
+ |
+ void CreateEncoder(webrtc::VideoCodecType codec_type) { |
+ DVLOG(3) << __FUNCTION__; |
+ rtc_encoder_ = base::WrapUnique( |
+ new RTCVideoEncoder(codec_type, mock_gpu_factories_.get())); |
+ } |
+ |
+ webrtc::VideoCodec GetDefaultCodec() { |
+ webrtc::VideoCodec codec; |
tommi (sloooow) - chröme
2016/07/28 07:40:40
skip memset and just do:
webrtc::VideoCodec codec
emircan
2016/07/28 19:35:45
Done.
|
+ memset(&codec, 0, sizeof(codec)); |
+ codec.width = kInputFrameWidth; |
+ codec.height = kInputFrameHeight; |
+ codec.codecType = webrtc::kVideoCodecVP8; |
+ codec.startBitrate = 100; |
Pawel Osciak
2016/07/28 01:48:08
Could this be a constant defined together with the
emircan
2016/07/28 19:35:45
Done.
|
+ return codec; |
+ } |
+ |
+ bool Initialize(media::VideoPixelFormat input_format, |
+ const gfx::Size& input_visible_size, |
+ media::VideoCodecProfile output_profile, |
+ uint32_t initial_bitrate, |
+ media::VideoEncodeAccelerator::Client* client) { |
+ client->RequireBitstreamBuffers(0, input_visible_size, 12345); |
Pawel Osciak
2016/07/28 01:48:08
Perhaps we could use input_visible_size_.GetArea()
emircan
2016/07/28 19:35:45
Done.
|
+ return true; |
+ } |
+ |
+ void RunUntilIdle() { |
+ DVLOG(3) << __FUNCTION__; |
+ vea_task_runner_->PostTask(FROM_HERE, |
+ base::Bind(&base::WaitableEvent::Signal, |
+ base::Unretained(&idle_waiter_))); |
+ idle_waiter_.Wait(); |
+ } |
+ |
+ void VerifyEncodeFrame(const scoped_refptr<media::VideoFrame>& frame, |
Pawel Osciak
2016/07/28 01:48:08
Perhaps s/Encode/Encoded/ ?
emircan
2016/07/28 19:35:45
Done.
|
+ bool force_keyframe) { |
+ EXPECT_EQ(kInputFrameWidth, frame->visible_rect().width()); |
+ EXPECT_EQ(kInputFrameHeight, frame->visible_rect().height()); |
+ } |
+ |
+ protected: |
+ std::unique_ptr<media::MockGpuVideoAcceleratorFactories> mock_gpu_factories_; |
+ media::MockVideoEncodeAccelerator* mock_vea_; |
+ std::unique_ptr<RTCVideoEncoder> rtc_encoder_; |
+ base::Thread vea_thread_; |
tommi (sloooow) - chröme
2016/07/28 07:40:40
encoder_thread_?
emircan
2016/07/28 19:35:45
Done.
|
+ |
+ private: |
+ scoped_refptr<base::SingleThreadTaskRunner> vea_task_runner_; |
tommi (sloooow) - chröme
2016/07/28 07:40:40
needed? or is vea_thread_.task_runner() enough?
emircan
2016/07/28 19:35:45
Done.
|
+ base::WaitableEvent idle_waiter_; |
+}; |
+ |
+TEST_P(RTCVideoEncoderTest, CreateAndInitSucceeds) { |
+ const webrtc::VideoCodecType codec_type = GetParam(); |
+ CreateEncoder(codec_type); |
+ webrtc::VideoCodec codec = GetDefaultCodec(); |
+ codec.codecType = codec_type; |
+ EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, rtc_encoder_->InitEncode(&codec, 1, 12345)); |
+} |
+ |
+TEST_F(RTCVideoEncoderTest, EncodeScaledFrame) { |
+ const webrtc::VideoCodecType codec_type = webrtc::kVideoCodecVP8; |
+ CreateEncoder(codec_type); |
+ webrtc::VideoCodec codec = GetDefaultCodec(); |
+ rtc_encoder_->InitEncode(&codec, 1, 12345); |
Pawel Osciak
2016/07/28 01:48:08
Should we check return values from InitEncode() an
emircan
2016/07/28 19:35:45
I was checking it in the previous test, but it def
|
+ |
+ EXPECT_CALL(*mock_vea_, Encode(_, _)) |
+ .Times(2) |
+ .WillRepeatedly(Invoke(this, &RTCVideoEncoderTest::VerifyEncodeFrame)); |
+ |
+ const auto& buffer = |
+ webrtc::I420Buffer::Create(kInputFrameWidth, kInputFrameHeight); |
Pawel Osciak
2016/07/28 01:48:08
Would it be possible to prefill buffers with one c
emircan
2016/07/28 19:35:45
I prefill the buffers with black and verify it in
Pawel Osciak
2016/08/02 05:53:20
Patterns would be preferable, we could maybe have
emircan
2016/08/02 18:04:44
I changed the color to a custom one. I will refrai
|
+ std::vector<webrtc::FrameType> frame_types; |
+ rtc_encoder_->Encode( |
+ webrtc::VideoFrame(buffer, 0, 0, webrtc::kVideoRotation_0), nullptr, |
+ &frame_types); |
+ |
+ const auto& scaled_buffer = |
+ webrtc::I420Buffer::Create(2 * kInputFrameWidth, 2 * kInputFrameHeight); |
+ rtc_encoder_->Encode( |
+ webrtc::VideoFrame(scaled_buffer, 0, 0, webrtc::kVideoRotation_0), |
+ nullptr, &frame_types); |
+} |
+ |
+INSTANTIATE_TEST_CASE_P(CodecProfiles, |
+ RTCVideoEncoderTest, |
+ Values(webrtc::kVideoCodecVP8, |
+ webrtc::kVideoCodecH264)); |
+} // namespace content |