OSDN Git Service

f214eab5e2db68292b9075776733e1c99f1c27fb
[android-x86/hardware-libhardware.git] / modules / usbaudio / alsa_device_profile.c
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #define LOG_TAG "alsa_device_profile"
18 /*#define LOG_NDEBUG 0*/
19 /*#define LOG_PCM_PARAMS 0*/
20
21 #include <errno.h>
22 #include <inttypes.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25
26 #include <log/log.h>
27
28 #include "alsa_device_profile.h"
29 #include "format.h"
30 #include "logging.h"
31
32 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
33
34 /*TODO - Evaluate if this value should/can be retrieved from a device-specific property */
35 #define BUFF_DURATION_MS   5
36
37 #define DEFAULT_PERIOD_SIZE 1024
38
39 static const char * const format_string_map[] = {
40     "AUDIO_FORMAT_PCM_16_BIT",      /* "PCM_FORMAT_S16_LE", */
41     "AUDIO_FORMAT_PCM_32_BIT",      /* "PCM_FORMAT_S32_LE", */
42     "AUDIO_FORMAT_PCM_8_BIT",       /* "PCM_FORMAT_S8", */
43     "AUDIO_FORMAT_PCM_8_24_BIT",    /* "PCM_FORMAT_S24_LE", */
44     "AUDIO_FORMAT_PCM_24_BIT_PACKED"/* "PCM_FORMAT_S24_3LE" */
45 };
46
47 static const unsigned const format_byte_size_map[] = {
48     2, /* PCM_FORMAT_S16_LE */
49     4, /* PCM_FORMAT_S32_LE */
50     1, /* PCM_FORMAT_S8 */
51     4, /* PCM_FORMAT_S24_LE */
52     3, /* PCM_FORMAT_S24_3LE */
53 };
54
55 extern int8_t const pcm_format_value_map[50];
56
57 /* sort these highest -> lowest (to default to best quality) */
58 static const unsigned std_sample_rates[] =
59     {48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000};
60
61 void profile_init(alsa_device_profile* profile, int direction)
62 {
63     profile->card = profile->device = -1;
64     profile->direction = direction;
65
66     /* Fill the attribute arrays with invalid values */
67     size_t index;
68     for (index = 0; index < ARRAY_SIZE(profile->formats); index++) {
69         profile->formats[index] = PCM_FORMAT_INVALID;
70     }
71
72     for (index = 0; index < ARRAY_SIZE(profile->sample_rates); index++) {
73         profile->sample_rates[index] = 0;
74     }
75
76     for (index = 0; index < ARRAY_SIZE(profile->channel_counts); index++) {
77         profile->channel_counts[index] = 0;
78     }
79
80     profile->min_period_size = profile->max_period_size = 0;
81     profile->min_channel_count = profile->max_channel_count = DEFAULT_CHANNEL_COUNT;
82
83     profile->is_valid = false;
84 }
85
86 bool profile_is_initialized(alsa_device_profile* profile)
87 {
88     return profile->card >= 0 && profile->device >= 0;
89 }
90
91 bool profile_is_valid(alsa_device_profile* profile) {
92     return profile->is_valid;
93 }
94
95 /*
96  * Returns the supplied value rounded up to the next even multiple of 16
97  */
98 static unsigned int round_to_16_mult(unsigned int size)
99 {
100     return (size + 15) & ~15;   // 0xFFFFFFF0;
101 }
102
103 /*
104  * Returns the system defined minimum period size based on the supplied sample rate.
105  */
106 unsigned profile_calc_min_period_size(alsa_device_profile* profile, unsigned sample_rate)
107 {
108     ALOGV("profile_calc_min_period_size(%p, rate:%d)", profile, sample_rate);
109     if (profile == NULL) {
110         return DEFAULT_PERIOD_SIZE;
111     } else {
112         unsigned num_sample_frames = (sample_rate * BUFF_DURATION_MS) / 1000;
113         if (num_sample_frames < profile->min_period_size) {
114             num_sample_frames = profile->min_period_size;
115         }
116         return round_to_16_mult(num_sample_frames) * 2;
117     }
118 }
119
120 unsigned int profile_get_period_size(alsa_device_profile* profile, unsigned sample_rate)
121 {
122     // return profile->default_config.period_size;
123     unsigned int period_size = profile_calc_min_period_size(profile, sample_rate);
124     ALOGV("profile_get_period_size(rate:%d) = %d", sample_rate, period_size);
125     return period_size;
126 }
127
128 /*
129  * Sample Rate
130  */
131 unsigned profile_get_default_sample_rate(alsa_device_profile* profile)
132 {
133     /*
134      * TODO this won't be right in general. we should store a preferred rate as we are scanning.
135      * But right now it will return the highest rate, which may be correct.
136      */
137     return profile_is_valid(profile) ? profile->sample_rates[0] : DEFAULT_SAMPLE_RATE;
138 }
139
140 bool profile_is_sample_rate_valid(alsa_device_profile* profile, unsigned rate)
141 {
142     if (profile_is_valid(profile)) {
143         size_t index;
144         for (index = 0; profile->sample_rates[index] != 0; index++) {
145             if (profile->sample_rates[index] == rate) {
146                 return true;
147             }
148         }
149
150         return false;
151     } else {
152         return rate == DEFAULT_SAMPLE_RATE;
153     }
154 }
155
156 /*
157  * Format
158  */
159 enum pcm_format profile_get_default_format(alsa_device_profile* profile)
160 {
161     /*
162      * TODO this won't be right in general. we should store a preferred format as we are scanning.
163      */
164     return profile_is_valid(profile) ? profile->formats[0] : DEFAULT_SAMPLE_FORMAT;
165 }
166
167 bool profile_is_format_valid(alsa_device_profile* profile, enum pcm_format fmt) {
168     if (profile_is_valid(profile)) {
169         size_t index;
170         for (index = 0; profile->formats[index] != PCM_FORMAT_INVALID; index++) {
171             if (profile->formats[index] == fmt) {
172                 return true;
173             }
174         }
175
176         return false;
177     } else {
178         return fmt == DEFAULT_SAMPLE_FORMAT;
179     }
180 }
181
182 /*
183  * Channels
184  */
185 unsigned profile_get_default_channel_count(alsa_device_profile* profile)
186 {
187     return profile_is_valid(profile) ? profile->channel_counts[0] : DEFAULT_CHANNEL_COUNT;
188 }
189
190 bool profile_is_channel_count_valid(alsa_device_profile* profile, unsigned count)
191 {
192     if (profile_is_initialized(profile)) {
193         return count >= profile->min_channel_count && count <= profile->max_channel_count;
194     } else {
195         return count == DEFAULT_CHANNEL_COUNT;
196     }
197 }
198
199 static bool profile_test_sample_rate(alsa_device_profile* profile, unsigned rate)
200 {
201     struct pcm_config config = profile->default_config;
202     config.rate = rate;
203
204     bool works = false; /* let's be pessimistic */
205     struct pcm * pcm = pcm_open(profile->card, profile->device,
206                                 profile->direction, &config);
207
208     if (pcm != NULL) {
209         works = pcm_is_ready(pcm);
210         pcm_close(pcm);
211     }
212
213     return works;
214 }
215
216 static unsigned profile_enum_sample_rates(alsa_device_profile* profile, unsigned min, unsigned max)
217 {
218     unsigned num_entries = 0;
219     unsigned index;
220
221     for (index = 0; index < ARRAY_SIZE(std_sample_rates) &&
222                     num_entries < ARRAY_SIZE(profile->sample_rates) - 1;
223          index++) {
224         if (std_sample_rates[index] >= min && std_sample_rates[index] <= max
225                 && profile_test_sample_rate(profile, std_sample_rates[index])) {
226             profile->sample_rates[num_entries++] = std_sample_rates[index];
227         }
228     }
229
230     return num_entries; /* return # of supported rates */
231 }
232
233 static unsigned profile_enum_sample_formats(alsa_device_profile* profile, struct pcm_mask * mask)
234 {
235     const int num_slots = ARRAY_SIZE(mask->bits);
236     const int bits_per_slot = sizeof(mask->bits[0]) * 8;
237
238     const int table_size = ARRAY_SIZE(pcm_format_value_map);
239
240     int slot_index, bit_index, table_index;
241     table_index = 0;
242     int num_written = 0;
243     for (slot_index = 0; slot_index < num_slots && table_index < table_size;
244             slot_index++) {
245         unsigned bit_mask = 1;
246         for (bit_index = 0;
247                 bit_index < bits_per_slot && table_index < table_size;
248                 bit_index++) {
249             if ((mask->bits[slot_index] & bit_mask) != 0) {
250                 enum pcm_format format = pcm_format_value_map[table_index];
251                 /* Never return invalid (unrecognized) or 8-bit */
252                 if (format != PCM_FORMAT_INVALID && format != PCM_FORMAT_S8) {
253                     profile->formats[num_written++] = format;
254                     if (num_written == ARRAY_SIZE(profile->formats) - 1) {
255                         /* leave at least one PCM_FORMAT_INVALID at the end */
256                         return num_written;
257                     }
258                 }
259             }
260             bit_mask <<= 1;
261             table_index++;
262         }
263     }
264
265     return num_written;
266 }
267
268 static unsigned profile_enum_channel_counts(alsa_device_profile* profile, unsigned min, unsigned max)
269 {
270     static const unsigned std_channel_counts[] = {8, 4, 2, 1};
271
272     unsigned num_counts = 0;
273     unsigned index;
274     /* TODO write a profile_test_channel_count() */
275     /* Ensure there is at least one invalid channel count to terminate the channel counts array */
276     for (index = 0; index < ARRAY_SIZE(std_channel_counts) &&
277                     num_counts < ARRAY_SIZE(profile->channel_counts) - 1;
278          index++) {
279         /* TODO Do we want a channel counts test? */
280         if (std_channel_counts[index] >= min && std_channel_counts[index] <= max /* &&
281          profile_test_channel_count(profile, std_channel_counts[index])*/) {
282             profile->channel_counts[num_counts++] = std_channel_counts[index];
283         }
284     }
285
286     return num_counts; /* return # of supported counts */
287 }
288
289 /*
290  * Reads and decodes configuration info from the specified ALSA card/device.
291  */
292 static int read_alsa_device_config(alsa_device_profile * profile, struct pcm_config * config)
293 {
294     ALOGV("usb:audio_hw - read_alsa_device_config(c:%d d:%d t:0x%X)",
295           profile->card, profile->device, profile->direction);
296
297     if (profile->card < 0 || profile->device < 0) {
298         return -EINVAL;
299     }
300
301     struct pcm_params * alsa_hw_params =
302         pcm_params_get(profile->card, profile->device, profile->direction);
303     if (alsa_hw_params == NULL) {
304         return -EINVAL;
305     }
306
307     profile->min_period_size = pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_SIZE);
308     profile->max_period_size = pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_SIZE);
309
310     profile->min_channel_count = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
311     profile->max_channel_count = pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS);
312
313     int ret = 0;
314
315     /*
316      * This Logging will be useful when testing new USB devices.
317      */
318 #ifdef LOG_PCM_PARAMS
319     log_pcm_params(alsa_hw_params);
320 #endif
321
322     config->channels = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
323     config->rate = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
324     config->period_size = profile_calc_min_period_size(profile, config->rate);
325     config->period_count = pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS);
326     config->format = get_pcm_format_for_mask(pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT));
327 #ifdef LOG_PCM_PARAMS
328     log_pcm_config(config, "read_alsa_device_config");
329 #endif
330     if (config->format == PCM_FORMAT_INVALID) {
331         ret = -EINVAL;
332     }
333
334     pcm_params_free(alsa_hw_params);
335
336     return ret;
337 }
338
339 bool profile_read_device_info(alsa_device_profile* profile)
340 {
341     if (!profile_is_initialized(profile)) {
342         return false;
343     }
344
345     /* let's get some defaults */
346     read_alsa_device_config(profile, &profile->default_config);
347     ALOGV("default_config chans:%d rate:%d format:%d count:%d size:%d",
348           profile->default_config.channels, profile->default_config.rate,
349           profile->default_config.format, profile->default_config.period_count,
350           profile->default_config.period_size);
351
352     struct pcm_params * alsa_hw_params = pcm_params_get(profile->card,
353                                                         profile->device,
354                                                         profile->direction);
355     if (alsa_hw_params == NULL) {
356         return false;
357     }
358
359     /* Formats */
360     struct pcm_mask * format_mask = pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT);
361     profile_enum_sample_formats(profile, format_mask);
362
363     /* Channels */
364     profile_enum_channel_counts(
365             profile, pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS),
366             pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS));
367
368     /* Sample Rates */
369     profile_enum_sample_rates(
370             profile, pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE),
371             pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE));
372
373     profile->is_valid = true;
374
375     return true;
376 }
377
378 char * profile_get_sample_rate_strs(alsa_device_profile* profile)
379 {
380     char buffer[128];
381     buffer[0] = '\0';
382     int buffSize = ARRAY_SIZE(buffer);
383
384     char numBuffer[32];
385
386     int numEntries = 0;
387     unsigned index;
388     for (index = 0; profile->sample_rates[index] != 0; index++) {
389         if (numEntries++ != 0) {
390             strncat(buffer, "|", buffSize);
391         }
392         snprintf(numBuffer, sizeof(numBuffer), "%u", profile->sample_rates[index]);
393         strncat(buffer, numBuffer, buffSize);
394     }
395
396     return strdup(buffer);
397 }
398
399 char * profile_get_format_strs(alsa_device_profile* profile)
400 {
401     /* TODO remove this hack when we have support for input in non PCM16 formats */
402     if (profile->direction == PCM_IN) {
403         return strdup("AUDIO_FORMAT_PCM_16_BIT");
404     }
405
406     char buffer[128];
407     buffer[0] = '\0';
408     int buffSize = ARRAY_SIZE(buffer);
409
410     int numEntries = 0;
411     unsigned index = 0;
412     for (index = 0; profile->formats[index] != PCM_FORMAT_INVALID; index++) {
413         if (numEntries++ != 0) {
414             strncat(buffer, "|", buffSize);
415         }
416         strncat(buffer, format_string_map[profile->formats[index]], buffSize);
417     }
418
419     return strdup(buffer);
420 }
421
422 char * profile_get_channel_count_strs(alsa_device_profile* profile)
423 {
424     static const char * const out_chans_strs[] = {
425         /* 0 */"AUDIO_CHANNEL_NONE", /* will never be taken as this is a terminator */
426         /* 1 */"AUDIO_CHANNEL_OUT_MONO",
427         /* 2 */"AUDIO_CHANNEL_OUT_STEREO",
428         /* 3 */ /* "AUDIO_CHANNEL_OUT_STEREO|AUDIO_CHANNEL_OUT_FRONT_CENTER" */ NULL,
429         /* 4 */"AUDIO_CHANNEL_OUT_QUAD",
430         /* 5 */ /* "AUDIO_CHANNEL_OUT_QUAD|AUDIO_CHANNEL_OUT_FRONT_CENTER" */ NULL,
431         /* 6 */"AUDIO_CHANNEL_OUT_5POINT1",
432         /* 7 */ /* "AUDIO_CHANNEL_OUT_5POINT1|AUDIO_CHANNEL_OUT_BACK_CENTER" */ NULL,
433         /* 8 */"AUDIO_CHANNEL_OUT_7POINT1",
434         /* channel counts greater than this not considered */
435     };
436
437     static const char * const in_chans_strs[] = {
438         /* 0 */"AUDIO_CHANNEL_NONE", /* will never be taken as this is a terminator */
439         /* 1 */"AUDIO_CHANNEL_IN_MONO",
440         /* 2 */"AUDIO_CHANNEL_IN_STEREO",
441         /* channel counts greater than this not considered */
442     };
443
444     const bool isOutProfile = profile->direction == PCM_OUT;
445     const char * const * const names_array = isOutProfile ? out_chans_strs : in_chans_strs;
446     const size_t names_size = isOutProfile ? ARRAY_SIZE(out_chans_strs)
447             : ARRAY_SIZE(in_chans_strs);
448
449     char buffer[256]; /* caution, may need to be expanded */
450     buffer[0] = '\0';
451     const int buffer_size = ARRAY_SIZE(buffer);
452     int num_entries = 0;
453     bool stereo_allowed = false;
454     unsigned index;
455     unsigned channel_count;
456
457     for (index = 0; (channel_count = profile->channel_counts[index]) != 0; index++) {
458         stereo_allowed = stereo_allowed || channel_count == 2;
459         if (channel_count < names_size && names_array[channel_count] != NULL) {
460             if (num_entries++ != 0) {
461                 strncat(buffer, "|", buffer_size);
462             }
463             strncat(buffer, names_array[channel_count], buffer_size);
464         }
465     }
466     /* emulated modes:
467      * always expose stereo as we can emulate it for PCM_OUT
468      */
469     if (!stereo_allowed && isOutProfile) {
470         if (num_entries++ != 0) {
471             strncat(buffer, "|", buffer_size);
472         }
473         strncat(buffer, names_array[2], buffer_size); /* stereo */
474     }
475     return strdup(buffer);
476 }