| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 // per-session basis. It is also possible to use of the IAudioEndpointVolume | 50 // per-session basis. It is also possible to use of the IAudioEndpointVolume |
| 51 // interface to control the master volume level of an audio endpoint device. | 51 // interface to control the master volume level of an audio endpoint device. |
| 52 // This implementation is using the ISimpleAudioVolume interface. | 52 // This implementation is using the ISimpleAudioVolume interface. |
| 53 // MSDN states that "In rare cases, a specialized audio application might | 53 // MSDN states that "In rare cases, a specialized audio application might |
| 54 // require the use of the IAudioEndpointVolume". | 54 // require the use of the IAudioEndpointVolume". |
| 55 // | 55 // |
| 56 #ifndef MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_INPUT_WIN_H_ | 56 #ifndef MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_INPUT_WIN_H_ |
| 57 #define MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_INPUT_WIN_H_ | 57 #define MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_INPUT_WIN_H_ |
| 58 | 58 |
| 59 #include <Audioclient.h> | 59 #include <Audioclient.h> |
| 60 #include <MMDeviceAPI.h> |
| 60 #include <endpointvolume.h> | 61 #include <endpointvolume.h> |
| 61 #include <MMDeviceAPI.h> | |
| 62 #include <stddef.h> | 62 #include <stddef.h> |
| 63 #include <stdint.h> | 63 #include <stdint.h> |
| 64 | 64 |
| 65 #include <memory> | 65 #include <memory> |
| 66 #include <string> | 66 #include <string> |
| 67 | 67 |
| 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" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 | 113 |
| 114 private: | 114 private: |
| 115 // DelegateSimpleThread::Delegate implementation. | 115 // DelegateSimpleThread::Delegate implementation. |
| 116 void Run() override; | 116 void Run() override; |
| 117 | 117 |
| 118 // Issues the OnError() callback to the |sink_|. | 118 // Issues the OnError() callback to the |sink_|. |
| 119 void HandleError(HRESULT err); | 119 void HandleError(HRESULT err); |
| 120 | 120 |
| 121 // The Open() method is divided into these sub methods. | 121 // The Open() method is divided into these sub methods. |
| 122 HRESULT SetCaptureDevice(); | 122 HRESULT SetCaptureDevice(); |
| 123 HRESULT ActivateCaptureDevice(); | |
| 124 HRESULT GetAudioEngineStreamFormat(); | 123 HRESULT GetAudioEngineStreamFormat(); |
| 125 bool DesiredFormatIsSupported(); | 124 bool DesiredFormatIsSupported(); |
| 126 HRESULT InitializeAudioEngine(); | 125 HRESULT InitializeAudioEngine(); |
| 126 void ReportOpenResult() const; |
| 127 |
| 128 // 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. |
| 130 // The reason might be expected (e.g. trying to open "default" on a machine |
| 131 // that has no audio devices). |
| 132 // Note: This enum is used to record a histogram value and should not be |
| 133 // re-ordered. |
| 134 enum StreamOpenResult { |
| 135 OPEN_RESULT_OK = 0, |
| 136 OPEN_RESULT_CREATE_INSTANCE = 1, |
| 137 OPEN_RESULT_NO_ENDPOINT = 2, |
| 138 OPEN_RESULT_NO_STATE = 3, |
| 139 OPEN_RESULT_DEVICE_NOT_ACTIVE = 4, |
| 140 OPEN_RESULT_ACTIVATION_FAILED = 5, |
| 141 OPEN_RESULT_FORMAT_NOT_SUPPORTED = 6, |
| 142 OPEN_RESULT_AUDIO_CLIENT_INIT_FAILED = 7, |
| 143 OPEN_RESULT_GET_BUFFER_SIZE_FAILED = 8, |
| 144 OPEN_RESULT_LOOPBACK_ACTIVATE_FAILED = 9, |
| 145 OPEN_RESULT_LOOPBACK_INIT_FAILED = 10, |
| 146 OPEN_RESULT_SET_EVENT_HANDLE = 11, |
| 147 OPEN_RESULT_NO_CAPTURE_CLIENT = 12, |
| 148 OPEN_RESULT_NO_AUDIO_VOLUME = 13, |
| 149 OPEN_RESULT_MAX = OPEN_RESULT_NO_AUDIO_VOLUME |
| 150 }; |
| 127 | 151 |
| 128 // Our creator, the audio manager needs to be notified when we close. | 152 // Our creator, the audio manager needs to be notified when we close. |
| 129 AudioManagerWin* manager_; | 153 AudioManagerWin* const manager_; |
| 130 | 154 |
| 131 // Capturing is driven by this thread (which has no message loop). | 155 // Capturing is driven by this thread (which has no message loop). |
| 132 // All OnData() callbacks will be called from this thread. | 156 // All OnData() callbacks will be called from this thread. |
| 133 base::DelegateSimpleThread* capture_thread_; | 157 std::unique_ptr<base::DelegateSimpleThread> capture_thread_; |
| 134 | 158 |
| 135 // Contains the desired audio format which is set up at construction. | 159 // Contains the desired audio format which is set up at construction. |
| 136 WAVEFORMATEX format_; | 160 WAVEFORMATEX format_; |
| 137 | 161 |
| 138 bool opened_; | 162 bool opened_ = false; |
| 139 bool started_; | 163 bool started_ = false; |
| 164 StreamOpenResult open_result_ = OPEN_RESULT_OK; |
| 140 | 165 |
| 141 // Size in bytes of each audio frame (4 bytes for 16-bit stereo PCM) | 166 // Size in bytes of each audio frame (4 bytes for 16-bit stereo PCM) |
| 142 size_t frame_size_; | 167 size_t frame_size_ = 0; |
| 143 | 168 |
| 144 // Size in audio frames of each audio packet where an audio packet | 169 // Size in audio frames of each audio packet where an audio packet |
| 145 // is defined as the block of data which the user received in each | 170 // is defined as the block of data which the user received in each |
| 146 // OnData() callback. | 171 // OnData() callback. |
| 147 size_t packet_size_frames_; | 172 size_t packet_size_frames_ = 0; |
| 148 | 173 |
| 149 // Size in bytes of each audio packet. | 174 // Size in bytes of each audio packet. |
| 150 size_t packet_size_bytes_; | 175 size_t packet_size_bytes_ = 0; |
| 151 | 176 |
| 152 // Length of the audio endpoint buffer. | 177 // Length of the audio endpoint buffer. |
| 153 uint32_t endpoint_buffer_size_frames_; | 178 uint32_t endpoint_buffer_size_frames_ = 0; |
| 154 | 179 |
| 155 // Contains the unique name of the selected endpoint device. | 180 // Contains the unique name of the selected endpoint device. |
| 156 // Note that AudioDeviceDescription::kDefaultDeviceId represents the default | 181 // Note that AudioDeviceDescription::kDefaultDeviceId represents the default |
| 157 // device role and is not a valid ID as such. | 182 // device role and is not a valid ID as such. |
| 158 std::string device_id_; | 183 std::string device_id_; |
| 159 | 184 |
| 160 // Conversion factor used in delay-estimation calculations. | 185 // Conversion factor used in delay-estimation calculations. |
| 161 // Converts a raw performance counter value to 100-nanosecond unit. | 186 // Converts a raw performance counter value to 100-nanosecond unit. |
| 162 double perf_count_to_100ns_units_; | 187 double perf_count_to_100ns_units_ = 0.0; |
| 163 | 188 |
| 164 // Conversion factor used in delay-estimation calculations. | 189 // Conversion factor used in delay-estimation calculations. |
| 165 // Converts from milliseconds to audio frames. | 190 // Converts from milliseconds to audio frames. |
| 166 double ms_to_frame_count_; | 191 double ms_to_frame_count_ = 0.0; |
| 167 | 192 |
| 168 // Pointer to the object that will receive the recorded audio samples. | 193 // Pointer to the object that will receive the recorded audio samples. |
| 169 AudioInputCallback* sink_; | 194 AudioInputCallback* sink_ = nullptr; |
| 170 | 195 |
| 171 // Windows Multimedia Device (MMDevice) API interfaces. | 196 // Windows Multimedia Device (MMDevice) API interfaces. |
| 172 | 197 |
| 173 // An IMMDevice interface which represents an audio endpoint device. | 198 // An IMMDevice interface which represents an audio endpoint device. |
| 174 base::win::ScopedComPtr<IMMDevice> endpoint_device_; | 199 base::win::ScopedComPtr<IMMDevice> endpoint_device_; |
| 175 | 200 |
| 176 // Windows Audio Session API (WASAPI) interfaces. | 201 // Windows Audio Session API (WASAPI) interfaces. |
| 177 | 202 |
| 178 // An IAudioClient interface which enables a client to create and initialize | 203 // An IAudioClient interface which enables a client to create and initialize |
| 179 // an audio stream between an audio application and the audio engine. | 204 // an audio stream between an audio application and the audio engine. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 209 base::win::ScopedHandle stop_capture_event_; | 234 base::win::ScopedHandle stop_capture_event_; |
| 210 | 235 |
| 211 // Extra audio bus used for storage of deinterleaved data for the OnData | 236 // Extra audio bus used for storage of deinterleaved data for the OnData |
| 212 // callback. | 237 // callback. |
| 213 std::unique_ptr<media::AudioBus> audio_bus_; | 238 std::unique_ptr<media::AudioBus> audio_bus_; |
| 214 | 239 |
| 215 // Never set it through external API. Only used when |device_id_| == | 240 // Never set it through external API. Only used when |device_id_| == |
| 216 // kLoopbackWithMuteDeviceId. | 241 // kLoopbackWithMuteDeviceId. |
| 217 // True, if we have muted the system audio for the stream capturing, and | 242 // True, if we have muted the system audio for the stream capturing, and |
| 218 // indicates that we need to unmute the system audio when stopping capturing. | 243 // indicates that we need to unmute the system audio when stopping capturing. |
| 219 bool mute_done_; | 244 bool mute_done_ = false; |
| 220 | 245 |
| 221 DISALLOW_COPY_AND_ASSIGN(WASAPIAudioInputStream); | 246 DISALLOW_COPY_AND_ASSIGN(WASAPIAudioInputStream); |
| 222 }; | 247 }; |
| 223 | 248 |
| 224 } // namespace media | 249 } // namespace media |
| 225 | 250 |
| 226 #endif // MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_INPUT_WIN_H_ | 251 #endif // MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_INPUT_WIN_H_ |
| OLD | NEW |