summaryrefslogtreecommitdiffstats
path: root/webkit/plugins/ppapi/ppb_video_decoder_impl.cc
blob: ec2d3f10e07813bb12b4fc99a09f190227240200 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "webkit/plugins/ppapi/ppb_video_decoder_impl.h"

#include <string>

#include "base/logging.h"
#include "ppapi/c/dev/pp_video_dev.h"
#include "ppapi/c/dev/ppb_video_decoder_dev.h"
#include "ppapi/c/pp_completion_callback.h"
#include "ppapi/c/pp_errors.h"
#include "webkit/plugins/ppapi/common.h"
#include "webkit/plugins/ppapi/var.h"
#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
#include "webkit/plugins/ppapi/ppb_file_ref_impl.h"
#include "webkit/plugins/ppapi/resource_tracker.h"

namespace webkit {
namespace ppapi {

namespace {

PP_Bool GetConfigs(PP_Instance instance_id,
                   PP_VideoDecoderConfig_Dev* proto_config,
                   PP_VideoDecoderConfig_Dev* matching_configs,
                   int32_t matching_configs_size,
                   int32_t* num_of_matching_configs) {
  PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id);
  if (!instance)
    return PP_FALSE;

  scoped_refptr<PPB_VideoDecoder_Impl> decoder(
      new PPB_VideoDecoder_Impl(instance));

  return BoolToPPBool(decoder->GetConfigs(proto_config,
                                          matching_configs,
                                          matching_configs_size,
                                          num_of_matching_configs));
}

PP_Resource Create(PP_Instance instance_id,
                   PP_VideoDecoderConfig_Dev* decoder_config) {
  PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id);
  if (!instance)
    return 0;

  scoped_refptr<PPB_VideoDecoder_Impl> decoder(
      new PPB_VideoDecoder_Impl(instance));

  if (!decoder->Init(const_cast<PP_VideoDecoderConfig_Dev*>(decoder_config)))
    return 0;

  return decoder->GetReference();
}

PP_Bool IsVideoDecoder(PP_Resource resource) {
  return BoolToPPBool(!!Resource::GetAs<PPB_VideoDecoder_Impl>(resource));
}

PP_Bool Decode(PP_Resource decoder_id,
               PP_VideoBitstreamBuffer_Dev* bitstream_buffer,
               PP_CompletionCallback callback) {
  scoped_refptr<PPB_VideoDecoder_Impl> decoder(
      Resource::GetAs<PPB_VideoDecoder_Impl>(decoder_id));
  if (!decoder)
    return PP_FALSE;

  return BoolToPPBool(decoder->Decode(bitstream_buffer, callback));
}

void AssignPictureBuffer(PP_Resource video_decoder,
                         uint32_t no_of_buffers,
                         union PP_PictureData_Dev* picture_buffer) {
  scoped_refptr<PPB_VideoDecoder_Impl> decoder(
      Resource::GetAs<PPB_VideoDecoder_Impl>(video_decoder));
  if (!decoder)
    return;

  decoder->AssignPictureBuffer(no_of_buffers, picture_buffer);
}

void ReusePictureBuffer(PP_Resource video_decoder,
                        union PP_PictureData_Dev* picture_buffer) {
  scoped_refptr<PPB_VideoDecoder_Impl> decoder(
      Resource::GetAs<PPB_VideoDecoder_Impl>(video_decoder));
  if (!decoder)
    return;

  decoder->ReusePictureBuffer(picture_buffer);
}

PP_Bool Flush(PP_Resource video_decoder,
              PP_CompletionCallback callback) {
  scoped_refptr<PPB_VideoDecoder_Impl> decoder(
      Resource::GetAs<PPB_VideoDecoder_Impl>(video_decoder));
  if (!decoder)
    return PP_FALSE;

  return BoolToPPBool(decoder->Flush(callback));
}

PP_Bool Abort(PP_Resource video_decoder,
              PP_CompletionCallback callback) {
  scoped_refptr<PPB_VideoDecoder_Impl> decoder(
      Resource::GetAs<PPB_VideoDecoder_Impl>(video_decoder));
  if (!decoder)
    return PP_FALSE;

  return BoolToPPBool(decoder->Abort(callback));
}


const PPB_VideoDecoder_Dev ppb_videodecoder = {
  &GetConfigs,
  &Create,
  &IsVideoDecoder,
  &Decode,
  &AssignPictureBuffer,
  &ReusePictureBuffer,
  &Flush,
  &Abort,
};

}  // namespace

PPB_VideoDecoder_Impl::PPB_VideoDecoder_Impl(PluginInstance* instance)
    : Resource(instance),
      callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
      abort_callback_(PP_BlockUntilComplete()),
      flush_callback_(PP_BlockUntilComplete()),
      bitstream_buffer_callback_(PP_BlockUntilComplete()) {
}

PPB_VideoDecoder_Impl::~PPB_VideoDecoder_Impl() {
}

// static
const PPB_VideoDecoder_Dev* PPB_VideoDecoder_Impl::GetInterface() {
  return &ppb_videodecoder;
}

PPB_VideoDecoder_Impl* PPB_VideoDecoder_Impl::AsPPB_VideoDecoder_Impl() {
  return this;
}

bool PPB_VideoDecoder_Impl::GetConfigs(
    PP_VideoDecoderConfig_Dev* proto_config,
    PP_VideoDecoderConfig_Dev* matching_configs,
    int32_t matching_configs_size,
    int32_t* num_of_matching_configs) {
  if (!instance())
    return false;
  if (!platform_video_decoder_.get())
    return false;

  // TODO(vmr): Implement.
  NOTIMPLEMENTED();

  return false;
}

bool PPB_VideoDecoder_Impl::Init(PP_VideoDecoderConfig_Dev* decoder_config) {
  if (!instance())
    return false;

  platform_video_decoder_.reset(
      instance()->delegate()->CreateVideoDecoder(decoder_config));

  return platform_video_decoder_.get()? true : false;
}

bool PPB_VideoDecoder_Impl::Decode(
    PP_VideoBitstreamBuffer_Dev* bitstream_buffer,
    PP_CompletionCallback callback) {
  if (!platform_video_decoder_.get())
    return false;

  media::BitstreamBuffer* decode_buffer = NULL;
  // TODO(vmr): Convert bitstream_buffer to BitstreamBuffer object.
  NOTIMPLEMENTED();

  // Store the callback to inform when bitstream buffer has been processed.
  // TODO(vmr): handle simultaneous decodes + callbacks.
  bitstream_buffer_callback_ = callback;

  return platform_video_decoder_->Decode(
      decode_buffer,
      callback_factory_.NewCallback(
          &PPB_VideoDecoder_Impl::OnBitstreamBufferProcessed));
}

void PPB_VideoDecoder_Impl::AssignPictureBuffer(
    uint32_t no_of_picture_buffers,
    PP_PictureData_Dev* picture_buffers) {
  if (!platform_video_decoder_.get())
    return;

  // TODO(vmr): Map PP_PictureData_Dev into PictureBuffer object.
  NOTIMPLEMENTED();

  media::VideoDecodeAccelerator::PictureBuffer* buffer = NULL;
  platform_video_decoder_->ReusePictureBuffer(buffer);
}

void PPB_VideoDecoder_Impl::ReusePictureBuffer(
    PP_PictureData_Dev* picture_buffer) {
  if (!platform_video_decoder_.get())
    return;

  // TODO(vmr): Map PP_PictureData_Dev into PictureBuffer object.
  NOTIMPLEMENTED();

  media::VideoDecodeAccelerator::PictureBuffer* buffer = NULL;
  platform_video_decoder_->ReusePictureBuffer(buffer);
}

bool PPB_VideoDecoder_Impl::Flush(PP_CompletionCallback callback) {
  if (!platform_video_decoder_.get())
    return false;

  // Store the callback to be called when Flush() is done.
  // TODO(vmr): Check for current flush/abort operations.
  flush_callback_ = callback;

  return platform_video_decoder_->Flush(
      callback_factory_.NewCallback(
          &PPB_VideoDecoder_Impl::OnFlushComplete));
}

bool PPB_VideoDecoder_Impl::Abort(PP_CompletionCallback callback) {
  if (!platform_video_decoder_.get())
    return false;

  // Store the callback to be called when Abort() is done.
  // TODO(vmr): Check for current flush/abort operations.
  abort_callback_ = callback;

  return platform_video_decoder_->Abort(
      callback_factory_.NewCallback(
          &PPB_VideoDecoder_Impl::OnAbortComplete));
}

void PPB_VideoDecoder_Impl::ProvidePictureBuffers(
    uint32_t requested_num_of_buffers,
    const std::vector<uint32_t>& buffer_properties) {
  // TODO(vmr): Implement.
  NOTIMPLEMENTED();
}

void PPB_VideoDecoder_Impl::PictureReady(
    media::VideoDecodeAccelerator::Picture* picture) {
  // TODO(vmr): Implement.
  NOTIMPLEMENTED();

  // Convert the picture.
  // Get plugin's PPP interface function pointers.
  // PPP_VideoDecoder* ppp_videodecoder;
  // Call ProvidePictureBuffers function pointer and return.
  // ppp_videodecoder->PictureReady(resource_decoder, picture,
  //                                pic_buffer_used_again);
}

void PPB_VideoDecoder_Impl::DismissPictureBuffer(
      media::VideoDecodeAccelerator::PictureBuffer* picture_buffer) {
  // TODO(vmr): Implement.
  NOTIMPLEMENTED();
}

void PPB_VideoDecoder_Impl::NotifyEndOfStream() {
  // TODO(vmr): Implement.
  NOTIMPLEMENTED();

  // Get decoder's resource handle.
  // PP_Resource resource_decoder;
  // Get plugin's PPP interface function pointers.
  // PPP_VideoDecoder* ppp_videodecoder;
  // Call EndOfStream function pointer and return.
  // ppp_videodecoder->EndOfStream(resource_decoder);
}

void PPB_VideoDecoder_Impl::NotifyError(
    media::VideoDecodeAccelerator::Error error) {
  // TODO(vmr): Implement.
  NOTIMPLEMENTED();

  // Get decoder's resource handle.
  // PP_Resource resource_decoder;
  // Get plugin's PPP interface function pointers.
  // PPP_VideoDecoder* ppp_videodecoder;
  // Call NotifyError function pointer.
  // ppp_videodecoder->NotifyError(error, error_data, data_size);
}

void PPB_VideoDecoder_Impl::OnAbortComplete() {
  if (abort_callback_.func == NULL)
    return;

  // Call the callback that was stored to be called when Abort is done.
  PP_CompletionCallback callback = PP_BlockUntilComplete();
  std::swap(callback, abort_callback_);
  PP_RunCompletionCallback(&callback, PP_OK);
}

void PPB_VideoDecoder_Impl::OnBitstreamBufferProcessed() {
  if (bitstream_buffer_callback_.func == NULL)
    return;

  // Call the callback that was stored to be called when bitstream was sent for
  // decoding.
  PP_CompletionCallback callback = PP_BlockUntilComplete();
  std::swap(callback, bitstream_buffer_callback_);
  PP_RunCompletionCallback(&callback, PP_OK);
}

void PPB_VideoDecoder_Impl::OnFlushComplete() {
  if (flush_callback_.func == NULL)
    return;

  // Call the callback that was stored to be called when Flush is done.
  PP_CompletionCallback callback = PP_BlockUntilComplete();
  std::swap(callback, flush_callback_);
  PP_RunCompletionCallback(&callback, PP_OK);
}

}  // namespace ppapi
}  // namespace webkit