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 #include <unistd.h>
16 #include <fstream>
17 #include <iostream>
18 #include <gtest/gtest.h>
19
20 #include "intell_voice_log.h"
21 #include "engine_event_callback.h"
22
23 #define LOG_TAG "EngineEventCallback"
24
25 using namespace std;
26
27 namespace {
28 constexpr int32_t ENROLL_CNT = 3;
29 static constexpr uint32_t BUFFER_SIZE = 1280;
30 static constexpr uint32_t WAIT_TIME = 1000 * 100;
31 }
32
33 namespace OHOS {
34 namespace IntellVoiceTests {
35 const std::string TEST_RESOURCE_PATH = "/data/test/resource/";
36
OnEvent(const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineCallBackEvent & event)37 void EngineEventCallback::OnEvent(
38 const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineCallBackEvent &event)
39 {
40 INTELL_VOICE_LOG_INFO("OnEvent EngineCallBackInfo: msgId: %{public}d, errCode: %{public}d, context: %{public}s",
41 event.msgId,
42 event.result,
43 event.info.c_str());
44
45 if (event.msgId == HDI::IntelligentVoice::Engine::V1_0::INTELL_VOICE_ENGINE_MSG_INIT_DONE) {
46 EXPECT_EQ(event.result, HDI::IntelligentVoice::Engine::V1_0::INTELL_VOICE_ENGINE_OK);
47 startCnt_ = 1;
48 engine_->Start(false);
49 std::thread([this]() {
50 usleep(WAIT_TIME);
51 EngineEventCallback::ReadFile(TEST_RESOURCE_PATH + "one.pcm");
52 }).detach();
53 }
54 if (event.msgId == HDI::IntelligentVoice::Engine::V1_0::INTELL_VOICE_ENGINE_MSG_ENROLL_COMPLETE) {
55 EXPECT_EQ(event.result, HDI::IntelligentVoice::Engine::V1_0::INTELL_VOICE_ENGINE_OK);
56 if (startCnt_ < ENROLL_CNT) {
57 ++startCnt_;
58 std::thread([this]() {
59 engine_->Start(startCnt_ == ENROLL_CNT ? true : false);
60 usleep(WAIT_TIME);
61 EngineEventCallback::ReadFile(TEST_RESOURCE_PATH + "one.pcm");
62 }).detach();
63 } else if (startCnt_ == ENROLL_CNT) {
64 std::thread([this]() {
65 engine_->SetParameter("CommitEnrollment=true");
66 }).detach();
67 }
68 }
69
70 if (event.msgId == HDI::IntelligentVoice::Engine::V1_0::INTELL_VOICE_ENGINE_MSG_COMMIT_ENROLL_COMPLETE) {
71 EXPECT_EQ(event.result, HDI::IntelligentVoice::Engine::V1_0::INTELL_VOICE_ENGINE_OK);
72 waitForResult_->SetIsReady();
73 }
74 }
75
ReadFile(const std::string & path)76 void EngineEventCallback::ReadFile(const std::string &path)
77 {
78 INTELL_VOICE_LOG_INFO("path: %{public}s", path.c_str());
79 ifstream infile;
80 infile.open(path, ios::in | ios::binary);
81
82 if (!infile.is_open()) {
83 INTELL_VOICE_LOG_INFO("open file failed");
84 }
85
86 infile.seekg(0, infile.end);
87 pcmSize_ = static_cast<uint32_t>(infile.tellg());
88 pcmData_ = std::shared_ptr<uint8_t>(new uint8_t[pcmSize_], [](uint8_t *p) { delete[] p; });
89 if (pcmData_ == nullptr) {
90 INTELL_VOICE_LOG_INFO("pcmData_ is null");
91 return;
92 }
93 INTELL_VOICE_LOG_INFO("read pcm, pcmSize:%{public}u", pcmSize_);
94 infile.seekg(0, infile.beg);
95 infile.read(reinterpret_cast<char *>(pcmData_.get()), pcmSize_);
96 infile.close();
97
98 for (uint32_t i = 0; i < pcmSize_ / BUFFER_SIZE; i++) {
99 engine_->WriteAudio(pcmData_.get() + i * BUFFER_SIZE, BUFFER_SIZE);
100 }
101 engine_->SetParameter("end_of_pcm=true");
102 }
103 } // namespace IntellVoiceTests
104 } // namespace OHOS
105