| OLD | NEW |
| 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 Loading... |
| 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 Loading... |
| 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 std::vector<pp::DeviceRef_Dev> devices_; |
| 133 }; | 142 }; |
| 134 | 143 |
| 135 VCDemoInstance::VCDemoInstance(PP_Instance instance, pp::Module* module) | 144 VCDemoInstance::VCDemoInstance(PP_Instance instance, pp::Module* module) |
| 136 : pp::Instance(instance), | 145 : pp::Instance(instance), |
| 137 pp::Graphics3DClient(this), | 146 pp::Graphics3DClient(this), |
| 138 pp::VideoCaptureClient_Dev(this), | 147 pp::VideoCaptureClient_Dev(this), |
| 139 is_painting_(false), | 148 is_painting_(false), |
| 140 needs_paint_(false), | 149 needs_paint_(false), |
| 141 texture_y_(0), | 150 texture_y_(0), |
| 142 texture_u_(0), | 151 texture_u_(0), |
| 143 texture_v_(0), | 152 texture_v_(0), |
| 144 video_capture_(*this), | 153 video_capture_(*this), |
| 145 callback_factory_(this), | 154 callback_factory_(this), |
| 146 context_(NULL) { | 155 context_(NULL) { |
| 147 gles2_if_ = static_cast<const struct PPB_OpenGLES2*>( | 156 gles2_if_ = static_cast<const struct PPB_OpenGLES2*>( |
| 148 module->GetBrowserInterface(PPB_OPENGLES2_INTERFACE)); | 157 module->GetBrowserInterface(PPB_OPENGLES2_INTERFACE)); |
| 149 assert(gles2_if_); | 158 assert(gles2_if_); |
| 150 InitializeCapture(); | 159 |
| 160 capture_info_.width = 320; |
| 161 capture_info_.height = 240; |
| 162 capture_info_.frames_per_second = 30; |
| 151 } | 163 } |
| 152 | 164 |
| 153 VCDemoInstance::~VCDemoInstance() { | 165 VCDemoInstance::~VCDemoInstance() { |
| 154 delete context_; | 166 delete context_; |
| 155 } | 167 } |
| 156 | 168 |
| 157 void VCDemoInstance::DidChangeView( | 169 void VCDemoInstance::DidChangeView( |
| 158 const pp::Rect& position, const pp::Rect& clip_ignored) { | 170 const pp::Rect& position, const pp::Rect& clip_ignored) { |
| 159 if (position.width() == 0 || position.height() == 0) | 171 if (position.width() == 0 || position.height() == 0) |
| 160 return; | 172 return; |
| 161 if (position.size() == position_size_) | 173 if (position.size() == position_size_) |
| 162 return; | 174 return; |
| 163 | 175 |
| 164 position_size_ = position.size(); | 176 position_size_ = position.size(); |
| 165 | 177 |
| 166 // Initialize graphics. | 178 // Initialize graphics. |
| 167 InitGL(); | 179 InitGL(); |
| 168 | 180 |
| 169 Render(); | 181 Render(); |
| 170 } | 182 } |
| 171 | 183 |
| 172 void VCDemoInstance::InitializeCapture() { | 184 void VCDemoInstance::HandleMessage(const pp::Var& message_data) { |
| 173 capture_info_.width = 320; | 185 if (message_data.is_string()) { |
| 174 capture_info_.height = 240; | 186 std::string event = message_data.AsString(); |
| 175 capture_info_.frames_per_second = 30; | 187 if (event == "PageInitialized") { |
| 176 | 188 pp::CompletionCallback callback = callback_factory_.NewCallback( |
| 177 video_capture_.StartCapture(capture_info_, 4); | 189 &VCDemoInstance::EnumerateDevicesFinished); |
| 190 video_capture_.EnumerateDevices(&devices_, callback); |
| 191 } else if (event == "UseDefault") { |
| 192 Open(pp::DeviceRef_Dev()); |
| 193 } else if (event == "UseDefault(v0.1)") { |
| 194 const PPB_VideoCapture_Dev_0_1* video_capture_0_1 = |
| 195 static_cast<const PPB_VideoCapture_Dev_0_1*>( |
| 196 pp::Module::Get()->GetBrowserInterface( |
| 197 PPB_VIDEOCAPTURE_DEV_INTERFACE_0_1)); |
| 198 video_capture_0_1->StartCapture(video_capture_.pp_resource(), |
| 199 &capture_info_, 4); |
| 200 } else if (event == "Stop") { |
| 201 video_capture_.StopCapture(); |
| 202 } |
| 203 } else if (message_data.is_number()) { |
| 204 int index = message_data.AsInt(); |
| 205 if (index >= 0 && index < static_cast<int>(devices_.size())) { |
| 206 Open(devices_[index]); |
| 207 } else { |
| 208 assert(false); |
| 209 } |
| 210 } |
| 178 } | 211 } |
| 179 | 212 |
| 180 void VCDemoInstance::InitGL() { | 213 void VCDemoInstance::InitGL() { |
| 181 assert(position_size_.width() && position_size_.height()); | 214 assert(position_size_.width() && position_size_.height()); |
| 182 is_painting_ = false; | 215 is_painting_ = false; |
| 183 | 216 |
| 184 delete context_; | 217 delete context_; |
| 185 int32_t attributes[] = { | 218 int32_t attributes[] = { |
| 186 PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 0, | 219 PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 0, |
| 187 PP_GRAPHICS3DATTRIB_BLUE_SIZE, 8, | 220 PP_GRAPHICS3DATTRIB_BLUE_SIZE, 8, |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 int32_t width = capture_info_.width; | 390 int32_t width = capture_info_.width; |
| 358 int32_t height = capture_info_.height; | 391 int32_t height = capture_info_.height; |
| 359 texture_y_ = CreateTexture(width, height, 0); | 392 texture_y_ = CreateTexture(width, height, 0); |
| 360 | 393 |
| 361 width /= 2; | 394 width /= 2; |
| 362 height /= 2; | 395 height /= 2; |
| 363 texture_u_ = CreateTexture(width, height, 1); | 396 texture_u_ = CreateTexture(width, height, 1); |
| 364 texture_v_ = CreateTexture(width, height, 2); | 397 texture_v_ = CreateTexture(width, height, 2); |
| 365 } | 398 } |
| 366 | 399 |
| 400 void VCDemoInstance::Open(const pp::DeviceRef_Dev& device) { |
| 401 pp::CompletionCallback callback = callback_factory_.NewCallback( |
| 402 &VCDemoInstance::OpenFinished); |
| 403 video_capture_.Open(device, capture_info_, 4, callback); |
| 404 } |
| 405 |
| 406 void VCDemoInstance::EnumerateDevicesFinished(int32_t result) { |
| 407 static const char* const kDelimiter = "#__#"; |
| 408 |
| 409 if (result == PP_OK) { |
| 410 std::string device_names; |
| 411 for (size_t index = 0; index < devices_.size(); ++index) { |
| 412 pp::Var name = devices_[index].GetName(); |
| 413 assert(name.is_string()); |
| 414 |
| 415 if (index != 0) |
| 416 device_names += kDelimiter; |
| 417 device_names += name.AsString(); |
| 418 } |
| 419 PostMessage(pp::Var(device_names)); |
| 420 } else { |
| 421 PostMessage(pp::Var("EnumerationFailed")); |
| 422 } |
| 423 } |
| 424 |
| 425 void VCDemoInstance::OpenFinished(int32_t result) { |
| 426 if (result == PP_OK) { |
| 427 video_capture_.StartCapture(); |
| 428 } else { |
| 429 PostMessage(pp::Var("OpenFailed")); |
| 430 } |
| 431 } |
| 432 |
| 367 pp::Instance* VCDemoModule::CreateInstance(PP_Instance instance) { | 433 pp::Instance* VCDemoModule::CreateInstance(PP_Instance instance) { |
| 368 return new VCDemoInstance(instance, this); | 434 return new VCDemoInstance(instance, this); |
| 369 } | 435 } |
| 370 | 436 |
| 371 } // anonymous namespace | 437 } // anonymous namespace |
| 372 | 438 |
| 373 namespace pp { | 439 namespace pp { |
| 374 // Factory function for your specialization of the Module object. | 440 // Factory function for your specialization of the Module object. |
| 375 Module* CreateModule() { | 441 Module* CreateModule() { |
| 376 return new VCDemoModule(); | 442 return new VCDemoModule(); |
| 377 } | 443 } |
| 378 } // namespace pp | 444 } // namespace pp |
| OLD | NEW |