• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "intell_voice_engine_proxy.h"
17 #include "intell_voice_log.h"
18 
19 #define LOG_TAG "IntellVoiceEngineProxy"
20 
21 namespace OHOS {
22 namespace IntellVoiceEngine {
SetCallback(sptr<IRemoteObject> object)23 void IntellVoiceEngineProxy::SetCallback(sptr<IRemoteObject> object)
24 {
25     MessageParcel data;
26     MessageParcel reply;
27     MessageOption option;
28     if (object == nullptr) {
29         INTELL_VOICE_LOG_ERROR("object is null");
30         return;
31     }
32 
33     data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
34     data.WriteRemoteObject(object);
35     int32_t error = Remote()->SendRequest(INTELL_VOICE_ENGINE_SET_CALLBACK, data, reply, option);
36     if (error != 0) {
37         INTELL_VOICE_LOG_ERROR("send request error: %{public}d", error);
38     }
39 }
40 
Attach(const IntellVoiceEngineInfo & info)41 int32_t IntellVoiceEngineProxy::Attach(const IntellVoiceEngineInfo &info)
42 {
43     MessageParcel data;
44     MessageParcel reply;
45     MessageOption option;
46 
47     data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
48     data.WriteString(info.wakeupPhrase);
49     data.WriteBool(info.isPcmFromExternal);
50     data.WriteInt32(info.minBufSize);
51     data.WriteInt32(info.sampleChannels);
52     data.WriteInt32(info.bitsPerSample);
53     data.WriteInt32(info.sampleRate);
54 
55     int32_t error = Remote()->SendRequest(INTELL_VOICE_ENGINE_ATTACH, data, reply, option);
56     if (error != 0) {
57         INTELL_VOICE_LOG_ERROR("attach error: %{public}d", error);
58         return -1;
59     }
60 
61     INTELL_VOICE_LOG_INFO("Attach call");
62     return reply.ReadInt32();
63 }
64 
Detach(void)65 int32_t IntellVoiceEngineProxy::Detach(void)
66 {
67     MessageParcel data;
68     MessageParcel reply;
69     MessageOption option;
70 
71     data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
72     int32_t error = Remote()->SendRequest(INTELL_VOICE_ENGINE_DETACH, data, reply, option);
73     if (error != 0) {
74         INTELL_VOICE_LOG_ERROR("detach error: %{public}d", error);
75         return -1;
76     }
77     return reply.ReadInt32();
78 }
79 
SetParameter(const std::string & keyValueList)80 int32_t IntellVoiceEngineProxy::SetParameter(const std::string &keyValueList)
81 {
82     MessageParcel data;
83     MessageParcel reply;
84     MessageOption option;
85 
86     data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
87     data.WriteString(keyValueList);
88     int32_t error = Remote()->SendRequest(INTELL_VOICE_ENGINE_SET_PARAMETER, data, reply, option);
89     if (error != 0) {
90         INTELL_VOICE_LOG_ERROR("set parameter error: %{public}d", error);
91         return -1;
92     }
93     return reply.ReadInt32();
94 }
95 
GetParameter(const std::string & key)96 std::string IntellVoiceEngineProxy::GetParameter(const std::string &key)
97 {
98     MessageParcel data;
99     MessageParcel reply;
100     MessageOption option;
101 
102     data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
103     data.WriteString(key);
104 
105     int32_t error = Remote()->SendRequest(INTELL_VOICE_ENGINE_GET_PARAMETER, data, reply, option);
106     if (error != 0) {
107         INTELL_VOICE_LOG_ERROR("get parameter error: %{public}d", error);
108         return "";
109     }
110     return reply.ReadString();
111 }
112 
Start(bool isLast)113 int32_t IntellVoiceEngineProxy::Start(bool isLast)
114 {
115     MessageParcel data;
116     MessageParcel reply;
117     MessageOption option;
118 
119     data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
120     data.WriteBool(isLast);
121 
122     int32_t error = Remote()->SendRequest(INTELL_VOICE_ENGINE_START, data, reply, option);
123     if (error != 0) {
124         INTELL_VOICE_LOG_ERROR("start error: %{public}d", error);
125         return -1;
126     }
127     return reply.ReadInt32();
128 }
129 
Stop(void)130 int32_t IntellVoiceEngineProxy::Stop(void)
131 {
132     MessageParcel data;
133     MessageParcel reply;
134     MessageOption option;
135 
136     data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
137 
138     int32_t error = Remote()->SendRequest(INTELL_VOICE_ENGINE_STOP, data, reply, option);
139     if (error != 0) {
140         INTELL_VOICE_LOG_ERROR("stop error: %{public}d", error);
141         return -1;
142     }
143     return reply.ReadInt32();
144 }
145 
WriteAudio(const uint8_t * buffer,uint32_t size)146 int32_t IntellVoiceEngineProxy::WriteAudio(const uint8_t *buffer, uint32_t size)
147 {
148     MessageParcel data;
149     MessageParcel reply;
150     MessageOption option;
151 
152     data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
153     data.WriteUint32(size);
154     data.WriteBuffer(buffer, size);
155 
156     int32_t error = Remote()->SendRequest(INTELL_VOICE_ENGINE_WRITE_AUDIO, data, reply, option);
157     if (error != 0) {
158         INTELL_VOICE_LOG_ERROR("write audio error: %{public}d", error);
159         return -1;
160     }
161     return reply.ReadInt32();
162 }
163 
StartCapturer(int32_t channels)164 int32_t IntellVoiceEngineProxy::StartCapturer(int32_t channels)
165 {
166     MessageParcel data;
167     MessageParcel reply;
168     MessageOption option;
169 
170     data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
171     data.WriteInt32(channels);
172     int32_t error = Remote()->SendRequest(INTELL_VOICE_ENGINE_STAET_CAPTURER, data, reply, option);
173     if (error != 0) {
174         INTELL_VOICE_LOG_ERROR("start capturer error: %{public}d", error);
175         return -1;
176     }
177     return reply.ReadInt32();
178 }
179 
Read(std::vector<uint8_t> & data)180 int32_t IntellVoiceEngineProxy::Read(std::vector<uint8_t> &data)
181 {
182     MessageParcel parcelData;
183     MessageParcel reply;
184     MessageOption option;
185 
186     parcelData.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
187     int32_t error = Remote()->SendRequest(INTELL_VOICE_ENGINE_READ, parcelData, reply, option);
188     if (error != 0) {
189         INTELL_VOICE_LOG_ERROR("read error: %{public}d", error);
190         return -1;
191     }
192 
193     int ret = reply.ReadInt32();
194     if (ret != 0) {
195         INTELL_VOICE_LOG_ERROR("failed to read wakeup pcm, ret:%{public}d", ret);
196         return ret;
197     }
198     uint32_t size = reply.ReadUint32();
199     if (size == 0) {
200         INTELL_VOICE_LOG_ERROR("buffer size is zero");
201         return -1;
202     }
203     const uint8_t *buff = reply.ReadBuffer(size);
204     if (buff == nullptr) {
205         INTELL_VOICE_LOG_ERROR("buffer is nullptr");
206         return -1;
207     }
208     data.resize(size);
209     std::copy(buff, buff + size, data.begin());
210     return ret;
211 }
212 
StopCapturer()213 int32_t IntellVoiceEngineProxy::StopCapturer()
214 {
215     MessageParcel data;
216     MessageParcel reply;
217     MessageOption option;
218 
219     data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
220     int32_t error = Remote()->SendRequest(INTELL_VOICE_ENGINE_STOP_CAPTURER, data, reply, option);
221     if (error != 0) {
222         INTELL_VOICE_LOG_ERROR("stop capturer error: %{public}d", error);
223         return -1;
224     }
225     return reply.ReadInt32();
226 }
227 
GetWakeupPcm(std::vector<uint8_t> & data)228 int32_t IntellVoiceEngineProxy::GetWakeupPcm(std::vector<uint8_t> &data)
229 {
230     MessageParcel parcelData;
231     MessageParcel reply;
232     MessageOption option;
233 
234     parcelData.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
235     int32_t error = Remote()->SendRequest(INTELL_VOICE_ENGINE_GET_WAKEUP_PCM, parcelData, reply, option);
236     if (error != 0) {
237         INTELL_VOICE_LOG_ERROR("get wakeup pcm error: %{public}d", error);
238         return -1;
239     }
240     int ret = reply.ReadInt32();
241     if (ret != 0) {
242         INTELL_VOICE_LOG_ERROR("failed to get wakeup pcm, ret:%{public}d", ret);
243         return ret;
244     }
245     uint32_t size = reply.ReadUint32();
246     if (size == 0) {
247         INTELL_VOICE_LOG_ERROR("buffer size is zero");
248         return -1;
249     }
250     const uint8_t *buff = reply.ReadBuffer(size);
251     if (buff == nullptr) {
252         INTELL_VOICE_LOG_ERROR("buffer is nullptr");
253         return -1;
254     }
255     data.resize(size);
256     std::copy(buff, buff + size, data.begin());
257     return ret;
258 }
259 
Evaluate(const std::string & word,EvaluationResult & result)260 int32_t IntellVoiceEngineProxy::Evaluate(const std::string &word, EvaluationResult &result)
261 {
262     MessageParcel data;
263     MessageParcel reply;
264     MessageOption option;
265 
266     data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
267     data.WriteString(word);
268     int32_t error = Remote()->SendRequest(INTELL_VOICE_ENGINE_EVALUATE, data, reply, option);
269     if (error != 0) {
270         INTELL_VOICE_LOG_ERROR("evaluate error: %{public}d", error);
271         return -1;
272     }
273     int32_t ret = reply.ReadInt32();
274     if (ret != 0) {
275         INTELL_VOICE_LOG_ERROR("failed to evaluate");
276         return ret;
277     }
278 
279     result.score = reply.ReadInt32();
280     result.resultCode = reply.ReadInt32();
281     return ret;
282 }
283 
NotifyHeadsetWakeEvent()284 int32_t IntellVoiceEngineProxy::NotifyHeadsetWakeEvent()
285 {
286     INTELL_VOICE_LOG_INFO("enter");
287     MessageParcel data;
288     MessageParcel reply;
289     MessageOption option;
290 
291     data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
292     int32_t error = Remote()->SendRequest(INTELL_VOICE_ENGINE_NOTIFY_HEADSET_WAKE_EVENT, data, reply, option);
293     if (error != 0) {
294         INTELL_VOICE_LOG_ERROR("notify headset wakeup event error: %{public}d", error);
295         return -1;
296     }
297     return reply.ReadInt32();
298 }
299 
NotifyHeadsetHostEvent(HeadsetHostEventType event)300 int32_t IntellVoiceEngineProxy::NotifyHeadsetHostEvent(HeadsetHostEventType event)
301 {
302     INTELL_VOICE_LOG_INFO("enter");
303     MessageParcel data;
304     MessageParcel reply;
305     MessageOption option;
306 
307     data.WriteInterfaceToken(IIntellVoiceEngine::GetDescriptor());
308     data.WriteInt32(event);
309     int32_t error = Remote()->SendRequest(INTELL_VOICE_ENGINE_NOTIFY_HEADSET_HOSTEVENT, data, reply, option);
310     if (error != 0) {
311         INTELL_VOICE_LOG_ERROR("notify headset host event error: %{public}d", error);
312         return -1;
313     }
314     return reply.ReadInt32();
315 }
316 }
317 }
318