blob: f5842a5444cf9fd756c02f6c1f0359a36b03b3f3 [file] [log] [blame]
Amit Pundirc738b812019-06-19 16:13:49 +05301/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "audio_hw_generic"
18//#define LOG_NDEBUG 0
19
20#include <errno.h>
21#include <malloc.h>
22#include <pthread.h>
23#include <stdint.h>
24#include <sys/time.h>
25#include <stdlib.h>
26#include <unistd.h>
27
28#include <log/log.h>
29#include <cutils/str_parms.h>
30#include <cutils/properties.h>
31
32#include <hardware/hardware.h>
33#include <system/audio.h>
34#include <hardware/audio.h>
35
36#include <sound/asound.h>
37#include <tinyalsa/asoundlib.h>
38#include <audio_utils/resampler.h>
39#include <audio_utils/echo_reference.h>
40#include <hardware/audio_effect.h>
41#include <hardware/audio_alsaops.h>
42#include <audio_effects/effect_aec.h>
43
44
45#define CARD_OUT 0
46#define PORT_CODEC 0
47/* Minimum granularity - Arbitrary but small value */
48#define CODEC_BASE_FRAME_COUNT 32
49
50/* number of base blocks in a short period (low latency) */
51#define PERIOD_MULTIPLIER 32 /* 21 ms */
52/* number of frames per short period (low latency) */
53#define PERIOD_SIZE (CODEC_BASE_FRAME_COUNT * PERIOD_MULTIPLIER)
54/* number of pseudo periods for low latency playback */
55#define PLAYBACK_PERIOD_COUNT 2
56#define PLAYBACK_PERIOD_START_THRESHOLD 2
57#define CODEC_SAMPLING_RATE 48000
58#define CHANNEL_STEREO 2
59#define MIN_WRITE_SLEEP_US 5000
60
61struct stub_stream_in {
62 struct audio_stream_in stream;
63};
64
65struct alsa_audio_device {
66 struct audio_hw_device hw_device;
67
68 pthread_mutex_t lock; /* see note below on mutex acquisition order */
69 int devices;
70 struct alsa_stream_in *active_input;
71 struct alsa_stream_out *active_output;
72 bool mic_mute;
73};
74
75struct alsa_stream_out {
76 struct audio_stream_out stream;
77
78 pthread_mutex_t lock; /* see note below on mutex acquisition order */
79 struct pcm_config config;
80 struct pcm *pcm;
81 bool unavailable;
82 int standby;
83 struct alsa_audio_device *dev;
84 int write_threshold;
85 unsigned int written;
86};
87
88
89/* must be called with hw device and output stream mutexes locked */
90static int start_output_stream(struct alsa_stream_out *out)
91{
92 struct alsa_audio_device *adev = out->dev;
93
94 if (out->unavailable)
95 return -ENODEV;
96
97 /* default to low power: will be corrected in out_write if necessary before first write to
98 * tinyalsa.
99 */
100 out->write_threshold = PLAYBACK_PERIOD_COUNT * PERIOD_SIZE;
101 out->config.start_threshold = PLAYBACK_PERIOD_START_THRESHOLD * PERIOD_SIZE;
102 out->config.avail_min = PERIOD_SIZE;
103
104 out->pcm = pcm_open(CARD_OUT, PORT_CODEC, PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC, &out->config);
105
106 if (!pcm_is_ready(out->pcm)) {
107 ALOGE("cannot open pcm_out driver: %s", pcm_get_error(out->pcm));
108 pcm_close(out->pcm);
109 adev->active_output = NULL;
110 out->unavailable = true;
111 return -ENODEV;
112 }
113
114 adev->active_output = out;
115 return 0;
116}
117
118static uint32_t out_get_sample_rate(const struct audio_stream *stream)
119{
120 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
121 return out->config.rate;
122}
123
124static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
125{
126 ALOGV("out_set_sample_rate: %d", 0);
127 return -ENOSYS;
128}
129
130static size_t out_get_buffer_size(const struct audio_stream *stream)
131{
132 ALOGV("out_get_buffer_size: %d", 4096);
133
134 /* return the closest majoring multiple of 16 frames, as
135 * audioflinger expects audio buffers to be a multiple of 16 frames */
136 size_t size = PERIOD_SIZE;
137 size = ((size + 15) / 16) * 16;
138 return size * audio_stream_out_frame_size((struct audio_stream_out *)stream);
139}
140
141static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
142{
143 ALOGV("out_get_channels");
144 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
145 return audio_channel_out_mask_from_count(out->config.channels);
146}
147
148static audio_format_t out_get_format(const struct audio_stream *stream)
149{
150 ALOGV("out_get_format");
151 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
152 return audio_format_from_pcm_format(out->config.format);
153}
154
155static int out_set_format(struct audio_stream *stream, audio_format_t format)
156{
157 ALOGV("out_set_format: %d",format);
158 return -ENOSYS;
159}
160
161static int do_output_standby(struct alsa_stream_out *out)
162{
163 struct alsa_audio_device *adev = out->dev;
164
165 if (!out->standby) {
166 pcm_close(out->pcm);
167 out->pcm = NULL;
168 adev->active_output = NULL;
169 out->standby = 1;
170 }
171 return 0;
172}
173
174static int out_standby(struct audio_stream *stream)
175{
176 ALOGV("out_standby");
177 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
178 int status;
179
180 pthread_mutex_lock(&out->dev->lock);
181 pthread_mutex_lock(&out->lock);
182 status = do_output_standby(out);
183 pthread_mutex_unlock(&out->lock);
184 pthread_mutex_unlock(&out->dev->lock);
185 return status;
186}
187
188static int out_dump(const struct audio_stream *stream, int fd)
189{
190 ALOGV("out_dump");
191 return 0;
192}
193
194static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
195{
196 ALOGV("out_set_parameters");
197 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
198 struct alsa_audio_device *adev = out->dev;
199 struct str_parms *parms;
200 char value[32];
201 int ret, val = 0;
202
203 parms = str_parms_create_str(kvpairs);
204
205 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
206 if (ret >= 0) {
207 val = atoi(value);
208 pthread_mutex_lock(&adev->lock);
209 pthread_mutex_lock(&out->lock);
210 if (((adev->devices & AUDIO_DEVICE_OUT_ALL) != val) && (val != 0)) {
211 adev->devices &= ~AUDIO_DEVICE_OUT_ALL;
212 adev->devices |= val;
213 }
214 pthread_mutex_unlock(&out->lock);
215 pthread_mutex_unlock(&adev->lock);
216 }
217
218 str_parms_destroy(parms);
219 return ret;
220}
221
222static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
223{
224 ALOGV("out_get_parameters");
225 return strdup("");
226}
227
228static uint32_t out_get_latency(const struct audio_stream_out *stream)
229{
230 ALOGV("out_get_latency");
231 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
232 return (PERIOD_SIZE * PLAYBACK_PERIOD_COUNT * 1000) / out->config.rate;
233}
234
235static int out_set_volume(struct audio_stream_out *stream, float left,
236 float right)
237{
238 ALOGV("out_set_volume: Left:%f Right:%f", left, right);
239 return 0;
240}
241
242static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
243 size_t bytes)
244{
245 int ret;
246 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
247 struct alsa_audio_device *adev = out->dev;
248 size_t frame_size = audio_stream_out_frame_size(stream);
249 size_t out_frames = bytes / frame_size;
250
251 /* acquiring hw device mutex systematically is useful if a low priority thread is waiting
252 * on the output stream mutex - e.g. executing select_mode() while holding the hw device
253 * mutex
254 */
255 pthread_mutex_lock(&adev->lock);
256 pthread_mutex_lock(&out->lock);
257 if (out->standby) {
258 ret = start_output_stream(out);
259 if (ret != 0) {
260 pthread_mutex_unlock(&adev->lock);
261 goto exit;
262 }
263 out->standby = 0;
264 }
265
266 pthread_mutex_unlock(&adev->lock);
267
268 ret = pcm_mmap_write(out->pcm, buffer, out_frames * frame_size);
269 if (ret == 0) {
270 out->written += out_frames;
271 }
272exit:
273 pthread_mutex_unlock(&out->lock);
274
275 if (ret != 0) {
276 usleep((int64_t)bytes * 1000000 / audio_stream_out_frame_size(stream) /
277 out_get_sample_rate(&stream->common));
278 }
279
280 return bytes;
281}
282
283static int out_get_render_position(const struct audio_stream_out *stream,
284 uint32_t *dsp_frames)
285{
286 *dsp_frames = 0;
287 ALOGV("out_get_render_position: dsp_frames: %p", dsp_frames);
288 return -EINVAL;
289}
290
291static int out_get_presentation_position(const struct audio_stream_out *stream,
292 uint64_t *frames, struct timespec *timestamp)
293{
294 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
295 int ret = -1;
296
297 if (out->pcm) {
298 unsigned int avail;
299 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
300 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
301 int64_t signed_frames = out->written - kernel_buffer_size + avail;
302 if (signed_frames >= 0) {
303 *frames = signed_frames;
304 ret = 0;
305 }
306 }
307 }
308
309 return ret;
310}
311
312
313static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
314{
315 ALOGV("out_add_audio_effect: %p", effect);
316 return 0;
317}
318
319static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
320{
321 ALOGV("out_remove_audio_effect: %p", effect);
322 return 0;
323}
324
325static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
326 int64_t *timestamp)
327{
328 *timestamp = 0;
329 ALOGV("out_get_next_write_timestamp: %ld", (long int)(*timestamp));
330 return -EINVAL;
331}
332
333/** audio_stream_in implementation **/
334static uint32_t in_get_sample_rate(const struct audio_stream *stream)
335{
336 ALOGV("in_get_sample_rate");
337 return 8000;
338}
339
340static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
341{
342 ALOGV("in_set_sample_rate: %d", rate);
343 return -ENOSYS;
344}
345
346static size_t in_get_buffer_size(const struct audio_stream *stream)
347{
348 ALOGV("in_get_buffer_size: %d", 320);
349 return 320;
350}
351
352static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
353{
354 ALOGV("in_get_channels: %d", AUDIO_CHANNEL_IN_MONO);
355 return AUDIO_CHANNEL_IN_MONO;
356}
357
358static audio_format_t in_get_format(const struct audio_stream *stream)
359{
360 return AUDIO_FORMAT_PCM_16_BIT;
361}
362
363static int in_set_format(struct audio_stream *stream, audio_format_t format)
364{
365 return -ENOSYS;
366}
367
368static int in_standby(struct audio_stream *stream)
369{
370 return 0;
371}
372
373static int in_dump(const struct audio_stream *stream, int fd)
374{
375 return 0;
376}
377
378static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
379{
380 return 0;
381}
382
383static char * in_get_parameters(const struct audio_stream *stream,
384 const char *keys)
385{
386 return strdup("");
387}
388
389static int in_set_gain(struct audio_stream_in *stream, float gain)
390{
391 return 0;
392}
393
394static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
395 size_t bytes)
396{
397 ALOGV("in_read: bytes %zu", bytes);
398 /* XXX: fake timing for audio input */
399 usleep((int64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
400 in_get_sample_rate(&stream->common));
401 memset(buffer, 0, bytes);
402 return bytes;
403}
404
405static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
406{
407 return 0;
408}
409
410static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
411{
412 return 0;
413}
414
415static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
416{
417 return 0;
418}
419
420static int adev_open_output_stream(struct audio_hw_device *dev,
421 audio_io_handle_t handle,
422 audio_devices_t devices,
423 audio_output_flags_t flags,
424 struct audio_config *config,
425 struct audio_stream_out **stream_out,
426 const char *address __unused)
427{
428 ALOGV("adev_open_output_stream...");
429
430 struct alsa_audio_device *ladev = (struct alsa_audio_device *)dev;
431 struct alsa_stream_out *out;
432 struct pcm_params *params;
433 int ret = 0;
434
435 params = pcm_params_get(CARD_OUT, PORT_CODEC, PCM_OUT);
436 if (!params)
437 return -ENOSYS;
438
439 out = (struct alsa_stream_out *)calloc(1, sizeof(struct alsa_stream_out));
440 if (!out)
441 return -ENOMEM;
442
443 out->stream.common.get_sample_rate = out_get_sample_rate;
444 out->stream.common.set_sample_rate = out_set_sample_rate;
445 out->stream.common.get_buffer_size = out_get_buffer_size;
446 out->stream.common.get_channels = out_get_channels;
447 out->stream.common.get_format = out_get_format;
448 out->stream.common.set_format = out_set_format;
449 out->stream.common.standby = out_standby;
450 out->stream.common.dump = out_dump;
451 out->stream.common.set_parameters = out_set_parameters;
452 out->stream.common.get_parameters = out_get_parameters;
453 out->stream.common.add_audio_effect = out_add_audio_effect;
454 out->stream.common.remove_audio_effect = out_remove_audio_effect;
455 out->stream.get_latency = out_get_latency;
456 out->stream.set_volume = out_set_volume;
457 out->stream.write = out_write;
458 out->stream.get_render_position = out_get_render_position;
459 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
460 out->stream.get_presentation_position = out_get_presentation_position;
461
462 out->config.channels = CHANNEL_STEREO;
463 out->config.rate = CODEC_SAMPLING_RATE;
464 out->config.format = PCM_FORMAT_S16_LE;
465 out->config.period_size = PERIOD_SIZE;
466 out->config.period_count = PLAYBACK_PERIOD_COUNT;
467
468 if (out->config.rate != config->sample_rate ||
469 audio_channel_count_from_out_mask(config->channel_mask) != CHANNEL_STEREO ||
470 out->config.format != pcm_format_from_audio_format(config->format) ) {
471 config->sample_rate = out->config.rate;
472 config->format = audio_format_from_pcm_format(out->config.format);
473 config->channel_mask = audio_channel_out_mask_from_count(CHANNEL_STEREO);
474 ret = -EINVAL;
475 }
476
477 ALOGI("adev_open_output_stream selects channels=%d rate=%d format=%d",
478 out->config.channels, out->config.rate, out->config.format);
479
480 out->dev = ladev;
481 out->standby = 1;
482 out->unavailable = false;
483
484 config->format = out_get_format(&out->stream.common);
485 config->channel_mask = out_get_channels(&out->stream.common);
486 config->sample_rate = out_get_sample_rate(&out->stream.common);
487
488 *stream_out = &out->stream;
489
490 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
491 ret = 0;
492
493 return ret;
494}
495
496static void adev_close_output_stream(struct audio_hw_device *dev,
497 struct audio_stream_out *stream)
498{
499 ALOGV("adev_close_output_stream...");
500 free(stream);
501}
502
503static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
504{
505 ALOGV("adev_set_parameters");
506 return -ENOSYS;
507}
508
509static char * adev_get_parameters(const struct audio_hw_device *dev,
510 const char *keys)
511{
512 ALOGV("adev_get_parameters");
513 return strdup("");
514}
515
516static int adev_init_check(const struct audio_hw_device *dev)
517{
518 ALOGV("adev_init_check");
519 return 0;
520}
521
522static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
523{
524 ALOGV("adev_set_voice_volume: %f", volume);
525 return -ENOSYS;
526}
527
528static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
529{
530 ALOGV("adev_set_master_volume: %f", volume);
531 return -ENOSYS;
532}
533
534static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
535{
536 ALOGV("adev_get_master_volume: %f", *volume);
537 return -ENOSYS;
538}
539
540static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
541{
542 ALOGV("adev_set_master_mute: %d", muted);
543 return -ENOSYS;
544}
545
546static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
547{
548 ALOGV("adev_get_master_mute: %d", *muted);
549 return -ENOSYS;
550}
551
552static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
553{
554 ALOGV("adev_set_mode: %d", mode);
555 return 0;
556}
557
558static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
559{
560 ALOGV("adev_set_mic_mute: %d",state);
561 return -ENOSYS;
562}
563
564static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
565{
566 ALOGV("adev_get_mic_mute");
567 return -ENOSYS;
568}
569
570static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
571 const struct audio_config *config)
572{
573 ALOGV("adev_get_input_buffer_size: %d", 320);
574 return 320;
575}
576
577static int adev_open_input_stream(struct audio_hw_device __unused *dev,
578 audio_io_handle_t handle,
579 audio_devices_t devices,
580 struct audio_config *config,
581 struct audio_stream_in **stream_in,
582 audio_input_flags_t flags __unused,
583 const char *address __unused,
584 audio_source_t source __unused)
585{
586 struct stub_stream_in *in;
587
588 ALOGV("adev_open_input_stream...");
589
590 in = (struct stub_stream_in *)calloc(1, sizeof(struct stub_stream_in));
591 if (!in)
592 return -ENOMEM;
593
594 in->stream.common.get_sample_rate = in_get_sample_rate;
595 in->stream.common.set_sample_rate = in_set_sample_rate;
596 in->stream.common.get_buffer_size = in_get_buffer_size;
597 in->stream.common.get_channels = in_get_channels;
598 in->stream.common.get_format = in_get_format;
599 in->stream.common.set_format = in_set_format;
600 in->stream.common.standby = in_standby;
601 in->stream.common.dump = in_dump;
602 in->stream.common.set_parameters = in_set_parameters;
603 in->stream.common.get_parameters = in_get_parameters;
604 in->stream.common.add_audio_effect = in_add_audio_effect;
605 in->stream.common.remove_audio_effect = in_remove_audio_effect;
606 in->stream.set_gain = in_set_gain;
607 in->stream.read = in_read;
608 in->stream.get_input_frames_lost = in_get_input_frames_lost;
609
610 *stream_in = &in->stream;
611 return 0;
612}
613
614static void adev_close_input_stream(struct audio_hw_device *dev,
615 struct audio_stream_in *in)
616{
617 ALOGV("adev_close_input_stream...");
618 return;
619}
620
621static int adev_dump(const audio_hw_device_t *device, int fd)
622{
623 ALOGV("adev_dump");
624 return 0;
625}
626
627static int adev_close(hw_device_t *device)
628{
629 ALOGV("adev_close");
630 free(device);
631 return 0;
632}
633
634static int adev_open(const hw_module_t* module, const char* name,
635 hw_device_t** device)
636{
637 struct alsa_audio_device *adev;
Amit Pundirc738b812019-06-19 16:13:49 +0530638
639 ALOGV("adev_open: %s", name);
640
641 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
642 return -EINVAL;
643
644 adev = calloc(1, sizeof(struct alsa_audio_device));
645 if (!adev)
646 return -ENOMEM;
647
648 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
649 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
650 adev->hw_device.common.module = (struct hw_module_t *) module;
651 adev->hw_device.common.close = adev_close;
652 adev->hw_device.init_check = adev_init_check;
653 adev->hw_device.set_voice_volume = adev_set_voice_volume;
654 adev->hw_device.set_master_volume = adev_set_master_volume;
655 adev->hw_device.get_master_volume = adev_get_master_volume;
656 adev->hw_device.set_master_mute = adev_set_master_mute;
657 adev->hw_device.get_master_mute = adev_get_master_mute;
658 adev->hw_device.set_mode = adev_set_mode;
659 adev->hw_device.set_mic_mute = adev_set_mic_mute;
660 adev->hw_device.get_mic_mute = adev_get_mic_mute;
661 adev->hw_device.set_parameters = adev_set_parameters;
662 adev->hw_device.get_parameters = adev_get_parameters;
663 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
664 adev->hw_device.open_output_stream = adev_open_output_stream;
665 adev->hw_device.close_output_stream = adev_close_output_stream;
666 adev->hw_device.open_input_stream = adev_open_input_stream;
667 adev->hw_device.close_input_stream = adev_close_input_stream;
668 adev->hw_device.dump = adev_dump;
669
670 adev->devices = AUDIO_DEVICE_NONE;
671
672 *device = &adev->hw_device.common;
673
Amit Pundirc738b812019-06-19 16:13:49 +0530674 return 0;
675}
676
677static struct hw_module_methods_t hal_module_methods = {
678 .open = adev_open,
679};
680
681struct audio_module HAL_MODULE_INFO_SYM = {
682 .common = {
683 .tag = HARDWARE_MODULE_TAG,
684 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
685 .hal_api_version = HARDWARE_HAL_API_VERSION,
686 .id = AUDIO_HARDWARE_MODULE_ID,
687 .name = "Generic Audio HAL",
688 .author = "The Android Open Source Project",
689 .methods = &hal_module_methods,
690 },
691};