| 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_AVDA_CODEC_ALLOCATOR_H_ | 5 #ifndef MEDIA_GPU_AVDA_CODEC_ALLOCATOR_H_ |
| 6 #define MEDIA_GPU_AVDA_CODEC_ALLOCATOR_H_ | 6 #define MEDIA_GPU_AVDA_CODEC_ALLOCATOR_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 | 11 |
| 12 #include "base/android/build_info.h" | 12 #include "base/android/build_info.h" |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/optional.h" | 15 #include "base/optional.h" |
| 16 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
| 17 #include "base/sys_info.h" | 17 #include "base/sys_info.h" |
| 18 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
| 19 #include "base/threading/thread_checker.h" | 19 #include "base/threading/thread_checker.h" |
| 20 #include "base/time/tick_clock.h" | 20 #include "base/time/tick_clock.h" |
| 21 #include "base/trace_event/trace_event.h" | 21 #include "base/trace_event/trace_event.h" |
| 22 #include "media/base/android/android_overlay.h" |
| 22 #include "media/base/android/media_codec_bridge_impl.h" | 23 #include "media/base/android/media_codec_bridge_impl.h" |
| 23 #include "media/base/android/media_drm_bridge_cdm_context.h" | 24 #include "media/base/android/media_drm_bridge_cdm_context.h" |
| 24 #include "media/base/media.h" | 25 #include "media/base/media.h" |
| 25 #include "media/base/surface_manager.h" | 26 #include "media/base/surface_manager.h" |
| 26 #include "media/base/video_codecs.h" | 27 #include "media/base/video_codecs.h" |
| 27 #include "media/gpu/media_gpu_export.h" | 28 #include "media/gpu/media_gpu_export.h" |
| 28 #include "ui/gfx/geometry/size.h" | 29 #include "ui/gfx/geometry/size.h" |
| 29 #include "ui/gl/android/scoped_java_surface.h" | 30 #include "ui/gl/android/scoped_java_surface.h" |
| 30 | 31 |
| 31 namespace media { | 32 namespace media { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 42 | 43 |
| 43 // Configuration info for MediaCodec. | 44 // Configuration info for MediaCodec. |
| 44 // This is used to shuttle configuration info between threads without needing | 45 // This is used to shuttle configuration info between threads without needing |
| 45 // to worry about the lifetime of the AVDA instance. | 46 // to worry about the lifetime of the AVDA instance. |
| 46 class CodecConfig : public base::RefCountedThreadSafe<CodecConfig> { | 47 class CodecConfig : public base::RefCountedThreadSafe<CodecConfig> { |
| 47 public: | 48 public: |
| 48 CodecConfig(); | 49 CodecConfig(); |
| 49 | 50 |
| 50 VideoCodec codec = kUnknownVideoCodec; | 51 VideoCodec codec = kUnknownVideoCodec; |
| 51 | 52 |
| 52 // The surface that MediaCodec is configured to output to. | 53 // The surface that MediaCodec is configured to output to. This can be either |
| 53 gl::ScopedJavaSurface surface; | 54 // a SurfaceTexture or other Surface provider. |
| 55 // TODO(liberato): it would be nice if we had an abstraction that included |
| 56 // SurfaceTexture and Overlay, but we don't right now. |
| 57 const base::android::JavaRef<jobject>& j_surface() const { |
| 58 if (overlay) |
| 59 return overlay->j_surface(); |
| 60 else |
| 61 return surface_texture_surface.j_surface(); |
| 62 } |
| 63 |
| 64 // Surface ID, if we're using a ContentVideoViewOverlay. |
| 54 int surface_id = SurfaceManager::kNoSurfaceID; | 65 int surface_id = SurfaceManager::kNoSurfaceID; |
| 55 | 66 |
| 56 // The SurfaceTexture attached to |surface|, or nullptr if |surface| is | 67 // If |overlay| is non-null, then |overlay| owns |surface|. |
| 57 // SurfaceView backed. | 68 std::unique_ptr<AndroidOverlay> overlay; |
| 69 |
| 70 // The SurfaceTexture attached to |surface_texture_surface|, or nullptr if |
| 71 // this is SurfaceView backed. |
| 58 scoped_refptr<gl::SurfaceTexture> surface_texture; | 72 scoped_refptr<gl::SurfaceTexture> surface_texture; |
| 59 | 73 |
| 74 // If |surface_texture| is not null, then this is the surface for it. |
| 75 gl::ScopedJavaSurface surface_texture_surface; |
| 76 |
| 60 // The MediaCrypto that MediaCodec is configured with for an encrypted stream. | 77 // The MediaCrypto that MediaCodec is configured with for an encrypted stream. |
| 61 MediaDrmBridgeCdmContext::JavaObjectPtr media_crypto; | 78 MediaDrmBridgeCdmContext::JavaObjectPtr media_crypto; |
| 62 | 79 |
| 63 // Whether the encryption scheme requires us to use a protected surface. | 80 // Whether the encryption scheme requires us to use a protected surface. |
| 64 bool needs_protected_surface = false; | 81 bool needs_protected_surface = false; |
| 65 | 82 |
| 66 // The initial coded size. The actual size might change at any time, so this | 83 // The initial coded size. The actual size might change at any time, so this |
| 67 // is only a hint. | 84 // is only a hint. |
| 68 gfx::Size initial_expected_coded_size; | 85 gfx::Size initial_expected_coded_size; |
| 69 | 86 |
| 70 // The type of allocation to use for. We use this to select the right thread | 87 // The type of allocation to use for. We use this to select the right thread |
| 71 // for construction / destruction, and to decide if we should restrict the | 88 // for construction / destruction, and to decide if we should restrict the |
| 72 // codec to be software only. | 89 // codec to be software only. |
| 73 TaskType task_type; | 90 TaskType task_type; |
| 74 | 91 |
| 75 // Codec specific data (SPS and PPS for H264). | 92 // Codec specific data (SPS and PPS for H264). |
| 76 std::vector<uint8_t> csd0; | 93 std::vector<uint8_t> csd0; |
| 77 std::vector<uint8_t> csd1; | 94 std::vector<uint8_t> csd1; |
| 78 | 95 |
| 79 protected: | 96 protected: |
| 80 friend class base::RefCountedThreadSafe<CodecConfig>; | 97 friend class base::RefCountedThreadSafe<CodecConfig>; |
| 81 virtual ~CodecConfig(); | 98 virtual ~CodecConfig(); |
| 82 | 99 |
| 83 private: | 100 private: |
| 84 DISALLOW_COPY_AND_ASSIGN(CodecConfig); | 101 DISALLOW_COPY_AND_ASSIGN(CodecConfig); |
| 85 }; | 102 }; |
| 86 | 103 |
| 87 class AVDACodecAllocatorClient { | 104 class AVDASurfaceAllocatorClient { |
| 88 public: | 105 public: |
| 89 // Called when the requested SurfaceView becomes available after a call to | 106 // Called when the requested SurfaceView becomes available after a call to |
| 90 // AllocateSurface() | 107 // AllocateSurface() |
| 91 virtual void OnSurfaceAvailable(bool success) = 0; | 108 virtual void OnSurfaceAvailable(bool success) = 0; |
| 92 | 109 |
| 93 // Called when the allocated surface is being destroyed. This must either | 110 // Called when the allocated surface is being destroyed. This must either |
| 94 // replace the surface with MediaCodec#setSurface, or release the MediaCodec | 111 // replace the surface with MediaCodec#setSurface, or release the MediaCodec |
| 95 // it's attached to. The client no longer owns the surface and doesn't | 112 // it's attached to. The client no longer owns the surface and doesn't |
| 96 // need to call DeallocateSurface(); | 113 // need to call DeallocateSurface(); |
| 97 virtual void OnSurfaceDestroyed() = 0; | 114 virtual void OnSurfaceDestroyed() = 0; |
| 98 | 115 |
| 116 protected: |
| 117 ~AVDASurfaceAllocatorClient() {} |
| 118 }; |
| 119 |
| 120 class AVDACodecAllocatorClient { |
| 121 public: |
| 99 // Called on the main thread when a new MediaCodec is configured. | 122 // Called on the main thread when a new MediaCodec is configured. |
| 100 // |media_codec| will be null if configuration failed. | 123 // |media_codec| will be null if configuration failed. |
| 101 virtual void OnCodecConfigured( | 124 virtual void OnCodecConfigured( |
| 125 scoped_refptr<CodecConfig> codec_config, |
| 102 std::unique_ptr<VideoCodecBridge> media_codec) = 0; | 126 std::unique_ptr<VideoCodecBridge> media_codec) = 0; |
| 103 | 127 |
| 104 protected: | 128 protected: |
| 105 ~AVDACodecAllocatorClient() {} | 129 ~AVDACodecAllocatorClient() {} |
| 106 }; | 130 }; |
| 107 | 131 |
| 108 // AVDACodecAllocator manages threads for allocating and releasing MediaCodec | 132 // AVDACodecAllocator manages threads for allocating and releasing MediaCodec |
| 109 // instances. These activities can hang, depending on android version, due | 133 // instances. These activities can hang, depending on android version, due |
| 110 // to mediaserver bugs. AVDACodecAllocator detects these cases, and reports | 134 // to mediaserver bugs. AVDACodecAllocator detects these cases, and reports |
| 111 // on them to allow software fallback if the HW path is hung up. | 135 // on them to allow software fallback if the HW path is hung up. |
| 112 class MEDIA_GPU_EXPORT AVDACodecAllocator { | 136 class MEDIA_GPU_EXPORT AVDACodecAllocator { |
| 113 public: | 137 public: |
| 114 static AVDACodecAllocator* Instance(); | 138 static AVDACodecAllocator* Instance(); |
| 115 | 139 |
| 116 // Called synchronously when the given surface is being destroyed on the | 140 // Called synchronously when the given surface is being destroyed on the |
| 117 // browser UI thread. | 141 // browser UI thread. |
| 118 void OnSurfaceDestroyed(int surface_id); | 142 void OnSurfaceDestroyed(int surface_id); |
| 119 | 143 |
| 120 // Make sure the construction threads are started for |client|. Returns true | 144 // Make sure the construction threads are started for |client|. Returns true |
| 121 // on success. | 145 // on success. |
| 122 bool StartThread(AVDACodecAllocatorClient* client); | 146 bool StartThread(AVDACodecAllocatorClient* client); |
| 123 | 147 |
| 124 void StopThread(AVDACodecAllocatorClient* client); | 148 void StopThread(AVDACodecAllocatorClient* client); |
| 125 | 149 |
| 126 // Returns true if the caller now owns the surface, or false if someone else | 150 // Returns true if the caller now owns the surface, or false if someone else |
| 127 // owns the surface. |client| will be notified when the surface is available | 151 // owns the surface. |client| will be notified when the surface is available |
| 128 // via OnSurfaceAvailable(). | 152 // via OnSurfaceAvailable(). |
| 129 bool AllocateSurface(AVDACodecAllocatorClient* client, int surface_id); | 153 bool AllocateSurface(AVDASurfaceAllocatorClient* client, int surface_id); |
| 130 | 154 |
| 131 // Relinquish ownership of the surface or stop waiting for it to be available. | 155 // Relinquish ownership of the surface or stop waiting for it to be available. |
| 132 // The caller must guarantee that when calling this the surface is either no | 156 // The caller must guarantee that when calling this the surface is either no |
| 133 // longer attached to a MediaCodec, or the MediaCodec it was attached to is | 157 // longer attached to a MediaCodec, or the MediaCodec it was attached to is |
| 134 // was released with ReleaseMediaCodec(). | 158 // was released with ReleaseMediaCodec(). |
| 135 void DeallocateSurface(AVDACodecAllocatorClient* client, int surface_id); | 159 void DeallocateSurface(AVDASurfaceAllocatorClient* client, int surface_id); |
| 136 | 160 |
| 137 // Create and configure a MediaCodec synchronously. | 161 // Create and configure a MediaCodec synchronously. |
| 138 std::unique_ptr<VideoCodecBridge> CreateMediaCodecSync( | 162 std::unique_ptr<VideoCodecBridge> CreateMediaCodecSync( |
| 139 scoped_refptr<CodecConfig> codec_config); | 163 scoped_refptr<CodecConfig> codec_config); |
| 140 | 164 |
| 141 // Create and configure a MediaCodec asynchronously. The result is delivered | 165 // Create and configure a MediaCodec asynchronously. The result is delivered |
| 142 // via OnCodecConfigured(). | 166 // via OnCodecConfigured(). |
| 143 virtual void CreateMediaCodecAsync( | 167 virtual void CreateMediaCodecAsync( |
| 144 base::WeakPtr<AVDACodecAllocatorClient> client, | 168 base::WeakPtr<AVDACodecAllocatorClient> client, |
| 145 scoped_refptr<CodecConfig> codec_config); | 169 scoped_refptr<CodecConfig> codec_config); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 173 protected: | 197 protected: |
| 174 // |tick_clock| and |stop_event| are for tests only. | 198 // |tick_clock| and |stop_event| are for tests only. |
| 175 AVDACodecAllocator(base::TickClock* tick_clock = nullptr, | 199 AVDACodecAllocator(base::TickClock* tick_clock = nullptr, |
| 176 base::WaitableEvent* stop_event = nullptr); | 200 base::WaitableEvent* stop_event = nullptr); |
| 177 virtual ~AVDACodecAllocator(); | 201 virtual ~AVDACodecAllocator(); |
| 178 | 202 |
| 179 private: | 203 private: |
| 180 friend class AVDACodecAllocatorTest; | 204 friend class AVDACodecAllocatorTest; |
| 181 | 205 |
| 182 struct OwnerRecord { | 206 struct OwnerRecord { |
| 183 AVDACodecAllocatorClient* owner = nullptr; | 207 AVDASurfaceAllocatorClient* owner = nullptr; |
| 184 AVDACodecAllocatorClient* waiter = nullptr; | 208 AVDASurfaceAllocatorClient* waiter = nullptr; |
| 185 }; | 209 }; |
| 186 | 210 |
| 187 class HangDetector : public base::MessageLoop::TaskObserver { | 211 class HangDetector : public base::MessageLoop::TaskObserver { |
| 188 public: | 212 public: |
| 189 HangDetector(base::TickClock* tick_clock); | 213 HangDetector(base::TickClock* tick_clock); |
| 190 void WillProcessTask(const base::PendingTask& pending_task) override; | 214 void WillProcessTask(const base::PendingTask& pending_task) override; |
| 191 void DidProcessTask(const base::PendingTask& pending_task) override; | 215 void DidProcessTask(const base::PendingTask& pending_task) override; |
| 192 bool IsThreadLikelyHung(); | 216 bool IsThreadLikelyHung(); |
| 193 | 217 |
| 194 private: | 218 private: |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 | 260 |
| 237 // For canceling pending StopThreadTask()s. | 261 // For canceling pending StopThreadTask()s. |
| 238 base::WeakPtrFactory<AVDACodecAllocator> weak_this_factory_; | 262 base::WeakPtrFactory<AVDACodecAllocator> weak_this_factory_; |
| 239 | 263 |
| 240 DISALLOW_COPY_AND_ASSIGN(AVDACodecAllocator); | 264 DISALLOW_COPY_AND_ASSIGN(AVDACodecAllocator); |
| 241 }; | 265 }; |
| 242 | 266 |
| 243 } // namespace media | 267 } // namespace media |
| 244 | 268 |
| 245 #endif // MEDIA_GPU_AVDA_CODEC_ALLOCATOR_H_ | 269 #endif // MEDIA_GPU_AVDA_CODEC_ALLOCATOR_H_ |
| OLD | NEW |