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

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

Issue 9234064: Implement device enumeration for PPB_VideoCapture_Dev. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sync & changes in response to Brett's suggestions Created 8 years, 10 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 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 <assert.h> 5 #include <assert.h>
6 #include <string.h> 6 #include <string.h>
7 7
8 #include <map> 8 #include <map>
9 #include <vector> 9 #include <vector>
10 10
11 #include "ppapi/c/dev/ppb_video_capture_dev.h"
11 #include "ppapi/c/pp_errors.h" 12 #include "ppapi/c/pp_errors.h"
12 #include "ppapi/c/ppb_opengles2.h" 13 #include "ppapi/c/ppb_opengles2.h"
13 #include "ppapi/cpp/dev/buffer_dev.h" 14 #include "ppapi/cpp/dev/buffer_dev.h"
15 #include "ppapi/cpp/dev/device_ref_dev.h"
14 #include "ppapi/cpp/dev/video_capture_dev.h" 16 #include "ppapi/cpp/dev/video_capture_dev.h"
15 #include "ppapi/cpp/dev/video_capture_client_dev.h" 17 #include "ppapi/cpp/dev/video_capture_client_dev.h"
16 #include "ppapi/cpp/completion_callback.h" 18 #include "ppapi/cpp/completion_callback.h"
17 #include "ppapi/cpp/graphics_3d_client.h" 19 #include "ppapi/cpp/graphics_3d_client.h"
18 #include "ppapi/cpp/graphics_3d.h" 20 #include "ppapi/cpp/graphics_3d.h"
19 #include "ppapi/cpp/instance.h" 21 #include "ppapi/cpp/instance.h"
20 #include "ppapi/cpp/module.h" 22 #include "ppapi/cpp/module.h"
21 #include "ppapi/cpp/rect.h" 23 #include "ppapi/cpp/rect.h"
24 #include "ppapi/cpp/var.h"
22 #include "ppapi/lib/gl/include/GLES2/gl2.h" 25 #include "ppapi/lib/gl/include/GLES2/gl2.h"
23 #include "ppapi/utility/completion_callback_factory.h" 26 #include "ppapi/utility/completion_callback_factory.h"
24 27
25 // Assert |context_| isn't holding any GL Errors. Done as a macro instead of a 28 // Assert |context_| isn't holding any GL Errors. Done as a macro instead of a
26 // function to preserve line number information in the failure message. 29 // function to preserve line number information in the failure message.
27 #define assertNoGLError() \ 30 #define assertNoGLError() \
28 assert(!gles2_if_->GetError(context_->pp_resource())); 31 assert(!gles2_if_->GetError(context_->pp_resource()));
29 32
30 namespace { 33 namespace {
31 34
(...skipping 10 matching lines...) Expand all
42 class VCDemoInstance : public pp::Instance, 45 class VCDemoInstance : public pp::Instance,
43 public pp::Graphics3DClient, 46 public pp::Graphics3DClient,
44 public pp::VideoCaptureClient_Dev { 47 public pp::VideoCaptureClient_Dev {
45 public: 48 public:
46 VCDemoInstance(PP_Instance instance, pp::Module* module); 49 VCDemoInstance(PP_Instance instance, pp::Module* module);
47 virtual ~VCDemoInstance(); 50 virtual ~VCDemoInstance();
48 51
49 // pp::Instance implementation (see PPP_Instance). 52 // pp::Instance implementation (see PPP_Instance).
50 virtual void DidChangeView(const pp::Rect& position, 53 virtual void DidChangeView(const pp::Rect& position,
51 const pp::Rect& clip_ignored); 54 const pp::Rect& clip_ignored);
55 virtual void HandleMessage(const pp::Var& message_data);
52 56
53 // pp::Graphics3DClient implementation. 57 // pp::Graphics3DClient implementation.
54 virtual void Graphics3DContextLost() { 58 virtual void Graphics3DContextLost() {
55 InitGL(); 59 InitGL();
56 CreateYUVTextures(); 60 CreateYUVTextures();
57 Render(); 61 Render();
58 } 62 }
59 63
60 virtual void OnDeviceInfo(PP_Resource resource, 64 virtual void OnDeviceInfo(PP_Resource resource,
61 const PP_VideoCaptureDeviceInfo_Dev& info, 65 const PP_VideoCaptureDeviceInfo_Dev& info,
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 needs_paint_ = true; 104 needs_paint_ = true;
101 else 105 else
102 Render(); 106 Render();
103 } 107 }
104 108
105 private: 109 private:
106 void Render(); 110 void Render();
107 111
108 // GL-related functions. 112 // GL-related functions.
109 void InitGL(); 113 void InitGL();
110 void InitializeCapture();
111 GLuint CreateTexture(int32_t width, int32_t height, int unit); 114 GLuint CreateTexture(int32_t width, int32_t height, int unit);
112 void CreateGLObjects(); 115 void CreateGLObjects();
113 void CreateShader(GLuint program, GLenum type, const char* source, int size); 116 void CreateShader(GLuint program, GLenum type, const char* source, int size);
114 void PaintFinished(int32_t result); 117 void PaintFinished(int32_t result);
115 void CreateYUVTextures(); 118 void CreateYUVTextures();
116 119
120 void Open(const pp::DeviceRef_Dev& device);
121 void EnumerateDevicesFinished(int32_t result);
122 void OpenFinished(int32_t result);
123
117 pp::Size position_size_; 124 pp::Size position_size_;
118 bool is_painting_; 125 bool is_painting_;
119 bool needs_paint_; 126 bool needs_paint_;
120 GLuint texture_y_; 127 GLuint texture_y_;
121 GLuint texture_u_; 128 GLuint texture_u_;
122 GLuint texture_v_; 129 GLuint texture_v_;
123 pp::VideoCapture_Dev video_capture_; 130 pp::VideoCapture_Dev video_capture_;
124 PP_VideoCaptureDeviceInfo_Dev capture_info_; 131 PP_VideoCaptureDeviceInfo_Dev capture_info_;
125 std::vector<pp::Buffer_Dev> buffers_; 132 std::vector<pp::Buffer_Dev> buffers_;
126 pp::CompletionCallbackFactory<VCDemoInstance> callback_factory_; 133 pp::CompletionCallbackFactory<VCDemoInstance> callback_factory_;
127 134
128 // Unowned pointers. 135 // Unowned pointers.
129 const struct PPB_OpenGLES2* gles2_if_; 136 const struct PPB_OpenGLES2* gles2_if_;
130 137
131 // Owned data. 138 // Owned data.
132 pp::Graphics3D* context_; 139 pp::Graphics3D* context_;
140
141 bool page_initialized_;
piman 2012/02/14 07:21:16 nit: I don't think this is used anywhere.
yzshen1 2012/02/15 21:17:54 Done.
142 std::vector<pp::DeviceRef_Dev> devices_;
133 }; 143 };
134 144
135 VCDemoInstance::VCDemoInstance(PP_Instance instance, pp::Module* module) 145 VCDemoInstance::VCDemoInstance(PP_Instance instance, pp::Module* module)
136 : pp::Instance(instance), 146 : pp::Instance(instance),
137 pp::Graphics3DClient(this), 147 pp::Graphics3DClient(this),
138 pp::VideoCaptureClient_Dev(this), 148 pp::VideoCaptureClient_Dev(this),
139 is_painting_(false), 149 is_painting_(false),
140 needs_paint_(false), 150 needs_paint_(false),
141 texture_y_(0), 151 texture_y_(0),
142 texture_u_(0), 152 texture_u_(0),
143 texture_v_(0), 153 texture_v_(0),
144 video_capture_(*this), 154 video_capture_(*this),
145 callback_factory_(this), 155 callback_factory_(this),
146 context_(NULL) { 156 context_(NULL),
157 page_initialized_(false) {
147 gles2_if_ = static_cast<const struct PPB_OpenGLES2*>( 158 gles2_if_ = static_cast<const struct PPB_OpenGLES2*>(
148 module->GetBrowserInterface(PPB_OPENGLES2_INTERFACE)); 159 module->GetBrowserInterface(PPB_OPENGLES2_INTERFACE));
149 assert(gles2_if_); 160 assert(gles2_if_);
150 InitializeCapture(); 161
162 capture_info_.width = 320;
163 capture_info_.height = 240;
164 capture_info_.frames_per_second = 30;
151 } 165 }
152 166
153 VCDemoInstance::~VCDemoInstance() { 167 VCDemoInstance::~VCDemoInstance() {
154 delete context_; 168 delete context_;
155 } 169 }
156 170
157 void VCDemoInstance::DidChangeView( 171 void VCDemoInstance::DidChangeView(
158 const pp::Rect& position, const pp::Rect& clip_ignored) { 172 const pp::Rect& position, const pp::Rect& clip_ignored) {
159 if (position.width() == 0 || position.height() == 0) 173 if (position.width() == 0 || position.height() == 0)
160 return; 174 return;
161 if (position.size() == position_size_) 175 if (position.size() == position_size_)
162 return; 176 return;
163 177
164 position_size_ = position.size(); 178 position_size_ = position.size();
165 179
166 // Initialize graphics. 180 // Initialize graphics.
167 InitGL(); 181 InitGL();
168 182
169 Render(); 183 Render();
170 } 184 }
171 185
172 void VCDemoInstance::InitializeCapture() { 186 void VCDemoInstance::HandleMessage(const pp::Var& message_data) {
173 capture_info_.width = 320; 187 if (message_data.is_string()) {
174 capture_info_.height = 240; 188 std::string event = message_data.AsString();
175 capture_info_.frames_per_second = 30; 189 if (event == "PageInitialized") {
176 190 pp::CompletionCallback callback = callback_factory_.NewCallback(
177 video_capture_.StartCapture(capture_info_, 4); 191 &VCDemoInstance::EnumerateDevicesFinished);
192 video_capture_.EnumerateDevices(&devices_, callback);
193 } else if (event == "UseDefault") {
194 Open(pp::DeviceRef_Dev());
195 } else if (event == "UseDefault(v0.1)") {
196 const PPB_VideoCapture_Dev_0_1* video_capture_0_1 =
197 static_cast<const PPB_VideoCapture_Dev_0_1*>(
198 pp::Module::Get()->GetBrowserInterface(
199 PPB_VIDEOCAPTURE_DEV_INTERFACE_0_1));
200 video_capture_0_1->StartCapture(video_capture_.pp_resource(),
201 &capture_info_, 4);
202 } else if (event == "Stop") {
203 video_capture_.StopCapture();
204 }
205 } else if (message_data.is_number()) {
206 int index = message_data.AsInt();
207 if (index >= 0 && index < static_cast<int>(devices_.size())) {
208 Open(devices_[index]);
209 } else {
210 assert(false);
211 }
212 }
178 } 213 }
179 214
180 void VCDemoInstance::InitGL() { 215 void VCDemoInstance::InitGL() {
181 assert(position_size_.width() && position_size_.height()); 216 assert(position_size_.width() && position_size_.height());
182 is_painting_ = false; 217 is_painting_ = false;
183 218
184 delete context_; 219 delete context_;
185 int32_t attributes[] = { 220 int32_t attributes[] = {
186 PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 0, 221 PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 0,
187 PP_GRAPHICS3DATTRIB_BLUE_SIZE, 8, 222 PP_GRAPHICS3DATTRIB_BLUE_SIZE, 8,
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 int32_t width = capture_info_.width; 392 int32_t width = capture_info_.width;
358 int32_t height = capture_info_.height; 393 int32_t height = capture_info_.height;
359 texture_y_ = CreateTexture(width, height, 0); 394 texture_y_ = CreateTexture(width, height, 0);
360 395
361 width /= 2; 396 width /= 2;
362 height /= 2; 397 height /= 2;
363 texture_u_ = CreateTexture(width, height, 1); 398 texture_u_ = CreateTexture(width, height, 1);
364 texture_v_ = CreateTexture(width, height, 2); 399 texture_v_ = CreateTexture(width, height, 2);
365 } 400 }
366 401
402 void VCDemoInstance::Open(const pp::DeviceRef_Dev& device) {
403 pp::CompletionCallback callback = callback_factory_.NewCallback(
404 &VCDemoInstance::OpenFinished);
405 video_capture_.Open(device, capture_info_, 4, callback);
406 }
407
408 void VCDemoInstance::EnumerateDevicesFinished(int32_t result) {
409 static const char* const kDelimiter = "#__#";
410
411 if (result == PP_OK) {
412 std::string device_names;
413 for (size_t index = 0; index < devices_.size(); ++index) {
414 pp::Var name = devices_[index].GetName();
415 assert(name.is_string());
416
417 if (index != 0)
418 device_names += kDelimiter;
419 device_names += name.AsString();
420 }
421 PostMessage(pp::Var(device_names));
422 } else {
423 PostMessage(pp::Var("EnumerationFailed"));
424 }
425 }
426
427 void VCDemoInstance::OpenFinished(int32_t result) {
428 if (result == PP_OK) {
429 video_capture_.StartCapture();
430 } else {
431 PostMessage(pp::Var("OpenFailed"));
432 }
433 }
434
367 pp::Instance* VCDemoModule::CreateInstance(PP_Instance instance) { 435 pp::Instance* VCDemoModule::CreateInstance(PP_Instance instance) {
368 return new VCDemoInstance(instance, this); 436 return new VCDemoInstance(instance, this);
369 } 437 }
370 438
371 } // anonymous namespace 439 } // anonymous namespace
372 440
373 namespace pp { 441 namespace pp {
374 // Factory function for your specialization of the Module object. 442 // Factory function for your specialization of the Module object.
375 Module* CreateModule() { 443 Module* CreateModule() {
376 return new VCDemoModule(); 444 return new VCDemoModule();
377 } 445 }
378 } // namespace pp 446 } // namespace pp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698