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. 4 // 5 // Implementation notes: 6 // 7 // - It is recommended to first acquire the native sample rate of the default 8 // output device and then use the same rate when creating this object. 9 // Use AUAudioOutputStream::HardwareSampleRate() to retrieve the sample rate. 10 // - Calling Close() also leads to self destruction. 11 // - The latency consists of two parts: 12 // 1) Hardware latency, which includes Audio Unit latency, audio device 13 // latency; 14 // 2) The delay between the moment getting the callback and the scheduled time 15 // stamp that tells when the data is going to be played out. 16 // 17 #ifndef MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_OUTPUT_MAC_H_ 18 #define MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_OUTPUT_MAC_H_ 19 20 #include <AudioUnit/AudioUnit.h> 21 #include <CoreAudio/CoreAudio.h> 22 23 #include "base/compiler_specific.h" 24 #include "base/synchronization/lock.h" 25 #include "media/audio/audio_io.h" 26 #include "media/audio/audio_parameters.h" 27 28 namespace media { 29 30 class AudioManagerMac; 31 32 // Implementation of AudioOuputStream for Mac OS X using the 33 // default output Audio Unit present in OS 10.4 and later. 34 // The default output Audio Unit is for low-latency audio I/O. 35 class AUAudioOutputStream : public AudioOutputStream { 36 public: 37 // The ctor takes all the usual parameters, plus |manager| which is the 38 // the audio manager who is creating this object. 39 AUAudioOutputStream(AudioManagerMac* manager, 40 const AudioParameters& params); 41 // The dtor is typically called by the AudioManager only and it is usually 42 // triggered by calling AudioOutputStream::Close(). 43 virtual ~AUAudioOutputStream(); 44 45 // Implementation of AudioOutputStream. 46 virtual bool Open() OVERRIDE; 47 virtual void Close() OVERRIDE; 48 virtual void Start(AudioSourceCallback* callback) OVERRIDE; 49 virtual void Stop() OVERRIDE; 50 virtual void SetVolume(double volume) OVERRIDE; 51 virtual void GetVolume(double* volume) OVERRIDE; 52 53 static int HardwareSampleRate(); 54 55 private: 56 // DefaultOutputUnit callback. 57 static OSStatus InputProc(void* user_data, 58 AudioUnitRenderActionFlags* flags, 59 const AudioTimeStamp* time_stamp, 60 UInt32 bus_number, 61 UInt32 number_of_frames, 62 AudioBufferList* io_data); 63 64 OSStatus Render(UInt32 number_of_frames, AudioBufferList* io_data, 65 const AudioTimeStamp* output_time_stamp); 66 67 // Sets up the stream format for the default output Audio Unit. 68 bool Configure(); 69 70 // Gets the fixed playout device hardware latency and stores it. Returns 0 71 // if not available. 72 double GetHardwareLatency(); 73 74 // Gets the current playout latency value. 75 double GetPlayoutLatency(const AudioTimeStamp* output_time_stamp); 76 77 // Our creator, the audio manager needs to be notified when we close. 78 AudioManagerMac* manager_; 79 80 size_t number_of_frames_; 81 82 // Pointer to the object that will provide the audio samples. 83 AudioSourceCallback* source_; 84 85 // Protects |source_|. Necessary since Render() calls seem to be in flight 86 // when |output_unit_| is supposedly stopped. See http://crbug.com/178765. 87 base::Lock source_lock_; 88 89 // Structure that holds the stream format details such as bitrate. 90 AudioStreamBasicDescription format_; 91 92 // The default output Audio Unit which talks to the audio hardware. 93 AudioUnit output_unit_; 94 95 // The UID refers to the current output audio device. 96 AudioDeviceID output_device_id_; 97 98 // Volume level from 0 to 1. 99 float volume_; 100 101 // Fixed playout hardware latency in frames. 102 double hardware_latency_frames_; 103 104 // The flag used to stop the streaming. 105 bool stopped_; 106 107 // Container for retrieving data from AudioSourceCallback::OnMoreData(). 108 scoped_ptr<AudioBus> audio_bus_; 109 110 DISALLOW_COPY_AND_ASSIGN(AUAudioOutputStream); 111 }; 112 113 } // namespace media 114 115 #endif // MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_OUTPUT_MAC_H_ 116