OLD | NEW |
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 // Implementation of AudioInputStream for Windows using Windows Core Audio | 5 // Implementation of AudioInputStream for Windows using Windows Core Audio |
6 // WASAPI for low latency capturing. | 6 // WASAPI for low latency capturing. |
7 // | 7 // |
8 // Overview of operation: | 8 // Overview of operation: |
9 // | 9 // |
10 // - An object of WASAPIAudioInputStream is created by the AudioManager | 10 // - An object of WASAPIAudioInputStream is created by the AudioManager |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 #include "base/compiler_specific.h" | 68 #include "base/compiler_specific.h" |
69 #include "base/macros.h" | 69 #include "base/macros.h" |
70 #include "base/threading/non_thread_safe.h" | 70 #include "base/threading/non_thread_safe.h" |
71 #include "base/threading/platform_thread.h" | 71 #include "base/threading/platform_thread.h" |
72 #include "base/threading/simple_thread.h" | 72 #include "base/threading/simple_thread.h" |
73 #include "base/win/scoped_co_mem.h" | 73 #include "base/win/scoped_co_mem.h" |
74 #include "base/win/scoped_com_initializer.h" | 74 #include "base/win/scoped_com_initializer.h" |
75 #include "base/win/scoped_comptr.h" | 75 #include "base/win/scoped_comptr.h" |
76 #include "base/win/scoped_handle.h" | 76 #include "base/win/scoped_handle.h" |
77 #include "media/audio/agc_audio_stream.h" | 77 #include "media/audio/agc_audio_stream.h" |
| 78 #include "media/base/audio_converter.h" |
78 #include "media/base/audio_parameters.h" | 79 #include "media/base/audio_parameters.h" |
79 #include "media/base/media_export.h" | 80 #include "media/base/media_export.h" |
80 | 81 |
81 namespace media { | 82 namespace media { |
82 | 83 |
| 84 class AudioBlockFifo; |
83 class AudioBus; | 85 class AudioBus; |
84 class AudioManagerWin; | 86 class AudioManagerWin; |
85 | 87 |
86 // AudioInputStream implementation using Windows Core Audio APIs. | 88 // AudioInputStream implementation using Windows Core Audio APIs. |
87 class MEDIA_EXPORT WASAPIAudioInputStream | 89 class MEDIA_EXPORT WASAPIAudioInputStream |
88 : public AgcAudioStream<AudioInputStream>, | 90 : public AgcAudioStream<AudioInputStream>, |
89 public base::DelegateSimpleThread::Delegate, | 91 public base::DelegateSimpleThread::Delegate, |
| 92 public AudioConverter::InputCallback, |
90 NON_EXPORTED_BASE(public base::NonThreadSafe) { | 93 NON_EXPORTED_BASE(public base::NonThreadSafe) { |
91 public: | 94 public: |
92 // The ctor takes all the usual parameters, plus |manager| which is the | 95 // The ctor takes all the usual parameters, plus |manager| which is the |
93 // the audio manager who is creating this object. | 96 // the audio manager who is creating this object. |
94 WASAPIAudioInputStream(AudioManagerWin* manager, | 97 WASAPIAudioInputStream(AudioManagerWin* manager, |
95 const AudioParameters& params, | 98 const AudioParameters& params, |
96 const std::string& device_id); | 99 const std::string& device_id); |
97 | 100 |
98 // The dtor is typically called by the AudioManager only and it is usually | 101 // The dtor is typically called by the AudioManager only and it is usually |
99 // triggered by calling AudioInputStream::Close(). | 102 // triggered by calling AudioInputStream::Close(). |
(...skipping 18 matching lines...) Expand all Loading... |
118 // Issues the OnError() callback to the |sink_|. | 121 // Issues the OnError() callback to the |sink_|. |
119 void HandleError(HRESULT err); | 122 void HandleError(HRESULT err); |
120 | 123 |
121 // The Open() method is divided into these sub methods. | 124 // The Open() method is divided into these sub methods. |
122 HRESULT SetCaptureDevice(); | 125 HRESULT SetCaptureDevice(); |
123 HRESULT GetAudioEngineStreamFormat(); | 126 HRESULT GetAudioEngineStreamFormat(); |
124 bool DesiredFormatIsSupported(); | 127 bool DesiredFormatIsSupported(); |
125 HRESULT InitializeAudioEngine(); | 128 HRESULT InitializeAudioEngine(); |
126 void ReportOpenResult() const; | 129 void ReportOpenResult() const; |
127 | 130 |
| 131 // AudioConverter::InputCallback implementation. |
| 132 double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override; |
| 133 |
128 // Used to track down where we fail during initialization which at the | 134 // Used to track down where we fail during initialization which at the |
129 // moment seems to be happening frequently and we're not sure why. | 135 // moment seems to be happening frequently and we're not sure why. |
130 // The reason might be expected (e.g. trying to open "default" on a machine | 136 // The reason might be expected (e.g. trying to open "default" on a machine |
131 // that has no audio devices). | 137 // that has no audio devices). |
132 // Note: This enum is used to record a histogram value and should not be | 138 // Note: This enum is used to record a histogram value and should not be |
133 // re-ordered. | 139 // re-ordered. |
134 enum StreamOpenResult { | 140 enum StreamOpenResult { |
135 OPEN_RESULT_OK = 0, | 141 OPEN_RESULT_OK = 0, |
136 OPEN_RESULT_CREATE_INSTANCE = 1, | 142 OPEN_RESULT_CREATE_INSTANCE = 1, |
137 OPEN_RESULT_NO_ENDPOINT = 2, | 143 OPEN_RESULT_NO_ENDPOINT = 2, |
138 OPEN_RESULT_NO_STATE = 3, | 144 OPEN_RESULT_NO_STATE = 3, |
139 OPEN_RESULT_DEVICE_NOT_ACTIVE = 4, | 145 OPEN_RESULT_DEVICE_NOT_ACTIVE = 4, |
140 OPEN_RESULT_ACTIVATION_FAILED = 5, | 146 OPEN_RESULT_ACTIVATION_FAILED = 5, |
141 OPEN_RESULT_FORMAT_NOT_SUPPORTED = 6, | 147 OPEN_RESULT_FORMAT_NOT_SUPPORTED = 6, |
142 OPEN_RESULT_AUDIO_CLIENT_INIT_FAILED = 7, | 148 OPEN_RESULT_AUDIO_CLIENT_INIT_FAILED = 7, |
143 OPEN_RESULT_GET_BUFFER_SIZE_FAILED = 8, | 149 OPEN_RESULT_GET_BUFFER_SIZE_FAILED = 8, |
144 OPEN_RESULT_LOOPBACK_ACTIVATE_FAILED = 9, | 150 OPEN_RESULT_LOOPBACK_ACTIVATE_FAILED = 9, |
145 OPEN_RESULT_LOOPBACK_INIT_FAILED = 10, | 151 OPEN_RESULT_LOOPBACK_INIT_FAILED = 10, |
146 OPEN_RESULT_SET_EVENT_HANDLE = 11, | 152 OPEN_RESULT_SET_EVENT_HANDLE = 11, |
147 OPEN_RESULT_NO_CAPTURE_CLIENT = 12, | 153 OPEN_RESULT_NO_CAPTURE_CLIENT = 12, |
148 OPEN_RESULT_NO_AUDIO_VOLUME = 13, | 154 OPEN_RESULT_NO_AUDIO_VOLUME = 13, |
149 OPEN_RESULT_MAX = OPEN_RESULT_NO_AUDIO_VOLUME | 155 OPEN_RESULT_OK_WITH_RESAMPLING = 14, |
| 156 OPEN_RESULT_MAX = OPEN_RESULT_OK_WITH_RESAMPLING |
150 }; | 157 }; |
151 | 158 |
152 // Our creator, the audio manager needs to be notified when we close. | 159 // Our creator, the audio manager needs to be notified when we close. |
153 AudioManagerWin* const manager_; | 160 AudioManagerWin* const manager_; |
154 | 161 |
155 // Capturing is driven by this thread (which has no message loop). | 162 // Capturing is driven by this thread (which has no message loop). |
156 // All OnData() callbacks will be called from this thread. | 163 // All OnData() callbacks will be called from this thread. |
157 std::unique_ptr<base::DelegateSimpleThread> capture_thread_; | 164 std::unique_ptr<base::DelegateSimpleThread> capture_thread_; |
158 | 165 |
159 // Contains the desired audio format which is set up at construction. | 166 // Contains the desired audio format which is set up at construction. |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 // the whole system. | 233 // the whole system. |
227 base::win::ScopedComPtr<IAudioEndpointVolume> system_audio_volume_; | 234 base::win::ScopedComPtr<IAudioEndpointVolume> system_audio_volume_; |
228 | 235 |
229 // The audio engine will signal this event each time a buffer has been | 236 // The audio engine will signal this event each time a buffer has been |
230 // recorded. | 237 // recorded. |
231 base::win::ScopedHandle audio_samples_ready_event_; | 238 base::win::ScopedHandle audio_samples_ready_event_; |
232 | 239 |
233 // This event will be signaled when capturing shall stop. | 240 // This event will be signaled when capturing shall stop. |
234 base::win::ScopedHandle stop_capture_event_; | 241 base::win::ScopedHandle stop_capture_event_; |
235 | 242 |
236 // Extra audio bus used for storage of deinterleaved data for the OnData | |
237 // callback. | |
238 std::unique_ptr<media::AudioBus> audio_bus_; | |
239 | |
240 // Never set it through external API. Only used when |device_id_| == | 243 // Never set it through external API. Only used when |device_id_| == |
241 // kLoopbackWithMuteDeviceId. | 244 // kLoopbackWithMuteDeviceId. |
242 // True, if we have muted the system audio for the stream capturing, and | 245 // True, if we have muted the system audio for the stream capturing, and |
243 // indicates that we need to unmute the system audio when stopping capturing. | 246 // indicates that we need to unmute the system audio when stopping capturing. |
244 bool mute_done_ = false; | 247 bool mute_done_ = false; |
245 | 248 |
| 249 // Used for the captured audio on the callback thread. |
| 250 std::unique_ptr<AudioBlockFifo> fifo_; |
| 251 |
| 252 // If the caller requires resampling (should only be in exceptional cases and |
| 253 // ideally, never), we support using an AudioConverter. |
| 254 std::unique_ptr<AudioConverter> converter_; |
| 255 std::unique_ptr<AudioBus> convert_bus_; |
| 256 bool imperfect_buffer_size_conversion_ = false; |
| 257 |
246 DISALLOW_COPY_AND_ASSIGN(WASAPIAudioInputStream); | 258 DISALLOW_COPY_AND_ASSIGN(WASAPIAudioInputStream); |
247 }; | 259 }; |
248 | 260 |
249 } // namespace media | 261 } // namespace media |
250 | 262 |
251 #endif // MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_INPUT_WIN_H_ | 263 #endif // MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_INPUT_WIN_H_ |
OLD | NEW |