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