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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
// Copyright (c) 2012 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 <assert.h>
#include <string.h>
#include <map>
#include <vector>
#include "ppapi/c/dev/ppb_video_capture_dev.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/ppb_opengles2.h"
#include "ppapi/cpp/dev/buffer_dev.h"
#include "ppapi/cpp/dev/device_ref_dev.h"
#include "ppapi/cpp/dev/video_capture_dev.h"
#include "ppapi/cpp/dev/video_capture_client_dev.h"
#include "ppapi/cpp/completion_callback.h"
#include "ppapi/cpp/graphics_3d_client.h"
#include "ppapi/cpp/graphics_3d.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/rect.h"
#include "ppapi/cpp/var.h"
#include "ppapi/lib/gl/include/GLES2/gl2.h"
#include "ppapi/utility/completion_callback_factory.h"
// Assert |context_| isn't holding any GL Errors. Done as a macro instead of a
// function to preserve line number information in the failure message.
#define assertNoGLError() \
assert(!gles2_if_->GetError(context_->pp_resource()));
namespace {
// This object is the global object representing this plugin library as long
// as it is loaded.
class VCDemoModule : public pp::Module {
public:
VCDemoModule() : pp::Module() {}
virtual ~VCDemoModule() {}
virtual pp::Instance* CreateInstance(PP_Instance instance);
};
class VCDemoInstance : public pp::Instance,
public pp::Graphics3DClient,
public pp::VideoCaptureClient_Dev {
public:
VCDemoInstance(PP_Instance instance, pp::Module* module);
virtual ~VCDemoInstance();
// pp::Instance implementation (see PPP_Instance).
virtual void DidChangeView(const pp::Rect& position,
const pp::Rect& clip_ignored);
virtual void HandleMessage(const pp::Var& message_data);
// pp::Graphics3DClient implementation.
virtual void Graphics3DContextLost() {
InitGL();
CreateYUVTextures();
Render();
}
virtual void OnDeviceInfo(PP_Resource resource,
const PP_VideoCaptureDeviceInfo_Dev& info,
const std::vector<pp::Buffer_Dev>& buffers) {
capture_info_ = info;
buffers_ = buffers;
CreateYUVTextures();
}
virtual void OnStatus(PP_Resource resource, uint32_t status) {
}
virtual void OnError(PP_Resource resource, uint32_t error) {
}
virtual void OnBufferReady(PP_Resource resource, uint32_t buffer) {
const char* data = static_cast<const char*>(buffers_[buffer].data());
int32_t width = capture_info_.width;
int32_t height = capture_info_.height;
gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE0);
gles2_if_->TexSubImage2D(
context_->pp_resource(), GL_TEXTURE_2D, 0, 0, 0, width, height,
GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
data += width * height;
width /= 2;
height /= 2;
gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE1);
gles2_if_->TexSubImage2D(
context_->pp_resource(), GL_TEXTURE_2D, 0, 0, 0, width, height,
GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
data += width * height;
gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE2);
gles2_if_->TexSubImage2D(
context_->pp_resource(), GL_TEXTURE_2D, 0, 0, 0, width, height,
GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
video_capture_.ReuseBuffer(buffer);
if (is_painting_)
needs_paint_ = true;
else
Render();
}
private:
void Render();
// GL-related functions.
void InitGL();
GLuint CreateTexture(int32_t width, int32_t height, int unit);
void CreateGLObjects();
void CreateShader(GLuint program, GLenum type, const char* source, int size);
void PaintFinished(int32_t result);
void CreateYUVTextures();
void Open(const pp::DeviceRef_Dev& device);
void EnumerateDevicesFinished(int32_t result);
void OpenFinished(int32_t result);
pp::Size position_size_;
bool is_painting_;
bool needs_paint_;
GLuint texture_y_;
GLuint texture_u_;
GLuint texture_v_;
pp::VideoCapture_Dev video_capture_;
PP_VideoCaptureDeviceInfo_Dev capture_info_;
std::vector<pp::Buffer_Dev> buffers_;
pp::CompletionCallbackFactory<VCDemoInstance> callback_factory_;
// Unowned pointers.
const struct PPB_OpenGLES2* gles2_if_;
// Owned data.
pp::Graphics3D* context_;
std::vector<pp::DeviceRef_Dev> devices_;
};
VCDemoInstance::VCDemoInstance(PP_Instance instance, pp::Module* module)
: pp::Instance(instance),
pp::Graphics3DClient(this),
pp::VideoCaptureClient_Dev(this),
is_painting_(false),
needs_paint_(false),
texture_y_(0),
texture_u_(0),
texture_v_(0),
video_capture_(*this),
callback_factory_(this),
context_(NULL) {
gles2_if_ = static_cast<const struct PPB_OpenGLES2*>(
module->GetBrowserInterface(PPB_OPENGLES2_INTERFACE));
assert(gles2_if_);
capture_info_.width = 320;
capture_info_.height = 240;
capture_info_.frames_per_second = 30;
}
VCDemoInstance::~VCDemoInstance() {
delete context_;
}
void VCDemoInstance::DidChangeView(
const pp::Rect& position, const pp::Rect& clip_ignored) {
if (position.width() == 0 || position.height() == 0)
return;
if (position.size() == position_size_)
return;
position_size_ = position.size();
// Initialize graphics.
InitGL();
Render();
}
void VCDemoInstance::HandleMessage(const pp::Var& message_data) {
if (message_data.is_string()) {
std::string event = message_data.AsString();
if (event == "PageInitialized") {
pp::CompletionCallback callback = callback_factory_.NewCallback(
&VCDemoInstance::EnumerateDevicesFinished);
video_capture_.EnumerateDevices(&devices_, callback);
} else if (event == "UseDefault") {
Open(pp::DeviceRef_Dev());
} else if (event == "UseDefault(v0.1)") {
const PPB_VideoCapture_Dev_0_1* video_capture_0_1 =
static_cast<const PPB_VideoCapture_Dev_0_1*>(
pp::Module::Get()->GetBrowserInterface(
PPB_VIDEOCAPTURE_DEV_INTERFACE_0_1));
video_capture_0_1->StartCapture(video_capture_.pp_resource(),
&capture_info_, 4);
} else if (event == "Stop") {
video_capture_.StopCapture();
}
} else if (message_data.is_number()) {
int index = message_data.AsInt();
if (index >= 0 && index < static_cast<int>(devices_.size())) {
Open(devices_[index]);
} else {
assert(false);
}
}
}
void VCDemoInstance::InitGL() {
assert(position_size_.width() && position_size_.height());
is_painting_ = false;
delete context_;
int32_t attributes[] = {
PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 0,
PP_GRAPHICS3DATTRIB_BLUE_SIZE, 8,
PP_GRAPHICS3DATTRIB_GREEN_SIZE, 8,
PP_GRAPHICS3DATTRIB_RED_SIZE, 8,
PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 0,
PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 0,
PP_GRAPHICS3DATTRIB_SAMPLES, 0,
PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0,
PP_GRAPHICS3DATTRIB_WIDTH, position_size_.width(),
PP_GRAPHICS3DATTRIB_HEIGHT, position_size_.height(),
PP_GRAPHICS3DATTRIB_NONE,
};
context_ = new pp::Graphics3D(this, attributes);
assert(!context_->is_null());
// Set viewport window size and clear color bit.
gles2_if_->ClearColor(context_->pp_resource(), 1, 0, 0, 1);
gles2_if_->Clear(context_->pp_resource(), GL_COLOR_BUFFER_BIT);
gles2_if_->Viewport(context_->pp_resource(), 0, 0,
position_size_.width(), position_size_.height());
BindGraphics(*context_);
assertNoGLError();
CreateGLObjects();
}
void VCDemoInstance::Render() {
assert(!is_painting_);
is_painting_ = true;
needs_paint_ = false;
if (texture_y_) {
gles2_if_->DrawArrays(context_->pp_resource(), GL_TRIANGLE_STRIP, 0, 4);
} else {
gles2_if_->Clear(context_->pp_resource(), GL_COLOR_BUFFER_BIT);
}
pp::CompletionCallback cb = callback_factory_.NewCallback(
&VCDemoInstance::PaintFinished);
context_->SwapBuffers(cb);
}
void VCDemoInstance::PaintFinished(int32_t result) {
is_painting_ = false;
if (needs_paint_)
Render();
}
GLuint VCDemoInstance::CreateTexture(int32_t width, int32_t height, int unit) {
GLuint texture_id;
gles2_if_->GenTextures(context_->pp_resource(), 1, &texture_id);
assertNoGLError();
// Assign parameters.
gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE0 + unit);
gles2_if_->BindTexture(context_->pp_resource(), GL_TEXTURE_2D, texture_id);
gles2_if_->TexParameteri(
context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
gles2_if_->TexParameteri(
context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
gles2_if_->TexParameterf(
context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_CLAMP_TO_EDGE);
gles2_if_->TexParameterf(
context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_CLAMP_TO_EDGE);
// Allocate texture.
gles2_if_->TexImage2D(
context_->pp_resource(), GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0,
GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL);
assertNoGLError();
return texture_id;
}
void VCDemoInstance::CreateGLObjects() {
// Code and constants for shader.
static const char kVertexShader[] =
"varying vec2 v_texCoord; \n"
"attribute vec4 a_position; \n"
"attribute vec2 a_texCoord; \n"
"void main() \n"
"{ \n"
" v_texCoord = a_texCoord; \n"
" gl_Position = a_position; \n"
"}";
static const char kFragmentShader[] =
"precision mediump float; \n"
"varying vec2 v_texCoord; \n"
"uniform sampler2D y_texture; \n"
"uniform sampler2D u_texture; \n"
"uniform sampler2D v_texture; \n"
"uniform mat3 color_matrix; \n"
"void main() \n"
"{ \n"
" vec3 yuv; \n"
" yuv.x = texture2D(y_texture, v_texCoord).r; \n"
" yuv.y = texture2D(u_texture, v_texCoord).r; \n"
" yuv.z = texture2D(v_texture, v_texCoord).r; \n"
" vec3 rgb = color_matrix * (yuv - vec3(0.0625, 0.5, 0.5));\n"
" gl_FragColor = vec4(rgb, 1.0); \n"
"}";
static const float kColorMatrix[9] = {
1.1643828125f, 1.1643828125f, 1.1643828125f,
0.0f, -0.39176171875f, 2.017234375f,
1.59602734375f, -0.81296875f, 0.0f
};
PP_Resource context = context_->pp_resource();
// Create shader program.
GLuint program = gles2_if_->CreateProgram(context);
CreateShader(program, GL_VERTEX_SHADER, kVertexShader, sizeof(kVertexShader));
CreateShader(
program, GL_FRAGMENT_SHADER, kFragmentShader, sizeof(kFragmentShader));
gles2_if_->LinkProgram(context, program);
gles2_if_->UseProgram(context, program);
gles2_if_->DeleteProgram(context, program);
gles2_if_->Uniform1i(
context, gles2_if_->GetUniformLocation(context, program, "y_texture"), 0);
gles2_if_->Uniform1i(
context, gles2_if_->GetUniformLocation(context, program, "u_texture"), 1);
gles2_if_->Uniform1i(
context, gles2_if_->GetUniformLocation(context, program, "v_texture"), 2);
gles2_if_->UniformMatrix3fv(
context,
gles2_if_->GetUniformLocation(context, program, "color_matrix"),
1, GL_FALSE, kColorMatrix);
assertNoGLError();
// Assign vertex positions and texture coordinates to buffers for use in
// shader program.
static const float kVertices[] = {
-1, 1, -1, -1, 1, 1, 1, -1, // Position coordinates.
0, 0, 0, 1, 1, 0, 1, 1, // Texture coordinates.
};
GLuint buffer;
gles2_if_->GenBuffers(context, 1, &buffer);
gles2_if_->BindBuffer(context, GL_ARRAY_BUFFER, buffer);
gles2_if_->BufferData(context, GL_ARRAY_BUFFER,
sizeof(kVertices), kVertices, GL_STATIC_DRAW);
assertNoGLError();
GLint pos_location = gles2_if_->GetAttribLocation(
context, program, "a_position");
GLint tc_location = gles2_if_->GetAttribLocation(
context, program, "a_texCoord");
assertNoGLError();
gles2_if_->EnableVertexAttribArray(context, pos_location);
gles2_if_->VertexAttribPointer(context, pos_location, 2,
GL_FLOAT, GL_FALSE, 0, 0);
gles2_if_->EnableVertexAttribArray(context, tc_location);
gles2_if_->VertexAttribPointer(
context, tc_location, 2, GL_FLOAT, GL_FALSE, 0,
static_cast<float*>(0) + 8); // Skip position coordinates.
assertNoGLError();
}
void VCDemoInstance::CreateShader(
GLuint program, GLenum type, const char* source, int size) {
PP_Resource context = context_->pp_resource();
GLuint shader = gles2_if_->CreateShader(context, type);
gles2_if_->ShaderSource(context, shader, 1, &source, &size);
gles2_if_->CompileShader(context, shader);
gles2_if_->AttachShader(context, program, shader);
gles2_if_->DeleteShader(context, shader);
}
void VCDemoInstance::CreateYUVTextures() {
int32_t width = capture_info_.width;
int32_t height = capture_info_.height;
texture_y_ = CreateTexture(width, height, 0);
width /= 2;
height /= 2;
texture_u_ = CreateTexture(width, height, 1);
texture_v_ = CreateTexture(width, height, 2);
}
void VCDemoInstance::Open(const pp::DeviceRef_Dev& device) {
pp::CompletionCallback callback = callback_factory_.NewCallback(
&VCDemoInstance::OpenFinished);
video_capture_.Open(device, capture_info_, 4, callback);
}
void VCDemoInstance::EnumerateDevicesFinished(int32_t result) {
static const char* const kDelimiter = "#__#";
if (result == PP_OK) {
std::string device_names;
for (size_t index = 0; index < devices_.size(); ++index) {
pp::Var name = devices_[index].GetName();
assert(name.is_string());
if (index != 0)
device_names += kDelimiter;
device_names += name.AsString();
}
PostMessage(pp::Var(device_names));
} else {
PostMessage(pp::Var("EnumerationFailed"));
}
}
void VCDemoInstance::OpenFinished(int32_t result) {
if (result == PP_OK) {
video_capture_.StartCapture();
} else {
PostMessage(pp::Var("OpenFailed"));
}
}
pp::Instance* VCDemoModule::CreateInstance(PP_Instance instance) {
return new VCDemoInstance(instance, this);
}
} // anonymous namespace
namespace pp {
// Factory function for your specialization of the Module object.
Module* CreateModule() {
return new VCDemoModule();
}
} // namespace pp
|