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 "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::MediaRenderer 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::MediaRenderer
) {
41 // |cdm_context_provider| can be used to find the CdmContext to support
42 // encrypted media. If null, encrypted media is not supported.
44 base::WeakPtr
<CdmContextProvider
> cdm_context_provider
,
45 RendererFactory
* renderer_factory
,
46 const scoped_refptr
<MediaLog
>& media_log
,
47 mojo::InterfaceRequest
<interfaces::MediaRenderer
> request
);
48 ~MojoRendererService() final
;
50 // interfaces::MediaRenderer implementation.
51 void Initialize(interfaces::MediaRendererClientPtr client
,
52 interfaces::DemuxerStreamPtr audio
,
53 interfaces::DemuxerStreamPtr video
,
54 const mojo::Callback
<void(bool)>& callback
) final
;
55 void Flush(const mojo::Closure
& callback
) final
;
56 void StartPlayingFrom(int64_t time_delta_usec
) final
;
57 void SetPlaybackRate(double playback_rate
) final
;
58 void SetVolume(float volume
) final
;
59 void SetCdm(int32_t cdm_id
, const mojo::Callback
<void(bool)>& callback
) final
;
70 // Called when the DemuxerStreamProviderShim is ready to go (has a config,
71 // pipe handle, etc) and can be handed off to a renderer for use.
72 void OnStreamReady(const mojo::Callback
<void(bool)>& callback
);
74 // Called when |audio_renderer_| initialization has completed.
75 void OnRendererInitializeDone(const mojo::Callback
<void(bool)>& callback
,
76 PipelineStatus status
);
78 // Callback executed by filters to update statistics.
79 void OnUpdateStatistics(const PipelineStatistics
& stats
);
81 // Periodically polls the media time from the renderer and notifies the client
82 // if the media time has changed since the last update. If |force| is true,
83 // the client is notified even if the time is unchanged.
84 void UpdateMediaTime(bool force
);
85 void CancelPeriodicMediaTimeUpdates();
86 void SchedulePeriodicMediaTimeUpdates();
88 // Callback executed by audio renderer when buffering state changes.
89 // TODO(tim): Need old and new.
90 void OnBufferingStateChanged(BufferingState new_buffering_state
);
92 // Callback executed when a renderer has ended.
93 void OnRendererEnded();
95 // Callback executed when a runtime error happens.
96 void OnError(PipelineStatus error
);
98 // Callback executed once Flush() completes.
99 void OnFlushCompleted(const mojo::Closure
& callback
);
101 // Callback executed once SetCdm() completes.
102 void OnCdmAttached(const mojo::Callback
<void(bool)>& callback
, bool success
);
104 mojo::StrongBinding
<interfaces::MediaRenderer
> binding_
;
106 base::WeakPtr
<CdmContextProvider
> cdm_context_provider_
;
110 // Note: |renderer_| should be destructed before these objects to avoid access
112 scoped_ptr
<DemuxerStreamProviderShim
> stream_provider_
;
113 scoped_refptr
<AudioRendererSink
> audio_renderer_sink_
;
114 scoped_ptr
<VideoRendererSink
> video_renderer_sink_
;
116 scoped_ptr
<Renderer
> renderer_
;
118 base::RepeatingTimer
<MojoRendererService
> time_update_timer_
;
119 uint64_t last_media_time_usec_
;
121 interfaces::MediaRendererClientPtr client_
;
123 base::WeakPtr
<MojoRendererService
> weak_this_
;
124 base::WeakPtrFactory
<MojoRendererService
> weak_factory_
;
126 DISALLOW_COPY_AND_ASSIGN(MojoRendererService
);
131 #endif // MEDIA_MOJO_SERVICES_MOJO_RENDERER_SERVICE_H_