1 /* 2 * Copyright (c) 2023-2025 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 #ifndef OH_AUDIO_STREAM_BUILDER_H 17 #define OH_AUDIO_STREAM_BUILDER_H 18 19 #include <cstdint> 20 #include "native_audiostream_base.h" 21 #include "audio_interrupt_info.h" 22 #include "OHAudioRenderer.h" 23 #include "OHAudioCapturer.h" 24 25 namespace OHOS { 26 namespace AudioStandard { 27 class OHAudioStreamBuilder { 28 public: 29 explicit OHAudioStreamBuilder(const int32_t type); 30 31 ~OHAudioStreamBuilder(); 32 33 OH_AudioStream_Result Generate(OH_AudioRenderer **renderer); 34 OH_AudioStream_Result Generate(OH_AudioCapturer **capturer); 35 36 OH_AudioStream_Result SetSamplingRate(int32_t rate); 37 OH_AudioStream_Result SetChannelCount(int32_t channelCount); 38 OH_AudioStream_Result SetSampleFormat(AudioSampleFormat sampleFormat); 39 OH_AudioStream_Result SetEncodingType(AudioEncodingType encodingType); 40 OH_AudioStream_Result SetPreferredFrameSize(int32_t frameSize); 41 OH_AudioStream_Result SetLatencyMode(int32_t latencyMode); 42 OH_AudioStream_Result SetChannelLayout(AudioChannelLayout channelLayout); 43 44 OH_AudioStream_Result SetRendererInfo(StreamUsage usage); 45 OH_AudioStream_Result SetAudioVolumeMode(AudioVolumeMode volumeMode); 46 OH_AudioStream_Result SetRendererCallback(OH_AudioRenderer_Callbacks callbacks, void *userData); 47 OH_AudioStream_Result SetRendererOutputDeviceChangeCallback(OH_AudioRenderer_OutputDeviceChangeCallback callback, 48 void *userData); 49 OH_AudioStream_Result SetRendererPrivacy(AudioPrivacyType privacyType); 50 OH_AudioStream_Result SetWriteDataWithMetadataCallback(OH_AudioRenderer_WriteDataWithMetadataCallback callback, 51 void *userData); 52 OH_AudioStream_Result SetRendererWriteDataCallback(OH_AudioRenderer_OnWriteDataCallback callback, 53 void* userData); 54 OH_AudioStream_Result SetRendererInterruptEventCallback(OH_AudioRenderer_OnInterruptCallback callback, 55 void* userData); 56 OH_AudioStream_Result SetRendererErrorCallback(OH_AudioRenderer_OnErrorCallback callback, void* userData); 57 58 OH_AudioStream_Result SetSourceType(SourceType type); 59 OH_AudioStream_Result SetCapturerCallback(OH_AudioCapturer_Callbacks callbacks, void *userData); 60 OH_AudioStream_Result SetInterruptMode(InterruptMode mode); 61 OH_AudioStream_Result SetCapturerReadDataCallback(OH_AudioCapturer_OnReadDataCallback callback, 62 void* userData); 63 OH_AudioStream_Result SetCapturerStreamEventCallback(OH_AudioCapturer_OnDeviceChangeCallback callback, 64 void* userData); 65 OH_AudioStream_Result SetCapturerInterruptCallback( 66 OH_AudioCapturer_OnInterruptCallback callback, void *userData); 67 OH_AudioStream_Result SetCapturerErrorCallback(OH_AudioCapturer_OnErrorCallback callback, 68 void *userData); 69 70 private: 71 int32_t streamType_; 72 int32_t latencyMode_ = 0; // default value is normal mode 73 int32_t preferredFrameSize_ = -1; // undefined clientBufferSizeInFrame 74 75 // stream params 76 int32_t samplingRate_ = SAMPLE_RATE_48000; 77 int32_t channelCount_ = STEREO; 78 AudioEncodingType encodingType_ = ENCODING_PCM; 79 AudioSampleFormat sampleFormat_ = SAMPLE_S16LE; 80 AudioChannelLayout channelLayout_ = CH_LAYOUT_UNKNOWN; 81 82 // renderer params 83 StreamUsage usage_ = STREAM_USAGE_UNKNOWN; 84 AudioPrivacyType privacyType_ = PRIVACY_TYPE_PUBLIC; 85 AudioVolumeMode volumeMode_ = AUDIOSTREAM_VOLUMEMODE_SYSTEM_GLOBAL; 86 // capturer params 87 SourceType sourceType_ = SOURCE_TYPE_MIC; 88 89 WriteDataCallbackType writeDataCallbackType_ = WRITE_DATA_CALLBACK_WITHOUT_RESULT; 90 ReadDataCallbackType readDataCallbackType_ = READ_DATA_CALLBACK_WITHOUT_RESULT; 91 StreamEventCallbackType streamEventCallbackType_ = STREAM_EVENT_CALLBACK_WITHOUT_RESULT; 92 InterruptEventCallbackType interruptCallbackType_ = INTERRUPT_EVENT_CALLBACK_WITHOUT_RESULT; 93 ErrorCallbackType errorCallbackType_ = ERROR_CALLBACK_WITHOUT_RESULT; 94 RendererCallback rendererCallbacks_ = { 95 {nullptr}, 96 97 nullptr, 98 99 nullptr, 100 }; 101 102 CapturerCallback capturerCallbacks_ = { 103 {nullptr}, 104 105 nullptr, 106 107 nullptr, 108 109 nullptr, 110 111 nullptr 112 }; 113 114 void *userData_ = nullptr; 115 116 OH_AudioRenderer_OutputDeviceChangeCallback outputDeviceChangecallback_ = nullptr; 117 void *outputDeviceChangeuserData_ = nullptr; 118 void *metadataUserData_ = nullptr; 119 InterruptMode interruptMode_ = SHARE_MODE; 120 }; 121 } // namespace AudioStandard 122 } // namespace OHOS 123 124 #endif // OH_AUDIO_STREAM_BUILDER_H 125