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