• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include <dlfcn.h>
17 #include <hdf_base.h>
18 #include <hdf_dlist.h>
19 #include <hdf_log.h>
20 #include <osal_mem.h>
21 
22 #include "v1_0/iaudio_manager.h"
23 
24 #define HDF_LOG_TAG    HDF_AUDIO_PRIMARY_SRV
25 
26 typedef struct IAudioManager* (*AudioManagerCreateIfInstance)(void);
27 typedef int32_t (*AudioManagerDestroyIfInstance)(struct IAudioManager *);
28 
29 struct AudioManagerPriv {
30     void *handle;
31     AudioManagerCreateIfInstance createIfInstance;
32     AudioManagerDestroyIfInstance destroyIfInstance;
33 };
34 
35 static const char *g_hdiAudioLibPath = HDF_LIBRARY_FULL_PATH("libaudio_primary_impl_vendor");
36 
GetAudioManagerPriv(void)37 static struct AudioManagerPriv *GetAudioManagerPriv(void)
38 {
39     static struct AudioManagerPriv priv;
40     return &priv;
41 }
42 
AudioManagerLoadPrimaryLib(struct AudioManagerPriv * priv)43 static int32_t AudioManagerLoadPrimaryLib(struct AudioManagerPriv *priv)
44 {
45     char *error = NULL;
46 
47     if (g_hdiAudioLibPath == NULL || priv == NULL) {
48         HDF_LOGE("%{public}s:para is null", __func__);
49         return HDF_ERR_INVALID_PARAM;
50     }
51 
52     priv->handle = dlopen(g_hdiAudioLibPath, RTLD_LAZY);
53     if (priv->handle == NULL) {
54         error = dlerror();
55         HDF_LOGE("%{public}s:load path%{public}s, dlopen err=%{public}s", __func__, g_hdiAudioLibPath, error);
56         return HDF_FAILURE;
57     }
58 
59     (void)dlerror(); // clear existing error
60 
61     priv->createIfInstance = dlsym(priv->handle, "AudioManagerCreateIfInstance");
62     if (priv->createIfInstance == NULL) {
63         error = dlerror();
64         HDF_LOGE("%{public}s:dlsym AudioManagerCreateIfInstance err=%{public}s", __func__, error);
65         goto ERROR;
66     }
67 
68     priv->destroyIfInstance = dlsym(priv->handle, "AudioManagerDestroyIfInstance");
69     if (priv->destroyIfInstance == NULL) {
70         error = dlerror();
71         HDF_LOGE("%{public}s:dlsym AudioManagerDestroyIfInstance err=%{public}s", __func__, error);
72         goto ERROR;
73     }
74 
75     HDF_LOGI("%{public}s:hdi audio impl lib load success", __func__);
76     return HDF_SUCCESS;
77 
78 ERROR:
79     if (priv->handle != NULL) {
80         dlclose(priv->handle);
81         priv->handle = NULL;
82     }
83     priv->createIfInstance = NULL;
84     priv->destroyIfInstance = NULL;
85     return HDF_FAILURE;
86 }
87 
AudioManagerImplGetInstance(void)88 struct IAudioManager *AudioManagerImplGetInstance(void)
89 {
90     struct AudioManagerPriv *priv = GetAudioManagerPriv();
91 
92     int32_t ret = AudioManagerLoadPrimaryLib(priv);
93     if (ret != HDF_SUCCESS) {
94         HDF_LOGE("%{public}s:Audio manager load lib failed, ret[%{public}d]", __func__, ret);
95         return NULL;
96     }
97 
98     if (priv->createIfInstance == NULL) {
99         HDF_LOGE("%{public}s:Audio manager createIfInstance is NULL", __func__);
100         dlclose(priv->handle);
101         priv->handle = NULL;
102         return NULL;
103     }
104 
105     struct IAudioManager *interface = priv->createIfInstance();
106     if (interface == NULL) {
107         HDF_LOGE("%{public}s:call createIfInstance fail", __func__);
108         dlclose(priv->handle);
109         priv->handle = NULL;
110         return NULL;
111     }
112 
113     return interface;
114 }
115 
AudioManagerImplRelease(struct IAudioManager * manager)116 void AudioManagerImplRelease(struct IAudioManager *manager)
117 {
118     struct AudioManagerPriv *priv = GetAudioManagerPriv();
119 
120     if (manager == NULL) {
121         HDF_LOGE("%{public}s:manager is null", __func__);
122         goto ERROR;
123     }
124 
125     if (priv->destroyIfInstance == NULL) {
126         HDF_LOGE("%{public}s:Audio manager destroyIfInstance is NULL", __func__);
127         goto ERROR;
128     }
129 
130     int32_t ret = priv->destroyIfInstance(manager);
131     if (ret != HDF_SUCCESS) {
132         HDF_LOGE("%{public}s:call destroyIfInstance fail, ret=%{public}d", __func__, ret);
133     }
134 
135 ERROR:
136     if (priv->handle != NULL) {
137         dlclose(priv->handle);
138         priv->handle = NULL;
139     }
140 
141     priv->createIfInstance = NULL;
142     priv->destroyIfInstance = NULL;
143 }
144