1 /*
2 * Copyright (C) 2012 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 * This code was forked from device/generic/goldfish/audio/audio_hw.c
17 *
18 * At the time of forking, the code was identical except that a fallback
19 * to a legacy HAL which does not use ALSA was removed, and the dependency
20 * on libdl was also removed.
21 */
22
23 #define LOG_TAG "audio_hw_generic"
24
25 #include <assert.h>
26 #include <errno.h>
27 #include <inttypes.h>
28 #include <pthread.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <sys/time.h>
32 #include <dlfcn.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35
36 #include <log/log.h>
37 #include <cutils/list.h>
38 #include <cutils/str_parms.h>
39
40 #include <hardware/hardware.h>
41 #include <system/audio.h>
42 #include <hardware/audio.h>
43 #include <tinyalsa/asoundlib.h>
44
45 #define PCM_CARD 0
46 #define PCM_DEVICE 0
47
48
49 #define OUT_PERIOD_MS 15
50 #define OUT_PERIOD_COUNT 4
51
52 #define IN_PERIOD_MS 15
53 #define IN_PERIOD_COUNT 4
54
55 struct generic_audio_device {
56 struct audio_hw_device device; // Constant after init
57 pthread_mutex_t lock;
58 bool mic_mute; // Protected by this->lock
59 struct mixer* mixer; // Protected by this->lock
60 struct listnode out_streams; // Record for output streams, protected by this->lock
61 struct listnode in_streams; // Record for input streams, protected by this->lock
62 audio_patch_handle_t next_patch_handle; // Protected by this->lock
63 };
64
65 /* If not NULL, this is a pointer to the fallback module.
66 * This really is the original goldfish audio device /dev/eac which we will use
67 * if no alsa devices are detected.
68 */
69 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state);
70 static int adev_get_microphones(const audio_hw_device_t *dev,
71 struct audio_microphone_characteristic_t *mic_array,
72 size_t *mic_count);
73
74
75 typedef struct audio_vbuffer {
76 pthread_mutex_t lock;
77 uint8_t * data;
78 size_t frame_size;
79 size_t frame_count;
80 size_t head;
81 size_t tail;
82 size_t live;
83 } audio_vbuffer_t;
84
audio_vbuffer_init(audio_vbuffer_t * audio_vbuffer,size_t frame_count,size_t frame_size)85 static int audio_vbuffer_init (audio_vbuffer_t * audio_vbuffer, size_t frame_count,
86 size_t frame_size) {
87 if (!audio_vbuffer) {
88 return -EINVAL;
89 }
90 audio_vbuffer->frame_size = frame_size;
91 audio_vbuffer->frame_count = frame_count;
92 size_t bytes = frame_count * frame_size;
93 audio_vbuffer->data = calloc(bytes, 1);
94 if (!audio_vbuffer->data) {
95 return -ENOMEM;
96 }
97 audio_vbuffer->head = 0;
98 audio_vbuffer->tail = 0;
99 audio_vbuffer->live = 0;
100 pthread_mutex_init (&audio_vbuffer->lock, (const pthread_mutexattr_t *) NULL);
101 return 0;
102 }
103
audio_vbuffer_destroy(audio_vbuffer_t * audio_vbuffer)104 static int audio_vbuffer_destroy (audio_vbuffer_t * audio_vbuffer) {
105 if (!audio_vbuffer) {
106 return -EINVAL;
107 }
108 free(audio_vbuffer->data);
109 pthread_mutex_destroy(&audio_vbuffer->lock);
110 return 0;
111 }
112
audio_vbuffer_live(audio_vbuffer_t * audio_vbuffer)113 static int audio_vbuffer_live (audio_vbuffer_t * audio_vbuffer) {
114 if (!audio_vbuffer) {
115 return -EINVAL;
116 }
117 pthread_mutex_lock (&audio_vbuffer->lock);
118 int live = audio_vbuffer->live;
119 pthread_mutex_unlock (&audio_vbuffer->lock);
120 return live;
121 }
122
123 #define MIN(a,b) (((a)<(b))?(a):(b))
audio_vbuffer_write(audio_vbuffer_t * audio_vbuffer,const void * buffer,size_t frame_count)124 static size_t audio_vbuffer_write (audio_vbuffer_t * audio_vbuffer, const void * buffer, size_t frame_count) {
125 size_t frames_written = 0;
126 pthread_mutex_lock (&audio_vbuffer->lock);
127
128 while (frame_count != 0) {
129 int frames = 0;
130 if (audio_vbuffer->live == 0 || audio_vbuffer->head > audio_vbuffer->tail) {
131 frames = MIN(frame_count, audio_vbuffer->frame_count - audio_vbuffer->head);
132 } else if (audio_vbuffer->head < audio_vbuffer->tail) {
133 frames = MIN(frame_count, audio_vbuffer->tail - (audio_vbuffer->head));
134 } else {
135 // Full
136 break;
137 }
138 memcpy(&audio_vbuffer->data[audio_vbuffer->head*audio_vbuffer->frame_size],
139 &((uint8_t*)buffer)[frames_written*audio_vbuffer->frame_size],
140 frames*audio_vbuffer->frame_size);
141 audio_vbuffer->live += frames;
142 frames_written += frames;
143 frame_count -= frames;
144 audio_vbuffer->head = (audio_vbuffer->head + frames) % audio_vbuffer->frame_count;
145 }
146
147 pthread_mutex_unlock (&audio_vbuffer->lock);
148 return frames_written;
149 }
150
audio_vbuffer_read(audio_vbuffer_t * audio_vbuffer,void * buffer,size_t frame_count)151 static size_t audio_vbuffer_read (audio_vbuffer_t * audio_vbuffer, void * buffer, size_t frame_count) {
152 size_t frames_read = 0;
153 pthread_mutex_lock (&audio_vbuffer->lock);
154
155 while (frame_count != 0) {
156 int frames = 0;
157 if (audio_vbuffer->live == audio_vbuffer->frame_count ||
158 audio_vbuffer->tail > audio_vbuffer->head) {
159 frames = MIN(frame_count, audio_vbuffer->frame_count - audio_vbuffer->tail);
160 } else if (audio_vbuffer->tail < audio_vbuffer->head) {
161 frames = MIN(frame_count, audio_vbuffer->head - audio_vbuffer->tail);
162 } else {
163 break;
164 }
165 memcpy(&((uint8_t*)buffer)[frames_read*audio_vbuffer->frame_size],
166 &audio_vbuffer->data[audio_vbuffer->tail*audio_vbuffer->frame_size],
167 frames*audio_vbuffer->frame_size);
168 audio_vbuffer->live -= frames;
169 frames_read += frames;
170 frame_count -= frames;
171 audio_vbuffer->tail = (audio_vbuffer->tail + frames) % audio_vbuffer->frame_count;
172 }
173
174 pthread_mutex_unlock (&audio_vbuffer->lock);
175 return frames_read;
176 }
177
178 struct generic_stream_out {
179 struct audio_stream_out stream; // Constant after init
180 pthread_mutex_t lock;
181 struct generic_audio_device *dev; // Constant after init
182 uint32_t num_devices; // Protected by this->lock
183 audio_devices_t devices[AUDIO_PATCH_PORTS_MAX]; // Protected by this->lock
184 struct audio_config req_config; // Constant after init
185 struct pcm_config pcm_config; // Constant after init
186 audio_vbuffer_t buffer; // Constant after init
187
188 // Time & Position Keeping
189 bool standby; // Protected by this->lock
190 uint64_t underrun_position; // Protected by this->lock
191 struct timespec underrun_time; // Protected by this->lock
192 uint64_t last_write_time_us; // Protected by this->lock
193 uint64_t frames_total_buffered; // Protected by this->lock
194 uint64_t frames_written; // Protected by this->lock
195 uint64_t frames_rendered; // Protected by this->lock
196
197 // Worker
198 pthread_t worker_thread; // Constant after init
199 pthread_cond_t worker_wake; // Protected by this->lock
200 bool worker_standby; // Protected by this->lock
201 bool worker_exit; // Protected by this->lock
202
203 audio_io_handle_t handle; // Constant after init
204 audio_patch_handle_t patch_handle; // Protected by this->dev->lock
205
206 struct listnode stream_node; // Protected by this->dev->lock
207 };
208
209 struct generic_stream_in {
210 struct audio_stream_in stream; // Constant after init
211 pthread_mutex_t lock;
212 struct generic_audio_device *dev; // Constant after init
213 audio_devices_t device; // Protected by this->lock
214 struct audio_config req_config; // Constant after init
215 struct pcm *pcm; // Protected by this->lock
216 struct pcm_config pcm_config; // Constant after init
217 int16_t *stereo_to_mono_buf; // Protected by this->lock
218 size_t stereo_to_mono_buf_size; // Protected by this->lock
219 audio_vbuffer_t buffer; // Protected by this->lock
220
221 // Time & Position Keeping
222 bool standby; // Protected by this->lock
223 int64_t standby_position; // Protected by this->lock
224 struct timespec standby_exit_time;// Protected by this->lock
225 int64_t standby_frames_read; // Protected by this->lock
226
227 // Worker
228 pthread_t worker_thread; // Constant after init
229 pthread_cond_t worker_wake; // Protected by this->lock
230 bool worker_standby; // Protected by this->lock
231 bool worker_exit; // Protected by this->lock
232
233 audio_io_handle_t handle; // Constant after init
234 audio_patch_handle_t patch_handle; // Protected by this->dev->lock
235
236 struct listnode stream_node; // Protected by this->dev->lock
237 };
238
239 static struct pcm_config pcm_config_out = {
240 .channels = 2,
241 .rate = 0,
242 .period_size = 0,
243 .period_count = OUT_PERIOD_COUNT,
244 .format = PCM_FORMAT_S16_LE,
245 .start_threshold = 0,
246 };
247
248 static struct pcm_config pcm_config_in = {
249 .channels = 2,
250 .rate = 0,
251 .period_size = 0,
252 .period_count = IN_PERIOD_COUNT,
253 .format = PCM_FORMAT_S16_LE,
254 .start_threshold = 0,
255 .stop_threshold = INT_MAX,
256 };
257
258 static pthread_mutex_t adev_init_lock = PTHREAD_MUTEX_INITIALIZER;
259 static unsigned int audio_device_ref_count = 0;
260
out_get_sample_rate(const struct audio_stream * stream)261 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
262 {
263 struct generic_stream_out *out = (struct generic_stream_out *)stream;
264 return out->req_config.sample_rate;
265 }
266
out_set_sample_rate(struct audio_stream * stream,uint32_t rate)267 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
268 {
269 return -ENOSYS;
270 }
271
out_get_buffer_size(const struct audio_stream * stream)272 static size_t out_get_buffer_size(const struct audio_stream *stream)
273 {
274 struct generic_stream_out *out = (struct generic_stream_out *)stream;
275 int size = out->pcm_config.period_size *
276 audio_stream_out_frame_size(&out->stream);
277
278 return size;
279 }
280
out_get_channels(const struct audio_stream * stream)281 static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
282 {
283 struct generic_stream_out *out = (struct generic_stream_out *)stream;
284 return out->req_config.channel_mask;
285 }
286
out_get_format(const struct audio_stream * stream)287 static audio_format_t out_get_format(const struct audio_stream *stream)
288 {
289 struct generic_stream_out *out = (struct generic_stream_out *)stream;
290
291 return out->req_config.format;
292 }
293
out_set_format(struct audio_stream * stream,audio_format_t format)294 static int out_set_format(struct audio_stream *stream, audio_format_t format)
295 {
296 return -ENOSYS;
297 }
298
out_dump(const struct audio_stream * stream,int fd)299 static int out_dump(const struct audio_stream *stream, int fd)
300 {
301 struct generic_stream_out *out = (struct generic_stream_out *)stream;
302 pthread_mutex_lock(&out->lock);
303 dprintf(fd, "\tout_dump:\n"
304 "\t\tsample rate: %u\n"
305 "\t\tbuffer size: %zu\n"
306 "\t\tchannel mask: %08x\n"
307 "\t\tformat: %d\n"
308 "\t\tdevice(s): ",
309 out_get_sample_rate(stream),
310 out_get_buffer_size(stream),
311 out_get_channels(stream),
312 out_get_format(stream));
313 if (out->num_devices == 0) {
314 dprintf(fd, "%08x\n", AUDIO_DEVICE_NONE);
315 } else {
316 for (uint32_t i = 0; i < out->num_devices; i++) {
317 if (i != 0) {
318 dprintf(fd, ", ");
319 }
320 dprintf(fd, "%08x", out->devices[i]);
321 }
322 dprintf(fd, "\n");
323 }
324 dprintf(fd, "\t\taudio dev: %p\n\n", out->dev);
325 pthread_mutex_unlock(&out->lock);
326 return 0;
327 }
328
out_set_parameters(struct audio_stream * stream,const char * kvpairs)329 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
330 {
331 struct str_parms *parms;
332 char value[32];
333 int success;
334 int ret = -EINVAL;
335
336 if (kvpairs == NULL || kvpairs[0] == 0) {
337 return 0;
338 }
339 parms = str_parms_create_str(kvpairs);
340 success = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
341 value, sizeof(value));
342 // As the hal version is 3.0, it must not use set parameters API to set audio devices.
343 // Instead, it should use create_audio_patch API.
344 assert(("Must not use set parameters API to set audio devices", success < 0));
345
346 if (str_parms_has_key(parms, AUDIO_PARAMETER_STREAM_FORMAT)) {
347 // match the return value of out_set_format
348 ret = -ENOSYS;
349 }
350
351 str_parms_destroy(parms);
352
353 if (ret == -EINVAL) {
354 ALOGW("%s(), unsupported parameter %s", __func__, kvpairs);
355 // There is not any key supported for set_parameters API.
356 // Return error when there is non-null value passed in.
357 }
358 return ret;
359 }
360
out_get_parameters(const struct audio_stream * stream,const char * keys)361 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
362 {
363 struct generic_stream_out *out = (struct generic_stream_out *)stream;
364 struct str_parms *query = str_parms_create_str(keys);
365 char *str = NULL;
366 char value[256];
367 struct str_parms *reply = str_parms_create();
368 int ret;
369 bool get = false;
370
371 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
372 if (ret >= 0) {
373 pthread_mutex_lock(&out->lock);
374 audio_devices_t device = AUDIO_DEVICE_NONE;
375 for (uint32_t i = 0; i < out->num_devices; i++) {
376 device |= out->devices[i];
377 }
378 str_parms_add_int(reply, AUDIO_PARAMETER_STREAM_ROUTING, device);
379 pthread_mutex_unlock(&out->lock);
380 get = true;
381 }
382
383 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
384 value[0] = 0;
385 strcat(value, "AUDIO_FORMAT_PCM_16_BIT");
386 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
387 get = true;
388 }
389
390 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_FORMAT)) {
391 value[0] = 0;
392 strcat(value, "AUDIO_FORMAT_PCM_16_BIT");
393 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_FORMAT, value);
394 get = true;
395 }
396
397 if (get) {
398 str = str_parms_to_str(reply);
399 }
400 else {
401 ALOGD("%s Unsupported paramter: %s", __FUNCTION__, keys);
402 }
403
404 str_parms_destroy(query);
405 str_parms_destroy(reply);
406 return str;
407 }
408
out_get_latency(const struct audio_stream_out * stream)409 static uint32_t out_get_latency(const struct audio_stream_out *stream)
410 {
411 struct generic_stream_out *out = (struct generic_stream_out *)stream;
412 return (out->pcm_config.period_size * 1000) / out->pcm_config.rate;
413 }
414
out_set_volume(struct audio_stream_out * stream,float left,float right)415 static int out_set_volume(struct audio_stream_out *stream, float left,
416 float right)
417 {
418 return -ENOSYS;
419 }
420
out_write_worker(void * args)421 static void *out_write_worker(void * args)
422 {
423 struct generic_stream_out *out = (struct generic_stream_out *)args;
424 struct pcm *pcm = NULL;
425 uint8_t *buffer = NULL;
426 int buffer_frames;
427 int buffer_size;
428 bool restart = false;
429 bool shutdown = false;
430 while (true) {
431 pthread_mutex_lock(&out->lock);
432 while (out->worker_standby || restart) {
433 restart = false;
434 if (pcm) {
435 pcm_close(pcm); // Frees pcm
436 pcm = NULL;
437 free(buffer);
438 buffer=NULL;
439 }
440 if (out->worker_exit) {
441 break;
442 }
443 pthread_cond_wait(&out->worker_wake, &out->lock);
444 }
445
446 if (out->worker_exit) {
447 if (!out->worker_standby) {
448 ALOGE("Out worker not in standby before exiting");
449 }
450 shutdown = true;
451 }
452
453 while (!shutdown && audio_vbuffer_live(&out->buffer) == 0) {
454 pthread_cond_wait(&out->worker_wake, &out->lock);
455 }
456
457 if (shutdown) {
458 pthread_mutex_unlock(&out->lock);
459 break;
460 }
461
462 if (!pcm) {
463 pcm = pcm_open(PCM_CARD, PCM_DEVICE,
464 PCM_OUT | PCM_MONOTONIC, &out->pcm_config);
465 if (!pcm_is_ready(pcm)) {
466 ALOGE("pcm_open(out) failed: %s: channels %d format %d rate %d",
467 pcm_get_error(pcm),
468 out->pcm_config.channels,
469 out->pcm_config.format,
470 out->pcm_config.rate
471 );
472 pthread_mutex_unlock(&out->lock);
473 break;
474 }
475 buffer_frames = out->pcm_config.period_size;
476 buffer_size = pcm_frames_to_bytes(pcm, buffer_frames);
477 buffer = malloc(buffer_size);
478 if (!buffer) {
479 ALOGE("could not allocate write buffer");
480 pthread_mutex_unlock(&out->lock);
481 break;
482 }
483 }
484 int frames = audio_vbuffer_read(&out->buffer, buffer, buffer_frames);
485 pthread_mutex_unlock(&out->lock);
486 int ret = pcm_write(pcm, buffer, pcm_frames_to_bytes(pcm, frames));
487 if (ret != 0) {
488 ALOGE("pcm_write failed %s", pcm_get_error(pcm));
489 restart = true;
490 }
491 }
492 if (buffer) {
493 free(buffer);
494 }
495
496 return NULL;
497 }
498
499 // Call with in->lock held
get_current_output_position(struct generic_stream_out * out,uint64_t * position,struct timespec * timestamp)500 static void get_current_output_position(struct generic_stream_out *out,
501 uint64_t * position,
502 struct timespec * timestamp) {
503 struct timespec curtime = { .tv_sec = 0, .tv_nsec = 0 };
504 clock_gettime(CLOCK_MONOTONIC, &curtime);
505 const int64_t now_us = (curtime.tv_sec * 1000000000LL + curtime.tv_nsec) / 1000;
506 if (timestamp) {
507 *timestamp = curtime;
508 }
509 int64_t position_since_underrun;
510 if (out->standby) {
511 position_since_underrun = 0;
512 } else {
513 const int64_t first_us = (out->underrun_time.tv_sec * 1000000000LL +
514 out->underrun_time.tv_nsec) / 1000;
515 position_since_underrun = (now_us - first_us) *
516 out_get_sample_rate(&out->stream.common) /
517 1000000;
518 if (position_since_underrun < 0) {
519 position_since_underrun = 0;
520 }
521 }
522 *position = out->underrun_position + position_since_underrun;
523
524 // The device will reuse the same output stream leading to periods of
525 // underrun.
526 if (*position > out->frames_written) {
527 ALOGW("Not supplying enough data to HAL, expected position %" PRIu64 " , only wrote "
528 "%" PRIu64,
529 *position, out->frames_written);
530
531 *position = out->frames_written;
532 out->underrun_position = *position;
533 out->underrun_time = curtime;
534 out->frames_total_buffered = 0;
535 }
536 }
537
538
out_write(struct audio_stream_out * stream,const void * buffer,size_t bytes)539 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
540 size_t bytes)
541 {
542 struct generic_stream_out *out = (struct generic_stream_out *)stream;
543 const size_t frames = bytes / audio_stream_out_frame_size(stream);
544
545 pthread_mutex_lock(&out->lock);
546
547 if (out->worker_standby) {
548 out->worker_standby = false;
549 }
550
551 uint64_t current_position;
552 struct timespec current_time;
553
554 get_current_output_position(out, ¤t_position, ¤t_time);
555 const uint64_t now_us = (current_time.tv_sec * 1000000000LL +
556 current_time.tv_nsec) / 1000;
557 if (out->standby) {
558 out->standby = false;
559 out->underrun_time = current_time;
560 out->frames_rendered = 0;
561 out->frames_total_buffered = 0;
562 }
563
564 size_t frames_written = audio_vbuffer_write(&out->buffer, buffer, frames);
565 pthread_cond_signal(&out->worker_wake);
566
567 /* Implementation just consumes bytes if we start getting backed up */
568 out->frames_written += frames;
569 out->frames_rendered += frames;
570 out->frames_total_buffered += frames;
571
572 // We simulate the audio device blocking when it's write buffers become
573 // full.
574
575 // At the beginning or after an underrun, try to fill up the vbuffer.
576 // This will be throttled by the PlaybackThread
577 int frames_sleep = out->frames_total_buffered < out->buffer.frame_count ? 0 : frames;
578
579 uint64_t sleep_time_us = frames_sleep * 1000000LL /
580 out_get_sample_rate(&stream->common);
581
582 // If the write calls are delayed, subtract time off of the sleep to
583 // compensate
584 uint64_t time_since_last_write_us = now_us - out->last_write_time_us;
585 if (time_since_last_write_us < sleep_time_us) {
586 sleep_time_us -= time_since_last_write_us;
587 } else {
588 sleep_time_us = 0;
589 }
590 out->last_write_time_us = now_us + sleep_time_us;
591
592 pthread_mutex_unlock(&out->lock);
593
594 if (sleep_time_us > 0) {
595 usleep(sleep_time_us);
596 }
597
598 if (frames_written < frames) {
599 ALOGW("Hardware backing HAL too slow, could only write %zu of %zu frames", frames_written, frames);
600 }
601
602 /* Always consume all bytes */
603 return bytes;
604 }
605
out_get_presentation_position(const struct audio_stream_out * stream,uint64_t * frames,struct timespec * timestamp)606 static int out_get_presentation_position(const struct audio_stream_out *stream,
607 uint64_t *frames, struct timespec *timestamp)
608
609 {
610 if (stream == NULL || frames == NULL || timestamp == NULL) {
611 return -EINVAL;
612 }
613 struct generic_stream_out *out = (struct generic_stream_out *)stream;
614
615 pthread_mutex_lock(&out->lock);
616 get_current_output_position(out, frames, timestamp);
617 pthread_mutex_unlock(&out->lock);
618
619 return 0;
620 }
621
out_get_render_position(const struct audio_stream_out * stream,uint32_t * dsp_frames)622 static int out_get_render_position(const struct audio_stream_out *stream,
623 uint32_t *dsp_frames)
624 {
625 if (stream == NULL || dsp_frames == NULL) {
626 return -EINVAL;
627 }
628 struct generic_stream_out *out = (struct generic_stream_out *)stream;
629 pthread_mutex_lock(&out->lock);
630 *dsp_frames = out->frames_rendered;
631 pthread_mutex_unlock(&out->lock);
632 return 0;
633 }
634
635 // Must be called with out->lock held
do_out_standby(struct generic_stream_out * out)636 static void do_out_standby(struct generic_stream_out *out)
637 {
638 int frames_sleep = 0;
639 uint64_t sleep_time_us = 0;
640 if (out->standby) {
641 return;
642 }
643 while (true) {
644 get_current_output_position(out, &out->underrun_position, NULL);
645 frames_sleep = out->frames_written - out->underrun_position;
646
647 if (frames_sleep == 0) {
648 break;
649 }
650
651 sleep_time_us = frames_sleep * 1000000LL /
652 out_get_sample_rate(&out->stream.common);
653
654 pthread_mutex_unlock(&out->lock);
655 usleep(sleep_time_us);
656 pthread_mutex_lock(&out->lock);
657 }
658 out->worker_standby = true;
659 out->standby = true;
660 }
661
out_standby(struct audio_stream * stream)662 static int out_standby(struct audio_stream *stream)
663 {
664 struct generic_stream_out *out = (struct generic_stream_out *)stream;
665 pthread_mutex_lock(&out->lock);
666 do_out_standby(out);
667 pthread_mutex_unlock(&out->lock);
668 return 0;
669 }
670
out_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)671 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
672 {
673 // out_add_audio_effect is a no op
674 return 0;
675 }
676
out_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)677 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
678 {
679 // out_remove_audio_effect is a no op
680 return 0;
681 }
682
out_get_next_write_timestamp(const struct audio_stream_out * stream,int64_t * timestamp)683 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
684 int64_t *timestamp)
685 {
686 return -ENOSYS;
687 }
688
in_get_sample_rate(const struct audio_stream * stream)689 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
690 {
691 struct generic_stream_in *in = (struct generic_stream_in *)stream;
692 return in->req_config.sample_rate;
693 }
694
in_set_sample_rate(struct audio_stream * stream,uint32_t rate)695 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
696 {
697 return -ENOSYS;
698 }
699
refine_output_parameters(uint32_t * sample_rate,audio_format_t * format,audio_channel_mask_t * channel_mask)700 static int refine_output_parameters(uint32_t *sample_rate, audio_format_t *format, audio_channel_mask_t *channel_mask)
701 {
702 static const uint32_t sample_rates [] = {8000,11025,16000,22050,24000,32000,
703 44100,48000};
704 static const int sample_rates_count = sizeof(sample_rates)/sizeof(uint32_t);
705 bool inval = false;
706 if (*format != AUDIO_FORMAT_PCM_16_BIT) {
707 *format = AUDIO_FORMAT_PCM_16_BIT;
708 inval = true;
709 }
710
711 int channel_count = popcount(*channel_mask);
712 if (channel_count != 1 && channel_count != 2) {
713 *channel_mask = AUDIO_CHANNEL_IN_STEREO;
714 inval = true;
715 }
716
717 int i;
718 for (i = 0; i < sample_rates_count; i++) {
719 if (*sample_rate < sample_rates[i]) {
720 *sample_rate = sample_rates[i];
721 inval=true;
722 break;
723 }
724 else if (*sample_rate == sample_rates[i]) {
725 break;
726 }
727 else if (i == sample_rates_count-1) {
728 // Cap it to the highest rate we support
729 *sample_rate = sample_rates[i];
730 inval=true;
731 }
732 }
733
734 if (inval) {
735 return -EINVAL;
736 }
737 return 0;
738 }
739
refine_input_parameters(uint32_t * sample_rate,audio_format_t * format,audio_channel_mask_t * channel_mask)740 static int refine_input_parameters(uint32_t *sample_rate, audio_format_t *format, audio_channel_mask_t *channel_mask)
741 {
742 static const uint32_t sample_rates [] = {8000, 11025, 16000, 22050, 44100, 48000};
743 static const int sample_rates_count = sizeof(sample_rates)/sizeof(uint32_t);
744 bool inval = false;
745 // Only PCM_16_bit is supported. If this is changed, stereo to mono drop
746 // must be fixed in in_read
747 if (*format != AUDIO_FORMAT_PCM_16_BIT) {
748 *format = AUDIO_FORMAT_PCM_16_BIT;
749 inval = true;
750 }
751
752 int channel_count = popcount(*channel_mask);
753 if (channel_count != 1 && channel_count != 2) {
754 *channel_mask = AUDIO_CHANNEL_IN_STEREO;
755 inval = true;
756 }
757
758 int i;
759 for (i = 0; i < sample_rates_count; i++) {
760 if (*sample_rate < sample_rates[i]) {
761 *sample_rate = sample_rates[i];
762 inval=true;
763 break;
764 }
765 else if (*sample_rate == sample_rates[i]) {
766 break;
767 }
768 else if (i == sample_rates_count-1) {
769 // Cap it to the highest rate we support
770 *sample_rate = sample_rates[i];
771 inval=true;
772 }
773 }
774
775 if (inval) {
776 return -EINVAL;
777 }
778 return 0;
779 }
780
check_input_parameters(uint32_t sample_rate,audio_format_t format,audio_channel_mask_t channel_mask)781 static int check_input_parameters(uint32_t sample_rate, audio_format_t format,
782 audio_channel_mask_t channel_mask)
783 {
784 return refine_input_parameters(&sample_rate, &format, &channel_mask);
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 {
790 size_t size;
791 int channel_count = popcount(channel_mask);
792 if (check_input_parameters(sample_rate, format, channel_mask) != 0)
793 return 0;
794
795 size = sample_rate*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
803
in_get_buffer_size(const struct audio_stream * stream)804 static size_t in_get_buffer_size(const struct audio_stream *stream)
805 {
806 struct generic_stream_in *in = (struct generic_stream_in *)stream;
807 int size = get_input_buffer_size(in->req_config.sample_rate,
808 in->req_config.format,
809 in->req_config.channel_mask);
810
811 return size;
812 }
813
in_get_channels(const struct audio_stream * stream)814 static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
815 {
816 struct generic_stream_in *in = (struct generic_stream_in *)stream;
817 return in->req_config.channel_mask;
818 }
819
in_get_format(const struct audio_stream * stream)820 static audio_format_t in_get_format(const struct audio_stream *stream)
821 {
822 struct generic_stream_in *in = (struct generic_stream_in *)stream;
823 return in->req_config.format;
824 }
825
in_set_format(struct audio_stream * stream,audio_format_t format)826 static int in_set_format(struct audio_stream *stream, audio_format_t format)
827 {
828 return -ENOSYS;
829 }
830
in_dump(const struct audio_stream * stream,int fd)831 static int in_dump(const struct audio_stream *stream, int fd)
832 {
833 struct generic_stream_in *in = (struct generic_stream_in *)stream;
834
835 pthread_mutex_lock(&in->lock);
836 dprintf(fd, "\tin_dump:\n"
837 "\t\tsample rate: %u\n"
838 "\t\tbuffer size: %zu\n"
839 "\t\tchannel mask: %08x\n"
840 "\t\tformat: %d\n"
841 "\t\tdevice: %08x\n"
842 "\t\taudio dev: %p\n\n",
843 in_get_sample_rate(stream),
844 in_get_buffer_size(stream),
845 in_get_channels(stream),
846 in_get_format(stream),
847 in->device,
848 in->dev);
849 pthread_mutex_unlock(&in->lock);
850 return 0;
851 }
852
in_set_parameters(struct audio_stream * stream,const char * kvpairs)853 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
854 {
855 struct str_parms *parms;
856 char value[32];
857 int success;
858 int ret = -EINVAL;
859
860 if (kvpairs == NULL || kvpairs[0] == 0) {
861 return 0;
862 }
863 parms = str_parms_create_str(kvpairs);
864 success = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
865 value, sizeof(value));
866 // As the hal version is 3.0, it must not use set parameters API to set audio device.
867 // Instead, it should use create_audio_patch API.
868 assert(("Must not use set parameters API to set audio devices", success < 0));
869
870 if (str_parms_has_key(parms, AUDIO_PARAMETER_STREAM_FORMAT)) {
871 // match the return value of in_set_format
872 ret = -ENOSYS;
873 }
874
875 str_parms_destroy(parms);
876
877 if (ret == -EINVAL) {
878 ALOGW("%s(), unsupported parameter %s", __func__, kvpairs);
879 // There is not any key supported for set_parameters API.
880 // Return error when there is non-null value passed in.
881 }
882 return ret;
883 }
884
in_get_parameters(const struct audio_stream * stream,const char * keys)885 static char * in_get_parameters(const struct audio_stream *stream,
886 const char *keys)
887 {
888 struct generic_stream_in *in = (struct generic_stream_in *)stream;
889 struct str_parms *query = str_parms_create_str(keys);
890 char *str = NULL;
891 char value[256];
892 struct str_parms *reply = str_parms_create();
893 int ret;
894 bool get = false;
895
896 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
897 if (ret >= 0) {
898 str_parms_add_int(reply, AUDIO_PARAMETER_STREAM_ROUTING, in->device);
899 get = true;
900 }
901
902 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
903 value[0] = 0;
904 strcat(value, "AUDIO_FORMAT_PCM_16_BIT");
905 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
906 get = true;
907 }
908
909 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_FORMAT)) {
910 value[0] = 0;
911 strcat(value, "AUDIO_FORMAT_PCM_16_BIT");
912 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_FORMAT, value);
913 get = true;
914 }
915
916 if (get) {
917 str = str_parms_to_str(reply);
918 }
919 else {
920 ALOGD("%s Unsupported paramter: %s", __FUNCTION__, keys);
921 }
922
923 str_parms_destroy(query);
924 str_parms_destroy(reply);
925 return str;
926 }
927
in_set_gain(struct audio_stream_in * stream,float gain)928 static int in_set_gain(struct audio_stream_in *stream, float gain)
929 {
930 // in_set_gain is a no op
931 return 0;
932 }
933
934 // Call with in->lock held
get_current_input_position(struct generic_stream_in * in,int64_t * position,struct timespec * timestamp)935 static void get_current_input_position(struct generic_stream_in *in,
936 int64_t * position,
937 struct timespec * timestamp) {
938 struct timespec t = { .tv_sec = 0, .tv_nsec = 0 };
939 clock_gettime(CLOCK_MONOTONIC, &t);
940 const int64_t now_us = (t.tv_sec * 1000000000LL + t.tv_nsec) / 1000;
941 if (timestamp) {
942 *timestamp = t;
943 }
944 int64_t position_since_standby;
945 if (in->standby) {
946 position_since_standby = 0;
947 } else {
948 const int64_t first_us = (in->standby_exit_time.tv_sec * 1000000000LL +
949 in->standby_exit_time.tv_nsec) / 1000;
950 position_since_standby = (now_us - first_us) *
951 in_get_sample_rate(&in->stream.common) /
952 1000000;
953 if (position_since_standby < 0) {
954 position_since_standby = 0;
955 }
956 }
957 *position = in->standby_position + position_since_standby;
958 }
959
960 // Must be called with in->lock held
do_in_standby(struct generic_stream_in * in)961 static void do_in_standby(struct generic_stream_in *in)
962 {
963 if (in->standby) {
964 return;
965 }
966 in->worker_standby = true;
967 get_current_input_position(in, &in->standby_position, NULL);
968 in->standby = true;
969 }
970
in_standby(struct audio_stream * stream)971 static int in_standby(struct audio_stream *stream)
972 {
973 struct generic_stream_in *in = (struct generic_stream_in *)stream;
974 pthread_mutex_lock(&in->lock);
975 do_in_standby(in);
976 pthread_mutex_unlock(&in->lock);
977 return 0;
978 }
979
in_read_worker(void * args)980 static void *in_read_worker(void * args)
981 {
982 struct generic_stream_in *in = (struct generic_stream_in *)args;
983 struct pcm *pcm = NULL;
984 uint8_t *buffer = NULL;
985 size_t buffer_frames;
986 int buffer_size;
987
988 bool restart = false;
989 bool shutdown = false;
990 while (true) {
991 pthread_mutex_lock(&in->lock);
992 while (in->worker_standby || restart) {
993 restart = false;
994 if (pcm) {
995 pcm_close(pcm); // Frees pcm
996 pcm = NULL;
997 free(buffer);
998 buffer=NULL;
999 }
1000 if (in->worker_exit) {
1001 break;
1002 }
1003 pthread_cond_wait(&in->worker_wake, &in->lock);
1004 }
1005
1006 if (in->worker_exit) {
1007 if (!in->worker_standby) {
1008 ALOGE("In worker not in standby before exiting");
1009 }
1010 shutdown = true;
1011 }
1012 if (shutdown) {
1013 pthread_mutex_unlock(&in->lock);
1014 break;
1015 }
1016 if (!pcm) {
1017 pcm = pcm_open(PCM_CARD, PCM_DEVICE,
1018 PCM_IN | PCM_MONOTONIC, &in->pcm_config);
1019 if (!pcm_is_ready(pcm)) {
1020 ALOGE("pcm_open(in) failed: %s: channels %d format %d rate %d",
1021 pcm_get_error(pcm),
1022 in->pcm_config.channels,
1023 in->pcm_config.format,
1024 in->pcm_config.rate
1025 );
1026 pthread_mutex_unlock(&in->lock);
1027 break;
1028 }
1029 buffer_frames = in->pcm_config.period_size;
1030 buffer_size = pcm_frames_to_bytes(pcm, buffer_frames);
1031 buffer = malloc(buffer_size);
1032 if (!buffer) {
1033 ALOGE("could not allocate worker read buffer");
1034 pthread_mutex_unlock(&in->lock);
1035 break;
1036 }
1037 }
1038 pthread_mutex_unlock(&in->lock);
1039 int ret = pcm_read(pcm, buffer, pcm_frames_to_bytes(pcm, buffer_frames));
1040 if (ret != 0) {
1041 ALOGW("pcm_read failed %s", pcm_get_error(pcm));
1042 restart = true;
1043 continue;
1044 }
1045
1046 pthread_mutex_lock(&in->lock);
1047 size_t frames_written = audio_vbuffer_write(&in->buffer, buffer, buffer_frames);
1048 pthread_mutex_unlock(&in->lock);
1049
1050 if (frames_written != buffer_frames) {
1051 ALOGW("in_read_worker only could write %zu / %zu frames", frames_written, buffer_frames);
1052 }
1053 }
1054 if (buffer) {
1055 free(buffer);
1056 }
1057 return NULL;
1058 }
1059
in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)1060 static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
1061 size_t bytes)
1062 {
1063 struct generic_stream_in *in = (struct generic_stream_in *)stream;
1064 struct generic_audio_device *adev = in->dev;
1065 const size_t frames = bytes / audio_stream_in_frame_size(stream);
1066 bool mic_mute = false;
1067 size_t read_bytes = 0;
1068
1069 adev_get_mic_mute(&adev->device, &mic_mute);
1070 pthread_mutex_lock(&in->lock);
1071
1072 if (in->worker_standby) {
1073 in->worker_standby = false;
1074 }
1075 pthread_cond_signal(&in->worker_wake);
1076
1077 int64_t current_position;
1078 struct timespec current_time;
1079
1080 get_current_input_position(in, ¤t_position, ¤t_time);
1081 if (in->standby) {
1082 in->standby = false;
1083 in->standby_exit_time = current_time;
1084 in->standby_frames_read = 0;
1085 }
1086
1087 const int64_t frames_available = current_position - in->standby_position - in->standby_frames_read;
1088 assert(frames_available >= 0);
1089
1090 const size_t frames_wait = ((uint64_t)frames_available > frames) ? 0 : frames - frames_available;
1091
1092 int64_t sleep_time_us = frames_wait * 1000000LL /
1093 in_get_sample_rate(&stream->common);
1094
1095 pthread_mutex_unlock(&in->lock);
1096
1097 if (sleep_time_us > 0) {
1098 usleep(sleep_time_us);
1099 }
1100
1101 pthread_mutex_lock(&in->lock);
1102 int read_frames = 0;
1103 if (in->standby) {
1104 ALOGW("Input put to sleep while read in progress");
1105 goto exit;
1106 }
1107 in->standby_frames_read += frames;
1108
1109 if (popcount(in->req_config.channel_mask) == 1 &&
1110 in->pcm_config.channels == 2) {
1111 // Need to resample to mono
1112 if (in->stereo_to_mono_buf_size < bytes*2) {
1113 in->stereo_to_mono_buf = realloc(in->stereo_to_mono_buf,
1114 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 (mic_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 {
1156 return 0;
1157 }
1158
in_get_capture_position(const struct audio_stream_in * stream,int64_t * frames,int64_t * time)1159 static int in_get_capture_position(const struct audio_stream_in *stream,
1160 int64_t *frames, int64_t *time)
1161 {
1162 struct generic_stream_in *in = (struct generic_stream_in *)stream;
1163 pthread_mutex_lock(&in->lock);
1164 struct timespec current_time;
1165 get_current_input_position(in, frames, ¤t_time);
1166 *time = (current_time.tv_sec * 1000000000LL + current_time.tv_nsec);
1167 pthread_mutex_unlock(&in->lock);
1168 return 0;
1169 }
1170
in_get_active_microphones(const struct audio_stream_in * stream,struct audio_microphone_characteristic_t * mic_array,size_t * mic_count)1171 static int in_get_active_microphones(const struct audio_stream_in *stream,
1172 struct audio_microphone_characteristic_t *mic_array,
1173 size_t *mic_count)
1174 {
1175 return adev_get_microphones(NULL, mic_array, mic_count);
1176 }
1177
in_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)1178 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1179 {
1180 // in_add_audio_effect is a no op
1181 return 0;
1182 }
1183
in_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)1184 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1185 {
1186 // in_add_audio_effect is a no op
1187 return 0;
1188 }
1189
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 __unused)1190 static int adev_open_output_stream(struct audio_hw_device *dev,
1191 audio_io_handle_t handle,
1192 audio_devices_t devices,
1193 audio_output_flags_t flags,
1194 struct audio_config *config,
1195 struct audio_stream_out **stream_out,
1196 const char *address __unused)
1197 {
1198 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1199 struct generic_stream_out *out;
1200 int ret = 0;
1201
1202 if (refine_output_parameters(&config->sample_rate, &config->format, &config->channel_mask)) {
1203 ALOGE("Error opening output stream format %d, channel_mask %04x, sample_rate %u",
1204 config->format, config->channel_mask, config->sample_rate);
1205 ret = -EINVAL;
1206 goto error;
1207 }
1208
1209 out = (struct generic_stream_out *)calloc(1, sizeof(struct generic_stream_out));
1210
1211 if (!out)
1212 return -ENOMEM;
1213
1214 out->stream.common.get_sample_rate = out_get_sample_rate;
1215 out->stream.common.set_sample_rate = out_set_sample_rate;
1216 out->stream.common.get_buffer_size = out_get_buffer_size;
1217 out->stream.common.get_channels = out_get_channels;
1218 out->stream.common.get_format = out_get_format;
1219 out->stream.common.set_format = out_set_format;
1220 out->stream.common.standby = out_standby;
1221 out->stream.common.dump = out_dump;
1222 out->stream.common.set_parameters = out_set_parameters;
1223 out->stream.common.get_parameters = out_get_parameters;
1224 out->stream.common.add_audio_effect = out_add_audio_effect;
1225 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1226 out->stream.get_latency = out_get_latency;
1227 out->stream.set_volume = out_set_volume;
1228 out->stream.write = out_write;
1229 out->stream.get_render_position = out_get_render_position;
1230 out->stream.get_presentation_position = out_get_presentation_position;
1231 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1232
1233 out->handle = handle;
1234
1235 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
1236 out->dev = adev;
1237 // Only 1 device is expected despite the argument being named 'devices'
1238 out->num_devices = 1;
1239 out->devices[0] = devices;
1240 memcpy(&out->req_config, config, sizeof(struct audio_config));
1241 memcpy(&out->pcm_config, &pcm_config_out, sizeof(struct pcm_config));
1242 out->pcm_config.rate = config->sample_rate;
1243 out->pcm_config.period_size = out->pcm_config.rate*OUT_PERIOD_MS/1000;
1244
1245 out->standby = true;
1246 out->underrun_position = 0;
1247 out->underrun_time.tv_sec = 0;
1248 out->underrun_time.tv_nsec = 0;
1249 out->last_write_time_us = 0;
1250 out->frames_total_buffered = 0;
1251 out->frames_written = 0;
1252 out->frames_rendered = 0;
1253
1254 ret = audio_vbuffer_init(&out->buffer,
1255 out->pcm_config.period_size*out->pcm_config.period_count,
1256 out->pcm_config.channels *
1257 pcm_format_to_bits(out->pcm_config.format) >> 3);
1258 if (ret == 0) {
1259 pthread_cond_init(&out->worker_wake, NULL);
1260 out->worker_standby = true;
1261 out->worker_exit = false;
1262 pthread_create(&out->worker_thread, NULL, out_write_worker, out);
1263
1264 }
1265
1266 pthread_mutex_lock(&adev->lock);
1267 list_add_tail(&adev->out_streams, &out->stream_node);
1268 pthread_mutex_unlock(&adev->lock);
1269
1270 *stream_out = &out->stream;
1271
1272 error:
1273
1274 return ret;
1275 }
1276
1277 // This must be called with adev->lock held.
get_stream_out_by_io_handle_l(struct generic_audio_device * adev,audio_io_handle_t handle)1278 struct generic_stream_out *get_stream_out_by_io_handle_l(
1279 struct generic_audio_device *adev, audio_io_handle_t handle) {
1280 struct listnode *node;
1281
1282 list_for_each(node, &adev->out_streams) {
1283 struct generic_stream_out *out = node_to_item(
1284 node, struct generic_stream_out, stream_node);
1285 if (out->handle == handle) {
1286 return out;
1287 }
1288 }
1289 return NULL;
1290 }
1291
adev_close_output_stream(struct audio_hw_device * dev,struct audio_stream_out * stream)1292 static void adev_close_output_stream(struct audio_hw_device *dev,
1293 struct audio_stream_out *stream)
1294 {
1295 struct generic_stream_out *out = (struct generic_stream_out *)stream;
1296 pthread_mutex_lock(&out->lock);
1297 do_out_standby(out);
1298
1299 out->worker_exit = true;
1300 pthread_cond_signal(&out->worker_wake);
1301 pthread_mutex_unlock(&out->lock);
1302
1303 pthread_join(out->worker_thread, NULL);
1304 pthread_mutex_destroy(&out->lock);
1305 audio_vbuffer_destroy(&out->buffer);
1306
1307 struct generic_audio_device *adev = (struct generic_audio_device *) dev;
1308 pthread_mutex_lock(&adev->lock);
1309 list_remove(&out->stream_node);
1310 pthread_mutex_unlock(&adev->lock);
1311 free(stream);
1312 }
1313
adev_set_parameters(struct audio_hw_device * dev,const char * kvpairs)1314 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1315 {
1316 return 0;
1317 }
1318
adev_get_parameters(const struct audio_hw_device * dev,const char * keys)1319 static char * adev_get_parameters(const struct audio_hw_device *dev,
1320 const char *keys)
1321 {
1322 return strdup("");
1323 }
1324
adev_init_check(const struct audio_hw_device * dev)1325 static int adev_init_check(const struct audio_hw_device *dev)
1326 {
1327 return 0;
1328 }
1329
adev_set_voice_volume(struct audio_hw_device * dev,float volume)1330 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1331 {
1332 // adev_set_voice_volume is a no op (simulates phones)
1333 return 0;
1334 }
1335
adev_set_master_volume(struct audio_hw_device * dev,float volume)1336 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1337 {
1338 return -ENOSYS;
1339 }
1340
adev_get_master_volume(struct audio_hw_device * dev,float * volume)1341 static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
1342 {
1343 return -ENOSYS;
1344 }
1345
adev_set_master_mute(struct audio_hw_device * dev,bool muted)1346 static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
1347 {
1348 return -ENOSYS;
1349 }
1350
adev_get_master_mute(struct audio_hw_device * dev,bool * muted)1351 static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
1352 {
1353 return -ENOSYS;
1354 }
1355
adev_set_mode(struct audio_hw_device * dev,audio_mode_t mode)1356 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1357 {
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 {
1364 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1365 pthread_mutex_lock(&adev->lock);
1366 adev->mic_mute = state;
1367 pthread_mutex_unlock(&adev->lock);
1368 return 0;
1369 }
1370
adev_get_mic_mute(const struct audio_hw_device * dev,bool * state)1371 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1372 {
1373 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1374 pthread_mutex_lock(&adev->lock);
1375 *state = adev->mic_mute;
1376 pthread_mutex_unlock(&adev->lock);
1377 return 0;
1378 }
1379
1380
adev_get_input_buffer_size(const struct audio_hw_device * dev,const struct audio_config * config)1381 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1382 const struct audio_config *config)
1383 {
1384 return get_input_buffer_size(config->sample_rate, config->format, config->channel_mask);
1385 }
1386
1387 // This must be called with adev->lock held.
get_stream_in_by_io_handle_l(struct generic_audio_device * adev,audio_io_handle_t handle)1388 struct generic_stream_in *get_stream_in_by_io_handle_l(
1389 struct generic_audio_device *adev, audio_io_handle_t handle) {
1390 struct listnode *node;
1391
1392 list_for_each(node, &adev->in_streams) {
1393 struct generic_stream_in *in = node_to_item(
1394 node, struct generic_stream_in, stream_node);
1395 if (in->handle == handle) {
1396 return in;
1397 }
1398 }
1399 return NULL;
1400 }
1401
adev_close_input_stream(struct audio_hw_device * dev,struct audio_stream_in * stream)1402 static void adev_close_input_stream(struct audio_hw_device *dev,
1403 struct audio_stream_in *stream)
1404 {
1405 struct generic_stream_in *in = (struct generic_stream_in *)stream;
1406 pthread_mutex_lock(&in->lock);
1407 do_in_standby(in);
1408
1409 in->worker_exit = true;
1410 pthread_cond_signal(&in->worker_wake);
1411 pthread_mutex_unlock(&in->lock);
1412 pthread_join(in->worker_thread, NULL);
1413
1414 if (in->stereo_to_mono_buf != NULL) {
1415 free(in->stereo_to_mono_buf);
1416 in->stereo_to_mono_buf_size = 0;
1417 }
1418
1419 pthread_mutex_destroy(&in->lock);
1420 audio_vbuffer_destroy(&in->buffer);
1421
1422 struct generic_audio_device *adev = (struct generic_audio_device *) dev;
1423 pthread_mutex_lock(&adev->lock);
1424 list_remove(&in->stream_node);
1425 pthread_mutex_unlock(&adev->lock);
1426 free(stream);
1427 }
1428
1429
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 __unused,audio_source_t source __unused)1430 static int adev_open_input_stream(struct audio_hw_device *dev,
1431 audio_io_handle_t handle,
1432 audio_devices_t devices,
1433 struct audio_config *config,
1434 struct audio_stream_in **stream_in,
1435 audio_input_flags_t flags __unused,
1436 const char *address __unused,
1437 audio_source_t source __unused)
1438 {
1439 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1440 struct generic_stream_in *in;
1441 int ret = 0;
1442 if (refine_input_parameters(&config->sample_rate, &config->format, &config->channel_mask)) {
1443 ALOGE("Error opening input stream format %d, channel_mask %04x, sample_rate %u",
1444 config->format, config->channel_mask, config->sample_rate);
1445 ret = -EINVAL;
1446 goto error;
1447 }
1448
1449 in = (struct generic_stream_in *)calloc(1, sizeof(struct generic_stream_in));
1450 if (!in) {
1451 ret = -ENOMEM;
1452 goto error;
1453 }
1454
1455 in->stream.common.get_sample_rate = in_get_sample_rate;
1456 in->stream.common.set_sample_rate = in_set_sample_rate; // no op
1457 in->stream.common.get_buffer_size = in_get_buffer_size;
1458 in->stream.common.get_channels = in_get_channels;
1459 in->stream.common.get_format = in_get_format;
1460 in->stream.common.set_format = in_set_format; // no op
1461 in->stream.common.standby = in_standby;
1462 in->stream.common.dump = in_dump;
1463 in->stream.common.set_parameters = in_set_parameters;
1464 in->stream.common.get_parameters = in_get_parameters;
1465 in->stream.common.add_audio_effect = in_add_audio_effect; // no op
1466 in->stream.common.remove_audio_effect = in_remove_audio_effect; // no op
1467 in->stream.set_gain = in_set_gain; // no op
1468 in->stream.read = in_read;
1469 in->stream.get_input_frames_lost = in_get_input_frames_lost; // no op
1470 in->stream.get_capture_position = in_get_capture_position;
1471 in->stream.get_active_microphones = in_get_active_microphones;
1472
1473 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
1474 in->dev = adev;
1475 in->device = devices;
1476 memcpy(&in->req_config, config, sizeof(struct audio_config));
1477 memcpy(&in->pcm_config, &pcm_config_in, sizeof(struct pcm_config));
1478 in->pcm_config.rate = config->sample_rate;
1479 in->pcm_config.period_size = in->pcm_config.rate*IN_PERIOD_MS/1000;
1480
1481 in->stereo_to_mono_buf = NULL;
1482 in->stereo_to_mono_buf_size = 0;
1483
1484 in->standby = true;
1485 in->standby_position = 0;
1486 in->standby_exit_time.tv_sec = 0;
1487 in->standby_exit_time.tv_nsec = 0;
1488 in->standby_frames_read = 0;
1489
1490 ret = audio_vbuffer_init(&in->buffer,
1491 in->pcm_config.period_size*in->pcm_config.period_count,
1492 in->pcm_config.channels *
1493 pcm_format_to_bits(in->pcm_config.format) >> 3);
1494 if (ret == 0) {
1495 pthread_cond_init(&in->worker_wake, NULL);
1496 in->worker_standby = true;
1497 in->worker_exit = false;
1498 pthread_create(&in->worker_thread, NULL, in_read_worker, in);
1499 }
1500 in->handle = handle;
1501
1502 pthread_mutex_lock(&adev->lock);
1503 list_add_tail(&adev->in_streams, &in->stream_node);
1504 pthread_mutex_unlock(&adev->lock);
1505
1506 *stream_in = &in->stream;
1507
1508 error:
1509 return ret;
1510 }
1511
1512
adev_dump(const audio_hw_device_t * dev,int fd)1513 static int adev_dump(const audio_hw_device_t *dev, int fd)
1514 {
1515 return 0;
1516 }
1517
adev_get_microphones(const audio_hw_device_t * dev,struct audio_microphone_characteristic_t * mic_array,size_t * mic_count)1518 static int adev_get_microphones(const audio_hw_device_t *dev,
1519 struct audio_microphone_characteristic_t *mic_array,
1520 size_t *mic_count)
1521 {
1522 if (mic_count == NULL) {
1523 return -ENOSYS;
1524 }
1525
1526 if (*mic_count == 0) {
1527 *mic_count = 1;
1528 return 0;
1529 }
1530
1531 if (mic_array == NULL) {
1532 return -ENOSYS;
1533 }
1534
1535 strncpy(mic_array->device_id, "mic_goldfish", AUDIO_MICROPHONE_ID_MAX_LEN - 1);
1536 mic_array->device = AUDIO_DEVICE_IN_BUILTIN_MIC;
1537 strncpy(mic_array->address, AUDIO_BOTTOM_MICROPHONE_ADDRESS,
1538 AUDIO_DEVICE_MAX_ADDRESS_LEN - 1);
1539 memset(mic_array->channel_mapping, AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED,
1540 sizeof(mic_array->channel_mapping));
1541 mic_array->location = AUDIO_MICROPHONE_LOCATION_UNKNOWN;
1542 mic_array->group = 0;
1543 mic_array->index_in_the_group = 0;
1544 mic_array->sensitivity = AUDIO_MICROPHONE_SENSITIVITY_UNKNOWN;
1545 mic_array->max_spl = AUDIO_MICROPHONE_SPL_UNKNOWN;
1546 mic_array->min_spl = AUDIO_MICROPHONE_SPL_UNKNOWN;
1547 mic_array->directionality = AUDIO_MICROPHONE_DIRECTIONALITY_UNKNOWN;
1548 mic_array->num_frequency_responses = 0;
1549 mic_array->geometric_location.x = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
1550 mic_array->geometric_location.y = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
1551 mic_array->geometric_location.z = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
1552 mic_array->orientation.x = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
1553 mic_array->orientation.y = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
1554 mic_array->orientation.z = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
1555
1556 *mic_count = 1;
1557 return 0;
1558 }
1559
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)1560 static int adev_create_audio_patch(struct audio_hw_device *dev,
1561 unsigned int num_sources,
1562 const struct audio_port_config *sources,
1563 unsigned int num_sinks,
1564 const struct audio_port_config *sinks,
1565 audio_patch_handle_t *handle) {
1566 if (num_sources != 1 || num_sinks == 0 || num_sinks > AUDIO_PATCH_PORTS_MAX) {
1567 return -EINVAL;
1568 }
1569
1570 if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1571 // If source is a device, the number of sinks should be 1.
1572 if (num_sinks != 1 || sinks[0].type != AUDIO_PORT_TYPE_MIX) {
1573 return -EINVAL;
1574 }
1575 } else if (sources[0].type == AUDIO_PORT_TYPE_MIX) {
1576 // If source is a mix, all sinks should be device.
1577 for (unsigned int i = 0; i < num_sinks; i++) {
1578 if (sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
1579 ALOGE("%s() invalid sink type %#x for mix source", __func__, sinks[i].type);
1580 return -EINVAL;
1581 }
1582 }
1583 } else {
1584 // All other cases are invalid.
1585 return -EINVAL;
1586 }
1587
1588 struct generic_audio_device* adev = (struct generic_audio_device*) dev;
1589 int ret = 0;
1590 bool generatedPatchHandle = false;
1591 pthread_mutex_lock(&adev->lock);
1592 if (*handle == AUDIO_PATCH_HANDLE_NONE) {
1593 *handle = ++adev->next_patch_handle;
1594 generatedPatchHandle = true;
1595 }
1596
1597 // Only handle patches for mix->devices and device->mix case.
1598 if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1599 struct generic_stream_in *in =
1600 get_stream_in_by_io_handle_l(adev, sinks[0].ext.mix.handle);
1601 if (in == NULL) {
1602 ALOGE("%s()can not find stream with handle(%d)", __func__, sources[0].ext.mix.handle);
1603 ret = -EINVAL;
1604 goto error;
1605 }
1606
1607 // Check if the patch handle match the recorded one if a valid patch handle is passed.
1608 if (!generatedPatchHandle && in->patch_handle != *handle) {
1609 ALOGE("%s() the patch handle(%d) does not match recorded one(%d) for stream "
1610 "with handle(%d) when creating audio patch for device->mix",
1611 __func__, *handle, in->patch_handle, in->handle);
1612 ret = -EINVAL;
1613 goto error;
1614 }
1615 pthread_mutex_lock(&in->lock);
1616 in->device = sources[0].ext.device.type;
1617 pthread_mutex_unlock(&in->lock);
1618 in->patch_handle = *handle;
1619 } else {
1620 struct generic_stream_out *out =
1621 get_stream_out_by_io_handle_l(adev, sources[0].ext.mix.handle);
1622 if (out == NULL) {
1623 ALOGE("%s()can not find stream with handle(%d)", __func__, sources[0].ext.mix.handle);
1624 ret = -EINVAL;
1625 goto error;
1626 }
1627
1628 // Check if the patch handle match the recorded one if a valid patch handle is passed.
1629 if (!generatedPatchHandle && out->patch_handle != *handle) {
1630 ALOGE("%s() the patch handle(%d) does not match recorded one(%d) for stream "
1631 "with handle(%d) when creating audio patch for mix->device",
1632 __func__, *handle, out->patch_handle, out->handle);
1633 ret = -EINVAL;
1634 pthread_mutex_unlock(&out->lock);
1635 goto error;
1636 }
1637 pthread_mutex_lock(&out->lock);
1638 for (out->num_devices = 0; out->num_devices < num_sinks; out->num_devices++) {
1639 out->devices[out->num_devices] = sinks[out->num_devices].ext.device.type;
1640 }
1641 pthread_mutex_unlock(&out->lock);
1642 out->patch_handle = *handle;
1643 }
1644
1645 error:
1646 if (ret != 0 && generatedPatchHandle) {
1647 *handle = AUDIO_PATCH_HANDLE_NONE;
1648 }
1649 pthread_mutex_unlock(&adev->lock);
1650 return 0;
1651 }
1652
1653 // This must be called with adev->lock held.
get_stream_out_by_patch_handle_l(struct generic_audio_device * adev,audio_patch_handle_t patch_handle)1654 struct generic_stream_out *get_stream_out_by_patch_handle_l(
1655 struct generic_audio_device *adev, audio_patch_handle_t patch_handle) {
1656 struct listnode *node;
1657
1658 list_for_each(node, &adev->out_streams) {
1659 struct generic_stream_out *out = node_to_item(
1660 node, struct generic_stream_out, stream_node);
1661 if (out->patch_handle == patch_handle) {
1662 return out;
1663 }
1664 }
1665 return NULL;
1666 }
1667
1668 // This must be called with adev->lock held.
get_stream_in_by_patch_handle_l(struct generic_audio_device * adev,audio_patch_handle_t patch_handle)1669 struct generic_stream_in *get_stream_in_by_patch_handle_l(
1670 struct generic_audio_device *adev, audio_patch_handle_t patch_handle) {
1671 struct listnode *node;
1672
1673 list_for_each(node, &adev->in_streams) {
1674 struct generic_stream_in *in = node_to_item(
1675 node, struct generic_stream_in, stream_node);
1676 if (in->patch_handle == patch_handle) {
1677 return in;
1678 }
1679 }
1680 return NULL;
1681 }
1682
adev_release_audio_patch(struct audio_hw_device * dev,audio_patch_handle_t patch_handle)1683 static int adev_release_audio_patch(struct audio_hw_device *dev,
1684 audio_patch_handle_t patch_handle) {
1685 struct generic_audio_device *adev = (struct generic_audio_device *) dev;
1686
1687 pthread_mutex_lock(&adev->lock);
1688 struct generic_stream_out *out = get_stream_out_by_patch_handle_l(adev, patch_handle);
1689 if (out != NULL) {
1690 pthread_mutex_lock(&out->lock);
1691 out->num_devices = 0;
1692 memset(out->devices, 0, sizeof(out->devices));
1693 pthread_mutex_unlock(&out->lock);
1694 out->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1695 pthread_mutex_unlock(&adev->lock);
1696 return 0;
1697 }
1698 struct generic_stream_in *in = get_stream_in_by_patch_handle_l(adev, patch_handle);
1699 if (in != NULL) {
1700 pthread_mutex_lock(&in->lock);
1701 in->device = AUDIO_DEVICE_NONE;
1702 pthread_mutex_unlock(&in->lock);
1703 in->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1704 pthread_mutex_unlock(&adev->lock);
1705 return 0;
1706 }
1707
1708 pthread_mutex_unlock(&adev->lock);
1709 ALOGW("%s() cannot find stream for patch handle: %d", __func__, patch_handle);
1710 return -EINVAL;
1711 }
1712
adev_close(hw_device_t * dev)1713 static int adev_close(hw_device_t *dev)
1714 {
1715 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
1716 int ret = 0;
1717 if (!adev)
1718 return 0;
1719
1720 pthread_mutex_lock(&adev_init_lock);
1721
1722 if (audio_device_ref_count == 0) {
1723 ALOGE("adev_close called when ref_count 0");
1724 ret = -EINVAL;
1725 goto error;
1726 }
1727
1728 if ((--audio_device_ref_count) == 0) {
1729 if (adev->mixer) {
1730 mixer_close(adev->mixer);
1731 }
1732 free(adev);
1733 }
1734
1735 error:
1736 pthread_mutex_unlock(&adev_init_lock);
1737 return ret;
1738 }
1739
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)1740 static int adev_open(const hw_module_t* module, const char* name,
1741 hw_device_t** device)
1742 {
1743 static struct generic_audio_device *adev;
1744
1745 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1746 return -EINVAL;
1747
1748 pthread_mutex_lock(&adev_init_lock);
1749 if (audio_device_ref_count != 0) {
1750 *device = &adev->device.common;
1751 audio_device_ref_count++;
1752 ALOGV("%s: returning existing instance of adev", __func__);
1753 ALOGV("%s: exit", __func__);
1754 goto unlock;
1755 }
1756 adev = calloc(1, sizeof(struct generic_audio_device));
1757
1758 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
1759
1760 adev->device.common.tag = HARDWARE_DEVICE_TAG;
1761 adev->device.common.version = AUDIO_DEVICE_API_VERSION_3_0;
1762 adev->device.common.module = (struct hw_module_t *) module;
1763 adev->device.common.close = adev_close;
1764
1765 adev->device.init_check = adev_init_check; // no op
1766 adev->device.set_voice_volume = adev_set_voice_volume; // no op
1767 adev->device.set_master_volume = adev_set_master_volume; // no op
1768 adev->device.get_master_volume = adev_get_master_volume; // no op
1769 adev->device.set_master_mute = adev_set_master_mute; // no op
1770 adev->device.get_master_mute = adev_get_master_mute; // no op
1771 adev->device.set_mode = adev_set_mode; // no op
1772 adev->device.set_mic_mute = adev_set_mic_mute;
1773 adev->device.get_mic_mute = adev_get_mic_mute;
1774 adev->device.set_parameters = adev_set_parameters; // no op
1775 adev->device.get_parameters = adev_get_parameters; // no op
1776 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
1777 adev->device.open_output_stream = adev_open_output_stream;
1778 adev->device.close_output_stream = adev_close_output_stream;
1779 adev->device.open_input_stream = adev_open_input_stream;
1780 adev->device.close_input_stream = adev_close_input_stream;
1781 adev->device.dump = adev_dump;
1782 adev->device.get_microphones = adev_get_microphones;
1783 adev->device.create_audio_patch = adev_create_audio_patch;
1784 adev->device.release_audio_patch = adev_release_audio_patch;
1785
1786 *device = &adev->device.common;
1787
1788 adev->next_patch_handle = AUDIO_PATCH_HANDLE_NONE;
1789 list_init(&adev->out_streams);
1790 list_init(&adev->in_streams);
1791
1792 adev->mixer = mixer_open(PCM_CARD);
1793 struct mixer_ctl *ctl;
1794
1795 // Set default mixer ctls
1796 // Enable channels and set volume
1797 for (int i = 0; i < (int)mixer_get_num_ctls(adev->mixer); i++) {
1798 ctl = mixer_get_ctl(adev->mixer, i);
1799 ALOGD("mixer %d name %s", i, mixer_ctl_get_name(ctl));
1800 if (!strcmp(mixer_ctl_get_name(ctl), "Master Playback Volume") ||
1801 !strcmp(mixer_ctl_get_name(ctl), "Capture Volume")) {
1802 for (int z = 0; z < (int)mixer_ctl_get_num_values(ctl); z++) {
1803 ALOGD("set ctl %d to %d", z, 100);
1804 mixer_ctl_set_percent(ctl, z, 100);
1805 }
1806 continue;
1807 }
1808 if (!strcmp(mixer_ctl_get_name(ctl), "Master Playback Switch") ||
1809 !strcmp(mixer_ctl_get_name(ctl), "Capture Switch")) {
1810 for (int z = 0; z < (int)mixer_ctl_get_num_values(ctl); z++) {
1811 ALOGD("set ctl %d to %d", z, 1);
1812 mixer_ctl_set_value(ctl, z, 1);
1813 }
1814 continue;
1815 }
1816 }
1817
1818 audio_device_ref_count++;
1819
1820 unlock:
1821 pthread_mutex_unlock(&adev_init_lock);
1822 return 0;
1823 }
1824
1825 static struct hw_module_methods_t hal_module_methods = {
1826 .open = adev_open,
1827 };
1828
1829 struct audio_module HAL_MODULE_INFO_SYM = {
1830 .common = {
1831 .tag = HARDWARE_MODULE_TAG,
1832 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1833 .hal_api_version = HARDWARE_HAL_API_VERSION,
1834 .id = AUDIO_HARDWARE_MODULE_ID,
1835 .name = "Generic audio HW HAL",
1836 .author = "The Android Open Source Project",
1837 .methods = &hal_module_methods,
1838 },
1839 };
1840