146f34cbfSopenharmony_ci/*
246f34cbfSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
346f34cbfSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
446f34cbfSopenharmony_ci * you may not use this file except in compliance with the License.
546f34cbfSopenharmony_ci * You may obtain a copy of the License at
646f34cbfSopenharmony_ci *
746f34cbfSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
846f34cbfSopenharmony_ci *
946f34cbfSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1046f34cbfSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1146f34cbfSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1246f34cbfSopenharmony_ci * See the License for the specific language governing permissions and
1346f34cbfSopenharmony_ci * limitations under the License.
1446f34cbfSopenharmony_ci */
1546f34cbfSopenharmony_ci#ifndef LOG_TAG
1646f34cbfSopenharmony_ci#define LOG_TAG "OHAudioCapturer"
1746f34cbfSopenharmony_ci#endif
1846f34cbfSopenharmony_ci
1946f34cbfSopenharmony_ci#include "OHAudioCapturer.h"
2046f34cbfSopenharmony_ci#include "audio_errors.h"
2146f34cbfSopenharmony_ci
2246f34cbfSopenharmony_ciusing OHOS::AudioStandard::Timestamp;
2346f34cbfSopenharmony_ci
2446f34cbfSopenharmony_cistatic const int64_t SECOND_TO_NANOSECOND = 1000000000;
2546f34cbfSopenharmony_ci
2646f34cbfSopenharmony_cistatic OHOS::AudioStandard::OHAudioCapturer *convertCapturer(OH_AudioCapturer* capturer)
2746f34cbfSopenharmony_ci{
2846f34cbfSopenharmony_ci    return (OHOS::AudioStandard::OHAudioCapturer*) capturer;
2946f34cbfSopenharmony_ci}
3046f34cbfSopenharmony_ci
3146f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioCapturer_Release(OH_AudioCapturer* capturer)
3246f34cbfSopenharmony_ci{
3346f34cbfSopenharmony_ci    OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
3446f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
3546f34cbfSopenharmony_ci    if (audioCapturer->Release()) {
3646f34cbfSopenharmony_ci        delete audioCapturer;
3746f34cbfSopenharmony_ci        audioCapturer = nullptr;
3846f34cbfSopenharmony_ci        return AUDIOSTREAM_SUCCESS;
3946f34cbfSopenharmony_ci    } else {
4046f34cbfSopenharmony_ci        return AUDIOSTREAM_ERROR_ILLEGAL_STATE;
4146f34cbfSopenharmony_ci    }
4246f34cbfSopenharmony_ci}
4346f34cbfSopenharmony_ci
4446f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioCapturer_Start(OH_AudioCapturer* capturer)
4546f34cbfSopenharmony_ci{
4646f34cbfSopenharmony_ci    OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
4746f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
4846f34cbfSopenharmony_ci    if (audioCapturer->Start()) {
4946f34cbfSopenharmony_ci        return AUDIOSTREAM_SUCCESS;
5046f34cbfSopenharmony_ci    } else {
5146f34cbfSopenharmony_ci        return AUDIOSTREAM_ERROR_ILLEGAL_STATE;
5246f34cbfSopenharmony_ci    }
5346f34cbfSopenharmony_ci}
5446f34cbfSopenharmony_ci
5546f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioCapturer_Pause(OH_AudioCapturer* capturer)
5646f34cbfSopenharmony_ci{
5746f34cbfSopenharmony_ci    OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
5846f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
5946f34cbfSopenharmony_ci
6046f34cbfSopenharmony_ci    if (audioCapturer->Pause()) {
6146f34cbfSopenharmony_ci        return AUDIOSTREAM_SUCCESS;
6246f34cbfSopenharmony_ci    } else {
6346f34cbfSopenharmony_ci        return AUDIOSTREAM_ERROR_ILLEGAL_STATE;
6446f34cbfSopenharmony_ci    }
6546f34cbfSopenharmony_ci}
6646f34cbfSopenharmony_ci
6746f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioCapturer_Stop(OH_AudioCapturer* capturer)
6846f34cbfSopenharmony_ci{
6946f34cbfSopenharmony_ci    OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
7046f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
7146f34cbfSopenharmony_ci
7246f34cbfSopenharmony_ci    if (audioCapturer->Stop()) {
7346f34cbfSopenharmony_ci        return AUDIOSTREAM_SUCCESS;
7446f34cbfSopenharmony_ci    } else {
7546f34cbfSopenharmony_ci        return AUDIOSTREAM_ERROR_ILLEGAL_STATE;
7646f34cbfSopenharmony_ci    }
7746f34cbfSopenharmony_ci}
7846f34cbfSopenharmony_ci
7946f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioCapturer_Flush(OH_AudioCapturer* capturer)
8046f34cbfSopenharmony_ci{
8146f34cbfSopenharmony_ci    OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
8246f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
8346f34cbfSopenharmony_ci
8446f34cbfSopenharmony_ci    if (audioCapturer->Flush()) {
8546f34cbfSopenharmony_ci        return AUDIOSTREAM_SUCCESS;
8646f34cbfSopenharmony_ci    } else {
8746f34cbfSopenharmony_ci        return AUDIOSTREAM_ERROR_ILLEGAL_STATE;
8846f34cbfSopenharmony_ci    }
8946f34cbfSopenharmony_ci}
9046f34cbfSopenharmony_ci
9146f34cbfSopenharmony_ci
9246f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioCapturer_GetCurrentState(OH_AudioCapturer* capturer, OH_AudioStream_State* state)
9346f34cbfSopenharmony_ci{
9446f34cbfSopenharmony_ci    OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
9546f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
9646f34cbfSopenharmony_ci
9746f34cbfSopenharmony_ci    OHOS::AudioStandard::CapturerState capturerState = audioCapturer->GetCurrentState();
9846f34cbfSopenharmony_ci    *state = (OH_AudioStream_State)capturerState;
9946f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
10046f34cbfSopenharmony_ci}
10146f34cbfSopenharmony_ci
10246f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioCapturer_GetLatencyMode(OH_AudioCapturer* capturer,
10346f34cbfSopenharmony_ci    OH_AudioStream_LatencyMode* latencyMode)
10446f34cbfSopenharmony_ci{
10546f34cbfSopenharmony_ci    OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
10646f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
10746f34cbfSopenharmony_ci    OHOS::AudioStandard::AudioCapturerInfo capturerInfo;
10846f34cbfSopenharmony_ci    audioCapturer->GetCapturerInfo(capturerInfo);
10946f34cbfSopenharmony_ci    *latencyMode = (OH_AudioStream_LatencyMode)capturerInfo.capturerFlags;
11046f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
11146f34cbfSopenharmony_ci}
11246f34cbfSopenharmony_ci
11346f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioCapturer_GetStreamId(OH_AudioCapturer* capturer, uint32_t* streamId)
11446f34cbfSopenharmony_ci{
11546f34cbfSopenharmony_ci    OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
11646f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
11746f34cbfSopenharmony_ci    audioCapturer->GetStreamId(*streamId);
11846f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
11946f34cbfSopenharmony_ci}
12046f34cbfSopenharmony_ci
12146f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioCapturer_GetChannelCount(OH_AudioCapturer* capturer, int32_t* channelCount)
12246f34cbfSopenharmony_ci{
12346f34cbfSopenharmony_ci    OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
12446f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
12546f34cbfSopenharmony_ci    *channelCount = audioCapturer->GetChannelCount();
12646f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
12746f34cbfSopenharmony_ci}
12846f34cbfSopenharmony_ci
12946f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioCapturer_GetSamplingRate(OH_AudioCapturer* capturer, int32_t* rate)
13046f34cbfSopenharmony_ci{
13146f34cbfSopenharmony_ci    OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
13246f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
13346f34cbfSopenharmony_ci
13446f34cbfSopenharmony_ci    *rate = audioCapturer->GetSamplingRate();
13546f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
13646f34cbfSopenharmony_ci}
13746f34cbfSopenharmony_ci
13846f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioCapturer_GetSampleFormat(OH_AudioCapturer* capturer,
13946f34cbfSopenharmony_ci    OH_AudioStream_SampleFormat* sampleFormat)
14046f34cbfSopenharmony_ci{
14146f34cbfSopenharmony_ci    OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
14246f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
14346f34cbfSopenharmony_ci    *sampleFormat = (OH_AudioStream_SampleFormat)audioCapturer->GetSampleFormat();
14446f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
14546f34cbfSopenharmony_ci}
14646f34cbfSopenharmony_ci
14746f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioCapturer_GetEncodingType(OH_AudioCapturer* capturer,
14846f34cbfSopenharmony_ci    OH_AudioStream_EncodingType* encodingType)
14946f34cbfSopenharmony_ci{
15046f34cbfSopenharmony_ci    OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
15146f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
15246f34cbfSopenharmony_ci    *encodingType = (OH_AudioStream_EncodingType)audioCapturer->GetEncodingType();
15346f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
15446f34cbfSopenharmony_ci}
15546f34cbfSopenharmony_ci
15646f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioCapturer_GetCapturerInfo(OH_AudioCapturer* capturer,
15746f34cbfSopenharmony_ci    OH_AudioStream_SourceType* sourceType)
15846f34cbfSopenharmony_ci{
15946f34cbfSopenharmony_ci    OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
16046f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
16146f34cbfSopenharmony_ci    OHOS::AudioStandard::AudioCapturerInfo capturerInfo;
16246f34cbfSopenharmony_ci    audioCapturer->GetCapturerInfo(capturerInfo);
16346f34cbfSopenharmony_ci    *sourceType = (OH_AudioStream_SourceType)capturerInfo.sourceType;
16446f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
16546f34cbfSopenharmony_ci}
16646f34cbfSopenharmony_ci
16746f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioCapturer_GetFrameSizeInCallback(OH_AudioCapturer* capturer, int32_t* frameSize)
16846f34cbfSopenharmony_ci{
16946f34cbfSopenharmony_ci    OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
17046f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
17146f34cbfSopenharmony_ci
17246f34cbfSopenharmony_ci    *frameSize = audioCapturer->GetFrameSizeInCallback();
17346f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
17446f34cbfSopenharmony_ci}
17546f34cbfSopenharmony_ci
17646f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioCapturer_GetTimestamp(OH_AudioCapturer* capturer,
17746f34cbfSopenharmony_ci    clockid_t clockId, int64_t* framePosition, int64_t* timestamp)
17846f34cbfSopenharmony_ci{
17946f34cbfSopenharmony_ci    OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
18046f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
18146f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(clockId == CLOCK_MONOTONIC, AUDIOSTREAM_ERROR_INVALID_PARAM, "error clockId value");
18246f34cbfSopenharmony_ci
18346f34cbfSopenharmony_ci    Timestamp stamp;
18446f34cbfSopenharmony_ci    Timestamp::Timestampbase base = Timestamp::Timestampbase::MONOTONIC;
18546f34cbfSopenharmony_ci    bool ret = audioCapturer->GetAudioTime(stamp, base);
18646f34cbfSopenharmony_ci    if (!ret) {
18746f34cbfSopenharmony_ci        AUDIO_ERR_LOG("GetAudioTime error!");
18846f34cbfSopenharmony_ci        return AUDIOSTREAM_ERROR_ILLEGAL_STATE;
18946f34cbfSopenharmony_ci    }
19046f34cbfSopenharmony_ci    *framePosition = stamp.framePosition;
19146f34cbfSopenharmony_ci    *timestamp = stamp.time.tv_sec * SECOND_TO_NANOSECOND + stamp.time.tv_nsec;
19246f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
19346f34cbfSopenharmony_ci}
19446f34cbfSopenharmony_ci
19546f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioCapturer_GetFramesRead(OH_AudioCapturer* capturer, int64_t* frames)
19646f34cbfSopenharmony_ci{
19746f34cbfSopenharmony_ci    OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
19846f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
19946f34cbfSopenharmony_ci
20046f34cbfSopenharmony_ci    *frames = audioCapturer->GetFramesRead();
20146f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
20246f34cbfSopenharmony_ci}
20346f34cbfSopenharmony_ci
20446f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioCapturer_GetOverflowCount(OH_AudioCapturer* capturer, uint32_t* count)
20546f34cbfSopenharmony_ci{
20646f34cbfSopenharmony_ci    OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer);
20746f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed");
20846f34cbfSopenharmony_ci
20946f34cbfSopenharmony_ci    *count = audioCapturer->GetOverflowCount();
21046f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
21146f34cbfSopenharmony_ci}
21246f34cbfSopenharmony_ci
21346f34cbfSopenharmony_cinamespace OHOS {
21446f34cbfSopenharmony_cinamespace AudioStandard {
21546f34cbfSopenharmony_ciOHAudioCapturer::OHAudioCapturer()
21646f34cbfSopenharmony_ci{
21746f34cbfSopenharmony_ci    AUDIO_INFO_LOG("OHAudioCapturer created!");
21846f34cbfSopenharmony_ci}
21946f34cbfSopenharmony_ci
22046f34cbfSopenharmony_ciOHAudioCapturer::~OHAudioCapturer()
22146f34cbfSopenharmony_ci{
22246f34cbfSopenharmony_ci    AUDIO_INFO_LOG("OHAudioCapturer destroyed!");
22346f34cbfSopenharmony_ci}
22446f34cbfSopenharmony_ci
22546f34cbfSopenharmony_cibool OHAudioCapturer::Initialize(const AudioCapturerOptions& capturerOptions)
22646f34cbfSopenharmony_ci{
22746f34cbfSopenharmony_ci    std::string cacheDir = "/data/storage/el2/base/temp";
22846f34cbfSopenharmony_ci    audioCapturer_ = AudioCapturer::Create(capturerOptions, cacheDir);
22946f34cbfSopenharmony_ci    return audioCapturer_ != nullptr;
23046f34cbfSopenharmony_ci}
23146f34cbfSopenharmony_ci
23246f34cbfSopenharmony_cibool OHAudioCapturer::Start()
23346f34cbfSopenharmony_ci{
23446f34cbfSopenharmony_ci    if (audioCapturer_ == nullptr) {
23546f34cbfSopenharmony_ci        AUDIO_ERR_LOG("capturer client is nullptr");
23646f34cbfSopenharmony_ci        return false;
23746f34cbfSopenharmony_ci    }
23846f34cbfSopenharmony_ci    return audioCapturer_->Start();
23946f34cbfSopenharmony_ci}
24046f34cbfSopenharmony_ci
24146f34cbfSopenharmony_cibool OHAudioCapturer::Pause()
24246f34cbfSopenharmony_ci{
24346f34cbfSopenharmony_ci    if (audioCapturer_ == nullptr) {
24446f34cbfSopenharmony_ci        AUDIO_ERR_LOG("capturer client is nullptr");
24546f34cbfSopenharmony_ci        return false;
24646f34cbfSopenharmony_ci    }
24746f34cbfSopenharmony_ci    return audioCapturer_->Pause();
24846f34cbfSopenharmony_ci}
24946f34cbfSopenharmony_ci
25046f34cbfSopenharmony_cibool OHAudioCapturer::Stop()
25146f34cbfSopenharmony_ci{
25246f34cbfSopenharmony_ci    if (audioCapturer_ == nullptr) {
25346f34cbfSopenharmony_ci        AUDIO_ERR_LOG("capturer client is nullptr");
25446f34cbfSopenharmony_ci        return false;
25546f34cbfSopenharmony_ci    }
25646f34cbfSopenharmony_ci    return audioCapturer_->Stop();
25746f34cbfSopenharmony_ci}
25846f34cbfSopenharmony_ci
25946f34cbfSopenharmony_cibool OHAudioCapturer::Flush()
26046f34cbfSopenharmony_ci{
26146f34cbfSopenharmony_ci    if (audioCapturer_ == nullptr) {
26246f34cbfSopenharmony_ci        AUDIO_ERR_LOG("capturer client is nullptr");
26346f34cbfSopenharmony_ci        return false;
26446f34cbfSopenharmony_ci    }
26546f34cbfSopenharmony_ci    return audioCapturer_->Flush();
26646f34cbfSopenharmony_ci}
26746f34cbfSopenharmony_ci
26846f34cbfSopenharmony_cibool OHAudioCapturer::Release()
26946f34cbfSopenharmony_ci{
27046f34cbfSopenharmony_ci    if (audioCapturer_ == nullptr) {
27146f34cbfSopenharmony_ci        AUDIO_ERR_LOG("capturer client is nullptr");
27246f34cbfSopenharmony_ci        return false;
27346f34cbfSopenharmony_ci    }
27446f34cbfSopenharmony_ci
27546f34cbfSopenharmony_ci    if (!audioCapturer_->Release()) {
27646f34cbfSopenharmony_ci        return false;
27746f34cbfSopenharmony_ci    }
27846f34cbfSopenharmony_ci    audioCapturer_ = nullptr;
27946f34cbfSopenharmony_ci    audioCapturerCallback_= nullptr;
28046f34cbfSopenharmony_ci    return true;
28146f34cbfSopenharmony_ci}
28246f34cbfSopenharmony_ci
28346f34cbfSopenharmony_ciCapturerState OHAudioCapturer::GetCurrentState()
28446f34cbfSopenharmony_ci{
28546f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, CAPTURER_INVALID, "capturer client is nullptr");
28646f34cbfSopenharmony_ci    return audioCapturer_->GetStatus();
28746f34cbfSopenharmony_ci}
28846f34cbfSopenharmony_ci
28946f34cbfSopenharmony_civoid OHAudioCapturer::GetStreamId(uint32_t &streamId)
29046f34cbfSopenharmony_ci{
29146f34cbfSopenharmony_ci    CHECK_AND_RETURN_LOG(audioCapturer_ != nullptr, "capturer client is nullptr");
29246f34cbfSopenharmony_ci    audioCapturer_->GetAudioStreamId(streamId);
29346f34cbfSopenharmony_ci}
29446f34cbfSopenharmony_ci
29546f34cbfSopenharmony_ciAudioChannel OHAudioCapturer::GetChannelCount()
29646f34cbfSopenharmony_ci{
29746f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, MONO, "capturer client is nullptr");
29846f34cbfSopenharmony_ci    AudioCapturerParams params;
29946f34cbfSopenharmony_ci    audioCapturer_->GetParams(params);
30046f34cbfSopenharmony_ci    return params.audioChannel;
30146f34cbfSopenharmony_ci}
30246f34cbfSopenharmony_ci
30346f34cbfSopenharmony_ciint32_t OHAudioCapturer::GetSamplingRate()
30446f34cbfSopenharmony_ci{
30546f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, MONO, "capturer client is nullptr");
30646f34cbfSopenharmony_ci    AudioCapturerParams params;
30746f34cbfSopenharmony_ci    audioCapturer_->GetParams(params);
30846f34cbfSopenharmony_ci    return params.samplingRate;
30946f34cbfSopenharmony_ci}
31046f34cbfSopenharmony_ci
31146f34cbfSopenharmony_ciAudioEncodingType OHAudioCapturer::GetEncodingType()
31246f34cbfSopenharmony_ci{
31346f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, ENCODING_INVALID, "capturer client is nullptr");
31446f34cbfSopenharmony_ci    AudioCapturerParams params;
31546f34cbfSopenharmony_ci    audioCapturer_->GetParams(params);
31646f34cbfSopenharmony_ci    return params.audioEncoding;
31746f34cbfSopenharmony_ci}
31846f34cbfSopenharmony_ci
31946f34cbfSopenharmony_civoid OHAudioCapturer::GetCapturerInfo(AudioCapturerInfo& capturerInfo)
32046f34cbfSopenharmony_ci{
32146f34cbfSopenharmony_ci    CHECK_AND_RETURN_LOG(audioCapturer_ != nullptr, "capturer client is nullptr");
32246f34cbfSopenharmony_ci    audioCapturer_->GetCapturerInfo(capturerInfo);
32346f34cbfSopenharmony_ci}
32446f34cbfSopenharmony_ci
32546f34cbfSopenharmony_ciAudioSampleFormat OHAudioCapturer::GetSampleFormat()
32646f34cbfSopenharmony_ci{
32746f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, INVALID_WIDTH, "capturer client is nullptr");
32846f34cbfSopenharmony_ci    AudioCapturerParams params;
32946f34cbfSopenharmony_ci    audioCapturer_->GetParams(params);
33046f34cbfSopenharmony_ci    return params.audioSampleFormat;
33146f34cbfSopenharmony_ci}
33246f34cbfSopenharmony_ci
33346f34cbfSopenharmony_civoid OHAudioCapturer::SetCapturerCallback(OH_AudioCapturer_Callbacks callbacks, void* userData)
33446f34cbfSopenharmony_ci{
33546f34cbfSopenharmony_ci    CHECK_AND_RETURN_LOG(audioCapturer_ != nullptr, "capturer client is nullptr");
33646f34cbfSopenharmony_ci    audioCapturer_->SetCaptureMode(CAPTURE_MODE_CALLBACK);
33746f34cbfSopenharmony_ci    if (callbacks.OH_AudioCapturer_OnReadData != nullptr) {
33846f34cbfSopenharmony_ci        std::shared_ptr<AudioCapturerReadCallback> callback = std::make_shared<OHAudioCapturerModeCallback>(callbacks,
33946f34cbfSopenharmony_ci            (OH_AudioCapturer*)this, userData);
34046f34cbfSopenharmony_ci        audioCapturer_->SetCapturerReadCallback(callback);
34146f34cbfSopenharmony_ci    } else {
34246f34cbfSopenharmony_ci        AUDIO_WARNING_LOG("The read callback function is not set");
34346f34cbfSopenharmony_ci    }
34446f34cbfSopenharmony_ci
34546f34cbfSopenharmony_ci    if (callbacks.OH_AudioCapturer_OnInterruptEvent != nullptr) {
34646f34cbfSopenharmony_ci        audioCapturerCallback_ = std::make_shared<OHAudioCapturerCallback>(callbacks,
34746f34cbfSopenharmony_ci            (OH_AudioCapturer*)this, userData);
34846f34cbfSopenharmony_ci        audioCapturer_->SetCapturerCallback(audioCapturerCallback_);
34946f34cbfSopenharmony_ci    } else {
35046f34cbfSopenharmony_ci        AUDIO_WARNING_LOG("The capturer interrupt event callback function is not set");
35146f34cbfSopenharmony_ci    }
35246f34cbfSopenharmony_ci}
35346f34cbfSopenharmony_ci
35446f34cbfSopenharmony_ciint64_t OHAudioCapturer::GetFramesRead()
35546f34cbfSopenharmony_ci{
35646f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, ERROR, "capturer client is nullptr");
35746f34cbfSopenharmony_ci    return audioCapturer_->GetFramesRead();
35846f34cbfSopenharmony_ci}
35946f34cbfSopenharmony_ci
36046f34cbfSopenharmony_cibool OHAudioCapturer::GetAudioTime(Timestamp &timestamp, Timestamp::Timestampbase base)
36146f34cbfSopenharmony_ci{
36246f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, false, "capturer client is nullptr");
36346f34cbfSopenharmony_ci    return audioCapturer_->GetAudioTime(timestamp, base);
36446f34cbfSopenharmony_ci}
36546f34cbfSopenharmony_ci
36646f34cbfSopenharmony_ciint32_t OHAudioCapturer::GetFrameSizeInCallback()
36746f34cbfSopenharmony_ci{
36846f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, ERROR, "capturer client is nullptr");
36946f34cbfSopenharmony_ci    uint32_t frameSize;
37046f34cbfSopenharmony_ci    audioCapturer_->GetFrameCount(frameSize);
37146f34cbfSopenharmony_ci    return static_cast<int32_t>(frameSize);
37246f34cbfSopenharmony_ci}
37346f34cbfSopenharmony_ci
37446f34cbfSopenharmony_ciint32_t OHAudioCapturer::GetBufferDesc(BufferDesc &bufDesc) const
37546f34cbfSopenharmony_ci{
37646f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, ERROR, "capturer client is nullptr");
37746f34cbfSopenharmony_ci    return audioCapturer_->GetBufferDesc(bufDesc);
37846f34cbfSopenharmony_ci}
37946f34cbfSopenharmony_ci
38046f34cbfSopenharmony_ciint32_t OHAudioCapturer::Enqueue(const BufferDesc &bufDesc) const
38146f34cbfSopenharmony_ci{
38246f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, ERROR, "capturer client is nullptr");
38346f34cbfSopenharmony_ci    return audioCapturer_->Enqueue(bufDesc);
38446f34cbfSopenharmony_ci}
38546f34cbfSopenharmony_ci
38646f34cbfSopenharmony_ciuint32_t OHAudioCapturer::GetOverflowCount() const
38746f34cbfSopenharmony_ci{
38846f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, ERROR, "capturer client is nullptr");
38946f34cbfSopenharmony_ci    return audioCapturer_->GetOverflowCount();
39046f34cbfSopenharmony_ci}
39146f34cbfSopenharmony_ci
39246f34cbfSopenharmony_civoid OHAudioCapturerModeCallback::OnReadData(size_t length)
39346f34cbfSopenharmony_ci{
39446f34cbfSopenharmony_ci    OHAudioCapturer* audioCapturer = (OHAudioCapturer*)ohAudioCapturer_;
39546f34cbfSopenharmony_ci    CHECK_AND_RETURN_LOG(audioCapturer != nullptr, "capturer client is nullptr");
39646f34cbfSopenharmony_ci    CHECK_AND_RETURN_LOG(callbacks_.OH_AudioCapturer_OnReadData != nullptr, "pointer to the fuction is nullptr");
39746f34cbfSopenharmony_ci    BufferDesc bufDesc;
39846f34cbfSopenharmony_ci    audioCapturer->GetBufferDesc(bufDesc);
39946f34cbfSopenharmony_ci    callbacks_.OH_AudioCapturer_OnReadData(ohAudioCapturer_,
40046f34cbfSopenharmony_ci        userData_,
40146f34cbfSopenharmony_ci        (void*)bufDesc.buffer,
40246f34cbfSopenharmony_ci        bufDesc.bufLength);
40346f34cbfSopenharmony_ci    audioCapturer->Enqueue(bufDesc);
40446f34cbfSopenharmony_ci}
40546f34cbfSopenharmony_ci
40646f34cbfSopenharmony_civoid OHAudioCapturerCallback::OnInterrupt(const InterruptEvent &interruptEvent)
40746f34cbfSopenharmony_ci{
40846f34cbfSopenharmony_ci    CHECK_AND_RETURN_LOG(ohAudioCapturer_ != nullptr, "capturer client is nullptr");
40946f34cbfSopenharmony_ci    CHECK_AND_RETURN_LOG(callbacks_.OH_AudioCapturer_OnInterruptEvent != nullptr, "pointer to the fuction is nullptr");
41046f34cbfSopenharmony_ci
41146f34cbfSopenharmony_ci    OH_AudioInterrupt_ForceType type = (OH_AudioInterrupt_ForceType)(interruptEvent.forceType);
41246f34cbfSopenharmony_ci    OH_AudioInterrupt_Hint hint = OH_AudioInterrupt_Hint(interruptEvent.hintType);
41346f34cbfSopenharmony_ci    callbacks_.OH_AudioCapturer_OnInterruptEvent(ohAudioCapturer_, userData_, type, hint);
41446f34cbfSopenharmony_ci}
41546f34cbfSopenharmony_ci}  // namespace AudioStandard
41646f34cbfSopenharmony_ci}  // namespace OHOS
417