| 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> | |
| 9 | |
| 10 #include <list> | |
| 11 #include <map> | |
| 12 #include <queue> | |
| 13 #include <vector> | |
| 14 | |
| 15 #include "base/compiler_specific.h" | |
| 16 #include "base/optional.h" | |
| 17 #include "base/threading/thread_checker.h" | 8 #include "base/threading/thread_checker.h" |
| 18 #include "base/timer/timer.h" | 9 #include "media/base/video_decoder.h" |
| 19 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" | |
| 20 #include "gpu/command_buffer/service/gpu_preferences.h" | |
| 21 #include "media/base/android/media_drm_bridge_cdm_context.h" | |
| 22 #include "media/base/android/sdk_media_codec_bridge.h" | |
| 23 #include "media/base/media_keys.h" | |
| 24 #include "media/gpu/avda_codec_allocator.h" | |
| 25 #include "media/gpu/avda_picture_buffer_manager.h" | |
| 26 #include "media/gpu/gpu_video_decode_accelerator_helpers.h" | |
| 27 #include "media/gpu/media_gpu_export.h" | 10 #include "media/gpu/media_gpu_export.h" |
| 28 #include "media/video/video_decode_accelerator.h" | |
| 29 #include "ui/gl/android/scoped_java_surface.h" | |
| 30 | 11 |
| 31 namespace media { | 12 namespace media { |
| 32 | 13 |
| 33 // An Android VideoDecoder that delegates to MediaCodec. | 14 // An Android VideoDecoder that delegates to MediaCodec. |
| 34 class MEDIA_GPU_EXPORT MediaCodecVideoDecoder | 15 class MEDIA_GPU_EXPORT MediaCodecVideoDecoder : public VideoDecoder { |
| 35 : public AVDACodecAllocatorClient { | |
| 36 public: | 16 public: |
| 37 MediaCodecVideoDecoder( | 17 MediaCodecVideoDecoder(); |
| 38 const MakeGLContextCurrentCallback& make_context_current_cb, | |
| 39 const GetGLES2DecoderCallback& get_gles2_decoder_cb); | |
| 40 | |
| 41 ~MediaCodecVideoDecoder() override; | 18 ~MediaCodecVideoDecoder() override; |
| 42 | 19 |
| 43 // VideoDecodeAccelerator implementation: | 20 // VideoDecoder implementation: |
| 44 bool Initialize(const Config& config, Client* client) override; | 21 std::string GetDisplayName() const override; |
| 45 void Decode(const BitstreamBuffer& bitstream_buffer) override; | 22 void Initialize(const VideoDecoderConfig& config, |
| 46 void Flush() override; | 23 bool low_delay, |
| 47 void Reset() override; | 24 CdmContext* cdm_context, |
| 48 void SetSurface(int32_t surface_id) override; | 25 const InitCB& init_cb, |
| 49 void Destroy() override; | 26 const OutputCB& output_cb) override; |
| 50 | 27 void Decode(const scoped_refptr<DecoderBuffer>& buffer, |
| 51 // AVDACodecAllocatorClient implementation: | 28 const DecodeCB& decode_cb) override; |
| 52 void OnSurfaceAvailable(bool success) override; | 29 void Reset(const base::Closure& closure) override; |
| 53 void OnSurfaceDestroyed() override; | 30 bool NeedsBitstreamConversion() const override; |
| 54 void OnCodecConfigured( | 31 bool CanReadWithoutStalling() const override; |
| 55 std::unique_ptr<VideoCodecBridge> media_codec) override; | 32 int GetMaxDecodeRequests() const override; |
| 56 | 33 |
| 57 private: | 34 private: |
| 58 friend class MCVDManager; | 35 DISALLOW_COPY_AND_ASSIGN(MediaCodecVideoDecoder); |
| 59 | |
| 60 enum State { | |
| 61 NO_ERROR, | |
| 62 ERROR, | |
| 63 // Set when we are asynchronously constructing the codec. Will transition | |
| 64 // to NO_ERROR or ERROR depending on success. | |
| 65 WAITING_FOR_CODEC, | |
| 66 // Set when we have a codec, but it doesn't yet have a key. | |
| 67 WAITING_FOR_KEY, | |
| 68 // The output surface was destroyed. We must not configure a new MediaCodec | |
| 69 // with the destroyed surface. | |
| 70 SURFACE_DESTROYED, | |
| 71 }; | |
| 72 | |
| 73 enum DrainType { | |
| 74 DRAIN_TYPE_NONE, | |
| 75 DRAIN_FOR_FLUSH, | |
| 76 DRAIN_FOR_RESET, | |
| 77 DRAIN_FOR_DESTROY, | |
| 78 }; | |
| 79 | |
| 80 // Initialize of the picture buffer manager. This is to be called when the | |
| 81 // SurfaceView in |surface_id_|, if any, is no longer busy. It will return | |
| 82 // false on failure, and true if initialization was successful. This includes | |
| 83 // synchronous and asynchronous init; the MCVD might not yet have a codec on | |
| 84 // success, but async init will at least be in progress. | |
| 85 bool InitializePictureBufferManager(); | |
| 86 | |
| 87 // A part of destruction process that is sometimes postponed after the drain. | |
| 88 void ActualDestroy(); | |
| 89 | |
| 90 // Configures |media_codec_| with the given codec parameters from the client. | |
| 91 // This configuration will (probably) not be complete before this call | |
| 92 // returns. Multiple calls before completion will be ignored. |state_| | |
| 93 // must be NO_ERROR or WAITING_FOR_CODEC. Note that, once you call this, | |
| 94 // you should be careful to avoid modifying members of |codec_config_| until | |
| 95 // |state_| is no longer WAITING_FOR_CODEC. | |
| 96 void ConfigureMediaCodecAsynchronously(); | |
| 97 | |
| 98 // Sends the decoded frame specified by |codec_buffer_index| to the client. | |
| 99 void SendDecodedFrameToClient(int32_t codec_buffer_index, | |
| 100 int32_t bitstream_id); | |
| 101 | |
| 102 // Does pending IO tasks if any. Once this is called, it polls |media_codec_| | |
| 103 // until it finishes pending tasks. For the polling, |kDecodePollDelay| is | |
| 104 // used. | |
| 105 void DoIOTask(bool start_timer); | |
| 106 | |
| 107 // Feeds input data to |media_codec_|. This checks | |
| 108 // |pending_bitstream_buffers_| and queues a buffer to |media_codec_|. | |
| 109 // Returns true if any input was processed. | |
| 110 bool QueueInput(); | |
| 111 | |
| 112 // Dequeues output from |media_codec_| and feeds the decoded frame to the | |
| 113 // client. Returns a hint about whether calling again might produce | |
| 114 // more output. | |
| 115 bool DequeueOutput(); | |
| 116 | |
| 117 // Decode the content in the |bitstream_buffer|. Note that a | |
| 118 // |bitstream_buffer| of id as -1 indicates a flush command. | |
| 119 void DecodeBuffer(const BitstreamBuffer& bitstream_buffer); | |
| 120 | |
| 121 // Called during Initialize() for encrypted streams to set up the CDM. | |
| 122 void InitializeCdm(); | |
| 123 | |
| 124 // Called after the CDM obtains a MediaCrypto object. | |
| 125 void OnMediaCryptoReady(MediaDrmBridgeCdmContext::JavaObjectPtr media_crypto, | |
| 126 bool needs_protected_surface); | |
| 127 | |
| 128 // Called when a new key is added to the CDM. | |
| 129 void OnKeyAdded(); | |
| 130 | |
| 131 // Notifies the client about the error and sets |state_| to |ERROR|. | |
| 132 void NotifyError(Error error); | |
| 133 | |
| 134 // Start or stop our work-polling timer based on whether we did any work, and | |
| 135 // how long it has been since we've done work. Calling this with true will | |
| 136 // start the timer. Calling it with false may stop the timer. | |
| 137 void ManageTimer(bool did_work); | |
| 138 | |
| 139 // Start the MediaCodec drain process by adding end_of_stream() buffer to the | |
| 140 // encoded buffers queue. When we receive EOS from the output buffer the drain | |
| 141 // process completes and we perform the action depending on the |drain_type|. | |
| 142 void StartCodecDrain(DrainType drain_type); | |
| 143 | |
| 144 // Returns true if we are currently draining the codec and doing that as part | |
| 145 // of Reset() or Destroy() VP8 workaround. (http://crbug.com/598963). We won't | |
| 146 // display any frames and disable normal errors handling. | |
| 147 bool IsDrainingForResetOrDestroy() const; | |
| 148 | |
| 149 // A helper method that performs the operation required after the drain | |
| 150 // completion (usually when we receive EOS in the output). The operation | |
| 151 // itself depends on the |drain_type_|. | |
| 152 void OnDrainCompleted(); | |
| 153 | |
| 154 // Resets MediaCodec and buffers/containers used for storing output. These | |
| 155 // components need to be reset upon EOS to decode a later stream. Input state | |
| 156 // (e.g. queued BitstreamBuffers) is not reset, as input following an EOS | |
| 157 // is still valid and should be processed. | |
| 158 void ResetCodecState(); | |
| 159 | |
| 160 // Indicates if MediaCodec should not be used for software decoding since we | |
| 161 // have safer versions elsewhere. | |
| 162 bool IsMediaCodecSoftwareDecodingForbidden() const; | |
| 163 | |
| 164 // On platforms which support seamless surface changes, this will reinitialize | |
| 165 // the picture buffer manager with the new surface. This function reads and | |
| 166 // clears the surface id from |pending_surface_id_|. It will issue a decode | |
| 167 // error if the surface change fails. Returns false on failure. | |
| 168 bool UpdateSurface(); | |
| 169 | |
| 170 // Used to DCHECK that we are called on the correct thread. | |
| 171 base::ThreadChecker thread_checker_; | |
| 172 | |
| 173 // Callback to set the correct gl context. | |
| 174 MakeGLContextCurrentCallback make_context_current_cb_; | |
| 175 | |
| 176 // Callback to get the GLES2Decoder instance. | |
| 177 GetGLES2DecoderCallback get_gles2_decoder_cb_; | |
| 178 | |
| 179 State state_; | |
| 180 | |
| 181 // The low-level decoder which Android SDK provides. | |
| 182 std::unique_ptr<VideoCodecBridge> media_codec_; | |
| 183 | |
| 184 // The resolution of the stream. | |
| 185 gfx::Size size_; | |
| 186 | |
| 187 // Encoded bitstream buffers to be passed to media codec, queued until an | |
| 188 // input buffer is available. | |
| 189 std::queue<BitstreamRecord> pending_bitstream_records_; | |
| 190 | |
| 191 // A map of presentation timestamp to bitstream buffer id for the bitstream | |
| 192 // buffers that have been submitted to the decoder but haven't yet produced an | |
| 193 // output frame with the same timestamp. Note: there will only be one entry | |
| 194 // for multiple bitstream buffers that have the same presentation timestamp. | |
| 195 std::map<base::TimeDelta, int32_t> bitstream_buffers_in_decoder_; | |
| 196 | |
| 197 // Keeps track of bitstream ids notified to the client with | |
| 198 // NotifyEndOfBitstreamBuffer() before getting output from the bitstream. | |
| 199 std::list<int32_t> bitstreams_notified_in_advance_; | |
| 200 | |
| 201 AVDAPictureBufferManager picture_buffer_manager_; | |
| 202 | |
| 203 // Time at which we last did useful work on io_timer_. | |
| 204 base::TimeTicks most_recent_work_; | |
| 205 | |
| 206 // Type of a drain request. We need to distinguish between DRAIN_FOR_FLUSH | |
| 207 // and other types, see IsDrainingForResetOrDestroy(). | |
| 208 DrainType drain_type_; | |
| 209 | |
| 210 // Holds a ref-count to the CDM to avoid using the CDM after it's destroyed. | |
| 211 scoped_refptr<MediaKeys> cdm_for_reference_holding_only_; | |
| 212 | |
| 213 MediaDrmBridgeCdmContext* media_drm_bridge_cdm_context_; | |
| 214 | |
| 215 // MediaDrmBridge requires registration/unregistration of the player, this | |
| 216 // registration id is used for this. | |
| 217 int cdm_registration_id_; | |
| 218 | |
| 219 // Configuration that we use for MediaCodec. | |
| 220 // Do not update any of its members while |state_| is WAITING_FOR_CODEC. | |
| 221 scoped_refptr<CodecConfig> codec_config_; | |
| 222 | |
| 223 // Index of the dequeued and filled buffer that we keep trying to enqueue. | |
| 224 // Such buffer appears in MEDIA_CODEC_NO_KEY processing. | |
| 225 int pending_input_buf_index_; | |
| 226 | |
| 227 // True if and only if VDA initialization is deferred, and we have not yet | |
| 228 // called NotifyInitializationComplete. | |
| 229 bool deferred_initialization_pending_; | |
| 230 | |
| 231 // Indicates if ResetCodecState() should be called upon the next call to | |
| 232 // Decode(). Allows us to avoid trashing the last few frames of a playback | |
| 233 // when the EOS buffer is received. | |
| 234 bool codec_needs_reset_; | |
| 235 | |
| 236 // True if surface creation and |picture_buffer_manager_| initialization has | |
| 237 // been defered until the first Decode() call. | |
| 238 bool defer_surface_creation_; | |
| 239 | |
| 240 // Has a value if a SetSurface() call has occurred and a new surface should be | |
| 241 // switched to when possible. Cleared during OnSurfaceDestroyed() and if all | |
| 242 // pictures have been rendered in DequeueOutput(). | |
| 243 base::Optional<int32_t> pending_surface_id_; | |
| 244 | |
| 245 // Copy of the VDA::Config we were given. | |
| 246 Config config_; | |
| 247 | |
| 248 // WeakPtrFactory for posting tasks back to |this|. | |
| 249 base::WeakPtrFactory<MediaCodecVideoDecoder> weak_this_factory_; | |
| 250 | |
| 251 friend class MediaCodecVideoDecoderTest; | |
| 252 }; | 36 }; |
| 253 | 37 |
| 254 } // namespace media | 38 } // namespace media |
| 255 | 39 |
| 256 #endif // MEDIA_GPU_ANDROID_MEDIA_CODEC_VIDEO_DECODER_H_ | 40 #endif // MEDIA_GPU_ANDROID_MEDIA_CODEC_VIDEO_DECODER_H_ |
| OLD | NEW |