Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / audio / audio_manager_base.h
blob346fe47f79bb6ab2b6931f3ba38edd452e12cb16
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_AUDIO_MANAGER_BASE_H_
6 #define MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_
8 #include <string>
9 #include <utility>
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/observer_list.h"
15 #include "base/threading/thread.h"
16 #include "media/audio/audio_manager.h"
18 #include "media/audio/audio_output_dispatcher.h"
20 #if defined(OS_WIN)
21 #include "base/win/scoped_com_initializer.h"
22 #endif
24 namespace media {
26 class AudioOutputDispatcher;
28 // AudioManagerBase provides AudioManager functions common for all platforms.
29 class MEDIA_EXPORT AudioManagerBase : public AudioManager {
30 public:
31 // TODO(sergeyu): The constants below belong to AudioManager interface, not
32 // to the base implementation.
34 // Name of the generic "default" device.
35 static const char kDefaultDeviceName[];
36 // Unique Id of the generic "default" device.
37 static const char kDefaultDeviceId[];
38 // Unique Id of the generic default communications device (where supported).
39 static const char kCommunicationsDeviceId[];
40 // Name of the generic default communications device (where supported).
41 static const char kCommunicationsDeviceName[];
43 // Input device ID used to capture the default system playback stream. When
44 // this device ID is passed to MakeAudioInputStream() the returned
45 // AudioInputStream will be capturing audio currently being played on the
46 // default playback device. At the moment this feature is supported only on
47 // some platforms. AudioInputStream::Intialize() will return an error on
48 // platforms that don't support it. GetInputStreamParameters() must be used
49 // to get the parameters of the loopback device before creating a loopback
50 // stream, otherwise stream initialization may fail.
51 static const char kLoopbackInputDeviceId[];
53 ~AudioManagerBase() override;
55 // AudioManager:
56 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() override;
57 scoped_refptr<base::SingleThreadTaskRunner> GetWorkerTaskRunner() override;
58 base::string16 GetAudioInputDeviceModel() override;
59 void ShowAudioInputSettings() override;
60 void GetAudioInputDeviceNames(AudioDeviceNames* device_names) override;
61 void GetAudioOutputDeviceNames(AudioDeviceNames* device_names) override;
62 AudioOutputStream* MakeAudioOutputStream(
63 const AudioParameters& params,
64 const std::string& device_id) override;
65 AudioInputStream* MakeAudioInputStream(const AudioParameters& params,
66 const std::string& device_id) override;
67 AudioOutputStream* MakeAudioOutputStreamProxy(
68 const AudioParameters& params,
69 const std::string& device_id) override;
71 // Listeners will be notified on the GetTaskRunner() task runner.
72 void AddOutputDeviceChangeListener(AudioDeviceListener* listener) override;
73 void RemoveOutputDeviceChangeListener(AudioDeviceListener* listener) override;
75 AudioParameters GetDefaultOutputStreamParameters() override;
76 AudioParameters GetOutputStreamParameters(
77 const std::string& device_id) override;
78 AudioParameters GetInputStreamParameters(
79 const std::string& device_id) override;
80 std::string GetAssociatedOutputDeviceID(
81 const std::string& input_device_id) override;
82 scoped_ptr<AudioLog> CreateAudioLog(
83 AudioLogFactory::AudioComponent component) override;
84 void SetHasKeyboardMic() override;
86 // AudioManagerBase:
88 // Called internally by the audio stream when it has been closed.
89 virtual void ReleaseOutputStream(AudioOutputStream* stream);
90 virtual void ReleaseInputStream(AudioInputStream* stream);
92 // Creates the output stream for the |AUDIO_PCM_LINEAR| format. The legacy
93 // name is also from |AUDIO_PCM_LINEAR|.
94 virtual AudioOutputStream* MakeLinearOutputStream(
95 const AudioParameters& params) = 0;
97 // Creates the output stream for the |AUDIO_PCM_LOW_LATENCY| format.
98 virtual AudioOutputStream* MakeLowLatencyOutputStream(
99 const AudioParameters& params,
100 const std::string& device_id) = 0;
102 // Creates the input stream for the |AUDIO_PCM_LINEAR| format. The legacy
103 // name is also from |AUDIO_PCM_LINEAR|.
104 virtual AudioInputStream* MakeLinearInputStream(
105 const AudioParameters& params, const std::string& device_id) = 0;
107 // Creates the input stream for the |AUDIO_PCM_LOW_LATENCY| format.
108 virtual AudioInputStream* MakeLowLatencyInputStream(
109 const AudioParameters& params, const std::string& device_id) = 0;
111 // Get number of input or output streams.
112 int input_stream_count() const { return num_input_streams_; }
113 int output_stream_count() const { return num_output_streams_; }
115 protected:
116 AudioManagerBase(AudioLogFactory* audio_log_factory);
118 // Shuts down the audio thread and releases all the audio output dispatchers
119 // on the audio thread. All audio streams should be freed before Shutdown()
120 // is called. This must be called in the destructor of every AudioManagerBase
121 // implementation.
122 void Shutdown();
124 void SetMaxOutputStreamsAllowed(int max) { max_num_output_streams_ = max; }
126 // Called by each platform specific AudioManager to notify output state change
127 // listeners that a state change has occurred. Must be called from the audio
128 // thread.
129 void NotifyAllOutputDeviceChangeListeners();
131 // Returns user buffer size as specified on the command line or 0 if no buffer
132 // size has been specified.
133 int GetUserBufferSize();
135 // Returns the preferred hardware audio output parameters for opening output
136 // streams. If the users inject a valid |input_params|, each AudioManager
137 // will decide if they should return the values from |input_params| or the
138 // default hardware values. If the |input_params| is invalid, it will return
139 // the default hardware audio parameters.
140 // If |output_device_id| is empty, the implementation must treat that as
141 // a request for the default output device.
142 virtual AudioParameters GetPreferredOutputStreamParameters(
143 const std::string& output_device_id,
144 const AudioParameters& input_params) = 0;
146 // Returns the ID of the default audio output device.
147 // Implementations that don't yet support this should return an empty string.
148 virtual std::string GetDefaultOutputDeviceID();
150 private:
151 struct DispatcherParams;
152 typedef ScopedVector<DispatcherParams> AudioOutputDispatchers;
154 class CompareByParams;
156 // Called by Shutdown().
157 void ShutdownOnAudioThread();
159 // Max number of open output streams, modified by
160 // SetMaxOutputStreamsAllowed().
161 int max_num_output_streams_;
163 // Max number of open input streams.
164 int max_num_input_streams_;
166 // Number of currently open output streams.
167 int num_output_streams_;
169 // Number of currently open input streams.
170 int num_input_streams_;
172 // Track output state change listeners.
173 base::ObserverList<AudioDeviceListener> output_listeners_;
175 // Thread used to interact with audio streams created by this audio manager.
176 base::Thread audio_thread_;
178 // The task runner of the audio thread this object runs on. Used for internal
179 // tasks which run on the audio thread even after Shutdown() has been started
180 // and GetTaskRunner() starts returning NULL.
181 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
183 // Map of cached AudioOutputDispatcher instances. Must only be touched
184 // from the audio thread (no locking).
185 AudioOutputDispatchers output_dispatchers_;
187 // Proxy for creating AudioLog objects.
188 AudioLogFactory* const audio_log_factory_;
190 DISALLOW_COPY_AND_ASSIGN(AudioManagerBase);
193 } // namespace media
195 #endif // MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_