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 "audio_source.h"
16
17 #include "securec.h"
18 #include "intell_voice_log.h"
19 #include "memory_guard.h"
20
21 #define LOG_TAG "AudioSource"
22
23 using namespace OHOS::AudioStandard;
24 using namespace OHOS::IntellVoiceUtils;
25
26 namespace OHOS {
27 namespace IntellVoiceEngine {
28 static const std::string CACHE_PATH = "/data/data/intell_voice/cache/";
29
AudioSource(uint32_t minBufferSize,uint32_t bufferCnt,std::unique_ptr<AudioSourceListener> listener,const OHOS::AudioStandard::AudioCapturerOptions & capturerOptions)30 AudioSource::AudioSource(uint32_t minBufferSize, uint32_t bufferCnt,
31 std::unique_ptr<AudioSourceListener> listener, const OHOS::AudioStandard::AudioCapturerOptions &capturerOptions)
32 : minBufferSize_(minBufferSize), bufferCnt_(bufferCnt), listener_(std::move(listener))
33 {
34 capturerOptions_.streamInfo.samplingRate = capturerOptions.streamInfo.samplingRate;
35 capturerOptions_.streamInfo.encoding = capturerOptions.streamInfo.encoding;
36 capturerOptions_.streamInfo.format = capturerOptions.streamInfo.format;
37 capturerOptions_.streamInfo.channels = capturerOptions.streamInfo.channels;
38 capturerOptions_.capturerInfo.sourceType = capturerOptions.capturerInfo.sourceType;
39 capturerOptions_.capturerInfo.capturerFlags = capturerOptions.capturerInfo.capturerFlags;
40
41 audioCapturer_ = AudioCapturer::Create(capturerOptions_, CACHE_PATH);
42 if (audioCapturer_ == nullptr) {
43 INTELL_VOICE_LOG_ERROR("create audio capturer failed");
44 }
45 }
46
~AudioSource()47 AudioSource::~AudioSource()
48 {
49 audioCapturer_ = nullptr;
50 }
51
Start()52 bool AudioSource::Start()
53 {
54 INTELL_VOICE_LOG_INFO("enter");
55 if (audioCapturer_ == nullptr) {
56 INTELL_VOICE_LOG_ERROR("audioCapturer_ is nullptr");
57 return false;
58 }
59
60 if (listener_ == nullptr) {
61 INTELL_VOICE_LOG_ERROR("listener_ is nullptr");
62 return false;
63 }
64
65 if (minBufferSize_ == 0) {
66 INTELL_VOICE_LOG_ERROR("minBufferSize_ is invalid");
67 return false;
68 }
69
70 buffer_ = std::shared_ptr<uint8_t>(new uint8_t[minBufferSize_], [](uint8_t *p) { delete[] p; });
71 if (buffer_ == nullptr) {
72 INTELL_VOICE_LOG_ERROR("malloc buffer failed");
73 return false;
74 }
75
76 CreateAudioDebugFile();
77
78 if (!audioCapturer_->Start()) {
79 INTELL_VOICE_LOG_ERROR("start audio capturer failed");
80 DestroyAudioDebugFile();
81 return false;
82 }
83
84 isReading_.store(true);
85 std::thread t1(std::bind(&AudioSource::ReadThread, this));
86 readThread_ = std::move(t1);
87 return true;
88 }
89
ReadThread()90 void AudioSource::ReadThread()
91 {
92 INTELL_VOICE_LOG_INFO("enter");
93 uint32_t readCnt = 0;
94 bool isError = true;
95 while (isReading_.load()) {
96 if (readCnt >= bufferCnt_) {
97 INTELL_VOICE_LOG_INFO("finish reading data");
98 isError = false;
99 break;
100 }
101
102 if (!Read()) {
103 INTELL_VOICE_LOG_WARN("failed to read data");
104 break;
105 }
106 ++readCnt;
107 }
108
109 if (listener_ != nullptr) {
110 listener_->bufferEndCb_(isError);
111 }
112 }
113
Read()114 bool AudioSource::Read()
115 {
116 size_t bytesRead = 0;
117 while (bytesRead < minBufferSize_) {
118 if (!isReading_.load()) {
119 INTELL_VOICE_LOG_WARN("stop to read");
120 break;
121 }
122 int32_t len = audioCapturer_->Read(*(buffer_.get() + bytesRead), minBufferSize_ - bytesRead, 0);
123 if (len >= 0) {
124 bytesRead += static_cast<uint32_t>(len);
125 } else {
126 INTELL_VOICE_LOG_ERROR("read data error, len is %{public}d", len);
127 break;
128 }
129 }
130
131 if (bytesRead != minBufferSize_) {
132 INTELL_VOICE_LOG_ERROR("failed to read data, bytesRead is %{public}zu", bytesRead);
133 return false;
134 }
135
136 WriteData(reinterpret_cast<char *>(buffer_.get()), minBufferSize_);
137
138 if (listener_ != nullptr) {
139 listener_->readBufferCb_(buffer_.get(), minBufferSize_);
140 }
141 return true;
142 }
143
Stop()144 void AudioSource::Stop()
145 {
146 INTELL_VOICE_LOG_INFO("enter");
147 if (!isReading_.load()) {
148 INTELL_VOICE_LOG_INFO("already stop");
149 return;
150 }
151
152 MemoryGuard memoryGuard;
153
154 isReading_.store(false);
155 readThread_.join();
156
157 DestroyAudioDebugFile();
158
159 if (audioCapturer_ == nullptr) {
160 INTELL_VOICE_LOG_ERROR("audioCapturer_ is nullptr");
161 return;
162 }
163
164 if (!audioCapturer_->Stop()) {
165 INTELL_VOICE_LOG_ERROR("stop audio capturer error");
166 }
167
168 if (!audioCapturer_->Release()) {
169 INTELL_VOICE_LOG_ERROR("release audio capturer error");
170 }
171
172 audioCapturer_ = nullptr;
173 listener_ = nullptr;
174 }
175 }
176 }