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 mixer_ = nullptr;
59 }
60 if (handle_) {
61 #ifndef TEST_COVERAGE
62 dlclose(handle_);
63 #endif
64 handle_ = nullptr;
65 }
66 }
67
InitMixer(AudioChannelLayout mode,int32_t channels)68 int32_t AudioDownMixStereo::InitMixer(AudioChannelLayout mode, int32_t channels)
69 {
70 if (!mixer_) {
71 AUDIO_ERR_LOG("mixer is nullptr.");
72 return ERR_INVALID_HANDLE;
73 }
74 AUDIO_INFO_LOG("channel layout:%{public}" PRIu64 ".", mode);
75 int32_t ret = mixer_->InitMixer(mode, channels);
76 if (ret != 0) {
77 AUDIO_ERR_LOG("init mixer failed.ret:%{public}d", ret);
78 return ERR_INVALID_HANDLE;
79 }
80 return SUCCESS;
81 }
82
Apply(const int32_t & frameLength,float * input,float * output)83 int32_t AudioDownMixStereo::Apply(const int32_t &frameLength, float *input, float *output)
84 {
85 if (!mixer_) {
86 AUDIO_ERR_LOG("mixer is nullptr.");
87 return ERR_INVALID_HANDLE;
88 }
89 int32_t ret = mixer_->Apply(frameLength, input, output);
90 if (ret != 0) {
91 AUDIO_ERR_LOG("apply mixer failed.ret:%{public}d", ret);
92 return ERR_INVALID_HANDLE;
93 }
94 return SUCCESS;
95 }
96 } // namespace AudioStandard
97 } // namespace OHOS