Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / media / audio / audio_manager_base.h
blob6dd51e30476fad329a64a945523a8893cc08a9f9
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 <map>
9 #include <string>
10 #include <utility>
12 #include "base/atomic_ref_count.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/synchronization/lock.h"
17 #include "media/audio/audio_manager.h"
19 #if defined(OS_WIN)
20 #include "base/win/scoped_com_initializer.h"
21 #endif
23 #if defined(OS_ANDROID)
24 #include "base/android/jni_android.h"
25 #endif
27 namespace base {
28 class Thread;
31 namespace media {
33 class AudioOutputDispatcher;
35 // AudioManagerBase provides AudioManager functions common for all platforms.
36 class MEDIA_EXPORT AudioManagerBase : public AudioManager {
37 public:
38 // Name of the generic "default" device.
39 static const char kDefaultDeviceName[];
40 // Unique Id of the generic "default" device.
41 static const char kDefaultDeviceId[];
43 virtual ~AudioManagerBase();
45 virtual scoped_refptr<base::MessageLoopProxy> GetMessageLoop() OVERRIDE;
47 virtual string16 GetAudioInputDeviceModel() OVERRIDE;
49 virtual void ShowAudioInputSettings() OVERRIDE;
51 virtual void GetAudioInputDeviceNames(
52 media::AudioDeviceNames* device_names) OVERRIDE;
54 virtual AudioOutputStream* MakeAudioOutputStream(
55 const AudioParameters& params) OVERRIDE;
57 virtual AudioInputStream* MakeAudioInputStream(
58 const AudioParameters& params, const std::string& device_id) OVERRIDE;
60 virtual AudioOutputStream* MakeAudioOutputStreamProxy(
61 const AudioParameters& params) OVERRIDE;
63 virtual bool IsRecordingInProcess() OVERRIDE;
65 // Called internally by the audio stream when it has been closed.
66 virtual void ReleaseOutputStream(AudioOutputStream* stream);
67 virtual void ReleaseInputStream(AudioInputStream* stream);
69 void IncreaseActiveInputStreamCount();
70 void DecreaseActiveInputStreamCount();
72 // Creates the output stream for the |AUDIO_PCM_LINEAR| format. The legacy
73 // name is also from |AUDIO_PCM_LINEAR|.
74 virtual AudioOutputStream* MakeLinearOutputStream(
75 const AudioParameters& params) = 0;
77 // Creates the output stream for the |AUDIO_PCM_LOW_LATENCY| format.
78 virtual AudioOutputStream* MakeLowLatencyOutputStream(
79 const AudioParameters& params) = 0;
81 // Creates the input stream for the |AUDIO_PCM_LINEAR| format. The legacy
82 // name is also from |AUDIO_PCM_LINEAR|.
83 virtual AudioInputStream* MakeLinearInputStream(
84 const AudioParameters& params, const std::string& device_id) = 0;
86 // Creates the input stream for the |AUDIO_PCM_LOW_LATENCY| format.
87 virtual AudioInputStream* MakeLowLatencyInputStream(
88 const AudioParameters& params, const std::string& device_id) = 0;
90 // Listeners will be notified on the AudioManager::GetMessageLoop() loop.
91 virtual void AddOutputDeviceChangeListener(
92 AudioDeviceListener* listener) OVERRIDE;
93 virtual void RemoveOutputDeviceChangeListener(
94 AudioDeviceListener* listener) OVERRIDE;
96 virtual AudioParameters GetDefaultOutputStreamParameters() OVERRIDE;
97 virtual AudioParameters GetInputStreamParameters(
98 const std::string& device_id) OVERRIDE;
100 #if defined(OS_ANDROID)
101 static bool RegisterAudioManager(JNIEnv* env);
102 #endif
104 protected:
105 AudioManagerBase();
107 // TODO(dalecurtis): This must change to map both input and output parameters
108 // to a single dispatcher, otherwise on a device state change we'll just get
109 // the exact same invalid dispatcher.
110 typedef std::map<std::pair<AudioParameters, AudioParameters>,
111 scoped_refptr<AudioOutputDispatcher> >
112 AudioOutputDispatchersMap;
114 // Shuts down the audio thread and releases all the audio output dispatchers
115 // on the audio thread. All audio streams should be freed before Shutdown()
116 // is called. This must be called in the destructor of every AudioManagerBase
117 // implementation.
118 void Shutdown();
120 void SetMaxOutputStreamsAllowed(int max) { max_num_output_streams_ = max; }
122 // Called by each platform specific AudioManager to notify output state change
123 // listeners that a state change has occurred. Must be called from the audio
124 // thread.
125 void NotifyAllOutputDeviceChangeListeners();
127 // Returns the preferred hardware audio output parameters for opening output
128 // streams. If the users inject a valid |input_params|, each AudioManager
129 // will decide if they should return the values from |input_params| or the
130 // default hardware values. If the |input_params| is invalid, it will return
131 // the default hardware audio parameters.
132 virtual AudioParameters GetPreferredOutputStreamParameters(
133 const AudioParameters& input_params) = 0;
135 // Map of cached AudioOutputDispatcher instances. Must only be touched
136 // from the audio thread (no locking).
137 AudioOutputDispatchersMap output_dispatchers_;
139 private:
140 // Called by Shutdown().
141 void ShutdownOnAudioThread();
143 #if defined(OS_ANDROID)
144 void SetAudioMode(int mode);
145 void RegisterHeadsetReceiver();
146 void UnregisterHeadsetReceiver();
147 #endif
149 // Counts the number of active input streams to find out if something else
150 // is currently recording in Chrome.
151 base::AtomicRefCount num_active_input_streams_;
153 // Max number of open output streams, modified by
154 // SetMaxOutputStreamsAllowed().
155 int max_num_output_streams_;
157 // Max number of open input streams.
158 int max_num_input_streams_;
160 // Number of currently open output streams.
161 int num_output_streams_;
163 // Number of currently open input streams.
164 int num_input_streams_;
166 // Track output state change listeners.
167 ObserverList<AudioDeviceListener> output_listeners_;
169 // Thread used to interact with audio streams created by this audio manager.
170 scoped_ptr<base::Thread> audio_thread_;
171 mutable base::Lock audio_thread_lock_;
173 // The message loop of the audio thread this object runs on. Used for internal
174 // tasks which run on the audio thread even after Shutdown() has been started
175 // and GetMessageLoop() starts returning NULL.
176 scoped_refptr<base::MessageLoopProxy> message_loop_;
178 #if defined(OS_ANDROID)
179 // Java AudioManager instance.
180 base::android::ScopedJavaGlobalRef<jobject> j_audio_manager_;
181 #endif
183 DISALLOW_COPY_AND_ASSIGN(AudioManagerBase);
186 } // namespace media
188 #endif // MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_