Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / common / gpu / media / android_video_decode_accelerator.h
blob6afd4376a4fd576c9ae164a14a8f6c70d67edc9b
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.
5 #ifndef CONTENT_COMMON_GPU_MEDIA_ANDROID_VIDEO_DECODE_ACCELERATOR_H_
6 #define CONTENT_COMMON_GPU_MEDIA_ANDROID_VIDEO_DECODE_ACCELERATOR_H_
8 #include <list>
9 #include <map>
10 #include <queue>
11 #include <string>
12 #include <vector>
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/android_video_decode_accelerator_state_provider.h"
19 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
20 #include "media/base/android/media_codec_bridge.h"
21 #include "media/video/video_decode_accelerator.h"
23 namespace gfx {
24 class SurfaceTexture;
27 namespace 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
32 // It delegates attaching pictures to PictureBuffers to a BackingStrategy, but
33 // otherwise handles the work of transferring data to / from MediaCodec.
34 class CONTENT_EXPORT AndroidVideoDecodeAccelerator
35 : public media::VideoDecodeAccelerator,
36 public AndroidVideoDecodeAcceleratorStateProvider {
37 public:
38 // A BackingStrategy is responsible for making a PictureBuffer's texture
39 // contain the image that a MediaCodec decoder buffer tells it to.
40 class BackingStrategy {
41 public:
42 virtual ~BackingStrategy() {}
44 // Notify about the state provider.
45 virtual void SetStateProvider(
46 AndroidVideoDecodeAcceleratorStateProvider*) = 0;
48 // Called before the AVDA does any Destroy() work. This will be
49 // the last call that the BackingStrategy receives.
50 virtual void Cleanup() = 0;
52 // Return the number of picture buffers that we can support.
53 virtual uint32 GetNumPictureBuffers() const = 0;
55 // Return the GL texture target that the PictureBuffer textures use.
56 virtual uint32 GetTextureTarget() const = 0;
58 // Use the provided PictureBuffer to hold the current surface.
59 virtual void AssignCurrentSurfaceToPictureBuffer(
60 int32 codec_buffer_index,
61 const media::PictureBuffer&) = 0;
64 AndroidVideoDecodeAccelerator(
65 const base::WeakPtr<gpu::gles2::GLES2Decoder> decoder,
66 const base::Callback<bool(void)>& make_context_current,
67 scoped_ptr<BackingStrategy> strategy);
69 ~AndroidVideoDecodeAccelerator() override;
71 // Does not take ownership of |client| which must outlive |*this|.
72 bool Initialize(media::VideoCodecProfile profile, Client* client) override;
73 void Decode(const media::BitstreamBuffer& bitstream_buffer) override;
74 void AssignPictureBuffers(
75 const std::vector<media::PictureBuffer>& buffers) override;
76 void ReusePictureBuffer(int32 picture_buffer_id) override;
77 void Flush() override;
78 void Reset() override;
79 void Destroy() override;
80 bool CanDecodeOnIOThread() override;
82 // AndroidVideoDecodeStateProvider
83 const gfx::Size& GetSize() const override;
84 const base::ThreadChecker& ThreadChecker() const override;
85 gfx::SurfaceTexture* GetSurfaceTexture() const override;
86 uint32 GetSurfaceTextureId() const override;
87 gpu::gles2::GLES2Decoder* GetGlDecoder() const override;
88 media::VideoCodecBridge* GetMediaCodec() override;
89 void PostError(const ::tracked_objects::Location& from_here,
90 media::VideoDecodeAccelerator::Error error) override;
92 static media::VideoDecodeAccelerator::SupportedProfiles
93 GetSupportedProfiles();
95 private:
96 enum State {
97 NO_ERROR,
98 ERROR,
101 static const base::TimeDelta kDecodePollDelay;
103 // Configures |media_codec_| with the given codec parameters from the client.
104 bool ConfigureMediaCodec();
106 // Sends the current picture on the surface to the client.
107 void SendCurrentSurfaceToClient(int32 codec_buffer_index, int32 bitstream_id);
109 // Does pending IO tasks if any. Once this is called, it polls |media_codec_|
110 // until it finishes pending tasks. For the polling, |kDecodePollDelay| is
111 // used.
112 void DoIOTask();
114 // Feeds input data to |media_codec_|. This checks
115 // |pending_bitstream_buffers_| and queues a buffer to |media_codec_|.
116 void QueueInput();
118 // Dequeues output from |media_codec_| and feeds the decoded frame to the
119 // client.
120 void DequeueOutput();
122 // Requests picture buffers from the client.
123 void RequestPictureBuffers();
125 // Notifies the client about the availability of a picture.
126 void NotifyPictureReady(const media::Picture& picture);
128 // Notifies the client that the input buffer identifed by input_buffer_id has
129 // been processed.
130 void NotifyEndOfBitstreamBuffer(int input_buffer_id);
132 // Notifies the client that the decoder was flushed.
133 void NotifyFlushDone();
135 // Notifies the client that the decoder was reset.
136 void NotifyResetDone();
138 // Notifies about decoding errors.
139 void NotifyError(media::VideoDecodeAccelerator::Error error);
141 // Used to DCHECK that we are called on the correct thread.
142 base::ThreadChecker thread_checker_;
144 // To expose client callbacks from VideoDecodeAccelerator.
145 Client* client_;
147 // Callback to set the correct gl context.
148 base::Callback<bool(void)> make_context_current_;
150 // Codec type. Used when we configure media codec.
151 media::VideoCodec codec_;
153 // The current state of this class. For now, this is used only for setting
154 // error state.
155 State state_;
157 // This map maintains the picture buffers passed to the client for decoding.
158 // The key is the picture buffer id.
159 typedef std::map<int32, media::PictureBuffer> OutputBufferMap;
160 OutputBufferMap output_picture_buffers_;
162 // This keeps the free picture buffer ids which can be used for sending
163 // decoded frames to the client.
164 std::queue<int32> free_picture_ids_;
166 // Picture buffer ids which have been dismissed and not yet re-assigned. Used
167 // to ignore ReusePictureBuffer calls that were in flight when the
168 // DismissPictureBuffer call was made.
169 std::set<int32> dismissed_picture_ids_;
171 // The low-level decoder which Android SDK provides.
172 scoped_ptr<media::VideoCodecBridge> media_codec_;
174 // A container of texture. Used to set a texture to |media_codec_|.
175 scoped_refptr<gfx::SurfaceTexture> surface_texture_;
177 // The texture id which is set to |surface_texture_|.
178 uint32 surface_texture_id_;
180 // Set to true after requesting picture buffers to the client.
181 bool picturebuffers_requested_;
183 // The resolution of the stream.
184 gfx::Size size_;
186 // Encoded bitstream buffers to be passed to media codec, queued until an
187 // input buffer is available, along with the time when they were first
188 // enqueued.
189 typedef std::queue<std::pair<media::BitstreamBuffer, base::Time> >
190 PendingBitstreamBuffers;
191 PendingBitstreamBuffers pending_bitstream_buffers_;
193 // A map of presentation timestamp to bitstream buffer id for the bitstream
194 // buffers that have been submitted to the decoder but haven't yet produced an
195 // output frame with the same timestamp. Note: there will only be one entry
196 // for multiple bitstream buffers that have the same presentation timestamp.
197 std::map<base::TimeDelta, int32> bitstream_buffers_in_decoder_;
199 // Keeps track of bitstream ids notified to the client with
200 // NotifyEndOfBitstreamBuffer() before getting output from the bitstream.
201 std::list<int32> bitstreams_notified_in_advance_;
203 // Owner of the GL context. Used to restore the context state.
204 base::WeakPtr<gpu::gles2::GLES2Decoder> gl_decoder_;
206 // Repeating timer responsible for draining pending IO to the codec.
207 base::RepeatingTimer<AndroidVideoDecodeAccelerator> io_timer_;
209 // Backing strategy that we'll use to connect PictureBuffers to frames.
210 scoped_ptr<BackingStrategy> strategy_;
212 // WeakPtrFactory for posting tasks back to |this|.
213 base::WeakPtrFactory<AndroidVideoDecodeAccelerator> weak_this_factory_;
215 friend class AndroidVideoDecodeAcceleratorTest;
218 } // namespace content
220 #endif // CONTENT_COMMON_GPU_MEDIA_ANDROID_VIDEO_DECODE_ACCELERATOR_H_