1 // Copyright 2021 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_SERVICES_MEDIA_FOUNDATION_RENDERER_WRAPPER_H_
6 #define MEDIA_MOJO_SERVICES_MEDIA_FOUNDATION_RENDERER_WRAPPER_H_
8 #include "base/callback_list.h"
9 #include "base/functional/callback.h"
10 #include "base/memory/raw_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/task/sequenced_task_runner.h"
13 #include "media/base/media_resource.h"
14 #include "media/base/pipeline_status.h"
15 #include "media/base/renderer.h"
16 #include "media/base/renderer_client.h"
17 #include "media/mojo/mojom/dcomp_surface_registry.mojom.h"
18 #include "media/mojo/mojom/frame_interface_factory.mojom.h"
19 #include "media/mojo/mojom/renderer_extensions.mojom.h"
20 #include "media/renderers/win/media_foundation_renderer.h"
21 #include "mojo/public/cpp/bindings/pending_receiver.h"
22 #include "mojo/public/cpp/bindings/receiver.h"
23 #include "mojo/public/cpp/bindings/remote.h"
27 // Wrap media::MediaFoundationRenderer to remove its dependence on
28 // media::mojom::MediaFoundationRendererExtension interface.
29 class MediaFoundationRendererWrapper final
31 public mojom::MediaFoundationRendererExtension,
32 public mojom::MuteStateObserver {
34 using RendererExtension = mojom::MediaFoundationRendererExtension;
35 using ClientExtension = mojom::MediaFoundationRendererClientExtension;
37 MediaFoundationRendererWrapper(
38 scoped_refptr<base::SequencedTaskRunner> task_runner,
39 mojom::FrameInterfaceFactory* frame_interfaces,
40 mojo::PendingRemote<mojom::MediaLog> media_log_remote,
41 mojo::PendingReceiver<RendererExtension> renderer_extension_receiver,
42 mojo::PendingRemote<ClientExtension> client_extension_remote);
43 MediaFoundationRendererWrapper(const MediaFoundationRendererWrapper&) =
45 MediaFoundationRendererWrapper operator=(
46 const MediaFoundationRendererWrapper&) = delete;
47 ~MediaFoundationRendererWrapper() override;
49 // Renderer implementation.
50 void Initialize(MediaResource* media_resource,
51 RendererClient* client,
52 PipelineStatusCallback init_cb) override;
53 void SetCdm(CdmContext* cdm_context, CdmAttachedCB cdm_attached_cb) override;
54 void SetLatencyHint(absl::optional<base::TimeDelta> latency_hint) override;
55 void Flush(base::OnceClosure flush_cb) override;
56 void StartPlayingFrom(base::TimeDelta time) override;
57 void SetPlaybackRate(double playback_rate) override;
58 void SetVolume(float volume) override;
59 base::TimeDelta GetMediaTime() override;
60 RendererType GetRendererType() override;
62 // mojom::MediaFoundationRendererExtension implementation.
63 void GetDCOMPSurface(GetDCOMPSurfaceCallback callback) override;
64 void SetVideoStreamEnabled(bool enabled) override;
65 void SetOutputRect(const gfx::Rect& output_rect,
66 SetOutputRectCallback callback) override;
67 void NotifyFrameReleased(const base::UnguessableToken& frame_token) override;
68 void RequestNextFrame() override;
69 void SetMediaFoundationRenderingMode(
70 MediaFoundationRenderingMode mode) override;
72 // mojom::MuteStateObserver implementation.
73 void OnMuteStateChange(bool muted) override;
76 void OnGpuLuidChange(const CHROME_LUID& adapter_luid);
77 void OnReceiveDCOMPSurface(GetDCOMPSurfaceCallback callback,
78 base::win::ScopedHandle handle,
79 const std::string& error);
80 void OnDCOMPSurfaceHandleRegistered(
81 GetDCOMPSurfaceCallback callback,
82 const absl::optional<base::UnguessableToken>& token);
83 void OnFrameGeneratedByMediaFoundation(
84 const base::UnguessableToken& frame_token,
85 const gfx::Size& frame_size,
86 base::TimeDelta frame_timestamp);
87 void OnFramePoolInitialized(
88 std::vector<MediaFoundationFrameInfo> frame_textures,
89 const gfx::Size& texture_size);
91 raw_ptr<mojom::FrameInterfaceFactory, FlakyDanglingUntriaged>
93 std::unique_ptr<MediaFoundationRenderer> renderer_;
94 mojo::Receiver<MediaFoundationRendererExtension> renderer_extension_receiver_;
95 mojo::Remote<media::mojom::MediaFoundationRendererClientExtension>
96 client_extension_remote_;
97 mojo::Receiver<mojom::MuteStateObserver> site_mute_observer_;
99 base::CallbackListSubscription luid_update_subscription_;
102 bool muted_ = false; // Whether the site (WebContents) is muted.
104 bool has_get_dcomp_surface_called_ = false;
105 mojo::Remote<mojom::DCOMPSurfaceRegistry> dcomp_surface_registry_;
106 base::UnguessableToken dcomp_surface_token_;
108 base::WeakPtrFactory<MediaFoundationRendererWrapper> weak_factory_{this};
113 #endif // MEDIA_MOJO_SERVICES_MEDIA_FOUNDATION_RENDERER_WRAPPER_H_