blob: 4c4a06270bcd2a2d12a6e687a26d107762bc1924 [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;
64 mParent->mVoipMicMute = 0;
65 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);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700210 mDevices = device;
211 if(device) {
212 mParent->doRouting(device);
213 }
214 param.remove(key);
215 }
Ajay Dudani9746c472012-06-18 16:01:16 -0700216#ifdef QCOM_FM_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700217 else {
218 key = String8(AudioParameter::keyHandleFm);
219 if (param.getInt(key, device) == NO_ERROR) {
Iliyan Malchev4113f342012-06-11 14:39:47 -0700220 ALOGD("setParameters(): handleFm with device %d", device);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700221 mDevices = device;
222 if(device) {
223 mParent->handleFm(device);
224 }
225 param.remove(key);
226 }
227 }
228#endif
229
230 return NO_ERROR;
231}
232
233String8 ALSAStreamOps::getParameters(const String8& keys)
234{
235 AudioParameter param = AudioParameter(keys);
236 String8 value;
237 String8 key = String8(AudioParameter::keyRouting);
238
239 if (param.get(key, value) == NO_ERROR) {
240 param.addInt(key, (int)mDevices);
241 }
242 else {
Ajay Dudani9746c472012-06-18 16:01:16 -0700243#ifdef QCOM_VOIP_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700244 key = String8(AudioParameter::keyVoipCheck);
245 if (param.get(key, value) == NO_ERROR) {
246 if((!strncmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, strlen(SND_USE_CASE_VERB_IP_VOICECALL))) ||
247 (!strncmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP, strlen(SND_USE_CASE_MOD_PLAY_VOIP))))
248 param.addInt(key, true);
249 else
250 param.addInt(key, false);
251 }
Iliyan Malchev4113f342012-06-11 14:39:47 -0700252#endif
Iliyan Malchev4765c432012-06-11 14:36:16 -0700253 }
Iliyan Malchev4113f342012-06-11 14:39:47 -0700254 ALOGV("getParameters() %s", param.toString().string());
Iliyan Malchev4765c432012-06-11 14:36:16 -0700255 return param.toString();
256}
257
258uint32_t ALSAStreamOps::sampleRate() const
259{
260 return mHandle->sampleRate;
261}
262
263//
264// Return the number of bytes (not frames)
265//
266size_t ALSAStreamOps::bufferSize() const
267{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700268 ALOGV("bufferSize() returns %d", mHandle->bufferSize);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700269 return mHandle->bufferSize;
270}
271
272int ALSAStreamOps::format() const
273{
274 int audioSystemFormat;
275
276 snd_pcm_format_t ALSAFormat = mHandle->format;
277
278 switch(ALSAFormat) {
279 case SNDRV_PCM_FORMAT_S8:
280 audioSystemFormat = AudioSystem::PCM_8_BIT;
281 break;
282
283 case AudioSystem::AMR_NB:
284 case AudioSystem::AMR_WB:
Ajay Dudani9746c472012-06-18 16:01:16 -0700285#ifdef QCOM_QCHAT_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700286 case AudioSystem::EVRC:
287 case AudioSystem::EVRCB:
288 case AudioSystem::EVRCWB:
289#endif
290 audioSystemFormat = mHandle->format;
291 break;
292 case SNDRV_PCM_FORMAT_S16_LE:
293 audioSystemFormat = AudioSystem::PCM_16_BIT;
294 break;
295
296 default:
297 LOG_FATAL("Unknown AudioSystem bit width %d!", audioSystemFormat);
298 audioSystemFormat = AudioSystem::PCM_16_BIT;
299 break;
300 }
301
Ajay Dudani86c852b2012-07-19 15:28:45 -0700302#if LOCAL_LOGD
Iliyan Malchev4113f342012-06-11 14:39:47 -0700303 ALOGD("ALSAFormat:0x%x,audioSystemFormat:0x%x",ALSAFormat,audioSystemFormat);
Ajay Dudani86c852b2012-07-19 15:28:45 -0700304#endif
Iliyan Malchev4765c432012-06-11 14:36:16 -0700305 return audioSystemFormat;
306}
307
308uint32_t ALSAStreamOps::channels() const
309{
310 unsigned int count = mHandle->channels;
311 uint32_t channels = 0;
312
313 if (mDevices & AudioSystem::DEVICE_OUT_ALL)
314 switch(count) {
315 case 4:
316 channels |= AudioSystem::CHANNEL_OUT_BACK_LEFT;
317 channels |= AudioSystem::CHANNEL_OUT_BACK_RIGHT;
318 // Fall through...
319 default:
320 case 2:
321 channels |= AudioSystem::CHANNEL_OUT_FRONT_RIGHT;
322 // Fall through...
323 case 1:
324 channels |= AudioSystem::CHANNEL_OUT_FRONT_LEFT;
325 break;
326 }
327 else
328 switch(count) {
Ajay Dudani9746c472012-06-18 16:01:16 -0700329#ifdef QCOM_SSR_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700330 // For 5.1 recording
331 case 6 :
332 channels |= AudioSystem::CHANNEL_IN_5POINT1;
333 break;
334 // Do not fall through...
335#endif
336 default:
337 case 2:
338 channels |= AudioSystem::CHANNEL_IN_RIGHT;
339 // Fall through...
340 case 1:
341 channels |= AudioSystem::CHANNEL_IN_LEFT;
342 break;
343 }
344
345 return channels;
346}
347
348void ALSAStreamOps::close()
349{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700350 ALOGD("close");
Iliyan Malchev4765c432012-06-11 14:36:16 -0700351 if((!strncmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, strlen(SND_USE_CASE_VERB_IP_VOICECALL))) ||
352 (!strncmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP, strlen(SND_USE_CASE_MOD_PLAY_VOIP)))) {
353 mParent->mVoipMicMute = false;
354 mParent->mVoipBitRate = 0;
355 mParent->mVoipStreamCount = 0;
356 }
357 mParent->mALSADevice->close(mHandle);
358}
359
360//
361// Set playback or capture PCM device. It's possible to support audio output
362// or input from multiple devices by using the ALSA plugins, but this is
363// not supported for simplicity.
364//
365// The AudioHardwareALSA API does not allow one to set the input routing.
366//
367// If the "routes" value does not map to a valid device, the default playback
368// device is used.
369//
370status_t ALSAStreamOps::open(int mode)
371{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700372 ALOGD("open");
Iliyan Malchev4765c432012-06-11 14:36:16 -0700373 return mParent->mALSADevice->open(mHandle);
374}
375
376} // namespace androidi_audio_legacy