1 // Copyright 2014 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.
8 #include "base/basictypes.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/threading/thread.h"
13 #include "base/time/time.h"
14 #include "chromecast/media/cma/base/buffering_frame_provider.h"
15 #include "chromecast/media/cma/test/frame_generator_for_test.h"
16 #include "chromecast/media/cma/test/mock_frame_consumer.h"
17 #include "chromecast/media/cma/test/mock_frame_provider.h"
18 #include "chromecast/public/media/cast_decoder_buffer.h"
19 #include "media/base/audio_decoder_config.h"
20 #include "media/base/decoder_buffer.h"
21 #include "media/base/video_decoder_config.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 namespace chromecast
{
27 class BufferingFrameProviderTest
: public testing::Test
{
29 BufferingFrameProviderTest();
30 ~BufferingFrameProviderTest() override
;
35 const std::vector
<bool>& provider_delayed_pattern
,
36 const std::vector
<bool>& consumer_delayed_pattern
);
42 scoped_ptr
<BufferingFrameProvider
> buffering_frame_provider_
;
43 scoped_ptr
<MockFrameConsumer
> frame_consumer_
;
47 void OnTestCompleted();
49 DISALLOW_COPY_AND_ASSIGN(BufferingFrameProviderTest
);
52 BufferingFrameProviderTest::BufferingFrameProviderTest() {
55 BufferingFrameProviderTest::~BufferingFrameProviderTest() {
58 void BufferingFrameProviderTest::Configure(
60 const std::vector
<bool>& provider_delayed_pattern
,
61 const std::vector
<bool>& consumer_delayed_pattern
) {
62 DCHECK_GE(frame_count
, 1u);
64 // Frame generation on the producer and consumer side.
65 std::vector
<FrameGeneratorForTest::FrameSpec
> frame_specs(frame_count
);
66 for (size_t k
= 0; k
< frame_specs
.size() - 1; k
++) {
67 frame_specs
[k
].has_config
= (k
== 0);
68 frame_specs
[k
].timestamp
= base::TimeDelta::FromMilliseconds(40) * k
;
69 frame_specs
[k
].size
= 512;
70 frame_specs
[k
].has_decrypt_config
= ((k
% 3) == 0);
72 frame_specs
[frame_specs
.size() - 1].is_eos
= true;
74 scoped_ptr
<FrameGeneratorForTest
> frame_generator_provider(
75 new FrameGeneratorForTest(frame_specs
));
76 scoped_ptr
<FrameGeneratorForTest
> frame_generator_consumer(
77 new FrameGeneratorForTest(frame_specs
));
79 scoped_ptr
<MockFrameProvider
> frame_provider(new MockFrameProvider());
80 frame_provider
->Configure(provider_delayed_pattern
,
81 frame_generator_provider
.Pass());
83 size_t max_frame_size
= 10 * 1024;
84 size_t buffer_size
= 10 * max_frame_size
;
85 buffering_frame_provider_
.reset(
86 new BufferingFrameProvider(
87 scoped_ptr
<CodedFrameProvider
>(frame_provider
.release()),
90 BufferingFrameProvider::FrameBufferedCB()));
92 frame_consumer_
.reset(
93 new MockFrameConsumer(buffering_frame_provider_
.get()));
94 frame_consumer_
->Configure(
95 consumer_delayed_pattern
,
97 frame_generator_consumer
.Pass());
100 void BufferingFrameProviderTest::Start() {
101 frame_consumer_
->Start(
102 base::Bind(&BufferingFrameProviderTest::OnTestCompleted
,
103 base::Unretained(this)));
106 void BufferingFrameProviderTest::OnTestTimeout() {
107 ADD_FAILURE() << "Test timed out";
108 if (base::MessageLoop::current())
109 base::MessageLoop::current()->QuitWhenIdle();
112 void BufferingFrameProviderTest::OnTestCompleted() {
113 base::MessageLoop::current()->QuitWhenIdle();
116 TEST_F(BufferingFrameProviderTest
, FastProviderSlowConsumer
) {
117 bool provider_delayed_pattern
[] = { false };
118 bool consumer_delayed_pattern
[] = { true };
120 const size_t frame_count
= 100u;
124 provider_delayed_pattern
,
125 provider_delayed_pattern
+ arraysize(provider_delayed_pattern
)),
127 consumer_delayed_pattern
,
128 consumer_delayed_pattern
+ arraysize(consumer_delayed_pattern
)));
130 scoped_ptr
<base::MessageLoop
> message_loop(new base::MessageLoop());
131 message_loop
->PostTask(
133 base::Bind(&BufferingFrameProviderTest::Start
, base::Unretained(this)));
137 TEST_F(BufferingFrameProviderTest
, SlowProviderFastConsumer
) {
138 bool provider_delayed_pattern
[] = { true };
139 bool consumer_delayed_pattern
[] = { false };
141 const size_t frame_count
= 100u;
145 provider_delayed_pattern
,
146 provider_delayed_pattern
+ arraysize(provider_delayed_pattern
)),
148 consumer_delayed_pattern
,
149 consumer_delayed_pattern
+ arraysize(consumer_delayed_pattern
)));
151 scoped_ptr
<base::MessageLoop
> message_loop(new base::MessageLoop());
152 message_loop
->PostTask(
154 base::Bind(&BufferingFrameProviderTest::Start
, base::Unretained(this)));
158 TEST_F(BufferingFrameProviderTest
, SlowFastProducerConsumer
) {
159 // Lengths are prime between each other so we can test a lot of combinations.
160 bool provider_delayed_pattern
[] = {
161 true, true, true, true, true,
162 false, false, false, false
164 bool consumer_delayed_pattern
[] = {
165 true, true, true, true, true, true, true,
166 false, false, false, false, false, false, false
169 const size_t frame_count
= 100u;
173 provider_delayed_pattern
,
174 provider_delayed_pattern
+ arraysize(provider_delayed_pattern
)),
176 consumer_delayed_pattern
,
177 consumer_delayed_pattern
+ arraysize(consumer_delayed_pattern
)));
179 scoped_ptr
<base::MessageLoop
> message_loop(new base::MessageLoop());
180 message_loop
->PostTask(
182 base::Bind(&BufferingFrameProviderTest::Start
, base::Unretained(this)));
187 } // namespace chromecast