From 765766c4efc6c71b353247a9875f19fce9ba1e1b Mon Sep 17 00:00:00 2001 From: Huang-Huang Bao Date: Tue, 9 Apr 2019 11:13:55 +0800 Subject: [PATCH] Fix #50 --- src/modules/bluetooth/a2dp/ffmpeg_libs.c | 147 ++++++++++-------- src/modules/bluetooth/a2dp/ldac_libs.c | 188 ++++++++++++----------- 2 files changed, 180 insertions(+), 155 deletions(-) diff --git a/src/modules/bluetooth/a2dp/ffmpeg_libs.c b/src/modules/bluetooth/a2dp/ffmpeg_libs.c index 1f2372a9..1c805dc6 100644 --- a/src/modules/bluetooth/a2dp/ffmpeg_libs.c +++ b/src/modules/bluetooth/a2dp/ffmpeg_libs.c @@ -34,7 +34,10 @@ #include "ffmpeg_libs.h" -static const char *AVCODEC_LIB_NAME = "libavcodec.so"; +static const char *AVCODEC_LIB_NAMES[] = { + "libavcodec.so.58", + "libavcodec.so" +}; static const char *avcodec_find_decoder_func_name = "avcodec_find_decoder"; static const char *avcodec_find_encoder_func_name = "avcodec_find_encoder"; @@ -62,7 +65,10 @@ avcodec_alloc_context3_func_t avcodec_alloc_context3_func; avcodec_free_context_func_t avcodec_free_context_func; avcodec_open2_func_t avcodec_open2_func; -static const char *AVUTIL_LIB_NAME = "libavutil.so"; +static const char *AVUTIL_LIB_NAMES[] = { + "libavutil.so.56", + "libavutil.so" +}; static const char *av_frame_alloc_func_name = "av_frame_alloc"; static const char *av_frame_get_buffer_func_name = "av_frame_get_buffer"; @@ -107,53 +113,6 @@ static void libavcodec_unload() { } } -static bool libavcodec_load() { - if (libavcodec_h) - return true; - libavcodec_h = dlopen(AVCODEC_LIB_NAME, RTLD_NOW); - if (libavcodec_h == NULL) { - pa_log_error("Cannot open libavcodec library: %s. %s", AVCODEC_LIB_NAME, dlerror()); - return false; - } - avcodec_find_decoder_func = load_func(libavcodec_h, avcodec_find_decoder_func_name); - if (avcodec_find_decoder_func == NULL) - return false; - avcodec_find_encoder_func = load_func(libavcodec_h, avcodec_find_encoder_func_name); - if (avcodec_find_encoder_func == NULL) - return false; - av_packet_alloc_func = load_func(libavcodec_h, av_packet_alloc_func_name); - if (av_packet_alloc_func == NULL) - return false; - av_packet_free_func = load_func(libavcodec_h, av_packet_free_func_name); - if (av_packet_free_func == NULL) - return false; - avcodec_send_packet_func = load_func(libavcodec_h, avcodec_send_packet_func_name); - if (avcodec_send_packet_func == NULL) - return false; - avcodec_receive_frame_func = load_func(libavcodec_h, avcodec_receive_frame_func_name); - if (avcodec_receive_frame_func == NULL) - return false; - avcodec_send_frame_func = load_func(libavcodec_h, avcodec_send_frame_func_name); - if (avcodec_send_frame_func == NULL) - return false; - avcodec_receive_packet_func = load_func(libavcodec_h, avcodec_receive_packet_func_name); - if (avcodec_receive_packet_func == NULL) - return false; - avcodec_flush_buffers_func = load_func(libavcodec_h, avcodec_flush_buffers_func_name); - if (avcodec_flush_buffers_func == NULL) - return false; - avcodec_alloc_context3_func = load_func(libavcodec_h, avcodec_alloc_context3_func_name); - if (avcodec_alloc_context3_func == NULL) - return false; - avcodec_free_context_func = load_func(libavcodec_h, avcodec_free_context_func_name); - if (avcodec_free_context_func == NULL) - return false; - avcodec_open2_func = load_func(libavcodec_h, avcodec_open2_func_name); - if (avcodec_open2_func == NULL) - return false; - return true; -} - static void libavutil_unload() { av_frame_alloc_func = NULL; av_frame_get_buffer_func = NULL; @@ -165,28 +124,82 @@ static void libavutil_unload() { } } +static bool libavcodec_load() { + if (libavcodec_h) + return true; + for (int i = 0; i < PA_ELEMENTSOF(AVCODEC_LIB_NAMES); ++i) { + libavutil_unload(); + libavcodec_h = dlopen(AVCODEC_LIB_NAMES[i], RTLD_NOW); + if (libavcodec_h == NULL) { + pa_log_warn("Cannot open libavcodec library: %s. %s", AVCODEC_LIB_NAMES[i], dlerror()); + continue; + } + avcodec_find_decoder_func = load_func(libavcodec_h, avcodec_find_decoder_func_name); + if (avcodec_find_decoder_func == NULL) + continue; + avcodec_find_encoder_func = load_func(libavcodec_h, avcodec_find_encoder_func_name); + if (avcodec_find_encoder_func == NULL) + continue; + av_packet_alloc_func = load_func(libavcodec_h, av_packet_alloc_func_name); + if (av_packet_alloc_func == NULL) + continue; + av_packet_free_func = load_func(libavcodec_h, av_packet_free_func_name); + if (av_packet_free_func == NULL) + continue; + avcodec_send_packet_func = load_func(libavcodec_h, avcodec_send_packet_func_name); + if (avcodec_send_packet_func == NULL) + continue; + avcodec_receive_frame_func = load_func(libavcodec_h, avcodec_receive_frame_func_name); + if (avcodec_receive_frame_func == NULL) + continue; + avcodec_send_frame_func = load_func(libavcodec_h, avcodec_send_frame_func_name); + if (avcodec_send_frame_func == NULL) + continue; + avcodec_receive_packet_func = load_func(libavcodec_h, avcodec_receive_packet_func_name); + if (avcodec_receive_packet_func == NULL) + continue; + avcodec_flush_buffers_func = load_func(libavcodec_h, avcodec_flush_buffers_func_name); + if (avcodec_flush_buffers_func == NULL) + continue; + avcodec_alloc_context3_func = load_func(libavcodec_h, avcodec_alloc_context3_func_name); + if (avcodec_alloc_context3_func == NULL) + continue; + avcodec_free_context_func = load_func(libavcodec_h, avcodec_free_context_func_name); + if (avcodec_free_context_func == NULL) + continue; + avcodec_open2_func = load_func(libavcodec_h, avcodec_open2_func_name); + if (avcodec_open2_func == NULL) + continue; + return true; + } + return false; +} + static bool libavutil_load() { if (libavutil_h) return true; - libavutil_h = dlopen(AVUTIL_LIB_NAME, RTLD_NOW); - if (libavutil_h == NULL) { - pa_log_error("Cannot open libavutil library: %s. %s", AVUTIL_LIB_NAME, dlerror()); - return false; + for (int i = 0; i < PA_ELEMENTSOF(AVUTIL_LIB_NAMES); ++i) { + libavutil_h = dlopen(AVUTIL_LIB_NAMES[i], RTLD_NOW); + if (libavutil_h == NULL) { + pa_log_warn("Cannot open libavutil library: %s. %s", AVUTIL_LIB_NAMES[i], dlerror()); + continue; + } + av_frame_alloc_func = load_func(libavutil_h, av_frame_alloc_func_name); + if (av_frame_alloc_func == NULL) + continue; + av_frame_get_buffer_func = load_func(libavutil_h, av_frame_get_buffer_func_name); + if (av_frame_get_buffer_func == NULL) + continue; + av_frame_make_writable_func = load_func(libavutil_h, av_frame_make_writable_func_name); + if (av_frame_make_writable_func == NULL) + continue; + av_frame_free_func = load_func(libavutil_h, av_frame_free_func_name); + if (av_frame_free_func == NULL) + continue; + return true; } - av_frame_alloc_func = load_func(libavutil_h, av_frame_alloc_func_name); - if (av_frame_alloc_func == NULL) - return false; - av_frame_get_buffer_func = load_func(libavutil_h, av_frame_get_buffer_func_name); - if (av_frame_get_buffer_func == NULL) - return false; - av_frame_make_writable_func = load_func(libavutil_h, av_frame_make_writable_func_name); - if (av_frame_make_writable_func == NULL) - return false; - av_frame_free_func = load_func(libavutil_h, av_frame_free_func_name); - if (av_frame_free_func == NULL) - return false; - - return true; + + return false; } bool ffmpeg_libs_load() { diff --git a/src/modules/bluetooth/a2dp/ldac_libs.c b/src/modules/bluetooth/a2dp/ldac_libs.c index e3bdcf2f..490e9266 100644 --- a/src/modules/bluetooth/a2dp/ldac_libs.c +++ b/src/modules/bluetooth/a2dp/ldac_libs.c @@ -31,7 +31,10 @@ #include "ldac_libs.h" -static const char *LDAC_ENCODER_LIB_NAME = "libldacBT_enc.so"; +static const char *LDAC_ENCODER_LIB_NAMES[] = { + "libldacBT_enc.so.2", + "libldacBT_enc.so" +}; static const char *LDAC_GET_HANDLE_FUNC_NAME = "ldacBT_get_handle"; static const char *LDAC_FREE_HANDLE_FUNC_NAME = "ldacBT_free_handle"; @@ -47,7 +50,10 @@ static const char *LDAC_ENCODE_FUNC_NAME = "ldacBT_encode"; static const char *LDAC_GET_ERROR_CODE_FUNC_NAME = "ldacBT_get_error_code"; -static const char *LDAC_ABR_LIB_NAME = "libldacBT_abr.so"; +static const char *LDAC_ABR_LIB_NAMES[] = { + "libldacBT_abr.so.2", + "libldacBT_abr.so" +}; static const char *LDAC_ABR_GET_HANDLE_FUNC_NAME = "ldac_ABR_get_handle"; static const char *LDAC_ABR_FREE_HANDLE_FUNC_NAME = "ldac_ABR_free_handle"; @@ -91,36 +97,6 @@ static void *load_func(void *lib_handle, const char *func_name) { return func; } -static bool ldac_abr_load() { - if (ldac_abr_lib_h) - return true; - ldac_abr_lib_h = dlopen(LDAC_ABR_LIB_NAME, RTLD_NOW); - if (ldac_abr_lib_h == NULL) { - pa_log_error("Cannot open LDAC abr library: %s. %s", LDAC_ABR_LIB_NAME, dlerror()); - return false; - } - - ldac_ABR_get_handle_func = (ldac_ABR_get_handle_func_t) load_func(ldac_abr_lib_h, - LDAC_ABR_GET_HANDLE_FUNC_NAME); - if (ldac_ABR_get_handle_func == NULL) - return false; - ldac_ABR_free_handle_func = (ldac_ABR_free_handle_func_t) load_func(ldac_abr_lib_h, - LDAC_ABR_FREE_HANDLE_FUNC_NAME); - if (ldac_ABR_free_handle_func == NULL) - return false; - ldac_ABR_Init_func = (ldac_ABR_Init_func_t) load_func(ldac_abr_lib_h, LDAC_ABR_INIT_FUNC_NAME); - if (ldac_ABR_Init_func == NULL) - return false; - ldac_ABR_set_thresholds_func = (ldac_ABR_set_thresholds_func_t) load_func(ldac_abr_lib_h, - LDAC_ABR_SET_THRESHOLDS_FUNC_NAME); - if (ldac_ABR_set_thresholds_func == NULL) - return false; - ldac_ABR_Proc_func = (ldac_ABR_Proc_func_t) load_func(ldac_abr_lib_h, LDAC_ABR_PROC_FUNC_NAME); - if (ldac_ABR_Proc_func == NULL) - return false; - return true; -} - static void ldac_abr_unload() { if (ldac_abr_lib_h != NULL) { dlclose(ldac_abr_lib_h); @@ -133,66 +109,39 @@ static void ldac_abr_unload() { ldac_ABR_Proc_func = NULL; } -static bool _ldac_encoder_load() { - if (ldac_encoder_lib_h) +static bool ldac_abr_load() { + if (ldac_abr_lib_h) return true; - ldac_encoder_lib_h = dlopen(LDAC_ENCODER_LIB_NAME, RTLD_NOW); - if (ldac_encoder_lib_h == NULL) { - pa_log_error("Cannot open LDAC encoder library: %s. %s", LDAC_ENCODER_LIB_NAME, dlerror()); - return false; - } - - ldacBT_get_handle_func = (ldacBT_get_handle_func_t) load_func(ldac_encoder_lib_h, LDAC_GET_HANDLE_FUNC_NAME); - if (ldacBT_get_handle_func == NULL) - return false; - ldacBT_free_handle_func = (ldacBT_free_handle_func_t) load_func(ldac_encoder_lib_h, LDAC_FREE_HANDLE_FUNC_NAME); - if (ldacBT_free_handle_func == NULL) - return false; - ldacBT_close_handle_func = (ldacBT_close_handle_func_t) load_func(ldac_encoder_lib_h, LDAC_CLOSE_HANDLE_FUNC_NAME); - if (ldacBT_close_handle_func == NULL) - return false; - ldacBT_get_version_func = (ldacBT_get_version_func_t) load_func(ldac_encoder_lib_h, LDAC_GET_VERSION_FUNC_NAME); - if (ldacBT_get_version_func == NULL) - return false; - ldacBT_get_sampling_freq_func = (ldacBT_get_sampling_freq_func_t) load_func(ldac_encoder_lib_h, - LDAC_GET_SAMPLING_FREQ_FUNC_NAME); - if (ldacBT_get_sampling_freq_func == NULL) - return false; - ldacBT_get_bitrate_func = (ldacBT_get_bitrate_func_t) load_func(ldac_encoder_lib_h, LDAC_GET_BITRATE_FUNC_NAME); - if (ldacBT_get_bitrate_func == NULL) - return false; - ldacBT_init_handle_encode_func = (ldacBT_init_handle_encode_func_t) load_func(ldac_encoder_lib_h, - LDAC_INIT_HANDLE_ENCODE_FUNC_NAME); - if (ldacBT_init_handle_encode_func == NULL) - return false; - ldacBT_set_eqmid_func = (ldacBT_set_eqmid_func_t) load_func(ldac_encoder_lib_h, LDAC_SET_EQMID_FUNC_NAME); - if (ldacBT_set_eqmid_func == NULL) - return false; - ldacBT_get_eqmid_func = (ldacBT_get_eqmid_func_t) load_func(ldac_encoder_lib_h, LDAC_GET_EQMID_FUNC_NAME); - if (ldacBT_get_eqmid_func == NULL) - return false; - ldacBT_alter_eqmid_priority_func = (ldacBT_alter_eqmid_priority_func_t) load_func(ldac_encoder_lib_h, - LDAC_ALTER_EQMID_PRIORITY_FUNC_NAME); - if (ldacBT_alter_eqmid_priority_func == NULL) - return false; - ldacBT_encode_func = (ldacBT_encode_func_t) load_func(ldac_encoder_lib_h, LDAC_ENCODE_FUNC_NAME); - if (ldacBT_encode_func == NULL) - return false; - ldacBT_get_error_code_func = (ldacBT_get_error_code_func_t) load_func(ldac_encoder_lib_h, - LDAC_GET_ERROR_CODE_FUNC_NAME); - if (ldacBT_get_error_code_func == NULL) - return false; - - if (!ldac_abr_load()) { - pa_log_debug("Cannot load the LDAC ABR library"); + for (int i = 0; i < PA_ELEMENTSOF(LDAC_ABR_LIB_NAMES); ++i) { ldac_abr_unload(); - ldac_abr_loaded = false; - } else - ldac_abr_loaded = true; - return true; + ldac_abr_lib_h = dlopen(LDAC_ABR_LIB_NAMES[i], RTLD_NOW); + if (ldac_abr_lib_h == NULL) { + pa_log_warn("Cannot open LDAC abr library: %s. %s", LDAC_ABR_LIB_NAMES[i], dlerror()); + continue; + } + ldac_ABR_get_handle_func = (ldac_ABR_get_handle_func_t) load_func(ldac_abr_lib_h, + LDAC_ABR_GET_HANDLE_FUNC_NAME); + if (ldac_ABR_get_handle_func == NULL) + continue; + ldac_ABR_free_handle_func = (ldac_ABR_free_handle_func_t) load_func(ldac_abr_lib_h, + LDAC_ABR_FREE_HANDLE_FUNC_NAME); + if (ldac_ABR_free_handle_func == NULL) + continue; + ldac_ABR_Init_func = (ldac_ABR_Init_func_t) load_func(ldac_abr_lib_h, LDAC_ABR_INIT_FUNC_NAME); + if (ldac_ABR_Init_func == NULL) + continue; + ldac_ABR_set_thresholds_func = (ldac_ABR_set_thresholds_func_t) load_func(ldac_abr_lib_h, + LDAC_ABR_SET_THRESHOLDS_FUNC_NAME); + if (ldac_ABR_set_thresholds_func == NULL) + continue; + ldac_ABR_Proc_func = (ldac_ABR_Proc_func_t) load_func(ldac_abr_lib_h, LDAC_ABR_PROC_FUNC_NAME); + if (ldac_ABR_Proc_func == NULL) + continue; + return true; + } + return false; } - static void ldac_encoder_unload() { if (ldac_encoder_lib_h != NULL) { dlclose(ldac_encoder_lib_h); @@ -217,6 +166,69 @@ static void ldac_encoder_unload() { ldac_ABR_Proc_func = NULL; } +static bool _ldac_encoder_load() { + if (ldac_encoder_lib_h) + return true; + for (int i = 0; i < PA_ELEMENTSOF(LDAC_ENCODER_LIB_NAMES); ++i) { + ldac_encoder_unload(); + ldac_encoder_lib_h = dlopen(LDAC_ENCODER_LIB_NAMES[i], RTLD_NOW); + if (ldac_encoder_lib_h == NULL) { + pa_log_warn("Cannot open LDAC encoder library: %s. %s", LDAC_ENCODER_LIB_NAMES[i], dlerror()); + continue; + } + ldacBT_get_handle_func = (ldacBT_get_handle_func_t) load_func(ldac_encoder_lib_h, LDAC_GET_HANDLE_FUNC_NAME); + if (ldacBT_get_handle_func == NULL) + continue; + ldacBT_free_handle_func = (ldacBT_free_handle_func_t) load_func(ldac_encoder_lib_h, LDAC_FREE_HANDLE_FUNC_NAME); + if (ldacBT_free_handle_func == NULL) + continue; + ldacBT_close_handle_func = (ldacBT_close_handle_func_t) load_func(ldac_encoder_lib_h, + LDAC_CLOSE_HANDLE_FUNC_NAME); + if (ldacBT_close_handle_func == NULL) + continue; + ldacBT_get_version_func = (ldacBT_get_version_func_t) load_func(ldac_encoder_lib_h, LDAC_GET_VERSION_FUNC_NAME); + if (ldacBT_get_version_func == NULL) + continue; + ldacBT_get_sampling_freq_func = (ldacBT_get_sampling_freq_func_t) load_func(ldac_encoder_lib_h, + LDAC_GET_SAMPLING_FREQ_FUNC_NAME); + if (ldacBT_get_sampling_freq_func == NULL) + continue; + ldacBT_get_bitrate_func = (ldacBT_get_bitrate_func_t) load_func(ldac_encoder_lib_h, LDAC_GET_BITRATE_FUNC_NAME); + if (ldacBT_get_bitrate_func == NULL) + continue; + ldacBT_init_handle_encode_func = (ldacBT_init_handle_encode_func_t) load_func(ldac_encoder_lib_h, + LDAC_INIT_HANDLE_ENCODE_FUNC_NAME); + if (ldacBT_init_handle_encode_func == NULL) + continue; + ldacBT_set_eqmid_func = (ldacBT_set_eqmid_func_t) load_func(ldac_encoder_lib_h, LDAC_SET_EQMID_FUNC_NAME); + if (ldacBT_set_eqmid_func == NULL) + continue; + ldacBT_get_eqmid_func = (ldacBT_get_eqmid_func_t) load_func(ldac_encoder_lib_h, LDAC_GET_EQMID_FUNC_NAME); + if (ldacBT_get_eqmid_func == NULL) + continue; + ldacBT_alter_eqmid_priority_func = (ldacBT_alter_eqmid_priority_func_t) load_func(ldac_encoder_lib_h, + LDAC_ALTER_EQMID_PRIORITY_FUNC_NAME); + if (ldacBT_alter_eqmid_priority_func == NULL) + continue; + ldacBT_encode_func = (ldacBT_encode_func_t) load_func(ldac_encoder_lib_h, LDAC_ENCODE_FUNC_NAME); + if (ldacBT_encode_func == NULL) + continue; + ldacBT_get_error_code_func = (ldacBT_get_error_code_func_t) load_func(ldac_encoder_lib_h, + LDAC_GET_ERROR_CODE_FUNC_NAME); + if (ldacBT_get_error_code_func == NULL) + continue; + if (!ldac_abr_load()) { + pa_log_debug("Cannot load the LDAC ABR library"); + ldac_abr_unload(); + ldac_abr_loaded = false; + } else + ldac_abr_loaded = true; + return true; + } + return false; +} + + bool ldac_encoder_load() { if (!_ldac_encoder_load()) { pa_log_debug("Cannot load the LDAC encoder library");