| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 MEDIA_GPU_ANDROID_MEDIA_CODEC_VIDEO_DECODER_H_ | 5 #ifndef MEDIA_GPU_ANDROID_MEDIA_CODEC_VIDEO_DECODER_H_ |
| 6 #define MEDIA_GPU_ANDROID_MEDIA_CODEC_VIDEO_DECODER_H_ | 6 #define MEDIA_GPU_ANDROID_MEDIA_CODEC_VIDEO_DECODER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <list> | 10 #include <list> |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "media/base/android/sdk_media_codec_bridge.h" | 22 #include "media/base/android/sdk_media_codec_bridge.h" |
| 23 #include "media/base/media_keys.h" | 23 #include "media/base/media_keys.h" |
| 24 #include "media/gpu/avda_codec_allocator.h" | 24 #include "media/gpu/avda_codec_allocator.h" |
| 25 #include "media/gpu/avda_picture_buffer_manager.h" | 25 #include "media/gpu/avda_picture_buffer_manager.h" |
| 26 #include "media/gpu/gpu_video_decode_accelerator_helpers.h" | 26 #include "media/gpu/gpu_video_decode_accelerator_helpers.h" |
| 27 #include "media/gpu/media_gpu_export.h" | 27 #include "media/gpu/media_gpu_export.h" |
| 28 #include "media/video/video_decode_accelerator.h" | 28 #include "media/video/video_decode_accelerator.h" |
| 29 #include "ui/gl/android/scoped_java_surface.h" | 29 #include "ui/gl/android/scoped_java_surface.h" |
| 30 | 30 |
| 31 namespace media { | 31 namespace media { |
| 32 class SharedMemoryRegion; | |
| 33 | 32 |
| 34 // A VideoDecodeAccelerator implementation for Android. This class decodes the | 33 // An Android VideoDecoder that delegates to MediaCodec. |
| 35 // encded input stream using Android's MediaCodec. It handles the work of | |
| 36 // transferring data to and from MediaCodec, and delegates attaching MediaCodec | |
| 37 // output buffers to PictureBuffers to AVDAPictureBufferManager. | |
| 38 class MEDIA_GPU_EXPORT MediaCodecVideoDecoder | 34 class MEDIA_GPU_EXPORT MediaCodecVideoDecoder |
| 39 : public VideoDecodeAccelerator, | 35 : public AVDACodecAllocatorClient { |
| 40 public AVDACodecAllocatorClient { | |
| 41 public: | 36 public: |
| 42 static VideoDecodeAccelerator::Capabilities GetCapabilities( | 37 static VideoDecodeAccelerator::Capabilities GetCapabilities( |
| 43 const gpu::GpuPreferences& gpu_preferences); | 38 const gpu::GpuPreferences& gpu_preferences); |
| 44 | 39 |
| 45 MediaCodecVideoDecoder( | 40 MediaCodecVideoDecoder( |
| 46 const MakeGLContextCurrentCallback& make_context_current_cb, | 41 const MakeGLContextCurrentCallback& make_context_current_cb, |
| 47 const GetGLES2DecoderCallback& get_gles2_decoder_cb); | 42 const GetGLES2DecoderCallback& get_gles2_decoder_cb); |
| 48 | 43 |
| 49 ~MediaCodecVideoDecoder() override; | 44 ~MediaCodecVideoDecoder() override; |
| 50 | 45 |
| 51 // VideoDecodeAccelerator implementation: | 46 // VideoDecodeAccelerator implementation: |
| 52 bool Initialize(const Config& config, Client* client) override; | 47 bool Initialize(const Config& config, Client* client) override; |
| 53 void Decode(const BitstreamBuffer& bitstream_buffer) override; | 48 void Decode(const BitstreamBuffer& bitstream_buffer) override; |
| 54 void AssignPictureBuffers(const std::vector<PictureBuffer>& buffers) override; | |
| 55 void ReusePictureBuffer(int32_t picture_buffer_id) override; | |
| 56 void Flush() override; | 49 void Flush() override; |
| 57 void Reset() override; | 50 void Reset() override; |
| 58 void SetSurface(int32_t surface_id) override; | 51 void SetSurface(int32_t surface_id) override; |
| 59 void Destroy() override; | 52 void Destroy() override; |
| 60 bool TryToSetupDecodeOnSeparateThread( | |
| 61 const base::WeakPtr<Client>& decode_client, | |
| 62 const scoped_refptr<base::SingleThreadTaskRunner>& decode_task_runner) | |
| 63 override; | |
| 64 | 53 |
| 65 // AVDACodecAllocatorClient implementation: | 54 // AVDACodecAllocatorClient implementation: |
| 66 void OnSurfaceAvailable(bool success) override; | 55 void OnSurfaceAvailable(bool success) override; |
| 67 void OnSurfaceDestroyed() override; | 56 void OnSurfaceDestroyed() override; |
| 68 void OnCodecConfigured( | 57 void OnCodecConfigured( |
| 69 std::unique_ptr<VideoCodecBridge> media_codec) override; | 58 std::unique_ptr<VideoCodecBridge> media_codec) override; |
| 70 | 59 |
| 71 private: | 60 private: |
| 72 friend class MCVDManager; | 61 friend class MCVDManager; |
| 73 | 62 |
| 74 // TODO(timav): evaluate the need for more states in the MCVD state machine. | |
| 75 enum State { | 63 enum State { |
| 76 NO_ERROR, | 64 NO_ERROR, |
| 77 ERROR, | 65 ERROR, |
| 78 // Set when we are asynchronously constructing the codec. Will transition | 66 // Set when we are asynchronously constructing the codec. Will transition |
| 79 // to NO_ERROR or ERROR depending on success. | 67 // to NO_ERROR or ERROR depending on success. |
| 80 WAITING_FOR_CODEC, | 68 WAITING_FOR_CODEC, |
| 81 // Set when we have a codec, but it doesn't yet have a key. | 69 // Set when we have a codec, but it doesn't yet have a key. |
| 82 WAITING_FOR_KEY, | 70 WAITING_FOR_KEY, |
| 83 // The output surface was destroyed. We must not configure a new MediaCodec | 71 // The output surface was destroyed. We must not configure a new MediaCodec |
| 84 // with the destroyed surface. | 72 // with the destroyed surface. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 103 void ActualDestroy(); | 91 void ActualDestroy(); |
| 104 | 92 |
| 105 // Configures |media_codec_| with the given codec parameters from the client. | 93 // Configures |media_codec_| with the given codec parameters from the client. |
| 106 // This configuration will (probably) not be complete before this call | 94 // This configuration will (probably) not be complete before this call |
| 107 // returns. Multiple calls before completion will be ignored. |state_| | 95 // returns. Multiple calls before completion will be ignored. |state_| |
| 108 // must be NO_ERROR or WAITING_FOR_CODEC. Note that, once you call this, | 96 // must be NO_ERROR or WAITING_FOR_CODEC. Note that, once you call this, |
| 109 // you should be careful to avoid modifying members of |codec_config_| until | 97 // you should be careful to avoid modifying members of |codec_config_| until |
| 110 // |state_| is no longer WAITING_FOR_CODEC. | 98 // |state_| is no longer WAITING_FOR_CODEC. |
| 111 void ConfigureMediaCodecAsynchronously(); | 99 void ConfigureMediaCodecAsynchronously(); |
| 112 | 100 |
| 113 // Like ConfigureMediaCodecAsynchronously, but synchronous. Returns true if | |
| 114 // and only if |media_codec_| is non-null. Since all configuration is done | |
| 115 // synchronously, there is no concern with modifying |codec_config_| after | |
| 116 // this returns. | |
| 117 bool ConfigureMediaCodecSynchronously(); | |
| 118 | |
| 119 // Instantiate a media codec using |codec_config|. | |
| 120 // This may be called on any thread. | |
| 121 static std::unique_ptr<VideoCodecBridge> ConfigureMediaCodecOnAnyThread( | |
| 122 scoped_refptr<CodecConfig> codec_config); | |
| 123 | |
| 124 // Sends the decoded frame specified by |codec_buffer_index| to the client. | 101 // Sends the decoded frame specified by |codec_buffer_index| to the client. |
| 125 void SendDecodedFrameToClient(int32_t codec_buffer_index, | 102 void SendDecodedFrameToClient(int32_t codec_buffer_index, |
| 126 int32_t bitstream_id); | 103 int32_t bitstream_id); |
| 127 | 104 |
| 128 // Does pending IO tasks if any. Once this is called, it polls |media_codec_| | 105 // Does pending IO tasks if any. Once this is called, it polls |media_codec_| |
| 129 // until it finishes pending tasks. For the polling, |kDecodePollDelay| is | 106 // until it finishes pending tasks. For the polling, |kDecodePollDelay| is |
| 130 // used. | 107 // used. |
| 131 void DoIOTask(bool start_timer); | 108 void DoIOTask(bool start_timer); |
| 132 | 109 |
| 133 // Feeds input data to |media_codec_|. This checks | 110 // Feeds input data to |media_codec_|. This checks |
| 134 // |pending_bitstream_buffers_| and queues a buffer to |media_codec_|. | 111 // |pending_bitstream_buffers_| and queues a buffer to |media_codec_|. |
| 135 // Returns true if any input was processed. | 112 // Returns true if any input was processed. |
| 136 bool QueueInput(); | 113 bool QueueInput(); |
| 137 | 114 |
| 138 // Dequeues output from |media_codec_| and feeds the decoded frame to the | 115 // Dequeues output from |media_codec_| and feeds the decoded frame to the |
| 139 // client. Returns a hint about whether calling again might produce | 116 // client. Returns a hint about whether calling again might produce |
| 140 // more output. | 117 // more output. |
| 141 bool DequeueOutput(); | 118 bool DequeueOutput(); |
| 142 | 119 |
| 143 // Requests picture buffers from the client. | |
| 144 void RequestPictureBuffers(); | |
| 145 | |
| 146 // Decode the content in the |bitstream_buffer|. Note that a | 120 // Decode the content in the |bitstream_buffer|. Note that a |
| 147 // |bitstream_buffer| of id as -1 indicates a flush command. | 121 // |bitstream_buffer| of id as -1 indicates a flush command. |
| 148 void DecodeBuffer(const BitstreamBuffer& bitstream_buffer); | 122 void DecodeBuffer(const BitstreamBuffer& bitstream_buffer); |
| 149 | 123 |
| 150 // Called during Initialize() for encrypted streams to set up the CDM. | 124 // Called during Initialize() for encrypted streams to set up the CDM. |
| 151 void InitializeCdm(); | 125 void InitializeCdm(); |
| 152 | 126 |
| 153 // Called after the CDM obtains a MediaCrypto object. | 127 // Called after the CDM obtains a MediaCrypto object. |
| 154 void OnMediaCryptoReady(MediaDrmBridgeCdmContext::JavaObjectPtr media_crypto, | 128 void OnMediaCryptoReady(MediaDrmBridgeCdmContext::JavaObjectPtr media_crypto, |
| 155 bool needs_protected_surface); | 129 bool needs_protected_surface); |
| 156 | 130 |
| 157 // Called when a new key is added to the CDM. | 131 // Called when a new key is added to the CDM. |
| 158 void OnKeyAdded(); | 132 void OnKeyAdded(); |
| 159 | 133 |
| 160 // Notifies the client of the result of deferred initialization. | |
| 161 void NotifyInitializationComplete(bool success); | |
| 162 | |
| 163 // Notifies the client about the availability of a picture. | |
| 164 void NotifyPictureReady(const Picture& picture); | |
| 165 | |
| 166 // Notifies the client that the input buffer identifed by input_buffer_id has | |
| 167 // been processed. | |
| 168 void NotifyEndOfBitstreamBuffer(int input_buffer_id); | |
| 169 | |
| 170 // Notifies the client that the decoder was flushed. | |
| 171 void NotifyFlushDone(); | |
| 172 | |
| 173 // Notifies the client that the decoder was reset. | |
| 174 void NotifyResetDone(); | |
| 175 | |
| 176 // Notifies the client about the error and sets |state_| to |ERROR|. | 134 // Notifies the client about the error and sets |state_| to |ERROR|. |
| 177 void NotifyError(Error error); | 135 void NotifyError(Error error); |
| 178 | 136 |
| 179 // Start or stop our work-polling timer based on whether we did any work, and | 137 // Start or stop our work-polling timer based on whether we did any work, and |
| 180 // how long it has been since we've done work. Calling this with true will | 138 // how long it has been since we've done work. Calling this with true will |
| 181 // start the timer. Calling it with false may stop the timer. | 139 // start the timer. Calling it with false may stop the timer. |
| 182 void ManageTimer(bool did_work); | 140 void ManageTimer(bool did_work); |
| 183 | 141 |
| 184 // Start the MediaCodec drain process by adding end_of_stream() buffer to the | 142 // Start the MediaCodec drain process by adding end_of_stream() buffer to the |
| 185 // encoded buffers queue. When we receive EOS from the output buffer the drain | 143 // encoded buffers queue. When we receive EOS from the output buffer the drain |
| (...skipping 22 matching lines...) Expand all Loading... |
| 208 | 166 |
| 209 // On platforms which support seamless surface changes, this will reinitialize | 167 // On platforms which support seamless surface changes, this will reinitialize |
| 210 // the picture buffer manager with the new surface. This function reads and | 168 // the picture buffer manager with the new surface. This function reads and |
| 211 // clears the surface id from |pending_surface_id_|. It will issue a decode | 169 // clears the surface id from |pending_surface_id_|. It will issue a decode |
| 212 // error if the surface change fails. Returns false on failure. | 170 // error if the surface change fails. Returns false on failure. |
| 213 bool UpdateSurface(); | 171 bool UpdateSurface(); |
| 214 | 172 |
| 215 // Used to DCHECK that we are called on the correct thread. | 173 // Used to DCHECK that we are called on the correct thread. |
| 216 base::ThreadChecker thread_checker_; | 174 base::ThreadChecker thread_checker_; |
| 217 | 175 |
| 218 // To expose client callbacks from VideoDecodeAccelerator. | |
| 219 Client* client_; | |
| 220 | |
| 221 // Callback to set the correct gl context. | 176 // Callback to set the correct gl context. |
| 222 MakeGLContextCurrentCallback make_context_current_cb_; | 177 MakeGLContextCurrentCallback make_context_current_cb_; |
| 223 | 178 |
| 224 // Callback to get the GLES2Decoder instance. | 179 // Callback to get the GLES2Decoder instance. |
| 225 GetGLES2DecoderCallback get_gles2_decoder_cb_; | 180 GetGLES2DecoderCallback get_gles2_decoder_cb_; |
| 226 | 181 |
| 227 // The current state of this class. For now, this is used only for setting | |
| 228 // error state. | |
| 229 State state_; | 182 State state_; |
| 230 | 183 |
| 231 // The assigned picture buffers by picture buffer id. | |
| 232 AVDAPictureBufferManager::PictureBufferMap output_picture_buffers_; | |
| 233 | |
| 234 // This keeps the free picture buffer ids which can be used for sending | |
| 235 // decoded frames to the client. | |
| 236 std::queue<int32_t> free_picture_ids_; | |
| 237 | |
| 238 // The low-level decoder which Android SDK provides. | 184 // The low-level decoder which Android SDK provides. |
| 239 std::unique_ptr<VideoCodecBridge> media_codec_; | 185 std::unique_ptr<VideoCodecBridge> media_codec_; |
| 240 | 186 |
| 241 // Set to true after requesting picture buffers to the client. | |
| 242 bool picturebuffers_requested_; | |
| 243 | |
| 244 // The resolution of the stream. | 187 // The resolution of the stream. |
| 245 gfx::Size size_; | 188 gfx::Size size_; |
| 246 | 189 |
| 247 // Handy structure to remember a BitstreamBuffer and also its shared memory, | |
| 248 // if any. The goal is to prevent leaving a BitstreamBuffer's shared memory | |
| 249 // handle open. | |
| 250 struct BitstreamRecord { | |
| 251 BitstreamRecord(const BitstreamBuffer&); | |
| 252 BitstreamRecord(BitstreamRecord&& other); | |
| 253 ~BitstreamRecord(); | |
| 254 | |
| 255 BitstreamBuffer buffer; | |
| 256 | |
| 257 // |memory| is not mapped, and may be null if buffer has no data. | |
| 258 std::unique_ptr<SharedMemoryRegion> memory; | |
| 259 }; | |
| 260 | |
| 261 // Encoded bitstream buffers to be passed to media codec, queued until an | 190 // Encoded bitstream buffers to be passed to media codec, queued until an |
| 262 // input buffer is available. | 191 // input buffer is available. |
| 263 std::queue<BitstreamRecord> pending_bitstream_records_; | 192 std::queue<BitstreamRecord> pending_bitstream_records_; |
| 264 | 193 |
| 265 // A map of presentation timestamp to bitstream buffer id for the bitstream | 194 // A map of presentation timestamp to bitstream buffer id for the bitstream |
| 266 // buffers that have been submitted to the decoder but haven't yet produced an | 195 // buffers that have been submitted to the decoder but haven't yet produced an |
| 267 // output frame with the same timestamp. Note: there will only be one entry | 196 // output frame with the same timestamp. Note: there will only be one entry |
| 268 // for multiple bitstream buffers that have the same presentation timestamp. | 197 // for multiple bitstream buffers that have the same presentation timestamp. |
| 269 std::map<base::TimeDelta, int32_t> bitstream_buffers_in_decoder_; | 198 std::map<base::TimeDelta, int32_t> bitstream_buffers_in_decoder_; |
| 270 | 199 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 291 int cdm_registration_id_; | 220 int cdm_registration_id_; |
| 292 | 221 |
| 293 // Configuration that we use for MediaCodec. | 222 // Configuration that we use for MediaCodec. |
| 294 // Do not update any of its members while |state_| is WAITING_FOR_CODEC. | 223 // Do not update any of its members while |state_| is WAITING_FOR_CODEC. |
| 295 scoped_refptr<CodecConfig> codec_config_; | 224 scoped_refptr<CodecConfig> codec_config_; |
| 296 | 225 |
| 297 // Index of the dequeued and filled buffer that we keep trying to enqueue. | 226 // Index of the dequeued and filled buffer that we keep trying to enqueue. |
| 298 // Such buffer appears in MEDIA_CODEC_NO_KEY processing. | 227 // Such buffer appears in MEDIA_CODEC_NO_KEY processing. |
| 299 int pending_input_buf_index_; | 228 int pending_input_buf_index_; |
| 300 | 229 |
| 301 // Monotonically increasing value that is used to prevent old, delayed errors | |
| 302 // from being sent after a reset. | |
| 303 int error_sequence_token_; | |
| 304 | |
| 305 // True if and only if VDA initialization is deferred, and we have not yet | 230 // True if and only if VDA initialization is deferred, and we have not yet |
| 306 // called NotifyInitializationComplete. | 231 // called NotifyInitializationComplete. |
| 307 bool deferred_initialization_pending_; | 232 bool deferred_initialization_pending_; |
| 308 | 233 |
| 309 // Indicates if ResetCodecState() should be called upon the next call to | 234 // Indicates if ResetCodecState() should be called upon the next call to |
| 310 // Decode(). Allows us to avoid trashing the last few frames of a playback | 235 // Decode(). Allows us to avoid trashing the last few frames of a playback |
| 311 // when the EOS buffer is received. | 236 // when the EOS buffer is received. |
| 312 bool codec_needs_reset_; | 237 bool codec_needs_reset_; |
| 313 | 238 |
| 314 // True if surface creation and |picture_buffer_manager_| initialization has | 239 // True if surface creation and |picture_buffer_manager_| initialization has |
| (...skipping 10 matching lines...) Expand all Loading... |
| 325 | 250 |
| 326 // WeakPtrFactory for posting tasks back to |this|. | 251 // WeakPtrFactory for posting tasks back to |this|. |
| 327 base::WeakPtrFactory<MediaCodecVideoDecoder> weak_this_factory_; | 252 base::WeakPtrFactory<MediaCodecVideoDecoder> weak_this_factory_; |
| 328 | 253 |
| 329 friend class MediaCodecVideoDecoderTest; | 254 friend class MediaCodecVideoDecoderTest; |
| 330 }; | 255 }; |
| 331 | 256 |
| 332 } // namespace media | 257 } // namespace media |
| 333 | 258 |
| 334 #endif // MEDIA_GPU_ANDROID_MEDIA_CODEC_VIDEO_DECODER_H_ | 259 #endif // MEDIA_GPU_ANDROID_MEDIA_CODEC_VIDEO_DECODER_H_ |
| OLD | NEW |