1 /*
2 * Copyright 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "a2dp_vendor_ldac_abr"
18
19 #include "a2dp_vendor_ldac_abr.h"
20
21 #include <dlfcn.h>
22 #include <inttypes.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "osi/include/log.h"
27
28 //
29 // LDAC ABR(Adaptive Bit Rate) Source Code
30 //
31
32 //
33 // The LDAC ABR shared library, and the functions to use
34 //
35 static const char* LDAC_ABR_LIB_NAME = "libldacBT_abr.so";
36 static void* ldac_abr_lib_handle = NULL;
37
38 static const char* LDAC_ABR_GET_HANDLE_NAME = "ldac_ABR_get_handle";
39 typedef HANDLE_LDAC_ABR (*tLDAC_ABR_GET_HANDLE)(void);
40
41 static const char* LDAC_ABR_FREE_HANDLE_NAME = "ldac_ABR_free_handle";
42 typedef void (*tLDAC_ABR_FREE_HANDLE)(HANDLE_LDAC_ABR hLdacAbr);
43
44 static const char* LDAC_ABR_INIT_NAME = "ldac_ABR_Init";
45 typedef int (*tLDAC_ABR_INIT)(HANDLE_LDAC_ABR hLdacAbr,
46 unsigned int interval_ms);
47
48 static const char* LDAC_ABR_SET_THRESHOLDS_NAME = "ldac_ABR_set_thresholds";
49 typedef int (*tLDAC_ABR_SET_THRESHOLDS)(HANDLE_LDAC_ABR hLdacAbr,
50 unsigned int th_critical,
51 unsigned int th_dangerous_trend,
52 unsigned int th_safety_4hqsq);
53
54 static const char* LDAC_ABR_PROC_NAME = "ldac_ABR_Proc";
55 typedef int (*tLDAC_ABR_PROC)(HANDLE_LDAC_BT hLdacParam,
56 HANDLE_LDAC_ABR hLdacAbr, unsigned int txq_length,
57 unsigned int flag_enable);
58
59 static tLDAC_ABR_GET_HANDLE ldac_abr_get_handle_func;
60 static tLDAC_ABR_FREE_HANDLE ldac_abr_free_handle_func;
61 static tLDAC_ABR_INIT ldac_abr_init_func;
62 static tLDAC_ABR_SET_THRESHOLDS ldac_abr_set_thresholds_func;
63 static tLDAC_ABR_PROC ldac_abr_proc_func;
64
A2DP_VendorLoadLdacAbr(void)65 bool A2DP_VendorLoadLdacAbr(void) {
66 if (ldac_abr_lib_handle != NULL) return true; // Already loaded
67
68 // Open the LDAC ABR library
69 ldac_abr_lib_handle = dlopen(LDAC_ABR_LIB_NAME, RTLD_NOW);
70 if (ldac_abr_lib_handle == NULL) {
71 LOG_ERROR("%s: cannot open LDAC ABR library %s: %s", __func__,
72 LDAC_ABR_LIB_NAME, dlerror());
73 A2DP_VendorUnloadLdacAbr();
74 return false;
75 }
76
77 // Load all functions
78 ldac_abr_get_handle_func = (tLDAC_ABR_GET_HANDLE)dlsym(
79 ldac_abr_lib_handle, LDAC_ABR_GET_HANDLE_NAME);
80 if (ldac_abr_get_handle_func == NULL) {
81 LOG_ERROR("%s: cannot find function '%s' in the LDAC ABR library: %s",
82 __func__, LDAC_ABR_GET_HANDLE_NAME, dlerror());
83 A2DP_VendorUnloadLdacAbr();
84 return false;
85 }
86
87 ldac_abr_free_handle_func = (tLDAC_ABR_FREE_HANDLE)dlsym(
88 ldac_abr_lib_handle, LDAC_ABR_FREE_HANDLE_NAME);
89 if (ldac_abr_free_handle_func == NULL) {
90 LOG_ERROR("%s: cannot find function '%s' in the LDAC ABR library: %s",
91 __func__, LDAC_ABR_FREE_HANDLE_NAME, dlerror());
92 A2DP_VendorUnloadLdacAbr();
93 return false;
94 }
95
96 ldac_abr_init_func =
97 (tLDAC_ABR_INIT)dlsym(ldac_abr_lib_handle, LDAC_ABR_INIT_NAME);
98 if (ldac_abr_init_func == NULL) {
99 LOG_ERROR("%s: cannot find function '%s' in the LDAC ABR library: %s",
100 __func__, LDAC_ABR_INIT_NAME, dlerror());
101 A2DP_VendorUnloadLdacAbr();
102 return false;
103 }
104
105 ldac_abr_set_thresholds_func = (tLDAC_ABR_SET_THRESHOLDS)dlsym(
106 ldac_abr_lib_handle, LDAC_ABR_SET_THRESHOLDS_NAME);
107 if (ldac_abr_set_thresholds_func == NULL) {
108 LOG_ERROR("%s: cannot find function '%s' in the LDAC ABR library: %s",
109 __func__, LDAC_ABR_SET_THRESHOLDS_NAME, dlerror());
110 A2DP_VendorUnloadLdacAbr();
111 return false;
112 }
113
114 ldac_abr_proc_func =
115 (tLDAC_ABR_PROC)dlsym(ldac_abr_lib_handle, LDAC_ABR_PROC_NAME);
116 if (ldac_abr_proc_func == NULL) {
117 LOG_ERROR("%s: cannot find function '%s' in the LDAC ABR library: %s",
118 __func__, LDAC_ABR_PROC_NAME, dlerror());
119 A2DP_VendorUnloadLdacAbr();
120 return false;
121 }
122
123 return true;
124 }
125
A2DP_VendorUnloadLdacAbr(void)126 void A2DP_VendorUnloadLdacAbr(void) {
127 ldac_abr_get_handle_func = NULL;
128 ldac_abr_free_handle_func = NULL;
129 ldac_abr_init_func = NULL;
130 ldac_abr_set_thresholds_func = NULL;
131 ldac_abr_proc_func = NULL;
132
133 if (ldac_abr_lib_handle != NULL) {
134 dlclose(ldac_abr_lib_handle);
135 ldac_abr_lib_handle = NULL;
136 }
137 }
138
a2dp_ldac_abr_get_handle(void)139 HANDLE_LDAC_ABR a2dp_ldac_abr_get_handle(void) {
140 return ldac_abr_get_handle_func();
141 }
142
a2dp_ldac_abr_free_handle(HANDLE_LDAC_ABR hLdacAbr)143 void a2dp_ldac_abr_free_handle(HANDLE_LDAC_ABR hLdacAbr) {
144 return ldac_abr_free_handle_func(hLdacAbr);
145 }
146
a2dp_ldac_abr_init(HANDLE_LDAC_ABR hLdacAbr,unsigned int interval_ms)147 int a2dp_ldac_abr_init(HANDLE_LDAC_ABR hLdacAbr, unsigned int interval_ms) {
148 return ldac_abr_init_func(hLdacAbr, interval_ms);
149 }
150
a2dp_ldac_abr_set_thresholds(HANDLE_LDAC_ABR hLdacAbr,unsigned int th_critical,unsigned int th_dangerous_trend,unsigned int th_4hqsq)151 int a2dp_ldac_abr_set_thresholds(HANDLE_LDAC_ABR hLdacAbr,
152 unsigned int th_critical,
153 unsigned int th_dangerous_trend,
154 unsigned int th_4hqsq) {
155 return ldac_abr_set_thresholds_func(hLdacAbr, th_critical, th_dangerous_trend,
156 th_4hqsq);
157 }
158
a2dp_ldac_abr_proc(HANDLE_LDAC_BT hLdacParam,HANDLE_LDAC_ABR hLdacAbr,size_t transmit_queue_length,unsigned int flag_enable)159 int a2dp_ldac_abr_proc(HANDLE_LDAC_BT hLdacParam, HANDLE_LDAC_ABR hLdacAbr,
160 size_t transmit_queue_length, unsigned int flag_enable) {
161 return ldac_abr_proc_func(hLdacParam, hLdacAbr, transmit_queue_length,
162 flag_enable);
163 }