1 // Copyright (c) 2012 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/buffers.h" 6 #include "media/base/decoder_buffer.h" 7 #include "media/base/decoder_buffer_queue.h" 8 #include "testing/gtest/include/gtest/gtest.h" 9 10 namespace media { 11 12 static base::TimeDelta ToTimeDelta(int seconds) { 13 if (seconds < 0) 14 return kNoTimestamp(); 15 return base::TimeDelta::FromSeconds(seconds); 16 } 17 18 // Helper to create buffers with specified timestamp in seconds. 19 // 20 // Negative numbers will be converted to kNoTimestamp(); 21 static scoped_refptr<DecoderBuffer> CreateBuffer(int timestamp) { 22 scoped_refptr<DecoderBuffer> buffer = new DecoderBuffer(0); 23 buffer->set_timestamp(ToTimeDelta(timestamp)); 24 buffer->set_duration(ToTimeDelta(0)); 25 return buffer; 26 } 27 28 static scoped_refptr<DecoderBuffer> CreateBuffer(int timestamp, int size) { 29 scoped_refptr<DecoderBuffer> buffer = new DecoderBuffer(size); 30 buffer->set_timestamp(ToTimeDelta(timestamp)); 31 buffer->set_duration(ToTimeDelta(0)); 32 return buffer; 33 } 34 35 TEST(DecoderBufferQueueTest, IsEmpty) { 36 DecoderBufferQueue queue; 37 EXPECT_TRUE(queue.IsEmpty()); 38 39 queue.Push(CreateBuffer(0)); 40 EXPECT_FALSE(queue.IsEmpty()); 41 } 42 43 TEST(DecoderBufferQueueTest, Clear) { 44 DecoderBufferQueue queue; 45 queue.Push(CreateBuffer(0)); 46 queue.Push(CreateBuffer(1)); 47 EXPECT_FALSE(queue.IsEmpty()); 48 EXPECT_EQ(1, queue.Duration().InSeconds()); 49 50 queue.Clear(); 51 EXPECT_TRUE(queue.IsEmpty()); 52 EXPECT_EQ(0, queue.Duration().InSeconds()); 53 } 54 55 TEST(DecoderBufferQueueTest, Duration) { 56 DecoderBufferQueue queue; 57 EXPECT_EQ(0, queue.Duration().InSeconds()); 58 59 queue.Push(CreateBuffer(0)); 60 EXPECT_EQ(0, queue.Duration().InSeconds()); 61 62 queue.Push(CreateBuffer(1)); 63 EXPECT_EQ(1, queue.Duration().InSeconds()); 64 65 queue.Push(CreateBuffer(2)); 66 EXPECT_EQ(2, queue.Duration().InSeconds()); 67 68 queue.Push(CreateBuffer(4)); 69 EXPECT_EQ(4, queue.Duration().InSeconds()); 70 71 queue.Pop(); 72 EXPECT_EQ(3, queue.Duration().InSeconds()); 73 74 queue.Pop(); 75 EXPECT_EQ(2, queue.Duration().InSeconds()); 76 77 queue.Pop(); 78 EXPECT_EQ(0, queue.Duration().InSeconds()); 79 80 queue.Pop(); 81 EXPECT_EQ(0, queue.Duration().InSeconds()); 82 } 83 84 TEST(DecoderBufferQueueTest, Duration_OutOfOrder) { 85 DecoderBufferQueue queue; 86 queue.Push(CreateBuffer(10)); 87 queue.Push(CreateBuffer(12)); 88 EXPECT_EQ(2, queue.Duration().InSeconds()); 89 90 // Out of order: duration shouldn't change. 91 queue.Push(CreateBuffer(8)); 92 EXPECT_EQ(2, queue.Duration().InSeconds()); 93 94 // Removing first buffer should leave the second buffer as the only buffer 95 // included in the duration calculation. 96 queue.Pop(); 97 EXPECT_EQ(0, queue.Duration().InSeconds()); 98 99 // Removing second buffer leaves the out-of-order buffer. It shouldn't be 100 // included in duration calculations. 101 queue.Pop(); 102 EXPECT_EQ(0, queue.Duration().InSeconds()); 103 104 // Push a still-too-early buffer. It shouldn't be included in duration 105 // calculations. 106 queue.Push(CreateBuffer(11)); 107 EXPECT_EQ(0, queue.Duration().InSeconds()); 108 109 // Push a buffer that's after the earliest valid time. It's a singular valid 110 // buffer so duration is still zero. 111 queue.Push(CreateBuffer(14)); 112 EXPECT_EQ(0, queue.Duration().InSeconds()); 113 114 // Push a second valid buffer. We should now have a duration. 115 queue.Push(CreateBuffer(17)); 116 EXPECT_EQ(3, queue.Duration().InSeconds()); 117 } 118 119 TEST(DecoderBufferQueueTest, Duration_NoTimestamp) { 120 // Buffers with no timestamp don't affect duration. 121 DecoderBufferQueue queue; 122 queue.Push(CreateBuffer(0)); 123 queue.Push(CreateBuffer(4)); 124 EXPECT_EQ(4, queue.Duration().InSeconds()); 125 126 queue.Push(CreateBuffer(-1)); 127 EXPECT_EQ(4, queue.Duration().InSeconds()); 128 129 queue.Push(CreateBuffer(6)); 130 EXPECT_EQ(6, queue.Duration().InSeconds()); 131 132 queue.Pop(); 133 EXPECT_EQ(2, queue.Duration().InSeconds()); 134 135 queue.Pop(); 136 EXPECT_EQ(0, queue.Duration().InSeconds()); 137 138 queue.Pop(); 139 EXPECT_EQ(0, queue.Duration().InSeconds()); 140 141 queue.Pop(); 142 EXPECT_EQ(0, queue.Duration().InSeconds()); 143 } 144 145 TEST(DecoderBufferQueueTest, DataSize) { 146 DecoderBufferQueue queue; 147 EXPECT_EQ(queue.data_size(), 0u); 148 149 queue.Push(CreateBuffer(0, 1200u)); 150 EXPECT_EQ(queue.data_size(), 1200u); 151 152 queue.Push(CreateBuffer(1, 1000u)); 153 EXPECT_EQ(queue.data_size(), 2200u); 154 155 queue.Pop(); 156 EXPECT_EQ(queue.data_size(), 1000u); 157 158 queue.Push(CreateBuffer(2, 999u)); 159 queue.Push(CreateBuffer(3, 999u)); 160 EXPECT_EQ(queue.data_size(), 2998u); 161 162 queue.Clear(); 163 EXPECT_EQ(queue.data_size(), 0u); 164 165 queue.Push(CreateBuffer(4, 1400u)); 166 EXPECT_EQ(queue.data_size(), 1400u); 167 } 168 169 } // namespace media 170