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 notes:
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
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.
17 #ifndef MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_OUTPUT_MAC_H_
18 #define MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_OUTPUT_MAC_H_
20 #include <AudioUnit/AudioUnit.h>
21 #include <CoreAudio/CoreAudio.h>
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"
30 class AudioManagerMac
;
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
{
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();
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
;
53 static int HardwareSampleRate();
56 // DefaultOutputUnit callback.
57 static OSStatus
InputProc(void* user_data
,
58 AudioUnitRenderActionFlags
* flags
,
59 const AudioTimeStamp
* time_stamp
,
61 UInt32 number_of_frames
,
62 AudioBufferList
* io_data
);
64 OSStatus
Render(UInt32 number_of_frames
, AudioBufferList
* io_data
,
65 const AudioTimeStamp
* output_time_stamp
);
67 // Sets up the stream format for the default output Audio Unit.
70 // Gets the fixed playout device hardware latency and stores it. Returns 0
72 double GetHardwareLatency();
74 // Gets the current playout latency value.
75 double GetPlayoutLatency(const AudioTimeStamp
* output_time_stamp
);
77 // Our creator, the audio manager needs to be notified when we close.
78 AudioManagerMac
* manager_
;
80 size_t number_of_frames_
;
82 // Pointer to the object that will provide the audio samples.
83 AudioSourceCallback
* source_
;
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_
;
89 // Structure that holds the stream format details such as bitrate.
90 AudioStreamBasicDescription format_
;
92 // The default output Audio Unit which talks to the audio hardware.
93 AudioUnit output_unit_
;
95 // The UID refers to the current output audio device.
96 AudioDeviceID output_device_id_
;
98 // Volume level from 0 to 1.
101 // Fixed playout hardware latency in frames.
102 double hardware_latency_frames_
;
104 // The flag used to stop the streaming.
107 // Container for retrieving data from AudioSourceCallback::OnMoreData().
108 scoped_ptr
<AudioBus
> audio_bus_
;
110 DISALLOW_COPY_AND_ASSIGN(AUAudioOutputStream
);
115 #endif // MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_OUTPUT_MAC_H_