blob: c37ced72db26bd62cda06e57679a0b9fb1105d8d [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
Iliyan Malchev4113f342012-06-11 14:39:47 -070027#define LOG_TAG "audio.primary.msm8960"
Iliyan Malchev4765c432012-06-11 14:36:16 -070028//#define LOG_NDEBUG 0
Iliyan Malchev4765c432012-06-11 14:36:16 -070029#include <utils/Log.h>
30#include <utils/String8.h>
31
32#include <cutils/properties.h>
33#include <media/AudioRecord.h>
34#include <hardware_legacy/power.h>
35
36#include "AudioHardwareALSA.h"
37
38namespace android_audio_legacy
39{
40
41// ----------------------------------------------------------------------------
42
43ALSAStreamOps::ALSAStreamOps(AudioHardwareALSA *parent, alsa_handle_t *handle) :
44 mParent(parent),
45 mHandle(handle)
46{
47}
48
49ALSAStreamOps::~ALSAStreamOps()
50{
51 Mutex::Autolock autoLock(mParent->mLock);
52
53 if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL)) ||
54 (!strcmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP))) {
55 if((mParent->mVoipStreamCount)) {
56 mParent->mVoipStreamCount--;
57 if(mParent->mVoipStreamCount > 0) {
Iliyan Malchev4113f342012-06-11 14:39:47 -070058 ALOGD("ALSAStreamOps::close() Ignore");
Iliyan Malchev4765c432012-06-11 14:36:16 -070059 return ;
60 }
61 }
62 mParent->mVoipStreamCount = 0;
63 mParent->mVoipMicMute = 0;
64 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) {
117#ifdef SSR_ENABLED
118 // 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:
154#if 0
155 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;
194 if (param.getInt(key, device) == NO_ERROR) {
195 // Ignore routing if device is 0.
Iliyan Malchev4113f342012-06-11 14:39:47 -0700196 ALOGD("setParameters(): keyRouting with device %d", device);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700197 mDevices = device;
198 if(device) {
199 mParent->doRouting(device);
200 }
201 param.remove(key);
202 }
203#ifdef FM_ENABLED
204 else {
205 key = String8(AudioParameter::keyHandleFm);
206 if (param.getInt(key, device) == NO_ERROR) {
Iliyan Malchev4113f342012-06-11 14:39:47 -0700207 ALOGD("setParameters(): handleFm with device %d", device);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700208 mDevices = device;
209 if(device) {
210 mParent->handleFm(device);
211 }
212 param.remove(key);
213 }
214 }
215#endif
216
217 return NO_ERROR;
218}
219
220String8 ALSAStreamOps::getParameters(const String8& keys)
221{
222 AudioParameter param = AudioParameter(keys);
223 String8 value;
224 String8 key = String8(AudioParameter::keyRouting);
225
226 if (param.get(key, value) == NO_ERROR) {
227 param.addInt(key, (int)mDevices);
228 }
229 else {
Iliyan Malchev4113f342012-06-11 14:39:47 -0700230#if 0
Iliyan Malchev4765c432012-06-11 14:36:16 -0700231 key = String8(AudioParameter::keyVoipCheck);
232 if (param.get(key, value) == NO_ERROR) {
233 if((!strncmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, strlen(SND_USE_CASE_VERB_IP_VOICECALL))) ||
234 (!strncmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP, strlen(SND_USE_CASE_MOD_PLAY_VOIP))))
235 param.addInt(key, true);
236 else
237 param.addInt(key, false);
238 }
Iliyan Malchev4113f342012-06-11 14:39:47 -0700239#endif
Iliyan Malchev4765c432012-06-11 14:36:16 -0700240 }
Iliyan Malchev4113f342012-06-11 14:39:47 -0700241 ALOGV("getParameters() %s", param.toString().string());
Iliyan Malchev4765c432012-06-11 14:36:16 -0700242 return param.toString();
243}
244
245uint32_t ALSAStreamOps::sampleRate() const
246{
247 return mHandle->sampleRate;
248}
249
250//
251// Return the number of bytes (not frames)
252//
253size_t ALSAStreamOps::bufferSize() const
254{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700255 ALOGV("bufferSize() returns %d", mHandle->bufferSize);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700256 return mHandle->bufferSize;
257}
258
259int ALSAStreamOps::format() const
260{
261 int audioSystemFormat;
262
263 snd_pcm_format_t ALSAFormat = mHandle->format;
264
265 switch(ALSAFormat) {
266 case SNDRV_PCM_FORMAT_S8:
267 audioSystemFormat = AudioSystem::PCM_8_BIT;
268 break;
269
270 case AudioSystem::AMR_NB:
271 case AudioSystem::AMR_WB:
272#if 0
273 case AudioSystem::EVRC:
274 case AudioSystem::EVRCB:
275 case AudioSystem::EVRCWB:
276#endif
277 audioSystemFormat = mHandle->format;
278 break;
279 case SNDRV_PCM_FORMAT_S16_LE:
280 audioSystemFormat = AudioSystem::PCM_16_BIT;
281 break;
282
283 default:
284 LOG_FATAL("Unknown AudioSystem bit width %d!", audioSystemFormat);
285 audioSystemFormat = AudioSystem::PCM_16_BIT;
286 break;
287 }
288
Iliyan Malchev4113f342012-06-11 14:39:47 -0700289 ALOGD("ALSAFormat:0x%x,audioSystemFormat:0x%x",ALSAFormat,audioSystemFormat);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700290 return audioSystemFormat;
291}
292
293uint32_t ALSAStreamOps::channels() const
294{
295 unsigned int count = mHandle->channels;
296 uint32_t channels = 0;
297
298 if (mDevices & AudioSystem::DEVICE_OUT_ALL)
299 switch(count) {
300 case 4:
301 channels |= AudioSystem::CHANNEL_OUT_BACK_LEFT;
302 channels |= AudioSystem::CHANNEL_OUT_BACK_RIGHT;
303 // Fall through...
304 default:
305 case 2:
306 channels |= AudioSystem::CHANNEL_OUT_FRONT_RIGHT;
307 // Fall through...
308 case 1:
309 channels |= AudioSystem::CHANNEL_OUT_FRONT_LEFT;
310 break;
311 }
312 else
313 switch(count) {
314#ifdef SSR_ENABLED
315 // For 5.1 recording
316 case 6 :
317 channels |= AudioSystem::CHANNEL_IN_5POINT1;
318 break;
319 // Do not fall through...
320#endif
321 default:
322 case 2:
323 channels |= AudioSystem::CHANNEL_IN_RIGHT;
324 // Fall through...
325 case 1:
326 channels |= AudioSystem::CHANNEL_IN_LEFT;
327 break;
328 }
329
330 return channels;
331}
332
333void ALSAStreamOps::close()
334{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700335 ALOGD("close");
Iliyan Malchev4765c432012-06-11 14:36:16 -0700336 if((!strncmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, strlen(SND_USE_CASE_VERB_IP_VOICECALL))) ||
337 (!strncmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP, strlen(SND_USE_CASE_MOD_PLAY_VOIP)))) {
338 mParent->mVoipMicMute = false;
339 mParent->mVoipBitRate = 0;
340 mParent->mVoipStreamCount = 0;
341 }
342 mParent->mALSADevice->close(mHandle);
343}
344
345//
346// Set playback or capture PCM device. It's possible to support audio output
347// or input from multiple devices by using the ALSA plugins, but this is
348// not supported for simplicity.
349//
350// The AudioHardwareALSA API does not allow one to set the input routing.
351//
352// If the "routes" value does not map to a valid device, the default playback
353// device is used.
354//
355status_t ALSAStreamOps::open(int mode)
356{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700357 ALOGD("open");
Iliyan Malchev4765c432012-06-11 14:36:16 -0700358 return mParent->mALSADevice->open(mHandle);
359}
360
361} // namespace androidi_audio_legacy