audio_manager_base.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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
23namespace base {
24class Thread;
25}
26
27namespace media {
28
29class AudioOutputDispatcher;
30
31// AudioManagerBase provides AudioManager functions common for all platforms.
32class MEDIA_EXPORT AudioManagerBase : public AudioManager {
33 public:
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
39  virtual ~AudioManagerBase();
40
41  virtual scoped_refptr<base::MessageLoopProxy> GetMessageLoop() OVERRIDE;
42
43  virtual string16 GetAudioInputDeviceModel() OVERRIDE;
44
45  virtual void ShowAudioInputSettings() OVERRIDE;
46
47  virtual void GetAudioInputDeviceNames(
48      media::AudioDeviceNames* device_names) OVERRIDE;
49
50  virtual AudioOutputStream* MakeAudioOutputStream(
51      const AudioParameters& params) OVERRIDE;
52
53  virtual AudioInputStream* MakeAudioInputStream(
54      const AudioParameters& params, const std::string& device_id) OVERRIDE;
55
56  virtual AudioOutputStream* MakeAudioOutputStreamProxy(
57      const AudioParameters& params) OVERRIDE;
58
59  virtual bool IsRecordingInProcess() OVERRIDE;
60
61  // Called internally by the audio stream when it has been closed.
62  virtual void ReleaseOutputStream(AudioOutputStream* stream);
63  virtual void ReleaseInputStream(AudioInputStream* stream);
64
65  void IncreaseActiveInputStreamCount();
66  void DecreaseActiveInputStreamCount();
67
68  // Creates the output stream for the |AUDIO_PCM_LINEAR| format. The legacy
69  // name is also from |AUDIO_PCM_LINEAR|.
70  virtual AudioOutputStream* MakeLinearOutputStream(
71      const AudioParameters& params) = 0;
72
73  // Creates the output stream for the |AUDIO_PCM_LOW_LATENCY| format.
74  virtual AudioOutputStream* MakeLowLatencyOutputStream(
75      const AudioParameters& params) = 0;
76
77  // Creates the input stream for the |AUDIO_PCM_LINEAR| format. The legacy
78  // name is also from |AUDIO_PCM_LINEAR|.
79  virtual AudioInputStream* MakeLinearInputStream(
80      const AudioParameters& params, const std::string& device_id) = 0;
81
82  // Creates the input stream for the |AUDIO_PCM_LOW_LATENCY| format.
83  virtual AudioInputStream* MakeLowLatencyInputStream(
84      const AudioParameters& params, const std::string& device_id) = 0;
85
86  // Listeners will be notified on the AudioManager::GetMessageLoop() loop.
87  virtual void AddOutputDeviceChangeListener(
88      AudioDeviceListener* listener) OVERRIDE;
89  virtual void RemoveOutputDeviceChangeListener(
90      AudioDeviceListener* listener) OVERRIDE;
91
92  virtual AudioParameters GetDefaultOutputStreamParameters() OVERRIDE;
93  virtual AudioParameters GetInputStreamParameters(
94      const std::string& device_id) OVERRIDE;
95
96 protected:
97  AudioManagerBase();
98
99  // TODO(dalecurtis): This must change to map both input and output parameters
100  // to a single dispatcher, otherwise on a device state change we'll just get
101  // the exact same invalid dispatcher.
102  typedef std::map<std::pair<AudioParameters, AudioParameters>,
103                   scoped_refptr<AudioOutputDispatcher> >
104      AudioOutputDispatchersMap;
105
106  // Shuts down the audio thread and releases all the audio output dispatchers
107  // on the audio thread.  All audio streams should be freed before Shutdown()
108  // is called.  This must be called in the destructor of every AudioManagerBase
109  // implementation.
110  void Shutdown();
111
112  void SetMaxOutputStreamsAllowed(int max) { max_num_output_streams_ = max; }
113
114  // Called by each platform specific AudioManager to notify output state change
115  // listeners that a state change has occurred.  Must be called from the audio
116  // thread.
117  void NotifyAllOutputDeviceChangeListeners();
118
119  // Returns the preferred hardware audio output parameters for opening output
120  // streams. If the users inject a valid |input_params|, each AudioManager
121  // will decide if they should return the values from |input_params| or the
122  // default hardware values. If the |input_params| is invalid, it will return
123  // the default hardware audio parameters.
124  virtual AudioParameters GetPreferredOutputStreamParameters(
125      const AudioParameters& input_params) = 0;
126
127  // Map of cached AudioOutputDispatcher instances.  Must only be touched
128  // from the audio thread (no locking).
129  AudioOutputDispatchersMap output_dispatchers_;
130
131  // Get number of input or output streams.
132  int input_stream_count() { return num_input_streams_; }
133  int output_stream_count() { return num_output_streams_; }
134
135 private:
136  // Called by Shutdown().
137  void ShutdownOnAudioThread();
138
139  // Counts the number of active input streams to find out if something else
140  // is currently recording in Chrome.
141  base::AtomicRefCount num_active_input_streams_;
142
143  // Max number of open output streams, modified by
144  // SetMaxOutputStreamsAllowed().
145  int max_num_output_streams_;
146
147  // Max number of open input streams.
148  int max_num_input_streams_;
149
150  // Number of currently open output streams.
151  int num_output_streams_;
152
153  // Number of currently open input streams.
154  int num_input_streams_;
155
156  // Track output state change listeners.
157  ObserverList<AudioDeviceListener> output_listeners_;
158
159  // Thread used to interact with audio streams created by this audio manager.
160  scoped_ptr<base::Thread> audio_thread_;
161  mutable base::Lock audio_thread_lock_;
162
163  // The message loop of the audio thread this object runs on. Used for internal
164  // tasks which run on the audio thread even after Shutdown() has been started
165  // and GetMessageLoop() starts returning NULL.
166  scoped_refptr<base::MessageLoopProxy> message_loop_;
167
168  DISALLOW_COPY_AND_ASSIGN(AudioManagerBase);
169};
170
171}  // namespace media
172
173#endif  // MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_
174