Pepper_47_C++_interfaces
video_decoder.cc
Go to the documentation of this file.
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 
6 
7 #include "ppapi/c/pp_errors.h"
8 #include "ppapi/c/ppb_video_decoder.h"
11 #include "ppapi/cpp/module.h"
12 #include "ppapi/cpp/module_impl.h"
13 
14 namespace pp {
15 
16 namespace {
17 
18 template <>
19 const char* interface_name<PPB_VideoDecoder_0_1>() {
20  return PPB_VIDEODECODER_INTERFACE_0_1;
21 }
22 
23 template <>
24 const char* interface_name<PPB_VideoDecoder_0_2>() {
25  return PPB_VIDEODECODER_INTERFACE_0_2;
26 }
27 
28 template <>
29 const char* interface_name<PPB_VideoDecoder_1_0>() {
30  return PPB_VIDEODECODER_INTERFACE_1_0;
31 }
32 
33 template <>
34 const char* interface_name<PPB_VideoDecoder_1_1>() {
35  return PPB_VIDEODECODER_INTERFACE_1_1;
36 }
37 
38 // This struct is used to adapt CompletionCallbackWithOutput<PP_VideoPicture> to
39 // the pre-1.0 APIs, which return PP_VideoPicture_0_1. This struct is allocated
40 // on the heap, and deleted in CallbackConverter.
41 struct CallbackData_0_1 {
42  explicit CallbackData_0_1(
43  const CompletionCallbackWithOutput<PP_VideoPicture>& cc)
44  : original_picture(cc.output()),
45  original_callback(cc.pp_completion_callback()) {}
46  PP_VideoPicture_0_1 picture;
47  PP_VideoPicture* original_picture;
48  PP_CompletionCallback original_callback;
49 };
50 
51 // Convert a 1.0 style callback to pre-1.0 callback.
52 void CallbackConverter(void* user_data, int32_t result) {
53  CallbackData_0_1* data = static_cast<CallbackData_0_1*>(user_data);
54  if (result == PP_OK) {
55  PP_VideoPicture_0_1* picture = &data->picture;
56  PP_VideoPicture* original_picture = data->original_picture;
57  original_picture->decode_id = picture->decode_id;
58  original_picture->texture_id = picture->texture_id;
59  original_picture->texture_target = picture->texture_target;
60  original_picture->texture_size = picture->texture_size;
61  // Set visible_rect to the entire picture.
62  original_picture->visible_rect = PP_MakeRectFromXYWH(
63  0, 0, picture->texture_size.width, picture->texture_size.height);
64  }
65 
66  // Now execute the original callback.
67  PP_RunCompletionCallback(&data->original_callback, result);
68  delete data;
69 }
70 
71 } // namespace
72 
74 }
75 
77  if (has_interface<PPB_VideoDecoder_1_1>()) {
79  get_interface<PPB_VideoDecoder_1_1>()->Create(instance.pp_instance()));
80  } else if (has_interface<PPB_VideoDecoder_1_0>()) {
82  get_interface<PPB_VideoDecoder_1_0>()->Create(instance.pp_instance()));
83  } else if (has_interface<PPB_VideoDecoder_0_2>()) {
85  get_interface<PPB_VideoDecoder_0_2>()->Create(instance.pp_instance()));
86  } else if (has_interface<PPB_VideoDecoder_0_1>()) {
88  get_interface<PPB_VideoDecoder_0_1>()->Create(instance.pp_instance()));
89  }
90 }
91 
93 }
94 
96  PP_VideoProfile profile,
97  PP_HardwareAcceleration acceleration,
98  uint32_t min_picture_count,
99  const CompletionCallback& cc) {
100  if (has_interface<PPB_VideoDecoder_1_1>()) {
101  return get_interface<PPB_VideoDecoder_1_1>()->Initialize(
102  pp_resource(), context.pp_resource(), profile, acceleration,
103  min_picture_count, cc.pp_completion_callback());
104  }
105  if (has_interface<PPB_VideoDecoder_1_0>()) {
106  if (min_picture_count != 0)
107  return cc.MayForce(PP_ERROR_NOTSUPPORTED);
108  return get_interface<PPB_VideoDecoder_1_0>()->Initialize(
109  pp_resource(), context.pp_resource(), profile, acceleration,
111  }
112  if (has_interface<PPB_VideoDecoder_0_2>()) {
113  if (min_picture_count != 0)
114  return cc.MayForce(PP_ERROR_NOTSUPPORTED);
115  return get_interface<PPB_VideoDecoder_0_2>()->Initialize(
116  pp_resource(), context.pp_resource(), profile, acceleration,
118  }
119  if (has_interface<PPB_VideoDecoder_0_1>()) {
120  if (min_picture_count != 0)
121  return cc.MayForce(PP_ERROR_NOTSUPPORTED);
122  if (acceleration == PP_HARDWAREACCELERATION_NONE)
123  return cc.MayForce(PP_ERROR_NOTSUPPORTED);
124  return get_interface<PPB_VideoDecoder_0_1>()->Initialize(
125  pp_resource(),
126  context.pp_resource(),
127  profile,
128  acceleration == PP_HARDWAREACCELERATION_WITHFALLBACK
129  ? PP_TRUE
130  : PP_FALSE,
132  }
133  return cc.MayForce(PP_ERROR_NOINTERFACE);
134 }
135 
136 int32_t VideoDecoder::Decode(uint32_t decode_id,
137  uint32_t size,
138  const void* buffer,
139  const CompletionCallback& cc) {
140  if (has_interface<PPB_VideoDecoder_1_0>()) {
141  return get_interface<PPB_VideoDecoder_1_0>()->Decode(
142  pp_resource(), decode_id, size, buffer, cc.pp_completion_callback());
143  }
144  if (has_interface<PPB_VideoDecoder_0_2>()) {
145  return get_interface<PPB_VideoDecoder_0_2>()->Decode(
146  pp_resource(), decode_id, size, buffer, cc.pp_completion_callback());
147  }
148  if (has_interface<PPB_VideoDecoder_0_1>()) {
149  return get_interface<PPB_VideoDecoder_0_1>()->Decode(
150  pp_resource(), decode_id, size, buffer, cc.pp_completion_callback());
151  }
152  return cc.MayForce(PP_ERROR_NOINTERFACE);
153 }
154 
157  if (has_interface<PPB_VideoDecoder_1_0>()) {
158  return get_interface<PPB_VideoDecoder_1_0>()->GetPicture(
160  }
161  if (has_interface<PPB_VideoDecoder_0_2>()) {
162  // Data for our callback wrapper. The callback handler will delete it.
163  CallbackData_0_1* data = new CallbackData_0_1(cc);
164  return get_interface<PPB_VideoDecoder_0_2>()->GetPicture(
165  pp_resource(), &data->picture,
166  PP_MakeCompletionCallback(&CallbackConverter, data));
167  }
168  if (has_interface<PPB_VideoDecoder_0_1>()) {
169  // Data for our callback wrapper. The callback handler will delete it.
170  CallbackData_0_1* data = new CallbackData_0_1(cc);
171  return get_interface<PPB_VideoDecoder_0_1>()->GetPicture(
172  pp_resource(), &data->picture,
173  PP_MakeCompletionCallback(&CallbackConverter, data));
174  }
175  return cc.MayForce(PP_ERROR_NOINTERFACE);
176 }
177 
178 void VideoDecoder::RecyclePicture(const PP_VideoPicture& picture) {
179  if (has_interface<PPB_VideoDecoder_1_0>()) {
180  get_interface<PPB_VideoDecoder_1_0>()->RecyclePicture(pp_resource(),
181  &picture);
182  } else if (has_interface<PPB_VideoDecoder_0_2>()) {
183  get_interface<PPB_VideoDecoder_0_2>()->RecyclePicture(pp_resource(),
184  &picture);
185  } else if (has_interface<PPB_VideoDecoder_0_1>()) {
186  get_interface<PPB_VideoDecoder_0_1>()->RecyclePicture(pp_resource(),
187  &picture);
188  }
189 }
190 
192  if (has_interface<PPB_VideoDecoder_1_0>()) {
193  return get_interface<PPB_VideoDecoder_1_0>()->Flush(
195  }
196  if (has_interface<PPB_VideoDecoder_0_2>()) {
197  return get_interface<PPB_VideoDecoder_0_2>()->Flush(
199  }
200  if (has_interface<PPB_VideoDecoder_0_1>()) {
201  return get_interface<PPB_VideoDecoder_0_1>()->Flush(
203  }
204  return cc.MayForce(PP_ERROR_NOINTERFACE);
205 }
206 
208  if (has_interface<PPB_VideoDecoder_1_0>()) {
209  return get_interface<PPB_VideoDecoder_1_0>()->Reset(
211  }
212  if (has_interface<PPB_VideoDecoder_0_2>()) {
213  return get_interface<PPB_VideoDecoder_0_2>()->Reset(
215  }
216  if (has_interface<PPB_VideoDecoder_0_1>()) {
217  return get_interface<PPB_VideoDecoder_0_1>()->Reset(
219  }
220  return cc.MayForce(PP_ERROR_NOINTERFACE);
221 }
222 
223 } // namespace pp
void PassRefFromConstructor(PP_Resource resource)
Definition: resource.cc:50
int32_t MayForce(int32_t result) const
const PP_CompletionCallback & pp_completion_callback() const
This class represents a 3D rendering context in the browser.
Definition: graphics_3d.h:19
int32_t Initialize(const Graphics3D &graphics3d_context, PP_VideoProfile profile, PP_HardwareAcceleration acceleration, uint32_t min_picture_count, const CompletionCallback &callback)
int32_t Flush(const CompletionCallback &callback)
void RecyclePicture(const PP_VideoPicture &picture)
int32_t Decode(uint32_t decode_id, uint32_t size, const void *buffer, const CompletionCallback &callback)
PP_Resource pp_resource() const
Definition: resource.h:47
int32_t GetPicture(const CompletionCallbackWithOutput< PP_VideoPicture > &callback)
int32_t Reset(const CompletionCallback &callback)
Returns PP_ERROR_FAILED if the decoder isn't initialized.
PP_Instance pp_instance() const
A reference counted module resource.
Definition: resource.h:20