1 // Copyright 2013 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_FILTERS_FAKE_VIDEO_DECODER_H_
6 #define MEDIA_FILTERS_FAKE_VIDEO_DECODER_H_
12 #include "base/functional/bind.h"
13 #include "base/functional/callback.h"
14 #include "base/functional/callback_helpers.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/sequence_checker.h"
17 #include "media/base/callback_holder.h"
18 #include "media/base/decoder_buffer.h"
19 #include "media/base/pipeline_status.h"
20 #include "media/base/video_decoder.h"
21 #include "media/base/video_decoder_config.h"
22 #include "media/base/video_frame.h"
23 #include "ui/gfx/geometry/size.h"
27 using BytesDecodedCB = base::RepeatingCallback<void(int)>;
29 class FakeVideoDecoder : public VideoDecoder {
31 // Constructs an object with a decoding delay of |decoding_delay| frames.
32 // |bytes_decoded_cb| is called after each decode. The sum of the byte
33 // count over all calls will be equal to total_bytes_decoded().
34 // Allows setting a fake ID so that tests for wrapper decoders can check
35 // that underlying decoders change successfully.
36 FakeVideoDecoder(int decoder_id,
38 int max_parallel_decoding_requests,
39 const BytesDecodedCB& bytes_decoded_cb);
41 FakeVideoDecoder(const FakeVideoDecoder&) = delete;
42 FakeVideoDecoder& operator=(const FakeVideoDecoder&) = delete;
44 ~FakeVideoDecoder() override;
46 // Enables encrypted config supported. Must be called before Initialize().
47 void EnableEncryptedConfigSupport();
49 // Sets whether this decoder is a platform decoder. Must be called before
51 void SetIsPlatformDecoder(bool value);
53 // Decoder implementation.
54 bool SupportsDecryption() const override;
55 bool IsPlatformDecoder() const override;
56 VideoDecoderType GetDecoderType() const override;
57 int GetDecoderId() { return decoder_id_; }
59 // VideoDecoder implementation
60 void Initialize(const VideoDecoderConfig& config,
62 CdmContext* cdm_context,
64 const OutputCB& output_cb,
65 const WaitingCB& waiting_cb) override;
66 void Decode(scoped_refptr<DecoderBuffer> buffer, DecodeCB decode_cb) override;
67 void Reset(base::OnceClosure closure) override;
68 int GetMaxDecodeRequests() const override;
70 base::WeakPtr<FakeVideoDecoder> GetWeakPtr();
72 // Holds the next init/decode/reset callback from firing.
77 // Satisfies the pending init/decode/reset callback, which must be ready to
78 // fire when these methods are called.
83 // Satisfies single decode request.
84 void SatisfySingleDecode();
87 // Fail with status DECODER_ERROR_NOT_SUPPORTED when Initialize() is called.
88 void SimulateFailureToInit();
90 int total_bytes_decoded() const { return total_bytes_decoded_; }
100 // Derived classes may override to customize the VideoFrame.
101 virtual scoped_refptr<VideoFrame> MakeVideoFrame(const DecoderBuffer& buffer);
103 // Callback for updating |total_bytes_decoded_|.
104 void OnFrameDecoded(int buffer_size,
106 DecoderStatus status);
108 // Runs |decode_cb| or puts it to |held_decode_callbacks_| depending on
109 // current value of |hold_decode_|.
110 void RunOrHoldDecode(DecodeCB decode_cb);
112 // Runs |decode_cb| with a frame from |decoded_frames_|.
113 void RunDecodeCallback(DecodeCB decode_cb);
117 SEQUENCE_CHECKER(sequence_checker_);
119 const int decoder_id_;
120 const size_t decoding_delay_;
121 const int max_parallel_decoding_requests_;
122 BytesDecodedCB bytes_decoded_cb_;
124 bool is_platform_decoder_ = false;
125 bool supports_encrypted_config_ = false;
129 CallbackHolder<InitCB> init_cb_;
130 CallbackHolder<base::OnceClosure> reset_cb_;
135 std::list<DecodeCB> held_decode_callbacks_;
137 VideoDecoderConfig current_config_;
139 std::list<scoped_refptr<VideoFrame> > decoded_frames_;
141 int total_bytes_decoded_;
143 bool fail_to_initialize_;
145 // NOTE: Weak pointers must be invalidated before all other member variables.
146 base::WeakPtrFactory<FakeVideoDecoder> weak_factory_{this};
151 #endif // MEDIA_FILTERS_FAKE_VIDEO_DECODER_H_