1// Copyright (c) 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 CONTENT_COMMON_GPU_MEDIA_ANDROID_VIDEO_DECODE_ACCELERATOR_H_
6#define CONTENT_COMMON_GPU_MEDIA_ANDROID_VIDEO_DECODE_ACCELERATOR_H_
7
8#include <list>
9#include <map>
10#include <queue>
11#include <string>
12#include <vector>
13
14#include "base/compiler_specific.h"
15#include "base/threading/thread_checker.h"
16#include "base/timer/timer.h"
17#include "content/common/content_export.h"
18#include "content/common/gpu/media/video_decode_accelerator_impl.h"
19#include "gpu/command_buffer/service/gles2_cmd_copy_texture_chromium.h"
20#include "gpu/command_buffer/service/gles2_cmd_decoder.h"
21#include "media/base/android/media_codec_bridge.h"
22#include "media/video/video_decode_accelerator.h"
23
24namespace gfx {
25class SurfaceTexture;
26}
27
28namespace content {
29// A VideoDecodeAccelerator implementation for Android.
30// This class decodes the input encoded stream by using Android's MediaCodec
31// class. http://developer.android.com/reference/android/media/MediaCodec.html
32class CONTENT_EXPORT AndroidVideoDecodeAccelerator
33    : public VideoDecodeAcceleratorImpl {
34 public:
35  // Does not take ownership of |client| which must outlive |*this|.
36  AndroidVideoDecodeAccelerator(
37      media::VideoDecodeAccelerator::Client* client,
38      const base::WeakPtr<gpu::gles2::GLES2Decoder> decoder,
39      const base::Callback<bool(void)>& make_context_current);
40
41  // media::VideoDecodeAccelerator implementation.
42  virtual bool Initialize(media::VideoCodecProfile profile) OVERRIDE;
43  virtual void Decode(const media::BitstreamBuffer& bitstream_buffer) OVERRIDE;
44  virtual void AssignPictureBuffers(
45      const std::vector<media::PictureBuffer>& buffers) OVERRIDE;
46  virtual void ReusePictureBuffer(int32 picture_buffer_id) OVERRIDE;
47  virtual void Flush() OVERRIDE;
48  virtual void Reset() OVERRIDE;
49  virtual void Destroy() OVERRIDE;
50
51 private:
52  enum State {
53    NO_ERROR,
54    ERROR,
55  };
56
57  static const base::TimeDelta kDecodePollDelay;
58
59  virtual ~AndroidVideoDecodeAccelerator();
60
61  // Configures |media_codec_| with the given codec parameters from the client.
62  bool ConfigureMediaCodec();
63
64  // Sends the current picture on the surface to the client.
65  void SendCurrentSurfaceToClient(int32 bitstream_id);
66
67  // Does pending IO tasks if any. Once this is called, it polls |media_codec_|
68  // until it finishes pending tasks. For the polling, |kDecodePollDelay| is
69  // used.
70  void DoIOTask();
71
72  // Feeds input data to |media_codec_|. This checks
73  // |pending_bitstream_buffers_| and queues a buffer to |media_codec_|.
74  void QueueInput();
75
76  // Dequeues output from |media_codec_| and feeds the decoded frame to the
77  // client.
78  void DequeueOutput();
79
80  // Notifies the client that initialize was completed.
81  void NotifyInitializeDone();
82
83  // Requests picture buffers from the client.
84  void RequestPictureBuffers();
85
86  // Notifies the client about the availability of a picture.
87  void NotifyPictureReady(const media::Picture& picture);
88
89  // Notifies the client that the input buffer identifed by input_buffer_id has
90  // been processed.
91  void NotifyEndOfBitstreamBuffer(int input_buffer_id);
92
93  // Notifies the client that the decoder was flushed.
94  void NotifyFlushDone();
95
96  // Notifies the client that the decoder was reset.
97  void NotifyResetDone();
98
99  // Notifies about decoding errors.
100  void NotifyError(media::VideoDecodeAccelerator::Error error);
101
102  // Used to DCHECK that we are called on the correct thread.
103  base::ThreadChecker thread_checker_;
104
105  // To expose client callbacks from VideoDecodeAccelerator.
106  Client* client_;
107
108  // Callback to set the correct gl context.
109  base::Callback<bool(void)> make_context_current_;
110
111  // Codec type. Used when we configure media codec.
112  media::VideoCodec codec_;
113
114  // The current state of this class. For now, this is used only for setting
115  // error state.
116  State state_;
117
118  // This map maintains the picture buffers passed to the client for decoding.
119  // The key is the picture buffer id.
120  typedef std::map<int32, media::PictureBuffer> OutputBufferMap;
121  OutputBufferMap output_picture_buffers_;
122
123  // This keeps the free picture buffer ids which can be used for sending
124  // decoded frames to the client.
125  std::queue<int32> free_picture_ids_;
126
127  // Picture buffer ids which have been dismissed and not yet re-assigned.  Used
128  // to ignore ReusePictureBuffer calls that were in flight when the
129  // DismissPictureBuffer call was made.
130  std::set<int32> dismissed_picture_ids_;
131
132  // The low-level decoder which Android SDK provides.
133  scoped_ptr<media::VideoCodecBridge> media_codec_;
134
135  // A container of texture. Used to set a texture to |media_codec_|.
136  scoped_refptr<gfx::SurfaceTexture> surface_texture_;
137
138  // The texture id which is set to |surface_texture_|.
139  uint32 surface_texture_id_;
140
141  // Set to true after requesting picture buffers to the client.
142  bool picturebuffers_requested_;
143
144  // The resolution of the stream.
145  gfx::Size size_;
146
147  // Encoded bitstream buffers to be passed to media codec, queued until an
148  // input buffer is available, along with the time when they were first
149  // enqueued.
150  typedef std::queue<std::pair<media::BitstreamBuffer, base::Time> >
151      PendingBitstreamBuffers;
152  PendingBitstreamBuffers pending_bitstream_buffers_;
153
154  // Keeps track of bitstream ids notified to the client with
155  // NotifyEndOfBitstreamBuffer() before getting output from the bitstream.
156  std::list<int32> bitstreams_notified_in_advance_;
157
158  // Owner of the GL context. Used to restore the context state.
159  base::WeakPtr<gpu::gles2::GLES2Decoder> gl_decoder_;
160
161  // Used for copy the texture from |surface_texture_| to picture buffers.
162  scoped_ptr<gpu::CopyTextureCHROMIUMResourceManager> copier_;
163
164  // Repeating timer responsible for draining pending IO to the codec.
165  base::RepeatingTimer<AndroidVideoDecodeAccelerator> io_timer_;
166
167  friend class AndroidVideoDecodeAcceleratorTest;
168};
169
170}  // namespace content
171
172#endif  // CONTENT_COMMON_GPU_MEDIA_ANDROID_VIDEO_DECODE_ACCELERATOR_H_
173