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 #ifndef MEDIA_AUDIO_MAC_AUDIO_MANAGER_MAC_H_
6 #define MEDIA_AUDIO_MAC_AUDIO_MANAGER_MAC_H_
8 #include <AudioUnit/AudioUnit.h>
9 #include <CoreAudio/AudioHardware.h>
13 #include "base/basictypes.h"
14 #include "base/compiler_specific.h"
15 #include "media/audio/audio_manager_base.h"
16 #include "media/audio/mac/audio_device_listener_mac.h"
20 class AUAudioInputStream
;
23 // Mac OS X implementation of the AudioManager singleton. This class is internal
24 // to the audio output and only internal users can call methods not exposed by
25 // the AudioManager class.
26 class MEDIA_EXPORT AudioManagerMac
: public AudioManagerBase
{
28 AudioManagerMac(AudioLogFactory
* audio_log_factory
);
30 // Implementation of AudioManager.
31 bool HasAudioOutputDevices() override
;
32 bool HasAudioInputDevices() override
;
33 void GetAudioInputDeviceNames(AudioDeviceNames
* device_names
) override
;
34 void GetAudioOutputDeviceNames(AudioDeviceNames
* device_names
) override
;
35 AudioParameters
GetInputStreamParameters(
36 const std::string
& device_id
) override
;
37 std::string
GetAssociatedOutputDeviceID(
38 const std::string
& input_device_id
) override
;
40 // Implementation of AudioManagerBase.
41 AudioOutputStream
* MakeLinearOutputStream(
42 const AudioParameters
& params
) override
;
43 AudioOutputStream
* MakeLowLatencyOutputStream(
44 const AudioParameters
& params
,
45 const std::string
& device_id
) override
;
46 AudioInputStream
* MakeLinearInputStream(
47 const AudioParameters
& params
,
48 const std::string
& device_id
) override
;
49 AudioInputStream
* MakeLowLatencyInputStream(
50 const AudioParameters
& params
,
51 const std::string
& device_id
) override
;
52 std::string
GetDefaultOutputDeviceID() override
;
54 // Used to track destruction of input and output streams.
55 void ReleaseOutputStream(AudioOutputStream
* stream
) override
;
56 void ReleaseInputStream(AudioInputStream
* stream
) override
;
58 static bool GetDefaultInputDevice(AudioDeviceID
* device
);
59 static bool GetDefaultOutputDevice(AudioDeviceID
* device
);
60 static bool GetDefaultDevice(AudioDeviceID
* device
, bool input
);
62 static bool GetDefaultOutputChannels(int* channels
);
64 static bool GetDeviceChannels(AudioDeviceID device
,
65 AudioObjectPropertyScope scope
,
68 static int HardwareSampleRateForDevice(AudioDeviceID device_id
);
69 static int HardwareSampleRate();
71 // OSX has issues with starting streams as the sytem goes into suspend and
72 // immediately after it wakes up from resume. See http://crbug.com/160920.
73 // As a workaround we delay Start() when it occurs after suspend and for a
74 // small amount of time after resume.
76 // Streams should consult ShouldDeferStreamStart() and if true check the value
77 // again after |kStartDelayInSecsForPowerEvents| has elapsed. If false, the
78 // stream may be started immediately.
79 enum { kStartDelayInSecsForPowerEvents
= 1 };
80 bool ShouldDeferStreamStart();
82 // Changes the buffer size for |device_id| if there are no active input or
83 // output streams on the device or |desired_buffer_size| is lower than the
84 // current device buffer size.
86 // Returns false if an error occurred. There is no indication if the buffer
87 // size was changed or not.
88 // |element| is 0 for output streams and 1 for input streams.
89 // TODO(dalecurtis): we could change the the last parameter to an input/output
90 // pointer so it can be updated if the buffer size is not changed.
91 // See http://crbug.com/428706 for details.
92 bool MaybeChangeBufferSize(AudioDeviceID device_id
,
94 AudioUnitElement element
,
95 size_t desired_buffer_size
);
98 ~AudioManagerMac() override
;
100 AudioParameters
GetPreferredOutputStreamParameters(
101 const std::string
& output_device_id
,
102 const AudioParameters
& input_params
) override
;
105 void InitializeOnAudioThread();
106 void ShutdownOnAudioThread();
108 int ChooseBufferSize(bool is_input
, int sample_rate
);
110 // Notify streams of a device change if the default output device or its
111 // sample rate has changed, otherwise does nothing.
112 void HandleDeviceChanges();
114 scoped_ptr
<AudioDeviceListenerMac
> output_device_listener_
;
116 // Track the output sample-rate and the default output device
117 // so we can intelligently handle device notifications only when necessary.
118 int current_sample_rate_
;
119 AudioDeviceID current_output_device_
;
121 // Helper class which monitors power events to determine if output streams
122 // should defer Start() calls. Required to workaround an OSX bug. See
123 // http://crbug.com/160920 for more details.
124 class AudioPowerObserver
;
125 scoped_ptr
<AudioPowerObserver
> power_observer_
;
127 // Tracks all constructed input and output streams so they can be stopped at
128 // shutdown. See ShutdownOnAudioThread() for more details.
129 std::list
<AudioInputStream
*> basic_input_streams_
;
130 std::list
<AUAudioInputStream
*> low_latency_input_streams_
;
131 std::list
<AUHALStream
*> output_streams_
;
133 DISALLOW_COPY_AND_ASSIGN(AudioManagerMac
);
138 #endif // MEDIA_AUDIO_MAC_AUDIO_MANAGER_MAC_H_