brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 11 | #include <stdint.h> |
| 12 | #include <stdio.h> |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 13 | #include <memory> |
| 14 | #include <string> |
| 15 | |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 16 | #include "api/scoped_refptr.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "api/video/i420_buffer.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 18 | #include "api/video/video_frame_buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "test/gtest.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "test/testsupport/file_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "test/testsupport/frame_reader.h" |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | namespace test { |
| 25 | |
| 26 | namespace { |
| 27 | const std::string kInputFileContents = "bazouk"; |
| 28 | |
| 29 | const size_t kFrameWidth = 2; |
| 30 | const size_t kFrameHeight = 2; |
| 31 | const size_t kFrameLength = 3 * kFrameWidth * kFrameHeight / 2; // I420. |
| 32 | } // namespace |
| 33 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame^] | 34 | class YuvFrameReaderTest : public ::testing::Test { |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 35 | protected: |
| 36 | YuvFrameReaderTest() = default; |
| 37 | ~YuvFrameReaderTest() override = default; |
| 38 | |
| 39 | void SetUp() override { |
| 40 | temp_filename_ = webrtc::test::TempFilename(webrtc::test::OutputPath(), |
| 41 | "yuv_frame_reader_unittest"); |
| 42 | FILE* dummy = fopen(temp_filename_.c_str(), "wb"); |
| 43 | fprintf(dummy, "%s", kInputFileContents.c_str()); |
| 44 | fclose(dummy); |
| 45 | |
| 46 | frame_reader_.reset( |
| 47 | new YuvFrameReaderImpl(temp_filename_, kFrameWidth, kFrameHeight)); |
| 48 | ASSERT_TRUE(frame_reader_->Init()); |
| 49 | } |
| 50 | |
| 51 | void TearDown() override { remove(temp_filename_.c_str()); } |
| 52 | |
| 53 | std::unique_ptr<FrameReader> frame_reader_; |
| 54 | std::string temp_filename_; |
| 55 | }; |
| 56 | |
| 57 | TEST_F(YuvFrameReaderTest, InitSuccess) {} |
| 58 | |
| 59 | TEST_F(YuvFrameReaderTest, FrameLength) { |
| 60 | EXPECT_EQ(kFrameLength, frame_reader_->FrameLength()); |
| 61 | } |
| 62 | |
| 63 | TEST_F(YuvFrameReaderTest, NumberOfFrames) { |
| 64 | EXPECT_EQ(1, frame_reader_->NumberOfFrames()); |
| 65 | } |
| 66 | |
| 67 | TEST_F(YuvFrameReaderTest, ReadFrame) { |
Magnus Jedvert | 90e3190 | 2017-06-07 11:32:50 +0200 | [diff] [blame] | 68 | rtc::scoped_refptr<I420BufferInterface> buffer = frame_reader_->ReadFrame(); |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 69 | ASSERT_TRUE(buffer); |
| 70 | // Expect I420 packed as YUV. |
| 71 | EXPECT_EQ(kInputFileContents[0], buffer->DataY()[0]); |
| 72 | EXPECT_EQ(kInputFileContents[1], buffer->DataY()[1]); |
| 73 | EXPECT_EQ(kInputFileContents[2], buffer->DataY()[2]); |
| 74 | EXPECT_EQ(kInputFileContents[3], buffer->DataY()[3]); |
| 75 | EXPECT_EQ(kInputFileContents[4], buffer->DataU()[0]); |
| 76 | EXPECT_EQ(kInputFileContents[5], buffer->DataV()[0]); |
| 77 | EXPECT_FALSE(frame_reader_->ReadFrame()); // End of file. |
| 78 | } |
| 79 | |
| 80 | TEST_F(YuvFrameReaderTest, ReadFrameUninitialized) { |
| 81 | YuvFrameReaderImpl file_reader(temp_filename_, kFrameWidth, kFrameHeight); |
| 82 | EXPECT_FALSE(file_reader.ReadFrame()); |
| 83 | } |
| 84 | |
| 85 | } // namespace test |
| 86 | } // namespace webrtc |