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#ifndef MEDIA_MOJO_SERVICES_MOJO_RENDERER_SERVICE_H_
6#define MEDIA_MOJO_SERVICES_MOJO_RENDERER_SERVICE_H_
7
8#include "base/callback.h"
9#include "base/macros.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/memory/weak_ptr.h"
12#include "media/base/audio_decoder_config.h"
13#include "media/base/audio_hardware_config.h"
14#include "media/base/buffering_state.h"
15#include "media/base/pipeline_status.h"
16#include "media/mojo/interfaces/media_renderer.mojom.h"
17#include "mojo/public/cpp/bindings/interface_impl.h"
18
19namespace mojo {
20class ApplicationConnection;
21}
22
23namespace media {
24
25class AudioRenderer;
26class MojoDemuxerStreamAdapter;
27
28// A mojo::MediaRenderer implementation that uses media::AudioRenderer to
29// decode and render audio to a sink obtained from the ApplicationConnection.
30class MojoRendererService : public mojo::InterfaceImpl<mojo::MediaRenderer> {
31 public:
32  // |connection| is a pointer to the connection back to our embedder. The
33  // embedder should have configured it (via ConfigureOutgoingConnection) to
34  // allow |this| to connect to a sink that will receive decoded data ready
35  // for playback.
36  explicit MojoRendererService(mojo::ApplicationConnection* connection);
37  virtual ~MojoRendererService();
38
39  // mojo::MediaRenderer implementation.
40  virtual void Initialize(mojo::DemuxerStreamPtr stream,
41                          const mojo::Callback<void()>& callback) MOJO_OVERRIDE;
42  virtual void Flush(const mojo::Callback<void()>& callback) MOJO_OVERRIDE;
43  virtual void StartPlayingFrom(int64_t time_delta_usec) MOJO_OVERRIDE;
44  virtual void SetPlaybackRate(float playback_rate) MOJO_OVERRIDE;
45  virtual void SetVolume(float volume) MOJO_OVERRIDE;
46
47 private:
48  // Called when the MojoDemuxerStreamAdapter is ready to go (has a config,
49  // pipe handle, etc) and can be handed off to a renderer for use.
50  void OnStreamReady();
51
52  // Called when |audio_renderer_| initialization has completed.
53  void OnAudioRendererInitializeDone(PipelineStatus status);
54
55  // Callback executed by filters to update statistics.
56  void OnUpdateStatistics(const PipelineStatistics& stats);
57
58  // Callback executed by audio renderer to update clock time.
59  void OnAudioTimeUpdate(base::TimeDelta time, base::TimeDelta max_time);
60
61  // Callback executed by audio renderer when buffering state changes.
62  // TODO(tim): Need old and new.
63  void OnBufferingStateChanged(BufferingState new_buffering_state);
64
65  // Callback executed when a renderer has ended.
66  void OnAudioRendererEnded();
67
68  // Callback executed when a runtime error happens.
69  void OnError(PipelineStatus error);
70
71  scoped_ptr<MojoDemuxerStreamAdapter> stream_;
72  scoped_ptr<AudioRenderer> audio_renderer_;
73
74  mojo::Callback<void()> init_cb_;
75
76  // TODO(tim): Figure out how to set up hardware config.
77  // NOTE: AudioRendererImpl stores a const& to the config we pass in (hmm..).
78  // Hence stack-allocating one and passing it to Initialize results in
79  // undefined badness (e.g, hangs trying to acquire config_lock_);
80  media::AudioHardwareConfig hardware_config_;
81
82  base::WeakPtrFactory<MojoRendererService> weak_factory_;
83  base::WeakPtr<MojoRendererService> weak_this_;
84  DISALLOW_COPY_AND_ASSIGN(MojoRendererService);
85};
86
87}  // namespace media
88
89#endif  // MEDIA_MOJO_SERVICES_MOJO_RENDERER_SERVICE_H_
90