Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / ppapi / proxy / audio_input_resource.h
blobe4d0e73cb7b3e8f73a7920fa9ae043eb4e8a6804
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 ppapi {
21 namespace proxy {
23 class ResourceMessageReplyParams;
25 class AudioInputResource : public PluginResource,
26 public thunk::PPB_AudioInput_API,
27 public base::DelegateSimpleThread::Delegate {
28 public:
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 EnumerateDevices(
39 const PP_ArrayOutput& output,
40 scoped_refptr<TrackedCallback> callback) OVERRIDE;
41 virtual int32_t MonitorDeviceChange(
42 PP_MonitorDeviceChangeCallback callback,
43 void* user_data) OVERRIDE;
44 virtual int32_t Open0_3(PP_Resource device_ref,
45 PP_Resource config,
46 PPB_AudioInput_Callback_0_3 audio_input_callback_0_3,
47 void* user_data,
48 scoped_refptr<TrackedCallback> callback) OVERRIDE;
49 virtual int32_t Open(PP_Resource device_ref,
50 PP_Resource config,
51 PPB_AudioInput_Callback audio_input_callback,
52 void* user_data,
53 scoped_refptr<TrackedCallback> callback) OVERRIDE;
54 virtual PP_Resource GetCurrentConfig() OVERRIDE;
55 virtual PP_Bool StartCapture() OVERRIDE;
56 virtual PP_Bool StopCapture() OVERRIDE;
57 virtual void Close() OVERRIDE;
59 protected:
60 // Resource override.
61 virtual void LastPluginRefWasDeleted() OVERRIDE;
63 private:
64 enum OpenState {
65 BEFORE_OPEN,
66 OPENED,
67 CLOSED
70 void OnPluginMsgOpenReply(const ResourceMessageReplyParams& params);
72 // Sets the shared memory and socket handles. This will automatically start
73 // capture if we're currently set to capture.
74 void SetStreamInfo(base::SharedMemoryHandle shared_memory_handle,
75 size_t shared_memory_size,
76 base::SyncSocket::Handle socket_handle);
78 // Starts execution of the audio input thread.
79 void StartThread();
81 // Stops execution of the audio input thread.
82 void StopThread();
84 // DelegateSimpleThread::Delegate implementation.
85 // Run on the audio input thread.
86 virtual void Run() OVERRIDE;
88 int32_t CommonOpen(PP_Resource device_ref,
89 PP_Resource config,
90 PPB_AudioInput_Callback_0_3 audio_input_callback_0_3,
91 PPB_AudioInput_Callback audio_input_callback,
92 void* user_data,
93 scoped_refptr<TrackedCallback> callback);
95 OpenState open_state_;
97 // True if capturing the stream.
98 bool capturing_;
100 // Socket used to notify us when new samples are available. This pointer is
101 // created in SetStreamInfo().
102 scoped_ptr<base::CancelableSyncSocket> socket_;
104 // Sample buffer in shared memory. This pointer is created in
105 // SetStreamInfo(). The memory is only mapped when the audio thread is
106 // created.
107 scoped_ptr<base::SharedMemory> shared_memory_;
109 // The size of the sample buffer in bytes.
110 size_t shared_memory_size_;
112 // When the callback is set, this thread is spawned for calling it.
113 scoped_ptr<base::DelegateSimpleThread> audio_input_thread_;
115 // Callback to call when new samples are available.
116 PPB_AudioInput_Callback_0_3 audio_input_callback_0_3_;
117 PPB_AudioInput_Callback audio_input_callback_;
119 // User data pointer passed verbatim to the callback function.
120 void* user_data_;
122 // The callback is not directly passed to OnPluginMsgOpenReply() because we
123 // would like to be able to cancel it early in Close().
124 scoped_refptr<TrackedCallback> open_callback_;
126 // Owning reference to the current config object. This isn't actually used,
127 // we just dish it out as requested by the plugin.
128 ScopedPPResource config_;
130 DeviceEnumerationResourceHelper enumeration_helper_;
132 // The data size (in bytes) of one second of audio input. Used to calculate
133 // latency.
134 size_t bytes_per_second_;
136 DISALLOW_COPY_AND_ASSIGN(AudioInputResource);
139 } // namespace proxy
140 } // namespace ppapi
142 #endif // PPAPI_PROXY_AUDIO_INPUT_RESOURCE_H_