Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chromecast / media / cma / pipeline / audio_video_pipeline_impl_unittest.cc
blob4d716524eee7def3586ed54985cde18f45109c98
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.
5 #include <vector>
7 #include "base/basictypes.h"
8 #include "base/bind.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/base/task_runner_impl.h"
16 #include "chromecast/media/cma/backend/media_pipeline_backend_default.h"
17 #include "chromecast/media/cma/base/buffering_controller.h"
18 #include "chromecast/media/cma/base/decoder_buffer_base.h"
19 #include "chromecast/media/cma/pipeline/audio_pipeline_impl.h"
20 #include "chromecast/media/cma/pipeline/av_pipeline_client.h"
21 #include "chromecast/media/cma/pipeline/media_pipeline_impl.h"
22 #include "chromecast/media/cma/pipeline/video_pipeline_client.h"
23 #include "chromecast/media/cma/pipeline/video_pipeline_impl.h"
24 #include "chromecast/media/cma/test/frame_generator_for_test.h"
25 #include "chromecast/media/cma/test/mock_frame_provider.h"
26 #include "chromecast/public/media/audio_pipeline_device.h"
27 #include "chromecast/public/media/media_clock_device.h"
28 #include "media/base/audio_decoder_config.h"
29 #include "media/base/decoder_buffer.h"
30 #include "media/base/video_decoder_config.h"
31 #include "testing/gtest/include/gtest/gtest.h"
33 namespace chromecast {
34 namespace media {
36 class AudioVideoPipelineImplTest : public testing::Test {
37 public:
38 AudioVideoPipelineImplTest();
39 ~AudioVideoPipelineImplTest() override;
41 void Initialize(const base::Closure& done_cb,
42 ::media::PipelineStatus status,
43 bool is_audio);
44 void StartPlaying(const base::Closure& done_cb,
45 ::media::PipelineStatus status);
47 void Flush(const base::Closure& done_cb, ::media::PipelineStatus status);
48 void Stop(const base::Closure& done_cb, ::media::PipelineStatus status);
50 void OnEos();
52 base::Closure task_after_eos_cb_;
53 scoped_ptr<base::MessageLoop> message_loop_;
55 private:
56 scoped_ptr<TaskRunnerImpl> task_runner_;
57 scoped_ptr<MediaPipelineImpl> media_pipeline_;
59 DISALLOW_COPY_AND_ASSIGN(AudioVideoPipelineImplTest);
62 AudioVideoPipelineImplTest::AudioVideoPipelineImplTest()
63 : message_loop_(new base::MessageLoop()),
64 task_runner_(new TaskRunnerImpl()),
65 media_pipeline_(new MediaPipelineImpl()) {
66 MediaPipelineDeviceParams params(task_runner_.get());
67 scoped_ptr<MediaPipelineBackend> backend =
68 make_scoped_ptr(new MediaPipelineBackendDefault(params));
69 media_pipeline_->Initialize(kLoadTypeURL, backend.Pass());
70 media_pipeline_->SetPlaybackRate(1.0);
73 AudioVideoPipelineImplTest::~AudioVideoPipelineImplTest() {
76 void AudioVideoPipelineImplTest::Initialize(
77 const base::Closure& done_cb,
78 ::media::PipelineStatus status,
79 bool is_audio) {
80 if (is_audio) {
81 AvPipelineClient client;
82 client.eos_cb =
83 base::Bind(&AudioVideoPipelineImplTest::OnEos, base::Unretained(this));
84 media_pipeline_->GetAudioPipeline()->SetClient(client);
85 } else {
86 VideoPipelineClient client;
87 client.av_pipeline_client.eos_cb =
88 base::Bind(&AudioVideoPipelineImplTest::OnEos, base::Unretained(this));
89 media_pipeline_->GetVideoPipeline()->SetClient(client);
92 ::media::AudioDecoderConfig audio_config(
93 ::media::kCodecMP3,
94 ::media::kSampleFormatS16,
95 ::media::CHANNEL_LAYOUT_STEREO,
96 44100,
97 NULL, 0, false);
98 std::vector<::media::VideoDecoderConfig> video_configs;
99 video_configs.push_back(::media::VideoDecoderConfig(
100 ::media::kCodecH264, ::media::H264PROFILE_MAIN,
101 ::media::PIXEL_FORMAT_I420, ::media::COLOR_SPACE_UNSPECIFIED,
102 gfx::Size(640, 480), gfx::Rect(0, 0, 640, 480), gfx::Size(640, 480), NULL,
103 0, false));
105 // Frame generation on the producer side.
106 std::vector<FrameGeneratorForTest::FrameSpec> frame_specs;
107 frame_specs.resize(100);
108 for (size_t k = 0; k < frame_specs.size() - 1; k++) {
109 frame_specs[k].has_config = (k == 0);
110 frame_specs[k].timestamp = base::TimeDelta::FromMilliseconds(40) * k;
111 frame_specs[k].size = 512;
112 frame_specs[k].has_decrypt_config = false;
114 frame_specs[frame_specs.size() - 1].is_eos = true;
116 scoped_ptr<FrameGeneratorForTest> frame_generator_provider(
117 new FrameGeneratorForTest(frame_specs));
118 bool provider_delayed_pattern[] = { true, false };
119 scoped_ptr<MockFrameProvider> frame_provider(new MockFrameProvider());
120 frame_provider->Configure(
121 std::vector<bool>(
122 provider_delayed_pattern,
123 provider_delayed_pattern + arraysize(provider_delayed_pattern)),
124 frame_generator_provider.Pass());
126 ::media::PipelineStatusCB next_task =
127 base::Bind(&AudioVideoPipelineImplTest::StartPlaying,
128 base::Unretained(this),
129 done_cb);
131 scoped_ptr<CodedFrameProvider> frame_provider_base(frame_provider.release());
132 base::Closure task = is_audio ?
133 base::Bind(&MediaPipeline::InitializeAudio,
134 base::Unretained(media_pipeline_.get()),
135 audio_config,
136 base::Passed(&frame_provider_base),
137 next_task) :
138 base::Bind(&MediaPipeline::InitializeVideo,
139 base::Unretained(media_pipeline_.get()),
140 video_configs,
141 base::Passed(&frame_provider_base),
142 next_task);
144 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task);
147 void AudioVideoPipelineImplTest::StartPlaying(
148 const base::Closure& done_cb, ::media::PipelineStatus status) {
149 base::TimeDelta start_time = base::TimeDelta::FromMilliseconds(0);
151 media_pipeline_->StartPlayingFrom(start_time);
152 if (!done_cb.is_null())
153 done_cb.Run();
156 void AudioVideoPipelineImplTest::OnEos() {
157 task_after_eos_cb_.Run();
160 void AudioVideoPipelineImplTest::Flush(
161 const base::Closure& done_cb, ::media::PipelineStatus status) {
162 ::media::PipelineStatusCB next_task =
163 base::Bind(&AudioVideoPipelineImplTest::Stop, base::Unretained(this),
164 done_cb);
165 base::ThreadTaskRunnerHandle::Get()->PostTask(
166 FROM_HERE,
167 base::Bind(&MediaPipeline::Flush, base::Unretained(media_pipeline_.get()),
168 next_task));
171 void AudioVideoPipelineImplTest::Stop(
172 const base::Closure& done_cb, ::media::PipelineStatus status) {
173 media_pipeline_->Stop();
174 if (!done_cb.is_null())
175 done_cb.Run();
176 base::MessageLoop::current()->QuitWhenIdle();
180 TEST_F(AudioVideoPipelineImplTest, AudioFullCycleInitToStop) {
181 bool is_audio = true;
182 task_after_eos_cb_ = base::Bind(
183 &AudioVideoPipelineImplTest::Flush, base::Unretained(this),
184 base::Closure(), ::media::PIPELINE_OK);
186 message_loop_->PostTask(FROM_HERE,
187 base::Bind(&AudioVideoPipelineImplTest::Initialize,
188 base::Unretained(this), base::Closure(),
189 ::media::PIPELINE_OK, is_audio));
190 message_loop_->Run();
193 TEST_F(AudioVideoPipelineImplTest, VideoFullCycleInitToStop) {
194 bool is_audio = false;
195 task_after_eos_cb_ = base::Bind(
196 &AudioVideoPipelineImplTest::Flush, base::Unretained(this),
197 base::Closure(), ::media::PIPELINE_OK);
199 message_loop_->PostTask(FROM_HERE,
200 base::Bind(&AudioVideoPipelineImplTest::Initialize,
201 base::Unretained(this), base::Closure(),
202 ::media::PIPELINE_OK, is_audio));
203 message_loop_->Run();
206 } // namespace media
207 } // namespace chromecast