MediaRenderer: Returns initialization result in the init callback.
[chromium-blink-merge.git] / media / mojo / services / mojo_renderer_service.h
blobd08780685db41bdff8cb8c4e8455745bd24a640d
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 #ifndef MEDIA_MOJO_SERVICES_MOJO_RENDERER_SERVICE_H_
6 #define MEDIA_MOJO_SERVICES_MOJO_RENDERER_SERVICE_H_
8 #include "base/callback.h"
9 #include "base/compiler_specific.h"
10 #include "base/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/timer/timer.h"
14 #include "media/base/audio_decoder_config.h"
15 #include "media/base/buffering_state.h"
16 #include "media/base/media_export.h"
17 #include "media/base/pipeline_status.h"
18 #include "media/mojo/interfaces/media_renderer.mojom.h"
19 #include "third_party/mojo/src/mojo/public/cpp/bindings/strong_binding.h"
21 namespace mojo {
22 class ApplicationConnection;
25 namespace media {
27 class AudioRendererSink;
28 class DemuxerStreamProviderShim;
29 class CdmContextProvider;
30 class MediaLog;
31 class Renderer;
32 class RendererFactory;
33 class VideoRendererSink;
35 // A mojo::MediaRenderer implementation that uses media::AudioRenderer to
36 // decode and render audio to a sink obtained from the ApplicationConnection.
37 class MEDIA_EXPORT MojoRendererService
38 : NON_EXPORTED_BASE(mojo::MediaRenderer) {
39 public:
40 // |cdm_context_provider| can be used to find the CdmContext to support
41 // encrypted media. If null, encrypted media is not supported.
42 MojoRendererService(CdmContextProvider* cdm_context_provider,
43 RendererFactory* renderer_factory,
44 const scoped_refptr<MediaLog>& media_log,
45 mojo::InterfaceRequest<mojo::MediaRenderer> request);
46 ~MojoRendererService() final;
48 // mojo::MediaRenderer implementation.
49 void Initialize(mojo::MediaRendererClientPtr client,
50 mojo::DemuxerStreamPtr audio,
51 mojo::DemuxerStreamPtr video,
52 const mojo::Callback<void(bool)>& callback) final;
53 void Flush(const mojo::Closure& callback) final;
54 void StartPlayingFrom(int64_t time_delta_usec) final;
55 void SetPlaybackRate(double playback_rate) final;
56 void SetVolume(float volume) final;
57 void SetCdm(int32_t cdm_id, const mojo::Callback<void(bool)>& callback) final;
59 private:
60 enum State {
61 STATE_UNINITIALIZED,
62 STATE_INITIALIZING,
63 STATE_FLUSHING,
64 STATE_PLAYING,
65 STATE_ERROR
68 // Called when the DemuxerStreamProviderShim is ready to go (has a config,
69 // pipe handle, etc) and can be handed off to a renderer for use.
70 void OnStreamReady(const mojo::Callback<void(bool)>& callback);
72 // Called when |audio_renderer_| initialization has completed.
73 void OnRendererInitializeDone(const mojo::Callback<void(bool)>& callback,
74 PipelineStatus status);
76 // Callback executed by filters to update statistics.
77 void OnUpdateStatistics(const PipelineStatistics& stats);
79 // Periodically polls the media time from the renderer and notifies the client
80 // if the media time has changed since the last update. If |force| is true,
81 // the client is notified even if the time is unchanged.
82 void UpdateMediaTime(bool force);
83 void CancelPeriodicMediaTimeUpdates();
84 void SchedulePeriodicMediaTimeUpdates();
86 // Callback executed by audio renderer when buffering state changes.
87 // TODO(tim): Need old and new.
88 void OnBufferingStateChanged(BufferingState new_buffering_state);
90 // Callback executed when a renderer has ended.
91 void OnRendererEnded();
93 // Callback executed when a runtime error happens.
94 void OnError(PipelineStatus error);
96 // Callback executed once Flush() completes.
97 void OnFlushCompleted(const mojo::Closure& callback);
99 // Callback executed once SetCdm() completes.
100 void OnCdmAttached(const mojo::Callback<void(bool)>& callback, bool success);
102 mojo::StrongBinding<mojo::MediaRenderer> binding_;
104 CdmContextProvider* cdm_context_provider_;
106 State state_;
108 // Note: |renderer_| should be destructed before these objects to avoid access
109 // violation.
110 scoped_ptr<DemuxerStreamProviderShim> stream_provider_;
111 scoped_refptr<AudioRendererSink> audio_renderer_sink_;
112 scoped_ptr<VideoRendererSink> video_renderer_sink_;
114 scoped_ptr<Renderer> renderer_;
116 base::RepeatingTimer<MojoRendererService> time_update_timer_;
117 uint64_t last_media_time_usec_;
119 mojo::MediaRendererClientPtr client_;
121 base::WeakPtr<MojoRendererService> weak_this_;
122 base::WeakPtrFactory<MojoRendererService> weak_factory_;
124 DISALLOW_COPY_AND_ASSIGN(MojoRendererService);
127 } // namespace media
129 #endif // MEDIA_MOJO_SERVICES_MOJO_RENDERER_SERVICE_H_