| 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 |
| 83 class AudioBus; | 84 class AudioBus; |
| 84 class AudioManagerWin; | 85 class AudioManagerWin; |
| 85 | 86 |
| 86 // AudioInputStream implementation using Windows Core Audio APIs. | 87 // AudioInputStream implementation using Windows Core Audio APIs. |
| 87 class MEDIA_EXPORT WASAPIAudioInputStream | 88 class MEDIA_EXPORT WASAPIAudioInputStream |
| 88 : public AgcAudioStream<AudioInputStream>, | 89 : public AgcAudioStream<AudioInputStream>, |
| 89 public base::DelegateSimpleThread::Delegate, | 90 public base::DelegateSimpleThread::Delegate, |
| 91 public AudioConverter::InputCallback, |
| 90 NON_EXPORTED_BASE(public base::NonThreadSafe) { | 92 NON_EXPORTED_BASE(public base::NonThreadSafe) { |
| 91 public: | 93 public: |
| 92 // The ctor takes all the usual parameters, plus |manager| which is the | 94 // The ctor takes all the usual parameters, plus |manager| which is the |
| 93 // the audio manager who is creating this object. | 95 // the audio manager who is creating this object. |
| 94 WASAPIAudioInputStream(AudioManagerWin* manager, | 96 WASAPIAudioInputStream(AudioManagerWin* manager, |
| 95 const AudioParameters& params, | 97 const AudioParameters& params, |
| 96 const std::string& device_id); | 98 const std::string& device_id); |
| 97 | 99 |
| 98 // The dtor is typically called by the AudioManager only and it is usually | 100 // The dtor is typically called by the AudioManager only and it is usually |
| 99 // triggered by calling AudioInputStream::Close(). | 101 // triggered by calling AudioInputStream::Close(). |
| (...skipping 18 matching lines...) Expand all Loading... |
| 118 // Issues the OnError() callback to the |sink_|. | 120 // Issues the OnError() callback to the |sink_|. |
| 119 void HandleError(HRESULT err); | 121 void HandleError(HRESULT err); |
| 120 | 122 |
| 121 // The Open() method is divided into these sub methods. | 123 // The Open() method is divided into these sub methods. |
| 122 HRESULT SetCaptureDevice(); | 124 HRESULT SetCaptureDevice(); |
| 123 HRESULT GetAudioEngineStreamFormat(); | 125 HRESULT GetAudioEngineStreamFormat(); |
| 124 bool DesiredFormatIsSupported(); | 126 bool DesiredFormatIsSupported(); |
| 125 HRESULT InitializeAudioEngine(); | 127 HRESULT InitializeAudioEngine(); |
| 126 void ReportOpenResult() const; | 128 void ReportOpenResult() const; |
| 127 | 129 |
| 130 // AudioConverter::InputCallback implementation. |
| 131 double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override; |
| 132 |
| 128 // Used to track down where we fail during initialization which at the | 133 // 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. | 134 // 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 | 135 // The reason might be expected (e.g. trying to open "default" on a machine |
| 131 // that has no audio devices). | 136 // that has no audio devices). |
| 132 // Note: This enum is used to record a histogram value and should not be | 137 // Note: This enum is used to record a histogram value and should not be |
| 133 // re-ordered. | 138 // re-ordered. |
| 134 enum StreamOpenResult { | 139 enum StreamOpenResult { |
| 135 OPEN_RESULT_OK = 0, | 140 OPEN_RESULT_OK = 0, |
| 136 OPEN_RESULT_CREATE_INSTANCE = 1, | 141 OPEN_RESULT_CREATE_INSTANCE = 1, |
| 137 OPEN_RESULT_NO_ENDPOINT = 2, | 142 OPEN_RESULT_NO_ENDPOINT = 2, |
| 138 OPEN_RESULT_NO_STATE = 3, | 143 OPEN_RESULT_NO_STATE = 3, |
| 139 OPEN_RESULT_DEVICE_NOT_ACTIVE = 4, | 144 OPEN_RESULT_DEVICE_NOT_ACTIVE = 4, |
| 140 OPEN_RESULT_ACTIVATION_FAILED = 5, | 145 OPEN_RESULT_ACTIVATION_FAILED = 5, |
| 141 OPEN_RESULT_FORMAT_NOT_SUPPORTED = 6, | 146 OPEN_RESULT_FORMAT_NOT_SUPPORTED = 6, |
| 142 OPEN_RESULT_AUDIO_CLIENT_INIT_FAILED = 7, | 147 OPEN_RESULT_AUDIO_CLIENT_INIT_FAILED = 7, |
| 143 OPEN_RESULT_GET_BUFFER_SIZE_FAILED = 8, | 148 OPEN_RESULT_GET_BUFFER_SIZE_FAILED = 8, |
| 144 OPEN_RESULT_LOOPBACK_ACTIVATE_FAILED = 9, | 149 OPEN_RESULT_LOOPBACK_ACTIVATE_FAILED = 9, |
| 145 OPEN_RESULT_LOOPBACK_INIT_FAILED = 10, | 150 OPEN_RESULT_LOOPBACK_INIT_FAILED = 10, |
| 146 OPEN_RESULT_SET_EVENT_HANDLE = 11, | 151 OPEN_RESULT_SET_EVENT_HANDLE = 11, |
| 147 OPEN_RESULT_NO_CAPTURE_CLIENT = 12, | 152 OPEN_RESULT_NO_CAPTURE_CLIENT = 12, |
| 148 OPEN_RESULT_NO_AUDIO_VOLUME = 13, | 153 OPEN_RESULT_NO_AUDIO_VOLUME = 13, |
| 149 OPEN_RESULT_MAX = OPEN_RESULT_NO_AUDIO_VOLUME | 154 OPEN_RESULT_OK_WITH_RESAMPLING = 14, |
| 155 OPEN_RESULT_MAX = OPEN_RESULT_OK_WITH_RESAMPLING |
| 150 }; | 156 }; |
| 151 | 157 |
| 152 // Our creator, the audio manager needs to be notified when we close. | 158 // Our creator, the audio manager needs to be notified when we close. |
| 153 AudioManagerWin* const manager_; | 159 AudioManagerWin* const manager_; |
| 154 | 160 |
| 155 // Capturing is driven by this thread (which has no message loop). | 161 // Capturing is driven by this thread (which has no message loop). |
| 156 // All OnData() callbacks will be called from this thread. | 162 // All OnData() callbacks will be called from this thread. |
| 157 std::unique_ptr<base::DelegateSimpleThread> capture_thread_; | 163 std::unique_ptr<base::DelegateSimpleThread> capture_thread_; |
| 158 | 164 |
| 159 // Contains the desired audio format which is set up at construction. | 165 // Contains the desired audio format which is set up at construction. |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 // Extra audio bus used for storage of deinterleaved data for the OnData | 242 // Extra audio bus used for storage of deinterleaved data for the OnData |
| 237 // callback. | 243 // callback. |
| 238 std::unique_ptr<media::AudioBus> audio_bus_; | 244 std::unique_ptr<media::AudioBus> audio_bus_; |
| 239 | 245 |
| 240 // Never set it through external API. Only used when |device_id_| == | 246 // Never set it through external API. Only used when |device_id_| == |
| 241 // kLoopbackWithMuteDeviceId. | 247 // kLoopbackWithMuteDeviceId. |
| 242 // True, if we have muted the system audio for the stream capturing, and | 248 // 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. | 249 // indicates that we need to unmute the system audio when stopping capturing. |
| 244 bool mute_done_ = false; | 250 bool mute_done_ = false; |
| 245 | 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 data_was_converted_ = 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 |