OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_COMMON_GPU_MEDIA_ANDROID_VIDEO_DECODE_ACCELERATOR_H_ | 5 #ifndef CONTENT_COMMON_GPU_MEDIA_ANDROID_VIDEO_DECODE_ACCELERATOR_H_ |
6 #define CONTENT_COMMON_GPU_MEDIA_ANDROID_VIDEO_DECODE_ACCELERATOR_H_ | 6 #define CONTENT_COMMON_GPU_MEDIA_ANDROID_VIDEO_DECODE_ACCELERATOR_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <map> | 9 #include <map> |
10 #include <queue> | 10 #include <queue> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
16 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
17 #include "content/common/content_export.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" | 18 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" |
20 #include "media/base/android/media_codec_bridge.h" | 19 #include "media/base/android/media_codec_bridge.h" |
21 #include "media/video/video_decode_accelerator.h" | 20 #include "media/video/video_decode_accelerator.h" |
22 | 21 |
23 namespace gfx { | 22 namespace gfx { |
24 class SurfaceTexture; | 23 class SurfaceTexture; |
25 } | 24 } |
26 | 25 |
27 namespace content { | 26 namespace content { |
28 // A VideoDecodeAccelerator implementation for Android. | 27 // A base class for VideoDecodeAccelerator implementations for Android. |
29 // This class decodes the input encoded stream by using Android's MediaCodec | 28 // This class decodes the input encoded stream by using Android's MediaCodec |
30 // class. http://developer.android.com/reference/android/media/MediaCodec.html | 29 // class. http://developer.android.com/reference/android/media/MediaCodec.html |
30 // It delegates attaching pictures to PictureBuffers to the client, but | |
31 // otherwise handles the work of transferring data to / from MediaCodec. | |
31 class CONTENT_EXPORT AndroidVideoDecodeAccelerator | 32 class CONTENT_EXPORT AndroidVideoDecodeAccelerator |
32 : public media::VideoDecodeAccelerator { | 33 : public media::VideoDecodeAccelerator { |
33 public: | 34 public: |
34 // Does not take ownership of |client| which must outlive |*this|. | 35 |
35 AndroidVideoDecodeAccelerator( | 36 // Helper class that provides the BackingStrategy with enough state |
37 // to do useful work. | |
38 class StateProvider { | |
watk
2015/09/04 23:15:16
I think this is mostly subjective, so take or leav
liberato (no reviews please)
2015/09/08 19:50:00
i had a similar this argument with myself. i deci
| |
39 public: | |
40 virtual ~StateProvider() {} | |
41 virtual const gfx::Size& GetSize() const = 0; | |
42 virtual const base::ThreadChecker& ThreadChecker() const = 0; | |
43 virtual gfx::SurfaceTexture* GetSurfaceTexture() const = 0; | |
44 virtual uint32 GetSurfaceTextureId() const = 0; | |
45 virtual gpu::gles2::GLES2Decoder* GetGlDecoder() const = 0; | |
46 virtual media::VideoCodecBridge* GetMediaCodec() = 0; | |
47 | |
48 // Helper function to report an error condition and stop decodnig. | |
49 // This will post NotifyError(), and transition to the error state. | |
50 // It is meant to be called from the RETURN_ON_FAILURE macro. | |
51 virtual void PostError(const ::tracked_objects::Location& from_here, | |
52 media::VideoDecodeAccelerator::Error error) = 0; | |
53 | |
54 }; | |
55 | |
56 // A BackingStrategy is responsible for making a PictureBuffer's texture | |
57 // contain the image that a MediaCodec decoder buffer tells it to. | |
58 class BackingStrategy : public base::RefCounted<BackingStrategy> { | |
sandersd (OOO until July 31)
2015/09/04 22:12:53
Why do backingstrategies need to be refcounted?
liberato (no reviews please)
2015/09/08 19:50:00
Done.
| |
59 public: | |
60 virtual ~BackingStrategy(){} | |
61 | |
62 // Notify about the state provider. | |
63 virtual void SetStateProvider(StateProvider*) = 0; | |
64 | |
65 // Called before the AVDA does any Destroy() work. This will be | |
66 // the last call that the BackingStrategy receives. | |
67 virtual void Cleanup() = 0; | |
68 | |
69 // Return the number of picture buffers that we can support. | |
70 virtual uint32 GetNumPictureBuffers() const = 0; | |
71 | |
72 // Return the GL texture target that the PictureBuffer textures use. | |
73 virtual uint32 GetTextureTarget() const = 0; | |
74 | |
75 // Use the provided PictureBuffer to hold the current surface. | |
76 virtual void AssignCurrentSurfaceToPictureBuffer(int32 codec_buffer_index, | |
77 const media::PictureBuffer&) = 0; | |
78 }; | |
79 | |
80 // Create an AVDA using the given backing strategy. | |
81 static AndroidVideoDecodeAccelerator* Create( | |
36 const base::WeakPtr<gpu::gles2::GLES2Decoder> decoder, | 82 const base::WeakPtr<gpu::gles2::GLES2Decoder> decoder, |
37 const base::Callback<bool(void)>& make_context_current); | 83 const base::Callback<bool(void)>& make_context_current, |
38 | 84 scoped_refptr<BackingStrategy> strategy); |
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; | |
49 | 85 |
50 static media::VideoDecodeAccelerator::SupportedProfiles | 86 static media::VideoDecodeAccelerator::SupportedProfiles |
51 GetSupportedProfiles(); | 87 GetSupportedProfiles(); |
52 | 88 |
53 private: | |
54 enum State { | |
55 NO_ERROR, | |
56 ERROR, | |
57 }; | |
58 | |
59 static const base::TimeDelta kDecodePollDelay; | |
60 | |
61 ~AndroidVideoDecodeAccelerator() override; | |
62 | |
63 // Configures |media_codec_| with the given codec parameters from the client. | |
64 bool ConfigureMediaCodec(); | |
65 | |
66 // Sends the current picture on the surface to the client. | |
67 void SendCurrentSurfaceToClient(int32 bitstream_id); | |
68 | |
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 | |
71 // used. | |
72 void DoIOTask(); | |
73 | |
74 // Feeds input data to |media_codec_|. This checks | |
75 // |pending_bitstream_buffers_| and queues a buffer to |media_codec_|. | |
76 void QueueInput(); | |
77 | |
78 // Dequeues output from |media_codec_| and feeds the decoded frame to the | |
79 // client. | |
80 void DequeueOutput(); | |
81 | |
82 // Requests picture buffers from the client. | |
83 void RequestPictureBuffers(); | |
84 | |
85 // Notifies the client about the availability of a picture. | |
86 void NotifyPictureReady(const media::Picture& picture); | |
87 | |
88 // Notifies the client that the input buffer identifed by input_buffer_id has | |
89 // been processed. | |
90 void NotifyEndOfBitstreamBuffer(int input_buffer_id); | |
91 | |
92 // Notifies the client that the decoder was flushed. | |
93 void NotifyFlushDone(); | |
94 | |
95 // Notifies the client that the decoder was reset. | |
96 void NotifyResetDone(); | |
97 | |
98 // Notifies about decoding errors. | |
99 void NotifyError(media::VideoDecodeAccelerator::Error error); | |
100 | |
101 // Used to DCHECK that we are called on the correct thread. | |
102 base::ThreadChecker thread_checker_; | |
103 | |
104 // To expose client callbacks from VideoDecodeAccelerator. | |
105 Client* client_; | |
106 | |
107 // Callback to set the correct gl context. | |
108 base::Callback<bool(void)> make_context_current_; | |
109 | |
110 // Codec type. Used when we configure media codec. | |
111 media::VideoCodec codec_; | |
112 | |
113 // The current state of this class. For now, this is used only for setting | |
114 // error state. | |
115 State state_; | |
116 | |
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_; | |
121 | |
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_; | |
125 | |
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_; | |
130 | |
131 // The low-level decoder which Android SDK provides. | |
132 scoped_ptr<media::VideoCodecBridge> media_codec_; | |
133 | |
134 // A container of texture. Used to set a texture to |media_codec_|. | |
135 scoped_refptr<gfx::SurfaceTexture> surface_texture_; | |
136 | |
137 // The texture id which is set to |surface_texture_|. | |
138 uint32 surface_texture_id_; | |
139 | |
140 // Set to true after requesting picture buffers to the client. | |
141 bool picturebuffers_requested_; | |
142 | |
143 // The resolution of the stream. | |
144 gfx::Size size_; | |
145 | |
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 | |
148 // enqueued. | |
149 typedef std::queue<std::pair<media::BitstreamBuffer, base::Time> > | |
150 PendingBitstreamBuffers; | |
151 PendingBitstreamBuffers pending_bitstream_buffers_; | |
152 | |
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_; | |
156 | |
157 // Owner of the GL context. Used to restore the context state. | |
158 base::WeakPtr<gpu::gles2::GLES2Decoder> gl_decoder_; | |
159 | |
160 // Used for copy the texture from |surface_texture_| to picture buffers. | |
161 scoped_ptr<gpu::CopyTextureCHROMIUMResourceManager> copier_; | |
162 | |
163 // Repeating timer responsible for draining pending IO to the codec. | |
164 base::RepeatingTimer<AndroidVideoDecodeAccelerator> io_timer_; | |
165 | |
166 // WeakPtrFactory for posting tasks back to |this|. | |
167 base::WeakPtrFactory<AndroidVideoDecodeAccelerator> weak_this_factory_; | |
168 | |
169 friend class AndroidVideoDecodeAcceleratorTest; | 89 friend class AndroidVideoDecodeAcceleratorTest; |
170 }; | 90 }; |
171 | 91 |
172 } // namespace content | 92 } // namespace content |
173 | 93 |
174 #endif // CONTENT_COMMON_GPU_MEDIA_ANDROID_VIDEO_DECODE_ACCELERATOR_H_ | 94 #endif // CONTENT_COMMON_GPU_MEDIA_ANDROID_VIDEO_DECODE_ACCELERATOR_H_ |
OLD | NEW |