From 161012be471e5bb8e5f1f5446fbfb62712c8affa Mon Sep 17 00:00:00 2001 From: s30056362 Date: Fri, 27 Dec 2024 14:11:51 +0800 Subject: [PATCH 1/4] Non blocking data reading, reading all data in the message queue at once Signed-off-by: s30056362 --- .../pulseaudio/modules/hdi/hdi_source.c | 43 ++++++++++++------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/frameworks/native/pulseaudio/modules/hdi/hdi_source.c b/frameworks/native/pulseaudio/modules/hdi/hdi_source.c index f04991eede..cb8c241a87 100644 --- a/frameworks/native/pulseaudio/modules/hdi/hdi_source.c +++ b/frameworks/native/pulseaudio/modules/hdi/hdi_source.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -211,14 +212,26 @@ static void FreeSceneMapsAndResampler(struct Userdata *u) static void FreeThread(struct Userdata *u) { + if (u->threadCap) { + pa_thread_free(u->threadCap); + } + if (u->thread) { pa_asyncmsgq_send(u->threadMq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL); pa_thread_free(u->thread); } - if (u->threadCap) { - pa_thread_free(u->threadCap); + uint32_t missedMsgqNum = PaAsyncqGetNumToRead(u->CaptureMq->asyncq); + if (missedMsgqNum > 0) { + AUDIO_ERR_LOG("OS_ProcessCapData missed message num: %{public}u", missedMsgqNum); + pa_memchunk chunk; + int32_t code = 0; + while (pa_asyncmsgq_get(u->CaptureMq, NULL, &code, NULL, NULL, &chunk, 0) == 0) { + pa_memblock_unref(chunk.memblock); + pa_asyncmsgq_done(u->CaptureMq, 0); + } } + if (u->CaptureMq) { pa_asyncmsgq_unref(u->CaptureMq); } @@ -793,7 +806,7 @@ static void PaRtpollProcessFunc(struct Userdata *u) eventfd_t value; int32_t readRet = eventfd_read(u->eventFd, &value); - if (readRet != 0) { + if ((readRet != 0) && (u->source->thread_info.state == PA_SOURCE_RUNNING)) { AUDIO_ERR_LOG("Failed to read from eventfd"); return; } @@ -802,11 +815,12 @@ static void PaRtpollProcessFunc(struct Userdata *u) int32_t code = 0; pa_usec_t now = pa_rtclock_now(); - pa_assert_se(pa_asyncmsgq_get(u->CaptureMq, NULL, &code, NULL, NULL, &chunk, 1) == 0); - if (code == HDI_POST) { - AudioEnhanceExistAndProcess(&chunk, u); + while (pa_asyncmsgq_get(u->CaptureMq, NULL, &code, NULL, NULL, &chunk, 0) == 0) { + if (code == HDI_POST) { + AudioEnhanceExistAndProcess(&chunk, u); + } + pa_asyncmsgq_done(u->CaptureMq, 0); } - pa_asyncmsgq_done(u->CaptureMq, 0); int32_t appsUid[PA_MAX_OUTPUTS_PER_SOURCE]; size_t count = 0; @@ -838,6 +852,7 @@ static void ThreadFuncProcessTimer(void *userdata) pa_thread_mq_install(&u->threadMq); u->timestamp = pa_rtclock_now(); + int32_t lastFlag = -1; AUDIO_DEBUG_LOG("HDI Source: u->timestamp : %{public}" PRIu64, u->timestamp); @@ -845,20 +860,16 @@ static void ThreadFuncProcessTimer(void *userdata) bool flag = (u->attrs.sourceType == SOURCE_TYPE_WAKEUP) ? (u->source->thread_info.state == PA_SOURCE_RUNNING && u->isCapturerStarted) : (PA_SOURCE_IS_OPENED(u->source->thread_info.state) && u->isCapturerStarted); - if (flag) { - pa_atomic_store(&u->captureFlag, 1); - } else { - pa_atomic_store(&u->captureFlag, 0); - } + pa_atomic_store(&u->captureFlag, flag); pa_rtpoll_set_timer_relative(u->rtpoll, RTPOLL_RUN_WAKEUP_INTERVAL_USEC); if (u->rtpollItem) { struct pollfd *pollFd = pa_rtpoll_item_get_pollfd(u->rtpollItem, NULL); - if (pollFd == NULL) { - AUDIO_ERR_LOG("get pollFd failed"); - break; + CHECK_AND_BREAK_LOG(pollFd != NULL, "pollFd is null"); + if (flag != lastFlag) { + pollFd->events = flag ? POLLIN : 0; + lastFlag = flag; } - pollFd->events = flag ? POLLIN : 0; } /* Hmm, nothing to do. Let's sleep */ int ret = pa_rtpoll_run(u->rtpoll); -- Gitee From 1f9d656c48786d0c8f378d0b6e059021edea8ac0 Mon Sep 17 00:00:00 2001 From: s30056362 Date: Fri, 27 Dec 2024 16:10:43 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E6=9A=B4=E9=9C=B2?= =?UTF-8?q?=E7=A7=81=E6=9C=89=E5=8F=98=E9=87=8F=EF=BC=8C=E4=B8=8D=E8=A6=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8E=9F=E6=9C=AC=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E9=9D=9Erunning=E7=8A=B6=E6=80=81=E4=B8=8D=E9=9C=80=E8=A6=81re?= =?UTF-8?q?ad=5Feventfd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: s30056362 --- .../pulseaudio/modules/hdi/hdi_source.c | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/frameworks/native/pulseaudio/modules/hdi/hdi_source.c b/frameworks/native/pulseaudio/modules/hdi/hdi_source.c index cb8c241a87..bb07d209fc 100644 --- a/frameworks/native/pulseaudio/modules/hdi/hdi_source.c +++ b/frameworks/native/pulseaudio/modules/hdi/hdi_source.c @@ -33,7 +33,6 @@ #include #include #include -#include #include #include @@ -221,15 +220,16 @@ static void FreeThread(struct Userdata *u) pa_thread_free(u->thread); } - uint32_t missedMsgqNum = PaAsyncqGetNumToRead(u->CaptureMq->asyncq); + pa_memchunk chunk; + int32_t code = 0; + int32_t missedMsgqNum = 0; + while (pa_asyncmsgq_get(u->CaptureMq, NULL, &code, NULL, NULL, &chunk, 0) == 0) { + pa_memblock_unref(chunk.memblock); + pa_asyncmsgq_done(u->CaptureMq, 0); + missedMsgqNum++; + } if (missedMsgqNum > 0) { AUDIO_ERR_LOG("OS_ProcessCapData missed message num: %{public}u", missedMsgqNum); - pa_memchunk chunk; - int32_t code = 0; - while (pa_asyncmsgq_get(u->CaptureMq, NULL, &code, NULL, NULL, &chunk, 0) == 0) { - pa_memblock_unref(chunk.memblock); - pa_asyncmsgq_done(u->CaptureMq, 0); - } } if (u->CaptureMq) { @@ -787,7 +787,7 @@ static void ThreadCaptureData(void *userdata) eventfd_t writEvent = 1; int32_t writeRes = eventfd_write(u->eventFd, writEvent); if (writeRes != 0) { - AUDIO_ERR_LOG("Failed to write from eventfd"); + AUDIO_ERR_LOG("Failed to write to eventfd"); continue; } cost = pa_rtclock_now() - now; @@ -806,7 +806,7 @@ static void PaRtpollProcessFunc(struct Userdata *u) eventfd_t value; int32_t readRet = eventfd_read(u->eventFd, &value); - if ((readRet != 0) && (u->source->thread_info.state == PA_SOURCE_RUNNING)) { + if (readRet != 0) { AUDIO_ERR_LOG("Failed to read from eventfd"); return; } @@ -887,7 +887,9 @@ static void ThreadFuncProcessTimer(void *userdata) getpid(), gettid()); break; } - PaRtpollProcessFunc(u); + if (flag) { + PaRtpollProcessFunc(u); + } } UnscheduleThreadInServer(getpid(), gettid()); } -- Gitee From b2e2185d6c66929a926671cf7775897b4668b43d Mon Sep 17 00:00:00 2001 From: Maninblack Date: Fri, 27 Dec 2024 08:24:54 +0000 Subject: [PATCH 3/4] fix codecheck Signed-off-by: Maninblack --- frameworks/native/pulseaudio/modules/hdi/hdi_source.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/native/pulseaudio/modules/hdi/hdi_source.c b/frameworks/native/pulseaudio/modules/hdi/hdi_source.c index bb07d209fc..89b3123660 100644 --- a/frameworks/native/pulseaudio/modules/hdi/hdi_source.c +++ b/frameworks/native/pulseaudio/modules/hdi/hdi_source.c @@ -852,7 +852,7 @@ static void ThreadFuncProcessTimer(void *userdata) pa_thread_mq_install(&u->threadMq); u->timestamp = pa_rtclock_now(); - int32_t lastFlag = -1; + bool lastFlag = false; AUDIO_DEBUG_LOG("HDI Source: u->timestamp : %{public}" PRIu64, u->timestamp); -- Gitee From 512fcbc1bf51e5bf528226f15b42da1682bec444 Mon Sep 17 00:00:00 2001 From: Maninblack Date: Fri, 27 Dec 2024 09:14:37 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=E9=9D=9Erunning=E7=8A=B6=E6=80=81=EF=BC=8C?= =?UTF-8?q?=E4=BB=8D=E7=84=B6=E5=8F=AF=E4=BB=A5=E8=BF=9B=E5=85=A5PaRtpollP?= =?UTF-8?q?rocessFunc=EF=BC=8C=E5=A6=82=E6=9E=9C=E4=BB=8E=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E9=98=9F=E5=88=97=E4=B8=AD=E8=8E=B7=E5=8F=96=E5=88=B0?= =?UTF-8?q?=E4=BA=86=E6=95=B0=E6=8D=AE=EF=BC=8C=E5=88=99=E9=87=8A=E6=94=BE?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maninblack --- .../pulseaudio/modules/hdi/hdi_source.c | 39 +++++++++---------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/frameworks/native/pulseaudio/modules/hdi/hdi_source.c b/frameworks/native/pulseaudio/modules/hdi/hdi_source.c index 89b3123660..1afd27fdb8 100644 --- a/frameworks/native/pulseaudio/modules/hdi/hdi_source.c +++ b/frameworks/native/pulseaudio/modules/hdi/hdi_source.c @@ -311,7 +311,7 @@ static int SourceProcessMsg(pa_msgobject *o, int code, void *data, int64_t offse return pa_source_process_msg(o, code, data, offset, chunk); } -static void SendInitCommandToAlgo() +static void SendInitCommandToAlgo(void) { pa_usec_t now = pa_rtclock_now(); int32_t ret = EnhanceChainManagerSendInitCommand(); @@ -804,11 +804,10 @@ static void PaRtpollProcessFunc(struct Userdata *u) { AUTO_CTRACE("PaRtpollProcessFunc"); - eventfd_t value; - int32_t readRet = eventfd_read(u->eventFd, &value); - if (readRet != 0) { - AUDIO_ERR_LOG("Failed to read from eventfd"); - return; + if (u->source->thread_info.state == PA_SOURCE_RUNNING) { + eventfd_t value; + int32_t readRet = eventfd_read(u->eventFd, &value); + CHECK_AND_RETURN_LOG(readRet == 0, "Failed to read from eventfd"); } pa_memchunk chunk; @@ -816,9 +815,13 @@ static void PaRtpollProcessFunc(struct Userdata *u) pa_usec_t now = pa_rtclock_now(); while (pa_asyncmsgq_get(u->CaptureMq, NULL, &code, NULL, NULL, &chunk, 0) == 0) { - if (code == HDI_POST) { - AudioEnhanceExistAndProcess(&chunk, u); + if (u->source->thread_info.state != PA_SOURCE_RUNNING) { + // when the source is not in running state, but we still recive data from CaptureMq. + pa_memblock_unref(chunk.memblock); + pa_asyncmsgq_done(u->CaptureMq, 0); + continue; } + AudioEnhanceExistAndProcess(&chunk, u); pa_asyncmsgq_done(u->CaptureMq, 0); } @@ -852,10 +855,15 @@ static void ThreadFuncProcessTimer(void *userdata) pa_thread_mq_install(&u->threadMq); u->timestamp = pa_rtclock_now(); - bool lastFlag = false; AUDIO_DEBUG_LOG("HDI Source: u->timestamp : %{public}" PRIu64, u->timestamp); + if (u->rtpollItem) { + struct pollfd *pollFd = pa_rtpoll_item_get_pollfd(u->rtpollItem, NULL); + CHECK_AND_BREAK_LOG(pollFd != NULL, "pollFd is null"); + pollFd->events = POLLIN; + } + while (true) { bool flag = (u->attrs.sourceType == SOURCE_TYPE_WAKEUP) ? (u->source->thread_info.state == PA_SOURCE_RUNNING && u->isCapturerStarted) : @@ -863,14 +871,7 @@ static void ThreadFuncProcessTimer(void *userdata) pa_atomic_store(&u->captureFlag, flag); pa_rtpoll_set_timer_relative(u->rtpoll, RTPOLL_RUN_WAKEUP_INTERVAL_USEC); - if (u->rtpollItem) { - struct pollfd *pollFd = pa_rtpoll_item_get_pollfd(u->rtpollItem, NULL); - CHECK_AND_BREAK_LOG(pollFd != NULL, "pollFd is null"); - if (flag != lastFlag) { - pollFd->events = flag ? POLLIN : 0; - lastFlag = flag; - } - } + /* Hmm, nothing to do. Let's sleep */ int ret = pa_rtpoll_run(u->rtpoll); if (ret < 0) { @@ -887,9 +888,7 @@ static void ThreadFuncProcessTimer(void *userdata) getpid(), gettid()); break; } - if (flag) { - PaRtpollProcessFunc(u); - } + PaRtpollProcessFunc(u); } UnscheduleThreadInServer(getpid(), gettid()); } -- Gitee