blob: 4e534fd3d7786d2ab42ccf6b2cf85d0cc8d5a1e5 [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>
SathishKumar Manib357a772012-09-25 23:28:29 -070036#include "AudioUtil.h"
Iliyan Malchev4765c432012-06-11 14:36:16 -070037#include "AudioHardwareALSA.h"
38
39namespace android_audio_legacy
40{
41
SathishKumar Manib357a772012-09-25 23:28:29 -070042// unused 'enumVal;' is to catch error at compile time if enumVal ever changes
43// or applied on a non-existent enum
44#define ENUM_TO_STRING(var, enumVal) {var = #enumVal; enumVal;}
45
Iliyan Malchev4765c432012-06-11 14:36:16 -070046// ----------------------------------------------------------------------------
47
48ALSAStreamOps::ALSAStreamOps(AudioHardwareALSA *parent, alsa_handle_t *handle) :
49 mParent(parent),
50 mHandle(handle)
51{
52}
53
54ALSAStreamOps::~ALSAStreamOps()
55{
56 Mutex::Autolock autoLock(mParent->mLock);
57
58 if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL)) ||
59 (!strcmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP))) {
60 if((mParent->mVoipStreamCount)) {
61 mParent->mVoipStreamCount--;
62 if(mParent->mVoipStreamCount > 0) {
Iliyan Malchev4113f342012-06-11 14:39:47 -070063 ALOGD("ALSAStreamOps::close() Ignore");
Iliyan Malchev4765c432012-06-11 14:36:16 -070064 return ;
65 }
66 }
67 mParent->mVoipStreamCount = 0;
Iliyan Malchev4765c432012-06-11 14:36:16 -070068 mParent->mVoipBitRate = 0;
69 }
70 close();
71
72 for(ALSAHandleList::iterator it = mParent->mDeviceList.begin();
73 it != mParent->mDeviceList.end(); ++it) {
74 if (mHandle == &(*it)) {
75 it->useCase[0] = 0;
76 mParent->mDeviceList.erase(it);
77 break;
78 }
79 }
80}
81
82// use emulated popcount optimization
83// http://www.df.lth.se/~john_e/gems/gem002d.html
84static inline uint32_t popCount(uint32_t u)
85{
86 u = ((u&0x55555555) + ((u>>1)&0x55555555));
87 u = ((u&0x33333333) + ((u>>2)&0x33333333));
88 u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
89 u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
90 u = ( u&0x0000ffff) + (u>>16);
91 return u;
92}
93
94status_t ALSAStreamOps::set(int *format,
95 uint32_t *channels,
96 uint32_t *rate,
97 uint32_t device)
98{
99 mDevices = device;
100 if (channels && *channels != 0) {
101 if (mHandle->channels != popCount(*channels))
102 return BAD_VALUE;
103 } else if (channels) {
104 *channels = 0;
105 if (mHandle->devices & AudioSystem::DEVICE_OUT_ALL) {
106 switch(mHandle->channels) {
SathishKumar Manibf1c8742012-09-25 23:34:51 -0700107 case 6:
108 case 5:
109 *channels |= audio_channel_out_mask_from_count(mHandle->channels);
110 break;
111 // Do not fall through
Iliyan Malchev4765c432012-06-11 14:36:16 -0700112 case 4:
113 *channels |= AudioSystem::CHANNEL_OUT_BACK_LEFT;
114 *channels |= AudioSystem::CHANNEL_OUT_BACK_RIGHT;
115 // Fall through...
116 default:
117 case 2:
118 *channels |= AudioSystem::CHANNEL_OUT_FRONT_RIGHT;
119 // Fall through...
120 case 1:
121 *channels |= AudioSystem::CHANNEL_OUT_FRONT_LEFT;
122 break;
123 }
124 } else {
125 switch(mHandle->channels) {
Ajay Dudani9746c472012-06-18 16:01:16 -0700126#ifdef QCOM_SSR_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700127 // For 5.1 recording
128 case 6 :
129 *channels |= AudioSystem::CHANNEL_IN_5POINT1;
130 break;
131#endif
132 // Do not fall through...
133 default:
134 case 2:
135 *channels |= AudioSystem::CHANNEL_IN_RIGHT;
136 // Fall through...
137 case 1:
138 *channels |= AudioSystem::CHANNEL_IN_LEFT;
139 break;
140 }
141 }
142 }
143
144 if (rate && *rate > 0) {
145 if (mHandle->sampleRate != *rate)
146 return BAD_VALUE;
147 } else if (rate) {
148 *rate = mHandle->sampleRate;
149 }
150
151 snd_pcm_format_t iformat = mHandle->format;
152
153 if (format) {
154 switch(*format) {
155 case AudioSystem::FORMAT_DEFAULT:
156 break;
157
158 case AudioSystem::PCM_16_BIT:
159 iformat = SNDRV_PCM_FORMAT_S16_LE;
160 break;
161 case AudioSystem::AMR_NB:
162 case AudioSystem::AMR_WB:
Ajay Dudani9746c472012-06-18 16:01:16 -0700163#ifdef QCOM_QCHAT_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700164 case AudioSystem::EVRC:
165 case AudioSystem::EVRCB:
166 case AudioSystem::EVRCWB:
167#endif
168 iformat = *format;
169 break;
170
171 case AudioSystem::PCM_8_BIT:
172 iformat = SNDRV_PCM_FORMAT_S8;
173 break;
174
175 default:
Iliyan Malchev4113f342012-06-11 14:39:47 -0700176 ALOGE("Unknown PCM format %i. Forcing default", *format);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700177 break;
178 }
179
180 if (mHandle->format != iformat)
181 return BAD_VALUE;
182
183 switch(iformat) {
184 case SNDRV_PCM_FORMAT_S16_LE:
185 *format = AudioSystem::PCM_16_BIT;
186 break;
187 case SNDRV_PCM_FORMAT_S8:
188 *format = AudioSystem::PCM_8_BIT;
189 break;
190 default:
191 break;
192 }
193 }
194
195 return NO_ERROR;
196}
197
198status_t ALSAStreamOps::setParameters(const String8& keyValuePairs)
199{
200 AudioParameter param = AudioParameter(keyValuePairs);
201 String8 key = String8(AudioParameter::keyRouting);
202 int device;
ty.lee74060de2012-08-02 00:47:00 +0900203
204#ifdef SEPERATED_AUDIO_INPUT
205 String8 key_input = String8(AudioParameter::keyInputSource);
206 int source;
207
208 if (param.getInt(key_input, source) == NO_ERROR) {
209 ALOGD("setParameters(), input_source = %d", source);
210 mParent->mALSADevice->setInput(source);
211 param.remove(key_input);
212 }
213#endif
214
Iliyan Malchev4765c432012-06-11 14:36:16 -0700215 if (param.getInt(key, device) == NO_ERROR) {
216 // Ignore routing if device is 0.
samin.ryu2c798912012-10-05 15:57:10 +0900217 ALOGD("setParameters(): keyRouting with device 0x%x", device);
Eric Laurent466e8a82012-09-26 18:40:35 -0700218 // reset to speaker when disconnecting HDMI to avoid timeout due to write errors
219 if ((device == 0) && (mDevices == AudioSystem::DEVICE_OUT_AUX_DIGITAL)) {
220 device = AudioSystem::DEVICE_OUT_SPEAKER;
221 }
samin.ryu2c798912012-10-05 15:57:10 +0900222 if (device)
223 mDevices = device;
224 else
225 ALOGV("must not change mDevices to 0");
226
Iliyan Malchev4765c432012-06-11 14:36:16 -0700227 if(device) {
228 mParent->doRouting(device);
229 }
230 param.remove(key);
231 }
Ajay Dudani9746c472012-06-18 16:01:16 -0700232#ifdef QCOM_FM_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700233 else {
234 key = String8(AudioParameter::keyHandleFm);
235 if (param.getInt(key, device) == NO_ERROR) {
Iliyan Malchev4113f342012-06-11 14:39:47 -0700236 ALOGD("setParameters(): handleFm with device %d", device);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700237 mDevices = device;
238 if(device) {
239 mParent->handleFm(device);
240 }
241 param.remove(key);
242 }
243 }
244#endif
245
246 return NO_ERROR;
247}
248
249String8 ALSAStreamOps::getParameters(const String8& keys)
250{
251 AudioParameter param = AudioParameter(keys);
252 String8 value;
253 String8 key = String8(AudioParameter::keyRouting);
254
255 if (param.get(key, value) == NO_ERROR) {
256 param.addInt(key, (int)mDevices);
257 }
258 else {
Ajay Dudani9746c472012-06-18 16:01:16 -0700259#ifdef QCOM_VOIP_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700260 key = String8(AudioParameter::keyVoipCheck);
261 if (param.get(key, value) == NO_ERROR) {
262 if((!strncmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, strlen(SND_USE_CASE_VERB_IP_VOICECALL))) ||
263 (!strncmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP, strlen(SND_USE_CASE_MOD_PLAY_VOIP))))
264 param.addInt(key, true);
265 else
266 param.addInt(key, false);
267 }
Iliyan Malchev4113f342012-06-11 14:39:47 -0700268#endif
Iliyan Malchev4765c432012-06-11 14:36:16 -0700269 }
SathishKumar Manib357a772012-09-25 23:28:29 -0700270 key = String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS);
271 if (param.get(key, value) == NO_ERROR) {
272 EDID_AUDIO_INFO info = { 0 };
273 bool first = true;
274 value = String8();
275 if (AudioUtil::getHDMIAudioSinkCaps(&info)) {
276 for (int i = 0; i < info.nAudioBlocks && i < MAX_EDID_BLOCKS; i++) {
277 String8 append;
278 switch (info.AudioBlocksArray[i].nChannels) {
279 //Do not handle stereo output in Multi-channel cases
280 //Stereo case is handled in normal playback path
281 case 6:
282 ENUM_TO_STRING(append, AUDIO_CHANNEL_OUT_5POINT1);
283 break;
284 case 8:
285 ENUM_TO_STRING(append, AUDIO_CHANNEL_OUT_7POINT1);
286 break;
287 default:
288 ALOGD("Unsupported number of channels %d", info.AudioBlocksArray[i].nChannels);
289 break;
290 }
291 if (!append.isEmpty()) {
292 value += (first ? append : String8("|") + append);
293 first = false;
294 }
295 }
296 } else {
297 ALOGE("Failed to get HDMI sink capabilities");
298 }
299 param.add(key, value);
300 }
Iliyan Malchev4113f342012-06-11 14:39:47 -0700301 ALOGV("getParameters() %s", param.toString().string());
Iliyan Malchev4765c432012-06-11 14:36:16 -0700302 return param.toString();
303}
304
305uint32_t ALSAStreamOps::sampleRate() const
306{
307 return mHandle->sampleRate;
308}
309
310//
311// Return the number of bytes (not frames)
312//
313size_t ALSAStreamOps::bufferSize() const
314{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700315 ALOGV("bufferSize() returns %d", mHandle->bufferSize);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700316 return mHandle->bufferSize;
317}
318
319int ALSAStreamOps::format() const
320{
321 int audioSystemFormat;
322
323 snd_pcm_format_t ALSAFormat = mHandle->format;
324
325 switch(ALSAFormat) {
326 case SNDRV_PCM_FORMAT_S8:
327 audioSystemFormat = AudioSystem::PCM_8_BIT;
328 break;
329
330 case AudioSystem::AMR_NB:
331 case AudioSystem::AMR_WB:
Ajay Dudani9746c472012-06-18 16:01:16 -0700332#ifdef QCOM_QCHAT_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700333 case AudioSystem::EVRC:
334 case AudioSystem::EVRCB:
335 case AudioSystem::EVRCWB:
336#endif
337 audioSystemFormat = mHandle->format;
338 break;
339 case SNDRV_PCM_FORMAT_S16_LE:
340 audioSystemFormat = AudioSystem::PCM_16_BIT;
341 break;
342
343 default:
344 LOG_FATAL("Unknown AudioSystem bit width %d!", audioSystemFormat);
345 audioSystemFormat = AudioSystem::PCM_16_BIT;
346 break;
347 }
348
SathishKumar Mani9efed762012-09-18 18:52:48 -0700349 ALOGV("ALSAFormat:0x%x,audioSystemFormat:0x%x",ALSAFormat,audioSystemFormat);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700350 return audioSystemFormat;
351}
352
353uint32_t ALSAStreamOps::channels() const
354{
355 unsigned int count = mHandle->channels;
356 uint32_t channels = 0;
357
358 if (mDevices & AudioSystem::DEVICE_OUT_ALL)
359 switch(count) {
SathishKumar Manibf1c8742012-09-25 23:34:51 -0700360 case 6:
361 case 5:
362 channels |=audio_channel_out_mask_from_count(count);
363 break;
364 // Do not fall through
Iliyan Malchev4765c432012-06-11 14:36:16 -0700365 case 4:
366 channels |= AudioSystem::CHANNEL_OUT_BACK_LEFT;
367 channels |= AudioSystem::CHANNEL_OUT_BACK_RIGHT;
368 // Fall through...
369 default:
370 case 2:
371 channels |= AudioSystem::CHANNEL_OUT_FRONT_RIGHT;
372 // Fall through...
373 case 1:
374 channels |= AudioSystem::CHANNEL_OUT_FRONT_LEFT;
375 break;
376 }
377 else
378 switch(count) {
Ajay Dudani9746c472012-06-18 16:01:16 -0700379#ifdef QCOM_SSR_ENABLED
Iliyan Malchev4765c432012-06-11 14:36:16 -0700380 // For 5.1 recording
381 case 6 :
382 channels |= AudioSystem::CHANNEL_IN_5POINT1;
383 break;
384 // Do not fall through...
385#endif
386 default:
387 case 2:
388 channels |= AudioSystem::CHANNEL_IN_RIGHT;
389 // Fall through...
390 case 1:
391 channels |= AudioSystem::CHANNEL_IN_LEFT;
392 break;
393 }
394
395 return channels;
396}
397
398void ALSAStreamOps::close()
399{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700400 ALOGD("close");
Iliyan Malchev4765c432012-06-11 14:36:16 -0700401 if((!strncmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, strlen(SND_USE_CASE_VERB_IP_VOICECALL))) ||
402 (!strncmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP, strlen(SND_USE_CASE_MOD_PLAY_VOIP)))) {
Iliyan Malchev4765c432012-06-11 14:36:16 -0700403 mParent->mVoipBitRate = 0;
404 mParent->mVoipStreamCount = 0;
405 }
406 mParent->mALSADevice->close(mHandle);
407}
408
409//
410// Set playback or capture PCM device. It's possible to support audio output
411// or input from multiple devices by using the ALSA plugins, but this is
412// not supported for simplicity.
413//
414// The AudioHardwareALSA API does not allow one to set the input routing.
415//
416// If the "routes" value does not map to a valid device, the default playback
417// device is used.
418//
419status_t ALSAStreamOps::open(int mode)
420{
Iliyan Malchev4113f342012-06-11 14:39:47 -0700421 ALOGD("open");
Iliyan Malchev4765c432012-06-11 14:36:16 -0700422 return mParent->mALSADevice->open(mHandle);
423}
424
425} // namespace androidi_audio_legacy