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