/* * Copyright (c) Hisilicon Technologies Co., Ltd. 2020-2020. All rights reserved. * Description: audio hal primary input source file. * Author: Hisilicon * Create: 2020-05-11 * Notes: NA * History: 2020-05-11 yinyingcai for CodingStyle */ #define LOG_TAG "audio_hal_hpi" #define LOG_NDEBUG 0 #include "hpi.h" #include #include #include #include "vnaudio_dbg.h" #define AI_CARD_ID 0 #define AI_DEVICE_ID 0 int hpi_create(struct hpi_data **pphpi) { int ret; struct hpi_data* hpi = (struct hpi_data *)malloc(sizeof(struct hpi_data)); if (hpi == NULL) { ALOGE("%s: malloc failed", __func__); return -ENOMEM; } ret = memset_s(hpi, sizeof(struct hpi_data), 0, sizeof(struct hpi_data)); if (ret != 0) { ALOGE("memset_s hpi failed(0x%x)", ret); } hpi->in_card = AI_CARD_ID; hpi->in_device = AI_DEVICE_ID; hpi->fd = NULL; *pphpi = hpi; return 0; } int hpi_open(struct hpi_data *hpi) { int ret; char value[PROPERTY_VALUE_MAX]; ALOGD("pcm_open card:device(hw:%d:%d)\n", hpi->in_card, hpi->in_device); ALOGD("config channels:%d rate:%d period_size=%d period_count=%d format=%x", hpi->in->config.channels, hpi->in->config.rate, hpi->in->config.period_size, hpi->in->config.period_count, hpi->in->config.format); hpi->pcm = pcm_open((unsigned int)hpi->in_card, (unsigned int)hpi->in_device, PCM_IN, &hpi->in->config); if (hpi->pcm == NULL || pcm_is_ready(hpi->pcm) == 0) { if (hpi->pcm != NULL) { ALOGE("pcm_open() failed: %s", pcm_get_error(hpi->pcm)); pcm_close(hpi->pcm); hpi->pcm = NULL; } return -ENOMEM; } ret = memset_s(value, PROPERTY_VALUE_MAX, 0, PROPERTY_VALUE_MAX); if (ret != 0) { ALOGE("memset_s value failed(0x%x)", ret); } property_get(PROPERTY_FILE_HPI_RECORD, value, ""); ALOGD("%s: %s is %s", __func__, PROPERTY_FILE_HPI_RECORD, value); if (strncmp(value, "true", strlen("true")) == 0) { if (hpi->fd == NULL) { hpi->fd = fopen(HPI_RECORD_FILE, "ab"); if (hpi->fd != NULL) { ALOGD("%s: enable save %s", __func__, HPI_RECORD_FILE); } else { ALOGD("%s: %s can not be created!", __func__, HPI_RECORD_FILE); } } } else { ALOGV("%s: disable save %s", __func__, HPI_RECORD_FILE); if (hpi->fd != NULL) { fclose(hpi->fd); hpi->fd = NULL; } } return 0; } int hpi_close(struct hpi_data *hpi) { ALOGD("%s: device(hw:%d:%d)\n", __func__, hpi->in_card, hpi->in_device); if (hpi->pcm != NULL) { pcm_close(hpi->pcm); hpi->pcm = NULL; } if (hpi->fd != NULL) { fclose(hpi->fd); hpi->fd = NULL; } return 0; } ssize_t hpi_read(struct hpi_data *hpi, const uint8_t* buffer, size_t bytes) { int ret = -1; if (hpi->pcm == NULL) { ALOGE("%s: pcm Null pointer", __func__); return ret; } ret = pcm_read(hpi->pcm, (void*)buffer, bytes); if (ret == 0) { hpi->frames_read += bytes / (hpi->in->config.channels * sizeof(short)); if (hpi->fd != NULL) { size_t count; count = fwrite(buffer, 1, bytes, hpi->fd); if (count < bytes) { ALOGE("%s: not save all data! expect=%d actual=%d", __func__, bytes, count); } } } else { ALOGE("%s:%d byte written failed", __func__, bytes); } return 0; } void hpi_destroy(struct hpi_data* hpi) { ALOGD("in function %s, handle(%d)", __func__, hpi->in->handle); if (hpi->fd != NULL) { fclose(hpi->fd); hpi->fd = NULL; } if (hpi->pcm != NULL) { pcm_close(hpi->pcm); hpi->pcm = NULL; } free(hpi); }