| 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 Mac OS X using the special AUHAL | 5 // Implementation of AudioInputStream for Mac OS X using the special AUHAL |
| 6 // input Audio Unit present in OS 10.4 and later. | 6 // input Audio Unit present in OS 10.4 and later. |
| 7 // The AUHAL input Audio Unit is for low-latency audio I/O. | 7 // The AUHAL input Audio Unit is for low-latency audio I/O. |
| 8 // | 8 // |
| 9 // Overview of operation: | 9 // Overview of operation: |
| 10 // | 10 // |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // and notify the audio manager, which likely will destroy this object. | 22 // and notify the audio manager, which likely will destroy this object. |
| 23 // | 23 // |
| 24 // Implementation notes: | 24 // Implementation notes: |
| 25 // | 25 // |
| 26 // - It is recommended to first acquire the native sample rate of the default | 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. | 27 // input device and then use the same rate when creating this object. |
| 28 // Use AUAudioInputStream::HardwareSampleRate() to retrieve the sample rate. | 28 // Use AUAudioInputStream::HardwareSampleRate() to retrieve the sample rate. |
| 29 // - Calling Close() also leads to self destruction. | 29 // - Calling Close() also leads to self destruction. |
| 30 // - The latency consists of two parts: | 30 // - The latency consists of two parts: |
| 31 // 1) Hardware latency, which includes Audio Unit latency, audio device | 31 // 1) Hardware latency, which includes Audio Unit latency, audio device |
| 32 // latency; | 32 // latency and audio stream latency; |
| 33 // 2) The delay between the actual recording instant and the time when the | 33 // 2) The delay between the actual recording instant and the time when the |
| 34 // data packet is provided as a callback. | 34 // data packet is provided as a callback. |
| 35 // | 35 // |
| 36 #ifndef MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_INPUT_MAC_H_ | 36 #ifndef MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_INPUT_MAC_H_ |
| 37 #define MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_INPUT_MAC_H_ | 37 #define MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_INPUT_MAC_H_ |
| 38 | 38 |
| 39 #include <AudioUnit/AudioUnit.h> | 39 #include <AudioUnit/AudioUnit.h> |
| 40 | 40 |
| 41 #include "base/memory/scoped_ptr.h" | 41 #include "base/memory/scoped_ptr.h" |
| 42 #include "base/synchronization/lock.h" | 42 #include "base/synchronization/lock.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 54 AudioDeviceID audio_device_id); | 54 AudioDeviceID audio_device_id); |
| 55 // The dtor is typically called by the AudioManager only and it is usually | 55 // The dtor is typically called by the AudioManager only and it is usually |
| 56 // triggered by calling AudioInputStream::Close(). | 56 // triggered by calling AudioInputStream::Close(). |
| 57 virtual ~AUAudioInputStream(); | 57 virtual ~AUAudioInputStream(); |
| 58 | 58 |
| 59 // Implementation of AudioInputStream. | 59 // Implementation of AudioInputStream. |
| 60 virtual bool Open() OVERRIDE; | 60 virtual bool Open() OVERRIDE; |
| 61 virtual void Start(AudioInputCallback* callback) OVERRIDE; | 61 virtual void Start(AudioInputCallback* callback) OVERRIDE; |
| 62 virtual void Stop() OVERRIDE; | 62 virtual void Stop() OVERRIDE; |
| 63 virtual void Close() OVERRIDE; | 63 virtual void Close() OVERRIDE; |
| 64 virtual double GetMaxVolume() OVERRIDE; |
| 65 virtual void SetVolume(double volume) OVERRIDE; |
| 66 virtual double GetVolume() OVERRIDE; |
| 64 | 67 |
| 65 // Returns the current hardware sample rate for the default input device. | 68 // Returns the current hardware sample rate for the default input device. |
| 66 static double HardwareSampleRate(); | 69 static double HardwareSampleRate(); |
| 67 | 70 |
| 68 bool started() const { return started_; } | 71 bool started() const { return started_; } |
| 69 AudioUnit audio_unit() { return audio_unit_; } | 72 AudioUnit audio_unit() { return audio_unit_; } |
| 70 AudioBufferList* audio_buffer_list() { return &audio_buffer_list_; } | 73 AudioBufferList* audio_buffer_list() { return &audio_buffer_list_; } |
| 71 | 74 |
| 72 private: | 75 private: |
| 73 // AudioOutputUnit callback. | 76 // AudioOutputUnit callback. |
| 74 static OSStatus InputProc(void* user_data, | 77 static OSStatus InputProc(void* user_data, |
| 75 AudioUnitRenderActionFlags* flags, | 78 AudioUnitRenderActionFlags* flags, |
| 76 const AudioTimeStamp* time_stamp, | 79 const AudioTimeStamp* time_stamp, |
| 77 UInt32 bus_number, | 80 UInt32 bus_number, |
| 78 UInt32 number_of_frames, | 81 UInt32 number_of_frames, |
| 79 AudioBufferList* io_data); | 82 AudioBufferList* io_data); |
| 80 | 83 |
| 81 // Pushes recorded data to consumer of the input audio stream. | 84 // Pushes recorded data to consumer of the input audio stream. |
| 82 OSStatus Provide(UInt32 number_of_frames, AudioBufferList* io_data, | 85 OSStatus Provide(UInt32 number_of_frames, AudioBufferList* io_data, |
| 83 const AudioTimeStamp* time_stamp); | 86 const AudioTimeStamp* time_stamp); |
| 84 | 87 |
| 85 // Gets the fixed capture hardware latency and store it during initialization. | 88 // Gets the fixed capture hardware latency and store it during initialization. |
| 86 // Returns 0 if not available. | 89 // Returns 0 if not available. |
| 87 double GetHardwareLatency(); | 90 double GetHardwareLatency(); |
| 88 | 91 |
| 89 // Gets the current capture delay value. | 92 // Gets the current capture delay value. |
| 90 double GetCaptureLatency(const AudioTimeStamp* input_time_stamp); | 93 double GetCaptureLatency(const AudioTimeStamp* input_time_stamp); |
| 91 | 94 |
| 95 // Gets the number of channels for a stream of audio data. |
| 96 UInt32 GetNumberOfChannelsFromStream(); |
| 97 |
| 92 // Issues the OnError() callback to the |sink_|. | 98 // Issues the OnError() callback to the |sink_|. |
| 93 void HandleError(OSStatus err); | 99 void HandleError(OSStatus err); |
| 94 | 100 |
| 101 // Helper function to check if the volume control is avialable on specific |
| 102 // channel. |
| 103 bool IsVolumeSettableOnChannel(int channel); |
| 104 |
| 95 // Our creator, the audio manager needs to be notified when we close. | 105 // Our creator, the audio manager needs to be notified when we close. |
| 96 AudioManagerMac* manager_; | 106 AudioManagerMac* manager_; |
| 97 | 107 |
| 98 // Contains the desired number of audio frames in each callback. | 108 // Contains the desired number of audio frames in each callback. |
| 99 size_t number_of_frames_; | 109 size_t number_of_frames_; |
| 100 | 110 |
| 101 // Pointer to the object that will receive the recorded audio samples. | 111 // Pointer to the object that will receive the recorded audio samples. |
| 102 AudioInputCallback* sink_; | 112 AudioInputCallback* sink_; |
| 103 | 113 |
| 104 // Structure that holds the desired output format of the stream. | 114 // Structure that holds the desired output format of the stream. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 119 // Temporary storage for recorded data. The InputProc() renders into this | 129 // Temporary storage for recorded data. The InputProc() renders into this |
| 120 // array as soon as a frame of the desired buffer size has been recorded. | 130 // array as soon as a frame of the desired buffer size has been recorded. |
| 121 scoped_array<uint8> audio_data_buffer_; | 131 scoped_array<uint8> audio_data_buffer_; |
| 122 | 132 |
| 123 // True after successfull Start(), false after successful Stop(). | 133 // True after successfull Start(), false after successful Stop(). |
| 124 bool started_; | 134 bool started_; |
| 125 | 135 |
| 126 // Fixed capture hardware latency in frames. | 136 // Fixed capture hardware latency in frames. |
| 127 double hardware_latency_frames_; | 137 double hardware_latency_frames_; |
| 128 | 138 |
| 139 // The number of channels in each frame of audio data, which is used |
| 140 // when querying the volume of each channel. |
| 141 UInt32 number_of_channels_in_frame_; |
| 142 |
| 129 DISALLOW_COPY_AND_ASSIGN(AUAudioInputStream); | 143 DISALLOW_COPY_AND_ASSIGN(AUAudioInputStream); |
| 130 }; | 144 }; |
| 131 | 145 |
| 132 #endif // MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_INPUT_MAC_H_ | 146 #endif // MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_INPUT_MAC_H_ |
| OLD | NEW |