blob: 18ca7e636a2a3e44f2cc3b99244fa28f57085f9e [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>
36
37#include "AudioHardwareALSA.h"
38
39namespace android_audio_legacy
40{
41
42// ----------------------------------------------------------------------------
43
44ALSAStreamOps::ALSAStreamOps(AudioHardwareALSA *parent, alsa_handle_t *handle) :
45 mParent(parent),
46 mHandle(handle)
47{
48}
49
50ALSAStreamOps::~ALSAStreamOps()
51{
52 Mutex::Autolock autoLock(mParent->mLock);
53
54 if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL)) ||
55 (!strcmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP))) {
56 if((mParent->mVoipStreamCount)) {
57 mParent->mVoipStreamCount--;
58 if(mParent->mVoipStreamCount > 0) {
Iliyan Malchev4113f342012-06-11 14:39:47 -070059 ALOGD("ALSAStreamOps::close() Ignore");
Iliyan Malchev4765c432012-06-11 14:36:16 -070060 return ;
61 }
62 }
63 mParent->mVoipStreamCount = 0;
Iliyan Malchev80baa422012-09-27 21:29:09 -070064 mParent->mVoipMicMute = 0;
Iliyan Malchev4765c432012-06-11 14:36:16 -070065 mParent->mVoipBitRate = 0;
66 }
67 close();
68
69 for(ALSAHandleList::iterator it = mParent->mDeviceList.begin();
70 it != mParent->mDeviceList.end(); ++it) {
71 if (mHandle == &(*it)) {
72 it->useCase[0] = 0;
73 mParent->mDeviceList.erase(it);
74 break;
75 }
76 }
77}
78
79// use emulated popcount optimization
80// http://www.df.lth.se/~john_e/gems/gem002d.html
81static inline uint32_t popCount(uint32_t u)
82{
83 u = ((u&0x55555555) + ((u>>1)&0x55555555));
84 u = ((u&0x33333333) + ((u>>2)&0x33333333));
85 u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
86 u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
87 u = ( u&0x0000ffff) + (u>>16);
88 return u;
89}
90
91status_t ALSAStreamOps::set(int *format,
92 uint32_t *channels,
93 uint32_t *rate,
94 uint32_t device)
95{
96 mDevices = device;
97 if (channels && *channels != 0) {
98 if (mHandle->channels != popCount(*channels))
99 return BAD_VALUE;
100 } else if (channels) {
101 *channels = 0;
102 if (mHandle->devices & AudioSystem::DEVICE_OUT_ALL) {
103 switch(mHandle->channels) {
104 case 4:
105 *channels |= AudioSystem::CHANNEL_OUT_BACK_LEFT;
106 *channels |= AudioSystem::CHANNEL_OUT_BACK_RIGHT;
107 // Fall through...
108 default:
109 case 2:
110 *channels |= AudioSystem::CHANNEL_OUT_FRONT_RIGHT;
111 // Fall through...
112 case 1:
113 *channels |= AudioSystem::CHANNEL_OUT_FRONT_LEFT;
114 break;
115 }
116 } else {
117 switch(mHandle->channels) {
Ajay Dudani9746c472012-06-18 16:01:16 -0700118#ifdef QCOM_SSR_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700119 // For 5.1 recording
120 case 6 :
121 *channels |= AudioSystem::CHANNEL_IN_5POINT1;
122 break;
123#endif
124 // Do not fall through...
125 default:
126 case 2:
127 *channels |= AudioSystem::CHANNEL_IN_RIGHT;
128 // Fall through...
129 case 1:
130 *channels |= AudioSystem::CHANNEL_IN_LEFT;
131 break;
132 }
133 }
134 }
135
136 if (rate && *rate > 0) {
137 if (mHandle->sampleRate != *rate)
138 return BAD_VALUE;
139 } else if (rate) {
140 *rate = mHandle->sampleRate;
141 }
142
143 snd_pcm_format_t iformat = mHandle->format;
144
145 if (format) {
146 switch(*format) {
147 case AudioSystem::FORMAT_DEFAULT:
148 break;
149
150 case AudioSystem::PCM_16_BIT:
151 iformat = SNDRV_PCM_FORMAT_S16_LE;
152 break;
153 case AudioSystem::AMR_NB:
154 case AudioSystem::AMR_WB:
Ajay Dudani9746c472012-06-18 16:01:16 -0700155#ifdef QCOM_QCHAT_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700156 case AudioSystem::EVRC:
157 case AudioSystem::EVRCB:
158 case AudioSystem::EVRCWB:
159#endif
160 iformat = *format;
161 break;
162
163 case AudioSystem::PCM_8_BIT:
164 iformat = SNDRV_PCM_FORMAT_S8;
165 break;
166
167 default:
Iliyan Malchev4113f342012-06-11 14:39:47 -0700168 ALOGE("Unknown PCM format %i. Forcing default", *format);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700169 break;
170 }
171
172 if (mHandle->format != iformat)
173 return BAD_VALUE;
174
175 switch(iformat) {
176 case SNDRV_PCM_FORMAT_S16_LE:
177 *format = AudioSystem::PCM_16_BIT;
178 break;
179 case SNDRV_PCM_FORMAT_S8:
180 *format = AudioSystem::PCM_8_BIT;
181 break;
182 default:
183 break;
184 }
185 }
186
187 return NO_ERROR;
188}
189
190status_t ALSAStreamOps::setParameters(const String8& keyValuePairs)
191{
192 AudioParameter param = AudioParameter(keyValuePairs);
193 String8 key = String8(AudioParameter::keyRouting);
194 int device;
ty.lee74060de2012-08-02 00:47:00 +0900195
196#ifdef SEPERATED_AUDIO_INPUT
197 String8 key_input = String8(AudioParameter::keyInputSource);
198 int source;
199
200 if (param.getInt(key_input, source) == NO_ERROR) {
201 ALOGD("setParameters(), input_source = %d", source);
202 mParent->mALSADevice->setInput(source);
203 param.remove(key_input);
204 }
205#endif
206
Iliyan Malchev4765c432012-06-11 14:36:16 -0700207 if (param.getInt(key, device) == NO_ERROR) {
208 // Ignore routing if device is 0.
Iliyan Malchev4113f342012-06-11 14:39:47 -0700209 ALOGD("setParameters(): keyRouting with device %d", device);
Eric Laurent466e8a82012-09-26 18:40:35 -0700210 // reset to speaker when disconnecting HDMI to avoid timeout due to write errors
211 if ((device == 0) && (mDevices == AudioSystem::DEVICE_OUT_AUX_DIGITAL)) {
212 device = AudioSystem::DEVICE_OUT_SPEAKER;
213 }
Iliyan Malchev4765c432012-06-11 14:36:16 -0700214 mDevices = device;
215 if(device) {
216 mParent->doRouting(device);
217 }
218 param.remove(key);
219 }
Ajay Dudani9746c472012-06-18 16:01:16 -0700220#ifdef QCOM_FM_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700221 else {
222 key = String8(AudioParameter::keyHandleFm);
223 if (param.getInt(key, device) == NO_ERROR) {
Iliyan Malchev4113f342012-06-11 14:39:47 -0700224 ALOGD("setParameters(): handleFm with device %d", device);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700225 mDevices = device;
226 if(device) {
227 mParent->handleFm(device);
228 }
229 param.remove(key);
230 }
231 }
232#endif
233
234 return NO_ERROR;
235}
236
237String8 ALSAStreamOps::getParameters(const String8& keys)
238{
239 AudioParameter param = AudioParameter(keys);
240 String8 value;
241 String8 key = String8(AudioParameter::keyRouting);
242
243 if (param.get(key, value) == NO_ERROR) {
244 param.addInt(key, (int)mDevices);
245 }
246 else {
Ajay Dudani9746c472012-06-18 16:01:16 -0700247#ifdef QCOM_VOIP_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700248 key = String8(AudioParameter::keyVoipCheck);
249 if (param.get(key, value) == NO_ERROR) {
250 if((!strncmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, strlen(SND_USE_CASE_VERB_IP_VOICECALL))) ||
251 (!strncmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP, strlen(SND_USE_CASE_MOD_PLAY_VOIP))))
252 param.addInt(key, true);
253 else
254 param.addInt(key, false);
255 }
Iliyan Malchev4113f342012-06-11 14:39:47 -0700256#endif
Iliyan Malchev4765c432012-06-11 14:36:16 -0700257 }
Iliyan Malchev4113f342012-06-11 14:39:47 -0700258 ALOGV("getParameters() %s", param.toString().string());
Iliyan Malchev4765c432012-06-11 14:36:16 -0700259 return param.toString();
260}
261
262uint32_t ALSAStreamOps::sampleRate() const
263{
264 return mHandle->sampleRate;
265}
266
267//
268// Return the number of bytes (not frames)
269//
270size_t ALSAStreamOps::bufferSize() const
271{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700272 ALOGV("bufferSize() returns %d", mHandle->bufferSize);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700273 return mHandle->bufferSize;
274}
275
276int ALSAStreamOps::format() const
277{
278 int audioSystemFormat;
279
280 snd_pcm_format_t ALSAFormat = mHandle->format;
281
282 switch(ALSAFormat) {
283 case SNDRV_PCM_FORMAT_S8:
284 audioSystemFormat = AudioSystem::PCM_8_BIT;
285 break;
286
287 case AudioSystem::AMR_NB:
288 case AudioSystem::AMR_WB:
Ajay Dudani9746c472012-06-18 16:01:16 -0700289#ifdef QCOM_QCHAT_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700290 case AudioSystem::EVRC:
291 case AudioSystem::EVRCB:
292 case AudioSystem::EVRCWB:
293#endif
294 audioSystemFormat = mHandle->format;
295 break;
296 case SNDRV_PCM_FORMAT_S16_LE:
297 audioSystemFormat = AudioSystem::PCM_16_BIT;
298 break;
299
300 default:
301 LOG_FATAL("Unknown AudioSystem bit width %d!", audioSystemFormat);
302 audioSystemFormat = AudioSystem::PCM_16_BIT;
303 break;
304 }
305
SathishKumar Mani9efed762012-09-18 18:52:48 -0700306 ALOGV("ALSAFormat:0x%x,audioSystemFormat:0x%x",ALSAFormat,audioSystemFormat);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700307 return audioSystemFormat;
308}
309
310uint32_t ALSAStreamOps::channels() const
311{
312 unsigned int count = mHandle->channels;
313 uint32_t channels = 0;
314
315 if (mDevices & AudioSystem::DEVICE_OUT_ALL)
316 switch(count) {
317 case 4:
318 channels |= AudioSystem::CHANNEL_OUT_BACK_LEFT;
319 channels |= AudioSystem::CHANNEL_OUT_BACK_RIGHT;
320 // Fall through...
321 default:
322 case 2:
323 channels |= AudioSystem::CHANNEL_OUT_FRONT_RIGHT;
324 // Fall through...
325 case 1:
326 channels |= AudioSystem::CHANNEL_OUT_FRONT_LEFT;
327 break;
328 }
329 else
330 switch(count) {
Ajay Dudani9746c472012-06-18 16:01:16 -0700331#ifdef QCOM_SSR_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700332 // For 5.1 recording
333 case 6 :
334 channels |= AudioSystem::CHANNEL_IN_5POINT1;
335 break;
336 // Do not fall through...
337#endif
338 default:
339 case 2:
340 channels |= AudioSystem::CHANNEL_IN_RIGHT;
341 // Fall through...
342 case 1:
343 channels |= AudioSystem::CHANNEL_IN_LEFT;
344 break;
345 }
346
347 return channels;
348}
349
350void ALSAStreamOps::close()
351{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700352 ALOGD("close");
Iliyan Malchev4765c432012-06-11 14:36:16 -0700353 if((!strncmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, strlen(SND_USE_CASE_VERB_IP_VOICECALL))) ||
354 (!strncmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP, strlen(SND_USE_CASE_MOD_PLAY_VOIP)))) {
Iliyan Malchev80baa422012-09-27 21:29:09 -0700355 mParent->mVoipMicMute = false;
Iliyan Malchev4765c432012-06-11 14:36:16 -0700356 mParent->mVoipBitRate = 0;
357 mParent->mVoipStreamCount = 0;
358 }
359 mParent->mALSADevice->close(mHandle);
360}
361
362//
363// Set playback or capture PCM device. It's possible to support audio output
364// or input from multiple devices by using the ALSA plugins, but this is
365// not supported for simplicity.
366//
367// The AudioHardwareALSA API does not allow one to set the input routing.
368//
369// If the "routes" value does not map to a valid device, the default playback
370// device is used.
371//
372status_t ALSAStreamOps::open(int mode)
373{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700374 ALOGD("open");
Iliyan Malchev4765c432012-06-11 14:36:16 -0700375 return mParent->mALSADevice->open(mHandle);
376}
377
378} // namespace androidi_audio_legacy