diff options
author | yzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-07 07:48:48 +0000 |
---|---|---|
committer | yzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-07 07:48:48 +0000 |
commit | eed24562b1e6a431fc726195e1114eed01b1694f (patch) | |
tree | 2ae570b6f55f7043e62bc25dca335d03ba75bb52 /ppapi/examples | |
parent | e974ad29d8f31b1f86bb551dc99ad4e5e62b002c (diff) | |
download | chromium_src-eed24562b1e6a431fc726195e1114eed01b1694f.zip chromium_src-eed24562b1e6a431fc726195e1114eed01b1694f.tar.gz chromium_src-eed24562b1e6a431fc726195e1114eed01b1694f.tar.bz2 |
PPB_AudioInput_Dev: support multiple audio input devices - Part 1.
- This CL implements PPB_AudioInput_Dev v0.2 and extends examples/audio_input.
- This CL doesn't actually open devices other than the default one. That will be in a separate CL.
BUG=None
TEST=examples/audio_input
Review URL: http://codereview.chromium.org/9557007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@125362 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi/examples')
-rw-r--r-- | ppapi/examples/audio_input/audio_input.cc | 107 | ||||
-rw-r--r-- | ppapi/examples/audio_input/audio_input.html | 91 |
2 files changed, 190 insertions, 8 deletions
diff --git a/ppapi/examples/audio_input/audio_input.cc b/ppapi/examples/audio_input/audio_input.cc index 203718e..44fd337 100644 --- a/ppapi/examples/audio_input/audio_input.cc +++ b/ppapi/examples/audio_input/audio_input.cc @@ -6,9 +6,11 @@ #include <algorithm> #include <limits> +#include <vector> #include "ppapi/cpp/audio_config.h" #include "ppapi/cpp/dev/audio_input_dev.h" +#include "ppapi/cpp/dev/device_ref_dev.h" #include "ppapi/cpp/graphics_2d.h" #include "ppapi/cpp/image_data.h" #include "ppapi/cpp/instance.h" @@ -28,10 +30,18 @@ class MyInstance : public pp::Instance { samples_(NULL), timer_interval_(0), pending_paint_(false), - waiting_for_flush_completion_(false) { + waiting_for_flush_completion_(false), + audio_input_0_1_(0), + audio_input_interface_0_1_(NULL) { } virtual ~MyInstance() { - audio_input_.StopCapture(); + if (!audio_input_.is_null()) { + audio_input_.Close(); + } else if (audio_input_0_1_ != 0) { + audio_input_interface_0_1_->StopCapture(audio_input_0_1_); + pp::Module::Get()->core()->ReleaseResource(audio_input_0_1_); + } + delete[] samples_; } @@ -52,7 +62,11 @@ class MyInstance : public pp::Instance { samples_ = new int16_t[sample_count_ * channel_count_]; memset(samples_, 0, sample_count_ * channel_count_ * sizeof(int16_t)); audio_input_ = pp::AudioInput_Dev(this, config, CaptureCallback, this); - if (!audio_input_.StartCapture()) + + audio_input_interface_0_1_ = static_cast<const PPB_AudioInput_Dev_0_1*>( + pp::Module::Get()->GetBrowserInterface( + PPB_AUDIO_INPUT_DEV_INTERFACE_0_1)); + if (!audio_input_interface_0_1_) return false; // Try to ensure that we pick up a new set of samples between each @@ -75,6 +89,42 @@ class MyInstance : public pp::Instance { Paint(); } + virtual void 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( + &MyInstance::EnumerateDevicesFinished); + int32_t result = audio_input_.EnumerateDevices(&devices_, callback); + if (result != PP_OK_COMPLETIONPENDING) + PostMessage(pp::Var("EnumerationFailed")); + } else if (event == "UseDefault") { + Open(pp::DeviceRef_Dev()); + } else if (event == "UseDefault(v0.1)") { + audio_input_0_1_ = audio_input_interface_0_1_->Create( + pp_instance(), audio_input_.config().pp_resource(), + CaptureCallback, this); + if (audio_input_0_1_ != 0) { + if (!audio_input_interface_0_1_->StartCapture(audio_input_0_1_)) + PostMessage(pp::Var("StartFailed")); + } else { + PostMessage(pp::Var("OpenFailed")); + } + + audio_input_ = pp::AudioInput_Dev(); + } else if (event == "Stop") { + Stop(); + } + } else if (message_data.is_number()) { + int index = message_data.AsInt(); + if (index >= 0 && index < static_cast<int>(devices_.size())) { + Open(devices_[index]); + } else { + PP_NOTREACHED(); + } + } + } + private: void ScheduleNextTimer() { PP_DCHECK(timer_interval_ > 0); @@ -161,6 +211,52 @@ class MyInstance : public pp::Instance { memcpy(thiz->samples_, samples, num_bytes); } + void Open(const pp::DeviceRef_Dev& device) { + pp::CompletionCallback callback = callback_factory_.NewCallback( + &MyInstance::OpenFinished); + int32_t result = audio_input_.Open(device, callback); + if (result != PP_OK_COMPLETIONPENDING) + PostMessage(pp::Var("OpenFailed")); + } + + void Stop() { + if (!audio_input_.is_null()) { + if (!audio_input_.StopCapture()) + PostMessage(pp::Var("StopFailed")); + } else if (audio_input_0_1_ != 0) { + if (!audio_input_interface_0_1_->StopCapture(audio_input_0_1_)) + PostMessage(pp::Var("StopFailed")); + } + } + + void 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(); + PP_DCHECK(name.is_string()); + + if (index != 0) + device_names += kDelimiter; + device_names += name.AsString(); + } + PostMessage(pp::Var(device_names)); + } else { + PostMessage(pp::Var("EnumerationFailed")); + } + } + + void OpenFinished(int32_t result) { + if (result == PP_OK) { + if (!audio_input_.StartCapture()) + PostMessage(pp::Var("StartFailed")); + } else { + PostMessage(pp::Var("OpenFailed")); + } + } + pp::CompletionCallbackFactory<MyInstance> callback_factory_; uint32_t sample_count_; @@ -176,6 +272,11 @@ class MyInstance : public pp::Instance { bool waiting_for_flush_completion_; pp::AudioInput_Dev audio_input_; + + PP_Resource audio_input_0_1_; + const PPB_AudioInput_Dev_0_1* audio_input_interface_0_1_; + + std::vector<pp::DeviceRef_Dev> devices_; }; class MyModule : public pp::Module { diff --git a/ppapi/examples/audio_input/audio_input.html b/ppapi/examples/audio_input/audio_input.html index bfa91cd..a5256fa 100644 --- a/ppapi/examples/audio_input/audio_input.html +++ b/ppapi/examples/audio_input/audio_input.html @@ -1,18 +1,99 @@ <!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>Audio Input Example</title> -</head> + <script type="text/javascript"> + var device_array = []; -<body> + function HandleMessage(message_event) { + if (message_event.data) { + var status = document.getElementById('status'); + if (message_event.data == 'EnumerationFailed') { + status.innerText = 'Device enumeration failed!'; + } else if (message_event.data == 'OpenFailed') { + status.innerText = 'Open device failed!'; + } else if (message_event.data == 'StartFailed') { + status.innerText = 'Start capturing failed!'; + } else if (message_event.data == 'StopFailed') { + status.innerText = 'Stop capturing 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-audio-input" - width="800" height="400"/> + 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-audio-input" + width="800" height="400"/> + <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 - use interface version 0.2 and NULL device ref</a></li> + </ul> + </div> + <div id="control_panel"></div> + <div id="status"></div> </body> </html> |