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 #ifndef AUDIO_CONVERTER_3DA
16 #define AUDIO_CONVERTER_3DA
17 
18 #include <cstdint>
19 #include <cstring>
20 #include <string>
21 #include <dlfcn.h>
22 #include <unistd.h>
23 #include "audio_info.h"
24 #include "audio_effect.h"
25 #include "audio_service_log.h"
26 
27 namespace OHOS {
28 namespace AudioStandard {
29 
30 class LibLoader {
31 public:
32     LibLoader() = default;
33     ~LibLoader();
34     bool Init();
35     uint32_t GetLatency();
36     bool AddAlgoHandle(Library lib);
37     void SetIOBufferConfig(bool isInput, uint32_t sampleRate, uint8_t format, uint32_t channels,
38         uint64_t channelLayout);
39     int32_t ApplyAlgo(AudioBuffer &inputBuffer, AudioBuffer &outputBuffer);
40     bool FlushAlgo();
41 
42 private:
43     bool LoadLibrary(const std::string &relativePath) noexcept;
44     std::unique_ptr<AudioEffectLibEntry> libEntry_;
45     uint32_t latency_;
46     AudioEffectHandle handle_;
47     AudioEffectConfig ioBufferConfig_;
48     void *libHandle_;
49 };
50 
51 class AudioSpatialChannelConverter {
52 public:
53     AudioSpatialChannelConverter() = default;
54     bool Init(const AudioStreamParams info, const ConverterConfig cfg);
55     bool GetInputBufferSize(size_t &bufferSize);
56     size_t GetMetaSize();
57     bool CheckInputValid(const BufferDesc bufDesc);
58     bool AllocateMem();
59     bool Flush();
60     uint32_t GetLatency();
61     void Process(const BufferDesc bufDesc);
62     void ConverterChannels(uint8_t &channel, uint64_t &channelLayout);
63     void GetOutputBufferStream(uint8_t *&buffer, uint32_t &bufferLen);
64 
65 private:
66     size_t GetPcmLength(int32_t channels, int8_t bps);
67 
68     std::unique_ptr<uint8_t[]> outPcmBuf_;
69 
70     int32_t inChannel_;
71     int32_t outChannel_;
72     int32_t sampleRate_;
73 
74     uint8_t bps_;
75     uint8_t encoding_;
76 
77     uint64_t outChannelLayout_;
78 
79     bool loadSuccess_;
80 
81     LibLoader externalLoader_;
82 };
83 
84 } // namespace AudioStandard
85 } // namespace OHOS
86 #endif // AUDIO_CONVERTER_3DA