diff --git a/src/a2dp.c b/src/a2dp.c index f3dbb7be5..34c5bf103 100644 --- a/src/a2dp.c +++ b/src/a2dp.c @@ -43,7 +43,6 @@ #endif #include "a2dp-sbc.h" #include "ba-config.h" -#include "ba-transport.h" #include "shared/a2dp-codecs.h" #include "shared/log.h" @@ -390,13 +389,3 @@ const char *a2dp_check_strerror( debug("Unknown error code: %#x", err); return "Check error"; } - -int a2dp_transport_init( - struct ba_transport *t) { - return t->a2dp.sep->transport_init(t); -} - -int a2dp_transport_start( - struct ba_transport *t) { - return t->a2dp.sep->transport_start(t); -} diff --git a/src/a2dp.h b/src/a2dp.h index d451310f8..0018133ed 100644 --- a/src/a2dp.h +++ b/src/a2dp.h @@ -240,9 +240,4 @@ enum a2dp_check_err a2dp_check_configuration( const char *a2dp_check_strerror( enum a2dp_check_err err); -int a2dp_transport_init( - struct ba_transport *t); -int a2dp_transport_start( - struct ba_transport *t); - #endif diff --git a/src/ba-transport.c b/src/ba-transport.c index 1c5a628eb..f0ace4b63 100644 --- a/src/ba-transport.c +++ b/src/ba-transport.c @@ -421,6 +421,9 @@ struct ba_transport *ba_transport_new_a2dp( t->a2dp.sep = sep; memcpy(&t->a2dp.configuration, configuration, sep->config.caps_size); + t->acquire = transport_acquire_bt_a2dp; + t->release = transport_release_bt_a2dp; + err |= transport_pcm_init(&t->a2dp.pcm, is_sink ? BA_TRANSPORT_PCM_MODE_SOURCE : BA_TRANSPORT_PCM_MODE_SINK, t, true); @@ -432,17 +435,18 @@ struct ba_transport *ba_transport_new_a2dp( if (err != 0) goto fail; + /* do codec-specific initialization */ + if (sep->transport_init(t) != 0) { + errno = EINVAL; + goto fail; + } + if ((errno = pthread_create(&t->thread_manager_thread_id, NULL, PTHREAD_FUNC(transport_thread_manager), t)) != 0) { t->thread_manager_thread_id = config.main_thread; goto fail; } - t->acquire = transport_acquire_bt_a2dp; - t->release = transport_release_bt_a2dp; - - a2dp_transport_init(t); - storage_pcm_data_sync(&t->a2dp.pcm); storage_pcm_data_sync(&t->a2dp.pcm_bc); @@ -552,26 +556,6 @@ struct ba_transport *ba_transport_new_sco( * there is no other option than the CVSD codec. */ t->codec_id = HFP_CODEC_CVSD; - err |= transport_pcm_init(&t->sco.pcm_spk, - is_ag ? BA_TRANSPORT_PCM_MODE_SINK : BA_TRANSPORT_PCM_MODE_SOURCE, - t, true); - - err |= transport_pcm_init(&t->sco.pcm_mic, - is_ag ? BA_TRANSPORT_PCM_MODE_SOURCE : BA_TRANSPORT_PCM_MODE_SINK, - t, false); - - if (err != 0) - goto fail; - - if ((errno = pthread_create(&t->thread_manager_thread_id, - NULL, PTHREAD_FUNC(transport_thread_manager), t)) != 0) { - t->thread_manager_thread_id = config.main_thread; - goto fail; - } - - t->acquire = transport_acquire_bt_sco; - t->release = transport_release_bt_sco; - #if ENABLE_HFP_CODEC_SELECTION /* Only HFP supports codec selection. */ if (profile & BA_TRANSPORT_PROFILE_MASK_HFP && @@ -589,7 +573,30 @@ struct ba_transport *ba_transport_new_sco( } #endif - sco_transport_init(t); + t->acquire = transport_acquire_bt_sco; + t->release = transport_release_bt_sco; + + err |= transport_pcm_init(&t->sco.pcm_spk, + is_ag ? BA_TRANSPORT_PCM_MODE_SINK : BA_TRANSPORT_PCM_MODE_SOURCE, + t, true); + + err |= transport_pcm_init(&t->sco.pcm_mic, + is_ag ? BA_TRANSPORT_PCM_MODE_SOURCE : BA_TRANSPORT_PCM_MODE_SINK, + t, false); + + if (err != 0) + goto fail; + + if (sco_transport_init(t) != 0) { + errno = EINVAL; + goto fail; + } + + if ((errno = pthread_create(&t->thread_manager_thread_id, + NULL, PTHREAD_FUNC(transport_thread_manager), t)) != 0) { + t->thread_manager_thread_id = config.main_thread; + goto fail; + } storage_pcm_data_sync(&t->sco.pcm_spk); storage_pcm_data_sync(&t->sco.pcm_mic); @@ -1026,7 +1033,7 @@ void ba_transport_set_codec( return; if (t->profile & BA_TRANSPORT_PROFILE_MASK_A2DP) - a2dp_transport_init(t); + t->a2dp.sep->transport_init(t); else if (t->profile & BA_TRANSPORT_PROFILE_MASK_SCO) sco_transport_init(t); @@ -1077,7 +1084,7 @@ int ba_transport_start(struct ba_transport *t) { debug("Starting transport: %s", ba_transport_debug_name(t)); if (t->profile & BA_TRANSPORT_PROFILE_MASK_A2DP) - return a2dp_transport_start(t); + return t->a2dp.sep->transport_start(t); if (t->profile & BA_TRANSPORT_PROFILE_MASK_SCO) return sco_transport_start(t); diff --git a/src/sco.c b/src/sco.c index a0421aa45..e839cbfb7 100644 --- a/src/sco.c +++ b/src/sco.c @@ -256,7 +256,7 @@ void *sco_dec_thread(struct ba_transport_pcm *pcm) { } } -void sco_transport_init(struct ba_transport *t) { +int sco_transport_init(struct ba_transport *t) { t->sco.pcm_spk.format = BA_TRANSPORT_PCM_FORMAT_S16_2LE; t->sco.pcm_spk.channels = 1; @@ -305,6 +305,7 @@ void sco_transport_init(struct ba_transport *t) { BA_DBUS_PCM_UPDATE_CODEC | BA_DBUS_PCM_UPDATE_CLIENT_DELAY); + return 0; } int sco_transport_start(struct ba_transport *t) { diff --git a/src/sco.h b/src/sco.h index a32db9391..c9cc23c8a 100644 --- a/src/sco.h +++ b/src/sco.h @@ -1,6 +1,6 @@ /* * BlueALSA - sco.h - * Copyright (c) 2016-2023 Arkadiusz Bokowy + * Copyright (c) 2016-2024 Arkadiusz Bokowy * * This file is a part of bluez-alsa. * @@ -20,7 +20,7 @@ #include "ba-transport.h" int sco_setup_connection_dispatcher(struct ba_adapter *a); -void sco_transport_init(struct ba_transport *t); +int sco_transport_init(struct ba_transport *t); int sco_transport_start(struct ba_transport *t); #endif diff --git a/test/test-ba.c b/test/test-ba.c index f629a9c60..c456564e2 100644 --- a/test/test-ba.c +++ b/test/test-ba.c @@ -49,8 +49,6 @@ /* Keep persistent storage in the current directory. */ #define TEST_BLUEALSA_STORAGE_DIR "storage-test-ba" -int a2dp_transport_init(struct ba_transport *t) { (void)t; return 0; } -int a2dp_transport_start(struct ba_transport *t) { (void)t; return 0; } int midi_transport_alsa_seq_create(struct ba_transport *t) { (void)t; return 0; } int midi_transport_alsa_seq_delete(struct ba_transport *t) { (void)t; return 0; } int midi_transport_start(struct ba_transport *t) { (void)t; return 0; } @@ -313,6 +311,11 @@ CK_START_TEST(test_ba_transport_pcm_format) { } CK_END_TEST +static int sep_transport_init(struct ba_transport *t) { + (void)t; + return 0; +} + CK_START_TEST(test_ba_transport_pcm_volume) { struct ba_adapter *a; @@ -325,7 +328,9 @@ CK_START_TEST(test_ba_transport_pcm_volume) { ck_assert_ptr_ne(d = ba_device_new(a, &addr), NULL); ck_assert_int_eq(storage_device_clear(d), 0); - struct a2dp_sep sep = { .config = { .type = A2DP_SINK, .codec_id = A2DP_CODEC_SBC } }; + struct a2dp_sep sep = { + .config = { .type = A2DP_SINK, .codec_id = A2DP_CODEC_SBC }, + .transport_init = sep_transport_init }; a2dp_sbc_t configuration = { .channel_mode = SBC_CHANNEL_MODE_STEREO }; ck_assert_ptr_ne(t_a2dp = ba_transport_new_a2dp(d, BA_TRANSPORT_PROFILE_A2DP_SINK, "/owner", "/path/a2dp", &sep, @@ -410,7 +415,9 @@ CK_START_TEST(test_storage) { ck_assert_ptr_ne(a = ba_adapter_new(0), NULL); ck_assert_ptr_ne(d = ba_device_new(a, &addr), NULL); - struct a2dp_sep sep = { .config = { .type = A2DP_SINK, .codec_id = A2DP_CODEC_SBC } }; + struct a2dp_sep sep = { + .config = { .type = A2DP_SINK, .codec_id = A2DP_CODEC_SBC }, + .transport_init = sep_transport_init }; a2dp_sbc_t configuration = { .channel_mode = SBC_CHANNEL_MODE_STEREO }; ck_assert_ptr_ne(t = ba_transport_new_a2dp(d, BA_TRANSPORT_PROFILE_A2DP_SINK, "/owner", "/path", &sep, diff --git a/test/test-io.c b/test/test-io.c index 158b6738b..2eea60f9b 100644 --- a/test/test-io.c +++ b/test/test-io.c @@ -726,8 +726,9 @@ static struct ba_transport *test_transport_new_a2dp( if (input_bt_file != NULL) configuration = &btdin->a2dp_configuration; #endif - struct ba_transport *t = ba_transport_new_a2dp(device, profile, ":test", - dbus_path, sep, configuration); + struct ba_transport *t; + ck_assert_ptr_nonnull(t = ba_transport_new_a2dp(device, profile, ":test", + dbus_path, sep, configuration)); t->acquire = test_transport_acquire; t->release = test_transport_release_bt_a2dp; return t; @@ -772,7 +773,9 @@ CK_START_TEST(test_a2dp_sbc) { CK_START_TEST(test_a2dp_sbc_invalid_config) { - const a2dp_sbc_t config_sbc_invalid = { 0 }; + const a2dp_sbc_t config_sbc_invalid = { + .sampling_freq = SBC_SAMPLING_FREQ_44100, + .channel_mode = SBC_CHANNEL_MODE_STEREO }; struct ba_transport *t = test_transport_new_a2dp(device1, BA_TRANSPORT_PROFILE_A2DP_SOURCE, "/path/sbc", &a2dp_sbc_source, &config_sbc_invalid); diff --git a/test/test-rfcomm.c b/test/test-rfcomm.c index 4374dd9fb..8b050d738 100644 --- a/test/test-rfcomm.c +++ b/test/test-rfcomm.c @@ -58,8 +58,6 @@ static void dbus_update_counters_wait(unsigned int *counter, unsigned int value) pthread_mutex_unlock(&dbus_update_mtx); } -int a2dp_transport_init(struct ba_transport *t) { (void)t; return 0; } -int a2dp_transport_start(struct ba_transport *t) { (void)t; return 0; } int midi_transport_alsa_seq_create(struct ba_transport *t) { (void)t; return 0; } int midi_transport_alsa_seq_delete(struct ba_transport *t) { (void)t; return 0; } int midi_transport_start(struct ba_transport *t) { (void)t; return 0; }