1 /*
2 * Copyright (C) 2017 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 /**
18 * Derived from goldfish/audio/audio_hw.c
19 * Changes made to adding support of AUDIO_DEVICE_OUT_BUS
20 */
21
22 #define LOG_TAG "audio_hw_generic"
23
24 #include <assert.h>
25 #include <errno.h>
26 #include <inttypes.h>
27 #include <math.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <sys/time.h>
31 #include <dlfcn.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34
35 #include <log/log.h>
36 #include <cutils/properties.h>
37 #include <cutils/str_parms.h>
38
39 #include <hardware/hardware.h>
40 #include <system/audio.h>
41
42 #include "audio_hw.h"
43 #include "ext_pcm.h"
44
45 #define PCM_CARD 0
46 #define PCM_DEVICE 0
47
48 #define OUT_PERIOD_MS 15
49 #define OUT_PERIOD_COUNT 4
50
51 #define IN_PERIOD_MS 15
52 #define IN_PERIOD_COUNT 4
53
54 #define _bool_str(x) ((x)?"true":"false")
55
56 #define PROP_KEY_SIMULATE_MULTI_ZONE_AUDIO "ro.aae.simulateMultiZoneAudio"
57
58 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state);
59
60 static struct pcm_config pcm_config_out = {
61 .channels = 2,
62 .rate = 0,
63 .period_size = 0,
64 .period_count = OUT_PERIOD_COUNT,
65 .format = PCM_FORMAT_S16_LE,
66 .start_threshold = 0,
67 };
68
69 static struct pcm_config pcm_config_in = {
70 .channels = 2,
71 .rate = 0,
72 .period_size = 0,
73 .period_count = IN_PERIOD_COUNT,
74 .format = PCM_FORMAT_S16_LE,
75 .start_threshold = 0,
76 .stop_threshold = INT_MAX,
77 };
78
79 static pthread_mutex_t adev_init_lock = PTHREAD_MUTEX_INITIALIZER;
80 static unsigned int audio_device_ref_count = 0;
81
out_get_sample_rate(const struct audio_stream * stream)82 static uint32_t out_get_sample_rate(const struct audio_stream *stream) {
83 struct generic_stream_out *out = (struct generic_stream_out *)stream;
84 return out->req_config.sample_rate;
85 }
86
out_set_sample_rate(struct audio_stream * stream,uint32_t rate)87 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) {
88 return -ENOSYS;
89 }
90
out_get_buffer_size(const struct audio_stream * stream)91 static size_t out_get_buffer_size(const struct audio_stream *stream) {
92 struct generic_stream_out *out = (struct generic_stream_out *)stream;
93 int size = out->pcm_config.period_size *
94 audio_stream_out_frame_size(&out->stream);
95
96 return size;
97 }
98
out_get_channels(const struct audio_stream * stream)99 static audio_channel_mask_t out_get_channels(const struct audio_stream *stream) {
100 struct generic_stream_out *out = (struct generic_stream_out *)stream;
101 return out->req_config.channel_mask;
102 }
103
out_get_format(const struct audio_stream * stream)104 static audio_format_t out_get_format(const struct audio_stream *stream) {
105 struct generic_stream_out *out = (struct generic_stream_out *)stream;
106 return out->req_config.format;
107 }
108
out_set_format(struct audio_stream * stream,audio_format_t format)109 static int out_set_format(struct audio_stream *stream, audio_format_t format) {
110 return -ENOSYS;
111 }
112
out_dump(const struct audio_stream * stream,int fd)113 static int out_dump(const struct audio_stream *stream, int fd) {
114 struct generic_stream_out *out = (struct generic_stream_out *)stream;
115 pthread_mutex_lock(&out->lock);
116 dprintf(fd, "\tout_dump:\n"
117 "\t\taddress: %s\n"
118 "\t\tsample rate: %u\n"
119 "\t\tbuffer size: %zu\n"
120 "\t\tchannel mask: %08x\n"
121 "\t\tformat: %d\n"
122 "\t\tdevice: %08x\n"
123 "\t\tamplitude ratio: %f\n"
124 "\t\tenabled channels: %d\n"
125 "\t\taudio dev: %p\n\n",
126 out->bus_address,
127 out_get_sample_rate(stream),
128 out_get_buffer_size(stream),
129 out_get_channels(stream),
130 out_get_format(stream),
131 out->device,
132 out->amplitude_ratio,
133 out->enabled_channels,
134 out->dev);
135 pthread_mutex_unlock(&out->lock);
136 return 0;
137 }
138
out_set_parameters(struct audio_stream * stream,const char * kvpairs)139 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) {
140 struct generic_stream_out *out = (struct generic_stream_out *)stream;
141 struct str_parms *parms;
142 char value[32];
143 int ret;
144 long val;
145 char *end;
146
147 pthread_mutex_lock(&out->lock);
148 if (!out->standby) {
149 //Do not support changing params while stream running
150 ret = -ENOSYS;
151 } else {
152 parms = str_parms_create_str(kvpairs);
153 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
154 value, sizeof(value));
155 if (ret >= 0) {
156 errno = 0;
157 val = strtol(value, &end, 10);
158 if (errno == 0 && (end != NULL) && (*end == '\0') && ((int)val == val)) {
159 out->device = (int)val;
160 ret = 0;
161 } else {
162 ret = -EINVAL;
163 }
164 }
165 str_parms_destroy(parms);
166 }
167 pthread_mutex_unlock(&out->lock);
168 return ret;
169 }
170
out_get_parameters(const struct audio_stream * stream,const char * keys)171 static char *out_get_parameters(const struct audio_stream *stream, const char *keys) {
172 struct generic_stream_out *out = (struct generic_stream_out *)stream;
173 struct str_parms *query = str_parms_create_str(keys);
174 char *str;
175 char value[256];
176 struct str_parms *reply = str_parms_create();
177 int ret;
178
179 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
180 if (ret >= 0) {
181 pthread_mutex_lock(&out->lock);
182 str_parms_add_int(reply, AUDIO_PARAMETER_STREAM_ROUTING, out->device);
183 pthread_mutex_unlock(&out->lock);
184 str = strdup(str_parms_to_str(reply));
185 } else {
186 str = strdup(keys);
187 }
188
189 str_parms_destroy(query);
190 str_parms_destroy(reply);
191 return str;
192 }
193
out_get_latency(const struct audio_stream_out * stream)194 static uint32_t out_get_latency(const struct audio_stream_out *stream) {
195 struct generic_stream_out *out = (struct generic_stream_out *)stream;
196 return (out->pcm_config.period_size * 1000) / out->pcm_config.rate;
197 }
198
out_set_volume(struct audio_stream_out * stream,float left,float right)199 static int out_set_volume(struct audio_stream_out *stream,
200 float left, float right) {
201 return -ENOSYS;
202 }
203
out_write_worker(void * args)204 static void *out_write_worker(void *args) {
205 struct generic_stream_out *out = (struct generic_stream_out *)args;
206 struct ext_pcm *ext_pcm = NULL;
207 uint8_t *buffer = NULL;
208 int buffer_frames;
209 int buffer_size;
210 bool restart = false;
211 bool shutdown = false;
212 while (true) {
213 pthread_mutex_lock(&out->lock);
214 while (out->worker_standby || restart) {
215 restart = false;
216 if (ext_pcm) {
217 ext_pcm_close(ext_pcm); // Frees pcm
218 ext_pcm = NULL;
219 free(buffer);
220 buffer=NULL;
221 }
222 if (out->worker_exit) {
223 break;
224 }
225 pthread_cond_wait(&out->worker_wake, &out->lock);
226 }
227
228 if (out->worker_exit) {
229 if (!out->worker_standby) {
230 ALOGE("Out worker:%s not in standby before exiting", out->bus_address);
231 }
232 shutdown = true;
233 }
234
235 while (!shutdown && audio_vbuffer_live(&out->buffer) == 0) {
236 pthread_cond_wait(&out->worker_wake, &out->lock);
237 }
238
239 if (shutdown) {
240 pthread_mutex_unlock(&out->lock);
241 break;
242 }
243
244 if (!ext_pcm) {
245 ext_pcm = ext_pcm_open(PCM_CARD, PCM_DEVICE,
246 PCM_OUT | PCM_MONOTONIC, &out->pcm_config);
247 if (!ext_pcm_is_ready(ext_pcm)) {
248 ALOGE("pcm_open(out) failed: %s: address %s channels %d format %d rate %d",
249 ext_pcm_get_error(ext_pcm),
250 out->bus_address,
251 out->pcm_config.channels,
252 out->pcm_config.format,
253 out->pcm_config.rate);
254 pthread_mutex_unlock(&out->lock);
255 break;
256 }
257 buffer_frames = out->pcm_config.period_size;
258 buffer_size = ext_pcm_frames_to_bytes(ext_pcm, buffer_frames);
259 buffer = malloc(buffer_size);
260 if (!buffer) {
261 ALOGE("could not allocate write buffer");
262 pthread_mutex_unlock(&out->lock);
263 break;
264 }
265 }
266 int frames = audio_vbuffer_read(&out->buffer, buffer, buffer_frames);
267 pthread_mutex_unlock(&out->lock);
268 int write_error = ext_pcm_write(ext_pcm, out->bus_address,
269 buffer, ext_pcm_frames_to_bytes(ext_pcm, frames));
270 if (write_error) {
271 ALOGE("pcm_write failed %s address %s", ext_pcm_get_error(ext_pcm), out->bus_address);
272 restart = true;
273 } else {
274 ALOGV("pcm_write succeed address %s", out->bus_address);
275 }
276 }
277 if (buffer) {
278 free(buffer);
279 }
280
281 return NULL;
282 }
283
284 // Call with in->lock held
get_current_output_position(struct generic_stream_out * out,uint64_t * position,struct timespec * timestamp)285 static void get_current_output_position(struct generic_stream_out *out,
286 uint64_t *position, struct timespec * timestamp) {
287 struct timespec curtime = { .tv_sec = 0, .tv_nsec = 0 };
288 clock_gettime(CLOCK_MONOTONIC, &curtime);
289 const int64_t now_us = (curtime.tv_sec * 1000000000LL + curtime.tv_nsec) / 1000;
290 if (timestamp) {
291 *timestamp = curtime;
292 }
293 int64_t position_since_underrun;
294 if (out->standby) {
295 position_since_underrun = 0;
296 } else {
297 const int64_t first_us = (out->underrun_time.tv_sec * 1000000000LL +
298 out->underrun_time.tv_nsec) / 1000;
299 position_since_underrun = (now_us - first_us) *
300 out_get_sample_rate(&out->stream.common) /
301 1000000;
302 if (position_since_underrun < 0) {
303 position_since_underrun = 0;
304 }
305 }
306 *position = out->underrun_position + position_since_underrun;
307
308 // The device will reuse the same output stream leading to periods of
309 // underrun.
310 if (*position > out->frames_written) {
311 ALOGW("Not supplying enough data to HAL, expected position %" PRIu64 " , only wrote "
312 "%" PRIu64,
313 *position, out->frames_written);
314
315 *position = out->frames_written;
316 out->underrun_position = *position;
317 out->underrun_time = curtime;
318 out->frames_total_buffered = 0;
319 }
320 }
321
322 // Applies gain naively, assumes AUDIO_FORMAT_PCM_16_BIT and stereo output
out_apply_gain(struct generic_stream_out * out,const void * buffer,size_t bytes)323 static void out_apply_gain(struct generic_stream_out *out, const void *buffer, size_t bytes) {
324 int16_t *int16_buffer = (int16_t *)buffer;
325 size_t int16_size = bytes / sizeof(int16_t);
326 for (int i = 0; i < int16_size; i++) {
327 if ((i % 2) && !(out->enabled_channels & RIGHT_CHANNEL)) {
328 int16_buffer[i] = 0;
329 } else if (!(i % 2) && !(out->enabled_channels & LEFT_CHANNEL)) {
330 int16_buffer[i] = 0;
331 } else {
332 float multiplied = int16_buffer[i] * out->amplitude_ratio;
333 if (multiplied > INT16_MAX) int16_buffer[i] = INT16_MAX;
334 else if (multiplied < INT16_MIN) int16_buffer[i] = INT16_MIN;
335 else int16_buffer[i] = (int16_t)multiplied;
336 }
337 }
338 }
339
out_write(struct audio_stream_out * stream,const void * buffer,size_t bytes)340 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer, size_t bytes) {
341 struct generic_stream_out *out = (struct generic_stream_out *)stream;
342 ALOGV("%s: to device %s", __func__, out->bus_address);
343 const size_t frames = bytes / audio_stream_out_frame_size(stream);
344
345 pthread_mutex_lock(&out->lock);
346
347 if (out->worker_standby) {
348 out->worker_standby = false;
349 }
350
351 uint64_t current_position;
352 struct timespec current_time;
353
354 get_current_output_position(out, ¤t_position, ¤t_time);
355 const uint64_t now_us = (current_time.tv_sec * 1000000000LL +
356 current_time.tv_nsec) / 1000;
357 if (out->standby) {
358 out->standby = false;
359 out->underrun_time = current_time;
360 out->frames_rendered = 0;
361 out->frames_total_buffered = 0;
362 }
363
364 size_t frames_written = frames;
365 if (out->dev->master_mute) {
366 ALOGV("%s: ignored due to master mute", __func__);
367 } else {
368 out_apply_gain(out, buffer, bytes);
369 frames_written = audio_vbuffer_write(&out->buffer, buffer, frames);
370 pthread_cond_signal(&out->worker_wake);
371 }
372
373 /* Implementation just consumes bytes if we start getting backed up */
374 out->frames_written += frames;
375 out->frames_rendered += frames;
376 out->frames_total_buffered += frames;
377
378 // We simulate the audio device blocking when it's write buffers become
379 // full.
380
381 // At the beginning or after an underrun, try to fill up the vbuffer.
382 // This will be throttled by the PlaybackThread
383 int frames_sleep = out->frames_total_buffered < out->buffer.frame_count ? 0 : frames;
384
385 uint64_t sleep_time_us = frames_sleep * 1000000LL /
386 out_get_sample_rate(&stream->common);
387
388 // If the write calls are delayed, subtract time off of the sleep to
389 // compensate
390 uint64_t time_since_last_write_us = now_us - out->last_write_time_us;
391 if (time_since_last_write_us < sleep_time_us) {
392 sleep_time_us -= time_since_last_write_us;
393 } else {
394 sleep_time_us = 0;
395 }
396 out->last_write_time_us = now_us + sleep_time_us;
397
398 pthread_mutex_unlock(&out->lock);
399
400 if (sleep_time_us > 0) {
401 usleep(sleep_time_us);
402 }
403
404 if (frames_written < frames) {
405 ALOGW("Hardware backing HAL too slow, could only write %zu of %zu frames",
406 frames_written, frames);
407 }
408
409 /* Always consume all bytes */
410 return bytes;
411 }
412
out_get_presentation_position(const struct audio_stream_out * stream,uint64_t * frames,struct timespec * timestamp)413 static int out_get_presentation_position(const struct audio_stream_out *stream,
414 uint64_t *frames, struct timespec *timestamp) {
415 int ret = -EINVAL;
416 if (stream == NULL || frames == NULL || timestamp == NULL) {
417 return -EINVAL;
418 }
419 struct generic_stream_out *out = (struct generic_stream_out *)stream;
420
421 pthread_mutex_lock(&out->lock);
422 get_current_output_position(out, frames, timestamp);
423 pthread_mutex_unlock(&out->lock);
424
425 return 0;
426 }
427
out_get_render_position(const struct audio_stream_out * stream,uint32_t * dsp_frames)428 static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames) {
429 if (stream == NULL || dsp_frames == NULL) {
430 return -EINVAL;
431 }
432 struct generic_stream_out *out = (struct generic_stream_out *)stream;
433 pthread_mutex_lock(&out->lock);
434 *dsp_frames = out->frames_rendered;
435 pthread_mutex_unlock(&out->lock);
436 return 0;
437 }
438
439 // Must be called with out->lock held
do_out_standby(struct generic_stream_out * out)440 static void do_out_standby(struct generic_stream_out *out) {
441 int frames_sleep = 0;
442 uint64_t sleep_time_us = 0;
443 if (out->standby) {
444 return;
445 }
446 while (true) {
447 get_current_output_position(out, &out->underrun_position, NULL);
448 frames_sleep = out->frames_written - out->underrun_position;
449
450 if (frames_sleep == 0) {
451 break;
452 }
453
454 sleep_time_us = frames_sleep * 1000000LL /
455 out_get_sample_rate(&out->stream.common);
456
457 pthread_mutex_unlock(&out->lock);
458 usleep(sleep_time_us);
459 pthread_mutex_lock(&out->lock);
460 }
461 out->worker_standby = true;
462 out->standby = true;
463 }
464
out_standby(struct audio_stream * stream)465 static int out_standby(struct audio_stream *stream) {
466 struct generic_stream_out *out = (struct generic_stream_out *)stream;
467 pthread_mutex_lock(&out->lock);
468 do_out_standby(out);
469 pthread_mutex_unlock(&out->lock);
470 return 0;
471 }
472
out_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)473 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) {
474 // out_add_audio_effect is a no op
475 return 0;
476 }
477
out_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)478 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) {
479 // out_remove_audio_effect is a no op
480 return 0;
481 }
482
out_get_next_write_timestamp(const struct audio_stream_out * stream,int64_t * timestamp)483 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
484 int64_t *timestamp) {
485 return -ENOSYS;
486 }
487
in_get_sample_rate(const struct audio_stream * stream)488 static uint32_t in_get_sample_rate(const struct audio_stream *stream) {
489 struct generic_stream_in *in = (struct generic_stream_in *)stream;
490 return in->req_config.sample_rate;
491 }
492
in_set_sample_rate(struct audio_stream * stream,uint32_t rate)493 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) {
494 return -ENOSYS;
495 }
496
refine_output_parameters(uint32_t * sample_rate,audio_format_t * format,audio_channel_mask_t * channel_mask)497 static int refine_output_parameters(uint32_t *sample_rate, audio_format_t *format,
498 audio_channel_mask_t *channel_mask) {
499 static const uint32_t sample_rates [] = {
500 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
501 };
502 static const int sample_rates_count = sizeof(sample_rates)/sizeof(uint32_t);
503 bool inval = false;
504 if (*format != AUDIO_FORMAT_PCM_16_BIT) {
505 *format = AUDIO_FORMAT_PCM_16_BIT;
506 inval = true;
507 }
508
509 int channel_count = popcount(*channel_mask);
510 if (channel_count != 1 && channel_count != 2) {
511 *channel_mask = AUDIO_CHANNEL_IN_STEREO;
512 inval = true;
513 }
514
515 int i;
516 for (i = 0; i < sample_rates_count; i++) {
517 if (*sample_rate < sample_rates[i]) {
518 *sample_rate = sample_rates[i];
519 inval=true;
520 break;
521 }
522 else if (*sample_rate == sample_rates[i]) {
523 break;
524 }
525 else if (i == sample_rates_count-1) {
526 // Cap it to the highest rate we support
527 *sample_rate = sample_rates[i];
528 inval=true;
529 }
530 }
531
532 if (inval) {
533 return -EINVAL;
534 }
535 return 0;
536 }
537
refine_input_parameters(uint32_t * sample_rate,audio_format_t * format,audio_channel_mask_t * channel_mask)538 static int refine_input_parameters(uint32_t *sample_rate, audio_format_t *format,
539 audio_channel_mask_t *channel_mask) {
540 static const uint32_t sample_rates [] = {
541 8000, 11025, 16000, 22050, 44100, 48000
542 };
543 static const int sample_rates_count = sizeof(sample_rates)/sizeof(uint32_t);
544 bool inval = false;
545 // Only PCM_16_bit is supported. If this is changed, stereo to mono drop
546 // must be fixed in in_read
547 if (*format != AUDIO_FORMAT_PCM_16_BIT) {
548 *format = AUDIO_FORMAT_PCM_16_BIT;
549 inval = true;
550 }
551
552 int channel_count = popcount(*channel_mask);
553 if (channel_count != 1 && channel_count != 2) {
554 *channel_mask = AUDIO_CHANNEL_IN_STEREO;
555 inval = true;
556 }
557
558 int i;
559 for (i = 0; i < sample_rates_count; i++) {
560 if (*sample_rate < sample_rates[i]) {
561 *sample_rate = sample_rates[i];
562 inval=true;
563 break;
564 }
565 else if (*sample_rate == sample_rates[i]) {
566 break;
567 }
568 else if (i == sample_rates_count-1) {
569 // Cap it to the highest rate we support
570 *sample_rate = sample_rates[i];
571 inval=true;
572 }
573 }
574
575 if (inval) {
576 return -EINVAL;
577 }
578 return 0;
579 }
580
get_input_buffer_size(uint32_t sample_rate,audio_format_t format,audio_channel_mask_t channel_mask)581 static size_t get_input_buffer_size(uint32_t sample_rate, audio_format_t format,
582 audio_channel_mask_t channel_mask) {
583 size_t size;
584 size_t device_rate;
585 int channel_count = popcount(channel_mask);
586 if (refine_input_parameters(&sample_rate, &format, &channel_mask) != 0)
587 return 0;
588
589 size = sample_rate*IN_PERIOD_MS/1000;
590 // Audioflinger expects audio buffers to be multiple of 16 frames
591 size = ((size + 15) / 16) * 16;
592 size *= sizeof(short) * channel_count;
593
594 return size;
595 }
596
in_get_buffer_size(const struct audio_stream * stream)597 static size_t in_get_buffer_size(const struct audio_stream *stream) {
598 struct generic_stream_in *in = (struct generic_stream_in *)stream;
599 int size = get_input_buffer_size(in->req_config.sample_rate,
600 in->req_config.format,
601 in->req_config.channel_mask);
602
603 return size;
604 }
605
in_get_channels(const struct audio_stream * stream)606 static audio_channel_mask_t in_get_channels(const struct audio_stream *stream) {
607 struct generic_stream_in *in = (struct generic_stream_in *)stream;
608 return in->req_config.channel_mask;
609 }
610
in_get_format(const struct audio_stream * stream)611 static audio_format_t in_get_format(const struct audio_stream *stream) {
612 struct generic_stream_in *in = (struct generic_stream_in *)stream;
613 return in->req_config.format;
614 }
615
in_set_format(struct audio_stream * stream,audio_format_t format)616 static int in_set_format(struct audio_stream *stream, audio_format_t format) {
617 return -ENOSYS;
618 }
619
in_dump(const struct audio_stream * stream,int fd)620 static int in_dump(const struct audio_stream *stream, int fd) {
621 struct generic_stream_in *in = (struct generic_stream_in *)stream;
622
623 pthread_mutex_lock(&in->lock);
624 dprintf(fd, "\tin_dump:\n"
625 "\t\tsample rate: %u\n"
626 "\t\tbuffer size: %zu\n"
627 "\t\tchannel mask: %08x\n"
628 "\t\tformat: %d\n"
629 "\t\tdevice: %08x\n"
630 "\t\taudio dev: %p\n\n",
631 in_get_sample_rate(stream),
632 in_get_buffer_size(stream),
633 in_get_channels(stream),
634 in_get_format(stream),
635 in->device,
636 in->dev);
637 pthread_mutex_unlock(&in->lock);
638 return 0;
639 }
640
in_set_parameters(struct audio_stream * stream,const char * kvpairs)641 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) {
642 struct generic_stream_in *in = (struct generic_stream_in *)stream;
643 struct str_parms *parms;
644 char value[32];
645 int ret;
646 long val;
647 char *end;
648
649 pthread_mutex_lock(&in->lock);
650 if (!in->standby) {
651 ret = -ENOSYS;
652 } else {
653 parms = str_parms_create_str(kvpairs);
654
655 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
656 value, sizeof(value));
657 if (ret >= 0) {
658 errno = 0;
659 val = strtol(value, &end, 10);
660 if ((errno == 0) && (end != NULL) && (*end == '\0') && ((int)val == val)) {
661 in->device = (int)val;
662 ret = 0;
663 } else {
664 ret = -EINVAL;
665 }
666 }
667
668 str_parms_destroy(parms);
669 }
670 pthread_mutex_unlock(&in->lock);
671 return ret;
672 }
673
in_get_parameters(const struct audio_stream * stream,const char * keys)674 static char *in_get_parameters(const struct audio_stream *stream, const char *keys) {
675 struct generic_stream_in *in = (struct generic_stream_in *)stream;
676 struct str_parms *query = str_parms_create_str(keys);
677 char *str;
678 char value[256];
679 struct str_parms *reply = str_parms_create();
680 int ret;
681
682 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
683 if (ret >= 0) {
684 str_parms_add_int(reply, AUDIO_PARAMETER_STREAM_ROUTING, in->device);
685 str = strdup(str_parms_to_str(reply));
686 } else {
687 str = strdup(keys);
688 }
689
690 str_parms_destroy(query);
691 str_parms_destroy(reply);
692 return str;
693 }
694
in_set_gain(struct audio_stream_in * stream,float gain)695 static int in_set_gain(struct audio_stream_in *stream, float gain) {
696 // TODO(hwwang): support adjusting input gain
697 return 0;
698 }
699
700 // Call with in->lock held
get_current_input_position(struct generic_stream_in * in,int64_t * position,struct timespec * timestamp)701 static void get_current_input_position(struct generic_stream_in *in,
702 int64_t * position, struct timespec * timestamp) {
703 struct timespec t = { .tv_sec = 0, .tv_nsec = 0 };
704 clock_gettime(CLOCK_MONOTONIC, &t);
705 const int64_t now_us = (t.tv_sec * 1000000000LL + t.tv_nsec) / 1000;
706 if (timestamp) {
707 *timestamp = t;
708 }
709 int64_t position_since_standby;
710 if (in->standby) {
711 position_since_standby = 0;
712 } else {
713 const int64_t first_us = (in->standby_exit_time.tv_sec * 1000000000LL +
714 in->standby_exit_time.tv_nsec) / 1000;
715 position_since_standby = (now_us - first_us) *
716 in_get_sample_rate(&in->stream.common) /
717 1000000;
718 if (position_since_standby < 0) {
719 position_since_standby = 0;
720 }
721 }
722 *position = in->standby_position + position_since_standby;
723 }
724
725 // Must be called with in->lock held
do_in_standby(struct generic_stream_in * in)726 static void do_in_standby(struct generic_stream_in *in) {
727 if (in->standby) {
728 return;
729 }
730 in->worker_standby = true;
731 get_current_input_position(in, &in->standby_position, NULL);
732 in->standby = true;
733 }
734
in_standby(struct audio_stream * stream)735 static int in_standby(struct audio_stream *stream) {
736 struct generic_stream_in *in = (struct generic_stream_in *)stream;
737 pthread_mutex_lock(&in->lock);
738 do_in_standby(in);
739 pthread_mutex_unlock(&in->lock);
740 return 0;
741 }
742
743 #define STEP (3.14159265 / 180)
744 // Generates pure tone for FM_TUNER
pseudo_pcm_read(void * data,unsigned int count)745 static int pseudo_pcm_read(void *data, unsigned int count) {
746 unsigned int length = count / sizeof(short);
747 short *sdata = (short *)data;
748 for (int index = 0; index < length; index++) {
749 sdata[index] = (short)(sin(index * STEP) * 4096);
750 }
751 return count;
752 }
753
in_read_worker(void * args)754 static void *in_read_worker(void *args) {
755 struct generic_stream_in *in = (struct generic_stream_in *)args;
756 struct pcm *pcm = NULL;
757 uint8_t *buffer = NULL;
758 size_t buffer_frames;
759 int buffer_size;
760
761 bool restart = false;
762 bool shutdown = false;
763 while (true) {
764 pthread_mutex_lock(&in->lock);
765 while (in->worker_standby || restart) {
766 restart = false;
767 if (pcm) {
768 pcm_close(pcm); // Frees pcm
769 pcm = NULL;
770 free(buffer);
771 buffer=NULL;
772 }
773 if (in->worker_exit) {
774 break;
775 }
776 pthread_cond_wait(&in->worker_wake, &in->lock);
777 }
778
779 if (in->worker_exit) {
780 if (!in->worker_standby) {
781 ALOGE("In worker not in standby before exiting");
782 }
783 shutdown = true;
784 }
785 if (shutdown) {
786 pthread_mutex_unlock(&in->lock);
787 break;
788 }
789 if (!pcm) {
790 pcm = pcm_open(PCM_CARD, PCM_DEVICE,
791 PCM_IN | PCM_MONOTONIC, &in->pcm_config);
792 if (!pcm_is_ready(pcm)) {
793 ALOGE("pcm_open(in) failed: %s: channels %d format %d rate %d",
794 pcm_get_error(pcm),
795 in->pcm_config.channels,
796 in->pcm_config.format,
797 in->pcm_config.rate);
798 pthread_mutex_unlock(&in->lock);
799 break;
800 }
801 buffer_frames = in->pcm_config.period_size;
802 buffer_size = pcm_frames_to_bytes(pcm, buffer_frames);
803 buffer = malloc(buffer_size);
804 if (!buffer) {
805 ALOGE("could not allocate worker read buffer");
806 pthread_mutex_unlock(&in->lock);
807 break;
808 }
809 }
810 pthread_mutex_unlock(&in->lock);
811 int ret = pcm_read(pcm, buffer, pcm_frames_to_bytes(pcm, buffer_frames));
812 if (ret != 0) {
813 ALOGW("pcm_read failed %s", pcm_get_error(pcm));
814 restart = true;
815 }
816
817 pthread_mutex_lock(&in->lock);
818 size_t frames_written = audio_vbuffer_write(&in->buffer, buffer, buffer_frames);
819 pthread_mutex_unlock(&in->lock);
820
821 if (frames_written != buffer_frames) {
822 ALOGW("in_read_worker only could write %zu / %zu frames",
823 frames_written, buffer_frames);
824 }
825 }
826 if (buffer) {
827 free(buffer);
828 }
829 return NULL;
830 }
831
in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)832 static ssize_t in_read(struct audio_stream_in *stream, void *buffer, size_t bytes) {
833 struct generic_stream_in *in = (struct generic_stream_in *)stream;
834 struct generic_audio_device *adev = in->dev;
835 const size_t frames = bytes / audio_stream_in_frame_size(stream);
836 int ret = 0;
837 bool mic_mute = false;
838 size_t read_bytes = 0;
839
840 adev_get_mic_mute(&adev->device, &mic_mute);
841 pthread_mutex_lock(&in->lock);
842
843 if (in->worker_standby) {
844 in->worker_standby = false;
845 }
846 // FM_TUNER fills the buffer via pseudo_pcm_read directly
847 if (in->device != AUDIO_DEVICE_IN_FM_TUNER) {
848 pthread_cond_signal(&in->worker_wake);
849 }
850
851 int64_t current_position;
852 struct timespec current_time;
853
854 get_current_input_position(in, ¤t_position, ¤t_time);
855 if (in->standby) {
856 in->standby = false;
857 in->standby_exit_time = current_time;
858 in->standby_frames_read = 0;
859 }
860
861 const int64_t frames_available =
862 current_position - in->standby_position - in->standby_frames_read;
863 assert(frames_available >= 0);
864
865 const size_t frames_wait =
866 ((uint64_t)frames_available > frames) ? 0 : frames - frames_available;
867
868 int64_t sleep_time_us = frames_wait * 1000000LL / in_get_sample_rate(&stream->common);
869
870 pthread_mutex_unlock(&in->lock);
871
872 if (sleep_time_us > 0) {
873 usleep(sleep_time_us);
874 }
875
876 pthread_mutex_lock(&in->lock);
877 int read_frames = 0;
878 if (in->standby) {
879 ALOGW("Input put to sleep while read in progress");
880 goto exit;
881 }
882 in->standby_frames_read += frames;
883
884 if (in->device == AUDIO_DEVICE_IN_FM_TUNER) {
885 int read_bytes = pseudo_pcm_read(buffer, bytes);
886 read_frames = read_bytes / audio_stream_in_frame_size(stream);
887 } else if (popcount(in->req_config.channel_mask) == 1 &&
888 in->pcm_config.channels == 2) {
889 // Need to resample to mono
890 if (in->stereo_to_mono_buf_size < bytes*2) {
891 in->stereo_to_mono_buf = realloc(in->stereo_to_mono_buf, bytes*2);
892 if (!in->stereo_to_mono_buf) {
893 ALOGE("Failed to allocate stereo_to_mono_buff");
894 goto exit;
895 }
896 }
897
898 read_frames = audio_vbuffer_read(&in->buffer, in->stereo_to_mono_buf, frames);
899
900 // Currently only pcm 16 is supported.
901 uint16_t *src = (uint16_t *)in->stereo_to_mono_buf;
902 uint16_t *dst = (uint16_t *)buffer;
903 size_t i;
904 // Resample stereo 16 to mono 16 by dropping one channel.
905 // The stereo stream is interleaved L-R-L-R
906 for (i = 0; i < frames; i++) {
907 *dst = *src;
908 src += 2;
909 dst += 1;
910 }
911 } else {
912 read_frames = audio_vbuffer_read(&in->buffer, buffer, frames);
913 }
914
915 exit:
916 read_bytes = read_frames*audio_stream_in_frame_size(stream);
917
918 if (mic_mute) {
919 read_bytes = 0;
920 }
921
922 if (read_bytes < bytes) {
923 memset (&((uint8_t *)buffer)[read_bytes], 0, bytes-read_bytes);
924 }
925
926 pthread_mutex_unlock(&in->lock);
927
928 return bytes;
929 }
930
in_get_input_frames_lost(struct audio_stream_in * stream)931 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) {
932 return 0;
933 }
934
in_get_capture_position(const struct audio_stream_in * stream,int64_t * frames,int64_t * time)935 static int in_get_capture_position(const struct audio_stream_in *stream,
936 int64_t *frames, int64_t *time) {
937 struct generic_stream_in *in = (struct generic_stream_in *)stream;
938 pthread_mutex_lock(&in->lock);
939 struct timespec current_time;
940 get_current_input_position(in, frames, ¤t_time);
941 *time = (current_time.tv_sec * 1000000000LL + current_time.tv_nsec);
942 pthread_mutex_unlock(&in->lock);
943 return 0;
944 }
945
in_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)946 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) {
947 // in_add_audio_effect is a no op
948 return 0;
949 }
950
in_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)951 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) {
952 // in_add_audio_effect is a no op
953 return 0;
954 }
955
adev_open_output_stream(struct audio_hw_device * dev,audio_io_handle_t handle,audio_devices_t devices,audio_output_flags_t flags,struct audio_config * config,struct audio_stream_out ** stream_out,const char * address)956 static int adev_open_output_stream(struct audio_hw_device *dev,
957 audio_io_handle_t handle, audio_devices_t devices, audio_output_flags_t flags,
958 struct audio_config *config, struct audio_stream_out **stream_out, const char *address) {
959 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
960 struct generic_stream_out *out;
961 int ret = 0;
962
963 if (refine_output_parameters(&config->sample_rate, &config->format, &config->channel_mask)) {
964 ALOGE("Error opening output stream format %d, channel_mask %04x, sample_rate %u",
965 config->format, config->channel_mask, config->sample_rate);
966 ret = -EINVAL;
967 goto error;
968 }
969
970 out = (struct generic_stream_out *)calloc(1, sizeof(struct generic_stream_out));
971
972 if (!out)
973 return -ENOMEM;
974
975 out->stream.common.get_sample_rate = out_get_sample_rate;
976 out->stream.common.set_sample_rate = out_set_sample_rate;
977 out->stream.common.get_buffer_size = out_get_buffer_size;
978 out->stream.common.get_channels = out_get_channels;
979 out->stream.common.get_format = out_get_format;
980 out->stream.common.set_format = out_set_format;
981 out->stream.common.standby = out_standby;
982 out->stream.common.dump = out_dump;
983 out->stream.common.set_parameters = out_set_parameters;
984 out->stream.common.get_parameters = out_get_parameters;
985 out->stream.common.add_audio_effect = out_add_audio_effect;
986 out->stream.common.remove_audio_effect = out_remove_audio_effect;
987 out->stream.get_latency = out_get_latency;
988 out->stream.set_volume = out_set_volume;
989 out->stream.write = out_write;
990 out->stream.get_render_position = out_get_render_position;
991 out->stream.get_presentation_position = out_get_presentation_position;
992 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
993
994 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
995 out->dev = adev;
996 out->device = devices;
997 memcpy(&out->req_config, config, sizeof(struct audio_config));
998 memcpy(&out->pcm_config, &pcm_config_out, sizeof(struct pcm_config));
999 out->pcm_config.rate = config->sample_rate;
1000 out->pcm_config.period_size = out->pcm_config.rate*OUT_PERIOD_MS/1000;
1001
1002 out->standby = true;
1003 out->underrun_position = 0;
1004 out->underrun_time.tv_sec = 0;
1005 out->underrun_time.tv_nsec = 0;
1006 out->last_write_time_us = 0;
1007 out->frames_total_buffered = 0;
1008 out->frames_written = 0;
1009 out->frames_rendered = 0;
1010
1011 ret = audio_vbuffer_init(&out->buffer,
1012 out->pcm_config.period_size*out->pcm_config.period_count,
1013 out->pcm_config.channels *
1014 pcm_format_to_bits(out->pcm_config.format) >> 3);
1015 if (ret == 0) {
1016 pthread_cond_init(&out->worker_wake, NULL);
1017 out->worker_standby = true;
1018 out->worker_exit = false;
1019 pthread_create(&out->worker_thread, NULL, out_write_worker, out);
1020 }
1021
1022 out->enabled_channels = BOTH_CHANNELS;
1023 if (address) {
1024 out->bus_address = calloc(strlen(address) + 1, sizeof(char));
1025 strncpy(out->bus_address, address, strlen(address));
1026 hashmapPut(adev->out_bus_stream_map, out->bus_address, out);
1027 /* TODO: read struct audio_gain from audio_policy_configuration */
1028 out->gain_stage = (struct audio_gain) {
1029 .min_value = -3200,
1030 .max_value = 600,
1031 .step_value = 100,
1032 };
1033 out->amplitude_ratio = 1.0;
1034 if (property_get_bool(PROP_KEY_SIMULATE_MULTI_ZONE_AUDIO, false)) {
1035 out->enabled_channels = strstr(out->bus_address, "rear")
1036 ? RIGHT_CHANNEL: LEFT_CHANNEL;
1037 }
1038 }
1039 *stream_out = &out->stream;
1040 ALOGD("%s bus:%s", __func__, out->bus_address);
1041
1042 error:
1043 return ret;
1044 }
1045
adev_close_output_stream(struct audio_hw_device * dev,struct audio_stream_out * stream)1046 static void adev_close_output_stream(struct audio_hw_device *dev,
1047 struct audio_stream_out *stream) {
1048 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1049 struct generic_stream_out *out = (struct generic_stream_out *)stream;
1050 ALOGD("%s bus:%s", __func__, out->bus_address);
1051 pthread_mutex_lock(&out->lock);
1052 do_out_standby(out);
1053
1054 out->worker_exit = true;
1055 pthread_cond_signal(&out->worker_wake);
1056 pthread_mutex_unlock(&out->lock);
1057
1058 pthread_join(out->worker_thread, NULL);
1059 pthread_mutex_destroy(&out->lock);
1060 audio_vbuffer_destroy(&out->buffer);
1061
1062 if (out->bus_address) {
1063 hashmapRemove(adev->out_bus_stream_map, out->bus_address);
1064 free(out->bus_address);
1065 }
1066 free(stream);
1067 }
1068
adev_set_parameters(struct audio_hw_device * dev,const char * kvpairs)1069 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) {
1070 return 0;
1071 }
1072
adev_get_parameters(const struct audio_hw_device * dev,const char * keys)1073 static char *adev_get_parameters(const struct audio_hw_device *dev, const char *keys) {
1074 return NULL;
1075 }
1076
adev_init_check(const struct audio_hw_device * dev)1077 static int adev_init_check(const struct audio_hw_device *dev) {
1078 return 0;
1079 }
1080
adev_set_voice_volume(struct audio_hw_device * dev,float volume)1081 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) {
1082 // adev_set_voice_volume is a no op (simulates phones)
1083 return 0;
1084 }
1085
adev_set_master_volume(struct audio_hw_device * dev,float volume)1086 static int adev_set_master_volume(struct audio_hw_device *dev, float volume) {
1087 return -ENOSYS;
1088 }
1089
adev_get_master_volume(struct audio_hw_device * dev,float * volume)1090 static int adev_get_master_volume(struct audio_hw_device *dev, float *volume) {
1091 return -ENOSYS;
1092 }
1093
adev_set_master_mute(struct audio_hw_device * dev,bool muted)1094 static int adev_set_master_mute(struct audio_hw_device *dev, bool muted) {
1095 ALOGD("%s: %s", __func__, _bool_str(muted));
1096 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1097 pthread_mutex_lock(&adev->lock);
1098 adev->master_mute = muted;
1099 pthread_mutex_unlock(&adev->lock);
1100 return 0;
1101 }
1102
adev_get_master_mute(struct audio_hw_device * dev,bool * muted)1103 static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted) {
1104 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1105 pthread_mutex_lock(&adev->lock);
1106 *muted = adev->master_mute;
1107 pthread_mutex_unlock(&adev->lock);
1108 ALOGD("%s: %s", __func__, _bool_str(*muted));
1109 return 0;
1110 }
1111
adev_set_mode(struct audio_hw_device * dev,audio_mode_t mode)1112 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) {
1113 // adev_set_mode is a no op (simulates phones)
1114 return 0;
1115 }
1116
adev_set_mic_mute(struct audio_hw_device * dev,bool state)1117 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) {
1118 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1119 pthread_mutex_lock(&adev->lock);
1120 adev->mic_mute = state;
1121 pthread_mutex_unlock(&adev->lock);
1122 return 0;
1123 }
1124
adev_get_mic_mute(const struct audio_hw_device * dev,bool * state)1125 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) {
1126 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1127 pthread_mutex_lock(&adev->lock);
1128 *state = adev->mic_mute;
1129 pthread_mutex_unlock(&adev->lock);
1130 return 0;
1131 }
1132
adev_get_input_buffer_size(const struct audio_hw_device * dev,const struct audio_config * config)1133 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1134 const struct audio_config *config) {
1135 return get_input_buffer_size(config->sample_rate, config->format, config->channel_mask);
1136 }
1137
adev_close_input_stream(struct audio_hw_device * dev,struct audio_stream_in * stream)1138 static void adev_close_input_stream(struct audio_hw_device *dev,
1139 struct audio_stream_in *stream) {
1140 struct generic_stream_in *in = (struct generic_stream_in *)stream;
1141 pthread_mutex_lock(&in->lock);
1142 do_in_standby(in);
1143
1144 in->worker_exit = true;
1145 pthread_cond_signal(&in->worker_wake);
1146 pthread_mutex_unlock(&in->lock);
1147 pthread_join(in->worker_thread, NULL);
1148
1149 if (in->stereo_to_mono_buf != NULL) {
1150 free(in->stereo_to_mono_buf);
1151 in->stereo_to_mono_buf_size = 0;
1152 }
1153
1154 if (in->bus_address) {
1155 free(in->bus_address);
1156 }
1157
1158 pthread_mutex_destroy(&in->lock);
1159 audio_vbuffer_destroy(&in->buffer);
1160 free(stream);
1161 }
1162
adev_open_input_stream(struct audio_hw_device * dev,audio_io_handle_t handle,audio_devices_t devices,struct audio_config * config,struct audio_stream_in ** stream_in,audio_input_flags_t flags __unused,const char * address,audio_source_t source)1163 static int adev_open_input_stream(struct audio_hw_device *dev,
1164 audio_io_handle_t handle, audio_devices_t devices, struct audio_config *config,
1165 struct audio_stream_in **stream_in, audio_input_flags_t flags __unused, const char *address,
1166 audio_source_t source) {
1167 ALOGV("%s: audio_source_t: %d", __func__, source);
1168 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1169 struct generic_stream_in *in;
1170 int ret = 0;
1171 if (refine_input_parameters(&config->sample_rate, &config->format, &config->channel_mask)) {
1172 ALOGE("Error opening input stream format %d, channel_mask %04x, sample_rate %u",
1173 config->format, config->channel_mask, config->sample_rate);
1174 ret = -EINVAL;
1175 goto error;
1176 }
1177
1178 in = (struct generic_stream_in *)calloc(1, sizeof(struct generic_stream_in));
1179 if (!in) {
1180 ret = -ENOMEM;
1181 goto error;
1182 }
1183
1184 in->stream.common.get_sample_rate = in_get_sample_rate;
1185 in->stream.common.set_sample_rate = in_set_sample_rate; // no op
1186 in->stream.common.get_buffer_size = in_get_buffer_size;
1187 in->stream.common.get_channels = in_get_channels;
1188 in->stream.common.get_format = in_get_format;
1189 in->stream.common.set_format = in_set_format; // no op
1190 in->stream.common.standby = in_standby;
1191 in->stream.common.dump = in_dump;
1192 in->stream.common.set_parameters = in_set_parameters;
1193 in->stream.common.get_parameters = in_get_parameters;
1194 in->stream.common.add_audio_effect = in_add_audio_effect; // no op
1195 in->stream.common.remove_audio_effect = in_remove_audio_effect; // no op
1196 in->stream.set_gain = in_set_gain; // no op
1197 in->stream.read = in_read;
1198 in->stream.get_input_frames_lost = in_get_input_frames_lost; // no op
1199 in->stream.get_capture_position = in_get_capture_position;
1200
1201 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
1202 in->dev = adev;
1203 in->device = devices;
1204 memcpy(&in->req_config, config, sizeof(struct audio_config));
1205 memcpy(&in->pcm_config, &pcm_config_in, sizeof(struct pcm_config));
1206 in->pcm_config.rate = config->sample_rate;
1207 in->pcm_config.period_size = in->pcm_config.rate*IN_PERIOD_MS/1000;
1208
1209 in->stereo_to_mono_buf = NULL;
1210 in->stereo_to_mono_buf_size = 0;
1211
1212 in->standby = true;
1213 in->standby_position = 0;
1214 in->standby_exit_time.tv_sec = 0;
1215 in->standby_exit_time.tv_nsec = 0;
1216 in->standby_frames_read = 0;
1217
1218 ret = audio_vbuffer_init(&in->buffer,
1219 in->pcm_config.period_size*in->pcm_config.period_count,
1220 in->pcm_config.channels *
1221 pcm_format_to_bits(in->pcm_config.format) >> 3);
1222 if (ret == 0) {
1223 pthread_cond_init(&in->worker_wake, NULL);
1224 in->worker_standby = true;
1225 in->worker_exit = false;
1226 pthread_create(&in->worker_thread, NULL, in_read_worker, in);
1227 }
1228
1229 if (address) {
1230 in->bus_address = calloc(strlen(address) + 1, sizeof(char));
1231 strncpy(in->bus_address, address, strlen(address));
1232 }
1233
1234 *stream_in = &in->stream;
1235
1236 error:
1237 return ret;
1238 }
1239
adev_dump(const audio_hw_device_t * dev,int fd)1240 static int adev_dump(const audio_hw_device_t *dev, int fd) {
1241 return 0;
1242 }
1243
adev_set_audio_port_config(struct audio_hw_device * dev,const struct audio_port_config * config)1244 static int adev_set_audio_port_config(struct audio_hw_device *dev,
1245 const struct audio_port_config *config) {
1246 int ret = 0;
1247 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1248 const char *bus_address = config->ext.device.address;
1249 struct generic_stream_out *out = hashmapGet(adev->out_bus_stream_map, bus_address);
1250 if (out) {
1251 pthread_mutex_lock(&out->lock);
1252 int gainIndex = (config->gain.values[0] - out->gain_stage.min_value) /
1253 out->gain_stage.step_value;
1254 int totalSteps = (out->gain_stage.max_value - out->gain_stage.min_value) /
1255 out->gain_stage.step_value;
1256 int minDb = out->gain_stage.min_value / 100;
1257 int maxDb = out->gain_stage.max_value / 100;
1258 // curve: 10^((minDb + (maxDb - minDb) * gainIndex / totalSteps) / 20)
1259 out->amplitude_ratio = pow(10,
1260 (minDb + (maxDb - minDb) * (gainIndex / (float)totalSteps)) / 20);
1261 pthread_mutex_unlock(&out->lock);
1262 ALOGD("%s: set audio gain: %f on %s",
1263 __func__, out->amplitude_ratio, bus_address);
1264 } else {
1265 ALOGE("%s: can not find output stream by bus_address:%s", __func__, bus_address);
1266 ret = -EINVAL;
1267 }
1268 return ret;
1269 }
1270
adev_create_audio_patch(struct audio_hw_device * dev,unsigned int num_sources,const struct audio_port_config * sources,unsigned int num_sinks,const struct audio_port_config * sinks,audio_patch_handle_t * handle)1271 static int adev_create_audio_patch(struct audio_hw_device *dev,
1272 unsigned int num_sources,
1273 const struct audio_port_config *sources,
1274 unsigned int num_sinks,
1275 const struct audio_port_config *sinks,
1276 audio_patch_handle_t *handle) {
1277 struct generic_audio_device *audio_dev = (struct generic_audio_device *)dev;
1278 for (int i = 0; i < num_sources; i++) {
1279 ALOGD("%s: source[%d] type=%d address=%s", __func__, i, sources[i].type,
1280 sources[i].type == AUDIO_PORT_TYPE_DEVICE
1281 ? sources[i].ext.device.address
1282 : "");
1283 }
1284 for (int i = 0; i < num_sinks; i++) {
1285 ALOGD("%s: sink[%d] type=%d address=%s", __func__, i, sinks[i].type,
1286 sinks[i].type == AUDIO_PORT_TYPE_DEVICE ? sinks[i].ext.device.address
1287 : "N/A");
1288 }
1289 if (num_sources == 1 && num_sinks == 1 &&
1290 sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
1291 sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
1292 pthread_mutex_lock(&audio_dev->lock);
1293 audio_dev->last_patch_id += 1;
1294 pthread_mutex_unlock(&audio_dev->lock);
1295 *handle = audio_dev->last_patch_id;
1296 ALOGD("%s: handle: %d", __func__, *handle);
1297 }
1298 return 0;
1299 }
1300
adev_release_audio_patch(struct audio_hw_device * dev,audio_patch_handle_t handle)1301 static int adev_release_audio_patch(struct audio_hw_device *dev,
1302 audio_patch_handle_t handle) {
1303 ALOGD("%s: handle: %d", __func__, handle);
1304 return 0;
1305 }
1306
adev_close(hw_device_t * dev)1307 static int adev_close(hw_device_t *dev) {
1308 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1309 int ret = 0;
1310 if (!adev)
1311 return 0;
1312
1313 pthread_mutex_lock(&adev_init_lock);
1314
1315 if (audio_device_ref_count == 0) {
1316 ALOGE("adev_close called when ref_count 0");
1317 ret = -EINVAL;
1318 goto error;
1319 }
1320
1321 if ((--audio_device_ref_count) == 0) {
1322 if (adev->mixer) {
1323 mixer_close(adev->mixer);
1324 }
1325 if (adev->out_bus_stream_map) {
1326 hashmapFree(adev->out_bus_stream_map);
1327 }
1328 free(adev);
1329 }
1330
1331 error:
1332 pthread_mutex_unlock(&adev_init_lock);
1333 return ret;
1334 }
1335
1336 /* copied from libcutils/str_parms.c */
str_eq(void * key_a,void * key_b)1337 static bool str_eq(void *key_a, void *key_b) {
1338 return !strcmp((const char *)key_a, (const char *)key_b);
1339 }
1340
1341 /**
1342 * use djb hash unless we find it inadequate.
1343 * copied from libcutils/str_parms.c
1344 */
1345 #ifdef __clang__
1346 __attribute__((no_sanitize("integer")))
1347 #endif
str_hash_fn(void * str)1348 static int str_hash_fn(void *str) {
1349 uint32_t hash = 5381;
1350 char *p;
1351 for (p = str; p && *p; p++) {
1352 hash = ((hash << 5) + hash) + *p;
1353 }
1354 return (int)hash;
1355 }
1356
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)1357 static int adev_open(const hw_module_t *module,
1358 const char *name, hw_device_t **device) {
1359 static struct generic_audio_device *adev;
1360
1361 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1362 return -EINVAL;
1363
1364 pthread_mutex_lock(&adev_init_lock);
1365 if (audio_device_ref_count != 0) {
1366 *device = &adev->device.common;
1367 audio_device_ref_count++;
1368 ALOGV("%s: returning existing instance of adev", __func__);
1369 ALOGV("%s: exit", __func__);
1370 goto unlock;
1371 }
1372 adev = calloc(1, sizeof(struct generic_audio_device));
1373
1374 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
1375
1376 adev->device.common.tag = HARDWARE_DEVICE_TAG;
1377 adev->device.common.version = AUDIO_DEVICE_API_VERSION_3_0;
1378 adev->device.common.module = (struct hw_module_t *) module;
1379 adev->device.common.close = adev_close;
1380
1381 adev->device.init_check = adev_init_check; // no op
1382 adev->device.set_voice_volume = adev_set_voice_volume; // no op
1383 adev->device.set_master_volume = adev_set_master_volume; // no op
1384 adev->device.get_master_volume = adev_get_master_volume; // no op
1385 adev->device.set_master_mute = adev_set_master_mute;
1386 adev->device.get_master_mute = adev_get_master_mute;
1387 adev->device.set_mode = adev_set_mode; // no op
1388 adev->device.set_mic_mute = adev_set_mic_mute;
1389 adev->device.get_mic_mute = adev_get_mic_mute;
1390 adev->device.set_parameters = adev_set_parameters; // no op
1391 adev->device.get_parameters = adev_get_parameters; // no op
1392 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
1393 adev->device.open_output_stream = adev_open_output_stream;
1394 adev->device.close_output_stream = adev_close_output_stream;
1395 adev->device.open_input_stream = adev_open_input_stream;
1396 adev->device.close_input_stream = adev_close_input_stream;
1397 adev->device.dump = adev_dump;
1398
1399 // New in AUDIO_DEVICE_API_VERSION_3_0
1400 adev->device.set_audio_port_config = adev_set_audio_port_config;
1401 adev->device.create_audio_patch = adev_create_audio_patch;
1402 adev->device.release_audio_patch = adev_release_audio_patch;
1403
1404 *device = &adev->device.common;
1405
1406 adev->mixer = mixer_open(PCM_CARD);
1407 struct mixer_ctl *ctl;
1408
1409 // Set default mixer ctls
1410 // Enable channels and set volume
1411 for (int i = 0; i < (int)mixer_get_num_ctls(adev->mixer); i++) {
1412 ctl = mixer_get_ctl(adev->mixer, i);
1413 ALOGD("mixer %d name %s", i, mixer_ctl_get_name(ctl));
1414 if (!strcmp(mixer_ctl_get_name(ctl), "Master Playback Volume") ||
1415 !strcmp(mixer_ctl_get_name(ctl), "Capture Volume")) {
1416 for (int z = 0; z < (int)mixer_ctl_get_num_values(ctl); z++) {
1417 ALOGD("set ctl %d to %d", z, 100);
1418 mixer_ctl_set_percent(ctl, z, 100);
1419 }
1420 continue;
1421 }
1422 if (!strcmp(mixer_ctl_get_name(ctl), "Master Playback Switch") ||
1423 !strcmp(mixer_ctl_get_name(ctl), "Capture Switch")) {
1424 for (int z = 0; z < (int)mixer_ctl_get_num_values(ctl); z++) {
1425 ALOGD("set ctl %d to %d", z, 1);
1426 mixer_ctl_set_value(ctl, z, 1);
1427 }
1428 continue;
1429 }
1430 }
1431
1432 // Initialize the bus address to output stream map
1433 adev->out_bus_stream_map = hashmapCreate(5, str_hash_fn, str_eq);
1434
1435 audio_device_ref_count++;
1436
1437 unlock:
1438 pthread_mutex_unlock(&adev_init_lock);
1439 return 0;
1440 }
1441
1442 static struct hw_module_methods_t hal_module_methods = {
1443 .open = adev_open,
1444 };
1445
1446 struct audio_module HAL_MODULE_INFO_SYM = {
1447 .common = {
1448 .tag = HARDWARE_MODULE_TAG,
1449 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1450 .hal_api_version = HARDWARE_HAL_API_VERSION,
1451 .id = AUDIO_HARDWARE_MODULE_ID,
1452 .name = "Generic car audio HW HAL",
1453 .author = "The Android Open Source Project",
1454 .methods = &hal_module_methods,
1455 },
1456 };
1457