mirror of
https://github.com/openharmony/ai_intelligent_voice_framework.git
synced 2026-06-30 20:27:58 -04:00
merge intelligent_voice_framework format fix
Signed-off-by: lvqiang214 <lvqiang1@huawei.com>
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
@@ -0,0 +1,64 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- 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.
|
||||
|
||||
Notes:
|
||||
This is project config file for OpenHarmony OSS Audit Tool.
|
||||
-->
|
||||
<!-- OSS Audit Tool (OAT) configuration guide:
|
||||
basedir: Root directory. the basedir + project path is the real source file location.
|
||||
licensefile:
|
||||
1.If the project does not have "LICENSE" in root dir, please define all the license files in this project in. OAT will check license files according to this rule.
|
||||
|
||||
tasklist(for batch mode only):
|
||||
1. task: OAT check check task. Each task will start a new thread.
|
||||
2. task name: Name of the OAT check task.
|
||||
3. task policy: Default policy for projects under this task. This field is mandatory and the specified policy must defined in policylist.
|
||||
4. task filter: Default filefilter for projects under this task. This field is mandatory and the specified filefilter must defined in filefilterlist.
|
||||
5. task project: Projects to be checked. The source root dir of the project is defined by the path field.
|
||||
|
||||
|
||||
policyList:
|
||||
1. policy: All policyitems will be merged to default rules in OAT.xml. The policy name doesn't affect OAT check process.
|
||||
2. policyitem: The fields type, name, path, desc are mandatory, and the fields rule, group, filefilter are optional. The default value is:
|
||||
<policyitem type="" name="" path="" desc="" rule="may" group="defaultGroup" filefilter="defaultPolicyFilter"/>
|
||||
3. policyitem type:
|
||||
"compatibility" is used to check license compatibility in the specified path;
|
||||
"license" is used to check source license header in the specified path;
|
||||
"copyright" is used to check source copyright header in the specified path;
|
||||
"import" is used to check source dependency in the specified path, such as import ... ,include ...
|
||||
"filetype" is used to check file type in the specified path, supported file types: archive, binary
|
||||
"filename" is used to check whether the specified file exists in the specified path(support projectroot in default OAT.xml), supported file names: LICENSE, README, README.OpenSource
|
||||
|
||||
4. policyitem name: This field is used to define the license, copyright, "*" means to match all, the "!" prefix means a failure to match this value. For example, "!GPL" means a failure to use GPL license.
|
||||
5. policyitem path: This field is used to define the source file scope to apply this policyitem. The "!" prefix means to exclude the files. For example, "!./lib/." means to exclude the files in lib dir while process this policyitem.
|
||||
6. policyitem rule and group: These two fields are used together to merge policy results. "may" policyitems in the same group means that the result will be passed if any policyitem in this group is passed.
|
||||
7. policyitem filefilter: This field is used to bind file filters, which defines filter rules.
|
||||
8. filefilter: This field is used to define filter rules. Wherein, filename is used to filter file names and filepath is used to filter file path.
|
||||
|
||||
Note: If the text contains special characters, please escape them according to the following rules:
|
||||
" == >
|
||||
& == >
|
||||
' == >
|
||||
< == >
|
||||
> == >
|
||||
-->
|
||||
<configuration>
|
||||
<oatconfig>
|
||||
<filefilterlist>
|
||||
<filefilter name="binaryFileTypePolicyFilter" desc="Filters for binary file policies" >
|
||||
<filteritem type="filepath" name="figures/.*" desc="architecture figure binary."/>
|
||||
</filefilter>
|
||||
</filefilterlist>
|
||||
</oatconfig>
|
||||
</configuration>
|
||||
Executable
+195
@@ -0,0 +1,195 @@
|
||||
# 智能语音组件
|
||||
|
||||
## 概述
|
||||
|
||||
### 功能简介
|
||||
|
||||
智能语音组件包括智能语音服务框架和智能语音驱动,主要实现了语音注册及语音唤醒相关功能。
|
||||
|
||||
**图 1** 智能语音组件架构图
|
||||
|
||||

|
||||
|
||||
智能语音服务框架支持如下功能:
|
||||
系统事件监测:开机解锁、亮灭屏等系统事件监测
|
||||
并发策略:智能语音业务并发管理
|
||||
智能语音业务:语音注册、语音唤醒等智能语音业务处理
|
||||
声音触发器:DSP模型加载、DSP算法启停、DSP事件处理
|
||||
|
||||
智能语音驱动支持如下功能:
|
||||
引擎算法:智能语音算法引擎以及事件上报
|
||||
设备驱动:DSP模型加载卸载、算法启停、事件上报以及硬件相关通路配置
|
||||
|
||||
### 基本概念
|
||||
- 语音注册:将用户说的唤醒词转换为声学模型以及声纹特征,以便后续的语音唤醒。
|
||||
- 语音唤醒:判断当前说话人是否为已注册的特定用户。
|
||||
- DSP:数字信号处理器(Digital Signal Processors),DSP芯片即指能够实现数字信号处理技术的芯片。
|
||||
|
||||
### 目录结构
|
||||
|
||||
仓目录结构如下:
|
||||
|
||||
```shell
|
||||
/foundation/ai/intelligent_voice_framework # 智能音频组件业务代码
|
||||
├── frameworks # 框架代码
|
||||
│ ├── native # 内部接口实现
|
||||
│ └── js # 外部接口实现
|
||||
├── interfaces # 接口代码
|
||||
│ ├── inner_api # 内部接口
|
||||
│ └── kits # 外部接口
|
||||
├── sa_profile # 服务配置文件
|
||||
├── services # 服务代码
|
||||
├── LICENSE # 证书文件
|
||||
├── tests # 开发者测试
|
||||
└── utils # 公共函数
|
||||
```
|
||||
|
||||
### 约束与限制
|
||||
|
||||
- 智能语音服务当前只支持一个唤醒词的注册以及唤醒。
|
||||
|
||||
|
||||
## 接口说明
|
||||
### 语音注册接口说明
|
||||
|
||||
|接口名称|接口描述|
|
||||
|---|---|
|
||||
|createEnrollIntelligentVoiceEngine(descriptor: EnrollIntelligentVoiceEngineDescriptor): EnrollIntelligentVoiceEngine|创建注册引擎。|
|
||||
|init(config: EnrollEngineConfig): EnrollIntelligentVoiceEngineCallbackInfo|初始化注册引擎。|
|
||||
|start(isLast: boolean): EnrollIntelligentVoiceEngineCallbackInfo|启动注册。|
|
||||
|stop(): void|停止注册。|
|
||||
|commit(): EnrollIntelligentVoiceEngineCallbackInfo|确认注册结果。|
|
||||
|setWakeupHapInfo(info: WakeupHapInfo): void|设置唤醒应用信息。|
|
||||
|setSensibility(sensibility: SensibilityType): void|设置灵敏度。|
|
||||
|release(): void|释放注册引擎。|
|
||||
|
||||
### 语音唤醒接口说明
|
||||
|
||||
|接口名称|接口描述|
|
||||
|---|---|
|
||||
|createWakeupIntelligentVoiceEngine(descriptor: WakeupIntelligentVoiceEngineDescriptor): WakeupIntelligentVoiceEngine|创建唤醒引擎。|
|
||||
|setWakeupHapInfo(info: WakeupHapInfo): void|设置唤醒应用信息。|
|
||||
|setSensibility(sensibility: SensibilityType): void|设置灵敏度。|
|
||||
|on(type: 'wakeupIntelligentVoiceEvent', callback: Callback<WakeupIntelligentVoiceEngineCallbackInfo>): void|订阅唤醒事件。|
|
||||
|release(): void|释放唤醒引擎。|
|
||||
|
||||
|
||||
## 开发实例
|
||||
|
||||
### 语音注册
|
||||
|
||||
语音注册流程是用户通过应用的注册界面主动发起的交互流程,主要流程如下:
|
||||
1. 用户启动注册(创建注册引擎并初始化注册引擎)后,进入注册界面。
|
||||
2. 界面提示用户说出唤醒词,用户根据提示说出相应唤醒词(启动注册),界面会让用户再次重复说出唤醒词,直到最后一次。
|
||||
3. 注册完成(确认注册结果)后,注册流程全部完成。
|
||||
示例代码如下:
|
||||
|
||||
```js
|
||||
// 引入智能音频
|
||||
import intelligentVoice from '@ohos.ai.intelligentVoice';
|
||||
|
||||
// 获取智能音频管理服务
|
||||
var manager = intellVoice.getIntelligentVoiceManager();
|
||||
if (manager == null) {
|
||||
console.error("Get IntelligentVoiceManager failed.");
|
||||
} else {
|
||||
console.info("Get IntelligentVoiceManager success.");
|
||||
return;
|
||||
}
|
||||
|
||||
// 创建注册引擎
|
||||
var engine = null;
|
||||
let engineDescriptor = {
|
||||
wakeupPhrase: '', // 设置唤醒词
|
||||
}
|
||||
await intellVoice.createEnrollIntelligentVoiceEngine(engineDescriptor).then((data) => {
|
||||
engine = data;
|
||||
console.info('Create EnrollIntelligentVoice Engine finish');
|
||||
}).catch((err) => {
|
||||
console.error('Create EnrollIntelligentVoice Engine failed, err: ' + err.message);
|
||||
});
|
||||
if (engine == null) {
|
||||
console.error('Create EnrollIntelligentVoice Engine failed');
|
||||
return;
|
||||
}
|
||||
|
||||
// 初始化注册引擎
|
||||
let config = {
|
||||
language: "zh", // 中文
|
||||
area: "CN", // 中国
|
||||
}
|
||||
engine.init(config).then((data) => {
|
||||
console.info('Init EnrollIntelligentVoice Engine finish');
|
||||
}).catch((err) => {
|
||||
console.info('Init EnrollIntelligentVoice Engine failed, err: '+ err.message);
|
||||
});
|
||||
|
||||
// 启动注册
|
||||
let isLast = true; // true: 最后一次启动,false: 非最后一次启动,实例为true
|
||||
engine.start(isLast).then((data) => {
|
||||
console.info('Start enrollment finish');
|
||||
}).catch((err) => {
|
||||
console.info('Start enrollment failed, err: '+ err.message);
|
||||
});
|
||||
|
||||
// 确认注册结果
|
||||
engine.commit().then((data) => {
|
||||
console.info('Commit enroll result finish');
|
||||
}).catch((err) => {
|
||||
console.info('Commit enroll result failed, err: '+ err.message);
|
||||
});
|
||||
|
||||
// 下发语音唤醒应用信息
|
||||
let info = {
|
||||
bundleName: "demo", // 应用的bundle name,demo只是个参考例子,具体填写由应用确定
|
||||
abilityName: "demo", // 应用的ability name,demo只是个参考例子,具体填写由应用确定
|
||||
}
|
||||
engine.setWakeupHapInfo(info).then((data) => {
|
||||
console.info('Set wakeup hap info finish');
|
||||
}).catch((err) => {
|
||||
console.info('Set wakeup hap info failed, err: '+ err.message);
|
||||
});
|
||||
|
||||
// 释放注册引擎
|
||||
engine.release().then((data) => {
|
||||
console.info('Release EnrollIntelligentVoice engine success.');
|
||||
}).catch((err) => {
|
||||
console.info('Release EnrollIntelligentVoice engine failed, err: '+ err.message);
|
||||
});
|
||||
```
|
||||
|
||||
### 语音唤醒
|
||||
|
||||
语言唤醒由智能语音组件控制,上层应用只需要调用`createWakeupIntelligentVoiceEngine`获取唤醒引擎后注册唤醒事件回调即可。
|
||||
|
||||
```js
|
||||
// 获取唤醒引擎
|
||||
var engine = null;
|
||||
let engineDescriptor = {
|
||||
needApAlgEngine: true, // 是否需要框架提供ap侧算法引擎
|
||||
wakeupPhrase: '', // 设置唤醒词
|
||||
}
|
||||
await intellVoice.createWakeupIntelligentVoiceEngine(engineDescriptor).then((data) => {
|
||||
engine = data;
|
||||
console.info('Create WakeupIntelligentVoice Engine finish');
|
||||
}).catch((err) => {
|
||||
console.error('Create WakeupIntelligentVoice Engine failed, err: ' + err.message);
|
||||
});
|
||||
if (engine == null) {
|
||||
console.error('Create WakeupIntelligentVoice Engine failed');
|
||||
return;
|
||||
}
|
||||
|
||||
// 注册事件回调
|
||||
engine.on('wakeupIntelligentVoiceEvent',(callback) => {
|
||||
console.info('wakeupIntelligentVoiceEvent CallBackInfo:')
|
||||
for (let prop in callback) {
|
||||
console.info('wakeupIntelligentVoiceEvent prop: ' + prop);
|
||||
console.info('wakeupIntelligentVoiceEvent value: ' + callback[prop]);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
## 相关仓
|
||||
|
||||
intelligent_voice_framework
|
||||
Executable
+57
@@ -0,0 +1,57 @@
|
||||
{
|
||||
"name": "@ohos/intelligent_voice_framework",
|
||||
"description": "intelligent_voice framework",
|
||||
"version": "1.0",
|
||||
"license": "Apache License 2.0",
|
||||
"publishAs": "code-segment",
|
||||
"segment": {
|
||||
"destPath": "foundation/ai/intelligent_voice_framework"
|
||||
},
|
||||
"component": {
|
||||
"name": "intelligent_voice_framework",
|
||||
"subsystem": "ai",
|
||||
"syscap": [
|
||||
"SystemCapability.AI.IntelligentVoice.Core"
|
||||
],
|
||||
"adapter_system_type": ["standard"],
|
||||
"rom": "",
|
||||
"ram": "",
|
||||
"deps": {
|
||||
"components": ["intelligent_voice_framework"],
|
||||
"third_party": []
|
||||
},
|
||||
"features": [
|
||||
],
|
||||
"build": {
|
||||
"sub_component": [
|
||||
"//foundation/ai/intelligent_voice_framework/services:intell_voice_server",
|
||||
"//foundation/ai/intelligent_voice_framework/services:intell_voice_proxy",
|
||||
"//foundation/ai/intelligent_voice_framework/services/etc:intell_voice_service.rc",
|
||||
"//foundation/ai/intelligent_voice_framework/frameworks/js:intelligentvoice",
|
||||
"//foundation/ai/intelligent_voice_framework/frameworks/js:intelligentvoice_js",
|
||||
"//foundation/ai/intelligent_voice_framework/frameworks/native:intellvoice_native",
|
||||
"//foundation/ai/intelligent_voice_framework/sa_profile:intell_voice_service_sa_profile"
|
||||
],
|
||||
"inner_kits": [
|
||||
{
|
||||
"type": "none",
|
||||
"name": "//foundation/ai/intelligent_voice_framework/frameworks/js:intelligentvoice_js",
|
||||
"header": {
|
||||
"header_files": [
|
||||
"intell_voice_manager_napi.h",
|
||||
"intell_voice_engine_napi.h",
|
||||
"enroll_intell_voice_engine_napi.h"
|
||||
],
|
||||
"header_base": [
|
||||
"//foundation/ai/intelligent_voice_framework/frameworks/js/napi/include"
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"test": [
|
||||
"//foundation/ai/intelligent_voice_framework/tests:intell_voice_unit_test",
|
||||
"//foundation/ai/intelligent_voice_framework/tests:intell_voice_fuzz_test"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
|
After Width: | Height: | Size: 54 KiB |
Executable
+73
@@ -0,0 +1,73 @@
|
||||
# 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.
|
||||
|
||||
import("//build/ohos.gni")
|
||||
import("//build/ohos/ace/ace.gni")
|
||||
|
||||
js_declaration("intelligentvoice_js") {
|
||||
part_name = "intelligent_voice_framework"
|
||||
sources = [ "//foundation/ai/intelligent_voice_framework/interfaces/kits/js/@ohos.ai.intelligentVoice.d.ts" ]
|
||||
}
|
||||
|
||||
ohos_copy("intelligentvoice_declaration") {
|
||||
sources = [ "//foundation/ai/intelligent_voice_framework/interfaces/kits/js/@ohos.ai.intelligentVoice.d.ts" ]
|
||||
outputs = [ target_out_dir + "/$target_name/" ]
|
||||
module_source_dir = target_out_dir + "/$target_name"
|
||||
module_install_name = ""
|
||||
}
|
||||
|
||||
ohos_shared_library("intelligentvoice") {
|
||||
sources = [
|
||||
"napi/engine_event_callback_napi.cpp",
|
||||
"napi/enroll_intell_voice_engine_callback_napi.cpp",
|
||||
"napi/enroll_intell_voice_engine_napi.cpp",
|
||||
"napi/intell_voice_manager_napi.cpp",
|
||||
"napi/intell_voice_napi_queue.cpp",
|
||||
"napi/intell_voice_napi_util.cpp",
|
||||
"napi/wakeup_intell_voice_engine_napi.cpp",
|
||||
]
|
||||
|
||||
include_dirs = [
|
||||
"//foundation/arkui/napi/interfaces/kits",
|
||||
"//foundation/ai/intelligent_voice_framework/interfaces/inner_api/native",
|
||||
"//foundation/ai/intelligent_voice_framework/services/intell_voice_engine",
|
||||
"//foundation/ai/intelligent_voice_framework/services/intell_voice_engine/proxy",
|
||||
"//foundation/ai/intelligent_voice_framework/utils",
|
||||
"napi",
|
||||
]
|
||||
|
||||
cflags_cc = [
|
||||
"-Wno-error=unused-parameter",
|
||||
"-DHILOG_ENABLE",
|
||||
"-DENABLE_DEBUG",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"//foundation/ai/intelligent_voice_framework/frameworks/native:intellvoice_native",
|
||||
"//foundation/ai/intelligent_voice_framework/services:intell_voice_proxy",
|
||||
"//foundation/arkui/napi:ace_napi",
|
||||
]
|
||||
|
||||
external_deps = [
|
||||
"ability_base:want",
|
||||
"c_utils:utils",
|
||||
"hiviewdfx_hilog_native:libhilog",
|
||||
"ipc:ipc_core",
|
||||
"safwk:system_ability_fwk",
|
||||
"samgr:samgr_proxy",
|
||||
]
|
||||
|
||||
relative_install_dir = "module/ai"
|
||||
subsystem_name = "ai"
|
||||
part_name = "intelligent_voice_framework"
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* 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 "engine_event_callback_napi.h"
|
||||
#include <uv.h>
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace OHOS::IntellVoiceEngine;
|
||||
#define LOG_TAG "EngineEventCallbackNapi"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceNapi {
|
||||
EngineEventCallbackNapi::EngineEventCallbackNapi(napi_env env, napi_value callback)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
env_ = env;
|
||||
if (env_ != nullptr) {
|
||||
napi_get_uv_event_loop(env_, &loop_);
|
||||
}
|
||||
callbackRef_ = make_shared<IntellVoiceRef>(env_, callback);
|
||||
}
|
||||
|
||||
EngineEventCallbackNapi::~EngineEventCallbackNapi()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
callbackRef_ = nullptr;
|
||||
}
|
||||
|
||||
void EngineEventCallbackNapi::OnEvent(const IntellVoiceEngineCallBackEvent &event)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (callbackRef_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("Failed to get engine event callback");
|
||||
return;
|
||||
}
|
||||
|
||||
EngineCallBackInfo cbInfo = {event.msgId, event.result, event.info};
|
||||
|
||||
INTELL_VOICE_LOG_INFO("OnEvent EngineCallBackInfo: msgId: %{public}d, errCode: %{public}d, context: %{public}s",
|
||||
cbInfo.msgId,
|
||||
cbInfo.errCode,
|
||||
cbInfo.context.c_str());
|
||||
|
||||
return OnEventUvCallback(cbInfo);
|
||||
}
|
||||
|
||||
void EngineEventCallbackNapi::OnEventUvCallback(EngineCallBackInfo &cbInfo)
|
||||
{
|
||||
CHECK_RETURN_VOID(loop_ != nullptr, "loop is nullptr");
|
||||
uv_work_t *work = new (nothrow) uv_work_t;
|
||||
CHECK_RETURN_VOID(work != nullptr, "Create uv work failed, no memory");
|
||||
|
||||
work->data = new EngineEventUvCallback {env_, cbInfo, callbackRef_};
|
||||
|
||||
uv_queue_work(
|
||||
loop_,
|
||||
work,
|
||||
[](uv_work_t *work) {},
|
||||
[](uv_work_t *work, int uvStatus) {
|
||||
shared_ptr<EngineEventUvCallback> uvCallback(
|
||||
static_cast<EngineEventUvCallback *>(work->data), [work](EngineEventUvCallback *data) {
|
||||
delete data;
|
||||
delete work;
|
||||
});
|
||||
CHECK_RETURN_VOID(uvCallback != nullptr, "uvCallback is nullptr");
|
||||
CHECK_RETURN_VOID(uvCallback->callback != nullptr, "uvCallback callback is nullptr");
|
||||
napi_env env = uvCallback->env_;
|
||||
INTELL_VOICE_LOG_INFO("uv_queue_work start");
|
||||
|
||||
napi_value jsCallback = uvCallback->callback->GetRefValue();
|
||||
if (jsCallback == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("get reference value fail");
|
||||
return;
|
||||
}
|
||||
|
||||
const size_t argc = 1;
|
||||
napi_value args[argc] = {nullptr};
|
||||
GetJsCallbackInfo(env, uvCallback->cbInfo, args[0]);
|
||||
if (args[0] == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to create engine event callback");
|
||||
return;
|
||||
}
|
||||
|
||||
napi_value result = nullptr;
|
||||
napi_status status = napi_call_function(env, nullptr, jsCallback, argc, args, &result);
|
||||
if (status != napi_ok) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to call engine event callback, error: %{public}d", status);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void GetJsCallbackInfo(const napi_env &env, const EngineCallBackInfo &callbackInfo, napi_value &jsObj)
|
||||
{
|
||||
napi_status status = napi_create_object(env, &jsObj);
|
||||
if (status != napi_ok || jsObj == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to create js callbackInfo, error: %{public}d", status);
|
||||
return;
|
||||
}
|
||||
|
||||
napi_set_named_property(env, jsObj, "msgId", SetValue(env, callbackInfo.msgId));
|
||||
napi_set_named_property(env, jsObj, "errCode", SetValue(env, callbackInfo.errCode));
|
||||
napi_set_named_property(env, jsObj, "context", SetValue(env, callbackInfo.context));
|
||||
}
|
||||
} // namespace IntellVoiceNapi
|
||||
} // namespace OHOS
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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 ENGINE_EVENT_CALLBACK_H
|
||||
#define ENGINE_EVENT_CALLBACK_H
|
||||
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
#include "intell_voice_napi_util.h"
|
||||
#include "i_intell_voice_engine_callback.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceNapi {
|
||||
using OHOS::IntellVoiceEngine::IIntellVoiceEngineEventCallback;
|
||||
using OHOS::IntellVoiceEngine::IntellVoiceEngineCallBackEvent;
|
||||
|
||||
struct EngineCallBackInfo {
|
||||
int32_t msgId;
|
||||
int32_t errCode;
|
||||
std::string context;
|
||||
};
|
||||
|
||||
class EngineEventCallbackNapi : public IIntellVoiceEngineEventCallback {
|
||||
public:
|
||||
explicit EngineEventCallbackNapi(napi_env env, napi_value callback);
|
||||
virtual ~EngineEventCallbackNapi();
|
||||
|
||||
void OnEvent(const IntellVoiceEngineCallBackEvent &event) override;
|
||||
|
||||
private:
|
||||
struct EngineEventUvCallback {
|
||||
napi_env env_ = nullptr;
|
||||
EngineCallBackInfo cbInfo;
|
||||
std::shared_ptr<IntellVoiceRef> callback;
|
||||
};
|
||||
|
||||
void OnEventUvCallback(EngineCallBackInfo &cbInfo);
|
||||
|
||||
napi_env env_ = nullptr;
|
||||
uv_loop_s *loop_ = nullptr;
|
||||
std::shared_ptr<IntellVoiceRef> callbackRef_ = nullptr;
|
||||
};
|
||||
|
||||
void GetJsCallbackInfo(const napi_env &env, const EngineCallBackInfo &callbackInfo, napi_value &jsObj);
|
||||
} // namespace IntellVoiceNapi
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
* 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 "enroll_intell_voice_engine_callback_napi.h"
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace OHOS::IntellVoiceEngine;
|
||||
#define LOG_TAG "EnrollIntellVoiceEngineCallbackNapi"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceNapi {
|
||||
void EnrollIntellVoiceEngineCallBackInfo::GetCallBackInfoNapiValue(const napi_env &env, napi_value &result)
|
||||
{
|
||||
napi_status status = napi_create_object(env, &result);
|
||||
if (status != napi_ok || result == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to create js callbackInfo, error: %{public}d", status);
|
||||
return;
|
||||
}
|
||||
|
||||
napi_set_named_property(env, result, "eventId", SetValue(env, eventId));
|
||||
napi_set_named_property(env, result, "errCode", SetValue(env, errCode));
|
||||
napi_set_named_property(env, result, "context", SetValue(env, context));
|
||||
}
|
||||
|
||||
EnrollIntellVoiceEngineCallbackNapi::EnrollIntellVoiceEngineCallbackNapi(const napi_env env)
|
||||
{
|
||||
env_ = env;
|
||||
contextMap_.clear();
|
||||
}
|
||||
|
||||
EnrollIntellVoiceEngineCallbackNapi::~EnrollIntellVoiceEngineCallbackNapi()
|
||||
{
|
||||
contextMap_.clear();
|
||||
}
|
||||
|
||||
void EnrollIntellVoiceEngineCallbackNapi::QueueAsyncWork(EnrollAsyncContext *context)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (contextMap_.find(context->type) == contextMap_.end()) {
|
||||
std::queue<EnrollAsyncContext *> contextQue;
|
||||
contextQue.push(context);
|
||||
contextMap_[context->type] = contextQue;
|
||||
} else {
|
||||
contextMap_.at(context->type).push(context);
|
||||
}
|
||||
}
|
||||
|
||||
void EnrollIntellVoiceEngineCallbackNapi::ClearAsyncWork(bool error, const std::string &msg)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("%{public}s", msg.c_str());
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
for (auto it = contextMap_.begin(); it != contextMap_.end(); it++) {
|
||||
auto &contextQue = it->second;
|
||||
while (!contextQue.empty()) {
|
||||
EnrollAsyncContext *context = contextQue.front();
|
||||
contextQue.pop();
|
||||
if (error) {
|
||||
INTELL_VOICE_LOG_WARN("error occured");
|
||||
}
|
||||
OnJsCallBack(context);
|
||||
}
|
||||
}
|
||||
contextMap_.clear();
|
||||
}
|
||||
|
||||
void EnrollIntellVoiceEngineCallbackNapi::OnEvent(const IntellVoiceEngineCallBackEvent &event)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("OnEvent: msgId: %{public}d, errCode: %{public}d, context: %{public}s",
|
||||
event.msgId,
|
||||
event.result,
|
||||
event.info.c_str());
|
||||
EnrollAsyncWorkType asyncType = ASYNC_WORK_INVALID;
|
||||
switch (event.msgId) {
|
||||
case HDI::IntelligentVoice::Engine::V1_0::INTELL_VOICE_ENGINE_MSG_INIT_DONE:
|
||||
asyncType = ASYNC_WORK_INIT;
|
||||
break;
|
||||
case HDI::IntelligentVoice::Engine::V1_0::INTELL_VOICE_ENGINE_MSG_ENROLL_COMPLETE:
|
||||
asyncType = ASYNC_WORK_START;
|
||||
break;
|
||||
case HDI::IntelligentVoice::Engine::V1_0::INTELL_VOICE_ENGINE_MSG_COMMIT_ENROLL_COMPLETE:
|
||||
asyncType = ASYNC_WORK_COMMIT;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (contextMap_.find(asyncType) == contextMap_.end() || contextMap_.at(asyncType).empty()) {
|
||||
INTELL_VOICE_LOG_ERROR("callback is called, But context is empty");
|
||||
return;
|
||||
}
|
||||
|
||||
EnrollAsyncContext *context = contextMap_.at(asyncType).front();
|
||||
if (context == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("context is nullptr");
|
||||
return;
|
||||
}
|
||||
contextMap_.at(asyncType).pop();
|
||||
|
||||
context->callbackInfo = {event.msgId, event.result, event.info};
|
||||
OnJsCallBack(context);
|
||||
}
|
||||
|
||||
void EnrollIntellVoiceEngineCallbackNapi::UvWorkCallBack(uv_work_t *work, int status)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
auto asyncContext = reinterpret_cast<EnrollAsyncContext *>(work->data);
|
||||
napi_value result = nullptr;
|
||||
if (asyncContext != nullptr) {
|
||||
napi_env env = asyncContext->env_;
|
||||
asyncContext->callbackInfo.GetCallBackInfoNapiValue(env, result);
|
||||
NapiAsync::CommonCallbackRoutine(asyncContext, result);
|
||||
}
|
||||
delete work;
|
||||
}
|
||||
|
||||
void EnrollIntellVoiceEngineCallbackNapi::OnJsCallBack(EnrollAsyncContext *context)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
uv_loop_s *loop = nullptr;
|
||||
napi_get_uv_event_loop(env_, &loop);
|
||||
if (loop != nullptr) {
|
||||
uv_work_t *work = new (std::nothrow) uv_work_t;
|
||||
if (work != nullptr) {
|
||||
work->data = reinterpret_cast<void *>(context);
|
||||
int ret = uv_queue_work(
|
||||
loop, work, [](uv_work_t *work) {}, UvWorkCallBack);
|
||||
if (ret != 0) {
|
||||
INTELL_VOICE_LOG_INFO("Failed to execute libuv work queue");
|
||||
delete context;
|
||||
delete work;
|
||||
}
|
||||
} else {
|
||||
delete context;
|
||||
}
|
||||
} else {
|
||||
delete context;
|
||||
}
|
||||
}
|
||||
} // namespace IntellVoiceNapi
|
||||
} // namespace OHOS
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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 ENROLL_INTELL_VOICE_ENGINE_CALLBACK_NAPI_H
|
||||
#define ENROLL_INTELL_VOICE_ENGINE_CALLBACK_NAPI_H
|
||||
|
||||
#include <queue>
|
||||
#include <map>
|
||||
#include <uv.h>
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
#include "intell_voice_napi_util.h"
|
||||
#include "i_intell_voice_engine_callback.h"
|
||||
#include "intell_voice_napi_queue.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceNapi {
|
||||
using OHOS::IntellVoiceEngine::IIntellVoiceEngineEventCallback;
|
||||
using OHOS::IntellVoiceEngine::IntellVoiceEngineCallBackEvent;
|
||||
|
||||
using ProcessWorkFunc = std::function<int32_t(AsyncContext *)>;
|
||||
|
||||
enum EnrollAsyncWorkType {
|
||||
ASYNC_WORK_INIT = 0,
|
||||
ASYNC_WORK_START,
|
||||
ASYNC_WORK_COMMIT,
|
||||
ASYNC_WORK_INVALID,
|
||||
};
|
||||
|
||||
struct EnrollIntellVoiceEngineCallBackInfo {
|
||||
int32_t eventId;
|
||||
int32_t errCode;
|
||||
std::string context;
|
||||
|
||||
void GetCallBackInfoNapiValue(const napi_env &env, napi_value &result);
|
||||
};
|
||||
|
||||
class EnrollAsyncContext : public AsyncContext {
|
||||
public:
|
||||
EnrollAsyncWorkType type = ASYNC_WORK_INVALID;
|
||||
ProcessWorkFunc processWork = nullptr;
|
||||
EnrollIntellVoiceEngineCallBackInfo callbackInfo;
|
||||
};
|
||||
|
||||
class EnrollIntellVoiceEngineCallbackNapi : public IIntellVoiceEngineEventCallback {
|
||||
public:
|
||||
explicit EnrollIntellVoiceEngineCallbackNapi(const napi_env env);
|
||||
virtual ~EnrollIntellVoiceEngineCallbackNapi();
|
||||
|
||||
void OnEvent(const IntellVoiceEngineCallBackEvent &event) override;
|
||||
void QueueAsyncWork(EnrollAsyncContext *context);
|
||||
void ClearAsyncWork(bool error, const std::string &msg);
|
||||
|
||||
private:
|
||||
void OnJsCallBack(EnrollAsyncContext *context);
|
||||
static void UvWorkCallBack(uv_work_t *work, int status);
|
||||
|
||||
private:
|
||||
std::mutex mutex_;
|
||||
napi_env env_ = nullptr;
|
||||
|
||||
std::map<EnrollAsyncWorkType, std::queue<EnrollAsyncContext *>> contextMap_;
|
||||
};
|
||||
} // namespace IntellVoiceNapi
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
+504
@@ -0,0 +1,504 @@
|
||||
/*
|
||||
* 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 "enroll_intell_voice_engine_napi.h"
|
||||
|
||||
#include "iservice_registry.h"
|
||||
#include "system_ability_definition.h"
|
||||
|
||||
#include "intell_voice_napi_util.h"
|
||||
#include "intell_voice_napi_queue.h"
|
||||
#include "intell_voice_log.h"
|
||||
#include "i_intell_voice_engine_callback.h"
|
||||
|
||||
#define LOG_TAG "EnrollEngineNapi"
|
||||
|
||||
using namespace OHOS::IntellVoice;
|
||||
using namespace std;
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceNapi {
|
||||
EnrollIntellVoiceEngineNapi::EnrollIntellVoiceEngineNapi()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter, %{public}p", this);
|
||||
}
|
||||
|
||||
EnrollIntellVoiceEngineNapi::~EnrollIntellVoiceEngineNapi()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (wrapper_ != nullptr) {
|
||||
napi_delete_reference(env_, wrapper_);
|
||||
}
|
||||
}
|
||||
|
||||
napi_value EnrollIntellVoiceEngineNapi::New(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t argc = 1;
|
||||
napi_value args[1] = {nullptr};
|
||||
|
||||
napi_value jsThis = nullptr;
|
||||
napi_value undefinedResult = nullptr;
|
||||
napi_get_undefined(env, &undefinedResult);
|
||||
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr));
|
||||
|
||||
unique_ptr<EnrollIntellVoiceEngineNapi> engineNapi = make_unique<EnrollIntellVoiceEngineNapi>();
|
||||
if (engineNapi == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("Failed to create EnrollIntellVoiceEngineNapi, No memory.");
|
||||
return undefinedResult;
|
||||
}
|
||||
engineNapi->env_ = env;
|
||||
|
||||
EnrollIntelligentVoiceEngineDescriptor descriptor = {};
|
||||
napi_value value = nullptr;
|
||||
if (napi_get_named_property(env, args[0], "wakeupPhrase", &value) == napi_ok) {
|
||||
GetValue(env, value, descriptor.wakeupPhrase);
|
||||
}
|
||||
INTELL_VOICE_LOG_INFO("EngineDescriptor wakeupPhrase: %{public}s", descriptor.wakeupPhrase.c_str());
|
||||
|
||||
engineNapi->engine_ = std::make_shared<EnrollIntellVoiceEngine>(descriptor);
|
||||
if (engineNapi->engine_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create intell voice engine faild.");
|
||||
return undefinedResult;
|
||||
}
|
||||
engineNapi->callbackNapi_ = std::make_shared<EnrollIntellVoiceEngineCallbackNapi>(env);
|
||||
if (engineNapi->callbackNapi_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create intell voice engine callback faild.");
|
||||
return undefinedResult;
|
||||
}
|
||||
engineNapi->engine_->SetCallback(engineNapi->callbackNapi_);
|
||||
|
||||
auto finalize = [](napi_env env, void *data, void *hint) {
|
||||
INTELL_VOICE_LOG_INFO("EnrollIntellVoiceEngineNapi finalize callback");
|
||||
if (data != nullptr) {
|
||||
auto obj = static_cast<EnrollIntellVoiceEngineNapi *>(data);
|
||||
delete obj;
|
||||
}
|
||||
};
|
||||
|
||||
NAPI_CALL(
|
||||
env, napi_wrap(env, jsThis, static_cast<void *>(engineNapi.get()), finalize, nullptr, &(engineNapi->wrapper_)));
|
||||
engineNapi.release();
|
||||
return jsThis;
|
||||
}
|
||||
|
||||
napi_value EnrollIntellVoiceEngineNapi::Constructor(napi_env env)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
napi_status status;
|
||||
napi_value constructor;
|
||||
napi_get_undefined(env, &constructor);
|
||||
|
||||
napi_property_descriptor properties[] = {
|
||||
DECLARE_NAPI_FUNCTION("getSupportedRegions", GetSupportedRegions),
|
||||
DECLARE_NAPI_FUNCTION("init", Init),
|
||||
DECLARE_NAPI_FUNCTION("start", Start),
|
||||
DECLARE_NAPI_FUNCTION("stop", Stop),
|
||||
DECLARE_NAPI_FUNCTION("setSensibility", SetSensibility),
|
||||
DECLARE_NAPI_FUNCTION("setWakeupHapInfo", SetWakeupHapInfo),
|
||||
DECLARE_NAPI_FUNCTION("setParameter", SetParameter),
|
||||
DECLARE_NAPI_FUNCTION("getParameter", GetParameter),
|
||||
DECLARE_NAPI_FUNCTION("release", Release),
|
||||
DECLARE_NAPI_FUNCTION("commit", Commit),
|
||||
};
|
||||
|
||||
status = napi_define_class(env,
|
||||
ENROLL_ENGINE_NAPI_CLASS_NAME,
|
||||
NAPI_AUTO_LENGTH,
|
||||
New,
|
||||
nullptr,
|
||||
sizeof(properties) / sizeof(properties[0]),
|
||||
properties,
|
||||
&constructor);
|
||||
|
||||
return constructor;
|
||||
}
|
||||
|
||||
napi_value EnrollIntellVoiceEngineNapi::GetSupportedRegions(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t cbIndex = 0;
|
||||
shared_ptr<AsyncContext> context = make_shared<AsyncContext>();
|
||||
if (context == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create AsyncContext faild, No memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
context->GetCbInfo(env, info, cbIndex, nullptr);
|
||||
|
||||
AsyncExecute execute = [](napi_env env, void *data) {
|
||||
auto asyncContext = static_cast<AsyncContext *>(data);
|
||||
asyncContext->status_ = napi_ok;
|
||||
};
|
||||
|
||||
AsyncComplete complete = [](AsyncContext *context, napi_value &result) {
|
||||
INTELL_VOICE_LOG_INFO("get supported regions complete");
|
||||
napi_create_array(context->env_, &result);
|
||||
napi_set_element(context->env_, result, 0, SetValue(context->env_, "CN"));
|
||||
};
|
||||
|
||||
return NapiAsync::AsyncWork(context, "GetSupportedRegions", execute, complete);
|
||||
}
|
||||
|
||||
napi_value EnrollIntellVoiceEngineNapi::Init(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t cbIndex = 1;
|
||||
auto context = make_shared<EnrollAsyncContext>();
|
||||
if (context == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create InitContext faild, No memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CbInfoParser parser = [env, context](size_t argc, napi_value *argv) {
|
||||
napi_value temp = nullptr;
|
||||
if (napi_get_named_property(env, argv[0], "language", &temp) == napi_ok) {
|
||||
GetValue(env, temp,
|
||||
reinterpret_cast<EnrollIntellVoiceEngineNapi *>(context->engineNapi_)->config_.language);
|
||||
}
|
||||
if (napi_get_named_property(env, argv[0], "region", &temp) == napi_ok) {
|
||||
GetValue(env, temp,
|
||||
reinterpret_cast<EnrollIntellVoiceEngineNapi *>(context->engineNapi_)->config_.region);
|
||||
}
|
||||
};
|
||||
|
||||
context->GetCbInfo(env, info, cbIndex, parser);
|
||||
|
||||
context->type = ASYNC_WORK_INIT;
|
||||
context->callbackInfo.eventId = INTELLIGENT_VOICE_EVENT_ENROLL_INIT_DONE;
|
||||
context->callbackInfo.errCode = INTELLIGENT_VOICE_INIT_FAILED;
|
||||
context->processWork = [&](AsyncContext *asyncContext) {
|
||||
EnrollIntellVoiceEngineNapi *engineNapi = reinterpret_cast<EnrollIntellVoiceEngineNapi *>(
|
||||
asyncContext->engineNapi_);
|
||||
auto engine = engineNapi->engine_;
|
||||
if (engine == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("get engine instance faild");
|
||||
return -1;
|
||||
}
|
||||
return engine->Init(engineNapi->config_);
|
||||
};
|
||||
|
||||
AsyncExecute execute = [](napi_env env, void *data) {};
|
||||
|
||||
return NapiAsync::AsyncWork(context, "Init", execute, CompleteCallback);
|
||||
}
|
||||
|
||||
napi_value EnrollIntellVoiceEngineNapi::Start(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t cbIndex = 1;
|
||||
auto context = make_shared<EnrollAsyncContext>();
|
||||
if (context == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create StartContext faild, No memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CbInfoParser parser = [env, context](size_t argc, napi_value *argv) {
|
||||
context->status_ = GetValue(env, argv[0],
|
||||
reinterpret_cast<EnrollIntellVoiceEngineNapi *>(context->engineNapi_)->isLast_);
|
||||
CHECK_STATUS_RETURN_VOID(context, "Failed to get isLast");
|
||||
};
|
||||
|
||||
context->GetCbInfo(env, info, cbIndex, parser);
|
||||
|
||||
context->type = ASYNC_WORK_START;
|
||||
context->callbackInfo.eventId = INTELLIGENT_VOICE_EVENT_ENROLL_COMPLETE;
|
||||
context->callbackInfo.errCode = INTELLIGENT_VOICE_ENROLL_FAILED;
|
||||
context->processWork = [&](AsyncContext *asyncContext) {
|
||||
EnrollIntellVoiceEngineNapi *engineNapi = reinterpret_cast<EnrollIntellVoiceEngineNapi *>(
|
||||
asyncContext->engineNapi_);
|
||||
auto engine = engineNapi->engine_;
|
||||
if (engine == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("get engine instance faild");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return engine->Start(engineNapi->isLast_);
|
||||
};
|
||||
|
||||
AsyncExecute execute = [](napi_env env, void *data) {};
|
||||
|
||||
return NapiAsync::AsyncWork(context, "Start", execute, CompleteCallback);
|
||||
}
|
||||
|
||||
napi_value EnrollIntellVoiceEngineNapi::Stop(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t cbIndex = 0;
|
||||
auto context = make_shared<AsyncContext>();
|
||||
if (context == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create AsyncContext faild, No memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
context->GetCbInfo(env, info, cbIndex, nullptr);
|
||||
|
||||
auto cb = reinterpret_cast<EnrollIntellVoiceEngineNapi *>(context->engineNapi_)->callbackNapi_;
|
||||
if (cb != nullptr) {
|
||||
cb->ClearAsyncWork(false, "the requests was aborted because user called stop");
|
||||
}
|
||||
|
||||
AsyncExecute execute = [](napi_env env, void *data) {
|
||||
auto asyncContext = static_cast<AsyncContext *>(data);
|
||||
auto engine = reinterpret_cast<EnrollIntellVoiceEngineNapi *>(asyncContext->engineNapi_)->engine_;
|
||||
if (engine == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("get engine instance faild");
|
||||
return;
|
||||
}
|
||||
asyncContext->result_ = engine->Stop();
|
||||
asyncContext->status_ = napi_ok;
|
||||
};
|
||||
|
||||
return NapiAsync::AsyncWork(context, "Stop", execute);
|
||||
}
|
||||
|
||||
napi_value EnrollIntellVoiceEngineNapi::SetParameter(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t cbIndex = 2;
|
||||
class SetParameterContext : public AsyncContext {
|
||||
public:
|
||||
string key;
|
||||
string value;
|
||||
};
|
||||
auto context = make_shared<SetParameterContext>();
|
||||
if (context == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create SetParameterContext faild, No memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CbInfoParser parser = [env, context](size_t argc, napi_value *argv) {
|
||||
context->status_ = GetValue(env, argv[0], context->key);
|
||||
CHECK_STATUS_RETURN_VOID(context, "Failed to get key");
|
||||
context->status_ = GetValue(env, argv[1], context->value);
|
||||
CHECK_STATUS_RETURN_VOID(context, "Failed to get value");
|
||||
};
|
||||
|
||||
context->GetCbInfo(env, info, cbIndex, parser);
|
||||
|
||||
AsyncExecute execute = [](napi_env env, void *data) {
|
||||
auto asyncContext = static_cast<SetParameterContext *>(data);
|
||||
auto engine = reinterpret_cast<EnrollIntellVoiceEngineNapi *>(asyncContext->engineNapi_)->engine_;
|
||||
if (engine == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("get engine instance faild");
|
||||
return;
|
||||
}
|
||||
asyncContext->result_ = engine->SetParameter(asyncContext->key, asyncContext->value);
|
||||
asyncContext->status_ = napi_ok;
|
||||
};
|
||||
|
||||
return NapiAsync::AsyncWork(context, "SetParameter", execute);
|
||||
}
|
||||
|
||||
napi_value EnrollIntellVoiceEngineNapi::GetParameter(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t cbIndex = 1;
|
||||
shared_ptr<AsyncContext> context = make_shared<AsyncContext>();
|
||||
if (context == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create AsyncContext faild, No memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
context->GetCbInfo(env, info, cbIndex, nullptr);
|
||||
|
||||
AsyncExecute execute = [](napi_env env, void *data) {
|
||||
auto asyncContext = static_cast<AsyncContext *>(data);
|
||||
asyncContext->status_ = napi_ok;
|
||||
};
|
||||
|
||||
AsyncComplete complete = [](AsyncContext *context, napi_value &result) {
|
||||
INTELL_VOICE_LOG_INFO("get parameter complete");
|
||||
result = SetValue(context->env_, "value");
|
||||
};
|
||||
return NapiAsync::AsyncWork(context, "GetParameter", execute, complete);
|
||||
}
|
||||
|
||||
napi_value EnrollIntellVoiceEngineNapi::SetSensibility(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t cbIndex = 1;
|
||||
class SetSensibilityContext : public AsyncContext {
|
||||
public:
|
||||
int32_t sensibility;
|
||||
};
|
||||
auto context = make_shared<SetSensibilityContext>();
|
||||
if (context == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create SetSensibilityContext faild, No memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CbInfoParser parser = [env, context](size_t argc, napi_value *argv) {
|
||||
context->status_ = GetValue(env, argv[0], context->sensibility);
|
||||
CHECK_STATUS_RETURN_VOID(context, "Failed to get sensibility");
|
||||
};
|
||||
|
||||
context->GetCbInfo(env, info, cbIndex, parser);
|
||||
|
||||
AsyncExecute execute = [](napi_env env, void *data) {
|
||||
auto asyncContext = static_cast<SetSensibilityContext *>(data);
|
||||
auto engine = reinterpret_cast<EnrollIntellVoiceEngineNapi *>(asyncContext->engineNapi_)->engine_;
|
||||
if (engine == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("get engine instance faild");
|
||||
return;
|
||||
}
|
||||
asyncContext->result_ = engine->SetSensibility(asyncContext->sensibility);
|
||||
asyncContext->status_ = napi_ok;
|
||||
};
|
||||
|
||||
return NapiAsync::AsyncWork(context, "SetSensibility", execute);
|
||||
}
|
||||
|
||||
napi_value EnrollIntellVoiceEngineNapi::SetWakeupHapInfo(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t cbIndex = 1;
|
||||
class SetWakeupHapContext : public AsyncContext {
|
||||
public:
|
||||
WakeupHapInfo hapInfo;
|
||||
};
|
||||
auto context = make_shared<SetWakeupHapContext>();
|
||||
if (context == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create SetWakeupHapContext faild, No memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CbInfoParser parser = [env, context](size_t argc, napi_value *argv) {
|
||||
napi_value temp = nullptr;
|
||||
if (napi_get_named_property(env, argv[0], "bundleName", &temp) == napi_ok) {
|
||||
GetValue(env, temp, context->hapInfo.bundleName);
|
||||
}
|
||||
if (napi_get_named_property(env, argv[0], "abilityName", &temp) == napi_ok) {
|
||||
GetValue(env, temp, context->hapInfo.abilityName);
|
||||
}
|
||||
};
|
||||
|
||||
context->GetCbInfo(env, info, cbIndex, parser);
|
||||
|
||||
AsyncExecute execute = [](napi_env env, void *data) {
|
||||
auto asyncContext = static_cast<SetWakeupHapContext *>(data);
|
||||
auto engine = reinterpret_cast<EnrollIntellVoiceEngineNapi *>(asyncContext->engineNapi_)->engine_;
|
||||
if (engine == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("get engine instance faild");
|
||||
return;
|
||||
}
|
||||
asyncContext->result_ = engine->SetWakeupHapInfo(asyncContext->hapInfo);
|
||||
asyncContext->status_ = napi_ok;
|
||||
};
|
||||
|
||||
return NapiAsync::AsyncWork(context, "SetWakeupHapInfo", execute);
|
||||
}
|
||||
|
||||
napi_value EnrollIntellVoiceEngineNapi::Commit(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t cbIndex = 0;
|
||||
auto context = make_shared<EnrollAsyncContext>();
|
||||
if (context == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create IntellVoiceContext faild, No memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
context->GetCbInfo(env, info, cbIndex, nullptr);
|
||||
|
||||
context->type = ASYNC_WORK_COMMIT;
|
||||
context->callbackInfo.eventId = INTELLIGENT_VOICE_EVENT_COMMIT_ENROLL_COMPLETE;
|
||||
context->callbackInfo.errCode = INTELLIGENT_VOICE_COMMIT_ENROLL_FAILED;
|
||||
context->processWork = [](AsyncContext *asyncContext) {
|
||||
auto engine = reinterpret_cast<EnrollIntellVoiceEngineNapi *>(asyncContext->engineNapi_)->engine_;
|
||||
if (engine == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("get engine instance faild");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return engine->Commit();
|
||||
};
|
||||
|
||||
AsyncExecute execute = [](napi_env env, void *data) {};
|
||||
|
||||
return NapiAsync::AsyncWork(context, "Commit", execute, CompleteCallback);
|
||||
}
|
||||
|
||||
napi_value EnrollIntellVoiceEngineNapi::Release(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t cbIndex = 0;
|
||||
shared_ptr<AsyncContext> context = make_shared<AsyncContext>();
|
||||
if (context == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create AsyncContext faild, No memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
context->GetCbInfo(env, info, cbIndex, nullptr);
|
||||
|
||||
AsyncExecute execute = [](napi_env env, void *data) {
|
||||
auto asyncContext = static_cast<AsyncContext *>(data);
|
||||
auto cb = reinterpret_cast<EnrollIntellVoiceEngineNapi *>(asyncContext->engineNapi_)->callbackNapi_;
|
||||
if (cb != nullptr) {
|
||||
cb->ClearAsyncWork(false, "the requests was aborted because user called release");
|
||||
}
|
||||
auto engine = reinterpret_cast<EnrollIntellVoiceEngineNapi *>(asyncContext->engineNapi_)->engine_;
|
||||
if (engine == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("get engine instance faild");
|
||||
return;
|
||||
}
|
||||
asyncContext->result_ = engine->Release();
|
||||
asyncContext->status_ = napi_ok;
|
||||
reinterpret_cast<EnrollIntellVoiceEngineNapi *>(asyncContext->engineNapi_)->engine_ = nullptr;
|
||||
reinterpret_cast<EnrollIntellVoiceEngineNapi *>(asyncContext->engineNapi_)->callbackNapi_ = nullptr;
|
||||
};
|
||||
|
||||
AsyncComplete complete = [](AsyncContext *context, napi_value &result) {
|
||||
INTELL_VOICE_LOG_INFO("release engine complete");
|
||||
};
|
||||
return NapiAsync::AsyncWork(context, "Release", execute, complete);
|
||||
}
|
||||
|
||||
void EnrollIntellVoiceEngineNapi::CompleteCallback(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
CHECK_RETURN_VOID(data != nullptr, "async complete callback data is null");
|
||||
napi_value result = nullptr;
|
||||
napi_get_undefined(env, &result);
|
||||
|
||||
auto asyncContext = static_cast<EnrollAsyncContext *>(data);
|
||||
if ((status != napi_ok) && (asyncContext->status_ == napi_ok)) {
|
||||
asyncContext->status_ = status;
|
||||
NapiAsync::CommonCallbackRoutine(asyncContext, result);
|
||||
return;
|
||||
}
|
||||
|
||||
if (asyncContext->processWork == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("process work is nullptr");
|
||||
NapiAsync::CommonCallbackRoutine(asyncContext, result);
|
||||
return;
|
||||
}
|
||||
|
||||
auto cb = reinterpret_cast<EnrollIntellVoiceEngineNapi *>(asyncContext->engineNapi_)->callbackNapi_;
|
||||
if (cb == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("get callback napi faild");
|
||||
NapiAsync::CommonCallbackRoutine(asyncContext, result);
|
||||
return;
|
||||
}
|
||||
|
||||
cb->QueueAsyncWork(asyncContext);
|
||||
if (asyncContext->processWork(asyncContext) != 0) {
|
||||
INTELL_VOICE_LOG_ERROR("process work failed");
|
||||
cb->ClearAsyncWork(true, "the request was aborted because intelligent voice processWork error");
|
||||
}
|
||||
}
|
||||
} // namespace IntellVoiceNapi
|
||||
} // namespace OHOS
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* 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 ENROLL_INTELL_VOICE_ENGINE_NAPI_H
|
||||
#define ENROLL_INTELL_VOICE_ENGINE_NAPI_H
|
||||
|
||||
#include <map>
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
#include "enroll_intell_voice_engine.h"
|
||||
#include "enroll_intell_voice_engine_callback_napi.h"
|
||||
|
||||
#define ENROLL_ENGINE_NAPI_CLASS_NAME "EnrollIntelligentVoiceEngine"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceNapi {
|
||||
using OHOS::IntellVoice::EnrollIntellVoiceEngine;
|
||||
using OHOS::IntellVoice::EngineConfig;
|
||||
|
||||
class EnrollIntellVoiceEngineNapi {
|
||||
public:
|
||||
EnrollIntellVoiceEngineNapi();
|
||||
~EnrollIntellVoiceEngineNapi();
|
||||
|
||||
static napi_value Constructor(napi_env env);
|
||||
|
||||
private:
|
||||
static napi_value New(napi_env env, napi_callback_info info);
|
||||
|
||||
static napi_value GetSupportedRegions(napi_env env, napi_callback_info info);
|
||||
static napi_value Init(napi_env env, napi_callback_info info);
|
||||
static napi_value Start(napi_env env, napi_callback_info info);
|
||||
static napi_value Stop(napi_env env, napi_callback_info info);
|
||||
static napi_value Commit(napi_env env, napi_callback_info info);
|
||||
static napi_value SetSensibility(napi_env env, napi_callback_info info);
|
||||
static napi_value SetWakeupHapInfo(napi_env env, napi_callback_info info);
|
||||
static napi_value SetParameter(napi_env env, napi_callback_info info);
|
||||
static napi_value GetParameter(napi_env env, napi_callback_info info);
|
||||
static napi_value Release(napi_env env, napi_callback_info info);
|
||||
|
||||
static void CompleteCallback(napi_env env, napi_status status, void *data);
|
||||
|
||||
std::shared_ptr<EnrollIntellVoiceEngine> engine_ = nullptr;
|
||||
std::shared_ptr<EnrollIntellVoiceEngineCallbackNapi> callbackNapi_ = nullptr;
|
||||
EngineConfig config_;
|
||||
bool isLast_ = false;
|
||||
|
||||
private:
|
||||
napi_env env_ = nullptr;
|
||||
napi_ref wrapper_ = nullptr;
|
||||
};
|
||||
} // namespace IntellVoiceNapi
|
||||
} // namespace OHOS
|
||||
|
||||
#endif
|
||||
+420
@@ -0,0 +1,420 @@
|
||||
/*
|
||||
* 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_manager_napi.h"
|
||||
#include "intell_voice_log.h"
|
||||
#include "intell_voice_napi_queue.h"
|
||||
#include "intell_voice_napi_util.h"
|
||||
#include "enroll_intell_voice_engine_napi.h"
|
||||
#include "wakeup_intell_voice_engine_napi.h"
|
||||
#include "intell_voice_info.h"
|
||||
|
||||
#define LOG_TAG "IntellVoiceManagerNapi"
|
||||
|
||||
using namespace std;
|
||||
using namespace OHOS::IntellVoice;
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceNapi {
|
||||
static const std::string INTELLIGENT_VOICE_MANAGER_NAPI_CLASS_NAME = "IntelligentVoiceManager";
|
||||
|
||||
static __thread napi_ref g_managerConstructor = nullptr;
|
||||
napi_ref IntellVoiceManagerNapi::serviceChangeTypeRef_ = nullptr;
|
||||
napi_ref IntellVoiceManagerNapi::engineTypeRef_ = nullptr;
|
||||
napi_ref IntellVoiceManagerNapi::sensibilityTypeRef_ = nullptr;
|
||||
napi_ref IntellVoiceManagerNapi::enrollEventTypeRef_ = nullptr;
|
||||
napi_ref IntellVoiceManagerNapi::wakeupEventTypeRef_ = nullptr;
|
||||
napi_ref IntellVoiceManagerNapi::errorCodeRef_ = nullptr;
|
||||
|
||||
static const std::map<std::string, OHOS::IntellVoice::ServiceChangeType> SERVICE_CHANGE_TYPE_MAP = {
|
||||
{"SERVICE_UNAVAILABLE", SERVICE_UNAVAILABLE},
|
||||
};
|
||||
|
||||
static const std::map<std::string, OHOS::IntellVoice::IntelligentVoiceEngineType> ENGINE_TYPE_MAP = {
|
||||
{"ENROLL_ENGINE_TYPE", ENROLL_ENGINE_TYPE},
|
||||
{"WAKEUP_ENGINE_TYPE", WAKEUP_ENGINE_TYPE},
|
||||
{"UPDATE_ENGINE_TYPE", UPDATE_ENGINE_TYPE},
|
||||
};
|
||||
|
||||
static const std::map<std::string, OHOS::IntellVoice::SensibilityType> SENSIBILITY_TYPE_MAP = {
|
||||
{"LOW_SENSIBILITY", LOW_SENSIBILITY},
|
||||
{"MIDDLE_SENSIBILITY", MIDDLE_SENSIBILITY},
|
||||
{"HIGH_SENSIBILITY", HIGH_SENSIBILITY},
|
||||
};
|
||||
|
||||
static const std::map<std::string, OHOS::IntellVoice::EnrollIntelligentVoiceEventType> ENROLL_EVENT_TYPE_MAP = {
|
||||
{"INTELLIGENT_VOICE_EVENT_ENROLL_NONE", INTELLIGENT_VOICE_EVENT_ENROLL_NONE},
|
||||
{"INTELLIGENT_VOICE_EVENT_ENROLL_INIT_DONE", INTELLIGENT_VOICE_EVENT_ENROLL_INIT_DONE},
|
||||
{"INTELLIGENT_VOICE_EVENT_ENROLL_COMPLETE", INTELLIGENT_VOICE_EVENT_ENROLL_COMPLETE},
|
||||
{"INTELLIGENT_VOICE_EVENT_COMMIT_ENROLL_COMPLETE", INTELLIGENT_VOICE_EVENT_COMMIT_ENROLL_COMPLETE},
|
||||
};
|
||||
|
||||
static const std::map<std::string, OHOS::IntellVoice::WakeupIntelligentVoiceEventType> WAKEUP_EVENT_TYPE_MAP = {
|
||||
{"INTELLIGENT_VOICE_EVENT_WAKEUP_NONE", INTELLIGENT_VOICE_EVENT_WAKEUP_NONE},
|
||||
{"INTELLIGENT_VOICE_EVENT_RECOGNIZE_COMPLETE", INTELLIGENT_VOICE_EVENT_RECOGNIZE_COMPLETE},
|
||||
};
|
||||
|
||||
static const std::map<std::string, OHOS::IntellVoice::IntelligentVoiceErrorCode> ERROR_CODE_MAP = {
|
||||
{"INTELLIGENT_VOICE_SUCCESS", INTELLIGENT_VOICE_SUCCESS},
|
||||
{"INTELLIGENT_VOICE_NO_MEMORY", INTELLIGENT_VOICE_NO_MEMORY},
|
||||
{"INTELLIGENT_VOICE_INVALID_PARAM", INTELLIGENT_VOICE_INVALID_PARAM},
|
||||
{"INTELLIGENT_VOICE_INIT_FAILED", INTELLIGENT_VOICE_INIT_FAILED},
|
||||
{"INTELLIGENT_VOICE_ENROLL_FAILED", INTELLIGENT_VOICE_ENROLL_FAILED},
|
||||
{"INTELLIGENT_VOICE_COMMIT_ENROLL_FAILED", INTELLIGENT_VOICE_COMMIT_ENROLL_FAILED},
|
||||
{"INTELLIGENT_VOICE_RECOGNIZE_FAILED", INTELLIGENT_VOICE_RECOGNIZE_FAILED},
|
||||
};
|
||||
|
||||
IntellVoiceManagerNapi::IntellVoiceManagerNapi()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
}
|
||||
|
||||
IntellVoiceManagerNapi::~IntellVoiceManagerNapi()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (wrapper_ != nullptr) {
|
||||
napi_delete_reference(env_, wrapper_);
|
||||
}
|
||||
}
|
||||
|
||||
void IntellVoiceManagerNapi::Destructor(napi_env env, void *nativeObject, void *finalize_hint)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (nativeObject != nullptr) {
|
||||
auto obj = static_cast<IntellVoiceManagerNapi *>(nativeObject);
|
||||
delete obj;
|
||||
}
|
||||
}
|
||||
|
||||
napi_value IntellVoiceManagerNapi::Constructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
napi_status status;
|
||||
size_t argCount = 0;
|
||||
napi_value jsThis = nullptr;
|
||||
napi_value undefinedResult = nullptr;
|
||||
napi_get_undefined(env, &undefinedResult);
|
||||
|
||||
status = napi_get_cb_info(env, info, &argCount, nullptr, &jsThis, nullptr);
|
||||
if (status != napi_ok) {
|
||||
INTELL_VOICE_LOG_ERROR("invalid number of arguments");
|
||||
return undefinedResult;
|
||||
}
|
||||
|
||||
unique_ptr<IntellVoiceManagerNapi> managerNapi = make_unique<IntellVoiceManagerNapi>();
|
||||
if (managerNapi == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("no memory.");
|
||||
return undefinedResult;
|
||||
}
|
||||
managerNapi->env_ = env;
|
||||
managerNapi->manager_ = IntellVoiceManager::GetInstance();
|
||||
if (managerNapi->manager_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create native manager faild.");
|
||||
return undefinedResult;
|
||||
}
|
||||
|
||||
status = napi_wrap(env,
|
||||
jsThis,
|
||||
static_cast<void *>(managerNapi.get()),
|
||||
IntellVoiceManagerNapi::Destructor,
|
||||
nullptr,
|
||||
&(managerNapi->wrapper_));
|
||||
if (status == napi_ok) {
|
||||
managerNapi.release();
|
||||
return jsThis;
|
||||
}
|
||||
|
||||
INTELL_VOICE_LOG_ERROR("failed to construct intell voice manager napi");
|
||||
return undefinedResult;
|
||||
}
|
||||
|
||||
napi_value IntellVoiceManagerNapi::Export(napi_env env, napi_value exports)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
napi_status status;
|
||||
napi_value constructor;
|
||||
napi_value result = nullptr;
|
||||
const int32_t refCount = 1;
|
||||
napi_get_undefined(env, &result);
|
||||
|
||||
napi_property_descriptor properties[] = {
|
||||
DECLARE_NAPI_FUNCTION("getCapabilityInfo", GetCapabilityInfo),
|
||||
DECLARE_NAPI_FUNCTION("on", On),
|
||||
DECLARE_NAPI_FUNCTION("off", Off),
|
||||
};
|
||||
|
||||
napi_property_descriptor staticProperties[] = {
|
||||
DECLARE_NAPI_STATIC_FUNCTION("getIntelligentVoiceManager", GetIntelligentVoiceManager),
|
||||
DECLARE_NAPI_PROPERTY("ServiceChangeType", CreatePropertyBase(env, SERVICE_CHANGE_TYPE_MAP,
|
||||
serviceChangeTypeRef_)),
|
||||
DECLARE_NAPI_PROPERTY("IntelligentVoiceEngineType", CreatePropertyBase(env, ENGINE_TYPE_MAP,
|
||||
engineTypeRef_)),
|
||||
DECLARE_NAPI_PROPERTY("SensibilityType", CreatePropertyBase(env, SENSIBILITY_TYPE_MAP,
|
||||
sensibilityTypeRef_)),
|
||||
DECLARE_NAPI_PROPERTY("EnrollIntelligentVoiceEventType", CreatePropertyBase(env, ENROLL_EVENT_TYPE_MAP,
|
||||
enrollEventTypeRef_)),
|
||||
DECLARE_NAPI_PROPERTY("WakeupIntelligentVoiceEventType", CreatePropertyBase(env, WAKEUP_EVENT_TYPE_MAP,
|
||||
wakeupEventTypeRef_)),
|
||||
DECLARE_NAPI_PROPERTY("IntelligentVoiceErrorCode", CreatePropertyBase(env, ERROR_CODE_MAP, errorCodeRef_)),
|
||||
};
|
||||
|
||||
status = napi_define_class(env,
|
||||
INTELLIGENT_VOICE_MANAGER_NAPI_CLASS_NAME.c_str(),
|
||||
NAPI_AUTO_LENGTH,
|
||||
Constructor,
|
||||
nullptr,
|
||||
sizeof(properties) / sizeof(properties[0]),
|
||||
properties,
|
||||
&constructor);
|
||||
if (status != napi_ok) {
|
||||
return result;
|
||||
}
|
||||
|
||||
status = napi_create_reference(env, constructor, refCount, &g_managerConstructor);
|
||||
if (status == napi_ok) {
|
||||
status = napi_set_named_property(env, exports, INTELLIGENT_VOICE_MANAGER_NAPI_CLASS_NAME.c_str(), constructor);
|
||||
if (status == napi_ok) {
|
||||
status = napi_define_properties(
|
||||
env, exports, sizeof(staticProperties) / sizeof(staticProperties[0]), staticProperties);
|
||||
if (status == napi_ok) {
|
||||
return exports;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
INTELL_VOICE_LOG_ERROR("failed to export intelligent voice manager napi");
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value IntellVoiceManagerNapi::GetCapabilityInfo(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
napi_value result = nullptr;
|
||||
napi_create_array(env, &result);
|
||||
napi_set_element(env, result, 0, SetValue(env, ENROLL_ENGINE_TYPE));
|
||||
napi_set_element(env, result, 1, SetValue(env, WAKEUP_ENGINE_TYPE));
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value IntellVoiceManagerNapi::On(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value IntellVoiceManagerNapi::Off(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value IntellVoiceManagerNapi::GetIntelligentVoiceManager(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
|
||||
napi_status status;
|
||||
size_t argCount = 0;
|
||||
|
||||
status = napi_get_cb_info(env, info, &argCount, nullptr, nullptr, nullptr);
|
||||
if (status != napi_ok || argCount != 0) {
|
||||
INTELL_VOICE_LOG_ERROR("Invalid arguments");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return IntellVoiceManagerNapi::GetIntelligentVoiceManagerWrapper(env);
|
||||
}
|
||||
|
||||
napi_value IntellVoiceManagerNapi::GetIntelligentVoiceManagerWrapper(napi_env env)
|
||||
{
|
||||
napi_status status;
|
||||
napi_value result = nullptr;
|
||||
napi_value constructor;
|
||||
|
||||
status = napi_get_reference_value(env, g_managerConstructor, &constructor);
|
||||
if (status == napi_ok) {
|
||||
status = napi_new_instance(env, constructor, 0, nullptr, &result);
|
||||
if (status == napi_ok) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
INTELL_VOICE_LOG_ERROR("failed to get intell voice manager wrapper");
|
||||
napi_get_undefined(env, &result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value IntellVoiceManagerNapi::CreateEnrollIntelligentVoiceEngine(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t cbIndex = 1;
|
||||
class CreateContext : public AsyncContext {
|
||||
public:
|
||||
napi_value object = nullptr;
|
||||
napi_ref ref = nullptr;
|
||||
};
|
||||
auto context = make_shared<CreateContext>();
|
||||
if (context == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create CreateContext faild, No memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CbInfoParser parser = [env, context](size_t argc, napi_value *argv) {
|
||||
context->status_ =
|
||||
napi_new_instance(env, EnrollIntellVoiceEngineNapi::Constructor(env), argc, argv, &(context->object));
|
||||
CHECK_STATUS_RETURN_VOID(context, "new napi instance failed");
|
||||
context->status_ = napi_create_reference(env, context->object, 1, &(context->ref));
|
||||
CHECK_STATUS_RETURN_VOID(context, "create ref failed");
|
||||
};
|
||||
context->GetCbInfo(env, info, cbIndex, parser);
|
||||
|
||||
AsyncExecute execute = [](napi_env env, void *data) {
|
||||
auto asyncContext = static_cast<CreateContext *>(data);
|
||||
asyncContext->status_ = napi_ok;
|
||||
};
|
||||
|
||||
AsyncComplete complete = [](AsyncContext *context, napi_value &result) {
|
||||
auto createContext = static_cast<CreateContext *>(context);
|
||||
napi_get_reference_value(createContext->env_, createContext->ref, &result);
|
||||
};
|
||||
|
||||
return NapiAsync::AsyncWork(context, "CreateEnrollIntelligentVoiceEngine", execute, complete);
|
||||
}
|
||||
|
||||
napi_value IntellVoiceManagerNapi::CreateWakeupIntelligentVoiceEngine(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t cbIndex = 1;
|
||||
class CreateContext : public AsyncContext {
|
||||
public:
|
||||
napi_value object = nullptr;
|
||||
napi_ref ref = nullptr;
|
||||
};
|
||||
auto context = make_shared<CreateContext>();
|
||||
if (context == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create CreateContext faild, No memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CbInfoParser parser = [env, context](size_t argc, napi_value *argv) {
|
||||
context->status_ =
|
||||
napi_new_instance(env, WakeupIntellVoiceEngineNapi::Constructor(env), argc, argv, &(context->object));
|
||||
CHECK_STATUS_RETURN_VOID(context, "new napi instance failed");
|
||||
context->status_ = napi_create_reference(env, context->object, 1, &(context->ref));
|
||||
CHECK_STATUS_RETURN_VOID(context, "create ref failed");
|
||||
};
|
||||
context->GetCbInfo(env, info, cbIndex, parser);
|
||||
|
||||
AsyncExecute execute = [](napi_env env, void *data) {
|
||||
auto asyncContext = static_cast<CreateContext *>(data);
|
||||
asyncContext->status_ = napi_ok;
|
||||
};
|
||||
|
||||
AsyncComplete complete = [](AsyncContext *context, napi_value &result) {
|
||||
auto createContext = static_cast<CreateContext *>(context);
|
||||
napi_get_reference_value(createContext->env_, createContext->ref, &result);
|
||||
};
|
||||
|
||||
return NapiAsync::AsyncWork(context, "CreateWakeupIntelligentVoiceEngine", execute, complete);
|
||||
}
|
||||
|
||||
template<typename T> napi_value IntellVoiceManagerNapi::CreatePropertyBase(napi_env env, T &propertyMap, napi_ref ref)
|
||||
{
|
||||
napi_value result = nullptr;
|
||||
napi_status status;
|
||||
string propName;
|
||||
int32_t enumValue;
|
||||
napi_value enumNapiValue;
|
||||
|
||||
status = napi_create_object(env, &result);
|
||||
if (status != napi_ok) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to create object");
|
||||
napi_get_undefined(env, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
for (auto &iter : propertyMap) {
|
||||
propName = iter.first;
|
||||
enumValue = iter.second;
|
||||
|
||||
enumNapiValue = SetValue(env, enumValue);
|
||||
status = napi_set_named_property(env, result, propName.c_str(), enumNapiValue);
|
||||
if (status != napi_ok) {
|
||||
break;
|
||||
}
|
||||
propName.clear();
|
||||
}
|
||||
|
||||
if (status != napi_ok) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to set base property");
|
||||
napi_get_undefined(env, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
status = napi_create_reference(env, result, 1, &ref);
|
||||
if (status != napi_ok) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to create base property");
|
||||
napi_get_undefined(env, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
INTELL_VOICE_LOG_INFO("create base property success");
|
||||
return result;
|
||||
}
|
||||
|
||||
EXTERN_C_START
|
||||
static napi_value Export(napi_env env, napi_value exports)
|
||||
{
|
||||
// Export static methods and enum
|
||||
napi_status status;
|
||||
napi_property_descriptor staticProperties[] = {
|
||||
DECLARE_NAPI_STATIC_FUNCTION(
|
||||
"createEnrollIntelligentVoiceEngine", IntellVoiceManagerNapi::CreateEnrollIntelligentVoiceEngine),
|
||||
DECLARE_NAPI_STATIC_FUNCTION(
|
||||
"createWakeupIntelligentVoiceEngine", IntellVoiceManagerNapi::CreateWakeupIntelligentVoiceEngine),
|
||||
};
|
||||
status =
|
||||
napi_define_properties(env, exports, sizeof(staticProperties) / sizeof(staticProperties[0]), staticProperties);
|
||||
INTELL_VOICE_LOG_INFO("init static properties, status: %{public}d", status);
|
||||
|
||||
// Export Class
|
||||
IntellVoiceManagerNapi::Export(env, exports);
|
||||
|
||||
status = napi_set_named_property(
|
||||
env, exports, ENROLL_ENGINE_NAPI_CLASS_NAME, EnrollIntellVoiceEngineNapi::Constructor(env));
|
||||
INTELL_VOICE_LOG_INFO("init %{public}s, status: %{public}d", ENROLL_ENGINE_NAPI_CLASS_NAME, status);
|
||||
|
||||
status = napi_set_named_property(
|
||||
env, exports, WAKEUP_ENGINE_NAPI_CLASS_NAME, WakeupIntellVoiceEngineNapi::Constructor(env));
|
||||
INTELL_VOICE_LOG_INFO("init %{public}s, status: %{public}d", WAKEUP_ENGINE_NAPI_CLASS_NAME, status);
|
||||
|
||||
return exports;
|
||||
}
|
||||
EXTERN_C_END
|
||||
|
||||
static napi_module g_module = {
|
||||
.nm_version = 1,
|
||||
.nm_flags = 0,
|
||||
.nm_filename = nullptr,
|
||||
.nm_register_func = Export,
|
||||
.nm_modname = "ai.intelligentVoice",
|
||||
.nm_priv = ((void *)0),
|
||||
.reserved = {0},
|
||||
};
|
||||
|
||||
extern "C" __attribute__((constructor)) void RegisterModule(void)
|
||||
{
|
||||
napi_module_register(&g_module);
|
||||
}
|
||||
} // namespace IntellVoiceNapi
|
||||
} // namespace OHOS
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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_MANAGER_NAPI_H
|
||||
#define INTELL_VOICE_MANAGER_NAPI_H
|
||||
|
||||
#include <functional>
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
|
||||
#include "intell_voice_manager.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceNapi {
|
||||
using OHOS::IntellVoice::IntellVoiceManager;
|
||||
|
||||
class IntellVoiceManagerNapi {
|
||||
public:
|
||||
IntellVoiceManagerNapi();
|
||||
~IntellVoiceManagerNapi();
|
||||
|
||||
static napi_value Export(napi_env env, napi_value exports);
|
||||
static napi_value CreateEnrollIntelligentVoiceEngine(napi_env env, napi_callback_info info);
|
||||
static napi_value CreateWakeupIntelligentVoiceEngine(napi_env env, napi_callback_info info);
|
||||
|
||||
private:
|
||||
static void Destructor(napi_env env, void *nativeObject, void *finalize_hint);
|
||||
static napi_value Constructor(napi_env env, napi_callback_info info);
|
||||
|
||||
static napi_value GetCapabilityInfo(napi_env env, napi_callback_info info);
|
||||
static napi_value On(napi_env env, napi_callback_info info);
|
||||
static napi_value Off(napi_env env, napi_callback_info info);
|
||||
|
||||
static napi_value GetIntelligentVoiceManager(napi_env env, napi_callback_info info);
|
||||
static napi_value GetIntelligentVoiceManagerWrapper(napi_env env);
|
||||
template<typename T> static napi_value CreatePropertyBase(napi_env env, T &propertyMap, napi_ref ref);
|
||||
|
||||
napi_env env_ = nullptr;
|
||||
napi_ref wrapper_ = nullptr;
|
||||
IntellVoiceManager *manager_ = nullptr;
|
||||
static napi_ref serviceChangeTypeRef_;
|
||||
static napi_ref engineTypeRef_;
|
||||
static napi_ref sensibilityTypeRef_;
|
||||
static napi_ref enrollEventTypeRef_;
|
||||
static napi_ref wakeupEventTypeRef_;
|
||||
static napi_ref errorCodeRef_;
|
||||
};
|
||||
} // namespace IntellVoiceNapi
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
+193
@@ -0,0 +1,193 @@
|
||||
/*
|
||||
* 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_napi_queue.h"
|
||||
#include "intell_voice_napi_util.h"
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
#define LOG_TAG "IntellVoiceNapiQueue"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceNapi {
|
||||
AsyncComplete NapiAsync::complete_ = nullptr;
|
||||
|
||||
AsyncContext::AsyncContext()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
}
|
||||
AsyncContext::~AsyncContext()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (env_ != nullptr) {
|
||||
if (work_ != nullptr) {
|
||||
napi_delete_async_work(env_, work_);
|
||||
}
|
||||
if (callbackRef_ != nullptr) {
|
||||
napi_delete_reference(env_, callbackRef_);
|
||||
}
|
||||
env_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncContext::GetCbInfo(napi_env env, napi_callback_info info, size_t callBackIndex, CbInfoParser parser)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
napi_value jsThis = nullptr;
|
||||
const int32_t refCount = 1;
|
||||
size_t argc = ARGC_MAX;
|
||||
napi_value args[ARGC_MAX] = {nullptr};
|
||||
|
||||
env_ = env;
|
||||
status_ = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
|
||||
CHECK_STATUS_RETURN_VOID(this, "Failed to get cb info");
|
||||
|
||||
status_ = napi_unwrap(env, jsThis, &engineNapi_);
|
||||
CHECK_STATUS_RETURN_VOID(this, "Failed to get engine napi instance");
|
||||
|
||||
if (argc > callBackIndex) {
|
||||
napi_valuetype valueType = napi_undefined;
|
||||
napi_typeof(env, args[callBackIndex], &valueType);
|
||||
if (valueType == napi_function) {
|
||||
status_ = napi_create_reference(env, args[callBackIndex], refCount, &callbackRef_);
|
||||
CHECK_STATUS_RETURN_VOID(this, "Failed to create callback reference");
|
||||
} else {
|
||||
INTELL_VOICE_LOG_ERROR("Failed to get asyncCallback, type mismatch");
|
||||
}
|
||||
}
|
||||
|
||||
if (parser) {
|
||||
parser(argc, args);
|
||||
}
|
||||
}
|
||||
|
||||
napi_value NapiAsync::AsyncWork(
|
||||
shared_ptr<AsyncContext> context, const string &name, AsyncExecute execute, AsyncComplete complete)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
napi_value result = nullptr;
|
||||
if (context->callbackRef_ == nullptr) {
|
||||
napi_create_promise(context->env_, &context->deferred_, &result);
|
||||
} else {
|
||||
napi_get_undefined(context->env_, &result);
|
||||
}
|
||||
|
||||
napi_value resource = nullptr;
|
||||
complete_ = complete;
|
||||
napi_create_string_utf8(context->env_, name.c_str(), NAPI_AUTO_LENGTH, &resource);
|
||||
napi_create_async_work(context->env_,
|
||||
nullptr,
|
||||
resource,
|
||||
execute,
|
||||
AsyncCompleteCallback,
|
||||
static_cast<void *>(context.get()),
|
||||
&context->work_);
|
||||
|
||||
napi_queue_async_work(context->env_, context->work_);
|
||||
context->contextSp_ = context;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value NapiAsync::AsyncWork(
|
||||
shared_ptr<AsyncContext> context, const string &name, AsyncExecute execute, napi_async_complete_callback complete)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
napi_value result = nullptr;
|
||||
if (context->callbackRef_ == nullptr) {
|
||||
napi_create_promise(context->env_, &context->deferred_, &result);
|
||||
} else {
|
||||
napi_get_undefined(context->env_, &result);
|
||||
}
|
||||
|
||||
napi_value resource = nullptr;
|
||||
napi_create_string_utf8(context->env_, name.c_str(), NAPI_AUTO_LENGTH, &resource);
|
||||
napi_create_async_work(
|
||||
context->env_, nullptr, resource, execute, complete, static_cast<void *>(context.get()), &context->work_);
|
||||
|
||||
napi_queue_async_work(context->env_, context->work_);
|
||||
context->contextSp_ = context;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void NapiAsync::CommonCallbackRoutine(AsyncContext *context, const napi_value &data)
|
||||
{
|
||||
CHECK_RETURN_VOID(context != nullptr, "async context is null");
|
||||
|
||||
if (context->deferred_) {
|
||||
HandlePromise(context, data);
|
||||
} else {
|
||||
HandleAsyncCallback(context, data);
|
||||
}
|
||||
context->contextSp_.reset();
|
||||
}
|
||||
|
||||
void NapiAsync::HandlePromise(AsyncContext *context, const napi_value &data)
|
||||
{
|
||||
napi_value result = nullptr;
|
||||
napi_value error = nullptr;
|
||||
napi_get_undefined(context->env_, &result);
|
||||
napi_get_undefined(context->env_, &error);
|
||||
|
||||
if (context->status_ == napi_ok) {
|
||||
result = data;
|
||||
napi_resolve_deferred(context->env_, context->deferred_, result);
|
||||
return;
|
||||
}
|
||||
napi_value message = nullptr;
|
||||
napi_create_string_utf8(context->env_, context->error_.c_str(), NAPI_AUTO_LENGTH, &message);
|
||||
napi_create_error(context->env_, nullptr, message, &error);
|
||||
napi_reject_deferred(context->env_, context->deferred_, error);
|
||||
}
|
||||
|
||||
void NapiAsync::HandleAsyncCallback(AsyncContext *context, const napi_value &data)
|
||||
{
|
||||
const size_t argc = 2;
|
||||
napi_value args[argc] = {nullptr, nullptr};
|
||||
napi_value callback = nullptr;
|
||||
napi_value ret = nullptr;
|
||||
if (context->status_ == napi_ok) {
|
||||
napi_get_undefined(context->env_, &args[0]);
|
||||
args[1] = data;
|
||||
} else {
|
||||
napi_value message = nullptr;
|
||||
napi_create_string_utf8(context->env_, context->error_.c_str(), NAPI_AUTO_LENGTH, &message);
|
||||
napi_create_error(context->env_, nullptr, message, &args[0]);
|
||||
napi_get_undefined(context->env_, &args[1]);
|
||||
}
|
||||
napi_get_reference_value(context->env_, context->callbackRef_, &callback);
|
||||
napi_call_function(context->env_, nullptr, callback, argc, args, &ret);
|
||||
}
|
||||
|
||||
void NapiAsync::AsyncCompleteCallback(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
CHECK_RETURN_VOID(data != nullptr, "async complete callback data is null");
|
||||
napi_value result = nullptr;
|
||||
napi_get_undefined(env, &result);
|
||||
auto context = static_cast<AsyncContext *>(data);
|
||||
if ((status != napi_ok) && (context->status_ == napi_ok)) {
|
||||
context->status_ = status;
|
||||
}
|
||||
|
||||
if (context->status_ == napi_ok && (complete_ != nullptr)) {
|
||||
complete_(context, result);
|
||||
}
|
||||
CommonCallbackRoutine(context, result);
|
||||
}
|
||||
} // namespace IntellVoiceNapi
|
||||
} // namespace OHOS
|
||||
Executable
+81
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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_NAPI_QUEUE_H
|
||||
#define INTELL_VOICE_NAPI_QUEUE_H
|
||||
|
||||
#include <functional>
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceNapi {
|
||||
constexpr size_t ARGC_MAX = 4;
|
||||
|
||||
using CbInfoParser = std::function<void(size_t argc, napi_value *argv)>;
|
||||
|
||||
#define CHECK_STATUS_RETURN_VOID(context, message) \
|
||||
do { \
|
||||
if ((context)->status_ != napi_ok) { \
|
||||
(context)->error_ = std::string(message); \
|
||||
INTELL_VOICE_LOG_ERROR(message); \
|
||||
return; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
class AsyncContext {
|
||||
public:
|
||||
AsyncContext();
|
||||
~AsyncContext();
|
||||
void GetCbInfo(napi_env env, napi_callback_info info, size_t callBackIndex, CbInfoParser parser);
|
||||
|
||||
napi_env env_ = nullptr;
|
||||
void *engineNapi_ = nullptr;
|
||||
|
||||
napi_status status_ = napi_invalid_arg;
|
||||
std::string error_;
|
||||
int32_t result_ = -1;
|
||||
|
||||
private:
|
||||
napi_async_work work_ = nullptr;
|
||||
napi_deferred deferred_ = nullptr;
|
||||
napi_ref callbackRef_ = nullptr;
|
||||
std::shared_ptr<AsyncContext> contextSp_ = nullptr;
|
||||
|
||||
friend class NapiAsync;
|
||||
};
|
||||
|
||||
using AsyncExecute = napi_async_execute_callback;
|
||||
using AsyncComplete = std::function<void(AsyncContext *, napi_value &)>;
|
||||
|
||||
class NapiAsync {
|
||||
public:
|
||||
static napi_value AsyncWork(std::shared_ptr<AsyncContext> context, const std::string &name, AsyncExecute execute,
|
||||
AsyncComplete complete = nullptr);
|
||||
static napi_value AsyncWork(std::shared_ptr<AsyncContext> context, const std::string &name, AsyncExecute execute,
|
||||
napi_async_complete_callback complete);
|
||||
static void CommonCallbackRoutine(AsyncContext *context, const napi_value &data);
|
||||
|
||||
private:
|
||||
static void AsyncCompleteCallback(napi_env env, napi_status status, void *data);
|
||||
static void HandlePromise(AsyncContext *context, const napi_value &data);
|
||||
static void HandleAsyncCallback(AsyncContext *context, const napi_value &data);
|
||||
|
||||
static AsyncComplete complete_;
|
||||
};
|
||||
} // namespace IntellVoiceNapi
|
||||
} // namespace OHOS
|
||||
|
||||
#endif
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* 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_napi_util.h"
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
#define LOG_TAG "IntellVoiceNapiUtil"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceNapi {
|
||||
static const int32_t MAX_STRING_BUFFSIZE = 1024;
|
||||
|
||||
IntellVoiceRef::IntellVoiceRef(napi_env env, napi_value value)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
env_ = env;
|
||||
napi_create_reference(env_, value, 1, &ref_);
|
||||
}
|
||||
|
||||
IntellVoiceRef::~IntellVoiceRef()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (env_ != nullptr && ref_ != nullptr) {
|
||||
napi_delete_reference(env_, ref_);
|
||||
}
|
||||
env_ = nullptr;
|
||||
ref_ = nullptr;
|
||||
}
|
||||
|
||||
napi_value IntellVoiceRef::GetRefValue()
|
||||
{
|
||||
napi_value value = nullptr;
|
||||
napi_status status = napi_get_reference_value(env_, ref_, &value);
|
||||
if (status != napi_ok || value == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("get reference value fail");
|
||||
return nullptr;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
napi_value SetValue(napi_env env, const int32_t &value)
|
||||
{
|
||||
napi_value result = nullptr;
|
||||
napi_status status = napi_create_int32(env, value, &result);
|
||||
if (status != napi_ok || result == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("get js value fail");
|
||||
return nullptr;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value SetValue(napi_env env, const string &value)
|
||||
{
|
||||
napi_value result = nullptr;
|
||||
napi_status status = napi_create_string_utf8(env, value.c_str(), value.size(), &result);
|
||||
if (status != napi_ok || result == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("get js value fail");
|
||||
return nullptr;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_status GetValue(napi_env env, napi_value jsValue, int32_t &value)
|
||||
{
|
||||
napi_valuetype valueType = napi_undefined;
|
||||
napi_typeof(env, jsValue, &valueType);
|
||||
if (valueType != napi_number) {
|
||||
return napi_generic_failure;
|
||||
}
|
||||
return napi_get_value_int32(env, jsValue, &value);
|
||||
}
|
||||
|
||||
napi_status GetValue(napi_env env, napi_value jsValue, bool &value)
|
||||
{
|
||||
napi_valuetype valueType = napi_undefined;
|
||||
napi_typeof(env, jsValue, &valueType);
|
||||
if (valueType != napi_boolean) {
|
||||
return napi_generic_failure;
|
||||
}
|
||||
return napi_get_value_bool(env, jsValue, &value);
|
||||
}
|
||||
|
||||
napi_status GetValue(napi_env env, napi_value jsValue, string &value)
|
||||
{
|
||||
size_t bufferSize = 0;
|
||||
napi_valuetype valueType = napi_undefined;
|
||||
napi_typeof(env, jsValue, &valueType);
|
||||
if (valueType != napi_string) {
|
||||
return napi_generic_failure;
|
||||
}
|
||||
napi_status status = napi_get_value_string_utf8(env, jsValue, nullptr, 0, &bufferSize);
|
||||
if (status != napi_ok || bufferSize == 0 || bufferSize >= MAX_STRING_BUFFSIZE) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to get buffersize");
|
||||
return napi_generic_failure;
|
||||
}
|
||||
|
||||
char *buffer = (char *)malloc((bufferSize + 1) * sizeof(char));
|
||||
if (buffer == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("no memory");
|
||||
return napi_generic_failure;
|
||||
}
|
||||
|
||||
status = napi_get_value_string_utf8(env, jsValue, buffer, bufferSize + 1, &bufferSize);
|
||||
value = buffer;
|
||||
free(buffer);
|
||||
buffer = nullptr;
|
||||
return status;
|
||||
}
|
||||
} // namespace IntellVoiceNapi
|
||||
} // namespace OHOS
|
||||
Executable
+51
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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_NAPI_UTIL_H
|
||||
#define INTELL_VOICE_NAPI_UTIL_H
|
||||
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceNapi {
|
||||
#define CHECK_RETURN_VOID(condition, message) \
|
||||
do { \
|
||||
if (!(condition)) { \
|
||||
INTELL_VOICE_LOG_ERROR(message); \
|
||||
return; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
class IntellVoiceRef {
|
||||
public:
|
||||
IntellVoiceRef(napi_env env, napi_value value);
|
||||
~IntellVoiceRef();
|
||||
napi_value GetRefValue();
|
||||
|
||||
private:
|
||||
napi_env env_ = nullptr;
|
||||
napi_ref ref_ = nullptr;
|
||||
};
|
||||
|
||||
napi_value SetValue(napi_env env, const int32_t &value);
|
||||
napi_value SetValue(napi_env env, const std::string &value);
|
||||
|
||||
napi_status GetValue(napi_env env, napi_value jsValue, int32_t &value);
|
||||
napi_status GetValue(napi_env env, napi_value jsValue, bool &value);
|
||||
napi_status GetValue(napi_env env, napi_value jsValue, std::string &value);
|
||||
} // namespace IntellVoiceNapi
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
+444
@@ -0,0 +1,444 @@
|
||||
/*
|
||||
* 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 "wakeup_intell_voice_engine_napi.h"
|
||||
|
||||
#include "want.h"
|
||||
#include "iservice_registry.h"
|
||||
#include "system_ability_definition.h"
|
||||
|
||||
#include "intell_voice_napi_util.h"
|
||||
#include "intell_voice_napi_queue.h"
|
||||
|
||||
#include "intell_voice_log.h"
|
||||
#include "i_intell_voice_engine_callback.h"
|
||||
|
||||
#define LOG_TAG "WakeupIntellVoiceEngineNapi"
|
||||
|
||||
using namespace std;
|
||||
using namespace OHOS::IntellVoice;
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceNapi {
|
||||
static const std::string INTELL_VOICE_EVENT_CALLBACK_NAME = "IntellVoiceEvent";
|
||||
|
||||
WakeupIntellVoiceEngineNapi::WakeupIntellVoiceEngineNapi()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter, %{public}p", this);
|
||||
}
|
||||
|
||||
WakeupIntellVoiceEngineNapi::~WakeupIntellVoiceEngineNapi()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (wrapper_ != nullptr) {
|
||||
napi_delete_reference(env_, wrapper_);
|
||||
}
|
||||
}
|
||||
|
||||
napi_value WakeupIntellVoiceEngineNapi::New(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t argc = 1;
|
||||
napi_value args[1] = {nullptr};
|
||||
|
||||
napi_value jsThis = nullptr;
|
||||
napi_value undefinedResult = nullptr;
|
||||
napi_get_undefined(env, &undefinedResult);
|
||||
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr));
|
||||
|
||||
unique_ptr<WakeupIntellVoiceEngineNapi> engineNapi = make_unique<WakeupIntellVoiceEngineNapi>();
|
||||
if (engineNapi == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("Failed to create WakeupIntellVoiceEngineNapi, No memory.");
|
||||
return undefinedResult;
|
||||
}
|
||||
engineNapi->env_ = env;
|
||||
|
||||
WakeupIntelligentVoiceEngineDescriptor descriptor = {};
|
||||
napi_value value = nullptr;
|
||||
if (napi_get_named_property(env, args[0], "needApAlgEngine", &value) == napi_ok) {
|
||||
GetValue(env, value, descriptor.needApAlgEngine);
|
||||
}
|
||||
if (napi_get_named_property(env, args[0], "wakeupPhrase", &value) == napi_ok) {
|
||||
GetValue(env, value, descriptor.wakeupPhrase);
|
||||
}
|
||||
INTELL_VOICE_LOG_INFO("EngineDescriptor needApAlgEngine: %{public}d", descriptor.needApAlgEngine);
|
||||
INTELL_VOICE_LOG_INFO("EngineDescriptor wakeupPhrase: %{public}s", descriptor.wakeupPhrase.c_str());
|
||||
|
||||
engineNapi->engine_ = std::make_shared<WakeupIntellVoiceEngine>(descriptor);
|
||||
if (engineNapi->engine_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create intell voice engine faild.");
|
||||
return undefinedResult;
|
||||
}
|
||||
|
||||
auto finalize = [](napi_env env, void *data, void *hint) {
|
||||
INTELL_VOICE_LOG_INFO("WakeupIntellVoiceEngineNapi finalize callback");
|
||||
if (data != nullptr) {
|
||||
auto obj = static_cast<WakeupIntellVoiceEngineNapi *>(data);
|
||||
delete obj;
|
||||
}
|
||||
};
|
||||
|
||||
NAPI_CALL(env,
|
||||
napi_wrap(env, jsThis, static_cast<void *>(engineNapi.get()), finalize, nullptr, &(engineNapi->wrapper_)));
|
||||
engineNapi.release();
|
||||
return jsThis;
|
||||
}
|
||||
|
||||
napi_value WakeupIntellVoiceEngineNapi::Constructor(napi_env env)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
napi_status status;
|
||||
napi_value constructor;
|
||||
napi_get_undefined(env, &constructor);
|
||||
|
||||
napi_property_descriptor properties[] = {
|
||||
DECLARE_NAPI_FUNCTION("getSupportedRegions", GetSupportedRegions),
|
||||
DECLARE_NAPI_FUNCTION("setSensibility", SetSensibility),
|
||||
DECLARE_NAPI_FUNCTION("setWakeupHapInfo", SetWakeupHapInfo),
|
||||
DECLARE_NAPI_FUNCTION("setParameter", SetParameter),
|
||||
DECLARE_NAPI_FUNCTION("getParameter", GetParameter),
|
||||
DECLARE_NAPI_FUNCTION("release", Release),
|
||||
DECLARE_NAPI_FUNCTION("on", On),
|
||||
DECLARE_NAPI_FUNCTION("off", Off),
|
||||
};
|
||||
|
||||
status = napi_define_class(env,
|
||||
WAKEUP_ENGINE_NAPI_CLASS_NAME,
|
||||
NAPI_AUTO_LENGTH,
|
||||
New,
|
||||
nullptr,
|
||||
sizeof(properties) / sizeof(properties[0]),
|
||||
properties,
|
||||
&constructor);
|
||||
|
||||
return constructor;
|
||||
}
|
||||
|
||||
napi_value WakeupIntellVoiceEngineNapi::GetSupportedRegions(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t cbIndex = 0;
|
||||
shared_ptr<AsyncContext> context = make_shared<AsyncContext>();
|
||||
if (context == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create AsyncContext faild, No memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
context->GetCbInfo(env, info, cbIndex, nullptr);
|
||||
|
||||
AsyncExecute execute = [](napi_env env, void *data) {
|
||||
auto asyncContext = static_cast<AsyncContext *>(data);
|
||||
asyncContext->status_ = napi_ok;
|
||||
};
|
||||
|
||||
AsyncComplete complete = [](AsyncContext *context, napi_value &result) {
|
||||
INTELL_VOICE_LOG_INFO("get supported regions complete");
|
||||
napi_create_array(context->env_, &result);
|
||||
napi_set_element(context->env_, result, 0, SetValue(context->env_, "CN"));
|
||||
};
|
||||
|
||||
return NapiAsync::AsyncWork(context, "GetSupportedRegions", execute, complete);
|
||||
}
|
||||
|
||||
napi_value WakeupIntellVoiceEngineNapi::SetParameter(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t cbIndex = 2;
|
||||
class SetParameterContext : public AsyncContext {
|
||||
public:
|
||||
string key;
|
||||
string value;
|
||||
};
|
||||
auto context = make_shared<SetParameterContext>();
|
||||
if (context == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create SetParameterContext faild, No memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CbInfoParser parser = [env, context](size_t argc, napi_value *argv) {
|
||||
context->status_ = GetValue(env, argv[0], context->key);
|
||||
CHECK_STATUS_RETURN_VOID(context, "Failed to get key");
|
||||
context->status_ = GetValue(env, argv[1], context->value);
|
||||
CHECK_STATUS_RETURN_VOID(context, "Failed to get value");
|
||||
};
|
||||
|
||||
context->GetCbInfo(env, info, cbIndex, parser);
|
||||
|
||||
AsyncExecute execute = [](napi_env env, void *data) {
|
||||
auto asyncContext = static_cast<SetParameterContext *>(data);
|
||||
auto engine = reinterpret_cast<WakeupIntellVoiceEngineNapi *>(asyncContext->engineNapi_)->engine_;
|
||||
if (engine == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("get engine instance faild");
|
||||
return;
|
||||
}
|
||||
asyncContext->result_ = engine->SetParameter(asyncContext->key, asyncContext->value);
|
||||
asyncContext->status_ = napi_ok;
|
||||
};
|
||||
|
||||
return NapiAsync::AsyncWork(context, "SetParameter", execute);
|
||||
}
|
||||
|
||||
napi_value WakeupIntellVoiceEngineNapi::GetParameter(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t cbIndex = 1;
|
||||
shared_ptr<AsyncContext> context = make_shared<AsyncContext>();
|
||||
if (context == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create AsyncContext faild, No memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
context->GetCbInfo(env, info, cbIndex, nullptr);
|
||||
|
||||
AsyncExecute execute = [](napi_env env, void *data) {
|
||||
auto asyncContext = static_cast<AsyncContext *>(data);
|
||||
asyncContext->status_ = napi_ok;
|
||||
};
|
||||
|
||||
AsyncComplete complete = [](AsyncContext *context, napi_value &result) {
|
||||
INTELL_VOICE_LOG_INFO("get parameter complete");
|
||||
result = SetValue(context->env_, "value");
|
||||
};
|
||||
return NapiAsync::AsyncWork(context, "GetParameter", execute, complete);
|
||||
}
|
||||
|
||||
napi_value WakeupIntellVoiceEngineNapi::SetSensibility(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t cbIndex = 1;
|
||||
class SetSensibilityContext : public AsyncContext {
|
||||
public:
|
||||
int32_t sensibility;
|
||||
};
|
||||
auto context = make_shared<SetSensibilityContext>();
|
||||
if (context == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create SetSensibilityContext faild, No memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CbInfoParser parser = [env, context](size_t argc, napi_value *argv) {
|
||||
context->status_ = GetValue(env, argv[0], context->sensibility);
|
||||
CHECK_STATUS_RETURN_VOID(context, "Failed to get sensibility");
|
||||
};
|
||||
|
||||
context->GetCbInfo(env, info, cbIndex, parser);
|
||||
|
||||
AsyncExecute execute = [](napi_env env, void *data) {
|
||||
auto asyncContext = static_cast<SetSensibilityContext *>(data);
|
||||
auto engine = reinterpret_cast<WakeupIntellVoiceEngineNapi *>(asyncContext->engineNapi_)->engine_;
|
||||
if (engine == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("get engine instance faild");
|
||||
return;
|
||||
}
|
||||
asyncContext->result_ = engine->SetSensibility(asyncContext->sensibility);
|
||||
asyncContext->status_ = napi_ok;
|
||||
};
|
||||
|
||||
return NapiAsync::AsyncWork(context, "SetSensibility", execute);
|
||||
}
|
||||
|
||||
napi_value WakeupIntellVoiceEngineNapi::SetWakeupHapInfo(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t cbIndex = 1;
|
||||
class SetWakeupHapContext : public AsyncContext {
|
||||
public:
|
||||
WakeupHapInfo hapInfo;
|
||||
};
|
||||
auto context = make_shared<SetWakeupHapContext>();
|
||||
if (context == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create SetWakeupHapContext faild, No memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CbInfoParser parser = [env, context](size_t argc, napi_value *argv) {
|
||||
napi_value temp = nullptr;
|
||||
if (napi_get_named_property(env, argv[0], "bundleName", &temp) == napi_ok) {
|
||||
GetValue(env, temp, context->hapInfo.bundleName);
|
||||
}
|
||||
if (napi_get_named_property(env, argv[0], "abilityName", &temp) == napi_ok) {
|
||||
GetValue(env, temp, context->hapInfo.abilityName);
|
||||
}
|
||||
};
|
||||
|
||||
context->GetCbInfo(env, info, cbIndex, parser);
|
||||
|
||||
AsyncExecute execute = [](napi_env env, void *data) {
|
||||
auto asyncContext = static_cast<SetWakeupHapContext *>(data);
|
||||
auto engine = reinterpret_cast<WakeupIntellVoiceEngineNapi *>(asyncContext->engineNapi_)->engine_;
|
||||
if (engine == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("get engine instance faild");
|
||||
return;
|
||||
}
|
||||
asyncContext->result_ = engine->SetWakeupHapInfo(asyncContext->hapInfo);
|
||||
asyncContext->status_ = napi_ok;
|
||||
};
|
||||
|
||||
return NapiAsync::AsyncWork(context, "SetWakeupHapInfo", execute);
|
||||
}
|
||||
|
||||
napi_value WakeupIntellVoiceEngineNapi::Release(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
size_t cbIndex = 0;
|
||||
shared_ptr<AsyncContext> context = make_shared<AsyncContext>();
|
||||
if (context == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create AsyncContext faild, No memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
context->GetCbInfo(env, info, cbIndex, nullptr);
|
||||
|
||||
AsyncExecute execute = [](napi_env env, void *data) {
|
||||
auto asyncContext = static_cast<AsyncContext *>(data);
|
||||
auto engine = reinterpret_cast<WakeupIntellVoiceEngineNapi *>(asyncContext->engineNapi_)->engine_;
|
||||
if (engine == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("get engine instance faild");
|
||||
return;
|
||||
}
|
||||
asyncContext->result_ = engine->Release();
|
||||
asyncContext->status_ = napi_ok;
|
||||
reinterpret_cast<WakeupIntellVoiceEngineNapi *>(asyncContext->engineNapi_)->engine_ = nullptr;
|
||||
};
|
||||
|
||||
AsyncComplete complete = [](AsyncContext *context, napi_value &result) {
|
||||
INTELL_VOICE_LOG_INFO("release engine complete");
|
||||
};
|
||||
return NapiAsync::AsyncWork(context, "Release", execute, complete);
|
||||
}
|
||||
|
||||
napi_value WakeupIntellVoiceEngineNapi::On(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
|
||||
napi_value undefinedResult = nullptr;
|
||||
napi_get_undefined(env, &undefinedResult);
|
||||
|
||||
const size_t expectArgCount = 2;
|
||||
size_t argCount = 2;
|
||||
napi_value args[expectArgCount] = {0};
|
||||
napi_value jsThis = nullptr;
|
||||
|
||||
napi_status status = napi_get_cb_info(env, info, &argCount, args, &jsThis, nullptr);
|
||||
if (status != napi_ok || argCount != expectArgCount) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to get parameters");
|
||||
return undefinedResult;
|
||||
}
|
||||
|
||||
napi_valuetype eventType = napi_undefined;
|
||||
if (napi_typeof(env, args[0], &eventType) != napi_ok || eventType != napi_string) {
|
||||
INTELL_VOICE_LOG_ERROR("callback event name type mismatch");
|
||||
return undefinedResult;
|
||||
}
|
||||
|
||||
string callbackName = "";
|
||||
status = GetValue(env, args[0], callbackName);
|
||||
if (status != napi_ok) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to get callbackName");
|
||||
return undefinedResult;
|
||||
}
|
||||
INTELL_VOICE_LOG_INFO("callbackName: %{public}s", callbackName.c_str());
|
||||
|
||||
napi_valuetype handler = napi_undefined;
|
||||
if (napi_typeof(env, args[1], &handler) != napi_ok || handler != napi_function) {
|
||||
INTELL_VOICE_LOG_ERROR("callback handler type mismatch");
|
||||
return undefinedResult;
|
||||
}
|
||||
|
||||
return RegisterCallback(env, jsThis, args);
|
||||
}
|
||||
|
||||
napi_value WakeupIntellVoiceEngineNapi::RegisterCallback(napi_env env, napi_value jsThis, napi_value *args)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
napi_value result = nullptr;
|
||||
napi_get_undefined(env, &result);
|
||||
|
||||
WakeupIntellVoiceEngineNapi *engineNapi = nullptr;
|
||||
napi_status status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&engineNapi));
|
||||
if (status != napi_ok || engineNapi == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("Failed to get engine napi instance");
|
||||
return result;
|
||||
}
|
||||
|
||||
if (engineNapi->engine_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("engine is null");
|
||||
return result;
|
||||
}
|
||||
|
||||
engineNapi->callbackNapi_ = make_shared<EngineEventCallbackNapi>(env, args[1]);
|
||||
engineNapi->engine_->SetCallback(engineNapi->callbackNapi_);
|
||||
INTELL_VOICE_LOG_INFO("Set callback finish");
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value WakeupIntellVoiceEngineNapi::Off(napi_env env, napi_callback_info info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
|
||||
napi_value undefinedResult = nullptr;
|
||||
napi_get_undefined(env, &undefinedResult);
|
||||
|
||||
const size_t expectArgCount = 1;
|
||||
size_t argCount = 1;
|
||||
napi_value args[expectArgCount] = {0};
|
||||
napi_value jsThis = nullptr;
|
||||
|
||||
napi_status status = napi_get_cb_info(env, info, &argCount, args, &jsThis, nullptr);
|
||||
if (status != napi_ok || argCount != expectArgCount) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to get parameters");
|
||||
return undefinedResult;
|
||||
}
|
||||
|
||||
napi_valuetype eventType = napi_undefined;
|
||||
if (napi_typeof(env, args[0], &eventType) != napi_ok || eventType != napi_string) {
|
||||
INTELL_VOICE_LOG_ERROR("callback event name type mismatch");
|
||||
return undefinedResult;
|
||||
}
|
||||
|
||||
string callbackName = "";
|
||||
status = GetValue(env, args[0], callbackName);
|
||||
if (status != napi_ok) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to get callbackName");
|
||||
return undefinedResult;
|
||||
}
|
||||
INTELL_VOICE_LOG_INFO("callbackName: %{public}s", callbackName.c_str());
|
||||
|
||||
return UnregisterCallback(env, jsThis, callbackName);
|
||||
}
|
||||
|
||||
napi_value WakeupIntellVoiceEngineNapi::UnregisterCallback(napi_env env, napi_value jsThis, const string &callbackName)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
napi_value result = nullptr;
|
||||
napi_get_undefined(env, &result);
|
||||
|
||||
WakeupIntellVoiceEngineNapi *engineNapi = nullptr;
|
||||
napi_status status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&engineNapi));
|
||||
if (status != napi_ok || engineNapi == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("Failed to get engine napi instance");
|
||||
return result;
|
||||
}
|
||||
|
||||
if (callbackName != INTELL_VOICE_EVENT_CALLBACK_NAME) {
|
||||
INTELL_VOICE_LOG_ERROR("No such off callback supported");
|
||||
return result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
} // namespace IntellVoiceNapi
|
||||
} // namespace OHOS
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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 WAKEUP_INTELL_VOICE_ENGINE_NAPI_H
|
||||
#define WAKEUP_INTELL_VOICE_ENGINE_NAPI_H
|
||||
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
#include "engine_event_callback_napi.h"
|
||||
#include "wakeup_intell_voice_engine.h"
|
||||
|
||||
#define WAKEUP_ENGINE_NAPI_CLASS_NAME "WakeupIntelligentVoiceEngine"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceNapi {
|
||||
using OHOS::IntellVoice::WakeupIntellVoiceEngine;
|
||||
|
||||
class WakeupIntellVoiceEngineNapi {
|
||||
public:
|
||||
WakeupIntellVoiceEngineNapi();
|
||||
~WakeupIntellVoiceEngineNapi();
|
||||
|
||||
static napi_value Constructor(napi_env env);
|
||||
|
||||
static napi_value GetSupportedRegions(napi_env env, napi_callback_info info);
|
||||
static napi_value SetSensibility(napi_env env, napi_callback_info info);
|
||||
static napi_value SetWakeupHapInfo(napi_env env, napi_callback_info info);
|
||||
static napi_value SetParameter(napi_env env, napi_callback_info info);
|
||||
static napi_value GetParameter(napi_env env, napi_callback_info info);
|
||||
static napi_value Release(napi_env env, napi_callback_info info);
|
||||
static napi_value On(napi_env env, napi_callback_info info);
|
||||
static napi_value Off(napi_env env, napi_callback_info info);
|
||||
|
||||
std::shared_ptr<WakeupIntellVoiceEngine> engine_ = nullptr;
|
||||
std::shared_ptr<EngineEventCallbackNapi> callbackNapi_ = nullptr;
|
||||
|
||||
private:
|
||||
static napi_value New(napi_env env, napi_callback_info info);
|
||||
|
||||
static napi_value RegisterCallback(napi_env env, napi_value jsThis, napi_value *args);
|
||||
static napi_value UnregisterCallback(napi_env env, napi_value jsThis, const std::string &callbackName);
|
||||
|
||||
private:
|
||||
napi_env env_ = nullptr;
|
||||
napi_ref wrapper_ = nullptr;
|
||||
};
|
||||
} // namespace IntellVoiceNapi
|
||||
} // namespace OHOS
|
||||
|
||||
#endif
|
||||
Executable
+52
@@ -0,0 +1,52 @@
|
||||
# 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.
|
||||
|
||||
import("//build/ohos.gni")
|
||||
|
||||
ohos_shared_library("intellvoice_native") {
|
||||
sources = [
|
||||
"enroll_intell_voice_engine.cpp",
|
||||
"intell_voice_manager.cpp",
|
||||
"wakeup_intell_voice_engine.cpp",
|
||||
]
|
||||
|
||||
include_dirs = [
|
||||
"//foundation/ai/intelligent_voice_framework/interfaces/inner_api/native",
|
||||
"//foundation/ai/intelligent_voice_framework/services/intell_voice_engine",
|
||||
"//foundation/ai/intelligent_voice_framework/services/intell_voice_engine/proxy",
|
||||
"//foundation/ai/intelligent_voice_framework/utils",
|
||||
]
|
||||
|
||||
cflags_cc = [
|
||||
"-Wno-error=unused-parameter",
|
||||
"-DHILOG_ENABLE",
|
||||
"-DENABLE_DEBUG",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"//foundation/ai/intelligent_voice_framework/services:intell_voice_proxy",
|
||||
]
|
||||
|
||||
external_deps = [
|
||||
"ability_base:base",
|
||||
"ability_base:want",
|
||||
"c_utils:utils",
|
||||
"hiviewdfx_hilog_native:libhilog",
|
||||
"ipc:ipc_core",
|
||||
"safwk:system_ability_fwk",
|
||||
"samgr:samgr_proxy",
|
||||
]
|
||||
|
||||
subsystem_name = "ai"
|
||||
part_name = "intelligent_voice_framework"
|
||||
}
|
||||
+163
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* 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 "enroll_intell_voice_engine.h"
|
||||
#include "i_intell_voice_engine.h"
|
||||
#include "intell_voice_manager.h"
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
#define LOG_TAG "EnrollIntellVoiceEngine"
|
||||
using namespace std;
|
||||
using namespace OHOS::IntellVoiceEngine;
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoice {
|
||||
static constexpr int32_t MIN_BUFFER_SIZE = 1280; // 16 * 2 * 40ms
|
||||
static constexpr int32_t CHANNEL_CNT = 1;
|
||||
static constexpr int32_t BITS_PER_SAMPLE = 16;
|
||||
static constexpr int32_t SAMPLE_RATE = 16000;
|
||||
|
||||
EnrollIntellVoiceEngine::EnrollIntellVoiceEngine(const EnrollIntelligentVoiceEngineDescriptor &descriptor)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
|
||||
descriptor_ = make_unique<EnrollIntelligentVoiceEngineDescriptor>();
|
||||
if (descriptor_ != nullptr) {
|
||||
descriptor_->wakeupPhrase = descriptor.wakeupPhrase;
|
||||
}
|
||||
IntellVoiceManager::GetInstance()->CreateIntellVoiceEngine(INTELL_VOICE_ENROLL, engine_);
|
||||
}
|
||||
|
||||
EnrollIntellVoiceEngine::~EnrollIntellVoiceEngine()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
IntellVoiceManager::GetInstance()->ReleaseIntellVoiceEngine(INTELL_VOICE_ENROLL);
|
||||
}
|
||||
|
||||
int32_t EnrollIntellVoiceEngine::Init(const EngineConfig &config)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (engine_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("IntellVoiceEngine is null");
|
||||
return -1;
|
||||
}
|
||||
|
||||
IntellVoiceEngineInfo info = {};
|
||||
info.wakeupPhrase = descriptor_->wakeupPhrase;
|
||||
info.isPcmFromExternal = false;
|
||||
info.minBufSize = MIN_BUFFER_SIZE;
|
||||
info.sampleChannels = CHANNEL_CNT;
|
||||
info.bitsPerSample = BITS_PER_SAMPLE;
|
||||
info.sampleRate = SAMPLE_RATE;
|
||||
|
||||
return engine_->Attach(info);
|
||||
}
|
||||
|
||||
int32_t EnrollIntellVoiceEngine::Start(const bool &isLast)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (engine_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("engine is null");
|
||||
return -1;
|
||||
}
|
||||
return engine_->Start(isLast);
|
||||
}
|
||||
|
||||
int32_t EnrollIntellVoiceEngine::Stop()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (engine_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("engine is null");
|
||||
return -1;
|
||||
}
|
||||
return engine_->Stop();
|
||||
}
|
||||
|
||||
int32_t EnrollIntellVoiceEngine::Commit()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (engine_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("engine is null");
|
||||
return -1;
|
||||
}
|
||||
string keyValueList = "CommitEnrollment=true";
|
||||
return engine_->SetParameter(keyValueList);
|
||||
}
|
||||
|
||||
int32_t EnrollIntellVoiceEngine::SetSensibility(const int32_t &sensibility)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (engine_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("engine is null");
|
||||
return -1;
|
||||
}
|
||||
string keyValueList = "Sensibility=" + to_string(sensibility);
|
||||
return engine_->SetParameter(keyValueList);
|
||||
}
|
||||
|
||||
int32_t EnrollIntellVoiceEngine::SetWakeupHapInfo(const WakeupHapInfo &info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
int32_t ret;
|
||||
if (engine_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("engine is null");
|
||||
return -1;
|
||||
}
|
||||
ret = engine_->SetParameter("wakeup_bundle_name=" + info.bundleName);
|
||||
ret = engine_->SetParameter("wakeup_ability_name=" + info.abilityName);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int32_t EnrollIntellVoiceEngine::SetParameter(const string &key, const string &value)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (engine_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("engine is null");
|
||||
return -1;
|
||||
}
|
||||
string keyValueList = key + "=" + value;
|
||||
return engine_->SetParameter(keyValueList);
|
||||
}
|
||||
|
||||
int32_t EnrollIntellVoiceEngine::Release()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (engine_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("engine is null");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return engine_->Detach();
|
||||
}
|
||||
|
||||
int32_t EnrollIntellVoiceEngine::SetCallback(shared_ptr<IIntellVoiceEngineEventCallback> callback)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (engine_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("engine is null");
|
||||
return -1;
|
||||
}
|
||||
|
||||
callback_ = sptr<EngineCallbackInner>(new (std::nothrow) EngineCallbackInner());
|
||||
if (callback_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("callback_ is nullptr");
|
||||
return -1;
|
||||
}
|
||||
callback_->SetEngineEventCallback(callback);
|
||||
engine_->SetCallback(callback_->AsObject());
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
+88
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* 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_manager.h"
|
||||
#include "iservice_registry.h"
|
||||
#include "system_ability_definition.h"
|
||||
#include "intell_voice_log.h"
|
||||
#include "i_intell_voice_service.h"
|
||||
#include "intell_voice_service_proxy.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace OHOS::IntellVoiceEngine;
|
||||
#define LOG_TAG "IntellVoiceManager"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoice {
|
||||
static sptr<IIntellVoiceService> g_sProxy = nullptr;
|
||||
|
||||
IntellVoiceManager::IntellVoiceManager()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
IntellVoiceManager::~IntellVoiceManager()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
}
|
||||
|
||||
IntellVoiceManager *IntellVoiceManager::GetInstance()
|
||||
{
|
||||
static IntellVoiceManager manager;
|
||||
return &manager;
|
||||
}
|
||||
|
||||
void IntellVoiceManager::Init()
|
||||
{
|
||||
auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
|
||||
if (samgr == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("get sa manager failed");
|
||||
return;
|
||||
}
|
||||
|
||||
sptr<IRemoteObject> object = samgr->GetSystemAbility(INTELL_VOICE_SERVICE_ID);
|
||||
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_ERROR("init Service Proxy failed.");
|
||||
} else {
|
||||
INTELL_VOICE_LOG_INFO("init Service Proxy success.");
|
||||
}
|
||||
}
|
||||
|
||||
int32_t IntellVoiceManager::CreateIntellVoiceEngine(IntellVoiceEngineType type, sptr<IIntellVoiceEngine> &inst)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (g_sProxy == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("IntellVoiceService Proxy is null.");
|
||||
return -1;
|
||||
}
|
||||
return g_sProxy->CreateIntellVoiceEngine(type, inst);
|
||||
}
|
||||
|
||||
int32_t IntellVoiceManager::ReleaseIntellVoiceEngine(IntellVoiceEngineType type)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (g_sProxy == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("IntellVoiceService Proxy is null.");
|
||||
return -1;
|
||||
}
|
||||
return g_sProxy->ReleaseIntellVoiceEngine(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* 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 "wakeup_intell_voice_engine.h"
|
||||
#include "i_intell_voice_engine.h"
|
||||
#include "intell_voice_manager.h"
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
using namespace std;
|
||||
#define LOG_TAG "WakeupIntellVoiceEngine"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoice {
|
||||
WakeupIntellVoiceEngine::WakeupIntellVoiceEngine(const WakeupIntelligentVoiceEngineDescriptor &descriptor)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
|
||||
descriptor_ = make_unique<WakeupIntelligentVoiceEngineDescriptor>();
|
||||
descriptor_->needApAlgEngine = descriptor.needApAlgEngine;
|
||||
descriptor_->wakeupPhrase = descriptor.wakeupPhrase;
|
||||
IntellVoiceManager::GetInstance()->CreateIntellVoiceEngine(INTELL_VOICE_WAKEUP, engine_);
|
||||
}
|
||||
|
||||
WakeupIntellVoiceEngine::~WakeupIntellVoiceEngine()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
IntellVoiceManager::GetInstance()->ReleaseIntellVoiceEngine(INTELL_VOICE_WAKEUP);
|
||||
}
|
||||
|
||||
int32_t WakeupIntellVoiceEngine::SetSensibility(const int32_t &sensibility)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (engine_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("engine is null");
|
||||
return -1;
|
||||
}
|
||||
string keyValueList = "Sensibility=" + to_string(sensibility);
|
||||
return engine_->SetParameter(keyValueList);
|
||||
}
|
||||
|
||||
int32_t WakeupIntellVoiceEngine::SetWakeupHapInfo(const WakeupHapInfo &info)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
int32_t ret;
|
||||
if (engine_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("engine is null");
|
||||
return -1;
|
||||
}
|
||||
ret = engine_->SetParameter("wakeup_bundle_name=" + info.bundleName);
|
||||
ret = engine_->SetParameter("wakeup_ability_name=" + info.abilityName);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int32_t WakeupIntellVoiceEngine::SetParameter(const string &key, const string &value)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (engine_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("engine is null");
|
||||
return -1;
|
||||
}
|
||||
string keyValueList = key + "=" + value;
|
||||
return engine_->SetParameter(keyValueList);
|
||||
}
|
||||
|
||||
int32_t WakeupIntellVoiceEngine::Release()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (engine_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("engine is null");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return engine_->Detach();
|
||||
}
|
||||
|
||||
int32_t WakeupIntellVoiceEngine::SetCallback(shared_ptr<IIntellVoiceEngineEventCallback> callback)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (engine_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("engine is null");
|
||||
return -1;
|
||||
}
|
||||
|
||||
callback_ = sptr<EngineCallbackInner>(new (std::nothrow) EngineCallbackInner());
|
||||
if (callback_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("callback_ is nullptr");
|
||||
return -1;
|
||||
}
|
||||
callback_->SetEngineEventCallback(callback);
|
||||
engine_->SetCallback(callback_->AsObject());
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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 ENROLL_INTELL_VOICE_ENGINE_H
|
||||
#define ENROLL_INTELL_VOICE_ENGINE_H
|
||||
|
||||
#include "i_intell_voice_engine.h"
|
||||
#include "engine_callback_inner.h"
|
||||
#include "intell_voice_info.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoice {
|
||||
using OHOS::IntellVoiceEngine::IIntellVoiceEngineEventCallback;
|
||||
using OHOS::IntellVoiceEngine::IIntellVoiceEngine;
|
||||
using OHOS::IntellVoiceEngine::EngineCallbackInner;
|
||||
|
||||
struct EnrollIntelligentVoiceEngineDescriptor {
|
||||
std::string wakeupPhrase;
|
||||
};
|
||||
|
||||
struct EngineConfig {
|
||||
std::string language;
|
||||
std::string region;
|
||||
};
|
||||
|
||||
class EnrollIntellVoiceEngine {
|
||||
public:
|
||||
EnrollIntellVoiceEngine(const EnrollIntelligentVoiceEngineDescriptor &descriptor);
|
||||
~EnrollIntellVoiceEngine();
|
||||
|
||||
int32_t Init(const EngineConfig &config);
|
||||
int32_t Start(const bool &isLast);
|
||||
int32_t Stop();
|
||||
int32_t Commit();
|
||||
int32_t SetSensibility(const int32_t &sensibility);
|
||||
int32_t SetWakeupHapInfo(const WakeupHapInfo &info);
|
||||
int32_t SetParameter(const std::string &key, const std::string &value);
|
||||
int32_t GetParameter(const std::string &key);
|
||||
int32_t Release();
|
||||
int32_t SetCallback(std::shared_ptr<IIntellVoiceEngineEventCallback> callback);
|
||||
|
||||
private:
|
||||
sptr<IIntellVoiceEngine> engine_ = nullptr;
|
||||
std::unique_ptr<EnrollIntelligentVoiceEngineDescriptor> descriptor_ = nullptr;
|
||||
sptr<EngineCallbackInner> callback_ = nullptr;
|
||||
};
|
||||
} // namespace IntellVoice
|
||||
} // namespace OHOS
|
||||
|
||||
#endif
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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_UTILS_H
|
||||
#define INTELL_VOICE_UTILS_H
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoice {
|
||||
enum ServiceChangeType {
|
||||
/* Service unavailable. */
|
||||
SERVICE_UNAVAILABLE = 0,
|
||||
};
|
||||
|
||||
enum IntelligentVoiceEngineType {
|
||||
/* Enroll engine. */
|
||||
ENROLL_ENGINE_TYPE = 0,
|
||||
/* Wakeup engine. */
|
||||
WAKEUP_ENGINE_TYPE = 1,
|
||||
/* Update engine. */
|
||||
UPDATE_ENGINE_TYPE = 2,
|
||||
};
|
||||
|
||||
enum SensibilityType {
|
||||
/* Low sensibility. */
|
||||
LOW_SENSIBILITY = 1,
|
||||
/* Middle sensibility. */
|
||||
MIDDLE_SENSIBILITY = 2,
|
||||
/* High sensibility. */
|
||||
HIGH_SENSIBILITY = 3,
|
||||
};
|
||||
|
||||
enum EnrollIntelligentVoiceEventType {
|
||||
/* Enroll None. */
|
||||
INTELLIGENT_VOICE_EVENT_ENROLL_NONE = 0,
|
||||
/* Init done. */
|
||||
INTELLIGENT_VOICE_EVENT_ENROLL_INIT_DONE = 1,
|
||||
/* Enroll complete. */
|
||||
INTELLIGENT_VOICE_EVENT_ENROLL_COMPLETE = 2,
|
||||
/* Commit enroll complete. */
|
||||
INTELLIGENT_VOICE_EVENT_COMMIT_ENROLL_COMPLETE = 3,
|
||||
};
|
||||
|
||||
enum WakeupIntelligentVoiceEventType {
|
||||
/* Wakeup None. */
|
||||
INTELLIGENT_VOICE_EVENT_WAKEUP_NONE = 0,
|
||||
/* Recognize complete. */
|
||||
INTELLIGENT_VOICE_EVENT_RECOGNIZE_COMPLETE = 1,
|
||||
};
|
||||
|
||||
enum IntelligentVoiceErrorCode {
|
||||
/* Success. */
|
||||
INTELLIGENT_VOICE_SUCCESS = 0,
|
||||
/* Memory is insufficient. */
|
||||
INTELLIGENT_VOICE_NO_MEMORY = 22700101,
|
||||
/* Invalid parameter. */
|
||||
INTELLIGENT_VOICE_INVALID_PARAM = 22700102,
|
||||
/* Init failed. */
|
||||
INTELLIGENT_VOICE_INIT_FAILED = 22700103,
|
||||
/* Enroll failed. */
|
||||
INTELLIGENT_VOICE_ENROLL_FAILED = 22700104,
|
||||
/* Commit enroll failed. */
|
||||
INTELLIGENT_VOICE_COMMIT_ENROLL_FAILED = 22700105,
|
||||
/* Recognize failed. */
|
||||
INTELLIGENT_VOICE_RECOGNIZE_FAILED = 22700106,
|
||||
};
|
||||
|
||||
struct WakeupHapInfo {
|
||||
std::string bundleName;
|
||||
std::string abilityName;
|
||||
};
|
||||
} // namespace IntellVoice
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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_MANAGER_H
|
||||
#define INTELL_VOICE_MANAGER_H
|
||||
|
||||
#include "i_intell_voice_engine.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoice {
|
||||
using namespace std;
|
||||
using namespace OHOS::IntellVoiceEngine;
|
||||
|
||||
class IntellVoiceManager {
|
||||
public:
|
||||
static IntellVoiceManager *GetInstance();
|
||||
|
||||
int32_t CreateIntellVoiceEngine(IntellVoiceEngineType type, sptr<IIntellVoiceEngine> &inst);
|
||||
int32_t ReleaseIntellVoiceEngine(IntellVoiceEngineType type);
|
||||
|
||||
private:
|
||||
IntellVoiceManager();
|
||||
~IntellVoiceManager();
|
||||
|
||||
void Init();
|
||||
};
|
||||
} // namespace IntellVoice
|
||||
} // namespace OHOS
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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 WAKEUP_INTELL_VOICE_ENGINE_H
|
||||
#define WAKEUP_INTELL_VOICE_ENGINE_H
|
||||
|
||||
#include "intell_voice_info.h"
|
||||
#include "i_intell_voice_engine.h"
|
||||
#include "engine_callback_inner.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoice {
|
||||
using OHOS::IntellVoiceEngine::IIntellVoiceEngineEventCallback;
|
||||
using OHOS::IntellVoiceEngine::IIntellVoiceEngine;
|
||||
using OHOS::IntellVoiceEngine::EngineCallbackInner;
|
||||
|
||||
struct WakeupIntelligentVoiceEngineDescriptor {
|
||||
bool needApAlgEngine;
|
||||
std::string wakeupPhrase;
|
||||
};
|
||||
|
||||
class WakeupIntellVoiceEngine {
|
||||
public:
|
||||
WakeupIntellVoiceEngine(const WakeupIntelligentVoiceEngineDescriptor &descriptor);
|
||||
~WakeupIntellVoiceEngine();
|
||||
|
||||
int32_t SetSensibility(const int32_t &sensibility);
|
||||
int32_t SetWakeupHapInfo(const WakeupHapInfo &info);
|
||||
int32_t SetParameter(const std::string &key, const std::string &value);
|
||||
int32_t GetParameter(const std::string &key);
|
||||
int32_t Release();
|
||||
int32_t SetCallback(std::shared_ptr<IIntellVoiceEngineEventCallback> callback);
|
||||
|
||||
private:
|
||||
sptr<IIntellVoiceEngine> engine_ = nullptr;
|
||||
std::unique_ptr<WakeupIntelligentVoiceEngineDescriptor> descriptor_ = nullptr;
|
||||
sptr<EngineCallbackInner> callback_ = nullptr;
|
||||
};
|
||||
} // namespace IntellVoice
|
||||
} // namespace OHOS
|
||||
|
||||
#endif
|
||||
+823
@@ -0,0 +1,823 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import { AsyncCallback, Callback } from './@ohos.base';
|
||||
|
||||
/**
|
||||
* @namespace intelligentVoice
|
||||
* @since 10
|
||||
*/
|
||||
declare namespace intelligentVoice {
|
||||
/**
|
||||
* Obtains an {@link IntelligentVoiceManager} instance.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @returns { IntelligentVoiceManager } this {@link IntelligentVoiceManager} object.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 22700101 - No memory.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
function getIntelligentVoiceManager(): IntelligentVoiceManager;
|
||||
|
||||
/**
|
||||
* Implements intelligent voice management.
|
||||
* @typedef IntelligentVoiceManager
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
interface IntelligentVoiceManager {
|
||||
/**
|
||||
* Obtains capability information.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @returns { Array<IntelligentVoiceEngineType> } array of supported IntelligentVoiceEngineType.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
getCapabilityInfo(): Array<IntelligentVoiceEngineType>;
|
||||
/**
|
||||
* Subscribes service change events. When the state of intelligent voice service changes,
|
||||
* the callback is invoked.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { string } type - Type of the event to listen for. Only the serviceChange event is supported.
|
||||
* @param { Callback<ServiceChangeType>Callback<ServiceChangeType> } callback - Callback is invoked when the event is triggered.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
on(type: 'serviceChange', callback: Callback<ServiceChangeType>): void;
|
||||
/**
|
||||
* Unsubscribes service change events.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { string } type - Type of the event to listen for. Only the serviceChange event is supported.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
off(type: 'serviceChange'): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enumerates service change type.
|
||||
* @enum {number}
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
enum ServiceChangeType {
|
||||
/**
|
||||
* Service unavailable.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
SERVICE_UNAVAILABLE = 0,
|
||||
}
|
||||
|
||||
/**
|
||||
* Enumerates intelligent voice engine type.
|
||||
* @enum {number}
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
enum IntelligentVoiceEngineType {
|
||||
/**
|
||||
* Enroll engine.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
ENROLL_ENGINE_TYPE = 0,
|
||||
/**
|
||||
* Wakeup engine.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
WAKEUP_ENGINE_TYPE = 1,
|
||||
/**
|
||||
* Update engine.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
UPDATE_ENGINE_TYPE = 2,
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes enroll intelligent voice engine.
|
||||
* @typedef EnrollIntelligentVoiceEngineDescriptor
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
interface EnrollIntelligentVoiceEngineDescriptor {
|
||||
/**
|
||||
* Wakeup phrase.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
wakeupPhrase: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes wakeup intelligent voice engine.
|
||||
* @typedef WakeupIntelligentVoiceEngineDescriptor
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
interface WakeupIntelligentVoiceEngineDescriptor {
|
||||
/**
|
||||
* Need ap algorithm engine.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
needApAlgEngine: boolean;
|
||||
/**
|
||||
* Wakeup phrase.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
wakeupPhrase: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains an {@link EnrollIntelligentVoiceEngine} instance. This method uses an asynchronous callback to return the EnrollIntelligentVoiceEngine instance.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { EnrollIntelligentVoiceEngineDescriptor } descriptor - descriptor indicates enroll intelligent voice engine descriptor.
|
||||
* @param { AsyncCallback<EnrollIntelligentVoiceEngine> } callback - the callback used to return the EnrollIntelligentVoiceEngine instance.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700101 - No memory.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
function createEnrollIntelligentVoiceEngine(descriptor: EnrollIntelligentVoiceEngineDescriptor, callback: AsyncCallback<EnrollIntelligentVoiceEngine>): void;
|
||||
|
||||
/**
|
||||
* Obtains an {@link EnrollIntelligentVoiceEngine} instance. This method uses a promise to return the EnrollIntelligentVoiceEngine instance.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { EnrollIntelligentVoiceEngineDescriptor } descriptor - descriptor indicates enroll intelligent voice engine descriptor.
|
||||
* @returns { Promise<EnrollIntelligentVoiceEngine>; } the promise used to return the EnrollIntelligentVoiceEngine instance.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700101 - No memory.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
function createEnrollIntelligentVoiceEngine(descriptor: EnrollIntelligentVoiceEngineDescriptor): Promise<EnrollIntelligentVoiceEngine>;
|
||||
|
||||
/**
|
||||
* Obtains an {@link WakeupIntelligentVoiceEngine} instance. This method uses an asynchronous callback to return the WakeupIntelligentVoiceEngine instance.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { WakeupIntelligentVoiceEngineDescriptor } descriptor - descriptor indicates wakeup intelligent voice engine descriptor.
|
||||
* @param { AsyncCallback<WakeupIntelligentVoiceEngine> } callback - the callback used to return the WakeupIntelligentVoiceEngine instance.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700101 - No memory.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
function createWakeupIntelligentVoiceEngine(descriptor: WakeupIntelligentVoiceEngineDescriptor, callback: AsyncCallback<WakeupIntelligentVoiceEngine>): void;
|
||||
|
||||
/**
|
||||
* Obtains an {@link WakeupIntelligentVoiceEngine} instance. This method uses a promise to return the WakeupIntelligentVoiceEngine instance.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { WakeupIntelligentVoiceEngineDescriptor } descriptor - descriptor indicates wakeup intelligent voice engine descriptor.
|
||||
* @returns { Promise<WakeupIntelligentVoiceEngine> } the promise used to return the WakeupIntelligentVoiceEngine instance.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700101 - No memory.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
function createWakeupIntelligentVoiceEngine(descriptor: WakeupIntelligentVoiceEngineDescriptor): Promise<WakeupIntelligentVoiceEngine>;
|
||||
|
||||
/**
|
||||
* Describes enroll engine config.
|
||||
* @typedef EnrollEngineConfig
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
interface EnrollEngineConfig {
|
||||
/**
|
||||
* Language that enroll engine supports.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
language: string;
|
||||
/**
|
||||
* Region that enroll engine supports.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
region: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enumerates sensibility type.
|
||||
* @enum {number}
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
enum SensibilityType {
|
||||
/**
|
||||
* Low sensibility.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
LOW_SENSIBILITY = 1,
|
||||
/**
|
||||
* Middle sensibility.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
MIDDLE_SENSIBILITY = 2,
|
||||
/**
|
||||
* High sensibility.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
HIGH_SENSIBILITY = 3,
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes wakeup hap information.
|
||||
* @typedef WakeupHapInfo
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
interface WakeupHapInfo {
|
||||
/**
|
||||
* Bundle name.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
bundleName: string;
|
||||
/**
|
||||
* Ability name.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
abilityName: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enumerates enroll intelligent voice event type.
|
||||
* @enum {number}
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
enum EnrollIntelligentVoiceEventType {
|
||||
/**
|
||||
* Enroll None.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
INTELLIGENT_VOICE_EVENT_ENROLL_NONE = 0,
|
||||
/**
|
||||
* Init done.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
INTELLIGENT_VOICE_EVENT_ENROLL_INIT_DONE = 1,
|
||||
/**
|
||||
* Enroll complete.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
INTELLIGENT_VOICE_EVENT_ENROLL_COMPLETE = 2,
|
||||
/**
|
||||
* Commit enroll complete.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
INTELLIGENT_VOICE_EVENT_COMMIT_ENROLL_COMPLETE = 3,
|
||||
}
|
||||
|
||||
/**
|
||||
* Enumerates wakeup intelligent voice event type.
|
||||
* @enum {number}
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
enum WakeupIntelligentVoiceEventType {
|
||||
/**
|
||||
* Wakeup None.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
INTELLIGENT_VOICE_EVENT_WAKEUP_NONE = 0,
|
||||
/**
|
||||
* Recognize complete.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
INTELLIGENT_VOICE_EVENT_RECOGNIZE_COMPLETE = 1,
|
||||
}
|
||||
|
||||
/**
|
||||
* Enumerates intelligent voice error code.
|
||||
* @enum {number}
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
enum IntelligentVoiceErrorCode {
|
||||
/**
|
||||
* Success.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
INTELLIGENT_VOICE_SUCCESS = 0,
|
||||
/**
|
||||
* Memory is insufficient.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
INTELLIGENT_VOICE_NO_MEMORY = 22700101,
|
||||
/**
|
||||
* Invalid parameter.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
INTELLIGENT_VOICE_INVALID_PARAM = 22700102,
|
||||
/**
|
||||
* Init failed.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
INTELLIGENT_VOICE_INIT_FAILED = 22700103,
|
||||
/**
|
||||
* Enroll failed.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
INTELLIGENT_VOICE_ENROLL_FAILED = 22700104,
|
||||
/**
|
||||
* Commit enroll failed.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
INTELLIGENT_VOICE_COMMIT_ENROLL_FAILED = 22700105,
|
||||
/**
|
||||
* Recognize failed.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
INTELLIGENT_VOICE_RECOGNIZE_FAILED = 22700106,
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes enrol intelligent voice engine callback information.
|
||||
* @typedef EnrollIntelligentVoiceEngineCallbackInfo
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
interface EnrollIntelligentVoiceEngineCallbackInfo {
|
||||
/**
|
||||
* Enroll event id.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
eventId: EnrollIntelligentVoiceEventType;
|
||||
/**
|
||||
* Error code.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
errCode: IntelligentVoiceErrorCode;
|
||||
/**
|
||||
* Describes enroll event context.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
context: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes wakeup intelligent voice engine callback information.
|
||||
* @typedef WakeupIntelligentVoiceEngineCallbackInfo
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
interface WakeupIntelligentVoiceEngineCallbackInfo {
|
||||
/**
|
||||
* Wakeup event id.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
eventId: WakeupIntelligentVoiceEventType;
|
||||
/**
|
||||
* Error code.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
errCode: IntelligentVoiceErrorCode;
|
||||
/**
|
||||
* Describes wakeup event context.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
context: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements enroll intelligent voice engine.
|
||||
* @typedef EnrollIntelligentVoiceEngine
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
interface EnrollIntelligentVoiceEngine {
|
||||
/**
|
||||
* Obtains the supported regions, This method uses an asynchronous callback to return the query result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { AsyncCallback<Array<string>> } callback - the callback used to return the supported regions.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
getSupportedRegions(callback: AsyncCallback<Array<string>>): void;
|
||||
/**
|
||||
* Obtains the supported regions, This method uses a promise to return the query result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @returns { Promise<Array<string>> } the promise used to return the supported regions.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
getSupportedRegions(): Promise<Array<string>>;
|
||||
/**
|
||||
* Initials the engine, This method uses an asynchronous callback to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { EnrollEngineConfig } config - config indicates enroll engine configuration.
|
||||
* @param { AsyncCallback<EnrollIntelligentVoiceEngineCallbackInfo> } callback - the callback used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
init(config: EnrollEngineConfig, callback: AsyncCallback<EnrollIntelligentVoiceEngineCallbackInfo>): void;
|
||||
/**
|
||||
* Initials the engine, This method uses a promise to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { EnrollEngineConfig } config - config indicates enroll engine configuration.
|
||||
* @returns { Promise<EnrollIntelligentVoiceEngineCallbackInfo> } the promise used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
init(config: EnrollEngineConfig): Promise<EnrollIntelligentVoiceEngineCallbackInfo>;
|
||||
/**
|
||||
* Starts the engine, This method uses an asynchronous callback to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { boolean } isLast - isLast indicates if it is the last time to start.
|
||||
* @param { AsyncCallback<EnrollIntelligentVoiceEngineCallbackInfo> } callback - the callback used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
start(isLast: boolean, callback: AsyncCallback<EnrollIntelligentVoiceEngineCallbackInfo>): void;
|
||||
/**
|
||||
* Starts the engine, This method uses a promise to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { boolean } isLast - isLast indicates if it is the last time to start.
|
||||
* @returns { Promise<EnrollIntelligentVoiceEngineCallbackInfo> } the promise used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
start(isLast: boolean): Promise<EnrollIntelligentVoiceEngineCallbackInfo>;
|
||||
/**
|
||||
* Stops the engine, This method uses an asynchronous callback to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { AsyncCallback<void> } callback - the callback used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
stop(callback: AsyncCallback<void>): void;
|
||||
/**
|
||||
* Stops the engine, This method uses a promise to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @returns { Promise<void> } the promise used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
stop(): Promise<void>;
|
||||
/**
|
||||
* commit enroll, This method uses an asynchronous callback to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { AsyncCallback<EnrollIntelligentVoiceEngineCallbackInfo> } callback - the callback used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
commit(callback: AsyncCallback<EnrollIntelligentVoiceEngineCallbackInfo>): void;
|
||||
/**
|
||||
* commit enroll, This method uses a promise to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @returns { Promise<EnrollIntelligentVoiceEngineCallbackInfo> } the promise used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
commit(): Promise<EnrollIntelligentVoiceEngineCallbackInfo>;
|
||||
/**
|
||||
* Sets wakeup hap information, This method uses an asynchronous callback to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { WakeupHapInfo } info - info indicates wakeup hap information.
|
||||
* @param { AsyncCallback<void> } callback - the callback used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
setWakeupHapInfo(info: WakeupHapInfo, callback: AsyncCallback<void>): void;
|
||||
/**
|
||||
* Sets wakeup hap information, This method uses a promise to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { WakeupHapInfo } info - info indicates wakeup hap information.
|
||||
* @returns { Promise<void> } the promise used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
setWakeupHapInfo(info: WakeupHapInfo): Promise<void>;
|
||||
/**
|
||||
* Sets sensibility, This method uses an asynchronous callback to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { SensibilityType } sensibility - sensibility to set.
|
||||
* @param { AsyncCallback<void> } callback - the callback used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
setSensibility(sensibility: SensibilityType, callback: AsyncCallback<void>): void;
|
||||
/**
|
||||
* Sets sensibility, This method uses a promise to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { SensibilityType } sensibility - sensibility to set.
|
||||
* @returns { Promise<void> } the promise used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
setSensibility(sensibility: SensibilityType): Promise<void>;
|
||||
/**
|
||||
* Sets an intelligent voice parameter. This method uses an asynchronous callback to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { string } key - the key of the intelligent voice parameter to set.
|
||||
* @param { string } value - the value of the intelligent voice parameter to set.
|
||||
* @param { AsyncCallback<void> } callback - the callback used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
setParameter(key: string, value: string, callback: AsyncCallback<void>): void;
|
||||
/**
|
||||
* Sets an intelligent voice parameter. This method uses a promise to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { string } key - the key of the intelligent voice parameter to set.
|
||||
* @param { string } value - the value of the intelligent voice parameter to set.
|
||||
* @returns { Promise<void> } the promise used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
setParameter(key: string, value: string): Promise<void>;
|
||||
/**
|
||||
* Obtains the value of an intelligent voice parameter. This method uses an asynchronous callback to return the query result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { string } key - the key of the intelligent voice parameter whose value is to be obtained.
|
||||
* @param { AsyncCallback<string> } callback - the callback used to return the value of the intelligent voice parameter.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
getParameter(key: string, callback: AsyncCallback<string>): void;
|
||||
/**
|
||||
* Obtains the value of an intelligent voice parameter. This method uses an asynchronous callback to return the query result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { string } key - the key of the intelligent voice parameter whose value is to be obtained.
|
||||
* @returns { Promise<string> } the promise used to return the value of the intelligent voice parameter.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
getParameter(key: string): Promise<string>;
|
||||
/**
|
||||
* Releases the engine, This method uses an asynchronous callback to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { AsyncCallback<void> } callback - the callback used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
release(callback: AsyncCallback<void>): void;
|
||||
/**
|
||||
* Releases the engine, This method uses a promise to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @returns { Promise<void> } the promise used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
release(): Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements wakeup intelligent voice engine.
|
||||
* @typedef WakeupIntelligentVoiceEngine
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
interface WakeupIntelligentVoiceEngine {
|
||||
/**
|
||||
* Obtains the supported regions, This method uses an asynchronous callback to return the query result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { AsyncCallback<Array<string>> } callback - the callback used to return the supported regions.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
getSupportedRegions(callback: AsyncCallback<Array<string>>): void;
|
||||
/**
|
||||
* Obtains the supported regions, This method uses a promise to return the query result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @returns { Promise<Array<string>> } the promise used to return the supported regions.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
getSupportedRegions(): Promise<Array<string>>;
|
||||
/**
|
||||
* Sets wakeup hap information, This method uses an asynchronous callback to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { WakeupHapInfo } info - info indicates wakeup hap information.
|
||||
* @param { AsyncCallback<void> } callback - the callback used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
setWakeupHapInfo(info: WakeupHapInfo, callback: AsyncCallback<void>): void;
|
||||
/**
|
||||
* Sets wakeup hap information, This method uses a promise to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { WakeupHapInfo } info - info indicates wakeup hap information.
|
||||
* @returns { Promise<void> } the promise used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
setWakeupHapInfo(info: WakeupHapInfo): Promise<void>;
|
||||
/**
|
||||
* Sets sensibility, This method uses an asynchronous callback to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { SensibilityType } sensibility - sensibility to set.
|
||||
* @param { AsyncCallback<void> } callback - the callback used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
setSensibility(sensibility: SensibilityType, callback: AsyncCallback<void>): void;
|
||||
/**
|
||||
* Sets sensibility, This method uses a promise to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { SensibilityType } sensibility - sensibility to set.
|
||||
* @returns { Promise<void> } the promise used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
setSensibility(sensibility: SensibilityType): Promise<void>;
|
||||
/**
|
||||
* Sets an intelligent voice parameter. This method uses an asynchronous callback to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { string } key - the key of the intelligent voice parameter to set.
|
||||
* @param { string } value - the value of the intelligent voice parameter to set.
|
||||
* @param { AsyncCallback<void> } callback - the callback used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
setParameter(key: string, value: string, callback: AsyncCallback<void>): void;
|
||||
/**
|
||||
* Sets an intelligent voice parameter. This method uses a promise to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { string } key - the key of the intelligent voice parameter to set.
|
||||
* @param { string } value - the value of the intelligent voice parameter to set.
|
||||
* @returns { Promise<void> } the promise used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
setParameter(key: string, value: string): Promise<void>;
|
||||
/**
|
||||
* Obtains the value of an intelligent voice parameter. This method uses an asynchronous callback to return the query result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { string } key - the key of the intelligent voice parameter whose value is to be obtained.
|
||||
* @param { AsyncCallback<string> } callback - the callback used to return the value of the intelligent voice parameter.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
getParameter(key: string, callback: AsyncCallback<string>): void;
|
||||
/**
|
||||
* Obtains the value of an intelligent voice parameter. This method uses an asynchronous callback to return the query result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { string } key - the key of the intelligent voice parameter whose value is to be obtained.
|
||||
* @returns { Promise<string> } the promise used to return the value of the intelligent voice parameter.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @throws { BusinessError } 401 - if input parameter type or number mismatch.
|
||||
* @throws { BusinessError } 22700102 - if input parameter value error.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
getParameter(key: string): Promise<string>;
|
||||
/**
|
||||
* Releases the engine, This method uses an asynchronous callback to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { AsyncCallback<void> } callback - the callback used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
release(callback: AsyncCallback<void>): void;
|
||||
/**
|
||||
* Releases the engine, This method uses a promise to return the result.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @returns { Promise<void> } the promise used to return the result.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
release(): Promise<void>;
|
||||
/**
|
||||
* Subscribes wakeup intelligent voice events. When wakeup intelligent voice events reach,
|
||||
* the callback is invoked.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { string } type - Type of the event to listen for. Only the wakeupIntelligentVoice event is supported.
|
||||
* @param { Callback<WakeupIntelligentVoiceEngineCallbackInfo> } callback - the callback invoked when the event is triggered.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
on(type: 'wakeupIntelligentVoiceEvent', callback: Callback<WakeupIntelligentVoiceEngineCallbackInfo>): void;
|
||||
/**
|
||||
* Unsubscribes wakeup intelligent voice events.
|
||||
* @permission ohos.permission.MANAGER_INTELLIGENT_VOICE
|
||||
* @param { string } type - Type of the event to listen for. Only the wakeupIntelligentVoice event is supported.
|
||||
* @throws { BusinessError } 201 - Permission denied.
|
||||
* @syscap SystemCapability.AI.IntelligentVoice.Core
|
||||
* @since 10
|
||||
*/
|
||||
off(type: 'wakeupIntelligentVoiceEvent');
|
||||
}
|
||||
}
|
||||
|
||||
export default intelligentVoice;
|
||||
Executable
+64
@@ -0,0 +1,64 @@
|
||||
# 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.
|
||||
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
|
||||
project(hdt VERSION 1.0 LANGUAGES C CXX)
|
||||
|
||||
# Library file compilation
|
||||
add_subdirectory(depend_libs/cmake)
|
||||
|
||||
# COMPONENT CMAKE
|
||||
include("${CMAKE_CURRENT_SOURCE_DIR}/conf/comp.cmake")
|
||||
|
||||
# PATH
|
||||
set(ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../)
|
||||
set(CLANG_DIR ${ROOT_DIR}prebuilts/clang/host/linux-x86/clang-r353983c/)
|
||||
|
||||
# COMPILE_OPTIONS
|
||||
set(CMAKE_CXX_COMPILER "${CLANG_DIR}/bin/clang++")
|
||||
set(CMAKE_C_COMPILER "${CLANG_DIR}/bin/clang")
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
|
||||
|
||||
# INCLUDE_DIR
|
||||
add_compile_options(-include memory)
|
||||
include("${CMAKE_CURRENT_SOURCE_DIR}/conf/include.cmake")
|
||||
include_directories(${INCLUDE_DIRECTORIES})
|
||||
|
||||
|
||||
# TEST_SOURCE_FILES
|
||||
aux_source_directory(${TEST_SRC_DIR}/src/ TEST_SRC)
|
||||
aux_source_directory(${TEST_SRC_DIR}/src/statemgr TEST_SRC)
|
||||
|
||||
|
||||
# TEST_CASE_FILES
|
||||
aux_source_directory(testcase/intell_voice_utils TEST_CASE)
|
||||
|
||||
# STUB_FILE
|
||||
aux_source_directory(stub STUB_FILE)
|
||||
|
||||
# TARGET
|
||||
add_executable(intellvoicetest ${TEST_SRC} ${TEST_CASE} ${STUB_FILE} ${DEPEND_SRC})
|
||||
|
||||
add_dependencies(intellvoicetest hilog sec utilsbase)
|
||||
target_link_libraries(intellvoicetest hilog sec utilsbase)
|
||||
|
||||
# MESSAGE
|
||||
get_target_property(COMPILE_FLAGS intellvoicetest COMPILE_OPTIONS)
|
||||
get_target_property(LINK_FLAGS intellvoicetest LINK_OPTIONS)
|
||||
message(STATUS "Compiler id: ${CMAKE_CXX_COMPILER_ID}")
|
||||
message(STATUS "Compile flags: ${COMPILE_FLAGS}")
|
||||
message(STATUS "Link flags: ${LINK_FLAGS}")
|
||||
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#!/bin/bash
|
||||
# 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.
|
||||
|
||||
set -e
|
||||
|
||||
CUR_DIR=$(dirname $(readlink -f $0))
|
||||
ROOT_DIR=${CUR_DIR}/../../../../../
|
||||
CLANG_DIR=${ROOT_DIR}prebuilts/clang/host/linux-x86/clang-r353983c/bin
|
||||
|
||||
export PATH=$PATH:${CLANG_DIR}
|
||||
|
||||
hdt comp -i --compiler clang9.0.3
|
||||
hdt build -s on -c on
|
||||
Executable
+23
@@ -0,0 +1,23 @@
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
|
||||
project(lib_cmake VERSION 1.0 LANGUAGES C CXX)
|
||||
|
||||
# CMAKE_WITH_ALL_COMPONENTS
|
||||
|
||||
# PATH
|
||||
set(ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../../)
|
||||
set(CLANG_DIR ${ROOT_DIR}prebuilts/clang/host/linux-x86/clang-r353983c/)
|
||||
set(INTELL_VOICE_DIR ${ROOT_DIR}foundation/ai/intelligent_voice_framework/llt/hdt/)
|
||||
set(LIB_PATH ${INTELL_VOICE_DIR}/build)
|
||||
|
||||
# COMPILE_OPTIONS
|
||||
set(CMAKE_CXX_COMPILER "${CLANG_DIR}/bin/clang++")
|
||||
set(CMAKE_C_COMPILER "${CLANG_DIR}/bin/clang")
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
|
||||
add_compile_options(-Wno-c++11-narrowing)
|
||||
|
||||
include("libhilog.cmake")
|
||||
include("libsec.cmake")
|
||||
include("libutilsbase.cmake")
|
||||
Executable
+31
@@ -0,0 +1,31 @@
|
||||
# Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
|
||||
# Description: liblog.cmake
|
||||
# Create: 2022-8-15
|
||||
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
set(HILOG_DIR ${ROOT_DIR}base/hiviewdfx/hilog/)
|
||||
set(HILOG_SRC_DIR ${ROOT_DIR}base/hiviewdfx/hilog/frameworks/libhilog/)
|
||||
|
||||
include_directories(${ROOT_DIR}third_party/bounds_checking_function/include)
|
||||
include_directories(${HILOG_DIR}frameworks/include/)
|
||||
include_directories(${HILOG_DIR}frameworks/libhilog/include/)
|
||||
include_directories(${HILOG_DIR}frameworks/libhilog/socket/include/)
|
||||
include_directories(${HILOG_DIR}frameworks/libhilog/param/include/)
|
||||
include_directories(${HILOG_DIR}frameworks/libhilog/utils/include/)
|
||||
include_directories(${HILOG_DIR}frameworks/libhilog/vsnprintf/include/)
|
||||
include_directories(${HILOG_DIR}interfaces/native/innerkits/include/)
|
||||
|
||||
set(LIBRARY_OUTPUT_PATH ${LIB_PATH})
|
||||
set(MACRO_DEFINITION_LOG -D__LINUX__)
|
||||
|
||||
# TEST_SOURCE_FILES
|
||||
file(GLOB LOG_SRCS
|
||||
${HILOG_SRC_DIR}hilog.cpp
|
||||
${HILOG_SRC_DIR}hilog_printf.cpp
|
||||
${HILOG_SRC_DIR}utils/log_print.cpp
|
||||
${HILOG_SRC_DIR}utils/log_utils.cpp
|
||||
${HILOG_SRC_DIR}vsnprintf/vsnprintf_s_p.cpp)
|
||||
|
||||
add_library(hilog STATIC ${LOG_SRCS})
|
||||
target_compile_definitions(hilog PRIVATE ${MACRO_DEFINITION_LOG})
|
||||
set_target_properties(hilog PROPERTIES COMPILE_FLAGS "-w")
|
||||
Executable
+27
@@ -0,0 +1,27 @@
|
||||
# Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
|
||||
# Description: liblog.cmake
|
||||
# Create: 2022-8-15
|
||||
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
set(SEC_DIR ${ROOT_DIR}third_party/bounds_checking_function/)
|
||||
|
||||
include_directories(${SEC_DIR}include)
|
||||
|
||||
set(LIBRARY_OUTPUT_PATH ${LIB_PATH})
|
||||
|
||||
set(MACRO_DEFINITION_LOG
|
||||
-D__LINUX__
|
||||
-D_INC_STRING_S
|
||||
-D_INC_WCHAR_S
|
||||
-D_SECIMP=//
|
||||
-D_STDIO_S_DEFINED
|
||||
-D_INC_STDIO_S
|
||||
-D_INC_STDLIB_S
|
||||
-D_INC_MEMORY_S)
|
||||
|
||||
# TEST_SOURCE_FILES
|
||||
aux_source_directory(${SEC_DIR}/src SEC_SRCS)
|
||||
|
||||
add_library(sec STATIC ${SEC_SRCS})
|
||||
target_compile_definitions(sec PRIVATE ${MACRO_DEFINITION_LOG})
|
||||
set_target_properties(sec PROPERTIES COMPILE_FLAGS "-w")
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
# Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
|
||||
# Description: liblog.cmake
|
||||
# Create: 2022-8-15
|
||||
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
set(UTILSBASE_DIR ${ROOT_DIR}commonlibrary/c_utils/base/)
|
||||
|
||||
include_directories(${UTILSBASE_DIR}include)
|
||||
include_directories(${UTILSBASE_DIR}src)
|
||||
include_directories(${ROOT_DIR}kernel/linux-5.10-lts/make_output/usr/include/)
|
||||
|
||||
set(LIBRARY_OUTPUT_PATH ${LIB_PATH})
|
||||
|
||||
# TEST_SOURCE_FILES
|
||||
aux_source_directory(${UTILSBASE_DIR}/src UTILSBASE_DIR_SRCS)
|
||||
|
||||
file(GLOB SRC_BLACK_LIST
|
||||
${UTILSBASE_DIR}/src/file_ex.cpp)
|
||||
|
||||
list(REMOVE_ITEM UTILSBASE_DIR_SRCS ${SRC_BLACK_LIST})
|
||||
|
||||
add_library(utilsbase STATIC ${UTILSBASE_DIR_SRCS})
|
||||
target_compile_options(utilsbase PRIVATE -include functional -includelimits.h)
|
||||
set_target_properties(utilsbase PROPERTIES COMPILE_FLAGS "-w")
|
||||
Executable
+346
@@ -0,0 +1,346 @@
|
||||
/*
|
||||
* 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 "file_ex.h"
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <cstdio>
|
||||
#include <securec.h>
|
||||
#include <cstring>
|
||||
#include "directory_ex.h"
|
||||
#include "utils_log.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int MAX_FILE_LENGTH = 32 * 1024 * 1024;
|
||||
|
||||
namespace OHOS {
|
||||
bool LoadStringFromFile(const string& filePath, string& content)
|
||||
{
|
||||
ifstream file(filePath.c_str());
|
||||
if (!file.is_open()) {
|
||||
UTILS_LOGD("open file failed! filePath:%{public}s", filePath.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
file.seekg(0, ios::end);
|
||||
const long fileLength = file.tellg();
|
||||
if (fileLength > MAX_FILE_LENGTH) {
|
||||
UTILS_LOGD("invalid file length(%{public}ld)!", fileLength);
|
||||
return false;
|
||||
}
|
||||
|
||||
content.clear();
|
||||
file.seekg(0, ios::beg);
|
||||
copy(istreambuf_iterator<char>(file), istreambuf_iterator<char>(), back_inserter(content));
|
||||
return true;
|
||||
}
|
||||
|
||||
string GetFileNameByFd(const int fd)
|
||||
{
|
||||
if (fd <= 0) {
|
||||
return string();
|
||||
}
|
||||
|
||||
string fdPath = "/proc/self/fd/" + std::to_string(fd);
|
||||
char fileName[PATH_MAX + 1] = {0};
|
||||
|
||||
int ret = readlink(fdPath.c_str(), fileName, PATH_MAX);
|
||||
if (ret < 0 || ret > PATH_MAX) {
|
||||
UTILS_LOGD("Get fileName failed, ret is: %{public}d!", ret);
|
||||
return string();
|
||||
}
|
||||
fileName[ret] = '\0';
|
||||
return string(fileName);
|
||||
}
|
||||
|
||||
bool LoadStringFromFdToFile(int fd, string& content)
|
||||
{
|
||||
string fileName = GetFileNameByFd(fd);
|
||||
if (fileName.empty()) {
|
||||
UTILS_LOGD("LoadStringFromFd get file name by fd failed!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!LoadStringFromFile(fileName, content)) {
|
||||
UTILS_LOGE("LoadStringFromFd get string from file failed!");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LoadStringFromFd(int fd, string& content)
|
||||
{
|
||||
if (fd <= 0) {
|
||||
UTILS_LOGD("invalid fd:%{public}d", fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
const long fileLength = lseek(fd, 0, SEEK_END);
|
||||
if (fileLength > MAX_FILE_LENGTH) {
|
||||
UTILS_LOGE("invalid file length(%{public}ld)!", fileLength);
|
||||
return false;
|
||||
}
|
||||
|
||||
// lseek is not support the linux file node
|
||||
if (fileLength < 0) {
|
||||
return LoadStringFromFdToFile(fd, content);
|
||||
}
|
||||
|
||||
if (fileLength == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
content.resize(fileLength);
|
||||
int loc = lseek(fd, 0, SEEK_SET);
|
||||
if (loc == -1) {
|
||||
UTILS_LOGE("lseek file to begin failed!");
|
||||
return false;
|
||||
}
|
||||
|
||||
const long len = read(fd, content.data(), fileLength);
|
||||
if (len != fileLength) {
|
||||
UTILS_LOGE("the length read from file is not equal to fileLength!len:%{public}ld,fileLen:%{public}ld",
|
||||
len, fileLength);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SaveStringToFile(const std::string& filePath, const std::string& content, bool truncated /* = true */)
|
||||
{
|
||||
if (content.empty()) {
|
||||
UTILS_LOGI("content is empty, no need to save!");
|
||||
return true;
|
||||
}
|
||||
|
||||
ofstream file;
|
||||
if (truncated) {
|
||||
file.open(filePath.c_str(), ios::out | ios::trunc);
|
||||
} else {
|
||||
file.open(filePath.c_str(), ios::out | ios::app);
|
||||
}
|
||||
|
||||
if (!file.is_open()) {
|
||||
UTILS_LOGD("open file failed! filePath:%{private}s", filePath.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
file.write(content.c_str(), content.length());
|
||||
if (file.fail()) {
|
||||
UTILS_LOGE("write content to file failed!file:%{private}s, content:%{private}s",
|
||||
filePath.c_str(), content.c_str());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SaveStringToFd(int fd, const std::string& content)
|
||||
{
|
||||
if (fd <= 0) {
|
||||
UTILS_LOGD("invalid fd:%{public}d", fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (content.empty()) {
|
||||
UTILS_LOGI("content is empty, no need to save!");
|
||||
return true;
|
||||
}
|
||||
|
||||
const long len = write(fd, content.c_str(), content.length());
|
||||
if (len < 0) {
|
||||
UTILS_LOGE("write file failed!errno:%{public}d, err:%{public}s", errno, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (static_cast<unsigned long>(len) != content.length()) {
|
||||
UTILS_LOGE("the length write to file is not equal to fileLength!len:%{public}ld, fileLen:%{public}zu",
|
||||
len, content.length());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LoadBufferFromNodeFile(const string& filePath, vector<char>& content)
|
||||
{
|
||||
string realPath;
|
||||
if (!PathToRealPath(filePath, realPath)) {
|
||||
UTILS_LOGD("filePath to realPath failed! filePath:%{private}s", filePath.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
FILE *fp = fopen(realPath.c_str(), "r");
|
||||
if (fp == nullptr) {
|
||||
UTILS_LOGD("open file failed! filePath:%{private}s", realPath.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
int byteCount = 1;
|
||||
while (!feof(fp)) {
|
||||
if (byteCount > MAX_FILE_LENGTH) {
|
||||
UTILS_LOGE("LoadBufferFromNodeFile invalid file length(%{public}d)!", byteCount);
|
||||
fclose(fp);
|
||||
fp = nullptr;
|
||||
content.clear();
|
||||
return false;
|
||||
}
|
||||
char ch = fgetc(fp);
|
||||
content.push_back(ch);
|
||||
byteCount++;
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
fp = nullptr;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* load file to buffer. If the buffer is not empty,then overwrite */
|
||||
bool LoadBufferFromFile(const string& filePath, vector<char>& content)
|
||||
{
|
||||
ifstream file;
|
||||
file.open(filePath.c_str(), ios::in | ios::binary);
|
||||
if (!file.is_open()) {
|
||||
UTILS_LOGD("open file failed! filePath:%{private}s", filePath.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
file.seekg(0, std::ios::end);
|
||||
const long fileLength = file.tellg();
|
||||
if (fileLength > MAX_FILE_LENGTH) {
|
||||
UTILS_LOGD("invalid file length(%{public}ld)!", fileLength);
|
||||
return false;
|
||||
}
|
||||
|
||||
// lseek is not support the linux file node
|
||||
if (fileLength < 0) {
|
||||
return LoadBufferFromNodeFile(filePath, content);
|
||||
}
|
||||
|
||||
if (fileLength == 0) {
|
||||
content.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
file.seekg(0, std::ios::beg);
|
||||
if (file.fail()) {
|
||||
UTILS_LOGE("seekg file to begin failed!filePath:%{private}s", filePath.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
content.resize(fileLength);
|
||||
file.read(&content[0], fileLength);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SaveBufferToFile(const string& filePath, const vector<char>& content, bool truncated /* = true */)
|
||||
{
|
||||
if (content.empty()) {
|
||||
UTILS_LOGI("content is empty, no need to save!");
|
||||
return true;
|
||||
}
|
||||
|
||||
// if the file is not exist,create it first!
|
||||
ios_base::openmode mode = truncated ? (ios::out | ios::binary | ios::trunc) : (ios::out | ios::binary | ios::app);
|
||||
ofstream file;
|
||||
file.open(filePath.c_str(), mode);
|
||||
if (!file.is_open()) {
|
||||
UTILS_LOGD("open file failed! filePath:%{private}s, mode:%{private}d", filePath.c_str(), mode);
|
||||
return false;
|
||||
}
|
||||
|
||||
file.write(&content[0], content.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FileExists(const string& fileName)
|
||||
{
|
||||
return (access(fileName.c_str(), F_OK) == 0);
|
||||
}
|
||||
|
||||
bool StringExistsInFile(const string& fileName, const string& subStr, bool caseSensitive /* = true */)
|
||||
{
|
||||
if (subStr.empty()) {
|
||||
UTILS_LOGD("String is empty");
|
||||
return false;
|
||||
}
|
||||
|
||||
string str;
|
||||
if (!LoadStringFromFile(fileName, str)) {
|
||||
UTILS_LOGD("File load fail, filePath:%{private}s", fileName.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (caseSensitive) {
|
||||
return (str.find(subStr) != string::npos);
|
||||
}
|
||||
|
||||
string strlower(str);
|
||||
string sublower(subStr);
|
||||
transform(str.begin(), str.end(), strlower.begin(), ::tolower);
|
||||
transform(subStr.begin(), subStr.end(), sublower.begin(), ::tolower);
|
||||
return (strlower.find(sublower) != string::npos);
|
||||
}
|
||||
|
||||
int CountStrInStr(const string& str, const string& subStr)
|
||||
{
|
||||
if (subStr.empty()) {
|
||||
UTILS_LOGD("subStr is empty");
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t position = 0;
|
||||
int count = 0;
|
||||
size_t length = subStr.length();
|
||||
while ((position = str.find(subStr, position)) != string::npos) {
|
||||
position += length;
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
int CountStrInFile(const string& fileName, const string& subStr, bool caseSensitive /* = true */)
|
||||
{
|
||||
if (subStr.empty()) {
|
||||
UTILS_LOGD("String is empty");
|
||||
return -1;
|
||||
}
|
||||
|
||||
string str;
|
||||
if (!LoadStringFromFile(fileName, str)) {
|
||||
UTILS_LOGD("File load fail, filePath:%{private}s", fileName.c_str());
|
||||
return -1;
|
||||
}
|
||||
|
||||
// If case-insensitive, strings are converted to lowercase.
|
||||
if (caseSensitive) {
|
||||
return CountStrInStr(str, subStr);
|
||||
}
|
||||
|
||||
string strlower(str);
|
||||
string sublower(subStr);
|
||||
transform(str.begin(), str.end(), strlower.begin(), ::tolower);
|
||||
transform(subStr.begin(), subStr.end(), sublower.begin(), ::tolower);
|
||||
return CountStrInStr(strlower, sublower);
|
||||
}
|
||||
}
|
||||
Executable
+23
@@ -0,0 +1,23 @@
|
||||
#!/bin/bash
|
||||
# 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.
|
||||
|
||||
set -e
|
||||
|
||||
CUR_DIR=$(dirname $(readlink -f $0))
|
||||
CUR_DIR=$(cd ${CUR_DIR}; pwd)
|
||||
ROOT_DIR=${CUR_DIR}/../../../../../
|
||||
ROOT_DIR=$(cd ${ROOT_DIR}; pwd)
|
||||
|
||||
hdt run
|
||||
hdt report --args='--filter='${TESTING_SRC_DIR}' --gcov-executable "'${ROOT_DIR}/'prebuilts/clang/host/linux-x86/clang-r353983c/bin/llvm-cov gcov"'
|
||||
Executable
+29
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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 "message_parcel.h"
|
||||
|
||||
namespace OHOS {
|
||||
bool WriteFileDescriptor(int fd)
|
||||
{
|
||||
(void)fd;
|
||||
return false;
|
||||
}
|
||||
|
||||
int ReadFileDescriptor()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
} // namespace OHOS
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#define CATCH_CONFIG_MAIN
|
||||
#include "catch2/catch.hpp"
|
||||
|
||||
#include "time_util.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceUtils {
|
||||
TEST_CASE("GetCurrTime", "intell_voice_time") {
|
||||
enum TimeFormat format = TIME_FORMAT_DEFAULT;
|
||||
std::string str = TimeUtil::GetCurrTime(format);
|
||||
REQUIRE(str != "");
|
||||
|
||||
SECTION("continuous time format") {
|
||||
TimeFormat format = TIME_FORMAT_CONTINOUS;
|
||||
str = TimeUtil::GetCurrTime(format);
|
||||
REQUIRE(str != "");
|
||||
}
|
||||
SECTION("standard time format") {
|
||||
TimeFormat format = TIME_FORMAT_STANDARD;
|
||||
str = TimeUtil::GetCurrTime(format);
|
||||
REQUIRE(str != "");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
+25
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- 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.
|
||||
-->
|
||||
<info>
|
||||
<process>intell_voice_service</process>
|
||||
<systemability>
|
||||
<name>2688</name>
|
||||
<libpath>/system/lib64/libintell_voice_server.z.so</libpath>
|
||||
<run-on-create>true</run-on-create>
|
||||
<distributed>false</distributed>
|
||||
<dump-level>1</dump-level>
|
||||
</systemability>
|
||||
</info>
|
||||
Executable
+19
@@ -0,0 +1,19 @@
|
||||
# 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.
|
||||
|
||||
import("//build/ohos/sa_profile/sa_profile.gni")
|
||||
|
||||
ohos_sa_profile("intell_voice_service_sa_profile") {
|
||||
sources = [ "2688.xml" ]
|
||||
part_name = "intelligent_voice_framework"
|
||||
}
|
||||
Executable
+146
@@ -0,0 +1,146 @@
|
||||
# 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.
|
||||
|
||||
import("//build/ohos.gni")
|
||||
|
||||
ohos_shared_library("intell_voice_proxy") {
|
||||
sources = [
|
||||
"intell_voice_engine/proxy/engine_callback_inner.cpp",
|
||||
"intell_voice_engine/proxy/intell_voice_engine_callback_stub.cpp",
|
||||
"intell_voice_engine/proxy/intell_voice_engine_proxy.cpp",
|
||||
"intell_voice_engine/proxy/intell_voice_service_proxy.cpp",
|
||||
]
|
||||
|
||||
include_dirs = [
|
||||
"intell_voice_engine",
|
||||
"intell_voice_engine/proxy",
|
||||
"//foundation/ai/intelligent_voice_framework/utils",
|
||||
]
|
||||
|
||||
cflags_cc = [
|
||||
"-Wno-error=unused-parameter",
|
||||
"-DHILOG_ENABLE",
|
||||
"-DENABLE_DEBUG",
|
||||
]
|
||||
|
||||
public_deps = [ "//drivers/interface/intelligent_voice/engine/v1_0:intell_voice_engine_idl_headers" ]
|
||||
|
||||
external_deps = [
|
||||
"c_utils:utils",
|
||||
"hdf_core:libhdf_host",
|
||||
"hdf_core:libhdf_ipc_adapter",
|
||||
"hdf_core:libhdf_utils",
|
||||
"hdf_core:libhdi",
|
||||
"hiviewdfx_hilog_native:libhilog",
|
||||
"ipc:ipc_core",
|
||||
"safwk:system_ability_fwk",
|
||||
"samgr:samgr_proxy",
|
||||
]
|
||||
|
||||
subsystem_name = "ai"
|
||||
part_name = "intelligent_voice_framework"
|
||||
}
|
||||
|
||||
ohos_shared_library("intell_voice_server") {
|
||||
configs = [ "//build/config/compiler:exceptions" ]
|
||||
|
||||
sources = [
|
||||
"intell_voice_engine/server/base/adapter_callback_service.cpp",
|
||||
"intell_voice_engine/server/base/audio_source.cpp",
|
||||
"intell_voice_engine/server/base/engine_base.cpp",
|
||||
"intell_voice_engine/server/base/engine_factory.cpp",
|
||||
"intell_voice_engine/server/base/file_source.cpp",
|
||||
"intell_voice_engine/server/enroll/enroll_adapter_listener.cpp",
|
||||
"intell_voice_engine/server/enroll/enroll_engine.cpp",
|
||||
"intell_voice_engine/server/sa/intell_voice_engine_callback_proxy.cpp",
|
||||
"intell_voice_engine/server/sa/intell_voice_engine_stub.cpp",
|
||||
"intell_voice_engine/server/sa/intell_voice_service.cpp",
|
||||
"intell_voice_engine/server/sa/intell_voice_service_manager.cpp",
|
||||
"intell_voice_engine/server/sa/intell_voice_service_stub.cpp",
|
||||
"intell_voice_engine/server/utils/history_info_mgr.cpp",
|
||||
"intell_voice_engine/server/utils/service_db_helper.cpp",
|
||||
"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/wakeup_adapter_listener.cpp",
|
||||
"intell_voice_engine/server/wakeup/wakeup_engine.cpp",
|
||||
"intell_voice_trigger/server/connector_mgr/trigger_callback_impl.cpp",
|
||||
"intell_voice_trigger/server/connector_mgr/trigger_connector.cpp",
|
||||
"intell_voice_trigger/server/connector_mgr/trigger_connector_internal_impl.cpp",
|
||||
"intell_voice_trigger/server/connector_mgr/trigger_connector_internal_validation.cpp",
|
||||
"intell_voice_trigger/server/connector_mgr/trigger_connector_mgr.cpp",
|
||||
"intell_voice_trigger/server/trigger_base_type.cpp",
|
||||
"intell_voice_trigger/server/trigger_db_helper.cpp",
|
||||
"intell_voice_trigger/server/trigger_detector.cpp",
|
||||
"intell_voice_trigger/server/trigger_detector_callback.cpp",
|
||||
"intell_voice_trigger/server/trigger_detector_recognition_callback.cpp",
|
||||
"intell_voice_trigger/server/trigger_helper.cpp",
|
||||
"intell_voice_trigger/server/trigger_manager.cpp",
|
||||
"intell_voice_trigger/server/trigger_service.cpp",
|
||||
]
|
||||
|
||||
public_deps = [
|
||||
"//drivers/interface/intelligent_voice/engine/v1_0:intell_voice_engine_idl_headers",
|
||||
"//drivers/interface/intelligent_voice/trigger/v1_0:intell_voice_trigger_idl_headers",
|
||||
]
|
||||
|
||||
include_dirs = [
|
||||
"//foundation/multimedia/audio_framework/interfaces/inner_api/native/audiocommon/include",
|
||||
"//foundation/multimedia/audio_framework/interfaces/inner_api/native/audiocapturer/include",
|
||||
"//foundation/ai/intelligent_voice_framework/utils",
|
||||
"intell_voice_engine",
|
||||
"intell_voice_engine/server/base",
|
||||
"intell_voice_engine/server/enroll",
|
||||
"intell_voice_engine/server/sa",
|
||||
"intell_voice_engine/server/utils",
|
||||
"intell_voice_engine/server/wakeup",
|
||||
"intell_voice_trigger/server",
|
||||
"intell_voice_trigger/server/connector_mgr",
|
||||
]
|
||||
|
||||
cflags_cc = [
|
||||
"-Wno-error=unused-parameter",
|
||||
"-DHILOG_ENABLE",
|
||||
"-DENABLE_DEBUG",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"//drivers/interface/intelligent_voice/engine/v1_0:libintell_voice_engine_proxy_1.0",
|
||||
"//drivers/interface/intelligent_voice/trigger/v1_0:libintell_voice_trigger_proxy_1.0",
|
||||
"//foundation/ability/ability_runtime/frameworks/native/appkit:app_context",
|
||||
"//foundation/ability/ability_runtime/interfaces/inner_api/dataobs_manager:dataobs_manager",
|
||||
"//foundation/ai/intelligent_voice_framework/utils:intell_voice_utils",
|
||||
"//foundation/multimedia/audio_framework/frameworks/native/audiocapturer:audio_capturer",
|
||||
]
|
||||
|
||||
external_deps = [
|
||||
"ability_base:want",
|
||||
"ability_base:zuri",
|
||||
"ability_runtime:ability_manager",
|
||||
"access_token:libaccesstoken_sdk",
|
||||
"c_utils:utils",
|
||||
"common_event_service:cesfwk_innerkits",
|
||||
"data_share:datashare_consumer",
|
||||
"eventhandler:libeventhandler",
|
||||
"hdf_core:libhdi",
|
||||
"hiviewdfx_hilog_native:libhilog",
|
||||
"ipc:ipc_core",
|
||||
"kv_store:distributeddata_inner",
|
||||
"relational_store:native_rdb",
|
||||
"safwk:system_ability_fwk",
|
||||
"samgr:samgr_proxy",
|
||||
]
|
||||
|
||||
subsystem_name = "ai"
|
||||
part_name = "intelligent_voice_framework"
|
||||
}
|
||||
Executable
+21
@@ -0,0 +1,21 @@
|
||||
# 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.
|
||||
|
||||
import("//build/ohos.gni")
|
||||
|
||||
ohos_prebuilt_etc("intell_voice_service.rc") {
|
||||
source = "intell_voice_service.cfg"
|
||||
relative_install_dir = "init"
|
||||
subsystem_name = "ai"
|
||||
part_name = "intelligent_voice_framework"
|
||||
}
|
||||
Executable
+22
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"jobs" : [{
|
||||
"name" : "post-fs-data",
|
||||
"cmds" : [
|
||||
"mkdir /data/service/el1/public/database 0711 ddms ddms",
|
||||
"mkdir /data/service/el1/public/database/intell_voice_service_manager 02770 intell_voice ddms",
|
||||
"mkdir /data/data/intell_voice 0760 intell_voice intell_voice",
|
||||
"mkdir /data/data/intell_voice/pcm_data 0760 intell_voice intell_voice",
|
||||
"start intell_voice_service"
|
||||
]
|
||||
}
|
||||
],
|
||||
"services" : [{
|
||||
"name" : "intell_voice_service",
|
||||
"path" : ["/system/bin/sa_main", "/system/profile/intell_voice_service.xml"],
|
||||
"uid" : "intell_voice",
|
||||
"gid" : ["system","intell_voice"],
|
||||
"permission" : ["ohos.permission.MANAGE_INTELLIGENT_VOICE"],
|
||||
"disabled" : 1
|
||||
}
|
||||
]
|
||||
}
|
||||
Executable
+8
@@ -0,0 +1,8 @@
|
||||
on post-fs-data
|
||||
start intell_voice_service
|
||||
service intell_voice_service /system/bin/sa_main /system/profile/intell_voice_service.xml
|
||||
class z_core
|
||||
user system
|
||||
group system shell
|
||||
seclabel u:r:intell_voice_service:s0
|
||||
onrestart restart intell_voice_hal_1_0
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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 IINTELL_VOICE_ENGINE_H
|
||||
#define IINTELL_VOICE_ENGINE_H
|
||||
#include "iremote_broker.h"
|
||||
#include "i_intell_voice_engine_callback.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
enum IntellVoiceEngineType {
|
||||
INTELL_VOICE_ENROLL = 0,
|
||||
INTELL_VOICE_WAKEUP,
|
||||
INTELL_VOICE_UPDATE,
|
||||
ENGINE_TYPE_BUT
|
||||
};
|
||||
|
||||
struct IntellVoiceEngineInfo {
|
||||
std::string wakeupPhrase;
|
||||
bool isPcmFromExternal { false };
|
||||
int32_t minBufSize { 0 };
|
||||
int32_t sampleChannels { 0 };
|
||||
int32_t bitsPerSample { 0 };
|
||||
int32_t sampleRate { 0 };
|
||||
};
|
||||
|
||||
class IIntellVoiceEngine : public IRemoteBroker {
|
||||
public:
|
||||
DECLARE_INTERFACE_DESCRIPTOR(u"HDI.IntellVoice.Engine");
|
||||
|
||||
enum {
|
||||
INTELL_VOICE_ENGINE_SET_CALLBACK = 0,
|
||||
INTELL_VOICE_ENGINE_ATTACH,
|
||||
INTELL_VOICE_ENGINE_DETACH,
|
||||
INTELL_VOICE_ENGINE_SET_PARAMETER,
|
||||
INTELL_VOICE_ENGINE_GET_PARAMETER,
|
||||
INTELL_VOICE_ENGINE_START,
|
||||
INTELL_VOICE_ENGINE_STOP,
|
||||
INTELL_VOICE_ENGINE_WRITE_AUDIO
|
||||
};
|
||||
|
||||
virtual void SetCallback(sptr<IRemoteObject> IntellVoiceEngineStub) = 0;
|
||||
virtual int32_t Attach(const IntellVoiceEngineInfo &info) = 0;
|
||||
virtual int32_t Detach(void) = 0;
|
||||
virtual int32_t SetParameter(const std::string &keyValueList) = 0;
|
||||
virtual std::string GetParameter(const std::string &key) = 0;
|
||||
virtual int32_t Start(bool isLast) = 0;
|
||||
virtual int32_t Stop(void) = 0;
|
||||
virtual int32_t WriteAudio(const uint8_t *buffer, uint32_t size) = 0;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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 I_INTELL_VOICE_ENGINE_CALLBACK_H
|
||||
#define I_INTELL_VOICE_ENGINE_CALLBACK_H
|
||||
|
||||
#include "iremote_broker.h"
|
||||
#include "v1_0/intell_voice_engine_types.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
using OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineCallBackEvent;
|
||||
|
||||
class IIntellVoiceEngineEventCallback {
|
||||
public:
|
||||
virtual ~IIntellVoiceEngineEventCallback() = default;
|
||||
virtual void OnEvent(const IntellVoiceEngineCallBackEvent ¶m) = 0;
|
||||
};
|
||||
|
||||
class IIntelligentVoiceEngineCallback : public IRemoteBroker {
|
||||
public:
|
||||
virtual void OnIntellVoiceEngineEvent(const IntellVoiceEngineCallBackEvent &event) = 0;
|
||||
enum Code {
|
||||
ON_INTELL_VOICE_ENGINE_EVENT = 0,
|
||||
};
|
||||
|
||||
public:
|
||||
DECLARE_INTERFACE_DESCRIPTOR(u"IIntelligentVoiceEngineCallback");
|
||||
};
|
||||
} // namespace IntellVoice
|
||||
} // namespace OHOS
|
||||
#endif // I_INTELL_VOICE_ENGINE_CALLBACK_H
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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 IINTELL_VOICE_SERVICE_H
|
||||
#define IINTELL_VOICE_SERVICE_H
|
||||
#include "iremote_broker.h"
|
||||
#include "i_intell_voice_engine.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
class IIntellVoiceService : public IRemoteBroker {
|
||||
public:
|
||||
DECLARE_INTERFACE_DESCRIPTOR(u"IntellVoiceFramework.Service");
|
||||
|
||||
enum {
|
||||
HDI_INTELL_VOICE_SERVICE_CREATE_ENGINE = 0,
|
||||
HDI_INTELL_VOICE_SERVICE_RELEASE_ENGINE,
|
||||
};
|
||||
|
||||
virtual int32_t CreateIntellVoiceEngine(IntellVoiceEngineType type, sptr<IIntellVoiceEngine> &inst) = 0;
|
||||
virtual int32_t ReleaseIntellVoiceEngine(IntellVoiceEngineType type) = 0;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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 "engine_callback_inner.h"
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
using namespace OHOS::HDI::IntelligentVoice::Engine::V1_0;
|
||||
#define LOG_TAG "EngineCallbackInner"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
void EngineCallbackInner::OnIntellVoiceEngineEvent(const IntellVoiceEngineCallBackEvent &event)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("receive event");
|
||||
if (cb_ == nullptr) {
|
||||
INTELL_VOICE_LOG_INFO("cb is null");
|
||||
return;
|
||||
}
|
||||
cb_->OnEvent(event);
|
||||
}
|
||||
|
||||
void EngineCallbackInner::SetEngineEventCallback(std::shared_ptr<IIntellVoiceEngineEventCallback> cb)
|
||||
{
|
||||
cb_ = cb;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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 ENGINE_CALLBACK_INNER_H
|
||||
#define ENGINE_CALLBACK_INNER_H
|
||||
|
||||
#include <memory>
|
||||
#include "intell_voice_engine_callback_stub.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
class EngineCallbackInner final : public IntellVoiceEngineCallbackStub {
|
||||
public:
|
||||
void OnIntellVoiceEngineEvent(
|
||||
const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineCallBackEvent &event) override;
|
||||
void SetEngineEventCallback(std::shared_ptr<IIntellVoiceEngineEventCallback> cb);
|
||||
private:
|
||||
std::shared_ptr<IIntellVoiceEngineEventCallback> cb_;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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_engine_callback_stub.h"
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
#define LOG_TAG "IntellVoiceEngineCallbackStub"
|
||||
|
||||
using namespace OHOS::HDI::IntelligentVoice::Engine::V1_0;
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
int IntellVoiceEngineCallbackStub::OnRemoteRequest(
|
||||
uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("OnRemoteRequest");
|
||||
if (data.ReadInterfaceToken() != IIntelligentVoiceEngineCallback::GetDescriptor()) {
|
||||
INTELL_VOICE_LOG_ERROR("ReadInterfaceToken failed");
|
||||
return -1;
|
||||
}
|
||||
IIntelligentVoiceEngineCallback::Code msgCode = static_cast<IIntelligentVoiceEngineCallback::Code>(code);
|
||||
switch (msgCode) {
|
||||
case ON_INTELL_VOICE_ENGINE_EVENT: {
|
||||
OnIntellVoiceEngineEventInner(data);
|
||||
reply.WriteInt32(0);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
INTELL_VOICE_LOG_WARN("invalid msgCode:%{public}d", msgCode);
|
||||
reply.WriteInt32(-1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void IntellVoiceEngineCallbackStub::OnIntellVoiceEngineEventInner(MessageParcel &data)
|
||||
{
|
||||
IntellVoiceEngineCallBackEvent event = {
|
||||
.msgId = static_cast<IntellVoiceEngineMessageType>(data.ReadInt32()),
|
||||
.result = static_cast<IntellVoiceEngineErrors>(data.ReadInt32()),
|
||||
.info = data.ReadString(),
|
||||
};
|
||||
OnIntellVoiceEngineEvent(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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_ENGINE_CALLBACK_STUB_H
|
||||
#define INTELL_VOICE_ENGINE_CALLBACK_STUB_H
|
||||
|
||||
#include "iremote_stub.h"
|
||||
#include "i_intell_voice_engine_callback.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
class IntellVoiceEngineCallbackStub : public IRemoteStub<IIntelligentVoiceEngineCallback> {
|
||||
public:
|
||||
int OnRemoteRequest(uint32_t code, MessageParcel &data,
|
||||
MessageParcel &reply, MessageOption &option) override;
|
||||
private:
|
||||
void OnIntellVoiceEngineEventInner(MessageParcel &data);
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* 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_engine_proxy.h"
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
#define LOG_TAG "IntellVoiceEngineProxy"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
void IntellVoiceEngineProxy::SetCallback(sptr<IRemoteObject> object)
|
||||
{
|
||||
MessageParcel data;
|
||||
MessageParcel reply;
|
||||
MessageOption option;
|
||||
if (object == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("object is null");
|
||||
return;
|
||||
}
|
||||
|
||||
data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
|
||||
data.WriteRemoteObject(object);
|
||||
Remote()->SendRequest(INTELL_VOICE_ENGINE_SET_CALLBACK, data, reply, option);
|
||||
}
|
||||
|
||||
int32_t IntellVoiceEngineProxy::Attach(const IntellVoiceEngineInfo &info)
|
||||
{
|
||||
MessageParcel data;
|
||||
MessageParcel reply;
|
||||
MessageOption option;
|
||||
|
||||
data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
|
||||
data.WriteString(info.wakeupPhrase);
|
||||
data.WriteBool(info.isPcmFromExternal);
|
||||
data.WriteInt32(info.minBufSize);
|
||||
data.WriteInt32(info.sampleChannels);
|
||||
data.WriteInt32(info.bitsPerSample);
|
||||
data.WriteInt32(info.sampleRate);
|
||||
|
||||
Remote()->SendRequest(INTELL_VOICE_ENGINE_ATTACH, data, reply, option);
|
||||
|
||||
INTELL_VOICE_LOG_INFO("Attach call");
|
||||
return reply.ReadInt32();
|
||||
}
|
||||
|
||||
int32_t IntellVoiceEngineProxy::Detach(void)
|
||||
{
|
||||
MessageParcel data;
|
||||
MessageParcel reply;
|
||||
MessageOption option;
|
||||
|
||||
data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
|
||||
Remote()->SendRequest(INTELL_VOICE_ENGINE_DETACH, data, reply, option);
|
||||
return reply.ReadInt32();
|
||||
}
|
||||
|
||||
int32_t IntellVoiceEngineProxy::SetParameter(const std::string &keyValueList)
|
||||
{
|
||||
MessageParcel data;
|
||||
MessageParcel reply;
|
||||
MessageOption option;
|
||||
|
||||
data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
|
||||
data.WriteString(keyValueList);
|
||||
Remote()->SendRequest(INTELL_VOICE_ENGINE_SET_PARAMETER, data, reply, option);
|
||||
return reply.ReadInt32();
|
||||
}
|
||||
|
||||
std::string IntellVoiceEngineProxy::GetParameter(const std::string &key)
|
||||
{
|
||||
MessageParcel data;
|
||||
MessageParcel reply;
|
||||
MessageOption option;
|
||||
|
||||
data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
|
||||
data.WriteString(key);
|
||||
|
||||
Remote()->SendRequest(INTELL_VOICE_ENGINE_GET_PARAMETER, data, reply, option);
|
||||
return reply.ReadString();
|
||||
}
|
||||
|
||||
int32_t IntellVoiceEngineProxy::Start(bool isLast)
|
||||
{
|
||||
MessageParcel data;
|
||||
MessageParcel reply;
|
||||
MessageOption option;
|
||||
|
||||
data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
|
||||
|
||||
Remote()->SendRequest(INTELL_VOICE_ENGINE_START, data, reply, option);
|
||||
return reply.ReadInt32();
|
||||
}
|
||||
|
||||
int32_t IntellVoiceEngineProxy::Stop(void)
|
||||
{
|
||||
MessageParcel data;
|
||||
MessageParcel reply;
|
||||
MessageOption option;
|
||||
|
||||
data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
|
||||
|
||||
Remote()->SendRequest(INTELL_VOICE_ENGINE_STOP, data, reply, option);
|
||||
return reply.ReadInt32();
|
||||
}
|
||||
|
||||
int32_t IntellVoiceEngineProxy::WriteAudio(const uint8_t *buffer, uint32_t size)
|
||||
{
|
||||
MessageParcel data;
|
||||
MessageParcel reply;
|
||||
MessageOption option;
|
||||
|
||||
data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
|
||||
data.WriteUint32(size);
|
||||
data.WriteBuffer(buffer, size);
|
||||
|
||||
Remote()->SendRequest(INTELL_VOICE_ENGINE_WRITE_AUDIO, data, reply, option);
|
||||
return reply.ReadInt32();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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_ENGINE_PROXY_H
|
||||
#define INTELL_VOICE_ENGINE_PROXY_H
|
||||
|
||||
#include <iremote_proxy.h>
|
||||
#include "i_intell_voice_engine.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
class IntellVoiceEngineProxy : public IRemoteProxy<IIntellVoiceEngine> {
|
||||
public:
|
||||
explicit IntellVoiceEngineProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IIntellVoiceEngine>(impl) {};
|
||||
virtual ~IntellVoiceEngineProxy() {};
|
||||
void SetCallback(sptr<IRemoteObject> object) override;
|
||||
int32_t Attach(const IntellVoiceEngineInfo &info) override;
|
||||
int32_t Detach(void) override;
|
||||
int32_t SetParameter(const std::string &keyValueList) override;
|
||||
std::string GetParameter(const std::string &key) override;
|
||||
int32_t Start(bool isLast) override;
|
||||
int32_t Stop(void) override;
|
||||
int32_t WriteAudio(const uint8_t *buffer, uint32_t size) override;
|
||||
|
||||
private:
|
||||
static inline BrokerDelegator<IntellVoiceEngineProxy> delegator_;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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_service_proxy.h"
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
#define LOG_TAG "IntellVoiceServiceProxy"
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
int32_t IntellVoiceServiceProxy::CreateIntellVoiceEngine(IntellVoiceEngineType type, sptr<IIntellVoiceEngine> &inst)
|
||||
{
|
||||
MessageParcel data;
|
||||
MessageParcel reply;
|
||||
MessageOption option;
|
||||
|
||||
data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
|
||||
data.WriteInt32(static_cast<int>(type));
|
||||
|
||||
Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_CREATE_ENGINE, data, reply, option);
|
||||
|
||||
sptr<IRemoteObject> remote = reply.ReadRemoteObject();
|
||||
if (remote == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("Create engine failed, engine is null");
|
||||
return -1;
|
||||
}
|
||||
inst = iface_cast<IIntellVoiceEngine>(remote);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t IntellVoiceServiceProxy::ReleaseIntellVoiceEngine(IntellVoiceEngineType type)
|
||||
{
|
||||
MessageParcel data;
|
||||
MessageParcel reply;
|
||||
MessageOption option;
|
||||
|
||||
data.WriteInterfaceToken(IIntellVoiceService::GetDescriptor());
|
||||
data.WriteInt32(type);
|
||||
|
||||
Remote()->SendRequest(HDI_INTELL_VOICE_SERVICE_RELEASE_ENGINE, data, reply, option);
|
||||
return reply.ReadInt32();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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_SERVICE_PROXY_H
|
||||
#define INTELL_VOICE_SERVICE_PROXY_H
|
||||
|
||||
#include <iremote_proxy.h>
|
||||
#include "i_intell_voice_service.h"
|
||||
#include "i_intell_voice_engine.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
class IntellVoiceServiceProxy : public IRemoteProxy<IIntellVoiceService> {
|
||||
public:
|
||||
explicit IntellVoiceServiceProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IIntellVoiceService>(impl) {}
|
||||
virtual ~IntellVoiceServiceProxy() {};
|
||||
|
||||
int32_t CreateIntellVoiceEngine(IntellVoiceEngineType type, sptr<IIntellVoiceEngine> &inst) override;
|
||||
int32_t ReleaseIntellVoiceEngine(IntellVoiceEngineType type) override;
|
||||
|
||||
private:
|
||||
static inline BrokerDelegator<IntellVoiceServiceProxy> delegator_;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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 "adapter_callback_service.h"
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
#define LOG_TAG "AdapterCallbackService"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
int32_t AdapterCallbackService::OnIntellVoiceHdiEvent(const IntellVoiceEngineCallBackEvent& event)
|
||||
{
|
||||
if (listener_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("listener_ is nullptr");
|
||||
return -1;
|
||||
}
|
||||
INTELL_VOICE_LOG_INFO("send hdi event");
|
||||
listener_->OnIntellVoiceHdiEvent(event);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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 ADAPTER_CALLBACK_SERVICE_H
|
||||
#define ADAPTER_CALLBACK_SERVICE_H
|
||||
|
||||
#include "v1_0/iintell_voice_engine_callback.h"
|
||||
#include "intell_voice_adapter_listener.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
using OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineCallback;
|
||||
using OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineCallBackEvent;
|
||||
|
||||
class AdapterCallbackService final: public IIntellVoiceEngineCallback {
|
||||
public:
|
||||
AdapterCallbackService(std::shared_ptr<IntellVoiceAdapterListener> listener) : listener_(listener) {}
|
||||
virtual ~AdapterCallbackService() = default;
|
||||
|
||||
int32_t OnIntellVoiceHdiEvent(const IntellVoiceEngineCallBackEvent& event) override;
|
||||
private:
|
||||
std::shared_ptr<IntellVoiceAdapterListener> listener_ = nullptr;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
+182
@@ -0,0 +1,182 @@
|
||||
/*
|
||||
* 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 "audio_source.h"
|
||||
|
||||
#include "securec.h"
|
||||
#include "time_util.h"
|
||||
#include "intell_voice_log.h"
|
||||
#include "memory_guard.h"
|
||||
|
||||
using namespace OHOS::AudioStandard;
|
||||
using namespace OHOS::IntellVoiceUtils;
|
||||
#define LOG_TAG "AudioSource"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
static const std::string PCM_DIR = "/data/data/intell_voice/pcm_data/";
|
||||
|
||||
AudioSource::AudioSource(uint32_t minBufferSize, uint32_t bufferCnt,
|
||||
std::unique_ptr<AudioSourceListener> listener, const OHOS::AudioStandard::AudioCapturerOptions &capturerOptions)
|
||||
: minBufferSize_(minBufferSize), bufferCnt_(bufferCnt), listener_(std::move(listener))
|
||||
{
|
||||
capturerOptions_.streamInfo.samplingRate = capturerOptions.streamInfo.samplingRate;
|
||||
capturerOptions_.streamInfo.encoding = capturerOptions.streamInfo.encoding;
|
||||
capturerOptions_.streamInfo.format = capturerOptions.streamInfo.format;
|
||||
capturerOptions_.streamInfo.channels = capturerOptions.streamInfo.channels;
|
||||
capturerOptions_.capturerInfo.sourceType = capturerOptions.capturerInfo.sourceType;
|
||||
capturerOptions_.capturerInfo.capturerFlags = capturerOptions.capturerInfo.capturerFlags;
|
||||
|
||||
audioCapturer_ = AudioCapturer::Create(capturerOptions_);
|
||||
if (audioCapturer_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create audio capturer failed");
|
||||
}
|
||||
}
|
||||
|
||||
AudioSource::~AudioSource()
|
||||
{
|
||||
audioCapturer_ = nullptr;
|
||||
}
|
||||
|
||||
bool AudioSource::Start()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (audioCapturer_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("audioCapturer_ is nullptr");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (listener_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("listener_ is nullptr");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (minBufferSize_ == 0) {
|
||||
INTELL_VOICE_LOG_ERROR("minBufferSize_ is invalid");
|
||||
return false;
|
||||
}
|
||||
|
||||
buffer_ = std::shared_ptr<uint8_t>(new uint8_t[minBufferSize_], [](uint8_t *p) { delete[] p; });
|
||||
if (buffer_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("malloc buffer failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
auto path = PCM_DIR + TimeUtil::GetCurrTime() + ".pcm";
|
||||
fileStream_ = std::make_unique<std::ofstream>(path);
|
||||
if (fileStream_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("open debug record file failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!audioCapturer_->Start()) {
|
||||
INTELL_VOICE_LOG_ERROR("start audio capturer failed");
|
||||
fileStream_->close();
|
||||
fileStream_ = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
isReading_.store(true);
|
||||
std::thread t1(std::bind(&AudioSource::ReadThread, this));
|
||||
readThread_ = std::move(t1);
|
||||
return true;
|
||||
}
|
||||
|
||||
void AudioSource::ReadThread()
|
||||
{
|
||||
uint32_t readCnt = 0;
|
||||
bool isError = true;
|
||||
while (isReading_.load()) {
|
||||
if (readCnt >= bufferCnt_) {
|
||||
INTELL_VOICE_LOG_INFO("finish reading data");
|
||||
isError = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!Read()) {
|
||||
INTELL_VOICE_LOG_WARN("failed to read data");
|
||||
break;
|
||||
}
|
||||
++readCnt;
|
||||
}
|
||||
|
||||
if (listener_ != nullptr) {
|
||||
listener_->bufferEndCb_(isError);
|
||||
}
|
||||
}
|
||||
|
||||
bool AudioSource::Read()
|
||||
{
|
||||
size_t bytesRead = 0;
|
||||
while (bytesRead < minBufferSize_) {
|
||||
int32_t len = audioCapturer_->Read(*(buffer_.get() + bytesRead), minBufferSize_ - bytesRead, 0);
|
||||
if (len >= 0) {
|
||||
bytesRead += static_cast<uint32_t>(len);
|
||||
} else {
|
||||
INTELL_VOICE_LOG_ERROR("read data error, len is %{public}d", len);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (bytesRead != minBufferSize_) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to read data, bytesRead is %zu", bytesRead);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (fileStream_ != nullptr) {
|
||||
fileStream_->write(reinterpret_cast<char *>(buffer_.get()), minBufferSize_);
|
||||
}
|
||||
|
||||
if (listener_ != nullptr) {
|
||||
listener_->readBufferCb_(buffer_.get(), minBufferSize_);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void AudioSource::Stop()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (!isReading_.load()) {
|
||||
INTELL_VOICE_LOG_INFO("already stop");
|
||||
return;
|
||||
}
|
||||
|
||||
MemoryGuard memoryGuard;
|
||||
|
||||
isReading_.store(false);
|
||||
readThread_.join();
|
||||
|
||||
if (fileStream_ != nullptr) {
|
||||
fileStream_->close();
|
||||
fileStream_ = nullptr;
|
||||
}
|
||||
|
||||
if (audioCapturer_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("audioCapturer_ is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!audioCapturer_->Stop()) {
|
||||
INTELL_VOICE_LOG_ERROR("stop audio capturer error");
|
||||
}
|
||||
|
||||
if (!audioCapturer_->Release()) {
|
||||
INTELL_VOICE_LOG_ERROR("release audio capturer error");
|
||||
}
|
||||
|
||||
audioCapturer_ = nullptr;
|
||||
listener_ = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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 AUDIO_SOURCE_H
|
||||
#define AUDIO_SOURCE_H
|
||||
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <fstream>
|
||||
#include "audio_capturer.h"
|
||||
#include "audio_info.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
using OnReadBufferCb = std::function<void(uint8_t *buffer, uint32_t size)>;
|
||||
using OnBufferEndCb = std::function<void(bool isError)>;
|
||||
|
||||
struct AudioSourceListener {
|
||||
AudioSourceListener(OnReadBufferCb readBufferCb, OnBufferEndCb bufferEndCb)
|
||||
: readBufferCb_(readBufferCb), bufferEndCb_(bufferEndCb) {}
|
||||
OnReadBufferCb readBufferCb_;
|
||||
OnBufferEndCb bufferEndCb_;
|
||||
};
|
||||
|
||||
class AudioSource {
|
||||
public:
|
||||
AudioSource(uint32_t minBufferSize, uint32_t bufferCnt, std::unique_ptr<AudioSourceListener> listener,
|
||||
const OHOS::AudioStandard::AudioCapturerOptions &capturerOptions);
|
||||
~AudioSource();
|
||||
bool Start();
|
||||
void Stop();
|
||||
|
||||
private:
|
||||
void ReadThread();
|
||||
bool Read();
|
||||
|
||||
private:
|
||||
uint32_t minBufferSize_ = 0;
|
||||
uint32_t bufferCnt_ = 0;
|
||||
std::unique_ptr<AudioSourceListener> listener_ = nullptr;
|
||||
std::atomic<bool> isReading_ = false;
|
||||
std::thread readThread_;
|
||||
OHOS::AudioStandard::AudioCapturerOptions capturerOptions_;
|
||||
std::shared_ptr<uint8_t> buffer_ = nullptr;
|
||||
std::unique_ptr<std::ofstream> fileStream_ = nullptr;
|
||||
std::unique_ptr<OHOS::AudioStandard::AudioCapturer> audioCapturer_ = nullptr;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* 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 "engine_base.h"
|
||||
|
||||
#include "intell_voice_log.h"
|
||||
#include "string_util.h"
|
||||
|
||||
using namespace OHOS::IntellVoiceUtils;
|
||||
using namespace OHOS::HDI::IntelligentVoice::Engine::V1_0;
|
||||
#define LOG_TAG "EngineBase"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
EngineBase::EngineBase()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("constructor");
|
||||
desc_.adapterType = ADAPTER_TYPE_BUT;
|
||||
}
|
||||
|
||||
int32_t EngineBase::SetParameter(const std::string &keyValueList)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (adapter_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("adapter is nullptr");
|
||||
return -1;
|
||||
}
|
||||
return adapter_->SetParameter(keyValueList);
|
||||
}
|
||||
|
||||
std::string EngineBase::GetParameter(const std::string &key)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (adapter_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("adapter is nullptr");
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string value;
|
||||
adapter_->GetParameter(key, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
int32_t EngineBase::WriteAudio(const uint8_t *buffer, uint32_t size)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (adapter_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("adapter is nullptr");
|
||||
return -1;
|
||||
}
|
||||
std::vector<uint8_t> audioBuff(&buffer[0], &buffer[size]);
|
||||
return adapter_->WriteAudio(audioBuff);
|
||||
}
|
||||
|
||||
int32_t EngineBase::Stop()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (adapter_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("adapter is nullptr");
|
||||
return -1;
|
||||
}
|
||||
return adapter_->Stop();
|
||||
}
|
||||
|
||||
void EngineBase::SplitStringToKVPair(const std::string &inputStr, std::map<std::string, std::string> &kvpairs)
|
||||
{
|
||||
std::vector<std::string> paramsList;
|
||||
StringUtil::Split(inputStr, ";", paramsList);
|
||||
for (auto &it : paramsList) {
|
||||
std::string key;
|
||||
std::string value;
|
||||
if (StringUtil::SplitLineToPair(it, key, value)) {
|
||||
kvpairs[key] = value;
|
||||
INTELL_VOICE_LOG_INFO("key:%{public}s, value:%{public}s", key.c_str(), value.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* 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 ENGINE_BASE_H
|
||||
#define ENGINE_BASE_H
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include "intell_voice_engine_stub.h"
|
||||
#include "v1_0/iintell_voice_engine_adapter.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
enum EngineRelationType {
|
||||
CONCURRENCY_TYPE,
|
||||
PREEMPTION_TYPE,
|
||||
REPLACEMENT_TYPE,
|
||||
};
|
||||
|
||||
class EngineBase : public IntellVoiceEngineStub {
|
||||
public:
|
||||
~EngineBase() = default;
|
||||
virtual bool Init() = 0;
|
||||
int32_t SetParameter(const std::string &keyValueList) override;
|
||||
std::string GetParameter(const std::string &key) override;
|
||||
int32_t WriteAudio(const uint8_t *buffer, uint32_t size) override;
|
||||
int32_t Stop() override;
|
||||
virtual void OnDetected() {};
|
||||
bool IsRunning()
|
||||
{
|
||||
return isRunning_;
|
||||
}
|
||||
EngineRelationType GetEngineRelationType()
|
||||
{
|
||||
return type_;
|
||||
}
|
||||
int32_t GetPriority()
|
||||
{
|
||||
return priority_;
|
||||
}
|
||||
|
||||
protected:
|
||||
EngineBase();
|
||||
void SplitStringToKVPair(const std::string &inputStr, std::map<std::string, std::string> &kvpairs);
|
||||
std::mutex mutex_;
|
||||
sptr<OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineAdapter> adapter_ = nullptr;
|
||||
OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterDescriptor desc_;
|
||||
|
||||
bool isRunning_ = false;
|
||||
EngineRelationType type_ = CONCURRENCY_TYPE;
|
||||
int32_t priority_ = 0;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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 "engine_factory.h"
|
||||
#include "enroll_engine.h"
|
||||
#include "wakeup_engine.h"
|
||||
#include "intell_voice_log.h"
|
||||
#include "intell_voice_generic_factory.h"
|
||||
|
||||
using namespace OHOS::IntellVoiceUtils;
|
||||
#define LOG_TAG "EngineFactory"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
sptr<EngineBase> EngineFactory::CreateEngineInst(IntellVoiceEngineType type)
|
||||
{
|
||||
sptr<EngineBase> engine = nullptr;
|
||||
switch (type) {
|
||||
case INTELL_VOICE_ENROLL:
|
||||
engine = SptrFactory<EnrollEngine>::CreateInstance();
|
||||
break;
|
||||
case INTELL_VOICE_WAKEUP:
|
||||
engine = SptrFactory<WakeupEngine>::CreateInstance();
|
||||
break;
|
||||
case INTELL_VOICE_UPDATE:
|
||||
default:
|
||||
INTELL_VOICE_LOG_INFO("create engine enter, type:%d", type);
|
||||
break;
|
||||
}
|
||||
|
||||
return engine;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* 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 ENGINE_FACTORY_H
|
||||
#define ENGINE_FACTORY_H
|
||||
|
||||
#include "engine_base.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
class EngineFactory {
|
||||
public:
|
||||
EngineFactory() = default;
|
||||
~EngineFactory() = default;
|
||||
static sptr<EngineBase> CreateEngineInst(IntellVoiceEngineType type);
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* 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 "file_source.h"
|
||||
|
||||
#include "securec.h"
|
||||
#include "intell_voice_log.h"
|
||||
#include "memory_guard.h"
|
||||
|
||||
using namespace OHOS::IntellVoiceUtils;
|
||||
#define LOG_TAG "FileSource"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
FileSource::FileSource(uint32_t minBufferSize, uint32_t bufferCnt, const std::string &filePath,
|
||||
std::unique_ptr<FileSourceListener> listener)
|
||||
: minBufferSize_(minBufferSize), bufferCnt_(bufferCnt), filePath_(filePath), listener_(std::move(listener))
|
||||
{
|
||||
}
|
||||
|
||||
FileSource::~FileSource()
|
||||
{
|
||||
if (isReading_.load()) {
|
||||
isReading_.store(false);
|
||||
readThread_.join();
|
||||
}
|
||||
}
|
||||
|
||||
bool FileSource::Start()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (listener_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("listener_ is nullptr");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (minBufferSize_ == 0) {
|
||||
INTELL_VOICE_LOG_ERROR("minBufferSize_ is invalid");
|
||||
return false;
|
||||
}
|
||||
|
||||
buffer_ = std::shared_ptr<uint8_t>(new uint8_t[minBufferSize_], [](uint8_t *p) { delete[] p; });
|
||||
if (buffer_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("malloc buffer failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
fileIn_ = std::make_unique<std::ifstream>(filePath_, std::ios::binary);
|
||||
if (fileIn_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("open input file failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
fileIn_->seekg(0, fileIn_->end);
|
||||
uint32_t size = static_cast<uint32_t>(fileIn_->tellg());
|
||||
if (size < minBufferSize_ * bufferCnt_) {
|
||||
INTELL_VOICE_LOG_ERROR("file size:%{public}u is smaller than required", size);
|
||||
fileIn_->close();
|
||||
fileIn_ = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
isReading_.store(true);
|
||||
std::thread t1(std::bind(&FileSource::ReadThread, this));
|
||||
readThread_ = std::move(t1);
|
||||
return true;
|
||||
}
|
||||
|
||||
void FileSource::ReadThread()
|
||||
{
|
||||
uint32_t readCnt = 0;
|
||||
bool isError = true;
|
||||
if (fileIn_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("fileIn_ is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
fileIn_->seekg(0, fileIn_->beg);
|
||||
|
||||
while (isReading_.load()) {
|
||||
if (readCnt >= bufferCnt_) {
|
||||
INTELL_VOICE_LOG_INFO("finish reading data");
|
||||
isError = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!(fileIn_->read(reinterpret_cast<char *>(buffer_.get()), minBufferSize_))) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to read file");
|
||||
break;
|
||||
}
|
||||
|
||||
if (listener_ != nullptr) {
|
||||
listener_->fileBufferCb_(buffer_.get(), minBufferSize_);
|
||||
}
|
||||
|
||||
++readCnt;
|
||||
}
|
||||
|
||||
if (listener_ != nullptr) {
|
||||
listener_->fileEndCb_(isError);
|
||||
}
|
||||
}
|
||||
|
||||
void FileSource::Stop()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
MemoryGuard memoryGuard;
|
||||
if (isReading_.load()) {
|
||||
isReading_.store(false);
|
||||
readThread_.join();
|
||||
}
|
||||
|
||||
if (fileIn_ != nullptr) {
|
||||
fileIn_->close();
|
||||
fileIn_ = nullptr;
|
||||
}
|
||||
|
||||
buffer_ = nullptr;
|
||||
listener_ = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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 FILE_SOURCE_H
|
||||
#define FILE_SOURCE_H
|
||||
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <functional>
|
||||
#include <atomic>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
using OnFileBufferCb = std::function<void(uint8_t *buffer, uint32_t size)>;
|
||||
using OnFileEndCb = std::function<void(bool isError)>;
|
||||
|
||||
struct FileSourceListener {
|
||||
FileSourceListener(OnFileBufferCb fileBufferCb, OnFileEndCb fileEndCb)
|
||||
: fileBufferCb_(fileBufferCb), fileEndCb_(fileEndCb) {}
|
||||
OnFileBufferCb fileBufferCb_;
|
||||
OnFileEndCb fileEndCb_;
|
||||
};
|
||||
|
||||
class FileSource {
|
||||
public:
|
||||
FileSource(uint32_t minBufferSize, uint32_t bufferCnt, const std::string &filePath,
|
||||
std::unique_ptr<FileSourceListener> listener);
|
||||
~FileSource();
|
||||
bool Start();
|
||||
void Stop();
|
||||
|
||||
private:
|
||||
void ReadThread();
|
||||
bool Read();
|
||||
|
||||
private:
|
||||
uint32_t minBufferSize_ = 0;
|
||||
uint32_t bufferCnt_ = 0;
|
||||
std::string filePath_;
|
||||
std::unique_ptr<FileSourceListener> listener_ = nullptr;
|
||||
std::atomic<bool> isReading_ = false;
|
||||
std::thread readThread_;
|
||||
std::unique_ptr<std::ifstream> fileIn_ = nullptr;
|
||||
std::shared_ptr<uint8_t> buffer_ = nullptr;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* 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_ADAPTER_LISTENER_H
|
||||
#define INTELL_VOICE_ADAPTER_LISTENER_H
|
||||
|
||||
#include "v1_0/intell_voice_engine_types.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
class IntellVoiceAdapterListener {
|
||||
public:
|
||||
virtual ~IntellVoiceAdapterListener() = default;
|
||||
virtual void OnIntellVoiceHdiEvent(
|
||||
const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineCallBackEvent &event) = 0;
|
||||
};
|
||||
} // namespace IntellVoice
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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 "enroll_adapter_listener.h"
|
||||
|
||||
#include <thread>
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
#define LOG_TAG "EnrollAdapterListener"
|
||||
|
||||
using namespace OHOS::HDI::IntelligentVoice::Engine::V1_0;
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
EnrollAdapterListener::EnrollAdapterListener(const sptr<IIntelligentVoiceEngineCallback> &cb,
|
||||
OnEnrollEventCb enrollEventCb) : cb_(cb), enrollEventCb_(enrollEventCb)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("constructor");
|
||||
}
|
||||
|
||||
EnrollAdapterListener::~EnrollAdapterListener()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("destructor");
|
||||
}
|
||||
|
||||
void EnrollAdapterListener::OnIntellVoiceHdiEvent(const IntellVoiceEngineCallBackEvent& event)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("OnIntellVoiceHdiEvent");
|
||||
|
||||
enrollEventCb_(event.msgId, event.result);
|
||||
|
||||
if (cb_ != nullptr) {
|
||||
cb_->OnIntellVoiceEngineEvent(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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 ENROLL_ADAPTER_LISTENER_H
|
||||
#define ENROLL_ADAPTER_LISTENER_H
|
||||
|
||||
#include <functional>
|
||||
#include "intell_voice_adapter_listener.h"
|
||||
#include "i_intell_voice_engine_callback.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
using OnEnrollEventCb = std::function<void(int32_t, int32_t)>;
|
||||
|
||||
class EnrollAdapterListener : public IntellVoiceAdapterListener {
|
||||
public:
|
||||
EnrollAdapterListener(const sptr<IIntelligentVoiceEngineCallback> &cb, OnEnrollEventCb enrollEventCb);
|
||||
~EnrollAdapterListener();
|
||||
|
||||
void OnIntellVoiceHdiEvent(
|
||||
const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineCallBackEvent& event) override;
|
||||
|
||||
private:
|
||||
sptr<IIntelligentVoiceEngineCallback> cb_ = nullptr;
|
||||
OnEnrollEventCb enrollEventCb_;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,377 @@
|
||||
/*
|
||||
* 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 "enroll_engine.h"
|
||||
#include <fstream>
|
||||
#include "securec.h"
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
#include "v1_0/iintell_voice_engine_manager.h"
|
||||
#include "v1_0/iintell_voice_engine_callback.h"
|
||||
|
||||
#include "enroll_adapter_listener.h"
|
||||
#include "time_util.h"
|
||||
#include "scope_guard.h"
|
||||
#include "trigger_manager.h"
|
||||
#include "adapter_callback_service.h"
|
||||
#include "intell_voice_service_manager.h"
|
||||
|
||||
#define LOG_TAG "EnrollEngine"
|
||||
|
||||
using namespace OHOS::IntellVoiceTrigger;
|
||||
using namespace OHOS::HDI::IntelligentVoice::Engine::V1_0;
|
||||
using namespace OHOS::IntellVoiceUtils;
|
||||
using namespace OHOS::AudioStandard;
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
static constexpr uint32_t MIN_BUFFER_SIZE = 1280; // 16 * 2 * 40ms
|
||||
static constexpr uint32_t INTERVAL = 125; // 125 * 40ms = 5s
|
||||
EnrollEngine::EnrollEngine()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
|
||||
capturerOptions_.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_16000;
|
||||
capturerOptions_.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
|
||||
capturerOptions_.streamInfo.format = AudioSampleFormat::SAMPLE_S16LE;
|
||||
capturerOptions_.streamInfo.channels = AudioChannel::MONO;
|
||||
capturerOptions_.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
|
||||
capturerOptions_.capturerInfo.capturerFlags = 0;
|
||||
}
|
||||
|
||||
EnrollEngine::~EnrollEngine()
|
||||
{
|
||||
auto mgr = IIntellVoiceEngineManager::Get();
|
||||
if (mgr != nullptr) {
|
||||
mgr->ReleaseAdapter(desc_);
|
||||
}
|
||||
adapter_ = nullptr;
|
||||
callback_ = nullptr;
|
||||
if (audioSource_ != nullptr) {
|
||||
audioSource_->Stop();
|
||||
audioSource_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void EnrollEngine::OnEnrollEvent(int32_t msgId, int32_t result)
|
||||
{
|
||||
if (msgId == INTELL_VOICE_ENGINE_MSG_ENROLL_COMPLETE) {
|
||||
std::thread(&EnrollEngine::OnEnrollComplete, this).detach();
|
||||
} else if (msgId == INTELL_VOICE_ENGINE_MSG_COMMIT_ENROLL_COMPLETE) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
enrollResult_ = result;
|
||||
} else {
|
||||
}
|
||||
}
|
||||
|
||||
void EnrollEngine::OnEnrollComplete()
|
||||
{
|
||||
StopAudioSource();
|
||||
}
|
||||
|
||||
bool EnrollEngine::Init()
|
||||
{
|
||||
desc_.adapterType = ENROLL_ADAPTER_TYPE;
|
||||
auto mgr = IIntellVoiceEngineManager::Get();
|
||||
if (mgr == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to get engine manager");
|
||||
return false;
|
||||
}
|
||||
|
||||
mgr->CreateAdapter(desc_, adapter_);
|
||||
if (adapter_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("adapter is nullptr");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void EnrollEngine::SetCallback(sptr<IRemoteObject> object)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (adapter_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("adapter is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
sptr<IIntelligentVoiceEngineCallback> callback = iface_cast<IIntelligentVoiceEngineCallback>(object);
|
||||
if (callback == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("callback is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
std::shared_ptr<IntellVoiceAdapterListener> listener = std::make_shared<EnrollAdapterListener>(callback,
|
||||
std::bind(&EnrollEngine::OnEnrollEvent, this, std::placeholders::_1, std::placeholders::_2));
|
||||
if (listener == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("listener is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
callback_ = sptr<IIntellVoiceEngineCallback>(new (std::nothrow) AdapterCallbackService(listener));
|
||||
if (callback_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("callback_ is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
adapter_->SetCallback(callback_);
|
||||
}
|
||||
|
||||
int32_t EnrollEngine::Attach(const IntellVoiceEngineInfo &info)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
INTELL_VOICE_LOG_INFO("attach");
|
||||
if (adapter_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("adapter is nullptr");
|
||||
return -1;
|
||||
}
|
||||
|
||||
isPcmFromExternal_ = info.isPcmFromExternal;
|
||||
|
||||
IntellVoiceEngineAdapterInfo adapterInfo = {
|
||||
.wakeupPhrase = info.wakeupPhrase,
|
||||
.minBufSize = info.minBufSize,
|
||||
.sampleChannels = info.sampleChannels,
|
||||
.bitsPerSample = info.bitsPerSample,
|
||||
.sampleRate = info.sampleRate,
|
||||
};
|
||||
return adapter_->Attach(adapterInfo);
|
||||
}
|
||||
|
||||
int32_t EnrollEngine::Detach(void)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (adapter_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("adapter is nullptr");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (enrollResult_ == 0) {
|
||||
ProcDspModel();
|
||||
}
|
||||
return adapter_->Detach();
|
||||
}
|
||||
|
||||
int32_t EnrollEngine::Start(bool isLast)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (audioSource_ != nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("audioSource_ existed, wait for last start to finish");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (adapter_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("adapter is nullptr");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (IntellVoiceServiceManager::GetInstance()->ApplyArbitration(INTELL_VOICE_ENROLL, ENGINE_EVENT_START) !=
|
||||
ARBITRATION_OK) {
|
||||
INTELL_VOICE_LOG_ERROR("policy manager reject to start engine");
|
||||
return 0;
|
||||
}
|
||||
|
||||
StartInfo info = {
|
||||
.isLast = isLast,
|
||||
};
|
||||
if (adapter_->Start(info)) {
|
||||
INTELL_VOICE_LOG_ERROR("start adapter failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (isPcmFromExternal_) {
|
||||
INTELL_VOICE_LOG_INFO("pcm is from external");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!StartAudioSource()) {
|
||||
INTELL_VOICE_LOG_ERROR("start audio source failed");
|
||||
adapter_->Stop();
|
||||
return -1;
|
||||
}
|
||||
|
||||
INTELL_VOICE_LOG_INFO("exit");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t EnrollEngine::Stop()
|
||||
{
|
||||
StopAudioSource();
|
||||
|
||||
return EngineBase::Stop();
|
||||
}
|
||||
|
||||
int32_t EnrollEngine::SetParameter(const std::string &keyValueList)
|
||||
{
|
||||
if (SetParameterInner(keyValueList)) {
|
||||
INTELL_VOICE_LOG_INFO("inner parameter");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return EngineBase::SetParameter(keyValueList);
|
||||
}
|
||||
|
||||
bool EnrollEngine::SetParameterInner(const std::string &keyValueList)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
const std::unique_ptr<HistoryInfoMgr> &historyInfoMgr =
|
||||
IntellVoiceServiceManager::GetInstance()->GetHistoryInfoMgr();
|
||||
if (historyInfoMgr == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("historyInfoMgr is nullptr");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> kvpairs;
|
||||
SplitStringToKVPair(keyValueList, kvpairs);
|
||||
for (auto it : kvpairs) {
|
||||
if (it.first == std::string("wakeup_bundle_name")) {
|
||||
INTELL_VOICE_LOG_INFO("set wakeup bundle name:%{public}s", it.second.c_str());
|
||||
historyInfoMgr->SetWakeupEngineBundleName(it.second);
|
||||
return true;
|
||||
}
|
||||
if (it.first == std::string("wakeup_ability_name")) {
|
||||
INTELL_VOICE_LOG_INFO("set wakeup ability name:%{public}s", it.second.c_str());
|
||||
historyInfoMgr->SetWakeupEngineAbilityName(it.second);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void EnrollEngine::WriteBufferFromAshmem(uint8_t *&buffer, uint32_t size, sptr<OHOS::Ashmem> ashmem)
|
||||
{
|
||||
if (!ashmem->MapReadOnlyAshmem()) {
|
||||
INTELL_VOICE_LOG_ERROR("map ashmem failed");
|
||||
return;
|
||||
}
|
||||
|
||||
const uint8_t *tmpBuffer = static_cast<const uint8_t *>(ashmem->ReadFromAshmem(size, 0));
|
||||
if (tmpBuffer == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("read from ashmem failed");
|
||||
return;
|
||||
}
|
||||
|
||||
buffer = new (std::nothrow) uint8_t[size];
|
||||
if (buffer == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("allocate buffer failed");
|
||||
return;
|
||||
}
|
||||
|
||||
if (memcpy_s(buffer, size, tmpBuffer, size) != 0) {
|
||||
INTELL_VOICE_LOG_ERROR("memcpy_s failed");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void EnrollEngine::ProcDspModel()
|
||||
{
|
||||
uint8_t *buffer = nullptr;
|
||||
uint32_t size = 0;
|
||||
sptr<Ashmem> ashmem;
|
||||
adapter_->Read(DSP_MODLE, ashmem);
|
||||
if (ashmem == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("ashmem is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
ON_SCOPE_EXIT_WITH_NAME(ashmemExit)
|
||||
{
|
||||
INTELL_VOICE_LOG_DEBUG("close ashmem");
|
||||
ashmem->UnmapAshmem();
|
||||
ashmem->CloseAshmem();
|
||||
};
|
||||
|
||||
size = static_cast<uint32_t>(ashmem->GetAshmemSize());
|
||||
if (size == 0) {
|
||||
INTELL_VOICE_LOG_ERROR("size is zero");
|
||||
return;
|
||||
}
|
||||
|
||||
WriteBufferFromAshmem(buffer, size, ashmem);
|
||||
if (buffer == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("buffer is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
ON_SCOPE_EXIT_WITH_NAME(bufferExit)
|
||||
{
|
||||
INTELL_VOICE_LOG_DEBUG("now delete buffer");
|
||||
delete[] buffer;
|
||||
buffer = nullptr;
|
||||
};
|
||||
|
||||
std::shared_ptr<GenericTriggerModel> model = std::make_shared<GenericTriggerModel>(
|
||||
(IntellVoiceServiceManager::GetEnrollModelUuid()), 1);
|
||||
if (model == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("model is null");
|
||||
return;
|
||||
}
|
||||
|
||||
model->SetData(buffer, size);
|
||||
auto triggerMgr = TriggerManager::GetInstance();
|
||||
if (triggerMgr == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("trigger manager is nullptr");
|
||||
return;
|
||||
}
|
||||
triggerMgr->UpdateModel(model);
|
||||
}
|
||||
|
||||
bool EnrollEngine::StartAudioSource()
|
||||
{
|
||||
auto listener = std::make_unique<AudioSourceListener>([&] (uint8_t *buffer, uint32_t size) {
|
||||
if (adapter_ != nullptr) {
|
||||
std::vector<uint8_t> audioBuff(&buffer[0], &buffer[size]);
|
||||
adapter_->WriteAudio(audioBuff);
|
||||
}}, [&] (bool isError)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("end of pcm, isError:%d", isError);
|
||||
if ((adapter_ != nullptr) && (!isError)) {
|
||||
adapter_->SetParameter("end_of_pcm=true");
|
||||
}
|
||||
});
|
||||
if (listener == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create listener failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
audioSource_ = std::make_unique<AudioSource>(MIN_BUFFER_SIZE, INTERVAL, std::move(listener),
|
||||
capturerOptions_);
|
||||
if (audioSource_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create audio source failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!audioSource_->Start()) {
|
||||
INTELL_VOICE_LOG_ERROR("start capturer failed");
|
||||
audioSource_ = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void EnrollEngine::StopAudioSource()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (audioSource_ != nullptr) {
|
||||
audioSource_->Stop();
|
||||
audioSource_ = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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 ENROLL_ENGINE_H
|
||||
#define ENROLL_ENGINE_H
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <ashmem.h>
|
||||
#include "engine_base.h"
|
||||
#include "v1_0/iintell_voice_engine_callback.h"
|
||||
#include "audio_info.h"
|
||||
#include "audio_source.h"
|
||||
#include "intell_voice_generic_factory.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
class EnrollEngine : public EngineBase {
|
||||
public:
|
||||
~EnrollEngine();
|
||||
virtual bool Init() override;
|
||||
void SetCallback(sptr<IRemoteObject> object) override;
|
||||
int32_t Attach(const IntellVoiceEngineInfo &info) override;
|
||||
int32_t Detach(void) override;
|
||||
int32_t Start(bool isLast) override;
|
||||
int32_t Stop() override;
|
||||
int32_t SetParameter(const std::string &keyValueList) override;
|
||||
|
||||
private:
|
||||
EnrollEngine();
|
||||
bool SetParameterInner(const std::string &keyValueList);
|
||||
bool StartAudioSource();
|
||||
void StopAudioSource();
|
||||
void OnEnrollEvent(int32_t msgId, int32_t result);
|
||||
void OnEnrollComplete();
|
||||
void ProcDspModel();
|
||||
void WriteBufferFromAshmem(uint8_t *&buffer, uint32_t size, sptr<OHOS::Ashmem> ashmem);
|
||||
|
||||
private:
|
||||
std::string name_ = "lp enroll engine instance";
|
||||
bool isPcmFromExternal_ = false;
|
||||
int32_t enrollResult_ = -1;
|
||||
sptr<OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineCallback> callback_ = nullptr;
|
||||
OHOS::AudioStandard::AudioCapturerOptions capturerOptions_;
|
||||
std::unique_ptr<AudioSource> audioSource_ = nullptr;
|
||||
friend class IntellVoiceUtils::SptrFactory<EnrollEngine>;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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_engine_callback_proxy.h"
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
#define LOG_TAG "IntellVoiceEngineCallbackProxy"
|
||||
|
||||
using namespace OHOS::HDI::IntelligentVoice::Engine::V1_0;
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
void IntellVoiceEngineCallbackProxy::OnIntellVoiceEngineEvent(const IntellVoiceEngineCallBackEvent &event)
|
||||
{
|
||||
MessageParcel data;
|
||||
MessageParcel reply;
|
||||
MessageOption option;
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
|
||||
if (!data.WriteInterfaceToken(IIntelligentVoiceEngineCallback::GetDescriptor())) {
|
||||
INTELL_VOICE_LOG_ERROR("WriteInterfaceToken failed");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data.WriteInt32(static_cast<int32_t>(event.msgId))) {
|
||||
INTELL_VOICE_LOG_ERROR("write msgid failed");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data.WriteInt32(static_cast<int32_t>(event.result))) {
|
||||
INTELL_VOICE_LOG_ERROR("write result failed");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data.WriteString(event.info)) {
|
||||
INTELL_VOICE_LOG_ERROR("write info failed");
|
||||
return;
|
||||
}
|
||||
|
||||
int32_t error = Remote()->SendRequest(IIntelligentVoiceEngineCallback::Code::ON_INTELL_VOICE_ENGINE_EVENT,
|
||||
data, reply, option);
|
||||
if (error != 0) {
|
||||
INTELL_VOICE_LOG_ERROR("send request error: %{public}d", error);
|
||||
}
|
||||
reply.ReadInt32();
|
||||
}
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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 ENGINE_CALLBACK_PROXY_H
|
||||
#define ENGINE_CALLBACK_PROXY_H
|
||||
|
||||
#include "iremote_proxy.h"
|
||||
#include "i_intell_voice_engine_callback.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
using OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineCallBackEvent;
|
||||
|
||||
class IntellVoiceEngineCallbackProxy : public IRemoteProxy<IIntelligentVoiceEngineCallback> {
|
||||
public:
|
||||
explicit IntellVoiceEngineCallbackProxy(const sptr<IRemoteObject> &impl)
|
||||
: IRemoteProxy<IIntelligentVoiceEngineCallback>(impl) {};
|
||||
virtual ~IntellVoiceEngineCallbackProxy() = default;
|
||||
void OnIntellVoiceEngineEvent(const IntellVoiceEngineCallBackEvent &event) override;
|
||||
private:
|
||||
static inline BrokerDelegator<IntellVoiceEngineCallbackProxy> delegator_;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 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_engine_stub.h"
|
||||
#include "securec.h"
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
#define LOG_TAG "IntellVoiceEngineStub"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
int32_t IntellVoiceEngineStub::OnRemoteRequest(uint32_t code,
|
||||
MessageParcel &data, MessageParcel &reply, MessageOption &option)
|
||||
{
|
||||
if (data.ReadInterfaceToken() != IIntellVoiceEngine::GetDescriptor()) {
|
||||
INTELL_VOICE_LOG_ERROR("token mismatch");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int32_t ret = 0;
|
||||
|
||||
switch (code) {
|
||||
case INTELL_VOICE_ENGINE_SET_CALLBACK:
|
||||
object = data.ReadRemoteObject();
|
||||
SetCallback(object);
|
||||
break;
|
||||
case INTELL_VOICE_ENGINE_ATTACH:
|
||||
info.wakeupPhrase = data.ReadString();
|
||||
info.isPcmFromExternal = data.ReadBool();
|
||||
info.minBufSize = data.ReadInt32();
|
||||
info.sampleChannels = data.ReadInt32();
|
||||
info.bitsPerSample = data.ReadInt32();
|
||||
info.sampleRate = data.ReadInt32();
|
||||
ret = Attach(info);
|
||||
break;
|
||||
case INTELL_VOICE_ENGINE_DETACH:
|
||||
ret = Detach();
|
||||
break;
|
||||
case INTELL_VOICE_ENGINE_SET_PARAMETER:
|
||||
ret = SetParameter(data.ReadString());
|
||||
break;
|
||||
case INTELL_VOICE_ENGINE_GET_PARAMETER:
|
||||
str = GetParameter(data.ReadString());
|
||||
reply.WriteString(str);
|
||||
break;
|
||||
case INTELL_VOICE_ENGINE_START:
|
||||
ret = Start(data.ReadBool());
|
||||
break;
|
||||
case INTELL_VOICE_ENGINE_STOP:
|
||||
ret = Stop();
|
||||
break;
|
||||
case INTELL_VOICE_ENGINE_WRITE_AUDIO:
|
||||
size = data.ReadInt32();
|
||||
buffer = data.ReadBuffer(size);
|
||||
ret = WriteAudio(buffer, size);
|
||||
break;
|
||||
default:
|
||||
ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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_ENGINE_STUB_H
|
||||
#define INTELL_VOICE_ENGINE_STUB_H
|
||||
#include "iremote_stub.h"
|
||||
#include "i_intell_voice_engine.h"
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
class IntellVoiceEngineStub : public IRemoteStub<IIntellVoiceEngine> {
|
||||
public:
|
||||
int32_t OnRemoteRequest(uint32_t code,
|
||||
MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
|
||||
private:
|
||||
sptr<IRemoteObject> object;
|
||||
IntellVoiceEngineInfo info;
|
||||
std::string str;
|
||||
int size = 0;
|
||||
const uint8_t *buffer;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,205 @@
|
||||
/*
|
||||
* 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_service.h"
|
||||
|
||||
#include <malloc.h>
|
||||
#include <fcntl.h>
|
||||
#include <cstdio>
|
||||
#include <unistd.h>
|
||||
#include "accesstoken_kit.h"
|
||||
#include "ipc_skeleton.h"
|
||||
#include "intell_voice_log.h"
|
||||
#include "system_ability_definition.h"
|
||||
#include "intell_voice_service_manager.h"
|
||||
#include "common_event_manager.h"
|
||||
#include "common_event_support.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace OHOS::AppExecFwk;
|
||||
using namespace OHOS::EventFwk;
|
||||
#define LOG_TAG "IntellVoiceService"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
REGISTER_SYSTEM_ABILITY_BY_ID(IntellVoiceService, INTELL_VOICE_SERVICE_ID, true);
|
||||
const std::string OHOS_PERMISSION_INTELL_VOICE = "ohos.permission.MANAGE_INTELLIGENT_VOICE";
|
||||
|
||||
IntellVoiceService::IntellVoiceService(int32_t systemAbilityId, bool runOnCreate)
|
||||
: SystemAbility(INTELL_VOICE_SERVICE_ID, true)
|
||||
{
|
||||
}
|
||||
|
||||
IntellVoiceService::~IntellVoiceService()
|
||||
{
|
||||
}
|
||||
|
||||
int32_t IntellVoiceService::CreateIntellVoiceEngine(IntellVoiceEngineType type, sptr<IIntellVoiceEngine> &inst)
|
||||
{
|
||||
if (!VerifyClientPermission(OHOS_PERMISSION_INTELL_VOICE)) {
|
||||
INTELL_VOICE_LOG_WARN("verify permission");
|
||||
}
|
||||
|
||||
INTELL_VOICE_LOG_INFO("enter, type: %{public}d", type);
|
||||
std::unique_ptr<IntellVoiceServiceManager> &mgr = IntellVoiceServiceManager::GetInstance();
|
||||
if (mgr == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("mgr is nullptr");
|
||||
return -1;
|
||||
}
|
||||
|
||||
inst = mgr->CreateEngine(type);
|
||||
if (inst == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("engine is nullptr");
|
||||
return -1;
|
||||
}
|
||||
INTELL_VOICE_LOG_INFO("create engine ok");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t IntellVoiceService::ReleaseIntellVoiceEngine(IntellVoiceEngineType type)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter, type: %{public}d", type);
|
||||
std::unique_ptr<IntellVoiceServiceManager> &mgr = IntellVoiceServiceManager::GetInstance();
|
||||
if (mgr == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("mgr is nullptr");
|
||||
return -1;
|
||||
}
|
||||
return mgr->ReleaseEngine(type);
|
||||
}
|
||||
|
||||
void IntellVoiceService::OnStart(void)
|
||||
{
|
||||
bool ret = Publish(this);
|
||||
if (!ret) {
|
||||
INTELL_VOICE_LOG_ERROR("publish failed!");
|
||||
return;
|
||||
}
|
||||
CreateSystemEventObserver();
|
||||
AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
|
||||
AddSystemAbilityListener(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
|
||||
RegisterPermissionCallback(OHOS_PERMISSION_INTELL_VOICE);
|
||||
INTELL_VOICE_LOG_INFO("publish ok");
|
||||
}
|
||||
|
||||
void IntellVoiceService::OnStop(void)
|
||||
{
|
||||
const auto &manager = IntellVoiceServiceManager::GetInstance();
|
||||
if (manager != nullptr) {
|
||||
manager->ReleaseSwitchProvider();
|
||||
}
|
||||
|
||||
if (systemEventObserver_ != nullptr) {
|
||||
systemEventObserver_->Unsubscribe();
|
||||
}
|
||||
}
|
||||
|
||||
void IntellVoiceService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("systemAbilityId:%{public}d", systemAbilityId);
|
||||
if (systemAbilityId == COMMON_EVENT_SERVICE_ID) {
|
||||
if (systemEventObserver_ != nullptr) {
|
||||
systemEventObserver_->Subscribe();
|
||||
}
|
||||
} else if (systemAbilityId == DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID) {
|
||||
const auto &manager = IntellVoiceServiceManager::GetInstance();
|
||||
if (manager != nullptr) {
|
||||
manager->CreateSwitchProvider();
|
||||
}
|
||||
} else {
|
||||
INTELL_VOICE_LOG_WARN("unhandled sysabilityId");
|
||||
}
|
||||
}
|
||||
|
||||
void IntellVoiceService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
|
||||
{}
|
||||
|
||||
void IntellVoiceService::CreateSystemEventObserver()
|
||||
{
|
||||
std::shared_ptr<EventRunner> runner = EventRunner::Create("service");
|
||||
if (runner == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("runner is null");
|
||||
return;
|
||||
}
|
||||
|
||||
std::shared_ptr<EventHandler> handler = std::make_shared<EventHandler>(runner);
|
||||
if (handler == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("handler is null");
|
||||
return;
|
||||
}
|
||||
|
||||
OHOS::EventFwk::MatchingSkills matchingSkills;
|
||||
matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
|
||||
matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
|
||||
matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED);
|
||||
matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REPLACED);
|
||||
matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
|
||||
OHOS::EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
|
||||
systemEventObserver_ = SystemEventObserver::Create(subscribeInfo);
|
||||
if (systemEventObserver_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("systemEventObserver_ is nullptr");
|
||||
return;
|
||||
}
|
||||
systemEventObserver_->SetEventHandler(handler);
|
||||
INTELL_VOICE_LOG_INFO("create system event observer successfully");
|
||||
}
|
||||
|
||||
static void print_to_file(void *fp, const char *s)
|
||||
{
|
||||
(void)fputs(s, (FILE *)fp);
|
||||
}
|
||||
|
||||
int IntellVoiceService::Dump(int fd, const std::vector<std::u16string> &args)
|
||||
{
|
||||
FILE* fp = fdopen(fd, "w+");
|
||||
if (fp != nullptr) {
|
||||
malloc_stats_print(print_to_file, fp, "");
|
||||
fp = nullptr;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool IntellVoiceService::VerifyClientPermission(const std::string &permissionName)
|
||||
{
|
||||
Security::AccessToken::AccessTokenID clientTokenId = IPCSkeleton::GetCallingTokenID();
|
||||
INTELL_VOICE_LOG_INFO("clientTokenId:%{public}d", clientTokenId);
|
||||
int res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(clientTokenId, permissionName);
|
||||
if (res != Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
|
||||
INTELL_VOICE_LOG_ERROR("Permission denied!");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void IntellVoiceService::RegisterPermissionCallback(const std::string &permissionName)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
Security::AccessToken::PermStateChangeScope scopeInfo;
|
||||
scopeInfo.permList = {permissionName};
|
||||
auto callbackPtr = std::make_shared<PerStateChangeCbCustomizeCallback>(scopeInfo);
|
||||
int32_t res = Security::AccessToken::AccessTokenKit::RegisterPermStateChangeCallback(callbackPtr);
|
||||
if (res < 0) {
|
||||
INTELL_VOICE_LOG_ERROR("fail to call RegisterPermStateChangeCallback.");
|
||||
}
|
||||
}
|
||||
|
||||
void IntellVoiceService::PerStateChangeCbCustomizeCallback::PermStateChangeCallback(
|
||||
Security::AccessToken::PermStateChangeInfo& result)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter, permStateChangeType: %{public}d", result.permStateChangeType);
|
||||
if (result.permStateChangeType == 0) {
|
||||
INTELL_VOICE_LOG_ERROR("The permission is canceled.");
|
||||
}
|
||||
}
|
||||
} // namespace IntellVoiceEngine
|
||||
} // namespace OHOS
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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_SERVICE_H
|
||||
#define INTELL_VOICE_SERVICE_H
|
||||
#include <map>
|
||||
#include "accesstoken_kit.h"
|
||||
#include "ipc_skeleton.h"
|
||||
#include "system_ability.h"
|
||||
#include "intell_voice_service_stub.h"
|
||||
#include "i_intell_voice_engine.h"
|
||||
#include "system_event_observer.h"
|
||||
|
||||
#include "trigger_manager.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
class IntellVoiceService : public SystemAbility, public IntellVoiceServiceStub {
|
||||
DECLARE_SYSTEM_ABILITY(IntellVoiceService);
|
||||
public:
|
||||
explicit IntellVoiceService(int32_t systemAbilityId, bool runOnCreate = true);
|
||||
~IntellVoiceService();
|
||||
int32_t CreateIntellVoiceEngine(IntellVoiceEngineType type, sptr<IIntellVoiceEngine> &inst) override;
|
||||
int32_t ReleaseIntellVoiceEngine(IntellVoiceEngineType type) override;
|
||||
int32_t Dump(int fd, const std::vector<std::u16string> &args) override;
|
||||
|
||||
class PerStateChangeCbCustomizeCallback : public Security::AccessToken::PermStateChangeCallbackCustomize {
|
||||
public:
|
||||
explicit PerStateChangeCbCustomizeCallback(const Security::AccessToken::PermStateChangeScope &scopeInfo)
|
||||
: PermStateChangeCallbackCustomize(scopeInfo) {}
|
||||
~PerStateChangeCbCustomizeCallback() {}
|
||||
|
||||
void PermStateChangeCallback(Security::AccessToken::PermStateChangeInfo& result) override;
|
||||
};
|
||||
|
||||
protected:
|
||||
void OnStart() override;
|
||||
void OnStop() override;
|
||||
void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
|
||||
void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
|
||||
private:
|
||||
void CreateSystemEventObserver();
|
||||
bool VerifyClientPermission(const std::string &permissionName);
|
||||
void RegisterPermissionCallback(const std::string &permissionName);
|
||||
|
||||
private:
|
||||
std::shared_ptr<SystemEventObserver> systemEventObserver_ = nullptr;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,368 @@
|
||||
/*
|
||||
* 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_service_manager.h"
|
||||
|
||||
#include "intell_voice_log.h"
|
||||
#include "engine_factory.h"
|
||||
#include "wakeup_engine.h"
|
||||
#include "trigger_manager.h"
|
||||
#include "intell_voice_generic_factory.h"
|
||||
#include "trigger_detector_callback.h"
|
||||
#include "memory_guard.h"
|
||||
|
||||
using namespace OHOS::IntellVoiceTrigger;
|
||||
using namespace OHOS::IntellVoiceUtils;
|
||||
#define LOG_TAG "IntellVoiceServiceManager"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
const int32_t IntellVoiceServiceManager::g_enrollModelUuid = 1;
|
||||
|
||||
std::unique_ptr<IntellVoiceServiceManager> IntellVoiceServiceManager::g_intellVoiceServiceMgr =
|
||||
std::unique_ptr<IntellVoiceServiceManager>(new (std::nothrow) IntellVoiceServiceManager());
|
||||
|
||||
IntellVoiceServiceManager::IntellVoiceServiceManager()
|
||||
{
|
||||
OHOS::IntellVoiceUtils::MemoryGuard memoryGuard;
|
||||
historyInfoMgr_ = std::make_unique<HistoryInfoMgr>();
|
||||
if (historyInfoMgr_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("historyInfoMgr_ is nullptr");
|
||||
}
|
||||
}
|
||||
|
||||
IntellVoiceServiceManager::~IntellVoiceServiceManager()
|
||||
{
|
||||
engines_.clear();
|
||||
switchObserver_ = nullptr;
|
||||
switchProvider_ = nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<IntellVoiceServiceManager> &IntellVoiceServiceManager::GetInstance()
|
||||
{
|
||||
return g_intellVoiceServiceMgr;
|
||||
}
|
||||
|
||||
sptr<IIntellVoiceEngine> IntellVoiceServiceManager::CreateEngine(IntellVoiceEngineType type)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter, type:%{public}d", type);
|
||||
if (type == INTELL_VOICE_ENROLL) {
|
||||
StopDetection();
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(engineMutex_);
|
||||
|
||||
if (ApplyArbitration(type, ENGINE_EVENT_CREATE) != ARBITRATION_OK) {
|
||||
INTELL_VOICE_LOG_ERROR("policy manager reject create engine, type:%{public}d", type);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return CreateEngineInner(type);
|
||||
}
|
||||
|
||||
sptr<IIntellVoiceEngine> IntellVoiceServiceManager::CreateEngineInner(IntellVoiceEngineType type)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("create engine enter, type: %d", type);
|
||||
OHOS::IntellVoiceUtils::MemoryGuard memoryGuard;
|
||||
auto it = engines_.find(type);
|
||||
if (it != engines_.end() && it->second != nullptr) {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
sptr<EngineBase> engine = EngineFactory::CreateEngineInst(type);
|
||||
if (engine == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create engine failed, type:%{public}d", type);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
engines_[type] = engine;
|
||||
INTELL_VOICE_LOG_INFO("create engine ok");
|
||||
return engine;
|
||||
}
|
||||
|
||||
int32_t IntellVoiceServiceManager::ReleaseEngine(IntellVoiceEngineType type)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter, type:%{public}d", type);
|
||||
std::lock_guard<std::mutex> lock(engineMutex_);
|
||||
auto ret = ReleaseEngineInner(type);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (type == INTELL_VOICE_ENROLL) {
|
||||
auto triggerMgr = TriggerManager::GetInstance();
|
||||
if (triggerMgr == nullptr) {
|
||||
INTELL_VOICE_LOG_WARN("trigger manager is nullptr");
|
||||
return 0;
|
||||
}
|
||||
auto model = triggerMgr->GetModel(IntellVoiceServiceManager::GetEnrollModelUuid());
|
||||
if (model == nullptr) {
|
||||
INTELL_VOICE_LOG_WARN("no model");
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto wakeupEngine = CreateEngineInner(INTELL_VOICE_WAKEUP);
|
||||
if (wakeupEngine == nullptr) {
|
||||
INTELL_VOICE_LOG_WARN("failed to create wakeup engine");
|
||||
return 0;
|
||||
}
|
||||
CreateDetector();
|
||||
if (switchProvider_->QuerySwitchStatus()) {
|
||||
StartDetection();
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t IntellVoiceServiceManager::ReleaseEngineInner(IntellVoiceEngineType type)
|
||||
{
|
||||
OHOS::IntellVoiceUtils::MemoryGuard memoryGuard;
|
||||
auto it = engines_.find(type);
|
||||
if (it == engines_.end()) {
|
||||
INTELL_VOICE_LOG_ERROR("there is no engine(%{public}d) in list", type);
|
||||
return -1;
|
||||
}
|
||||
|
||||
it->second = nullptr;
|
||||
engines_.erase(type);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void IntellVoiceServiceManager::CreateSwitchProvider()
|
||||
{
|
||||
switchObserver_ = sptr<SwitchObserver>(new (std::nothrow) SwitchObserver());
|
||||
if (switchObserver_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("switchObserver_ is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
switchObserver_->SetUpdateFunc([]() {
|
||||
const auto &manager = IntellVoiceServiceManager::GetInstance();
|
||||
if (manager != nullptr) {
|
||||
manager->OnSwitchChange();
|
||||
}
|
||||
});
|
||||
|
||||
switchProvider_ = UniquePtrFactory<SwitchProvider>::CreateInstance();
|
||||
if (switchProvider_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("switchProvider_ is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
if (switchObserver_ != nullptr) {
|
||||
switchProvider_->RegisterObserver(switchObserver_);
|
||||
}
|
||||
}
|
||||
|
||||
void IntellVoiceServiceManager::ReleaseSwitchProvider()
|
||||
{
|
||||
if (switchProvider_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("switchProvider_ is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
if (switchObserver_ != nullptr) {
|
||||
switchProvider_->UnregisterObserver(switchObserver_);
|
||||
}
|
||||
|
||||
switchProvider_ = nullptr;
|
||||
}
|
||||
|
||||
void IntellVoiceServiceManager::CreateDetector()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(detectorMutex_);
|
||||
if (detector_ != nullptr) {
|
||||
INTELL_VOICE_LOG_INFO("detector is already existed, no need to create");
|
||||
return;
|
||||
}
|
||||
|
||||
std::shared_ptr<TriggerDetectorCallback> cb = std::make_shared<TriggerDetectorCallback>([&]() { OnDetected(); });
|
||||
if (cb == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("cb is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
auto triggerMgr = TriggerManager::GetInstance();
|
||||
if (triggerMgr == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("trigger manager is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
detector_ = triggerMgr->CreateTriggerDetector(1, cb);
|
||||
if (detector_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("detector_ is nullptr");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void IntellVoiceServiceManager::StartDetection()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(detectorMutex_);
|
||||
if (detector_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("detector_ is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
detector_->StartRecognition();
|
||||
}
|
||||
|
||||
void IntellVoiceServiceManager::StopDetection()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(detectorMutex_);
|
||||
if (detector_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
detector_->StopRecognition();
|
||||
}
|
||||
|
||||
void IntellVoiceServiceManager::OnDetected()
|
||||
{
|
||||
sptr<EngineBase> engine = nullptr;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(engineMutex_);
|
||||
auto it = engines_.find(INTELL_VOICE_WAKEUP);
|
||||
if ((it == engines_.end()) || (it->second == nullptr)) {
|
||||
INTELL_VOICE_LOG_ERROR("wakeup engine is not existed");
|
||||
return;
|
||||
}
|
||||
|
||||
engine = it->second;
|
||||
}
|
||||
|
||||
engine->OnDetected();
|
||||
}
|
||||
|
||||
void IntellVoiceServiceManager::OnUserUnlock()
|
||||
{
|
||||
auto triggerMgr = TriggerManager::GetInstance();
|
||||
if (triggerMgr == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("trigger manager is nullptr");
|
||||
return;
|
||||
}
|
||||
auto model = triggerMgr->GetModel(IntellVoiceServiceManager::GetEnrollModelUuid());
|
||||
if (model == nullptr) {
|
||||
INTELL_VOICE_LOG_INFO("no model");
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(engineMutex_);
|
||||
sptr<EngineBase> wakeupEngine = EngineFactory::CreateEngineInst(INTELL_VOICE_WAKEUP);
|
||||
if (wakeupEngine == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("wakeupEngine is nullptr");
|
||||
return;
|
||||
}
|
||||
engines_[INTELL_VOICE_WAKEUP] = wakeupEngine;
|
||||
}
|
||||
|
||||
CreateDetector();
|
||||
if (switchProvider_->QuerySwitchStatus()) {
|
||||
StartDetection();
|
||||
}
|
||||
}
|
||||
|
||||
void IntellVoiceServiceManager::OnSwitchChange()
|
||||
{
|
||||
if (switchProvider_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("switchProvider_ is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(engineMutex_);
|
||||
auto it = engines_.find(INTELL_VOICE_ENROLL);
|
||||
if ((it != engines_.end()) && (it->second != nullptr)) {
|
||||
INTELL_VOICE_LOG_INFO("enroll engine is existed");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (switchProvider_->QuerySwitchStatus()) {
|
||||
INTELL_VOICE_LOG_INFO("switch on");
|
||||
StartDetection();
|
||||
} else {
|
||||
INTELL_VOICE_LOG_INFO("switch off");
|
||||
StopDetection();
|
||||
}
|
||||
}
|
||||
|
||||
int32_t IntellVoiceServiceManager::ApplyArbitration(IntellVoiceEngineType type, EngineEvent event)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
switch (event) {
|
||||
case ENGINE_EVENT_CREATE:
|
||||
return CreateArbitration(type);
|
||||
case ENGINE_EVENT_START:
|
||||
return StartArbitration(type);
|
||||
default:
|
||||
INTELL_VOICE_LOG_INFO("unknown engine event:%d", event);
|
||||
break;
|
||||
}
|
||||
return ARBITRATION_OK;
|
||||
}
|
||||
|
||||
int32_t IntellVoiceServiceManager::CreateArbitration(IntellVoiceEngineType type)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (type == INTELL_VOICE_ENROLL) {
|
||||
auto wakeupEngineIt = engines_.find(INTELL_VOICE_WAKEUP);
|
||||
if (wakeupEngineIt != engines_.end()) {
|
||||
HandlePreemption(wakeupEngineIt->second);
|
||||
}
|
||||
|
||||
auto enrollEngineIt = engines_.find(INTELL_VOICE_ENROLL);
|
||||
if (enrollEngineIt != engines_.end()) {
|
||||
HandleReplace(enrollEngineIt->second);
|
||||
}
|
||||
}
|
||||
return ARBITRATION_OK;
|
||||
}
|
||||
|
||||
int32_t IntellVoiceServiceManager::StartArbitration(IntellVoiceEngineType type)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (type == INTELL_VOICE_WAKEUP) {
|
||||
auto it = engines_.find(INTELL_VOICE_ENROLL);
|
||||
if (it != engines_.end()) {
|
||||
return ARBITRATION_REJECT;
|
||||
}
|
||||
}
|
||||
return ARBITRATION_OK;
|
||||
}
|
||||
|
||||
void IntellVoiceServiceManager::HandlePreemption(sptr<IIntellVoiceEngine> currentEngine)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (currentEngine == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to stop current engine, current engine is null");
|
||||
return;
|
||||
}
|
||||
currentEngine->Stop();
|
||||
}
|
||||
|
||||
void IntellVoiceServiceManager::HandleReplace(sptr<IIntellVoiceEngine> currentEngine)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (currentEngine == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to detach current engine, current engine is null");
|
||||
return;
|
||||
}
|
||||
currentEngine->Detach();
|
||||
currentEngine = nullptr;
|
||||
}
|
||||
} // namespace IntellVoiceEngine
|
||||
} // namespace OHOS
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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 SERVICE_MANAGER_H
|
||||
#define SERVICE_MANAGER_H
|
||||
#include <mutex>
|
||||
#include <map>
|
||||
#include "engine_base.h"
|
||||
#include "trigger_detector.h"
|
||||
#include "switch_observer.h"
|
||||
#include "switch_provider.h"
|
||||
#include "history_info_mgr.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
enum EngineEvent {
|
||||
ENGINE_EVENT_CREATE = 0,
|
||||
ENGINE_EVENT_START,
|
||||
};
|
||||
|
||||
enum ArbitrationResult {
|
||||
ARBITRATION_OK = 0,
|
||||
ARBITRATION_REJECT,
|
||||
};
|
||||
|
||||
class IntellVoiceServiceManager {
|
||||
public:
|
||||
~IntellVoiceServiceManager();
|
||||
static std::unique_ptr<IntellVoiceServiceManager> &GetInstance();
|
||||
static int32_t GetEnrollModelUuid()
|
||||
{
|
||||
return g_enrollModelUuid;
|
||||
}
|
||||
sptr<IIntellVoiceEngine> CreateEngine(IntellVoiceEngineType type);
|
||||
int32_t ReleaseEngine(IntellVoiceEngineType type);
|
||||
const std::unique_ptr<HistoryInfoMgr> &GetHistoryInfoMgr()
|
||||
{
|
||||
return historyInfoMgr_;
|
||||
}
|
||||
void OnUserUnlock();
|
||||
void CreateSwitchProvider();
|
||||
void ReleaseSwitchProvider();
|
||||
void StartDetection();
|
||||
void StopDetection();
|
||||
|
||||
int32_t ApplyArbitration(IntellVoiceEngineType type, EngineEvent event);
|
||||
|
||||
private:
|
||||
IntellVoiceServiceManager();
|
||||
void OnSwitchChange();
|
||||
void OnDetected();
|
||||
void CreateDetector();
|
||||
|
||||
int32_t CreateArbitration(IntellVoiceEngineType type);
|
||||
int32_t StartArbitration(IntellVoiceEngineType type);
|
||||
void HandlePreemption(sptr<IIntellVoiceEngine> currentEngine);
|
||||
void HandleReplace(sptr<IIntellVoiceEngine> currentEngine);
|
||||
|
||||
sptr<IIntellVoiceEngine> CreateEngineInner(IntellVoiceEngineType type);
|
||||
int32_t ReleaseEngineInner(IntellVoiceEngineType type);
|
||||
|
||||
private:
|
||||
static const int32_t g_enrollModelUuid;
|
||||
static std::unique_ptr<IntellVoiceServiceManager> g_intellVoiceServiceMgr;
|
||||
std::mutex engineMutex_;
|
||||
std::mutex detectorMutex_;
|
||||
std::map<IntellVoiceEngineType, sptr<EngineBase>> engines_;
|
||||
std::shared_ptr<IntellVoiceTrigger::TriggerDetector> detector_ = nullptr;
|
||||
sptr<SwitchObserver> switchObserver_ = nullptr;
|
||||
IntellVoiceUtils::UniqueProductType<SwitchProvider> switchProvider_ =
|
||||
IntellVoiceUtils::UniqueProductType<SwitchProvider> {nullptr, nullptr};
|
||||
std::unique_ptr<HistoryInfoMgr> historyInfoMgr_ = nullptr;
|
||||
};
|
||||
} // namespace IntellVoice
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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_service_stub.h"
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
#define LOG_TAG "IntellVoiceServiceStub"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
int32_t IntellVoiceServiceStub::OnRemoteRequest(uint32_t code,
|
||||
MessageParcel &data, MessageParcel &reply, MessageOption &option)
|
||||
{
|
||||
if (data.ReadInterfaceToken() != IIntellVoiceService::GetDescriptor()) {
|
||||
INTELL_VOICE_LOG_ERROR("token mismatch");
|
||||
return -1;
|
||||
}
|
||||
|
||||
IntellVoiceEngineType type = static_cast<IntellVoiceEngineType>(data.ReadInt32());
|
||||
int32_t ret = 0;
|
||||
|
||||
sptr<IIntellVoiceEngine> engine;
|
||||
switch (code) {
|
||||
case HDI_INTELL_VOICE_SERVICE_CREATE_ENGINE:
|
||||
ret = CreateIntellVoiceEngine(type, engine);
|
||||
reply.WriteRemoteObject(engine->AsObject());
|
||||
return ret;
|
||||
|
||||
case HDI_INTELL_VOICE_SERVICE_RELEASE_ENGINE:
|
||||
return ReleaseIntellVoiceEngine(type);
|
||||
default:
|
||||
return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* 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_SERVICE_STUB_H
|
||||
#define INTELL_VOICE_SERVICE_STUB_H
|
||||
#include "iremote_stub.h"
|
||||
#include "i_intell_voice_service.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
class IntellVoiceServiceStub : public IRemoteStub<IIntellVoiceService> {
|
||||
public:
|
||||
int32_t OnRemoteRequest(uint32_t code,
|
||||
MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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 "history_info_mgr.h"
|
||||
|
||||
#include "string_util.h"
|
||||
|
||||
using namespace OHOS::IntellVoiceUtils;
|
||||
#define LOG_TAG "HistoryInfoMgr"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
constexpr int decimalNotation = 10;
|
||||
const std::string KEY_ENROLL_ENGINE_UID = "EnrollEngineUid";
|
||||
const std::string KEY_WAKEUP_ENGINE_BUNDLE_NAME = "WakeupEngineBundleName";
|
||||
const std::string KEY_WAKEUP_ENGINE_ABILITY_NAME = "WakeupEngineAbilityName";
|
||||
|
||||
HistoryInfoMgr::HistoryInfoMgr()
|
||||
: ServiceDbHelper("intell_voice_service_manager", "local_intell_voice_history_mgr_storeId")
|
||||
{
|
||||
}
|
||||
|
||||
void HistoryInfoMgr::SetEnrollEngineUid(int32_t uid)
|
||||
{
|
||||
SetValue(KEY_ENROLL_ENGINE_UID, StringUtil::Int2String(uid));
|
||||
}
|
||||
|
||||
int32_t HistoryInfoMgr::GetEnrollEngineUid()
|
||||
{
|
||||
std::string value = GetValue(KEY_ENROLL_ENGINE_UID);
|
||||
return static_cast<int32_t>(strtol(value.c_str(), nullptr, decimalNotation));
|
||||
}
|
||||
|
||||
void HistoryInfoMgr::SetWakeupEngineBundleName(std::string bundleName)
|
||||
{
|
||||
SetValue(KEY_WAKEUP_ENGINE_BUNDLE_NAME, bundleName);
|
||||
}
|
||||
|
||||
std::string HistoryInfoMgr::GetWakeupEngineBundleName()
|
||||
{
|
||||
return GetValue(KEY_WAKEUP_ENGINE_BUNDLE_NAME);
|
||||
}
|
||||
|
||||
void HistoryInfoMgr::SetWakeupEngineAbilityName(std::string abilityName)
|
||||
{
|
||||
SetValue(KEY_WAKEUP_ENGINE_ABILITY_NAME, abilityName);
|
||||
}
|
||||
|
||||
std::string HistoryInfoMgr::GetWakeupEngineAbilityName()
|
||||
{
|
||||
return GetValue(KEY_WAKEUP_ENGINE_ABILITY_NAME);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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 HISTORY_INFO_MGR_H
|
||||
#define HISTORY_INFO_MGR_H
|
||||
|
||||
#include "service_db_helper.h"
|
||||
#include "nocopyable.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
class HistoryInfoMgr : private ServiceDbHelper {
|
||||
public:
|
||||
HistoryInfoMgr();
|
||||
~HistoryInfoMgr() = default;
|
||||
|
||||
void SetEnrollEngineUid(int32_t uid);
|
||||
int32_t GetEnrollEngineUid();
|
||||
void SetWakeupEngineBundleName(std::string bundleName);
|
||||
std::string GetWakeupEngineBundleName();
|
||||
void SetWakeupEngineAbilityName(std::string abilityName);
|
||||
std::string GetWakeupEngineAbilityName();
|
||||
|
||||
private:
|
||||
DISALLOW_COPY_AND_MOVE(HistoryInfoMgr);
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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 "service_db_helper.h"
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
using namespace OHOS::DistributedKv;
|
||||
#define LOG_TAG "ServiceDbHelper"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
ServiceDbHelper::ServiceDbHelper(const std::string &inAppId, const std::string &inStoreId)
|
||||
{
|
||||
AppId appId = { inAppId };
|
||||
StoreId storeId = { inStoreId };
|
||||
|
||||
Options options = {
|
||||
.createIfMissing = true,
|
||||
.encrypt = false,
|
||||
.autoSync = true,
|
||||
.securityLevel = SecurityLevel::S1,
|
||||
.area = Area::EL1,
|
||||
.kvStoreType = KvStoreType::SINGLE_VERSION,
|
||||
.baseDir = "/data/service/el1/public/database/" + appId.appId
|
||||
};
|
||||
|
||||
INTELL_VOICE_LOG_INFO("inAppId:%{public}s, inStoreId:%{public}s, options.baseDir:%{public}s",
|
||||
inAppId.c_str(), appId.appId.c_str(), options.baseDir.c_str());
|
||||
|
||||
DistributedKvDataManager manager;
|
||||
Status status = manager.GetSingleKvStore(options, appId, storeId, kvStore_);
|
||||
if (status != Status::SUCCESS) {
|
||||
INTELL_VOICE_LOG_INFO("GetSingleKvStore failed, status: %{public}d.", status);
|
||||
} else {
|
||||
INTELL_VOICE_LOG_INFO("GetSingleKvStore success");
|
||||
}
|
||||
}
|
||||
|
||||
ServiceDbHelper::~ServiceDbHelper()
|
||||
{
|
||||
kvStore_ = nullptr;
|
||||
}
|
||||
|
||||
void ServiceDbHelper::SetValue(const std::string &key, const std::string &value)
|
||||
{
|
||||
if (kvStore_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("kvStore_ is nullptr");
|
||||
return;
|
||||
}
|
||||
kvStore_->Put(key, value);
|
||||
}
|
||||
|
||||
std::string ServiceDbHelper::GetValue(const std::string &key)
|
||||
{
|
||||
if (kvStore_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("kvStore_ is nullptr");
|
||||
return "";
|
||||
}
|
||||
Value value;
|
||||
kvStore_->Get(key, value);
|
||||
return value.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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_SERVICE_DB_HELPER_H
|
||||
#define INTELL_VOICE_SERVICE_DB_HELPER_H
|
||||
|
||||
#include "distributed_kv_data_manager.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
class ServiceDbHelper {
|
||||
public:
|
||||
ServiceDbHelper(const std::string &inAppId, const std::string &inStoreId);
|
||||
~ServiceDbHelper();
|
||||
|
||||
void SetValue(const std::string &key, const std::string &value);
|
||||
std::string GetValue(const std::string &key);
|
||||
|
||||
private:
|
||||
std::shared_ptr<DistributedKv::SingleKvStore> kvStore_ = nullptr;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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 "switch_observer.h"
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
#define LOG_TAG "SwitchObserver"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
void SwitchObserver::OnChange()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("OnChange");
|
||||
if (update_) {
|
||||
update_();
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchObserver::SetUpdateFunc(UpdateFunc func)
|
||||
{
|
||||
update_ = func;
|
||||
}
|
||||
} // namespace IntellVoice
|
||||
} // namespace OHOS
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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 SWITCH_OBSERVER_H
|
||||
#define SWITCH_OBSERVER_H
|
||||
|
||||
#include <functional>
|
||||
#include "data_ability_observer_stub.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
using UpdateFunc = std::function<void()>;
|
||||
|
||||
class SwitchObserver : public AAFwk::DataAbilityObserverStub {
|
||||
public:
|
||||
SwitchObserver() = default;
|
||||
virtual ~SwitchObserver() = default;
|
||||
void OnChange() override;
|
||||
void SetUpdateFunc(UpdateFunc func);
|
||||
|
||||
private:
|
||||
UpdateFunc update_ = nullptr;
|
||||
};
|
||||
} // namespace IntellVoice
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* 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 "switch_provider.h"
|
||||
#include "intell_voice_log.h"
|
||||
#include "iservice_registry.h"
|
||||
#include "system_ability_definition.h"
|
||||
|
||||
using namespace OHOS::IntellVoiceUtils;
|
||||
#define LOG_TAG "SwitchProvider"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
const std::string SWITCH_URI_PROXY = "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true";
|
||||
const std::string SWITCH_KEY = "intell_voice_trigger_enabled";
|
||||
|
||||
SwitchProvider::SwitchProvider()
|
||||
{
|
||||
}
|
||||
|
||||
SwitchProvider::~SwitchProvider()
|
||||
{
|
||||
helper_ = nullptr;
|
||||
}
|
||||
|
||||
bool SwitchProvider::Init()
|
||||
{
|
||||
auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
|
||||
if (saManager == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("saManager is nullptr");
|
||||
return false;
|
||||
}
|
||||
auto remoteObj = saManager->GetSystemAbility(INTELL_VOICE_SERVICE_ID);
|
||||
if (remoteObj == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("remoteObj is nullptr");
|
||||
return false;
|
||||
}
|
||||
|
||||
helper_ = DataShare::DataShareHelper::Creator(remoteObj, SWITCH_URI_PROXY);
|
||||
if (helper_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("helper_ is nullptr");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SwitchProvider::RegisterObserver(const sptr<SwitchObserver> &observer)
|
||||
{
|
||||
auto uri = AssembleUri(SWITCH_KEY);
|
||||
helper_->RegisterObserver(uri, observer);
|
||||
}
|
||||
|
||||
void SwitchProvider::UnregisterObserver(const sptr<SwitchObserver> &observer)
|
||||
{
|
||||
auto uri = AssembleUri(SWITCH_KEY);
|
||||
helper_->UnregisterObserver(uri, observer);
|
||||
}
|
||||
|
||||
bool SwitchProvider::QuerySwitchStatus()
|
||||
{
|
||||
std::vector<std::string> columns = {"VALUE"};
|
||||
DataShare::DataSharePredicates predicates;
|
||||
predicates.EqualTo("KEYWORD", SWITCH_KEY);
|
||||
auto uri = AssembleUri(SWITCH_KEY);
|
||||
auto resultSet = helper_->Query(uri, predicates, columns);
|
||||
if (resultSet == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("helper->Query return nullptr");
|
||||
return false;
|
||||
}
|
||||
|
||||
int32_t count;
|
||||
resultSet->GetRowCount(count);
|
||||
if (count == 0) {
|
||||
INTELL_VOICE_LOG_ERROR("not found value");
|
||||
return false;
|
||||
}
|
||||
const int32_t INDEX = 0;
|
||||
resultSet->GoToRow(INDEX);
|
||||
std::string value;
|
||||
resultSet->GetString(INDEX, value);
|
||||
resultSet->Close();
|
||||
|
||||
if (value == "0") {
|
||||
return false;
|
||||
} else if (value == "1") {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Uri SwitchProvider::AssembleUri(const std::string& key)
|
||||
{
|
||||
Uri uri(SWITCH_URI_PROXY + "&key=" + key);
|
||||
return uri;
|
||||
}
|
||||
} // namespace IntellVoice
|
||||
} // namespace OHOS
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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 SWITCH_PROVIDER_H
|
||||
#define SWITCH_PROVIDER_H
|
||||
|
||||
#include "switch_observer.h"
|
||||
#include "datashare_helper.h"
|
||||
#include "intell_voice_generic_factory.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
class SwitchProvider {
|
||||
public:
|
||||
bool Init();
|
||||
void RegisterObserver(const sptr<SwitchObserver> &observer);
|
||||
void UnregisterObserver(const sptr<SwitchObserver> &observer);
|
||||
bool QuerySwitchStatus();
|
||||
|
||||
private:
|
||||
Uri AssembleUri(const std::string& key);
|
||||
|
||||
private:
|
||||
SwitchProvider();
|
||||
~SwitchProvider();
|
||||
|
||||
std::shared_ptr<DataShare::DataShareHelper> helper_ = nullptr;
|
||||
friend class IntellVoiceUtils::UniquePtrFactory<SwitchProvider>;
|
||||
};
|
||||
} // namespace IntellVoice
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* 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 "system_event_observer.h"
|
||||
#include "event_handler.h"
|
||||
#include "common_event_manager.h"
|
||||
#include "common_event_support.h"
|
||||
#include "intell_voice_log.h"
|
||||
#include "intell_voice_service_manager.h"
|
||||
|
||||
using namespace OHOS::AppExecFwk;
|
||||
using namespace OHOS::EventFwk;
|
||||
#define LOG_TAG "SystemEventObserver"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
SystemEventObserver::SystemEventObserver(const OHOS::EventFwk::CommonEventSubscribeInfo &subscribeInfo)
|
||||
: EventFwk::CommonEventSubscriber(subscribeInfo), handler_(nullptr)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("SystemEventObserver create");
|
||||
}
|
||||
|
||||
SystemEventObserver::~SystemEventObserver()
|
||||
{
|
||||
handler_ = nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<SystemEventObserver> SystemEventObserver::Create(
|
||||
const OHOS::EventFwk::CommonEventSubscribeInfo &subscribeInfo)
|
||||
{
|
||||
return std::shared_ptr<SystemEventObserver>(new (std::nothrow) SystemEventObserver(subscribeInfo));
|
||||
}
|
||||
|
||||
bool SystemEventObserver::Subscribe()
|
||||
{
|
||||
if (!OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(GetPtr())) {
|
||||
INTELL_VOICE_LOG_ERROR("SubscribeCommonEvent occur exception.");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SystemEventObserver::Unsubscribe()
|
||||
{
|
||||
if (!OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(GetPtr())) {
|
||||
INTELL_VOICE_LOG_ERROR("UnsubscribeCommonEvent occur exception.");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void SystemEventObserver::SetEventHandler(const std::shared_ptr<OHOS::AppExecFwk::EventHandler> &handler)
|
||||
{
|
||||
handler_ = handler;
|
||||
}
|
||||
|
||||
void SystemEventObserver::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
|
||||
const OHOS::AAFwk::Want& want = eventData.GetWant();
|
||||
std::string action = want.GetAction();
|
||||
if (action.empty()) {
|
||||
INTELL_VOICE_LOG_ERROR("action is empty");
|
||||
return;
|
||||
}
|
||||
|
||||
if (handler_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("handler_ is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
INTELL_VOICE_LOG_INFO("action:%{public}s.", action.c_str());
|
||||
if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
|
||||
INTELL_VOICE_LOG_INFO("COMMON_EVENT_SCREEN_ON");
|
||||
} else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
|
||||
INTELL_VOICE_LOG_INFO("COMMON_EVENT_SCREEN_OFF");
|
||||
} else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
|
||||
INTELL_VOICE_LOG_INFO("COMMON_EVENT_PACKAGE_REMOVED");
|
||||
} else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REPLACED) {
|
||||
INTELL_VOICE_LOG_INFO("COMMON_EVENT_PACKAGE_REPLACED");
|
||||
} else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) {
|
||||
INTELL_VOICE_LOG_INFO("COMMON_EVENT_PACKAGE_DATA_CLEARED");
|
||||
} else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
|
||||
INTELL_VOICE_LOG_INFO("COMMON_EVENT_USER_UNLOCKED");
|
||||
IntellVoiceServiceManager::GetInstance()->OnUserUnlock();
|
||||
} else {
|
||||
INTELL_VOICE_LOG_INFO("unkonw event");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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 SYSTEM_EVENT_OBSERVER_H
|
||||
#define SYSTEM_EVENT_OBSERVER_H
|
||||
|
||||
#include <memory>
|
||||
#include "common_event_subscriber.h"
|
||||
#include "event_handler.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
class SystemEventObserver : public OHOS::EventFwk::CommonEventSubscriber,
|
||||
public std::enable_shared_from_this<SystemEventObserver> {
|
||||
public:
|
||||
~SystemEventObserver();
|
||||
static std::shared_ptr<SystemEventObserver> Create(const OHOS::EventFwk::CommonEventSubscribeInfo &subscribeInfo);
|
||||
std::shared_ptr<SystemEventObserver> GetPtr()
|
||||
{
|
||||
return shared_from_this();
|
||||
}
|
||||
void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData) override;
|
||||
void SetEventHandler(const std::shared_ptr<OHOS::AppExecFwk::EventHandler> &handler);
|
||||
bool Subscribe();
|
||||
bool Unsubscribe();
|
||||
|
||||
private:
|
||||
explicit SystemEventObserver(const OHOS::EventFwk::CommonEventSubscribeInfo &subscribeInfo);
|
||||
|
||||
std::shared_ptr<OHOS::AppExecFwk::EventHandler> handler_ = nullptr;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* 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 "wakeup_adapter_listener.h"
|
||||
|
||||
#include <thread>
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
#define LOG_TAG "WakeupAdapterListener"
|
||||
|
||||
using namespace OHOS::HDI::IntelligentVoice::Engine::V1_0;
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
WakeupAdapterListener::WakeupAdapterListener(OnWakeupEventCb wakeupEventCb) : wakeupEventCb_(wakeupEventCb)
|
||||
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("constructor");
|
||||
}
|
||||
|
||||
WakeupAdapterListener::~WakeupAdapterListener()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("destructor");
|
||||
}
|
||||
|
||||
void WakeupAdapterListener::SetCallback(const sptr<IIntelligentVoiceEngineCallback> &cb)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (cb == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("cb is nullptr");
|
||||
return;
|
||||
}
|
||||
cb_ = cb;
|
||||
if (historyEvent_ != nullptr) {
|
||||
cb_->OnIntellVoiceEngineEvent(*(historyEvent_.get()));
|
||||
historyEvent_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void WakeupAdapterListener::OnIntellVoiceHdiEvent(const IntellVoiceEngineCallBackEvent &event)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("OnIntellVoiceHdiEvent");
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (cb_ == nullptr) {
|
||||
INTELL_VOICE_LOG_WARN("cb_ is nullptr");
|
||||
BackupCallBackEvent(event);
|
||||
wakeupEventCb_(event.msgId, event.result);
|
||||
return;
|
||||
}
|
||||
|
||||
historyEvent_ = nullptr;
|
||||
}
|
||||
|
||||
if (event.msgId == INTELL_VOICE_ENGINE_MSG_RECOGNIZE_COMPLETE) {
|
||||
cb_->OnIntellVoiceEngineEvent(event);
|
||||
}
|
||||
|
||||
wakeupEventCb_(event.msgId, event.result);
|
||||
}
|
||||
|
||||
void WakeupAdapterListener::BackupCallBackEvent(const IntellVoiceEngineCallBackEvent &event)
|
||||
{
|
||||
if (event.msgId != INTELL_VOICE_ENGINE_MSG_RECOGNIZE_COMPLETE) {
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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 WAKEUP_ADAPTER_LISTENER_H
|
||||
#define WAKEUP_ADAPTER_LISTENER_H
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include "intell_voice_adapter_listener.h"
|
||||
#include "i_intell_voice_engine_callback.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
using OnWakeupEventCb = std::function<void(int32_t, int32_t)>;
|
||||
|
||||
class WakeupAdapterListener : public IntellVoiceAdapterListener {
|
||||
public:
|
||||
explicit WakeupAdapterListener(OnWakeupEventCb wakeupEventCb);
|
||||
~WakeupAdapterListener();
|
||||
|
||||
void SetCallback(const sptr<IIntelligentVoiceEngineCallback> &cb);
|
||||
void OnIntellVoiceHdiEvent(
|
||||
const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineCallBackEvent &event) override;
|
||||
|
||||
private:
|
||||
void BackupCallBackEvent(const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineCallBackEvent &event);
|
||||
|
||||
private:
|
||||
std::mutex mutex_;
|
||||
std::shared_ptr<OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineCallBackEvent> historyEvent_ = nullptr;
|
||||
sptr<IIntelligentVoiceEngineCallback> cb_ = nullptr;
|
||||
OnWakeupEventCb wakeupEventCb_ = nullptr;
|
||||
};
|
||||
} // namespace IntellVoice
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -0,0 +1,337 @@
|
||||
/*
|
||||
* 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 "wakeup_engine.h"
|
||||
#include <fstream>
|
||||
#include "securec.h"
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
#include "v1_0/iintell_voice_engine_manager.h"
|
||||
#include "v1_0/iintell_voice_engine_callback.h"
|
||||
|
||||
#include "time_util.h"
|
||||
#include "scope_guard.h"
|
||||
#include "adapter_callback_service.h"
|
||||
#include "intell_voice_service_manager.h"
|
||||
#include "ability_manager_client.h"
|
||||
#include "memory_guard.h"
|
||||
|
||||
using namespace OHOS::HDI::IntelligentVoice::Engine::V1_0;
|
||||
using namespace OHOS::IntellVoiceUtils;
|
||||
using namespace OHOS::AudioStandard;
|
||||
#define LOG_TAG "WakeupEngine"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
static constexpr uint32_t MIN_BUFFER_SIZE = 1280;
|
||||
static constexpr uint32_t INTERVAL = 50;
|
||||
static const std::string RECOGNITION_FILE = "/data/data/recognition.pcm";
|
||||
|
||||
WakeupEngine::WakeupEngine()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
|
||||
capturerOptions_.streamInfo.channels = AudioChannel::MONO;
|
||||
capturerOptions_.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_16000;
|
||||
capturerOptions_.streamInfo.format = AudioSampleFormat::SAMPLE_S16LE;
|
||||
capturerOptions_.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
|
||||
capturerOptions_.capturerInfo.capturerFlags = 0;
|
||||
}
|
||||
|
||||
WakeupEngine::~WakeupEngine()
|
||||
{
|
||||
auto mgr = IIntellVoiceEngineManager::Get();
|
||||
if (mgr != nullptr) {
|
||||
mgr->ReleaseAdapter(desc_);
|
||||
}
|
||||
adapter_ = nullptr;
|
||||
callback_ = nullptr;
|
||||
}
|
||||
|
||||
void WakeupEngine::OnDetected()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("on detected");
|
||||
StartAbility();
|
||||
SetParameter("VprTrdType=0;WakeupScene=0");
|
||||
Start(true);
|
||||
}
|
||||
|
||||
void WakeupEngine::OnWakeupEvent(int32_t msgId, int32_t result)
|
||||
{
|
||||
if (msgId == INTELL_VOICE_ENGINE_MSG_RECOGNIZE_COMPLETE) {
|
||||
std::thread(&WakeupEngine::OnWakeupRecognition, this).detach();
|
||||
}
|
||||
}
|
||||
|
||||
void WakeupEngine::OnWakeupRecognition()
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("on wakeup recognition");
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
if (fileSource_ != nullptr) {
|
||||
fileSource_->Stop();
|
||||
fileSource_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
const auto &manager = IntellVoiceServiceManager::GetInstance();
|
||||
if (manager != nullptr) {
|
||||
manager->StartDetection();
|
||||
}
|
||||
|
||||
Stop();
|
||||
}
|
||||
|
||||
bool WakeupEngine::SetCallback()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (adapter_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("adapter is nullptr");
|
||||
return false;
|
||||
}
|
||||
|
||||
adapterListener_ = std::make_shared<WakeupAdapterListener>(
|
||||
std::bind(&WakeupEngine::OnWakeupEvent, this, std::placeholders::_1, std::placeholders::_2));
|
||||
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;
|
||||
}
|
||||
|
||||
bool WakeupEngine::Init()
|
||||
{
|
||||
desc_.adapterType = WAKEUP_ADAPTER_TYPE;
|
||||
auto mgr = IIntellVoiceEngineManager::Get();
|
||||
if (mgr == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to get engine manager");
|
||||
return false;
|
||||
}
|
||||
|
||||
mgr->CreateAdapter(desc_, adapter_);
|
||||
if (adapter_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("adapter is nullptr");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!SetCallback()) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to set callback");
|
||||
return false;
|
||||
}
|
||||
|
||||
IntellVoiceEngineInfo info = {
|
||||
.wakeupPhrase = "\xE5\xB0\x8F\xE8\x89\xBA\xE5\xB0\x8F\xE8\x89\xBA",
|
||||
.isPcmFromExternal = false,
|
||||
.minBufSize = 1280,
|
||||
.sampleChannels = 1,
|
||||
.bitsPerSample = 16,
|
||||
.sampleRate = 16000,
|
||||
};
|
||||
|
||||
if (Attach(info) != 0) {
|
||||
INTELL_VOICE_LOG_ERROR("failed to attach");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void WakeupEngine::SetCallback(sptr<IRemoteObject> object)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (adapterListener_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("adapter listener is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
sptr<IIntelligentVoiceEngineCallback> callback = iface_cast<IIntelligentVoiceEngineCallback>(object);
|
||||
if (callback == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("callback is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
adapterListener_->SetCallback(callback);
|
||||
}
|
||||
|
||||
int32_t WakeupEngine::Attach(const IntellVoiceEngineInfo &info)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
INTELL_VOICE_LOG_INFO("attach");
|
||||
if (adapter_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("adapter is nullptr");
|
||||
return -1;
|
||||
}
|
||||
|
||||
isPcmFromExternal_ = info.isPcmFromExternal;
|
||||
|
||||
IntellVoiceEngineAdapterInfo adapterInfo = {
|
||||
.wakeupPhrase = info.wakeupPhrase,
|
||||
.minBufSize = info.minBufSize,
|
||||
.sampleChannels = info.sampleChannels,
|
||||
.bitsPerSample = info.bitsPerSample,
|
||||
.sampleRate = info.sampleRate,
|
||||
};
|
||||
return adapter_->Attach(adapterInfo);
|
||||
}
|
||||
|
||||
int32_t WakeupEngine::Detach(void)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (adapter_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("adapter is nullptr");
|
||||
return -1;
|
||||
}
|
||||
return adapter_->Detach();
|
||||
}
|
||||
|
||||
int32_t WakeupEngine::Start(bool isLast)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
INTELL_VOICE_LOG_INFO("enter");
|
||||
if (adapter_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("adapter is nullptr");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (IntellVoiceServiceManager::GetInstance()->ApplyArbitration(INTELL_VOICE_WAKEUP, ENGINE_EVENT_START) !=
|
||||
ARBITRATION_OK) {
|
||||
INTELL_VOICE_LOG_ERROR("policy manager reject to start engine");
|
||||
return 0;
|
||||
}
|
||||
|
||||
StartInfo info = {
|
||||
.isLast = isLast,
|
||||
};
|
||||
if (adapter_->Start(info)) {
|
||||
INTELL_VOICE_LOG_ERROR("start adapter failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (isPcmFromExternal_) {
|
||||
INTELL_VOICE_LOG_INFO("pcm is from external");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!StartFileSource()) {
|
||||
INTELL_VOICE_LOG_ERROR("start file source failed");
|
||||
adapter_->Stop();
|
||||
return -1;
|
||||
}
|
||||
|
||||
INTELL_VOICE_LOG_INFO("exit");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void WakeupEngine::StartAbility()
|
||||
{
|
||||
AAFwk::Want want;
|
||||
const std::unique_ptr<HistoryInfoMgr> &historyInfoMgr =
|
||||
IntellVoiceServiceManager::GetInstance()->GetHistoryInfoMgr();
|
||||
if (historyInfoMgr == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("historyInfoMgr is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
std::string bundleName = historyInfoMgr->GetWakeupEngineBundleName();
|
||||
std::string abilityName = historyInfoMgr->GetWakeupEngineAbilityName();
|
||||
INTELL_VOICE_LOG_INFO("bundleName:%{public}s, abilityName:%{public}s", bundleName.c_str(), abilityName.c_str());
|
||||
want.SetElementName(bundleName, abilityName);
|
||||
want.SetParam("serviceName", std::string("intell_voice"));
|
||||
AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
|
||||
}
|
||||
|
||||
int32_t WakeupEngine::SetParameter(const std::string &keyValueList)
|
||||
{
|
||||
if (SetParameterInner(keyValueList)) {
|
||||
INTELL_VOICE_LOG_INFO("inner parameter");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return EngineBase::SetParameter(keyValueList);
|
||||
}
|
||||
|
||||
bool WakeupEngine::SetParameterInner(const std::string &keyValueList)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
const auto &manager = IntellVoiceServiceManager::GetInstance();
|
||||
if (manager == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR();
|
||||
return false;
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> kvpairs;
|
||||
SplitStringToKVPair(keyValueList, kvpairs);
|
||||
for (auto it : kvpairs) {
|
||||
if (it.first == std::string("start_stream")) {
|
||||
INTELL_VOICE_LOG_INFO("start stream:%{public}s", it.second.c_str());
|
||||
manager->StopDetection();
|
||||
return true;
|
||||
}
|
||||
if (it.first == std::string("stop_stream")) {
|
||||
INTELL_VOICE_LOG_INFO("stop stream:%{public}s", it.second.c_str());
|
||||
manager->StartDetection();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool WakeupEngine::StartFileSource()
|
||||
{
|
||||
auto listener = std::make_unique<FileSourceListener>(
|
||||
[&](uint8_t *buffer, uint32_t size) {
|
||||
if (adapter_ != nullptr) {
|
||||
std::vector<uint8_t> audioBuff(&buffer[0], &buffer[size]);
|
||||
adapter_->WriteAudio(audioBuff);
|
||||
}
|
||||
},
|
||||
[&](bool isError)
|
||||
{
|
||||
INTELL_VOICE_LOG_INFO("end of pcm, isError:%d", isError);
|
||||
if (adapter_ != nullptr) {
|
||||
adapter_->SetParameter("end_of_pcm=true");
|
||||
}
|
||||
});
|
||||
if (listener == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create listener failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
fileSource_ = std::make_unique<FileSource>(MIN_BUFFER_SIZE, INTERVAL, RECOGNITION_FILE, std::move(listener));
|
||||
if (fileSource_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("create file source failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!fileSource_->Start()) {
|
||||
INTELL_VOICE_LOG_ERROR("start capturer failed");
|
||||
fileSource_ = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
} // namespace IntellVoice
|
||||
} // namespace OHOS
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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 WAKEUP_ENGINE_H
|
||||
#define WAKEUP_ENGINE_H
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "engine_base.h"
|
||||
#include "intell_voice_engine_stub.h"
|
||||
#include "wakeup_adapter_listener.h"
|
||||
#include "v1_0/iintell_voice_engine_callback.h"
|
||||
|
||||
#include "audio_info.h"
|
||||
#include "file_source.h"
|
||||
#include "intell_voice_generic_factory.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceEngine {
|
||||
class WakeupEngine : public EngineBase {
|
||||
public:
|
||||
~WakeupEngine();
|
||||
bool Init() override;
|
||||
void SetCallback(sptr<IRemoteObject> object) override;
|
||||
int32_t Attach(const IntellVoiceEngineInfo &info) override;
|
||||
int32_t Detach(void) override;
|
||||
int32_t Start(bool isLast) override;
|
||||
int32_t SetParameter(const std::string &keyValueList) override;
|
||||
|
||||
void OnDetected() override;
|
||||
|
||||
private:
|
||||
WakeupEngine();
|
||||
void OnWakeupEvent(int32_t msgId, int32_t result);
|
||||
|
||||
void OnWakeupRecognition();
|
||||
bool SetCallback();
|
||||
bool StartFileSource();
|
||||
void StartAbility();
|
||||
bool SetParameterInner(const std::string &keyValueList);
|
||||
|
||||
private:
|
||||
bool isPcmFromExternal_ = false;
|
||||
std::shared_ptr<WakeupAdapterListener> adapterListener_ = nullptr;
|
||||
sptr<OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineCallback> callback_ = nullptr;
|
||||
OHOS::AudioStandard::AudioCapturerOptions capturerOptions_;
|
||||
std::unique_ptr<FileSource> fileSource_ = nullptr;
|
||||
friend class IntellVoiceUtils::SptrFactory<WakeupEngine>;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Executable
+33
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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 I_INTELL_VOICE_TRIGGER_CONNECTOR_CALLBACK_H
|
||||
#define I_INTELL_VOICE_TRIGGER_CONNECTOR_CALLBACK_H
|
||||
|
||||
#include "v1_0/intell_voice_trigger_types.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceTrigger {
|
||||
using OHOS::HDI::IntelligentVoice::Trigger::V1_0::IntellVoiceRecognitionEvent;
|
||||
|
||||
class IIntellVoiceTriggerConnectorCallback {
|
||||
public:
|
||||
IIntellVoiceTriggerConnectorCallback() = default;
|
||||
virtual ~IIntellVoiceTriggerConnectorCallback() = default;
|
||||
|
||||
virtual void OnRecognition(int32_t modelHandle, const struct IntellVoiceRecognitionEvent &event) = 0;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Executable
+37
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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 I_INTELL_VOICE_TRIGGER_CONNECTOR_INTERNAL_H
|
||||
#define I_INTELL_VOICE_TRIGGER_CONNECTOR_INTERNAL_H
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "trigger_connector_common_type.h"
|
||||
#include "i_intell_voice_trigger_connector_module.h"
|
||||
#include "i_intell_voice_trigger_connector_callback.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceTrigger {
|
||||
class IIntellVoiceTriggerConnectorInternal {
|
||||
public:
|
||||
IIntellVoiceTriggerConnectorInternal() = default;
|
||||
virtual ~IIntellVoiceTriggerConnectorInternal() = default;
|
||||
|
||||
virtual std::vector<TriggerConnectorModuleDesc> ListModuleDescriptors() = 0;
|
||||
virtual std::shared_ptr<IIntellVoiceTriggerConnectorModule> GetModule(const std::string &adapterName,
|
||||
std::shared_ptr<IIntellVoiceTriggerConnectorCallback> callback) = 0;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Executable
+36
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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 I_INTELL_VOICE_TRIGGER_CONNECTOR_MODULE_H
|
||||
#define I_INTELL_VOICE_TRIGGER_CONNECTOR_MODULE_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include "trigger_base_type.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceTrigger {
|
||||
class IIntellVoiceTriggerConnectorModule {
|
||||
public:
|
||||
IIntellVoiceTriggerConnectorModule() = default;
|
||||
virtual ~IIntellVoiceTriggerConnectorModule() = default;
|
||||
|
||||
virtual int32_t LoadModel(std::shared_ptr<GenericTriggerModel> model, int32_t &modelHandle) = 0;
|
||||
virtual int32_t UnloadModel(int32_t modelHandle) = 0;
|
||||
virtual int32_t Start(int32_t modelHandle) = 0;
|
||||
virtual int32_t Stop(int32_t modelHandle) = 0;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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 "trigger_callback_impl.h"
|
||||
#include "intell_voice_log.h"
|
||||
|
||||
using namespace OHOS::HDI::IntelligentVoice::Trigger::V1_0;
|
||||
|
||||
#define LOG_TAG "TriggerCallbackImpl"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceTrigger {
|
||||
TriggerCallbackImpl::TriggerCallbackImpl(std::shared_ptr<IIntellVoiceTriggerAdapterListener> listener)
|
||||
:listener_(listener)
|
||||
{
|
||||
}
|
||||
|
||||
int32_t TriggerCallbackImpl::OnRecognitionHdiEvent(const IntellVoiceRecognitionEvent &event, int32_t cookie)
|
||||
{
|
||||
if (listener_ == nullptr) {
|
||||
INTELL_VOICE_LOG_ERROR("listener_ is nullptr");
|
||||
return -1;
|
||||
}
|
||||
|
||||
listener_->OnRecognitionHdiEvent(event, cookie);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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_TRIGGER_CALLBACK_IMPL_H
|
||||
#define INTELL_VOICE_TRIGGER_CALLBACK_IMPL_H
|
||||
|
||||
#include "v1_0/iintell_voice_trigger_callback.h"
|
||||
#include "i_intell_voice_trigger_adapter_listener.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace IntellVoiceTrigger {
|
||||
using OHOS::HDI::IntelligentVoice::Trigger::V1_0::IntellVoiceRecognitionEvent;
|
||||
using OHOS::HDI::IntelligentVoice::Trigger::V1_0::IIntellVoiceTriggerCallback;
|
||||
|
||||
class TriggerCallbackImpl final : public IIntellVoiceTriggerCallback {
|
||||
public:
|
||||
explicit TriggerCallbackImpl(std::shared_ptr<IIntellVoiceTriggerAdapterListener> listener);
|
||||
virtual ~TriggerCallbackImpl() = default;
|
||||
|
||||
int32_t OnRecognitionHdiEvent(const IntellVoiceRecognitionEvent &event, int32_t cookie) override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<IIntellVoiceTriggerAdapterListener> listener_ = nullptr;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user