Add/resurrect support for bundles of WebStore items.
[chromium-blink-merge.git] / ppapi / cpp / video_decoder.cc
blobea954a66fa3991babdc7fbce950375e5f24dbf9a
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.
5 #include "ppapi/cpp/video_decoder.h"
7 #include "ppapi/c/pp_errors.h"
8 #include "ppapi/c/ppb_video_decoder.h"
9 #include "ppapi/cpp/completion_callback.h"
10 #include "ppapi/cpp/instance_handle.h"
11 #include "ppapi/cpp/module.h"
12 #include "ppapi/cpp/module_impl.h"
14 namespace pp {
16 namespace {
18 template <>
19 const char* interface_name<PPB_VideoDecoder_0_1>() {
20 return PPB_VIDEODECODER_INTERFACE_0_1;
23 template <>
24 const char* interface_name<PPB_VideoDecoder_0_2>() {
25 return PPB_VIDEODECODER_INTERFACE_0_2;
28 template <>
29 const char* interface_name<PPB_VideoDecoder_1_0>() {
30 return PPB_VIDEODECODER_INTERFACE_1_0;
33 // This struct is used to adapt CompletionCallbackWithOutput<PP_VideoPicture> to
34 // the pre-1.0 APIs, which return PP_VideoPicture_0_1. This struct is allocated
35 // on the heap, and deleted in CallbackConverter.
36 struct CallbackData_0_1 {
37 explicit CallbackData_0_1(
38 const CompletionCallbackWithOutput<PP_VideoPicture>& cc)
39 : original_picture(cc.output()),
40 original_callback(cc.pp_completion_callback()) {}
41 PP_VideoPicture_0_1 picture;
42 PP_VideoPicture* original_picture;
43 PP_CompletionCallback original_callback;
46 // Convert a 1.0 style callback to pre-1.0 callback.
47 void CallbackConverter(void* user_data, int32_t result) {
48 CallbackData_0_1* data = static_cast<CallbackData_0_1*>(user_data);
49 if (result == PP_OK) {
50 PP_VideoPicture_0_1* picture = &data->picture;
51 PP_VideoPicture* original_picture = data->original_picture;
52 original_picture->decode_id = picture->decode_id;
53 original_picture->texture_id = picture->texture_id;
54 original_picture->texture_target = picture->texture_target;
55 original_picture->texture_size = picture->texture_size;
56 // Set visible_rect to the entire picture.
57 original_picture->visible_rect = PP_MakeRectFromXYWH(
58 0, 0, picture->texture_size.width, picture->texture_size.height);
61 // Now execute the original callback.
62 PP_RunCompletionCallback(&data->original_callback, result);
63 delete data;
66 } // namespace
68 VideoDecoder::VideoDecoder() {
71 VideoDecoder::VideoDecoder(const InstanceHandle& instance) {
72 if (has_interface<PPB_VideoDecoder_1_0>()) {
73 PassRefFromConstructor(
74 get_interface<PPB_VideoDecoder_1_0>()->Create(instance.pp_instance()));
75 } else if (has_interface<PPB_VideoDecoder_0_2>()) {
76 PassRefFromConstructor(
77 get_interface<PPB_VideoDecoder_0_2>()->Create(instance.pp_instance()));
78 } else if (has_interface<PPB_VideoDecoder_0_1>()) {
79 PassRefFromConstructor(
80 get_interface<PPB_VideoDecoder_0_1>()->Create(instance.pp_instance()));
84 VideoDecoder::VideoDecoder(const VideoDecoder& other) : Resource(other) {
87 int32_t VideoDecoder::Initialize(const Graphics3D& context,
88 PP_VideoProfile profile,
89 PP_HardwareAcceleration acceleration,
90 const CompletionCallback& cc) {
91 if (has_interface<PPB_VideoDecoder_1_0>()) {
92 return get_interface<PPB_VideoDecoder_1_0>()->Initialize(
93 pp_resource(), context.pp_resource(), profile, acceleration,
94 cc.pp_completion_callback());
96 if (has_interface<PPB_VideoDecoder_0_2>()) {
97 return get_interface<PPB_VideoDecoder_0_2>()->Initialize(
98 pp_resource(), context.pp_resource(), profile, acceleration,
99 cc.pp_completion_callback());
101 if (has_interface<PPB_VideoDecoder_0_1>()) {
102 if (acceleration == PP_HARDWAREACCELERATION_NONE)
103 return cc.MayForce(PP_ERROR_NOTSUPPORTED);
104 return get_interface<PPB_VideoDecoder_0_1>()->Initialize(
105 pp_resource(),
106 context.pp_resource(),
107 profile,
108 acceleration == PP_HARDWAREACCELERATION_WITHFALLBACK
109 ? PP_TRUE
110 : PP_FALSE,
111 cc.pp_completion_callback());
113 return cc.MayForce(PP_ERROR_NOINTERFACE);
116 int32_t VideoDecoder::Decode(uint32_t decode_id,
117 uint32_t size,
118 const void* buffer,
119 const CompletionCallback& cc) {
120 if (has_interface<PPB_VideoDecoder_1_0>()) {
121 return get_interface<PPB_VideoDecoder_1_0>()->Decode(
122 pp_resource(), decode_id, size, buffer, cc.pp_completion_callback());
124 if (has_interface<PPB_VideoDecoder_0_2>()) {
125 return get_interface<PPB_VideoDecoder_0_2>()->Decode(
126 pp_resource(), decode_id, size, buffer, cc.pp_completion_callback());
128 if (has_interface<PPB_VideoDecoder_0_1>()) {
129 return get_interface<PPB_VideoDecoder_0_1>()->Decode(
130 pp_resource(), decode_id, size, buffer, cc.pp_completion_callback());
132 return cc.MayForce(PP_ERROR_NOINTERFACE);
135 int32_t VideoDecoder::GetPicture(
136 const CompletionCallbackWithOutput<PP_VideoPicture>& cc) {
137 if (has_interface<PPB_VideoDecoder_1_0>()) {
138 return get_interface<PPB_VideoDecoder_1_0>()->GetPicture(
139 pp_resource(), cc.output(), cc.pp_completion_callback());
141 if (has_interface<PPB_VideoDecoder_0_2>()) {
142 // Data for our callback wrapper. The callback handler will delete it.
143 CallbackData_0_1* data = new CallbackData_0_1(cc);
144 return get_interface<PPB_VideoDecoder_0_2>()->GetPicture(
145 pp_resource(), &data->picture,
146 PP_MakeCompletionCallback(&CallbackConverter, data));
148 if (has_interface<PPB_VideoDecoder_0_1>()) {
149 // Data for our callback wrapper. The callback handler will delete it.
150 CallbackData_0_1* data = new CallbackData_0_1(cc);
151 return get_interface<PPB_VideoDecoder_0_1>()->GetPicture(
152 pp_resource(), &data->picture,
153 PP_MakeCompletionCallback(&CallbackConverter, data));
155 return cc.MayForce(PP_ERROR_NOINTERFACE);
158 void VideoDecoder::RecyclePicture(const PP_VideoPicture& picture) {
159 if (has_interface<PPB_VideoDecoder_1_0>()) {
160 get_interface<PPB_VideoDecoder_1_0>()->RecyclePicture(pp_resource(),
161 &picture);
162 } else if (has_interface<PPB_VideoDecoder_0_2>()) {
163 get_interface<PPB_VideoDecoder_0_2>()->RecyclePicture(pp_resource(),
164 &picture);
165 } else if (has_interface<PPB_VideoDecoder_0_1>()) {
166 get_interface<PPB_VideoDecoder_0_1>()->RecyclePicture(pp_resource(),
167 &picture);
171 int32_t VideoDecoder::Flush(const CompletionCallback& cc) {
172 if (has_interface<PPB_VideoDecoder_1_0>()) {
173 return get_interface<PPB_VideoDecoder_1_0>()->Flush(
174 pp_resource(), cc.pp_completion_callback());
176 if (has_interface<PPB_VideoDecoder_0_2>()) {
177 return get_interface<PPB_VideoDecoder_0_2>()->Flush(
178 pp_resource(), cc.pp_completion_callback());
180 if (has_interface<PPB_VideoDecoder_0_1>()) {
181 return get_interface<PPB_VideoDecoder_0_1>()->Flush(
182 pp_resource(), cc.pp_completion_callback());
184 return cc.MayForce(PP_ERROR_NOINTERFACE);
187 int32_t VideoDecoder::Reset(const CompletionCallback& cc) {
188 if (has_interface<PPB_VideoDecoder_1_0>()) {
189 return get_interface<PPB_VideoDecoder_1_0>()->Reset(
190 pp_resource(), cc.pp_completion_callback());
192 if (has_interface<PPB_VideoDecoder_0_2>()) {
193 return get_interface<PPB_VideoDecoder_0_2>()->Reset(
194 pp_resource(), cc.pp_completion_callback());
196 if (has_interface<PPB_VideoDecoder_0_1>()) {
197 return get_interface<PPB_VideoDecoder_0_1>()->Reset(
198 pp_resource(), cc.pp_completion_callback());
200 return cc.MayForce(PP_ERROR_NOINTERFACE);
203 } // namespace pp