summaryrefslogtreecommitdiffstats
path: root/ppapi/shared_impl
diff options
context:
space:
mode:
authoryzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-07 07:48:48 +0000
committeryzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-07 07:48:48 +0000
commiteed24562b1e6a431fc726195e1114eed01b1694f (patch)
tree2ae570b6f55f7043e62bc25dca335d03ba75bb52 /ppapi/shared_impl
parente974ad29d8f31b1f86bb551dc99ad4e5e62b002c (diff)
downloadchromium_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/shared_impl')
-rw-r--r--ppapi/shared_impl/ppb_audio_input_shared.cc189
-rw-r--r--ppapi/shared_impl/ppb_audio_input_shared.h111
2 files changed, 265 insertions, 35 deletions
diff --git a/ppapi/shared_impl/ppb_audio_input_shared.cc b/ppapi/shared_impl/ppb_audio_input_shared.cc
index ae5b37e..3028894 100644
--- a/ppapi/shared_impl/ppb_audio_input_shared.cc
+++ b/ppapi/shared_impl/ppb_audio_input_shared.cc
@@ -1,21 +1,117 @@
-// 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.
#include "ppapi/shared_impl/ppb_audio_input_shared.h"
#include "base/logging.h"
+#include "ppapi/c/pp_errors.h"
+#include "ppapi/shared_impl/ppapi_globals.h"
+#include "ppapi/shared_impl/ppb_device_ref_shared.h"
+#include "ppapi/shared_impl/resource_tracker.h"
+#include "ppapi/thunk/enter.h"
+#include "ppapi/thunk/ppb_audio_config_api.h"
namespace ppapi {
-PPB_AudioInput_Shared::PPB_AudioInput_Shared()
- : capturing_(false),
+namespace {
+
+void IgnoredCompletionCallback(void* user_data, int32_t result) {
+ // Do nothing.
+}
+
+} // namespace
+
+PPB_AudioInput_Shared::PPB_AudioInput_Shared(const HostResource& audio_input)
+ : Resource(OBJECT_IS_PROXY, audio_input),
+ open_state_(BEFORE_OPEN),
+ capturing_(false),
+ shared_memory_size_(0),
+ audio_input_callback_(NULL),
+ user_data_(NULL),
+ devices_(NULL),
+ resource_object_type_(OBJECT_IS_PROXY) {
+}
+
+PPB_AudioInput_Shared::PPB_AudioInput_Shared(PP_Instance instance)
+ : Resource(OBJECT_IS_IMPL, instance),
+ open_state_(BEFORE_OPEN),
+ capturing_(false),
shared_memory_size_(0),
- callback_(NULL),
- user_data_(NULL) {
+ audio_input_callback_(NULL),
+ user_data_(NULL),
+ devices_(NULL),
+ resource_object_type_(OBJECT_IS_IMPL) {
}
PPB_AudioInput_Shared::~PPB_AudioInput_Shared() {
+ DCHECK(open_state_ == CLOSED);
+}
+
+thunk::PPB_AudioInput_API* PPB_AudioInput_Shared::AsPPB_AudioInput_API() {
+ return this;
+}
+
+int32_t PPB_AudioInput_Shared::EnumerateDevices(
+ PP_Resource* devices,
+ PP_CompletionCallback callback) {
+ if (!callback.func)
+ return PP_ERROR_BLOCKS_MAIN_THREAD;
+ if (TrackedCallback::IsPending(enumerate_devices_callback_))
+ return PP_ERROR_INPROGRESS;
+
+ return InternalEnumerateDevices(devices, callback);
+}
+
+int32_t PPB_AudioInput_Shared::Open(
+ const std::string& device_id,
+ PP_Resource config,
+ PPB_AudioInput_Callback audio_input_callback,
+ void* user_data,
+ PP_CompletionCallback callback) {
+ if (!audio_input_callback)
+ return PP_ERROR_BADARGUMENT;
+
+ return CommonOpen(device_id, config, audio_input_callback, user_data,
+ callback);
+}
+
+PP_Resource PPB_AudioInput_Shared::GetCurrentConfig() {
+ // AddRef for the caller.
+ if (config_.get())
+ PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(config_);
+ return config_;
+}
+
+PP_Bool PPB_AudioInput_Shared::StartCapture() {
+ if (open_state_ == CLOSED || (open_state_ == BEFORE_OPEN &&
+ !TrackedCallback::IsPending(open_callback_))) {
+ return PP_FALSE;
+ }
+ if (capturing_)
+ return PP_TRUE;
+
+ return InternalStartCapture();
+}
+
+PP_Bool PPB_AudioInput_Shared::StopCapture() {
+ if (open_state_ == CLOSED || (open_state_ == BEFORE_OPEN &&
+ !TrackedCallback::IsPending(open_callback_))) {
+ return PP_FALSE;
+ }
+ if (!capturing_)
+ return PP_TRUE;
+
+ return InternalStopCapture();
+}
+
+void PPB_AudioInput_Shared::Close() {
+ if (open_state_ == CLOSED)
+ return;
+
+ open_state_ = CLOSED;
+ InternalClose();
+
// Closing the socket causes the thread to exit - wait for it.
if (socket_.get())
socket_->Close();
@@ -23,12 +119,47 @@ PPB_AudioInput_Shared::~PPB_AudioInput_Shared() {
audio_input_thread_->Join();
audio_input_thread_.reset();
}
+
+ if (TrackedCallback::IsPending(open_callback_))
+ open_callback_->PostAbort();
}
-void PPB_AudioInput_Shared::SetCallback(PPB_AudioInput_Callback callback,
- void* user_data) {
- callback_ = callback;
- user_data_ = user_data;
+void PPB_AudioInput_Shared::OnEnumerateDevicesComplete(
+ int32_t result,
+ const std::vector<DeviceRefData>& devices) {
+ DCHECK(TrackedCallback::IsPending(enumerate_devices_callback_));
+
+ if (result == PP_OK && devices_) {
+ *devices_ = PPB_DeviceRef_Shared::CreateResourceArray(
+ resource_object_type_, pp_instance(), devices);
+ }
+ devices_ = NULL;
+
+ TrackedCallback::ClearAndRun(&enumerate_devices_callback_, result);
+}
+
+void PPB_AudioInput_Shared::OnOpenComplete(
+ int32_t result,
+ base::SharedMemoryHandle shared_memory_handle,
+ size_t shared_memory_size,
+ base::SyncSocket::Handle socket_handle) {
+ if (open_state_ == BEFORE_OPEN && result == PP_OK) {
+ open_state_ = OPENED;
+ SetStreamInfo(shared_memory_handle, shared_memory_size, socket_handle);
+ } else {
+ // Clean up the handles.
+ base::SyncSocket temp_socket(socket_handle);
+ base::SharedMemory temp_mem(shared_memory_handle, false);
+ }
+
+ // The callback may have been aborted by Close().
+ if (TrackedCallback::IsPending(open_callback_))
+ TrackedCallback::ClearAndRun(&open_callback_, result);
+}
+
+// static
+PP_CompletionCallback PPB_AudioInput_Shared::MakeIgnoredCompletionCallback() {
+ return PP_MakeCompletionCallback(&IgnoredCompletionCallback, NULL);
}
void PPB_AudioInput_Shared::SetStartCaptureState() {
@@ -37,10 +168,10 @@ void PPB_AudioInput_Shared::SetStartCaptureState() {
// If the socket doesn't exist, that means that the plugin has started before
// the browser has had a chance to create all the shared memory info and
- // notify us. This is a common case. In this case, we just set the playing_
+ // notify us. This is a common case. In this case, we just set the capturing_
// flag and the capture will automatically start when that data is available
// in SetStreamInfo.
- if (socket_.get())
+ if (audio_input_callback_ && socket_.get())
StartThread();
capturing_ = true;
}
@@ -63,17 +194,17 @@ void PPB_AudioInput_Shared::SetStreamInfo(
shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false));
shared_memory_size_ = shared_memory_size;
- if (callback_) {
+ if (audio_input_callback_) {
shared_memory_->Map(shared_memory_size_);
- // In common case StartCapture() was called before StreamCreated().
+ // StartCapture() may be called before SetSreamInfo().
if (capturing_)
StartThread();
}
}
void PPB_AudioInput_Shared::StartThread() {
- DCHECK(callback_);
+ DCHECK(audio_input_callback_);
DCHECK(!audio_input_thread_.get());
audio_input_thread_.reset(new base::DelegateSimpleThread(
this, "plugin_audio_input_thread"));
@@ -87,8 +218,36 @@ void PPB_AudioInput_Shared::Run() {
while (sizeof(pending_data) == socket_->Receive(&pending_data,
sizeof(pending_data)) &&
pending_data >= 0) {
- callback_(buffer, shared_memory_size_, user_data_);
+ audio_input_callback_(buffer, shared_memory_size_, user_data_);
}
}
+int32_t PPB_AudioInput_Shared::CommonOpen(
+ const std::string& device_id,
+ PP_Resource config,
+ PPB_AudioInput_Callback audio_input_callback,
+ void* user_data,
+ PP_CompletionCallback callback) {
+ if (open_state_ != BEFORE_OPEN)
+ return PP_ERROR_FAILED;
+
+ thunk::EnterResourceNoLock<thunk::PPB_AudioConfig_API> enter_config(config,
+ true);
+ if (enter_config.failed())
+ return PP_ERROR_BADARGUMENT;
+
+ if (!callback.func)
+ return PP_ERROR_BLOCKS_MAIN_THREAD;
+
+ if (TrackedCallback::IsPending(open_callback_))
+ return PP_ERROR_INPROGRESS;
+
+ config_ = config;
+ audio_input_callback_ = audio_input_callback;
+ user_data_ = user_data;
+
+ return InternalOpen(device_id, enter_config.object()->GetSampleRate(),
+ enter_config.object()->GetSampleFrameCount(), callback);
+}
+
} // namespace ppapi
diff --git a/ppapi/shared_impl/ppb_audio_input_shared.h b/ppapi/shared_impl/ppb_audio_input_shared.h
index 5c5e88d..5d44ddc 100644
--- a/ppapi/shared_impl/ppb_audio_input_shared.h
+++ b/ppapi/shared_impl/ppb_audio_input_shared.h
@@ -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.
@@ -6,12 +6,17 @@
#define PPAPI_SHARED_IMPL_PPB_AUDIO_INPUT_SHARED_H_
#include "base/basictypes.h"
+#include "base/compiler_specific.h"
+#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/shared_memory.h"
#include "base/sync_socket.h"
#include "base/threading/simple_thread.h"
#include "ppapi/c/dev/ppb_audio_input_dev.h"
+#include "ppapi/c/ppb_audio_config.h"
#include "ppapi/shared_impl/resource.h"
+#include "ppapi/shared_impl/scoped_pp_resource.h"
+#include "ppapi/shared_impl/tracked_callback.h"
#include "ppapi/thunk/ppb_audio_input_api.h"
namespace ppapi {
@@ -20,38 +25,77 @@ namespace ppapi {
// from the sync socket. Both the proxy and the renderer implementation use
// this code.
class PPAPI_SHARED_EXPORT PPB_AudioInput_Shared
- : public thunk::PPB_AudioInput_API,
+ : public Resource,
+ public thunk::PPB_AudioInput_API,
public base::DelegateSimpleThread::Delegate {
public:
- PPB_AudioInput_Shared();
+ // Used by the proxy.
+ explicit PPB_AudioInput_Shared(const HostResource& audio_input);
+ // Used by the impl.
+ explicit PPB_AudioInput_Shared(PP_Instance instance);
virtual ~PPB_AudioInput_Shared();
- bool capturing() const { return capturing_; }
-
- // Sets the callback information that the background thread will use. This
- // is optional. Without a callback, the thread will not be run. This
- // non-callback mode is used in the renderer with the proxy, since the proxy
- // handles the callback entirely within the plugin process.
- void SetCallback(PPB_AudioInput_Callback callback, void* user_data);
-
- // Configures the current state to be playing or not. The caller is
+ // Resource overrides.
+ virtual thunk::PPB_AudioInput_API* AsPPB_AudioInput_API() OVERRIDE;
+
+ // Implementation of PPB_AudioInput_API non-trusted methods.
+ virtual int32_t EnumerateDevices(PP_Resource* devices,
+ PP_CompletionCallback callback) OVERRIDE;
+ virtual int32_t Open(const std::string& device_id,
+ PP_Resource config,
+ PPB_AudioInput_Callback audio_input_callback,
+ void* user_data,
+ PP_CompletionCallback callback) OVERRIDE;
+ virtual PP_Resource GetCurrentConfig() OVERRIDE;
+ virtual PP_Bool StartCapture() OVERRIDE;
+ virtual PP_Bool StopCapture() OVERRIDE;
+ virtual void Close() OVERRIDE;
+
+ void OnEnumerateDevicesComplete(int32_t result,
+ const std::vector<DeviceRefData>& devices);
+ void OnOpenComplete(int32_t result,
+ base::SharedMemoryHandle shared_memory_handle,
+ size_t shared_memory_size,
+ base::SyncSocket::Handle socket_handle);
+
+ static PP_CompletionCallback MakeIgnoredCompletionCallback();
+
+ protected:
+ enum OpenState {
+ BEFORE_OPEN,
+ OPENED,
+ CLOSED
+ };
+
+ // Subclasses should implement these methods to do impl- and proxy-specific
+ // work.
+ virtual int32_t InternalEnumerateDevices(PP_Resource* devices,
+ PP_CompletionCallback callback) = 0;
+ virtual int32_t InternalOpen(const std::string& device_id,
+ PP_AudioSampleRate sample_rate,
+ uint32_t sample_frame_count,
+ PP_CompletionCallback callback) = 0;
+ virtual PP_Bool InternalStartCapture() = 0;
+ virtual PP_Bool InternalStopCapture() = 0;
+ virtual void InternalClose() = 0;
+
+ // Configures the current state to be capturing or not. The caller is
// responsible for ensuring the new state is the opposite of the current one.
//
// This is the implementation for PPB_AudioInput.Start/StopCapture, except
// that it does not actually notify the audio system to stop capture, it just
// configures our object to stop generating callbacks. The actual stop
- // playback request will be done in the derived classes and will be different
+ // capture request will be done in the derived classes and will be different
// from the proxy and the renderer.
void SetStartCaptureState();
void SetStopCaptureState();
// Sets the shared memory and socket handles. This will automatically start
- // playback if we're currently set to play.
+ // capture if we're currently set to capture.
void SetStreamInfo(base::SharedMemoryHandle shared_memory_handle,
size_t shared_memory_size,
base::SyncSocket::Handle socket_handle);
- private:
// Starts execution of the audio input thread.
void StartThread();
@@ -59,15 +103,29 @@ class PPAPI_SHARED_EXPORT PPB_AudioInput_Shared
// Run on the audio input thread.
virtual void Run();
+ // The common implementation of OpenTrusted() and Open(). It will call
+ // InternalOpen() to do impl- and proxy-specific work.
+ // OpenTrusted() will call this methods with a NULL |audio_input_callback|,
+ // in this case, the thread will not be run. This non-callback mode is used in
+ // the renderer with the proxy, since the proxy handles the callback entirely
+ // within the plugin process.
+ int32_t CommonOpen(const std::string& device_id,
+ PP_Resource config,
+ PPB_AudioInput_Callback audio_input_callback,
+ void* user_data,
+ PP_CompletionCallback callback);
+
+ OpenState open_state_;
+
// True if capturing the stream.
bool capturing_;
- // Socket used to notify us when audio is ready to accept new samples. This
- // pointer is created in StreamCreated().
+ // Socket used to notify us when new samples are available. This pointer is
+ // created in SetStreamInfo().
scoped_ptr<base::SyncSocket> socket_;
// Sample buffer in shared memory. This pointer is created in
- // StreamCreated(). The memory is only mapped when the audio thread is
+ // SetStreamInfo(). The memory is only mapped when the audio thread is
// created.
scoped_ptr<base::SharedMemory> shared_memory_;
@@ -77,12 +135,25 @@ class PPAPI_SHARED_EXPORT PPB_AudioInput_Shared
// When the callback is set, this thread is spawned for calling it.
scoped_ptr<base::DelegateSimpleThread> audio_input_thread_;
- // Callback to call when audio is ready to produce new samples.
- PPB_AudioInput_Callback callback_;
+ // Callback to call when new samples are available.
+ PPB_AudioInput_Callback audio_input_callback_;
// User data pointer passed verbatim to the callback function.
void* user_data_;
+ scoped_refptr<TrackedCallback> enumerate_devices_callback_;
+ scoped_refptr<TrackedCallback> open_callback_;
+
+ // Owning reference to the current config object. This isn't actually used,
+ // we just dish it out as requested by the plugin.
+ ScopedPPResource config_;
+
+ // Output parameter of EnumerateDevices(). It should not be accessed after
+ // |enumerate_devices_callback_| is run.
+ PP_Resource* devices_;
+
+ ResourceObjectType resource_object_type_;
+
DISALLOW_COPY_AND_ASSIGN(PPB_AudioInput_Shared);
};