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
->SetTimestamp(ToTimeDelta(timestamp
));
24 buffer
->SetDuration(ToTimeDelta(0));
28 TEST(DecoderBufferQueueTest
, IsEmpty
) {
29 DecoderBufferQueue queue
;
30 EXPECT_TRUE(queue
.IsEmpty());
32 queue
.Push(CreateBuffer(0));
33 EXPECT_FALSE(queue
.IsEmpty());
36 TEST(DecoderBufferQueueTest
, Clear
) {
37 DecoderBufferQueue queue
;
38 queue
.Push(CreateBuffer(0));
39 queue
.Push(CreateBuffer(1));
40 EXPECT_FALSE(queue
.IsEmpty());
41 EXPECT_EQ(1, queue
.Duration().InSeconds());
44 EXPECT_TRUE(queue
.IsEmpty());
45 EXPECT_EQ(0, queue
.Duration().InSeconds());
48 TEST(DecoderBufferQueueTest
, Duration
) {
49 DecoderBufferQueue queue
;
50 EXPECT_EQ(0, queue
.Duration().InSeconds());
52 queue
.Push(CreateBuffer(0));
53 EXPECT_EQ(0, queue
.Duration().InSeconds());
55 queue
.Push(CreateBuffer(1));
56 EXPECT_EQ(1, queue
.Duration().InSeconds());
58 queue
.Push(CreateBuffer(2));
59 EXPECT_EQ(2, queue
.Duration().InSeconds());
61 queue
.Push(CreateBuffer(4));
62 EXPECT_EQ(4, queue
.Duration().InSeconds());
65 EXPECT_EQ(3, queue
.Duration().InSeconds());
68 EXPECT_EQ(2, queue
.Duration().InSeconds());
71 EXPECT_EQ(0, queue
.Duration().InSeconds());
74 EXPECT_EQ(0, queue
.Duration().InSeconds());
77 TEST(DecoderBufferQueueTest
, Duration_OutOfOrder
) {
78 DecoderBufferQueue queue
;
79 queue
.Push(CreateBuffer(10));
80 queue
.Push(CreateBuffer(12));
81 EXPECT_EQ(2, queue
.Duration().InSeconds());
83 // Out of order: duration shouldn't change.
84 queue
.Push(CreateBuffer(8));
85 EXPECT_EQ(2, queue
.Duration().InSeconds());
87 // Removing first buffer should leave the second buffer as the only buffer
88 // included in the duration calculation.
90 EXPECT_EQ(0, queue
.Duration().InSeconds());
92 // Removing second buffer leaves the out-of-order buffer. It shouldn't be
93 // included in duration calculations.
95 EXPECT_EQ(0, queue
.Duration().InSeconds());
97 // Push a still-too-early buffer. It shouldn't be included in duration
99 queue
.Push(CreateBuffer(11));
100 EXPECT_EQ(0, queue
.Duration().InSeconds());
102 // Push a buffer that's after the earliest valid time. It's a singular valid
103 // buffer so duration is still zero.
104 queue
.Push(CreateBuffer(14));
105 EXPECT_EQ(0, queue
.Duration().InSeconds());
107 // Push a second valid buffer. We should now have a duration.
108 queue
.Push(CreateBuffer(17));
109 EXPECT_EQ(3, queue
.Duration().InSeconds());
112 TEST(DecoderBufferQueueTest
, Duration_NoTimestamp
) {
113 // Buffers with no timestamp don't affect duration.
114 DecoderBufferQueue queue
;
115 queue
.Push(CreateBuffer(0));
116 queue
.Push(CreateBuffer(4));
117 EXPECT_EQ(4, queue
.Duration().InSeconds());
119 queue
.Push(CreateBuffer(-1));
120 EXPECT_EQ(4, queue
.Duration().InSeconds());
122 queue
.Push(CreateBuffer(6));
123 EXPECT_EQ(6, queue
.Duration().InSeconds());
126 EXPECT_EQ(2, queue
.Duration().InSeconds());
129 EXPECT_EQ(0, queue
.Duration().InSeconds());
132 EXPECT_EQ(0, queue
.Duration().InSeconds());
135 EXPECT_EQ(0, queue
.Duration().InSeconds());