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

Side by Side Diff: media/audio/win/audio_low_latency_input_win.h

Issue 8283032: Low-latency AudioInputStream implementation based on WASAPI for Windows. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Added ViewHostMsg_GetHardwareInputSampleRate IPC message Created 9 years, 2 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 | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2011 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.
4 //
5 // Implementation of AudioInputStream for Windows using Windows Core Audio
6 // WASAPI for low latency capturing.
7 //
8 // Overview of operation:
9 //
10 // - An object of WASAPIAudioInputStream is created by the AudioManager
11 // factory.
12 // - Next some thread will call Open(), at that point the underlying
13 // Core Audio APIs are utilized to create two WASAPI interfaces called
14 // IAudioClient and IAudioCaptureClient.
15 // - Then some thread will call Start(sink).
16 // A thread called "wasapi_capture_thread" is started and this thread listens
17 // on an event signal which is set periodically by the audio engine for
18 // each recorded data packet. As a result, data samples will be provided
19 // to the registered sink.
20 // - At some point, a thread will call Stop(), which stops and joins the
21 // capture thread and at the same time stops audio streaming.
22 // - The same thread that called stop will call Close() where we cleanup
23 // and notify the audio manager, which likely will destroy this object.
24 //
25 // Implementation notes:
26 //
27 // - The minimum supported client is Windows Vista.
28 // - This implementation is single-threaded, hence:
29 // o Construction and destruction must take place from the same thread.
30 // o It is recommended to call all APIs from the same thread as well.
31 // - It is recommended to first acquire the native sample rate of the default
32 // input device and then use the same rate when creating this object. Use
33 // WASAPIAudioInputStream::HardwareSampleRate() to retrieve the sample rate.
34 // - Calling Close() also leads to self destruction.
35 //
36 // Core Audio API details:
37 //
38 // - CoInitializeEx() is called on the creating thread and on the internal
39 // capture thread. Each thread's concurrency model and apartment is set
40 // to multi-threaded (MTA). CHECK() is called to ensure that we crash if
41 // CoInitializeEx(MTA) fails.
42 // - Utilized MMDevice interfaces:
43 // o IMMDeviceEnumerator
44 // o IMMDevice
45 // - Utilized WASAPI interfaces:
46 // o IAudioClient
47 // o IAudioCaptureClient
48 // - The stream is initialized in shared mode and the processing of the
49 // audio buffer is event driven.
50 // - The Multimedia Class Scheduler service (MMCSS) is utilized to boost
51 // the priority of the capture thread.
52 //
53 #ifndef MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_INPUT_WIN_H_
54 #define MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_INPUT_WIN_H_
55
56 #include <Audioclient.h>
57 #include <MMDeviceAPI.h>
58
59 #include "base/compiler_specific.h"
60 #include "base/threading/platform_thread.h"
61 #include "base/threading/simple_thread.h"
62 #include "base/win/scoped_comptr.h"
63 #include "base/win/scoped_handle.h"
64 #include "media/audio/audio_io.h"
65 #include "media/audio/audio_parameters.h"
66
67 class AudioManagerWin;
68
69 // Initializes COM in the constructor (MTA), and uninitializes COM in the
70 // destructor.
71 class ScopedCOMInitializerMTA {
72 public:
73 ScopedCOMInitializerMTA() : hr_(CoInitializeEx(NULL, COINIT_MULTITHREADED)) {
74 CHECK(SUCCEEDED(hr_));
75 #ifndef NDEBUG
76 creating_thread_id_ = base::PlatformThread::CurrentId();
77 #endif
78 }
79
80 ScopedCOMInitializerMTA::~ScopedCOMInitializerMTA() {
81 #ifndef NDEBUG
82 DCHECK_EQ(base::PlatformThread::CurrentId(), creating_thread_id_);
83 #endif
84 if (SUCCEEDED(hr_))
85 CoUninitialize();
86 }
87
88 private:
89 HRESULT hr_;
90 #ifndef NDEBUG
91 base::PlatformThreadId creating_thread_id_;
92 #endif
93 DISALLOW_COPY_AND_ASSIGN(ScopedCOMInitializerMTA);
94 };
95
96 // A convenience class for COM memory handling.
97 template <class T>
98 class ScopedComMem {
99 public:
100 ScopedComMem() : ptr_(NULL) {}
101 ~ScopedComMem() {
102 Free();
103 }
104
105 T* operator->() {
106 DCHECK(ptr_ != NULL);
107 return ptr_;
108 }
109
110 T* get() const { return ptr_; }
111
112 T** Receive() {
113 DCHECK(ptr_ == NULL);
114 return &ptr_;
115 }
116
117 void Free() {
118 if (ptr_) {
119 ::CoTaskMemFree(ptr_);
120 ptr_ = NULL;
121 }
122 }
123
124 bool valid() const { return ptr_ != NULL; }
125
126 protected:
127 T* ptr_;
128
129 private:
130 DISALLOW_COPY_AND_ASSIGN(ScopedComMem);
131 };
132
133 // AudioInputStream implementation using Windows Core Audio APIs.
134 class WASAPIAudioInputStream : public AudioInputStream,
135 public base::DelegateSimpleThread::Delegate {
136 public:
137 // The ctor takes all the usual parameters, plus |manager| which is the
138 // the audio manager who is creating this object.
139 WASAPIAudioInputStream(AudioManagerWin* manager,
140 const AudioParameters& params,
141 ERole device_role);
142 // The dtor is typically called by the AudioManager only and it is usually
143 // triggered by calling AudioInputStream::Close().
144 virtual ~WASAPIAudioInputStream();
145
146 // Implementation of AudioInputStream.
147 virtual bool Open() OVERRIDE;
148 virtual void Start(AudioInputCallback* callback) OVERRIDE;
149 virtual void Stop() OVERRIDE;
150 virtual void Close() OVERRIDE;
151
152 // Retrieve the stream format that the audio engine uses for its internal
153 // processing/mixing of shared-mode streams.
154 static double HardwareSampleRate(ERole device_role);
155
156 bool started() const { return started_; }
157
158 private:
159 // DelegateSimpleThread::Delegate implementation.
160 virtual void Run() OVERRIDE;
161
162 // Issues the OnError() callback to the |sink_|.
163 void HandleError(HRESULT err);
164
165 // The Open() method is divided into these sub methods.
166 HRESULT SetCaptureDevice(ERole device_role);
167 HRESULT ActivateCaptureDevice();
168 HRESULT GetAudioEngineStreamFormat();
169 bool DesiredFormatIsSupported();
170 HRESULT InitializeAudioEngine();
171
172 // Initializes the COM library for use by the calling thread and set the
173 // thread's concurrency model to multi-threaded.
174 ScopedCOMInitializerMTA com_init_;
175
176 // Our creator, the audio manager needs to be notified when we close.
177 AudioManagerWin* manager_;
178
179 // Capturing is driven by this thread (which has no message loop).
180 // All OnData() callbacks will be called from this thread.
181 base::DelegateSimpleThread* capture_thread_;
182
183 // Contains the desired audio format which is set up at construction.
184 WAVEFORMATEX format_;
185
186 // Copy of the audio format which we know the audio engine supports.
187 // It is recommended to ensure that the sample rate in |format_| is identical
188 // to the sample rate in |audio_engine_mix_format_|.
189 ScopedComMem<WAVEFORMATEX> audio_engine_mix_format_;
190
191 bool opened_;
192 bool started_;
193
194 // Size in bytes of each audio frame (4 bytes for 16-bit stereo PCM)
195 size_t frame_size_;
196
197 // Size in audio frames of each audio packet where an audio packet
198 // is defined as the block of data which the user received in each
199 // OnData() callback.
200 size_t packet_size_frames_;
201
202 // Size in bytes of each audio packet.
203 size_t packet_size_bytes_;
204
205 // Length of the audio endpoint buffer.
206 size_t endpoint_buffer_size_frames_;
207
208 // Defines the role that the system has assigned to an audio endpoint device.
209 ERole device_role_;
210
211 // Conversion factor used in delay-estimation calculations.
212 // Converts a raw performance counter value to 100-nanosecond unit.
213 double perf_count_to_100ns_units_;
214
215 // Conversion factor used in delay-estimation calculations.
216 // Converts from milliseconds to audio frames.
217 double ms_to_frame_count_;
218
219 // Pointer to the object that will receive the recorded audio samples.
220 AudioInputCallback* sink_;
221
222 // An IMMDevice interface which represents an audio endpoint device.
223 base::win::ScopedComPtr<IMMDevice> endpoint_device_;
224
225 // An IAudioClient interface which enables a client to create and initialize
226 // an audio stream between an audio application and the audio engine.
227 base::win::ScopedComPtr<IAudioClient> audio_client_;
228
229 // The IAudioCaptureClient interface enables a client to read input data
230 // from a capture endpoint buffer.
231 base::win::ScopedComPtr<IAudioCaptureClient> audio_capture_client_;
232
233 // The audio engine will signal this event each time a buffer has been
234 // recorded.
235 base::win::ScopedHandle audio_samples_ready_event_;
236
237 // This event will be signaled when capturing shall stop.
238 base::win::ScopedHandle stop_capture_event_;
239
240 DISALLOW_COPY_AND_ASSIGN(WASAPIAudioInputStream);
241 };
242
243 #endif // MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_INPUT_WIN_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698