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 <CoreAudio/AudioHardware.h>
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "media/audio/audio_manager_base.h"
14 #include "media/audio/mac/aggregate_device_manager.h"
15 #include "media/audio/mac/audio_device_listener_mac.h"
19 // Mac OS X implementation of the AudioManager singleton. This class is internal
20 // to the audio output and only internal users can call methods not exposed by
21 // the AudioManager class.
22 class MEDIA_EXPORT AudioManagerMac : public AudioManagerBase {
24 AudioManagerMac(AudioLogFactory* audio_log_factory);
26 // Implementation of AudioManager.
27 virtual bool HasAudioOutputDevices() OVERRIDE;
28 virtual bool HasAudioInputDevices() OVERRIDE;
29 virtual void GetAudioInputDeviceNames(
30 AudioDeviceNames* device_names) OVERRIDE;
31 virtual void GetAudioOutputDeviceNames(
32 AudioDeviceNames* device_names) OVERRIDE;
33 virtual AudioParameters GetInputStreamParameters(
34 const std::string& device_id) OVERRIDE;
35 virtual std::string GetAssociatedOutputDeviceID(
36 const std::string& input_device_id) OVERRIDE;
38 // Implementation of AudioManagerBase.
39 virtual AudioOutputStream* MakeLinearOutputStream(
40 const AudioParameters& params) OVERRIDE;
41 virtual AudioOutputStream* MakeLowLatencyOutputStream(
42 const AudioParameters& params,
43 const std::string& device_id,
44 const std::string& input_device_id) OVERRIDE;
45 virtual AudioInputStream* MakeLinearInputStream(
46 const AudioParameters& params, const std::string& device_id) OVERRIDE;
47 virtual AudioInputStream* MakeLowLatencyInputStream(
48 const AudioParameters& params, const std::string& device_id) OVERRIDE;
49 virtual std::string GetDefaultOutputDeviceID() OVERRIDE;
51 static bool GetDefaultInputDevice(AudioDeviceID* device);
52 static bool GetDefaultOutputDevice(AudioDeviceID* device);
53 static bool GetDefaultDevice(AudioDeviceID* device, bool input);
55 static bool GetDefaultOutputChannels(int* channels);
57 static bool GetDeviceChannels(AudioDeviceID device,
58 AudioObjectPropertyScope scope,
61 static int HardwareSampleRateForDevice(AudioDeviceID device_id);
62 static int HardwareSampleRate();
64 // OSX has issues with starting streams as the sytem goes into suspend and
65 // immediately after it wakes up from resume. See http://crbug.com/160920.
66 // As a workaround we delay Start() when it occurs after suspend and for a
67 // small amount of time after resume.
69 // Output streams should consult ShouldDeferOutputStreamStart() and if true
70 // check the value again after |kStartDelayInSecsForPowerEvents| has elapsed.
71 // If false, the stream may be started immediately.
72 enum { kStartDelayInSecsForPowerEvents = 1 };
73 bool ShouldDeferOutputStreamStart();
76 virtual ~AudioManagerMac();
78 virtual AudioParameters GetPreferredOutputStreamParameters(
79 const std::string& output_device_id,
80 const AudioParameters& input_params) OVERRIDE;
83 bool HasUnifiedDefaultIO();
85 // Helper methods for constructing AudioDeviceListenerMac on the audio thread.
86 void CreateDeviceListener();
87 void DestroyDeviceListener();
89 int ChooseBufferSize(int output_sample_rate);
91 // Notify streams of a device change if the default output device or its
92 // sample rate has changed, otherwise does nothing.
93 void HandleDeviceChanges();
95 scoped_ptr<AudioDeviceListenerMac> output_device_listener_;
97 // Track the output sample-rate and the default output device
98 // so we can intelligently handle device notifications only when necessary.
99 int current_sample_rate_;
100 AudioDeviceID current_output_device_;
102 AggregateDeviceManager aggregate_device_manager_;
104 // Helper class which monitors power events to determine if output streams
105 // should defer Start() calls. Required to workaround an OSX bug. See
106 // http://crbug.com/160920 for more details.
107 class AudioPowerObserver;
108 scoped_ptr<AudioPowerObserver> power_observer_;
110 DISALLOW_COPY_AND_ASSIGN(AudioManagerMac);
115 #endif // MEDIA_AUDIO_MAC_AUDIO_MANAGER_MAC_H_