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 SetRendererWriteDataCallbackAdvanced(OH_AudioRenderer_OnWriteDataCallbackAdvanced callback, 57 void *userData); 58 OH_AudioStream_Result SetRendererErrorCallback(OH_AudioRenderer_OnErrorCallback callback, void* userData); 59 OH_AudioStream_Result SetRendererFastStatusChangeCallback( 60 OH_AudioRenderer_OnFastStatusChange callback, void* userData); 61 62 OH_AudioStream_Result SetSourceType(SourceType type); 63 OH_AudioStream_Result SetCapturerCallback(OH_AudioCapturer_Callbacks callbacks, void *userData); 64 OH_AudioStream_Result SetInterruptMode(InterruptMode mode); 65 OH_AudioStream_Result SetCapturerReadDataCallback(OH_AudioCapturer_OnReadDataCallback callback, 66 void* userData); 67 OH_AudioStream_Result SetCapturerStreamEventCallback(OH_AudioCapturer_OnDeviceChangeCallback callback, 68 void* userData); 69 OH_AudioStream_Result SetCapturerInterruptCallback( 70 OH_AudioCapturer_OnInterruptCallback callback, void *userData); 71 OH_AudioStream_Result SetCapturerErrorCallback(OH_AudioCapturer_OnErrorCallback callback, 72 void *userData); 73 OH_AudioStream_Result SetMuteWhenInterrupted(bool muteWhenInterrupted); 74 OH_AudioStream_Result SetCapturerFastStatusChangeCallback( 75 OH_AudioCapturer_OnFastStatusChange callback, void *userData); 76 77 private: 78 int32_t streamType_; 79 int32_t latencyMode_ = 0; // default value is normal mode 80 int32_t preferredFrameSize_ = -1; // undefined clientBufferSizeInFrame 81 82 // stream params 83 int32_t samplingRate_ = SAMPLE_RATE_48000; 84 int32_t channelCount_ = STEREO; 85 AudioEncodingType encodingType_ = ENCODING_PCM; 86 AudioSampleFormat sampleFormat_ = SAMPLE_S16LE; 87 AudioChannelLayout channelLayout_ = CH_LAYOUT_UNKNOWN; 88 89 // renderer params 90 StreamUsage usage_ = STREAM_USAGE_UNKNOWN; 91 AudioPrivacyType privacyType_ = PRIVACY_TYPE_PUBLIC; 92 AudioVolumeMode volumeMode_ = AUDIOSTREAM_VOLUMEMODE_SYSTEM_GLOBAL; 93 // capturer params 94 SourceType sourceType_ = SOURCE_TYPE_MIC; 95 96 WriteDataCallbackType writeDataCallbackType_ = WRITE_DATA_CALLBACK_WITHOUT_RESULT; 97 ReadDataCallbackType readDataCallbackType_ = READ_DATA_CALLBACK_WITHOUT_RESULT; 98 StreamEventCallbackType streamEventCallbackType_ = STREAM_EVENT_CALLBACK_COMBINED; 99 InterruptEventCallbackType interruptCallbackType_ = INTERRUPT_EVENT_CALLBACK_COMBINED; 100 ErrorCallbackType errorCallbackType_ = ERROR_CALLBACK_COMBINED; 101 RendererCallback rendererCallbacks_ = { 102 {nullptr}, 103 104 nullptr, 105 106 nullptr, 107 }; 108 109 CapturerCallback capturerCallbacks_ = { 110 {nullptr}, 111 112 nullptr, 113 114 nullptr, 115 116 nullptr, 117 118 nullptr 119 }; 120 121 void *userData_ = nullptr; 122 123 OH_AudioRenderer_OutputDeviceChangeCallback outputDeviceChangecallback_ = nullptr; 124 void *outputDeviceChangeuserData_ = nullptr; 125 void *metadataUserData_ = nullptr; 126 OH_AudioRenderer_OnFastStatusChange rendererFastStatusChangeCallback_ = nullptr; 127 void *rendererFastStatusChangeUserData_ = nullptr; 128 OH_AudioCapturer_OnFastStatusChange capturerFastStatusChangeCallback_ = nullptr; 129 void *capturerFastStatusChangeUserData_ = nullptr; 130 InterruptMode interruptMode_ = SHARE_MODE; 131 InterruptStrategy strategy_ = InterruptStrategy::DEFAULT; 132 }; 133 } // namespace AudioStandard 134 } // namespace OHOS 135 136 #endif // OH_AUDIO_STREAM_BUILDER_H 137