[M120 Migration][hbbtv] Audio tracks count notification
[platform/framework/web/chromium-efl.git] / media / filters / fake_video_decoder.h
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.
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/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"
24
25 namespace media {
26
27 using BytesDecodedCB = base::RepeatingCallback<void(int)>;
28
29 class FakeVideoDecoder : public VideoDecoder {
30  public:
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,
37                    int decoding_delay,
38                    int max_parallel_decoding_requests,
39                    const BytesDecodedCB& bytes_decoded_cb);
40
41   FakeVideoDecoder(const FakeVideoDecoder&) = delete;
42   FakeVideoDecoder& operator=(const FakeVideoDecoder&) = delete;
43
44   ~FakeVideoDecoder() override;
45
46   // Enables encrypted config supported. Must be called before Initialize().
47   void EnableEncryptedConfigSupport();
48
49   // Sets whether this decoder is a platform decoder. Must be called before
50   // Initialize().
51   void SetIsPlatformDecoder(bool value);
52
53   // Decoder implementation.
54   bool SupportsDecryption() const override;
55   bool IsPlatformDecoder() const override;
56   VideoDecoderType GetDecoderType() const override;
57   int GetDecoderId() { return decoder_id_; }
58
59   // VideoDecoder implementation
60   void Initialize(const VideoDecoderConfig& config,
61                   bool low_delay,
62                   CdmContext* cdm_context,
63                   InitCB init_cb,
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;
69
70   base::WeakPtr<FakeVideoDecoder> GetWeakPtr();
71
72   // Holds the next init/decode/reset callback from firing.
73   void HoldNextInit();
74   void HoldDecode();
75   void HoldNextReset();
76
77   // Satisfies the pending init/decode/reset callback, which must be ready to
78   // fire when these methods are called.
79   void SatisfyInit();
80   void SatisfyDecode();
81   void SatisfyReset();
82
83   // Satisfies single  decode request.
84   void SatisfySingleDecode();
85
86   void SimulateError();
87   // Fail with status DECODER_ERROR_NOT_SUPPORTED when Initialize() is called.
88   void SimulateFailureToInit();
89
90   int total_bytes_decoded() const { return total_bytes_decoded_; }
91
92  protected:
93   enum State {
94     STATE_UNINITIALIZED,
95     STATE_NORMAL,
96     STATE_END_OF_STREAM,
97     STATE_ERROR,
98   };
99
100   // Derived classes may override to customize the VideoFrame.
101   virtual scoped_refptr<VideoFrame> MakeVideoFrame(const DecoderBuffer& buffer);
102
103   // Callback for updating |total_bytes_decoded_|.
104   void OnFrameDecoded(int buffer_size,
105                       DecodeCB decode_cb,
106                       DecoderStatus status);
107
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);
111
112   // Runs |decode_cb| with a frame from |decoded_frames_|.
113   void RunDecodeCallback(DecodeCB decode_cb);
114
115   void DoReset();
116
117   SEQUENCE_CHECKER(sequence_checker_);
118
119   const int decoder_id_;
120   const size_t decoding_delay_;
121   const int max_parallel_decoding_requests_;
122   BytesDecodedCB bytes_decoded_cb_;
123
124   bool is_platform_decoder_ = false;
125   bool supports_encrypted_config_ = false;
126
127   State state_;
128
129   CallbackHolder<InitCB> init_cb_;
130   CallbackHolder<base::OnceClosure> reset_cb_;
131
132   OutputCB output_cb_;
133
134   bool hold_decode_;
135   std::list<DecodeCB> held_decode_callbacks_;
136
137   VideoDecoderConfig current_config_;
138
139   std::list<scoped_refptr<VideoFrame> > decoded_frames_;
140
141   int total_bytes_decoded_;
142
143   bool fail_to_initialize_;
144
145   // NOTE: Weak pointers must be invalidated before all other member variables.
146   base::WeakPtrFactory<FakeVideoDecoder> weak_factory_{this};
147 };
148
149 }  // namespace media
150
151 #endif  // MEDIA_FILTERS_FAKE_VIDEO_DECODER_H_