12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "media/base/buffers.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "media/base/decoder_buffer.h"
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "media/base/decoder_buffer_queue.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace media {
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static base::TimeDelta ToTimeDelta(int seconds) {
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (seconds < 0)
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return kNoTimestamp();
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return base::TimeDelta::FromSeconds(seconds);
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Helper to create buffers with specified timestamp in seconds.
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Negative numbers will be converted to kNoTimestamp();
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static scoped_refptr<DecoderBuffer> CreateBuffer(int timestamp) {
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_refptr<DecoderBuffer> buffer = new DecoderBuffer(0);
23ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  buffer->set_timestamp(ToTimeDelta(timestamp));
24ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  buffer->set_duration(ToTimeDelta(0));
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return buffer;
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)static scoped_refptr<DecoderBuffer> CreateBuffer(int timestamp, int size) {
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_refptr<DecoderBuffer> buffer = new DecoderBuffer(size);
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  buffer->set_timestamp(ToTimeDelta(timestamp));
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  buffer->set_duration(ToTimeDelta(0));
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return buffer;
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST(DecoderBufferQueueTest, IsEmpty) {
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DecoderBufferQueue queue;
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(queue.IsEmpty());
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Push(CreateBuffer(0));
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(queue.IsEmpty());
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST(DecoderBufferQueueTest, Clear) {
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DecoderBufferQueue queue;
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Push(CreateBuffer(0));
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Push(CreateBuffer(1));
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(queue.IsEmpty());
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(1, queue.Duration().InSeconds());
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Clear();
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(queue.IsEmpty());
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(0, queue.Duration().InSeconds());
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST(DecoderBufferQueueTest, Duration) {
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DecoderBufferQueue queue;
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(0, queue.Duration().InSeconds());
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Push(CreateBuffer(0));
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(0, queue.Duration().InSeconds());
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Push(CreateBuffer(1));
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(1, queue.Duration().InSeconds());
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Push(CreateBuffer(2));
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(2, queue.Duration().InSeconds());
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Push(CreateBuffer(4));
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(4, queue.Duration().InSeconds());
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Pop();
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(3, queue.Duration().InSeconds());
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Pop();
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(2, queue.Duration().InSeconds());
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Pop();
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(0, queue.Duration().InSeconds());
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Pop();
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(0, queue.Duration().InSeconds());
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST(DecoderBufferQueueTest, Duration_OutOfOrder) {
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DecoderBufferQueue queue;
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Push(CreateBuffer(10));
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Push(CreateBuffer(12));
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(2, queue.Duration().InSeconds());
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Out of order: duration shouldn't change.
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Push(CreateBuffer(8));
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(2, queue.Duration().InSeconds());
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Removing first buffer should leave the second buffer as the only buffer
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // included in the duration calculation.
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Pop();
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(0, queue.Duration().InSeconds());
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Removing second buffer leaves the out-of-order buffer. It shouldn't be
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // included in duration calculations.
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Pop();
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(0, queue.Duration().InSeconds());
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Push a still-too-early buffer. It shouldn't be included in duration
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // calculations.
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Push(CreateBuffer(11));
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(0, queue.Duration().InSeconds());
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Push a buffer that's after the earliest valid time. It's a singular valid
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // buffer so duration is still zero.
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Push(CreateBuffer(14));
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(0, queue.Duration().InSeconds());
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Push a second valid buffer. We should now have a duration.
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Push(CreateBuffer(17));
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(3, queue.Duration().InSeconds());
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST(DecoderBufferQueueTest, Duration_NoTimestamp) {
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Buffers with no timestamp don't affect duration.
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DecoderBufferQueue queue;
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Push(CreateBuffer(0));
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Push(CreateBuffer(4));
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(4, queue.Duration().InSeconds());
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Push(CreateBuffer(-1));
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(4, queue.Duration().InSeconds());
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Push(CreateBuffer(6));
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(6, queue.Duration().InSeconds());
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Pop();
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(2, queue.Duration().InSeconds());
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Pop();
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(0, queue.Duration().InSeconds());
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Pop();
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(0, queue.Duration().InSeconds());
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  queue.Pop();
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(0, queue.Duration().InSeconds());
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
145a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST(DecoderBufferQueueTest, DataSize) {
146a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DecoderBufferQueue queue;
147a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(queue.data_size(), 0u);
148a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
149a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  queue.Push(CreateBuffer(0, 1200u));
150a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(queue.data_size(), 1200u);
151a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
152a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  queue.Push(CreateBuffer(1, 1000u));
153a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(queue.data_size(), 2200u);
154a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
155a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  queue.Pop();
156a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(queue.data_size(), 1000u);
157a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
158a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  queue.Push(CreateBuffer(2, 999u));
159a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  queue.Push(CreateBuffer(3, 999u));
160a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(queue.data_size(), 2998u);
161a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
162a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  queue.Clear();
163a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(queue.data_size(), 0u);
164a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
165a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  queue.Push(CreateBuffer(4, 1400u));
166a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(queue.data_size(), 1400u);
167a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
168a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace media
170