Home | History | Annotate | Download | only in media
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CONTENT_COMMON_GPU_MEDIA_ANDROID_VIDEO_DECODE_ACCELERATOR_H_
      6 #define CONTENT_COMMON_GPU_MEDIA_ANDROID_VIDEO_DECODE_ACCELERATOR_H_
      7 
      8 #include <dlfcn.h>
      9 #include <list>
     10 #include <map>
     11 #include <queue>
     12 #include <string>
     13 #include <vector>
     14 
     15 #include "base/compiler_specific.h"
     16 #include "base/threading/thread_checker.h"
     17 #include "content/common/content_export.h"
     18 #include "gpu/command_buffer/service/gles2_cmd_copy_texture_chromium.h"
     19 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
     20 #include "media/base/android/media_codec_bridge.h"
     21 #include "media/video/video_decode_accelerator.h"
     22 
     23 namespace gfx {
     24 class SurfaceTextureBridge;
     25 }
     26 
     27 namespace content {
     28 // A VideoDecodeAccelerator implementation for Android.
     29 // This class decodes the input encoded stream by using Android's MediaCodec
     30 // class. http://developer.android.com/reference/android/media/MediaCodec.html
     31 class CONTENT_EXPORT AndroidVideoDecodeAccelerator :
     32     public media::VideoDecodeAccelerator {
     33  public:
     34   // Does not take ownership of |client| which must outlive |*this|.
     35   AndroidVideoDecodeAccelerator(
     36       media::VideoDecodeAccelerator::Client* client,
     37       const base::WeakPtr<gpu::gles2::GLES2Decoder> decoder,
     38       const base::Callback<bool(void)>& make_context_current);
     39 
     40   // media::VideoDecodeAccelerator implementation.
     41   virtual bool Initialize(media::VideoCodecProfile profile) OVERRIDE;
     42   virtual void Decode(const media::BitstreamBuffer& bitstream_buffer) OVERRIDE;
     43   virtual void AssignPictureBuffers(
     44       const std::vector<media::PictureBuffer>& buffers) OVERRIDE;
     45   virtual void ReusePictureBuffer(int32 picture_buffer_id) OVERRIDE;
     46   virtual void Flush() OVERRIDE;
     47   virtual void Reset() OVERRIDE;
     48   virtual void Destroy() OVERRIDE;
     49 
     50  private:
     51   enum State {
     52     NO_ERROR,
     53     ERROR,
     54   };
     55 
     56   static const base::TimeDelta kDecodePollDelay;
     57 
     58   virtual ~AndroidVideoDecodeAccelerator();
     59 
     60   // Configures |media_codec_| with the given codec parameters from the client.
     61   bool ConfigureMediaCodec();
     62 
     63   // Sends the current picture on the surface to the client.
     64   void SendCurrentSurfaceToClient(int32 bitstream_id);
     65 
     66   // Does pending IO tasks if any. Once this is called, it polls |media_codec_|
     67   // until it finishes pending tasks. For the polling, |kDecodePollDelay| is
     68   // used.
     69   void DoIOTask();
     70 
     71   // Feeds input data to |media_codec_|. This checks
     72   // |pending_bitstream_buffers_| and queues a buffer to |media_codec_|.
     73   void QueueInput();
     74 
     75   // Dequeues output from |media_codec_| and feeds the decoded frame to the
     76   // client.
     77   void DequeueOutput();
     78 
     79   // Notifies the client that initialize was completed.
     80   void NotifyInitializeDone();
     81 
     82   // Requests picture buffers from the client.
     83   void RequestPictureBuffers();
     84 
     85   // Notifies the client about the availability of a picture.
     86   void NotifyPictureReady(const media::Picture& picture);
     87 
     88   // Notifies the client that the input buffer identifed by input_buffer_id has
     89   // been processed.
     90   void NotifyEndOfBitstreamBuffer(int input_buffer_id);
     91 
     92   // Notifies the client that the decoder was flushed.
     93   void NotifyFlushDone();
     94 
     95   // Notifies the client that the decoder was reset.
     96   void NotifyResetDone();
     97 
     98   // Notifies about decoding errors.
     99   void NotifyError(media::VideoDecodeAccelerator::Error error);
    100 
    101   // Used to DCHECK that we are called on the correct thread.
    102   base::ThreadChecker thread_checker_;
    103 
    104   // To expose client callbacks from VideoDecodeAccelerator.
    105   Client* client_;
    106 
    107   // Callback to set the correct gl context.
    108   base::Callback<bool(void)> make_context_current_;
    109 
    110   // Codec type. Used when we configure media codec.
    111   media::VideoCodec codec_;
    112 
    113   // The current state of this class. For now, this is used only for setting
    114   // error state.
    115   State state_;
    116 
    117   // This map maintains the picture buffers passed to the client for decoding.
    118   // The key is the picture buffer id.
    119   typedef std::map<int32, media::PictureBuffer> OutputBufferMap;
    120   OutputBufferMap output_picture_buffers_;
    121 
    122   // This keeps the free picture buffer ids which can be used for sending
    123   // decoded frames to the client.
    124   std::queue<int32> free_picture_ids_;
    125 
    126   // The low-level decoder which Android SDK provides.
    127   scoped_ptr<media::VideoCodecBridge> media_codec_;
    128 
    129   // A container of texture. Used to set a texture to |media_codec_|.
    130   scoped_refptr<gfx::SurfaceTextureBridge> surface_texture_;
    131 
    132   // The texture id which is set to |surface_texture_|.
    133   uint32 surface_texture_id_;
    134 
    135   // Set to true after requesting picture buffers to the client.
    136   bool picturebuffers_requested_;
    137 
    138   // Set to true when DoIOTask is in the message loop.
    139   bool io_task_is_posted_;
    140 
    141   // Set to true when decoder outputs EOS (end of stream).
    142   bool decoder_met_eos_;
    143 
    144   // The resolution of the stream.
    145   gfx::Size size_;
    146 
    147   // Encoded bitstream buffers to be passed to media codec, queued until a input
    148   // buffer is available.
    149   typedef std::queue<media::BitstreamBuffer> BitstreamBufferList;
    150   BitstreamBufferList pending_bitstream_buffers_;
    151 
    152   // Indicates the number of bytes already passed to the decoder in the first
    153   // buffer in |pending_bitstream_buffers_|.
    154   size_t num_bytes_used_in_the_pending_buffer_;
    155 
    156   // Keeps track of bitstream ids notified to the client with
    157   // NotifyEndOfBitstreamBuffer() before getting output from the bitstream.
    158   std::list<int32> bitstreams_notified_in_advance_;
    159 
    160   // Owner of the GL context. Used to restore the context state.
    161   base::WeakPtr<gpu::gles2::GLES2Decoder> gl_decoder_;
    162 
    163   // Used for copy the texture from |surface_texture_| to picture buffers.
    164   scoped_ptr<gpu::CopyTextureCHROMIUMResourceManager> copier_;
    165 
    166   friend class AndroidVideoDecodeAcceleratorTest;
    167 };
    168 
    169 }  // namespace content
    170 
    171 #endif  // CONTENT_COMMON_GPU_MEDIA_ANDROID_VIDEO_DECODE_ACCELERATOR_H_
    172