1 // Copyright 2016 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_MOJO_VIDEO_DECODER_SERVICE_H_
6 #define MEDIA_MOJO_SERVICES_MOJO_VIDEO_DECODER_SERVICE_H_
11 #include "base/memory/raw_ptr.h"
12 #include "base/memory/scoped_refptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/unguessable_token.h"
15 #include "media/base/cdm_context.h"
16 #include "media/base/decoder_status.h"
17 #include "media/base/overlay_info.h"
18 #include "media/base/video_decoder.h"
19 #include "media/mojo/mojom/stable/stable_video_decoder.mojom.h"
20 #include "media/mojo/mojom/video_decoder.mojom.h"
21 #include "media/mojo/services/media_mojo_export.h"
22 #include "media/mojo/services/mojo_media_client.h"
23 #include "mojo/public/cpp/bindings/associated_remote.h"
24 #include "mojo/public/cpp/bindings/pending_associated_remote.h"
25 #include "mojo/public/cpp/bindings/pending_remote.h"
26 #include "mojo/public/cpp/bindings/self_owned_receiver.h"
27 #include "third_party/abseil-cpp/absl/types/optional.h"
32 class MojoCdmServiceContext;
33 class MojoDecoderBufferReader;
34 class MojoMediaClient;
38 // Implementation of a mojom::VideoDecoder which runs in the GPU process,
39 // and wraps a media::VideoDecoder.
40 class MEDIA_MOJO_EXPORT MojoVideoDecoderService final
41 : public mojom::VideoDecoder {
43 explicit MojoVideoDecoderService(
44 MojoMediaClient* mojo_media_client,
45 MojoCdmServiceContext* mojo_cdm_service_context,
46 mojo::PendingRemote<stable::mojom::StableVideoDecoder>
47 oop_video_decoder_remote);
49 MojoVideoDecoderService(const MojoVideoDecoderService&) = delete;
50 MojoVideoDecoderService& operator=(const MojoVideoDecoderService&) = delete;
52 ~MojoVideoDecoderService() final;
54 // mojom::VideoDecoder implementation
55 void GetSupportedConfigs(GetSupportedConfigsCallback callback) final;
57 mojo::PendingAssociatedRemote<mojom::VideoDecoderClient> client,
58 mojo::PendingRemote<mojom::MediaLog> media_log,
59 mojo::PendingReceiver<mojom::VideoFrameHandleReleaser>
60 video_frame_handle_receiver,
61 mojo::ScopedDataPipeConsumerHandle decoder_buffer_pipe,
62 mojom::CommandBufferIdPtr command_buffer_id,
63 const gfx::ColorSpace& target_color_space) final;
64 void Initialize(const VideoDecoderConfig& config,
66 const absl::optional<base::UnguessableToken>& cdm_id,
67 InitializeCallback callback) final;
68 void Decode(mojom::DecoderBufferPtr buffer, DecodeCallback callback) final;
69 void Reset(ResetCallback callback) final;
70 void OnOverlayInfoChanged(const OverlayInfo& overlay_info) final;
73 // Helper methods so that we can bind them with a weak pointer to avoid
74 // running mojom::VideoDecoder callbacks after connection error happens and
75 // |this| is deleted. It's not safe to run the callbacks after a connection
77 void OnDecoderInitialized(DecoderStatus status);
78 void OnReaderRead(DecodeCallback callback,
79 std::unique_ptr<ScopedDecodeTrace> trace_event,
80 scoped_refptr<DecoderBuffer> buffer);
81 void OnDecoderDecoded(DecodeCallback callback,
82 std::unique_ptr<ScopedDecodeTrace> trace_event,
83 DecoderStatus status);
85 // Called by |mojo_decoder_buffer_reader_| when reset is finished.
86 void OnReaderFlushed();
88 void OnDecoderReset();
89 void OnDecoderOutput(scoped_refptr<VideoFrame> frame);
91 void OnDecoderWaiting(WaitingReason reason);
93 void OnDecoderRequestedOverlayInfo(
94 bool restart_for_transitions,
95 ProvideOverlayInfoCB provide_overlay_info_cb);
97 // Whether this instance is active (Decode() was called at least once).
98 bool is_active_instance_ = false;
100 // Codec information stored via crash key.
101 std::string codec_string_;
104 raw_ptr<MojoMediaClient, LeakedDanglingUntriaged> mojo_media_client_;
106 // A helper object required to get the CDM from a CDM ID.
107 raw_ptr<MojoCdmServiceContext, LeakedDanglingUntriaged>
108 mojo_cdm_service_context_ = nullptr;
110 // Channel for sending async messages to the client.
111 mojo::AssociatedRemote<mojom::VideoDecoderClient> client_;
113 // Proxy object for providing media log services.
114 std::unique_ptr<MojoMediaLog> media_log_;
116 // Holds VideoFrame references on behalf of the client, until the client
117 // releases them or is disconnected.
118 mojo::SelfOwnedReceiverRef<mojom::VideoFrameHandleReleaser>
119 video_frame_handle_releaser_;
121 // Helper for reading DecoderBuffer data from the DataPipe.
122 std::unique_ptr<MojoDecoderBufferReader> mojo_decoder_buffer_reader_;
124 // The CDM ID and the corresponding CdmContextRef, which must be held to keep
125 // the CdmContext alive for the lifetime of the |decoder_|.
126 absl::optional<base::UnguessableToken> cdm_id_;
127 std::unique_ptr<CdmContextRef> cdm_context_ref_;
129 std::unique_ptr<media::VideoDecoder> decoder_;
131 // An out-of-process video decoder to forward decode requests to. This member
132 // just holds the PendingRemote in between the construction of the
133 // MojoVideoDecoderService and the call to
134 // |mojo_media_client_|->CreateVideoDecoder().
135 mojo::PendingRemote<stable::mojom::StableVideoDecoder>
136 oop_video_decoder_pending_remote_;
138 InitializeCallback init_cb_;
139 ResetCallback reset_cb_;
141 ProvideOverlayInfoCB provide_overlay_info_cb_;
143 base::WeakPtr<MojoVideoDecoderService> weak_this_;
144 base::WeakPtrFactory<MojoVideoDecoderService> weak_factory_{this};
149 #endif // MEDIA_MOJO_SERVICES_MOJO_VIDEO_DECODER_SERVICE_H_