• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <string.h>
14 
15 #include <memory>
16 #include <string>
17 
18 #include "test/gtest.h"
19 #include "test/testsupport/file_utils.h"
20 #include "test/testsupport/frame_writer.h"
21 
22 namespace webrtc {
23 namespace test {
24 
25 namespace {
26 const size_t kFrameWidth = 50;
27 const size_t kFrameHeight = 20;
28 const size_t kFrameLength = 3 * kFrameWidth * kFrameHeight / 2;  // I420.
29 const size_t kFrameRate = 30;
30 
31 const std::string kFileHeader = "YUV4MPEG2 W50 H20 F30:1 C420\n";
32 const std::string kFrameHeader = "FRAME\n";
33 }  // namespace
34 
35 class Y4mFrameWriterTest : public ::testing::Test {
36  protected:
37   Y4mFrameWriterTest() = default;
38   ~Y4mFrameWriterTest() override = default;
39 
SetUp()40   void SetUp() override {
41     temp_filename_ = webrtc::test::TempFilename(webrtc::test::OutputPath(),
42                                                 "y4m_frame_writer_unittest");
43     frame_writer_.reset(new Y4mFrameWriterImpl(temp_filename_, kFrameWidth,
44                                                kFrameHeight, kFrameRate));
45     ASSERT_TRUE(frame_writer_->Init());
46   }
47 
TearDown()48   void TearDown() override { remove(temp_filename_.c_str()); }
49 
50   std::unique_ptr<FrameWriter> frame_writer_;
51   std::string temp_filename_;
52 };
53 
TEST_F(Y4mFrameWriterTest,InitSuccess)54 TEST_F(Y4mFrameWriterTest, InitSuccess) {}
55 
TEST_F(Y4mFrameWriterTest,FrameLength)56 TEST_F(Y4mFrameWriterTest, FrameLength) {
57   EXPECT_EQ(kFrameLength, frame_writer_->FrameLength());
58 }
59 
TEST_F(Y4mFrameWriterTest,WriteFrame)60 TEST_F(Y4mFrameWriterTest, WriteFrame) {
61   uint8_t buffer[kFrameLength];
62   memset(buffer, 9, kFrameLength);  // Write lots of 9s to the buffer.
63   bool result = frame_writer_->WriteFrame(buffer);
64   ASSERT_TRUE(result);
65   result = frame_writer_->WriteFrame(buffer);
66   ASSERT_TRUE(result);
67 
68   frame_writer_->Close();
69   EXPECT_EQ(kFileHeader.size() + 2 * kFrameHeader.size() + 2 * kFrameLength,
70             GetFileSize(temp_filename_));
71 }
72 
TEST_F(Y4mFrameWriterTest,WriteFrameUninitialized)73 TEST_F(Y4mFrameWriterTest, WriteFrameUninitialized) {
74   uint8_t buffer[kFrameLength];
75   Y4mFrameWriterImpl frame_writer(temp_filename_, kFrameWidth, kFrameHeight,
76                                   kFrameRate);
77   EXPECT_FALSE(frame_writer.WriteFrame(buffer));
78 }
79 
80 }  // namespace test
81 }  // namespace webrtc
82