Index: ppapi/examples/video_capture/video_capture.cc |
diff --git a/ppapi/examples/video_capture/video_capture.cc b/ppapi/examples/video_capture/video_capture.cc |
index 63e2cb80312a3dbaf17959e08625add21afb4409..c0d96eba15e4c10318730e40118547556bdb8397 100644 |
--- a/ppapi/examples/video_capture/video_capture.cc |
+++ b/ppapi/examples/video_capture/video_capture.cc |
@@ -1,4 +1,4 @@ |
-// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// 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. |
@@ -11,6 +11,7 @@ |
#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" |
@@ -19,6 +20,7 @@ |
#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" |
@@ -49,6 +51,7 @@ class VCDemoInstance : public pp::Instance, |
// 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() { |
@@ -66,6 +69,9 @@ class VCDemoInstance : public pp::Instance, |
} |
virtual void OnStatus(PP_Resource resource, uint32_t status) { |
+ status_ = static_cast<PP_VideoCaptureStatus_Dev>(status); |
+ if (status_ == PP_VIDEO_CAPTURE_STATUS_STOPPED) |
+ StartCapture(); |
} |
virtual void OnError(PP_Resource resource, uint32_t error) { |
@@ -107,13 +113,15 @@ class VCDemoInstance : public pp::Instance, |
// GL-related functions. |
void InitGL(); |
- void InitializeCapture(); |
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 EnumerateDevicesFinished(int32_t result); |
+ void StartCapture(); |
+ |
pp::Size position_size_; |
bool is_painting_; |
bool needs_paint_; |
@@ -130,6 +138,15 @@ class VCDemoInstance : public pp::Instance, |
// Owned data. |
pp::Graphics3D* context_; |
+ |
+ bool page_initialized_; |
+ PP_VideoCaptureStatus_Dev status_; |
+ std::vector<pp::DeviceRef_Dev> devices_; |
+ |
+ // >= 0 means index of |devices_|; |
+ // -1 means the default device; |
+ // -2 means no pending request to start capture. |
+ int pending_start_capture_device_; |
}; |
VCDemoInstance::VCDemoInstance(PP_Instance instance, pp::Module* module) |
@@ -143,11 +160,17 @@ VCDemoInstance::VCDemoInstance(PP_Instance instance, pp::Module* module) |
texture_v_(0), |
video_capture_(*this), |
callback_factory_(this), |
- context_(NULL) { |
+ context_(NULL), |
+ page_initialized_(false), |
+ status_(PP_VIDEO_CAPTURE_STATUS_STOPPED), |
+ pending_start_capture_device_(-2) { |
gles2_if_ = static_cast<const struct PPB_OpenGLES2*>( |
module->GetBrowserInterface(PPB_OPENGLES2_INTERFACE)); |
assert(gles2_if_); |
- InitializeCapture(); |
+ |
+ capture_info_.width = 320; |
+ capture_info_.height = 240; |
+ capture_info_.frames_per_second = 30; |
} |
VCDemoInstance::~VCDemoInstance() { |
@@ -169,12 +192,26 @@ void VCDemoInstance::DidChangeView( |
Render(); |
} |
-void VCDemoInstance::InitializeCapture() { |
- capture_info_.width = 320; |
- capture_info_.height = 240; |
- capture_info_.frames_per_second = 30; |
- |
- video_capture_.StartCapture(capture_info_, 4); |
+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(callback); |
+ } else if (event == "UseDefault") { |
+ pending_start_capture_device_ = -1; |
+ StartCapture(); |
+ } |
+ } else if (message_data.is_number()) { |
+ int index = message_data.AsInt(); |
+ if (index >= 0 && index < static_cast<int>(devices_.size())) { |
+ pending_start_capture_device_= index; |
+ StartCapture(); |
+ } else { |
+ assert(false); |
+ } |
+ } |
} |
void VCDemoInstance::InitGL() { |
@@ -364,6 +401,42 @@ void VCDemoInstance::CreateYUVTextures() { |
texture_v_ = CreateTexture(width, height, 2); |
} |
+void VCDemoInstance::EnumerateDevicesFinished(int32_t result) { |
+ static const char* const kDelimiter = "#__#"; |
+ |
+ if (result == PP_OK) { |
+ std::string device_names; |
+ video_capture_.GetDevices(&devices_); |
+ 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::StartCapture() { |
+ if (status_ != PP_VIDEO_CAPTURE_STATUS_STOPPED) { |
+ video_capture_.StopCapture(); |
+ return; |
+ } |
+ |
+ if (pending_start_capture_device_ > -2 && |
+ pending_start_capture_device_ < static_cast<int>(devices_.size())) { |
+ video_capture_.StartCapture( |
+ pending_start_capture_device_ == -1 ? |
+ pp::DeviceRef_Dev() : devices_[pending_start_capture_device_], |
+ capture_info_, 4); |
+ } |
+ pending_start_capture_device_ = -2; |
+} |
+ |
pp::Instance* VCDemoModule::CreateInstance(PP_Instance instance) { |
return new VCDemoInstance(instance, this); |
} |