Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ppapi / cpp / video_decoder.cc
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 #include "ppapi/cpp/video_decoder.h"
6
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"
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 // 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;
44 };
45
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);
59   }
60
61   // Now execute the original callback.
62   PP_RunCompletionCallback(&data->original_callback, result);
63   delete data;
64 }
65
66 }  // namespace
67
68 VideoDecoder::VideoDecoder() {
69 }
70
71 VideoDecoder::VideoDecoder(const InstanceHandle& instance) {
72   if (has_interface<PPB_VideoDecoder_0_1>()) {
73     PassRefFromConstructor(
74         get_interface<PPB_VideoDecoder_0_1>()->Create(instance.pp_instance()));
75   }
76 }
77
78 VideoDecoder::VideoDecoder(const VideoDecoder& other) : Resource(other) {
79 }
80
81 int32_t VideoDecoder::Initialize(const Graphics3D& context,
82                                  PP_VideoProfile profile,
83                                  PP_HardwareAcceleration acceleration,
84                                  const CompletionCallback& cc) {
85   if (has_interface<PPB_VideoDecoder_1_0>()) {
86     return get_interface<PPB_VideoDecoder_1_0>()->Initialize(
87         pp_resource(), context.pp_resource(), profile, acceleration,
88         cc.pp_completion_callback());
89   }
90   if (has_interface<PPB_VideoDecoder_0_2>()) {
91     return get_interface<PPB_VideoDecoder_0_2>()->Initialize(
92         pp_resource(), context.pp_resource(), profile, acceleration,
93         cc.pp_completion_callback());
94   }
95   if (has_interface<PPB_VideoDecoder_0_1>()) {
96     if (acceleration == PP_HARDWAREACCELERATION_NONE)
97       return cc.MayForce(PP_ERROR_NOTSUPPORTED);
98     return get_interface<PPB_VideoDecoder_0_1>()->Initialize(
99         pp_resource(),
100         context.pp_resource(),
101         profile,
102         acceleration == PP_HARDWAREACCELERATION_WITHFALLBACK
103             ? PP_TRUE
104             : PP_FALSE,
105         cc.pp_completion_callback());
106   }
107   return cc.MayForce(PP_ERROR_NOINTERFACE);
108 }
109
110 int32_t VideoDecoder::Decode(uint32_t decode_id,
111                              uint32_t size,
112                              const void* buffer,
113                              const CompletionCallback& cc) {
114   if (has_interface<PPB_VideoDecoder_1_0>()) {
115     return get_interface<PPB_VideoDecoder_1_0>()->Decode(
116         pp_resource(), decode_id, size, buffer, cc.pp_completion_callback());
117   }
118   if (has_interface<PPB_VideoDecoder_0_2>()) {
119     return get_interface<PPB_VideoDecoder_0_2>()->Decode(
120         pp_resource(), decode_id, size, buffer, cc.pp_completion_callback());
121   }
122   if (has_interface<PPB_VideoDecoder_0_1>()) {
123     return get_interface<PPB_VideoDecoder_0_1>()->Decode(
124         pp_resource(), decode_id, size, buffer, cc.pp_completion_callback());
125   }
126   return cc.MayForce(PP_ERROR_NOINTERFACE);
127 }
128
129 int32_t VideoDecoder::GetPicture(
130     const CompletionCallbackWithOutput<PP_VideoPicture>& cc) {
131   if (has_interface<PPB_VideoDecoder_1_0>()) {
132     return get_interface<PPB_VideoDecoder_1_0>()->GetPicture(
133         pp_resource(), cc.output(), cc.pp_completion_callback());
134   }
135   if (has_interface<PPB_VideoDecoder_0_2>()) {
136     // Data for our callback wrapper. The callback handler will delete it.
137     CallbackData_0_1* data = new CallbackData_0_1(cc);
138     return get_interface<PPB_VideoDecoder_0_2>()->GetPicture(
139         pp_resource(), &data->picture,
140         PP_MakeCompletionCallback(&CallbackConverter, data));
141   }
142   if (has_interface<PPB_VideoDecoder_0_1>()) {
143     // Data for our callback wrapper. The callback handler will delete it.
144     CallbackData_0_1* data = new CallbackData_0_1(cc);
145     return get_interface<PPB_VideoDecoder_0_1>()->GetPicture(
146         pp_resource(), &data->picture,
147         PP_MakeCompletionCallback(&CallbackConverter, data));
148   }
149   return cc.MayForce(PP_ERROR_NOINTERFACE);
150 }
151
152 void VideoDecoder::RecyclePicture(const PP_VideoPicture& picture) {
153   if (has_interface<PPB_VideoDecoder_1_0>()) {
154     get_interface<PPB_VideoDecoder_1_0>()->RecyclePicture(pp_resource(),
155                                                           &picture);
156   } else if (has_interface<PPB_VideoDecoder_0_2>()) {
157     get_interface<PPB_VideoDecoder_0_2>()->RecyclePicture(pp_resource(),
158                                                           &picture);
159   } else if (has_interface<PPB_VideoDecoder_0_1>()) {
160     get_interface<PPB_VideoDecoder_0_1>()->RecyclePicture(pp_resource(),
161                                                           &picture);
162   }
163 }
164
165 int32_t VideoDecoder::Flush(const CompletionCallback& cc) {
166   if (has_interface<PPB_VideoDecoder_1_0>()) {
167     return get_interface<PPB_VideoDecoder_1_0>()->Flush(
168         pp_resource(), cc.pp_completion_callback());
169   }
170   if (has_interface<PPB_VideoDecoder_0_2>()) {
171     return get_interface<PPB_VideoDecoder_0_2>()->Flush(
172         pp_resource(), cc.pp_completion_callback());
173   }
174   if (has_interface<PPB_VideoDecoder_0_1>()) {
175     return get_interface<PPB_VideoDecoder_0_1>()->Flush(
176         pp_resource(), cc.pp_completion_callback());
177   }
178   return cc.MayForce(PP_ERROR_NOINTERFACE);
179 }
180
181 int32_t VideoDecoder::Reset(const CompletionCallback& cc) {
182   if (has_interface<PPB_VideoDecoder_1_0>()) {
183     return get_interface<PPB_VideoDecoder_1_0>()->Reset(
184         pp_resource(), cc.pp_completion_callback());
185   }
186   if (has_interface<PPB_VideoDecoder_0_2>()) {
187     return get_interface<PPB_VideoDecoder_0_2>()->Reset(
188         pp_resource(), cc.pp_completion_callback());
189   }
190   if (has_interface<PPB_VideoDecoder_0_1>()) {
191     return get_interface<PPB_VideoDecoder_0_1>()->Reset(
192         pp_resource(), cc.pp_completion_callback());
193   }
194   return cc.MayForce(PP_ERROR_NOINTERFACE);
195 }
196
197 }  // namespace pp