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_
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 "gpu/command_buffer/service/gles2_cmd_copy_texture_chromium.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"
28 // A VideoDecodeAccelerator implementation for Android.
29 // This class decodes the input encoded stream by using Android's MediaCodec
30 // class. http://developer.android.com/reference/android/media/MediaCodec.html
31 class CONTENT_EXPORT AndroidVideoDecodeAccelerator
32 : public media::VideoDecodeAccelerator
{
34 // Does not take ownership of |client| which must outlive |*this|.
35 AndroidVideoDecodeAccelerator(
36 const base::WeakPtr
<gpu::gles2::GLES2Decoder
> decoder
,
37 const base::Callback
<bool(void)>& make_context_current
);
39 // media::VideoDecodeAccelerator implementation.
40 bool Initialize(media::VideoCodecProfile profile
, Client
* client
) override
;
41 void Decode(const media::BitstreamBuffer
& bitstream_buffer
) override
;
42 void AssignPictureBuffers(
43 const std::vector
<media::PictureBuffer
>& buffers
) override
;
44 void ReusePictureBuffer(int32 picture_buffer_id
) override
;
45 void Flush() override
;
46 void Reset() override
;
47 void Destroy() override
;
48 bool CanDecodeOnIOThread() override
;
50 static media::VideoDecodeAccelerator::SupportedProfiles
51 GetSupportedProfiles();
59 static const base::TimeDelta kDecodePollDelay
;
61 ~AndroidVideoDecodeAccelerator() override
;
63 // Configures |media_codec_| with the given codec parameters from the client.
64 bool ConfigureMediaCodec();
66 // Sends the current picture on the surface to the client.
67 void SendCurrentSurfaceToClient(int32 bitstream_id
);
69 // Does pending IO tasks if any. Once this is called, it polls |media_codec_|
70 // until it finishes pending tasks. For the polling, |kDecodePollDelay| is
74 // Feeds input data to |media_codec_|. This checks
75 // |pending_bitstream_buffers_| and queues a buffer to |media_codec_|.
78 // Dequeues output from |media_codec_| and feeds the decoded frame to the
82 // Requests picture buffers from the client.
83 void RequestPictureBuffers();
85 // Notifies the client about the availability of a picture.
86 void NotifyPictureReady(const media::Picture
& picture
);
88 // Notifies the client that the input buffer identifed by input_buffer_id has
90 void NotifyEndOfBitstreamBuffer(int input_buffer_id
);
92 // Notifies the client that the decoder was flushed.
93 void NotifyFlushDone();
95 // Notifies the client that the decoder was reset.
96 void NotifyResetDone();
98 // Notifies about decoding errors.
99 void NotifyError(media::VideoDecodeAccelerator::Error error
);
101 // Used to DCHECK that we are called on the correct thread.
102 base::ThreadChecker thread_checker_
;
104 // To expose client callbacks from VideoDecodeAccelerator.
107 // Callback to set the correct gl context.
108 base::Callback
<bool(void)> make_context_current_
;
110 // Codec type. Used when we configure media codec.
111 media::VideoCodec codec_
;
113 // The current state of this class. For now, this is used only for setting
117 // This map maintains the picture buffers passed to the client for decoding.
118 // The key is the picture buffer id.
119 typedef std::map
<int32
, media::PictureBuffer
> OutputBufferMap
;
120 OutputBufferMap output_picture_buffers_
;
122 // This keeps the free picture buffer ids which can be used for sending
123 // decoded frames to the client.
124 std::queue
<int32
> free_picture_ids_
;
126 // Picture buffer ids which have been dismissed and not yet re-assigned. Used
127 // to ignore ReusePictureBuffer calls that were in flight when the
128 // DismissPictureBuffer call was made.
129 std::set
<int32
> dismissed_picture_ids_
;
131 // The low-level decoder which Android SDK provides.
132 scoped_ptr
<media::VideoCodecBridge
> media_codec_
;
134 // A container of texture. Used to set a texture to |media_codec_|.
135 scoped_refptr
<gfx::SurfaceTexture
> surface_texture_
;
137 // The texture id which is set to |surface_texture_|.
138 uint32 surface_texture_id_
;
140 // Set to true after requesting picture buffers to the client.
141 bool picturebuffers_requested_
;
143 // The resolution of the stream.
146 // Encoded bitstream buffers to be passed to media codec, queued until an
147 // input buffer is available, along with the time when they were first
149 typedef std::queue
<std::pair
<media::BitstreamBuffer
, base::Time
> >
150 PendingBitstreamBuffers
;
151 PendingBitstreamBuffers pending_bitstream_buffers_
;
153 // Keeps track of bitstream ids notified to the client with
154 // NotifyEndOfBitstreamBuffer() before getting output from the bitstream.
155 std::list
<int32
> bitstreams_notified_in_advance_
;
157 // Owner of the GL context. Used to restore the context state.
158 base::WeakPtr
<gpu::gles2::GLES2Decoder
> gl_decoder_
;
160 // Used for copy the texture from |surface_texture_| to picture buffers.
161 scoped_ptr
<gpu::CopyTextureCHROMIUMResourceManager
> copier_
;
163 // Repeating timer responsible for draining pending IO to the codec.
164 base::RepeatingTimer
<AndroidVideoDecodeAccelerator
> io_timer_
;
166 // WeakPtrFactory for posting tasks back to |this|.
167 base::WeakPtrFactory
<AndroidVideoDecodeAccelerator
> weak_this_factory_
;
169 friend class AndroidVideoDecodeAcceleratorTest
;
172 } // namespace content
174 #endif // CONTENT_COMMON_GPU_MEDIA_ANDROID_VIDEO_DECODE_ACCELERATOR_H_