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.
6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h"
8 #include "media/base/audio_decoder_config.h"
9 #include "media/base/channel_layout.h"
10 #include "media/base/demuxer_stream_provider.h"
11 #include "media/base/sample_format.h"
12 #include "media/base/video_decoder_config.h"
13 #include "media/mojo/services/mojo_renderer_impl.h"
14 #include "mojo/public/cpp/application/application_delegate.h"
15 #include "mojo/public/cpp/application/application_impl.h"
16 #include "mojo/public/cpp/application/application_test_base.h"
17 #include "mojo/public/cpp/application/connect.h"
21 // This class is here to give the gtest class access to the
22 // mojo::ApplicationImpl so that the tests can connect to other applications.
23 class MojoRendererTestHelper
: public mojo::ApplicationDelegate
{
25 MojoRendererTestHelper() : application_impl_(NULL
) {}
26 ~MojoRendererTestHelper() override
{}
28 // ApplicationDelegate implementation.
29 void Initialize(mojo::ApplicationImpl
* app
) override
{
30 application_impl_
= app
;
33 mojo::ApplicationImpl
* application_impl() { return application_impl_
; }
36 mojo::ApplicationImpl
* application_impl_
;
38 DISALLOW_COPY_AND_ASSIGN(MojoRendererTestHelper
);
41 // TODO(tim): Make media::FakeDemuxerStream support audio and use that for the
42 // DemuxerStream implementation instead.
43 class FakeDemuxerStream
: public media::DemuxerStreamProvider
,
44 public media::DemuxerStream
{
46 FakeDemuxerStream() {}
47 ~FakeDemuxerStream() override
{}
49 // media::Demuxer implementation.
50 media::DemuxerStream
* GetStream(media::DemuxerStream::Type type
) override
{
51 if (type
== media::DemuxerStream::AUDIO
)
56 // media::DemuxerStream implementation.
57 void Read(const ReadCB
& read_cb
) override
{}
59 media::AudioDecoderConfig
audio_decoder_config() override
{
60 media::AudioDecoderConfig config
;
61 config
.Initialize(media::kCodecAAC
,
62 media::kSampleFormatU8
,
63 media::CHANNEL_LAYOUT_SURROUND
,
74 media::VideoDecoderConfig
video_decoder_config() override
{
76 return media::VideoDecoderConfig();
79 media::DemuxerStream::Type
type() const override
{
80 return media::DemuxerStream::AUDIO
;
83 void EnableBitstreamConverter() override
{}
85 bool SupportsConfigChanges() override
{ return true; }
87 media::VideoRotation
video_rotation() override
{
89 return media::VIDEO_ROTATION_0
;
93 DISALLOW_COPY_AND_ASSIGN(FakeDemuxerStream
);
100 class MojoRendererTest
: public mojo::test::ApplicationTestBase
{
102 MojoRendererTest() : service_provider_(NULL
) {}
103 ~MojoRendererTest() override
{}
106 // ApplicationTestBase implementation.
107 mojo::ApplicationDelegate
* GetApplicationDelegate() override
{
108 return &mojo_renderer_test_helper_
;
111 void SetUp() override
{
112 ApplicationTestBase::SetUp();
113 demuxer_stream_provider_
.reset(new FakeDemuxerStream());
116 ->ConnectToApplication("mojo:media")
117 ->GetServiceProvider();
120 mojo::MediaRendererPtr
CreateMediaRenderer() {
121 mojo::MediaRendererPtr mojo_media_renderer
;
122 mojo::ConnectToService(service_provider_
,
123 &mojo_media_renderer
);
124 return mojo_media_renderer
.Pass();
127 DemuxerStreamProvider
* stream_provider() {
128 return demuxer_stream_provider_
.get();
130 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner() {
131 return base::MessageLoop::current()->task_runner();
135 MojoRendererTestHelper mojo_renderer_test_helper_
;
136 scoped_ptr
<DemuxerStreamProvider
> demuxer_stream_provider_
;
137 mojo::ServiceProvider
* service_provider_
;
139 DISALLOW_COPY_AND_ASSIGN(MojoRendererTest
);
142 void ErrorCallback(PipelineStatus
* output
, PipelineStatus status
) {
146 // Tests that a MojoRendererImpl can successfully establish communication
147 // with a MojoRendererService and set up a MojoDemuxerStream
148 // connection. The test also initializes a media::AudioRendererImpl which
149 // will error-out expectedly due to lack of support for decoder selection.
150 TEST_F(MojoRendererTest
, BasicInitialize
) {
151 MojoRendererImpl
mojo_renderer_impl(task_runner(), CreateMediaRenderer());
152 PipelineStatus
expected_error(PIPELINE_OK
);
153 mojo_renderer_impl
.Initialize(
154 stream_provider(), base::MessageLoop::current()->QuitClosure(),
155 media::StatisticsCB(), media::BufferingStateCB(),
156 media::Renderer::PaintCB(), base::Closure(),
157 base::Bind(&ErrorCallback
, &expected_error
));
158 base::MessageLoop::current()->Run();
159 EXPECT_EQ(PIPELINE_OK
, expected_error
);