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 "OHAudioStreamBuilder"
1746f34cbfSopenharmony_ci#endif
1846f34cbfSopenharmony_ci
1946f34cbfSopenharmony_ci#include <memory>
2046f34cbfSopenharmony_ci#include "native_audiostreambuilder.h"
2146f34cbfSopenharmony_ci#include "OHAudioStreamBuilder.h"
2246f34cbfSopenharmony_ci#include "OHAudioCapturer.h"
2346f34cbfSopenharmony_ci
2446f34cbfSopenharmony_ciusing OHOS::AudioStandard::OHAudioStreamBuilder;
2546f34cbfSopenharmony_ciusing OHOS::AudioStandard::AudioSampleFormat;
2646f34cbfSopenharmony_ciusing OHOS::AudioStandard::StreamUsage;
2746f34cbfSopenharmony_ciusing OHOS::AudioStandard::AudioEncodingType;
2846f34cbfSopenharmony_ciusing OHOS::AudioStandard::ContentType;
2946f34cbfSopenharmony_ciusing OHOS::AudioStandard::SourceType;
3046f34cbfSopenharmony_ciusing OHOS::AudioStandard::InterruptMode;
3146f34cbfSopenharmony_ciusing OHOS::AudioStandard::AudioChannelLayout;
3246f34cbfSopenharmony_ciusing OHOS::AudioStandard::AudioPrivacyType;
3346f34cbfSopenharmony_ci
3446f34cbfSopenharmony_cistatic const int32_t RENDERER_TYPE = 1;
3546f34cbfSopenharmony_cistatic const int32_t CAPTURER_TYPE = 2;
3646f34cbfSopenharmony_ciconstexpr int32_t UNDEFINED_SIZE = -1;
3746f34cbfSopenharmony_ci
3846f34cbfSopenharmony_cistatic OHOS::AudioStandard::OHAudioStreamBuilder *convertBuilder(OH_AudioStreamBuilder *builder)
3946f34cbfSopenharmony_ci{
4046f34cbfSopenharmony_ci    return (OHOS::AudioStandard::OHAudioStreamBuilder*) builder;
4146f34cbfSopenharmony_ci}
4246f34cbfSopenharmony_ci
4346f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_SetSamplingRate(OH_AudioStreamBuilder *builder, int32_t rate)
4446f34cbfSopenharmony_ci{
4546f34cbfSopenharmony_ci    OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder);
4646f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed");
4746f34cbfSopenharmony_ci    return audioStreamBuilder->SetSamplingRate(rate);
4846f34cbfSopenharmony_ci}
4946f34cbfSopenharmony_ci
5046f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_SetChannelCount(OH_AudioStreamBuilder *builder, int32_t channelCount)
5146f34cbfSopenharmony_ci{
5246f34cbfSopenharmony_ci    OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder);
5346f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed");
5446f34cbfSopenharmony_ci    return audioStreamBuilder->SetChannelCount(channelCount);
5546f34cbfSopenharmony_ci}
5646f34cbfSopenharmony_ci
5746f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_SetSampleFormat(OH_AudioStreamBuilder *builder,
5846f34cbfSopenharmony_ci    OH_AudioStream_SampleFormat format)
5946f34cbfSopenharmony_ci{
6046f34cbfSopenharmony_ci    OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder);
6146f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed");
6246f34cbfSopenharmony_ci    AudioSampleFormat sampleFormat = (AudioSampleFormat)format;
6346f34cbfSopenharmony_ci    return audioStreamBuilder->SetSampleFormat(sampleFormat);
6446f34cbfSopenharmony_ci}
6546f34cbfSopenharmony_ci
6646f34cbfSopenharmony_ci
6746f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_SetFrameSizeInCallback(OH_AudioStreamBuilder *builder,
6846f34cbfSopenharmony_ci    int32_t frameSize)
6946f34cbfSopenharmony_ci{
7046f34cbfSopenharmony_ci    OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder);
7146f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed");
7246f34cbfSopenharmony_ci    return audioStreamBuilder->SetPreferredFrameSize(frameSize);
7346f34cbfSopenharmony_ci}
7446f34cbfSopenharmony_ci
7546f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_SetEncodingType(OH_AudioStreamBuilder *builder,
7646f34cbfSopenharmony_ci    OH_AudioStream_EncodingType encodingType)
7746f34cbfSopenharmony_ci{
7846f34cbfSopenharmony_ci    OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder);
7946f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed");
8046f34cbfSopenharmony_ci    AudioEncodingType type = (AudioEncodingType)encodingType;
8146f34cbfSopenharmony_ci    return audioStreamBuilder->SetEncodingType(type);
8246f34cbfSopenharmony_ci}
8346f34cbfSopenharmony_ci
8446f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_SetLatencyMode(OH_AudioStreamBuilder *builder,
8546f34cbfSopenharmony_ci    OH_AudioStream_LatencyMode latencyMode)
8646f34cbfSopenharmony_ci{
8746f34cbfSopenharmony_ci    OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder);
8846f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed");
8946f34cbfSopenharmony_ci    int32_t innerLatencyMode = (int32_t)latencyMode;
9046f34cbfSopenharmony_ci    return audioStreamBuilder->SetLatencyMode(innerLatencyMode);
9146f34cbfSopenharmony_ci}
9246f34cbfSopenharmony_ci
9346f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_SetChannelLayout(OH_AudioStreamBuilder *builder,
9446f34cbfSopenharmony_ci    OH_AudioChannelLayout channelLayout)
9546f34cbfSopenharmony_ci{
9646f34cbfSopenharmony_ci    OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder);
9746f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed");
9846f34cbfSopenharmony_ci    AudioChannelLayout layout = (AudioChannelLayout)channelLayout;
9946f34cbfSopenharmony_ci    return audioStreamBuilder->SetChannelLayout(layout);
10046f34cbfSopenharmony_ci}
10146f34cbfSopenharmony_ci
10246f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_SetRendererInfo(OH_AudioStreamBuilder *builder,
10346f34cbfSopenharmony_ci    OH_AudioStream_Usage usage)
10446f34cbfSopenharmony_ci{
10546f34cbfSopenharmony_ci    OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder);
10646f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed");
10746f34cbfSopenharmony_ci    return audioStreamBuilder->SetRendererInfo(static_cast<StreamUsage>(usage));
10846f34cbfSopenharmony_ci}
10946f34cbfSopenharmony_ci
11046f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_SetRendererCallback(OH_AudioStreamBuilder *builder,
11146f34cbfSopenharmony_ci    OH_AudioRenderer_Callbacks callbacks, void *userData)
11246f34cbfSopenharmony_ci{
11346f34cbfSopenharmony_ci    OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder);
11446f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed");
11546f34cbfSopenharmony_ci    return audioStreamBuilder->SetRendererCallback(callbacks, userData);
11646f34cbfSopenharmony_ci}
11746f34cbfSopenharmony_ci
11846f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_SetCapturerInfo(OH_AudioStreamBuilder *builder,
11946f34cbfSopenharmony_ci    OH_AudioStream_SourceType sourceType)
12046f34cbfSopenharmony_ci{
12146f34cbfSopenharmony_ci    OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder);
12246f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed");
12346f34cbfSopenharmony_ci    SourceType type = (SourceType)sourceType;
12446f34cbfSopenharmony_ci    return audioStreamBuilder->SetSourceType(type);
12546f34cbfSopenharmony_ci}
12646f34cbfSopenharmony_ci
12746f34cbfSopenharmony_ci
12846f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_Create(OH_AudioStreamBuilder **builder, OH_AudioStream_Type type)
12946f34cbfSopenharmony_ci{
13046f34cbfSopenharmony_ci    int32_t streamType = type == AUDIOSTREAM_TYPE_RENDERER ? RENDERER_TYPE : CAPTURER_TYPE;
13146f34cbfSopenharmony_ci    OHAudioStreamBuilder *streamBuilder = new OHAudioStreamBuilder(streamType);
13246f34cbfSopenharmony_ci
13346f34cbfSopenharmony_ci    *builder = (OH_AudioStreamBuilder*)streamBuilder;
13446f34cbfSopenharmony_ci
13546f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
13646f34cbfSopenharmony_ci}
13746f34cbfSopenharmony_ci
13846f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_SetCapturerCallback(OH_AudioStreamBuilder *builder,
13946f34cbfSopenharmony_ci    OH_AudioCapturer_Callbacks callbacks, void *userData)
14046f34cbfSopenharmony_ci{
14146f34cbfSopenharmony_ci    OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder);
14246f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed");
14346f34cbfSopenharmony_ci
14446f34cbfSopenharmony_ci    return audioStreamBuilder->SetCapturerCallback(callbacks, userData);
14546f34cbfSopenharmony_ci}
14646f34cbfSopenharmony_ci
14746f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_SetRendererOutputDeviceChangeCallback(OH_AudioStreamBuilder *builder,
14846f34cbfSopenharmony_ci    OH_AudioRenderer_OutputDeviceChangeCallback callback, void *userData)
14946f34cbfSopenharmony_ci{
15046f34cbfSopenharmony_ci    OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder);
15146f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed");
15246f34cbfSopenharmony_ci
15346f34cbfSopenharmony_ci    return audioStreamBuilder->SetRendererOutputDeviceChangeCallback(callback, userData);
15446f34cbfSopenharmony_ci}
15546f34cbfSopenharmony_ci
15646f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_SetRendererPrivacy(OH_AudioStreamBuilder* builder,
15746f34cbfSopenharmony_ci    OH_AudioStream_PrivacyType privacy)
15846f34cbfSopenharmony_ci{
15946f34cbfSopenharmony_ci    OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder);
16046f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed");
16146f34cbfSopenharmony_ci
16246f34cbfSopenharmony_ci    if (privacy != AUDIO_STREAM_PRIVACY_TYPE_PUBLIC && privacy != AUDIO_STREAM_PRIVACY_TYPE_PRIVATE) {
16346f34cbfSopenharmony_ci        AUDIO_ERR_LOG("Invalid param: privacy type");
16446f34cbfSopenharmony_ci        return AUDIOSTREAM_ERROR_INVALID_PARAM;
16546f34cbfSopenharmony_ci    }
16646f34cbfSopenharmony_ci
16746f34cbfSopenharmony_ci    return audioStreamBuilder->SetRendererPrivacy((AudioPrivacyType)privacy);
16846f34cbfSopenharmony_ci}
16946f34cbfSopenharmony_ci
17046f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_SetWriteDataWithMetadataCallback(OH_AudioStreamBuilder *builder,
17146f34cbfSopenharmony_ci    OH_AudioRenderer_WriteDataWithMetadataCallback callback, void *userData)
17246f34cbfSopenharmony_ci{
17346f34cbfSopenharmony_ci    OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder);
17446f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed");
17546f34cbfSopenharmony_ci    return audioStreamBuilder->SetWriteDataWithMetadataCallback(callback, userData);
17646f34cbfSopenharmony_ci}
17746f34cbfSopenharmony_ci
17846f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_SetRendererWriteDataCallback(OH_AudioStreamBuilder* builder,
17946f34cbfSopenharmony_ci    OH_AudioRenderer_OnWriteDataCallback callback, void* userData)
18046f34cbfSopenharmony_ci{
18146f34cbfSopenharmony_ci    OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder);
18246f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed");
18346f34cbfSopenharmony_ci    return audioStreamBuilder->SetRendererWriteDataCallback(callback, userData);
18446f34cbfSopenharmony_ci}
18546f34cbfSopenharmony_ci
18646f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_GenerateRenderer(OH_AudioStreamBuilder *builder,
18746f34cbfSopenharmony_ci    OH_AudioRenderer **audioRenderer)
18846f34cbfSopenharmony_ci{
18946f34cbfSopenharmony_ci    OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder);
19046f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed");
19146f34cbfSopenharmony_ci    return audioStreamBuilder->Generate(audioRenderer);
19246f34cbfSopenharmony_ci}
19346f34cbfSopenharmony_ci
19446f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_GenerateCapturer(OH_AudioStreamBuilder *builder,
19546f34cbfSopenharmony_ci    OH_AudioCapturer **audioCapturer)
19646f34cbfSopenharmony_ci{
19746f34cbfSopenharmony_ci    OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder);
19846f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed");
19946f34cbfSopenharmony_ci    return audioStreamBuilder->Generate(audioCapturer);
20046f34cbfSopenharmony_ci}
20146f34cbfSopenharmony_ci
20246f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_Destroy(OH_AudioStreamBuilder *builder)
20346f34cbfSopenharmony_ci{
20446f34cbfSopenharmony_ci    OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder);
20546f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed");
20646f34cbfSopenharmony_ci    if (audioStreamBuilder != nullptr) {
20746f34cbfSopenharmony_ci        delete audioStreamBuilder;
20846f34cbfSopenharmony_ci        audioStreamBuilder = nullptr;
20946f34cbfSopenharmony_ci        return AUDIOSTREAM_SUCCESS;
21046f34cbfSopenharmony_ci    }
21146f34cbfSopenharmony_ci    return AUDIOSTREAM_ERROR_ILLEGAL_STATE;
21246f34cbfSopenharmony_ci}
21346f34cbfSopenharmony_ci
21446f34cbfSopenharmony_ciOH_AudioStream_Result OH_AudioStreamBuilder_SetRendererInterruptMode(OH_AudioStreamBuilder* builder,
21546f34cbfSopenharmony_ci    OH_AudioInterrupt_Mode mode)
21646f34cbfSopenharmony_ci{
21746f34cbfSopenharmony_ci    OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder);
21846f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed");
21946f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG((mode == AUDIOSTREAM_INTERRUPT_MODE_SHARE ||
22046f34cbfSopenharmony_ci        mode == AUDIOSTREAM_INTERRUPT_MODE_INDEPENDENT), AUDIOSTREAM_ERROR_INVALID_PARAM, "mode is invalid");
22146f34cbfSopenharmony_ci    InterruptMode interruptMode = static_cast<InterruptMode>(mode);
22246f34cbfSopenharmony_ci    return audioStreamBuilder->SetInterruptMode(interruptMode);
22346f34cbfSopenharmony_ci}
22446f34cbfSopenharmony_ci
22546f34cbfSopenharmony_cinamespace OHOS {
22646f34cbfSopenharmony_cinamespace AudioStandard {
22746f34cbfSopenharmony_ci
22846f34cbfSopenharmony_ciOHAudioStreamBuilder::OHAudioStreamBuilder(const int32_t type) : streamType_(RENDERER_TYPE)
22946f34cbfSopenharmony_ci{
23046f34cbfSopenharmony_ci    AUDIO_INFO_LOG("OHAudioStreamBuilder created, type is %{public}d", type);
23146f34cbfSopenharmony_ci    streamType_ = type;
23246f34cbfSopenharmony_ci}
23346f34cbfSopenharmony_ci
23446f34cbfSopenharmony_ciOHAudioStreamBuilder::~OHAudioStreamBuilder()
23546f34cbfSopenharmony_ci{
23646f34cbfSopenharmony_ci    AUDIO_INFO_LOG("OHAudioStreamBuilder destroyed, type is %{public}d", streamType_);
23746f34cbfSopenharmony_ci}
23846f34cbfSopenharmony_ci
23946f34cbfSopenharmony_ciOH_AudioStream_Result OHAudioStreamBuilder::SetSamplingRate(int32_t rate)
24046f34cbfSopenharmony_ci{
24146f34cbfSopenharmony_ci    switch (rate) {
24246f34cbfSopenharmony_ci        case AudioSamplingRate::SAMPLE_RATE_8000:
24346f34cbfSopenharmony_ci        case AudioSamplingRate::SAMPLE_RATE_11025:
24446f34cbfSopenharmony_ci        case AudioSamplingRate::SAMPLE_RATE_12000:
24546f34cbfSopenharmony_ci        case AudioSamplingRate::SAMPLE_RATE_16000:
24646f34cbfSopenharmony_ci        case AudioSamplingRate::SAMPLE_RATE_22050:
24746f34cbfSopenharmony_ci        case AudioSamplingRate::SAMPLE_RATE_24000:
24846f34cbfSopenharmony_ci        case AudioSamplingRate::SAMPLE_RATE_32000:
24946f34cbfSopenharmony_ci        case AudioSamplingRate::SAMPLE_RATE_44100:
25046f34cbfSopenharmony_ci        case AudioSamplingRate::SAMPLE_RATE_48000:
25146f34cbfSopenharmony_ci        case AudioSamplingRate::SAMPLE_RATE_64000:
25246f34cbfSopenharmony_ci        case AudioSamplingRate::SAMPLE_RATE_88200:
25346f34cbfSopenharmony_ci        case AudioSamplingRate::SAMPLE_RATE_96000:
25446f34cbfSopenharmony_ci        case AudioSamplingRate::SAMPLE_RATE_176400:
25546f34cbfSopenharmony_ci        case AudioSamplingRate::SAMPLE_RATE_192000:
25646f34cbfSopenharmony_ci            AUDIO_DEBUG_LOG("sampleFormat input value is valid");
25746f34cbfSopenharmony_ci            break;
25846f34cbfSopenharmony_ci        default:
25946f34cbfSopenharmony_ci            AUDIO_ERR_LOG("sampleFormat input value is invalid");
26046f34cbfSopenharmony_ci            return AUDIOSTREAM_ERROR_INVALID_PARAM;
26146f34cbfSopenharmony_ci    }
26246f34cbfSopenharmony_ci    samplingRate_ = rate;
26346f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
26446f34cbfSopenharmony_ci}
26546f34cbfSopenharmony_ci
26646f34cbfSopenharmony_ciOH_AudioStream_Result OHAudioStreamBuilder::SetChannelCount(int32_t channelCount)
26746f34cbfSopenharmony_ci{
26846f34cbfSopenharmony_ci    switch (channelCount) {
26946f34cbfSopenharmony_ci        case AudioChannel::MONO:
27046f34cbfSopenharmony_ci        case AudioChannel::STEREO:
27146f34cbfSopenharmony_ci        case AudioChannel::CHANNEL_3:
27246f34cbfSopenharmony_ci        case AudioChannel::CHANNEL_4:
27346f34cbfSopenharmony_ci        case AudioChannel::CHANNEL_5:
27446f34cbfSopenharmony_ci        case AudioChannel::CHANNEL_6:
27546f34cbfSopenharmony_ci        case AudioChannel::CHANNEL_7:
27646f34cbfSopenharmony_ci        case AudioChannel::CHANNEL_8:
27746f34cbfSopenharmony_ci        case AudioChannel::CHANNEL_9:
27846f34cbfSopenharmony_ci        case AudioChannel::CHANNEL_10:
27946f34cbfSopenharmony_ci        case AudioChannel::CHANNEL_11:
28046f34cbfSopenharmony_ci        case AudioChannel::CHANNEL_12:
28146f34cbfSopenharmony_ci        case AudioChannel::CHANNEL_13:
28246f34cbfSopenharmony_ci        case AudioChannel::CHANNEL_14:
28346f34cbfSopenharmony_ci        case AudioChannel::CHANNEL_15:
28446f34cbfSopenharmony_ci        case AudioChannel::CHANNEL_16:
28546f34cbfSopenharmony_ci            AUDIO_DEBUG_LOG("channelCount input value is valid");
28646f34cbfSopenharmony_ci            break;
28746f34cbfSopenharmony_ci        default:
28846f34cbfSopenharmony_ci            AUDIO_ERR_LOG("channelCount input value is invalid");
28946f34cbfSopenharmony_ci            return AUDIOSTREAM_ERROR_INVALID_PARAM;
29046f34cbfSopenharmony_ci    }
29146f34cbfSopenharmony_ci    channelCount_ = channelCount;
29246f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
29346f34cbfSopenharmony_ci}
29446f34cbfSopenharmony_ci
29546f34cbfSopenharmony_ciOH_AudioStream_Result OHAudioStreamBuilder::SetSampleFormat(AudioSampleFormat sampleFormat)
29646f34cbfSopenharmony_ci{
29746f34cbfSopenharmony_ci    sampleFormat_ = sampleFormat;
29846f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
29946f34cbfSopenharmony_ci}
30046f34cbfSopenharmony_ci
30146f34cbfSopenharmony_ci
30246f34cbfSopenharmony_ciOH_AudioStream_Result OHAudioStreamBuilder::SetPreferredFrameSize(int32_t frameSize)
30346f34cbfSopenharmony_ci{
30446f34cbfSopenharmony_ci    preferredFrameSize_ = frameSize;
30546f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
30646f34cbfSopenharmony_ci}
30746f34cbfSopenharmony_ci
30846f34cbfSopenharmony_ciOH_AudioStream_Result OHAudioStreamBuilder::SetRendererInfo(StreamUsage usage)
30946f34cbfSopenharmony_ci{
31046f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(streamType_ != CAPTURER_TYPE && usage != StreamUsage::STREAM_USAGE_UNKNOWN,
31146f34cbfSopenharmony_ci        AUDIOSTREAM_ERROR_INVALID_PARAM, "Error, invalid type input");
31246f34cbfSopenharmony_ci    usage_ = usage;
31346f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
31446f34cbfSopenharmony_ci}
31546f34cbfSopenharmony_ci
31646f34cbfSopenharmony_ciOH_AudioStream_Result OHAudioStreamBuilder::SetEncodingType(AudioEncodingType encodingType)
31746f34cbfSopenharmony_ci{
31846f34cbfSopenharmony_ci    encodingType_ = encodingType;
31946f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
32046f34cbfSopenharmony_ci}
32146f34cbfSopenharmony_ci
32246f34cbfSopenharmony_ciOH_AudioStream_Result OHAudioStreamBuilder::SetSourceType(SourceType type)
32346f34cbfSopenharmony_ci{
32446f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(streamType_ != RENDERER_TYPE && type != SOURCE_TYPE_INVALID,
32546f34cbfSopenharmony_ci        AUDIOSTREAM_ERROR_INVALID_PARAM, "Error, invalid type input");
32646f34cbfSopenharmony_ci
32746f34cbfSopenharmony_ci    sourceType_ = type;
32846f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
32946f34cbfSopenharmony_ci}
33046f34cbfSopenharmony_ci
33146f34cbfSopenharmony_ci
33246f34cbfSopenharmony_ciOH_AudioStream_Result OHAudioStreamBuilder::SetLatencyMode(int32_t latencyMode)
33346f34cbfSopenharmony_ci{
33446f34cbfSopenharmony_ci    latencyMode_ = latencyMode;
33546f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
33646f34cbfSopenharmony_ci}
33746f34cbfSopenharmony_ci
33846f34cbfSopenharmony_ciOH_AudioStream_Result OHAudioStreamBuilder::SetChannelLayout(AudioChannelLayout channelLayout)
33946f34cbfSopenharmony_ci{
34046f34cbfSopenharmony_ci    channelLayout_ = channelLayout;
34146f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
34246f34cbfSopenharmony_ci}
34346f34cbfSopenharmony_ci
34446f34cbfSopenharmony_ciOH_AudioStream_Result OHAudioStreamBuilder::Generate(OH_AudioRenderer **renderer)
34546f34cbfSopenharmony_ci{
34646f34cbfSopenharmony_ci    AUDIO_INFO_LOG("Generate OHAudioRenderer");
34746f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(streamType_ == RENDERER_TYPE, AUDIOSTREAM_ERROR_INVALID_PARAM,
34846f34cbfSopenharmony_ci        "Error, invalid type input");
34946f34cbfSopenharmony_ci
35046f34cbfSopenharmony_ci    AudioStreamInfo streamInfo = {
35146f34cbfSopenharmony_ci        (AudioSamplingRate)samplingRate_,
35246f34cbfSopenharmony_ci        encodingType_,
35346f34cbfSopenharmony_ci        sampleFormat_,
35446f34cbfSopenharmony_ci        (AudioChannel)channelCount_,
35546f34cbfSopenharmony_ci        channelLayout_
35646f34cbfSopenharmony_ci    };
35746f34cbfSopenharmony_ci
35846f34cbfSopenharmony_ci    AudioRendererInfo rendererInfo = {
35946f34cbfSopenharmony_ci        CONTENT_TYPE_UNKNOWN,
36046f34cbfSopenharmony_ci        usage_,
36146f34cbfSopenharmony_ci        latencyMode_
36246f34cbfSopenharmony_ci    };
36346f34cbfSopenharmony_ci
36446f34cbfSopenharmony_ci    AudioRendererOptions options = {
36546f34cbfSopenharmony_ci        streamInfo,
36646f34cbfSopenharmony_ci        rendererInfo,
36746f34cbfSopenharmony_ci        privacyType_
36846f34cbfSopenharmony_ci    };
36946f34cbfSopenharmony_ci
37046f34cbfSopenharmony_ci    OHAudioRenderer *audioRenderer = new OHAudioRenderer();
37146f34cbfSopenharmony_ci    if (audioRenderer->Initialize(options)) {
37246f34cbfSopenharmony_ci        audioRenderer->SetRendererCallbackType(writeDataCallbackType_);
37346f34cbfSopenharmony_ci        audioRenderer->SetRendererCallback(rendererCallbacks_, userData_, metadataUserData_);
37446f34cbfSopenharmony_ci        audioRenderer->SetRendererOutputDeviceChangeCallback(outputDeviceChangecallback_, outputDeviceChangeuserData_);
37546f34cbfSopenharmony_ci        audioRenderer->SetInterruptMode(interruptMode_);
37646f34cbfSopenharmony_ci        *renderer = (OH_AudioRenderer*)audioRenderer;
37746f34cbfSopenharmony_ci        if (preferredFrameSize_ != UNDEFINED_SIZE) {
37846f34cbfSopenharmony_ci            audioRenderer->SetPreferredFrameSize(preferredFrameSize_);
37946f34cbfSopenharmony_ci        }
38046f34cbfSopenharmony_ci        return AUDIOSTREAM_SUCCESS;
38146f34cbfSopenharmony_ci    }
38246f34cbfSopenharmony_ci    AUDIO_ERR_LOG("Create OHAudioRenderer failed");
38346f34cbfSopenharmony_ci    delete audioRenderer;
38446f34cbfSopenharmony_ci    audioRenderer = nullptr;
38546f34cbfSopenharmony_ci    return AUDIOSTREAM_ERROR_INVALID_PARAM;
38646f34cbfSopenharmony_ci}
38746f34cbfSopenharmony_ci
38846f34cbfSopenharmony_ciOH_AudioStream_Result OHAudioStreamBuilder::Generate(OH_AudioCapturer **capturer)
38946f34cbfSopenharmony_ci{
39046f34cbfSopenharmony_ci    AUDIO_INFO_LOG("Generate OHAudioCapturer");
39146f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(streamType_ == CAPTURER_TYPE, AUDIOSTREAM_ERROR_INVALID_PARAM,
39246f34cbfSopenharmony_ci        "Error, invalid type input");
39346f34cbfSopenharmony_ci    AudioStreamInfo streamInfo = {
39446f34cbfSopenharmony_ci        (AudioSamplingRate)samplingRate_,
39546f34cbfSopenharmony_ci        encodingType_,
39646f34cbfSopenharmony_ci        sampleFormat_,
39746f34cbfSopenharmony_ci        (AudioChannel)channelCount_,
39846f34cbfSopenharmony_ci        channelLayout_
39946f34cbfSopenharmony_ci    };
40046f34cbfSopenharmony_ci
40146f34cbfSopenharmony_ci    AudioCapturerInfo capturerInfo = {
40246f34cbfSopenharmony_ci        sourceType_,
40346f34cbfSopenharmony_ci        latencyMode_
40446f34cbfSopenharmony_ci    };
40546f34cbfSopenharmony_ci
40646f34cbfSopenharmony_ci    AudioCapturerOptions options = {
40746f34cbfSopenharmony_ci        streamInfo,
40846f34cbfSopenharmony_ci        capturerInfo
40946f34cbfSopenharmony_ci    };
41046f34cbfSopenharmony_ci
41146f34cbfSopenharmony_ci    OHAudioCapturer *audioCapturer = new OHAudioCapturer();
41246f34cbfSopenharmony_ci    if (audioCapturer->Initialize(options)) {
41346f34cbfSopenharmony_ci        audioCapturer->SetCapturerCallback(capturerCallbacks_, userData_);
41446f34cbfSopenharmony_ci        *capturer = (OH_AudioCapturer*)audioCapturer;
41546f34cbfSopenharmony_ci        return AUDIOSTREAM_SUCCESS;
41646f34cbfSopenharmony_ci    }
41746f34cbfSopenharmony_ci    AUDIO_ERR_LOG("Create OHAudioCapturer failed");
41846f34cbfSopenharmony_ci    delete audioCapturer;
41946f34cbfSopenharmony_ci    audioCapturer = nullptr;
42046f34cbfSopenharmony_ci    return AUDIOSTREAM_ERROR_INVALID_PARAM;
42146f34cbfSopenharmony_ci}
42246f34cbfSopenharmony_ci
42346f34cbfSopenharmony_ciOH_AudioStream_Result OHAudioStreamBuilder::SetRendererCallback(OH_AudioRenderer_Callbacks callbacks, void *userData)
42446f34cbfSopenharmony_ci{
42546f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(streamType_ != CAPTURER_TYPE, AUDIOSTREAM_ERROR_INVALID_PARAM,
42646f34cbfSopenharmony_ci        "SetRendererCallback Error, invalid type input");
42746f34cbfSopenharmony_ci    writeDataCallbackType_ = WRITE_DATA_CALLBACK_WITHOUT_RESULT;
42846f34cbfSopenharmony_ci    rendererCallbacks_.callbacks = callbacks;
42946f34cbfSopenharmony_ci    userData_ = userData;
43046f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
43146f34cbfSopenharmony_ci}
43246f34cbfSopenharmony_ci
43346f34cbfSopenharmony_ciOH_AudioStream_Result OHAudioStreamBuilder::SetCapturerCallback(OH_AudioCapturer_Callbacks callbacks, void *userData)
43446f34cbfSopenharmony_ci{
43546f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(streamType_ != RENDERER_TYPE, AUDIOSTREAM_ERROR_INVALID_PARAM,
43646f34cbfSopenharmony_ci        "SetCapturerCallback Error, invalid type input");
43746f34cbfSopenharmony_ci    capturerCallbacks_ = callbacks;
43846f34cbfSopenharmony_ci    userData_ = userData;
43946f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
44046f34cbfSopenharmony_ci}
44146f34cbfSopenharmony_ci
44246f34cbfSopenharmony_ciOH_AudioStream_Result OHAudioStreamBuilder::SetRendererOutputDeviceChangeCallback(
44346f34cbfSopenharmony_ci    OH_AudioRenderer_OutputDeviceChangeCallback callback, void *userData)
44446f34cbfSopenharmony_ci{
44546f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(streamType_ != CAPTURER_TYPE, AUDIOSTREAM_ERROR_INVALID_PARAM,
44646f34cbfSopenharmony_ci        "SetRendererCallback Error, invalid type input");
44746f34cbfSopenharmony_ci    outputDeviceChangecallback_ = callback;
44846f34cbfSopenharmony_ci    outputDeviceChangeuserData_ = userData;
44946f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
45046f34cbfSopenharmony_ci}
45146f34cbfSopenharmony_ci
45246f34cbfSopenharmony_ciOH_AudioStream_Result OHAudioStreamBuilder::SetRendererPrivacy(AudioPrivacyType privacyType)
45346f34cbfSopenharmony_ci{
45446f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(streamType_ != CAPTURER_TYPE, AUDIOSTREAM_ERROR_INVALID_PARAM,
45546f34cbfSopenharmony_ci        "SetRendererPrivacy Error, invalid type input");
45646f34cbfSopenharmony_ci    privacyType_ = privacyType;
45746f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
45846f34cbfSopenharmony_ci}
45946f34cbfSopenharmony_ci
46046f34cbfSopenharmony_ciOH_AudioStream_Result OHAudioStreamBuilder::SetInterruptMode(InterruptMode mode)
46146f34cbfSopenharmony_ci{
46246f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(streamType_ == RENDERER_TYPE, AUDIOSTREAM_ERROR_INVALID_PARAM,
46346f34cbfSopenharmony_ci        "Error, invalid type input");
46446f34cbfSopenharmony_ci    interruptMode_ = mode;
46546f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
46646f34cbfSopenharmony_ci}
46746f34cbfSopenharmony_ci
46846f34cbfSopenharmony_ciOH_AudioStream_Result OHAudioStreamBuilder::SetWriteDataWithMetadataCallback(
46946f34cbfSopenharmony_ci    OH_AudioRenderer_WriteDataWithMetadataCallback callback, void *userData)
47046f34cbfSopenharmony_ci{
47146f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(streamType_ != CAPTURER_TYPE, AUDIOSTREAM_ERROR_INVALID_PARAM,
47246f34cbfSopenharmony_ci        "SetRendererCallback Error, invalid type input");
47346f34cbfSopenharmony_ci    rendererCallbacks_.writeDataWithMetadataCallback = callback;
47446f34cbfSopenharmony_ci    metadataUserData_ = userData;
47546f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
47646f34cbfSopenharmony_ci}
47746f34cbfSopenharmony_ci
47846f34cbfSopenharmony_ciOH_AudioStream_Result OHAudioStreamBuilder::SetRendererWriteDataCallback(
47946f34cbfSopenharmony_ci    OH_AudioRenderer_OnWriteDataCallback callback, void *userData)
48046f34cbfSopenharmony_ci{
48146f34cbfSopenharmony_ci    CHECK_AND_RETURN_RET_LOG(streamType_ != CAPTURER_TYPE, AUDIOSTREAM_ERROR_INVALID_PARAM,
48246f34cbfSopenharmony_ci        "Set renderer callback error, invalid type input.");
48346f34cbfSopenharmony_ci    writeDataCallbackType_ = WRITE_DATA_CALLBACK_WITH_RESULT;
48446f34cbfSopenharmony_ci    rendererCallbacks_.onWriteDataCallback = callback;
48546f34cbfSopenharmony_ci    userData_ = userData;
48646f34cbfSopenharmony_ci    return AUDIOSTREAM_SUCCESS;
48746f34cbfSopenharmony_ci}
48846f34cbfSopenharmony_ci}  // namespace AudioStandard
48946f34cbfSopenharmony_ci}  // namespace OHOS
490