| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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" |
| 14 #include "ppapi/cpp/dev/buffer_dev.h" | |
| 15 #include "ppapi/cpp/dev/device_ref_dev.h" | |
| 16 #include "ppapi/cpp/dev/video_capture_dev.h" | |
| 17 #include "ppapi/cpp/dev/video_capture_client_dev.h" | |
| 18 #include "ppapi/cpp/completion_callback.h" | 10 #include "ppapi/cpp/completion_callback.h" |
| 11 #include "ppapi/cpp/dev/var_resource_dev.h" |
| 12 #include "ppapi/cpp/graphics_3d.h" |
| 19 #include "ppapi/cpp/graphics_3d_client.h" | 13 #include "ppapi/cpp/graphics_3d_client.h" |
| 20 #include "ppapi/cpp/graphics_3d.h" | |
| 21 #include "ppapi/cpp/instance.h" | 14 #include "ppapi/cpp/instance.h" |
| 15 #include "ppapi/cpp/media_stream_video_track.h" |
| 22 #include "ppapi/cpp/module.h" | 16 #include "ppapi/cpp/module.h" |
| 23 #include "ppapi/cpp/rect.h" | 17 #include "ppapi/cpp/rect.h" |
| 24 #include "ppapi/cpp/var.h" | 18 #include "ppapi/cpp/var.h" |
| 19 #include "ppapi/cpp/video_frame.h" |
| 25 #include "ppapi/lib/gl/include/GLES2/gl2.h" | 20 #include "ppapi/lib/gl/include/GLES2/gl2.h" |
| 26 #include "ppapi/utility/completion_callback_factory.h" | 21 #include "ppapi/utility/completion_callback_factory.h" |
| 27 | 22 |
| 28 // When compiling natively on Windows, PostMessage can be #define-d to | 23 // When compiling natively on Windows, PostMessage can be #define-d to |
| 29 // something else. | 24 // something else. |
| 30 #ifdef PostMessage | 25 #ifdef PostMessage |
| 31 #undef PostMessage | 26 #undef PostMessage |
| 32 #endif | 27 #endif |
| 33 | 28 |
| 34 // Assert |context_| isn't holding any GL Errors. Done as a macro instead of a | 29 // 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. | 30 // function to preserve line number information in the failure message. |
| 36 #define AssertNoGLError() \ | 31 #define AssertNoGLError() \ |
| 37 PP_DCHECK(!gles2_if_->GetError(context_->pp_resource())); | 32 PP_DCHECK(!gles2_if_->GetError(context_->pp_resource())); |
| 38 | 33 |
| 39 namespace { | 34 namespace { |
| 40 | 35 |
| 41 const char* const kDelimiter = "#__#"; | |
| 42 | |
| 43 // This object is the global object representing this plugin library as long | 36 // This object is the global object representing this plugin library as long |
| 44 // as it is loaded. | 37 // as it is loaded. |
| 45 class VCDemoModule : public pp::Module { | 38 class MSVDemoModule : public pp::Module { |
| 46 public: | 39 public: |
| 47 VCDemoModule() : pp::Module() {} | 40 MSVDemoModule() : pp::Module() {} |
| 48 virtual ~VCDemoModule() {} | 41 virtual ~MSVDemoModule() {} |
| 49 | 42 |
| 50 virtual pp::Instance* CreateInstance(PP_Instance instance); | 43 virtual pp::Instance* CreateInstance(PP_Instance instance); |
| 51 }; | 44 }; |
| 52 | 45 |
| 53 class VCDemoInstance : public pp::Instance, | 46 class MSVDemoInstance : public pp::Instance, |
| 54 public pp::Graphics3DClient, | 47 public pp::Graphics3DClient { |
| 55 public pp::VideoCaptureClient_Dev { | |
| 56 public: | 48 public: |
| 57 VCDemoInstance(PP_Instance instance, pp::Module* module); | 49 MSVDemoInstance(PP_Instance instance, pp::Module* module); |
| 58 virtual ~VCDemoInstance(); | 50 virtual ~MSVDemoInstance(); |
| 59 | 51 |
| 60 // pp::Instance implementation (see PPP_Instance). | 52 // pp::Instance implementation (see PPP_Instance). |
| 61 virtual void DidChangeView(const pp::Rect& position, | 53 virtual void DidChangeView(const pp::Rect& position, |
| 62 const pp::Rect& clip_ignored); | 54 const pp::Rect& clip_ignored); |
| 63 virtual void HandleMessage(const pp::Var& message_data); | 55 virtual void HandleMessage(const pp::Var& message_data); |
| 64 | 56 |
| 65 // pp::Graphics3DClient implementation. | 57 // pp::Graphics3DClient implementation. |
| 66 virtual void Graphics3DContextLost() { | 58 virtual void Graphics3DContextLost() { |
| 67 InitGL(); | 59 InitGL(); |
| 68 CreateYUVTextures(); | 60 CreateYUVTextures(); |
| 69 Render(); | 61 Render(); |
| 70 } | 62 } |
| 71 | 63 |
| 72 virtual void OnDeviceInfo(PP_Resource resource, | 64 void OnGetFrame(int32_t result, pp::VideoFrame frame) { |
| 73 const PP_VideoCaptureDeviceInfo_Dev& info, | 65 if (result != PP_OK) |
| 74 const std::vector<pp::Buffer_Dev>& buffers) { | 66 return; |
| 75 capture_info_ = info; | 67 const char* data = static_cast<const char*>(frame.GetDataBuffer()); |
| 76 buffers_ = buffers; | 68 pp::Size size; |
| 77 CreateYUVTextures(); | 69 PP_DCHECK(frame.GetSize(&size)); |
| 78 } | 70 if (size != frame_size_) { |
| 71 frame_size_ = size; |
| 72 CreateYUVTextures(); |
| 73 } |
| 79 | 74 |
| 80 virtual void OnStatus(PP_Resource resource, uint32_t status) { | 75 int32_t width = frame_size_.width(); |
| 81 } | 76 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); | 77 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE0); |
| 91 gles2_if_->TexSubImage2D( | 78 gles2_if_->TexSubImage2D( |
| 92 context_->pp_resource(), GL_TEXTURE_2D, 0, 0, 0, width, height, | 79 context_->pp_resource(), GL_TEXTURE_2D, 0, 0, 0, width, height, |
| 93 GL_LUMINANCE, GL_UNSIGNED_BYTE, data); | 80 GL_LUMINANCE, GL_UNSIGNED_BYTE, data); |
| 94 | 81 |
| 95 data += width * height; | 82 data += width * height; |
| 96 width /= 2; | 83 width /= 2; |
| 97 height /= 2; | 84 height /= 2; |
| 98 | 85 |
| 99 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE1); | 86 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE1); |
| 100 gles2_if_->TexSubImage2D( | 87 gles2_if_->TexSubImage2D( |
| 101 context_->pp_resource(), GL_TEXTURE_2D, 0, 0, 0, width, height, | 88 context_->pp_resource(), GL_TEXTURE_2D, 0, 0, 0, width, height, |
| 102 GL_LUMINANCE, GL_UNSIGNED_BYTE, data); | 89 GL_LUMINANCE, GL_UNSIGNED_BYTE, data); |
| 103 | 90 |
| 104 data += width * height; | 91 data += width * height; |
| 105 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE2); | 92 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE2); |
| 106 gles2_if_->TexSubImage2D( | 93 gles2_if_->TexSubImage2D( |
| 107 context_->pp_resource(), GL_TEXTURE_2D, 0, 0, 0, width, height, | 94 context_->pp_resource(), GL_TEXTURE_2D, 0, 0, 0, width, height, |
| 108 GL_LUMINANCE, GL_UNSIGNED_BYTE, data); | 95 GL_LUMINANCE, GL_UNSIGNED_BYTE, data); |
| 109 | 96 |
| 110 video_capture_.ReuseBuffer(buffer); | |
| 111 if (is_painting_) | 97 if (is_painting_) |
| 112 needs_paint_ = true; | 98 needs_paint_ = true; |
| 113 else | 99 else |
| 114 Render(); | 100 Render(); |
| 101 |
| 102 video_track_.RecycleFrame(frame); |
| 103 video_track_.GetFrame(callback_factory_.NewCallbackWithOutput( |
| 104 &MSVDemoInstance::OnGetFrame)); |
| 115 } | 105 } |
| 116 | 106 |
| 117 private: | 107 private: |
| 118 void Render(); | 108 void Render(); |
| 119 | 109 |
| 120 // GL-related functions. | 110 // GL-related functions. |
| 121 void InitGL(); | 111 void InitGL(); |
| 122 GLuint CreateTexture(int32_t width, int32_t height, int unit); | 112 GLuint CreateTexture(int32_t width, int32_t height, int unit); |
| 123 void CreateGLObjects(); | 113 void CreateGLObjects(); |
| 124 void CreateShader(GLuint program, GLenum type, const char* source, int size); | 114 void CreateShader(GLuint program, GLenum type, const char* source, int size); |
| 125 void PaintFinished(int32_t result); | 115 void PaintFinished(int32_t result); |
| 126 void CreateYUVTextures(); | 116 void CreateYUVTextures(); |
| 127 | 117 |
| 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_; | 118 pp::Size position_size_; |
| 140 bool is_painting_; | 119 bool is_painting_; |
| 141 bool needs_paint_; | 120 bool needs_paint_; |
| 142 GLuint texture_y_; | 121 GLuint texture_y_; |
| 143 GLuint texture_u_; | 122 GLuint texture_u_; |
| 144 GLuint texture_v_; | 123 GLuint texture_v_; |
| 145 pp::VideoCapture_Dev video_capture_; | 124 pp::MediaStreamVideoTrack video_track_; |
| 146 PP_VideoCaptureDeviceInfo_Dev capture_info_; | 125 pp::CompletionCallbackFactory<MSVDemoInstance> callback_factory_; |
| 147 std::vector<pp::Buffer_Dev> buffers_; | |
| 148 pp::CompletionCallbackFactory<VCDemoInstance> callback_factory_; | |
| 149 | 126 |
| 150 // Unowned pointers. | 127 // Unowned pointers. |
| 151 const struct PPB_OpenGLES2* gles2_if_; | 128 const struct PPB_OpenGLES2* gles2_if_; |
| 152 | 129 |
| 153 // Owned data. | 130 // Owned data. |
| 154 pp::Graphics3D* context_; | 131 pp::Graphics3D* context_; |
| 155 | 132 |
| 156 std::vector<pp::DeviceRef_Dev> enumerate_devices_; | 133 pp::Size frame_size_; |
| 157 std::vector<pp::DeviceRef_Dev> monitor_devices_; | |
| 158 }; | 134 }; |
| 159 | 135 |
| 160 VCDemoInstance::VCDemoInstance(PP_Instance instance, pp::Module* module) | 136 MSVDemoInstance::MSVDemoInstance(PP_Instance instance, pp::Module* module) |
| 161 : pp::Instance(instance), | 137 : pp::Instance(instance), |
| 162 pp::Graphics3DClient(this), | 138 pp::Graphics3DClient(this), |
| 163 pp::VideoCaptureClient_Dev(this), | |
| 164 is_painting_(false), | 139 is_painting_(false), |
| 165 needs_paint_(false), | 140 needs_paint_(false), |
| 166 texture_y_(0), | 141 texture_y_(0), |
| 167 texture_u_(0), | 142 texture_u_(0), |
| 168 texture_v_(0), | 143 texture_v_(0), |
| 169 video_capture_(this), | |
| 170 callback_factory_(this), | 144 callback_factory_(this), |
| 171 context_(NULL) { | 145 context_(NULL) { |
| 172 gles2_if_ = static_cast<const struct PPB_OpenGLES2*>( | 146 gles2_if_ = static_cast<const struct PPB_OpenGLES2*>( |
| 173 module->GetBrowserInterface(PPB_OPENGLES2_INTERFACE)); | 147 module->GetBrowserInterface(PPB_OPENGLES2_INTERFACE)); |
| 174 PP_DCHECK(gles2_if_); | 148 PP_DCHECK(gles2_if_); |
| 175 | |
| 176 capture_info_.width = 320; | |
| 177 capture_info_.height = 240; | |
| 178 capture_info_.frames_per_second = 30; | |
| 179 } | 149 } |
| 180 | 150 |
| 181 VCDemoInstance::~VCDemoInstance() { | 151 MSVDemoInstance::~MSVDemoInstance() { |
| 182 video_capture_.MonitorDeviceChange(NULL, NULL); | |
| 183 delete context_; | 152 delete context_; |
| 184 } | 153 } |
| 185 | 154 |
| 186 void VCDemoInstance::DidChangeView( | 155 void MSVDemoInstance::DidChangeView( |
| 187 const pp::Rect& position, const pp::Rect& clip_ignored) { | 156 const pp::Rect& position, const pp::Rect& clip_ignored) { |
| 188 if (position.width() == 0 || position.height() == 0) | 157 if (position.width() == 0 || position.height() == 0) |
| 189 return; | 158 return; |
| 190 if (position.size() == position_size_) | 159 if (position.size() == position_size_) |
| 191 return; | 160 return; |
| 192 | 161 |
| 193 position_size_ = position.size(); | 162 position_size_ = position.size(); |
| 194 | 163 |
| 195 // Initialize graphics. | 164 // Initialize graphics. |
| 196 InitGL(); | 165 InitGL(); |
| 197 | |
| 198 Render(); | 166 Render(); |
| 199 } | 167 } |
| 200 | 168 |
| 201 void VCDemoInstance::HandleMessage(const pp::Var& message_data) { | 169 void MSVDemoInstance::HandleMessage(const pp::Var& var_message) { |
| 202 if (message_data.is_string()) { | 170 if (!var_message.is_dictionary()) |
| 203 std::string event = message_data.AsString(); | 171 return; |
| 204 if (event == "PageInitialized") { | 172 pp::VarDictionary var_dictionary_message(var_message); |
| 205 int32_t result = video_capture_.MonitorDeviceChange( | 173 pp::Var var_track = var_dictionary_message.Get("track"); |
| 206 &VCDemoInstance::MonitorDeviceChangeCallback, this); | 174 if (!var_track.is_resource()) |
| 207 if (result != PP_OK) | 175 return; |
| 208 PostMessage(pp::Var("MonitorDeviceChangeFailed")); | |
| 209 | 176 |
| 210 pp::CompletionCallbackWithOutput<std::vector<pp::DeviceRef_Dev> > | 177 pp::Resource resource_track = pp::VarResource_Dev(var_track).AsResource(); |
| 211 callback = callback_factory_.NewCallbackWithOutput( | 178 |
| 212 &VCDemoInstance::EnumerateDevicesFinished); | 179 video_track_ = pp::MediaStreamVideoTrack(resource_track); |
| 213 result = video_capture_.EnumerateDevices(callback); | 180 |
| 214 if (result != PP_OK_COMPLETIONPENDING) | 181 video_track_.GetFrame(callback_factory_.NewCallbackWithOutput( |
| 215 PostMessage(pp::Var("EnumerationFailed")); | 182 &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 } | 183 } |
| 239 | 184 |
| 240 void VCDemoInstance::InitGL() { | 185 void MSVDemoInstance::InitGL() { |
| 241 PP_DCHECK(position_size_.width() && position_size_.height()); | 186 PP_DCHECK(position_size_.width() && position_size_.height()); |
| 242 is_painting_ = false; | 187 is_painting_ = false; |
| 243 | 188 |
| 244 delete context_; | 189 delete context_; |
| 245 int32_t attributes[] = { | 190 int32_t attributes[] = { |
| 246 PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 0, | 191 PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 0, |
| 247 PP_GRAPHICS3DATTRIB_BLUE_SIZE, 8, | 192 PP_GRAPHICS3DATTRIB_BLUE_SIZE, 8, |
| 248 PP_GRAPHICS3DATTRIB_GREEN_SIZE, 8, | 193 PP_GRAPHICS3DATTRIB_GREEN_SIZE, 8, |
| 249 PP_GRAPHICS3DATTRIB_RED_SIZE, 8, | 194 PP_GRAPHICS3DATTRIB_RED_SIZE, 8, |
| 250 PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 0, | 195 PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 0, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 263 gles2_if_->Clear(context_->pp_resource(), GL_COLOR_BUFFER_BIT); | 208 gles2_if_->Clear(context_->pp_resource(), GL_COLOR_BUFFER_BIT); |
| 264 gles2_if_->Viewport(context_->pp_resource(), 0, 0, | 209 gles2_if_->Viewport(context_->pp_resource(), 0, 0, |
| 265 position_size_.width(), position_size_.height()); | 210 position_size_.width(), position_size_.height()); |
| 266 | 211 |
| 267 BindGraphics(*context_); | 212 BindGraphics(*context_); |
| 268 AssertNoGLError(); | 213 AssertNoGLError(); |
| 269 | 214 |
| 270 CreateGLObjects(); | 215 CreateGLObjects(); |
| 271 } | 216 } |
| 272 | 217 |
| 273 void VCDemoInstance::Render() { | 218 void MSVDemoInstance::Render() { |
| 274 PP_DCHECK(!is_painting_); | 219 PP_DCHECK(!is_painting_); |
| 275 is_painting_ = true; | 220 is_painting_ = true; |
| 276 needs_paint_ = false; | 221 needs_paint_ = false; |
| 277 if (texture_y_) { | 222 if (texture_y_) { |
| 278 gles2_if_->DrawArrays(context_->pp_resource(), GL_TRIANGLE_STRIP, 0, 4); | 223 gles2_if_->DrawArrays(context_->pp_resource(), GL_TRIANGLE_STRIP, 0, 4); |
| 279 } else { | 224 } else { |
| 280 gles2_if_->Clear(context_->pp_resource(), GL_COLOR_BUFFER_BIT); | 225 gles2_if_->Clear(context_->pp_resource(), GL_COLOR_BUFFER_BIT); |
| 281 } | 226 } |
| 282 pp::CompletionCallback cb = callback_factory_.NewCallback( | 227 pp::CompletionCallback cb = callback_factory_.NewCallback( |
| 283 &VCDemoInstance::PaintFinished); | 228 &MSVDemoInstance::PaintFinished); |
| 284 context_->SwapBuffers(cb); | 229 context_->SwapBuffers(cb); |
| 285 } | 230 } |
| 286 | 231 |
| 287 void VCDemoInstance::PaintFinished(int32_t result) { | 232 void MSVDemoInstance::PaintFinished(int32_t result) { |
| 288 is_painting_ = false; | 233 is_painting_ = false; |
| 289 if (needs_paint_) | 234 if (needs_paint_) |
| 290 Render(); | 235 Render(); |
| 291 } | 236 } |
| 292 | 237 |
| 293 GLuint VCDemoInstance::CreateTexture(int32_t width, int32_t height, int unit) { | 238 GLuint MSVDemoInstance::CreateTexture(int32_t width, int32_t height, int unit) { |
| 294 GLuint texture_id; | 239 GLuint texture_id; |
| 295 gles2_if_->GenTextures(context_->pp_resource(), 1, &texture_id); | 240 gles2_if_->GenTextures(context_->pp_resource(), 1, &texture_id); |
| 296 AssertNoGLError(); | 241 AssertNoGLError(); |
| 297 // Assign parameters. | 242 // Assign parameters. |
| 298 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE0 + unit); | 243 gles2_if_->ActiveTexture(context_->pp_resource(), GL_TEXTURE0 + unit); |
| 299 gles2_if_->BindTexture(context_->pp_resource(), GL_TEXTURE_2D, texture_id); | 244 gles2_if_->BindTexture(context_->pp_resource(), GL_TEXTURE_2D, texture_id); |
| 300 gles2_if_->TexParameteri( | 245 gles2_if_->TexParameteri( |
| 301 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, | 246 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, |
| 302 GL_NEAREST); | 247 GL_NEAREST); |
| 303 gles2_if_->TexParameteri( | 248 gles2_if_->TexParameteri( |
| 304 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, | 249 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, |
| 305 GL_NEAREST); | 250 GL_NEAREST); |
| 306 gles2_if_->TexParameterf( | 251 gles2_if_->TexParameterf( |
| 307 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, | 252 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, |
| 308 GL_CLAMP_TO_EDGE); | 253 GL_CLAMP_TO_EDGE); |
| 309 gles2_if_->TexParameterf( | 254 gles2_if_->TexParameterf( |
| 310 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, | 255 context_->pp_resource(), GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, |
| 311 GL_CLAMP_TO_EDGE); | 256 GL_CLAMP_TO_EDGE); |
| 312 | 257 |
| 313 // Allocate texture. | 258 // Allocate texture. |
| 314 gles2_if_->TexImage2D( | 259 gles2_if_->TexImage2D( |
| 315 context_->pp_resource(), GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, | 260 context_->pp_resource(), GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, |
| 316 GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL); | 261 GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL); |
| 317 AssertNoGLError(); | 262 AssertNoGLError(); |
| 318 return texture_id; | 263 return texture_id; |
| 319 } | 264 } |
| 320 | 265 |
| 321 void VCDemoInstance::CreateGLObjects() { | 266 void MSVDemoInstance::CreateGLObjects() { |
| 322 // Code and constants for shader. | 267 // Code and constants for shader. |
| 323 static const char kVertexShader[] = | 268 static const char kVertexShader[] = |
| 324 "varying vec2 v_texCoord; \n" | 269 "varying vec2 v_texCoord; \n" |
| 325 "attribute vec4 a_position; \n" | 270 "attribute vec4 a_position; \n" |
| 326 "attribute vec2 a_texCoord; \n" | 271 "attribute vec2 a_texCoord; \n" |
| 327 "void main() \n" | 272 "void main() \n" |
| 328 "{ \n" | 273 "{ \n" |
| 329 " v_texCoord = a_texCoord; \n" | 274 " v_texCoord = a_texCoord; \n" |
| 330 " gl_Position = a_position; \n" | 275 " gl_Position = a_position; \n" |
| 331 "}"; | 276 "}"; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 gles2_if_->EnableVertexAttribArray(context, pos_location); | 341 gles2_if_->EnableVertexAttribArray(context, pos_location); |
| 397 gles2_if_->VertexAttribPointer(context, pos_location, 2, | 342 gles2_if_->VertexAttribPointer(context, pos_location, 2, |
| 398 GL_FLOAT, GL_FALSE, 0, 0); | 343 GL_FLOAT, GL_FALSE, 0, 0); |
| 399 gles2_if_->EnableVertexAttribArray(context, tc_location); | 344 gles2_if_->EnableVertexAttribArray(context, tc_location); |
| 400 gles2_if_->VertexAttribPointer( | 345 gles2_if_->VertexAttribPointer( |
| 401 context, tc_location, 2, GL_FLOAT, GL_FALSE, 0, | 346 context, tc_location, 2, GL_FLOAT, GL_FALSE, 0, |
| 402 static_cast<float*>(0) + 8); // Skip position coordinates. | 347 static_cast<float*>(0) + 8); // Skip position coordinates. |
| 403 AssertNoGLError(); | 348 AssertNoGLError(); |
| 404 } | 349 } |
| 405 | 350 |
| 406 void VCDemoInstance::CreateShader( | 351 void MSVDemoInstance::CreateShader( |
| 407 GLuint program, GLenum type, const char* source, int size) { | 352 GLuint program, GLenum type, const char* source, int size) { |
| 408 PP_Resource context = context_->pp_resource(); | 353 PP_Resource context = context_->pp_resource(); |
| 409 GLuint shader = gles2_if_->CreateShader(context, type); | 354 GLuint shader = gles2_if_->CreateShader(context, type); |
| 410 gles2_if_->ShaderSource(context, shader, 1, &source, &size); | 355 gles2_if_->ShaderSource(context, shader, 1, &source, &size); |
| 411 gles2_if_->CompileShader(context, shader); | 356 gles2_if_->CompileShader(context, shader); |
| 412 gles2_if_->AttachShader(context, program, shader); | 357 gles2_if_->AttachShader(context, program, shader); |
| 413 gles2_if_->DeleteShader(context, shader); | 358 gles2_if_->DeleteShader(context, shader); |
| 414 } | 359 } |
| 415 | 360 |
| 416 void VCDemoInstance::CreateYUVTextures() { | 361 void MSVDemoInstance::CreateYUVTextures() { |
| 417 int32_t width = capture_info_.width; | 362 int32_t width = frame_size_.width(); |
| 418 int32_t height = capture_info_.height; | 363 int32_t height = frame_size_.height(); |
| 364 if (width == 0 || height == 0) |
| 365 return; |
| 419 texture_y_ = CreateTexture(width, height, 0); | 366 texture_y_ = CreateTexture(width, height, 0); |
| 420 | 367 |
| 421 width /= 2; | 368 width /= 2; |
| 422 height /= 2; | 369 height /= 2; |
| 423 texture_u_ = CreateTexture(width, height, 1); | 370 texture_u_ = CreateTexture(width, height, 1); |
| 424 texture_v_ = CreateTexture(width, height, 2); | 371 texture_v_ = CreateTexture(width, height, 2); |
| 425 } | 372 } |
| 426 | 373 |
| 427 void VCDemoInstance::Open(const pp::DeviceRef_Dev& device) { | 374 pp::Instance* MSVDemoModule::CreateInstance(PP_Instance instance) { |
| 428 pp::CompletionCallback callback = callback_factory_.NewCallback( | 375 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 } | 376 } |
| 496 | 377 |
| 497 } // anonymous namespace | 378 } // anonymous namespace |
| 498 | 379 |
| 499 namespace pp { | 380 namespace pp { |
| 500 // Factory function for your specialization of the Module object. | 381 // Factory function for your specialization of the Module object. |
| 501 Module* CreateModule() { | 382 Module* CreateModule() { |
| 502 return new VCDemoModule(); | 383 return new MSVDemoModule(); |
| 503 } | 384 } |
| 504 } // namespace pp | 385 } // namespace pp |
| OLD | NEW |