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.
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"
12 static base::TimeDelta
ToTimeDelta(int seconds
) {
14 return kNoTimestamp();
15 return base::TimeDelta::FromSeconds(seconds
);
18 // Helper to create buffers with specified timestamp in seconds.
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));
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));
35 TEST(DecoderBufferQueueTest
, IsEmpty
) {
36 DecoderBufferQueue queue
;
37 EXPECT_TRUE(queue
.IsEmpty());
39 queue
.Push(CreateBuffer(0));
40 EXPECT_FALSE(queue
.IsEmpty());
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());
51 EXPECT_TRUE(queue
.IsEmpty());
52 EXPECT_EQ(0, queue
.Duration().InSeconds());
55 TEST(DecoderBufferQueueTest
, Duration
) {
56 DecoderBufferQueue queue
;
57 EXPECT_EQ(0, queue
.Duration().InSeconds());
59 queue
.Push(CreateBuffer(0));
60 EXPECT_EQ(0, queue
.Duration().InSeconds());
62 queue
.Push(CreateBuffer(1));
63 EXPECT_EQ(1, queue
.Duration().InSeconds());
65 queue
.Push(CreateBuffer(2));
66 EXPECT_EQ(2, queue
.Duration().InSeconds());
68 queue
.Push(CreateBuffer(4));
69 EXPECT_EQ(4, queue
.Duration().InSeconds());
72 EXPECT_EQ(3, queue
.Duration().InSeconds());
75 EXPECT_EQ(2, queue
.Duration().InSeconds());
78 EXPECT_EQ(0, queue
.Duration().InSeconds());
81 EXPECT_EQ(0, queue
.Duration().InSeconds());
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());
90 // Out of order: duration shouldn't change.
91 queue
.Push(CreateBuffer(8));
92 EXPECT_EQ(2, queue
.Duration().InSeconds());
94 // Removing first buffer should leave the second buffer as the only buffer
95 // included in the duration calculation.
97 EXPECT_EQ(0, queue
.Duration().InSeconds());
99 // Removing second buffer leaves the out-of-order buffer. It shouldn't be
100 // included in duration calculations.
102 EXPECT_EQ(0, queue
.Duration().InSeconds());
104 // Push a still-too-early buffer. It shouldn't be included in duration
106 queue
.Push(CreateBuffer(11));
107 EXPECT_EQ(0, queue
.Duration().InSeconds());
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());
114 // Push a second valid buffer. We should now have a duration.
115 queue
.Push(CreateBuffer(17));
116 EXPECT_EQ(3, queue
.Duration().InSeconds());
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());
126 queue
.Push(CreateBuffer(-1));
127 EXPECT_EQ(4, queue
.Duration().InSeconds());
129 queue
.Push(CreateBuffer(6));
130 EXPECT_EQ(6, queue
.Duration().InSeconds());
133 EXPECT_EQ(2, queue
.Duration().InSeconds());
136 EXPECT_EQ(0, queue
.Duration().InSeconds());
139 EXPECT_EQ(0, queue
.Duration().InSeconds());
142 EXPECT_EQ(0, queue
.Duration().InSeconds());
145 TEST(DecoderBufferQueueTest
, DataSize
) {
146 DecoderBufferQueue queue
;
147 EXPECT_EQ(queue
.data_size(), 0u);
149 queue
.Push(CreateBuffer(0, 1200u));
150 EXPECT_EQ(queue
.data_size(), 1200u);
152 queue
.Push(CreateBuffer(1, 1000u));
153 EXPECT_EQ(queue
.data_size(), 2200u);
156 EXPECT_EQ(queue
.data_size(), 1000u);
158 queue
.Push(CreateBuffer(2, 999u));
159 queue
.Push(CreateBuffer(3, 999u));
160 EXPECT_EQ(queue
.data_size(), 2998u);
163 EXPECT_EQ(queue
.data_size(), 0u);
165 queue
.Push(CreateBuffer(4, 1400u));
166 EXPECT_EQ(queue
.data_size(), 1400u);