• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.aae.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, &current_position, &current_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             ALOGW("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 
in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)1040 static ssize_t in_read(struct audio_stream_in *stream, void *buffer, size_t bytes) {
1041     struct generic_stream_in *in = (struct generic_stream_in *)stream;
1042     struct generic_audio_device *adev = in->dev;
1043     const size_t frames =  bytes / audio_stream_in_frame_size(stream);
1044     int ret = 0;
1045     bool mic_mute = false;
1046     size_t read_bytes = 0;
1047 
1048     set_shortened_thread_name(pthread_self(), __func__);
1049 
1050     adev_get_mic_mute(&adev->device, &mic_mute);
1051     pthread_mutex_lock(&in->lock);
1052 
1053     if (in->worker_standby) {
1054         in->worker_standby = false;
1055     }
1056 
1057     // Tone generators fill the buffer via pseudo_pcm_read directly
1058     if (!is_tone_generator_device(in)) {
1059         pthread_cond_signal(&in->worker_wake);
1060     }
1061 
1062     int64_t current_position;
1063     struct timespec current_time;
1064 
1065     get_current_input_position(in, &current_position, &current_time);
1066     if (in->standby) {
1067         in->standby = false;
1068         in->standby_exit_time = current_time;
1069         in->standby_frames_read = 0;
1070     }
1071 
1072     const int64_t frames_available =
1073         current_position - in->standby_position - in->standby_frames_read;
1074     assert(frames_available >= 0);
1075 
1076     const size_t frames_wait =
1077         ((uint64_t)frames_available > frames) ? 0 : frames - frames_available;
1078 
1079     int64_t sleep_time_us  = frames_wait * 1000000LL / in_get_sample_rate(&stream->common);
1080 
1081     pthread_mutex_unlock(&in->lock);
1082 
1083     if (sleep_time_us > 0) {
1084         usleep(sleep_time_us);
1085     }
1086 
1087     pthread_mutex_lock(&in->lock);
1088     int read_frames = 0;
1089     if (in->standby) {
1090         ALOGW("Input put to sleep while read in progress");
1091         goto exit;
1092     }
1093     in->standby_frames_read += frames;
1094 
1095     if (is_tone_generator_device(in)) {
1096         int read_bytes = pseudo_pcm_read(buffer, bytes, &in->oscillator);
1097         read_frames = read_bytes / audio_stream_in_frame_size(stream);
1098     } else if (popcount(in->req_config.channel_mask) == 1 &&
1099         in->pcm_config.channels == 2) {
1100         // Need to resample to mono
1101         if (in->stereo_to_mono_buf_size < bytes*2) {
1102             in->stereo_to_mono_buf = realloc(in->stereo_to_mono_buf, bytes*2);
1103             if (!in->stereo_to_mono_buf) {
1104                 ALOGE("Failed to allocate stereo_to_mono_buff");
1105                 goto exit;
1106             }
1107         }
1108 
1109         read_frames = audio_vbuffer_read(&in->buffer, in->stereo_to_mono_buf, frames);
1110 
1111         // Currently only pcm 16 is supported.
1112         uint16_t *src = (uint16_t *)in->stereo_to_mono_buf;
1113         uint16_t *dst = (uint16_t *)buffer;
1114         size_t i;
1115         // Resample stereo 16 to mono 16 by dropping one channel.
1116         // The stereo stream is interleaved L-R-L-R
1117         for (i = 0; i < frames; i++) {
1118             *dst = *src;
1119             src += 2;
1120             dst += 1;
1121         }
1122     } else {
1123         read_frames = audio_vbuffer_read(&in->buffer, buffer, frames);
1124     }
1125 
1126 exit:
1127     read_bytes = read_frames*audio_stream_in_frame_size(stream);
1128 
1129     if (mic_mute) {
1130         read_bytes = 0;
1131     }
1132 
1133     if (read_bytes < bytes) {
1134         memset (&((uint8_t *)buffer)[read_bytes], 0, bytes-read_bytes);
1135     }
1136 
1137     pthread_mutex_unlock(&in->lock);
1138 
1139     return bytes;
1140 }
1141 
in_get_input_frames_lost(struct audio_stream_in * stream)1142 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) {
1143     return 0;
1144 }
1145 
in_get_capture_position(const struct audio_stream_in * stream,int64_t * frames,int64_t * time)1146 static int in_get_capture_position(const struct audio_stream_in *stream,
1147         int64_t *frames, int64_t *time) {
1148     struct generic_stream_in *in = (struct generic_stream_in *)stream;
1149     pthread_mutex_lock(&in->lock);
1150     struct timespec current_time;
1151     get_current_input_position(in, frames, &current_time);
1152     *time = (current_time.tv_sec * 1000000000LL + current_time.tv_nsec);
1153     pthread_mutex_unlock(&in->lock);
1154     return 0;
1155 }
1156 
in_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)1157 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) {
1158     // in_add_audio_effect is a no op
1159     return 0;
1160 }
1161 
in_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)1162 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) {
1163     // in_add_audio_effect is a no op
1164     return 0;
1165 }
1166 
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)1167 static int adev_open_output_stream(struct audio_hw_device *dev,
1168         audio_io_handle_t handle, audio_devices_t devices, audio_output_flags_t flags,
1169         struct audio_config *config, struct audio_stream_out **stream_out, const char *address) {
1170     struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1171     struct generic_stream_out *out;
1172     int ret = 0;
1173 
1174     if (refine_output_parameters(&config->sample_rate, &config->format, &config->channel_mask)) {
1175         ALOGE("Error opening output stream format %d, channel_mask %04x, sample_rate %u",
1176               config->format, config->channel_mask, config->sample_rate);
1177         ret = -EINVAL;
1178         goto error;
1179     }
1180 
1181     out = (struct generic_stream_out *)calloc(1, sizeof(struct generic_stream_out));
1182 
1183     if (!out)
1184         return -ENOMEM;
1185 
1186     out->stream.common.get_sample_rate = out_get_sample_rate;
1187     out->stream.common.set_sample_rate = out_set_sample_rate;
1188     out->stream.common.get_buffer_size = out_get_buffer_size;
1189     out->stream.common.get_channels = out_get_channels;
1190     out->stream.common.get_format = out_get_format;
1191     out->stream.common.set_format = out_set_format;
1192     out->stream.common.standby = out_standby;
1193     out->stream.common.dump = out_dump;
1194     out->stream.common.set_parameters = out_set_parameters;
1195     out->stream.common.get_parameters = out_get_parameters;
1196     out->stream.common.add_audio_effect = out_add_audio_effect;
1197     out->stream.common.remove_audio_effect = out_remove_audio_effect;
1198     out->stream.get_latency = out_get_latency;
1199     out->stream.set_volume = out_set_volume;
1200     out->stream.write = out_write;
1201     out->stream.get_render_position = out_get_render_position;
1202     out->stream.get_presentation_position = out_get_presentation_position;
1203     out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1204 
1205     pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
1206     out->dev = adev;
1207     out->device = devices;
1208     memcpy(&out->req_config, config, sizeof(struct audio_config));
1209     memcpy(&out->pcm_config, &pcm_config_out, sizeof(struct pcm_config));
1210     out->pcm_config.rate = config->sample_rate;
1211     out->pcm_config.period_size = out->pcm_config.rate * get_out_period_ms() / 1000;
1212 
1213     out->standby = true;
1214     out->underrun_position = 0;
1215     out->underrun_time.tv_sec = 0;
1216     out->underrun_time.tv_nsec = 0;
1217     out->last_write_time_us = 0;
1218     out->frames_total_buffered = 0;
1219     out->frames_written = 0;
1220     out->frames_rendered = 0;
1221 
1222     ret = audio_vbuffer_init(&out->buffer,
1223             out->pcm_config.period_size*out->pcm_config.period_count,
1224             out->pcm_config.channels *
1225             pcm_format_to_bits(out->pcm_config.format) >> 3);
1226     if (ret == 0) {
1227         pthread_cond_init(&out->worker_wake, NULL);
1228         out->worker_standby = true;
1229         out->worker_exit = false;
1230 
1231         out->enabled_channels = BOTH_CHANNELS;
1232         // For targets where output streams are closed regularly, currently ducked/muted addresses
1233         // should be tracked so that the address of new streams can be checked to determine the
1234         // default state
1235         out->is_ducked = 0;
1236         out->is_muted = 0;
1237         if (address) {
1238             out->bus_address = calloc(strlen(address) + 1, sizeof(char));
1239             strncpy(out->bus_address, address, strlen(address));
1240             hashmapPut(adev->out_bus_stream_map, out->bus_address, out);
1241             /* TODO: read struct audio_gain from audio_policy_configuration */
1242             out->gain_stage = (struct audio_gain) {
1243                 .min_value = -3200,
1244                 .max_value = 600,
1245                 .step_value = 100,
1246             };
1247             out->amplitude_ratio = 1.0;
1248             if (property_get_bool(PROP_KEY_SIMULATE_MULTI_ZONE_AUDIO, false)) {
1249                 out->enabled_channels = strstr(out->bus_address, AUDIO_ZONE_KEYWORD)
1250                     ? RIGHT_CHANNEL: LEFT_CHANNEL;
1251                 ALOGD("%s Routing %s to %s channel", __func__,
1252                  out->bus_address, out->enabled_channels == RIGHT_CHANNEL ? "Right" : "Left");
1253             }
1254         }
1255         pthread_create(&out->worker_thread, NULL, out_write_worker, out);
1256         set_shortened_thread_name(out->worker_thread, address);
1257         *stream_out = &out->stream;
1258         ALOGD("%s bus: %s", __func__, out->bus_address);
1259     }
1260 
1261 error:
1262     return ret;
1263 }
1264 
adev_close_output_stream(struct audio_hw_device * dev,struct audio_stream_out * stream)1265 static void adev_close_output_stream(struct audio_hw_device *dev,
1266         struct audio_stream_out *stream) {
1267     struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1268     struct generic_stream_out *out = (struct generic_stream_out *)stream;
1269     ALOGD("%s bus:%s", __func__, out->bus_address);
1270     pthread_mutex_lock(&out->lock);
1271     do_out_standby(out);
1272 
1273     out->worker_exit = true;
1274     pthread_cond_signal(&out->worker_wake);
1275     pthread_mutex_unlock(&out->lock);
1276 
1277     pthread_join(out->worker_thread, NULL);
1278     pthread_mutex_destroy(&out->lock);
1279     audio_vbuffer_destroy(&out->buffer);
1280 
1281     if (out->bus_address) {
1282         hashmapRemove(adev->out_bus_stream_map, out->bus_address);
1283         free(out->bus_address);
1284     }
1285     free(stream);
1286 }
1287 
adev_set_parameters(struct audio_hw_device * dev,const char * kvpairs)1288 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) {
1289     struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1290     pthread_mutex_lock(&adev->lock);
1291     struct str_parms *parms = str_parms_create_str(kvpairs);
1292     int value = 0;
1293     int results = get_int_value(parms, AAE_PARAMETER_KEY_FOR_SELECTED_ZONE, &value);
1294     if (results >= 0) {
1295         adev->last_zone_selected_to_play = value;
1296         results = 0;
1297         ALOGD("%s Changed play zone id to %d", __func__, adev->last_zone_selected_to_play);
1298     }
1299     results = audio_extn_hfp_set_parameters(adev, parms);
1300     str_parms_destroy(parms);
1301     pthread_mutex_unlock(&adev->lock);
1302     return results;
1303 }
1304 
adev_get_parameters(const struct audio_hw_device * dev,const char * keys)1305 static char *adev_get_parameters(const struct audio_hw_device * dev, const char *keys) {
1306     return NULL;
1307 }
1308 
adev_init_check(const struct audio_hw_device * dev)1309 static int adev_init_check(const struct audio_hw_device *dev) {
1310     return 0;
1311 }
1312 
adev_set_voice_volume(struct audio_hw_device * dev,float volume)1313 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) {
1314     // adev_set_voice_volume is a no op (simulates phones)
1315     return 0;
1316 }
1317 
adev_set_master_volume(struct audio_hw_device * dev,float volume)1318 static int adev_set_master_volume(struct audio_hw_device *dev, float volume) {
1319     return -ENOSYS;
1320 }
1321 
adev_get_master_volume(struct audio_hw_device * dev,float * volume)1322 static int adev_get_master_volume(struct audio_hw_device *dev, float *volume) {
1323     return -ENOSYS;
1324 }
1325 
adev_set_master_mute(struct audio_hw_device * dev,bool muted)1326 static int adev_set_master_mute(struct audio_hw_device *dev, bool muted) {
1327     ALOGD("%s: %s", __func__, _bool_str(muted));
1328     struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1329     pthread_mutex_lock(&adev->lock);
1330     adev->master_mute = muted;
1331     pthread_mutex_unlock(&adev->lock);
1332     return 0;
1333 }
1334 
adev_get_master_mute(struct audio_hw_device * dev,bool * muted)1335 static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted) {
1336     struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1337     pthread_mutex_lock(&adev->lock);
1338     *muted = adev->master_mute;
1339     pthread_mutex_unlock(&adev->lock);
1340     ALOGD("%s: %s", __func__, _bool_str(*muted));
1341     return 0;
1342 }
1343 
adev_set_mode(struct audio_hw_device * dev,audio_mode_t mode)1344 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) {
1345     // adev_set_mode is a no op (simulates phones)
1346     return 0;
1347 }
1348 
adev_set_mic_mute(struct audio_hw_device * dev,bool state)1349 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) {
1350     struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1351     pthread_mutex_lock(&adev->lock);
1352     adev->mic_mute = state;
1353     pthread_mutex_unlock(&adev->lock);
1354     return 0;
1355 }
1356 
adev_get_mic_mute(const struct audio_hw_device * dev,bool * state)1357 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) {
1358     struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1359     pthread_mutex_lock(&adev->lock);
1360     *state = adev->mic_mute;
1361     pthread_mutex_unlock(&adev->lock);
1362     return 0;
1363 }
1364 
adev_get_input_buffer_size(const struct audio_hw_device * dev,const struct audio_config * config)1365 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1366         const struct audio_config *config) {
1367     return get_input_buffer_size(config->sample_rate, config->format, config->channel_mask);
1368 }
1369 
adev_close_input_stream(struct audio_hw_device * dev,struct audio_stream_in * stream)1370 static void adev_close_input_stream(struct audio_hw_device *dev,
1371         struct audio_stream_in *stream) {
1372     struct generic_stream_in *in = (struct generic_stream_in *)stream;
1373     pthread_mutex_lock(&in->lock);
1374     do_in_standby(in);
1375 
1376     in->worker_exit = true;
1377     pthread_cond_signal(&in->worker_wake);
1378     pthread_mutex_unlock(&in->lock);
1379     pthread_join(in->worker_thread, NULL);
1380 
1381     if (in->stereo_to_mono_buf != NULL) {
1382         free(in->stereo_to_mono_buf);
1383         in->stereo_to_mono_buf_size = 0;
1384     }
1385 
1386     if (in->bus_address) {
1387         free(in->bus_address);
1388     }
1389 
1390     pthread_mutex_destroy(&in->lock);
1391     audio_vbuffer_destroy(&in->buffer);
1392     free(stream);
1393 }
1394 
increase_next_tone_frequency(struct generic_audio_device * adev)1395 static void increase_next_tone_frequency(struct generic_audio_device *adev) {
1396     adev->next_tone_frequency_to_assign += TONE_FREQUENCY_INCREASE;
1397     if (adev->next_tone_frequency_to_assign > MAX_TONE_FREQUENCY) {
1398         adev->next_tone_frequency_to_assign = DEFAULT_FREQUENCY;
1399     }
1400 }
1401 
create_or_fetch_tone_frequency(struct generic_audio_device * adev,char * address,int update_frequency)1402 static int create_or_fetch_tone_frequency(struct generic_audio_device *adev,
1403         char *address, int update_frequency) {
1404     int *frequency = hashmapGet(adev->in_bus_tone_frequency_map, address);
1405     if (frequency == NULL) {
1406         frequency = calloc(1, sizeof(int));
1407         *frequency = update_frequency;
1408         hashmapPut(adev->in_bus_tone_frequency_map, strdup(address), frequency);
1409         ALOGD("%s assigned frequency %d to %s", __func__, *frequency, address);
1410     }
1411     return *frequency;
1412 }
1413 
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)1414 static int adev_open_input_stream(struct audio_hw_device *dev,
1415         audio_io_handle_t handle, audio_devices_t devices, struct audio_config *config,
1416         struct audio_stream_in **stream_in, audio_input_flags_t flags __unused, const char *address,
1417         audio_source_t source) {
1418     ALOGV("%s: audio_source_t: %d", __func__, source);
1419     struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1420     struct generic_stream_in *in;
1421     int ret = 0;
1422     if (refine_input_parameters(&config->sample_rate, &config->format, &config->channel_mask)) {
1423         ALOGE("Error opening input stream format %d, channel_mask %04x, sample_rate %u",
1424               config->format, config->channel_mask, config->sample_rate);
1425         ret = -EINVAL;
1426         goto error;
1427     }
1428 
1429     in = (struct generic_stream_in *)calloc(1, sizeof(struct generic_stream_in));
1430     if (!in) {
1431         ret = -ENOMEM;
1432         goto error;
1433     }
1434 
1435     in->stream.common.get_sample_rate = in_get_sample_rate;
1436     in->stream.common.set_sample_rate = in_set_sample_rate;         // no op
1437     in->stream.common.get_buffer_size = in_get_buffer_size;
1438     in->stream.common.get_channels = in_get_channels;
1439     in->stream.common.get_format = in_get_format;
1440     in->stream.common.set_format = in_set_format;                   // no op
1441     in->stream.common.standby = in_standby;
1442     in->stream.common.dump = in_dump;
1443     in->stream.common.set_parameters = in_set_parameters;
1444     in->stream.common.get_parameters = in_get_parameters;
1445     in->stream.common.add_audio_effect = in_add_audio_effect;       // no op
1446     in->stream.common.remove_audio_effect = in_remove_audio_effect; // no op
1447     in->stream.set_gain = in_set_gain;                              // no op
1448     in->stream.read = in_read;
1449     in->stream.get_input_frames_lost = in_get_input_frames_lost;    // no op
1450     in->stream.get_capture_position = in_get_capture_position;
1451 
1452     pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
1453     in->dev = adev;
1454     in->device = devices;
1455     memcpy(&in->req_config, config, sizeof(struct audio_config));
1456     memcpy(&in->pcm_config, &pcm_config_in, sizeof(struct pcm_config));
1457     in->pcm_config.rate = config->sample_rate;
1458     in->pcm_config.period_size = in->pcm_config.rate * get_in_period_ms() / 1000;
1459 
1460     in->stereo_to_mono_buf = NULL;
1461     in->stereo_to_mono_buf_size = 0;
1462 
1463     in->standby = true;
1464     in->standby_position = 0;
1465     in->standby_exit_time.tv_sec = 0;
1466     in->standby_exit_time.tv_nsec = 0;
1467     in->standby_frames_read = 0;
1468 
1469     ret = audio_vbuffer_init(&in->buffer,
1470             in->pcm_config.period_size*in->pcm_config.period_count,
1471             in->pcm_config.channels *
1472             pcm_format_to_bits(in->pcm_config.format) >> 3);
1473     if (ret == 0) {
1474         pthread_cond_init(&in->worker_wake, NULL);
1475         in->worker_standby = true;
1476         in->worker_exit = false;
1477         pthread_create(&in->worker_thread, NULL, in_read_worker, in);
1478         set_shortened_thread_name(in->worker_thread, address ? address : "mic");
1479     }
1480 
1481     if (address) {
1482         in->bus_address = strdup(address);
1483         if (is_tone_generator_device(in)) {
1484             int update_frequency = adev->next_tone_frequency_to_assign;
1485             int frequency = create_or_fetch_tone_frequency(adev, address, update_frequency);
1486             if (update_frequency == frequency) {
1487                 increase_next_tone_frequency(adev);
1488             }
1489             in->oscillator.phase = 0.0f;
1490             in->oscillator.phase_increment = (TWO_PI*(frequency))
1491                 / ((float) in_get_sample_rate(&in->stream.common));
1492         }
1493     }
1494 
1495     *stream_in = &in->stream;
1496 
1497 error:
1498     return ret;
1499 }
1500 
adev_dump(const audio_hw_device_t * dev,int fd)1501 static int adev_dump(const audio_hw_device_t *dev, int fd) {
1502     return 0;
1503 }
1504 
adev_set_audio_port_config(struct audio_hw_device * dev,const struct audio_port_config * config)1505 static int adev_set_audio_port_config(struct audio_hw_device *dev,
1506         const struct audio_port_config *config) {
1507     int ret = 0;
1508     struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1509     const char *bus_address = config->ext.device.address;
1510     struct generic_stream_out *out = hashmapGet(adev->out_bus_stream_map, bus_address);
1511     if (out) {
1512         pthread_mutex_lock(&out->lock);
1513         int gainIndex = (config->gain.values[0] - out->gain_stage.min_value) /
1514             out->gain_stage.step_value;
1515         int totalSteps = (out->gain_stage.max_value - out->gain_stage.min_value) /
1516             out->gain_stage.step_value;
1517         int minDb = out->gain_stage.min_value / 100;
1518         int maxDb = out->gain_stage.max_value / 100;
1519         // curve: 10^((minDb + (maxDb - minDb) * gainIndex / totalSteps) / 20)
1520         // 2x10, where 10 comes from the log 10 conversion from power ratios,
1521         // which are the square (2) of amplitude
1522         out->amplitude_ratio = pow(10,
1523                 (minDb + (maxDb - minDb) * (gainIndex / (float)totalSteps)) / 20);
1524         pthread_mutex_unlock(&out->lock);
1525         ALOGD("%s: set audio gain: %f on %s",
1526                 __func__, out->amplitude_ratio, bus_address);
1527     } else {
1528         ALOGE("%s: can not find output stream by bus_address:%s", __func__, bus_address);
1529         ret = -EINVAL;
1530     }
1531     return ret;
1532 }
1533 
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)1534 static int adev_create_audio_patch(struct audio_hw_device *dev,
1535         unsigned int num_sources,
1536         const struct audio_port_config *sources,
1537         unsigned int num_sinks,
1538         const struct audio_port_config *sinks,
1539         audio_patch_handle_t *handle) {
1540     struct generic_audio_device *audio_dev = (struct generic_audio_device *)dev;
1541     for (int i = 0; i < num_sources; i++) {
1542         ALOGD("%s: source[%d] type=%d address=%s", __func__, i, sources[i].type,
1543                 sources[i].type == AUDIO_PORT_TYPE_DEVICE
1544                 ? sources[i].ext.device.address
1545                 : "");
1546     }
1547     for (int i = 0; i < num_sinks; i++) {
1548         ALOGD("%s: sink[%d] type=%d address=%s", __func__, i, sinks[i].type,
1549                 sinks[i].type == AUDIO_PORT_TYPE_DEVICE ? sinks[i].ext.device.address
1550                 : "N/A");
1551     }
1552     if (num_sources == 1 && num_sinks == 1 &&
1553             sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
1554             sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
1555         pthread_mutex_lock(&audio_dev->lock);
1556         audio_dev->last_patch_id += 1;
1557         pthread_mutex_unlock(&audio_dev->lock);
1558         *handle = audio_dev->last_patch_id;
1559         ALOGD("%s: handle: %d", __func__, *handle);
1560     }
1561     return 0;
1562 }
1563 
adev_release_audio_patch(struct audio_hw_device * dev,audio_patch_handle_t handle)1564 static int adev_release_audio_patch(struct audio_hw_device *dev,
1565         audio_patch_handle_t handle) {
1566     ALOGD("%s: handle: %d", __func__, handle);
1567     return 0;
1568 }
1569 
adev_close(hw_device_t * dev)1570 static int adev_close(hw_device_t *dev) {
1571     struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1572     int ret = 0;
1573     if (!adev)
1574         return 0;
1575 
1576     pthread_mutex_lock(&lock);
1577 
1578     if (audio_device_ref_count == 0) {
1579         ALOGE("adev_close called when ref_count 0");
1580         ret = -EINVAL;
1581         goto error;
1582     }
1583 
1584     if ((--audio_device_ref_count) == 0) {
1585         if (adev->mixer) {
1586             mixer_close(adev->mixer);
1587         }
1588         if (adev->out_bus_stream_map) {
1589             hashmapFree(adev->out_bus_stream_map);
1590         }
1591         if (adev->in_bus_tone_frequency_map) {
1592             hashmapFree(adev->in_bus_tone_frequency_map);
1593         }
1594 
1595         device_handle = 0;
1596         free(adev);
1597     }
1598 
1599 error:
1600     pthread_mutex_unlock(&lock);
1601     return ret;
1602 }
1603 
1604 /* copied from libcutils/str_parms.c */
str_eq(void * key_a,void * key_b)1605 static bool str_eq(void *key_a, void *key_b) {
1606     return !strcmp((const char *)key_a, (const char *)key_b);
1607 }
1608 
1609 /**
1610  * use djb hash unless we find it inadequate.
1611  * copied from libcutils/str_parms.c
1612  */
1613 #ifdef __clang__
1614 __attribute__((no_sanitize("integer")))
1615 #endif
str_hash_fn(void * str)1616 static int str_hash_fn(void *str) {
1617     uint32_t hash = 5381;
1618     char *p;
1619     for (p = str; p && *p; p++) {
1620         hash = ((hash << 5) + hash) + *p;
1621     }
1622     return (int)hash;
1623 }
1624 
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)1625 static int adev_open(const hw_module_t *module,
1626         const char *name, hw_device_t **device) {
1627     static struct generic_audio_device *adev;
1628 
1629     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1630         return -EINVAL;
1631 
1632     pthread_mutex_lock(&lock);
1633     if (audio_device_ref_count != 0) {
1634         *device = &adev->device.common;
1635         audio_device_ref_count++;
1636         ALOGV("%s: returning existing instance of adev", __func__);
1637         ALOGV("%s: exit", __func__);
1638         goto unlock;
1639     }
1640 
1641     pcm_config_in.period_count = get_in_period_count();
1642     pcm_config_out.period_count = get_out_period_count();
1643 
1644     adev = calloc(1, sizeof(struct generic_audio_device));
1645 
1646     pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
1647 
1648     adev->device.common.tag = HARDWARE_DEVICE_TAG;
1649     adev->device.common.version = AUDIO_DEVICE_API_VERSION_3_0;
1650     adev->device.common.module = (struct hw_module_t *) module;
1651     adev->device.common.close = adev_close;
1652 
1653     adev->device.init_check = adev_init_check;               // no op
1654     adev->device.set_voice_volume = adev_set_voice_volume;   // no op
1655     adev->device.set_master_volume = adev_set_master_volume; // no op
1656     adev->device.get_master_volume = adev_get_master_volume; // no op
1657     adev->device.set_master_mute = adev_set_master_mute;
1658     adev->device.get_master_mute = adev_get_master_mute;
1659     adev->device.set_mode = adev_set_mode;                   // no op
1660     adev->device.set_mic_mute = adev_set_mic_mute;
1661     adev->device.get_mic_mute = adev_get_mic_mute;
1662     adev->device.set_parameters = adev_set_parameters;       // no op
1663     adev->device.get_parameters = adev_get_parameters;       // no op
1664     adev->device.get_input_buffer_size = adev_get_input_buffer_size;
1665     adev->device.open_output_stream = adev_open_output_stream;
1666     adev->device.close_output_stream = adev_close_output_stream;
1667     adev->device.open_input_stream = adev_open_input_stream;
1668     adev->device.close_input_stream = adev_close_input_stream;
1669     adev->device.dump = adev_dump;
1670 
1671     // New in AUDIO_DEVICE_API_VERSION_3_0
1672     adev->device.set_audio_port_config = adev_set_audio_port_config;
1673     adev->device.create_audio_patch = adev_create_audio_patch;
1674     adev->device.release_audio_patch = adev_release_audio_patch;
1675 
1676     *device = &adev->device.common;
1677 
1678     adev->mixer = mixer_open(PCM_CARD);
1679 
1680     ALOGD("%s Mixer name %s", __func__, mixer_get_name(adev->mixer));
1681     struct mixer_ctl *ctl;
1682 
1683     // Set default mixer ctls
1684     // Enable channels and set volume
1685     for (int i = 0; i < (int)mixer_get_num_ctls(adev->mixer); i++) {
1686         ctl = mixer_get_ctl(adev->mixer, i);
1687         ALOGD("mixer %d name %s", i, mixer_ctl_get_name(ctl));
1688         if (!strcmp(mixer_ctl_get_name(ctl), "Master Playback Volume") ||
1689             !strcmp(mixer_ctl_get_name(ctl), "Capture Volume")) {
1690             for (int z = 0; z < (int)mixer_ctl_get_num_values(ctl); z++) {
1691                 ALOGD("set ctl %d to %d", z, 100);
1692                 mixer_ctl_set_percent(ctl, z, 100);
1693             }
1694             continue;
1695         }
1696         if (!strcmp(mixer_ctl_get_name(ctl), "Master Playback Switch") ||
1697             !strcmp(mixer_ctl_get_name(ctl), "Capture Switch")) {
1698             for (int z = 0; z < (int)mixer_ctl_get_num_values(ctl); z++) {
1699                 ALOGD("set ctl %d to %d", z, 1);
1700                 mixer_ctl_set_value(ctl, z, 1);
1701             }
1702             continue;
1703         }
1704     }
1705 
1706     // Initialize the bus address to output stream map
1707     adev->out_bus_stream_map = hashmapCreate(5, str_hash_fn, str_eq);
1708 
1709     // Initialize the bus address to input stream map
1710     adev->in_bus_tone_frequency_map = hashmapCreate(5, str_hash_fn, str_eq);
1711 
1712     adev->next_tone_frequency_to_assign = DEFAULT_FREQUENCY;
1713 
1714     adev->last_zone_selected_to_play = DEFAULT_ZONE_TO_LEFT_SPEAKER;
1715 
1716     adev->hfp_running = false;
1717 
1718     device_handle = adev;
1719 
1720     audio_device_ref_count++;
1721 
1722 unlock:
1723     pthread_mutex_unlock(&lock);
1724     return 0;
1725 }
1726 
1727 static struct hw_module_methods_t hal_module_methods = {
1728     .open = adev_open,
1729 };
1730 
1731 struct audio_module HAL_MODULE_INFO_SYM = {
1732     .common = {
1733         .tag = HARDWARE_MODULE_TAG,
1734         .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1735         .hal_api_version = HARDWARE_HAL_API_VERSION,
1736         .id = AUDIO_HARDWARE_MODULE_ID,
1737         .name = "Generic car audio HW HAL",
1738         .author = "The Android Open Source Project",
1739         .methods = &hal_module_methods,
1740     },
1741 };
1742