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 #ifndef LOG_TAG
16 #define LOG_TAG "OHAudioCapturer"
17 #endif
18
19 #include "OHAudioCapturer.h"
20 #include "audio_errors.h"
21
22 using OHOS::AudioStandard::Timestamp;
23
24 static const int64_t SECOND_TO_NANOSECOND = 1000000000;
25
convertCapturer(OH_AudioCapturer * capturer)26 static OHOS::AudioStandard::OHAudioCapturer *convertCapturer(OH_AudioCapturer* capturer)
27 {
28 return (OHOS::AudioStandard::OHAudioCapturer*) capturer;
29 }
30
OH_AudioCapturer_Release(OH_AudioCapturer * capturer)31 OH_AudioStream_Result OH_AudioCapturer_Release(OH_AudioCapturer* capturer)
32 {
33 OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
34 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
35 if (audioCapturer->Release()) {
36 delete audioCapturer;
37 audioCapturer = nullptr;
38 return AUDIOSTREAM_SUCCESS;
39 } else {
40 return AUDIOSTREAM_ERROR_ILLEGAL_STATE;
41 }
42 }
43
OH_AudioCapturer_Start(OH_AudioCapturer * capturer)44 OH_AudioStream_Result OH_AudioCapturer_Start(OH_AudioCapturer* capturer)
45 {
46 OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
47 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
48 if (audioCapturer->Start()) {
49 return AUDIOSTREAM_SUCCESS;
50 } else {
51 return AUDIOSTREAM_ERROR_ILLEGAL_STATE;
52 }
53 }
54
OH_AudioCapturer_Pause(OH_AudioCapturer * capturer)55 OH_AudioStream_Result OH_AudioCapturer_Pause(OH_AudioCapturer* capturer)
56 {
57 OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
58 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
59
60 if (audioCapturer->Pause()) {
61 return AUDIOSTREAM_SUCCESS;
62 } else {
63 return AUDIOSTREAM_ERROR_ILLEGAL_STATE;
64 }
65 }
66
OH_AudioCapturer_Stop(OH_AudioCapturer * capturer)67 OH_AudioStream_Result OH_AudioCapturer_Stop(OH_AudioCapturer* capturer)
68 {
69 OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
70 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
71
72 if (audioCapturer->Stop()) {
73 return AUDIOSTREAM_SUCCESS;
74 } else {
75 return AUDIOSTREAM_ERROR_ILLEGAL_STATE;
76 }
77 }
78
OH_AudioCapturer_Flush(OH_AudioCapturer * capturer)79 OH_AudioStream_Result OH_AudioCapturer_Flush(OH_AudioCapturer* capturer)
80 {
81 OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
82 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
83
84 if (audioCapturer->Flush()) {
85 return AUDIOSTREAM_SUCCESS;
86 } else {
87 return AUDIOSTREAM_ERROR_ILLEGAL_STATE;
88 }
89 }
90
91
OH_AudioCapturer_GetCurrentState(OH_AudioCapturer * capturer,OH_AudioStream_State * state)92 OH_AudioStream_Result OH_AudioCapturer_GetCurrentState(OH_AudioCapturer* capturer, OH_AudioStream_State* state)
93 {
94 OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
95 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
96
97 OHOS::AudioStandard::CapturerState capturerState = audioCapturer->GetCurrentState();
98 *state = (OH_AudioStream_State)capturerState;
99 return AUDIOSTREAM_SUCCESS;
100 }
101
OH_AudioCapturer_GetLatencyMode(OH_AudioCapturer * capturer,OH_AudioStream_LatencyMode * latencyMode)102 OH_AudioStream_Result OH_AudioCapturer_GetLatencyMode(OH_AudioCapturer* capturer,
103 OH_AudioStream_LatencyMode* latencyMode)
104 {
105 OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
106 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
107 OHOS::AudioStandard::AudioCapturerInfo capturerInfo;
108 audioCapturer->GetCapturerInfo(capturerInfo);
109 *latencyMode = (OH_AudioStream_LatencyMode)capturerInfo.capturerFlags;
110 return AUDIOSTREAM_SUCCESS;
111 }
112
OH_AudioCapturer_GetStreamId(OH_AudioCapturer * capturer,uint32_t * streamId)113 OH_AudioStream_Result OH_AudioCapturer_GetStreamId(OH_AudioCapturer* capturer, uint32_t* streamId)
114 {
115 OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
116 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
117 audioCapturer->GetStreamId(*streamId);
118 return AUDIOSTREAM_SUCCESS;
119 }
120
OH_AudioCapturer_GetChannelCount(OH_AudioCapturer * capturer,int32_t * channelCount)121 OH_AudioStream_Result OH_AudioCapturer_GetChannelCount(OH_AudioCapturer* capturer, int32_t* channelCount)
122 {
123 OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
124 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
125 *channelCount = audioCapturer->GetChannelCount();
126 return AUDIOSTREAM_SUCCESS;
127 }
128
OH_AudioCapturer_GetSamplingRate(OH_AudioCapturer * capturer,int32_t * rate)129 OH_AudioStream_Result OH_AudioCapturer_GetSamplingRate(OH_AudioCapturer* capturer, int32_t* rate)
130 {
131 OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
132 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
133
134 *rate = audioCapturer->GetSamplingRate();
135 return AUDIOSTREAM_SUCCESS;
136 }
137
OH_AudioCapturer_GetSampleFormat(OH_AudioCapturer * capturer,OH_AudioStream_SampleFormat * sampleFormat)138 OH_AudioStream_Result OH_AudioCapturer_GetSampleFormat(OH_AudioCapturer* capturer,
139 OH_AudioStream_SampleFormat* sampleFormat)
140 {
141 OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
142 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
143 *sampleFormat = (OH_AudioStream_SampleFormat)audioCapturer->GetSampleFormat();
144 return AUDIOSTREAM_SUCCESS;
145 }
146
OH_AudioCapturer_GetEncodingType(OH_AudioCapturer * capturer,OH_AudioStream_EncodingType * encodingType)147 OH_AudioStream_Result OH_AudioCapturer_GetEncodingType(OH_AudioCapturer* capturer,
148 OH_AudioStream_EncodingType* encodingType)
149 {
150 OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
151 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
152 *encodingType = (OH_AudioStream_EncodingType)audioCapturer->GetEncodingType();
153 return AUDIOSTREAM_SUCCESS;
154 }
155
OH_AudioCapturer_GetCapturerInfo(OH_AudioCapturer * capturer,OH_AudioStream_SourceType * sourceType)156 OH_AudioStream_Result OH_AudioCapturer_GetCapturerInfo(OH_AudioCapturer* capturer,
157 OH_AudioStream_SourceType* sourceType)
158 {
159 OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
160 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
161 OHOS::AudioStandard::AudioCapturerInfo capturerInfo;
162 audioCapturer->GetCapturerInfo(capturerInfo);
163 *sourceType = (OH_AudioStream_SourceType)capturerInfo.sourceType;
164 return AUDIOSTREAM_SUCCESS;
165 }
166
OH_AudioCapturer_GetFrameSizeInCallback(OH_AudioCapturer * capturer,int32_t * frameSize)167 OH_AudioStream_Result OH_AudioCapturer_GetFrameSizeInCallback(OH_AudioCapturer* capturer, int32_t* frameSize)
168 {
169 OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
170 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
171
172 *frameSize = audioCapturer->GetFrameSizeInCallback();
173 return AUDIOSTREAM_SUCCESS;
174 }
175
OH_AudioCapturer_GetTimestamp(OH_AudioCapturer * capturer,clockid_t clockId,int64_t * framePosition,int64_t * timestamp)176 OH_AudioStream_Result OH_AudioCapturer_GetTimestamp(OH_AudioCapturer* capturer,
177 clockid_t clockId, int64_t* framePosition, int64_t* timestamp)
178 {
179 OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
180 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
181 CHECK_AND_RETURN_RET_LOG(clockId == CLOCK_MONOTONIC, AUDIOSTREAM_ERROR_INVALID_PARAM, "error clockId value");
182
183 Timestamp stamp;
184 Timestamp::Timestampbase base = Timestamp::Timestampbase::MONOTONIC;
185 bool ret = audioCapturer->GetAudioTime(stamp, base);
186 if (!ret) {
187 AUDIO_ERR_LOG("GetAudioTime error!");
188 return AUDIOSTREAM_ERROR_ILLEGAL_STATE;
189 }
190 *framePosition = stamp.framePosition;
191 *timestamp = stamp.time.tv_sec * SECOND_TO_NANOSECOND + stamp.time.tv_nsec;
192 return AUDIOSTREAM_SUCCESS;
193 }
194
OH_AudioCapturer_GetFramesRead(OH_AudioCapturer * capturer,int64_t * frames)195 OH_AudioStream_Result OH_AudioCapturer_GetFramesRead(OH_AudioCapturer* capturer, int64_t* frames)
196 {
197 OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
198 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
199
200 *frames = audioCapturer->GetFramesRead();
201 return AUDIOSTREAM_SUCCESS;
202 }
203
OH_AudioCapturer_GetOverflowCount(OH_AudioCapturer * capturer,uint32_t * count)204 OH_AudioStream_Result OH_AudioCapturer_GetOverflowCount(OH_AudioCapturer* capturer, uint32_t* count)
205 {
206 OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
207 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
208
209 *count = audioCapturer->GetOverflowCount();
210 return AUDIOSTREAM_SUCCESS;
211 }
212
213 namespace OHOS {
214 namespace AudioStandard {
OHAudioCapturer()215 OHAudioCapturer::OHAudioCapturer()
216 {
217 AUDIO_INFO_LOG("OHAudioCapturer created!");
218 }
219
~OHAudioCapturer()220 OHAudioCapturer::~OHAudioCapturer()
221 {
222 AUDIO_INFO_LOG("OHAudioCapturer destroyed!");
223 }
224
Initialize(const AudioCapturerOptions & capturerOptions)225 bool OHAudioCapturer::Initialize(const AudioCapturerOptions& capturerOptions)
226 {
227 std::string cacheDir = "/data/storage/el2/base/temp";
228 audioCapturer_ = AudioCapturer::Create(capturerOptions, cacheDir);
229 return audioCapturer_ != nullptr;
230 }
231
Start()232 bool OHAudioCapturer::Start()
233 {
234 if (audioCapturer_ == nullptr) {
235 AUDIO_ERR_LOG("capturer client is nullptr");
236 return false;
237 }
238 return audioCapturer_->Start();
239 }
240
Pause()241 bool OHAudioCapturer::Pause()
242 {
243 if (audioCapturer_ == nullptr) {
244 AUDIO_ERR_LOG("capturer client is nullptr");
245 return false;
246 }
247 return audioCapturer_->Pause();
248 }
249
Stop()250 bool OHAudioCapturer::Stop()
251 {
252 if (audioCapturer_ == nullptr) {
253 AUDIO_ERR_LOG("capturer client is nullptr");
254 return false;
255 }
256 return audioCapturer_->Stop();
257 }
258
Flush()259 bool OHAudioCapturer::Flush()
260 {
261 if (audioCapturer_ == nullptr) {
262 AUDIO_ERR_LOG("capturer client is nullptr");
263 return false;
264 }
265 return audioCapturer_->Flush();
266 }
267
Release()268 bool OHAudioCapturer::Release()
269 {
270 if (audioCapturer_ == nullptr) {
271 AUDIO_ERR_LOG("capturer client is nullptr");
272 return false;
273 }
274
275 if (!audioCapturer_->Release()) {
276 return false;
277 }
278 audioCapturer_ = nullptr;
279 audioCapturerCallback_= nullptr;
280 return true;
281 }
282
GetCurrentState()283 CapturerState OHAudioCapturer::GetCurrentState()
284 {
285 CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, CAPTURER_INVALID, "capturer client is nullptr");
286 return audioCapturer_->GetStatus();
287 }
288
GetStreamId(uint32_t & streamId)289 void OHAudioCapturer::GetStreamId(uint32_t &streamId)
290 {
291 CHECK_AND_RETURN_LOG(audioCapturer_ != nullptr, "capturer client is nullptr");
292 audioCapturer_->GetAudioStreamId(streamId);
293 }
294
GetChannelCount()295 AudioChannel OHAudioCapturer::GetChannelCount()
296 {
297 CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, MONO, "capturer client is nullptr");
298 AudioCapturerParams params;
299 audioCapturer_->GetParams(params);
300 return params.audioChannel;
301 }
302
GetSamplingRate()303 int32_t OHAudioCapturer::GetSamplingRate()
304 {
305 CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, MONO, "capturer client is nullptr");
306 AudioCapturerParams params;
307 audioCapturer_->GetParams(params);
308 return params.samplingRate;
309 }
310
GetEncodingType()311 AudioEncodingType OHAudioCapturer::GetEncodingType()
312 {
313 CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, ENCODING_INVALID, "capturer client is nullptr");
314 AudioCapturerParams params;
315 audioCapturer_->GetParams(params);
316 return params.audioEncoding;
317 }
318
GetCapturerInfo(AudioCapturerInfo & capturerInfo)319 void OHAudioCapturer::GetCapturerInfo(AudioCapturerInfo& capturerInfo)
320 {
321 CHECK_AND_RETURN_LOG(audioCapturer_ != nullptr, "capturer client is nullptr");
322 audioCapturer_->GetCapturerInfo(capturerInfo);
323 }
324
GetSampleFormat()325 AudioSampleFormat OHAudioCapturer::GetSampleFormat()
326 {
327 CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, INVALID_WIDTH, "capturer client is nullptr");
328 AudioCapturerParams params;
329 audioCapturer_->GetParams(params);
330 return params.audioSampleFormat;
331 }
332
SetCapturerCallback(OH_AudioCapturer_Callbacks callbacks,void * userData)333 void OHAudioCapturer::SetCapturerCallback(OH_AudioCapturer_Callbacks callbacks, void* userData)
334 {
335 CHECK_AND_RETURN_LOG(audioCapturer_ != nullptr, "capturer client is nullptr");
336 audioCapturer_->SetCaptureMode(CAPTURE_MODE_CALLBACK);
337 if (callbacks.OH_AudioCapturer_OnReadData != nullptr) {
338 std::shared_ptr<AudioCapturerReadCallback> callback = std::make_shared<OHAudioCapturerModeCallback>(callbacks,
339 (OH_AudioCapturer*)this, userData);
340 audioCapturer_->SetCapturerReadCallback(callback);
341 } else {
342 AUDIO_WARNING_LOG("The read callback function is not set");
343 }
344
345 if (callbacks.OH_AudioCapturer_OnInterruptEvent != nullptr) {
346 audioCapturerCallback_ = std::make_shared<OHAudioCapturerCallback>(callbacks,
347 (OH_AudioCapturer*)this, userData);
348 audioCapturer_->SetCapturerCallback(audioCapturerCallback_);
349 } else {
350 AUDIO_WARNING_LOG("The capturer interrupt event callback function is not set");
351 }
352 }
353
GetFramesRead()354 int64_t OHAudioCapturer::GetFramesRead()
355 {
356 CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, ERROR, "capturer client is nullptr");
357 return audioCapturer_->GetFramesRead();
358 }
359
GetAudioTime(Timestamp & timestamp,Timestamp::Timestampbase base)360 bool OHAudioCapturer::GetAudioTime(Timestamp ×tamp, Timestamp::Timestampbase base)
361 {
362 CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, false, "capturer client is nullptr");
363 return audioCapturer_->GetAudioTime(timestamp, base);
364 }
365
GetFrameSizeInCallback()366 int32_t OHAudioCapturer::GetFrameSizeInCallback()
367 {
368 CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, ERROR, "capturer client is nullptr");
369 uint32_t frameSize;
370 audioCapturer_->GetFrameCount(frameSize);
371 return static_cast<int32_t>(frameSize);
372 }
373
GetBufferDesc(BufferDesc & bufDesc) const374 int32_t OHAudioCapturer::GetBufferDesc(BufferDesc &bufDesc) const
375 {
376 CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, ERROR, "capturer client is nullptr");
377 return audioCapturer_->GetBufferDesc(bufDesc);
378 }
379
Enqueue(const BufferDesc & bufDesc) const380 int32_t OHAudioCapturer::Enqueue(const BufferDesc &bufDesc) const
381 {
382 CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, ERROR, "capturer client is nullptr");
383 return audioCapturer_->Enqueue(bufDesc);
384 }
385
GetOverflowCount() const386 uint32_t OHAudioCapturer::GetOverflowCount() const
387 {
388 CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, ERROR, "capturer client is nullptr");
389 return audioCapturer_->GetOverflowCount();
390 }
391
OnReadData(size_t length)392 void OHAudioCapturerModeCallback::OnReadData(size_t length)
393 {
394 OHAudioCapturer* audioCapturer = (OHAudioCapturer*)ohAudioCapturer_;
395 CHECK_AND_RETURN_LOG(audioCapturer != nullptr, "capturer client is nullptr");
396 CHECK_AND_RETURN_LOG(callbacks_.OH_AudioCapturer_OnReadData != nullptr, "pointer to the fuction is nullptr");
397 BufferDesc bufDesc;
398 audioCapturer->GetBufferDesc(bufDesc);
399 callbacks_.OH_AudioCapturer_OnReadData(ohAudioCapturer_,
400 userData_,
401 (void*)bufDesc.buffer,
402 bufDesc.bufLength);
403 audioCapturer->Enqueue(bufDesc);
404 }
405
OnInterrupt(const InterruptEvent & interruptEvent)406 void OHAudioCapturerCallback::OnInterrupt(const InterruptEvent &interruptEvent)
407 {
408 CHECK_AND_RETURN_LOG(ohAudioCapturer_ != nullptr, "capturer client is nullptr");
409 CHECK_AND_RETURN_LOG(callbacks_.OH_AudioCapturer_OnInterruptEvent != nullptr, "pointer to the fuction is nullptr");
410
411 OH_AudioInterrupt_ForceType type = (OH_AudioInterrupt_ForceType)(interruptEvent.forceType);
412 OH_AudioInterrupt_Hint hint = OH_AudioInterrupt_Hint(interruptEvent.hintType);
413 callbacks_.OH_AudioCapturer_OnInterruptEvent(ohAudioCapturer_, userData_, type, hint);
414 }
415 } // namespace AudioStandard
416 } // namespace OHOS
417