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