Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(541)

Side by Side Diff: ppapi/examples/media_stream_video/media_stream_video.cc

Issue 122383002: [WIP][PPAPI] Implement MediaStreamVideoTrackResource. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@io_stream_cl
Patch Set: Update Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdlib.h> 5 #include <stdlib.h>
6 #include <string.h> 6 #include <string.h>
7 7
8 #include <map>
9 #include <vector>
10
11 #include "ppapi/c/dev/ppb_video_capture_dev.h"
12 #include "ppapi/c/pp_errors.h" 8 #include "ppapi/c/pp_errors.h"
13 #include "ppapi/c/ppb_opengles2.h" 9 #include "ppapi/c/ppb_opengles2.h"
10 #include "ppapi/cpp/completion_callback.h"
14 #include "ppapi/cpp/dev/buffer_dev.h" 11 #include "ppapi/cpp/dev/buffer_dev.h"
15 #include "ppapi/cpp/dev/device_ref_dev.h" 12 #include "ppapi/cpp/dev/device_ref_dev.h"
16 #include "ppapi/cpp/dev/video_capture_dev.h" 13 #include "ppapi/cpp/dev/var_resource_dev.h"
17 #include "ppapi/cpp/dev/video_capture_client_dev.h" 14 #include "ppapi/cpp/graphics_3d.h"
18 #include "ppapi/cpp/completion_callback.h"
19 #include "ppapi/cpp/graphics_3d_client.h" 15 #include "ppapi/cpp/graphics_3d_client.h"
20 #include "ppapi/cpp/graphics_3d.h"
21 #include "ppapi/cpp/instance.h" 16 #include "ppapi/cpp/instance.h"
17 #include "ppapi/cpp/media_stream_video_track.h"
22 #include "ppapi/cpp/module.h" 18 #include "ppapi/cpp/module.h"
23 #include "ppapi/cpp/rect.h" 19 #include "ppapi/cpp/rect.h"
24 #include "ppapi/cpp/var.h" 20 #include "ppapi/cpp/var.h"
21 #include "ppapi/cpp/video_frame.h"
25 #include "ppapi/lib/gl/include/GLES2/gl2.h" 22 #include "ppapi/lib/gl/include/GLES2/gl2.h"
26 #include "ppapi/utility/completion_callback_factory.h" 23 #include "ppapi/utility/completion_callback_factory.h"
27 24
28 // When compiling natively on Windows, PostMessage can be #define-d to 25 // When compiling natively on Windows, PostMessage can be #define-d to
29 // something else. 26 // something else.
30 #ifdef PostMessage 27 #ifdef PostMessage
31 #undef PostMessage 28 #undef PostMessage
32 #endif 29 #endif
33 30
34 // Assert |context_| isn't holding any GL Errors. Done as a macro instead of a 31 // Assert |context_| isn't holding any GL Errors. Done as a macro instead of a
35 // function to preserve line number information in the failure message. 32 // function to preserve line number information in the failure message.
36 #define AssertNoGLError() \ 33 #define AssertNoGLError() \
37 PP_DCHECK(!gles2_if_->GetError(context_->pp_resource())); 34 PP_DCHECK(!gles2_if_->GetError(context_->pp_resource()));
38 35
39 namespace { 36 namespace {
40 37
41 const char* const kDelimiter = "#__#";
42
43 // This object is the global object representing this plugin library as long 38 // This object is the global object representing this plugin library as long
44 // as it is loaded. 39 // as it is loaded.
45 class VCDemoModule : public pp::Module { 40 class MSVDemoModule : public pp::Module {
46 public: 41 public:
47 VCDemoModule() : pp::Module() {} 42 MSVDemoModule() : pp::Module() {}
48 virtual ~VCDemoModule() {} 43 virtual ~MSVDemoModule() {}
49 44
50 virtual pp::Instance* CreateInstance(PP_Instance instance); 45 virtual pp::Instance* CreateInstance(PP_Instance instance);
51 }; 46 };
52 47
53 class VCDemoInstance : public pp::Instance, 48 class MSVDemoInstance : public pp::Instance,
54 public pp::Graphics3DClient, 49 public pp::Graphics3DClient {
55 public pp::VideoCaptureClient_Dev {
56 public: 50 public:
57 VCDemoInstance(PP_Instance instance, pp::Module* module); 51 MSVDemoInstance(PP_Instance instance, pp::Module* module);
58 virtual ~VCDemoInstance(); 52 virtual ~MSVDemoInstance();
59 53
60 // pp::Instance implementation (see PPP_Instance). 54 // pp::Instance implementation (see PPP_Instance).
61 virtual void DidChangeView(const pp::Rect& position, 55 virtual void DidChangeView(const pp::Rect& position,
62 const pp::Rect& clip_ignored); 56 const pp::Rect& clip_ignored);
63 virtual void HandleMessage(const pp::Var& message_data); 57 virtual void HandleMessage(const pp::Var& message_data);
64 58
65 // pp::Graphics3DClient implementation. 59 // pp::Graphics3DClient implementation.
66 virtual void Graphics3DContextLost() { 60 virtual void Graphics3DContextLost() {
67 InitGL(); 61 InitGL();
68 CreateYUVTextures(); 62 CreateYUVTextures();
69 Render(); 63 Render();
70 } 64 }
71 65
72 virtual void OnDeviceInfo(PP_Resource resource, 66 void OnGetFrame(int32_t result, pp::VideoFrame frame) {
73 const PP_VideoCaptureDeviceInfo_Dev& info, 67 if (result != PP_OK)
74 const std::vector<pp::Buffer_Dev>& buffers) { 68 return;
75 capture_info_ = info; 69 const char* data = static_cast<const char*>(frame.GetDataBuffer());
76 buffers_ = buffers; 70 pp::Size size;
77 CreateYUVTextures(); 71 PP_DCHECK(frame.GetSize(&size));
78 } 72 if (size != frame_size_) {
73 frame_size_ = size;
74 CreateYUVTextures();
75 }
79 76
80 virtual void OnStatus(PP_Resource resource, uint32_t status) { 77 int32_t width = frame_size_.width();
81 } 78 int32_t height = frame_size_.height();
82
83 virtual void OnError(PP_Resource resource, uint32_t error) {
84 }
85
86 virtual void OnBufferReady(PP_Resource resource, uint32_t buffer) {
87 const char* data = static_cast<const char*>(buffers_[buffer].data());
88 int32_t width = capture_info_.width;
89 int32_t height = capture_info_.height;
90 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE0); 79 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE0);
91 gles2_if_->TexSubImage2D( 80 gles2_if_->TexSubImage2D(
92 context_->pp_resource(), GL_TEXTURE_2D, 0, 0, 0, width, height, 81 context_->pp_resource(), GL_TEXTURE_2D, 0, 0, 0, width, height,
93 GL_LUMINANCE, GL_UNSIGNED_BYTE, data); 82 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
94 83
95 data += width * height; 84 data += width * height;
96 width /= 2; 85 width /= 2;
97 height /= 2; 86 height /= 2;
98 87
99 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE1); 88 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE1);
100 gles2_if_->TexSubImage2D( 89 gles2_if_->TexSubImage2D(
101 context_->pp_resource(), GL_TEXTURE_2D, 0, 0, 0, width, height, 90 context_->pp_resource(), GL_TEXTURE_2D, 0, 0, 0, width, height,
102 GL_LUMINANCE, GL_UNSIGNED_BYTE, data); 91 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
103 92
104 data += width * height; 93 data += width * height;
105 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE2); 94 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE2);
106 gles2_if_->TexSubImage2D( 95 gles2_if_->TexSubImage2D(
107 context_->pp_resource(), GL_TEXTURE_2D, 0, 0, 0, width, height, 96 context_->pp_resource(), GL_TEXTURE_2D, 0, 0, 0, width, height,
108 GL_LUMINANCE, GL_UNSIGNED_BYTE, data); 97 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
109 98
110 video_capture_.ReuseBuffer(buffer);
111 if (is_painting_) 99 if (is_painting_)
112 needs_paint_ = true; 100 needs_paint_ = true;
113 else 101 else
114 Render(); 102 Render();
103
104 video_track_.RecycleFrame(frame);
105 video_track_.GetFrame(callback_factory_.NewCallbackWithOutput(
106 &MSVDemoInstance::OnGetFrame));
115 } 107 }
116 108
117 private: 109 private:
118 void Render(); 110 void Render();
119 111
120 // GL-related functions. 112 // GL-related functions.
121 void InitGL(); 113 void InitGL();
122 GLuint CreateTexture(int32_t width, int32_t height, int unit); 114 GLuint CreateTexture(int32_t width, int32_t height, int unit);
123 void CreateGLObjects(); 115 void CreateGLObjects();
124 void CreateShader(GLuint program, GLenum type, const char* source, int size); 116 void CreateShader(GLuint program, GLenum type, const char* source, int size);
125 void PaintFinished(int32_t result); 117 void PaintFinished(int32_t result);
126 void CreateYUVTextures(); 118 void CreateYUVTextures();
127 119
128 void Open(const pp::DeviceRef_Dev& device);
129 void Stop();
130 void Start();
131 void EnumerateDevicesFinished(int32_t result,
132 std::vector<pp::DeviceRef_Dev>& devices);
133 void OpenFinished(int32_t result);
134
135 static void MonitorDeviceChangeCallback(void* user_data,
136 uint32_t device_count,
137 const PP_Resource devices[]);
138
139 pp::Size position_size_; 120 pp::Size position_size_;
140 bool is_painting_; 121 bool is_painting_;
141 bool needs_paint_; 122 bool needs_paint_;
142 GLuint texture_y_; 123 GLuint texture_y_;
143 GLuint texture_u_; 124 GLuint texture_u_;
144 GLuint texture_v_; 125 GLuint texture_v_;
145 pp::VideoCapture_Dev video_capture_; 126 pp::MediaStreamVideoTrack video_track_;
146 PP_VideoCaptureDeviceInfo_Dev capture_info_; 127 pp::CompletionCallbackFactory<MSVDemoInstance> callback_factory_;
147 std::vector<pp::Buffer_Dev> buffers_;
148 pp::CompletionCallbackFactory<VCDemoInstance> callback_factory_;
149 128
150 // Unowned pointers. 129 // Unowned pointers.
151 const struct PPB_OpenGLES2* gles2_if_; 130 const struct PPB_OpenGLES2* gles2_if_;
152 131
153 // Owned data. 132 // Owned data.
154 pp::Graphics3D* context_; 133 pp::Graphics3D* context_;
155 134
156 std::vector<pp::DeviceRef_Dev> enumerate_devices_; 135 pp::Size frame_size_;
157 std::vector<pp::DeviceRef_Dev> monitor_devices_;
158 }; 136 };
159 137
160 VCDemoInstance::VCDemoInstance(PP_Instance instance, pp::Module* module) 138 MSVDemoInstance::MSVDemoInstance(PP_Instance instance, pp::Module* module)
161 : pp::Instance(instance), 139 : pp::Instance(instance),
162 pp::Graphics3DClient(this), 140 pp::Graphics3DClient(this),
163 pp::VideoCaptureClient_Dev(this),
164 is_painting_(false), 141 is_painting_(false),
165 needs_paint_(false), 142 needs_paint_(false),
166 texture_y_(0), 143 texture_y_(0),
167 texture_u_(0), 144 texture_u_(0),
168 texture_v_(0), 145 texture_v_(0),
169 video_capture_(this),
170 callback_factory_(this), 146 callback_factory_(this),
171 context_(NULL) { 147 context_(NULL) {
172 gles2_if_ = static_cast<const struct PPB_OpenGLES2*>( 148 gles2_if_ = static_cast<const struct PPB_OpenGLES2*>(
173 module->GetBrowserInterface(PPB_OPENGLES2_INTERFACE)); 149 module->GetBrowserInterface(PPB_OPENGLES2_INTERFACE));
174 PP_DCHECK(gles2_if_); 150 PP_DCHECK(gles2_if_);
175
176 capture_info_.width = 320;
177 capture_info_.height = 240;
178 capture_info_.frames_per_second = 30;
179 } 151 }
180 152
181 VCDemoInstance::~VCDemoInstance() { 153 MSVDemoInstance::~MSVDemoInstance() {
182 video_capture_.MonitorDeviceChange(NULL, NULL);
183 delete context_; 154 delete context_;
184 } 155 }
185 156
186 void VCDemoInstance::DidChangeView( 157 void MSVDemoInstance::DidChangeView(
187 const pp::Rect& position, const pp::Rect& clip_ignored) { 158 const pp::Rect& position, const pp::Rect& clip_ignored) {
188 if (position.width() == 0 || position.height() == 0) 159 if (position.width() == 0 || position.height() == 0)
189 return; 160 return;
190 if (position.size() == position_size_) 161 if (position.size() == position_size_)
191 return; 162 return;
192 163
193 position_size_ = position.size(); 164 position_size_ = position.size();
194 165
195 // Initialize graphics. 166 // Initialize graphics.
196 InitGL(); 167 InitGL();
197
198 Render(); 168 Render();
199 } 169 }
200 170
201 void VCDemoInstance::HandleMessage(const pp::Var& message_data) { 171 void MSVDemoInstance::HandleMessage(const pp::Var& var_message) {
202 if (message_data.is_string()) { 172 if (!var_message.is_dictionary())
203 std::string event = message_data.AsString(); 173 return;
204 if (event == "PageInitialized") { 174 pp::VarDictionary var_dictionary_message(var_message);
205 int32_t result = video_capture_.MonitorDeviceChange( 175 pp::Var var_track = var_dictionary_message.Get("track");
206 &VCDemoInstance::MonitorDeviceChangeCallback, this); 176 if (!var_track.is_resource())
207 if (result != PP_OK) 177 return;
208 PostMessage(pp::Var("MonitorDeviceChangeFailed"));
209 178
210 pp::CompletionCallbackWithOutput<std::vector<pp::DeviceRef_Dev> > 179 pp::Resource resource_track = pp::VarResource_Dev(var_track).AsResource();
211 callback = callback_factory_.NewCallbackWithOutput( 180
212 &VCDemoInstance::EnumerateDevicesFinished); 181 video_track_ = pp::MediaStreamVideoTrack(resource_track);
213 result = video_capture_.EnumerateDevices(callback); 182
214 if (result != PP_OK_COMPLETIONPENDING) 183 video_track_.GetFrame(callback_factory_.NewCallbackWithOutput(
215 PostMessage(pp::Var("EnumerationFailed")); 184 &MSVDemoInstance::OnGetFrame));
216 } else if (event == "UseDefault") {
217 Open(pp::DeviceRef_Dev());
218 } else if (event == "Stop") {
219 Stop();
220 } else if (event == "Start") {
221 Start();
222 } else if (event.find("Monitor:") == 0) {
223 std::string index_str = event.substr(strlen("Monitor:"));
224 int index = atoi(index_str.c_str());
225 if (index >= 0 && index < static_cast<int>(monitor_devices_.size()))
226 Open(monitor_devices_[index]);
227 else
228 PP_NOTREACHED();
229 } else if (event.find("Enumerate:") == 0) {
230 std::string index_str = event.substr(strlen("Enumerate:"));
231 int index = atoi(index_str.c_str());
232 if (index >= 0 && index < static_cast<int>(enumerate_devices_.size()))
233 Open(enumerate_devices_[index]);
234 else
235 PP_NOTREACHED();
236 }
237 }
238 } 185 }
239 186
240 void VCDemoInstance::InitGL() { 187 void MSVDemoInstance::InitGL() {
241 PP_DCHECK(position_size_.width() && position_size_.height()); 188 PP_DCHECK(position_size_.width() && position_size_.height());
242 is_painting_ = false; 189 is_painting_ = false;
243 190
244 delete context_; 191 delete context_;
245 int32_t attributes[] = { 192 int32_t attributes[] = {
246 PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 0, 193 PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 0,
247 PP_GRAPHICS3DATTRIB_BLUE_SIZE, 8, 194 PP_GRAPHICS3DATTRIB_BLUE_SIZE, 8,
248 PP_GRAPHICS3DATTRIB_GREEN_SIZE, 8, 195 PP_GRAPHICS3DATTRIB_GREEN_SIZE, 8,
249 PP_GRAPHICS3DATTRIB_RED_SIZE, 8, 196 PP_GRAPHICS3DATTRIB_RED_SIZE, 8,
250 PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 0, 197 PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 0,
(...skipping 12 matching lines...) Expand all
263 gles2_if_->Clear(context_->pp_resource(), GL_COLOR_BUFFER_BIT); 210 gles2_if_->Clear(context_->pp_resource(), GL_COLOR_BUFFER_BIT);
264 gles2_if_->Viewport(context_->pp_resource(), 0, 0, 211 gles2_if_->Viewport(context_->pp_resource(), 0, 0,
265 position_size_.width(), position_size_.height()); 212 position_size_.width(), position_size_.height());
266 213
267 BindGraphics(*context_); 214 BindGraphics(*context_);
268 AssertNoGLError(); 215 AssertNoGLError();
269 216
270 CreateGLObjects(); 217 CreateGLObjects();
271 } 218 }
272 219
273 void VCDemoInstance::Render() { 220 void MSVDemoInstance::Render() {
274 PP_DCHECK(!is_painting_); 221 PP_DCHECK(!is_painting_);
275 is_painting_ = true; 222 is_painting_ = true;
276 needs_paint_ = false; 223 needs_paint_ = false;
277 if (texture_y_) { 224 if (texture_y_) {
278 gles2_if_->DrawArrays(context_->pp_resource(), GL_TRIANGLE_STRIP, 0, 4); 225 gles2_if_->DrawArrays(context_->pp_resource(), GL_TRIANGLE_STRIP, 0, 4);
279 } else { 226 } else {
280 gles2_if_->Clear(context_->pp_resource(), GL_COLOR_BUFFER_BIT); 227 gles2_if_->Clear(context_->pp_resource(), GL_COLOR_BUFFER_BIT);
281 } 228 }
282 pp::CompletionCallback cb = callback_factory_.NewCallback( 229 pp::CompletionCallback cb = callback_factory_.NewCallback(
283 &VCDemoInstance::PaintFinished); 230 &MSVDemoInstance::PaintFinished);
284 context_->SwapBuffers(cb); 231 context_->SwapBuffers(cb);
285 } 232 }
286 233
287 void VCDemoInstance::PaintFinished(int32_t result) { 234 void MSVDemoInstance::PaintFinished(int32_t result) {
288 is_painting_ = false; 235 is_painting_ = false;
289 if (needs_paint_) 236 if (needs_paint_)
290 Render(); 237 Render();
291 } 238 }
292 239
293 GLuint VCDemoInstance::CreateTexture(int32_t width, int32_t height, int unit) { 240 GLuint MSVDemoInstance::CreateTexture(int32_t width, int32_t height, int unit) {
294 GLuint texture_id; 241 GLuint texture_id;
295 gles2_if_->GenTextures(context_->pp_resource(), 1, &texture_id); 242 gles2_if_->GenTextures(context_->pp_resource(), 1, &texture_id);
296 AssertNoGLError(); 243 AssertNoGLError();
297 // Assign parameters. 244 // Assign parameters.
298 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE0 + unit); 245 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE0 + unit);
299 gles2_if_->BindTexture(context_->pp_resource(), GL_TEXTURE_2D, texture_id); 246 gles2_if_->BindTexture(context_->pp_resource(), GL_TEXTURE_2D, texture_id);
300 gles2_if_->TexParameteri( 247 gles2_if_->TexParameteri(
301 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 248 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
302 GL_NEAREST); 249 GL_NEAREST);
303 gles2_if_->TexParameteri( 250 gles2_if_->TexParameteri(
304 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 251 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
305 GL_NEAREST); 252 GL_NEAREST);
306 gles2_if_->TexParameterf( 253 gles2_if_->TexParameterf(
307 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, 254 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
308 GL_CLAMP_TO_EDGE); 255 GL_CLAMP_TO_EDGE);
309 gles2_if_->TexParameterf( 256 gles2_if_->TexParameterf(
310 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, 257 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
311 GL_CLAMP_TO_EDGE); 258 GL_CLAMP_TO_EDGE);
312 259
313 // Allocate texture. 260 // Allocate texture.
314 gles2_if_->TexImage2D( 261 gles2_if_->TexImage2D(
315 context_->pp_resource(), GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, 262 context_->pp_resource(), GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0,
316 GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL); 263 GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL);
317 AssertNoGLError(); 264 AssertNoGLError();
318 return texture_id; 265 return texture_id;
319 } 266 }
320 267
321 void VCDemoInstance::CreateGLObjects() { 268 void MSVDemoInstance::CreateGLObjects() {
322 // Code and constants for shader. 269 // Code and constants for shader.
323 static const char kVertexShader[] = 270 static const char kVertexShader[] =
324 "varying vec2 v_texCoord; \n" 271 "varying vec2 v_texCoord; \n"
325 "attribute vec4 a_position; \n" 272 "attribute vec4 a_position; \n"
326 "attribute vec2 a_texCoord; \n" 273 "attribute vec2 a_texCoord; \n"
327 "void main() \n" 274 "void main() \n"
328 "{ \n" 275 "{ \n"
329 " v_texCoord = a_texCoord; \n" 276 " v_texCoord = a_texCoord; \n"
330 " gl_Position = a_position; \n" 277 " gl_Position = a_position; \n"
331 "}"; 278 "}";
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 gles2_if_->EnableVertexAttribArray(context, pos_location); 343 gles2_if_->EnableVertexAttribArray(context, pos_location);
397 gles2_if_->VertexAttribPointer(context, pos_location, 2, 344 gles2_if_->VertexAttribPointer(context, pos_location, 2,
398 GL_FLOAT, GL_FALSE, 0, 0); 345 GL_FLOAT, GL_FALSE, 0, 0);
399 gles2_if_->EnableVertexAttribArray(context, tc_location); 346 gles2_if_->EnableVertexAttribArray(context, tc_location);
400 gles2_if_->VertexAttribPointer( 347 gles2_if_->VertexAttribPointer(
401 context, tc_location, 2, GL_FLOAT, GL_FALSE, 0, 348 context, tc_location, 2, GL_FLOAT, GL_FALSE, 0,
402 static_cast<float*>(0) + 8); // Skip position coordinates. 349 static_cast<float*>(0) + 8); // Skip position coordinates.
403 AssertNoGLError(); 350 AssertNoGLError();
404 } 351 }
405 352
406 void VCDemoInstance::CreateShader( 353 void MSVDemoInstance::CreateShader(
407 GLuint program, GLenum type, const char* source, int size) { 354 GLuint program, GLenum type, const char* source, int size) {
408 PP_Resource context = context_->pp_resource(); 355 PP_Resource context = context_->pp_resource();
409 GLuint shader = gles2_if_->CreateShader(context, type); 356 GLuint shader = gles2_if_->CreateShader(context, type);
410 gles2_if_->ShaderSource(context, shader, 1, &source, &size); 357 gles2_if_->ShaderSource(context, shader, 1, &source, &size);
411 gles2_if_->CompileShader(context, shader); 358 gles2_if_->CompileShader(context, shader);
412 gles2_if_->AttachShader(context, program, shader); 359 gles2_if_->AttachShader(context, program, shader);
413 gles2_if_->DeleteShader(context, shader); 360 gles2_if_->DeleteShader(context, shader);
414 } 361 }
415 362
416 void VCDemoInstance::CreateYUVTextures() { 363 void MSVDemoInstance::CreateYUVTextures() {
417 int32_t width = capture_info_.width; 364 int32_t width = frame_size_.width();
418 int32_t height = capture_info_.height; 365 int32_t height = frame_size_.height();
366 if (width == 0 || height == 0)
367 return;
419 texture_y_ = CreateTexture(width, height, 0); 368 texture_y_ = CreateTexture(width, height, 0);
420 369
421 width /= 2; 370 width /= 2;
422 height /= 2; 371 height /= 2;
423 texture_u_ = CreateTexture(width, height, 1); 372 texture_u_ = CreateTexture(width, height, 1);
424 texture_v_ = CreateTexture(width, height, 2); 373 texture_v_ = CreateTexture(width, height, 2);
425 } 374 }
426 375
427 void VCDemoInstance::Open(const pp::DeviceRef_Dev& device) { 376 pp::Instance* MSVDemoModule::CreateInstance(PP_Instance instance) {
428 pp::CompletionCallback callback = callback_factory_.NewCallback( 377 return new MSVDemoInstance(instance, this);
429 &VCDemoInstance::OpenFinished);
430 int32_t result = video_capture_.Open(device, capture_info_, 4, callback);
431 if (result != PP_OK_COMPLETIONPENDING)
432 PostMessage(pp::Var("OpenFailed"));
433 }
434
435 void VCDemoInstance::Stop() {
436 if (video_capture_.StopCapture() != PP_OK)
437 PostMessage(pp::Var("StopFailed"));
438 }
439
440 void VCDemoInstance::Start() {
441 if (video_capture_.StartCapture() != PP_OK)
442 PostMessage(pp::Var("StartFailed"));
443 }
444
445 void VCDemoInstance::EnumerateDevicesFinished(
446 int32_t result,
447 std::vector<pp::DeviceRef_Dev>& devices) {
448 if (result == PP_OK) {
449 enumerate_devices_.swap(devices);
450 std::string device_names = "Enumerate:";
451 for (size_t index = 0; index < enumerate_devices_.size(); ++index) {
452 pp::Var name = enumerate_devices_[index].GetName();
453 PP_DCHECK(name.is_string());
454
455 if (index != 0)
456 device_names += kDelimiter;
457 device_names += name.AsString();
458 }
459 PostMessage(pp::Var(device_names));
460 } else {
461 PostMessage(pp::Var("EnumerationFailed"));
462 }
463 }
464
465 void VCDemoInstance::OpenFinished(int32_t result) {
466 if (result == PP_OK)
467 Start();
468 else
469 PostMessage(pp::Var("OpenFailed"));
470 }
471
472 // static
473 void VCDemoInstance::MonitorDeviceChangeCallback(void* user_data,
474 uint32_t device_count,
475 const PP_Resource devices[]) {
476 VCDemoInstance* thiz = static_cast<VCDemoInstance*>(user_data);
477
478 std::string device_names = "Monitor:";
479 thiz->monitor_devices_.clear();
480 thiz->monitor_devices_.reserve(device_count);
481 for (size_t index = 0; index < device_count; ++index) {
482 thiz->monitor_devices_.push_back(pp::DeviceRef_Dev(devices[index]));
483 pp::Var name = thiz->monitor_devices_.back().GetName();
484 PP_DCHECK(name.is_string());
485
486 if (index != 0)
487 device_names += kDelimiter;
488 device_names += name.AsString();
489 }
490 thiz->PostMessage(pp::Var(device_names));
491 }
492
493 pp::Instance* VCDemoModule::CreateInstance(PP_Instance instance) {
494 return new VCDemoInstance(instance, this);
495 } 378 }
496 379
497 } // anonymous namespace 380 } // anonymous namespace
498 381
499 namespace pp { 382 namespace pp {
500 // Factory function for your specialization of the Module object. 383 // Factory function for your specialization of the Module object.
501 Module* CreateModule() { 384 Module* CreateModule() {
502 return new VCDemoModule(); 385 return new MSVDemoModule();
503 } 386 }
504 } // namespace pp 387 } // namespace pp
OLDNEW
« no previous file with comments | « content/renderer/pepper/resource_converter.cc ('k') | ppapi/examples/media_stream_video/media_stream_video.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698