1 // Copyright 2013 The Chromium Authors. All rights reserved.
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_FILTERS_FAKE_VIDEO_DECODER_H_
6 #define MEDIA_FILTERS_FAKE_VIDEO_DECODER_H_
7 
8 #include <stddef.h>
9 
10 #include <list>
11 
12 #include "base/bind.h"
13 #include "base/callback.h"
14 #include "base/callback_helpers.h"
15 #include "base/macros.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/sequence_checker.h"
18 #include "media/base/callback_holder.h"
19 #include "media/base/decoder_buffer.h"
20 #include "media/base/pipeline_status.h"
21 #include "media/base/video_decoder.h"
22 #include "media/base/video_decoder_config.h"
23 #include "media/base/video_frame.h"
24 #include "ui/gfx/geometry/size.h"
25 
26 namespace media {
27 
28 using BytesDecodedCB = base::RepeatingCallback<void(int)>;
29 
30 class FakeVideoDecoder : public VideoDecoder {
31  public:
32   // Constructs an object with a decoding delay of |decoding_delay| frames.
33   // |bytes_decoded_cb| is called after each decode. The sum of the byte
34   // count over all calls will be equal to total_bytes_decoded().
35   FakeVideoDecoder(const std::string& decoder_name,
36                    int decoding_delay,
37                    int max_parallel_decoding_requests,
38                    const BytesDecodedCB& bytes_decoded_cb);
39   ~FakeVideoDecoder() override;
40 
41   // Enables encrypted config supported. Must be called before Initialize().
42   void EnableEncryptedConfigSupport();
43 
44   // Sets whether this decoder is a platform decoder. Must be called before
45   // Initialize().
46   void SetIsPlatformDecoder(bool value);
47 
48   // Decoder implementation.
49   bool SupportsDecryption() const override;
50   bool IsPlatformDecoder() const override;
51   std::string GetDisplayName() const override;
52 
53   // VideoDecoder implementation
54   void Initialize(const VideoDecoderConfig& config,
55                   bool low_delay,
56                   CdmContext* cdm_context,
57                   InitCB init_cb,
58                   const OutputCB& output_cb,
59                   const WaitingCB& waiting_cb) override;
60   void Decode(scoped_refptr<DecoderBuffer> buffer, DecodeCB decode_cb) override;
61   void Reset(base::OnceClosure closure) override;
62   int GetMaxDecodeRequests() const override;
63 
64   base::WeakPtr<FakeVideoDecoder> GetWeakPtr();
65 
66   // Holds the next init/decode/reset callback from firing.
67   void HoldNextInit();
68   void HoldDecode();
69   void HoldNextReset();
70 
71   // Satisfies the pending init/decode/reset callback, which must be ready to
72   // fire when these methods are called.
73   void SatisfyInit();
74   void SatisfyDecode();
75   void SatisfyReset();
76 
77   // Satisfies single  decode request.
78   void SatisfySingleDecode();
79 
80   void SimulateError();
81   // Fail with status DECODER_ERROR_NOT_SUPPORTED when Initialize() is called.
82   void SimulateFailureToInit();
83 
total_bytes_decoded()84   int total_bytes_decoded() const { return total_bytes_decoded_; }
85 
86  protected:
87   enum State {
88     STATE_UNINITIALIZED,
89     STATE_NORMAL,
90     STATE_END_OF_STREAM,
91     STATE_ERROR,
92   };
93 
94   // Derived classes may override to customize the VideoFrame.
95   virtual scoped_refptr<VideoFrame> MakeVideoFrame(const DecoderBuffer& buffer);
96 
97   // Callback for updating |total_bytes_decoded_|.
98   void OnFrameDecoded(int buffer_size, DecodeCB decode_cb, Status status);
99 
100   // Runs |decode_cb| or puts it to |held_decode_callbacks_| depending on
101   // current value of |hold_decode_|.
102   void RunOrHoldDecode(DecodeCB decode_cb);
103 
104   // Runs |decode_cb| with a frame from |decoded_frames_|.
105   void RunDecodeCallback(DecodeCB decode_cb);
106 
107   void DoReset();
108 
109   SEQUENCE_CHECKER(sequence_checker_);
110 
111   const std::string decoder_name_;
112   const size_t decoding_delay_;
113   const int max_parallel_decoding_requests_;
114   BytesDecodedCB bytes_decoded_cb_;
115 
116   bool is_platform_decoder_ = false;
117   bool supports_encrypted_config_ = false;
118 
119   State state_;
120 
121   CallbackHolder<InitCB> init_cb_;
122   CallbackHolder<base::OnceClosure> reset_cb_;
123 
124   OutputCB output_cb_;
125 
126   bool hold_decode_;
127   std::list<DecodeCB> held_decode_callbacks_;
128 
129   VideoDecoderConfig current_config_;
130 
131   std::list<scoped_refptr<VideoFrame> > decoded_frames_;
132 
133   int total_bytes_decoded_;
134 
135   bool fail_to_initialize_;
136 
137   // NOTE: Weak pointers must be invalidated before all other member variables.
138   base::WeakPtrFactory<FakeVideoDecoder> weak_factory_{this};
139 
140   DISALLOW_COPY_AND_ASSIGN(FakeVideoDecoder);
141 };
142 
143 }  // namespace media
144 
145 #endif  // MEDIA_FILTERS_FAKE_VIDEO_DECODER_H_
146