Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: media/audio/audio_input_controller.h

Issue 21183002: Adding key press detection in the browser process. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/DEPS ('k') | media/audio/audio_input_controller.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MEDIA_AUDIO_AUDIO_INPUT_CONTROLLER_H_ 5 #ifndef MEDIA_AUDIO_AUDIO_INPUT_CONTROLLER_H_
6 #define MEDIA_AUDIO_AUDIO_INPUT_CONTROLLER_H_ 6 #define MEDIA_AUDIO_AUDIO_INPUT_CONTROLLER_H_
7 7
8 #include <string> 8 #include <string>
9 #include "base/atomicops.h" 9 #include "base/atomicops.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/synchronization/lock.h" 13 #include "base/synchronization/lock.h"
14 #include "base/synchronization/waitable_event.h" 14 #include "base/synchronization/waitable_event.h"
15 #include "base/threading/thread.h" 15 #include "base/threading/thread.h"
16 #include "base/timer/timer.h" 16 #include "base/timer/timer.h"
17 #include "media/audio/audio_io.h" 17 #include "media/audio/audio_io.h"
18 #include "media/audio/audio_manager_base.h" 18 #include "media/audio/audio_manager_base.h"
19 #include "media/base/user_input_monitor.h"
19 20
20 // An AudioInputController controls an AudioInputStream and records data 21 // An AudioInputController controls an AudioInputStream and records data
21 // from this input stream. The two main methods are Record() and Close() and 22 // from this input stream. The two main methods are Record() and Close() and
22 // they are both executed on the audio thread which is injected by the two 23 // they are both executed on the audio thread which is injected by the two
23 // alternative factory methods, Create() or CreateLowLatency(). 24 // alternative factory methods, Create() or CreateLowLatency().
24 // 25 //
25 // All public methods of AudioInputController are non-blocking. 26 // All public methods of AudioInputController are non-blocking.
26 // 27 //
27 // Here is a state diagram for the AudioInputController: 28 // Here is a state diagram for the AudioInputController:
28 // 29 //
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 // 68 //
68 // The audio thread itself is owned by the AudioManager that the 69 // The audio thread itself is owned by the AudioManager that the
69 // AudioInputController holds a reference to. When performing tasks on the 70 // AudioInputController holds a reference to. When performing tasks on the
70 // audio thread, the controller must not add or release references to the 71 // audio thread, the controller must not add or release references to the
71 // AudioManager or itself (since it in turn holds a reference to the manager). 72 // AudioManager or itself (since it in turn holds a reference to the manager).
72 // 73 //
73 namespace media { 74 namespace media {
74 75
75 class MEDIA_EXPORT AudioInputController 76 class MEDIA_EXPORT AudioInputController
76 : public base::RefCountedThreadSafe<AudioInputController>, 77 : public base::RefCountedThreadSafe<AudioInputController>,
77 public AudioInputStream::AudioInputCallback { 78 public AudioInputStream::AudioInputCallback,
79 public UserInputMonitor::KeyStrokeListener {
78 public: 80 public:
79 // An event handler that receives events from the AudioInputController. The 81 // An event handler that receives events from the AudioInputController. The
80 // following methods are all called on the audio thread. 82 // following methods are all called on the audio thread.
81 class MEDIA_EXPORT EventHandler { 83 class MEDIA_EXPORT EventHandler {
82 public: 84 public:
83 virtual void OnCreated(AudioInputController* controller) = 0; 85 virtual void OnCreated(AudioInputController* controller) = 0;
84 virtual void OnRecording(AudioInputController* controller) = 0; 86 virtual void OnRecording(AudioInputController* controller) = 0;
85 virtual void OnError(AudioInputController* controller) = 0; 87 virtual void OnError(AudioInputController* controller) = 0;
86 virtual void OnData(AudioInputController* controller, const uint8* data, 88 virtual void OnData(AudioInputController* controller, const uint8* data,
87 uint32 size) = 0; 89 uint32 size) = 0;
88 90
89 protected: 91 protected:
90 virtual ~EventHandler() {} 92 virtual ~EventHandler() {}
91 }; 93 };
92 94
93 // A synchronous writer interface used by AudioInputController for 95 // A synchronous writer interface used by AudioInputController for
94 // synchronous writing. 96 // synchronous writing.
95 class SyncWriter { 97 class SyncWriter {
96 public: 98 public:
97 virtual ~SyncWriter() {} 99 virtual ~SyncWriter() {}
98 100
99 // Notify the synchronous writer about the number of bytes in the 101 // Notify the synchronous writer about the number of bytes in the
100 // soundcard which has been recorded. 102 // soundcard which has been recorded.
101 virtual void UpdateRecordedBytes(uint32 bytes) = 0; 103 virtual void UpdateRecordedBytes(uint32 bytes) = 0;
102 104
103 // Write certain amount of data from |data|. This method returns 105 // Write certain amount of data from |data|. This method returns
104 // number of written bytes. 106 // number of written bytes.
105 virtual uint32 Write(const void* data, uint32 size, double volume) = 0; 107 virtual uint32 Write(const void* data,
108 uint32 size,
109 double volume,
110 bool key_pressed) = 0;
106 111
107 // Close this synchronous writer. 112 // Close this synchronous writer.
108 virtual void Close() = 0; 113 virtual void Close() = 0;
109 }; 114 };
110 115
111 // AudioInputController::Create() can use the currently registered Factory 116 // AudioInputController::Create() can use the currently registered Factory
112 // to create the AudioInputController. Factory is intended for testing only. 117 // to create the AudioInputController. Factory is intended for testing only.
118 // |user_input_monitor| is used for typing detection and can be NULL.
113 class Factory { 119 class Factory {
114 public: 120 public:
115 virtual AudioInputController* Create(AudioManager* audio_manager, 121 virtual AudioInputController* Create(
116 EventHandler* event_handler, 122 AudioManager* audio_manager,
117 AudioParameters params) = 0; 123 EventHandler* event_handler,
124 AudioParameters params,
125 UserInputMonitor* user_input_monitor) = 0;
126
118 protected: 127 protected:
119 virtual ~Factory() {} 128 virtual ~Factory() {}
120 }; 129 };
121 130
122 // Factory method for creating an AudioInputController. 131 // Factory method for creating an AudioInputController.
123 // The audio device will be created on the audio thread, and when that is 132 // The audio device will be created on the audio thread, and when that is
124 // done, the event handler will receive an OnCreated() call from that same 133 // done, the event handler will receive an OnCreated() call from that same
125 // thread. |device_id| is the unique ID of the audio device to be opened. 134 // thread. |device_id| is the unique ID of the audio device to be opened.
135 // |user_input_monitor| is used for typing detection and can be NULL.
126 static scoped_refptr<AudioInputController> Create( 136 static scoped_refptr<AudioInputController> Create(
127 AudioManager* audio_manager, 137 AudioManager* audio_manager,
128 EventHandler* event_handler, 138 EventHandler* event_handler,
129 const AudioParameters& params, 139 const AudioParameters& params,
130 const std::string& device_id); 140 const std::string& device_id,
141 UserInputMonitor* user_input_monitor);
131 142
132 // Sets the factory used by the static method Create(). AudioInputController 143 // Sets the factory used by the static method Create(). AudioInputController
133 // does not take ownership of |factory|. A value of NULL results in an 144 // does not take ownership of |factory|. A value of NULL results in an
134 // AudioInputController being created directly. 145 // AudioInputController being created directly.
135 static void set_factory_for_testing(Factory* factory) { factory_ = factory; } 146 static void set_factory_for_testing(Factory* factory) { factory_ = factory; }
136 AudioInputStream* stream_for_testing() { return stream_; } 147 AudioInputStream* stream_for_testing() { return stream_; }
137 148
138 // Factory method for creating an AudioInputController for low-latency mode. 149 // Factory method for creating an AudioInputController for low-latency mode.
139 // The audio device will be created on the audio thread, and when that is 150 // The audio device will be created on the audio thread, and when that is
140 // done, the event handler will receive an OnCreated() call from that same 151 // done, the event handler will receive an OnCreated() call from that same
141 // thread. 152 // thread. |user_input_monitor| is used for typing detection and can be NULL.
142 static scoped_refptr<AudioInputController> CreateLowLatency( 153 static scoped_refptr<AudioInputController> CreateLowLatency(
143 AudioManager* audio_manager, 154 AudioManager* audio_manager,
144 EventHandler* event_handler, 155 EventHandler* event_handler,
145 const AudioParameters& params, 156 const AudioParameters& params,
146 const std::string& device_id, 157 const std::string& device_id,
147 // External synchronous writer for audio controller. 158 // External synchronous writer for audio controller.
148 SyncWriter* sync_writer); 159 SyncWriter* sync_writer,
160 UserInputMonitor* user_input_monitor);
149 161
150 // Factory method for creating an AudioInputController for low-latency mode, 162 // Factory method for creating an AudioInputController for low-latency mode,
151 // taking ownership of |stream|. The stream will be opened on the audio 163 // taking ownership of |stream|. The stream will be opened on the audio
152 // thread, and when that is done, the event handler will receive an 164 // thread, and when that is done, the event handler will receive an
153 // OnCreated() call from that same thread. 165 // OnCreated() call from that same thread. |user_input_monitor| is used for
166 // typing detection and can be NULL.
154 static scoped_refptr<AudioInputController> CreateForStream( 167 static scoped_refptr<AudioInputController> CreateForStream(
155 const scoped_refptr<base::MessageLoopProxy>& message_loop, 168 const scoped_refptr<base::MessageLoopProxy>& message_loop,
156 EventHandler* event_handler, 169 EventHandler* event_handler,
157 AudioInputStream* stream, 170 AudioInputStream* stream,
158 // External synchronous writer for audio controller. 171 // External synchronous writer for audio controller.
159 SyncWriter* sync_writer); 172 SyncWriter* sync_writer,
173 UserInputMonitor* user_input_monitor);
160 174
161 // Starts recording using the created audio input stream. 175 // Starts recording using the created audio input stream.
162 // This method is called on the creator thread. 176 // This method is called on the creator thread.
163 virtual void Record(); 177 virtual void Record();
164 178
165 // Closes the audio input stream. The state is changed and the resources 179 // Closes the audio input stream. The state is changed and the resources
166 // are freed on the audio thread. |closed_task| is then executed on the thread 180 // are freed on the audio thread. |closed_task| is then executed on the thread
167 // that called Close(). 181 // that called Close().
168 // Callbacks (EventHandler and SyncWriter) must exist until |closed_task| 182 // Callbacks (EventHandler and SyncWriter) must exist until |closed_task|
169 // is called. 183 // is called.
(...skipping 12 matching lines...) Expand all
182 196
183 // AudioInputCallback implementation. Threading details depends on the 197 // AudioInputCallback implementation. Threading details depends on the
184 // device-specific implementation. 198 // device-specific implementation.
185 virtual void OnData(AudioInputStream* stream, const uint8* src, uint32 size, 199 virtual void OnData(AudioInputStream* stream, const uint8* src, uint32 size,
186 uint32 hardware_delay_bytes, double volume) OVERRIDE; 200 uint32 hardware_delay_bytes, double volume) OVERRIDE;
187 virtual void OnClose(AudioInputStream* stream) OVERRIDE; 201 virtual void OnClose(AudioInputStream* stream) OVERRIDE;
188 virtual void OnError(AudioInputStream* stream) OVERRIDE; 202 virtual void OnError(AudioInputStream* stream) OVERRIDE;
189 203
190 bool LowLatencyMode() const { return sync_writer_ != NULL; } 204 bool LowLatencyMode() const { return sync_writer_ != NULL; }
191 205
206 // Impl of KeyStrokeListener.
207 virtual void OnKeyStroke() OVERRIDE;
208
192 protected: 209 protected:
193 friend class base::RefCountedThreadSafe<AudioInputController>; 210 friend class base::RefCountedThreadSafe<AudioInputController>;
194 211
195 // Internal state of the source. 212 // Internal state of the source.
196 enum State { 213 enum State {
197 kEmpty, 214 kEmpty,
198 kCreated, 215 kCreated,
199 kRecording, 216 kRecording,
200 kClosed, 217 kClosed,
201 kError 218 kError
202 }; 219 };
203 220
204 AudioInputController(EventHandler* handler, SyncWriter* sync_writer); 221 AudioInputController(EventHandler* handler,
222 SyncWriter* sync_writer,
223 UserInputMonitor* user_input_monitor);
205 virtual ~AudioInputController(); 224 virtual ~AudioInputController();
206 225
207 // Methods called on the audio thread (owned by the AudioManager). 226 // Methods called on the audio thread (owned by the AudioManager).
208 void DoCreate(AudioManager* audio_manager, const AudioParameters& params, 227 void DoCreate(AudioManager* audio_manager, const AudioParameters& params,
209 const std::string& device_id); 228 const std::string& device_id);
210 void DoCreateForStream(AudioInputStream* stream_to_control, 229 void DoCreateForStream(AudioInputStream* stream_to_control,
211 bool enable_nodata_timer); 230 bool enable_nodata_timer);
212 void DoRecord(); 231 void DoRecord();
213 void DoClose(); 232 void DoClose();
214 void DoReportError(); 233 void DoReportError();
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 278
260 base::Lock lock_; 279 base::Lock lock_;
261 280
262 // SyncWriter is used only in low-latency mode for synchronous writing. 281 // SyncWriter is used only in low-latency mode for synchronous writing.
263 SyncWriter* sync_writer_; 282 SyncWriter* sync_writer_;
264 283
265 static Factory* factory_; 284 static Factory* factory_;
266 285
267 double max_volume_; 286 double max_volume_;
268 287
288 UserInputMonitor* user_input_monitor_;
289
290 // True if any key has been pressed after the last OnData call.
291 bool key_pressed_;
292
269 DISALLOW_COPY_AND_ASSIGN(AudioInputController); 293 DISALLOW_COPY_AND_ASSIGN(AudioInputController);
270 }; 294 };
271 295
272 } // namespace media 296 } // namespace media
273 297
274 #endif // MEDIA_AUDIO_AUDIO_INPUT_CONTROLLER_H_ 298 #endif // MEDIA_AUDIO_AUDIO_INPUT_CONTROLLER_H_
OLDNEW
« no previous file with comments | « media/DEPS ('k') | media/audio/audio_input_controller.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698