blob: aafc7369cc4039c3508cb1d820809082886fb236 [file] [log] [blame]
Iliyan Malchev4765c432012-06-11 14:36:16 -07001/* ALSAStreamOps.cpp
2 **
3 ** Copyright 2008-2009 Wind River Systems
4 ** Copyright (c) 2011, Code Aurora Forum. All rights reserved.
5 **
6 ** Licensed under the Apache License, Version 2.0 (the "License");
7 ** you may not use this file except in compliance with the License.
8 ** You may obtain a copy of the License at
9 **
10 ** http://www.apache.org/licenses/LICENSE-2.0
11 **
12 ** Unless required by applicable law or agreed to in writing, software
13 ** distributed under the License is distributed on an "AS IS" BASIS,
14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ** See the License for the specific language governing permissions and
16 ** limitations under the License.
17 */
18
19#include <errno.h>
20#include <stdarg.h>
21#include <sys/stat.h>
22#include <fcntl.h>
23#include <stdlib.h>
24#include <unistd.h>
25#include <dlfcn.h>
26
Ajay Dudani9746c472012-06-18 16:01:16 -070027#define LOG_TAG "ALSAStreamOps"
Iliyan Malchev4765c432012-06-11 14:36:16 -070028//#define LOG_NDEBUG 0
Ajay Dudani9746c472012-06-18 16:01:16 -070029#define LOG_NDDEBUG 0
Iliyan Malchev4765c432012-06-11 14:36:16 -070030#include <utils/Log.h>
31#include <utils/String8.h>
32
33#include <cutils/properties.h>
34#include <media/AudioRecord.h>
35#include <hardware_legacy/power.h>
SathishKumar Manib357a772012-09-25 23:28:29 -070036#include "AudioUtil.h"
Iliyan Malchev4765c432012-06-11 14:36:16 -070037#include "AudioHardwareALSA.h"
38
39namespace android_audio_legacy
40{
41
SathishKumar Manib357a772012-09-25 23:28:29 -070042// unused 'enumVal;' is to catch error at compile time if enumVal ever changes
43// or applied on a non-existent enum
44#define ENUM_TO_STRING(var, enumVal) {var = #enumVal; enumVal;}
45
Iliyan Malchev4765c432012-06-11 14:36:16 -070046// ----------------------------------------------------------------------------
47
48ALSAStreamOps::ALSAStreamOps(AudioHardwareALSA *parent, alsa_handle_t *handle) :
49 mParent(parent),
50 mHandle(handle)
51{
52}
53
54ALSAStreamOps::~ALSAStreamOps()
55{
56 Mutex::Autolock autoLock(mParent->mLock);
57
58 if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL)) ||
59 (!strcmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP))) {
60 if((mParent->mVoipStreamCount)) {
61 mParent->mVoipStreamCount--;
62 if(mParent->mVoipStreamCount > 0) {
Iliyan Malchev4113f342012-06-11 14:39:47 -070063 ALOGD("ALSAStreamOps::close() Ignore");
Iliyan Malchev4765c432012-06-11 14:36:16 -070064 return ;
65 }
66 }
67 mParent->mVoipStreamCount = 0;
Iliyan Malchev4765c432012-06-11 14:36:16 -070068 mParent->mVoipBitRate = 0;
69 }
70 close();
71
72 for(ALSAHandleList::iterator it = mParent->mDeviceList.begin();
73 it != mParent->mDeviceList.end(); ++it) {
74 if (mHandle == &(*it)) {
75 it->useCase[0] = 0;
76 mParent->mDeviceList.erase(it);
77 break;
78 }
79 }
80}
81
82// use emulated popcount optimization
83// http://www.df.lth.se/~john_e/gems/gem002d.html
84static inline uint32_t popCount(uint32_t u)
85{
86 u = ((u&0x55555555) + ((u>>1)&0x55555555));
87 u = ((u&0x33333333) + ((u>>2)&0x33333333));
88 u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
89 u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
90 u = ( u&0x0000ffff) + (u>>16);
91 return u;
92}
93
94status_t ALSAStreamOps::set(int *format,
95 uint32_t *channels,
96 uint32_t *rate,
97 uint32_t device)
98{
99 mDevices = device;
100 if (channels && *channels != 0) {
101 if (mHandle->channels != popCount(*channels))
102 return BAD_VALUE;
103 } else if (channels) {
104 *channels = 0;
105 if (mHandle->devices & AudioSystem::DEVICE_OUT_ALL) {
106 switch(mHandle->channels) {
SathishKumar Manibf1c8742012-09-25 23:34:51 -0700107 case 6:
108 case 5:
109 *channels |= audio_channel_out_mask_from_count(mHandle->channels);
110 break;
111 // Do not fall through
Iliyan Malchev4765c432012-06-11 14:36:16 -0700112 case 4:
113 *channels |= AudioSystem::CHANNEL_OUT_BACK_LEFT;
114 *channels |= AudioSystem::CHANNEL_OUT_BACK_RIGHT;
115 // Fall through...
116 default:
117 case 2:
118 *channels |= AudioSystem::CHANNEL_OUT_FRONT_RIGHT;
119 // Fall through...
120 case 1:
121 *channels |= AudioSystem::CHANNEL_OUT_FRONT_LEFT;
122 break;
123 }
124 } else {
125 switch(mHandle->channels) {
Ajay Dudani9746c472012-06-18 16:01:16 -0700126#ifdef QCOM_SSR_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700127 // For 5.1 recording
128 case 6 :
129 *channels |= AudioSystem::CHANNEL_IN_5POINT1;
130 break;
131#endif
132 // Do not fall through...
133 default:
134 case 2:
135 *channels |= AudioSystem::CHANNEL_IN_RIGHT;
136 // Fall through...
137 case 1:
138 *channels |= AudioSystem::CHANNEL_IN_LEFT;
139 break;
140 }
141 }
142 }
143
144 if (rate && *rate > 0) {
145 if (mHandle->sampleRate != *rate)
146 return BAD_VALUE;
147 } else if (rate) {
148 *rate = mHandle->sampleRate;
149 }
150
151 snd_pcm_format_t iformat = mHandle->format;
152
153 if (format) {
154 switch(*format) {
155 case AudioSystem::FORMAT_DEFAULT:
156 break;
157
158 case AudioSystem::PCM_16_BIT:
159 iformat = SNDRV_PCM_FORMAT_S16_LE;
160 break;
161 case AudioSystem::AMR_NB:
162 case AudioSystem::AMR_WB:
Ajay Dudani9746c472012-06-18 16:01:16 -0700163#ifdef QCOM_QCHAT_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700164 case AudioSystem::EVRC:
165 case AudioSystem::EVRCB:
166 case AudioSystem::EVRCWB:
167#endif
168 iformat = *format;
169 break;
170
171 case AudioSystem::PCM_8_BIT:
172 iformat = SNDRV_PCM_FORMAT_S8;
173 break;
174
175 default:
Iliyan Malchev4113f342012-06-11 14:39:47 -0700176 ALOGE("Unknown PCM format %i. Forcing default", *format);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700177 break;
178 }
179
180 if (mHandle->format != iformat)
181 return BAD_VALUE;
182
183 switch(iformat) {
184 case SNDRV_PCM_FORMAT_S16_LE:
185 *format = AudioSystem::PCM_16_BIT;
186 break;
187 case SNDRV_PCM_FORMAT_S8:
188 *format = AudioSystem::PCM_8_BIT;
189 break;
190 default:
191 break;
192 }
193 }
194
195 return NO_ERROR;
196}
197
198status_t ALSAStreamOps::setParameters(const String8& keyValuePairs)
199{
200 AudioParameter param = AudioParameter(keyValuePairs);
201 String8 key = String8(AudioParameter::keyRouting);
202 int device;
ty.lee74060de2012-08-02 00:47:00 +0900203
204#ifdef SEPERATED_AUDIO_INPUT
205 String8 key_input = String8(AudioParameter::keyInputSource);
206 int source;
207
208 if (param.getInt(key_input, source) == NO_ERROR) {
209 ALOGD("setParameters(), input_source = %d", source);
210 mParent->mALSADevice->setInput(source);
211 param.remove(key_input);
212 }
213#endif
214
Iliyan Malchev4765c432012-06-11 14:36:16 -0700215 if (param.getInt(key, device) == NO_ERROR) {
216 // Ignore routing if device is 0.
Iliyan Malchev4113f342012-06-11 14:39:47 -0700217 ALOGD("setParameters(): keyRouting with device %d", device);
Eric Laurent466e8a82012-09-26 18:40:35 -0700218 // reset to speaker when disconnecting HDMI to avoid timeout due to write errors
219 if ((device == 0) && (mDevices == AudioSystem::DEVICE_OUT_AUX_DIGITAL)) {
220 device = AudioSystem::DEVICE_OUT_SPEAKER;
221 }
Iliyan Malchev4765c432012-06-11 14:36:16 -0700222 mDevices = device;
223 if(device) {
224 mParent->doRouting(device);
225 }
226 param.remove(key);
227 }
Ajay Dudani9746c472012-06-18 16:01:16 -0700228#ifdef QCOM_FM_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700229 else {
230 key = String8(AudioParameter::keyHandleFm);
231 if (param.getInt(key, device) == NO_ERROR) {
Iliyan Malchev4113f342012-06-11 14:39:47 -0700232 ALOGD("setParameters(): handleFm with device %d", device);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700233 mDevices = device;
234 if(device) {
235 mParent->handleFm(device);
236 }
237 param.remove(key);
238 }
239 }
240#endif
241
242 return NO_ERROR;
243}
244
245String8 ALSAStreamOps::getParameters(const String8& keys)
246{
247 AudioParameter param = AudioParameter(keys);
248 String8 value;
249 String8 key = String8(AudioParameter::keyRouting);
250
251 if (param.get(key, value) == NO_ERROR) {
252 param.addInt(key, (int)mDevices);
253 }
254 else {
Ajay Dudani9746c472012-06-18 16:01:16 -0700255#ifdef QCOM_VOIP_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700256 key = String8(AudioParameter::keyVoipCheck);
257 if (param.get(key, value) == NO_ERROR) {
258 if((!strncmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, strlen(SND_USE_CASE_VERB_IP_VOICECALL))) ||
259 (!strncmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP, strlen(SND_USE_CASE_MOD_PLAY_VOIP))))
260 param.addInt(key, true);
261 else
262 param.addInt(key, false);
263 }
Iliyan Malchev4113f342012-06-11 14:39:47 -0700264#endif
Iliyan Malchev4765c432012-06-11 14:36:16 -0700265 }
SathishKumar Manib357a772012-09-25 23:28:29 -0700266 key = String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS);
267 if (param.get(key, value) == NO_ERROR) {
268 EDID_AUDIO_INFO info = { 0 };
269 bool first = true;
270 value = String8();
271 if (AudioUtil::getHDMIAudioSinkCaps(&info)) {
272 for (int i = 0; i < info.nAudioBlocks && i < MAX_EDID_BLOCKS; i++) {
273 String8 append;
274 switch (info.AudioBlocksArray[i].nChannels) {
275 //Do not handle stereo output in Multi-channel cases
276 //Stereo case is handled in normal playback path
277 case 6:
278 ENUM_TO_STRING(append, AUDIO_CHANNEL_OUT_5POINT1);
279 break;
280 case 8:
281 ENUM_TO_STRING(append, AUDIO_CHANNEL_OUT_7POINT1);
282 break;
283 default:
284 ALOGD("Unsupported number of channels %d", info.AudioBlocksArray[i].nChannels);
285 break;
286 }
287 if (!append.isEmpty()) {
288 value += (first ? append : String8("|") + append);
289 first = false;
290 }
291 }
292 } else {
293 ALOGE("Failed to get HDMI sink capabilities");
294 }
295 param.add(key, value);
296 }
Iliyan Malchev4113f342012-06-11 14:39:47 -0700297 ALOGV("getParameters() %s", param.toString().string());
Iliyan Malchev4765c432012-06-11 14:36:16 -0700298 return param.toString();
299}
300
301uint32_t ALSAStreamOps::sampleRate() const
302{
303 return mHandle->sampleRate;
304}
305
306//
307// Return the number of bytes (not frames)
308//
309size_t ALSAStreamOps::bufferSize() const
310{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700311 ALOGV("bufferSize() returns %d", mHandle->bufferSize);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700312 return mHandle->bufferSize;
313}
314
315int ALSAStreamOps::format() const
316{
317 int audioSystemFormat;
318
319 snd_pcm_format_t ALSAFormat = mHandle->format;
320
321 switch(ALSAFormat) {
322 case SNDRV_PCM_FORMAT_S8:
323 audioSystemFormat = AudioSystem::PCM_8_BIT;
324 break;
325
326 case AudioSystem::AMR_NB:
327 case AudioSystem::AMR_WB:
Ajay Dudani9746c472012-06-18 16:01:16 -0700328#ifdef QCOM_QCHAT_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700329 case AudioSystem::EVRC:
330 case AudioSystem::EVRCB:
331 case AudioSystem::EVRCWB:
332#endif
333 audioSystemFormat = mHandle->format;
334 break;
335 case SNDRV_PCM_FORMAT_S16_LE:
336 audioSystemFormat = AudioSystem::PCM_16_BIT;
337 break;
338
339 default:
340 LOG_FATAL("Unknown AudioSystem bit width %d!", audioSystemFormat);
341 audioSystemFormat = AudioSystem::PCM_16_BIT;
342 break;
343 }
344
SathishKumar Mani9efed762012-09-18 18:52:48 -0700345 ALOGV("ALSAFormat:0x%x,audioSystemFormat:0x%x",ALSAFormat,audioSystemFormat);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700346 return audioSystemFormat;
347}
348
349uint32_t ALSAStreamOps::channels() const
350{
351 unsigned int count = mHandle->channels;
352 uint32_t channels = 0;
353
354 if (mDevices & AudioSystem::DEVICE_OUT_ALL)
355 switch(count) {
SathishKumar Manibf1c8742012-09-25 23:34:51 -0700356 case 6:
357 case 5:
358 channels |=audio_channel_out_mask_from_count(count);
359 break;
360 // Do not fall through
Iliyan Malchev4765c432012-06-11 14:36:16 -0700361 case 4:
362 channels |= AudioSystem::CHANNEL_OUT_BACK_LEFT;
363 channels |= AudioSystem::CHANNEL_OUT_BACK_RIGHT;
364 // Fall through...
365 default:
366 case 2:
367 channels |= AudioSystem::CHANNEL_OUT_FRONT_RIGHT;
368 // Fall through...
369 case 1:
370 channels |= AudioSystem::CHANNEL_OUT_FRONT_LEFT;
371 break;
372 }
373 else
374 switch(count) {
Ajay Dudani9746c472012-06-18 16:01:16 -0700375#ifdef QCOM_SSR_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700376 // For 5.1 recording
377 case 6 :
378 channels |= AudioSystem::CHANNEL_IN_5POINT1;
379 break;
380 // Do not fall through...
381#endif
382 default:
383 case 2:
384 channels |= AudioSystem::CHANNEL_IN_RIGHT;
385 // Fall through...
386 case 1:
387 channels |= AudioSystem::CHANNEL_IN_LEFT;
388 break;
389 }
390
391 return channels;
392}
393
394void ALSAStreamOps::close()
395{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700396 ALOGD("close");
Iliyan Malchev4765c432012-06-11 14:36:16 -0700397 if((!strncmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, strlen(SND_USE_CASE_VERB_IP_VOICECALL))) ||
398 (!strncmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP, strlen(SND_USE_CASE_MOD_PLAY_VOIP)))) {
Iliyan Malchev4765c432012-06-11 14:36:16 -0700399 mParent->mVoipBitRate = 0;
400 mParent->mVoipStreamCount = 0;
401 }
402 mParent->mALSADevice->close(mHandle);
403}
404
405//
406// Set playback or capture PCM device. It's possible to support audio output
407// or input from multiple devices by using the ALSA plugins, but this is
408// not supported for simplicity.
409//
410// The AudioHardwareALSA API does not allow one to set the input routing.
411//
412// If the "routes" value does not map to a valid device, the default playback
413// device is used.
414//
415status_t ALSAStreamOps::open(int mode)
416{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700417 ALOGD("open");
Iliyan Malchev4765c432012-06-11 14:36:16 -0700418 return mParent->mALSADevice->open(mHandle);
419}
420
421} // namespace androidi_audio_legacy