1 // Copyright 2022 Samsung Electronics Inc. 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_RENDERERS_TIZEN_RENDERER_IMPL_H_
6 #define MEDIA_RENDERERS_TIZEN_RENDERER_IMPL_H_
10 #include "base/memory/raw_ptr.h"
11 #include "content/common/content_export.h"
12 #include "media/base/demuxer_stream.h"
13 #include "media/base/media_export.h"
14 #include "media/base/media_resource.h"
15 #include "media/base/pipeline_status.h"
16 #include "media/base/renderer.h"
17 #include "media/base/renderer_client.h"
18 #include "media/base/video_renderer_sink.h"
19 #include "media/base/waiting.h"
20 #include "media/mojo/mojom/renderer_extensions.mojom.h"
21 #include "mojo/public/cpp/bindings/pending_receiver.h"
22 #include "mojo/public/cpp/bindings/pending_remote.h"
23 #include "mojo/public/cpp/bindings/receiver.h"
24 #include "mojo/public/cpp/bindings/remote.h"
25 #include "third_party/abseil-cpp/absl/types/optional.h"
26 #include "tizen_src/chromium_impl/media/filters/media_player_tizen_client.h"
27 #include "ui/gfx/geometry/size.h"
30 class SingleThreadTaskRunner;
34 class MediaPlayerTizen;
41 class MediaPlayerRendererWebContentsObserver;
43 class CONTENT_EXPORT TizenRendererImpl
44 : public media::Renderer,
45 public media::mojom::MediaPlayerRendererExtension,
46 public media::MediaPlayerTizenClient {
48 using RendererExtension = media::mojom::MediaPlayerRendererExtension;
49 using ClientExtension = media::mojom::MediaPlayerRendererClientExtension;
52 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
53 media::VideoRendererSink* sink);
57 WebContents* web_contents,
58 mojo::PendingReceiver<RendererExtension> renderer_extension_receiver,
59 mojo::PendingRemote<ClientExtension> client_extension_remote);
60 TizenRendererImpl(const TizenRendererImpl&) = delete;
61 TizenRendererImpl& operator=(const TizenRendererImpl&) = delete;
64 // Renderer implementation.
65 void Initialize(media::MediaResource* media_resource,
66 media::RendererClient* client,
67 media::PipelineStatusCallback init_cb) override;
68 void SetCdm(media::CdmContext* cdm_context,
69 CdmAttachedCB cdm_attached_cb) override;
70 void SetLatencyHint(absl::optional<base::TimeDelta> latency_hint) override;
71 void SetPreservesPitch(bool preserves_pitch) override;
72 void Flush(base::OnceClosure flush_cb) override;
73 void StartPlayingFrom(base::TimeDelta time) override;
74 void SetPlaybackRate(double playback_rate) override;
75 void SetVolume(float volume) override;
76 base::TimeDelta GetMediaTime() override;
77 media::RendererType GetRendererType() override;
78 void OnSelectedVideoTracksChanged(
79 const std::vector<media::DemuxerStream*>& enabled_tracks,
80 base::OnceClosure change_completed_cb) override;
81 void OnEnabledAudioTracksChanged(
82 const std::vector<media::DemuxerStream*>& enabled_tracks,
83 base::OnceClosure change_completed_cb) override;
84 void InitiateScopedSurfaceRequest(
85 InitiateScopedSurfaceRequestCallback callback) override;
86 void OnUpdateAudioMutingState(bool muted) {}
87 void OnWebContentsDestroyed();
89 void Seek(base::TimeDelta time, base::OnceClosure seek_cb);
91 void Suspend() override;
93 void ToggleFullscreenMode(bool is_fullscreen, ToggledFullscreenCB cb) final;
95 // MediaPlayerTizenClient implementation.
96 void OnError(media::PipelineStatus error) override;
97 void OnFallback(media::PipelineStatus fallback) override {}
98 void OnEnded() override;
99 void OnStatisticsUpdate(const media::PipelineStatistics& stats) override;
100 void OnSeekableTimeChange(base::TimeDelta min_time,
101 base::TimeDelta max_time,
102 bool is_live) override;
103 void OnBufferingStateChange(
104 media::BufferingState new_buffering_state,
105 media::BufferingStateChangeReason reason) override;
106 void OnWaiting(media::WaitingReason reason) override;
107 void OnAudioConfigChange(const media::AudioDecoderConfig& config) override;
108 void OnVideoConfigChange(const media::VideoDecoderConfig& config) override;
109 void OnVideoNaturalSizeChange(const gfx::Size& size) override;
110 void OnVideoOpacityChange(bool opaque) override;
111 void OnVideoFrameRateChange(absl::optional<int> fps) override;
112 void OnRequestSeek(base::TimeDelta time) override;
113 void OnRequestSuspend(bool resource_conflict) override;
114 void OnVideoSizeChange(const gfx::Size& size) override;
115 void OnDurationChange(base::TimeDelta duration) override;
116 void OnBufferUpdate(base::TimeDelta time) override;
117 #if defined(TIZEN_TBM_SUPPORT)
118 void OnNewTbmFrameAvailable(uint32_t player_id,
119 gfx::TbmBufferHandle tbm_handle,
120 base::TimeDelta timestamp) override;
121 void OnTbmBufferExhausted(const gfx::TbmBufferHandle& tbm_handle) override;
123 void OnNewFrameAvailable(uint32_t playerId,
124 base::UnsafeSharedMemoryRegion frame,
126 base::TimeDelta timestamp,
128 uint32_t height) override;
131 #if BUILDFLAG(IS_TIZEN_TV)
132 content::WebContentsDelegate* GetWebContentsDelegate() const override;
135 #if defined(TIZEN_VIDEO_HOLE)
136 void SetVideoHole(bool is_video_hole) final;
137 void SetMediaGeometry(const gfx::RectF& rect) final;
138 gfx::Rect GetViewportRect() const;
142 const float kDefaultVolume = 1.0;
146 STATE_INIT_PENDING_CDM, // Initialization is waiting for the CDM to be set.
147 STATE_INITIALIZING, // Initializing audio/video renderers.
148 STATE_FLUSHING, // Flushing is in progress.
149 STATE_FLUSHED, // After initialization or after flush completed.
150 STATE_PLAYING, // After StartPlayingFrom has been called.
154 void SetStreamInfo();
155 void SetPlayerVolume();
156 void SetPlayerInitialize();
157 void SetPlayerPrepare();
158 #if defined(TIZEN_VIDEO_HOLE)
159 void SetPlayerVideoHole();
160 void SetPlayerMediaGeometry();
161 void OnWebViewMoved();
163 content::WebContents* GetWebContents() const;
165 media::RendererClient* client_ = nullptr;
167 State state_ = STATE_UNINITIALIZED;
169 // Task runner used to execute pipeline tasks.
170 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
172 media::VideoRendererSink* const sink_ = nullptr;
173 mojo::Remote<ClientExtension> client_extension_;
177 // Identifiers to find the RenderFrameHost that created |this|.
178 // NOTE: We store these IDs rather than a RenderFrameHost* because we do not
179 // know when the RenderFrameHost is destroyed.
180 int render_process_id_ = 0;
183 // Temporary callback used for Initialize().
184 media::PipelineStatusCallback init_cb_;
186 media::MediaResource* media_resource_ = nullptr;
187 media::DemuxerStream* audio_stream_ = nullptr;
188 media::DemuxerStream* video_stream_ = nullptr;
190 std::unique_ptr<media::MediaPlayerTizen> media_player_;
191 double volume_ = kDefaultVolume;
192 double playback_rate_ = 0.0;
194 // The time to start playback from after starting/seeking has completed.
195 base::TimeDelta start_time_;
197 media::BufferingState audio_buffering_state_ = media::BUFFERING_HAVE_NOTHING;
198 media::BufferingState video_buffering_state_ = media::BUFFERING_HAVE_NOTHING;
200 // Whether we've received the audio/video ended events.
201 bool media_ended_ = false;
202 bool web_contents_muted_ = false;
203 mojo::Receiver<MediaPlayerRendererExtension> renderer_extension_receiver_;
204 raw_ptr<content::MediaPlayerRendererWebContentsObserver>
205 web_contents_observer_ = nullptr;
207 // Indicates if a serious error has been encountered by the |media_player_|.
208 bool has_error_ = false;
209 bool resource_conflicted_ = true;
210 bool is_suspended_ = false;
212 #if defined(TIZEN_VIDEO_HOLE)
213 bool is_video_hole_ = false;
214 gfx::RectF video_rect_;
217 WebContents* web_contents_ = nullptr;
219 base::WeakPtrFactory<TizenRendererImpl> weak_factory_{this};
222 } // namespace content
224 #endif // MEDIA_RENDERERS_TIZEN_RENDERER_IMPL_H_