audio_manager_base.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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.
4
5#ifndef MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_
6#define MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_
7
8#include <map>
9#include <string>
10#include <utility>
11
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"
18
19#if defined(OS_WIN)
20#include "base/win/scoped_com_initializer.h"
21#endif
22
23#if defined(OS_ANDROID)
24#include "base/android/jni_android.h"
25#endif
26
27namespace base {
28class Thread;
29}
30
31namespace media {
32
33class AudioOutputDispatcher;
34
35// AudioManagerBase provides AudioManager functions common for all platforms.
36class 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[];
42
43  virtual ~AudioManagerBase();
44
45  virtual scoped_refptr<base::MessageLoopProxy> GetMessageLoop() OVERRIDE;
46
47  virtual string16 GetAudioInputDeviceModel() OVERRIDE;
48
49  virtual void ShowAudioInputSettings() OVERRIDE;
50
51  virtual void GetAudioInputDeviceNames(
52      media::AudioDeviceNames* device_names) OVERRIDE;
53
54  virtual AudioOutputStream* MakeAudioOutputStream(
55      const AudioParameters& params) OVERRIDE;
56
57  virtual AudioInputStream* MakeAudioInputStream(
58      const AudioParameters& params, const std::string& device_id) OVERRIDE;
59
60  virtual AudioOutputStream* MakeAudioOutputStreamProxy(
61      const AudioParameters& params) OVERRIDE;
62
63  virtual bool IsRecordingInProcess() OVERRIDE;
64
65  // Called internally by the audio stream when it has been closed.
66  virtual void ReleaseOutputStream(AudioOutputStream* stream);
67  virtual void ReleaseInputStream(AudioInputStream* stream);
68
69  void IncreaseActiveInputStreamCount();
70  void DecreaseActiveInputStreamCount();
71
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;
76
77  // Creates the output stream for the |AUDIO_PCM_LOW_LATENCY| format.
78  virtual AudioOutputStream* MakeLowLatencyOutputStream(
79      const AudioParameters& params) = 0;
80
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;
85
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;
89
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;
95
96  virtual AudioParameters GetDefaultOutputStreamParameters() OVERRIDE;
97  virtual AudioParameters GetInputStreamParameters(
98      const std::string& device_id) OVERRIDE;
99
100#if defined(OS_ANDROID)
101  static bool RegisterAudioManager(JNIEnv* env);
102#endif
103
104 protected:
105  AudioManagerBase();
106
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;
113
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();
119
120  void SetMaxOutputStreamsAllowed(int max) { max_num_output_streams_ = max; }
121
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();
126
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;
134
135  // Map of cached AudioOutputDispatcher instances.  Must only be touched
136  // from the audio thread (no locking).
137  AudioOutputDispatchersMap output_dispatchers_;
138
139 private:
140  // Called by Shutdown().
141  void ShutdownOnAudioThread();
142
143#if defined(OS_ANDROID)
144  void SetAudioMode(int mode);
145  void RegisterHeadsetReceiver();
146  void UnregisterHeadsetReceiver();
147#endif
148
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_;
152
153  // Max number of open output streams, modified by
154  // SetMaxOutputStreamsAllowed().
155  int max_num_output_streams_;
156
157  // Max number of open input streams.
158  int max_num_input_streams_;
159
160  // Number of currently open output streams.
161  int num_output_streams_;
162
163  // Number of currently open input streams.
164  int num_input_streams_;
165
166  // Track output state change listeners.
167  ObserverList<AudioDeviceListener> output_listeners_;
168
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_;
172
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_;
177
178#if defined(OS_ANDROID)
179  // Java AudioManager instance.
180  base::android::ScopedJavaGlobalRef<jobject> j_audio_manager_;
181#endif
182
183  DISALLOW_COPY_AND_ASSIGN(AudioManagerBase);
184};
185
186}  // namespace media
187
188#endif  // MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_
189