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
3 // found in the LICENSE file.
5 // Implementation of AudioInputStream for Mac OS X using the special AUHAL
6 // input Audio Unit present in OS 10.4 and later.
7 // The AUHAL input Audio Unit is for low-latency audio I/O.
9 // Overview of operation:
11 // - An object of AUAudioInputStream is created by the AudioManager
12 // factory: audio_man->MakeAudioInputStream().
13 // - Next some thread will call Open(), at that point the underlying
14 // AUHAL output Audio Unit is created and configured.
15 // - Then some thread will call Start(sink).
16 // Then the Audio Unit is started which creates its own thread which
17 // periodically will provide the sink with more data as buffers are being
19 // - At some point some thread will call Stop(), which we handle by directly
20 // stopping the AUHAL output Audio Unit.
21 // - The same thread that called stop will call Close() where we cleanup
22 // and notify the audio manager, which likely will destroy this object.
24 // Implementation notes:
26 // - It is recommended to first acquire the native sample rate of the default
27 // input device and then use the same rate when creating this object.
28 // Use AUAudioInputStream::HardwareSampleRate() to retrieve the sample rate.
29 // - Calling Close() also leads to self destruction.
30 // - The latency consists of two parts:
31 // 1) Hardware latency, which includes Audio Unit latency, audio device
33 // 2) The delay between the actual recording instant and the time when the
34 // data packet is provided as a callback.
36 #ifndef MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_INPUT_MAC_H_
37 #define MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_INPUT_MAC_H_
39 #include <AudioUnit/AudioUnit.h>
40 #include <CoreAudio/CoreAudio.h>
42 #include "base/cancelable_callback.h"
43 #include "base/memory/scoped_ptr.h"
44 #include "base/synchronization/lock.h"
45 #include "base/time/time.h"
46 #include "media/audio/agc_audio_stream.h"
47 #include "media/audio/audio_io.h"
48 #include "media/audio/audio_parameters.h"
49 #include "media/base/audio_block_fifo.h"
54 class AudioManagerMac
;
57 class AUAudioInputStream
: public AgcAudioStream
<AudioInputStream
> {
59 // The ctor takes all the usual parameters, plus |manager| which is the
60 // the audio manager who is creating this object.
61 AUAudioInputStream(AudioManagerMac
* manager
,
62 const AudioParameters
& input_params
,
63 AudioDeviceID audio_device_id
);
64 // The dtor is typically called by the AudioManager only and it is usually
65 // triggered by calling AudioInputStream::Close().
66 ~AUAudioInputStream() override
;
68 // Implementation of AudioInputStream.
70 void Start(AudioInputCallback
* callback
) override
;
72 void Close() override
;
73 double GetMaxVolume() override
;
74 void SetVolume(double volume
) override
;
75 double GetVolume() override
;
76 bool IsMuted() override
;
78 // Returns the current hardware sample rate for the default input device.
79 MEDIA_EXPORT
static int HardwareSampleRate();
81 bool started() const { return started_
; }
82 AudioUnit
audio_unit() const { return audio_unit_
; }
83 AudioBufferList
* audio_buffer_list() { return &audio_buffer_list_
; }
84 AudioDeviceID
device_id() const { return input_device_id_
; }
85 size_t requested_buffer_size() const { return number_of_frames_
; }
88 // AudioOutputUnit callback.
89 static OSStatus
InputProc(void* user_data
,
90 AudioUnitRenderActionFlags
* flags
,
91 const AudioTimeStamp
* time_stamp
,
93 UInt32 number_of_frames
,
94 AudioBufferList
* io_data
);
96 // Pushes recorded data to consumer of the input audio stream.
97 OSStatus
Provide(UInt32 number_of_frames
, AudioBufferList
* io_data
,
98 const AudioTimeStamp
* time_stamp
);
100 // Gets the fixed capture hardware latency and store it during initialization.
101 // Returns 0 if not available.
102 double GetHardwareLatency();
104 // Gets the current capture delay value.
105 double GetCaptureLatency(const AudioTimeStamp
* input_time_stamp
);
107 // Gets the number of channels for a stream of audio data.
108 int GetNumberOfChannelsFromStream();
110 // Issues the OnError() callback to the |sink_|.
111 void HandleError(OSStatus err
);
113 // Helper function to check if the volume control is avialable on specific
115 bool IsVolumeSettableOnChannel(int channel
);
117 // Our creator, the audio manager needs to be notified when we close.
118 AudioManagerMac
* manager_
;
120 // Contains the desired number of audio frames in each callback.
121 const size_t number_of_frames_
;
123 // Pointer to the object that will receive the recorded audio samples.
124 AudioInputCallback
* sink_
;
126 // Structure that holds the desired output format of the stream.
127 // Note that, this format can differ from the device(=input) format.
128 AudioStreamBasicDescription format_
;
130 // The special Audio Unit called AUHAL, which allows us to pass audio data
131 // directly from a microphone, through the HAL, and to our application.
132 // The AUHAL also enables selection of non default devices.
133 AudioUnit audio_unit_
;
135 // The UID refers to the current input audio device.
136 AudioDeviceID input_device_id_
;
138 // Provides a mechanism for encapsulating one or more buffers of audio data.
139 AudioBufferList audio_buffer_list_
;
141 // Temporary storage for recorded data. The InputProc() renders into this
142 // array as soon as a frame of the desired buffer size has been recorded.
143 scoped_ptr
<uint8
[]> audio_data_buffer_
;
145 // True after successfull Start(), false after successful Stop().
148 // Fixed capture hardware latency in frames.
149 double hardware_latency_frames_
;
151 // The number of channels in each frame of audio data, which is used
152 // when querying the volume of each channel.
153 int number_of_channels_in_frame_
;
155 // FIFO used to accumulates recorded data.
156 media::AudioBlockFifo fifo_
;
158 // Used to defer Start() to workaround http://crbug.com/160920.
159 base::CancelableClosure deferred_start_cb_
;
161 // Contains time of last successful call to AudioUnitRender().
162 // Initialized first time in Start() and then updated for each valid
163 // audio buffer. Used to detect long error sequences and to take actions
164 // if length of error sequence is above a certain limit.
165 base::TimeTicks last_success_time_
;
167 DISALLOW_COPY_AND_ASSIGN(AUAudioInputStream
);
172 #endif // MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_INPUT_MAC_H_