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