audio_io.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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_IO_H_
6#define MEDIA_AUDIO_AUDIO_IO_H_
7
8#include "base/basictypes.h"
9#include "media/audio/audio_buffers_state.h"
10#include "media/base/audio_bus.h"
11
12// Low-level audio output support. To make sound there are 3 objects involved:
13// - AudioSource : produces audio samples on a pull model. Implements
14//   the AudioSourceCallback interface.
15// - AudioOutputStream : uses the AudioSource to render audio on a given
16//   channel, format and sample frequency configuration. Data from the
17//   AudioSource is delivered in a 'pull' model.
18// - AudioManager : factory for the AudioOutputStream objects, manager
19//   of the hardware resources and mixer control.
20//
21// The number and configuration of AudioOutputStream does not need to match the
22// physically available hardware resources. For example you can have:
23//
24//  MonoPCMSource1 --> MonoPCMStream1 --> |       | --> audio left channel
25//  StereoPCMSource -> StereoPCMStream -> | mixer |
26//  MonoPCMSource2 --> MonoPCMStream2 --> |       | --> audio right channel
27//
28// This facility's objective is mix and render audio with low overhead using
29// the OS basic audio support, abstracting as much as possible the
30// idiosyncrasies of each platform. Non-goals:
31// - Positional, 3d audio
32// - Dependence on non-default libraries such as DirectX 9, 10, XAudio
33// - Digital signal processing or effects
34// - Extra features if a specific hardware is installed (EAX, X-fi)
35//
36// The primary client of this facility is audio coming from several tabs.
37// Specifically for this case we avoid supporting complex formats such as MP3
38// or WMA. Complex format decoding should be done by the renderers.
39
40
41// Models an audio stream that gets rendered to the audio hardware output.
42// Because we support more audio streams than physically available channels
43// a given AudioOutputStream might or might not talk directly to hardware.
44// An audio stream allocates several buffers for audio data and calls
45// AudioSourceCallback::OnMoreData() periodically to fill these buffers,
46// as the data is written to the audio device. Size of each packet is determined
47// by |samples_per_packet| specified in AudioParameters  when the stream is
48// created.
49
50namespace media {
51
52class MEDIA_EXPORT AudioOutputStream {
53 public:
54  // Audio sources must implement AudioSourceCallback. This interface will be
55  // called in a random thread which very likely is a high priority thread. Do
56  // not rely on using this thread TLS or make calls that alter the thread
57  // itself such as creating Windows or initializing COM.
58  class MEDIA_EXPORT AudioSourceCallback {
59   public:
60    // Provide more data by fully filling |dest|.  The source will return
61    // the number of frames it filled.  |buffers_state| contains current state
62    // of the buffers, and can be used by the source to calculate delay.
63    virtual int OnMoreData(AudioBus* dest,
64                           AudioBuffersState buffers_state) = 0;
65
66    // There was an error while playing a buffer. Audio source cannot be
67    // destroyed yet. No direct action needed by the AudioStream, but it is
68    // a good place to stop accumulating sound data since is is likely that
69    // playback will not continue.
70    virtual void OnError(AudioOutputStream* stream) = 0;
71
72   protected:
73    virtual ~AudioSourceCallback() {}
74  };
75
76  virtual ~AudioOutputStream() {}
77
78  // Open the stream. false is returned if the stream cannot be opened.  Open()
79  // must always be followed by a call to Close() even if Open() fails.
80  virtual bool Open() = 0;
81
82  // Starts playing audio and generating AudioSourceCallback::OnMoreData().
83  // Since implementor of AudioOutputStream may have internal buffers, right
84  // after calling this method initial buffers are fetched.
85  //
86  // The output stream does not take ownership of this callback.
87  virtual void Start(AudioSourceCallback* callback) = 0;
88
89  // Stops playing audio. Effect might not be instantaneous as the hardware
90  // might have locked audio data that is processing.
91  virtual void Stop() = 0;
92
93  // Sets the relative volume, with range [0.0, 1.0] inclusive.
94  virtual void SetVolume(double volume) = 0;
95
96  // Gets the relative volume, with range [0.0, 1.0] inclusive.
97  virtual void GetVolume(double* volume) = 0;
98
99  // Close the stream. This also generates AudioSourceCallback::OnClose().
100  // After calling this method, the object should not be used anymore.
101  virtual void Close() = 0;
102};
103
104// Models an audio sink receiving recorded audio from the audio driver.
105class MEDIA_EXPORT AudioInputStream {
106 public:
107  class MEDIA_EXPORT AudioInputCallback {
108   public:
109    // Called by the audio recorder when a full packet of audio data is
110    // available. This is called from a special audio thread and the
111    // implementation should return as soon as possible.
112    virtual void OnData(AudioInputStream* stream, const uint8* src,
113                        uint32 size, uint32 hardware_delay_bytes,
114                        double volume) = 0;
115
116    // There was an error while recording audio. The audio sink cannot be
117    // destroyed yet. No direct action needed by the AudioInputStream, but it
118    // is a good place to stop accumulating sound data since is is likely that
119    // recording will not continue.
120    virtual void OnError(AudioInputStream* stream) = 0;
121
122   protected:
123    virtual ~AudioInputCallback() {}
124  };
125
126  virtual ~AudioInputStream() {}
127
128  // Open the stream and prepares it for recording. Call Start() to actually
129  // begin recording.
130  virtual bool Open() = 0;
131
132  // Starts recording audio and generating AudioInputCallback::OnData().
133  // The input stream does not take ownership of this callback.
134  virtual void Start(AudioInputCallback* callback) = 0;
135
136  // Stops recording audio. Effect might not be instantaneous as there could be
137  // pending audio callbacks in the queue which will be issued first before
138  // recording stops.
139  virtual void Stop() = 0;
140
141  // Close the stream. This also generates AudioInputCallback::OnClose(). This
142  // should be the last call made on this object.
143  virtual void Close() = 0;
144
145  // Returns the maximum microphone analog volume or 0.0 if device does not
146  // have volume control.
147  virtual double GetMaxVolume() = 0;
148
149  // Sets the microphone analog volume, with range [0, max_volume] inclusive.
150  virtual void SetVolume(double volume) = 0;
151
152  // Returns the microphone analog volume, with range [0, max_volume] inclusive.
153  virtual double GetVolume() = 0;
154
155  // Sets the Automatic Gain Control (AGC) state.
156  virtual void SetAutomaticGainControl(bool enabled) = 0;
157
158  // Returns the Automatic Gain Control (AGC) state.
159  virtual bool GetAutomaticGainControl() = 0;
160};
161
162}  // namespace media
163
164#endif  // MEDIA_AUDIO_AUDIO_IO_H_
165