Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 1 | /* |
| 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_hikey" |
| 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 | |
| 27 | #include <cutils/log.h> |
| 28 | #include <cutils/str_parms.h> |
| 29 | #include <cutils/properties.h> |
| 30 | |
| 31 | #include <hardware/hardware.h> |
| 32 | #include <system/audio.h> |
| 33 | #include <hardware/audio.h> |
| 34 | |
| 35 | #include <sound/asound.h> |
| 36 | #include <tinyalsa/asoundlib.h> |
| 37 | #include <audio_utils/resampler.h> |
| 38 | #include <audio_utils/echo_reference.h> |
| 39 | #include <hardware/audio_effect.h> |
| 40 | #include <hardware/audio_alsaops.h> |
| 41 | #include <audio_effects/effect_aec.h> |
| 42 | |
Niranjan Yadla | efa6b4d | 2017-09-18 13:31:35 -0700 | [diff] [blame] | 43 | #include <sys/ioctl.h> |
| 44 | #include <linux/audio_hifi.h> |
Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 45 | |
| 46 | #define CARD_OUT 0 |
| 47 | #define PORT_CODEC 0 |
| 48 | /* Minimum granularity - Arbitrary but small value */ |
| 49 | #define CODEC_BASE_FRAME_COUNT 32 |
| 50 | |
| 51 | /* number of base blocks in a short period (low latency) */ |
| 52 | #define PERIOD_MULTIPLIER 32 /* 21 ms */ |
| 53 | /* number of frames per short period (low latency) */ |
| 54 | #define PERIOD_SIZE (CODEC_BASE_FRAME_COUNT * PERIOD_MULTIPLIER) |
| 55 | /* number of pseudo periods for low latency playback */ |
| 56 | #define PLAYBACK_PERIOD_COUNT 4 |
| 57 | #define PLAYBACK_PERIOD_START_THRESHOLD 2 |
| 58 | #define CODEC_SAMPLING_RATE 48000 |
| 59 | #define CHANNEL_STEREO 2 |
| 60 | #define MIN_WRITE_SLEEP_US 5000 |
| 61 | |
Niranjan Yadla | 672a346 | 2018-05-08 16:27:06 -0700 | [diff] [blame] | 62 | #ifdef ENABLE_XAF_DSP_DEVICE |
| 63 | #include "xaf-utils-test.h" |
| 64 | #include "audio/xa_vorbis_dec_api.h" |
| 65 | #include "audio/xa-audio-decoder-api.h" |
| 66 | #define NUM_COMP_IN_GRAPH 1 |
| 67 | |
| 68 | struct alsa_audio_device; |
| 69 | |
| 70 | struct xaf_dsp_device { |
| 71 | void *p_adev; |
| 72 | void *p_decoder; |
| 73 | xaf_info_t comp_info; |
| 74 | /* ...playback format */ |
| 75 | xaf_format_t pb_format; |
| 76 | xaf_comp_status dec_status; |
| 77 | int dec_info[4]; |
| 78 | void *dec_inbuf[2]; |
| 79 | int read_length; |
| 80 | xf_id_t dec_id; |
| 81 | int xaf_started; |
| 82 | mem_obj_t* mem_handle; |
| 83 | int num_comp; |
| 84 | int (*dec_setup)(void *p_comp, struct alsa_audio_device *audio_device); |
| 85 | int xafinitdone; |
| 86 | }; |
| 87 | #endif |
| 88 | |
Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 89 | struct stub_stream_in { |
| 90 | struct audio_stream_in stream; |
| 91 | }; |
| 92 | |
| 93 | struct alsa_audio_device { |
| 94 | struct audio_hw_device hw_device; |
| 95 | |
| 96 | pthread_mutex_t lock; /* see note below on mutex acquisition order */ |
| 97 | int devices; |
| 98 | struct alsa_stream_in *active_input; |
| 99 | struct alsa_stream_out *active_output; |
| 100 | bool mic_mute; |
Niranjan Yadla | 672a346 | 2018-05-08 16:27:06 -0700 | [diff] [blame] | 101 | #ifdef ENABLE_XAF_DSP_DEVICE |
| 102 | struct xaf_dsp_device dsp_device; |
| 103 | int hifi_dsp_fd; |
| 104 | #endif |
Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | struct alsa_stream_out { |
| 108 | struct audio_stream_out stream; |
| 109 | |
| 110 | pthread_mutex_t lock; /* see note below on mutex acquisition order */ |
| 111 | struct pcm_config config; |
| 112 | struct pcm *pcm; |
| 113 | bool unavailable; |
| 114 | int standby; |
| 115 | struct alsa_audio_device *dev; |
| 116 | int write_threshold; |
| 117 | unsigned int written; |
| 118 | }; |
| 119 | |
Niranjan Yadla | 672a346 | 2018-05-08 16:27:06 -0700 | [diff] [blame] | 120 | #ifdef ENABLE_XAF_DSP_DEVICE |
| 121 | static int pcm_setup(void *p_pcm, struct alsa_audio_device *audio_device) |
| 122 | { |
| 123 | int param[6]; |
| 124 | |
| 125 | param[0] = XA_CODEC_CONFIG_PARAM_SAMPLE_RATE; |
| 126 | param[1] = audio_device->dsp_device.pb_format.sample_rate; |
| 127 | param[2] = XA_CODEC_CONFIG_PARAM_CHANNELS; |
| 128 | param[3] = audio_device->dsp_device.pb_format.channels; |
| 129 | param[4] = XA_CODEC_CONFIG_PARAM_PCM_WIDTH; |
| 130 | param[5] = audio_device->dsp_device.pb_format.pcm_width; |
| 131 | |
| 132 | XF_CHK_API(xaf_comp_set_config(p_pcm, 3, ¶m[0])); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | void xa_thread_exit_handler(int sig) |
| 138 | { |
| 139 | /* ...unused arg */ |
| 140 | (void) sig; |
| 141 | |
| 142 | pthread_exit(0); |
| 143 | } |
| 144 | |
| 145 | /*xtensa audio device init*/ |
| 146 | static int xa_device_init(struct alsa_audio_device *audio_device) |
| 147 | { |
| 148 | /* ...initialize playback format */ |
| 149 | audio_device->dsp_device.p_adev = NULL; |
| 150 | audio_device->dsp_device.pb_format.sample_rate = 48000; |
| 151 | audio_device->dsp_device.pb_format.channels = 2; |
| 152 | audio_device->dsp_device.pb_format.pcm_width = 16; |
| 153 | audio_device->dsp_device.xafinitdone = 0; |
| 154 | audio_frmwk_buf_size = 0; //unused |
| 155 | audio_comp_buf_size = 0; //unused |
| 156 | audio_device->dsp_device.num_comp = NUM_COMP_IN_GRAPH; |
| 157 | struct sigaction actions; |
| 158 | memset(&actions, 0, sizeof(actions)); |
| 159 | sigemptyset(&actions.sa_mask); |
| 160 | actions.sa_flags = 0; |
| 161 | actions.sa_handler = xa_thread_exit_handler; |
| 162 | sigaction(SIGUSR1,&actions,NULL); |
| 163 | /* ...initialize tracing facility */ |
| 164 | audio_device->dsp_device.xaf_started =1; |
| 165 | audio_device->dsp_device.dec_id = "audio-decoder/pcm"; |
| 166 | audio_device->dsp_device.dec_setup = pcm_setup; |
| 167 | audio_device->dsp_device.mem_handle = mem_init(); //initialize memory handler |
| 168 | XF_CHK_API(xaf_adev_open(&audio_device->dsp_device.p_adev, audio_frmwk_buf_size, audio_comp_buf_size, mem_malloc, mem_free)); |
| 169 | /* ...create decoder component */ |
| 170 | XF_CHK_API(xaf_comp_create(audio_device->dsp_device.p_adev, &audio_device->dsp_device.p_decoder, audio_device->dsp_device.dec_id, 1, 1, &audio_device->dsp_device.dec_inbuf[0], XAF_DECODER)); |
| 171 | XF_CHK_API(audio_device->dsp_device.dec_setup(audio_device->dsp_device.p_decoder,audio_device)); |
| 172 | |
| 173 | /* ...start decoder component */ |
| 174 | XF_CHK_API(xaf_comp_process(audio_device->dsp_device.p_adev, audio_device->dsp_device.p_decoder, NULL, 0, XAF_START_FLAG)); |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static int xa_device_run(struct audio_stream_out *stream, const void *buffer, size_t frame_size, size_t out_frames, size_t bytes) |
| 179 | { |
| 180 | struct alsa_stream_out *out = (struct alsa_stream_out *)stream; |
| 181 | struct alsa_audio_device *adev = out->dev; |
| 182 | int ret=0; |
| 183 | void *p_comp=adev->dsp_device.p_decoder; |
| 184 | xaf_comp_status comp_status; |
| 185 | memcpy(adev->dsp_device.dec_inbuf[0],buffer,bytes); |
| 186 | adev->dsp_device.read_length=bytes; |
| 187 | |
| 188 | if (adev->dsp_device.xafinitdone == 0) { |
| 189 | XF_CHK_API(xaf_comp_process(adev->dsp_device.p_adev, adev->dsp_device.p_decoder, adev->dsp_device.dec_inbuf[0], adev->dsp_device.read_length, XAF_INPUT_READY_FLAG)); |
| 190 | XF_CHK_API(xaf_comp_get_status(adev->dsp_device.p_adev, adev->dsp_device.p_decoder, &adev->dsp_device.dec_status, &adev->dsp_device.comp_info)); |
| 191 | ALOGE("PROXY:%s xaf_comp_get_status %d\n",__func__,adev->dsp_device.dec_status); |
| 192 | if (adev->dsp_device.dec_status == XAF_INIT_DONE) { |
| 193 | adev->dsp_device.xafinitdone = 1; |
| 194 | out->written += out_frames; |
| 195 | XF_CHK_API(xaf_comp_process(NULL, p_comp, NULL, 0, XAF_EXEC_FLAG)); |
| 196 | } |
| 197 | } else { |
| 198 | XF_CHK_API(xaf_comp_process(NULL, adev->dsp_device.p_decoder, adev->dsp_device.dec_inbuf[0], adev->dsp_device.read_length, XAF_INPUT_READY_FLAG)); |
| 199 | while (1) { |
| 200 | XF_CHK_API(xaf_comp_get_status(NULL, p_comp, &comp_status, &adev->dsp_device.comp_info)); |
| 201 | if (comp_status == XAF_EXEC_DONE) break; |
| 202 | if (comp_status == XAF_NEED_INPUT) { |
| 203 | ALOGV("PROXY:%s loop:XAF_NEED_INPUT\n",__func__); |
| 204 | break; |
| 205 | } |
| 206 | if (comp_status == XAF_OUTPUT_READY) { |
| 207 | void *p_buf = (void *)adev->dsp_device.comp_info.buf; |
| 208 | int size = adev->dsp_device.comp_info.length; |
| 209 | ret = pcm_mmap_write(out->pcm, p_buf, size); |
| 210 | if (ret == 0) { |
| 211 | out->written += out_frames; |
| 212 | } |
| 213 | XF_CHK_API(xaf_comp_process(NULL, adev->dsp_device.p_decoder, (void *)adev->dsp_device.comp_info.buf, adev->dsp_device.comp_info.length, XAF_NEED_OUTPUT_FLAG)); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | static int xa_device_close(struct alsa_audio_device *audio_device) |
| 221 | { |
| 222 | if (audio_device->dsp_device.xaf_started) { |
| 223 | xaf_comp_status comp_status; |
| 224 | audio_device->dsp_device.xaf_started=0; |
| 225 | while (1) { |
| 226 | XF_CHK_API(xaf_comp_get_status(NULL, audio_device->dsp_device.p_decoder, &comp_status, &audio_device->dsp_device.comp_info)); |
| 227 | ALOGV("PROXY:comp_status:%d,audio_device->dsp_device.comp_info.length:%d\n",(int)comp_status,audio_device->dsp_device.comp_info.length); |
| 228 | if (comp_status == XAF_EXEC_DONE) |
| 229 | break; |
| 230 | if (comp_status == XAF_NEED_INPUT) { |
| 231 | XF_CHK_API(xaf_comp_process(NULL, audio_device->dsp_device.p_decoder, NULL, 0, XAF_INPUT_OVER_FLAG)); |
| 232 | } |
| 233 | |
| 234 | if (comp_status == XAF_OUTPUT_READY) { |
| 235 | XF_CHK_API(xaf_comp_process(NULL, audio_device->dsp_device.p_decoder, (void *)audio_device->dsp_device.comp_info.buf, audio_device->dsp_device.comp_info.length, XAF_NEED_OUTPUT_FLAG)); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /* ...exec done, clean-up */ |
| 240 | XF_CHK_API(xaf_comp_delete(audio_device->dsp_device.p_decoder)); |
| 241 | XF_CHK_API(xaf_adev_close(audio_device->dsp_device.p_adev, 0 /*unused*/)); |
| 242 | mem_exit(); |
| 243 | XF_CHK_API(print_mem_mcps_info(audio_device->dsp_device.mem_handle, audio_device->dsp_device.num_comp)); |
| 244 | } |
| 245 | return 0; |
| 246 | } |
| 247 | #endif |
Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 248 | |
| 249 | /* must be called with hw device and output stream mutexes locked */ |
| 250 | static int start_output_stream(struct alsa_stream_out *out) |
| 251 | { |
| 252 | struct alsa_audio_device *adev = out->dev; |
| 253 | |
| 254 | if (out->unavailable) |
| 255 | return -ENODEV; |
| 256 | |
| 257 | /* default to low power: will be corrected in out_write if necessary before first write to |
| 258 | * tinyalsa. |
| 259 | */ |
| 260 | out->write_threshold = PLAYBACK_PERIOD_COUNT * PERIOD_SIZE; |
| 261 | out->config.start_threshold = PLAYBACK_PERIOD_START_THRESHOLD * PERIOD_SIZE; |
| 262 | out->config.avail_min = PERIOD_SIZE; |
| 263 | |
| 264 | out->pcm = pcm_open(CARD_OUT, PORT_CODEC, PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC, &out->config); |
| 265 | |
| 266 | if (!pcm_is_ready(out->pcm)) { |
| 267 | ALOGE("cannot open pcm_out driver: %s", pcm_get_error(out->pcm)); |
| 268 | pcm_close(out->pcm); |
| 269 | adev->active_output = NULL; |
| 270 | out->unavailable = true; |
| 271 | return -ENODEV; |
| 272 | } |
| 273 | |
| 274 | adev->active_output = out; |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 279 | { |
| 280 | struct alsa_stream_out *out = (struct alsa_stream_out *)stream; |
| 281 | return out->config.rate; |
| 282 | } |
| 283 | |
| 284 | static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 285 | { |
| 286 | ALOGV("out_set_sample_rate: %d", 0); |
| 287 | return -ENOSYS; |
| 288 | } |
| 289 | |
| 290 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 291 | { |
| 292 | ALOGV("out_get_buffer_size: %d", 4096); |
Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 293 | |
| 294 | /* return the closest majoring multiple of 16 frames, as |
| 295 | * audioflinger expects audio buffers to be a multiple of 16 frames */ |
| 296 | size_t size = PERIOD_SIZE; |
| 297 | size = ((size + 15) / 16) * 16; |
| 298 | return size * audio_stream_out_frame_size((struct audio_stream_out *)stream); |
| 299 | } |
| 300 | |
| 301 | static audio_channel_mask_t out_get_channels(const struct audio_stream *stream) |
| 302 | { |
| 303 | ALOGV("out_get_channels"); |
| 304 | struct alsa_stream_out *out = (struct alsa_stream_out *)stream; |
| 305 | return audio_channel_out_mask_from_count(out->config.channels); |
| 306 | } |
| 307 | |
| 308 | static audio_format_t out_get_format(const struct audio_stream *stream) |
| 309 | { |
| 310 | ALOGV("out_get_format"); |
| 311 | struct alsa_stream_out *out = (struct alsa_stream_out *)stream; |
| 312 | return audio_format_from_pcm_format(out->config.format); |
| 313 | } |
| 314 | |
| 315 | static int out_set_format(struct audio_stream *stream, audio_format_t format) |
| 316 | { |
| 317 | ALOGV("out_set_format: %d",format); |
| 318 | return -ENOSYS; |
| 319 | } |
| 320 | |
| 321 | static int do_output_standby(struct alsa_stream_out *out) |
| 322 | { |
| 323 | struct alsa_audio_device *adev = out->dev; |
| 324 | |
| 325 | if (!out->standby) { |
| 326 | pcm_close(out->pcm); |
| 327 | out->pcm = NULL; |
| 328 | adev->active_output = NULL; |
| 329 | out->standby = 1; |
| 330 | } |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static int out_standby(struct audio_stream *stream) |
| 335 | { |
| 336 | ALOGV("out_standby"); |
| 337 | struct alsa_stream_out *out = (struct alsa_stream_out *)stream; |
| 338 | int status; |
| 339 | |
| 340 | pthread_mutex_lock(&out->dev->lock); |
| 341 | pthread_mutex_lock(&out->lock); |
Niranjan Yadla | 672a346 | 2018-05-08 16:27:06 -0700 | [diff] [blame] | 342 | #ifdef ENABLE_XAF_DSP_DEVICE |
| 343 | xa_device_close(out->dev); |
| 344 | #endif |
Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 345 | status = do_output_standby(out); |
| 346 | pthread_mutex_unlock(&out->lock); |
| 347 | pthread_mutex_unlock(&out->dev->lock); |
| 348 | return status; |
| 349 | } |
| 350 | |
| 351 | static int out_dump(const struct audio_stream *stream, int fd) |
| 352 | { |
| 353 | ALOGV("out_dump"); |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 358 | { |
| 359 | ALOGV("out_set_parameters"); |
| 360 | struct alsa_stream_out *out = (struct alsa_stream_out *)stream; |
| 361 | struct alsa_audio_device *adev = out->dev; |
| 362 | struct str_parms *parms; |
Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 363 | char value[32]; |
| 364 | int ret, val = 0; |
| 365 | |
| 366 | parms = str_parms_create_str(kvpairs); |
| 367 | |
| 368 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); |
| 369 | if (ret >= 0) { |
| 370 | val = atoi(value); |
| 371 | pthread_mutex_lock(&adev->lock); |
| 372 | pthread_mutex_lock(&out->lock); |
| 373 | if (((adev->devices & AUDIO_DEVICE_OUT_ALL) != val) && (val != 0)) { |
| 374 | adev->devices &= ~AUDIO_DEVICE_OUT_ALL; |
| 375 | adev->devices |= val; |
| 376 | } |
| 377 | pthread_mutex_unlock(&out->lock); |
| 378 | pthread_mutex_unlock(&adev->lock); |
| 379 | } |
| 380 | |
| 381 | str_parms_destroy(parms); |
| 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | static char * out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 386 | { |
| 387 | ALOGV("out_get_parameters"); |
| 388 | return strdup(""); |
| 389 | } |
| 390 | |
| 391 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 392 | { |
| 393 | ALOGV("out_get_latency"); |
| 394 | struct alsa_stream_out *out = (struct alsa_stream_out *)stream; |
| 395 | return (PERIOD_SIZE * PLAYBACK_PERIOD_COUNT * 1000) / out->config.rate; |
| 396 | } |
| 397 | |
| 398 | static int out_set_volume(struct audio_stream_out *stream, float left, |
| 399 | float right) |
| 400 | { |
| 401 | ALOGV("out_set_volume: Left:%f Right:%f", left, right); |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, |
| 406 | size_t bytes) |
| 407 | { |
| 408 | int ret; |
| 409 | struct alsa_stream_out *out = (struct alsa_stream_out *)stream; |
| 410 | struct alsa_audio_device *adev = out->dev; |
| 411 | size_t frame_size = audio_stream_out_frame_size(stream); |
| 412 | size_t out_frames = bytes / frame_size; |
Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 413 | |
| 414 | /* acquiring hw device mutex systematically is useful if a low priority thread is waiting |
| 415 | * on the output stream mutex - e.g. executing select_mode() while holding the hw device |
| 416 | * mutex |
| 417 | */ |
| 418 | pthread_mutex_lock(&adev->lock); |
| 419 | pthread_mutex_lock(&out->lock); |
| 420 | if (out->standby) { |
Niranjan Yadla | 672a346 | 2018-05-08 16:27:06 -0700 | [diff] [blame] | 421 | #ifdef ENABLE_XAF_DSP_DEVICE |
| 422 | if (adev->hifi_dsp_fd >= 0) { |
| 423 | xa_device_init(adev); |
| 424 | } |
| 425 | #endif |
Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 426 | ret = start_output_stream(out); |
| 427 | if (ret != 0) { |
| 428 | pthread_mutex_unlock(&adev->lock); |
| 429 | goto exit; |
| 430 | } |
| 431 | out->standby = 0; |
| 432 | } |
| 433 | |
| 434 | pthread_mutex_unlock(&adev->lock); |
| 435 | |
Niranjan Yadla | 672a346 | 2018-05-08 16:27:06 -0700 | [diff] [blame] | 436 | #ifdef ENABLE_XAF_DSP_DEVICE |
| 437 | /*fallback to original audio processing*/ |
| 438 | if (adev->dsp_device.p_adev != NULL) { |
| 439 | ret = xa_device_run(stream, buffer,frame_size, out_frames, bytes); |
| 440 | } else { |
| 441 | #endif |
| 442 | ret = pcm_mmap_write(out->pcm, buffer, out_frames * frame_size); |
| 443 | if (ret == 0) { |
| 444 | out->written += out_frames; |
| 445 | } |
| 446 | #ifdef ENABLE_XAF_DSP_DEVICE |
Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 447 | } |
Niranjan Yadla | 672a346 | 2018-05-08 16:27:06 -0700 | [diff] [blame] | 448 | #endif |
Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 449 | exit: |
| 450 | pthread_mutex_unlock(&out->lock); |
| 451 | |
| 452 | if (ret != 0) { |
| 453 | usleep((int64_t)bytes * 1000000 / audio_stream_out_frame_size(stream) / |
| 454 | out_get_sample_rate(&stream->common)); |
| 455 | } |
| 456 | |
| 457 | return bytes; |
| 458 | } |
| 459 | |
| 460 | static int out_get_render_position(const struct audio_stream_out *stream, |
| 461 | uint32_t *dsp_frames) |
| 462 | { |
| 463 | *dsp_frames = 0; |
| 464 | ALOGV("out_get_render_position: dsp_frames: %p", dsp_frames); |
| 465 | return -EINVAL; |
| 466 | } |
| 467 | |
| 468 | static int out_get_presentation_position(const struct audio_stream_out *stream, |
| 469 | uint64_t *frames, struct timespec *timestamp) |
| 470 | { |
| 471 | struct alsa_stream_out *out = (struct alsa_stream_out *)stream; |
| 472 | int ret = -1; |
| 473 | |
| 474 | if (out->pcm) { |
| 475 | unsigned int avail; |
| 476 | if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) { |
| 477 | size_t kernel_buffer_size = out->config.period_size * out->config.period_count; |
| 478 | int64_t signed_frames = out->written - kernel_buffer_size + avail; |
| 479 | if (signed_frames >= 0) { |
| 480 | *frames = signed_frames; |
| 481 | ret = 0; |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | |
| 490 | static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 491 | { |
| 492 | ALOGV("out_add_audio_effect: %p", effect); |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 497 | { |
| 498 | ALOGV("out_remove_audio_effect: %p", effect); |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | static int out_get_next_write_timestamp(const struct audio_stream_out *stream, |
| 503 | int64_t *timestamp) |
| 504 | { |
| 505 | *timestamp = 0; |
| 506 | ALOGV("out_get_next_write_timestamp: %ld", (long int)(*timestamp)); |
| 507 | return -EINVAL; |
| 508 | } |
| 509 | |
| 510 | /** audio_stream_in implementation **/ |
| 511 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 512 | { |
| 513 | ALOGV("in_get_sample_rate"); |
| 514 | return 8000; |
| 515 | } |
| 516 | |
| 517 | static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 518 | { |
| 519 | ALOGV("in_set_sample_rate: %d", rate); |
| 520 | return -ENOSYS; |
| 521 | } |
| 522 | |
| 523 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 524 | { |
| 525 | ALOGV("in_get_buffer_size: %d", 320); |
| 526 | return 320; |
| 527 | } |
| 528 | |
| 529 | static audio_channel_mask_t in_get_channels(const struct audio_stream *stream) |
| 530 | { |
| 531 | ALOGV("in_get_channels: %d", AUDIO_CHANNEL_IN_MONO); |
| 532 | return AUDIO_CHANNEL_IN_MONO; |
| 533 | } |
| 534 | |
| 535 | static audio_format_t in_get_format(const struct audio_stream *stream) |
| 536 | { |
| 537 | return AUDIO_FORMAT_PCM_16_BIT; |
| 538 | } |
| 539 | |
| 540 | static int in_set_format(struct audio_stream *stream, audio_format_t format) |
| 541 | { |
| 542 | return -ENOSYS; |
| 543 | } |
| 544 | |
| 545 | static int in_standby(struct audio_stream *stream) |
| 546 | { |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | static int in_dump(const struct audio_stream *stream, int fd) |
| 551 | { |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 556 | { |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | static char * in_get_parameters(const struct audio_stream *stream, |
| 561 | const char *keys) |
| 562 | { |
| 563 | return strdup(""); |
| 564 | } |
| 565 | |
| 566 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
| 567 | { |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | static ssize_t in_read(struct audio_stream_in *stream, void* buffer, |
| 572 | size_t bytes) |
| 573 | { |
| 574 | ALOGV("in_read: bytes %zu", bytes); |
| 575 | /* XXX: fake timing for audio input */ |
| 576 | usleep((int64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) / |
| 577 | in_get_sample_rate(&stream->common)); |
| 578 | memset(buffer, 0, bytes); |
| 579 | return bytes; |
| 580 | } |
| 581 | |
| 582 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| 583 | { |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 588 | { |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 593 | { |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | static int adev_open_output_stream(struct audio_hw_device *dev, |
| 598 | audio_io_handle_t handle, |
| 599 | audio_devices_t devices, |
| 600 | audio_output_flags_t flags, |
| 601 | struct audio_config *config, |
| 602 | struct audio_stream_out **stream_out, |
| 603 | const char *address __unused) |
| 604 | { |
| 605 | ALOGV("adev_open_output_stream..."); |
| 606 | |
| 607 | struct alsa_audio_device *ladev = (struct alsa_audio_device *)dev; |
| 608 | struct alsa_stream_out *out; |
| 609 | struct pcm_params *params; |
| 610 | int ret = 0; |
| 611 | |
| 612 | params = pcm_params_get(CARD_OUT, PORT_CODEC, PCM_OUT); |
| 613 | if (!params) |
| 614 | return -ENOSYS; |
| 615 | |
| 616 | out = (struct alsa_stream_out *)calloc(1, sizeof(struct alsa_stream_out)); |
| 617 | if (!out) |
| 618 | return -ENOMEM; |
| 619 | |
| 620 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 621 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 622 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 623 | out->stream.common.get_channels = out_get_channels; |
| 624 | out->stream.common.get_format = out_get_format; |
| 625 | out->stream.common.set_format = out_set_format; |
| 626 | out->stream.common.standby = out_standby; |
| 627 | out->stream.common.dump = out_dump; |
| 628 | out->stream.common.set_parameters = out_set_parameters; |
| 629 | out->stream.common.get_parameters = out_get_parameters; |
| 630 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 631 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 632 | out->stream.get_latency = out_get_latency; |
| 633 | out->stream.set_volume = out_set_volume; |
| 634 | out->stream.write = out_write; |
| 635 | out->stream.get_render_position = out_get_render_position; |
| 636 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
| 637 | out->stream.get_presentation_position = out_get_presentation_position; |
| 638 | |
| 639 | out->config.channels = CHANNEL_STEREO; |
| 640 | out->config.rate = CODEC_SAMPLING_RATE; |
| 641 | out->config.format = PCM_FORMAT_S16_LE; |
| 642 | out->config.period_size = PERIOD_SIZE; |
| 643 | out->config.period_count = PLAYBACK_PERIOD_COUNT; |
| 644 | |
| 645 | if (out->config.rate != config->sample_rate || |
| 646 | audio_channel_count_from_out_mask(config->channel_mask) != CHANNEL_STEREO || |
| 647 | out->config.format != pcm_format_from_audio_format(config->format) ) { |
| 648 | config->sample_rate = out->config.rate; |
| 649 | config->format = audio_format_from_pcm_format(out->config.format); |
| 650 | config->channel_mask = audio_channel_out_mask_from_count(CHANNEL_STEREO); |
| 651 | ret = -EINVAL; |
| 652 | } |
| 653 | |
| 654 | ALOGI("adev_open_output_stream selects channels=%d rate=%d format=%d", |
| 655 | out->config.channels, out->config.rate, out->config.format); |
| 656 | |
| 657 | out->dev = ladev; |
| 658 | out->standby = 1; |
| 659 | out->unavailable = false; |
| 660 | |
| 661 | config->format = out_get_format(&out->stream.common); |
| 662 | config->channel_mask = out_get_channels(&out->stream.common); |
| 663 | config->sample_rate = out_get_sample_rate(&out->stream.common); |
| 664 | |
| 665 | *stream_out = &out->stream; |
| 666 | |
| 667 | /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */ |
| 668 | ret = 0; |
| 669 | |
| 670 | return ret; |
| 671 | } |
| 672 | |
| 673 | static void adev_close_output_stream(struct audio_hw_device *dev, |
| 674 | struct audio_stream_out *stream) |
| 675 | { |
| 676 | ALOGV("adev_close_output_stream..."); |
| 677 | free(stream); |
| 678 | } |
| 679 | |
| 680 | static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| 681 | { |
| 682 | ALOGV("adev_set_parameters"); |
| 683 | return -ENOSYS; |
| 684 | } |
| 685 | |
| 686 | static char * adev_get_parameters(const struct audio_hw_device *dev, |
| 687 | const char *keys) |
| 688 | { |
| 689 | ALOGV("adev_get_parameters"); |
| 690 | return strdup(""); |
| 691 | } |
| 692 | |
| 693 | static int adev_init_check(const struct audio_hw_device *dev) |
| 694 | { |
| 695 | ALOGV("adev_init_check"); |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| 700 | { |
| 701 | ALOGV("adev_set_voice_volume: %f", volume); |
| 702 | return -ENOSYS; |
| 703 | } |
| 704 | |
| 705 | static int adev_set_master_volume(struct audio_hw_device *dev, float volume) |
| 706 | { |
| 707 | ALOGV("adev_set_master_volume: %f", volume); |
| 708 | return -ENOSYS; |
| 709 | } |
| 710 | |
| 711 | static int adev_get_master_volume(struct audio_hw_device *dev, float *volume) |
| 712 | { |
| 713 | ALOGV("adev_get_master_volume: %f", *volume); |
| 714 | return -ENOSYS; |
| 715 | } |
| 716 | |
| 717 | static int adev_set_master_mute(struct audio_hw_device *dev, bool muted) |
| 718 | { |
| 719 | ALOGV("adev_set_master_mute: %d", muted); |
| 720 | return -ENOSYS; |
| 721 | } |
| 722 | |
| 723 | static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted) |
| 724 | { |
| 725 | ALOGV("adev_get_master_mute: %d", *muted); |
| 726 | return -ENOSYS; |
| 727 | } |
| 728 | |
| 729 | static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) |
| 730 | { |
| 731 | ALOGV("adev_set_mode: %d", mode); |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| 736 | { |
| 737 | ALOGV("adev_set_mic_mute: %d",state); |
| 738 | return -ENOSYS; |
| 739 | } |
| 740 | |
| 741 | static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| 742 | { |
| 743 | ALOGV("adev_get_mic_mute"); |
| 744 | return -ENOSYS; |
| 745 | } |
| 746 | |
| 747 | static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, |
| 748 | const struct audio_config *config) |
| 749 | { |
| 750 | ALOGV("adev_get_input_buffer_size: %d", 320); |
| 751 | return 320; |
| 752 | } |
| 753 | |
Dmitry Shmidt | 6b34f04 | 2017-11-29 13:23:01 -0800 | [diff] [blame] | 754 | static int adev_open_input_stream(struct audio_hw_device __unused *dev, |
Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 755 | audio_io_handle_t handle, |
| 756 | audio_devices_t devices, |
| 757 | struct audio_config *config, |
| 758 | struct audio_stream_in **stream_in, |
| 759 | audio_input_flags_t flags __unused, |
| 760 | const char *address __unused, |
| 761 | audio_source_t source __unused) |
| 762 | { |
Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 763 | struct stub_stream_in *in; |
Dmitry Shmidt | 6b34f04 | 2017-11-29 13:23:01 -0800 | [diff] [blame] | 764 | |
| 765 | ALOGV("adev_open_input_stream..."); |
Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 766 | |
| 767 | in = (struct stub_stream_in *)calloc(1, sizeof(struct stub_stream_in)); |
| 768 | if (!in) |
| 769 | return -ENOMEM; |
| 770 | |
| 771 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 772 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 773 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 774 | in->stream.common.get_channels = in_get_channels; |
| 775 | in->stream.common.get_format = in_get_format; |
| 776 | in->stream.common.set_format = in_set_format; |
| 777 | in->stream.common.standby = in_standby; |
| 778 | in->stream.common.dump = in_dump; |
| 779 | in->stream.common.set_parameters = in_set_parameters; |
| 780 | in->stream.common.get_parameters = in_get_parameters; |
| 781 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 782 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 783 | in->stream.set_gain = in_set_gain; |
| 784 | in->stream.read = in_read; |
| 785 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
| 786 | |
| 787 | *stream_in = &in->stream; |
| 788 | return 0; |
| 789 | } |
| 790 | |
| 791 | static void adev_close_input_stream(struct audio_hw_device *dev, |
| 792 | struct audio_stream_in *in) |
| 793 | { |
| 794 | ALOGV("adev_close_input_stream..."); |
| 795 | return; |
| 796 | } |
| 797 | |
| 798 | static int adev_dump(const audio_hw_device_t *device, int fd) |
| 799 | { |
| 800 | ALOGV("adev_dump"); |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | static int adev_close(hw_device_t *device) |
| 805 | { |
Niranjan Yadla | 672a346 | 2018-05-08 16:27:06 -0700 | [diff] [blame] | 806 | #ifdef ENABLE_XAF_DSP_DEVICE |
| 807 | struct alsa_audio_device *adev = (struct alsa_audio_device *)device; |
| 808 | #endif |
Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 809 | ALOGV("adev_close"); |
Niranjan Yadla | 672a346 | 2018-05-08 16:27:06 -0700 | [diff] [blame] | 810 | #ifdef ENABLE_XAF_DSP_DEVICE |
| 811 | if (adev->hifi_dsp_fd >= 0) |
| 812 | close(adev->hifi_dsp_fd); |
| 813 | #endif |
Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 814 | free(device); |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | static int adev_open(const hw_module_t* module, const char* name, |
| 819 | hw_device_t** device) |
| 820 | { |
Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 821 | struct alsa_audio_device *adev; |
Dmitry Shmidt | 6b34f04 | 2017-11-29 13:23:01 -0800 | [diff] [blame] | 822 | |
| 823 | ALOGV("adev_open: %s", name); |
Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 824 | |
| 825 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) |
| 826 | return -EINVAL; |
| 827 | |
| 828 | adev = calloc(1, sizeof(struct alsa_audio_device)); |
| 829 | if (!adev) |
| 830 | return -ENOMEM; |
| 831 | |
| 832 | adev->hw_device.common.tag = HARDWARE_DEVICE_TAG; |
| 833 | adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
| 834 | adev->hw_device.common.module = (struct hw_module_t *) module; |
| 835 | adev->hw_device.common.close = adev_close; |
| 836 | adev->hw_device.init_check = adev_init_check; |
| 837 | adev->hw_device.set_voice_volume = adev_set_voice_volume; |
| 838 | adev->hw_device.set_master_volume = adev_set_master_volume; |
| 839 | adev->hw_device.get_master_volume = adev_get_master_volume; |
| 840 | adev->hw_device.set_master_mute = adev_set_master_mute; |
| 841 | adev->hw_device.get_master_mute = adev_get_master_mute; |
| 842 | adev->hw_device.set_mode = adev_set_mode; |
| 843 | adev->hw_device.set_mic_mute = adev_set_mic_mute; |
| 844 | adev->hw_device.get_mic_mute = adev_get_mic_mute; |
| 845 | adev->hw_device.set_parameters = adev_set_parameters; |
| 846 | adev->hw_device.get_parameters = adev_get_parameters; |
| 847 | adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size; |
| 848 | adev->hw_device.open_output_stream = adev_open_output_stream; |
| 849 | adev->hw_device.close_output_stream = adev_close_output_stream; |
| 850 | adev->hw_device.open_input_stream = adev_open_input_stream; |
| 851 | adev->hw_device.close_input_stream = adev_close_input_stream; |
| 852 | adev->hw_device.dump = adev_dump; |
| 853 | |
| 854 | adev->devices = AUDIO_DEVICE_NONE; |
| 855 | |
| 856 | *device = &adev->hw_device.common; |
Niranjan Yadla | 672a346 | 2018-05-08 16:27:06 -0700 | [diff] [blame] | 857 | #ifdef ENABLE_XAF_DSP_DEVICE |
| 858 | adev->hifi_dsp_fd = open(HIFI_DSP_MISC_DRIVER, O_WRONLY, 0); |
| 859 | if (adev->hifi_dsp_fd < 0) { |
| 860 | ALOGW("hifi_dsp: Error opening device %d", errno); |
| 861 | } else { |
| 862 | ALOGI("hifi_dsp: Open device"); |
| 863 | } |
| 864 | #endif |
Vishal Bhoj | 2cf4939 | 2016-01-13 14:01:08 +0000 | [diff] [blame] | 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | static struct hw_module_methods_t hal_module_methods = { |
| 869 | .open = adev_open, |
| 870 | }; |
| 871 | |
| 872 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 873 | .common = { |
| 874 | .tag = HARDWARE_MODULE_TAG, |
| 875 | .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| 876 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 877 | .id = AUDIO_HARDWARE_MODULE_ID, |
| 878 | .name = "Hikey audio HW HAL", |
| 879 | .author = "The Android Open Source Project", |
| 880 | .methods = &hal_module_methods, |
| 881 | }, |
| 882 | }; |