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_proxy"
18 /*#define LOG_NDEBUG 0*/
19 /*#define LOG_PCM_PARAMS 0*/
20 
21 #include <log/log.h>
22 
23 #include <errno.h>
24 
25 #include "include/alsa_device_proxy.h"
26 
27 #include "include/alsa_logging.h"
28 
29 #define DEFAULT_PERIOD_SIZE     1024
30 #define DEFAULT_PERIOD_COUNT    2
31 
32 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
33 
34 static const unsigned format_byte_size_map[] = {
35     2, /* PCM_FORMAT_S16_LE */
36     4, /* PCM_FORMAT_S32_LE */
37     1, /* PCM_FORMAT_S8 */
38     4, /* PCM_FORMAT_S24_LE */
39     3, /* PCM_FORMAT_S24_3LE */
40 };
41 
proxy_prepare(alsa_device_proxy * proxy,alsa_device_profile * profile,struct pcm_config * config)42 void proxy_prepare(alsa_device_proxy * proxy, alsa_device_profile* profile,
43                    struct pcm_config * config)
44 {
45     ALOGV("proxy_prepare(c:%d, d:%d)", profile->card, profile->device);
46 
47     proxy->profile = profile;
48 
49 #ifdef LOG_PCM_PARAMS
50     log_pcm_config(config, "proxy_setup()");
51 #endif
52 
53     if (config->format != PCM_FORMAT_INVALID && profile_is_format_valid(profile, config->format)) {
54         proxy->alsa_config.format = config->format;
55     } else {
56         ALOGW("Invalid format %d - using default %d.",
57               config->format, profile->default_config.format);
58         proxy->alsa_config.format = profile->default_config.format;
59     }
60 
61     if (config->rate != 0 && profile_is_sample_rate_valid(profile, config->rate)) {
62         proxy->alsa_config.rate = config->rate;
63     } else {
64         ALOGW("Invalid sample rate %u - using default %u.",
65               config->rate, profile->default_config.rate);
66         proxy->alsa_config.rate = profile->default_config.rate;
67     }
68 
69     if (config->channels != 0 && profile_is_channel_count_valid(profile, config->channels)) {
70         proxy->alsa_config.channels = config->channels;
71     } else {
72         ALOGW("Invalid channel count %u - using default %u.",
73               config->channels, profile->default_config.channels);
74         proxy->alsa_config.channels = profile->default_config.channels;
75 
76     }
77 
78     proxy->alsa_config.period_count = profile->default_config.period_count;
79     proxy->alsa_config.period_size =
80             profile_get_period_size(proxy->profile, proxy->alsa_config.rate);
81 
82     // Hack for USB accessory audio.
83     // Here we set the correct value for period_count if tinyalsa fails to get it from the
84     // f_audio_source driver.
85     if (proxy->alsa_config.period_count == 0) {
86         proxy->alsa_config.period_count = 4;
87     }
88 
89     proxy->pcm = NULL;
90     // config format should be checked earlier against profile.
91     if (config->format >= 0 && (size_t)config->format < ARRAY_SIZE(format_byte_size_map)) {
92         proxy->frame_size = format_byte_size_map[config->format] * proxy->alsa_config.channels;
93     } else {
94         proxy->frame_size = 1;
95     }
96 }
97 
proxy_open(alsa_device_proxy * proxy)98 int proxy_open(alsa_device_proxy * proxy)
99 {
100     alsa_device_profile* profile = proxy->profile;
101     ALOGV("proxy_open(card:%d device:%d %s)", profile->card, profile->device,
102           profile->direction == PCM_OUT ? "PCM_OUT" : "PCM_IN");
103 
104     if (profile->card < 0 || profile->device < 0) {
105         return -EINVAL;
106     }
107 
108     proxy->pcm = pcm_open(profile->card, profile->device,
109             profile->direction | PCM_MONOTONIC, &proxy->alsa_config);
110     if (proxy->pcm == NULL) {
111         return -ENOMEM;
112     }
113 
114     if (!pcm_is_ready(proxy->pcm)) {
115         ALOGE("  proxy_open() pcm_open() failed: %s", pcm_get_error(proxy->pcm));
116 #if defined(LOG_PCM_PARAMS)
117         log_pcm_config(&proxy->alsa_config, "config");
118 #endif
119         pcm_close(proxy->pcm);
120         proxy->pcm = NULL;
121         return -ENOMEM;
122     }
123 
124     return 0;
125 }
126 
proxy_close(alsa_device_proxy * proxy)127 void proxy_close(alsa_device_proxy * proxy)
128 {
129     ALOGV("proxy_close() [pcm:%p]", proxy->pcm);
130 
131     if (proxy->pcm != NULL) {
132         pcm_close(proxy->pcm);
133         proxy->pcm = NULL;
134     }
135 }
136 
137 /*
138  * Sample Rate
139  */
proxy_get_sample_rate(const alsa_device_proxy * proxy)140 unsigned proxy_get_sample_rate(const alsa_device_proxy * proxy)
141 {
142     return proxy->alsa_config.rate;
143 }
144 
145 /*
146  * Format
147  */
proxy_get_format(const alsa_device_proxy * proxy)148 enum pcm_format proxy_get_format(const alsa_device_proxy * proxy)
149 {
150     return proxy->alsa_config.format;
151 }
152 
153 /*
154  * Channel Count
155  */
proxy_get_channel_count(const alsa_device_proxy * proxy)156 unsigned proxy_get_channel_count(const alsa_device_proxy * proxy)
157 {
158     return proxy->alsa_config.channels;
159 }
160 
161 /*
162  * Other
163  */
proxy_get_period_size(const alsa_device_proxy * proxy)164 unsigned int proxy_get_period_size(const alsa_device_proxy * proxy)
165 {
166     return proxy->alsa_config.period_size;
167 }
168 
proxy_get_period_count(const alsa_device_proxy * proxy)169 unsigned int proxy_get_period_count(const alsa_device_proxy * proxy)
170 {
171     return proxy->alsa_config.period_count;
172 }
173 
proxy_get_latency(const alsa_device_proxy * proxy)174 unsigned proxy_get_latency(const alsa_device_proxy * proxy)
175 {
176     return (proxy_get_period_size(proxy) * proxy_get_period_count(proxy) * 1000)
177                / proxy_get_sample_rate(proxy);
178 }
179 
proxy_get_presentation_position(const alsa_device_proxy * proxy,uint64_t * frames,struct timespec * timestamp)180 int proxy_get_presentation_position(const alsa_device_proxy * proxy,
181         uint64_t *frames, struct timespec *timestamp)
182 {
183     int ret = -EPERM; // -1
184     unsigned int avail;
185     if (proxy->pcm != NULL
186             && pcm_get_htimestamp(proxy->pcm, &avail, timestamp) == 0) {
187         const size_t kernel_buffer_size =
188                 proxy->alsa_config.period_size * proxy->alsa_config.period_count;
189         if (avail > kernel_buffer_size) {
190             ALOGE("available frames(%u) > buffer size(%zu)", avail, kernel_buffer_size);
191         } else {
192             int64_t signed_frames = proxy->transferred - kernel_buffer_size + avail;
193             // It is possible to compensate for additional driver and device delay
194             // by changing signed_frames.  Example:
195             // signed_frames -= 20 /* ms */ * proxy->alsa_config.rate / 1000;
196             if (signed_frames >= 0) {
197                 *frames = signed_frames;
198                 ret = 0;
199             }
200         }
201     }
202     return ret;
203 }
204 
205 /*
206  * I/O
207  */
proxy_write(alsa_device_proxy * proxy,const void * data,unsigned int count)208 int proxy_write(alsa_device_proxy * proxy, const void *data, unsigned int count)
209 {
210     int ret = pcm_write(proxy->pcm, data, count);
211     if (ret == 0) {
212         proxy->transferred += count / proxy->frame_size;
213     }
214     return ret;
215 }
216 
proxy_read(const alsa_device_proxy * proxy,void * data,unsigned int count)217 int proxy_read(const alsa_device_proxy * proxy, void *data, unsigned int count)
218 {
219     return pcm_read(proxy->pcm, data, count);
220 }
221