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