Home | History | Annotate | Download | only in proxy
      1 // Copyright (c) 2014 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 PPAPI_PROXY_VIDEO_DECODER_RESOURCE_H_
      6 #define PPAPI_PROXY_VIDEO_DECODER_RESOURCE_H_
      7 
      8 #include <queue>
      9 
     10 #include "base/containers/hash_tables.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/scoped_vector.h"
     14 #include "ppapi/proxy/connection.h"
     15 #include "ppapi/proxy/plugin_resource.h"
     16 #include "ppapi/proxy/ppapi_proxy_export.h"
     17 #include "ppapi/shared_impl/resource.h"
     18 #include "ppapi/shared_impl/scoped_pp_resource.h"
     19 #include "ppapi/thunk/ppb_video_decoder_api.h"
     20 
     21 namespace gpu {
     22 struct Mailbox;
     23 namespace gles2 {
     24 class GLES2Implementation;
     25 }
     26 }
     27 
     28 namespace ppapi {
     29 
     30 class PPB_Graphics3D_Shared;
     31 class TrackedCallback;
     32 
     33 namespace proxy {
     34 
     35 class PPAPI_PROXY_EXPORT VideoDecoderResource
     36     : public PluginResource,
     37       public thunk::PPB_VideoDecoder_API {
     38  public:
     39   VideoDecoderResource(Connection connection, PP_Instance instance);
     40   virtual ~VideoDecoderResource();
     41 
     42   // Resource overrides.
     43   virtual thunk::PPB_VideoDecoder_API* AsPPB_VideoDecoder_API() OVERRIDE;
     44 
     45   // PPB_VideoDecoder_API implementation.
     46   virtual int32_t Initialize0_1(
     47       PP_Resource graphics_context,
     48       PP_VideoProfile profile,
     49       PP_Bool allow_software_fallback,
     50       scoped_refptr<TrackedCallback> callback) OVERRIDE;
     51   virtual int32_t Initialize(PP_Resource graphics_context,
     52                              PP_VideoProfile profile,
     53                              PP_HardwareAcceleration acceleration,
     54                              scoped_refptr<TrackedCallback> callback) OVERRIDE;
     55   virtual int32_t Decode(uint32_t decode_id,
     56                          uint32_t size,
     57                          const void* buffer,
     58                          scoped_refptr<TrackedCallback> callback) OVERRIDE;
     59   virtual int32_t GetPicture(PP_VideoPicture* picture,
     60                              scoped_refptr<TrackedCallback> callback) OVERRIDE;
     61   virtual void RecyclePicture(const PP_VideoPicture* picture) OVERRIDE;
     62   virtual int32_t Flush(scoped_refptr<TrackedCallback> callback) OVERRIDE;
     63   virtual int32_t Reset(scoped_refptr<TrackedCallback> callback) OVERRIDE;
     64 
     65   // PluginResource implementation.
     66   virtual void OnReplyReceived(const ResourceMessageReplyParams& params,
     67                                const IPC::Message& msg) OVERRIDE;
     68 
     69   // Called only by unit tests. This bypasses Graphics3D setup, which doesn't
     70   // work in ppapi::proxy::PluginProxyTest.
     71   void SetForTest();
     72 
     73  private:
     74   // Struct to hold a shared memory buffer.
     75   struct ShmBuffer {
     76     ShmBuffer(scoped_ptr<base::SharedMemory> shm,
     77               uint32_t size,
     78               uint32_t shm_id);
     79     ~ShmBuffer();
     80 
     81     const scoped_ptr<base::SharedMemory> shm;
     82     void* addr;
     83     // Index into shm_buffers_ vector, used as an id. This should map 1:1 to
     84     // the index on the host side of the proxy.
     85     const uint32_t shm_id;
     86   };
     87 
     88   // Struct to hold texture information.
     89   struct Texture {
     90     Texture(uint32_t texture_target, const PP_Size& size);
     91     ~Texture();
     92 
     93     const uint32_t texture_target;
     94     const PP_Size size;
     95   };
     96 
     97   // Struct to hold a picture received from the decoder.
     98   struct Picture {
     99     Picture(int32_t decode_id, uint32_t texture_id);
    100     ~Picture();
    101 
    102     int32_t decode_id;
    103     uint32_t texture_id;
    104   };
    105 
    106   int32_t InitializeInternal(PP_Resource graphics_context,
    107                              PP_VideoProfile profile,
    108                              PP_Bool allow_software_fallback,
    109                              scoped_refptr<TrackedCallback> callback,
    110                              bool testing);
    111 
    112   // Unsolicited reply message handlers.
    113   void OnPluginMsgRequestTextures(const ResourceMessageReplyParams& params,
    114                                   uint32_t num_textures,
    115                                   const PP_Size& size,
    116                                   uint32_t texture_target,
    117                                   const std::vector<gpu::Mailbox>& mailboxes);
    118   void OnPluginMsgPictureReady(const ResourceMessageReplyParams& params,
    119                                int32_t decode_id,
    120                                uint32_t texture_id);
    121   void OnPluginMsgDismissPicture(const ResourceMessageReplyParams& params,
    122                                  uint32_t texture_id);
    123   void OnPluginMsgNotifyError(const ResourceMessageReplyParams& params,
    124                               int32_t error);
    125 
    126   // Reply message handlers for operations that are done in the host.
    127   void OnPluginMsgInitializeComplete(const ResourceMessageReplyParams& params);
    128   void OnPluginMsgDecodeComplete(const ResourceMessageReplyParams& params,
    129                                  uint32_t shm_id);
    130   void OnPluginMsgFlushComplete(const ResourceMessageReplyParams& params);
    131   void OnPluginMsgResetComplete(const ResourceMessageReplyParams& params);
    132 
    133   void RunCallbackWithError(scoped_refptr<TrackedCallback>* callback);
    134   void DeleteGLTexture(uint32_t texture_id);
    135   void WriteNextPicture(PP_VideoPicture* picture);
    136 
    137   // ScopedVector to own the shared memory buffers.
    138   ScopedVector<ShmBuffer> shm_buffers_;
    139 
    140   // List of available shared memory buffers.
    141   typedef std::vector<ShmBuffer*> ShmBufferList;
    142   ShmBufferList available_shm_buffers_;
    143 
    144   // Map of GL texture id to texture info.
    145   typedef base::hash_map<uint32_t, Texture> TextureMap;
    146   TextureMap textures_;
    147 
    148   // Queue of received pictures.
    149   typedef std::queue<Picture> PictureQueue;
    150   PictureQueue received_pictures_;
    151 
    152   // Pending callbacks.
    153   scoped_refptr<TrackedCallback> initialize_callback_;
    154   scoped_refptr<TrackedCallback> decode_callback_;
    155   scoped_refptr<TrackedCallback> get_picture_callback_;
    156   scoped_refptr<TrackedCallback> flush_callback_;
    157   scoped_refptr<TrackedCallback> reset_callback_;
    158 
    159   // Number of Decode calls made, mod 2^31, to serve as a uid for each decode.
    160   int32_t num_decodes_;
    161   // The maximum delay (in Decode calls) before we receive a picture. If we
    162   // haven't received a picture from a Decode call after this many successive
    163   // calls to Decode, then we will never receive a picture from the call.
    164   // Note that this isn't guaranteed by H264 or other codecs. In practice, this
    165   // number is less than 16. Make it much larger just to be safe.
    166   // NOTE: because we count decodes mod 2^31, this value must be a power of 2.
    167   static const int kMaximumPictureDelay = 128;
    168   uint32_t decode_ids_[kMaximumPictureDelay];
    169 
    170   // State for pending get_picture_callback_.
    171   PP_VideoPicture* get_picture_;
    172 
    173   ScopedPPResource graphics3d_;
    174   gpu::gles2::GLES2Implementation* gles2_impl_;
    175 
    176   bool initialized_;
    177   bool testing_;
    178   int32_t decoder_last_error_;
    179 
    180   DISALLOW_COPY_AND_ASSIGN(VideoDecoderResource);
    181 };
    182 
    183 }  // namespace proxy
    184 }  // namespace ppapi
    185 
    186 #endif  // PPAPI_PROXY_VIDEO_DECODER_RESOURCE_H_
    187