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);
89 // Proxy functions for MediaPlayerRendererClientExtension
90 void OnVideoSizeChange(const gfx::Size& size);
91 void OnDurationChange(base::TimeDelta duration);
92 void OnBufferUpdate(base::TimeDelta time);
94 #if defined(TIZEN_VIDEO_HOLE)
95 void SetVideoHole(bool is_video_hole) final;
96 void SetMediaGeometry(const gfx::RectF& rect) final;
98 gfx::Rect GetViewportRect() const;
101 void OnNewFrameAvailable(uint32_t playerId,
102 base::UnsafeSharedMemoryRegion frame,
104 base::TimeDelta timestamp,
108 #if defined(TIZEN_TBM_SUPPORT)
109 void OnNewTbmFrameAvailable(uint32_t player_id,
110 gfx::TbmBufferHandle tbm_handle,
111 base::TimeDelta timestamp);
112 void OnTbmBufferExhausted(const gfx::TbmBufferHandle& tbm_handle) override;
116 class RendererClientInternal;
117 const float kDefaultVolume = 1.0;
121 STATE_INIT_PENDING_CDM, // Initialization is waiting for the CDM to be set.
122 STATE_INITIALIZING, // Initializing audio/video renderers.
123 STATE_FLUSHING, // Flushing is in progress.
124 STATE_FLUSHED, // After initialization or after flush completed.
125 STATE_PLAYING, // After StartPlayingFrom has been called.
129 void SetStreamInfo();
130 void SetPlayerVolume();
131 #if defined(TIZEN_VIDEO_HOLE)
132 void SetPlayerVideoHole();
133 void SetPlayerMediaGeometry();
135 void OnRendererEnded();
136 void OnError(media::PipelineStatus error);
137 void OnStatisticsUpdate(const media::PipelineStatistics& stats);
138 void OnBufferingStateChange(media::BufferingState new_buffering_state,
139 media::BufferingStateChangeReason reason);
140 void OnWaiting(media::WaitingReason reason);
141 void OnAudioConfigChange(const media::AudioDecoderConfig& config);
142 void OnVideoConfigChange(const media::VideoDecoderConfig& config);
143 void OnVideoNaturalSizeChange(const gfx::Size& size);
144 void OnVideoOpacityChange(bool opaque);
145 void OnVideoFrameRateChange(absl::optional<int> fps);
147 media::RendererClient* client_;
151 // Task runner used to execute pipeline tasks.
152 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
154 media::VideoRendererSink* const sink_ = nullptr;
155 mojo::Remote<ClientExtension> client_extension_;
156 // Identifiers to find the RenderFrameHost that created |this|.
157 // NOTE: We store these IDs rather than a RenderFrameHost* because we do not
158 // know when the RenderFrameHost is destroyed.
159 int render_process_id_;
162 // Temporary callback used for Initialize() and Flush().
163 media::PipelineStatusCallback init_cb_;
164 base::OnceClosure flush_cb_;
166 std::unique_ptr<RendererClientInternal> audio_renderer_client_;
167 std::unique_ptr<RendererClientInternal> video_renderer_client_;
169 media::MediaResource* media_resource_;
170 media::DemuxerStream* audio_stream_;
171 media::DemuxerStream* video_stream_;
173 std::unique_ptr<media::MediaPlayerTizen> media_player_;
174 double volume_ = kDefaultVolume;
175 double playback_rate_ = 0.0;
177 // The time to start playback from after starting/seeking has completed.
178 base::TimeDelta start_time_;
180 media::BufferingState audio_buffering_state_;
181 media::BufferingState video_buffering_state_;
183 // Whether we've received the audio/video ended events.
184 bool media_ended_ = false;
185 bool web_contents_muted_ = false;
186 mojo::Receiver<MediaPlayerRendererExtension> renderer_extension_receiver_;
187 raw_ptr<content::MediaPlayerRendererWebContentsObserver>
188 web_contents_observer_;
190 // Indicates if a serious error has been encountered by the |media_player_|.
191 bool has_error_ = false;
193 #if defined(TIZEN_VIDEO_HOLE)
194 bool is_video_hole_ = false;
195 gfx::RectF video_rect_;
198 WebContents* web_contents_ = nullptr;
200 base::WeakPtrFactory<TizenRendererImpl> weak_factory_{this};
203 } // namespace content
205 #endif // MEDIA_RENDERERS_TIZEN_RENDERER_IMPL_H_