OSDN Git Service

0568a1eb69d5b9b72611a8c7e370e285eb0157f6
[android-x86/hardware-alsa_sound.git] / AudioHardwareALSA.h
1 /* AudioHardwareALSA.h
2  **
3  ** Copyright 2008, Wind River Systems
4  **
5  ** Licensed under the Apache License, Version 2.0 (the "License");
6  ** you may not use this file except in compliance with the License.
7  ** You may obtain a copy of the License at
8  **
9  **     http://www.apache.org/licenses/LICENSE-2.0
10  **
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  */
17
18 #ifndef ANDROID_AUDIO_HARDWARE_ALSA_H
19 #define ANDROID_AUDIO_HARDWARE_ALSA_H
20
21 #include <stdint.h>
22 #include <sys/types.h>
23 #include <alsa/asoundlib.h>
24
25 #include <hardware_legacy/AudioHardwareBase.h>
26
27 namespace android
28 {
29
30     class AudioHardwareALSA;
31
32     // ----------------------------------------------------------------------------
33
34     class ALSAMixer
35     {
36         public:
37                                     ALSAMixer();
38             virtual                ~ALSAMixer();
39
40             bool                    isValid() { return !!mMixer[SND_PCM_STREAM_PLAYBACK]; }
41             status_t                setMasterVolume(float volume);
42             status_t                setMasterGain(float gain);
43
44             status_t                setVolume(uint32_t device, float volume);
45             status_t                setGain(uint32_t device, float gain);
46
47             status_t                setCaptureMuteState(uint32_t device, bool state);
48             status_t                getCaptureMuteState(uint32_t device, bool *state);
49             status_t                setPlaybackMuteState(uint32_t device, bool state);
50             status_t                getPlaybackMuteState(uint32_t device, bool *state);
51
52         private:
53             snd_mixer_t            *mMixer[SND_PCM_STREAM_LAST+1];
54     };
55
56     class ALSAControl
57     {
58         public:
59                                     ALSAControl(const char *device = "default");
60             virtual                ~ALSAControl();
61
62             status_t                get(const char *name, unsigned int &value, int index = 0);
63             status_t                set(const char *name, unsigned int value, int index = -1);
64
65         private:
66             snd_ctl_t              *mHandle;
67     };
68
69     class ALSAStreamOps
70     {
71         public:
72             struct StreamDefaults
73             {
74                 const char *        devicePrefix;
75                 snd_pcm_stream_t    direction;       // playback or capture
76                 snd_pcm_format_t    format;
77                 int                 channels;
78                 uint32_t            sampleRate;
79                 unsigned int        latency;         // Delay in usec
80                 unsigned int        bufferSize;      // Size of sample buffer
81             };
82
83                                     ALSAStreamOps();
84             virtual                ~ALSAStreamOps();
85
86             status_t                set(int format,
87                                         int channels,
88                                         uint32_t rate);
89             virtual uint32_t        sampleRate() const;
90             status_t                sampleRate(uint32_t rate);
91             virtual size_t          bufferSize() const;
92             virtual int             format() const;
93             virtual int             channelCount() const;
94             status_t                channelCount(int channels);
95             const char             *streamName();
96             virtual status_t        setDevice(int mode, uint32_t device);
97
98             const char             *deviceName(int mode, uint32_t device);
99
100         protected:
101             friend class AudioStreamOutALSA;
102             friend class AudioStreamInALSA;
103
104             status_t                open(int mode, uint32_t device);
105             void                    close();
106             status_t                setSoftwareParams();
107             status_t                setPCMFormat(snd_pcm_format_t format);
108             status_t                setHardwareResample(bool resample);
109
110             void                    setStreamDefaults(StreamDefaults *dev) {
111                 mDefaults = dev;
112             }
113
114             Mutex                   mLock;
115
116         private:
117             snd_pcm_t              *mHandle;
118             snd_pcm_hw_params_t    *mHardwareParams;
119             snd_pcm_sw_params_t    *mSoftwareParams;
120             int                     mMode;
121             uint32_t                mDevice;
122
123             StreamDefaults         *mDefaults;
124     };
125
126     // ----------------------------------------------------------------------------
127
128     class AudioStreamOutALSA : public AudioStreamOut, public ALSAStreamOps
129     {
130         public:
131                                     AudioStreamOutALSA(AudioHardwareALSA *parent);
132             virtual                ~AudioStreamOutALSA();
133
134             status_t                set(int format          = 0,
135                                         int channelCount    = 0,
136                                         uint32_t sampleRate = 0) {
137                 return ALSAStreamOps::set(format, channelCount, sampleRate);
138             }
139
140             virtual uint32_t        sampleRate() const
141             {
142                 return ALSAStreamOps::sampleRate();
143             }
144
145             virtual size_t          bufferSize() const
146             {
147                 return ALSAStreamOps::bufferSize();
148             }
149
150             virtual int             channelCount() const;
151
152             virtual int             format() const
153             {
154                 return ALSAStreamOps::format();
155             }
156
157             virtual uint32_t        latency() const;
158
159             virtual ssize_t         write(const void *buffer, size_t bytes);
160             virtual status_t        dump(int fd, const Vector<String16>& args);
161             virtual status_t        setDevice(int mode, uint32_t newDevice);
162
163             status_t                setVolume(float volume);
164
165             status_t                standby();
166             bool                    isStandby();
167
168         private:
169             AudioHardwareALSA      *mParent;
170             bool                    mPowerLock;
171     };
172
173     class AudioStreamInALSA : public AudioStreamIn, public ALSAStreamOps
174     {
175         public:
176                                     AudioStreamInALSA(AudioHardwareALSA *parent);
177             virtual                ~AudioStreamInALSA();
178
179             status_t                set(int      format       = 0,
180                                         int      channelCount = 0,
181                                         uint32_t sampleRate   = 0) {
182                 return ALSAStreamOps::set(format, channelCount, sampleRate);
183             }
184
185             virtual uint32_t        sampleRate() {
186                 return ALSAStreamOps::sampleRate();
187             }
188
189             virtual size_t          bufferSize() const
190             {
191                 return ALSAStreamOps::bufferSize();
192             }
193
194             virtual int             channelCount() const
195             {
196                 return ALSAStreamOps::channelCount();
197             }
198
199             virtual int             format() const
200             {
201                 return ALSAStreamOps::format();
202             }
203
204             virtual ssize_t         read(void* buffer, ssize_t bytes);
205             virtual status_t        dump(int fd, const Vector<String16>& args);
206             virtual status_t        setDevice(int mode, uint32_t newDevice);
207
208             virtual status_t        setGain(float gain);
209
210             virtual status_t        standby();
211
212         private:
213             AudioHardwareALSA *mParent;
214     };
215
216     class AudioHardwareALSA : public AudioHardwareBase
217     {
218         public:
219                                     AudioHardwareALSA();
220             virtual                ~AudioHardwareALSA();
221
222             /**
223              * check to see if the audio hardware interface has been initialized.
224              * return status based on values defined in include/utils/Errors.h
225              */
226             virtual status_t        initCheck();
227
228             /**
229              * put the audio hardware into standby mode to conserve power. Returns
230              * status based on include/utils/Errors.h
231              */
232             virtual status_t        standby();
233
234             /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
235             virtual status_t        setVoiceVolume(float volume);
236
237             /**
238              * set the audio volume for all audio activities other than voice call.
239              * Range between 0.0 and 1.0. If any value other than NO_ERROR is returned,
240              * the software mixer will emulate this capability.
241              */
242             virtual status_t        setMasterVolume(float volume);
243
244             // mic mute
245             virtual status_t        setMicMute(bool state);
246             virtual status_t        getMicMute(bool* state);
247
248             /** This method creates and opens the audio hardware output stream */
249             virtual AudioStreamOut* openOutputStream(
250                 int format=0,
251                 int channelCount=0,
252                 uint32_t sampleRate=0,
253                 status_t *status=0);
254
255             /** This method creates and opens the audio hardware input stream */
256             virtual AudioStreamIn*  openInputStream(
257                 int format,
258                 int channelCount,
259                 uint32_t sampleRate,
260                 status_t *status,
261                 AudioSystem::audio_in_acoustics acoustics);
262
263         protected:
264             /**
265              * doRouting actually initiates the routing. A call to setRouting
266              * or setMode may result in a routing change. The generic logic calls
267              * doRouting when required. If the device has any special requirements these
268              * methods can be overriden.
269              */
270             virtual status_t    doRouting();
271
272             virtual status_t    dump(int fd, const Vector<String16>& args);
273
274             friend class AudioStreamOutALSA;
275             friend class AudioStreamInALSA;
276
277             ALSAMixer          *mMixer;
278             AudioStreamOutALSA *mOutput;
279             AudioStreamInALSA  *mInput;
280
281         private:
282             Mutex               mLock;
283     };
284
285     // ----------------------------------------------------------------------------
286
287 };        // namespace android
288 #endif    // ANDROID_AUDIO_HARDWARE_ALSA_H