Elim cr-checkbox
[chromium-blink-merge.git] / ppapi / proxy / audio_input_resource.h
bloba94ff9c5e3e38f4b85c030e90c68b99c13439cdc
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"
20 namespace media {
21 class AudioBus;
24 namespace ppapi {
25 namespace proxy {
27 class ResourceMessageReplyParams;
29 class AudioInputResource : public PluginResource,
30 public thunk::PPB_AudioInput_API,
31 public base::DelegateSimpleThread::Delegate {
32 public:
33 AudioInputResource(Connection connection, PP_Instance instance);
34 ~AudioInputResource() override;
36 // Resource overrides.
37 thunk::PPB_AudioInput_API* AsPPB_AudioInput_API() override;
38 void OnReplyReceived(const ResourceMessageReplyParams& params,
39 const IPC::Message& msg) override;
41 // PPB_AudioInput_API implementation.
42 int32_t EnumerateDevices(const PP_ArrayOutput& output,
43 scoped_refptr<TrackedCallback> callback) override;
44 int32_t MonitorDeviceChange(PP_MonitorDeviceChangeCallback callback,
45 void* user_data) override;
46 int32_t Open0_3(PP_Resource device_ref,
47 PP_Resource config,
48 PPB_AudioInput_Callback_0_3 audio_input_callback_0_3,
49 void* user_data,
50 scoped_refptr<TrackedCallback> callback) override;
51 int32_t Open(PP_Resource device_ref,
52 PP_Resource config,
53 PPB_AudioInput_Callback audio_input_callback,
54 void* user_data,
55 scoped_refptr<TrackedCallback> callback) override;
56 PP_Resource GetCurrentConfig() override;
57 PP_Bool StartCapture() override;
58 PP_Bool StopCapture() override;
59 void Close() override;
61 protected:
62 // Resource override.
63 void LastPluginRefWasDeleted() override;
65 private:
66 enum OpenState {
67 BEFORE_OPEN,
68 OPENED,
69 CLOSED
72 void OnPluginMsgOpenReply(const ResourceMessageReplyParams& params);
74 // Sets the shared memory and socket handles. This will automatically start
75 // capture if we're currently set to capture.
76 void SetStreamInfo(base::SharedMemoryHandle shared_memory_handle,
77 size_t shared_memory_size,
78 base::SyncSocket::Handle socket_handle);
80 // Starts execution of the audio input thread.
81 void StartThread();
83 // Stops execution of the audio input thread.
84 void StopThread();
86 // DelegateSimpleThread::Delegate implementation.
87 // Run on the audio input thread.
88 void Run() override;
90 int32_t CommonOpen(PP_Resource device_ref,
91 PP_Resource config,
92 PPB_AudioInput_Callback_0_3 audio_input_callback_0_3,
93 PPB_AudioInput_Callback audio_input_callback,
94 void* user_data,
95 scoped_refptr<TrackedCallback> callback);
97 OpenState open_state_;
99 // True if capturing the stream.
100 bool capturing_;
102 // Socket used to notify us when new samples are available. This pointer is
103 // created in SetStreamInfo().
104 scoped_ptr<base::CancelableSyncSocket> socket_;
106 // Sample buffer in shared memory. This pointer is created in
107 // SetStreamInfo(). The memory is only mapped when the audio thread is
108 // created.
109 scoped_ptr<base::SharedMemory> shared_memory_;
111 // The size of the sample buffer in bytes.
112 size_t shared_memory_size_;
114 // When the callback is set, this thread is spawned for calling it.
115 scoped_ptr<base::DelegateSimpleThread> audio_input_thread_;
117 // Callback to call when new samples are available.
118 PPB_AudioInput_Callback_0_3 audio_input_callback_0_3_;
119 PPB_AudioInput_Callback audio_input_callback_;
121 // User data pointer passed verbatim to the callback function.
122 void* user_data_;
124 // The callback is not directly passed to OnPluginMsgOpenReply() because we
125 // would like to be able to cancel it early in Close().
126 scoped_refptr<TrackedCallback> open_callback_;
128 // Owning reference to the current config object. This isn't actually used,
129 // we just dish it out as requested by the plugin.
130 ScopedPPResource config_;
132 DeviceEnumerationResourceHelper enumeration_helper_;
134 // The data size (in bytes) of one second of audio input. Used to calculate
135 // latency.
136 size_t bytes_per_second_;
138 // AudioBus for shuttling data across the shared memory.
139 scoped_ptr<media::AudioBus> audio_bus_;
140 int sample_frame_count_;
142 // Internal buffer for client's integer audio data.
143 int client_buffer_size_bytes_;
144 scoped_ptr<uint8_t[]> client_buffer_;
146 DISALLOW_COPY_AND_ASSIGN(AudioInputResource);
149 } // namespace proxy
150 } // namespace ppapi
152 #endif // PPAPI_PROXY_AUDIO_INPUT_RESOURCE_H_