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.
7 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/time/time.h"
15 #include "chromecast/media/cma/backend/audio_pipeline_device.h"
16 #include "chromecast/media/cma/backend/media_clock_device.h"
17 #include "chromecast/media/cma/backend/media_pipeline_device.h"
18 #include "chromecast/media/cma/backend/media_pipeline_device_default.h"
19 #include "chromecast/media/cma/base/buffering_controller.h"
20 #include "chromecast/media/cma/base/decoder_buffer_base.h"
21 #include "chromecast/media/cma/pipeline/audio_pipeline_impl.h"
22 #include "chromecast/media/cma/pipeline/av_pipeline_client.h"
23 #include "chromecast/media/cma/pipeline/media_pipeline_impl.h"
24 #include "chromecast/media/cma/pipeline/video_pipeline_client.h"
25 #include "chromecast/media/cma/pipeline/video_pipeline_impl.h"
26 #include "chromecast/media/cma/test/frame_generator_for_test.h"
27 #include "chromecast/media/cma/test/mock_frame_provider.h"
28 #include "media/base/audio_decoder_config.h"
29 #include "media/base/buffers.h"
30 #include "media/base/decoder_buffer.h"
31 #include "media/base/video_decoder_config.h"
32 #include "testing/gtest/include/gtest/gtest.h"
34 namespace chromecast
{
37 class AudioVideoPipelineImplTest
: public testing::Test
{
39 AudioVideoPipelineImplTest();
40 ~AudioVideoPipelineImplTest() override
;
42 void Initialize(const base::Closure
& done_cb
,
43 ::media::PipelineStatus status
,
45 void StartPlaying(const base::Closure
& done_cb
,
46 ::media::PipelineStatus status
);
48 void Flush(const base::Closure
& done_cb
, ::media::PipelineStatus status
);
49 void Stop(const base::Closure
& done_cb
, ::media::PipelineStatus status
);
53 base::Closure task_after_eos_cb_
;
56 scoped_ptr
<MediaPipelineImpl
> media_pipeline_
;
58 DISALLOW_COPY_AND_ASSIGN(AudioVideoPipelineImplTest
);
61 AudioVideoPipelineImplTest::AudioVideoPipelineImplTest()
62 : media_pipeline_(new MediaPipelineImpl()) {
63 scoped_ptr
<MediaPipelineDevice
> media_pipeline_device(
64 new MediaPipelineDeviceDefault());
65 media_pipeline_
->Initialize(kLoadTypeURL
, media_pipeline_device
.Pass());
66 media_pipeline_
->SetPlaybackRate(1.0);
69 AudioVideoPipelineImplTest::~AudioVideoPipelineImplTest() {
72 void AudioVideoPipelineImplTest::Initialize(
73 const base::Closure
& done_cb
,
74 ::media::PipelineStatus status
,
77 AvPipelineClient client
;
79 base::Bind(&AudioVideoPipelineImplTest::OnEos
, base::Unretained(this));
80 media_pipeline_
->GetAudioPipeline()->SetClient(client
);
82 VideoPipelineClient client
;
83 client
.av_pipeline_client
.eos_cb
=
84 base::Bind(&AudioVideoPipelineImplTest::OnEos
, base::Unretained(this));
85 media_pipeline_
->GetVideoPipeline()->SetClient(client
);
88 ::media::AudioDecoderConfig
audio_config(
90 ::media::kSampleFormatS16
,
91 ::media::CHANNEL_LAYOUT_STEREO
,
94 std::vector
<::media::VideoDecoderConfig
> video_configs
;
95 video_configs
.push_back(::media::VideoDecoderConfig(
97 ::media::H264PROFILE_MAIN
,
98 ::media::VideoFrame::I420
,
100 gfx::Rect(0, 0, 640, 480),
104 // Frame generation on the producer side.
105 std::vector
<FrameGeneratorForTest::FrameSpec
> frame_specs
;
106 frame_specs
.resize(100);
107 for (size_t k
= 0; k
< frame_specs
.size() - 1; k
++) {
108 frame_specs
[k
].has_config
= (k
== 0);
109 frame_specs
[k
].timestamp
= base::TimeDelta::FromMilliseconds(40) * k
;
110 frame_specs
[k
].size
= 512;
111 frame_specs
[k
].has_decrypt_config
= false;
113 frame_specs
[frame_specs
.size() - 1].is_eos
= true;
115 scoped_ptr
<FrameGeneratorForTest
> frame_generator_provider(
116 new FrameGeneratorForTest(frame_specs
));
117 bool provider_delayed_pattern
[] = { true, false };
118 scoped_ptr
<MockFrameProvider
> frame_provider(new MockFrameProvider());
119 frame_provider
->Configure(
121 provider_delayed_pattern
,
122 provider_delayed_pattern
+ arraysize(provider_delayed_pattern
)),
123 frame_generator_provider
.Pass());
125 ::media::PipelineStatusCB next_task
=
126 base::Bind(&AudioVideoPipelineImplTest::StartPlaying
,
127 base::Unretained(this),
130 scoped_ptr
<CodedFrameProvider
> frame_provider_base(frame_provider
.release());
131 base::Closure task
= is_audio
?
132 base::Bind(&MediaPipeline::InitializeAudio
,
133 base::Unretained(media_pipeline_
.get()),
135 base::Passed(&frame_provider_base
),
137 base::Bind(&MediaPipeline::InitializeVideo
,
138 base::Unretained(media_pipeline_
.get()),
140 base::Passed(&frame_provider_base
),
143 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, task
);
146 void AudioVideoPipelineImplTest::StartPlaying(
147 const base::Closure
& done_cb
, ::media::PipelineStatus status
) {
148 base::TimeDelta start_time
= base::TimeDelta::FromMilliseconds(0);
150 media_pipeline_
->StartPlayingFrom(start_time
);
151 if (!done_cb
.is_null())
155 void AudioVideoPipelineImplTest::OnEos() {
156 task_after_eos_cb_
.Run();
159 void AudioVideoPipelineImplTest::Flush(
160 const base::Closure
& done_cb
, ::media::PipelineStatus status
) {
161 ::media::PipelineStatusCB next_task
=
162 base::Bind(&AudioVideoPipelineImplTest::Stop
, base::Unretained(this),
164 base::ThreadTaskRunnerHandle::Get()->PostTask(
166 base::Bind(&MediaPipeline::Flush
, base::Unretained(media_pipeline_
.get()),
170 void AudioVideoPipelineImplTest::Stop(
171 const base::Closure
& done_cb
, ::media::PipelineStatus status
) {
172 media_pipeline_
->Stop();
173 if (!done_cb
.is_null())
175 base::MessageLoop::current()->QuitWhenIdle();
179 TEST_F(AudioVideoPipelineImplTest
, AudioFullCycleInitToStop
) {
180 bool is_audio
= true;
181 task_after_eos_cb_
= base::Bind(
182 &AudioVideoPipelineImplTest::Flush
, base::Unretained(this),
183 base::Closure(), ::media::PIPELINE_OK
);
185 scoped_ptr
<base::MessageLoop
> message_loop(new base::MessageLoop());
186 message_loop
->PostTask(
188 base::Bind(&AudioVideoPipelineImplTest::Initialize
,
189 base::Unretained(this),
191 ::media::PIPELINE_OK
, is_audio
));
195 TEST_F(AudioVideoPipelineImplTest
, VideoFullCycleInitToStop
) {
196 bool is_audio
= false;
197 task_after_eos_cb_
= base::Bind(
198 &AudioVideoPipelineImplTest::Flush
, base::Unretained(this),
199 base::Closure(), ::media::PIPELINE_OK
);
201 scoped_ptr
<base::MessageLoop
> message_loop(new base::MessageLoop());
202 message_loop
->PostTask(
204 base::Bind(&AudioVideoPipelineImplTest::Initialize
,
205 base::Unretained(this),
207 ::media::PIPELINE_OK
, is_audio
));
212 } // namespace chromecast