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 "ui/gfx/geometry/size.h"
29 class SingleThreadTaskRunner;
33 class MediaPlayerTizen;
40 class MediaPlayerRendererWebContentsObserver;
42 class CONTENT_EXPORT TizenRendererImpl
43 : public media::Renderer,
44 public media::mojom::MediaPlayerRendererExtension {
46 using RendererExtension = media::mojom::MediaPlayerRendererExtension;
47 using ClientExtension = media::mojom::MediaPlayerRendererClientExtension;
50 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
51 media::VideoRendererSink* sink);
55 WebContents* web_contents,
56 mojo::PendingReceiver<RendererExtension> renderer_extension_receiver,
57 mojo::PendingRemote<ClientExtension> client_extension_remote);
58 TizenRendererImpl(const TizenRendererImpl&) = delete;
59 TizenRendererImpl& operator=(const TizenRendererImpl&) = delete;
62 // Renderer implementation.
63 void Initialize(media::MediaResource* media_resource,
64 media::RendererClient* client,
65 media::PipelineStatusCallback init_cb) override;
66 void SetCdm(media::CdmContext* cdm_context,
67 CdmAttachedCB cdm_attached_cb) override;
68 void SetLatencyHint(absl::optional<base::TimeDelta> latency_hint) override;
69 void SetPreservesPitch(bool preserves_pitch) override;
70 void Flush(base::OnceClosure flush_cb) override;
71 void StartPlayingFrom(base::TimeDelta time) override;
72 void SetPlaybackRate(double playback_rate) override;
73 void SetVolume(float volume) override;
74 base::TimeDelta GetMediaTime() override;
75 void OnSelectedVideoTracksChanged(
76 const std::vector<media::DemuxerStream*>& enabled_tracks,
77 base::OnceClosure change_completed_cb) override;
78 void OnEnabledAudioTracksChanged(
79 const std::vector<media::DemuxerStream*>& enabled_tracks,
80 base::OnceClosure change_completed_cb) override;
81 void InitiateScopedSurfaceRequest(
82 InitiateScopedSurfaceRequestCallback callback) override;
83 void OnUpdateAudioMutingState(bool muted) {}
84 void OnWebContentsDestroyed();
87 void Seek(base::TimeDelta time, base::OnceClosure seek_cb);
89 void Suspend() override;
91 // Proxy functions for MediaPlayerRendererClientExtension
92 void OnVideoSizeChange(const gfx::Size& size);
93 void OnDurationChange(base::TimeDelta duration);
94 void OnBufferUpdate(base::TimeDelta time);
96 #if defined(TIZEN_VIDEO_HOLE)
97 void SetVideoHole(bool is_video_hole) final;
98 void SetMediaGeometry(const gfx::RectF& rect) final;
100 gfx::Rect GetViewportRect() const;
103 void OnNewFrameAvailable(uint32_t playerId,
104 base::UnsafeSharedMemoryRegion frame,
106 base::TimeDelta timestamp,
110 #if defined(TIZEN_TBM_SUPPORT)
111 void OnNewTbmFrameAvailable(uint32_t player_id,
112 gfx::TbmBufferHandle tbm_handle,
113 base::TimeDelta timestamp);
114 void OnTbmBufferExhausted(const gfx::TbmBufferHandle& tbm_handle) override;
117 void OnRequestSuspend(bool resource_conflict);
120 class RendererClientInternal;
121 const float kDefaultVolume = 1.0;
125 STATE_INIT_PENDING_CDM, // Initialization is waiting for the CDM to be set.
126 STATE_INITIALIZING, // Initializing audio/video renderers.
127 STATE_FLUSHING, // Flushing is in progress.
128 STATE_FLUSHED, // After initialization or after flush completed.
129 STATE_PLAYING, // After StartPlayingFrom has been called.
133 void SetStreamInfo();
134 void SetPlayerVolume();
135 #if defined(TIZEN_VIDEO_HOLE)
136 void SetPlayerVideoHole();
137 void SetPlayerMediaGeometry();
139 void OnRendererEnded();
140 void OnError(media::PipelineStatus error);
141 void OnStatisticsUpdate(const media::PipelineStatistics& stats);
142 void OnBufferingStateChange(media::BufferingState new_buffering_state,
143 media::BufferingStateChangeReason reason);
144 void OnWaiting(media::WaitingReason reason);
145 void OnAudioConfigChange(const media::AudioDecoderConfig& config);
146 void OnVideoConfigChange(const media::VideoDecoderConfig& config);
147 void OnVideoNaturalSizeChange(const gfx::Size& size);
148 void OnVideoOpacityChange(bool opaque);
149 void OnVideoFrameRateChange(absl::optional<int> fps);
151 void OnRequestSeek(base::TimeDelta time);
153 media::RendererClient* client_ = nullptr;
155 State state_ = STATE_UNINITIALIZED;
157 // Task runner used to execute pipeline tasks.
158 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
160 media::VideoRendererSink* const sink_ = nullptr;
161 mojo::Remote<ClientExtension> client_extension_;
165 // Identifiers to find the RenderFrameHost that created |this|.
166 // NOTE: We store these IDs rather than a RenderFrameHost* because we do not
167 // know when the RenderFrameHost is destroyed.
168 int render_process_id_ = 0;
171 // Temporary callback used for Initialize().
172 media::PipelineStatusCallback init_cb_;
174 std::unique_ptr<RendererClientInternal> audio_renderer_client_;
175 std::unique_ptr<RendererClientInternal> video_renderer_client_;
177 media::MediaResource* media_resource_ = nullptr;
178 media::DemuxerStream* audio_stream_ = nullptr;
179 media::DemuxerStream* video_stream_ = nullptr;
181 std::unique_ptr<media::MediaPlayerTizen> media_player_;
182 double volume_ = kDefaultVolume;
183 double playback_rate_ = 0.0;
185 // The time to start playback from after starting/seeking has completed.
186 base::TimeDelta start_time_;
188 media::BufferingState audio_buffering_state_ = media::BUFFERING_HAVE_NOTHING;
189 media::BufferingState video_buffering_state_ = media::BUFFERING_HAVE_NOTHING;
191 // Whether we've received the audio/video ended events.
192 bool media_ended_ = false;
193 bool web_contents_muted_ = false;
194 mojo::Receiver<MediaPlayerRendererExtension> renderer_extension_receiver_;
195 raw_ptr<content::MediaPlayerRendererWebContentsObserver>
196 web_contents_observer_ = nullptr;
198 // Indicates if a serious error has been encountered by the |media_player_|.
199 bool has_error_ = false;
200 bool resource_conflicted_ = true;
201 bool is_suspended_ = false;
203 #if defined(TIZEN_VIDEO_HOLE)
204 bool is_video_hole_ = false;
205 gfx::RectF video_rect_;
208 WebContents* web_contents_ = nullptr;
210 base::WeakPtrFactory<TizenRendererImpl> weak_factory_{this};
213 } // namespace content
215 #endif // MEDIA_RENDERERS_TIZEN_RENDERER_IMPL_H_