1 // Copyright 2021 The libgav1 Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "examples/file_reader_factory.h"
16
17 #include <cstddef>
18 #include <cstdint>
19 #include <memory>
20 #include <new>
21 #include <string>
22 #include <vector>
23
24 #include "absl/memory/memory.h"
25 #include "examples/file_reader_interface.h"
26 #include "gtest/gtest.h"
27
28 namespace libgav1 {
29 namespace {
30
31 class AlwaysFailFileReader : public FileReaderInterface {
32 public:
Open(const std::string &,bool)33 static std::unique_ptr<FileReaderInterface> Open(
34 const std::string& /*file_name*/, bool /*error_tolerant*/) {
35 return nullptr;
36 }
37
38 AlwaysFailFileReader() = delete;
39 AlwaysFailFileReader(const AlwaysFailFileReader&) = delete;
40 AlwaysFailFileReader& operator=(const AlwaysFailFileReader&) = delete;
41 // Note this isn't overridden as the class can never be instantiated. This
42 // avoids an unused function warning.
43 // ~AlwaysFailFileReader() override = default;
44
ReadTemporalUnit(std::vector<uint8_t> *,int64_t *)45 bool ReadTemporalUnit(std::vector<uint8_t>* /*data*/,
46 int64_t* /*pts*/) override {
47 return false;
48 }
IsEndOfFile() const49 bool IsEndOfFile() const override { return false; }
50
width() const51 size_t width() const override { return 0; }
height() const52 size_t height() const override { return 0; }
frame_rate() const53 size_t frame_rate() const override { return 0; }
time_scale() const54 size_t time_scale() const override { return 0; }
55
56 static bool is_registered_;
57 };
58
59 class AlwaysOkFileReader : public FileReaderInterface {
60 public:
Open(const std::string &,bool)61 static std::unique_ptr<FileReaderInterface> Open(
62 const std::string& /*file_name*/, bool /*error_tolerant*/) {
63 auto reader = absl::WrapUnique(new (std::nothrow) AlwaysOkFileReader());
64
65 return reader;
66 }
67
68 AlwaysOkFileReader(const AlwaysOkFileReader&) = delete;
69 AlwaysOkFileReader& operator=(const AlwaysOkFileReader&) = delete;
70 ~AlwaysOkFileReader() override = default;
71
ReadTemporalUnit(std::vector<uint8_t> *,int64_t *)72 bool ReadTemporalUnit(std::vector<uint8_t>* /*data*/,
73 int64_t* /*pts*/) override {
74 return true;
75 }
IsEndOfFile() const76 bool IsEndOfFile() const override { return true; }
77
width() const78 size_t width() const override { return 1; }
height() const79 size_t height() const override { return 1; }
frame_rate() const80 size_t frame_rate() const override { return 1; }
time_scale() const81 size_t time_scale() const override { return 1; }
82
83 static bool is_registered_;
84
85 private:
86 AlwaysOkFileReader() = default;
87 };
88
89 bool AlwaysFailFileReader::is_registered_ =
90 FileReaderFactory::RegisterReader(AlwaysFailFileReader::Open);
91
92 bool AlwaysOkFileReader::is_registered_ =
93 FileReaderFactory::RegisterReader(AlwaysOkFileReader::Open);
94
TEST(FileReaderFactoryTest,RegistrationFail)95 TEST(FileReaderFactoryTest, RegistrationFail) {
96 EXPECT_FALSE(FileReaderFactory::RegisterReader(nullptr));
97 }
98
TEST(FileReaderFactoryTest,OpenReader)99 TEST(FileReaderFactoryTest, OpenReader) {
100 ASSERT_TRUE(AlwaysOkFileReader::is_registered_);
101 ASSERT_TRUE(AlwaysFailFileReader::is_registered_);
102
103 auto reader = FileReaderFactory::OpenReader("fake file");
104 EXPECT_NE(reader, nullptr);
105 EXPECT_TRUE(reader->IsEndOfFile());
106 EXPECT_TRUE(reader->ReadTemporalUnit(nullptr, nullptr));
107 EXPECT_EQ(reader->width(), 1);
108 EXPECT_EQ(reader->height(), 1);
109 EXPECT_EQ(reader->frame_rate(), 1);
110 EXPECT_EQ(reader->time_scale(), 1);
111 }
112
113 } // namespace
114 } // namespace libgav1
115