Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Side by Side Diff: media/gpu/android_video_decode_accelerator.h

Issue 2296513003: Delete AVDACopyingBackingStrategy and rename AVDADeferredRenderingBackingStrategy (Closed)
Patch Set: Undelete & fix unittest Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <memory>
13 #include <queue> 12 #include <queue>
14 #include <string>
15 #include <vector> 13 #include <vector>
16 14
17 #include "base/compiler_specific.h" 15 #include "base/compiler_specific.h"
18 #include "base/threading/thread_checker.h" 16 #include "base/threading/thread_checker.h"
19 #include "base/timer/timer.h" 17 #include "base/timer/timer.h"
20 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" 18 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
21 #include "gpu/command_buffer/service/gpu_preferences.h" 19 #include "gpu/command_buffer/service/gpu_preferences.h"
22 #include "media/base/android/media_drm_bridge_cdm_context.h" 20 #include "media/base/android/media_drm_bridge_cdm_context.h"
23 #include "media/base/android/sdk_media_codec_bridge.h" 21 #include "media/base/android/sdk_media_codec_bridge.h"
24 #include "media/base/media_keys.h" 22 #include "media/base/media_keys.h"
23 #include "media/gpu/avda_picture_buffer_manager.h"
25 #include "media/gpu/avda_state_provider.h" 24 #include "media/gpu/avda_state_provider.h"
26 #include "media/gpu/avda_surface_tracker.h" 25 #include "media/gpu/avda_surface_tracker.h"
27 #include "media/gpu/gpu_video_decode_accelerator_helpers.h" 26 #include "media/gpu/gpu_video_decode_accelerator_helpers.h"
28 #include "media/gpu/media_gpu_export.h" 27 #include "media/gpu/media_gpu_export.h"
29 #include "media/video/video_decode_accelerator.h" 28 #include "media/video/video_decode_accelerator.h"
30 #include "ui/gl/android/scoped_java_surface.h" 29 #include "ui/gl/android/scoped_java_surface.h"
31 30
32 namespace gl { 31 namespace gl {
33 class SurfaceTexture; 32 class SurfaceTexture;
34 } 33 }
35 34
36 namespace media { 35 namespace media {
37
38 class SharedMemoryRegion; 36 class SharedMemoryRegion;
39 37
40 // A VideoDecodeAccelerator implementation for Android. 38 // A VideoDecodeAccelerator implementation for Android. This class decodes the
41 // This class decodes the input encoded stream by using Android's MediaCodec 39 // encded input stream using Android's MediaCodec. It handles the work of
42 // class. http://developer.android.com/reference/android/media/MediaCodec.html 40 // transferring data to and from MediaCodec, and delegates attaching MediaCodec
43 // It delegates attaching pictures to PictureBuffers to a BackingStrategy, but 41 // output buffers to PictureBuffers to AVDAPictureBufferManager.
44 // otherwise handles the work of transferring data to / from MediaCodec.
45 class MEDIA_GPU_EXPORT AndroidVideoDecodeAccelerator 42 class MEDIA_GPU_EXPORT AndroidVideoDecodeAccelerator
46 : public VideoDecodeAccelerator, 43 : public VideoDecodeAccelerator,
47 public AVDAStateProvider { 44 public AVDAStateProvider {
48 public: 45 public:
49 using OutputBufferMap = std::map<int32_t, PictureBuffer>;
50
51 // A BackingStrategy is responsible for making a PictureBuffer's texture
52 // contain the image that a MediaCodec decoder buffer tells it to.
53 class BackingStrategy {
54 public:
55 virtual ~BackingStrategy() {}
56
57 // Must be called before anything else. If surface_view_id is not equal to
58 // |kNoSurfaceID| it refers to a SurfaceView that the strategy must render
59 // to.
60 // Returns the Java surface to configure MediaCodec with.
61 virtual gl::ScopedJavaSurface Initialize(int surface_view_id) = 0;
62
63 // Called before the AVDA does any Destroy() work. The strategy should
64 // release any pending codec buffers, for example.
65 virtual void BeginCleanup(bool have_context,
66 const OutputBufferMap& buffer_map) = 0;
67
68 // Called before the AVDA closes up entirely. This will be
69 // the last call that the BackingStrategy receives.
70 virtual void EndCleanup() = 0;
71
72 // This returns the SurfaceTexture created by Initialize, or nullptr if
73 // the strategy was initialized with a SurfaceView.
74 virtual scoped_refptr<gl::SurfaceTexture> GetSurfaceTexture() const = 0;
75
76 // Return the GL texture target that the PictureBuffer textures use.
77 virtual uint32_t GetTextureTarget() const = 0;
78
79 // Return the size to use when requesting picture buffers.
80 virtual gfx::Size GetPictureBufferSize() const = 0;
81
82 // Make the provided PictureBuffer draw the image that is represented by
83 // the decoded output buffer at codec_buffer_index.
84 virtual void UseCodecBufferForPictureBuffer(
85 int32_t codec_buffer_index,
86 const PictureBuffer& picture_buffer) = 0;
87
88 // Notify strategy that a picture buffer has been assigned.
89 virtual void AssignOnePictureBuffer(const PictureBuffer& picture_buffer,
90 bool have_context) {}
91
92 // Notify strategy that a picture buffer has been reused.
93 virtual void ReuseOnePictureBuffer(const PictureBuffer& picture_buffer) {}
94
95 // Release MediaCodec buffers.
96 virtual void ReleaseCodecBuffers(
97 const AndroidVideoDecodeAccelerator::OutputBufferMap& buffers) {}
98
99 // Attempts to free up codec output buffers by rendering early.
100 virtual void MaybeRenderEarly() {}
101
102 // Notify strategy that we have a new android MediaCodec instance. This
103 // happens when we're starting up or re-configuring mid-stream. Any
104 // previously provided codec should no longer be referenced.
105 virtual void CodecChanged(VideoCodecBridge* codec) = 0;
106
107 // Notify the strategy that a frame is available. This callback can happen
108 // on any thread at any time.
109 virtual void OnFrameAvailable() = 0;
110
111 // Whether the pictures produced by this backing strategy are overlayable.
112 virtual bool ArePicturesOverlayable() = 0;
113
114 // Size may have changed due to resolution change since the last time this
115 // PictureBuffer was used. Update the size of the picture buffer to
116 // |new_size| and also update any size-dependent state (e.g. size of
117 // associated texture). Callers should set the correct GL context prior to
118 // calling.
119 virtual void UpdatePictureBufferSize(PictureBuffer* picture_buffer,
120 const gfx::Size& new_size) = 0;
121 };
122
123 AndroidVideoDecodeAccelerator( 46 AndroidVideoDecodeAccelerator(
124 const MakeGLContextCurrentCallback& make_context_current_cb, 47 const MakeGLContextCurrentCallback& make_context_current_cb,
125 const GetGLES2DecoderCallback& get_gles2_decoder_cb); 48 const GetGLES2DecoderCallback& get_gles2_decoder_cb);
126 49
127 ~AndroidVideoDecodeAccelerator() override; 50 ~AndroidVideoDecodeAccelerator() override;
128 51
129 // VideoDecodeAccelerator implementation: 52 // VideoDecodeAccelerator implementation:
130 bool Initialize(const Config& config, Client* client) override; 53 bool Initialize(const Config& config, Client* client) override;
131 void Decode(const BitstreamBuffer& bitstream_buffer) override; 54 void Decode(const BitstreamBuffer& bitstream_buffer) override;
132 void AssignPictureBuffers(const std::vector<PictureBuffer>& buffers) override; 55 void AssignPictureBuffers(const std::vector<PictureBuffer>& buffers) override;
133 void ReusePictureBuffer(int32_t picture_buffer_id) override; 56 void ReusePictureBuffer(int32_t picture_buffer_id) override;
134 void Flush() override; 57 void Flush() override;
135 void Reset() override; 58 void Reset() override;
136 void Destroy() override; 59 void Destroy() override;
137 bool TryToSetupDecodeOnSeparateThread( 60 bool TryToSetupDecodeOnSeparateThread(
138 const base::WeakPtr<Client>& decode_client, 61 const base::WeakPtr<Client>& decode_client,
139 const scoped_refptr<base::SingleThreadTaskRunner>& decode_task_runner) 62 const scoped_refptr<base::SingleThreadTaskRunner>& decode_task_runner)
140 override; 63 override;
141 64
142 // AVDAStateProvider implementation: 65 // AVDAStateProvider implementation:
143 const gfx::Size& GetSize() const override; 66 const gfx::Size& GetSize() const override;
144 const base::ThreadChecker& ThreadChecker() const override;
145 base::WeakPtr<gpu::gles2::GLES2Decoder> GetGlDecoder() const override; 67 base::WeakPtr<gpu::gles2::GLES2Decoder> GetGlDecoder() const override;
146 gpu::gles2::TextureRef* GetTextureForPicture(
147 const PictureBuffer& picture_buffer) override;
148 scoped_refptr<gl::SurfaceTexture> CreateAttachedSurfaceTexture(
149 GLuint* service_id) override;
150 void PostError(const ::tracked_objects::Location& from_here, 68 void PostError(const ::tracked_objects::Location& from_here,
151 VideoDecodeAccelerator::Error error) override; 69 VideoDecodeAccelerator::Error error) override;
152 70
153 static VideoDecodeAccelerator::Capabilities GetCapabilities( 71 static VideoDecodeAccelerator::Capabilities GetCapabilities(
154 const gpu::GpuPreferences& gpu_preferences); 72 const gpu::GpuPreferences& gpu_preferences);
155 73
156 // Notifies about SurfaceTexture::OnFrameAvailable. This can happen on any
157 // thread at any time!
158 void OnFrameAvailable();
159
160 private: 74 private:
161 friend class AVDAManager; 75 friend class AVDAManager;
162 76
163 // TODO(timav): evaluate the need for more states in the AVDA state machine. 77 // TODO(timav): evaluate the need for more states in the AVDA state machine.
164 enum State { 78 enum State {
165 NO_ERROR, 79 NO_ERROR,
166 ERROR, 80 ERROR,
167 // Set when we are asynchronously constructing the codec. Will transition 81 // Set when we are asynchronously constructing the codec. Will transition
168 // to NO_ERROR or ERROR depending on success. 82 // to NO_ERROR or ERROR depending on success.
169 WAITING_FOR_CODEC, 83 WAITING_FOR_CODEC,
(...skipping 18 matching lines...) Expand all
188 class CodecConfig : public base::RefCountedThreadSafe<CodecConfig> { 102 class CodecConfig : public base::RefCountedThreadSafe<CodecConfig> {
189 public: 103 public:
190 CodecConfig(); 104 CodecConfig();
191 105
192 // Codec type. Used when we configure media codec. 106 // Codec type. Used when we configure media codec.
193 VideoCodec codec_ = kUnknownVideoCodec; 107 VideoCodec codec_ = kUnknownVideoCodec;
194 108
195 // Whether encryption scheme requires to use protected surface. 109 // Whether encryption scheme requires to use protected surface.
196 bool needs_protected_surface_ = false; 110 bool needs_protected_surface_ = false;
197 111
198 // The surface that MediaCodec is configured to output to. It's created by 112 // The surface that MediaCodec is configured to output to.
199 // the backing strategy.
200 gl::ScopedJavaSurface surface_; 113 gl::ScopedJavaSurface surface_;
201 114
202 // The MediaCrypto object is used in the MediaCodec.configure() in case of 115 // The MediaCrypto object is used in the MediaCodec.configure() in case of
203 // an encrypted stream. 116 // an encrypted stream.
204 MediaDrmBridgeCdmContext::JavaObjectPtr media_crypto_; 117 MediaDrmBridgeCdmContext::JavaObjectPtr media_crypto_;
205 118
206 // Initial coded size. The actual size might change at any time, so this 119 // Initial coded size. The actual size might change at any time, so this
207 // is only a hint. 120 // is only a hint.
208 gfx::Size initial_expected_coded_size_; 121 gfx::Size initial_expected_coded_size_;
209 122
210 // Should we allow MediaCodec to autodetect the codec type (true), or 123 // Should we allow MediaCodec to autodetect the codec type (true), or
211 // select a software decoder manually (false). This is because fallback to 124 // select a software decoder manually (false). This is because fallback to
212 // software when autodetecting can sometimes hang mediaserver. 125 // software when autodetecting can sometimes hang mediaserver.
213 bool allow_autodetection_ = false; 126 bool allow_autodetection_ = false;
214 127
215 protected: 128 protected:
216 friend class base::RefCountedThreadSafe<CodecConfig>; 129 friend class base::RefCountedThreadSafe<CodecConfig>;
217 virtual ~CodecConfig(); 130 virtual ~CodecConfig();
218 131
219 private: 132 private:
220 DISALLOW_COPY_AND_ASSIGN(CodecConfig); 133 DISALLOW_COPY_AND_ASSIGN(CodecConfig);
221 }; 134 };
222 135
223 // Callback that is called when the SurfaceView becomes available, if it's 136 // Callback that is called when the SurfaceView becomes available, if it's
224 // not during Initialize. |success| is true if it is now available, false 137 // not during Initialize. |success| is true if it is now available, false
225 // if initialization should stop. 138 // if initialization should stop.
226 void OnSurfaceAvailable(bool success); 139 void OnSurfaceAvailable(bool success);
227 140
228 // Finish initialization of the strategy. This is to be called when the 141 // Initialize of the picture buffer manager. This is to be called when the
229 // SurfaceView in |surface_id_|, if any, is no longer busy. It will return 142 // SurfaceView in |surface_id_|, if any, is no longer busy. It will return
230 // false on failure, and true if initialization was successful. This includes 143 // false on failure, and true if initialization was successful. This includes
231 // synchronous and asynchronous init; the AVDA might not yet have a codec on 144 // synchronous and asynchronous init; the AVDA might not yet have a codec on
232 // success, but async init will at least be in progress. 145 // success, but async init will at least be in progress.
233 bool InitializeStrategy(); 146 bool InitializePictureBufferManager();
234 147
235 // A part of destruction process that is sometimes postponed after the drain. 148 // A part of destruction process that is sometimes postponed after the drain.
236 void ActualDestroy(); 149 void ActualDestroy();
237 150
238 // Configures |media_codec_| with the given codec parameters from the client. 151 // Configures |media_codec_| with the given codec parameters from the client.
239 // This configuration will (probably) not be complete before this call 152 // This configuration will (probably) not be complete before this call
240 // returns. Multiple calls before completion will be ignored. |state_| 153 // returns. Multiple calls before completion will be ignored. |state_|
241 // must be NO_ERROR or WAITING_FOR_CODEC. Note that, once you call this, 154 // must be NO_ERROR or WAITING_FOR_CODEC. Note that, once you call this,
242 // you should be careful to avoid modifying members of |codec_config_| until 155 // you should be careful to avoid modifying members of |codec_config_| until
243 // |state_| is no longer WAITING_FOR_CODEC. 156 // |state_| is no longer WAITING_FOR_CODEC.
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 // components need to be reset upon EOS to decode a later stream. Input state 261 // components need to be reset upon EOS to decode a later stream. Input state
349 // (e.g. queued BitstreamBuffers) is not reset, as input following an EOS 262 // (e.g. queued BitstreamBuffers) is not reset, as input following an EOS
350 // is still valid and should be processed. 263 // is still valid and should be processed.
351 void ResetCodecState(); 264 void ResetCodecState();
352 265
353 // Registered to be called when surfaces are being destroyed. If |surface_id| 266 // Registered to be called when surfaces are being destroyed. If |surface_id|
354 // is our surface, we should release the MediaCodec before returning from 267 // is our surface, we should release the MediaCodec before returning from
355 // this. 268 // this.
356 void OnDestroyingSurface(int surface_id); 269 void OnDestroyingSurface(int surface_id);
357 270
358 // Returns true if and only if we should use deferred rendering.
359 static bool UseDeferredRenderingStrategy(
360 const gpu::GpuPreferences& gpu_preferences);
361
362 // Indicates if MediaCodec should not be used for software decoding since we 271 // Indicates if MediaCodec should not be used for software decoding since we
363 // have safer versions elsewhere. 272 // have safer versions elsewhere.
364 bool IsMediaCodecSoftwareDecodingForbidden() const; 273 bool IsMediaCodecSoftwareDecodingForbidden() const;
365 274
366 // Used to DCHECK that we are called on the correct thread. 275 // Used to DCHECK that we are called on the correct thread.
367 base::ThreadChecker thread_checker_; 276 base::ThreadChecker thread_checker_;
368 277
369 // To expose client callbacks from VideoDecodeAccelerator. 278 // To expose client callbacks from VideoDecodeAccelerator.
370 Client* client_; 279 Client* client_;
371 280
372 // Callback to set the correct gl context. 281 // Callback to set the correct gl context.
373 MakeGLContextCurrentCallback make_context_current_cb_; 282 MakeGLContextCurrentCallback make_context_current_cb_;
374 283
375 // Callback to get the GLES2Decoder instance. 284 // Callback to get the GLES2Decoder instance.
376 GetGLES2DecoderCallback get_gles2_decoder_cb_; 285 GetGLES2DecoderCallback get_gles2_decoder_cb_;
377 286
378 // The current state of this class. For now, this is used only for setting 287 // The current state of this class. For now, this is used only for setting
379 // error state. 288 // error state.
380 State state_; 289 State state_;
381 290
382 // This map maintains the picture buffers passed to the client for decoding. 291 // The assigned picture buffers by picture buffer id.
383 // The key is the picture buffer id. 292 AVDAPictureBufferManager::PictureBufferMap output_picture_buffers_;
384 OutputBufferMap output_picture_buffers_;
385 293
386 // This keeps the free picture buffer ids which can be used for sending 294 // This keeps the free picture buffer ids which can be used for sending
387 // decoded frames to the client. 295 // decoded frames to the client.
388 std::queue<int32_t> free_picture_ids_; 296 std::queue<int32_t> free_picture_ids_;
389 297
390 // The low-level decoder which Android SDK provides. 298 // The low-level decoder which Android SDK provides.
391 std::unique_ptr<VideoCodecBridge> media_codec_; 299 std::unique_ptr<VideoCodecBridge> media_codec_;
392 300
393 // Set to true after requesting picture buffers to the client. 301 // Set to true after requesting picture buffers to the client.
394 bool picturebuffers_requested_; 302 bool picturebuffers_requested_;
(...skipping 22 matching lines...) Expand all
417 // A map of presentation timestamp to bitstream buffer id for the bitstream 325 // A map of presentation timestamp to bitstream buffer id for the bitstream
418 // buffers that have been submitted to the decoder but haven't yet produced an 326 // buffers that have been submitted to the decoder but haven't yet produced an
419 // output frame with the same timestamp. Note: there will only be one entry 327 // output frame with the same timestamp. Note: there will only be one entry
420 // for multiple bitstream buffers that have the same presentation timestamp. 328 // for multiple bitstream buffers that have the same presentation timestamp.
421 std::map<base::TimeDelta, int32_t> bitstream_buffers_in_decoder_; 329 std::map<base::TimeDelta, int32_t> bitstream_buffers_in_decoder_;
422 330
423 // Keeps track of bitstream ids notified to the client with 331 // Keeps track of bitstream ids notified to the client with
424 // NotifyEndOfBitstreamBuffer() before getting output from the bitstream. 332 // NotifyEndOfBitstreamBuffer() before getting output from the bitstream.
425 std::list<int32_t> bitstreams_notified_in_advance_; 333 std::list<int32_t> bitstreams_notified_in_advance_;
426 334
427 // Backing strategy that we'll use to connect PictureBuffers to frames. 335 AVDAPictureBufferManager picture_buffer_manager_;
428 std::unique_ptr<BackingStrategy> strategy_;
429
430 // Helper class that manages asynchronous OnFrameAvailable callbacks.
431 class OnFrameAvailableHandler;
432 scoped_refptr<OnFrameAvailableHandler> on_frame_available_handler_;
433 336
434 // Time at which we last did useful work on io_timer_. 337 // Time at which we last did useful work on io_timer_.
435 base::TimeTicks most_recent_work_; 338 base::TimeTicks most_recent_work_;
436 339
437 // Type of a drain request. We need to distinguish between DRAIN_FOR_FLUSH 340 // Type of a drain request. We need to distinguish between DRAIN_FOR_FLUSH
438 // and other types, see IsDrainingForResetOrDestroy(). 341 // and other types, see IsDrainingForResetOrDestroy().
439 DrainType drain_type_; 342 DrainType drain_type_;
440 343
441 // Holds a ref-count to the CDM to avoid using the CDM after it's destroyed. 344 // Holds a ref-count to the CDM to avoid using the CDM after it's destroyed.
442 scoped_refptr<MediaKeys> cdm_for_reference_holding_only_; 345 scoped_refptr<MediaKeys> cdm_for_reference_holding_only_;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 381
479 // WeakPtrFactory for posting tasks back to |this|. 382 // WeakPtrFactory for posting tasks back to |this|.
480 base::WeakPtrFactory<AndroidVideoDecodeAccelerator> weak_this_factory_; 383 base::WeakPtrFactory<AndroidVideoDecodeAccelerator> weak_this_factory_;
481 384
482 friend class AndroidVideoDecodeAcceleratorTest; 385 friend class AndroidVideoDecodeAcceleratorTest;
483 }; 386 };
484 387
485 } // namespace media 388 } // namespace media
486 389
487 #endif // MEDIA_GPU_ANDROID_VIDEO_DECODE_ACCELERATOR_H_ 390 #endif // MEDIA_GPU_ANDROID_VIDEO_DECODE_ACCELERATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698