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_CLIENTS_MOJO_RENDERER_H_
6 #define MEDIA_MOJO_CLIENTS_MOJO_RENDERER_H_
13 #include "base/memory/raw_ptr.h"
14 #include "base/task/sequenced_task_runner.h"
15 #include "base/time/default_tick_clock.h"
16 #include "base/unguessable_token.h"
17 #include "media/base/demuxer_stream.h"
18 #include "media/base/renderer.h"
19 #include "media/base/time_delta_interpolator.h"
20 #include "media/mojo/mojom/renderer.mojom.h"
21 #include "mojo/public/cpp/bindings/associated_receiver.h"
22 #include "mojo/public/cpp/bindings/pending_remote.h"
23 #include "mojo/public/cpp/bindings/remote.h"
24 #include "third_party/abseil-cpp/absl/types/optional.h"
29 class MojoDemuxerStreamImpl;
30 class VideoOverlayFactory;
31 class VideoRendererSink;
33 // A media::Renderer that proxies to a mojom::Renderer. That
34 // mojom::Renderer proxies back to the MojoRenderer via the
35 // mojom::RendererClient interface.
37 // This class can be created on any thread, where the |remote_renderer| is
38 // connected and passed in the constructor. Then Initialize() will be called on
39 // the |task_runner| and starting from that point this class is bound to the
40 // |task_runner|*. That means all Renderer and RendererClient methods will be
41 // called/dispached on the |task_runner|. The only exception is GetMediaTime(),
42 // which can be called on any thread.
43 class MojoRenderer : public Renderer, public mojom::RendererClient {
45 MojoRenderer(const scoped_refptr<base::SequencedTaskRunner>& task_runner,
46 std::unique_ptr<VideoOverlayFactory> video_overlay_factory,
47 VideoRendererSink* video_renderer_sink,
48 mojo::PendingRemote<mojom::Renderer> remote_renderer);
50 MojoRenderer(const MojoRenderer&) = delete;
51 MojoRenderer& operator=(const MojoRenderer&) = delete;
53 ~MojoRenderer() override;
55 // Renderer implementation.
56 void Initialize(MediaResource* media_resource,
57 media::RendererClient* client,
58 PipelineStatusCallback init_cb) override;
59 void SetCdm(CdmContext* cdm_context, CdmAttachedCB cdm_attached_cb) override;
60 void SetLatencyHint(absl::optional<base::TimeDelta> latency_hint) override;
61 void Flush(base::OnceClosure flush_cb) override;
62 #if defined(TIZEN_MULTIMEDIA)
63 void Seek(base::TimeDelta time, base::OnceClosure seek_cb) override;
64 void Suspend() override;
65 void ToggleFullscreenMode(bool is_fullscreen,
66 ToggledFullscreenCB cb) override;
68 void StartPlayingFrom(base::TimeDelta time) override;
69 void SetPlaybackRate(double playback_rate) override;
70 void SetVolume(float volume) override;
71 base::TimeDelta GetMediaTime() override;
72 RendererType GetRendererType() override;
74 #if defined(TIZEN_VIDEO_HOLE)
75 void SetVideoHole(bool is_video_hole) override;
76 void SetMediaGeometry(const gfx::RectF& rect) override;
79 #if BUILDFLAG(IS_TIZEN_TV)
80 void SetContentMimeType(const std::string& mime_type) override;
81 void SetParentalRatingResult(bool is_pass) override;
85 // mojom::RendererClient implementation, dispatched on the |task_runner_|.
86 void OnTimeUpdate(base::TimeDelta time,
87 base::TimeDelta max_time,
88 base::TimeTicks capture_time) override;
89 void OnBufferingStateChange(BufferingState state,
90 BufferingStateChangeReason reason) override;
91 void OnEnded() override;
92 void OnError(const PipelineStatus& status) override;
93 void OnAudioConfigChange(const AudioDecoderConfig& config) override;
94 void OnVideoConfigChange(const VideoDecoderConfig& config) override;
95 void OnVideoNaturalSizeChange(const gfx::Size& size) override;
96 void OnVideoOpacityChange(bool opaque) override;
97 void OnWaiting(WaitingReason reason) override;
98 void OnStatisticsUpdate(const PipelineStatistics& stats) override;
99 #if defined(TIZEN_MULTIMEDIA)
100 void OnRequestSuspend(bool resource_conflicted) override;
101 void OnRequestSeek(base::TimeDelta time) override;
102 void OnSeekableTimeChange(base::TimeDelta min_time,
103 base::TimeDelta max_time,
104 bool is_live) override;
107 // Binds |remote_renderer_| to the mojo message pipe. Can be called multiple
108 // times. If an error occurs during connection, OnConnectionError will be
109 // called asynchronously.
110 void BindRemoteRendererIfNeeded();
112 // Initialize the remote renderer when |media_resource| is of type
113 // MediaResource::Type::STREAM.
114 void InitializeRendererFromStreams(media::RendererClient* client);
116 // Initialize the remote renderer when |media_resource| is of type
117 // MediaResource::Type::URL.
118 void InitializeRendererFromUrl(media::RendererClient* client);
120 // Callback for connection error on |remote_renderer_|.
121 void OnConnectionError();
123 // Callback for connection error on any of |streams_|. The |stream| parameter
124 // indicates which stream the error happened on.
125 void OnDemuxerStreamConnectionError(MojoDemuxerStreamImpl* stream);
127 // Callbacks for |remote_renderer_| methods.
128 void OnInitialized(media::RendererClient* client, bool success);
130 void OnCdmAttached(bool success);
132 #if defined(TIZEN_MULTIMEDIA)
133 void OnSeekCompleted();
136 void CancelPendingCallbacks();
138 // |task_runner| on which all methods are invoked, except for GetMediaTime(),
139 // which can be called on any thread.
140 scoped_refptr<base::SequencedTaskRunner> task_runner_;
142 // Overlay factory used to create overlays for video frames rendered
143 // by the remote renderer.
144 std::unique_ptr<VideoOverlayFactory> video_overlay_factory_;
146 // Video frame overlays are rendered onto this sink.
147 // Rendering of a new overlay is only needed when video natural size changes.
148 raw_ptr<VideoRendererSink> video_renderer_sink_ = nullptr;
150 // Provider of audio/video DemuxerStreams. Must be valid throughout the
151 // lifetime of |this|.
152 raw_ptr<MediaResource> media_resource_ = nullptr;
154 // Client of |this| renderer passed in Initialize.
155 raw_ptr<media::RendererClient> client_ = nullptr;
157 // Mojo demuxer streams.
158 // Owned by MojoRenderer instead of remote mojom::Renderer
159 // because these demuxer streams need to be destroyed as soon as |this| is
160 // destroyed. The local demuxer streams returned by MediaResource cannot be
161 // used after |this| is destroyed.
162 // TODO(alokp): Add tests for MojoDemuxerStreamImpl.
163 std::vector<std::unique_ptr<MojoDemuxerStreamImpl>> streams_;
165 // This class is constructed on one thread and used exclusively on another
166 // thread. This member is used to safely pass the PendingRemote from one
167 // thread to another. It is set in the constructor and is consumed in
169 mojo::PendingRemote<mojom::Renderer> remote_renderer_pending_remote_;
171 // Remote Renderer, bound to |task_runner_| during Initialize().
172 mojo::Remote<mojom::Renderer> remote_renderer_;
174 // Receiver for RendererClient, bound to the |task_runner_|.
175 mojo::AssociatedReceiver<RendererClient> client_receiver_{this};
177 bool encountered_error_ = false;
179 PipelineStatusCallback init_cb_;
180 base::OnceClosure flush_cb_;
181 CdmAttachedCB cdm_attached_cb_;
183 #if defined(TIZEN_MULTIMEDIA)
184 base::OnceClosure seek_cb_;
187 float volume_ = 1.0f;
189 // Lock used to serialize access for |time_interpolator_|.
190 mutable base::Lock lock_;
191 media::TimeDeltaInterpolator media_time_interpolator_;
193 absl::optional<PipelineStatistics> pending_stats_;
198 #endif // MEDIA_MOJO_CLIENTS_MOJO_RENDERER_H_