Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / media / filters / fake_video_decoder.h
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 <list>
9
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/callback_helpers.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/threading/thread_checker.h"
15 #include "media/base/callback_holder.h"
16 #include "media/base/decoder_buffer.h"
17 #include "media/base/pipeline_status.h"
18 #include "media/base/video_decoder.h"
19 #include "media/base/video_decoder_config.h"
20 #include "media/base/video_frame.h"
21 #include "ui/gfx/size.h"
22
23 using base::ResetAndReturn;
24
25 namespace base {
26 class SingleThreadTaskRunner;
27 }
28
29 namespace media {
30
31 class FakeVideoDecoder : public VideoDecoder {
32  public:
33   // Constructs an object with a decoding delay of |decoding_delay| frames.
34   FakeVideoDecoder(int decoding_delay,
35                    bool supports_get_decode_output,
36                    int max_parallel_decoding_requests);
37   virtual ~FakeVideoDecoder();
38
39   // VideoDecoder implementation.
40   virtual void Initialize(const VideoDecoderConfig& config,
41                           bool low_delay,
42                           const PipelineStatusCB& status_cb) OVERRIDE;
43   virtual void Decode(const scoped_refptr<DecoderBuffer>& buffer,
44                       const DecodeCB& decode_cb) OVERRIDE;
45   virtual void Reset(const base::Closure& closure) OVERRIDE;
46   virtual void Stop() OVERRIDE;
47   virtual scoped_refptr<VideoFrame> GetDecodeOutput() OVERRIDE;
48   virtual int GetMaxDecodeRequests() const OVERRIDE;
49
50   // Holds the next init/decode/reset callback from firing.
51   void HoldNextInit();
52   void HoldDecode();
53   void HoldNextReset();
54
55   // Satisfies the pending init/decode/reset callback, which must be ready to
56   // fire when these methods are called.
57   void SatisfyInit();
58   void SatisfyDecode();
59   void SatisfyReset();
60
61   // Satisfies single  decode request.
62   void SatisfySingleDecode();
63
64   void SimulateError();
65
66   int total_bytes_decoded() const { return total_bytes_decoded_; }
67
68  private:
69   enum State {
70     STATE_UNINITIALIZED,
71     STATE_NORMAL,
72     STATE_END_OF_STREAM,
73     STATE_ERROR,
74   };
75
76   // Callback for updating |total_bytes_decoded_|.
77   void OnFrameDecoded(int buffer_size,
78                       const DecodeCB& decode_cb,
79                       Status status,
80                       const scoped_refptr<VideoFrame>& video_frame);
81
82   // Runs |decode_cb| or puts it to |held_decode_callbacks_| depending on
83   // current value of |hold_decode_|.
84   void RunOrHoldDecode(const DecodeCB& decode_cb);
85
86   // Runs |decode_cb| with a frame from |decoded_frames_|.
87   void RunDecodeCallback(const DecodeCB& decode_cb);
88
89   void DoReset();
90
91   base::ThreadChecker thread_checker_;
92
93   const size_t decoding_delay_;
94   const bool supports_get_decode_output_;
95   const int max_parallel_decoding_requests_;
96
97   State state_;
98
99   CallbackHolder<PipelineStatusCB> init_cb_;
100   CallbackHolder<base::Closure> reset_cb_;
101
102   bool hold_decode_;
103   std::list<DecodeCB> held_decode_callbacks_;
104
105   VideoDecoderConfig current_config_;
106
107   std::list<scoped_refptr<VideoFrame> > decoded_frames_;
108
109   int total_bytes_decoded_;
110
111   // NOTE: Weak pointers must be invalidated before all other member variables.
112   base::WeakPtrFactory<FakeVideoDecoder> weak_factory_;
113
114   DISALLOW_COPY_AND_ASSIGN(FakeVideoDecoder);
115 };
116
117 }  // namespace media
118
119 #endif  // MEDIA_FILTERS_FAKE_VIDEO_DECODER_H_