| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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_VIDEO_DECODE_ACCELERATOR_H_ | 5 #ifndef MEDIA_GPU_ANDROID_VIDEO_DECODE_ACCELERATOR_H_ |
| 6 #define MEDIA_GPU_ANDROID_VIDEO_DECODE_ACCELERATOR_H_ | 6 #define MEDIA_GPU_ANDROID_VIDEO_DECODE_ACCELERATOR_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <list> | 10 #include <list> |
| 11 #include <map> | 11 #include <map> |
| 12 #include <queue> | 12 #include <queue> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 16 #include "base/optional.h" | 16 #include "base/optional.h" |
| 17 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
| 18 #include "base/timer/timer.h" | 18 #include "base/timer/timer.h" |
| 19 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" | 19 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" |
| 20 #include "gpu/command_buffer/service/gpu_preferences.h" | 20 #include "gpu/command_buffer/service/gpu_preferences.h" |
| 21 #include "media/base/android/media_codec_bridge_impl.h" | 21 #include "media/base/android/media_codec_bridge_impl.h" |
| 22 #include "media/base/android/media_drm_bridge_cdm_context.h" | 22 #include "media/base/android/media_drm_bridge_cdm_context.h" |
| 23 #include "media/base/content_decryption_module.h" | 23 #include "media/base/content_decryption_module.h" |
| 24 #include "media/gpu/android_video_surface_chooser.h" |
| 24 #include "media/gpu/avda_codec_allocator.h" | 25 #include "media/gpu/avda_codec_allocator.h" |
| 25 #include "media/gpu/avda_picture_buffer_manager.h" | 26 #include "media/gpu/avda_picture_buffer_manager.h" |
| 26 #include "media/gpu/avda_state_provider.h" | 27 #include "media/gpu/avda_state_provider.h" |
| 27 #include "media/gpu/gpu_video_decode_accelerator_helpers.h" | 28 #include "media/gpu/gpu_video_decode_accelerator_helpers.h" |
| 28 #include "media/gpu/media_gpu_export.h" | 29 #include "media/gpu/media_gpu_export.h" |
| 29 #include "media/video/video_decode_accelerator.h" | 30 #include "media/video/video_decode_accelerator.h" |
| 30 #include "ui/gl/android/scoped_java_surface.h" | 31 #include "ui/gl/android/scoped_java_surface.h" |
| 31 #include "ui/gl/android/surface_texture.h" | 32 #include "ui/gl/android/surface_texture.h" |
| 32 | 33 |
| 33 namespace media { | 34 namespace media { |
| 34 class SharedMemoryRegion; | 35 class SharedMemoryRegion; |
| 35 | 36 |
| 36 // A VideoDecodeAccelerator implementation for Android. This class decodes the | 37 // A VideoDecodeAccelerator implementation for Android. This class decodes the |
| 37 // encded input stream using Android's MediaCodec. It handles the work of | 38 // encoded input stream using Android's MediaCodec. It handles the work of |
| 38 // transferring data to and from MediaCodec, and delegates attaching MediaCodec | 39 // transferring data to and from MediaCodec, and delegates attaching MediaCodec |
| 39 // output buffers to PictureBuffers to AVDAPictureBufferManager. | 40 // output buffers to PictureBuffers to AVDAPictureBufferManager. |
| 40 class MEDIA_GPU_EXPORT AndroidVideoDecodeAccelerator | 41 class MEDIA_GPU_EXPORT AndroidVideoDecodeAccelerator |
| 41 : public VideoDecodeAccelerator, | 42 : public VideoDecodeAccelerator, |
| 42 public AVDAStateProvider, | 43 public AVDAStateProvider, |
| 43 public AVDACodecAllocatorClient { | 44 public AVDACodecAllocatorClient { |
| 44 public: | 45 public: |
| 45 static VideoDecodeAccelerator::Capabilities GetCapabilities( | 46 static VideoDecodeAccelerator::Capabilities GetCapabilities( |
| 46 const gpu::GpuPreferences& gpu_preferences); | 47 const gpu::GpuPreferences& gpu_preferences); |
| 47 | 48 |
| 49 // Various things that we normally ask the platform for, mostly for testing. |
| 50 struct PlatformConfig { |
| 51 int sdk_int = base::android::SDK_VERSION_MARSHMALLOW; |
| 52 |
| 53 // Is SetSurface supported? This is not the same as >= M, since we |
| 54 // blacklist some devices. |
| 55 bool allow_setsurface = true; |
| 56 |
| 57 // For testing. |
| 58 bool force_deferred_surface_creation = false; |
| 59 |
| 60 // Create a default value that's appropriate for use in production. |
| 61 static PlatformConfig CreateDefault(); |
| 62 }; |
| 63 |
| 48 AndroidVideoDecodeAccelerator( | 64 AndroidVideoDecodeAccelerator( |
| 49 AVDACodecAllocator* codec_allocator, | 65 AVDACodecAllocator* codec_allocator, |
| 66 std::unique_ptr<AndroidVideoSurfaceChooser> overlay_helper, |
| 50 const MakeGLContextCurrentCallback& make_context_current_cb, | 67 const MakeGLContextCurrentCallback& make_context_current_cb, |
| 51 const GetGLES2DecoderCallback& get_gles2_decoder_cb); | 68 const GetGLES2DecoderCallback& get_gles2_decoder_cb, |
| 69 const PlatformConfig& platform_config); |
| 52 | 70 |
| 53 ~AndroidVideoDecodeAccelerator() override; | 71 ~AndroidVideoDecodeAccelerator() override; |
| 54 | 72 |
| 55 // VideoDecodeAccelerator implementation: | 73 // VideoDecodeAccelerator implementation: |
| 56 bool Initialize(const Config& config, Client* client) override; | 74 bool Initialize(const Config& config, Client* client) override; |
| 57 void Decode(const BitstreamBuffer& bitstream_buffer) override; | 75 void Decode(const BitstreamBuffer& bitstream_buffer) override; |
| 58 void AssignPictureBuffers(const std::vector<PictureBuffer>& buffers) override; | 76 void AssignPictureBuffers(const std::vector<PictureBuffer>& buffers) override; |
| 59 void ReusePictureBuffer(int32_t picture_buffer_id) override; | 77 void ReusePictureBuffer(int32_t picture_buffer_id) override; |
| 60 void Flush() override; | 78 void Flush() override; |
| 61 void Reset() override; | 79 void Reset() override; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 79 void OnCodecConfigured( | 97 void OnCodecConfigured( |
| 80 std::unique_ptr<MediaCodecBridge> media_codec) override; | 98 std::unique_ptr<MediaCodecBridge> media_codec) override; |
| 81 | 99 |
| 82 private: | 100 private: |
| 83 friend class AVDAManager; | 101 friend class AVDAManager; |
| 84 | 102 |
| 85 // TODO(timav): evaluate the need for more states in the AVDA state machine. | 103 // TODO(timav): evaluate the need for more states in the AVDA state machine. |
| 86 enum State { | 104 enum State { |
| 87 NO_ERROR, | 105 NO_ERROR, |
| 88 ERROR, | 106 ERROR, |
| 89 // We have requested a surface, but haven't allocated it yet. When the | 107 // We haven't initialized |overlay_helper_| yet, so we don't have a surface |
| 90 // surface arrives, we'll transition to WAITING_FOR_CODEC, NO_ERROR, or | 108 // or a codec. After we initialize |overlay_helper_|, we'll transition to |
| 91 // ERROR. This is also the initial state, before we've even requested a | 109 // WAITING_FOR_CODEC, NO_ERROR, or ERROR. |
| 92 // surface, just because it's convenient. | 110 BEFORE_OVERLAY_INIT, |
| 93 WAITING_FOR_SURFACE, | |
| 94 // Set when we are asynchronously constructing the codec. Will transition | 111 // Set when we are asynchronously constructing the codec. Will transition |
| 95 // to NO_ERROR or ERROR depending on success. | 112 // to NO_ERROR or ERROR depending on success. |
| 96 WAITING_FOR_CODEC, | 113 WAITING_FOR_CODEC, |
| 97 // Set when we have a codec, but it doesn't yet have a key. | 114 // Set when we have a codec, but it doesn't yet have a key. |
| 98 WAITING_FOR_KEY, | 115 WAITING_FOR_KEY, |
| 99 // The output surface was destroyed. We must not configure a new MediaCodec | 116 // The output surface was destroyed. We must not configure a new MediaCodec |
| 100 // with the destroyed surface. | 117 // with the destroyed surface. |
| 101 SURFACE_DESTROYED, | 118 SURFACE_DESTROYED, |
| 102 }; | 119 }; |
| 103 | 120 |
| 104 enum DrainType { | 121 enum DrainType { |
| 105 DRAIN_FOR_FLUSH, | 122 DRAIN_FOR_FLUSH, |
| 106 DRAIN_FOR_RESET, | 123 DRAIN_FOR_RESET, |
| 107 DRAIN_FOR_DESTROY, | 124 DRAIN_FOR_DESTROY, |
| 108 }; | 125 }; |
| 109 | 126 |
| 110 // Entry point for configuring / reconfiguring a codec with a new surface. | 127 // Called once before (possibly deferred) initialization succeeds, to set up |
| 111 // Start surface creation by trying to allocate the surface id. Will either | 128 // |overlay_helper_| with our initial factory from VDA::Config. |
| 112 // InitializePictureBufferManager if the surface is available immediately, or | 129 void StartOverlayHelper(); |
| 113 // will wait for OnOverlayReady to do it. This will transition |state_| | |
| 114 // to WAITING_FOR_SURFACE or WAITING_FOR_CODEC, as needed (or NO_ERROR if it | |
| 115 // gets the surface and the codec without waiting). | |
| 116 // Note that this requires that you create a new |incoming_bundle_| with the | |
| 117 // appropriate surface id. | |
| 118 void StartSurfaceCreation(); | |
| 119 | 130 |
| 120 // Called by AndroidOverlay when a surface becomes available. | 131 // Start a transition to an overlay, or, if |!overlay|, SurfaceTexture. The |
| 121 void OnOverlayReady(AndroidOverlay* overlay); | 132 // transition doesn't have to be immediate; we'll favor not dropping frames. |
| 133 void OnSurfaceTransition(std::unique_ptr<AndroidOverlay> overlay); |
| 122 | 134 |
| 123 // Called by AndroidOverlay when the overlay will not call OnOverlayReady. | 135 // Called by AndroidOverlay when a surface is lost. We will discard pending |
| 124 void OnOverlayFailed(AndroidOverlay* overlay); | 136 // frames, as needed, to switch away from |overlay| if we're using it. Before |
| 125 | 137 // we return, we will have either dropped |overlay| if we own it, or posted |
| 126 // Called by AndroidOverlay when a surface is lost. | 138 // it for async release with the codec that's using it. We also handle the |
| 127 void OnSurfaceDestroyed(AndroidOverlay* overlay); | 139 // case where we're not using |overlay| at all, since that can happen too |
| 140 // while async codec release is pending. |
| 141 void OnStopUsingOverlayImmediately(AndroidOverlay* overlay); |
| 128 | 142 |
| 129 // Initializes the picture buffer manager to use the current surface, once | 143 // Initializes the picture buffer manager to use the current surface, once |
| 130 // it is available. This is not normally called directly, but rather via | 144 // it is available. This is not normally called directly, but rather via |
| 131 // StartSurfaceCreation. If we have a media codec already, then this will | 145 // StartSurfaceCreation. If we have a media codec already, then this will |
| 132 // attempt to setSurface the new surface. Otherwise, it will start codec | 146 // attempt to setSurface the new surface. Otherwise, it will start codec |
| 133 // config using the new surface. In that case, there might not be a codec | 147 // config using the new surface. In that case, there might not be a codec |
| 134 // ready even if this succeeds, but async config will be started. If | 148 // ready even if this succeeds, but async config will be started. If |
| 135 // setSurface fails, this will not replace the codec. On failure, this will | 149 // setSurface fails, this will not replace the codec. On failure, this will |
| 136 // transition |state_| to ERROR. | 150 // transition |state_| to ERROR. |
| 137 // Note that this assumes that there is an |incoming_bundle_| that we'll use. | 151 // Note that this assumes that there is an |incoming_bundle_| that we'll use. |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 // On platforms which support seamless surface changes, this will reinitialize | 254 // On platforms which support seamless surface changes, this will reinitialize |
| 241 // the picture buffer manager with the new surface. This function reads and | 255 // the picture buffer manager with the new surface. This function reads and |
| 242 // clears the surface id from |pending_surface_id_|. It will issue a decode | 256 // clears the surface id from |pending_surface_id_|. It will issue a decode |
| 243 // error if the surface change fails. Returns false on failure. | 257 // error if the surface change fails. Returns false on failure. |
| 244 bool UpdateSurface(); | 258 bool UpdateSurface(); |
| 245 | 259 |
| 246 // Release |media_codec_| if it's not null, and notify | 260 // Release |media_codec_| if it's not null, and notify |
| 247 // |picture_buffer_manager_|. | 261 // |picture_buffer_manager_|. |
| 248 void ReleaseCodec(); | 262 void ReleaseCodec(); |
| 249 | 263 |
| 250 // Returns the surface ID from the incoming bundle, if we have one, or | 264 // ReleaseCodec(), and also drop our ref to it's surface bundle. This is the |
| 251 // the current surface bundle if not. The reasoning is that, if we have an | 265 // right thing to do unless you're planning to re-use the bundle with another |
| 252 // incoming bundle, then the current (outgoing) one has already been returned | 266 // codec. Normally, one doesn't. |
| 253 // to the codec allocator via DeallocateSurface. The only place this happens | 267 void ReleaseCodecAndBundle(); |
| 254 // is UpdateSurface, which handles it specially. | |
| 255 int surface_id() const { | |
| 256 return incoming_bundle_ ? incoming_bundle_->surface_id | |
| 257 : codec_config_->surface_bundle->surface_id; | |
| 258 } | |
| 259 | 268 |
| 260 // Used to DCHECK that we are called on the correct thread. | 269 // Used to DCHECK that we are called on the correct thread. |
| 261 base::ThreadChecker thread_checker_; | 270 base::ThreadChecker thread_checker_; |
| 262 | 271 |
| 263 // To expose client callbacks from VideoDecodeAccelerator. | 272 // To expose client callbacks from VideoDecodeAccelerator. |
| 264 Client* client_; | 273 Client* client_; |
| 265 | 274 |
| 266 AVDACodecAllocator* codec_allocator_; | 275 AVDACodecAllocator* codec_allocator_; |
| 267 | 276 |
| 268 // Callback to set the correct gl context. | 277 // Callback to set the correct gl context. |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 // Copy of the VDA::Config we were given. | 382 // Copy of the VDA::Config we were given. |
| 374 Config config_; | 383 Config config_; |
| 375 | 384 |
| 376 // SurfaceBundle that we're going to use for StartSurfaceCreation. This is | 385 // SurfaceBundle that we're going to use for StartSurfaceCreation. This is |
| 377 // separate than the bundle in |codec_config_|, since we can start surface | 386 // separate than the bundle in |codec_config_|, since we can start surface |
| 378 // creation while another codec is using the old surface. For example, if | 387 // creation while another codec is using the old surface. For example, if |
| 379 // we're going to SetSurface, then the current codec will depend on the | 388 // we're going to SetSurface, then the current codec will depend on the |
| 380 // current bundle until then. | 389 // current bundle until then. |
| 381 scoped_refptr<AVDASurfaceBundle> incoming_bundle_; | 390 scoped_refptr<AVDASurfaceBundle> incoming_bundle_; |
| 382 | 391 |
| 392 // If we have been given an overlay to use, then this is it. If we've been |
| 393 // told to move to SurfaceTexture, then this will be value() == nullptr. |
| 394 base::Optional<std::unique_ptr<AndroidOverlay>> incoming_overlay_; |
| 395 |
| 396 std::unique_ptr<AndroidVideoSurfaceChooser> surface_chooser_; |
| 397 |
| 398 PlatformConfig platform_config_; |
| 399 |
| 383 // WeakPtrFactory for posting tasks back to |this|. | 400 // WeakPtrFactory for posting tasks back to |this|. |
| 384 base::WeakPtrFactory<AndroidVideoDecodeAccelerator> weak_this_factory_; | 401 base::WeakPtrFactory<AndroidVideoDecodeAccelerator> weak_this_factory_; |
| 385 | 402 |
| 386 friend class AndroidVideoDecodeAcceleratorTest; | 403 friend class AndroidVideoDecodeAcceleratorTest; |
| 387 }; | 404 }; |
| 388 | 405 |
| 389 } // namespace media | 406 } // namespace media |
| 390 | 407 |
| 391 #endif // MEDIA_GPU_ANDROID_VIDEO_DECODE_ACCELERATOR_H_ | 408 #endif // MEDIA_GPU_ANDROID_VIDEO_DECODE_ACCELERATOR_H_ |
| OLD | NEW |