| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string.h> | 5 #include <string.h> |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "media/formats/mp4/box_reader.h" | 10 #include "media/formats/mp4/box_reader.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 protected: | 79 protected: |
| 80 std::vector<uint8> GetBuf() { | 80 std::vector<uint8> GetBuf() { |
| 81 return std::vector<uint8>(kSkipBox, kSkipBox + sizeof(kSkipBox)); | 81 return std::vector<uint8>(kSkipBox, kSkipBox + sizeof(kSkipBox)); |
| 82 } | 82 } |
| 83 }; | 83 }; |
| 84 | 84 |
| 85 TEST_F(BoxReaderTest, ExpectedOperationTest) { | 85 TEST_F(BoxReaderTest, ExpectedOperationTest) { |
| 86 std::vector<uint8> buf = GetBuf(); | 86 std::vector<uint8> buf = GetBuf(); |
| 87 bool err; | 87 bool err; |
| 88 scoped_ptr<BoxReader> reader( | 88 scoped_ptr<BoxReader> reader( |
| 89 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), LogCB(), &err)); | 89 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), new MediaLog(), &err)); |
| 90 EXPECT_FALSE(err); | 90 EXPECT_FALSE(err); |
| 91 EXPECT_TRUE(reader.get()); | 91 EXPECT_TRUE(reader.get()); |
| 92 | 92 |
| 93 SkipBox box; | 93 SkipBox box; |
| 94 EXPECT_TRUE(box.Parse(reader.get())); | 94 EXPECT_TRUE(box.Parse(reader.get())); |
| 95 EXPECT_EQ(0x01, reader->version()); | 95 EXPECT_EQ(0x01, reader->version()); |
| 96 EXPECT_EQ(0x020304u, reader->flags()); | 96 EXPECT_EQ(0x020304u, reader->flags()); |
| 97 EXPECT_EQ(0x05, box.a); | 97 EXPECT_EQ(0x05, box.a); |
| 98 EXPECT_EQ(0x06, box.b); | 98 EXPECT_EQ(0x06, box.b); |
| 99 EXPECT_EQ(0x0708, box.c); | 99 EXPECT_EQ(0x0708, box.c); |
| 100 EXPECT_EQ(static_cast<int32>(0xf90a0b0c), box.d); | 100 EXPECT_EQ(static_cast<int32>(0xf90a0b0c), box.d); |
| 101 EXPECT_EQ(static_cast<int32>(0xfd0e0f10), box.e); | 101 EXPECT_EQ(static_cast<int32>(0xfd0e0f10), box.e); |
| 102 | 102 |
| 103 EXPECT_EQ(2u, box.kids.size()); | 103 EXPECT_EQ(2u, box.kids.size()); |
| 104 EXPECT_EQ(0xdeadbeef, box.kids[0].val); | 104 EXPECT_EQ(0xdeadbeef, box.kids[0].val); |
| 105 EXPECT_EQ(0xfacecafe, box.kids[1].val); | 105 EXPECT_EQ(0xfacecafe, box.kids[1].val); |
| 106 | 106 |
| 107 // Accounting for the extra byte outside of the box above | 107 // Accounting for the extra byte outside of the box above |
| 108 EXPECT_EQ(buf.size(), static_cast<uint64>(reader->size() + 1)); | 108 EXPECT_EQ(buf.size(), static_cast<uint64>(reader->size() + 1)); |
| 109 } | 109 } |
| 110 | 110 |
| 111 TEST_F(BoxReaderTest, OuterTooShortTest) { | 111 TEST_F(BoxReaderTest, OuterTooShortTest) { |
| 112 std::vector<uint8> buf = GetBuf(); | 112 std::vector<uint8> buf = GetBuf(); |
| 113 bool err; | 113 bool err; |
| 114 | 114 |
| 115 // Create a soft failure by truncating the outer box. | 115 // Create a soft failure by truncating the outer box. |
| 116 scoped_ptr<BoxReader> r( | 116 scoped_ptr<BoxReader> r(BoxReader::ReadTopLevelBox(&buf[0], buf.size() - 2, |
| 117 BoxReader::ReadTopLevelBox(&buf[0], buf.size() - 2, LogCB(), &err)); | 117 new MediaLog(), &err)); |
| 118 | 118 |
| 119 EXPECT_FALSE(err); | 119 EXPECT_FALSE(err); |
| 120 EXPECT_FALSE(r.get()); | 120 EXPECT_FALSE(r.get()); |
| 121 } | 121 } |
| 122 | 122 |
| 123 TEST_F(BoxReaderTest, InnerTooLongTest) { | 123 TEST_F(BoxReaderTest, InnerTooLongTest) { |
| 124 std::vector<uint8> buf = GetBuf(); | 124 std::vector<uint8> buf = GetBuf(); |
| 125 bool err; | 125 bool err; |
| 126 | 126 |
| 127 // Make an inner box too big for its outer box. | 127 // Make an inner box too big for its outer box. |
| 128 buf[25] = 1; | 128 buf[25] = 1; |
| 129 scoped_ptr<BoxReader> reader( | 129 scoped_ptr<BoxReader> reader( |
| 130 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), LogCB(), &err)); | 130 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), new MediaLog(), &err)); |
| 131 | 131 |
| 132 SkipBox box; | 132 SkipBox box; |
| 133 EXPECT_FALSE(box.Parse(reader.get())); | 133 EXPECT_FALSE(box.Parse(reader.get())); |
| 134 } | 134 } |
| 135 | 135 |
| 136 TEST_F(BoxReaderTest, WrongFourCCTest) { | 136 TEST_F(BoxReaderTest, WrongFourCCTest) { |
| 137 std::vector<uint8> buf = GetBuf(); | 137 std::vector<uint8> buf = GetBuf(); |
| 138 bool err; | 138 bool err; |
| 139 | 139 |
| 140 // Set an unrecognized top-level FourCC. | 140 // Set an unrecognized top-level FourCC. |
| 141 buf[5] = 1; | 141 buf[5] = 1; |
| 142 scoped_ptr<BoxReader> reader( | 142 scoped_ptr<BoxReader> reader( |
| 143 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), LogCB(), &err)); | 143 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), new MediaLog(), &err)); |
| 144 EXPECT_FALSE(reader.get()); | 144 EXPECT_FALSE(reader.get()); |
| 145 EXPECT_TRUE(err); | 145 EXPECT_TRUE(err); |
| 146 } | 146 } |
| 147 | 147 |
| 148 TEST_F(BoxReaderTest, ScanChildrenTest) { | 148 TEST_F(BoxReaderTest, ScanChildrenTest) { |
| 149 std::vector<uint8> buf = GetBuf(); | 149 std::vector<uint8> buf = GetBuf(); |
| 150 bool err; | 150 bool err; |
| 151 scoped_ptr<BoxReader> reader( | 151 scoped_ptr<BoxReader> reader( |
| 152 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), LogCB(), &err)); | 152 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), new MediaLog(), &err)); |
| 153 | 153 |
| 154 EXPECT_TRUE(reader->SkipBytes(16) && reader->ScanChildren()); | 154 EXPECT_TRUE(reader->SkipBytes(16) && reader->ScanChildren()); |
| 155 | 155 |
| 156 FreeBox free; | 156 FreeBox free; |
| 157 EXPECT_TRUE(reader->ReadChild(&free)); | 157 EXPECT_TRUE(reader->ReadChild(&free)); |
| 158 EXPECT_FALSE(reader->ReadChild(&free)); | 158 EXPECT_FALSE(reader->ReadChild(&free)); |
| 159 EXPECT_TRUE(reader->MaybeReadChild(&free)); | 159 EXPECT_TRUE(reader->MaybeReadChild(&free)); |
| 160 | 160 |
| 161 std::vector<PsshBox> kids; | 161 std::vector<PsshBox> kids; |
| 162 | 162 |
| 163 EXPECT_TRUE(reader->ReadChildren(&kids)); | 163 EXPECT_TRUE(reader->ReadChildren(&kids)); |
| 164 EXPECT_EQ(2u, kids.size()); | 164 EXPECT_EQ(2u, kids.size()); |
| 165 kids.clear(); | 165 kids.clear(); |
| 166 EXPECT_FALSE(reader->ReadChildren(&kids)); | 166 EXPECT_FALSE(reader->ReadChildren(&kids)); |
| 167 EXPECT_TRUE(reader->MaybeReadChildren(&kids)); | 167 EXPECT_TRUE(reader->MaybeReadChildren(&kids)); |
| 168 } | 168 } |
| 169 | 169 |
| 170 TEST_F(BoxReaderTest, ReadAllChildrenTest) { | 170 TEST_F(BoxReaderTest, ReadAllChildrenTest) { |
| 171 std::vector<uint8> buf = GetBuf(); | 171 std::vector<uint8> buf = GetBuf(); |
| 172 // Modify buffer to exclude its last 'free' box | 172 // Modify buffer to exclude its last 'free' box |
| 173 buf[3] = 0x38; | 173 buf[3] = 0x38; |
| 174 bool err; | 174 bool err; |
| 175 scoped_ptr<BoxReader> reader( | 175 scoped_ptr<BoxReader> reader( |
| 176 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), LogCB(), &err)); | 176 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), new MediaLog(), &err)); |
| 177 | 177 |
| 178 std::vector<PsshBox> kids; | 178 std::vector<PsshBox> kids; |
| 179 EXPECT_TRUE(reader->SkipBytes(16) && reader->ReadAllChildren(&kids)); | 179 EXPECT_TRUE(reader->SkipBytes(16) && reader->ReadAllChildren(&kids)); |
| 180 EXPECT_EQ(2u, kids.size()); | 180 EXPECT_EQ(2u, kids.size()); |
| 181 EXPECT_EQ(kids[0].val, 0xdeadbeef); // Ensure order is preserved | 181 EXPECT_EQ(kids[0].val, 0xdeadbeef); // Ensure order is preserved |
| 182 } | 182 } |
| 183 | 183 |
| 184 static void TestTopLevelBox(const uint8* data, int size, uint32 fourCC) { | 184 static void TestTopLevelBox(const uint8* data, int size, uint32 fourCC) { |
| 185 | 185 |
| 186 std::vector<uint8> buf(data, data + size); | 186 std::vector<uint8> buf(data, data + size); |
| 187 | 187 |
| 188 bool err; | 188 bool err; |
| 189 scoped_ptr<BoxReader> reader( | 189 scoped_ptr<BoxReader> reader( |
| 190 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), LogCB(), &err)); | 190 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), new MediaLog(), &err)); |
| 191 | 191 |
| 192 EXPECT_FALSE(err); | 192 EXPECT_FALSE(err); |
| 193 EXPECT_TRUE(reader); | 193 EXPECT_TRUE(reader); |
| 194 EXPECT_EQ(fourCC, reader->type()); | 194 EXPECT_EQ(fourCC, reader->type()); |
| 195 EXPECT_EQ(reader->size(), size); | 195 EXPECT_EQ(reader->size(), size); |
| 196 } | 196 } |
| 197 | 197 |
| 198 TEST_F(BoxReaderTest, SkippingBloc) { | 198 TEST_F(BoxReaderTest, SkippingBloc) { |
| 199 static const uint8 kData[] = { | 199 static const uint8 kData[] = { |
| 200 0x00, 0x00, 0x00, 0x09, 'b', 'l', 'o', 'c', 0x00 | 200 0x00, 0x00, 0x00, 0x09, 'b', 'l', 'o', 'c', 0x00 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 226 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, | 226 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 227 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, // usertype | 227 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, // usertype |
| 228 0x00, | 228 0x00, |
| 229 }; | 229 }; |
| 230 | 230 |
| 231 TestTopLevelBox(kData, sizeof(kData), FOURCC_UUID); | 231 TestTopLevelBox(kData, sizeof(kData), FOURCC_UUID); |
| 232 } | 232 } |
| 233 | 233 |
| 234 } // namespace mp4 | 234 } // namespace mp4 |
| 235 } // namespace media | 235 } // namespace media |
| OLD | NEW |