headset wakeup

Signed-off-by: lvqiang214 <lvqiang1@huawei.com>
This commit is contained in:
lvqiang214 2024-05-08 16:32:39 +08:00
parent 03a07b71ce
commit d1033815e3
39 changed files with 1504 additions and 131 deletions

View File

@ -68,6 +68,18 @@
"//foundation/ai/intelligent_voice_framework/frameworks/js/napi/include"
]
}
},
{
"type": "none",
"name": "//foundation/ai/intelligent_voice_framework/frameworks/native:intellvoice_native",
"header": {
"header_files": [
"intell_voice_manager.h",
"i_headset_wakeup.h",
"wakeup_intell_voice_engine.h"
],
"header_base": "//foundation/ai/intelligent_voice_framework/interfaces/inner_api/native"
}
}
],
"test": [

View File

@ -92,12 +92,14 @@ void EngineEventCallbackNapi::OnEvent(const IntellVoiceEngineCallBackEvent &even
} else if (event.msgId == static_cast<OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineMessageType>(
OHOS::HDI::IntelligentVoice::Engine::V1_2::INTELL_VOICE_ENGINE_MSG_RECONFIRM_RECOGNITION_COMPLETE)) {
eventId = INTELLIGENT_VOICE_EVENT_RECONFIRM_RECOGNITION_COMPLETE;
} else if (event.msgId == static_cast<OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineMessageType>(
OHOS::HDI::IntelligentVoice::Engine::V1_2::INTELL_VOICE_ENGINE_MSG_HEADSET_RECOGNIZE_COMPLETE)) {
eventId = INTELLIGENT_VOICE_EVENT_HEADSET_RECOGNIZE_COMPLETE;
} else {
INTELL_VOICE_LOG_ERROR("error msgId:%{public}d", event.msgId);
return;
}
EngineCallBackInfo cbInfo = {eventId, event.result == 0 ? true : false, event.info};
INTELL_VOICE_LOG_INFO("OnEvent EngineCallBackInfo: eventId: %{public}d, isSuccess: %{public}u, context: %{public}s",
cbInfo.eventId, cbInfo.isSuccess, cbInfo.context.c_str());

View File

@ -13,6 +13,15 @@
import("//build/ohos.gni")
config("intellvoice_native_config") {
include_dirs = [
"../../interfaces/inner_api/native",
"../../services/intell_voice_engine",
"../../services/intell_voice_engine/proxy",
"../../utils",
]
}
ohos_shared_library("intellvoice_native") {
sources = [
"enroll_intell_voice_engine.cpp",
@ -28,6 +37,8 @@ ohos_shared_library("intellvoice_native") {
"../../utils",
]
public_configs = [ ":intellvoice_native_config" ]
cflags_cc = [
"-Wno-error=unused-parameter",
"-DHILOG_ENABLE",

View File

@ -131,5 +131,25 @@ int32_t WakeupIntellVoiceEngine::GetWakeupPcm(std::vector<uint8_t> &data)
return engine_->GetWakeupPcm(data);
}
int32_t WakeupIntellVoiceEngine::NotifyHeadsetWakeEvent()
{
INTELL_VOICE_LOG_INFO("enter");
if (engine_ == nullptr) {
INTELL_VOICE_LOG_ERROR("engine is null");
return -1;
}
return engine_->NotifyHeadsetWakeEvent();
}
int32_t WakeupIntellVoiceEngine::NotifyHeadsetHostEvent(int32_t event)
{
INTELL_VOICE_LOG_INFO("enter, event:%{public}d", event);
if (engine_ == nullptr) {
INTELL_VOICE_LOG_ERROR("engine is null");
return -1;
}
return engine_->NotifyHeadsetHostEvent(static_cast<OHOS::IntellVoiceEngine::HeadsetHostEventType>(event));
}
}
}

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2024 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 I_HEADSET_WAKEUP_H
#define I_HEADSET_WAKEUP_H
#include <cstdint>
#include <vector>
namespace OHOS {
namespace IntellVoiceEngine {
class IHeadsetWakeup {
public:
virtual int32_t ReadHeadsetStream(std::vector<uint8_t> &audioStream) = 0;
virtual int32_t NotifyVerifyResult(bool result) = 0;
virtual int32_t StopReadingStream() = 0;
virtual int32_t GetHeadsetAwakeState() = 0;
};
}
}
#endif

View File

@ -46,6 +46,8 @@ public:
int32_t Read(std::vector<uint8_t> &data);
int32_t StopCapturer();
int32_t GetWakeupPcm(std::vector<uint8_t> &data);
int32_t NotifyHeadsetWakeEvent();
int32_t NotifyHeadsetHostEvent(int32_t event);
sptr<IIntellVoiceEngine> GetEngine()
{
return engine_;

View File

@ -69,6 +69,8 @@ ohos_shared_library("intell_voice_server") {
"intell_voice_engine/server/enroll/enroll_engine.cpp",
"intell_voice_engine/server/hdi_adapter/adapter_host_manager.cpp",
"intell_voice_engine/server/hdi_adapter/engine_host_manager.cpp",
"intell_voice_engine/server/hdi_adapter/headset_adapter_host_manager.cpp",
"intell_voice_engine/server/hdi_adapter/headset_host_manager.cpp",
"intell_voice_engine/server/sa/intell_voice_engine_arbitration.cpp",
"intell_voice_engine/server/sa/intell_voice_engine_callback_proxy.cpp",
"intell_voice_engine/server/sa/intell_voice_engine_stub.cpp",
@ -87,6 +89,8 @@ ohos_shared_library("intell_voice_server") {
"intell_voice_engine/server/utils/switch_observer.cpp",
"intell_voice_engine/server/utils/switch_provider.cpp",
"intell_voice_engine/server/utils/system_event_observer.cpp",
"intell_voice_engine/server/wakeup/headset/headset_wakeup_engine_impl.cpp",
"intell_voice_engine/server/wakeup/headset/headset_wakeup_wrapper.cpp",
"intell_voice_engine/server/wakeup/wakeup_adapter_listener.cpp",
"intell_voice_engine/server/wakeup/wakeup_engine.cpp",
"intell_voice_engine/server/wakeup/wakeup_engine_impl.cpp",
@ -110,6 +114,7 @@ ohos_shared_library("intell_voice_server") {
include_dirs = [
"../../../ai/intelligent_voice_framework/utils",
"../../../ai/intelligent_voice_framework/interfaces/inner_api/native",
"intell_voice_engine",
"intell_voice_engine/server/base",
"intell_voice_engine/server/enroll",
@ -120,6 +125,7 @@ ohos_shared_library("intell_voice_server") {
"intell_voice_engine/server/sa",
"intell_voice_engine/server/utils",
"intell_voice_engine/server/wakeup",
"intell_voice_engine/server/wakeup/headset",
"intell_voice_trigger/server",
"intell_voice_trigger/server/connector_mgr",
]

View File

@ -38,6 +38,11 @@ enum IntellVoiceUpdateSceneType {
INTELL_VOICE_SCENE_TYPE_BUT
};
enum HeadsetHostEventType {
HEADSET_HOST_OFF = 0,
HEADSET_HOST_ON = 1,
};
struct IntellVoiceEngineInfo {
std::string wakeupPhrase;
bool isPcmFromExternal { false };
@ -64,7 +69,9 @@ public:
INTELL_VOICE_ENGINE_READ,
INTELL_VOICE_ENGINE_STOP_CAPTURER,
INTELL_VOICE_ENGINE_GET_WAKEUP_PCM,
INTELL_VOICE_ENGINE_EVALUATE
INTELL_VOICE_ENGINE_EVALUATE,
INTELL_VOICE_ENGINE_NOTIFY_HEADSET_WAKE_EVENT,
INTELL_VOICE_ENGINE_NOTIFY_HEADSET_HOSTEVENT
};
virtual void SetCallback(sptr<IRemoteObject> object) = 0;
@ -80,6 +87,8 @@ public:
virtual int32_t StopCapturer() = 0;
virtual int32_t GetWakeupPcm(std::vector<uint8_t> &data) = 0;
virtual int32_t Evaluate(const std::string &word, EvaluationResultInfo &info) = 0;
virtual int32_t NotifyHeadsetWakeEvent() = 0;
virtual int32_t NotifyHeadsetHostEvent(HeadsetHostEventType event) = 0;
};
}
}

View File

@ -229,5 +229,30 @@ int32_t IntellVoiceEngineProxy::Evaluate(const std::string &word, EvaluationResu
info.resultCode = static_cast<OHOS::HDI::IntelligentVoice::Engine::V1_2::EvaluationResultCode>(reply.ReadInt32());
return ret;
}
int32_t IntellVoiceEngineProxy::NotifyHeadsetWakeEvent()
{
INTELL_VOICE_LOG_ERROR("enter");
MessageParcel data;
MessageParcel reply;
MessageOption option;
data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
Remote()->SendRequest(INTELL_VOICE_ENGINE_NOTIFY_HEADSET_WAKE_EVENT, data, reply, option);
return reply.ReadInt32();
}
int32_t IntellVoiceEngineProxy::NotifyHeadsetHostEvent(HeadsetHostEventType event)
{
INTELL_VOICE_LOG_ERROR("enter");
MessageParcel data;
MessageParcel reply;
MessageOption option;
data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
data.WriteInt32(event);
Remote()->SendRequest(INTELL_VOICE_ENGINE_NOTIFY_HEADSET_HOSTEVENT, data, reply, option);
return reply.ReadInt32();
}
}
}

View File

@ -40,6 +40,8 @@ public:
int32_t StopCapturer() override;
int32_t GetWakeupPcm(std::vector<uint8_t> &data) override;
int32_t Evaluate(const std::string &word, EvaluationResultInfo &info) override;
int32_t NotifyHeadsetWakeEvent() override;
int32_t NotifyHeadsetHostEvent(HeadsetHostEventType event) override;
private:
static inline BrokerDelegator<IntellVoiceEngineProxy> delegator_;

View File

@ -31,14 +31,25 @@ int32_t EngineBase::StopCapturer()
return 0;
}
int32_t EngineBase::GetWakeupPcm(std::vector<uint8_t> & /* data */)
{
return 0;
}
int32_t EngineBase::NotifyHeadsetWakeEvent()
{
return 0;
}
int32_t EngineBase::Evaluate(const std::string & /* word */, EvaluationResultInfo & /* info */)
{
return 0;
}
int32_t EngineBase::NotifyHeadsetHostEvent(HeadsetHostEventType /* event */)
{
return 0;
}
}
}

View File

@ -40,7 +40,8 @@ public:
int32_t StopCapturer() override;
int32_t GetWakeupPcm(std::vector<uint8_t> &data) override;
int32_t Evaluate(const std::string &word, EvaluationResultInfo &info) override;
int32_t NotifyHeadsetWakeEvent() override;
int32_t NotifyHeadsetHostEvent(HeadsetHostEventType event) override;
protected:
EngineBase() = default;
};

View File

@ -39,18 +39,6 @@ EngineUtil::EngineUtil()
desc_.adapterType = ADAPTER_TYPE_BUT;
}
bool EngineUtil::CreateAdapterInner(OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterType type)
{
desc_.adapterType = type;
adapter_ = EngineHostManager::GetInstance().CreateEngineAdapter(desc_);
if (adapter_ == nullptr) {
INTELL_VOICE_LOG_ERROR("adapter is nullptr");
return false;
}
return true;
}
int32_t EngineUtil::SetParameter(const std::string &keyValueList)
{
if (adapter_ == nullptr) {
@ -238,12 +226,6 @@ void EngineUtil::ProcDspModel(OHOS::HDI::IntelligentVoice::Engine::V1_0::Content
triggerMgr->UpdateModel(model);
}
void EngineUtil::ReleaseAdapterInner()
{
EngineHostManager::GetInstance().ReleaseEngineAdapter(desc_);
adapter_ = nullptr;
}
void EngineUtil::SetLanguage()
{
if (adapter_ == nullptr) {

View File

@ -19,15 +19,71 @@
#include <string>
#include <map>
#include <ashmem.h>
#include "adapter_host_manager.h"
#include "i_adapter_host_manager.h"
#include <i_intell_voice_engine_callback.h>
#include "v1_0/iintell_voice_engine_adapter.h"
#include "v1_0/iintell_voice_engine_callback.h"
namespace OHOS {
namespace IntellVoiceEngine {
using OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineCallback;
enum EngineEvent {
NONE = 0,
INIT,
INIT_DONE,
START_RECOGNIZE,
STOP_RECOGNIZE,
RECOGNIZE_COMPLETE,
RECONFIRM_RECOGNITION_COMPLETE,
START_CAPTURER,
READ,
STOP_CAPTURER,
RECOGNIZING_TIMEOUT,
RECOGNIZE_COMPLETE_TIMEOUT,
READ_CAPTURER_TIMEOUT,
SET_LISTENER,
SET_PARAM,
GET_PARAM,
GET_WAKEUP_PCM,
RELEASE_ADAPTER,
RESET_ADAPTER,
RELEASE,
};
struct SetListenerMsg {
explicit SetListenerMsg(sptr<IIntelligentVoiceEngineCallback> cb) : callback(cb) {}
sptr<IIntelligentVoiceEngineCallback> callback = nullptr;
};
struct CapturerData {
std::vector<uint8_t> data;
};
struct StringParam {
explicit StringParam(const std::string &str = "") : strParam(str) {}
std::string strParam;
};
class EngineUtil {
public:
EngineUtil();
~EngineUtil() = default;
bool CreateAdapterInner(OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterType type);
template<typename T> bool CreateAdapterInner(T &mgr,
OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterType type)
{
desc_.adapterType = type;
adapter_ = mgr.CreateEngineAdapter(desc_);
if (adapter_ == nullptr) {
return false;
}
return true;
}
template<typename T> void ReleaseAdapterInner(T &mgr)
{
mgr.ReleaseEngineAdapter(desc_);
adapter_ = nullptr;
}
int32_t SetParameter(const std::string &keyValueList);
std::string GetParameter(const std::string &key);
int32_t WriteAudio(const uint8_t *buffer, uint32_t size);
@ -36,13 +92,12 @@ public:
int32_t Evaluate(const std::string &word, EvaluationResultInfo &info);
bool SetDspFeatures();
void ProcDspModel(OHOS::HDI::IntelligentVoice::Engine::V1_0::ContentType type);
void ReleaseAdapterInner();
void SetLanguage();
void SetArea();
protected:
static void SplitStringToKVPair(const std::string &inputStr, std::map<std::string, std::string> &kvpairs);
std::shared_ptr<AdapterHostManager> adapter_ = nullptr;
std::shared_ptr<IAdapterHostManager> adapter_ = nullptr;
OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterDescriptor desc_;
private:

View File

@ -23,6 +23,7 @@
#include "adapter_callback_service.h"
#include "intell_voice_service_manager.h"
#include "update_engine_utils.h"
#include "engine_host_manager.h"
#define LOG_TAG "EnrollEngine"
@ -72,7 +73,11 @@ void EnrollEngine::OnEnrollComplete()
bool EnrollEngine::Init(const std::string &param)
{
return EngineUtil::CreateAdapterInner(ENROLL_ADAPTER_TYPE);
if (!EngineUtil::CreateAdapterInner(EngineHostManager::GetInstance(), ENROLL_ADAPTER_TYPE)) {
INTELL_VOICE_LOG_ERROR("failed to create adapter");
return false;
}
return true;
}
void EnrollEngine::SetCallback(sptr<IRemoteObject> object)
@ -147,7 +152,7 @@ int32_t EnrollEngine::Detach(void)
}
int32_t ret = adapter_->Detach();
ReleaseAdapterInner();
ReleaseAdapterInner(EngineHostManager::GetInstance());
return ret;
}

View File

@ -13,6 +13,7 @@
* limitations under the License.
*/
#include "adapter_host_manager.h"
#include "engine_host_manager.h"
#include "intell_voice_log.h"
#include "intell_voice_util.h"
@ -29,11 +30,11 @@ AdapterHostManager::~AdapterHostManager()
adapterProxy1_2_ = nullptr;
}
bool AdapterHostManager::Init(const IntellVoiceEngineAdapterDescriptor &desc,
const sptr<OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineManager> &engineHostProxy1_0,
const sptr<OHOS::HDI::IntelligentVoice::Engine::V1_2::IIntellVoiceEngineManager> &engineHostProxy1_2)
bool AdapterHostManager::Init(const IntellVoiceEngineAdapterDescriptor &desc)
{
INTELL_VOICE_LOG_INFO("enter");
const auto &engineHostProxy1_0 = EngineHostManager::GetInstance().GetEngineHostProxy1_0();
const auto &engineHostProxy1_2 = EngineHostManager::GetInstance().GetEngineHostProxy1_2();
CHECK_CONDITION_RETURN_FALSE(engineHostProxy1_0 == nullptr, "engine host proxy_v1_0 is null");
if (engineHostProxy1_2 != nullptr) {

View File

@ -14,35 +14,27 @@
*/
#ifndef ADAPTER_MANAGER_HOST_H
#define ADAPTER_MANAGER_HOST_H
#include "v1_0/iintell_voice_engine_manager.h"
#include "v1_2/iintell_voice_engine_manager.h"
#include "v1_0/iintell_voice_engine_adapter.h"
#include "v1_2/iintell_voice_engine_adapter.h"
#include "i_adapter_host_manager.h"
namespace OHOS {
namespace IntellVoiceEngine {
using OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterDescriptor;
using OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineCallback;
using OHOS::HDI::IntelligentVoice::Engine::V1_2::EvaluationResultInfo;
class AdapterHostManager {
class AdapterHostManager : public IAdapterHostManager {
public:
AdapterHostManager() = default;
~AdapterHostManager();
bool Init(const IntellVoiceEngineAdapterDescriptor &desc,
const sptr<OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineManager> &engineHostProxy1_0,
const sptr<OHOS::HDI::IntelligentVoice::Engine::V1_2::IIntellVoiceEngineManager> &engineHostProxy1_2);
int32_t SetCallback(const sptr<IIntellVoiceEngineCallback> &engineCallback);
int32_t Attach(const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterInfo &info);
int32_t Detach();
int32_t SetParameter(const std::string &keyValueList);
int32_t GetParameter(const std::string &keyList, std::string &valueList);
int32_t Start(const OHOS::HDI::IntelligentVoice::Engine::V1_0::StartInfo &info);
int32_t Stop();
int32_t WriteAudio(const std::vector<uint8_t> &buffer);
int32_t Read(OHOS::HDI::IntelligentVoice::Engine::V1_0::ContentType type, sptr<Ashmem> &buffer);
int32_t GetWakeupPcm(std::vector<uint8_t> &data);
int32_t Evaluate(const std::string &word, EvaluationResultInfo &info);
~AdapterHostManager() override;
bool Init(const IntellVoiceEngineAdapterDescriptor &desc) override;
int32_t SetCallback(const sptr<IIntellVoiceEngineCallback> &engineCallback) override;
int32_t Attach(const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterInfo &info) override;
int32_t Detach() override;
int32_t SetParameter(const std::string &keyValueList) override;
int32_t GetParameter(const std::string &keyList, std::string &valueList) override;
int32_t Start(const OHOS::HDI::IntelligentVoice::Engine::V1_0::StartInfo &info) override;
int32_t Stop() override;
int32_t WriteAudio(const std::vector<uint8_t> &buffer) override;
int32_t Read(OHOS::HDI::IntelligentVoice::Engine::V1_0::ContentType type, sptr<Ashmem> &buffer) override;
int32_t GetWakeupPcm(std::vector<uint8_t> &data) override;
int32_t Evaluate(const std::string &word, EvaluationResultInfo &info) override;
private:
sptr<OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineAdapter> adapterProxy1_0_ = nullptr;

View File

@ -18,6 +18,7 @@
#include "intell_voice_log.h"
#include "data_operation_callback.h"
#include "intell_voice_util.h"
#include "adapter_host_manager.h"
#define LOG_TAG "EngineHostManager"
@ -56,9 +57,8 @@ bool EngineHostManager::Init()
if (castResult_V1_1 != nullptr) {
engineHostProxy1_1_ = castResult_V1_1;
}
}
if (IntellVoiceUtil::GetHdiVersionId(majorVer, minorVer) == IntellVoiceUtil::GetHdiVersionId(1, MINOR_VERSION_2)) {
} else if (IntellVoiceUtil::GetHdiVersionId(majorVer, minorVer) ==
IntellVoiceUtil::GetHdiVersionId(1, MINOR_VERSION_2)) {
INTELL_VOICE_LOG_INFO("version is 1.2");
auto castResult_V1_2 =
OHOS::HDI::IntelligentVoice::Engine::V1_2::IIntellVoiceEngineManager::CastFrom(engineHostProxy1_0_);
@ -141,7 +141,7 @@ void EngineHostManager::SetDataOprCallback()
engineHostProxy1_1_->SetDataOprCallback(dataOprCb_);
}
std::shared_ptr<AdapterHostManager> EngineHostManager::CreateEngineAdapter(
std::shared_ptr<IAdapterHostManager> EngineHostManager::CreateEngineAdapter(
const IntellVoiceEngineAdapterDescriptor &desc)
{
INTELL_VOICE_LOG_INFO("enter");
@ -156,7 +156,7 @@ std::shared_ptr<AdapterHostManager> EngineHostManager::CreateEngineAdapter(
return nullptr;
}
if (!adapter->Init(desc, engineHostProxy1_0_, engineHostProxy1_2_)) {
if (!adapter->Init(desc)) {
return nullptr;
}

View File

@ -17,7 +17,7 @@
#include "v1_0/iintell_voice_engine_manager.h"
#include "v1_2/iintell_voice_engine_manager.h"
#include "intell_voice_death_recipient.h"
#include "adapter_host_manager.h"
#include "i_adapter_host_manager.h"
namespace OHOS {
namespace IntellVoiceEngine {
@ -40,12 +40,24 @@ public:
bool RegisterEngineHDIDeathRecipient();
void DeregisterEngineHDIDeathRecipient();
void SetDataOprCallback();
std::shared_ptr<AdapterHostManager> CreateEngineAdapter(const IntellVoiceEngineAdapterDescriptor &desc);
std::shared_ptr<IAdapterHostManager> CreateEngineAdapter(const IntellVoiceEngineAdapterDescriptor &desc);
void ReleaseEngineAdapter(const IntellVoiceEngineAdapterDescriptor &desc);
int GetUploadFiles(int numMax, std::vector<UploadHdiFile> &files);
int32_t GetWakeupSourceFilesList(std::vector<std::string>& cloneFiles);
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);
const sptr<OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineManager> &GetEngineHostProxy1_0()
{
return engineHostProxy1_0_;
}
const sptr<OHOS::HDI::IntelligentVoice::Engine::V1_1::IIntellVoiceEngineManager> &GetEngineHostProxy1_1()
{
return engineHostProxy1_1_;
}
const sptr<OHOS::HDI::IntelligentVoice::Engine::V1_2::IIntellVoiceEngineManager> &GetEngineHostProxy1_2()
{
return engineHostProxy1_2_;
}
private:
static void OnEngineHDIDiedCallback();

View File

@ -0,0 +1,111 @@
/*
* Copyright (c) 2024 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 "headset_adapter_host_manager.h"
#include "headset_host_manager.h"
#include "intell_voice_log.h"
#include "intell_voice_util.h"
#define LOG_TAG "HeadsetAdapterHostMgr"
using namespace OHOS::IntellVoiceUtils;
namespace OHOS {
namespace IntellVoiceEngine {
HeadsetAdapterHostManager::~HeadsetAdapterHostManager()
{
headsetAdapterProxy1_0_ = nullptr;
}
bool HeadsetAdapterHostManager::Init(const IntellVoiceEngineAdapterDescriptor &desc)
{
INTELL_VOICE_LOG_INFO("enter");
const auto &headsetHostProxy1_0 = HeadsetHostManager::GetInstance().GetHeadsetHostProxy1_0();
CHECK_CONDITION_RETURN_FALSE(headsetHostProxy1_0 == nullptr, "headset host proxy_v1_0 is null");
headsetHostProxy1_0->CreateAdapter(desc, headsetAdapterProxy1_0_);
if (headsetAdapterProxy1_0_ == nullptr) {
INTELL_VOICE_LOG_ERROR("engine adapter proxy1_0_ is nullptr");
return false;
}
return true;
}
int32_t HeadsetAdapterHostManager::SetCallback(const sptr<IIntellVoiceEngineCallback> &engineCallback)
{
CHECK_CONDITION_RETURN_RET(headsetAdapterProxy1_0_ == nullptr, -1, "adapter proxy is null");
return headsetAdapterProxy1_0_->SetCallback(engineCallback);
}
int32_t HeadsetAdapterHostManager::Attach(
const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterInfo &info)
{
CHECK_CONDITION_RETURN_RET(headsetAdapterProxy1_0_ == nullptr, -1, "adapter proxy is null");
return headsetAdapterProxy1_0_->Attach(info);
}
int32_t HeadsetAdapterHostManager::Detach()
{
CHECK_CONDITION_RETURN_RET(headsetAdapterProxy1_0_ == nullptr, -1, "adapter proxy is null");
return headsetAdapterProxy1_0_->Detach();
}
int32_t HeadsetAdapterHostManager::SetParameter(const std::string &keyValueList)
{
CHECK_CONDITION_RETURN_RET(headsetAdapterProxy1_0_ == nullptr, -1, "adapter proxy is null");
return headsetAdapterProxy1_0_->SetParameter(keyValueList);
}
int32_t HeadsetAdapterHostManager::GetParameter(const std::string &keyList, std::string &valueList)
{
CHECK_CONDITION_RETURN_RET(headsetAdapterProxy1_0_ == nullptr, -1, "adapter proxy is null");
return headsetAdapterProxy1_0_->GetParameter(keyList, valueList);
}
int32_t HeadsetAdapterHostManager::Start(const OHOS::HDI::IntelligentVoice::Engine::V1_0::StartInfo &info)
{
CHECK_CONDITION_RETURN_RET(headsetAdapterProxy1_0_ == nullptr, -1, "adapter proxy is null");
return headsetAdapterProxy1_0_->Start(info);
}
int32_t HeadsetAdapterHostManager::Stop()
{
CHECK_CONDITION_RETURN_RET(headsetAdapterProxy1_0_ == nullptr, -1, "adapter proxy is null");
return headsetAdapterProxy1_0_->Stop();
}
int32_t HeadsetAdapterHostManager::WriteAudio(const std::vector<uint8_t> &buffer)
{
CHECK_CONDITION_RETURN_RET(headsetAdapterProxy1_0_ == nullptr, -1, "adapter proxy is null");
return headsetAdapterProxy1_0_->WriteAudio(buffer);
}
int32_t HeadsetAdapterHostManager::Read(OHOS::HDI::IntelligentVoice::Engine::V1_0::ContentType /* type */,
sptr<Ashmem> & /* buffer */)
{
return 0;
}
int32_t HeadsetAdapterHostManager::GetWakeupPcm(std::vector<uint8_t> & /* data */)
{
return 0;
}
int32_t HeadsetAdapterHostManager::Evaluate(const std::string & /* word */, EvaluationResultInfo & /* info */)
{
return 0;
}
}
}

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2024 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 HEADSET_ADAPTER_MANAGER_HOST_H
#define HEADSET_ADAPTER_MANAGER_HOST_H
#include "i_adapter_host_manager.h"
namespace OHOS {
namespace IntellVoiceEngine {
class HeadsetAdapterHostManager : public IAdapterHostManager {
public:
HeadsetAdapterHostManager() = default;
~HeadsetAdapterHostManager() override;
bool Init(const IntellVoiceEngineAdapterDescriptor &desc) override;
int32_t SetCallback(const sptr<IIntellVoiceEngineCallback> &engineCallback) override;
int32_t Attach(const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterInfo &info) override;
int32_t Detach() override;
int32_t SetParameter(const std::string &keyValueList) override;
int32_t GetParameter(const std::string &keyList, std::string &valueList) override;
int32_t Start(const OHOS::HDI::IntelligentVoice::Engine::V1_0::StartInfo &info) override;
int32_t Stop() override;
int32_t WriteAudio(const std::vector<uint8_t> &buffer) override;
int32_t Read(OHOS::HDI::IntelligentVoice::Engine::V1_0::ContentType type, sptr<Ashmem> &buffer) override;
int32_t GetWakeupPcm(std::vector<uint8_t> &data) override;
int32_t Evaluate(const std::string &word, EvaluationResultInfo &info) override;
private:
sptr<OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineAdapter> headsetAdapterProxy1_0_ = nullptr;
};
}
}
#endif

View File

@ -0,0 +1,134 @@
/*
* Copyright (c) 2024 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 "headset_host_manager.h"
#include "iproxy_broker.h"
#include "intell_voice_log.h"
#include "data_operation_callback.h"
#include "intell_voice_util.h"
#include "intell_voice_service_manager.h"
#include "headset_adapter_host_manager.h"
#define LOG_TAG "HeadsetHostManager"
using namespace OHOS::IntellVoiceUtils;
namespace OHOS {
namespace IntellVoiceEngine {
const std::string HEADSET_SERVICE_NAME = "intell_voice_headset_manager_service";
HeadsetHostManager::~HeadsetHostManager()
{
headsetHostProxy1_0_ = nullptr;
engineHdiDeathRecipient_ = nullptr;
}
bool HeadsetHostManager::Init()
{
INTELL_VOICE_LOG_INFO("enter");
headsetHostProxy1_0_ =
OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineManager::Get(HEADSET_SERVICE_NAME, true);
if (headsetHostProxy1_0_ == nullptr) {
INTELL_VOICE_LOG_ERROR("headsetHostProxy1_0_ is nullptr");
return false;
}
return true;
}
bool HeadsetHostManager::RegisterEngineHDIDeathRecipient()
{
INTELL_VOICE_LOG_INFO("enter");
if (headsetHostProxy1_0_ == nullptr) {
INTELL_VOICE_LOG_ERROR("failed to get engine manager");
return false;
}
sptr<IRemoteObject> object = OHOS::HDI::hdi_objcast<
OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineManager>(headsetHostProxy1_0_);
if (object == nullptr) {
INTELL_VOICE_LOG_ERROR("object is nullptr");
return false;
}
engineHdiDeathRecipient_ = new (std::nothrow) IntellVoiceDeathRecipient(
std::bind(&HeadsetHostManager::OnEngineHDIDiedCallback));
if (engineHdiDeathRecipient_ == nullptr) {
INTELL_VOICE_LOG_ERROR("create death recipient failed");
return false;
}
return object->AddDeathRecipient(engineHdiDeathRecipient_);
}
void HeadsetHostManager::DeregisterEngineHDIDeathRecipient()
{
INTELL_VOICE_LOG_INFO("enter");
if (engineHdiDeathRecipient_ == nullptr) {
INTELL_VOICE_LOG_ERROR("recipient is nullptr");
return;
}
if (headsetHostProxy1_0_ == nullptr) {
INTELL_VOICE_LOG_ERROR("headsetHostProxy1_0_ is nullptr");
return;
}
sptr<IRemoteObject> object = OHOS::HDI::hdi_objcast<
OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineManager>(headsetHostProxy1_0_);
if (object == nullptr) {
INTELL_VOICE_LOG_ERROR("object is nullptr");
return;
}
object->RemoveDeathRecipient(engineHdiDeathRecipient_);
}
std::shared_ptr<IAdapterHostManager> HeadsetHostManager::CreateEngineAdapter(
const IntellVoiceEngineAdapterDescriptor &desc)
{
if (headsetHostProxy1_0_ == nullptr) {
INTELL_VOICE_LOG_ERROR("headsetHostProxy1_0_ is nullptr");
return nullptr;
}
auto adapter = std::make_shared<HeadsetAdapterHostManager>();
if (adapter == nullptr) {
INTELL_VOICE_LOG_ERROR("failed to create engine adapter");
return nullptr;
}
if (!adapter->Init(desc)) {
return nullptr;
}
return adapter;
}
void HeadsetHostManager::ReleaseEngineAdapter(const IntellVoiceEngineAdapterDescriptor &desc)
{
if (headsetHostProxy1_0_ == nullptr) {
INTELL_VOICE_LOG_ERROR("headsetHostProxy1_0_ is nullptr");
return;
}
headsetHostProxy1_0_->ReleaseAdapter(desc);
}
void HeadsetHostManager::OnEngineHDIDiedCallback()
{
INTELL_VOICE_LOG_INFO("enter");
IntellVoiceServiceManager::GetInstance()->HandleHeadsetHostDie();
}
}
}

View File

@ -0,0 +1,55 @@
/*
* 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 HEADSET_MANAGER_HOST_H
#define HEADSET_MANAGER_HOST_H
#include "v1_0/iintell_voice_engine_manager.h"
#include "v1_1/iintell_voice_engine_manager.h"
#include "intell_voice_death_recipient.h"
#include "i_adapter_host_manager.h"
namespace OHOS {
namespace IntellVoiceEngine {
using OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineAdapter;
using OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterDescriptor;
class HeadsetHostManager {
public:
HeadsetHostManager() = default;
~HeadsetHostManager();
static HeadsetHostManager &GetInstance()
{
static HeadsetHostManager headsetHostMgr;
return headsetHostMgr;
}
bool Init();
bool RegisterEngineHDIDeathRecipient();
void DeregisterEngineHDIDeathRecipient();
std::shared_ptr<IAdapterHostManager> CreateEngineAdapter(const IntellVoiceEngineAdapterDescriptor &desc);
void ReleaseEngineAdapter(const IntellVoiceEngineAdapterDescriptor &desc);
const sptr<OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineManager> &GetHeadsetHostProxy1_0()
{
return headsetHostProxy1_0_;
}
private:
static void OnEngineHDIDiedCallback();
sptr<OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineManager> headsetHostProxy1_0_ = nullptr;
sptr<OHOS::IntellVoiceUtils::IntellVoiceDeathRecipient> engineHdiDeathRecipient_ = nullptr;
};
}
}
#endif

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2024 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 I_ADAPTER_HOST_MANAGER_H
#define I_ADAPTER_HOST_MANAGER_H
#include "v1_0/iintell_voice_engine_manager.h"
#include "v1_2/iintell_voice_engine_manager.h"
#include "v1_0/iintell_voice_engine_adapter.h"
#include "v1_2/iintell_voice_engine_adapter.h"
namespace OHOS {
namespace IntellVoiceEngine {
using OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterDescriptor;
using OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineCallback;
using OHOS::HDI::IntelligentVoice::Engine::V1_2::EvaluationResultInfo;
class IAdapterHostManager {
public:
IAdapterHostManager() = default;
virtual ~IAdapterHostManager() = default;
virtual bool Init(const IntellVoiceEngineAdapterDescriptor &desc) = 0;
virtual int32_t SetCallback(const sptr<IIntellVoiceEngineCallback> &engineCallback) = 0;
virtual int32_t Attach(const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterInfo &info) = 0;
virtual int32_t Detach() = 0;
virtual int32_t SetParameter(const std::string &keyValueList) = 0;
virtual int32_t GetParameter(const std::string &keyList, std::string &valueList) = 0;
virtual int32_t Start(const OHOS::HDI::IntelligentVoice::Engine::V1_0::StartInfo &info) = 0;
virtual int32_t Stop() = 0;
virtual int32_t WriteAudio(const std::vector<uint8_t> &buffer) = 0;
virtual int32_t Read(OHOS::HDI::IntelligentVoice::Engine::V1_0::ContentType type, sptr<Ashmem> &buffer) = 0;
virtual int32_t GetWakeupPcm(std::vector<uint8_t> &data) = 0;
virtual int32_t Evaluate(const std::string &word, EvaluationResultInfo &info) = 0;
};
}
}
#endif

View File

@ -49,6 +49,10 @@ IntellVoiceEngineStub::IntellVoiceEngineStub()
MessageParcel &reply) -> int32_t { return this->GetWakeupPcmInner(data, reply); };
processFuncMap_[INTELL_VOICE_ENGINE_EVALUATE] = [this](MessageParcel &data,
MessageParcel &reply) -> int32_t { return this->EvaluateInner(data, reply); };
processFuncMap_[INTELL_VOICE_ENGINE_NOTIFY_HEADSET_WAKE_EVENT] = [this](MessageParcel &data,
MessageParcel &reply) -> int32_t { return this->NotifyHeadSetWakeEventInner(data, reply); };
processFuncMap_[INTELL_VOICE_ENGINE_NOTIFY_HEADSET_HOSTEVENT] = [this](MessageParcel &data,
MessageParcel &reply) -> int32_t { return this->NotifyHeadSetHostEventInner(data, reply); };
}
IntellVoiceEngineStub::~IntellVoiceEngineStub()
@ -206,5 +210,22 @@ int32_t IntellVoiceEngineStub::EvaluateInner(MessageParcel &data, MessageParcel
reply.WriteInt32(info.resultCode);
return ret;
}
int32_t IntellVoiceEngineStub::NotifyHeadSetWakeEventInner(MessageParcel &data, MessageParcel &reply)
{
INTELL_VOICE_LOG_ERROR("enter");
int32_t ret = NotifyHeadsetWakeEvent();
reply.WriteInt32(ret);
return ret;
}
int32_t IntellVoiceEngineStub::NotifyHeadSetHostEventInner(MessageParcel &data, MessageParcel &reply)
{
INTELL_VOICE_LOG_ERROR("enter");
int event = data.ReadInt32();
int32_t ret = NotifyHeadsetHostEvent(static_cast<OHOS::IntellVoiceEngine::HeadsetHostEventType>(event));
reply.WriteInt32(ret);
return ret;
}
}
}

View File

@ -42,6 +42,8 @@ private:
int32_t StopCapturerInner(MessageParcel &data, MessageParcel &reply);
int32_t GetWakeupPcmInner(MessageParcel &data, MessageParcel &reply);
int32_t EvaluateInner(MessageParcel &data, MessageParcel &reply);
int32_t NotifyHeadSetWakeEventInner(MessageParcel &data, MessageParcel &reply);
int32_t NotifyHeadSetHostEventInner(MessageParcel &data, MessageParcel &reply);
std::map<uint32_t, std::function<int32_t(MessageParcel &data, MessageParcel &reply)>> processFuncMap_;
};

View File

@ -794,7 +794,6 @@ int32_t IntellVoiceServiceManager::UnloadIntellVoiceService()
INTELL_VOICE_LOG_ERROR("failed to unload intellvoice service, ret: %{public}d", ret);
return;
}
INTELL_VOICE_LOG_INFO("success to notify samgr to unload intell voice service");
}).detach();
@ -820,5 +819,15 @@ void IntellVoiceServiceManager::HandleServiceStop()
{
TaskExecutor::AddSyncTask([this]() -> int32_t { return ServiceStopProc(); });
}
void IntellVoiceServiceManager::HandleHeadsetHostDie()
{
TaskExecutor::AddAsyncTask([this]() {
auto engine = GetEngine(INTELL_VOICE_WAKEUP, engines_);
if (engine != nullptr) {
engine->NotifyHeadsetHostEvent(HEADSET_HOST_ON);
}
});
}
} // namespace IntellVoiceEngine
} // namespace OHOS

View File

@ -72,6 +72,7 @@ public:
void HandleUnloadIntellVoiceService(bool isAsync);
bool HandleOnIdle();
void HandleServiceStop();
void HandleHeadsetHostDie();
void ProcBreathModel();
void CreateSwitchProvider();

View File

@ -24,6 +24,7 @@
#include "adapter_callback_service.h"
#include "intell_voice_service_manager.h"
#include "update_engine_utils.h"
#include "engine_host_manager.h"
#define LOG_TAG "UpdateEngine"
@ -80,7 +81,8 @@ void UpdateEngine::OnUpdateEvent(int32_t msgId, int32_t result)
bool UpdateEngine::Init(const std::string &param)
{
if (!EngineUtil::CreateAdapterInner(UPDATE_ADAPTER_TYPE)) {
if (!EngineUtil::CreateAdapterInner(EngineHostManager::GetInstance(), UPDATE_ADAPTER_TYPE)) {
INTELL_VOICE_LOG_ERROR("failed to create adapter");
return false;
}
@ -103,7 +105,7 @@ bool UpdateEngine::Init(const std::string &param)
int ret = Attach(info);
if (ret != 0) {
INTELL_VOICE_LOG_ERROR("attach err");
ReleaseAdapterInner();
EngineUtil::ReleaseAdapterInner(EngineHostManager::GetInstance());
return false;
}
@ -154,7 +156,7 @@ int32_t UpdateEngine::Detach(void)
}
int ret = adapter_->Detach();
ReleaseAdapterInner();
EngineUtil::ReleaseAdapterInner(EngineHostManager::GetInstance());
if (updateResult_ == UpdateState::UPDATE_STATE_DEFAULT) {
INTELL_VOICE_LOG_WARN("detach defore receive commit enroll msg");

View File

@ -0,0 +1,413 @@
/*
* Copyright (c) 2024 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 "headset_wakeup_engine_impl.h"
#include "audio_system_manager.h"
#include "adapter_callback_service.h"
#include "intell_voice_log.h"
#include "history_info_mgr.h"
#include "intell_voice_util.h"
#include "intell_voice_service_manager.h"
#include "headset_host_manager.h"
#define LOG_TAG "HeadsetWakeupEngineImpl"
using namespace OHOS::HDI::IntelligentVoice::Engine::V1_0;
using namespace OHOS::IntellVoiceUtils;
using namespace OHOS::AudioStandard;
using namespace std;
namespace OHOS {
namespace IntellVoiceEngine {
static constexpr int64_t RECOGNIZING_TIMEOUT_US = 10 * 1000 * 1000; //10s
static constexpr int64_t RECOGNIZE_COMPLETE_TIMEOUT_US = 1 * 1000; //1ms
static constexpr int64_t READ_CAPTURER_TIMEOUT_US = 10 * 1000 * 1000; //10s
HeadsetWakeupEngineImpl::HeadsetWakeupEngineImpl() : ModuleStates(State(IDLE), "HeadsetWakeupEngineImpl")
{
}
HeadsetWakeupEngineImpl::~HeadsetWakeupEngineImpl()
{
}
bool HeadsetWakeupEngineImpl::Init()
{
if (!InitStates()) {
INTELL_VOICE_LOG_ERROR("init state failed");
return false;
}
adapterListener_ = std::make_shared<WakeupAdapterListener>(
std::bind(&HeadsetWakeupEngineImpl::OnWakeupEvent, this, std::placeholders::_1, std::placeholders::_2));
if (adapterListener_ == nullptr) {
INTELL_VOICE_LOG_ERROR("adapterListener_ is nullptr");
return false;
}
return true;
}
bool HeadsetWakeupEngineImpl::InitStates()
{
for (int i = IDLE; i <= READ_CAPTURER; i++) {
ForState(State(i))
.ACT(SET_LISTENER, HandleSetListener)
.ACT(SET_PARAM, HandleSetParam);
}
ForState(IDLE)
.ACT(INIT, HandleInit)
.ACT(RESET_ADAPTER, HandleResetAdapter);
ForState(INITIALIZING)
.ACT(INIT_DONE, HandleInitDone);
ForState(INITIALIZED)
.ACT(START_RECOGNIZE, HandleStart);
ForState(RECOGNIZING)
.WaitUntil(RECOGNIZING_TIMEOUT, std::bind(&HeadsetWakeupEngineImpl::HandleRecognizingTimeout,
this, std::placeholders::_1, std::placeholders::_2), RECOGNIZING_TIMEOUT_US)
.ACT(STOP_RECOGNIZE, HandleStop)
.ACT(RECOGNIZE_COMPLETE, HandleRecognizeComplete);
ForState(RECOGNIZED)
.WaitUntil(RECOGNIZE_COMPLETE_TIMEOUT, std::bind(&HeadsetWakeupEngineImpl::HandleStopCapturer,
this, std::placeholders::_1, std::placeholders::_2), RECOGNIZE_COMPLETE_TIMEOUT_US)
.ACT(START_CAPTURER, HandleStartCapturer);
ForState(READ_CAPTURER)
.WaitUntil(READ_CAPTURER_TIMEOUT, std::bind(&HeadsetWakeupEngineImpl::HandleStopCapturer,
this, std::placeholders::_1, std::placeholders::_2), READ_CAPTURER_TIMEOUT_US)
.ACT(READ, HandleRead)
.ACT(STOP_CAPTURER, HandleStopCapturer);
FromState(INITIALIZING, READ_CAPTURER)
.ACT(RELEASE_ADAPTER, HandleRelease)
.ACT(RELEASE, HandleRelease);
return IsStatesInitSucc();
}
int32_t HeadsetWakeupEngineImpl::Handle(const StateMsg &msg)
{
if (!IsStatesInitSucc()) {
INTELL_VOICE_LOG_ERROR("failed to init state");
return -1;
}
return ModuleStates::HandleMsg(msg);
}
bool HeadsetWakeupEngineImpl::SetCallbackInner()
{
INTELL_VOICE_LOG_INFO("enter");
if (adapter_ == nullptr) {
INTELL_VOICE_LOG_ERROR("adapter is nullptr");
return false;
}
if (adapterListener_ == nullptr) {
INTELL_VOICE_LOG_ERROR("adapterListener_ is nullptr");
return false;
}
callback_ = sptr<IIntellVoiceEngineCallback>(new (std::nothrow) AdapterCallbackService(adapterListener_));
if (callback_ == nullptr) {
INTELL_VOICE_LOG_ERROR("callback_ is nullptr");
return false;
}
adapter_->SetCallback(callback_);
return true;
}
int32_t HeadsetWakeupEngineImpl::AttachInner(const IntellVoiceEngineInfo &info)
{
INTELL_VOICE_LOG_INFO("enter");
if (adapter_ == nullptr) {
INTELL_VOICE_LOG_ERROR("adapter is nullptr");
return -1;
}
IntellVoiceEngineAdapterInfo adapterInfo = {
};
return adapter_->Attach(adapterInfo);
}
void HeadsetWakeupEngineImpl::OnWakeupEvent(int32_t msgId, int32_t result)
{
INTELL_VOICE_LOG_INFO("enter, msgId:%{public}d, result:%{public}d", msgId, result);
if (msgId == INTELL_VOICE_ENGINE_MSG_INIT_DONE) {
std::thread(&HeadsetWakeupEngineImpl::OnInitDone, this, result).detach();
} else if (msgId == HDI::IntelligentVoice::Engine::V1_2::INTELL_VOICE_ENGINE_MSG_HEADSET_RECOGNIZE_COMPLETE) {
std::thread(&HeadsetWakeupEngineImpl::OnWakeupRecognition, this, result).detach();
} else {
}
}
void HeadsetWakeupEngineImpl::OnInitDone(int32_t result)
{
INTELL_VOICE_LOG_INFO("enter, result:%{public}d", result);
StateMsg msg(INIT_DONE, &result, sizeof(int32_t));
Handle(msg);
}
void HeadsetWakeupEngineImpl::OnWakeupRecognition(int32_t result)
{
INTELL_VOICE_LOG_INFO("enter, result:%{public}d", result);
StateMsg msg(RECOGNIZE_COMPLETE, &result, sizeof(int32_t));
Handle(msg);
}
int32_t HeadsetWakeupEngineImpl::HandleInit(const StateMsg & /* msg */, State &nextState)
{
INTELL_VOICE_LOG_INFO("enter");
if (!EngineUtil::CreateAdapterInner(HeadsetHostManager::GetInstance(), WAKEUP_ADAPTER_TYPE)) {
INTELL_VOICE_LOG_ERROR("failed to create adapter");
return -1;
}
if (!SetCallbackInner()) {
INTELL_VOICE_LOG_ERROR("failed to set callback");
return -1;
}
EngineUtil::SetLanguage();
EngineUtil::SetArea();
adapter_->SetParameter("model_path=/data/local/tmp/encoder.om");
IntellVoiceEngineInfo info = {
};
if (AttachInner(info) != 0) {
INTELL_VOICE_LOG_ERROR("failed to attach");
return -1;
}
nextState = State(INITIALIZING);
return 0;
}
int32_t HeadsetWakeupEngineImpl::HandleInitDone(const StateMsg &msg, State &nextState)
{
INTELL_VOICE_LOG_INFO("enter");
int32_t *result = reinterpret_cast<int32_t *>(msg.inMsg);
if ((result == nullptr) || (*result != 0)) {
INTELL_VOICE_LOG_ERROR("init done failed");
return -1;
}
nextState = State(INITIALIZED);
return 0;
}
int32_t HeadsetWakeupEngineImpl::HandleSetListener(const StateMsg &msg, State & /* nextState */)
{
SetListenerMsg *listenerMsg = reinterpret_cast<SetListenerMsg *>(msg.inMsg);
if (listenerMsg == nullptr || adapterListener_ == nullptr) {
INTELL_VOICE_LOG_ERROR("listenerMsg or adapter listener is nullptr");
return -1;
}
adapterListener_->SetCallback(listenerMsg->callback);
return 0;
}
int32_t HeadsetWakeupEngineImpl::HandleStart(const StateMsg & /* msg */, State &nextState)
{
INTELL_VOICE_LOG_INFO("enter");
if (adapter_ == nullptr) {
INTELL_VOICE_LOG_ERROR("adapter is nullptr");
return -1;
}
StartInfo info = {
.isLast = true,
};
if (adapter_->Start(info)) {
INTELL_VOICE_LOG_ERROR("start adapter failed");
return -1;
}
if (!StartAudioSource()) {
INTELL_VOICE_LOG_ERROR("start audio source failed");
adapter_->Stop();
return -1;
}
INTELL_VOICE_LOG_INFO("exit");
nextState = State(RECOGNIZING);
return 0;
}
void HeadsetWakeupEngineImpl::ReadThread()
{
while (isReading_.load()) {
std::vector<uint8_t> audioStream;
int ret = ReadHeadsetStream(audioStream);
if (ret == -1) {
INTELL_VOICE_LOG_INFO("finish reading");
adapter_->SetParameter("end_of_pcm=true");
break;
}
adapter_->WriteAudio(audioStream);
WakeupSourceProcess::Write({ audioStream });
}
}
bool HeadsetWakeupEngineImpl::StartAudioSource()
{
INTELL_VOICE_LOG_INFO("enter");
isReading_.store(true);
WakeupSourceProcess::Init(1);
std::thread t1(std::bind(&HeadsetWakeupEngineImpl::ReadThread, this));
readThread_ = std::move(t1);
return true;
}
void HeadsetWakeupEngineImpl::StopAudioSource()
{
if (!isReading_.load()) {
INTELL_VOICE_LOG_INFO("already stop");
return;
}
isReading_.store(false);
readThread_.join();
StopReadingStream();
WakeupSourceProcess::Release();
}
int32_t HeadsetWakeupEngineImpl::HandleStop(const StateMsg & /* msg */, State &nextState)
{
StopAudioSource();
EngineUtil::Stop();
nextState = State(INITIALIZED);
return 0;
}
int32_t HeadsetWakeupEngineImpl::HandleRecognizeComplete(const StateMsg &msg, State &nextState)
{
EngineUtil::Stop();
int32_t *result = reinterpret_cast<int32_t *>(msg.inMsg);
if (result == nullptr) {
INTELL_VOICE_LOG_ERROR("result is nullptr");
return -1;
}
if (*result != 0) {
INTELL_VOICE_LOG_INFO("wakeup failed");
StopAudioSource();
NotifyVerifyResult(false);
nextState = State(INITIALIZED);
} else {
NotifyVerifyResult(true);
nextState = State(RECOGNIZED);
}
return 0;
}
int32_t HeadsetWakeupEngineImpl::HandleStartCapturer(const StateMsg &msg, State &nextState)
{
INTELL_VOICE_LOG_INFO("enter");
int32_t *msgBody = reinterpret_cast<int32_t *>(msg.inMsg);
if (msgBody == nullptr) {
INTELL_VOICE_LOG_ERROR("msgBody is nullptr");
return -1;
}
channels_ = *msgBody;
nextState = State(READ_CAPTURER);
return 0;
}
int32_t HeadsetWakeupEngineImpl::HandleRead(const StateMsg &msg, State & /* nextState */)
{
CapturerData *capturerData = reinterpret_cast<CapturerData *>(msg.outMsg);
auto ret = WakeupSourceProcess::Read(capturerData->data, channels_);
if (ret != 0) {
INTELL_VOICE_LOG_ERROR("read capturer data failed");
return ret;
}
ResetTimerDelay();
return 0;
}
int32_t HeadsetWakeupEngineImpl::HandleStopCapturer(const StateMsg & /* msg */, State &nextState)
{
INTELL_VOICE_LOG_INFO("enter");
StopAudioSource();
nextState = State(INITIALIZED);
return 0;
}
int32_t HeadsetWakeupEngineImpl::HandleRecognizingTimeout(const StateMsg & /* msg */, State &nextState)
{
INTELL_VOICE_LOG_INFO("enter");
StopAudioSource();
EngineUtil::Stop();
nextState = State(INITIALIZED);
return 0;
}
int32_t HeadsetWakeupEngineImpl::HandleResetAdapter(const StateMsg & /* msg */, State &nextState)
{
INTELL_VOICE_LOG_INFO("enter");
if (!EngineUtil::CreateAdapterInner(HeadsetHostManager::GetInstance(), WAKEUP_ADAPTER_TYPE)) {
INTELL_VOICE_LOG_ERROR("failed to create adapter");
return -1;
}
adapter_->SetCallback(callback_);
EngineUtil::SetLanguage();
EngineUtil::SetArea();
IntellVoiceEngineAdapterInfo adapterInfo = {
};
if (adapter_->Attach(adapterInfo) != 0) {
INTELL_VOICE_LOG_ERROR("failed to attach");
EngineUtil::ReleaseAdapterInner(HeadsetHostManager::GetInstance());
return -1;
}
nextState = State(INITIALIZING);
return 0;
}
int32_t HeadsetWakeupEngineImpl::HandleRelease(const StateMsg & /* msg */, State &nextState)
{
StopAudioSource();
if (adapter_ != nullptr) {
adapter_->Detach();
ReleaseAdapterInner(HeadsetHostManager::GetInstance());
}
nextState = State(IDLE);
return 0;
}
int32_t HeadsetWakeupEngineImpl::HandleSetParam(const StateMsg &msg, State & /* nextState */)
{
StringParam *param = reinterpret_cast<StringParam *>(msg.inMsg);
if (param == nullptr) {
INTELL_VOICE_LOG_INFO("param is nullptr");
return -1;
}
return EngineUtil::SetParameter(param->strParam);
}
}
}

View File

@ -0,0 +1,93 @@
/*
* Copyright (c) 2024 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 HEADSET_WAKEUP_ENGINE_IMPL_H
#define HEADSET_WAKEUP_ENGINE_IMPL_H
#include <memory>
#include <string>
#include <atomic>
#include "v1_0/iintell_voice_engine_callback.h"
#include "audio_info.h"
#include "intell_voice_generic_factory.h"
#include "i_intell_voice_engine.h"
#include "state_manager.h"
#include "engine_util.h"
#include "wakeup_adapter_listener.h"
#include "headset_wakeup_wrapper.h"
#include "wakeup_source_process.h"
namespace OHOS {
namespace IntellVoiceEngine {
using OHOS::IntellVoiceUtils::StateMsg;
using OHOS::IntellVoiceUtils::State;
class HeadsetWakeupEngineImpl : private OHOS::IntellVoiceUtils::ModuleStates, private EngineUtil,
private WakeupSourceProcess, private HeadsetWakeupWrapper {
public:
HeadsetWakeupEngineImpl();
~HeadsetWakeupEngineImpl();
bool Init();
int32_t Handle(const StateMsg &msg);
using HeadsetWakeupWrapper::GetHeadsetAwakeState;
private:
enum EngineState {
IDLE = 0,
INITIALIZING = 1,
INITIALIZED = 2,
RECOGNIZING = 3,
RECOGNIZED = 4,
READ_CAPTURER = 5,
};
private:
bool SetCallbackInner();
int32_t AttachInner(const IntellVoiceEngineInfo &info);
void OnWakeupEvent(int32_t msgId, int32_t result);
void OnInitDone(int32_t result);
void OnWakeupRecognition(int32_t result);
bool StartAudioSource();
void StopAudioSource();
void ReadThread();
private:
bool InitStates();
int32_t HandleSetParam(const StateMsg &msg, State &nextState);
int32_t HandleInit(const StateMsg &msg, State &nextState);
int32_t HandleSetListener(const StateMsg &msg, State &nextState);
int32_t HandleInitDone(const StateMsg &msg, State &nextState);
int32_t HandleStart(const StateMsg &msg, State &nextState);
int32_t HandleStop(const StateMsg &msg, State &nextState);
int32_t HandleRecognizeComplete(const StateMsg &msg, State &nextState);
int32_t HandleStartCapturer(const StateMsg &msg, State &nextState);
int32_t HandleRead(const StateMsg &msg, State &nextState);
int32_t HandleStopCapturer(const StateMsg &msg, State &nextState);
int32_t HandleRecognizingTimeout(const StateMsg &msg, State &nextState);
int32_t HandleResetAdapter(const StateMsg &msg, State &nextState);
int32_t HandleRelease(const StateMsg &msg, State &nextState);
private:
using EngineUtil::adapter_;
int32_t channels_ = 0;
std::atomic<bool> isReading_ = false;
std::thread readThread_;
sptr<OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineCallback> callback_ = nullptr;
std::shared_ptr<WakeupAdapterListener> adapterListener_ = nullptr;
friend class OHOS::IntellVoiceUtils::UniquePtrFactory<HeadsetWakeupEngineImpl>;
};
}
}
#endif

View File

@ -0,0 +1,125 @@
/*
* Copyright (c) 2024 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 "headset_wakeup_wrapper.h"
#include <dlfcn.h>
#include "intell_voice_log.h"
#define LOG_TAG "HeadsetWakeupWrapper"
namespace OHOS {
namespace IntellVoiceEngine {
static const std::string HEADSET_SO_PATH = "/system/lib64/libaam_connection_inner_client.z.so";
int32_t HeadsetWakeupWrapper::LoadHeadsetLib()
{
const char *strError = nullptr;
headsetWakeupPriv_.handle = dlopen(HEADSET_SO_PATH.c_str(), RTLD_LAZY);
if (headsetWakeupPriv_.handle == nullptr) {
strError = dlerror();
INTELL_VOICE_LOG_ERROR("dlopen err:%{public}s", strError);
return -1;
}
(void)dlerror(); // clear existing error
headsetWakeupPriv_.getHeadsetWakeupInst = reinterpret_cast<GetHeadsetWakeupInstFunc>(dlsym(
headsetWakeupPriv_.handle, "GetHeadsetWakeupInst"));
if (headsetWakeupPriv_.getHeadsetWakeupInst == nullptr) {
strError = dlerror();
INTELL_VOICE_LOG_ERROR("dlsym GetHeadSetWakeupInst err:%{public}s", strError);
dlclose(headsetWakeupPriv_.handle);
headsetWakeupPriv_.handle = nullptr;
return -1;
}
INTELL_VOICE_LOG_INFO("load headset lib success");
return 0;
}
void HeadsetWakeupWrapper::UnloadHeadsetLib()
{
if (headsetWakeupPriv_.handle != nullptr) {
dlclose(headsetWakeupPriv_.handle);
headsetWakeupPriv_.handle = nullptr;
}
}
HeadsetWakeupWrapper::HeadsetWakeupWrapper()
{
INTELL_VOICE_LOG_INFO("enter");
if (LoadHeadsetLib() == 0) {
inst_ = headsetWakeupPriv_.getHeadsetWakeupInst();
if (inst_ == nullptr) {
INTELL_VOICE_LOG_ERROR("failed to get headset inst");
}
}
}
HeadsetWakeupWrapper::~HeadsetWakeupWrapper()
{
INTELL_VOICE_LOG_INFO("enter");
UnloadHeadsetLib();
inst_ = nullptr;
}
int32_t HeadsetWakeupWrapper::ReadHeadsetStream(std::vector<uint8_t> &audioStream)
{
INTELL_VOICE_LOG_INFO("enter");
std::lock_guard<std::mutex> lock(mutex_);
if (inst_ == nullptr) {
INTELL_VOICE_LOG_ERROR("inst is nullptr");
return -1;
}
return inst_->ReadHeadsetStream(audioStream);
}
int32_t HeadsetWakeupWrapper::NotifyVerifyResult(bool result)
{
INTELL_VOICE_LOG_INFO("enter");
std::lock_guard<std::mutex> lock(mutex_);
if (inst_ == nullptr) {
INTELL_VOICE_LOG_ERROR("inst is nullptr");
return -1;
}
return inst_->NotifyVerifyResult(result);
}
int32_t HeadsetWakeupWrapper::StopReadingStream()
{
INTELL_VOICE_LOG_INFO("enter");
std::lock_guard<std::mutex> lock(mutex_);
if (inst_ == nullptr) {
INTELL_VOICE_LOG_ERROR("inst is nullptr");
return -1;
}
return inst_->StopReadingStream();
}
int32_t HeadsetWakeupWrapper::GetHeadsetAwakeState()
{
INTELL_VOICE_LOG_INFO("enter");
std::lock_guard<std::mutex> lock(mutex_);
if (inst_ == nullptr) {
INTELL_VOICE_LOG_ERROR("inst is nullptr");
return -1;
}
return inst_->GetHeadsetAwakeState();
}
} // namespace IntellVoice
} // namespace OHOS

View File

@ -0,0 +1,53 @@
/*
* Copyright (c) 2024 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 HEADSET_WAKEUP_WRAPPER_H
#define HEADSET_WAKEUP_WRAPPER_H
#include <string>
#include <mutex>
#include "i_headset_wakeup.h"
namespace OHOS {
namespace IntellVoiceEngine {
using GetHeadsetWakeupInstFunc = IHeadsetWakeup *(*)();
struct HeadsetWakeupManagerPriv {
void *handle { nullptr };
GetHeadsetWakeupInstFunc getHeadsetWakeupInst { nullptr };
};
class HeadsetWakeupWrapper {
public:
HeadsetWakeupWrapper();
~HeadsetWakeupWrapper();
int32_t ReadHeadsetStream(std::vector<uint8_t> &audioStream);
int32_t NotifyVerifyResult(bool result);
int32_t StopReadingStream();
int32_t GetHeadsetAwakeState();
private:
int32_t LoadHeadsetLib();
void UnloadHeadsetLib();
private:
std::mutex mutex_ {};
HeadsetWakeupManagerPriv headsetWakeupPriv_;
IHeadsetWakeup *inst_ = nullptr;
};
}
}
#endif

View File

@ -36,6 +36,7 @@ WakeupAdapterListener::~WakeupAdapterListener()
void WakeupAdapterListener::SetCallback(const sptr<IIntelligentVoiceEngineCallback> &cb)
{
INTELL_VOICE_LOG_INFO("enter");
std::lock_guard<std::mutex> lock(mutex_);
if (cb == nullptr) {
INTELL_VOICE_LOG_INFO("clear callback");
@ -65,7 +66,9 @@ void WakeupAdapterListener::OnIntellVoiceHdiEvent(const IntellVoiceEngineCallBac
historyEvent_ = nullptr;
if ((event.msgId == OHOS::HDI::IntelligentVoice::Engine::V1_0::INTELL_VOICE_ENGINE_MSG_RECOGNIZE_COMPLETE) ||
(event.msgId == static_cast<OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineMessageType>(
INTELL_VOICE_ENGINE_MSG_RECONFIRM_RECOGNITION_COMPLETE))) {
INTELL_VOICE_ENGINE_MSG_RECONFIRM_RECOGNITION_COMPLETE)) || (
event.msgId == static_cast<OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineMessageType>(
INTELL_VOICE_ENGINE_MSG_HEADSET_RECOGNIZE_COMPLETE))) {
cb_->OnIntellVoiceEngineEvent(event);
}
}
@ -75,21 +78,23 @@ void WakeupAdapterListener::OnIntellVoiceHdiEvent(const IntellVoiceEngineCallBac
void WakeupAdapterListener::BackupCallBackEvent(const IntellVoiceEngineCallBackEvent &event)
{
if (event.msgId != OHOS::HDI::IntelligentVoice::Engine::V1_0::INTELL_VOICE_ENGINE_MSG_RECOGNIZE_COMPLETE) {
return;
if ((event.msgId == OHOS::HDI::IntelligentVoice::Engine::V1_0::INTELL_VOICE_ENGINE_MSG_RECOGNIZE_COMPLETE) || (
event.msgId == static_cast<OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineMessageType>(
INTELL_VOICE_ENGINE_MSG_HEADSET_RECOGNIZE_COMPLETE))) {
INTELL_VOICE_LOG_INFO("backup callBackEvent, msg id:%{public}d", event.msgId);
historyEvent_ = std::make_shared<IntellVoiceEngineCallBackEvent>();
if (historyEvent_ == nullptr) {
INTELL_VOICE_LOG_INFO("historyEvent_ is nullptr");
return;
}
historyEvent_->msgId = event.msgId;
historyEvent_->result = event.result;
historyEvent_->info = event.info;
} else {
INTELL_VOICE_LOG_WARN("unknow msg id:%{public}d", event.msgId);
}
INTELL_VOICE_LOG_INFO("Backup CallBackEvent");
historyEvent_ = std::make_shared<IntellVoiceEngineCallBackEvent>();
if (historyEvent_ == nullptr) {
INTELL_VOICE_LOG_INFO("historyEvent_ is nullptr");
return;
}
historyEvent_->msgId = event.msgId;
historyEvent_->result = event.result;
historyEvent_->info = event.info;
}
}
}

View File

@ -14,12 +14,15 @@
*/
#include "wakeup_engine.h"
#include "ability_manager_client.h"
#include "idevmgr_hdi.h"
#include "intell_voice_service_manager.h"
#include "intell_voice_log.h"
#include "headset_host_manager.h"
#define LOG_TAG "WakeupEngine"
using namespace OHOS::IntellVoiceUtils;
using OHOS::HDI::DeviceManager::V1_0::IDeviceManager;
namespace OHOS {
namespace IntellVoiceEngine {
@ -36,7 +39,15 @@ WakeupEngine::~WakeupEngine()
void WakeupEngine::OnDetected(int32_t uuid)
{
INTELL_VOICE_LOG_INFO("enter, uuid is %{public}d", uuid);
std::thread([uuid]() { WakeupEngine::StartAbility(uuid); }).detach();
{
std::lock_guard<std::mutex> lock(headsetMutex_);
if ((headsetImpl_ != nullptr) && (headsetImpl_->GetHeadsetAwakeState() == 1)) {
INTELL_VOICE_LOG_INFO("headset wakeup is exist");
return;
}
}
std::thread([uuid]() { WakeupEngine::StartAbility(GetEventValue(uuid)); }).detach();
StateMsg msg(START_RECOGNIZE, &uuid, sizeof(int32_t));
if (ROLE(WakeupEngineImpl).Handle(msg) != 0) {
INTELL_VOICE_LOG_WARN("start failed");
@ -67,6 +78,12 @@ void WakeupEngine::SetCallback(sptr<IRemoteObject> object)
SetListenerMsg listenerMsg(callback);
StateMsg msg(SET_LISTENER, &listenerMsg, sizeof(SetListenerMsg));
ROLE(WakeupEngineImpl).Handle(msg);
{
std::lock_guard<std::mutex> lock(headsetMutex_);
if (headsetImpl_ != nullptr) {
headsetImpl_->Handle(msg);
}
}
}
int32_t WakeupEngine::Attach(const IntellVoiceEngineInfo & /* info */)
@ -100,6 +117,84 @@ int32_t WakeupEngine::StopCapturer()
return ROLE(WakeupEngineImpl).Handle(msg);
}
int32_t WakeupEngine::NotifyHeadsetWakeEvent()
{
INTELL_VOICE_LOG_INFO("enter");
std::lock_guard<std::mutex> lock(headsetMutex_);
if (headsetImpl_ == nullptr) {
INTELL_VOICE_LOG_ERROR("headset impl is nullptr");
return -1;
}
std::thread([]() { WakeupEngine::StartAbility("headset_event"); }).detach();
StateMsg msg(START_RECOGNIZE);
return headsetImpl_->Handle(msg);
}
int32_t WakeupEngine::HandleHeadsetOff()
{
{
std::lock_guard<std::mutex> lock(headsetMutex_);
if (headsetImpl_ != nullptr) {
StateMsg msg(RELEASE);
if (headsetImpl_->Handle(msg) != 0) {
INTELL_VOICE_LOG_ERROR("release headset wakeup engine impl failed");
}
headsetImpl_ = nullptr;
}
}
HeadsetHostManager::GetInstance().DeregisterEngineHDIDeathRecipient();
auto devmgr = IDeviceManager::Get();
if (devmgr == nullptr) {
INTELL_VOICE_LOG_ERROR("get devmgr failed");
return -1;
}
devmgr->UnloadDevice("intell_voice_headset_manager_service");
return 0;
}
int32_t WakeupEngine::HandleHeadsetOn()
{
auto devmgr = IDeviceManager::Get();
if (devmgr == nullptr) {
INTELL_VOICE_LOG_ERROR("get devmgr failed");
return -1;
}
devmgr->LoadDevice("intell_voice_headset_manager_service");
if (!HeadsetHostManager::GetInstance().Init()) {
INTELL_VOICE_LOG_ERROR("init headset host failed");
return -1;
}
std::lock_guard<std::mutex> lock(headsetMutex_);
headsetImpl_ = UniquePtrFactory<HeadsetWakeupEngineImpl>::CreateInstance();
if (headsetImpl_ == nullptr) {
INTELL_VOICE_LOG_ERROR("failed to allocate headset impl");
return -1;
}
StateMsg msg(INIT);
if (headsetImpl_->Handle(msg) != 0) {
INTELL_VOICE_LOG_ERROR("init headset wakeup engine impl failed");
return -1;
}
return 0;
}
int32_t WakeupEngine::NotifyHeadsetHostEvent(HeadsetHostEventType event)
{
INTELL_VOICE_LOG_INFO("enter, event:%{public}d", event);
if (event == HEADSET_HOST_OFF) {
return HandleHeadsetOff();
} else if (event == HEADSET_HOST_ON) {
return HandleHeadsetOn();
}
INTELL_VOICE_LOG_WARN("invalid event:%{public}d", event);
return 0;
}
int32_t WakeupEngine::Detach(void)
{
StateMsg msg(RELEASE);
@ -159,7 +254,7 @@ std::string WakeupEngine::GetEventValue(int32_t uuid)
return "recognition_event";
}
void WakeupEngine::StartAbility(int32_t uuid)
void WakeupEngine::StartAbility(const std::string &event)
{
AAFwk::Want want;
HistoryInfoMgr &historyInfoMgr = HistoryInfoMgr::GetInstance();
@ -170,7 +265,7 @@ void WakeupEngine::StartAbility(int32_t uuid)
want.SetElementName(bundleName, abilityName);
want.SetParam("serviceName", std::string("intell_voice"));
want.SetParam("servicePid", getpid());
want.SetParam("eventType", GetEventValue(uuid));
want.SetParam("eventType", event);
AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
}

View File

@ -19,6 +19,7 @@
#include "base_macros.h"
#include "engine_base.h"
#include "wakeup_engine_impl.h"
#include "headset_wakeup_engine_impl.h"
namespace OHOS {
namespace IntellVoiceEngine {
@ -43,11 +44,20 @@ public:
int32_t StartCapturer(int32_t channels) override;
int32_t Read(std::vector<uint8_t> &data) override;
int32_t StopCapturer() override;
int32_t NotifyHeadsetWakeEvent() override;
int32_t NotifyHeadsetHostEvent(HeadsetHostEventType event) override;
private:
static void StartAbility(int32_t uuid);
static void StartAbility(const std::string &event);
static std::string GetEventValue(int32_t uuid);
int32_t HandleHeadsetOff();
int32_t HandleHeadsetOn();
USE_ROLE(WakeupEngineImpl);
private:
OHOS::IntellVoiceUtils::UniqueProductType<HeadsetWakeupEngineImpl> headsetImpl_ =
OHOS::IntellVoiceUtils::UniqueProductType<HeadsetWakeupEngineImpl>(nullptr, nullptr);
std::mutex headsetMutex_;
};
}
}

View File

@ -14,13 +14,14 @@
*/
#include "wakeup_engine_impl.h"
#include "audio_system_manager.h"
#include "v1_2/intell_voice_engine_types.h"
#include "adapter_callback_service.h"
#include "intell_voice_log.h"
#include "history_info_mgr.h"
#include "intell_voice_util.h"
#include "intell_voice_service_manager.h"
#include "trigger_manager.h"
#include "v1_2/intell_voice_engine_types.h"
#include "engine_host_manager.h"
#define LOG_TAG "WakeupEngineImpl"
@ -346,7 +347,8 @@ int32_t WakeupEngineImpl::HandleGetParam(const StateMsg &msg, State & /* nextSta
int32_t WakeupEngineImpl::HandleInit(const StateMsg & /* msg */, State &nextState)
{
INTELL_VOICE_LOG_INFO("enter");
if (!EngineUtil::CreateAdapterInner(WAKEUP_ADAPTER_TYPE)) {
if (!EngineUtil::CreateAdapterInner(EngineHostManager::GetInstance(), WAKEUP_ADAPTER_TYPE)) {
INTELL_VOICE_LOG_ERROR("failed to create adapter");
return -1;
}
@ -460,7 +462,7 @@ int32_t WakeupEngineImpl::HandleRecognizeComplete(const StateMsg &msg, State &ne
return -1;
}
if (*result != 0) {
INTELL_VOICE_LOG_INFO("wakeup failed");
INTELL_VOICE_LOG_ERROR("wakeup failed");
StopAudioSource();
EngineUtil::Stop();
nextState = State(INITIALIZED);
@ -478,7 +480,7 @@ int32_t WakeupEngineImpl::HandleReconfirmRecognitionComplete(const StateMsg &msg
int32_t WakeupEngineImpl::HandleStartCapturer(const StateMsg &msg, State &nextState)
{
INTELL_VOICE_LOG_ERROR("enter");
INTELL_VOICE_LOG_INFO("enter");
int32_t *msgBody = reinterpret_cast<int32_t *>(msg.inMsg);
if (msgBody == nullptr) {
INTELL_VOICE_LOG_ERROR("msgBody is nullptr");
@ -509,7 +511,7 @@ int32_t WakeupEngineImpl::HandleRead(const StateMsg &msg, State & /* nextState *
int32_t WakeupEngineImpl::HandleStopCapturer(const StateMsg & /* msg */, State &nextState)
{
INTELL_VOICE_LOG_ERROR("enter");
INTELL_VOICE_LOG_INFO("enter");
StopAudioSource();
EngineUtil::Stop();
nextState = State(INITIALIZED);
@ -518,7 +520,7 @@ int32_t WakeupEngineImpl::HandleStopCapturer(const StateMsg & /* msg */, State &
int32_t WakeupEngineImpl::HandleRecognizingTimeout(const StateMsg & /* msg */, State &nextState)
{
INTELL_VOICE_LOG_ERROR("enter");
INTELL_VOICE_LOG_INFO("enter");
StopAudioSource();
EngineUtil::Stop();
nextState = State(INITIALIZED);
@ -542,8 +544,9 @@ int32_t WakeupEngineImpl::HandleGetWakeupPcm(const StateMsg &msg, State & /* nex
int32_t WakeupEngineImpl::HandleResetAdapter(const StateMsg & /* msg */, State &nextState)
{
INTELL_VOICE_LOG_ERROR("enter");
if (!EngineUtil::CreateAdapterInner(WAKEUP_ADAPTER_TYPE)) {
INTELL_VOICE_LOG_INFO("enter");
if (!EngineUtil::CreateAdapterInner(EngineHostManager::GetInstance(), WAKEUP_ADAPTER_TYPE)) {
INTELL_VOICE_LOG_ERROR("failed to create adapter");
return -1;
}
adapter_->SetCallback(callback_);
@ -561,7 +564,7 @@ int32_t WakeupEngineImpl::HandleResetAdapter(const StateMsg & /* msg */, State &
};
if (adapter_->Attach(adapterInfo) != 0) {
INTELL_VOICE_LOG_ERROR("failed to attach");
EngineUtil::ReleaseAdapterInner();
EngineUtil::ReleaseAdapterInner(EngineHostManager::GetInstance());
return -1;
}
@ -575,7 +578,7 @@ int32_t WakeupEngineImpl::HandleRelease(const StateMsg & /* msg */, State &nextS
StopAudioSource();
if (adapter_ != nullptr) {
adapter_->Detach();
ReleaseAdapterInner();
ReleaseAdapterInner(EngineHostManager::GetInstance());
}
nextState = State(IDLE);
return 0;

View File

@ -33,43 +33,6 @@ namespace IntellVoiceEngine {
using OHOS::IntellVoiceUtils::StateMsg;
using OHOS::IntellVoiceUtils::State;
enum EngineEvent {
NONE = 0,
INIT,
INIT_DONE,
START_RECOGNIZE,
STOP_RECOGNIZE,
RECOGNIZE_COMPLETE,
RECONFIRM_RECOGNITION_COMPLETE,
START_CAPTURER,
READ,
STOP_CAPTURER,
RECOGNIZING_TIMEOUT,
RECOGNIZE_COMPLETE_TIMEOUT,
READ_CAPTURER_TIMEOUT,
SET_LISTENER,
SET_PARAM,
GET_PARAM,
GET_WAKEUP_PCM,
RELEASE_ADAPTER,
RESET_ADAPTER,
RELEASE,
};
struct SetListenerMsg {
explicit SetListenerMsg(sptr<IIntelligentVoiceEngineCallback> cb) : callback(cb) {}
sptr<IIntelligentVoiceEngineCallback> callback = nullptr;
};
struct CapturerData {
std::vector<uint8_t> data;
};
struct StringParam {
explicit StringParam(const std::string &str = "") : strParam(str) {}
std::string strParam;
};
class WakeupEngineImpl : private OHOS::IntellVoiceUtils::ModuleStates, private EngineUtil,
private WakeupSourceProcess {
public:

View File

@ -103,6 +103,12 @@ bool IntellVoiceUtil::VerifyClientPermission(const std::string &permissionName)
bool IntellVoiceUtil::CheckIsSystemApp()
{
uint64_t fullTokenId = IPCSkeleton::GetCallingFullTokenID();
if (Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(IPCSkeleton::GetCallingTokenID()) ==
Security::AccessToken::TOKEN_NATIVE) {
INTELL_VOICE_LOG_INFO("calling by native");
return true;
}
if (!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId)) {
INTELL_VOICE_LOG_INFO("Not system app, permission reject tokenid: %{public}" PRIu64 "", fullTokenId);
return false;