Skip to content

Commit

Permalink
cras: alsa: move jack to node lookups into common
Browse files Browse the repository at this point in the history
They only use common parts, so make them common functions

BUG=None
TEST=None

Change-Id: Ibbd4dd5f1f072921fdc1e97a54a9e9b17702fba8
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/adhd/+/5191965
Reviewed-by: Yu-Hsuan Hsu <yuhsuan@chromium.org>
Tested-by: chromeos-cop-builder@chromeos-cop.iam.gserviceaccount.com <chromeos-cop-builder@chromeos-cop.iam.gserviceaccount.com>
Commit-Queue: Curtis Malainey <cujomalainey@chromium.org>
  • Loading branch information
cujomalainey authored and Chromeos LUCI committed Jan 16, 2024
1 parent 9212b50 commit c0ddee9
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 125 deletions.
41 changes: 41 additions & 0 deletions cras/src/server/cras_alsa_common_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "cras/src/common/cras_alsa_card_info.h"
#include "cras/src/server/audio_thread.h"
#include "cras/src/server/cras_alsa_helpers.h"
#include "cras/src/server/cras_alsa_jack.h"
#include "cras/src/server/cras_alsa_ucm.h"
#include "cras/src/server/cras_iodev.h"
#include "cras/src/server/cras_iodev_list.h"
Expand Down Expand Up @@ -276,3 +277,43 @@ size_t cras_alsa_get_fixed_channels(struct alsa_common_io* aio) {
&channels);
return (rc) ? 0 : channels;
}

struct alsa_common_node* cras_alsa_get_output_node_from_jack(
struct alsa_common_io* aio,
const struct cras_alsa_jack* jack) {
struct mixer_control* mixer_output;
struct cras_ionode* node = NULL;
struct alsa_common_node* anode = NULL;

// Search by jack first.
DL_SEARCH_SCALAR_WITH_CAST(aio->base.nodes, node, anode, jack, jack);
if (anode) {
return anode;
}

// Search by mixer control next.
mixer_output = cras_alsa_jack_get_mixer_output(jack);
if (mixer_output == NULL) {
return NULL;
}

DL_SEARCH_SCALAR_WITH_CAST(aio->base.nodes, node, anode, mixer, mixer_output);
return anode;
}

struct alsa_common_node* cras_alsa_get_input_node_from_jack(
struct alsa_common_io* aio,
const struct cras_alsa_jack* jack) {
struct mixer_control* mixer_input;
struct alsa_common_node* anode = NULL;
struct cras_ionode* node = NULL;

mixer_input = cras_alsa_jack_get_mixer_input(jack);
if (mixer_input == NULL) {
DL_SEARCH_SCALAR_WITH_CAST(aio->base.nodes, node, anode, jack, jack);
return anode;
}

DL_SEARCH_SCALAR_WITH_CAST(aio->base.nodes, node, anode, mixer, mixer_input);
return anode;
}
10 changes: 10 additions & 0 deletions cras/src/server/cras_alsa_common_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ int cras_alsa_common_get_htimestamp(const struct cras_iodev* iodev,
int cras_alsa_get_fixed_rate(struct alsa_common_io* aio);
size_t cras_alsa_get_fixed_channels(struct alsa_common_io* aio);

/*
* Finds the output node associated with the jack. Returns NULL if not found.
*/
struct alsa_common_node* cras_alsa_get_output_node_from_jack(
struct alsa_common_io* aio,
const struct cras_alsa_jack* jack);
struct alsa_common_node* cras_alsa_get_input_node_from_jack(
struct alsa_common_io* aio,
const struct cras_alsa_jack* jack);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
87 changes: 22 additions & 65 deletions cras/src/server/cras_alsa_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -1203,51 +1203,6 @@ static void new_input_by_mixer_control(struct mixer_control* cras_input,
new_input(aio, cras_input, ctl_name);
}

/*
* Finds the output node associated with the jack. Returns NULL if not found.
*/
static struct alsa_output_node* get_output_node_from_jack(
struct alsa_io* aio,
const struct cras_alsa_jack* jack) {
struct mixer_control* mixer_output;
struct cras_ionode* node = NULL;
struct alsa_common_node* anode = NULL;

// Search by jack first.
DL_SEARCH_SCALAR_WITH_CAST(aio->common.base.nodes, node, anode, jack, jack);
if (anode) {
return (struct alsa_output_node*)anode;
}

// Search by mixer control next.
mixer_output = cras_alsa_jack_get_mixer_output(jack);
if (mixer_output == NULL) {
return NULL;
}

DL_SEARCH_SCALAR_WITH_CAST(aio->common.base.nodes, node, anode, mixer,
mixer_output);
return (struct alsa_output_node*)anode;
}

static struct alsa_input_node* get_input_node_from_jack(
struct alsa_io* aio,
const struct cras_alsa_jack* jack) {
struct mixer_control* mixer_input;
struct cras_ionode* node = NULL;
struct alsa_common_node* anode = NULL;

mixer_input = cras_alsa_jack_get_mixer_input(jack);
if (mixer_input == NULL) {
DL_SEARCH_SCALAR_WITH_CAST(aio->common.base.nodes, node, anode, jack, jack);
return (struct alsa_input_node*)anode;
}

DL_SEARCH_SCALAR_WITH_CAST(aio->common.base.nodes, node, anode, mixer,
mixer_input);
return (struct alsa_input_node*)anode;
}

/*
* Returns the dsp name specified in the ucm config. If there is a dsp name
* specified for the active node, use that. Otherwise NULL should be returned.
Expand Down Expand Up @@ -1420,23 +1375,24 @@ static void jack_output_plug_event(const struct cras_alsa_jack* jack,
}

struct alsa_io* aio = (struct alsa_io*)arg;
struct alsa_output_node* anode = get_output_node_from_jack(aio, jack);
struct alsa_common_node* anode =
cras_alsa_get_output_node_from_jack(&aio->common, jack);
const char* jack_name = cras_alsa_jack_get_name(jack);

if (anode == NULL) {
syslog(LOG_ERR, "No matching node found for jack %s", jack_name);
return;
}
if (anode->common.jack == NULL) {
if (anode->jack == NULL) {
syslog(LOG_ERR, "Jack %s isn't associated with the matched node",
jack_name);
return;
}

syslog(LOG_DEBUG, "%s plugged: %d, %s", jack_name, plugged,
cras_alsa_mixer_get_control_name(anode->common.mixer));
cras_alsa_mixer_get_control_name(anode->mixer));

struct cras_ionode* node = &anode->common.base;
struct cras_ionode* node = &anode->base;
cras_alsa_jack_update_monitor_name(jack, node->name, sizeof(node->name));
if (node->type == CRAS_NODE_TYPE_HDMI && plugged) {
node->stable_id = cras_alsa_jack_get_monitor_stable_id(
Expand Down Expand Up @@ -1470,25 +1426,26 @@ static void jack_input_plug_event(const struct cras_alsa_jack* jack,
return;
}
struct alsa_io* aio = (struct alsa_io*)arg;
struct alsa_input_node* node = get_input_node_from_jack(aio, jack);
struct alsa_common_node* node =
cras_alsa_get_input_node_from_jack(&aio->common, jack);
const char* jack_name = cras_alsa_jack_get_name(jack);

if (node == NULL) {
syslog(LOG_ERR, "No matching node found for jack %s", jack_name);
return;
}
if (node->common.jack == NULL) {
if (node->jack == NULL) {
syslog(LOG_ERR, "Jack %s isn't associated with the matched node",
jack_name);
return;
}

syslog(LOG_DEBUG, "%s plugged: %d, %s", jack_name, plugged,
cras_alsa_mixer_get_control_name(node->common.mixer));
cras_alsa_mixer_get_control_name(node->mixer));

cras_iodev_set_node_plugged(&node->common.base, plugged);
cras_iodev_set_node_plugged(&node->base, plugged);

check_auto_unplug_input_node(aio, &node->common.base, plugged);
check_auto_unplug_input_node(aio, &node->base, plugged);
}

/*
Expand Down Expand Up @@ -2297,59 +2254,59 @@ struct cras_iodev* alsa_iodev_create(
static void add_input_node_or_associate_jack(const struct cras_alsa_jack* jack,
void* arg) {
struct alsa_io* aio;
struct alsa_input_node* node;
struct alsa_common_node* node;
struct mixer_control* cras_input;
const char* jack_name;

CRAS_CHECK(arg);

aio = (struct alsa_io*)arg;
node = get_input_node_from_jack(aio, jack);
node = cras_alsa_get_input_node_from_jack(&aio->common, jack);
jack_name = cras_alsa_jack_get_name(jack);

// If there isn't a node for this jack, create one.
if (node == NULL) {
cras_input = cras_alsa_jack_get_mixer_input(jack);
node = new_input(aio, cras_input, jack_name);
node = (struct alsa_common_node*)new_input(aio, cras_input, jack_name);
if (node == NULL) {
return;
}
}

// If we already have the node, associate with the jack.
if (!node->common.jack) {
node->common.jack = jack;
if (!node->jack) {
node->jack = jack;
}
}

static void add_output_node_or_associate_jack(const struct cras_alsa_jack* jack,
void* arg) {
struct alsa_io* aio;
struct alsa_output_node* node;
struct alsa_common_node* node;
const char* jack_name;

CRAS_CHECK(arg);

aio = (struct alsa_io*)arg;
node = get_output_node_from_jack(aio, jack);
node = cras_alsa_get_output_node_from_jack(&aio->common, jack);
jack_name = cras_alsa_jack_get_name(jack);
if (!jack_name || !strcmp(jack_name, "Speaker Phantom Jack")) {
jack_name = INTERNAL_SPEAKER;
}

// If there isn't a node for this jack, create one.
if (node == NULL) {
node = new_output(aio, NULL, jack_name);
node = (struct alsa_common_node*)new_output(aio, NULL, jack_name);
if (node == NULL) {
return;
}

cras_alsa_jack_update_node_type(jack, &node->common.base.type);
cras_alsa_jack_update_node_type(jack, &node->base.type);
}

if (!node->common.jack) {
if (!node->jack) {
// If we already have the node, associate with the jack.
node->common.jack = jack;
node->jack = jack;
}
}

Expand Down
Loading

0 comments on commit c0ddee9

Please sign in to comment.