xref: /aosp_15_r20/system/media/alsa_utils/include/alsa_device_profile.h (revision b9df5ad1c9ac98a7fefaac271a55f7ae3db05414)
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 #ifndef ANDROID_SYSTEM_MEDIA_ALSA_UTILS_ALSA_DEVICE_PROFILE_H
18 #define ANDROID_SYSTEM_MEDIA_ALSA_UTILS_ALSA_DEVICE_PROFILE_H
19 
20 #include <stdbool.h>
21 #include <system/audio.h>
22 #include <tinyalsa/asoundlib.h>
23 
24 #define MAX_PROFILE_FORMATS         6  /* We long support the 5 standard formats defined
25                                         * in asound.h, so we just need this to be 1 more
26                                         * than that */
27 #define MAX_PROFILE_SAMPLE_RATES    14 /* this number needs to be 1 more than the number of
28                                         * sample rates in std_sample_rates[]
29                                         * (in alsa_device_profile.c) */
30 #define MAX_PROFILE_CHANNEL_COUNTS  (FCC_LIMIT + 1)
31                                        /* this number need to be 1 more than the number of
32                                         * standard channel formats in std_channel_counts[]
33                                         * (in alsa_device_profile.c) */
34 
35 #define DEFAULT_SAMPLE_RATE         44100
36 #define DEFAULT_SAMPLE_FORMAT       PCM_FORMAT_S16_LE
37 #define DEFAULT_CHANNEL_COUNT       2
38 #define DEFAULT_PERIOD_COUNT        4
39 
40 typedef struct  {
41     int card;
42     int device;
43     int direction; /* PCM_OUT or PCM_IN */
44     int extra_latency_ms;  /* any extra latency in addition to the buffer */
45 
46     enum pcm_format formats[MAX_PROFILE_FORMATS];
47 
48     /* note that this list is sorted highest rate to lowest */
49     unsigned sample_rates[MAX_PROFILE_SAMPLE_RATES];
50 
51     unsigned channel_counts[MAX_PROFILE_CHANNEL_COUNTS];
52 
53     bool is_valid;
54 
55     /* read from the hardware device */
56     struct pcm_config default_config;
57 
58     unsigned min_period_size;
59     unsigned max_period_size;
60 
61     unsigned min_channel_count;
62     unsigned max_channel_count;
63 } alsa_device_profile;
64 
65 void profile_init(alsa_device_profile* profile, int direction);
66 bool profile_is_initialized(const alsa_device_profile* profile);
67 bool profile_is_valid(const alsa_device_profile* profile);
68 bool profile_is_cached_for(const alsa_device_profile* profile, int card, int device);
69 void profile_decache(alsa_device_profile* profile);
70 
71 bool profile_fill_builtin_device_info(alsa_device_profile* profile, struct pcm_config* config,
72                                       unsigned buffer_frame_count);
73 bool profile_read_device_info(alsa_device_profile* profile);
74 
75 /* Audio Config Strings Methods */
76 char * profile_get_sample_rate_strs(const alsa_device_profile* profile);
77 char * profile_get_format_strs(const alsa_device_profile* profile);
78 char * profile_get_channel_count_strs(const alsa_device_profile* profile);
79 
80 /* Sample Rate Methods */
81 unsigned profile_get_default_sample_rate(const alsa_device_profile* profile);
82 unsigned profile_get_highest_sample_rate(const alsa_device_profile* profile);
83 bool profile_is_sample_rate_valid(const alsa_device_profile* profile, unsigned rate);
84 
85 /* Format Methods */
86 enum pcm_format profile_get_default_format(const alsa_device_profile* profile);
87 bool profile_is_format_valid(const alsa_device_profile* profile, enum pcm_format fmt);
88 
89 /* Channel Methods */
90 unsigned profile_get_default_channel_count(const alsa_device_profile* profile);
91 unsigned profile_get_closest_channel_count(const alsa_device_profile* profile, unsigned count);
92 bool profile_is_channel_count_valid(const alsa_device_profile* profile, unsigned count);
93 
94 /* Utility */
95 unsigned profile_calc_min_period_size(const alsa_device_profile* profile, unsigned sample_rate);
96 unsigned int profile_get_period_size(const alsa_device_profile* profile, unsigned sample_rate);
97 
98 /* Debugging */
99 void profile_dump(const alsa_device_profile* profile, int fd);
100 
101 #endif /* ANDROID_SYSTEM_MEDIA_ALSA_UTILS_ALSA_DEVICE_PROFILE_H */
102