| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/base/android/access_unit_queue.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 |
| 8 #define ARRAY_SIZE(x) (int)(sizeof(x) / sizeof(x[0])) |
| 9 |
| 10 namespace media { |
| 11 |
| 12 class AccessUnitQueueTest : public testing::Test { |
| 13 public: |
| 14 AccessUnitQueueTest() {} |
| 15 ~AccessUnitQueueTest() override {} |
| 16 |
| 17 protected: |
| 18 enum Flags { kNone = 0, kKeyFrame = 1, kEOS = 2, kConfig = 4 }; |
| 19 struct AUDescriptor { |
| 20 int flags; |
| 21 std::string data; |
| 22 }; |
| 23 |
| 24 DemuxerData CreateDemuxerData(const AUDescriptor* descr, int descr_length); |
| 25 }; |
| 26 |
| 27 DemuxerData AccessUnitQueueTest::CreateDemuxerData(const AUDescriptor* descr, |
| 28 int descr_length) { |
| 29 DemuxerData result; |
| 30 result.type = DemuxerStream::AUDIO; // assign a valid type |
| 31 |
| 32 for (int i = 0; i < descr_length; ++i) { |
| 33 result.access_units.push_back(AccessUnit()); |
| 34 AccessUnit& au = result.access_units.back(); |
| 35 au.status = (descr[i].flags & kConfig) ? DemuxerStream::kConfigChanged |
| 36 : DemuxerStream::kOk; |
| 37 if (au.status == DemuxerStream::kConfigChanged) { |
| 38 result.demuxer_configs.push_back(DemuxerConfigs()); |
| 39 continue; |
| 40 } |
| 41 |
| 42 au.data = std::vector<uint8>(descr[i].data.begin(), descr[i].data.end()); |
| 43 au.is_key_frame = (descr[i].flags & kKeyFrame); |
| 44 au.is_end_of_stream = (descr[i].flags & kEOS); |
| 45 } |
| 46 return result; |
| 47 } |
| 48 |
| 49 #define VERIFY_FIRST_BYTE(expected, info) \ |
| 50 do { \ |
| 51 EXPECT_NE(nullptr, info.front_unit); \ |
| 52 EXPECT_TRUE(info.front_unit->data.size() > 0); \ |
| 53 EXPECT_EQ(expected, info.front_unit->data[0]); \ |
| 54 } while (0) |
| 55 |
| 56 TEST_F(AccessUnitQueueTest, InitializedEmpty) { |
| 57 AccessUnitQueue au_queue; |
| 58 AccessUnitQueue::Info info = au_queue.GetInfo(); |
| 59 |
| 60 EXPECT_EQ(0, info.length); |
| 61 EXPECT_FALSE(info.has_eos); |
| 62 EXPECT_EQ(nullptr, info.front_unit); |
| 63 EXPECT_EQ(nullptr, info.configs); |
| 64 } |
| 65 |
| 66 TEST_F(AccessUnitQueueTest, SkipToKeyFrameEmptyQueue) { |
| 67 AccessUnitQueue au_queue; |
| 68 EXPECT_FALSE(au_queue.SkipToKeyFrame()); |
| 69 } |
| 70 |
| 71 TEST_F(AccessUnitQueueTest, PushAndAdvance) { |
| 72 AUDescriptor chunk1[] = {{kNone, "0"}, |
| 73 {kNone, "1"}, |
| 74 {kNone, "2"}, |
| 75 {kNone, "3"}, |
| 76 {kNone, "4"}, |
| 77 {kNone, "5"}}; |
| 78 AUDescriptor chunk2[] = {{kNone, "6"}, {kNone, "7"}, {kNone, "8"}}; |
| 79 |
| 80 int total_size = ARRAY_SIZE(chunk1) + ARRAY_SIZE(chunk2); |
| 81 |
| 82 AccessUnitQueue au_queue; |
| 83 au_queue.PushBack(CreateDemuxerData(chunk1, ARRAY_SIZE(chunk1))); |
| 84 au_queue.PushBack(CreateDemuxerData(chunk2, ARRAY_SIZE(chunk2))); |
| 85 |
| 86 AccessUnitQueue::Info info; |
| 87 for (int i = 0; i < total_size; ++i) { |
| 88 info = au_queue.GetInfo(); |
| 89 |
| 90 EXPECT_FALSE(info.has_eos); |
| 91 EXPECT_EQ(total_size - i, info.length); |
| 92 EXPECT_EQ(nullptr, info.configs); |
| 93 |
| 94 EXPECT_NE(nullptr, info.front_unit); |
| 95 EXPECT_TRUE(info.front_unit->data.size() > 0); |
| 96 EXPECT_EQ('0' + i, info.front_unit->data[0]); |
| 97 |
| 98 au_queue.Advance(); |
| 99 } |
| 100 |
| 101 // After we advanced past the last AU, GetInfo() should report starvation. |
| 102 info = au_queue.GetInfo(); |
| 103 |
| 104 EXPECT_EQ(0, info.length); |
| 105 EXPECT_FALSE(info.has_eos); |
| 106 EXPECT_EQ(nullptr, info.front_unit); |
| 107 EXPECT_EQ(nullptr, info.configs); |
| 108 } |
| 109 |
| 110 TEST_F(AccessUnitQueueTest, ChunksDoNotLeak) { |
| 111 AUDescriptor chunk[] = { |
| 112 {kNone, "0"}, {kNone, "1"}, {kNone, "2"}, {kNone, "3"}}; |
| 113 |
| 114 AccessUnitQueue au_queue; |
| 115 au_queue.PushBack(CreateDemuxerData(chunk, ARRAY_SIZE(chunk))); |
| 116 |
| 117 // Verify that the old chunks get deleted (we rely on NumChunksForTesting()) |
| 118 for (int i = 0; i < 100; ++i) { |
| 119 au_queue.PushBack(CreateDemuxerData(chunk, ARRAY_SIZE(chunk))); |
| 120 for (int j = 0; j < ARRAY_SIZE(chunk); ++j) |
| 121 au_queue.Advance(); |
| 122 |
| 123 // 5 is an arbitrary number, it implies that we keep 4 chunks of history. |
| 124 EXPECT_GT(5U, au_queue.NumChunksForTesting()); |
| 125 } |
| 126 } |
| 127 |
| 128 TEST_F(AccessUnitQueueTest, PushAfterStarvation) { |
| 129 // Two chunks |
| 130 AUDescriptor chunk[][4] = { |
| 131 {{kNone, "0"}, {kNone, "1"}, {kNone, "2"}, {kNone, "3"}}, |
| 132 {{kNone, "4"}, {kNone, "5"}, {kNone, "6"}, {kNone, "7"}}}; |
| 133 |
| 134 AccessUnitQueue au_queue; |
| 135 |
| 136 // Push the first chunk. |
| 137 au_queue.PushBack(CreateDemuxerData(chunk[0], ARRAY_SIZE(chunk[0]))); |
| 138 |
| 139 // Advance past the end of queue. |
| 140 for (int i = 0; i < ARRAY_SIZE(chunk[0]); ++i) |
| 141 au_queue.Advance(); |
| 142 |
| 143 // An extra Advance() should not change anything. |
| 144 au_queue.Advance(); |
| 145 |
| 146 // Push the second chunk |
| 147 au_queue.PushBack(CreateDemuxerData(chunk[1], ARRAY_SIZE(chunk[1]))); |
| 148 |
| 149 // Verify that we get the next access unit. |
| 150 AccessUnitQueue::Info info = au_queue.GetInfo(); |
| 151 VERIFY_FIRST_BYTE('4', info); |
| 152 } |
| 153 |
| 154 TEST_F(AccessUnitQueueTest, HasEOS) { |
| 155 // Two chunks |
| 156 AUDescriptor chunk[][4] = { |
| 157 {{kNone, "0"}, {kNone, "1"}, {kNone, "2"}, {kNone, "3"}}, |
| 158 {{kNone, "4"}, {kNone, "5"}, {kNone, "6"}, {kEOS, "7"}}}; |
| 159 |
| 160 AccessUnitQueue au_queue; |
| 161 au_queue.PushBack(CreateDemuxerData(chunk[0], ARRAY_SIZE(chunk[0]))); |
| 162 au_queue.PushBack(CreateDemuxerData(chunk[1], ARRAY_SIZE(chunk[1]))); |
| 163 |
| 164 // Verify that after EOS has been pushed into the queue, |
| 165 // it is reported for every GetInfo() |
| 166 for (int i = 0; i < 8; ++i) { |
| 167 AccessUnitQueue::Info info = au_queue.GetInfo(); |
| 168 |
| 169 EXPECT_TRUE(info.has_eos); |
| 170 EXPECT_EQ(nullptr, info.configs); |
| 171 |
| 172 VERIFY_FIRST_BYTE('0' + i, info); |
| 173 |
| 174 au_queue.Advance(); |
| 175 } |
| 176 } |
| 177 |
| 178 TEST_F(AccessUnitQueueTest, HasConfigs) { |
| 179 AUDescriptor chunk[] = { |
| 180 {kNone, "0"}, {kNone, "1"}, {kNone, "2"}, {kConfig, "3"}}; |
| 181 |
| 182 AccessUnitQueue au_queue; |
| 183 au_queue.PushBack(CreateDemuxerData(chunk, ARRAY_SIZE(chunk))); |
| 184 |
| 185 for (int i = 0; i < 4; ++i) { |
| 186 AccessUnitQueue::Info info = au_queue.GetInfo(); |
| 187 |
| 188 if (i != 3) |
| 189 EXPECT_EQ(nullptr, info.configs); |
| 190 else |
| 191 EXPECT_NE(nullptr, info.configs); |
| 192 |
| 193 au_queue.Advance(); |
| 194 } |
| 195 } |
| 196 |
| 197 TEST_F(AccessUnitQueueTest, ConfigsAndKeyFrame) { |
| 198 // Two chunks |
| 199 AUDescriptor chunk[][4] = { |
| 200 {{kNone, "0"}, {kKeyFrame, "1"}, {kNone, "2"}, {kConfig, "3"}}, |
| 201 {{kKeyFrame, "4"}, {kNone, "5"}, {kNone, "6"}, {kNone, "7"}}}; |
| 202 |
| 203 AccessUnitQueue::Info info; |
| 204 |
| 205 AccessUnitQueue au_queue; |
| 206 au_queue.PushBack(CreateDemuxerData(chunk[0], ARRAY_SIZE(chunk[0]))); |
| 207 au_queue.PushBack(CreateDemuxerData(chunk[1], ARRAY_SIZE(chunk[1]))); |
| 208 |
| 209 // There is no prior key frame |
| 210 EXPECT_FALSE(au_queue.SkipToKeyFrame()); |
| 211 |
| 212 // Consume first access unit. |
| 213 au_queue.Advance(); |
| 214 |
| 215 // Now the current one is the key frame. It would be safe to configure codec |
| 216 // at this moment, so SkipToKeyFrame() should return true. |
| 217 EXPECT_TRUE(au_queue.SkipToKeyFrame()); |
| 218 |
| 219 info = au_queue.GetInfo(); |
| 220 VERIFY_FIRST_BYTE('1', info); |
| 221 |
| 222 au_queue.Advance(); // now current unit is "2" |
| 223 |
| 224 info = au_queue.GetInfo(); |
| 225 VERIFY_FIRST_BYTE('2', info); |
| 226 |
| 227 EXPECT_TRUE(au_queue.SkipToKeyFrame()); // should go back to "1" |
| 228 |
| 229 info = au_queue.GetInfo(); |
| 230 VERIFY_FIRST_BYTE('1', info); |
| 231 |
| 232 au_queue.Advance(); // now current unit is "2" |
| 233 au_queue.Advance(); // now current unit is "3" |
| 234 |
| 235 // Verify that we are at "3". |
| 236 info = au_queue.GetInfo(); |
| 237 EXPECT_NE(nullptr, info.configs); |
| 238 |
| 239 // Although it would be safe to configure codec (with old config) in this |
| 240 // position since it will be immediately reconfigured from the next unit "3", |
| 241 // current implementation returns unit "1". |
| 242 |
| 243 EXPECT_TRUE(au_queue.SkipToKeyFrame()); // should go back to "1" |
| 244 |
| 245 info = au_queue.GetInfo(); |
| 246 VERIFY_FIRST_BYTE('1', info); |
| 247 |
| 248 au_queue.Advance(); // now current unit is "2" |
| 249 au_queue.Advance(); // now current unit is "3" |
| 250 au_queue.Advance(); // now current unit is "4" |
| 251 |
| 252 info = au_queue.GetInfo(); |
| 253 VERIFY_FIRST_BYTE('4', info); |
| 254 |
| 255 EXPECT_TRUE(au_queue.SkipToKeyFrame()); // should stay at "4" |
| 256 |
| 257 info = au_queue.GetInfo(); |
| 258 VERIFY_FIRST_BYTE('4', info); |
| 259 |
| 260 au_queue.Advance(); // now current unit is "5" |
| 261 au_queue.Advance(); // now current unit is "6" |
| 262 |
| 263 info = au_queue.GetInfo(); |
| 264 VERIFY_FIRST_BYTE('6', info); |
| 265 |
| 266 EXPECT_TRUE(au_queue.SkipToKeyFrame()); // should go back to "4" |
| 267 |
| 268 info = au_queue.GetInfo(); |
| 269 VERIFY_FIRST_BYTE('4', info); |
| 270 } |
| 271 |
| 272 } // namespace media |
| OLD | NEW |