Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / media / audio / audio_manager_base.h
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 <string>
9 #include <utility>
10
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"
17
18 #include "media/audio/audio_output_dispatcher.h"
19
20 #if defined(OS_WIN)
21 #include "base/win/scoped_com_initializer.h"
22 #endif
23
24 namespace media {
25
26 class AudioOutputDispatcher;
27
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.
33
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   // Input device ID used to capture the default system playback stream. When
40   // this device ID is passed to MakeAudioInputStream() the returned
41   // AudioInputStream will be capturing audio currently being played on the
42   // default playback device. At the moment this feature is supported only on
43   // some platforms. AudioInputStream::Intialize() will return an error on
44   // platforms that don't support it. GetInputStreamParameters() must be used
45   // to get the parameters of the loopback device before creating a loopback
46   // stream, otherwise stream initialization may fail.
47   static const char kLoopbackInputDeviceId[];
48
49   ~AudioManagerBase() override;
50
51   scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() override;
52   scoped_refptr<base::SingleThreadTaskRunner> GetWorkerTaskRunner() override;
53
54   base::string16 GetAudioInputDeviceModel() override;
55
56   void ShowAudioInputSettings() override;
57
58   void GetAudioInputDeviceNames(AudioDeviceNames* device_names) override;
59
60   void GetAudioOutputDeviceNames(AudioDeviceNames* device_names) override;
61
62   AudioOutputStream* MakeAudioOutputStream(
63       const AudioParameters& params,
64       const std::string& device_id) override;
65
66   AudioInputStream* MakeAudioInputStream(const AudioParameters& params,
67                                          const std::string& device_id) override;
68
69   AudioOutputStream* MakeAudioOutputStreamProxy(
70       const AudioParameters& params,
71       const std::string& device_id) override;
72
73   // Called internally by the audio stream when it has been closed.
74   virtual void ReleaseOutputStream(AudioOutputStream* stream);
75   virtual void ReleaseInputStream(AudioInputStream* stream);
76
77   // Creates the output stream for the |AUDIO_PCM_LINEAR| format. The legacy
78   // name is also from |AUDIO_PCM_LINEAR|.
79   virtual AudioOutputStream* MakeLinearOutputStream(
80       const AudioParameters& params) = 0;
81
82   // Creates the output stream for the |AUDIO_PCM_LOW_LATENCY| format.
83   virtual AudioOutputStream* MakeLowLatencyOutputStream(
84       const AudioParameters& params,
85       const std::string& device_id) = 0;
86
87   // Creates the input stream for the |AUDIO_PCM_LINEAR| format. The legacy
88   // name is also from |AUDIO_PCM_LINEAR|.
89   virtual AudioInputStream* MakeLinearInputStream(
90       const AudioParameters& params, const std::string& device_id) = 0;
91
92   // Creates the input stream for the |AUDIO_PCM_LOW_LATENCY| format.
93   virtual AudioInputStream* MakeLowLatencyInputStream(
94       const AudioParameters& params, const std::string& device_id) = 0;
95
96   // Listeners will be notified on the GetTaskRunner() task runner.
97   void AddOutputDeviceChangeListener(AudioDeviceListener* listener) override;
98   void RemoveOutputDeviceChangeListener(AudioDeviceListener* listener) override;
99
100   AudioParameters GetDefaultOutputStreamParameters() override;
101   AudioParameters GetOutputStreamParameters(
102       const std::string& device_id) override;
103
104   AudioParameters GetInputStreamParameters(
105       const std::string& device_id) override;
106
107   std::string GetAssociatedOutputDeviceID(
108       const std::string& input_device_id) override;
109
110   scoped_ptr<AudioLog> CreateAudioLog(
111       AudioLogFactory::AudioComponent component) override;
112
113   void SetHasKeyboardMic() override;
114
115   // Get number of input or output streams.
116   int input_stream_count() const { return num_input_streams_; }
117   int output_stream_count() const { return num_output_streams_; }
118
119  protected:
120   AudioManagerBase(AudioLogFactory* audio_log_factory);
121
122   // Shuts down the audio thread and releases all the audio output dispatchers
123   // on the audio thread.  All audio streams should be freed before Shutdown()
124   // is called.  This must be called in the destructor of every AudioManagerBase
125   // implementation.
126   void Shutdown();
127
128   void SetMaxOutputStreamsAllowed(int max) { max_num_output_streams_ = max; }
129
130   // Called by each platform specific AudioManager to notify output state change
131   // listeners that a state change has occurred.  Must be called from the audio
132   // thread.
133   void NotifyAllOutputDeviceChangeListeners();
134
135   // Returns user buffer size as specified on the command line or 0 if no buffer
136   // size has been specified.
137   int GetUserBufferSize();
138
139   // Returns the preferred hardware audio output parameters for opening output
140   // streams. If the users inject a valid |input_params|, each AudioManager
141   // will decide if they should return the values from |input_params| or the
142   // default hardware values. If the |input_params| is invalid, it will return
143   // the default hardware audio parameters.
144   // If |output_device_id| is empty, the implementation must treat that as
145   // a request for the default output device.
146   virtual AudioParameters GetPreferredOutputStreamParameters(
147       const std::string& output_device_id,
148       const AudioParameters& input_params) = 0;
149
150   // Returns the ID of the default audio output device.
151   // Implementations that don't yet support this should return an empty string.
152   virtual std::string GetDefaultOutputDeviceID();
153
154  private:
155   struct DispatcherParams;
156   typedef ScopedVector<DispatcherParams> AudioOutputDispatchers;
157
158   class CompareByParams;
159
160   // Called by Shutdown().
161   void ShutdownOnAudioThread();
162
163   // Max number of open output streams, modified by
164   // SetMaxOutputStreamsAllowed().
165   int max_num_output_streams_;
166
167   // Max number of open input streams.
168   int max_num_input_streams_;
169
170   // Number of currently open output streams.
171   int num_output_streams_;
172
173   // Number of currently open input streams.
174   int num_input_streams_;
175
176   // Track output state change listeners.
177   ObserverList<AudioDeviceListener> output_listeners_;
178
179   // Thread used to interact with audio streams created by this audio manager.
180   base::Thread audio_thread_;
181
182   // The task runner of the audio thread this object runs on. Used for internal
183   // tasks which run on the audio thread even after Shutdown() has been started
184   // and GetTaskRunner() starts returning NULL.
185   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
186
187   // Map of cached AudioOutputDispatcher instances.  Must only be touched
188   // from the audio thread (no locking).
189   AudioOutputDispatchers output_dispatchers_;
190
191   // Proxy for creating AudioLog objects.
192   AudioLogFactory* const audio_log_factory_;
193
194   DISALLOW_COPY_AND_ASSIGN(AudioManagerBase);
195 };
196
197 }  // namespace media
198
199 #endif  // MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_