1 // Copyright 2014 The Chromium Authors
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_
11 #include "base/compiler_specific.h"
12 #include "base/functional/callback.h"
13 #include "base/memory/raw_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/time/time.h"
16 #include "base/timer/timer.h"
17 #include "base/unguessable_token.h"
18 #include "media/base/buffering_state.h"
19 #include "media/base/media_resource.h"
20 #include "media/base/pipeline_status.h"
21 #include "media/base/renderer_client.h"
22 #include "media/mojo/mojom/renderer.mojom.h"
23 #include "media/mojo/services/media_mojo_export.h"
24 #include "mojo/public/cpp/bindings/associated_remote.h"
25 #include "mojo/public/cpp/bindings/pending_associated_remote.h"
26 #include "mojo/public/cpp/bindings/pending_receiver.h"
27 #include "mojo/public/cpp/bindings/pending_remote.h"
28 #include "mojo/public/cpp/bindings/self_owned_receiver.h"
29 #include "third_party/abseil-cpp/absl/types/optional.h"
34 class MediaResourceShim;
35 class MojoCdmServiceContext;
38 // A mojom::Renderer implementation that use a media::Renderer to render
40 class MEDIA_MOJO_EXPORT MojoRendererService final : public mojom::Renderer,
41 public RendererClient {
43 // Helper function to bind MojoRendererService with a SelfOwendReceiver,
44 // which is safely accessible via the returned SelfOwnedReceiverRef.
45 static mojo::SelfOwnedReceiverRef<mojom::Renderer> Create(
46 MojoCdmServiceContext* mojo_cdm_service_context,
47 std::unique_ptr<media::Renderer> renderer,
48 mojo::PendingReceiver<mojom::Renderer> receiver);
50 // |mojo_cdm_service_context| can be used to find the CDM to support
51 // encrypted media. If null, encrypted media is not supported.
52 MojoRendererService(MojoCdmServiceContext* mojo_cdm_service_context,
53 std::unique_ptr<media::Renderer> renderer);
55 MojoRendererService(const MojoRendererService&) = delete;
56 MojoRendererService& operator=(const MojoRendererService&) = delete;
58 ~MojoRendererService() final;
60 // mojom::Renderer implementation.
62 mojo::PendingAssociatedRemote<mojom::RendererClient> client,
63 absl::optional<std::vector<mojo::PendingRemote<mojom::DemuxerStream>>>
65 mojom::MediaUrlParamsPtr media_url_params,
66 InitializeCallback callback) final;
67 void Flush(FlushCallback callback) final;
68 #if defined(TIZEN_MULTIMEDIA)
69 void Seek(base::TimeDelta time, base::OnceClosure seek_cb) final;
71 void ToggleFullscreenMode(bool is_fullscreen,
72 ToggleFullscreenModeCallback callback) final;
74 void StartPlayingFrom(base::TimeDelta time_delta) final;
75 void SetPlaybackRate(double playback_rate) final;
76 void SetVolume(float volume) final;
77 void SetCdm(const absl::optional<base::UnguessableToken>& cdm_id,
78 SetCdmCallback callback) final;
80 #if defined(TIZEN_VIDEO_HOLE)
81 void SetVideoHole(bool is_video_hole) final;
82 void SetMediaGeometry(const gfx::RectF& rect) final;
94 // RendererClient implementation.
95 void OnError(PipelineStatus status) final;
96 void OnFallback(PipelineStatus status) final;
98 void OnStatisticsUpdate(const PipelineStatistics& stats) final;
99 void OnBufferingStateChange(BufferingState state,
100 BufferingStateChangeReason reason) final;
101 void OnWaiting(WaitingReason reason) final;
102 void OnAudioConfigChange(const AudioDecoderConfig& config) final;
103 void OnVideoConfigChange(const VideoDecoderConfig& config) final;
104 void OnVideoNaturalSizeChange(const gfx::Size& size) final;
105 void OnVideoOpacityChange(bool opaque) final;
106 void OnVideoFrameRateChange(absl::optional<int> fps) final;
107 #if defined(TIZEN_MULTIMEDIA)
108 void OnRequestSuspend(bool resource_conflicted) final;
109 void OnRequestSeek(base::TimeDelta time) final;
110 void OnSeekableTimeChange(base::TimeDelta min_time,
111 base::TimeDelta max_time,
115 // Called when the MediaResourceShim is ready to go (has a config,
116 // pipe handle, etc) and can be handed off to a renderer for use.
117 void OnAllStreamsReady(base::OnceCallback<void(bool)> callback);
119 // Called when |audio_renderer_| initialization has completed.
120 void OnRendererInitializeDone(base::OnceCallback<void(bool)> callback,
121 PipelineStatus status);
123 // Periodically polls the media time from the renderer and notifies the client
124 // if the media time has changed since the last update.
125 // If |force| is true, the client is notified even if the time is unchanged.
126 // If |range| is true, an interpolation time range is reported.
127 void UpdateMediaTime(bool force);
128 void CancelPeriodicMediaTimeUpdates();
129 void SchedulePeriodicMediaTimeUpdates();
131 // Callback executed after seek() completes.
132 #if defined(TIZEN_MULTIMEDIA)
133 void OnSeekCompleted(SeekCallback seek_cb);
136 // Callback executed once Flush() completes.
137 void OnFlushCompleted(FlushCallback callback);
139 // Callback executed once SetCdm() completes.
140 void OnCdmAttached(base::OnceCallback<void(bool)> callback, bool success);
142 const raw_ptr<MojoCdmServiceContext> mojo_cdm_service_context_ = nullptr;
145 double playback_rate_;
147 std::unique_ptr<MediaResource> media_resource_;
149 base::RepeatingTimer time_update_timer_;
150 base::TimeDelta last_media_time_;
152 mojo::AssociatedRemote<mojom::RendererClient> client_;
154 // Holds the CdmContextRef to keep the CdmContext alive for the lifetime of
156 std::unique_ptr<CdmContextRef> cdm_context_ref_;
158 // Note: Destroy |renderer_| first to avoid access violation into other
159 // members, e.g. |media_resource_| and |cdm_|.
160 // Must use "media::" because "Renderer" is ambiguous.
161 std::unique_ptr<media::Renderer> renderer_;
163 base::WeakPtr<MojoRendererService> weak_this_;
164 base::WeakPtrFactory<MojoRendererService> weak_factory_{this};
169 #endif // MEDIA_MOJO_SERVICES_MOJO_RENDERER_SERVICE_H_