• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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_decoder"
18 #define ATRACE_TAG ATRACE_TAG_AUDIO
19 
20 #include "a2dp_vendor_ldac_decoder.h"
21 
22 #ifndef OS_GENERIC
23 #include <cutils/trace.h>
24 #endif
25 #include <dlfcn.h>
26 #include <inttypes.h>
27 #include <stdio.h>
28 #include <string.h>
29 
30 #include <ldacBT.h>
31 
32 #include "a2dp_vendor.h"
33 #include "a2dp_vendor_ldac.h"
34 #include "bt_common.h"
35 #include "osi/include/log.h"
36 #include "osi/include/osi.h"
37 
38 //
39 // Decoder for LDAC Source Codec
40 //
41 
42 //
43 // The LDAC decoder shared library, and the functions to use
44 //
45 static const char* LDAC_DECODER_LIB_NAME = "libldacBT_dec.so";
46 static void* ldac_decoder_lib_handle = NULL;
47 
48 static const char* LDAC_GET_HANDLE_NAME = "ldacBT_get_handle";
49 typedef HANDLE_LDAC_BT (*tLDAC_GET_HANDLE)(void);
50 
51 static const char* LDAC_FREE_HANDLE_NAME = "ldacBT_free_handle";
52 typedef void (*tLDAC_FREE_HANDLE)(HANDLE_LDAC_BT hLdacParam);
53 
54 static const char* LDAC_CLOSE_HANDLE_NAME = "ldacBT_close_handle";
55 typedef void (*tLDAC_CLOSE_HANDLE)(HANDLE_LDAC_BT hLdacParam);
56 
57 static const char* LDAC_GET_VERSION_NAME = "ldacBT_get_version";
58 typedef int (*tLDAC_GET_VERSION)(void);
59 
60 static const char* LDAC_GET_BITRATE_NAME = "ldacBT_get_bitrate";
61 typedef int (*tLDAC_GET_BITRATE)(HANDLE_LDAC_BT hLdacParam);
62 
63 static const char* LDAC_GET_SAMPLING_FREQ_NAME = "ldacBT_get_sampling_freq";
64 typedef int (*tLDAC_GET_SAMPLING_FREQ)(HANDLE_LDAC_BT hLdacParam);
65 
66 static const char* LDAC_INIT_HANDLE_DECODE_NAME = "ldacBT_init_handle_decode";
67 typedef int (*tLDAC_INIT_HANDLE_DECODE)(HANDLE_LDAC_BT hLdacParam, int cm,
68                                         int sf, int var0, int var1, int var2);
69 
70 static const char* LDAC_DECODE_NAME = "ldacBT_decode";
71 typedef int (*tLDAC_DECODE)(HANDLE_LDAC_BT hLdacBt, unsigned char* p_bs,
72                             unsigned char* p_pcm, LDACBT_SMPL_FMT_T fmt,
73                             int bs_bytes, int* used_bytes, int* wrote_bytes);
74 
75 static const char* LDAC_GET_ERROR_CODE_NAME = "ldacBT_get_error_code";
76 typedef int (*tLDAC_GET_ERROR_CODE)(HANDLE_LDAC_BT hLdacParam);
77 
78 static tLDAC_GET_HANDLE ldac_get_handle_func;
79 static tLDAC_FREE_HANDLE ldac_free_handle_func;
80 static tLDAC_CLOSE_HANDLE ldac_close_handle_func;
81 static tLDAC_GET_VERSION ldac_get_version_func;
82 static tLDAC_GET_BITRATE ldac_get_bitrate_func;
83 static tLDAC_GET_SAMPLING_FREQ ldac_get_sampling_freq_func;
84 static tLDAC_INIT_HANDLE_DECODE ldac_init_handle_decode_func;
85 static tLDAC_DECODE ldac_decode_func;
86 static tLDAC_GET_ERROR_CODE ldac_get_error_code_func;
87 
88 // offset
89 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
90 #define A2DP_LDAC_OFFSET (AVDT_MEDIA_OFFSET + A2DP_LDAC_MPL_HDR_LEN + 1)
91 #else
92 #define A2DP_LDAC_OFFSET (AVDT_MEDIA_OFFSET + A2DP_LDAC_MPL_HDR_LEN)
93 #endif
94 
95 typedef struct {
96   uint32_t sample_rate;
97   uint8_t channel_mode;
98   uint8_t bits_per_sample;
99   int pcm_wlength;
100   LDACBT_SMPL_FMT_T pcm_fmt;
101 } tA2DP_LDAC_DECODER_PARAMS;
102 
103 typedef struct {
104   bool use_SCMS_T;
105   bool is_peer_edr;          // True if the peer device supports EDR
106   bool peer_supports_3mbps;  // True if the peer device supports 3Mbps EDR
107   uint16_t peer_mtu;         // MTU of the A2DP peer
108   uint32_t timestamp;        // Timestamp for the A2DP frames
109 
110   HANDLE_LDAC_BT ldac_handle;
111   bool has_ldac_handle;  // True if ldac_handle is valid
112   unsigned char* decode_buf;
113   decoded_data_callback_t decode_callback;
114 } tA2DP_LDAC_DECODER_CB;
115 
116 static tA2DP_LDAC_DECODER_CB a2dp_ldac_decoder_cb;
117 
load_func(const char * func_name)118 static void* load_func(const char* func_name) {
119   void* func_ptr = dlsym(ldac_decoder_lib_handle, func_name);
120   if (func_ptr == NULL) {
121     LOG_ERROR(LOG_TAG,
122               "%s: cannot find function '%s' in the decoder library: %s",
123               __func__, func_name, dlerror());
124     A2DP_VendorUnloadDecoderLdac();
125     return NULL;
126   }
127   return func_ptr;
128 }
129 
A2DP_VendorLoadDecoderLdac(void)130 bool A2DP_VendorLoadDecoderLdac(void) {
131   if (ldac_decoder_lib_handle != NULL) return true;  // Already loaded
132 
133   // Initialize the control block
134   memset(&a2dp_ldac_decoder_cb, 0, sizeof(a2dp_ldac_decoder_cb));
135 
136   // Open the decoder library
137   ldac_decoder_lib_handle = dlopen(LDAC_DECODER_LIB_NAME, RTLD_NOW);
138   if (ldac_decoder_lib_handle == NULL) {
139     LOG_ERROR(LOG_TAG, "%s: cannot open LDAC decoder library %s: %s", __func__,
140               LDAC_DECODER_LIB_NAME, dlerror());
141     return false;
142   }
143 
144   // Load all functions
145   ldac_get_handle_func = (tLDAC_GET_HANDLE)load_func(LDAC_GET_HANDLE_NAME);
146   if (ldac_get_handle_func == NULL) return false;
147   ldac_free_handle_func = (tLDAC_FREE_HANDLE)load_func(LDAC_FREE_HANDLE_NAME);
148   if (ldac_free_handle_func == NULL) return false;
149   ldac_close_handle_func =
150       (tLDAC_CLOSE_HANDLE)load_func(LDAC_CLOSE_HANDLE_NAME);
151   if (ldac_close_handle_func == NULL) return false;
152   ldac_get_version_func = (tLDAC_GET_VERSION)load_func(LDAC_GET_VERSION_NAME);
153   if (ldac_get_version_func == NULL) return false;
154   ldac_get_bitrate_func = (tLDAC_GET_BITRATE)load_func(LDAC_GET_BITRATE_NAME);
155   if (ldac_get_bitrate_func == NULL) return false;
156   ldac_get_sampling_freq_func =
157       (tLDAC_GET_SAMPLING_FREQ)load_func(LDAC_GET_SAMPLING_FREQ_NAME);
158   if (ldac_get_sampling_freq_func == NULL) return false;
159   ldac_init_handle_decode_func =
160       (tLDAC_INIT_HANDLE_DECODE)load_func(LDAC_INIT_HANDLE_DECODE_NAME);
161   if (ldac_init_handle_decode_func == NULL) return false;
162   ldac_decode_func = (tLDAC_DECODE)load_func(LDAC_DECODE_NAME);
163   if (ldac_decode_func == NULL) return false;
164   ldac_get_error_code_func =
165       (tLDAC_GET_ERROR_CODE)load_func(LDAC_GET_ERROR_CODE_NAME);
166   if (ldac_get_error_code_func == NULL) return false;
167 
168   return true;
169 }
170 
A2DP_VendorUnloadDecoderLdac(void)171 void A2DP_VendorUnloadDecoderLdac(void) {
172   // Cleanup any LDAC-related state
173   if (a2dp_ldac_decoder_cb.has_ldac_handle && ldac_free_handle_func != NULL)
174     ldac_free_handle_func(a2dp_ldac_decoder_cb.ldac_handle);
175   memset(&a2dp_ldac_decoder_cb, 0, sizeof(a2dp_ldac_decoder_cb));
176 
177   ldac_get_handle_func = NULL;
178   ldac_free_handle_func = NULL;
179   ldac_close_handle_func = NULL;
180   ldac_get_version_func = NULL;
181   ldac_get_bitrate_func = NULL;
182   ldac_get_sampling_freq_func = NULL;
183   ldac_init_handle_decode_func = NULL;
184   ldac_decode_func = NULL;
185   ldac_get_error_code_func = NULL;
186 
187   if (ldac_decoder_lib_handle != NULL) {
188     dlclose(ldac_decoder_lib_handle);
189     ldac_decoder_lib_handle = NULL;
190   }
191 }
192 
a2dp_vendor_ldac_decoder_init(decoded_data_callback_t decode_callback)193 bool a2dp_vendor_ldac_decoder_init(decoded_data_callback_t decode_callback) {
194   if (a2dp_ldac_decoder_cb.has_ldac_handle)
195     ldac_free_handle_func(a2dp_ldac_decoder_cb.ldac_handle);
196   memset(&a2dp_ldac_decoder_cb, 0, sizeof(a2dp_ldac_decoder_cb));
197 
198   a2dp_vendor_ldac_decoder_cleanup();
199 
200   a2dp_ldac_decoder_cb.ldac_handle = ldac_get_handle_func();
201   a2dp_ldac_decoder_cb.has_ldac_handle = true;
202   a2dp_ldac_decoder_cb.decode_buf = static_cast<unsigned char*>(
203       osi_malloc(sizeof(a2dp_ldac_decoder_cb.decode_buf[0]) * LDACBT_MAX_LSU *
204                  LDAC_PRCNCH * sizeof(int)));
205   a2dp_ldac_decoder_cb.decode_callback = decode_callback;
206 
207   // initialize
208   ldac_init_handle_decode_func(a2dp_ldac_decoder_cb.ldac_handle,
209                                LDACBT_CHANNEL_MODE_STEREO, 96000, 0, 0, 0);
210   return true;
211 }
212 
a2dp_vendor_ldac_decoder_cleanup(void)213 void a2dp_vendor_ldac_decoder_cleanup(void) {
214   if (a2dp_ldac_decoder_cb.has_ldac_handle)
215     ldac_free_handle_func(a2dp_ldac_decoder_cb.ldac_handle);
216   memset(&a2dp_ldac_decoder_cb, 0, sizeof(a2dp_ldac_decoder_cb));
217 }
218 
a2dp_vendor_ldac_decoder_decode_packet(BT_HDR * p_buf)219 bool a2dp_vendor_ldac_decoder_decode_packet(BT_HDR* p_buf) {
220   if (p_buf == nullptr) {
221     LOG_ERROR(LOG_TAG, "%s Dropping packet with nullptr", __func__);
222     return false;
223   }
224   unsigned char* pBuffer =
225       reinterpret_cast<unsigned char*>(p_buf->data + p_buf->offset);
226   //  unsigned int bufferSize = p_buf->len;
227   unsigned int bytesValid = p_buf->len;
228   int err;
229   if (bytesValid == 0) {
230     LOG_WARN(LOG_TAG, "%s Dropping packet with zero length", __func__);
231     return false;
232   }
233 
234   LDACBT_SMPL_FMT_T fmt;
235   int bs_bytes, used_bytes, wrote_bytes, frame_number;
236 
237   fmt = LDACBT_SMPL_FMT_S32;
238   frame_number = (int)pBuffer[0];
239   pBuffer++;
240   bs_bytes = (int)bytesValid - 1;
241   bytesValid -= 1;
242   LOG_VERBOSE(LOG_TAG, "%s:INPUT size : %d, frame : %d", __func__, bs_bytes,
243               frame_number);
244 
245   while (bytesValid > 0) {
246 #if 0
247     err = ldacDecoder_Fill(a2dp_ldac_decoder_cb.ldac_handle,
248                                &pBuffer, &bufferSize, &bytesValid);
249     if (err != LDACBT_ERR_NONE) {
250       LOG_ERROR(LOG_TAG, "%s: ldacDecoder_Fill failed: 0x%x", __func__,
251                 static_cast<unsigned>(err));
252       return false;
253     }
254 #endif
255     while (true) {
256       // Todo : implement LDAC Buffer Control Operation instead of
257       // ldac_decode_func().
258       err = ldac_decode_func(a2dp_ldac_decoder_cb.ldac_handle, pBuffer,
259                              a2dp_ldac_decoder_cb.decode_buf, fmt, bs_bytes,
260                              &used_bytes, &wrote_bytes);
261       //      if (err == LDAC_DEC_NOT_ENOUGH_FRAMES) {
262       //        break;
263       //      }
264       if (LDACBT_ERROR(err)) {
265         err = ldac_get_error_code_func(a2dp_ldac_decoder_cb.ldac_handle);
266         LOG_ERROR(LOG_TAG, "%s: ldacDecoder_DecodeFrame failed: %d:%d:%d",
267                   __func__, LDACBT_API_ERR(err), LDACBT_HANDLE_ERR(err),
268                   LDACBT_BLOCK_ERR(err));
269         if (LDACBT_FATAL(err)) {
270           break;
271         }
272       }
273 
274       if (wrote_bytes > 0) {
275         size_t frame_len = (size_t)wrote_bytes;
276         a2dp_ldac_decoder_cb.decode_callback(
277             reinterpret_cast<uint8_t*>(a2dp_ldac_decoder_cb.decode_buf),
278             frame_len);
279       }
280       pBuffer += used_bytes;
281       bs_bytes -= used_bytes;
282       if (bs_bytes <= 1) {
283         bytesValid = 0;
284         break;
285       }
286     }
287   }
288 
289   return true;
290 }
291