OLD | NEW |
(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_co_mem.h" |
| 63 #include "base/win/scoped_com_initializer.h" |
| 64 #include "base/win/scoped_comptr.h" |
| 65 #include "base/win/scoped_handle.h" |
| 66 #include "media/audio/audio_io.h" |
| 67 #include "media/audio/audio_parameters.h" |
| 68 |
| 69 class AudioManagerWin; |
| 70 |
| 71 // AudioInputStream implementation using Windows Core Audio APIs. |
| 72 class WASAPIAudioInputStream |
| 73 : public AudioInputStream, |
| 74 public base::DelegateSimpleThread::Delegate { |
| 75 public: |
| 76 // The ctor takes all the usual parameters, plus |manager| which is the |
| 77 // the audio manager who is creating this object. |
| 78 WASAPIAudioInputStream(AudioManagerWin* manager, |
| 79 const AudioParameters& params, |
| 80 ERole device_role); |
| 81 // The dtor is typically called by the AudioManager only and it is usually |
| 82 // triggered by calling AudioInputStream::Close(). |
| 83 virtual ~WASAPIAudioInputStream(); |
| 84 |
| 85 // Implementation of AudioInputStream. |
| 86 virtual bool Open() OVERRIDE; |
| 87 virtual void Start(AudioInputCallback* callback) OVERRIDE; |
| 88 virtual void Stop() OVERRIDE; |
| 89 virtual void Close() OVERRIDE; |
| 90 |
| 91 // Retrieves the stream format that the audio engine uses for its internal |
| 92 // processing/mixing of shared-mode streams. |
| 93 static double HardwareSampleRate(ERole device_role); |
| 94 |
| 95 bool started() const { return started_; } |
| 96 |
| 97 private: |
| 98 // DelegateSimpleThread::Delegate implementation. |
| 99 virtual void Run() OVERRIDE; |
| 100 |
| 101 // Issues the OnError() callback to the |sink_|. |
| 102 void HandleError(HRESULT err); |
| 103 |
| 104 // The Open() method is divided into these sub methods. |
| 105 HRESULT SetCaptureDevice(ERole device_role); |
| 106 HRESULT ActivateCaptureDevice(); |
| 107 HRESULT GetAudioEngineStreamFormat(); |
| 108 bool DesiredFormatIsSupported(); |
| 109 HRESULT InitializeAudioEngine(); |
| 110 |
| 111 // Initializes the COM library for use by the calling thread and set the |
| 112 // thread's concurrency model to multi-threaded. |
| 113 base::win::ScopedCOMInitializer com_init_; |
| 114 |
| 115 // Our creator, the audio manager needs to be notified when we close. |
| 116 AudioManagerWin* manager_; |
| 117 |
| 118 // Capturing is driven by this thread (which has no message loop). |
| 119 // All OnData() callbacks will be called from this thread. |
| 120 base::DelegateSimpleThread* capture_thread_; |
| 121 |
| 122 // Contains the desired audio format which is set up at construction. |
| 123 WAVEFORMATEX format_; |
| 124 |
| 125 // Copy of the audio format which we know the audio engine supports. |
| 126 // It is recommended to ensure that the sample rate in |format_| is identical |
| 127 // to the sample rate in |audio_engine_mix_format_|. |
| 128 base::win::ScopedCoMem<WAVEFORMATEX> audio_engine_mix_format_; |
| 129 |
| 130 bool opened_; |
| 131 bool started_; |
| 132 |
| 133 // Size in bytes of each audio frame (4 bytes for 16-bit stereo PCM) |
| 134 size_t frame_size_; |
| 135 |
| 136 // Size in audio frames of each audio packet where an audio packet |
| 137 // is defined as the block of data which the user received in each |
| 138 // OnData() callback. |
| 139 size_t packet_size_frames_; |
| 140 |
| 141 // Size in bytes of each audio packet. |
| 142 size_t packet_size_bytes_; |
| 143 |
| 144 // Length of the audio endpoint buffer. |
| 145 size_t endpoint_buffer_size_frames_; |
| 146 |
| 147 // Defines the role that the system has assigned to an audio endpoint device. |
| 148 ERole device_role_; |
| 149 |
| 150 // Conversion factor used in delay-estimation calculations. |
| 151 // Converts a raw performance counter value to 100-nanosecond unit. |
| 152 double perf_count_to_100ns_units_; |
| 153 |
| 154 // Conversion factor used in delay-estimation calculations. |
| 155 // Converts from milliseconds to audio frames. |
| 156 double ms_to_frame_count_; |
| 157 |
| 158 // Pointer to the object that will receive the recorded audio samples. |
| 159 AudioInputCallback* sink_; |
| 160 |
| 161 // An IMMDevice interface which represents an audio endpoint device. |
| 162 base::win::ScopedComPtr<IMMDevice> endpoint_device_; |
| 163 |
| 164 // An IAudioClient interface which enables a client to create and initialize |
| 165 // an audio stream between an audio application and the audio engine. |
| 166 base::win::ScopedComPtr<IAudioClient> audio_client_; |
| 167 |
| 168 // The IAudioCaptureClient interface enables a client to read input data |
| 169 // from a capture endpoint buffer. |
| 170 base::win::ScopedComPtr<IAudioCaptureClient> audio_capture_client_; |
| 171 |
| 172 // The audio engine will signal this event each time a buffer has been |
| 173 // recorded. |
| 174 base::win::ScopedHandle audio_samples_ready_event_; |
| 175 |
| 176 // This event will be signaled when capturing shall stop. |
| 177 base::win::ScopedHandle stop_capture_event_; |
| 178 |
| 179 DISALLOW_COPY_AND_ASSIGN(WASAPIAudioInputStream); |
| 180 }; |
| 181 |
| 182 #endif // MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_INPUT_WIN_H_ |
OLD | NEW |