3 ** Copyright 2008-2010 Wind River Systems
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
26 #define LOG_TAG "AudioHardwareALSA"
27 #include <utils/Log.h>
28 #include <utils/String8.h>
30 #include <cutils/properties.h>
31 #include <media/AudioRecord.h>
32 #include <hardware_legacy/power.h>
34 #include "AudioHardwareALSA.h"
36 #define SND_MIXER_VOL_RANGE_MIN (0)
37 #define SND_MIXER_VOL_RANGE_MAX (100)
39 #define ALSA_NAME_MAX 128
41 #define ALSA_STRCAT(x,y) \
42 if (strlen(x) + strlen(y) < ALSA_NAME_MAX) \
48 // ----------------------------------------------------------------------------
52 struct alsa_properties_t
54 const AudioSystem::audio_devices device;
56 const char *propDefault;
60 #define ALSA_PROP(dev, name, out, in) \
62 {dev, "alsa.mixer.playback." name, out, NULL},\
63 {dev, "alsa.mixer.capture." name, in, NULL}\
66 static alsa_properties_t
67 mixerMasterProp[SND_PCM_STREAM_LAST+1] =
68 ALSA_PROP(AudioSystem::DEVICE_OUT_ALL, "master", "PCM", "Capture");
70 static alsa_properties_t
71 mixerProp[][SND_PCM_STREAM_LAST+1] = {
72 ALSA_PROP(AudioSystem::DEVICE_OUT_EARPIECE, "earpiece", "Earpiece", "Capture"),
73 ALSA_PROP(AudioSystem::DEVICE_OUT_SPEAKER, "speaker", "Speaker", ""),
74 ALSA_PROP(AudioSystem::DEVICE_OUT_WIRED_HEADSET, "headset", "Headphone", "Capture"),
75 ALSA_PROP(AudioSystem::DEVICE_OUT_BLUETOOTH_SCO, "bluetooth.sco", "Bluetooth", "Bluetooth Capture"),
76 ALSA_PROP(AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP, "bluetooth.a2dp", "Bluetooth A2DP", "Bluetooth A2DP Capture"),
77 ALSA_PROP(static_cast<AudioSystem::audio_devices>(0), "", NULL, NULL)
84 min(SND_MIXER_VOL_RANGE_MIN),
85 max(SND_MIXER_VOL_RANGE_MAX),
90 snd_mixer_elem_t *elem;
95 char name[ALSA_NAME_MAX];
98 static int initMixer (snd_mixer_t **mixer, const char *name)
102 if ((err = snd_mixer_open(mixer, 0)) < 0) {
103 LOGE("Unable to open mixer: %s", snd_strerror(err));
107 if ((err = snd_mixer_attach(*mixer, name)) < 0) {
108 LOGW("Unable to attach mixer to device %s: %s",
109 name, snd_strerror(err));
111 if ((err = snd_mixer_attach(*mixer, "hw:00")) < 0) {
112 LOGE("Unable to attach mixer to device default: %s",
115 snd_mixer_close (*mixer);
121 if ((err = snd_mixer_selem_register(*mixer, NULL, NULL)) < 0) {
122 LOGE("Unable to register mixer elements: %s", snd_strerror(err));
123 snd_mixer_close (*mixer);
128 // Get the mixer controls from the kernel
129 if ((err = snd_mixer_load(*mixer)) < 0) {
130 LOGE("Unable to load mixer elements: %s", snd_strerror(err));
131 snd_mixer_close (*mixer);
139 typedef int (*hasVolume_t)(snd_mixer_elem_t*);
141 static const hasVolume_t hasVolume[] = {
142 snd_mixer_selem_has_playback_volume,
143 snd_mixer_selem_has_capture_volume
146 typedef int (*getVolumeRange_t)(snd_mixer_elem_t*, long int*, long int*);
148 static const getVolumeRange_t getVolumeRange[] = {
149 snd_mixer_selem_get_playback_volume_range,
150 snd_mixer_selem_get_capture_volume_range
153 typedef int (*setVolume_t)(snd_mixer_elem_t*, long int);
155 static const setVolume_t setVol[] = {
156 snd_mixer_selem_set_playback_volume_all,
157 snd_mixer_selem_set_capture_volume_all
160 ALSAMixer::ALSAMixer()
164 initMixer (&mMixer[SND_PCM_STREAM_PLAYBACK], "AndroidOut");
165 initMixer (&mMixer[SND_PCM_STREAM_CAPTURE], "AndroidIn");
167 snd_mixer_selem_id_t *sid;
168 snd_mixer_selem_id_alloca(&sid);
170 for (int i = 0; i <= SND_PCM_STREAM_LAST; i++) {
172 if (!mMixer[i]) continue;
174 mixer_info_t *info = mixerMasterProp[i].mInfo = new mixer_info_t;
176 property_get (mixerMasterProp[i].propName,
178 mixerMasterProp[i].propDefault);
180 for (snd_mixer_elem_t *elem = snd_mixer_first_elem(mMixer[i]);
182 elem = snd_mixer_elem_next(elem)) {
184 if (!snd_mixer_selem_is_active(elem))
187 snd_mixer_selem_get_id(elem, sid);
189 // Find PCM playback volume control element.
190 const char *elementName = snd_mixer_selem_id_get_name(sid);
192 if (info->elem == NULL &&
193 strcmp(elementName, info->name) == 0 &&
194 hasVolume[i] (elem)) {
197 getVolumeRange[i] (elem, &info->min, &info->max);
198 info->volume = info->max;
199 setVol[i] (elem, info->volume);
200 if (i == SND_PCM_STREAM_PLAYBACK &&
201 snd_mixer_selem_has_playback_switch (elem))
202 snd_mixer_selem_set_playback_switch_all (elem, 1);
207 LOGV("Mixer: master '%s' %s.", info->name, info->elem ? "found" : "not found");
209 for (int j = 0; mixerProp[j][i].device; j++) {
211 mixer_info_t *info = mixerProp[j][i].mInfo = new mixer_info_t;
213 property_get (mixerProp[j][i].propName,
215 mixerProp[j][i].propDefault);
217 for (snd_mixer_elem_t *elem = snd_mixer_first_elem(mMixer[i]);
219 elem = snd_mixer_elem_next(elem)) {
221 if (!snd_mixer_selem_is_active(elem))
224 snd_mixer_selem_get_id(elem, sid);
226 // Find PCM playback volume control element.
227 const char *elementName = snd_mixer_selem_id_get_name(sid);
229 if (info->elem == NULL &&
230 strcmp(elementName, info->name) == 0 &&
231 hasVolume[i] (elem)) {
234 getVolumeRange[i] (elem, &info->min, &info->max);
235 info->volume = info->max;
236 setVol[i] (elem, info->volume);
237 if (i == SND_PCM_STREAM_PLAYBACK &&
238 snd_mixer_selem_has_playback_switch (elem))
239 snd_mixer_selem_set_playback_switch_all (elem, 1);
243 LOGV("Mixer: route '%s' %s.", info->name, info->elem ? "found" : "not found");
246 LOGV("mixer initialized.");
249 ALSAMixer::~ALSAMixer()
251 for (int i = 0; i <= SND_PCM_STREAM_LAST; i++) {
252 if (mMixer[i]) snd_mixer_close (mMixer[i]);
253 if (mixerMasterProp[i].mInfo) {
254 delete mixerMasterProp[i].mInfo;
255 mixerMasterProp[i].mInfo = NULL;
257 for (int j = 0; mixerProp[j][i].device; j++) {
258 if (mixerProp[j][i].mInfo) {
259 delete mixerProp[j][i].mInfo;
260 mixerProp[j][i].mInfo = NULL;
264 LOGV("mixer destroyed.");
267 status_t ALSAMixer::setMasterVolume(float volume)
269 mixer_info_t *info = mixerMasterProp[SND_PCM_STREAM_PLAYBACK].mInfo;
270 if (!info || !info->elem) return INVALID_OPERATION;
272 long minVol = info->min;
273 long maxVol = info->max;
275 // Make sure volume is between bounds.
276 long vol = minVol + volume * (maxVol - minVol);
277 if (vol > maxVol) vol = maxVol;
278 if (vol < minVol) vol = minVol;
281 snd_mixer_selem_set_playback_volume_all (info->elem, vol);
286 status_t ALSAMixer::setMasterGain(float gain)
288 mixer_info_t *info = mixerMasterProp[SND_PCM_STREAM_CAPTURE].mInfo;
289 if (!info || !info->elem) return INVALID_OPERATION;
291 long minVol = info->min;
292 long maxVol = info->max;
294 // Make sure volume is between bounds.
295 long vol = minVol + gain * (maxVol - minVol);
296 if (vol > maxVol) vol = maxVol;
297 if (vol < minVol) vol = minVol;
300 snd_mixer_selem_set_capture_volume_all (info->elem, vol);
305 status_t ALSAMixer::setVolume(uint32_t device, float left, float right)
307 for (int j = 0; mixerProp[j][SND_PCM_STREAM_PLAYBACK].device; j++)
308 if (mixerProp[j][SND_PCM_STREAM_PLAYBACK].device & device) {
310 mixer_info_t *info = mixerProp[j][SND_PCM_STREAM_PLAYBACK].mInfo;
311 if (!info || !info->elem) return INVALID_OPERATION;
313 long minVol = info->min;
314 long maxVol = info->max;
316 // Make sure volume is between bounds.
317 long vol = minVol + left * (maxVol - minVol);
318 if (vol > maxVol) vol = maxVol;
319 if (vol < minVol) vol = minVol;
322 snd_mixer_selem_set_playback_volume_all (info->elem, vol);
328 status_t ALSAMixer::setGain(uint32_t device, float gain)
330 for (int j = 0; mixerProp[j][SND_PCM_STREAM_CAPTURE].device; j++)
331 if (mixerProp[j][SND_PCM_STREAM_CAPTURE].device & device) {
333 mixer_info_t *info = mixerProp[j][SND_PCM_STREAM_CAPTURE].mInfo;
334 if (!info || !info->elem) return INVALID_OPERATION;
336 long minVol = info->min;
337 long maxVol = info->max;
339 // Make sure volume is between bounds.
340 long vol = minVol + gain * (maxVol - minVol);
341 if (vol > maxVol) vol = maxVol;
342 if (vol < minVol) vol = minVol;
345 snd_mixer_selem_set_capture_volume_all (info->elem, vol);
351 status_t ALSAMixer::setCaptureMuteState(uint32_t device, bool state)
353 for (int j = 0; mixerProp[j][SND_PCM_STREAM_CAPTURE].device; j++)
354 if (mixerProp[j][SND_PCM_STREAM_CAPTURE].device & device) {
356 mixer_info_t *info = mixerProp[j][SND_PCM_STREAM_CAPTURE].mInfo;
357 if (!info || !info->elem) return INVALID_OPERATION;
359 if (snd_mixer_selem_has_capture_switch (info->elem)) {
361 int err = snd_mixer_selem_set_capture_switch_all (info->elem, static_cast<int>(!state));
363 LOGE("Unable to %s capture mixer switch %s",
364 state ? "enable" : "disable", info->name);
365 return INVALID_OPERATION;
375 status_t ALSAMixer::getCaptureMuteState(uint32_t device, bool *state)
377 if (!state) return BAD_VALUE;
379 for (int j = 0; mixerProp[j][SND_PCM_STREAM_CAPTURE].device; j++)
380 if (mixerProp[j][SND_PCM_STREAM_CAPTURE].device & device) {
382 mixer_info_t *info = mixerProp[j][SND_PCM_STREAM_CAPTURE].mInfo;
383 if (!info || !info->elem) return INVALID_OPERATION;
392 status_t ALSAMixer::setPlaybackMuteState(uint32_t device, bool state)
394 for (int j = 0; mixerProp[j][SND_PCM_STREAM_PLAYBACK].device; j++)
395 if (mixerProp[j][SND_PCM_STREAM_PLAYBACK].device & device) {
397 mixer_info_t *info = mixerProp[j][SND_PCM_STREAM_PLAYBACK].mInfo;
398 if (!info || !info->elem) return INVALID_OPERATION;
400 if (snd_mixer_selem_has_playback_switch (info->elem)) {
402 int err = snd_mixer_selem_set_playback_switch_all (info->elem, static_cast<int>(!state));
404 LOGE("Unable to %s playback mixer switch %s",
405 state ? "enable" : "disable", info->name);
406 return INVALID_OPERATION;
416 status_t ALSAMixer::getPlaybackMuteState(uint32_t device, bool *state)
418 if (!state) return BAD_VALUE;
420 for (int j = 0; mixerProp[j][SND_PCM_STREAM_PLAYBACK].device; j++)
421 if (mixerProp[j][SND_PCM_STREAM_PLAYBACK].device & device) {
423 mixer_info_t *info = mixerProp[j][SND_PCM_STREAM_PLAYBACK].mInfo;
424 if (!info || !info->elem) return INVALID_OPERATION;
433 }; // namespace android