You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
5.3 KiB

/*
* Copyright (c) Hisilicon Technologies Co., Ltd.. 2020-2020. All rights reserved.
* Description: audio phone
* Author: audio
* Create: 2020-10-28
*/
#include "audio_phone.h"
#include <log/log.h>
#if (UAPI_VERSION_CODE == UAPI_VERSION(1, 0))
#include "uapi_system.h"
#define PHONE_TRACK_PREGAIN 10 // 10db pregain
static td_void track_deinit(const aiao_ctx *ctx)
{
td_s32 ret;
ret = uapi_snd_destroy_track(ctx->track);
if (ret != TD_SUCCESS) {
ALOGE("call uapi_snd_create_track failed(0x%x)", ret);
}
}
static td_s32 track_init(const aiao_ctx *ctx)
{
td_s32 ret;
uapi_audio_track_attr track_attr;
uapi_snd_preci_gain preci_gain;
ret = uapi_snd_get_default_track_attr(UAPI_SND_TRACK_TYPE_SLAVE, &track_attr);
if (ret != TD_SUCCESS) {
ALOGE("call uapi_snd_get_default_track_attr failed(0x%x)", ret);
return ret;
}
ret = uapi_snd_create_track(ctx->snd, &track_attr, (td_handle*)(&ctx->track));
if (ret != TD_SUCCESS) {
ALOGE("call uapi_snd_create_track failed(0x%x)", ret);
return ret;
}
// voice in call too small give 10 pregain
preci_gain.integer = PHONE_TRACK_PREGAIN;
preci_gain.decimal = 0;
ret = uapi_snd_set_track_prescale(ctx->track, &preci_gain);
if (ret != TD_SUCCESS) {
ALOGE("call uapi_snd_set_track_prescale failed(0x%x", ret);
return ret;
}
return TD_SUCCESS;
}
static td_void ai_deinit(const aiao_ctx *ctx)
{
td_s32 ret;
ret = uapi_ai_destroy(ctx->ai);
if (ret != TD_SUCCESS) {
ALOGE("call uapi_ai_destroy failed\n");
return;
}
ret = uapi_ai_deinit();
if (ret != TD_SUCCESS) {
ALOGE("call uapi_ai_deinit failed\n");
}
}
static td_s32 ai_init(const aiao_ctx *ctx)
{
td_s32 ret;
uapi_ai_attr ai_attr;
ret = uapi_ai_init();
if (ret != TD_SUCCESS) {
ALOGE("call uapi_ai_init failed(0x%x)", ret);
return ret;
}
ret = uapi_ai_get_default_attr(ctx->ai_port, &ai_attr);
if (ret != TD_SUCCESS) {
ALOGE("call uapi_ai_get_default_attr failed\n");
goto ai_deinit;
}
ai_attr.pcm_frame_max_num = 0x8; // 8: max channels
ai_attr.sample_rate = UAPI_AUDIO_SAMPLE_RATE_16K;
ai_attr.attr.i2s_attr.attr.bit_depth = UAPI_AUDIO_BIT_DEPTH_16;
ai_attr.attr.i2s_attr.attr.channels = UAPI_AUDIO_CHANNEL_1;
ai_attr.attr.i2s_attr.attr.master = TD_FALSE;
ai_attr.attr.i2s_attr.attr.i2s_mode = UAPI_AUDIO_I2S_PCM_MODE;
ai_attr.attr.i2s_attr.attr.pcm_delay_cycle = UAPI_AUDIO_I2S_PCM_1_DELAY;
ai_attr.attr.i2s_attr.attr.bclk_sel = UAPI_AUDIO_I2S_BCLK_8_DIV;
ai_attr.attr.i2s_attr.attr.mclk_sel = UAPI_AUDIO_I2S_MCLK_256_FS;
ret = uapi_ai_create(ctx->ai_port, &ai_attr, (td_handle*)(&ctx->ai));
if (ret != TD_SUCCESS) {
ALOGE("call uapi_ai_create failed\n");
goto ai_deinit;
}
return TD_SUCCESS;
ai_deinit:
(td_void)uapi_ai_deinit();
return ret;
}
static td_void route_stop(const aiao_ctx *ctx)
{
td_s32 ret;
ret = uapi_ai_set_enable(ctx->ai, TD_FALSE);
if (ret != TD_SUCCESS) {
ALOGE("call uapi_ai_set_enable failed(0x%x)\n", ret);
return;
}
ret = uapi_snd_detach(ctx->track, ctx->ai);
if (ret != TD_SUCCESS) {
ALOGE("call uapi_snd_detach failed(0x%x)\n", ret);
}
}
static td_s32 route_start(const aiao_ctx *ctx)
{
td_s32 ret;
ret = uapi_snd_attach(ctx->track, ctx->ai);
if (ret != TD_SUCCESS) {
ALOGE("call uapi_snd_attach failed.\n");
return ret;
}
ret = uapi_ai_set_enable(ctx->ai, TD_TRUE);
if (ret != TD_SUCCESS) {
ALOGE("call uapi_snd_attach failed.\n");
goto out;
}
return TD_SUCCESS;
out:
(td_void)uapi_snd_detach(ctx->track, ctx->ai);
return ret;
}
static const struct {
td_s32 (*init)(const aiao_ctx *ctx);
td_void (*deinit)(const aiao_ctx *ctx);
} g_sample_module[] = {
{ai_init, ai_deinit},
{track_init, track_deinit},
{route_start, route_stop},
};
td_s32 create_down_link(aiao_ctx *ctx)
{
td_s32 ret;
td_s32 module;
const td_s32 num = (td_s32)(sizeof(g_sample_module) / sizeof(g_sample_module[0]));
if (ctx == NULL) {
return 0;
}
ctx->snd = UAPI_SND_0;
ctx->ai_port = UAPI_AI_PORT_I2S0;
ret = uapi_sys_init();
if (ret != TD_SUCCESS) {
ALOGE("call uapi_sys_init failed(0x%x)", ret);
return ret;
}
for (module = 0; module < num; module++) {
ret = g_sample_module[module].init(ctx);
if (ret != TD_SUCCESS) {
ALOGE("call init failed(0x%x)", ret);
goto out;
}
}
return TD_SUCCESS;
out:
module--;
for (; module >= 0; module--) {
g_sample_module[module].deinit(ctx);
}
return ret;
}
td_void destroy_down_link(const aiao_ctx *ctx)
{
td_s32 ret;
td_s32 module;
const td_s32 num = (td_s32)(sizeof(g_sample_module) / sizeof(g_sample_module[0]));
for (module = num - 1; module < num && module >= 0; module--) {
g_sample_module[module].deinit(ctx);
}
ret = uapi_sys_deinit();
if (ret != TD_SUCCESS) {
ALOGE("call uapi_sys_deinit failed(0x%x)", ret);
}
}
#endif