blob: 1cd75cb0cf43b08cace6e8871e29f080270bc038 [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);
Eric Laurent466e8a82012-09-26 18:40:35 -0700209 // reset to speaker when disconnecting HDMI to avoid timeout due to write errors
210 if ((device == 0) && (mDevices == AudioSystem::DEVICE_OUT_AUX_DIGITAL)) {
211 device = AudioSystem::DEVICE_OUT_SPEAKER;
212 }
Iliyan Malchev4765c432012-06-11 14:36:16 -0700213 mDevices = device;
214 if(device) {
215 mParent->doRouting(device);
216 }
217 param.remove(key);
218 }
Ajay Dudani9746c472012-06-18 16:01:16 -0700219#ifdef QCOM_FM_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700220 else {
221 key = String8(AudioParameter::keyHandleFm);
222 if (param.getInt(key, device) == NO_ERROR) {
Iliyan Malchev4113f342012-06-11 14:39:47 -0700223 ALOGD("setParameters(): handleFm with device %d", device);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700224 mDevices = device;
225 if(device) {
226 mParent->handleFm(device);
227 }
228 param.remove(key);
229 }
230 }
231#endif
232
233 return NO_ERROR;
234}
235
236String8 ALSAStreamOps::getParameters(const String8& keys)
237{
238 AudioParameter param = AudioParameter(keys);
239 String8 value;
240 String8 key = String8(AudioParameter::keyRouting);
241
242 if (param.get(key, value) == NO_ERROR) {
243 param.addInt(key, (int)mDevices);
244 }
245 else {
Ajay Dudani9746c472012-06-18 16:01:16 -0700246#ifdef QCOM_VOIP_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700247 key = String8(AudioParameter::keyVoipCheck);
248 if (param.get(key, value) == NO_ERROR) {
249 if((!strncmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, strlen(SND_USE_CASE_VERB_IP_VOICECALL))) ||
250 (!strncmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP, strlen(SND_USE_CASE_MOD_PLAY_VOIP))))
251 param.addInt(key, true);
252 else
253 param.addInt(key, false);
254 }
Iliyan Malchev4113f342012-06-11 14:39:47 -0700255#endif
Iliyan Malchev4765c432012-06-11 14:36:16 -0700256 }
Iliyan Malchev4113f342012-06-11 14:39:47 -0700257 ALOGV("getParameters() %s", param.toString().string());
Iliyan Malchev4765c432012-06-11 14:36:16 -0700258 return param.toString();
259}
260
261uint32_t ALSAStreamOps::sampleRate() const
262{
263 return mHandle->sampleRate;
264}
265
266//
267// Return the number of bytes (not frames)
268//
269size_t ALSAStreamOps::bufferSize() const
270{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700271 ALOGV("bufferSize() returns %d", mHandle->bufferSize);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700272 return mHandle->bufferSize;
273}
274
275int ALSAStreamOps::format() const
276{
277 int audioSystemFormat;
278
279 snd_pcm_format_t ALSAFormat = mHandle->format;
280
281 switch(ALSAFormat) {
282 case SNDRV_PCM_FORMAT_S8:
283 audioSystemFormat = AudioSystem::PCM_8_BIT;
284 break;
285
286 case AudioSystem::AMR_NB:
287 case AudioSystem::AMR_WB:
Ajay Dudani9746c472012-06-18 16:01:16 -0700288#ifdef QCOM_QCHAT_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700289 case AudioSystem::EVRC:
290 case AudioSystem::EVRCB:
291 case AudioSystem::EVRCWB:
292#endif
293 audioSystemFormat = mHandle->format;
294 break;
295 case SNDRV_PCM_FORMAT_S16_LE:
296 audioSystemFormat = AudioSystem::PCM_16_BIT;
297 break;
298
299 default:
300 LOG_FATAL("Unknown AudioSystem bit width %d!", audioSystemFormat);
301 audioSystemFormat = AudioSystem::PCM_16_BIT;
302 break;
303 }
304
SathishKumar Mani9efed762012-09-18 18:52:48 -0700305 ALOGV("ALSAFormat:0x%x,audioSystemFormat:0x%x",ALSAFormat,audioSystemFormat);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700306 return audioSystemFormat;
307}
308
309uint32_t ALSAStreamOps::channels() const
310{
311 unsigned int count = mHandle->channels;
312 uint32_t channels = 0;
313
314 if (mDevices & AudioSystem::DEVICE_OUT_ALL)
315 switch(count) {
316 case 4:
317 channels |= AudioSystem::CHANNEL_OUT_BACK_LEFT;
318 channels |= AudioSystem::CHANNEL_OUT_BACK_RIGHT;
319 // Fall through...
320 default:
321 case 2:
322 channels |= AudioSystem::CHANNEL_OUT_FRONT_RIGHT;
323 // Fall through...
324 case 1:
325 channels |= AudioSystem::CHANNEL_OUT_FRONT_LEFT;
326 break;
327 }
328 else
329 switch(count) {
Ajay Dudani9746c472012-06-18 16:01:16 -0700330#ifdef QCOM_SSR_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700331 // For 5.1 recording
332 case 6 :
333 channels |= AudioSystem::CHANNEL_IN_5POINT1;
334 break;
335 // Do not fall through...
336#endif
337 default:
338 case 2:
339 channels |= AudioSystem::CHANNEL_IN_RIGHT;
340 // Fall through...
341 case 1:
342 channels |= AudioSystem::CHANNEL_IN_LEFT;
343 break;
344 }
345
346 return channels;
347}
348
349void ALSAStreamOps::close()
350{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700351 ALOGD("close");
Iliyan Malchev4765c432012-06-11 14:36:16 -0700352 if((!strncmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, strlen(SND_USE_CASE_VERB_IP_VOICECALL))) ||
353 (!strncmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP, strlen(SND_USE_CASE_MOD_PLAY_VOIP)))) {
Iliyan Malchev4765c432012-06-11 14:36:16 -0700354 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