From 633f7cc9db78fb663e49c679921ef9bbfbe7cbc4 Mon Sep 17 00:00:00 2001 From: xue <2144439972@qq.com> Date: Thu, 11 Dec 2025 16:58:34 +0800 Subject: [PATCH] =?utf8?q?=E4=BF=AE=E6=94=B9=E6=94=AF=E6=8C=81=E4=B8=A4?= =?utf8?q?=E8=B7=AF=E4=B8=8A=E8=A1=8C=E5=BD=95=E9=9F=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../brandy/bluetooth/bluetooth_sub_port.c | 36 +++++----- .../brandy/common/include/plugin_common.h | 2 +- .../plugins/brandy/common/src/plugin_common.c | 9 +-- .../brandy/primary/primary_sub_port_in.c | 65 ++++++++++++++----- .../brandy/primary/primary_sub_port_out.c | 2 +- 5 files changed, 74 insertions(+), 40 deletions(-) diff --git a/code/sdk/middleware/services/media/hal/audio/plugins/brandy/bluetooth/bluetooth_sub_port.c b/code/sdk/middleware/services/media/hal/audio/plugins/brandy/bluetooth/bluetooth_sub_port.c index 4687d8c4f..ac203a3b4 100644 --- a/code/sdk/middleware/services/media/hal/audio/plugins/brandy/bluetooth/bluetooth_sub_port.c +++ b/code/sdk/middleware/services/media/hal/audio/plugins/brandy/bluetooth/bluetooth_sub_port.c @@ -492,7 +492,7 @@ static int32_t SubPortTrackInit(SubPortContext *portCtx, const struct PortPlugin portTrackCtx->common.GetVolume = AudioBluetoothSubPortTrackGetVolume; portTrackCtx->common.Invoke = AudioBluetoothSubPortInvoke; portTrackCtx->common.RegCallback = AudioBluetoothSubPortTrackRegCallback; - portTrackCtx->isRaw = IsRawFormat(inputAttr->audioFormat); + portTrackCtx->isRaw = false; portTrackCtx->subPortCtx = portCtx; portTrackCtx->isHandlePublicResource = true; MEDIA_HAL_LOGD(MODULE_NAME, "success"); @@ -1411,10 +1411,10 @@ static int32_t PauseTrackInBluetoothA2DPSourceNormalScene(SubPortTrackContext *p #ifdef MINI_MEMORY_SUPPORT MediaHalMutexLock(portTrackCtx->subPortCtx->a2dpTrackMutex); int32_t ret = SetPause(portTrackCtx->trackId, true); - if (ret != MEDIA_HAL_OK) { - MEDIA_HAL_LOGE(MODULE_NAME, " pause track failed"); - MediaHalMutexUnLock(portTrackCtx->subPortCtx->a2dpTrackMutex); - return MEDIA_HAL_ERR; + if (ret != MEDIA_HAL_OK) { + MEDIA_HAL_LOGE(MODULE_NAME, " pause track failed"); + MediaHalMutexUnLock(portTrackCtx->subPortCtx->a2dpTrackMutex); + return MEDIA_HAL_ERR; } portTrackCtx->isHandlePublicResource = IsNeedHandlePublicResource(); // pick up bluetooth voice call when bluetooth music playback @@ -1594,20 +1594,20 @@ static int32_t ResumeTrackInBluetoothA2DPSourceNormalScene(SubPortTrackContext * MediaHalMutexLock(portTrackCtx->subPortCtx->a2dpTrackMutex); portTrackCtx->isHandlePublicResource = IsNeedHandlePublicResource(); int32_t ret = SetPause(portTrackCtx->trackId, false); - if (ret != MEDIA_HAL_OK) { - MEDIA_HAL_LOGE(MODULE_NAME, " resume track failed"); - MediaHalMutexUnLock(portTrackCtx->subPortCtx->a2dpTrackMutex); - return MEDIA_HAL_ERR; + if (ret != MEDIA_HAL_OK) { + MEDIA_HAL_LOGE(MODULE_NAME, " resume track failed"); + MediaHalMutexUnLock(portTrackCtx->subPortCtx->a2dpTrackMutex); + return MEDIA_HAL_ERR; } // 该判断放在设置状态后面的原因:两路混音场景,第一路恢复播放,需要重新搭建通路, // 第二路恢复播放,不需要搭建通路,如果判断放在前面,就会直接返回,不会设置状态, // 因此,当前代码逻辑简单理解,先设置状态,再判断是否搭建通路 ret = AudioSDKOpenInternal(portTrackCtx); - if (ret != MEDIA_HAL_OK) { - MEDIA_HAL_LOGE(MODULE_NAME, "audio open failed"); - MediaHalMutexUnLock(portTrackCtx->subPortCtx->a2dpTrackMutex); - return ret; + if (ret != MEDIA_HAL_OK) { + MEDIA_HAL_LOGE(MODULE_NAME, "audio open failed"); + MediaHalMutexUnLock(portTrackCtx->subPortCtx->a2dpTrackMutex); + return ret; } ret = AudioSDKAttachInternal(portTrackCtx); @@ -2847,9 +2847,9 @@ int32_t AudioBluetoothSubPortCreateTrack(AudioHandle portHandle, const struct Po int32_t trackCount = -1; MediaHalMutexLock(portCtx->a2dpTrackMutex); int32_t trackId = SubPortGetFreeTrack(&trackCount); - if (trackId == INVALID_ID) { - MediaHalMutexUnLock(portCtx->a2dpTrackMutex); - return MEDIA_HAL_INVALID_PARAM; + if (trackId == INVALID_ID) { + MediaHalMutexUnLock(portCtx->a2dpTrackMutex); + return MEDIA_HAL_INVALID_PARAM; } int32_t ret = MEDIA_HAL_ERR; SubPortTrackContext *portTrackCtx = (SubPortTrackContext *)malloc(sizeof(SubPortTrackContext)); @@ -2883,9 +2883,9 @@ int32_t AudioBluetoothSubPortCreateTrack(AudioHandle portHandle, const struct Po portCtx->portTrack[trackId] = portTrackCtx; *trackHandle = &portTrackCtx->common; /* A2dp sink 和 sco 不占用蓝牙资源和aenc,所以这里默认就是暂停的状态 */ - if (portTrackCtx->workScene == BLUETOOTH_IN_A2DP_NORMAL || + if (portTrackCtx->workScene == BLUETOOTH_IN_A2DP_NORMAL || portTrackCtx->workScene == BLUETOOTH_SCO) { - SetPause(trackId, true); + SetPause(trackId, true); } MediaHalMutexUnLock(portCtx->a2dpTrackMutex); return MEDIA_HAL_OK; diff --git a/code/sdk/middleware/services/media/hal/audio/plugins/brandy/common/include/plugin_common.h b/code/sdk/middleware/services/media/hal/audio/plugins/brandy/common/include/plugin_common.h index 53d8460a9..44d92d7b3 100644 --- a/code/sdk/middleware/services/media/hal/audio/plugins/brandy/common/include/plugin_common.h +++ b/code/sdk/middleware/services/media/hal/audio/plugins/brandy/common/include/plugin_common.h @@ -65,7 +65,7 @@ typedef struct { void VolumePercentToDb(float volume, int32_t *db); -bool IsRawFormat(enum AudioFormat audioFormat); +bool IsRawFormat(struct PortPluginAttr inputAttr); #ifdef AUDIO_SDK_SUPPORT diff --git a/code/sdk/middleware/services/media/hal/audio/plugins/brandy/common/src/plugin_common.c b/code/sdk/middleware/services/media/hal/audio/plugins/brandy/common/src/plugin_common.c index 16d43ef93..fee0715ff 100644 --- a/code/sdk/middleware/services/media/hal/audio/plugins/brandy/common/src/plugin_common.c +++ b/code/sdk/middleware/services/media/hal/audio/plugins/brandy/common/src/plugin_common.c @@ -38,6 +38,7 @@ extern "C" { #define MODULE_NAME "AudioPortPluginCommon" #define WAIT_CREATE_AENC_LOOP_CNT 100 #define WAIT_CREATE_AENC_SUCCESS_US 10000 +#define DEFAULT_SAMPLERATE 16000 static bool g_audioSdkIsInited = false; static int32_t g_isCreateAencCount = 0; @@ -65,11 +66,11 @@ void VolumePercentToDb(float volume, int32_t *db) *db = (int32_t)tmpDb; } -bool IsRawFormat(enum AudioFormat audioFormat) +bool IsRawFormat(struct PortPluginAttr inputAttr) { - // The 24-bit PCM is processed as the ES stream and decoded by the ADEC. - // The 16-bit PCM is processed as the ES stream for BT SCO msbc and cvsd pipeline same. - if (audioFormat == AUDIO_FORMAT_PCM_8_BIT || audioFormat == AUDIO_FORMAT_PCM_32_BIT) { + if (inputAttr.audioFormat == AUDIO_FORMAT_PCM_16_BIT && + inputAttr.sampleRate == DEFAULT_SAMPLERATE && + inputAttr.channelCount == 1) { return true; } return false; diff --git a/code/sdk/middleware/services/media/hal/audio/plugins/brandy/primary/primary_sub_port_in.c b/code/sdk/middleware/services/media/hal/audio/plugins/brandy/primary/primary_sub_port_in.c index 1ca8cf7d0..7351283eb 100644 --- a/code/sdk/middleware/services/media/hal/audio/plugins/brandy/primary/primary_sub_port_in.c +++ b/code/sdk/middleware/services/media/hal/audio/plugins/brandy/primary/primary_sub_port_in.c @@ -71,7 +71,7 @@ typedef struct { /* attr context */ bool isRaw; struct PortPluginAttr inputAttr; - } SubPortTrackInContext; +} SubPortTrackInContext; typedef struct { #ifdef AUDIO_SDK_SUPPORT @@ -154,7 +154,7 @@ static int32_t SubPortTrackInInit(SubPortTrackInContext *portTrackInCtx, const s portTrackInCtx->common.SetVolume = AudioPrimarySubPortInSetVolume; portTrackInCtx->common.GetVolume = AudioPrimarySubPortInGetVolume; portTrackInCtx->common.Invoke = AudioPrimarySubPortInInvoke; - MEDIA_HAL_LOGD(MODULE_NAME, "success"); + MEDIA_HAL_LOGD(MODULE_NAME, "success"); return MEDIA_HAL_OK; } @@ -267,7 +267,7 @@ static int32_t AudioSDKOpenInternal(SubPortTrackInContext *portTrackInCtx) aencAttr.param.interleaved = true; if (inputAttrTemp.type == AUDIO_IN_MEDIA) { aencAttr.frame_mode_output = TD_TRUE; - } + } aencAttr.param.channels = inputAttrTemp.channelCount; aencAttr.param.bit_depth = inputAttrTemp.bitWidth; aencAttr.param.sample_rate = inputAttrTemp.sampleRate; @@ -368,15 +368,33 @@ AI_DETACH_AENC: static int32_t EstablishPipeline(td_handle aiHandle, SubPortTrackInContext *portTrackInCtx) { int32_t ret; - portTrackInCtx->isRaw = IsRawFormat(portTrackInCtx->inputAttr.audioFormat); + portTrackInCtx->isRaw = IsRawFormat(portTrackInCtx->inputAttr); if (portTrackInCtx->isRaw) { ret = AudioSDKAdpOpen(&portTrackInCtx->outputAdpHandle); CHK_FAILED_RETURN(ret, MEDIA_HAL_OK, ret, "output adp open failed"); +#ifdef AUDIO_SEA_SUPPORT + ret = AudioSDKSeaOpen(&portTrackInCtx->seaHandle, portTrackInCtx->inputAttr); + if (ret != MEDIA_HAL_OK) { + MEDIA_HAL_LOGE(MODULE_NAME, "sea open failed"); + goto ADP_CLOSE; + } + ret = AudioSDKAttach(AUDIO_MOD_AI_SEA, aiHandle, portTrackInCtx->seaHandle); + if (ret != MEDIA_HAL_OK) { + MEDIA_HAL_LOGE(MODULE_NAME, "ai attach sea failed!"); + goto SEA_CLOSE; + } + ret = AudioSDKAttach(AUDIO_MOD_SEA, portTrackInCtx->seaHandle, portTrackInCtx->outputAdpHandle); + if (ret != MEDIA_HAL_OK) { + MEDIA_HAL_LOGE(MODULE_NAME, "sea attach aenc failed"); + goto AI_DETACH_SEA; + } +#else ret = AudioSDKAttach(AUDIO_MOD_AI, aiHandle, portTrackInCtx->outputAdpHandle); if (ret != MEDIA_HAL_OK) { MEDIA_HAL_LOGE(MODULE_NAME, "ai attach adp failed"); goto ADP_CLOSE; } +#endif } else { ret = AudioSDKOpenInternal(portTrackInCtx); CHK_FAILED_RETURN(ret, MEDIA_HAL_OK, ret, "AudioSDKOpenInternal failed"); @@ -393,6 +411,14 @@ static int32_t EstablishPipeline(td_handle aiHandle, SubPortTrackInContext *port AUDIO_SDK_CLOSE: (void)AudioSDKCloseInternal(portTrackInCtx); +#ifdef AUDIO_SEA_SUPPORT + if (portTrackInCtx->isRaw) { +AI_DETACH_SEA: + (void)AudioSDKDetach(AUDIO_MOD_AI_SEA, portTrackInCtx->aiHandle, portTrackInCtx->seaHandle); +SEA_CLOSE: + AudioSDKSeaClose(portTrackInCtx->seaHandle); +#endif + } ADP_CLOSE: (void)AudioSDKAdpClose(portTrackInCtx->outputAdpHandle); return ret; @@ -403,10 +429,18 @@ static int32_t AIDetach(const SubPortTrackInContext *portTrackInCtx) { #ifdef AUDIO_SDK_SUPPORT int32_t ret; - if (portTrackInCtx->isRaw) { +#ifdef AUDIO_SEA_SUPPORT + ret = AudioSDKDetach(AUDIO_MOD_AI_SEA, portTrackInCtx->aiHandle, portTrackInCtx->seaHandle); + CHK_FAILED_NO_RETURN(ret, MEDIA_HAL_OK, "ai detach sea failed"); + ret = AudioSDKDetach(AUDIO_MOD_SEA, portTrackInCtx->seaHandle, portTrackInCtx->outputAdpHandle); + CHK_FAILED_NO_RETURN(ret, MEDIA_HAL_OK, "sea detach outputAdpHandle failed"); + ret = AudioSDKSeaClose(portTrackInCtx->seaHandle); + CHK_FAILED_NO_RETURN(ret, MEDIA_HAL_OK, "sea close failed"); +#else ret = AudioSDKDetach(AUDIO_MOD_AI, portTrackInCtx->aiHandle, portTrackInCtx->outputAdpHandle); CHK_FAILED_NO_RETURN(ret, MEDIA_HAL_OK, "ai detach adp failed"); +#endif } else { #ifdef AUDIO_SEA_SUPPORT ret = AudioSDKDetach(AUDIO_MOD_AI_SEA, portTrackInCtx->aiHandle, portTrackInCtx->seaHandle); @@ -455,7 +489,6 @@ int32_t AudioPrimarySubPortInEnableTrack(AudioHandle trackHandle) MEDIA_HAL_DOFUNC_TIME(ret = uapi_ai_start(portTrackInCtx->aiHandle)); CHK_FAILED_RETURN(ret, MEDIA_HAL_OK, ret, "uapi_ai_start failed"); } - if (!portTrackInCtx->isRaw) { #ifdef AUDIO_SEA_SUPPORT if (!isMultipleSeaOpen()) { MEDIA_HAL_DOFUNC_TIME(ret = uapi_sea_start(portTrackInCtx->seaHandle)); @@ -465,6 +498,7 @@ int32_t AudioPrimarySubPortInEnableTrack(AudioHandle trackHandle) } } #endif + if (!portTrackInCtx->isRaw) { MEDIA_HAL_DOFUNC_TIME(ret = uapi_aenc_start(portTrackInCtx->aencHandle)); if (ret != MEDIA_HAL_OK) { MEDIA_HAL_LOGI(MODULE_NAME, "uapi_aenc_start failed"); @@ -495,20 +529,18 @@ int32_t AudioPrimarySubPortInDisableTrack(AudioHandle trackHandle) { #ifdef AUDIO_SDK_SUPPORT SubPortTrackInContext *portTrackInCtx = (SubPortTrackInContext *)trackHandle; - int32_t ret; if (!isMultipleAIOpen()) { MEDIA_HAL_DOFUNC_TIME(ret = uapi_ai_stop(portTrackInCtx->aiHandle)); CHK_FAILED_RETURN(ret, MEDIA_HAL_OK, ret, "uapi_ai_stop failed"); } - - if (!portTrackInCtx->isRaw) { #ifdef AUDIO_SEA_SUPPORT if (!isMultipleSeaOpen()) { MEDIA_HAL_DOFUNC_TIME(ret = uapi_sea_stop(portTrackInCtx->seaHandle)); CHK_FAILED_RETURN(ret, MEDIA_HAL_OK, ret, "uapi_sea_stop failed"); } #endif + if (!portTrackInCtx->isRaw) { MEDIA_HAL_DOFUNC_TIME(ret = uapi_aenc_stop(portTrackInCtx->aencHandle)); CHK_FAILED_RETURN(ret, MEDIA_HAL_OK, ret, "uapi_aenc_stop failed"); } @@ -527,13 +559,13 @@ static int32_t CheckParam(SubPortTrackInContext *portTrackInCtx, uint8_t *buffer MEDIA_HAL_LOGE(MODULE_NAME, "input buffer too small, actual: %llu vs except: %u", requestBytes, audioStream.size); return MEDIA_HAL_INVALID_PARAM; - } - + } + if (requestBytes > SIZE_MAX) { MEDIA_HAL_LOGE(MODULE_NAME, "input buffer size overflow"); return MEDIA_HAL_INVALID_PARAM; } - + if (memcpy_s(buffer, (size_t)requestBytes, audioStream.data, audioStream.size) != EOK) { MEDIA_HAL_LOGE(MODULE_NAME, "memcpy_s failed"); return MEDIA_HAL_ERR; @@ -567,11 +599,11 @@ int32_t AudioPrimarySubPortInAcquireFrame(AudioHandle trackHandle, uint8_t *buff *replyBytes = audioStream.size; ret = CheckParam(portTrackInCtx, buffer, requestBytes, audioStream); if (ret != MEDIA_HAL_OK) { - MEDIA_HAL_LOGE(MODULE_NAME, "param is invalid"); + MEDIA_HAL_LOGE(MODULE_NAME, "param is invalid"); (void)uapi_adp_release_stream(portTrackInCtx->outputAdpHandle, &audioStream); return ret; } - + ret = uapi_adp_release_stream(portTrackInCtx->outputAdpHandle, &audioStream); if (ret != MEDIA_HAL_OK) { MEDIA_HAL_LOGE(MODULE_NAME, "release stream failed: 0x%x", ret); @@ -896,12 +928,13 @@ FREE_TRACK_IN: int32_t AudioPrimarySubPortInDestroyTrack(AudioHandle portHandle, AudioHandle trackHandle) { #ifdef AUDIO_SDK_SUPPORT - SubPortTrackInContext *portTrackInCtx = (SubPortTrackInContext *)trackHandle; + SubPortInContext *portInCtx = (SubPortInContext *)portHandle; + SubPortTrackInContext *portTrackInCtx = (SubPortTrackInContext *)trackHandle; int32_t ret = AIDetach(portTrackInCtx); CHK_FAILED_NO_RETURN(ret, MEDIA_HAL_OK, "AIDetach failed"); - ret = AIDevClose(portTrackInCtx->aiHandle); + ret = AIDevClose(portInCtx->aiHandle); CHK_FAILED_NO_RETURN(ret, MEDIA_HAL_OK, "AIDevClose failed"); free(portTrackInCtx); diff --git a/code/sdk/middleware/services/media/hal/audio/plugins/brandy/primary/primary_sub_port_out.c b/code/sdk/middleware/services/media/hal/audio/plugins/brandy/primary/primary_sub_port_out.c index 36814369f..aa40c37e7 100644 --- a/code/sdk/middleware/services/media/hal/audio/plugins/brandy/primary/primary_sub_port_out.c +++ b/code/sdk/middleware/services/media/hal/audio/plugins/brandy/primary/primary_sub_port_out.c @@ -450,7 +450,7 @@ static int32_t SndCreateTrackFromFormat(SubPortOutContext *portOutCtx, SubPortTr ret = uapi_snd_create_track(&(portTrackOutCtx->trackHandle), portOutCtx->sndHandle, &sndTrackDefAttr)); CHK_FAILED_RETURN(ret, MEDIA_HAL_OK, ret, "uapi_snd_create_track failed"); - portTrackOutCtx->isRaw = IsRawFormat(portTrackOutCtx->inputAttr.audioFormat); + portTrackOutCtx->isRaw = false; #else MEDIA_HAL_UNUSED(portOutCtx); MEDIA_HAL_UNUSED(portTrackOutCtx); -- 2.17.1