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/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/timer/timer.h"
13 #include "media/base/audio_decoder_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"
20 class ApplicationConnection
;
25 class AudioRendererSink
;
26 class DemuxerStreamProviderShim
;
27 class MojoDemuxerStreamAdapter
;
30 // A mojo::MediaRenderer implementation that uses media::AudioRenderer to
31 // decode and render audio to a sink obtained from the ApplicationConnection.
32 class MojoRendererService
: public mojo::InterfaceImpl
<mojo::MediaRenderer
> {
34 // |connection| is a pointer to the connection back to our embedder. The
35 // embedder should have configured it (via ConfigureOutgoingConnection) to
36 // allow |this| to connect to a sink that will receive decoded data ready
38 explicit MojoRendererService(mojo::ApplicationConnection
* connection
);
39 ~MojoRendererService() override
;
41 // mojo::MediaRenderer implementation.
42 void Initialize(mojo::DemuxerStreamPtr audio
,
43 mojo::DemuxerStreamPtr video
,
44 const mojo::Closure
& callback
) override
;
45 void Flush(const mojo::Closure
& callback
) override
;
46 void StartPlayingFrom(int64_t time_delta_usec
) override
;
47 void SetPlaybackRate(float playback_rate
) override
;
48 void SetVolume(float volume
) override
;
59 // Called when the MojoDemuxerStreamAdapter is ready to go (has a config,
60 // pipe handle, etc) and can be handed off to a renderer for use.
61 void OnStreamReady(const mojo::Closure
& callback
);
63 // Called when |audio_renderer_| initialization has completed.
64 void OnRendererInitializeDone(const mojo::Closure
& callback
);
66 // Callback executed by filters to update statistics.
67 void OnUpdateStatistics(const PipelineStatistics
& stats
);
69 // Periodically polls the media time from the renderer and notifies the client
70 // if the media time has changed since the last update. If |force| is true,
71 // the client is notified even if the time is unchanged.
72 void UpdateMediaTime(bool force
);
73 void SchedulePeriodicMediaTimeUpdates();
75 // Callback executed by audio renderer when buffering state changes.
76 // TODO(tim): Need old and new.
77 void OnBufferingStateChanged(BufferingState new_buffering_state
);
79 // Callback executed when a renderer has ended.
80 void OnRendererEnded();
82 // Callback executed when a runtime error happens.
83 void OnError(PipelineStatus error
);
87 scoped_refptr
<AudioRendererSink
> audio_renderer_sink_
;
88 scoped_ptr
<Renderer
> renderer_
;
89 scoped_ptr
<DemuxerStreamProviderShim
> stream_provider_
;
91 base::RepeatingTimer
<MojoRendererService
> time_update_timer_
;
92 uint64_t last_media_time_usec_
;
94 base::WeakPtrFactory
<MojoRendererService
> weak_factory_
;
95 base::WeakPtr
<MojoRendererService
> weak_this_
;
97 DISALLOW_COPY_AND_ASSIGN(MojoRendererService
);
102 #endif // MEDIA_MOJO_SERVICES_MOJO_RENDERER_SERVICE_H_