It's more convenient that way. --- src/modules/bluetooth/module-bluetooth-device.c | 89 +++++++++++-------------- 1 file changed, 39 insertions(+), 50 deletions(-) diff --git a/src/modules/bluetooth/module-bluetooth-device.c b/src/modules/bluetooth/module-bluetooth-device.c index f133d56..59acaf8 100644 --- a/src/modules/bluetooth/module-bluetooth-device.c +++ b/src/modules/bluetooth/module-bluetooth-device.c @@ -152,8 +152,8 @@ struct userdata { pa_bluetooth_discovery *discovery; bool auto_connect; - char *output_port_name; - char *input_port_name; + pa_device_port *output_port; + pa_device_port *input_port; pa_card *card; pa_device_prototype *sink_prototype; @@ -1251,7 +1251,6 @@ static void handle_transport_state_change(struct userdata *u, struct pa_bluetoot enum profile profile; pa_card_profile *cp; pa_bluetooth_transport_state_t state; - pa_device_port *port; pa_assert(u); pa_assert(transport); @@ -1266,11 +1265,8 @@ static void handle_transport_state_change(struct userdata *u, struct pa_bluetoot pa_card_profile_set_available(cp, transport_state_to_availability(state)); /* Update port availability */ - pa_assert_se(port = pa_hashmap_get(u->card->ports, u->output_port_name)); - pa_device_port_set_available(port, get_port_availability(u, PA_DIRECTION_OUTPUT)); - - pa_assert_se(port = pa_hashmap_get(u->card->ports, u->input_port_name)); - pa_device_port_set_available(port, get_port_availability(u, PA_DIRECTION_INPUT)); + pa_device_port_set_available(u->output_port, get_port_availability(u, PA_DIRECTION_OUTPUT)); + pa_device_port_set_available(u->input_port, get_port_availability(u, PA_DIRECTION_INPUT)); /* Acquire or release transport as needed */ acquire = (state == PA_BLUETOOTH_TRANSPORT_STATE_PLAYING && u->profile == profile); @@ -1508,20 +1504,16 @@ static pa_hook_result_t transport_speaker_gain_changed_cb(pa_bluetooth_discovery } static void connect_ports(struct userdata *u, void *sink_or_source_new_data, pa_direction_t direction) { - pa_device_port *port; - if (direction == PA_DIRECTION_OUTPUT) { pa_sink_new_data *sink_new_data = sink_or_source_new_data; - pa_assert_se(port = pa_hashmap_get(u->card->ports, u->output_port_name)); - pa_assert_se(pa_hashmap_put(sink_new_data->ports, port->name, port) >= 0); - pa_device_port_ref(port); + pa_assert_se(pa_hashmap_put(sink_new_data->ports, u->output_port->name, u->output_port) >= 0); + pa_device_port_ref(u->output_port); } else { pa_source_new_data *source_new_data = sink_or_source_new_data; - pa_assert_se(port = pa_hashmap_get(u->card->ports, u->input_port_name)); - pa_assert_se(pa_hashmap_put(source_new_data->ports, port->name, port) >= 0); - pa_device_port_ref(port); + pa_assert_se(pa_hashmap_put(source_new_data->ports, u->input_port->name, u->input_port) >= 0); + pa_device_port_ref(u->input_port); } } @@ -2071,10 +2063,9 @@ off: /* Run from main thread */ static int create_card_ports(struct userdata *u, pa_hashmap *ports) { - pa_device_port *output_port = NULL; - pa_device_port *input_port = NULL; pa_device_port_new_data port_data; - + char *output_port_name; + char *input_port_name; const char *name_prefix = NULL; const char *input_description = NULL; const char *output_description = NULL; @@ -2142,50 +2133,57 @@ static int create_card_ports(struct userdata *u, pa_hashmap *ports) { if (!input_description) input_description = _("Bluetooth Input"); - u->output_port_name = pa_sprintf_malloc("%s-output", name_prefix); - u->input_port_name = pa_sprintf_malloc("%s-input", name_prefix); + output_port_name = pa_sprintf_malloc("%s-output", name_prefix); + input_port_name = pa_sprintf_malloc("%s-input", name_prefix); pa_device_port_new_data_init(&port_data); - pa_device_port_new_data_set_name(&port_data, u->output_port_name); + pa_device_port_new_data_set_name(&port_data, output_port_name); pa_device_port_new_data_set_description(&port_data, output_description); pa_device_port_new_data_set_direction(&port_data, PA_DIRECTION_OUTPUT); pa_device_port_new_data_set_available(&port_data, get_port_availability(u, PA_DIRECTION_OUTPUT)); pa_node_new_data_set_fallback_name_prefix(&port_data.node_data, "bluetooth"); - output_port = pa_device_port_new(u->core, &port_data, 0); + u->output_port = pa_device_port_new(u->core, &port_data, 0); pa_device_port_new_data_done(&port_data); - if (!output_port) { - pa_log("Failed to create port %s.", u->output_port_name); + if (!u->output_port) { + pa_log("Failed to create port %s.", output_port_name); goto fail; } pa_device_port_new_data_init(&port_data); - pa_device_port_new_data_set_name(&port_data, u->input_port_name); + pa_device_port_new_data_set_name(&port_data, input_port_name); pa_device_port_new_data_set_description(&port_data, input_description); pa_device_port_new_data_set_direction(&port_data, PA_DIRECTION_INPUT); pa_device_port_new_data_set_available(&port_data, get_port_availability(u, PA_DIRECTION_INPUT)); pa_node_new_data_set_fallback_name_prefix(&port_data.node_data, "bluetooth"); - input_port = pa_device_port_new(u->core, &port_data, 0); + u->input_port = pa_device_port_new(u->core, &port_data, 0); pa_device_port_new_data_done(&port_data); - if (!input_port) { - pa_log("Failed to create port %s.", u->input_port_name); + if (!u->input_port) { + pa_log("Failed to create port %s.", input_port_name); goto fail; } - pa_assert_se(pa_hashmap_put(ports, output_port->name, output_port) >= 0); - pa_assert_se(pa_hashmap_put(ports, input_port->name, input_port) >= 0); + pa_assert_se(pa_hashmap_put(ports, u->output_port->name, u->output_port) >= 0); + pa_assert_se(pa_hashmap_put(ports, u->input_port->name, u->input_port) >= 0); return 0; fail: - if (output_port) - pa_device_port_unref(output_port); + if (u->output_port) { + pa_device_port_unref(u->output_port); + u->output_port = NULL; + } + + if (u->input_port) { + pa_device_port_unref(u->input_port); + u->input_port = NULL; + } - if (input_port) - pa_device_port_unref(input_port); + pa_xfree(input_port_name); + pa_xfree(output_port_name); return -1; } @@ -2193,15 +2191,9 @@ fail: /* Run from main thread */ static pa_card_profile *create_card_profile(struct userdata *u, const char *uuid, pa_hashmap *ports) { enum profile profile = PROFILE_OFF; - pa_device_port *input_port, *output_port; pa_card_profile *p = NULL; enum profile *d; - pa_assert(u->input_port_name); - pa_assert(u->output_port_name); - pa_assert_se(input_port = pa_hashmap_get(ports, u->input_port_name)); - pa_assert_se(output_port = pa_hashmap_get(ports, u->output_port_name)); - if (pa_streq(uuid, A2DP_SINK_UUID)) profile = PROFILE_A2DP; else if (pa_streq(uuid, A2DP_SOURCE_UUID)) @@ -2227,7 +2219,7 @@ static pa_card_profile *create_card_profile(struct userdata *u, const char *uuid pa_hashmap_put(p->sink_prototypes, u->sink_prototype, u->sink_prototype); p->max_sink_channels = 2; p->max_source_channels = 0; - pa_hashmap_put(output_port->profiles, p->name, p); + pa_hashmap_put(u->output_port->profiles, p->name, p); d = PA_CARD_PROFILE_DATA(p); *d = PROFILE_A2DP; @@ -2237,7 +2229,7 @@ static pa_card_profile *create_card_profile(struct userdata *u, const char *uuid pa_hashmap_put(p->source_prototypes, u->source_prototype, u->source_prototype); p->max_sink_channels = 0; p->max_source_channels = 2; - pa_hashmap_put(input_port->profiles, p->name, p); + pa_hashmap_put(u->input_port->profiles, p->name, p); d = PA_CARD_PROFILE_DATA(p); *d = PROFILE_A2DP_SOURCE; @@ -2252,8 +2244,8 @@ static pa_card_profile *create_card_profile(struct userdata *u, const char *uuid p->max_source_channels = 1; } - pa_hashmap_put(input_port->profiles, p->name, p); - pa_hashmap_put(output_port->profiles, p->name, p); + pa_hashmap_put(u->input_port->profiles, p->name, p); + pa_hashmap_put(u->output_port->profiles, p->name, p); d = PA_CARD_PROFILE_DATA(p); *d = PROFILE_HSP; @@ -2268,8 +2260,8 @@ static pa_card_profile *create_card_profile(struct userdata *u, const char *uuid p->max_source_channels = 1; } - pa_hashmap_put(input_port->profiles, p->name, p); - pa_hashmap_put(output_port->profiles, p->name, p); + pa_hashmap_put(u->input_port->profiles, p->name, p); + pa_hashmap_put(u->output_port->profiles, p->name, p); d = PA_CARD_PROFILE_DATA(p); *d = PROFILE_HFGW; @@ -2672,9 +2664,6 @@ void pa__done(pa_module *m) { if (u->modargs) pa_modargs_free(u->modargs); - pa_xfree(u->output_port_name); - pa_xfree(u->input_port_name); - pa_xfree(u->address); pa_xfree(u->path); -- 1.8.3.1