[M120 Migration][MM][CAPI] Fix the logic for media using capi player.
[platform/framework/web/chromium-efl.git] / media / mojo / services / mojo_renderer_service.h
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.
4
5 #ifndef MEDIA_MOJO_SERVICES_MOJO_RENDERER_SERVICE_H_
6 #define MEDIA_MOJO_SERVICES_MOJO_RENDERER_SERVICE_H_
7
8 #include <stdint.h>
9 #include <memory>
10
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"
30
31 namespace media {
32
33 class CdmContextRef;
34 class MediaResourceShim;
35 class MojoCdmServiceContext;
36 class Renderer;
37
38 // A mojom::Renderer implementation that use a media::Renderer to render
39 // media streams.
40 class MEDIA_MOJO_EXPORT MojoRendererService final : public mojom::Renderer,
41                                                     public RendererClient {
42  public:
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);
49
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);
54
55   MojoRendererService(const MojoRendererService&) = delete;
56   MojoRendererService& operator=(const MojoRendererService&) = delete;
57
58   ~MojoRendererService() final;
59
60   // mojom::Renderer implementation.
61   void Initialize(
62       mojo::PendingAssociatedRemote<mojom::RendererClient> client,
63       absl::optional<std::vector<mojo::PendingRemote<mojom::DemuxerStream>>>
64           streams,
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;
70   void Suspend() final;
71   void ToggleFullscreenMode(bool is_fullscreen,
72                             ToggleFullscreenModeCallback callback) final;
73 #endif
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;
79
80 #if defined(TIZEN_VIDEO_HOLE)
81   void SetVideoHole(bool is_video_hole) final;
82   void SetMediaGeometry(const gfx::RectF& rect) final;
83 #endif
84
85  private:
86   enum State {
87     STATE_UNINITIALIZED,
88     STATE_INITIALIZING,
89     STATE_FLUSHING,
90     STATE_PLAYING,
91     STATE_ERROR
92   };
93
94   // RendererClient implementation.
95   void OnError(PipelineStatus status) final;
96   void OnFallback(PipelineStatus status) final;
97   void OnEnded() 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,
112                             bool is_live) final;
113 #endif
114
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);
118
119   // Called when |audio_renderer_| initialization has completed.
120   void OnRendererInitializeDone(base::OnceCallback<void(bool)> callback,
121                                 PipelineStatus status);
122
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();
130
131   // Callback executed after seek() completes.
132 #if defined(TIZEN_MULTIMEDIA)
133   void OnSeekCompleted(SeekCallback seek_cb);
134 #endif
135
136   // Callback executed once Flush() completes.
137   void OnFlushCompleted(FlushCallback callback);
138
139   // Callback executed once SetCdm() completes.
140   void OnCdmAttached(base::OnceCallback<void(bool)> callback, bool success);
141
142   const raw_ptr<MojoCdmServiceContext> mojo_cdm_service_context_ = nullptr;
143
144   State state_;
145   double playback_rate_;
146
147   std::unique_ptr<MediaResource> media_resource_;
148
149   base::RepeatingTimer time_update_timer_;
150   base::TimeDelta last_media_time_;
151
152   mojo::AssociatedRemote<mojom::RendererClient> client_;
153
154   // Holds the CdmContextRef to keep the CdmContext alive for the lifetime of
155   // the |renderer_|.
156   std::unique_ptr<CdmContextRef> cdm_context_ref_;
157
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_;
162
163   base::WeakPtr<MojoRendererService> weak_this_;
164   base::WeakPtrFactory<MojoRendererService> weak_factory_{this};
165 };
166
167 }  // namespace media
168
169 #endif  // MEDIA_MOJO_SERVICES_MOJO_RENDERER_SERVICE_H_