!56 查询isWhispering

Merge pull request !56 from lvqiang214/master
This commit is contained in:
openharmony_ci 2024-06-19 11:29:38 +00:00 committed by Gitee
commit ace7f0788e
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
16 changed files with 84 additions and 169 deletions

View File

@ -25,7 +25,6 @@ config("intellvoice_native_config") {
ohos_shared_library("intellvoice_native") {
sources = [
"enroll_intell_voice_engine.cpp",
"intell_voice_load_callback.cpp",
"intell_voice_manager.cpp",
"wakeup_intell_voice_engine.cpp",
]

View File

@ -1,43 +0,0 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "intell_voice_load_callback.h"
#include "intell_voice_log.h"
#include "intell_voice_manager.h"
#define LOG_TAG "IntellVoiceLoadCallback"
namespace OHOS {
namespace IntellVoice {
IntellVoiceLoadCallback::IntellVoiceLoadCallback(OnLoadSystemAbilitySuccessCb loadSASuccessCb,
OnLoadSystemAbilityFailCb loadSAFailCb) : loadSASuccessCb_(loadSASuccessCb), loadSAFailCb_(loadSAFailCb)
{
}
void IntellVoiceLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
const sptr<IRemoteObject> &remoteObject)
{
INTELL_VOICE_LOG_INFO("Load system ability success!");
loadSASuccessCb_(remoteObject);
}
void IntellVoiceLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
{
INTELL_VOICE_LOG_INFO("Load system ability failed!");
loadSAFailCb_();
}
}
}

View File

@ -20,7 +20,6 @@
#include "memory_guard.h"
#include "scope_guard.h"
#include "intell_voice_log.h"
#include "intell_voice_load_callback.h"
#include "intell_voice_service_proxy.h"
#define LOG_TAG "IntellVoiceManager"
@ -30,7 +29,7 @@ using namespace OHOS::IntellVoiceEngine;
namespace OHOS {
namespace IntellVoice {
constexpr int32_t LOAD_SA_TIMEOUT_MS = 5000;
constexpr int32_t LOAD_SA_TIMEOUT_S = 4; // 4s
IntellVoiceManager::IntellVoiceManager()
{
@ -53,8 +52,8 @@ IntellVoiceManager *IntellVoiceManager::GetInstance()
bool IntellVoiceManager::Init()
{
std::unique_lock<std::mutex> lock(mutex_);
INTELL_VOICE_LOG_INFO("enter");
std::unique_lock<std::mutex> lock(mutex_);
auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (samgr == nullptr) {
@ -62,49 +61,18 @@ bool IntellVoiceManager::Init()
return false;
}
sptr<IntellVoiceLoadCallback> loadCallback = new (std::nothrow)
IntellVoiceLoadCallback(std::bind(&IntellVoiceManager::LoadSystemAbilitySuccess, this, std::placeholders::_1),
std::bind(&IntellVoiceManager::LoadSystemAbilityFail, this));
if (loadCallback == nullptr) {
INTELL_VOICE_LOG_ERROR("loadCallback is nullptr");
return false;
}
int32_t ret = samgr->LoadSystemAbility(INTELL_VOICE_SERVICE_ID, loadCallback);
if (ret != ERR_OK) {
auto object = samgr->LoadSystemAbility(INTELL_VOICE_SERVICE_ID, LOAD_SA_TIMEOUT_S);
if (object == nullptr) {
INTELL_VOICE_LOG_ERROR("Failed to load systemAbility");
return false;
}
auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS));
if (waitStatus != std::cv_status::no_timeout) {
INTELL_VOICE_LOG_ERROR("Load systemAbility timeout");
return false;
}
INTELL_VOICE_LOG_INFO("Load systemAbility success");
return true;
}
void IntellVoiceManager::LoadSystemAbilitySuccess(const sptr<IRemoteObject> &object)
{
std::unique_lock<std::mutex> lock(mutex_);
INTELL_VOICE_LOG_INFO("IntellVoiceManager finish start systemAbility");
if (object == nullptr) {
INTELL_VOICE_LOG_ERROR("get system ability failed");
return;
}
g_sProxy = iface_cast<IIntellVoiceService>(object);
if (g_sProxy != nullptr) {
INTELL_VOICE_LOG_INFO("init Service Proxy success");
}
proxyConVar_.notify_one();
}
void IntellVoiceManager::LoadSystemAbilityFail()
{
std::unique_lock<std::mutex> lock(mutex_);
g_sProxy = nullptr;
proxyConVar_.notify_one();
INTELL_VOICE_LOG_INFO("Load systemAbility success");
return true;
}
int32_t IntellVoiceManager::CreateIntellVoiceEngine(IntellVoiceEngineType type, sptr<IIntellVoiceEngine> &inst)

View File

@ -1,43 +0,0 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef INTELL_VOICE_LOAD_CALLBACK_H
#define INTELL_VOICE_LOAD_CALLBACK_H
#include <cstdint>
#include <string>
#include "refbase.h"
#include "system_ability_load_callback_stub.h"
namespace OHOS {
namespace IntellVoice {
using OnLoadSystemAbilitySuccessCb = std::function<void(const sptr<IRemoteObject> &remoteObject)>;
using OnLoadSystemAbilityFailCb = std::function<void()>;
class IntellVoiceLoadCallback : public SystemAbilityLoadCallbackStub {
public:
IntellVoiceLoadCallback(OnLoadSystemAbilitySuccessCb loadSASuccessCb, OnLoadSystemAbilityFailCb loadSAFailCb);
void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject) override;
void OnLoadSystemAbilityFail(int32_t systemAbilityId) override;
private:
OnLoadSystemAbilitySuccessCb loadSASuccessCb_;
OnLoadSystemAbilityFailCb loadSAFailCb_;
};
} // namespace IntellVoice
} // namespace OHOS
#endif

View File

@ -42,8 +42,6 @@ public:
int32_t CreateIntellVoiceEngine(IntellVoiceEngineType type, sptr<IIntellVoiceEngine> &inst);
int32_t ReleaseIntellVoiceEngine(IntellVoiceEngineType type);
void LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject);
void LoadSystemAbilityFail();
bool Init();
int32_t RegisterServiceDeathRecipient(sptr<OHOS::IRemoteObject::DeathRecipient> callback);

View File

@ -33,7 +33,7 @@ int32_t IntellVoiceServiceProxy::CreateIntellVoiceEngine(IntellVoiceEngineType t
data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
data.WriteInt32(static_cast<int>(type));
auto stub = new IntellVoiceDeathRecipientStub();
auto stub = sptr<IntellVoiceDeathRecipientStub>(new (std::nothrow) IntellVoiceDeathRecipientStub());
data.WriteRemoteObject(stub);
Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_CREATE_ENGINE, data, reply, option);

View File

@ -27,7 +27,7 @@ using namespace OHOS::IntellVoiceUtils;
namespace OHOS {
namespace IntellVoiceEngine {
const std::string HEADSET_SERVICE_NAME = "intell_voice_headset_manager_service";
const std::string HEADSET_SERVICE_NAME = "tws_kws_service";
HeadsetHostManager::~HeadsetHostManager()
{

View File

@ -17,6 +17,7 @@
#include <vector>
#include <fstream>
#include <cstdio>
#include "audio_system_manager.h"
#include "intell_voice_log.h"
#include "intell_voice_util.h"
#include "engine_factory.h"
@ -270,11 +271,11 @@ void IntellVoiceServiceManager::CreateDetector(int32_t uuid)
detector_[uuid] = detector;
}
void IntellVoiceServiceManager::StartDetection(int32_t uuid)
bool IntellVoiceServiceManager::StartDetection(int32_t uuid)
{
if ((IsEngineExist(INTELL_VOICE_ENROLL)) || (IsEngineExist(INTELL_VOICE_UPDATE))) {
INTELL_VOICE_LOG_INFO("enroll engine or update engine exist, do nothing");
return;
return false;
}
for (uint32_t cnt = 1; cnt <= MAX_ATTEMPT_CNT; ++cnt) {
@ -282,24 +283,24 @@ void IntellVoiceServiceManager::StartDetection(int32_t uuid)
std::lock_guard<std::mutex> lock(detectorMutex_);
if ((detector_.count(uuid) == 0) || (detector_[uuid] == nullptr)) {
INTELL_VOICE_LOG_INFO("detector is not existed, uuid:%{public}d", uuid);
return;
return false;
}
if (!QuerySwitchByUuid(uuid)) {
INTELL_VOICE_LOG_INFO("switch is off, uuid is %{public}d", uuid);
return;
return false;
}
auto triggerMgr = TriggerManager::GetInstance();
if (triggerMgr == nullptr) {
INTELL_VOICE_LOG_ERROR("trigger manager is nullptr");
return;
return false;
}
if (triggerMgr->GetParameter("audio_hal_status") == "true") {
INTELL_VOICE_LOG_INFO("audio hal is ready");
detector_[uuid]->StartRecognition();
return;
return true;
}
}
INTELL_VOICE_LOG_INFO("begin to wait");
@ -307,6 +308,7 @@ void IntellVoiceServiceManager::StartDetection(int32_t uuid)
INTELL_VOICE_LOG_INFO("end to wait");
}
INTELL_VOICE_LOG_ERROR("failed to start recognition");
return false;
}
void IntellVoiceServiceManager::StopDetection(int32_t uuid)
@ -324,6 +326,9 @@ void IntellVoiceServiceManager::OnDetected(int32_t uuid)
TaskExecutor::AddAsyncTask([uuid, this]() {
auto engine = GetEngine(INTELL_VOICE_WAKEUP, engines_);
if (engine == nullptr) {
INTELL_VOICE_LOG_WARN("wakeup engine is nullptr");
StartDetection(VOICE_WAKEUP_MODEL_UUID);
StartDetection(PROXIMAL_WAKEUP_MODEL_UUID);
return;
}
engine->OnDetected(uuid);
@ -348,13 +353,17 @@ void IntellVoiceServiceManager::CreateAndStartServiceObject(int32_t uuid, bool n
return;
}
CreateDetector(uuid);
if (!StartDetection(uuid)) {
INTELL_VOICE_LOG_ERROR("failed to start detection");
return;
}
if (!needResetAdapter) {
CreateEngine(INTELL_VOICE_WAKEUP);
} else {
CreateOrResetWakeupEngine();
}
CreateDetector(uuid);
StartDetection(uuid);
}
void IntellVoiceServiceManager::SetDspSensibility(const std::string &sensibility)
@ -635,6 +644,14 @@ std::string IntellVoiceServiceManager::GetParameter(const std::string &key)
} else if (key == "wakeup_capability") {
val = GetWakeupCapability();
INTELL_VOICE_LOG_INFO("get wakeup capability result %{public}s", val.c_str());
} else if (key == "isWhispering") {
auto audioSystemManager = AudioStandard::AudioSystemManager::GetInstance();
if (audioSystemManager == nullptr) {
INTELL_VOICE_LOG_ERROR("audioSystemManager is nullptr");
return val;
}
val = std::to_string(audioSystemManager->IsWhispering());
INTELL_VOICE_LOG_INFO("get isWhispering result %{public}s", val.c_str());
}
return val;
@ -924,5 +941,11 @@ void IntellVoiceServiceManager::HandleHeadsetHostDie()
}
});
}
void IntellVoiceServiceManager::OnTriggerConnectServiceStart()
{
HandleSwitchOn(true, VOICE_WAKEUP_MODEL_UUID, false);
HandleSwitchOn(true, PROXIMAL_WAKEUP_MODEL_UUID, false);
}
} // namespace IntellVoiceEngine
} // namespace OHOS

View File

@ -77,7 +77,7 @@ public:
void ProcBreathModel();
void CreateSwitchProvider();
void ReleaseSwitchProvider();
void StartDetection(int32_t uuid);
bool StartDetection(int32_t uuid);
void StopDetection(int32_t uuid);
bool QuerySwitchStatus(const std::string &key);
@ -96,6 +96,7 @@ public:
int32_t GetWakeupSourceFile(const std::string &filePath, std::vector<uint8_t> &buffer);
int32_t SendWakeupFile(const std::string &filePath, const std::vector<uint8_t> &buffer);
void SetDspSensibility(const std::string &sensibility);
void OnTriggerConnectServiceStart();
private:
IntellVoiceServiceManager();

View File

@ -60,6 +60,7 @@ void WakeupSourceProcess::Write(const std::vector<std::vector<uint8_t>> &audioDa
if (channelCnt_ == CHANNEL_CNT_1) {
WriteChannelData(audioData[CHANNEL_ID_0], CHANNEL_ID_0);
} else if (channelCnt_ == CHANNEL_CNT_4) {
WriteChannelData(audioData[CHANNEL_ID_0], CHANNEL_ID_0);
WriteChannelData(audioData[CHANNEL_ID_1], CHANNEL_ID_1);
WriteChannelData(audioData[CHANNEL_ID_2], CHANNEL_ID_2);
WriteChannelData(audioData[CHANNEL_ID_3], CHANNEL_ID_3);

View File

@ -33,11 +33,11 @@ using namespace OHOS::HDI::IntelligentVoice::Trigger::V1_1;
namespace OHOS {
namespace IntellVoiceTrigger {
static constexpr int32_t MAX_GET_HDI_SERVICE_TIMEOUT = 3;
static constexpr uint32_t MAX_RECOGNITION_EVENT_NUM = 100;
static const std::string INTELL_VOICE_TRIGGER_SERVICE = "intell_voice_trigger_manager_service";
static const std::string THREAD_NAME = "TriggerThread_";
TriggerConnector::TriggerConnector(const IntellVoiceTriggerAdapterDsecriptor &desc)
TriggerConnector::TriggerConnector(OnServiceStartCb cb, const IntellVoiceTriggerAdapterDsecriptor &desc) : cb_(cb)
{
desc_.adapterName = desc.adapterName;
LoadHdiAdapter();
@ -79,16 +79,6 @@ std::shared_ptr<IIntellVoiceTriggerConnectorModule> TriggerConnector::GetModule(
INTELL_VOICE_LOG_INFO("already load hdi adapter");
break;
}
{
std::unique_lock<mutex> serviceStateLock(serviceStateMutex_);
if (serviceState_ != SERVIE_STATUS_START) {
if (!cv_.wait_for(serviceStateLock, chrono::seconds(MAX_GET_HDI_SERVICE_TIMEOUT),
[&] { return serviceState_ == SERVIE_STATUS_START; })) {
INTELL_VOICE_LOG_ERROR("time out");
return nullptr;
}
}
}
INTELL_VOICE_LOG_INFO("start to load hdi adapter");
if (!LoadHdiAdapter()) {
return nullptr;
@ -118,14 +108,21 @@ void TriggerConnector::OnReceive(const ServiceStatus &serviceStatus)
}
INTELL_VOICE_LOG_INFO("status:%{public}d", serviceStatus.status);
{
std::unique_lock<mutex> serviceSatetLock(serviceStateMutex_);
if (serviceStatus.status == serviceState_) {
return;
}
serviceState_ = serviceStatus.status;
std::lock_guard<std::mutex> lock(mutex_);
if (serviceStatus.status == serviceState_) {
INTELL_VOICE_LOG_INFO("service state not change");
return;
}
serviceState_ = serviceStatus.status;
if ((serviceState_ != SERVIE_STATUS_START) || (TriggerHostManager::GetAdapter() != nullptr)) {
INTELL_VOICE_LOG_INFO("no need to notify mgr");
return;
}
if (cb_ != nullptr) {
cb_(desc_);
}
cv_.notify_all();
}
TriggerConnector::TriggerSession::TriggerSession(TriggerConnector *connector,

View File

@ -20,7 +20,6 @@
#include <map>
#include <atomic>
#include <mutex>
#include <condition_variable>
#include <iservmgr_hdi.h>
#include "i_intell_voice_trigger_adapter_listener.h"
#include "i_intell_voice_trigger_connector_module.h"
@ -39,11 +38,11 @@ using OHOS::HDI::IntelligentVoice::Trigger::V1_0::IntellVoiceRecognitionEvent;
using OHOS::HDI::IntelligentVoice::Trigger::V1_0::IntellVoiceTriggerAdapterDsecriptor;
using OHOS::HDI::IntelligentVoice::Trigger::V1_0::IntellVoiceTriggerProperties;
const std::string INTELL_VOICE_TRIGGER_SERVICE = "intell_voice_trigger_manager_service";
using OnServiceStartCb = std::function<void(const IntellVoiceTriggerAdapterDsecriptor &desc)>;
class TriggerConnector : public ServStatListenerStub, private TriggerHostManager {
public:
explicit TriggerConnector(const IntellVoiceTriggerAdapterDsecriptor &desc);
TriggerConnector(OnServiceStartCb cb, const IntellVoiceTriggerAdapterDsecriptor &desc);
~TriggerConnector() override;
std::shared_ptr<IIntellVoiceTriggerConnectorModule> GetModule(
std::shared_ptr<IIntellVoiceTriggerConnectorCallback> callback);
@ -134,11 +133,10 @@ private:
private:
std::mutex mutex_ {};
std::mutex serviceStateMutex_ {};
std::condition_variable cv_;
uint16_t serviceState_ = HDI::ServiceManager::V1_0::SERVIE_STATUS_MAX;
OnServiceStartCb cb_;
IntellVoiceTriggerAdapterDsecriptor desc_;
std::set<std::shared_ptr<TriggerSession>> activeSessions_;
uint16_t serviceState_ = HDI::ServiceManager::V1_0::SERVIE_STATUS_MAX;
};
} // namespace IntellVoiceTrigger
} // namespace OHOS

View File

@ -25,14 +25,14 @@ using namespace OHOS::HDI::IntelligentVoice::Trigger::V1_0;
namespace OHOS {
namespace IntellVoiceTrigger {
TriggerConnectorInternalImpl::TriggerConnectorInternalImpl() : servmgr_(IServiceManager::Get())
TriggerConnectorInternalImpl::TriggerConnectorInternalImpl(OnServiceStartCb cb) : servmgr_(IServiceManager::Get())
{
OHOS::IntellVoiceUtils::MemoryGuard memoryGuard;
IntellVoiceTriggerAdapterDsecriptor descriptor;
descriptor.adapterName = "primary";
if (servmgr_ != nullptr) {
auto connector = sptr<TriggerConnector>(new (std::nothrow) TriggerConnector(descriptor));
auto connector = sptr<TriggerConnector>(new (std::nothrow) TriggerConnector(cb, descriptor));
if (connector != nullptr) {
servmgr_->RegisterServiceStatusListener(connector, DEVICE_CLASS_DEFAULT);
connectors_[descriptor.adapterName] = connector;

View File

@ -20,13 +20,13 @@
#include "i_intell_voice_trigger_connector_internal.h"
#include "trigger_connector.h"
using OHOS::HDI::ServiceManager::V1_0::IServiceManager;
namespace OHOS {
namespace IntellVoiceTrigger {
using OHOS::HDI::ServiceManager::V1_0::IServiceManager;
class TriggerConnectorInternalImpl : public IIntellVoiceTriggerConnectorInternal {
public:
TriggerConnectorInternalImpl();
explicit TriggerConnectorInternalImpl(OnServiceStartCb cb);
~TriggerConnectorInternalImpl() override;
std::vector<TriggerConnectorModuleDesc> ListModuleDescriptors() override;
std::shared_ptr<IIntellVoiceTriggerConnectorModule> GetModule(const std::string &adapterName,

View File

@ -14,6 +14,7 @@
*/
#include "trigger_connector_mgr.h"
#include "intell_voice_log.h"
#include "intell_voice_service_manager.h"
#include "trigger_connector_internal_validation.h"
#include "trigger_connector_internal_impl.h"
@ -24,7 +25,9 @@ namespace IntellVoiceTrigger {
std::unique_ptr<TriggerConnectorMgr> TriggerConnectorMgr::g_connectorMgr =
std::unique_ptr<TriggerConnectorMgr>(
new (std::nothrow) TriggerConnectorMgr(std::make_unique<TriggerConnectorInternalValidation>(
std::make_unique<TriggerConnectorInternalImpl>())));
std::make_unique<TriggerConnectorInternalImpl>([](const IntellVoiceTriggerAdapterDsecriptor &desc) {
TriggerConnectorMgr::OnTriggerConnectServiceStart(desc);
}))));
TriggerConnectorMgr::TriggerConnectorMgr(std::unique_ptr<IIntellVoiceTriggerConnectorInternal> delegate)
: delegate_(std::move(delegate))
@ -55,5 +58,14 @@ std::shared_ptr<IIntellVoiceTriggerConnectorModule> TriggerConnectorMgr::GetConn
return delegate_->GetModule(adapterName, callback);
}
void TriggerConnectorMgr::OnTriggerConnectServiceStart(const IntellVoiceTriggerAdapterDsecriptor &desc)
{
INTELL_VOICE_LOG_INFO("enter, adapter name:%{public}s", desc.adapterName.c_str());
const auto &manager = OHOS::IntellVoiceEngine::IntellVoiceServiceManager::GetInstance();
if (manager != nullptr) {
manager->OnTriggerConnectServiceStart();
}
}
} // namespace IntellVoiceTrigger
} // namespace OHOS

View File

@ -16,10 +16,13 @@
#define INTELL_VOICE_TRIGGER_CONNECTOR_MGR_H
#include "nocopyable.h"
#include "v1_0/intell_voice_trigger_types.h"
#include "i_intell_voice_trigger_connector_internal.h"
namespace OHOS {
namespace IntellVoiceTrigger {
using OHOS::HDI::IntelligentVoice::Trigger::V1_0::IntellVoiceTriggerAdapterDsecriptor;
class TriggerConnectorMgr {
public:
~TriggerConnectorMgr() = default;
@ -30,6 +33,7 @@ public:
private:
explicit TriggerConnectorMgr(std::unique_ptr<IIntellVoiceTriggerConnectorInternal> delegate);
static void OnTriggerConnectServiceStart(const IntellVoiceTriggerAdapterDsecriptor &desc);
private:
static std::unique_ptr<TriggerConnectorMgr> g_connectorMgr;