diff options
author | yzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-15 22:58:15 +0000 |
---|---|---|
committer | yzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-15 22:58:15 +0000 |
commit | cc7c244894cea1b16e4a0cfe078dfda1bd69e594 (patch) | |
tree | ce5cf1a203f35d413f36d19e9a5f4aa7239231de /ppapi/examples | |
parent | e21dcefec09d37abfd800cf64f3b61bebffd21e7 (diff) | |
download | chromium_src-cc7c244894cea1b16e4a0cfe078dfda1bd69e594.zip chromium_src-cc7c244894cea1b16e4a0cfe078dfda1bd69e594.tar.gz chromium_src-cc7c244894cea1b16e4a0cfe078dfda1bd69e594.tar.bz2 |
Implement device enumeration for PPB_VideoCapture_Dev.
- Implement PPB_VideoCapture_Dev v0.2.
- Use a ref-counted PlatformVideoCapture to manage lifespan of media::VideoCapture::EventHandler, instead of manipulating the ref count of PPB_VideoCapture_Impl.
- Extend examples/video_capture.
BUG=None
TEST=examples/video_capture
Review URL: https://chromiumcodereview.appspot.com/9234064
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@122176 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi/examples')
-rw-r--r-- | ppapi/examples/video_capture/video_capture.cc | 84 | ||||
-rw-r--r-- | ppapi/examples/video_capture/video_capture.html | 87 |
2 files changed, 157 insertions, 14 deletions
diff --git a/ppapi/examples/video_capture/video_capture.cc b/ppapi/examples/video_capture/video_capture.cc index 63e2cb8..512cca4 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. @@ -8,9 +8,11 @@ #include <map> #include <vector> +#include "ppapi/c/dev/ppb_video_capture_dev.h" #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 +21,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 +52,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() { @@ -107,13 +111,16 @@ 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 Open(const pp::DeviceRef_Dev& device); + void EnumerateDevicesFinished(int32_t result); + void OpenFinished(int32_t result); + pp::Size position_size_; bool is_painting_; bool needs_paint_; @@ -130,6 +137,8 @@ class VCDemoInstance : public pp::Instance, // Owned data. pp::Graphics3D* context_; + + std::vector<pp::DeviceRef_Dev> devices_; }; VCDemoInstance::VCDemoInstance(PP_Instance instance, pp::Module* module) @@ -147,7 +156,10 @@ VCDemoInstance::VCDemoInstance(PP_Instance instance, pp::Module* module) 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 +181,33 @@ 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(&devices_, callback); + } else if (event == "UseDefault") { + Open(pp::DeviceRef_Dev()); + } else if (event == "UseDefault(v0.1)") { + const PPB_VideoCapture_Dev_0_1* video_capture_0_1 = + static_cast<const PPB_VideoCapture_Dev_0_1*>( + pp::Module::Get()->GetBrowserInterface( + PPB_VIDEOCAPTURE_DEV_INTERFACE_0_1)); + video_capture_0_1->StartCapture(video_capture_.pp_resource(), + &capture_info_, 4); + } else if (event == "Stop") { + video_capture_.StopCapture(); + } + } else if (message_data.is_number()) { + int index = message_data.AsInt(); + if (index >= 0 && index < static_cast<int>(devices_.size())) { + Open(devices_[index]); + } else { + assert(false); + } + } } void VCDemoInstance::InitGL() { @@ -364,6 +397,39 @@ void VCDemoInstance::CreateYUVTextures() { texture_v_ = CreateTexture(width, height, 2); } +void VCDemoInstance::Open(const pp::DeviceRef_Dev& device) { + pp::CompletionCallback callback = callback_factory_.NewCallback( + &VCDemoInstance::OpenFinished); + video_capture_.Open(device, capture_info_, 4, callback); +} + +void VCDemoInstance::EnumerateDevicesFinished(int32_t result) { + static const char* const kDelimiter = "#__#"; + + if (result == PP_OK) { + std::string device_names; + 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::OpenFinished(int32_t result) { + if (result == PP_OK) { + video_capture_.StartCapture(); + } else { + PostMessage(pp::Var("OpenFailed")); + } +} + pp::Instance* VCDemoModule::CreateInstance(PP_Instance instance) { return new VCDemoInstance(instance, this); } diff --git a/ppapi/examples/video_capture/video_capture.html b/ppapi/examples/video_capture/video_capture.html index 44247fa..4aa888e 100644 --- a/ppapi/examples/video_capture/video_capture.html +++ b/ppapi/examples/video_capture/video_capture.html @@ -1,18 +1,95 @@ <!DOCTYPE html> <html> <!-- - 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. --> <head> <title>Video Capture Example</title> -</head> + <script type="text/javascript"> + var device_array = []; -<body> + function HandleMessage(message_event) { + if (message_event.data) { + if (message_event.data == 'EnumerationFailed') { + var status = document.getElementById('status'); + status.innerText = 'Device enumeration failed!'; + } else if (message_event.data == 'OpenFailed') { + var status = document.getElementById('status'); + status.innerText = 'Open device failed!'; + } else { + device_array = message_event.data.split('#__#'); + + var list = document.getElementById('device_list'); + for (var i = 0; i < device_array.length; ++i) { + var list_item = document.createElement('li'); + var link = document.createElement('a'); + link.href = 'javascript:UseDesignatedDevice(' + i + ');'; + link.innerText = device_array[i]; + list_item.appendChild(link); + list.appendChild(list_item); + } + } + } + } + + function UseDesignatedDevice(index) { + UseDevice(device_array[index], index); + } + + function UseDefaultDevice(use_0_1_interface) { + var display_text = use_0_1_interface ? 'Default(v0.1)' : 'Default'; + var command = use_0_1_interface ? 'UseDefault(v0.1)' : 'UseDefault'; + UseDevice(display_text, command); + } + + function UseDevice(display_text, command) { + var in_use_device = document.getElementById('in_use_device'); + in_use_device.innerText = display_text; + var plugin = document.getElementById('plugin'); + plugin.postMessage(command); -<embed id="plugin" type="application/x-ppapi-example-video-capture" - width="320" height="240"/> + var available_devices = document.getElementById('available_devices'); + available_devices.parentNode.removeChild(available_devices); + var control_panel = document.getElementById('control_panel'); + var link = document.createElement('a'); + link.href = 'javascript:Stop();'; + link.innerText = 'Stop'; + control_panel.appendChild(link); + } + + function Stop() { + var plugin = document.getElementById('plugin'); + plugin.postMessage('Stop'); + } + + function Initialize() { + var plugin = document.getElementById('plugin'); + plugin.addEventListener('message', HandleMessage, false); + plugin.postMessage('PageInitialized'); + } + + document.addEventListener('DOMContentLoaded', Initialize, false); + </script> +</head> + +<body> + <embed id="plugin" type="application/x-ppapi-example-video-capture" + width="320" height="240"/> + <div style="margin-bottom:10px">In-use device: + <span id="in_use_device" style="font-weight:bold">None</span> + </div> + <div id="available_devices"> + Available device(s), choose one to open: + <ul id="device_list"> + <li><a href="javascript:UseDefaultDevice(true);"> + Default - use interface version 0.1</a></li> + <li><a href="javascript:UseDefaultDevice(false);">Default</a></li> + </ul> + </div> + <div id="control_panel"></div> + <div id="status"></div> </body> </html> |