summaryrefslogtreecommitdiffstats
path: root/ppapi/shared_impl/video_decoder_impl.cc
blob: ef20f8bdc396d394bb342dcd16ed33d70ed2c534 (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
// 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 "ppapi/shared_impl/video_decoder_impl.h"

#include "base/logging.h"
#include "gpu/command_buffer/client/gles2_implementation.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/proxy/plugin_resource_tracker.h"
#include "ppapi/thunk/ppb_context_3d_api.h"
#include "ppapi/thunk/enter.h"

using ppapi::thunk::PPB_Context3D_API;

namespace ppapi {

VideoDecoderImpl::VideoDecoderImpl()
    : flush_callback_(PP_MakeCompletionCallback(NULL, NULL)),
      reset_callback_(PP_MakeCompletionCallback(NULL, NULL)),
      context3d_id_(0),
      gles2_impl_(NULL) {
}

VideoDecoderImpl::~VideoDecoderImpl() {
}

bool VideoDecoderImpl::Init(PP_Resource context3d_id,
                            PPB_Context3D_API* context3d,
                            const PP_VideoConfigElement* decoder_config) {
  if (!context3d || !decoder_config || !context3d_id)
    return false;

  DCHECK(!gles2_impl_ && !context3d_id_);
  gles2_impl_ = context3d->GetGLES2Impl();
  AddRefResource(context3d_id);
  context3d_id_ = context3d_id;
  return true;
}

void VideoDecoderImpl::Destroy() {
  context3d_id_ = 0;
  gles2_impl_ = NULL;
  UnrefResource(context3d_id_);
}

bool VideoDecoderImpl::SetFlushCallback(PP_CompletionCallback callback) {
  CHECK(callback.func);
  if (flush_callback_.func)
    return false;
  flush_callback_ = callback;
  return true;
}

bool VideoDecoderImpl::SetResetCallback(PP_CompletionCallback callback) {
  CHECK(callback.func);
  if (reset_callback_.func)
    return false;
  reset_callback_ = callback;
  return true;
}

bool VideoDecoderImpl::SetBitstreamBufferCallback(
    int32 bitstream_buffer_id, PP_CompletionCallback callback) {
  return bitstream_buffer_callbacks_.insert(
      std::make_pair(bitstream_buffer_id, callback)).second;
}

void VideoDecoderImpl::RunFlushCallback(int32 result) {
  DCHECK(flush_callback_.func);
  PP_RunAndClearCompletionCallback(&flush_callback_, result);
}

void VideoDecoderImpl::RunResetCallback(int32 result) {
  DCHECK(reset_callback_.func);
  PP_RunAndClearCompletionCallback(&reset_callback_, result);
}

void VideoDecoderImpl::RunBitstreamBufferCallback(
    int32 bitstream_buffer_id, int32 result) {
  CallbackById::iterator it =
      bitstream_buffer_callbacks_.find(bitstream_buffer_id);
  DCHECK(it != bitstream_buffer_callbacks_.end());
  PP_CompletionCallback cc = it->second;
  bitstream_buffer_callbacks_.erase(it);
  PP_RunCompletionCallback(&cc, PP_OK);
}

void VideoDecoderImpl::FlushCommandBuffer() {
  if (gles2_impl_)
    gles2_impl_->Flush();
}

bool VideoDecoderImpl::CopyConfigsToVector(
    const PP_VideoConfigElement* configs_to_copy,
    std::vector<PP_VideoConfigElement>* out_configs) {
  // TODO(fischman/vrk): This is still broken. We need to get rid of the silly
  // PP_VideoConfigElement vector in favor of a struct (see TODO in
  // ppb_video_decoder_dev.h).
  const PP_VideoConfigElement* current = configs_to_copy;
  while (current && *current != PP_VIDEOATTR_DICTIONARY_TERMINATOR) {
    out_configs->push_back(*current);
    out_configs->push_back(*(current + 1));
    current += 2;
  }
  return true;
}

}  // namespace ppapi