1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef PPAPI_PROXY_AUDIO_INPUT_RESOURCE_H_
6 #define PPAPI_PROXY_AUDIO_INPUT_RESOURCE_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/shared_memory.h"
13 #include "base/sync_socket.h"
14 #include "base/threading/simple_thread.h"
15 #include "ppapi/proxy/device_enumeration_resource_helper.h"
16 #include "ppapi/proxy/plugin_resource.h"
17 #include "ppapi/shared_impl/scoped_pp_resource.h"
18 #include "ppapi/thunk/ppb_audio_input_api.h"
23 class ResourceMessageReplyParams
;
25 class AudioInputResource
: public PluginResource
,
26 public thunk::PPB_AudioInput_API
,
27 public base::DelegateSimpleThread::Delegate
{
29 AudioInputResource(Connection connection
, PP_Instance instance
);
30 virtual ~AudioInputResource();
32 // Resource overrides.
33 virtual thunk::PPB_AudioInput_API
* AsPPB_AudioInput_API() OVERRIDE
;
34 virtual void OnReplyReceived(const ResourceMessageReplyParams
& params
,
35 const IPC::Message
& msg
) OVERRIDE
;
37 // PPB_AudioInput_API implementation.
38 virtual int32_t EnumerateDevices0_2(
40 scoped_refptr
<TrackedCallback
> callback
) OVERRIDE
;
41 virtual int32_t EnumerateDevices(
42 const PP_ArrayOutput
& output
,
43 scoped_refptr
<TrackedCallback
> callback
) OVERRIDE
;
44 virtual int32_t MonitorDeviceChange(
45 PP_MonitorDeviceChangeCallback callback
,
46 void* user_data
) OVERRIDE
;
47 virtual int32_t Open0_2(PP_Resource device_ref
,
49 PPB_AudioInput_Callback_0_2 audio_input_callback_0_2
,
51 scoped_refptr
<TrackedCallback
> callback
) OVERRIDE
;
52 virtual int32_t Open(PP_Resource device_ref
,
54 PPB_AudioInput_Callback audio_input_callback
,
56 scoped_refptr
<TrackedCallback
> callback
) OVERRIDE
;
57 virtual PP_Resource
GetCurrentConfig() OVERRIDE
;
58 virtual PP_Bool
StartCapture() OVERRIDE
;
59 virtual PP_Bool
StopCapture() OVERRIDE
;
60 virtual void Close() OVERRIDE
;
64 virtual void LastPluginRefWasDeleted() OVERRIDE
;
73 void OnPluginMsgOpenReply(const ResourceMessageReplyParams
& params
);
75 // Sets the shared memory and socket handles. This will automatically start
76 // capture if we're currently set to capture.
77 void SetStreamInfo(base::SharedMemoryHandle shared_memory_handle
,
78 size_t shared_memory_size
,
79 base::SyncSocket::Handle socket_handle
);
81 // Starts execution of the audio input thread.
84 // Stops execution of the audio input thread.
87 // DelegateSimpleThread::Delegate implementation.
88 // Run on the audio input thread.
89 virtual void Run() OVERRIDE
;
91 int32_t CommonOpen(PP_Resource device_ref
,
93 PPB_AudioInput_Callback_0_2 audio_input_callback_0_2
,
94 PPB_AudioInput_Callback audio_input_callback
,
96 scoped_refptr
<TrackedCallback
> callback
);
98 OpenState open_state_
;
100 // True if capturing the stream.
103 // Socket used to notify us when new samples are available. This pointer is
104 // created in SetStreamInfo().
105 scoped_ptr
<base::CancelableSyncSocket
> socket_
;
107 // Sample buffer in shared memory. This pointer is created in
108 // SetStreamInfo(). The memory is only mapped when the audio thread is
110 scoped_ptr
<base::SharedMemory
> shared_memory_
;
112 // The size of the sample buffer in bytes.
113 size_t shared_memory_size_
;
115 // When the callback is set, this thread is spawned for calling it.
116 scoped_ptr
<base::DelegateSimpleThread
> audio_input_thread_
;
118 // Callback to call when new samples are available.
119 PPB_AudioInput_Callback_0_2 audio_input_callback_0_2_
;
120 PPB_AudioInput_Callback audio_input_callback_
;
122 // User data pointer passed verbatim to the callback function.
125 // The callback is not directly passed to OnPluginMsgOpenReply() because we
126 // would like to be able to cancel it early in Close().
127 scoped_refptr
<TrackedCallback
> open_callback_
;
129 // Owning reference to the current config object. This isn't actually used,
130 // we just dish it out as requested by the plugin.
131 ScopedPPResource config_
;
133 DeviceEnumerationResourceHelper enumeration_helper_
;
135 // The data size (in bytes) of one second of audio input. Used to calculate
137 size_t bytes_per_second_
;
139 DISALLOW_COPY_AND_ASSIGN(AudioInputResource
);
145 #endif // PPAPI_PROXY_AUDIO_INPUT_RESOURCE_H_