blob: 0ce9494c991f2018bb9a9b77927ed86e802d4277 [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 Malchev4765c432012-06-11 14:36:16 -070064 mParent->mVoipBitRate = 0;
65 }
66 close();
67
68 for(ALSAHandleList::iterator it = mParent->mDeviceList.begin();
69 it != mParent->mDeviceList.end(); ++it) {
70 if (mHandle == &(*it)) {
71 it->useCase[0] = 0;
72 mParent->mDeviceList.erase(it);
73 break;
74 }
75 }
76}
77
78// use emulated popcount optimization
79// http://www.df.lth.se/~john_e/gems/gem002d.html
80static inline uint32_t popCount(uint32_t u)
81{
82 u = ((u&0x55555555) + ((u>>1)&0x55555555));
83 u = ((u&0x33333333) + ((u>>2)&0x33333333));
84 u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
85 u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
86 u = ( u&0x0000ffff) + (u>>16);
87 return u;
88}
89
90status_t ALSAStreamOps::set(int *format,
91 uint32_t *channels,
92 uint32_t *rate,
93 uint32_t device)
94{
95 mDevices = device;
96 if (channels && *channels != 0) {
97 if (mHandle->channels != popCount(*channels))
98 return BAD_VALUE;
99 } else if (channels) {
100 *channels = 0;
101 if (mHandle->devices & AudioSystem::DEVICE_OUT_ALL) {
102 switch(mHandle->channels) {
103 case 4:
104 *channels |= AudioSystem::CHANNEL_OUT_BACK_LEFT;
105 *channels |= AudioSystem::CHANNEL_OUT_BACK_RIGHT;
106 // Fall through...
107 default:
108 case 2:
109 *channels |= AudioSystem::CHANNEL_OUT_FRONT_RIGHT;
110 // Fall through...
111 case 1:
112 *channels |= AudioSystem::CHANNEL_OUT_FRONT_LEFT;
113 break;
114 }
115 } else {
116 switch(mHandle->channels) {
Ajay Dudani9746c472012-06-18 16:01:16 -0700117#ifdef QCOM_SSR_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700118 // For 5.1 recording
119 case 6 :
120 *channels |= AudioSystem::CHANNEL_IN_5POINT1;
121 break;
122#endif
123 // Do not fall through...
124 default:
125 case 2:
126 *channels |= AudioSystem::CHANNEL_IN_RIGHT;
127 // Fall through...
128 case 1:
129 *channels |= AudioSystem::CHANNEL_IN_LEFT;
130 break;
131 }
132 }
133 }
134
135 if (rate && *rate > 0) {
136 if (mHandle->sampleRate != *rate)
137 return BAD_VALUE;
138 } else if (rate) {
139 *rate = mHandle->sampleRate;
140 }
141
142 snd_pcm_format_t iformat = mHandle->format;
143
144 if (format) {
145 switch(*format) {
146 case AudioSystem::FORMAT_DEFAULT:
147 break;
148
149 case AudioSystem::PCM_16_BIT:
150 iformat = SNDRV_PCM_FORMAT_S16_LE;
151 break;
152 case AudioSystem::AMR_NB:
153 case AudioSystem::AMR_WB:
Ajay Dudani9746c472012-06-18 16:01:16 -0700154#ifdef QCOM_QCHAT_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700155 case AudioSystem::EVRC:
156 case AudioSystem::EVRCB:
157 case AudioSystem::EVRCWB:
158#endif
159 iformat = *format;
160 break;
161
162 case AudioSystem::PCM_8_BIT:
163 iformat = SNDRV_PCM_FORMAT_S8;
164 break;
165
166 default:
Iliyan Malchev4113f342012-06-11 14:39:47 -0700167 ALOGE("Unknown PCM format %i. Forcing default", *format);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700168 break;
169 }
170
171 if (mHandle->format != iformat)
172 return BAD_VALUE;
173
174 switch(iformat) {
175 case SNDRV_PCM_FORMAT_S16_LE:
176 *format = AudioSystem::PCM_16_BIT;
177 break;
178 case SNDRV_PCM_FORMAT_S8:
179 *format = AudioSystem::PCM_8_BIT;
180 break;
181 default:
182 break;
183 }
184 }
185
186 return NO_ERROR;
187}
188
189status_t ALSAStreamOps::setParameters(const String8& keyValuePairs)
190{
191 AudioParameter param = AudioParameter(keyValuePairs);
192 String8 key = String8(AudioParameter::keyRouting);
193 int device;
ty.lee74060de2012-08-02 00:47:00 +0900194
195#ifdef SEPERATED_AUDIO_INPUT
196 String8 key_input = String8(AudioParameter::keyInputSource);
197 int source;
198
199 if (param.getInt(key_input, source) == NO_ERROR) {
200 ALOGD("setParameters(), input_source = %d", source);
201 mParent->mALSADevice->setInput(source);
202 param.remove(key_input);
203 }
204#endif
205
Iliyan Malchev4765c432012-06-11 14:36:16 -0700206 if (param.getInt(key, device) == NO_ERROR) {
207 // Ignore routing if device is 0.
Iliyan Malchev4113f342012-06-11 14:39:47 -0700208 ALOGD("setParameters(): keyRouting with device %d", device);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700209 mDevices = device;
210 if(device) {
211 mParent->doRouting(device);
212 }
213 param.remove(key);
214 }
Ajay Dudani9746c472012-06-18 16:01:16 -0700215#ifdef QCOM_FM_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700216 else {
217 key = String8(AudioParameter::keyHandleFm);
218 if (param.getInt(key, device) == NO_ERROR) {
Iliyan Malchev4113f342012-06-11 14:39:47 -0700219 ALOGD("setParameters(): handleFm with device %d", device);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700220 mDevices = device;
221 if(device) {
222 mParent->handleFm(device);
223 }
224 param.remove(key);
225 }
226 }
227#endif
228
229 return NO_ERROR;
230}
231
232String8 ALSAStreamOps::getParameters(const String8& keys)
233{
234 AudioParameter param = AudioParameter(keys);
235 String8 value;
236 String8 key = String8(AudioParameter::keyRouting);
237
238 if (param.get(key, value) == NO_ERROR) {
239 param.addInt(key, (int)mDevices);
240 }
241 else {
Ajay Dudani9746c472012-06-18 16:01:16 -0700242#ifdef QCOM_VOIP_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700243 key = String8(AudioParameter::keyVoipCheck);
244 if (param.get(key, value) == NO_ERROR) {
245 if((!strncmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, strlen(SND_USE_CASE_VERB_IP_VOICECALL))) ||
246 (!strncmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP, strlen(SND_USE_CASE_MOD_PLAY_VOIP))))
247 param.addInt(key, true);
248 else
249 param.addInt(key, false);
250 }
Iliyan Malchev4113f342012-06-11 14:39:47 -0700251#endif
Iliyan Malchev4765c432012-06-11 14:36:16 -0700252 }
Iliyan Malchev4113f342012-06-11 14:39:47 -0700253 ALOGV("getParameters() %s", param.toString().string());
Iliyan Malchev4765c432012-06-11 14:36:16 -0700254 return param.toString();
255}
256
257uint32_t ALSAStreamOps::sampleRate() const
258{
259 return mHandle->sampleRate;
260}
261
262//
263// Return the number of bytes (not frames)
264//
265size_t ALSAStreamOps::bufferSize() const
266{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700267 ALOGV("bufferSize() returns %d", mHandle->bufferSize);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700268 return mHandle->bufferSize;
269}
270
271int ALSAStreamOps::format() const
272{
273 int audioSystemFormat;
274
275 snd_pcm_format_t ALSAFormat = mHandle->format;
276
277 switch(ALSAFormat) {
278 case SNDRV_PCM_FORMAT_S8:
279 audioSystemFormat = AudioSystem::PCM_8_BIT;
280 break;
281
282 case AudioSystem::AMR_NB:
283 case AudioSystem::AMR_WB:
Ajay Dudani9746c472012-06-18 16:01:16 -0700284#ifdef QCOM_QCHAT_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700285 case AudioSystem::EVRC:
286 case AudioSystem::EVRCB:
287 case AudioSystem::EVRCWB:
288#endif
289 audioSystemFormat = mHandle->format;
290 break;
291 case SNDRV_PCM_FORMAT_S16_LE:
292 audioSystemFormat = AudioSystem::PCM_16_BIT;
293 break;
294
295 default:
296 LOG_FATAL("Unknown AudioSystem bit width %d!", audioSystemFormat);
297 audioSystemFormat = AudioSystem::PCM_16_BIT;
298 break;
299 }
300
SathishKumar Mani9efed762012-09-18 18:52:48 -0700301 ALOGV("ALSAFormat:0x%x,audioSystemFormat:0x%x",ALSAFormat,audioSystemFormat);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700302 return audioSystemFormat;
303}
304
305uint32_t ALSAStreamOps::channels() const
306{
307 unsigned int count = mHandle->channels;
308 uint32_t channels = 0;
309
310 if (mDevices & AudioSystem::DEVICE_OUT_ALL)
311 switch(count) {
312 case 4:
313 channels |= AudioSystem::CHANNEL_OUT_BACK_LEFT;
314 channels |= AudioSystem::CHANNEL_OUT_BACK_RIGHT;
315 // Fall through...
316 default:
317 case 2:
318 channels |= AudioSystem::CHANNEL_OUT_FRONT_RIGHT;
319 // Fall through...
320 case 1:
321 channels |= AudioSystem::CHANNEL_OUT_FRONT_LEFT;
322 break;
323 }
324 else
325 switch(count) {
Ajay Dudani9746c472012-06-18 16:01:16 -0700326#ifdef QCOM_SSR_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700327 // For 5.1 recording
328 case 6 :
329 channels |= AudioSystem::CHANNEL_IN_5POINT1;
330 break;
331 // Do not fall through...
332#endif
333 default:
334 case 2:
335 channels |= AudioSystem::CHANNEL_IN_RIGHT;
336 // Fall through...
337 case 1:
338 channels |= AudioSystem::CHANNEL_IN_LEFT;
339 break;
340 }
341
342 return channels;
343}
344
345void ALSAStreamOps::close()
346{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700347 ALOGD("close");
Iliyan Malchev4765c432012-06-11 14:36:16 -0700348 if((!strncmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, strlen(SND_USE_CASE_VERB_IP_VOICECALL))) ||
349 (!strncmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP, strlen(SND_USE_CASE_MOD_PLAY_VOIP)))) {
Iliyan Malchev4765c432012-06-11 14:36:16 -0700350 mParent->mVoipBitRate = 0;
351 mParent->mVoipStreamCount = 0;
352 }
353 mParent->mALSADevice->close(mHandle);
354}
355
356//
357// Set playback or capture PCM device. It's possible to support audio output
358// or input from multiple devices by using the ALSA plugins, but this is
359// not supported for simplicity.
360//
361// The AudioHardwareALSA API does not allow one to set the input routing.
362//
363// If the "routes" value does not map to a valid device, the default playback
364// device is used.
365//
366status_t ALSAStreamOps::open(int mode)
367{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700368 ALOGD("open");
Iliyan Malchev4765c432012-06-11 14:36:16 -0700369 return mParent->mALSADevice->open(mHandle);
370}
371
372} // namespace androidi_audio_legacy