audio_manager.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_H_
6#define MEDIA_AUDIO_AUDIO_MANAGER_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "base/string16.h"
13#include "media/audio/audio_device_name.h"
14#include "media/audio/audio_parameters.h"
15
16namespace base {
17class MessageLoop;
18class MessageLoopProxy;
19}
20
21namespace media {
22
23class AudioInputStream;
24class AudioOutputStream;
25
26// Manages all audio resources. In particular it owns the AudioOutputStream
27// objects. Provides some convenience functions that avoid the need to provide
28// iterators over the existing streams.
29class MEDIA_EXPORT AudioManager {
30 public:
31  virtual ~AudioManager();
32
33  // Use to construct the audio manager.
34  // NOTE: There should only be one instance.
35  static AudioManager* Create();
36
37  // Returns the pointer to the last created instance, or NULL if not yet
38  // created. This is a utility method for the code outside of media directory,
39  // like src/chrome.
40  static AudioManager* Get();
41
42  // Returns true if the OS reports existence of audio devices. This does not
43  // guarantee that the existing devices support all formats and sample rates.
44  virtual bool HasAudioOutputDevices() = 0;
45
46  // Returns true if the OS reports existence of audio recording devices. This
47  // does not guarantee that the existing devices support all formats and
48  // sample rates.
49  virtual bool HasAudioInputDevices() = 0;
50
51  // Returns a human readable string for the model/make of the active audio
52  // input device for this computer.
53  virtual string16 GetAudioInputDeviceModel() = 0;
54
55  // Opens the platform default audio input settings UI.
56  // Note: This could invoke an external application/preferences pane, so
57  // ideally must not be called from the UI thread or other time sensitive
58  // threads to avoid blocking the rest of the application.
59  virtual void ShowAudioInputSettings() = 0;
60
61  // Appends a list of available input devices. It is not guaranteed that
62  // all the devices in the list support all formats and sample rates for
63  // recording.
64  virtual void GetAudioInputDeviceNames(AudioDeviceNames* device_names) = 0;
65
66  // Factory for all the supported stream formats. |params| defines parameters
67  // of the audio stream to be created.
68  //
69  // |params.sample_per_packet| is the requested buffer allocation which the
70  // audio source thinks it can usually fill without blocking. Internally two
71  // or three buffers are created, one will be locked for playback and one will
72  // be ready to be filled in the call to AudioSourceCallback::OnMoreData().
73  //
74  // Returns NULL if the combination of the parameters is not supported, or if
75  // we have reached some other platform specific limit.
76  //
77  // |params.format| can be set to AUDIO_PCM_LOW_LATENCY and that has two
78  // effects:
79  // 1- Instead of triple buffered the audio will be double buffered.
80  // 2- A low latency driver or alternative audio subsystem will be used when
81  //    available.
82  //
83  // Do not free the returned AudioOutputStream. It is owned by AudioManager.
84  virtual AudioOutputStream* MakeAudioOutputStream(
85      const AudioParameters& params) = 0;
86
87  // Creates new audio output proxy. A proxy implements
88  // AudioOutputStream interface, but unlike regular output stream
89  // created with MakeAudioOutputStream() it opens device only when a
90  // sound is actually playing.
91  virtual AudioOutputStream* MakeAudioOutputStreamProxy(
92      const AudioParameters& params) = 0;
93
94  // Factory to create audio recording streams.
95  // |channels| can be 1 or 2.
96  // |sample_rate| is in hertz and can be any value supported by the platform.
97  // |bits_per_sample| can be any value supported by the platform.
98  // |samples_per_packet| is in hertz as well and can be 0 to |sample_rate|,
99  // with 0 suggesting that the implementation use a default value for that
100  // platform.
101  // Returns NULL if the combination of the parameters is not supported, or if
102  // we have reached some other platform specific limit.
103  //
104  // Do not free the returned AudioInputStream. It is owned by AudioManager.
105  // When you are done with it, call |Stop()| and |Close()| to release it.
106  virtual AudioInputStream* MakeAudioInputStream(
107      const AudioParameters& params, const std::string& device_id) = 0;
108
109  // Used to determine if something else is currently making use of audio input.
110  virtual bool IsRecordingInProcess() = 0;
111
112  // Returns message loop used for audio IO.
113  virtual scoped_refptr<base::MessageLoopProxy> GetMessageLoop() = 0;
114
115  // Allows clients to listen for device state changes; e.g. preferred sample
116  // rate or channel layout changes.  The typical response to receiving this
117  // callback is to recreate the stream.
118  class AudioDeviceListener {
119   public:
120    virtual void OnDeviceChange() = 0;
121  };
122
123  virtual void AddOutputDeviceChangeListener(AudioDeviceListener* listener) = 0;
124  virtual void RemoveOutputDeviceChangeListener(
125      AudioDeviceListener* listener) = 0;
126
127  // Returns the default output hardware audio parameters for opening output
128  // streams. It is a convenience interface to
129  // AudioManagerBase::GetPreferredOutputStreamParameters and each AudioManager
130  // does not need their own implementation to this interface.
131  virtual AudioParameters GetDefaultOutputStreamParameters() = 0;
132
133  // Returns the input hardware audio parameters of the specific device
134  // for opening input streams. Each AudioManager needs to implement their own
135  // version of this interface.
136  virtual AudioParameters GetInputStreamParameters(
137      const std::string& device_id) = 0;
138
139 protected:
140  AudioManager();
141
142 private:
143  DISALLOW_COPY_AND_ASSIGN(AudioManager);
144};
145
146}  // namespace media
147
148#endif  // MEDIA_AUDIO_AUDIO_MANAGER_H_
149