• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* 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 
27 #define LOG_TAG "ALSAStreamOps"
28 //#define LOG_NDEBUG 0
29 #define LOG_NDDEBUG 0
30 #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 #include "AudioUtil.h"
37 #include "AudioHardwareALSA.h"
38 
39 namespace android_audio_legacy
40 {
41 
42 // 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 
46 // ----------------------------------------------------------------------------
47 
ALSAStreamOps(AudioHardwareALSA * parent,alsa_handle_t * handle)48 ALSAStreamOps::ALSAStreamOps(AudioHardwareALSA *parent, alsa_handle_t *handle) :
49     mParent(parent),
50     mHandle(handle)
51 {
52 }
53 
~ALSAStreamOps()54 ALSAStreamOps::~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) {
63                 ALOGD("ALSAStreamOps::close() Ignore");
64                 return ;
65             }
66        }
67        mParent->mVoipStreamCount = 0;
68        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
popCount(uint32_t u)84 static 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 
set(int * format,uint32_t * channels,uint32_t * rate,uint32_t device)94 status_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) {
107                 case 6:
108                 case 5:
109                     *channels |= audio_channel_out_mask_from_count(mHandle->channels);
110                     break;
111                     // Do not fall through
112                 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) {
126 #ifdef QCOM_SSR_ENABLED
127                 // 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:
163 #ifdef QCOM_QCHAT_ENABLED
164             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:
176                 ALOGE("Unknown PCM format %i. Forcing default", *format);
177                 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 
setParameters(const String8 & keyValuePairs)198 status_t ALSAStreamOps::setParameters(const String8& keyValuePairs)
199 {
200     AudioParameter param = AudioParameter(keyValuePairs);
201     String8 key = String8(AudioParameter::keyRouting);
202     int device;
203 
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 
215     if (param.getInt(key, device) == NO_ERROR) {
216         // Ignore routing if device is 0.
217         ALOGD("setParameters(): keyRouting with device 0x%x", device);
218         // 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         }
222         if (device)
223             mDevices = device;
224         else
225             ALOGV("must not change mDevices to 0");
226 
227         if(device) {
228             mParent->doRouting(device);
229         }
230         param.remove(key);
231     }
232 #ifdef QCOM_FM_ENABLED
233     else {
234         key = String8(AudioParameter::keyHandleFm);
235         if (param.getInt(key, device) == NO_ERROR) {
236         ALOGD("setParameters(): handleFm with device %d", device);
237         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 
getParameters(const String8 & keys)249 String8 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 {
259 #ifdef QCOM_VOIP_ENABLED
260         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         }
268 #endif
269     }
270     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     }
301     ALOGV("getParameters() %s", param.toString().string());
302     return param.toString();
303 }
304 
sampleRate() const305 uint32_t ALSAStreamOps::sampleRate() const
306 {
307     return mHandle->sampleRate;
308 }
309 
310 //
311 // Return the number of bytes (not frames)
312 //
bufferSize() const313 size_t ALSAStreamOps::bufferSize() const
314 {
315     ALOGV("bufferSize() returns %d", mHandle->bufferSize);
316     return mHandle->bufferSize;
317 }
318 
format() const319 int 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:
332 #ifdef QCOM_QCHAT_ENABLED
333         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 
349     ALOGV("ALSAFormat:0x%x,audioSystemFormat:0x%x",ALSAFormat,audioSystemFormat);
350     return audioSystemFormat;
351 }
352 
channels() const353 uint32_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) {
360             case 6:
361             case 5:
362                 channels |=audio_channel_out_mask_from_count(count);
363                 break;
364                 // Do not fall through
365             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) {
379 #ifdef QCOM_SSR_ENABLED
380             // 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 
close()398 void ALSAStreamOps::close()
399 {
400     ALOGD("close");
401     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)))) {
403        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 //
open(int mode)419 status_t ALSAStreamOps::open(int mode)
420 {
421     ALOGD("open");
422     return mParent->mALSADevice->open(mHandle);
423 }
424 
425 }       // namespace androidi_audio_legacy
426