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.
5 #ifndef MEDIA_MOJO_SERVICES_MOJO_DEMUXER_STREAM_ADAPTER_H_
6 #define MEDIA_MOJO_SERVICES_MOJO_DEMUXER_STREAM_ADAPTER_H_
10 #include "base/memory/weak_ptr.h"
11 #include "media/base/audio_decoder_config.h"
12 #include "media/base/demuxer_stream.h"
13 #include "media/base/video_decoder_config.h"
14 #include "media/mojo/mojom/demuxer_stream.mojom.h"
15 #include "media/mojo/services/media_mojo_export.h"
16 #include "mojo/public/cpp/bindings/pending_remote.h"
17 #include "mojo/public/cpp/bindings/remote.h"
18 #include "third_party/abseil-cpp/absl/types/optional.h"
22 class MojoDecoderBufferReader;
24 // This class acts as a MojoRendererService-side stub for a real DemuxerStream
25 // that is part of a Pipeline in a remote application. Roughly speaking, it
26 // takes a mojo::Remote<mojom::DemuxerStream> and exposes it as a DemuxerStream
27 // for use by media components.
28 class MEDIA_MOJO_EXPORT MojoDemuxerStreamAdapter : public DemuxerStream {
30 // |demuxer_stream| is connected to the mojom::DemuxerStream that |this|
32 // become the client of.
33 // |stream_ready_cb| will be invoked when |demuxer_stream| has fully
34 // initialized and |this| is ready for use.
35 // NOTE: Illegal to call any methods until |stream_ready_cb| is invoked.
36 MojoDemuxerStreamAdapter(
37 mojo::PendingRemote<mojom::DemuxerStream> demuxer_stream,
38 base::OnceClosure stream_ready_cb);
40 MojoDemuxerStreamAdapter(const MojoDemuxerStreamAdapter&) = delete;
41 MojoDemuxerStreamAdapter& operator=(const MojoDemuxerStreamAdapter&) = delete;
43 ~MojoDemuxerStreamAdapter() override;
45 // DemuxerStream implementation.
46 void Read(uint32_t count, ReadCB read_cb) override;
47 AudioDecoderConfig audio_decoder_config() override;
48 VideoDecoderConfig video_decoder_config() override;
49 Type type() const override;
50 void EnableBitstreamConverter() override;
51 bool SupportsConfigChanges() override;
54 void OnStreamReady(Type type,
55 mojo::ScopedDataPipeConsumerHandle consumer_handle,
56 const absl::optional<AudioDecoderConfig>& audio_config,
57 const absl::optional<VideoDecoderConfig>& video_config);
59 // The callback from |demuxer_stream_| that a read operation has completed.
60 // |read_cb| is a callback from the client who invoked Read() on |this|.
61 void OnBufferReady(Status status,
62 std::vector<mojom::DecoderBufferPtr> batch_buffers,
63 const absl::optional<AudioDecoderConfig>& audio_config,
64 const absl::optional<VideoDecoderConfig>& video_config);
66 void OnBufferRead(scoped_refptr<DecoderBuffer> buffer);
68 void UpdateConfig(const absl::optional<AudioDecoderConfig>& audio_config,
69 const absl::optional<VideoDecoderConfig>& video_config);
71 // See constructor for descriptions.
72 mojo::Remote<mojom::DemuxerStream> demuxer_stream_;
73 base::OnceClosure stream_ready_cb_;
75 // The last ReadCB received through a call to Read().
76 // Used to store the results of OnBufferReady() in the event it is called
77 // with Status::kConfigChanged and we don't have an up to date
78 // AudioDecoderConfig yet. In that case we can't forward the results
79 // on to the caller of Read() until OnAudioDecoderConfigChanged is observed.
82 // The current config.
83 AudioDecoderConfig audio_config_;
84 VideoDecoderConfig video_config_;
86 Type type_ = Type::UNKNOWN;
87 Status status_ = Status::kOk;
89 size_t actual_read_count_ = 0;
91 DemuxerStream::DecoderBufferVector buffer_queue_;
93 std::unique_ptr<MojoDecoderBufferReader> mojo_decoder_buffer_reader_;
95 base::WeakPtrFactory<MojoDemuxerStreamAdapter> weak_factory_{this};
100 #endif // MEDIA_MOJO_SERVICES_MOJO_DEMUXER_STREAM_ADAPTER_H_