summaryrefslogtreecommitdiffstats
path: root/media/mojo/services/media_renderer_apptest.cc
blob: 8813b381a111f26b0130fe472c0fb173dea5c87a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "media/base/audio_decoder_config.h"
#include "media/base/channel_layout.h"
#include "media/base/demuxer_stream_provider.h"
#include "media/base/sample_format.h"
#include "media/base/video_decoder_config.h"
#include "media/mojo/services/mojo_renderer_impl.h"
#include "mojo/public/cpp/application/application_delegate.h"
#include "mojo/public/cpp/application/application_impl.h"
#include "mojo/public/cpp/application/application_test_base.h"

namespace {

// This class is here to give the gtest class access to the
// mojo::ApplicationImpl so that the tests can connect to other applications.
class MojoRendererTestHelper : public mojo::ApplicationDelegate {
 public:
  MojoRendererTestHelper() : application_impl_(NULL) {}
  ~MojoRendererTestHelper() override {}

  // ApplicationDelegate implementation.
  void Initialize(mojo::ApplicationImpl* app) override {
    application_impl_ = app;
  }

  mojo::ApplicationImpl* application_impl() { return application_impl_; }

 private:
  mojo::ApplicationImpl* application_impl_;

  DISALLOW_COPY_AND_ASSIGN(MojoRendererTestHelper);
};

// TODO(tim): Make media::FakeDemuxerStream support audio and use that for the
// DemuxerStream implementation instead.
class FakeDemuxerStream : public media::DemuxerStreamProvider,
                          public media::DemuxerStream {
 public:
  FakeDemuxerStream() {}
  ~FakeDemuxerStream() override {}

  // media::Demuxer implementation.
  media::DemuxerStream* GetStream(media::DemuxerStream::Type type) override {
    if (type == media::DemuxerStream::AUDIO)
      return this;
    return nullptr;
  }

  // media::DemuxerStream implementation.
  void Read(const ReadCB& read_cb) override {}

  media::AudioDecoderConfig audio_decoder_config() override {
    media::AudioDecoderConfig config;
    config.Initialize(media::kCodecAAC,
                      media::kSampleFormatU8,
                      media::CHANNEL_LAYOUT_SURROUND,
                      48000,
                      NULL,
                      0,
                      false,
                      false,
                      base::TimeDelta(),
                      0);
    return config;
  }

  media::VideoDecoderConfig video_decoder_config() override {
    NOTREACHED();
    return media::VideoDecoderConfig();
  }

  media::DemuxerStream::Type type() const override {
    return media::DemuxerStream::AUDIO;
  }

  void EnableBitstreamConverter() override {}

  bool SupportsConfigChanges() override { return true; }

  media::VideoRotation video_rotation() override {
    NOTREACHED();
    return media::VIDEO_ROTATION_0;
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(FakeDemuxerStream);
};

}  // namespace

namespace media {

class MojoRendererTest : public mojo::test::ApplicationTestBase {
 public:
  MojoRendererTest()
      : service_provider_(NULL) {}
  ~MojoRendererTest() override {}

 protected:
  // ApplicationTestBase implementation.
  mojo::ApplicationDelegate* GetApplicationDelegate() override {
    return &mojo_renderer_test_helper_;
  }

  void SetUp() override {
    ApplicationTestBase::SetUp();
    demuxer_stream_provider_.reset(new FakeDemuxerStream());
    service_provider_ =
        application_impl()
            ->ConnectToApplication("mojo:mojo_media_renderer_app")
            ->GetServiceProvider();
  }

  mojo::ServiceProvider* service_provider() { return service_provider_; }
  DemuxerStreamProvider* stream_provider() {
    return demuxer_stream_provider_.get();
  }
  scoped_refptr<base::SingleThreadTaskRunner> task_runner() {
    return base::MessageLoop::current()->task_runner();
  }

 private:
  MojoRendererTestHelper mojo_renderer_test_helper_;
  scoped_ptr<DemuxerStreamProvider> demuxer_stream_provider_;
  mojo::ServiceProvider* service_provider_;

  DISALLOW_COPY_AND_ASSIGN(MojoRendererTest);
};

void ErrorCallback(PipelineStatus* output, PipelineStatus status) {
  *output = status;
}

// Tests that a MojoRendererImpl can successfully establish communication
// with a MojoRendererService and set up a MojoDemuxerStream
// connection. The test also initializes a media::AudioRendererImpl which
// will error-out expectedly due to lack of support for decoder selection.
TEST_F(MojoRendererTest, BasicInitialize) {
  MojoRendererImpl mojo_renderer_impl(task_runner(), service_provider());
  PipelineStatus expected_error(PIPELINE_OK);
  mojo_renderer_impl.Initialize(
      stream_provider(), base::MessageLoop::current()->QuitClosure(),
      media::StatisticsCB(), media::BufferingStateCB(),
      media::Renderer::PaintCB(), base::Closure(),
      base::Bind(&ErrorCallback, &expected_error));
  base::MessageLoop::current()->Run();

  // We expect an error during initialization because MojoRendererService
  // doesn't initialize any decoders, which causes an error.
  EXPECT_EQ(PIPELINE_ERROR_COULD_NOT_RENDER, expected_error);
}

}  // namespace media