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.
4
5 #include "base/at_exit.h"
6 #include "base/bind.h"
7 #include "base/command_line.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "media/base/audio_decoder_config.h"
11 #include "media/base/channel_layout.h"
12 #include "media/base/demuxer_stream_provider.h"
13 #include "media/base/sample_format.h"
14 #include "media/base/video_decoder_config.h"
15 #include "media/mojo/services/mojo_renderer_impl.h"
16 #include "mojo/public/c/system/main.h"
17 #include "mojo/public/cpp/application/application_delegate.h"
18 #include "mojo/public/cpp/application/application_impl.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace {
22
23 // This class is here to give the gtest class access to the
24 // mojo::ApplicationImpl so that the tests can connect to other applications.
25 class MojoRendererTestHelper : public mojo::ApplicationDelegate {
26 public:
MojoRendererTestHelper()27 MojoRendererTestHelper() : application_impl_(NULL) {}
~MojoRendererTestHelper()28 virtual ~MojoRendererTestHelper() {}
29
30 // ApplicationDelegate implementation.
Initialize(mojo::ApplicationImpl * app)31 virtual void Initialize(mojo::ApplicationImpl* app) OVERRIDE {
32 application_impl_ = app;
33 }
34
application_impl()35 mojo::ApplicationImpl* application_impl() { return application_impl_; }
36
37 private:
38 mojo::ApplicationImpl* application_impl_;
39
40 DISALLOW_COPY_AND_ASSIGN(MojoRendererTestHelper);
41 };
42
43 // TODO(tim): Reconcile this with mojo apptest framework when ready.
44 MojoRendererTestHelper* g_test_delegate = NULL;
45
46 // TODO(tim): Make media::FakeDemuxerStream support audio and use that for the
47 // DemuxerStream implementation instead.
48 class FakeDemuxerStream : public media::DemuxerStreamProvider,
49 public media::DemuxerStream {
50 public:
FakeDemuxerStream()51 FakeDemuxerStream() {}
~FakeDemuxerStream()52 virtual ~FakeDemuxerStream() {}
53
54 // media::Demuxer implementation.
GetStream(media::DemuxerStream::Type type)55 virtual media::DemuxerStream* GetStream(
56 media::DemuxerStream::Type type) OVERRIDE {
57 DCHECK_EQ(media::DemuxerStream::AUDIO, type);
58 return this;
59 }
GetLiveness() const60 virtual media::DemuxerStreamProvider::Liveness GetLiveness() const OVERRIDE {
61 return media::DemuxerStreamProvider::LIVENESS_UNKNOWN;
62 }
63
64 // media::DemuxerStream implementation.
Read(const ReadCB & read_cb)65 virtual void Read(const ReadCB& read_cb) OVERRIDE {}
66
audio_decoder_config()67 virtual media::AudioDecoderConfig audio_decoder_config() OVERRIDE {
68 media::AudioDecoderConfig config;
69 config.Initialize(media::kCodecAAC,
70 media::kSampleFormatU8,
71 media::CHANNEL_LAYOUT_SURROUND,
72 48000,
73 NULL,
74 0,
75 false,
76 false,
77 base::TimeDelta(),
78 0);
79 return config;
80 }
81
video_decoder_config()82 virtual media::VideoDecoderConfig video_decoder_config() OVERRIDE {
83 NOTREACHED();
84 return media::VideoDecoderConfig();
85 }
86
type()87 virtual media::DemuxerStream::Type type() OVERRIDE {
88 return media::DemuxerStream::AUDIO;
89 }
90
EnableBitstreamConverter()91 virtual void EnableBitstreamConverter() OVERRIDE {}
92
SupportsConfigChanges()93 virtual bool SupportsConfigChanges() OVERRIDE { return true; }
94
video_rotation()95 virtual media::VideoRotation video_rotation() OVERRIDE {
96 NOTREACHED();
97 return media::VIDEO_ROTATION_0;
98 }
99
100 private:
101 DISALLOW_COPY_AND_ASSIGN(FakeDemuxerStream);
102 };
103
104 } // namespace
105
106 namespace media {
107
108 class MojoRendererTest : public testing::Test {
109 public:
MojoRendererTest()110 MojoRendererTest() : service_provider_(NULL) {}
111
SetUp()112 virtual void SetUp() OVERRIDE {
113 demuxer_stream_provider_.reset(new FakeDemuxerStream());
114 service_provider_ =
115 g_test_delegate->application_impl()
116 ->ConnectToApplication("mojo:media_mojo_renderer_app")
117 ->GetServiceProvider();
118 }
119
service_provider()120 mojo::ServiceProvider* service_provider() { return service_provider_; }
stream_provider()121 DemuxerStreamProvider* stream_provider() {
122 return demuxer_stream_provider_.get();
123 }
task_runner()124 scoped_refptr<base::SingleThreadTaskRunner> task_runner() {
125 return base::MessageLoop::current()->task_runner();
126 }
127
128 private:
129 scoped_ptr<DemuxerStreamProvider> demuxer_stream_provider_;
130 mojo::ServiceProvider* service_provider_;
131
132 DISALLOW_COPY_AND_ASSIGN(MojoRendererTest);
133 };
134
ErrorCallback(PipelineStatus * output,PipelineStatus status)135 void ErrorCallback(PipelineStatus* output, PipelineStatus status) {
136 *output = status;
137 }
138
139 // Tests that a MojoRendererImpl can successfully establish communication
140 // with a MojoRendererService and set up a MojoDemuxerStream
141 // connection. The test also initializes a media::AudioRendererImpl which
142 // will error-out expectedly due to lack of support for decoder selection.
TEST_F(MojoRendererTest,BasicInitialize)143 TEST_F(MojoRendererTest, BasicInitialize) {
144 MojoRendererImpl rimpl(task_runner(), stream_provider(), service_provider());
145 PipelineStatus expected_error(PIPELINE_OK);
146 rimpl.Initialize(base::MessageLoop::current()->QuitClosure(),
147 media::StatisticsCB(),
148 base::Closure(),
149 base::Bind(&ErrorCallback, &expected_error),
150 media::BufferingStateCB());
151 base::MessageLoop::current()->Run();
152
153 // We expect an error during initialization because MojoRendererService
154 // doesn't initialize any decoders, which causes an error.
155 EXPECT_EQ(PIPELINE_ERROR_COULD_NOT_RENDER, expected_error);
156 }
157
158 } // namespace media
159
MojoMain(MojoHandle shell_handle)160 MojoResult MojoMain(MojoHandle shell_handle) {
161 base::CommandLine::Init(0, NULL);
162 #if !defined(COMPONENT_BUILD)
163 base::AtExitManager at_exit;
164 #endif
165
166 // TODO(tim): Reconcile this with apptest framework when it is ready.
167 scoped_ptr<mojo::ApplicationDelegate> delegate(new MojoRendererTestHelper());
168 g_test_delegate = static_cast<MojoRendererTestHelper*>(delegate.get());
169 {
170 base::MessageLoop loop;
171 mojo::ApplicationImpl impl(
172 delegate.get(),
173 mojo::MakeScopedHandle(mojo::MessagePipeHandle(shell_handle)));
174
175 int argc = 0;
176 char** argv = NULL;
177 testing::InitGoogleTest(&argc, argv);
178 mojo_ignore_result(RUN_ALL_TESTS());
179 }
180
181 g_test_delegate = NULL;
182 delegate.reset();
183 return MOJO_RESULT_OK;
184 }
185