OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #ifndef WEBRTC_AUDIO_DEVICE_AUDIO_DEVICE_WAVE_WIN_H | |
12 #define WEBRTC_AUDIO_DEVICE_AUDIO_DEVICE_WAVE_WIN_H | |
13 | |
14 #include <memory> | |
15 | |
16 #include "webrtc/base/platform_thread.h" | |
17 #include "webrtc/modules/audio_device/audio_device_generic.h" | |
18 #include "webrtc/modules/audio_device/win/audio_mixer_manager_win.h" | |
19 | |
20 #pragma comment( lib, "winmm.lib" ) | |
21 | |
22 namespace webrtc { | |
23 class EventTimerWrapper; | |
24 class EventWrapper; | |
25 | |
26 const uint32_t TIMER_PERIOD_MS = 2; | |
27 const uint32_t REC_CHECK_TIME_PERIOD_MS = 4; | |
28 const uint16_t REC_PUT_BACK_DELAY = 4; | |
29 | |
30 const uint32_t N_REC_SAMPLES_PER_SEC = 48000; | |
31 const uint32_t N_PLAY_SAMPLES_PER_SEC = 48000; | |
32 | |
33 const uint32_t N_REC_CHANNELS = 1; // default is mono recording | |
34 const uint32_t N_PLAY_CHANNELS = 2; // default is stereo playout | |
35 | |
36 // NOTE - CPU load will not be correct for other sizes than 10ms | |
37 const uint32_t REC_BUF_SIZE_IN_SAMPLES = (N_REC_SAMPLES_PER_SEC/100); | |
38 const uint32_t PLAY_BUF_SIZE_IN_SAMPLES = (N_PLAY_SAMPLES_PER_SEC/100); | |
39 | |
40 enum { N_BUFFERS_IN = 200 }; | |
41 enum { N_BUFFERS_OUT = 200 }; | |
42 | |
43 class AudioDeviceWindowsWave : public AudioDeviceGeneric | |
44 { | |
45 public: | |
46 AudioDeviceWindowsWave(const int32_t id); | |
47 ~AudioDeviceWindowsWave(); | |
48 | |
49 // Retrieve the currently utilized audio layer | |
50 virtual int32_t ActiveAudioLayer(AudioDeviceModule::AudioLayer& audioLayer)
const; | |
51 | |
52 // Main initializaton and termination | |
53 virtual InitStatus Init(); | |
54 virtual int32_t Terminate(); | |
55 virtual bool Initialized() const; | |
56 | |
57 // Device enumeration | |
58 virtual int16_t PlayoutDevices(); | |
59 virtual int16_t RecordingDevices(); | |
60 virtual int32_t PlayoutDeviceName( | |
61 uint16_t index, | |
62 char name[kAdmMaxDeviceNameSize], | |
63 char guid[kAdmMaxGuidSize]); | |
64 virtual int32_t RecordingDeviceName( | |
65 uint16_t index, | |
66 char name[kAdmMaxDeviceNameSize], | |
67 char guid[kAdmMaxGuidSize]); | |
68 | |
69 // Device selection | |
70 virtual int32_t SetPlayoutDevice(uint16_t index); | |
71 virtual int32_t SetPlayoutDevice(AudioDeviceModule::WindowsDeviceType device
); | |
72 virtual int32_t SetRecordingDevice(uint16_t index); | |
73 virtual int32_t SetRecordingDevice(AudioDeviceModule::WindowsDeviceType devi
ce); | |
74 | |
75 // Audio transport initialization | |
76 virtual int32_t PlayoutIsAvailable(bool& available); | |
77 virtual int32_t InitPlayout(); | |
78 virtual bool PlayoutIsInitialized() const; | |
79 virtual int32_t RecordingIsAvailable(bool& available); | |
80 virtual int32_t InitRecording(); | |
81 virtual bool RecordingIsInitialized() const; | |
82 | |
83 // Audio transport control | |
84 virtual int32_t StartPlayout(); | |
85 virtual int32_t StopPlayout(); | |
86 virtual bool Playing() const; | |
87 virtual int32_t StartRecording(); | |
88 virtual int32_t StopRecording(); | |
89 virtual bool Recording() const; | |
90 | |
91 // Microphone Automatic Gain Control (AGC) | |
92 virtual int32_t SetAGC(bool enable); | |
93 virtual bool AGC() const; | |
94 | |
95 // Volume control based on the Windows Wave API (Windows only) | |
96 virtual int32_t SetWaveOutVolume(uint16_t volumeLeft, uint16_t volumeRight); | |
97 virtual int32_t WaveOutVolume(uint16_t& volumeLeft, uint16_t& volumeRight) c
onst; | |
98 | |
99 // Audio mixer initialization | |
100 virtual int32_t InitSpeaker(); | |
101 virtual bool SpeakerIsInitialized() const; | |
102 virtual int32_t InitMicrophone(); | |
103 virtual bool MicrophoneIsInitialized() const; | |
104 | |
105 // Speaker volume controls | |
106 virtual int32_t SpeakerVolumeIsAvailable(bool& available); | |
107 virtual int32_t SetSpeakerVolume(uint32_t volume); | |
108 virtual int32_t SpeakerVolume(uint32_t& volume) const; | |
109 virtual int32_t MaxSpeakerVolume(uint32_t& maxVolume) const; | |
110 virtual int32_t MinSpeakerVolume(uint32_t& minVolume) const; | |
111 virtual int32_t SpeakerVolumeStepSize(uint16_t& stepSize) const; | |
112 | |
113 // Microphone volume controls | |
114 virtual int32_t MicrophoneVolumeIsAvailable(bool& available); | |
115 virtual int32_t SetMicrophoneVolume(uint32_t volume); | |
116 virtual int32_t MicrophoneVolume(uint32_t& volume) const; | |
117 virtual int32_t MaxMicrophoneVolume(uint32_t& maxVolume) const; | |
118 virtual int32_t MinMicrophoneVolume(uint32_t& minVolume) const; | |
119 virtual int32_t MicrophoneVolumeStepSize(uint16_t& stepSize) const; | |
120 | |
121 // Speaker mute control | |
122 virtual int32_t SpeakerMuteIsAvailable(bool& available); | |
123 virtual int32_t SetSpeakerMute(bool enable); | |
124 virtual int32_t SpeakerMute(bool& enabled) const; | |
125 | |
126 // Microphone mute control | |
127 virtual int32_t MicrophoneMuteIsAvailable(bool& available); | |
128 virtual int32_t SetMicrophoneMute(bool enable); | |
129 virtual int32_t MicrophoneMute(bool& enabled) const; | |
130 | |
131 // Microphone boost control | |
132 virtual int32_t MicrophoneBoostIsAvailable(bool& available); | |
133 virtual int32_t SetMicrophoneBoost(bool enable); | |
134 virtual int32_t MicrophoneBoost(bool& enabled) const; | |
135 | |
136 // Stereo support | |
137 virtual int32_t StereoPlayoutIsAvailable(bool& available); | |
138 virtual int32_t SetStereoPlayout(bool enable); | |
139 virtual int32_t StereoPlayout(bool& enabled) const; | |
140 virtual int32_t StereoRecordingIsAvailable(bool& available); | |
141 virtual int32_t SetStereoRecording(bool enable); | |
142 virtual int32_t StereoRecording(bool& enabled) const; | |
143 | |
144 // Delay information and control | |
145 virtual int32_t SetPlayoutBuffer(const AudioDeviceModule::BufferType type, u
int16_t sizeMS); | |
146 virtual int32_t PlayoutBuffer(AudioDeviceModule::BufferType& type, uint16_t&
sizeMS) const; | |
147 virtual int32_t PlayoutDelay(uint16_t& delayMS) const; | |
148 virtual int32_t RecordingDelay(uint16_t& delayMS) const; | |
149 | |
150 // CPU load | |
151 virtual int32_t CPULoad(uint16_t& load) const; | |
152 | |
153 public: | |
154 virtual bool PlayoutWarning() const; | |
155 virtual bool PlayoutError() const; | |
156 virtual bool RecordingWarning() const; | |
157 virtual bool RecordingError() const; | |
158 virtual void ClearPlayoutWarning(); | |
159 virtual void ClearPlayoutError(); | |
160 virtual void ClearRecordingWarning(); | |
161 virtual void ClearRecordingError(); | |
162 | |
163 public: | |
164 virtual void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer); | |
165 | |
166 private: | |
167 void Lock() { _critSect.Enter(); }; | |
168 void UnLock() { _critSect.Leave(); }; | |
169 int32_t Id() {return _id;} | |
170 bool IsUsingOutputDeviceIndex() const {return _usingOutputDeviceIndex;} | |
171 AudioDeviceModule::WindowsDeviceType OutputDevice() const {return _outputDev
ice;} | |
172 uint16_t OutputDeviceIndex() const {return _outputDeviceIndex;} | |
173 bool IsUsingInputDeviceIndex() const {return _usingInputDeviceIndex;} | |
174 AudioDeviceModule::WindowsDeviceType InputDevice() const {return _inputDevic
e;} | |
175 uint16_t InputDeviceIndex() const {return _inputDeviceIndex;} | |
176 | |
177 private: | |
178 inline int32_t InputSanityCheckAfterUnlockedPeriod() const; | |
179 inline int32_t OutputSanityCheckAfterUnlockedPeriod() const; | |
180 | |
181 private: | |
182 bool KeyPressed() const; | |
183 | |
184 private: | |
185 int32_t EnumeratePlayoutDevices(); | |
186 int32_t EnumerateRecordingDevices(); | |
187 void TraceSupportFlags(DWORD dwSupport) const; | |
188 void TraceWaveInError(MMRESULT error) const; | |
189 void TraceWaveOutError(MMRESULT error) const; | |
190 int32_t PrepareStartRecording(); | |
191 int32_t PrepareStartPlayout(); | |
192 | |
193 int32_t RecProc(LONGLONG& consumedTime); | |
194 int PlayProc(LONGLONG& consumedTime); | |
195 | |
196 int32_t GetPlayoutBufferDelay(uint32_t& writtenSamples, uint32_t& playedSamp
les); | |
197 int32_t GetRecordingBufferDelay(uint32_t& readSamples, uint32_t& recSamples)
; | |
198 int32_t Write(int8_t* data, uint16_t nSamples); | |
199 int32_t GetClockDrift(const uint32_t plSamp, const uint32_t rcSamp); | |
200 int32_t MonitorRecording(const uint32_t time); | |
201 int32_t RestartTimerIfNeeded(const uint32_t time); | |
202 | |
203 private: | |
204 static bool ThreadFunc(void*); | |
205 bool ThreadProcess(); | |
206 | |
207 static DWORD WINAPI GetCaptureVolumeThread(LPVOID context); | |
208 DWORD DoGetCaptureVolumeThread(); | |
209 | |
210 static DWORD WINAPI SetCaptureVolumeThread(LPVOID context); | |
211 DWORD DoSetCaptureVolumeThread(); | |
212 | |
213 private: | |
214 AudioDeviceBuffer* _ptrAudioBuffer; | |
215 | |
216 CriticalSectionWrapper& _critSect; | |
217 EventTimerWrapper& _timeEvent; | |
218 EventWrapper& _recStartEvent; | |
219 EventWrapper& _playStartEvent; | |
220 | |
221 HANDLE _hGetCaptureVolumeThread; | |
222 HANDLE _hShutdownGetVolumeEvent; | |
223 HANDLE _hSetCaptureVolumeThread; | |
224 HANDLE _hShutdownSetVolumeEvent; | |
225 HANDLE _hSetCaptureVolumeEvent; | |
226 | |
227 // TODO(pbos): Remove unique_ptr usage and use PlatformThread directly | |
228 std::unique_ptr<rtc::PlatformThread> _ptrThread; | |
229 | |
230 CriticalSectionWrapper& _critSectCb; | |
231 | |
232 int32_t _id; | |
233 | |
234 AudioMixerManager _mixerManager; | |
235 | |
236 bool _usingInputDeviceIndex; | |
237 bool _usingOutputDeviceIndex; | |
238 AudioDeviceModule::WindowsDeviceType _inputDevice; | |
239 AudioDeviceModule::WindowsDeviceType _outputDevice; | |
240 uint16_t _inputDeviceIndex; | |
241 uint16_t _outputDeviceIndex; | |
242 bool _inputDeviceIsSpecified; | |
243 bool _outputDeviceIsSpecified; | |
244 | |
245 WAVEFORMATEX _waveFormatIn; | |
246 WAVEFORMATEX _waveFormatOut; | |
247 | |
248 HWAVEIN _hWaveIn; | |
249 HWAVEOUT _hWaveOut; | |
250 | |
251 WAVEHDR _waveHeaderIn[N_BUFFERS_IN]; | |
252 WAVEHDR _waveHeaderOut[N_BUFFERS_OUT]; | |
253 | |
254 uint8_t _recChannels; | |
255 uint8_t _playChannels; | |
256 uint16_t _recBufCount; | |
257 uint16_t _recDelayCount; | |
258 uint16_t _recPutBackDelay; | |
259 | |
260 int8_t _recBuffer[N_BUFFERS_IN][4*REC_BUF_SIZE_IN_SAMPLES]; | |
261 int8_t _playBuffer[N_BUFFERS_OUT][4*PLAY_BUF_SIZE_IN_SAMPLES]; | |
262 | |
263 AudioDeviceModule::BufferType _playBufType; | |
264 | |
265 private: | |
266 bool _initialized; | |
267 bool _recording; | |
268 bool _playing; | |
269 bool _recIsInitialized; | |
270 bool _playIsInitialized; | |
271 bool _startRec; | |
272 bool _stopRec; | |
273 bool _startPlay; | |
274 bool _stopPlay; | |
275 bool _AGC; | |
276 | |
277 private: | |
278 uint32_t _prevPlayTime; | |
279 uint32_t _prevRecTime; | |
280 uint32_t _prevTimerCheckTime; | |
281 | |
282 uint16_t _playBufCount; // playout buffer
index | |
283 uint16_t _dTcheckPlayBufDelay; // dT for check of
play buffer, {2,5,10} [ms] | |
284 uint16_t _playBufDelay; // playback delay | |
285 uint16_t _playBufDelayFixed; // fixed playback
delay | |
286 uint16_t _minPlayBufDelay; // minimum playbac
k delay | |
287 uint16_t _MAX_minBuffer; // level of (adapt
ive) min threshold must be < _MAX_minBuffer | |
288 | |
289 int32_t _erZeroCounter; // counts "buffer-
is-empty" events | |
290 int32_t _intro; | |
291 int32_t _waitCounter; | |
292 | |
293 uint32_t _writtenSamples; | |
294 uint32_t _writtenSamplesOld; | |
295 uint32_t _playedSamplesOld; | |
296 | |
297 uint32_t _sndCardPlayDelay; | |
298 uint32_t _sndCardRecDelay; | |
299 | |
300 uint32_t _plSampOld; | |
301 uint32_t _rcSampOld; | |
302 | |
303 uint32_t _read_samples; | |
304 uint32_t _read_samples_old; | |
305 uint32_t _rec_samples_old; | |
306 | |
307 // State that detects driver problems: | |
308 int32_t _dc_diff_mean; | |
309 int32_t _dc_y_prev; | |
310 int32_t _dc_penalty_counter; | |
311 int32_t _dc_prevtime; | |
312 uint32_t _dc_prevplay; | |
313 | |
314 uint32_t _recordedBytes; // accumulated #re
corded bytes (reset periodically) | |
315 uint32_t _prevRecByteCheckTime; // time when we la
st checked the recording process | |
316 | |
317 // CPU load measurements | |
318 LARGE_INTEGER _perfFreq; | |
319 LONGLONG _playAcc; // accumulat
ed time for playout callback | |
320 float _avgCPULoad; // average t
otal (rec+play) CPU load | |
321 | |
322 int32_t _wrapCounter; | |
323 | |
324 int32_t _useHeader; | |
325 int16_t _timesdwBytes; | |
326 int32_t _no_of_msecleft_warnings; | |
327 int32_t _writeErrors; | |
328 int32_t _timerFaults; | |
329 int32_t _timerRestartAttempts; | |
330 | |
331 uint16_t _playWarning; | |
332 uint16_t _playError; | |
333 uint16_t _recWarning; | |
334 uint16_t _recError; | |
335 | |
336 uint32_t _newMicLevel; | |
337 uint32_t _minMicVolume; | |
338 uint32_t _maxMicVolume; | |
339 }; | |
340 | |
341 } // namespace webrtc | |
342 | |
343 #endif // WEBRTC_AUDIO_DEVICE_AUDIO_DEVICE_WAVE_WIN_H | |
OLD | NEW |