1 /*
2 * Copyright (c) 2024 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 LOG_TAG
16 #define LOG_TAG "AudioDownMixStereo"
17 #endif
18
19 #include "audio_down_mix_stereo.h"
20 #include "audio_service_log.h"
21 #include "audio_errors.h"
22 #include <dlfcn.h>
23 #include <cinttypes>
24
25 namespace OHOS {
26 namespace AudioStandard {
27 #if (defined(__aarch64__) || defined(__x86_64__))
28 const char *DOWN_MIXER_FILE_NAME = "/system/lib64/libaudio_downmixer_mcr.z.so";
29 #else
30 const char *DOWN_MIXER_FILE_NAME = "/system/lib/libaudio_downmixer_mcr.z.so";
31 #endif
32 const char *DOWN_MIX_FUNC_NAME = "AudioMcrMixerClassCreate";
33
AudioDownMixStereo()34 AudioDownMixStereo::AudioDownMixStereo() : handle_(nullptr), mixer_(nullptr)
35 {
36 if (access(DOWN_MIXER_FILE_NAME, R_OK) != 0) {
37 AUDIO_ERR_LOG("so file not exist.");
38 return;
39 }
40 handle_ = ::dlopen(DOWN_MIXER_FILE_NAME, RTLD_NOW);
41 if (!handle_) {
42 AUDIO_ERR_LOG("dlopen failed check so file exists");
43 return;
44 }
45 AudioMcrMixerClassCreateFunc *createMixerFunc =
46 reinterpret_cast<AudioMcrMixerClassCreateFunc *>(dlsym(handle_, DOWN_MIX_FUNC_NAME));
47 if (!createMixerFunc) {
48 AUDIO_ERR_LOG("dlsym failed.check so has this function.");
49 return;
50 }
51 createMixerFunc(&mixer_);
52 }
53
~AudioDownMixStereo()54 AudioDownMixStereo::~AudioDownMixStereo()
55 {
56 if (mixer_) {
57 free(mixer_);
58 }
59 if (handle_) {
60 #ifndef TEST_COVERAGE
61 dlclose(handle_);
62 #endif
63 handle_ = nullptr;
64 }
65 }
66
InitMixer(AudioChannelLayout mode,int32_t channels)67 int32_t AudioDownMixStereo::InitMixer(AudioChannelLayout mode, int32_t channels)
68 {
69 if (!mixer_) {
70 AUDIO_ERR_LOG("mixer is nullptr.");
71 return ERR_INVALID_HANDLE;
72 }
73 AUDIO_INFO_LOG("channel layout:%{public}" PRIu64 ".", mode);
74 int32_t ret = mixer_->InitMixer(mode, channels);
75 if (ret != 0) {
76 AUDIO_ERR_LOG("init mixer failed.ret:%{public}d", ret);
77 return ERR_INVALID_HANDLE;
78 }
79 return SUCCESS;
80 }
81
Apply(const int32_t & frameLength,float * input,float * output)82 int32_t AudioDownMixStereo::Apply(const int32_t &frameLength, float *input, float *output)
83 {
84 if (!mixer_) {
85 AUDIO_ERR_LOG("mixer is nullptr.");
86 return ERR_INVALID_HANDLE;
87 }
88 int32_t ret = mixer_->Apply(frameLength, input, output);
89 if (ret != 0) {
90 AUDIO_ERR_LOG("apply mixer failed.ret:%{public}d", ret);
91 return ERR_INVALID_HANDLE;
92 }
93 return SUCCESS;
94 }
95 } // namespace AudioStandard
96 } // namespace OHOS