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/renderer.mojom.h"
19 #include "media/mojo/services/mojo_cdm_service_context.h"
20 #include "third_party/mojo/src/mojo/public/cpp/bindings/strong_binding.h"
23 class ApplicationConnection
;
28 class AudioRendererSink
;
29 class DemuxerStreamProviderShim
;
30 class CdmContextProvider
;
33 class RendererFactory
;
34 class VideoRendererSink
;
36 // A interfaces::Renderer implementation that uses media::AudioRenderer to
37 // decode and render audio to a sink obtained from the ApplicationConnection.
38 class MEDIA_EXPORT MojoRendererService
39 : NON_EXPORTED_BASE(interfaces::Renderer
) {
41 // |cdm_context_provider| can be used to find the CdmContext to support
42 // encrypted media. If null, encrypted media is not supported.
43 MojoRendererService(base::WeakPtr
<CdmContextProvider
> cdm_context_provider
,
44 RendererFactory
* renderer_factory
,
45 const scoped_refptr
<MediaLog
>& media_log
,
46 mojo::InterfaceRequest
<interfaces::Renderer
> request
);
47 ~MojoRendererService() final
;
49 // interfaces::Renderer implementation.
50 void Initialize(interfaces::RendererClientPtr client
,
51 interfaces::DemuxerStreamPtr audio
,
52 interfaces::DemuxerStreamPtr video
,
53 const mojo::Callback
<void(bool)>& callback
) final
;
54 void Flush(const mojo::Closure
& callback
) final
;
55 void StartPlayingFrom(int64_t time_delta_usec
) final
;
56 void SetPlaybackRate(double playback_rate
) final
;
57 void SetVolume(float volume
) final
;
58 void SetCdm(int32_t cdm_id
, const mojo::Callback
<void(bool)>& callback
) final
;
69 // Called when the DemuxerStreamProviderShim is ready to go (has a config,
70 // pipe handle, etc) and can be handed off to a renderer for use.
71 void OnStreamReady(const mojo::Callback
<void(bool)>& callback
);
73 // Called when |audio_renderer_| initialization has completed.
74 void OnRendererInitializeDone(const mojo::Callback
<void(bool)>& callback
,
75 PipelineStatus status
);
77 // Callback executed by filters to update statistics.
78 void OnUpdateStatistics(const PipelineStatistics
& stats
);
80 // Periodically polls the media time from the renderer and notifies the client
81 // if the media time has changed since the last update. If |force| is true,
82 // the client is notified even if the time is unchanged.
83 void UpdateMediaTime(bool force
);
84 void CancelPeriodicMediaTimeUpdates();
85 void SchedulePeriodicMediaTimeUpdates();
87 // Callback executed by audio renderer when buffering state changes.
88 // TODO(tim): Need old and new.
89 void OnBufferingStateChanged(BufferingState new_buffering_state
);
91 // Callback executed when a renderer has ended.
92 void OnRendererEnded();
94 // Callback executed when a runtime error happens.
95 void OnError(PipelineStatus error
);
97 // Callback executed once Flush() completes.
98 void OnFlushCompleted(const mojo::Closure
& callback
);
100 // Callback executed once SetCdm() completes.
101 void OnCdmAttached(const mojo::Callback
<void(bool)>& callback
, bool success
);
103 mojo::StrongBinding
<interfaces::Renderer
> binding_
;
105 base::WeakPtr
<CdmContextProvider
> cdm_context_provider_
;
109 // Note: |renderer_| should be destructed before these objects to avoid access
111 scoped_ptr
<DemuxerStreamProviderShim
> stream_provider_
;
112 scoped_refptr
<AudioRendererSink
> audio_renderer_sink_
;
113 scoped_ptr
<VideoRendererSink
> video_renderer_sink_
;
115 scoped_ptr
<media::Renderer
> renderer_
;
117 base::RepeatingTimer
<MojoRendererService
> time_update_timer_
;
118 uint64_t last_media_time_usec_
;
120 interfaces::RendererClientPtr client_
;
122 base::WeakPtr
<MojoRendererService
> weak_this_
;
123 base::WeakPtrFactory
<MojoRendererService
> weak_factory_
;
125 DISALLOW_COPY_AND_ASSIGN(MojoRendererService
);
130 #endif // MEDIA_MOJO_SERVICES_MOJO_RENDERER_SERVICE_H_