mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-10-29 05:40:27 -04:00
alsa-compress-offload: Add device object and udev based autodetection
As part of this, in alsa-udev.c, certain structures and variables referred to as "device" are renamed to "card". Otherwise, there is ambiguity, since "device" can mean a udev device, an SPA device, a compress-offload device, a PCM device etc. Also, replace "card id" with "card number" to emphasize that these integers are not actually SPA object IDs.
This commit is contained in:
parent
ae4bff78a3
commit
8e6d070148
8 changed files with 1050 additions and 237 deletions
|
|
@ -90,8 +90,10 @@ extern "C" {
|
|||
#define SPA_NAME_API_ALSA_SEQ_BRIDGE "api.alsa.seq.bridge" /**< an alsa Node interface for
|
||||
* bridging midi ports */
|
||||
#define SPA_NAME_API_ALSA_ACP_DEVICE "api.alsa.acp.device" /**< an alsa ACP Device interface */
|
||||
#define SPA_NAME_API_ALSA_COMPRESS_OFFLOAD_SINK "api.alsa.compress.offload.sink" /**< an alsa Node interface for
|
||||
* compressed audio */
|
||||
#define SPA_NAME_API_ALSA_COMPRESS_OFFLOAD_DEVICE "api.alsa.compress.offload.device" /**< an alsa Device interface for
|
||||
* compressed audio */
|
||||
#define SPA_NAME_API_ALSA_COMPRESS_OFFLOAD_SINK "api.alsa.compress.offload.sink" /**< an alsa Node interface for
|
||||
* compressed audio */
|
||||
|
||||
/** keys for bluez5 factory names */
|
||||
#define SPA_NAME_API_BLUEZ5_ENUM_DBUS "api.bluez5.enum.dbus" /**< a dbus Device interface */
|
||||
|
|
|
|||
578
spa/plugins/alsa/alsa-compress-offload-device.c
Normal file
578
spa/plugins/alsa/alsa-compress-offload-device.c
Normal file
|
|
@ -0,0 +1,578 @@
|
|||
/* Spa ALSA Compress-Offload device */
|
||||
/* SPDX-FileCopyrightText: Copyright @ 2023 Carlos Rafael Giani */
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <limits.h>
|
||||
#include <dirent.h>
|
||||
|
||||
#include <alsa/asoundlib.h>
|
||||
|
||||
#include <spa/debug/dict.h>
|
||||
#include <spa/debug/log.h>
|
||||
#include <spa/debug/pod.h>
|
||||
#include <spa/node/node.h>
|
||||
#include <spa/support/plugin.h>
|
||||
#include <spa/monitor/device.h>
|
||||
#include <spa/monitor/utils.h>
|
||||
#include <spa/node/keys.h>
|
||||
#include <spa/param/param.h>
|
||||
#include <spa/pod/filter.h>
|
||||
#include <spa/pod/parser.h>
|
||||
#include <spa/utils/cleanup.h>
|
||||
#include <spa/utils/dict.h>
|
||||
#include <spa/utils/keys.h>
|
||||
#include <spa/utils/names.h>
|
||||
#include <spa/utils/string.h>
|
||||
|
||||
#include "compress-offload-api-util.h"
|
||||
#include "alsa.h"
|
||||
|
||||
static const char default_device[] = "hw:0";
|
||||
|
||||
struct props {
|
||||
char device[64];
|
||||
unsigned int card_nr;
|
||||
};
|
||||
|
||||
static void reset_props(struct props *props)
|
||||
{
|
||||
strncpy(props->device, default_device, 64);
|
||||
props->card_nr = 0;
|
||||
}
|
||||
|
||||
struct impl {
|
||||
struct spa_handle handle;
|
||||
struct spa_device device;
|
||||
|
||||
struct spa_log *log;
|
||||
|
||||
struct spa_hook_list hooks;
|
||||
|
||||
struct props props;
|
||||
uint32_t n_nodes;
|
||||
uint32_t n_capture;
|
||||
uint32_t n_playback;
|
||||
|
||||
uint32_t profile;
|
||||
};
|
||||
|
||||
#define ADD_DICT_ITEM(key, value) do { items[n_items++] = SPA_DICT_ITEM_INIT(key, value); } while (0)
|
||||
|
||||
static void emit_node(struct impl *this, const char *device_node, unsigned int device_nr,
|
||||
enum spa_compress_offload_direction direction, snd_ctl_card_info_t *cardinfo,
|
||||
uint32_t id)
|
||||
{
|
||||
struct spa_dict_item items[5];
|
||||
uint32_t n_items = 0;
|
||||
char alsa_path[128], path[180];
|
||||
char node_name[200];
|
||||
char node_desc[200];
|
||||
struct spa_device_object_info info;
|
||||
const char *stream;
|
||||
|
||||
spa_log_debug(this->log, "emitting node info for device %s (card nr %u device nr %u)",
|
||||
device_node, this->props.card_nr, device_nr);
|
||||
|
||||
info = SPA_DEVICE_OBJECT_INFO_INIT();
|
||||
info.type = SPA_TYPE_INTERFACE_Node;
|
||||
|
||||
if (direction == SPA_COMPRESS_OFFLOAD_DIRECTION_PLAYBACK) {
|
||||
stream = "playback";
|
||||
info.factory_name = SPA_NAME_API_ALSA_COMPRESS_OFFLOAD_SINK;
|
||||
} else {
|
||||
stream = "capture";
|
||||
/* TODO: This is not yet implemented, because getting Compress-Offload
|
||||
* hardware that can capture audio is difficult to do. The only hardware
|
||||
* known is the Wolfson ADSP; the only driver in the kernel that exposes
|
||||
* Compress-Offload capture devices is the one for that hardware. */
|
||||
assert(false);
|
||||
}
|
||||
|
||||
info.change_mask = SPA_DEVICE_OBJECT_CHANGE_MASK_PROPS;
|
||||
|
||||
snprintf(alsa_path, sizeof(alsa_path), "%s,%u", this->props.device, device_nr);
|
||||
snprintf(path, sizeof(path), "alsa:compressed:%s:%u:%s", snd_ctl_card_info_get_id(cardinfo), device_nr, stream);
|
||||
snprintf(node_name, sizeof(node_name), "comprC%uD%u", this->props.card_nr, device_nr);
|
||||
snprintf(node_desc, sizeof(node_desc), "Compress-Offload sink node (ALSA card %u device %u)", this->props.card_nr, device_nr);
|
||||
|
||||
ADD_DICT_ITEM(SPA_KEY_NODE_NAME, node_name);
|
||||
ADD_DICT_ITEM(SPA_KEY_NODE_DESCRIPTION, node_desc);
|
||||
ADD_DICT_ITEM(SPA_KEY_OBJECT_PATH, path);
|
||||
ADD_DICT_ITEM(SPA_KEY_API_ALSA_PATH, alsa_path);
|
||||
/* NOTE: Set alsa.name, since session managers look for this, or for
|
||||
* SPA_KEY_API_ALSA_PCM_NAME, or other items. The best fit in this
|
||||
* case seems to be alsa.name, since SPA_KEY_API_ALSA_PCM_NAME is
|
||||
* PCM specific, as the name suggests. If none of these items are
|
||||
* provided, session managers may not work properly. WirePlumber's
|
||||
* alsa.lua script looks for these for example.
|
||||
* And, since we have no good way of getting a name, just reuse
|
||||
* the alsa_path here. */
|
||||
ADD_DICT_ITEM("alsa.name", alsa_path);
|
||||
|
||||
info.props = &SPA_DICT_INIT(items, n_items);
|
||||
|
||||
spa_log_debug(this->log, "node information:");
|
||||
spa_debug_dict(2, info.props);
|
||||
|
||||
spa_device_emit_object_info(&this->hooks, id, &info);
|
||||
}
|
||||
|
||||
static int set_profile(struct impl *this, uint32_t id)
|
||||
{
|
||||
int ret = 0;
|
||||
uint32_t i, n_cap, n_play;
|
||||
char prefix[32];
|
||||
int prefix_length;
|
||||
struct dirent *entry;
|
||||
DIR *snd_dir = NULL;
|
||||
snd_ctl_t *ctl_handle = NULL;
|
||||
snd_ctl_card_info_t *cardinfo;
|
||||
|
||||
spa_log_debug(this->log, "enumerate Compress-Offload nodes for card %s; profile: %d",
|
||||
this->props.device, id);
|
||||
|
||||
if ((ret = snd_ctl_open(&ctl_handle, this->props.device, 0)) < 0) {
|
||||
spa_log_error(this->log, "can't open control for card %s: %s",
|
||||
this->props.device, snd_strerror(ret));
|
||||
goto finish;
|
||||
}
|
||||
|
||||
this->profile = id;
|
||||
|
||||
snd_ctl_card_info_alloca(&cardinfo);
|
||||
if ((ret = snd_ctl_card_info(ctl_handle, cardinfo)) < 0) {
|
||||
spa_log_error(this->log, "error card info: %s", snd_strerror(ret));
|
||||
goto finish;
|
||||
}
|
||||
|
||||
/* Clear any previous node object info. */
|
||||
for (i = 0; i < this->n_nodes; i++)
|
||||
spa_device_emit_object_info(&this->hooks, i, NULL);
|
||||
|
||||
this->n_nodes = this->n_capture = this->n_playback = 0;
|
||||
|
||||
/* Profile ID 0 is the "off" profile, that is, the profile where the device
|
||||
* is "disabled". To implement such a disabled state, simply exit here without
|
||||
* adding any nodes after we removed any existing one (see above). */
|
||||
if (id == 0)
|
||||
{
|
||||
spa_log_debug(this->log, "\"Off\" profile selected - exiting without "
|
||||
"creating any nodes after all previous ones were removed");
|
||||
goto finish;
|
||||
}
|
||||
|
||||
spa_scnprintf(prefix, sizeof(prefix), "comprC%uD", this->props.card_nr);
|
||||
prefix_length = strlen(prefix);
|
||||
|
||||
/* There is no API to enumerate all Compress-Offload devices, so we have
|
||||
* to stick to walking through the /dev/snd directory entries and looking
|
||||
* for device nodes that match the comprC<card number>D prefix. */
|
||||
snd_dir = opendir("/dev/snd");
|
||||
if (snd_dir == NULL)
|
||||
goto errno_error;
|
||||
|
||||
i = 0;
|
||||
i = n_cap = n_play = 0;
|
||||
while ((errno = 0, entry = readdir(snd_dir)) != NULL) {
|
||||
long long device_nr;
|
||||
enum spa_compress_offload_direction direction;
|
||||
|
||||
if (!(entry->d_type == DT_CHR && spa_strstartswith(entry->d_name, prefix)))
|
||||
continue;
|
||||
|
||||
/* Parse the device number from the device filename. We know that the filename
|
||||
* is always structured like this: comprC<card number>D<device number>
|
||||
* We consider "comprC<card number>D" to form the "prefix" here. Right after
|
||||
* that prefix, the device number can be parsed, so skip the prefix. */
|
||||
device_nr = strtol(entry->d_name + prefix_length, NULL, 10);
|
||||
if ((device_nr < 0) || (device_nr > UINT_MAX)) {
|
||||
spa_log_warn(this->log, "device %s contains unusable device number; "
|
||||
"skipping", entry->d_name);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (get_compress_offload_device_direction(this->props.card_nr, device_nr,
|
||||
this->log, &direction) < 0)
|
||||
goto finish;
|
||||
|
||||
switch (direction) {
|
||||
case SPA_COMPRESS_OFFLOAD_DIRECTION_PLAYBACK:
|
||||
n_play++;
|
||||
emit_node(this, entry->d_name, device_nr, direction, cardinfo, i++);
|
||||
break;
|
||||
case SPA_COMPRESS_OFFLOAD_DIRECTION_CAPTURE:
|
||||
/* TODO: Disabled for now. See the TODO in emit_node() for details. */
|
||||
#if 0
|
||||
n_cap++;
|
||||
emit_node(this, entry->d_name, device_nr, direction, cardinfo, i++);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
this->n_capture = n_cap;
|
||||
this->n_playback = n_play;
|
||||
this->n_nodes = i;
|
||||
|
||||
finish:
|
||||
if (snd_dir != NULL)
|
||||
closedir(snd_dir);
|
||||
if (ctl_handle != NULL)
|
||||
snd_ctl_close(ctl_handle);
|
||||
return ret;
|
||||
|
||||
errno_error:
|
||||
ret = -errno;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
static int emit_info(struct impl *this, bool full)
|
||||
{
|
||||
int err = 0;
|
||||
struct spa_dict_item items[20];
|
||||
uint32_t n_items = 0;
|
||||
snd_ctl_t *ctl_hndl;
|
||||
snd_ctl_card_info_t *info;
|
||||
struct spa_device_info dinfo;
|
||||
struct spa_param_info params[2];
|
||||
char path[128];
|
||||
char device_name[200];
|
||||
char device_desc[200];
|
||||
|
||||
spa_log_debug(this->log, "open card %s", this->props.device);
|
||||
if ((err = snd_ctl_open(&ctl_hndl, this->props.device, 0)) < 0) {
|
||||
spa_log_error(this->log, "can't open control for card %s: %s",
|
||||
this->props.device, snd_strerror(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
snd_ctl_card_info_alloca(&info);
|
||||
if ((err = snd_ctl_card_info(ctl_hndl, info)) < 0) {
|
||||
spa_log_error(this->log, "error hardware info: %s", snd_strerror(err));
|
||||
goto finish;
|
||||
}
|
||||
|
||||
dinfo = SPA_DEVICE_INFO_INIT();
|
||||
|
||||
dinfo.change_mask = SPA_DEVICE_CHANGE_MASK_PROPS;
|
||||
|
||||
snprintf(path, sizeof(path), "alsa:compressed:%s", snd_ctl_card_info_get_id(info));
|
||||
snprintf(device_name, sizeof(device_name), "comprC%u", this->props.card_nr);
|
||||
snprintf(device_desc, sizeof(device_desc), "Compress-Offload device (ALSA card %u)", this->props.card_nr);
|
||||
|
||||
ADD_DICT_ITEM(SPA_KEY_OBJECT_PATH, path);
|
||||
ADD_DICT_ITEM(SPA_KEY_DEVICE_API, "alsa:compressed");
|
||||
ADD_DICT_ITEM(SPA_KEY_DEVICE_NICK, "alsa:compressed");
|
||||
ADD_DICT_ITEM(SPA_KEY_DEVICE_NAME, device_name);
|
||||
ADD_DICT_ITEM(SPA_KEY_DEVICE_DESCRIPTION, device_desc);
|
||||
ADD_DICT_ITEM(SPA_KEY_MEDIA_CLASS, "Audio/Device");
|
||||
ADD_DICT_ITEM(SPA_KEY_API_ALSA_PATH, (char *)this->props.device);
|
||||
ADD_DICT_ITEM(SPA_KEY_API_ALSA_CARD_ID, snd_ctl_card_info_get_id(info));
|
||||
ADD_DICT_ITEM(SPA_KEY_API_ALSA_CARD_COMPONENTS, snd_ctl_card_info_get_components(info));
|
||||
ADD_DICT_ITEM(SPA_KEY_API_ALSA_CARD_DRIVER, snd_ctl_card_info_get_driver(info));
|
||||
ADD_DICT_ITEM(SPA_KEY_API_ALSA_CARD_NAME, snd_ctl_card_info_get_name(info));
|
||||
ADD_DICT_ITEM(SPA_KEY_API_ALSA_CARD_LONGNAME, snd_ctl_card_info_get_longname(info));
|
||||
ADD_DICT_ITEM(SPA_KEY_API_ALSA_CARD_MIXERNAME, snd_ctl_card_info_get_mixername(info));
|
||||
|
||||
dinfo.props = &SPA_DICT_INIT(items, n_items);
|
||||
|
||||
dinfo.change_mask |= SPA_DEVICE_CHANGE_MASK_PARAMS;
|
||||
params[0] = SPA_PARAM_INFO(SPA_PARAM_EnumProfile, SPA_PARAM_INFO_READ);
|
||||
params[1] = SPA_PARAM_INFO(SPA_PARAM_Profile, SPA_PARAM_INFO_READWRITE);
|
||||
dinfo.n_params = SPA_N_ELEMENTS(params);
|
||||
dinfo.params = params;
|
||||
|
||||
spa_device_emit_info(&this->hooks, &dinfo);
|
||||
|
||||
finish:
|
||||
spa_log_debug(this->log, "close card %s", this->props.device);
|
||||
snd_ctl_close(ctl_hndl);
|
||||
return err;
|
||||
}
|
||||
|
||||
static int impl_add_listener(void *object,
|
||||
struct spa_hook *listener,
|
||||
const struct spa_device_events *events,
|
||||
void *data)
|
||||
{
|
||||
struct impl *this = object;
|
||||
struct spa_hook_list save;
|
||||
|
||||
spa_return_val_if_fail(this != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(events != NULL, -EINVAL);
|
||||
|
||||
spa_hook_list_isolate(&this->hooks, &save, listener, events, data);
|
||||
|
||||
if (events->info || events->object_info)
|
||||
emit_info(this, true);
|
||||
|
||||
spa_hook_list_join(&this->hooks, &save);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int impl_sync(void *object, int seq)
|
||||
{
|
||||
struct impl *this = object;
|
||||
|
||||
spa_return_val_if_fail(this != NULL, -EINVAL);
|
||||
|
||||
spa_device_emit_result(&this->hooks, seq, 0, 0, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct spa_pod *build_profile(struct impl *this, struct spa_pod_builder *b,
|
||||
uint32_t id, uint32_t index)
|
||||
{
|
||||
struct spa_pod_frame f[2];
|
||||
const char *name, *desc;
|
||||
|
||||
switch (index) {
|
||||
case 0:
|
||||
name = "off";
|
||||
desc = "Off";
|
||||
break;
|
||||
case 1:
|
||||
name = "on";
|
||||
desc = "On";
|
||||
break;
|
||||
default:
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
spa_pod_builder_push_object(b, &f[0], SPA_TYPE_OBJECT_ParamProfile, id);
|
||||
spa_pod_builder_add(b,
|
||||
SPA_PARAM_PROFILE_index, SPA_POD_Int(index),
|
||||
SPA_PARAM_PROFILE_name, SPA_POD_String(name),
|
||||
SPA_PARAM_PROFILE_description, SPA_POD_String(desc),
|
||||
0);
|
||||
if (index == 1) {
|
||||
spa_pod_builder_prop(b, SPA_PARAM_PROFILE_classes, 0);
|
||||
spa_pod_builder_push_struct(b, &f[1]);
|
||||
if (this->n_capture) {
|
||||
spa_pod_builder_add_struct(b,
|
||||
SPA_POD_String("Audio/Source"),
|
||||
SPA_POD_Int(this->n_capture));
|
||||
}
|
||||
if (this->n_playback) {
|
||||
spa_pod_builder_add_struct(b,
|
||||
SPA_POD_String("Audio/Sink"),
|
||||
SPA_POD_Int(this->n_playback));
|
||||
}
|
||||
spa_pod_builder_pop(b, &f[1]);
|
||||
}
|
||||
return spa_pod_builder_pop(b, &f[0]);
|
||||
|
||||
}
|
||||
|
||||
static int impl_enum_params(void *object, int seq,
|
||||
uint32_t id, uint32_t start, uint32_t num,
|
||||
const struct spa_pod *filter)
|
||||
{
|
||||
struct impl *this = object;
|
||||
struct spa_pod *param;
|
||||
struct spa_pod_builder b = { 0 };
|
||||
uint8_t buffer[1024];
|
||||
struct spa_result_device_params result;
|
||||
uint32_t count = 0;
|
||||
|
||||
spa_return_val_if_fail(this != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(num != 0, -EINVAL);
|
||||
|
||||
result.id = id;
|
||||
result.next = start;
|
||||
next:
|
||||
result.index = result.next++;
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
|
||||
switch (id) {
|
||||
case SPA_PARAM_EnumProfile:
|
||||
{
|
||||
switch (result.index) {
|
||||
case 0:
|
||||
case 1:
|
||||
param = build_profile(this, &b, id, result.index);
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SPA_PARAM_Profile:
|
||||
{
|
||||
switch (result.index) {
|
||||
case 0:
|
||||
param = build_profile(this, &b, id, this->profile);
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
if (spa_pod_filter(&b, &result.param, param, filter) < 0)
|
||||
goto next;
|
||||
|
||||
spa_device_emit_result(&this->hooks, seq, 0,
|
||||
SPA_RESULT_TYPE_DEVICE_PARAMS, &result);
|
||||
|
||||
if (++count != num)
|
||||
goto next;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int impl_set_param(void *object,
|
||||
uint32_t id, uint32_t flags,
|
||||
const struct spa_pod *param)
|
||||
{
|
||||
struct impl *this = object;
|
||||
int res;
|
||||
|
||||
spa_return_val_if_fail(this != NULL, -EINVAL);
|
||||
|
||||
switch (id) {
|
||||
case SPA_PARAM_Profile:
|
||||
{
|
||||
uint32_t idx;
|
||||
|
||||
if ((res = spa_pod_parse_object(param,
|
||||
SPA_TYPE_OBJECT_ParamProfile, NULL,
|
||||
SPA_PARAM_PROFILE_index, SPA_POD_Int(&idx))) < 0) {
|
||||
spa_log_warn(this->log, "can't parse profile");
|
||||
spa_debug_log_pod(this->log, SPA_LOG_LEVEL_DEBUG, 0, NULL, param);
|
||||
return res;
|
||||
}
|
||||
|
||||
set_profile(this, idx);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return -ENOENT;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct spa_device_methods impl_device = {
|
||||
SPA_VERSION_DEVICE_METHODS,
|
||||
.add_listener = impl_add_listener,
|
||||
.sync = impl_sync,
|
||||
.enum_params = impl_enum_params,
|
||||
.set_param = impl_set_param,
|
||||
};
|
||||
|
||||
static int impl_get_interface(struct spa_handle *handle, const char *type, void **interface)
|
||||
{
|
||||
struct impl *this;
|
||||
|
||||
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(interface != NULL, -EINVAL);
|
||||
|
||||
this = (struct impl *) handle;
|
||||
|
||||
if (spa_streq(type, SPA_TYPE_INTERFACE_Device))
|
||||
*interface = &this->device;
|
||||
else
|
||||
return -ENOENT;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int impl_clear(struct spa_handle *handle)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static size_t impl_get_size(const struct spa_handle_factory *factory,
|
||||
const struct spa_dict *params)
|
||||
{
|
||||
return sizeof(struct impl);
|
||||
}
|
||||
|
||||
static int impl_init(const struct spa_handle_factory *factory,
|
||||
struct spa_handle *handle,
|
||||
const struct spa_dict *info,
|
||||
const struct spa_support *support,
|
||||
uint32_t n_support)
|
||||
{
|
||||
struct impl *this;
|
||||
|
||||
spa_return_val_if_fail(factory != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
||||
|
||||
handle->get_interface = impl_get_interface;
|
||||
handle->clear = impl_clear;
|
||||
|
||||
this = (struct impl *) handle;
|
||||
|
||||
this->log = spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Log);
|
||||
alsa_log_topic_init(this->log);
|
||||
|
||||
this->device.iface = SPA_INTERFACE_INIT(
|
||||
SPA_TYPE_INTERFACE_Device,
|
||||
SPA_VERSION_DEVICE,
|
||||
&impl_device, this);
|
||||
spa_hook_list_init(&this->hooks);
|
||||
|
||||
reset_props(&this->props);
|
||||
|
||||
snd_config_update_free_global();
|
||||
|
||||
if (info) {
|
||||
uint32_t i;
|
||||
for (i = 0; info && i < info->n_items; i++) {
|
||||
const char *k = info->items[i].key;
|
||||
const char *s = info->items[i].value;
|
||||
|
||||
if (spa_streq(k, SPA_KEY_API_ALSA_PATH)) {
|
||||
snprintf(this->props.device, 64, "%s", s);
|
||||
spa_log_debug(this->log, "using ALSA path \"%s\"", this->props.device);
|
||||
} else if (spa_streq(k, SPA_KEY_API_ALSA_CARD)) {
|
||||
long long card_nr = strtol(s, NULL, 10);
|
||||
if ((card_nr >= 0) && (card_nr <= UINT_MAX)) {
|
||||
this->props.card_nr = card_nr;
|
||||
spa_log_debug(this->log, "using ALSA card number %u", this->props.card_nr);
|
||||
} else
|
||||
spa_log_warn(this->log, "invalid ALSA card number \"%s\"; using default", s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct spa_interface_info impl_interfaces[] = {
|
||||
{SPA_TYPE_INTERFACE_Device,},
|
||||
};
|
||||
|
||||
static int
|
||||
impl_enum_interface_info(const struct spa_handle_factory *factory,
|
||||
const struct spa_interface_info **info,
|
||||
uint32_t *index)
|
||||
{
|
||||
spa_return_val_if_fail(factory != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(info != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(index != NULL, -EINVAL);
|
||||
|
||||
if (*index >= SPA_N_ELEMENTS(impl_interfaces))
|
||||
return 0;
|
||||
|
||||
*info = &impl_interfaces[(*index)++];
|
||||
return 1;
|
||||
}
|
||||
|
||||
const struct spa_handle_factory spa_alsa_compress_offload_device_factory = {
|
||||
SPA_VERSION_HANDLE_FACTORY,
|
||||
SPA_NAME_API_ALSA_COMPRESS_OFFLOAD_DEVICE,
|
||||
NULL,
|
||||
impl_get_size,
|
||||
impl_init,
|
||||
impl_enum_interface_info,
|
||||
};
|
||||
|
|
@ -24,24 +24,51 @@
|
|||
#include <spa/support/plugin.h>
|
||||
#include <spa/monitor/device.h>
|
||||
#include <spa/monitor/utils.h>
|
||||
#include <spa/debug/dict.h>
|
||||
|
||||
#include "alsa.h"
|
||||
|
||||
#define MAX_DEVICES 64
|
||||
#define MAX_CARDS 64
|
||||
|
||||
#define ACTION_ADD 0
|
||||
#define ACTION_REMOVE 1
|
||||
#define ACTION_DISABLE 2
|
||||
|
||||
struct device {
|
||||
uint32_t id;
|
||||
struct udev_device *dev;
|
||||
/* Used for unavailable devices in the card structure. */
|
||||
#define ID_DEVICE_NOT_SUPPORTED 0
|
||||
|
||||
/* This represents an ALSA card.
|
||||
* One card can have up to 1 PCM and 1 Compress-Offload device. */
|
||||
struct card {
|
||||
unsigned int card_nr;
|
||||
struct udev_device *udev_device;
|
||||
unsigned int unavailable:1;
|
||||
unsigned int accessible:1;
|
||||
unsigned int ignored:1;
|
||||
unsigned int emitted:1;
|
||||
|
||||
/* Local SPA object IDs. (Global IDs are produced by PipeWire
|
||||
* out of this using its registry.) Compress-Offload or PCM
|
||||
* is not available, the corresponding ID is set to
|
||||
* ID_DEVICE_NOT_SUPPORTED (= 0).
|
||||
* PCM device IDs are (card nr + 1) * 2, and Compress-Offload
|
||||
* device IDs are (card nr + 1) * 2 + 1. Assigning IDs like this
|
||||
* makes it easy to deal with removed devices. (card nr + 1)
|
||||
* is used because 0 is a valid ALSA card number. */
|
||||
uint32_t pcm_device_id;
|
||||
uint32_t compress_offload_device_id;
|
||||
};
|
||||
|
||||
static uint32_t calc_pcm_device_id(struct card *card)
|
||||
{
|
||||
return (card->card_nr + 1) * 2 + 0;
|
||||
}
|
||||
|
||||
static uint32_t calc_compress_offload_device_id(struct card *card)
|
||||
{
|
||||
return (card->card_nr + 1) * 2 + 1;
|
||||
}
|
||||
|
||||
struct impl {
|
||||
struct spa_handle handle;
|
||||
struct spa_device device;
|
||||
|
|
@ -58,8 +85,8 @@ struct impl {
|
|||
struct udev *udev;
|
||||
struct udev_monitor *umonitor;
|
||||
|
||||
struct device devices[MAX_DEVICES];
|
||||
uint32_t n_devices;
|
||||
struct card cards[MAX_CARDS];
|
||||
unsigned int n_cards;
|
||||
|
||||
struct spa_source source;
|
||||
struct spa_source notify;
|
||||
|
|
@ -84,58 +111,60 @@ static int impl_udev_close(struct impl *this)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static struct device *add_device(struct impl *this, uint32_t id, struct udev_device *dev)
|
||||
static struct card *add_card(struct impl *this, unsigned int card_nr, struct udev_device *udev_device)
|
||||
{
|
||||
struct device *device;
|
||||
struct card *card;
|
||||
|
||||
if (this->n_devices >= MAX_DEVICES)
|
||||
if (this->n_cards >= MAX_CARDS)
|
||||
return NULL;
|
||||
device = &this->devices[this->n_devices++];
|
||||
spa_zero(*device);
|
||||
device->id = id;
|
||||
udev_device_ref(dev);
|
||||
device->dev = dev;
|
||||
return device;
|
||||
|
||||
card = &this->cards[this->n_cards++];
|
||||
spa_zero(*card);
|
||||
card->card_nr = card_nr;
|
||||
udev_device_ref(udev_device);
|
||||
card->udev_device = udev_device;
|
||||
|
||||
return card;
|
||||
}
|
||||
|
||||
static struct device *find_device(struct impl *this, uint32_t id)
|
||||
static struct card *find_card(struct impl *this, unsigned int card_nr)
|
||||
{
|
||||
uint32_t i;
|
||||
for (i = 0; i < this->n_devices; i++) {
|
||||
if (this->devices[i].id == id)
|
||||
return &this->devices[i];
|
||||
unsigned int i;
|
||||
for (i = 0; i < this->n_cards; i++) {
|
||||
if (this->cards[i].card_nr == card_nr)
|
||||
return &this->cards[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void remove_device(struct impl *this, struct device *device)
|
||||
static void remove_card(struct impl *this, struct card *card)
|
||||
{
|
||||
udev_device_unref(device->dev);
|
||||
*device = this->devices[--this->n_devices];
|
||||
udev_device_unref(card->udev_device);
|
||||
*card = this->cards[--this->n_cards];
|
||||
}
|
||||
|
||||
static void clear_devices(struct impl *this)
|
||||
static void clear_cards(struct impl *this)
|
||||
{
|
||||
uint32_t i;
|
||||
for (i = 0; i < this->n_devices; i++)
|
||||
udev_device_unref(this->devices[i].dev);
|
||||
this->n_devices = 0;
|
||||
unsigned int i;
|
||||
for (i = 0; i < this->n_cards; i++)
|
||||
udev_device_unref(this->cards[i].udev_device);
|
||||
this->n_cards = 0;
|
||||
}
|
||||
|
||||
static uint32_t get_card_id(struct impl *this, struct udev_device *dev)
|
||||
static unsigned int get_card_nr(struct impl *this, struct udev_device *udev_device)
|
||||
{
|
||||
const char *e, *str;
|
||||
|
||||
if (udev_device_get_property_value(dev, "ACP_IGNORE"))
|
||||
if (udev_device_get_property_value(udev_device, "ACP_IGNORE"))
|
||||
return SPA_ID_INVALID;
|
||||
|
||||
if ((str = udev_device_get_property_value(dev, "SOUND_CLASS")) && spa_streq(str, "modem"))
|
||||
if ((str = udev_device_get_property_value(udev_device, "SOUND_CLASS")) && spa_streq(str, "modem"))
|
||||
return SPA_ID_INVALID;
|
||||
|
||||
if (udev_device_get_property_value(dev, "SOUND_INITIALIZED") == NULL)
|
||||
if (udev_device_get_property_value(udev_device, "SOUND_INITIALIZED") == NULL)
|
||||
return SPA_ID_INVALID;
|
||||
|
||||
if ((str = udev_device_get_property_value(dev, "DEVPATH")) == NULL)
|
||||
if ((str = udev_device_get_property_value(udev_device, "DEVPATH")) == NULL)
|
||||
return SPA_ID_INVALID;
|
||||
|
||||
if ((e = strrchr(str, '/')) == NULL)
|
||||
|
|
@ -244,7 +273,7 @@ static int check_device_pcm_class(const char *devname)
|
|||
return spa_strstartswith(buf, "modem") ? -ENXIO : 0;
|
||||
}
|
||||
|
||||
static int get_num_pcm_devices(unsigned int card_id)
|
||||
static int get_num_pcm_devices(unsigned int card_nr)
|
||||
{
|
||||
char prefix[32];
|
||||
struct dirent *entry;
|
||||
|
|
@ -253,7 +282,7 @@ static int get_num_pcm_devices(unsigned int card_id)
|
|||
|
||||
/* Check if card has PCM devices, without opening them */
|
||||
|
||||
spa_scnprintf(prefix, sizeof(prefix), "pcmC%uD", card_id);
|
||||
spa_scnprintf(prefix, sizeof(prefix), "pcmC%uD", card_nr);
|
||||
|
||||
spa_autoptr(DIR) snd = opendir("/dev/snd");
|
||||
if (snd == NULL)
|
||||
|
|
@ -274,7 +303,33 @@ static int get_num_pcm_devices(unsigned int card_id)
|
|||
return errno != 0 ? -errno : num_dev;
|
||||
}
|
||||
|
||||
static int check_device_available(struct impl *this, struct device *device, int *num_pcm)
|
||||
static int get_num_compress_offload_devices(unsigned int card_nr)
|
||||
{
|
||||
char prefix[32];
|
||||
struct dirent *entry;
|
||||
int num_dev = 0;
|
||||
|
||||
/* Check if card has Compress-Offload devices, without opening them */
|
||||
|
||||
spa_scnprintf(prefix, sizeof(prefix), "comprC%uD", card_nr);
|
||||
|
||||
spa_autoptr(DIR) snd = opendir("/dev/snd");
|
||||
if (snd == NULL)
|
||||
return -errno;
|
||||
|
||||
while ((errno = 0, entry = readdir(snd)) != NULL) {
|
||||
if (!(entry->d_type == DT_CHR &&
|
||||
spa_strstartswith(entry->d_name, prefix)))
|
||||
continue;
|
||||
|
||||
++num_dev;
|
||||
}
|
||||
|
||||
return errno != 0 ? -errno : num_dev;
|
||||
}
|
||||
|
||||
static int check_pcm_device_availability(struct impl *this, struct card *card,
|
||||
int *num_pcm_devices)
|
||||
{
|
||||
char path[PATH_MAX];
|
||||
char buf[16];
|
||||
|
|
@ -282,15 +337,16 @@ static int check_device_available(struct impl *this, struct device *device, int
|
|||
struct dirent *entry, *entry_pcm;
|
||||
int res;
|
||||
|
||||
res = get_num_pcm_devices(device->id);
|
||||
res = get_num_pcm_devices(card->card_nr);
|
||||
if (res < 0) {
|
||||
spa_log_error(this->log, "Error finding PCM devices for ALSA card %u: %s",
|
||||
(unsigned int)device->id, spa_strerror(res));
|
||||
card->card_nr, spa_strerror(res));
|
||||
return res;
|
||||
}
|
||||
*num_pcm = res;
|
||||
*num_pcm_devices = res;
|
||||
|
||||
spa_log_debug(this->log, "card %u has %d pcm device(s)", (unsigned int)device->id, *num_pcm);
|
||||
spa_log_debug(this->log, "card %u has %d PCM device(s)",
|
||||
card->card_nr, *num_pcm_devices);
|
||||
|
||||
/*
|
||||
* Check if some pcm devices of the card are busy. Check it via /proc, as we
|
||||
|
|
@ -304,25 +360,25 @@ static int check_device_available(struct impl *this, struct device *device, int
|
|||
|
||||
res = 0;
|
||||
|
||||
spa_scnprintf(path, sizeof(path), "/proc/asound/card%u", (unsigned int)device->id);
|
||||
spa_scnprintf(path, sizeof(path), "/proc/asound/card%u", card->card_nr);
|
||||
|
||||
spa_autoptr(DIR) card = opendir(path);
|
||||
if (card == NULL)
|
||||
spa_autoptr(DIR) card_dir = opendir(path);
|
||||
if (card_dir == NULL)
|
||||
goto done;
|
||||
|
||||
while ((errno = 0, entry = readdir(card)) != NULL) {
|
||||
while ((errno = 0, entry = readdir(card_dir)) != NULL) {
|
||||
if (!(entry->d_type == DT_DIR &&
|
||||
spa_strstartswith(entry->d_name, "pcm")))
|
||||
continue;
|
||||
|
||||
spa_scnprintf(path, sizeof(path), "pcmC%uD%s",
|
||||
(unsigned int)device->id, entry->d_name+3);
|
||||
card->card_nr, entry->d_name+3);
|
||||
if (check_device_pcm_class(path) < 0)
|
||||
continue;
|
||||
|
||||
/* Check busy status */
|
||||
spa_scnprintf(path, sizeof(path), "/proc/asound/card%u/%s",
|
||||
(unsigned int)device->id, entry->d_name);
|
||||
card->card_nr, entry->d_name);
|
||||
|
||||
spa_autoptr(DIR) pcm = opendir(path);
|
||||
if (pcm == NULL)
|
||||
|
|
@ -334,7 +390,7 @@ static int check_device_available(struct impl *this, struct device *device, int
|
|||
continue;
|
||||
|
||||
spa_scnprintf(path, sizeof(path), "/proc/asound/card%u/%s/%s/status",
|
||||
(unsigned int)device->id, entry->d_name, entry_pcm->d_name);
|
||||
card->card_nr, entry->d_name, entry_pcm->d_name);
|
||||
|
||||
spa_autoptr(FILE) f = fopen(path, "re");
|
||||
if (f == NULL)
|
||||
|
|
@ -344,12 +400,12 @@ static int check_device_available(struct impl *this, struct device *device, int
|
|||
|
||||
if (!spa_strstartswith(buf, "closed")) {
|
||||
spa_log_debug(this->log, "card %u pcm device %s busy",
|
||||
(unsigned int)device->id, entry->d_name);
|
||||
card->card_nr, entry->d_name);
|
||||
res = -EBUSY;
|
||||
goto done;
|
||||
}
|
||||
spa_log_debug(this->log, "card %u pcm device %s free",
|
||||
(unsigned int)device->id, entry->d_name);
|
||||
card->card_nr, entry->d_name);
|
||||
}
|
||||
if (errno != 0)
|
||||
goto done;
|
||||
|
|
@ -360,158 +416,249 @@ static int check_device_available(struct impl *this, struct device *device, int
|
|||
done:
|
||||
if (errno != 0) {
|
||||
spa_log_info(this->log, "card %u: failed to find busy status (%s)",
|
||||
(unsigned int)device->id, spa_strerror(-errno));
|
||||
card->card_nr, spa_strerror(-errno));
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static int emit_object_info(struct impl *this, struct device *device)
|
||||
static int check_compress_offload_device_availability(struct impl *this, struct card *card,
|
||||
int *num_compress_offload_devices)
|
||||
{
|
||||
struct spa_device_object_info info;
|
||||
uint32_t id = device->id;
|
||||
struct udev_device *dev = device->dev;
|
||||
int res;
|
||||
|
||||
res = get_num_compress_offload_devices(card->card_nr);
|
||||
if (res < 0) {
|
||||
spa_log_error(this->log, "Error finding Compress-Offload devices for ALSA card %u: %s",
|
||||
card->card_nr, spa_strerror(res));
|
||||
return res;
|
||||
}
|
||||
*num_compress_offload_devices = res;
|
||||
|
||||
spa_log_debug(this->log, "card %u has %d Compress-Offload device(s)",
|
||||
card->card_nr, *num_compress_offload_devices);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int emit_added_object_info(struct impl *this, struct card *card)
|
||||
{
|
||||
char path[32];
|
||||
int res, num_pcm_devices, num_compress_offload_devices;
|
||||
const char *str;
|
||||
char path[32], *cn = NULL, *cln = NULL;
|
||||
struct spa_dict_item items[25];
|
||||
uint32_t n_items = 0;
|
||||
int res, pcm;
|
||||
struct udev_device *udev_device = card->udev_device;
|
||||
|
||||
/*
|
||||
* inotify close events under /dev/snd must not be emitted, except after setting
|
||||
* device->emitted to true. alsalib functions can be used after that.
|
||||
* card->emitted to true. alsalib functions can be used after that.
|
||||
*/
|
||||
|
||||
snprintf(path, sizeof(path), "hw:%u", id);
|
||||
snprintf(path, sizeof(path), "hw:%u", card->card_nr);
|
||||
|
||||
if ((res = check_device_available(this, device, &pcm)) < 0)
|
||||
if ((res = check_pcm_device_availability(this, card, &num_pcm_devices)) < 0)
|
||||
return res;
|
||||
if (pcm == 0) {
|
||||
spa_log_debug(this->log, "no pcm devices for %s", path);
|
||||
device->ignored = true;
|
||||
if ((res = check_compress_offload_device_availability(this, card, &num_compress_offload_devices)) < 0)
|
||||
return res;
|
||||
|
||||
if ((num_pcm_devices == 0) && (num_compress_offload_devices == 0)) {
|
||||
spa_log_debug(this->log, "no PCM and no Compress-Offload devices for %s", path);
|
||||
card->ignored = true;
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
spa_log_debug(this->log, "emitting card %s", path);
|
||||
device->emitted = true;
|
||||
card->emitted = true;
|
||||
|
||||
info = SPA_DEVICE_OBJECT_INFO_INIT();
|
||||
if (num_pcm_devices > 0) {
|
||||
struct spa_device_object_info info;
|
||||
char *cn = NULL, *cln = NULL;
|
||||
struct spa_dict_item items[25];
|
||||
unsigned int n_items = 0;
|
||||
|
||||
info.type = SPA_TYPE_INTERFACE_Device;
|
||||
info.factory_name = this->use_acp ?
|
||||
SPA_NAME_API_ALSA_ACP_DEVICE :
|
||||
SPA_NAME_API_ALSA_PCM_DEVICE;
|
||||
info.change_mask = SPA_DEVICE_OBJECT_CHANGE_MASK_FLAGS |
|
||||
SPA_DEVICE_OBJECT_CHANGE_MASK_PROPS;
|
||||
info.flags = 0;
|
||||
card->pcm_device_id = calc_pcm_device_id(card);
|
||||
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_ENUM_API, "udev");
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_API, "alsa");
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_MEDIA_CLASS, "Audio/Device");
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_API_ALSA_PATH, path);
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_API_ALSA_CARD, path+3);
|
||||
if (snd_card_get_name(id, &cn) >= 0)
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_API_ALSA_CARD_NAME, cn);
|
||||
if (snd_card_get_longname(id, &cln) >= 0)
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_API_ALSA_CARD_LONGNAME, cln);
|
||||
spa_log_debug(this->log, "emitting ACP/PCM device interface for card %s; "
|
||||
"using local alsa-udev object ID %" PRIu32, path, card->pcm_device_id);
|
||||
|
||||
if ((str = udev_device_get_property_value(dev, "ACP_NAME")) && *str)
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_NAME, str);
|
||||
info = SPA_DEVICE_OBJECT_INFO_INIT();
|
||||
|
||||
if ((str = udev_device_get_property_value(dev, "ACP_PROFILE_SET")) && *str)
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_PROFILE_SET, str);
|
||||
info.type = SPA_TYPE_INTERFACE_Device;
|
||||
info.factory_name = this->use_acp ?
|
||||
SPA_NAME_API_ALSA_ACP_DEVICE :
|
||||
SPA_NAME_API_ALSA_PCM_DEVICE;
|
||||
info.change_mask = SPA_DEVICE_OBJECT_CHANGE_MASK_FLAGS |
|
||||
SPA_DEVICE_OBJECT_CHANGE_MASK_PROPS;
|
||||
info.flags = 0;
|
||||
|
||||
if ((str = udev_device_get_property_value(dev, "SOUND_CLASS")) && *str)
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_CLASS, str);
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_ENUM_API, "udev");
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_API, "alsa");
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_MEDIA_CLASS, "Audio/Device");
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_API_ALSA_PATH, path);
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_API_ALSA_CARD, path+3);
|
||||
if (snd_card_get_name(card->card_nr, &cn) >= 0)
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_API_ALSA_CARD_NAME, cn);
|
||||
if (snd_card_get_longname(card->card_nr, &cln) >= 0)
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_API_ALSA_CARD_LONGNAME, cln);
|
||||
|
||||
if ((str = udev_device_get_property_value(dev, "USEC_INITIALIZED")) && *str)
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_PLUGGED_USEC, str);
|
||||
if ((str = udev_device_get_property_value(udev_device, "ACP_NAME")) && *str)
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_NAME, str);
|
||||
|
||||
str = udev_device_get_property_value(dev, "ID_PATH");
|
||||
if (!(str && *str))
|
||||
str = udev_device_get_syspath(dev);
|
||||
if (str && *str) {
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_BUS_PATH, str);
|
||||
}
|
||||
if ((str = udev_device_get_devpath(dev)) && *str) {
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_SYSFS_PATH, str);
|
||||
}
|
||||
if ((str = udev_device_get_property_value(dev, "ID_ID")) && *str) {
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_BUS_ID, str);
|
||||
}
|
||||
if ((str = udev_device_get_property_value(dev, "ID_BUS")) && *str) {
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_BUS, str);
|
||||
}
|
||||
if ((str = udev_device_get_property_value(dev, "SUBSYSTEM")) && *str) {
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_SUBSYSTEM, str);
|
||||
}
|
||||
if ((str = udev_device_get_property_value(dev, "ID_VENDOR_ID")) && *str) {
|
||||
int32_t val;
|
||||
if (spa_atoi32(str, &val, 16)) {
|
||||
char *dec = alloca(12); /* 0xffffffff is max */
|
||||
snprintf(dec, 12, "0x%04x", val);
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_VENDOR_ID, dec);
|
||||
if ((str = udev_device_get_property_value(udev_device, "ACP_PROFILE_SET")) && *str)
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_PROFILE_SET, str);
|
||||
|
||||
if ((str = udev_device_get_property_value(udev_device, "SOUND_CLASS")) && *str)
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_CLASS, str);
|
||||
|
||||
if ((str = udev_device_get_property_value(udev_device, "USEC_INITIALIZED")) && *str)
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_PLUGGED_USEC, str);
|
||||
|
||||
str = udev_device_get_property_value(udev_device, "ID_PATH");
|
||||
if (!(str && *str))
|
||||
str = udev_device_get_syspath(udev_device);
|
||||
if (str && *str) {
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_BUS_PATH, str);
|
||||
}
|
||||
}
|
||||
str = udev_device_get_property_value(dev, "ID_VENDOR_FROM_DATABASE");
|
||||
if (!(str && *str)) {
|
||||
str = udev_device_get_property_value(dev, "ID_VENDOR_ENC");
|
||||
if ((str = udev_device_get_devpath(udev_device)) && *str) {
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_SYSFS_PATH, str);
|
||||
}
|
||||
if ((str = udev_device_get_property_value(udev_device, "ID_ID")) && *str) {
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_BUS_ID, str);
|
||||
}
|
||||
if ((str = udev_device_get_property_value(udev_device, "ID_BUS")) && *str) {
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_BUS, str);
|
||||
}
|
||||
if ((str = udev_device_get_property_value(udev_device, "SUBSYSTEM")) && *str) {
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_SUBSYSTEM, str);
|
||||
}
|
||||
if ((str = udev_device_get_property_value(udev_device, "ID_VENDOR_ID")) && *str) {
|
||||
int32_t val;
|
||||
if (spa_atoi32(str, &val, 16)) {
|
||||
char *dec = alloca(12); /* 0xffffffff is max */
|
||||
snprintf(dec, 12, "0x%04x", val);
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_VENDOR_ID, dec);
|
||||
}
|
||||
}
|
||||
str = udev_device_get_property_value(udev_device, "ID_VENDOR_FROM_DATABASE");
|
||||
if (!(str && *str)) {
|
||||
str = udev_device_get_property_value(dev, "ID_VENDOR");
|
||||
} else {
|
||||
char *t = alloca(strlen(str) + 1);
|
||||
unescape(str, t);
|
||||
str = t;
|
||||
str = udev_device_get_property_value(udev_device, "ID_VENDOR_ENC");
|
||||
if (!(str && *str)) {
|
||||
str = udev_device_get_property_value(udev_device, "ID_VENDOR");
|
||||
} else {
|
||||
char *t = alloca(strlen(str) + 1);
|
||||
unescape(str, t);
|
||||
str = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (str && *str) {
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_VENDOR_NAME, str);
|
||||
}
|
||||
if ((str = udev_device_get_property_value(dev, "ID_MODEL_ID")) && *str) {
|
||||
int32_t val;
|
||||
if (spa_atoi32(str, &val, 16)) {
|
||||
char *dec = alloca(12); /* 0xffffffff is max */
|
||||
snprintf(dec, 12, "0x%04x", val);
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_PRODUCT_ID, dec);
|
||||
if (str && *str) {
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_VENDOR_NAME, str);
|
||||
}
|
||||
}
|
||||
str = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE");
|
||||
if (!(str && *str)) {
|
||||
str = udev_device_get_property_value(dev, "ID_MODEL_ENC");
|
||||
if ((str = udev_device_get_property_value(udev_device, "ID_MODEL_ID")) && *str) {
|
||||
int32_t val;
|
||||
if (spa_atoi32(str, &val, 16)) {
|
||||
char *dec = alloca(12); /* 0xffffffff is max */
|
||||
snprintf(dec, 12, "0x%04x", val);
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_PRODUCT_ID, dec);
|
||||
}
|
||||
}
|
||||
str = udev_device_get_property_value(udev_device, "ID_MODEL_FROM_DATABASE");
|
||||
if (!(str && *str)) {
|
||||
str = udev_device_get_property_value(dev, "ID_MODEL");
|
||||
} else {
|
||||
char *t = alloca(strlen(str) + 1);
|
||||
unescape(str, t);
|
||||
str = t;
|
||||
str = udev_device_get_property_value(udev_device, "ID_MODEL_ENC");
|
||||
if (!(str && *str)) {
|
||||
str = udev_device_get_property_value(udev_device, "ID_MODEL");
|
||||
} else {
|
||||
char *t = alloca(strlen(str) + 1);
|
||||
unescape(str, t);
|
||||
str = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (str && *str)
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_PRODUCT_NAME, str);
|
||||
if (str && *str)
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_PRODUCT_NAME, str);
|
||||
|
||||
if ((str = udev_device_get_property_value(dev, "ID_SERIAL")) && *str) {
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_SERIAL, str);
|
||||
}
|
||||
if ((str = udev_device_get_property_value(dev, "SOUND_FORM_FACTOR")) && *str) {
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_FORM_FACTOR, str);
|
||||
}
|
||||
info.props = &SPA_DICT_INIT(items, n_items);
|
||||
if ((str = udev_device_get_property_value(udev_device, "ID_SERIAL")) && *str) {
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_SERIAL, str);
|
||||
}
|
||||
if ((str = udev_device_get_property_value(udev_device, "SOUND_FORM_FACTOR")) && *str) {
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_FORM_FACTOR, str);
|
||||
}
|
||||
info.props = &SPA_DICT_INIT(items, n_items);
|
||||
|
||||
spa_device_emit_object_info(&this->hooks, id, &info);
|
||||
free(cn);
|
||||
free(cln);
|
||||
spa_log_debug(this->log, "interface information:");
|
||||
spa_debug_dict(2, info.props);
|
||||
|
||||
spa_device_emit_object_info(&this->hooks, card->pcm_device_id, &info);
|
||||
free(cn);
|
||||
free(cln);
|
||||
} else {
|
||||
card->pcm_device_id = ID_DEVICE_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
if (num_compress_offload_devices > 0) {
|
||||
struct spa_device_object_info info;
|
||||
struct spa_dict_item items[11];
|
||||
unsigned int n_items = 0;
|
||||
char device_name[200];
|
||||
char device_desc[200];
|
||||
|
||||
card->compress_offload_device_id = calc_compress_offload_device_id(card);
|
||||
|
||||
spa_log_debug(this->log, "emitting Compress-Offload device interface for card %s; "
|
||||
"using local alsa-udev object ID %" PRIu32, path, card->compress_offload_device_id);
|
||||
|
||||
info = SPA_DEVICE_OBJECT_INFO_INIT();
|
||||
|
||||
info.type = SPA_TYPE_INTERFACE_Device;
|
||||
info.factory_name = SPA_NAME_API_ALSA_COMPRESS_OFFLOAD_DEVICE;
|
||||
info.change_mask = SPA_DEVICE_OBJECT_CHANGE_MASK_FLAGS |
|
||||
SPA_DEVICE_OBJECT_CHANGE_MASK_PROPS;
|
||||
info.flags = 0;
|
||||
|
||||
snprintf(device_name, sizeof(device_name), "comprC%u", card->card_nr);
|
||||
snprintf(device_desc, sizeof(device_desc), "Compress-Offload device (ALSA card %u)", card->card_nr);
|
||||
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_ENUM_API, "udev");
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_API, "alsa:compressed");
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_NAME, device_name);
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_DESCRIPTION, device_desc);
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_MEDIA_CLASS, "Audio/Device");
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_API_ALSA_PATH, path);
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_API_ALSA_CARD, path+3);
|
||||
|
||||
if ((str = udev_device_get_property_value(udev_device, "USEC_INITIALIZED")) && *str)
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_PLUGGED_USEC, str);
|
||||
|
||||
str = udev_device_get_property_value(udev_device, "ID_PATH");
|
||||
if (!(str && *str))
|
||||
str = udev_device_get_syspath(udev_device);
|
||||
if (str && *str) {
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_BUS_PATH, str);
|
||||
}
|
||||
if ((str = udev_device_get_devpath(udev_device)) && *str) {
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_SYSFS_PATH, str);
|
||||
}
|
||||
if ((str = udev_device_get_property_value(udev_device, "SUBSYSTEM")) && *str) {
|
||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_SUBSYSTEM, str);
|
||||
}
|
||||
|
||||
info.props = &SPA_DICT_INIT(items, n_items);
|
||||
|
||||
spa_log_debug(this->log, "interface information:");
|
||||
spa_debug_dict(2, info.props);
|
||||
|
||||
spa_device_emit_object_info(&this->hooks, card->compress_offload_device_id, &info);
|
||||
} else {
|
||||
card->compress_offload_device_id = ID_DEVICE_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static bool check_access(struct impl *this, struct device *device)
|
||||
static bool check_access(struct impl *this, struct card *card)
|
||||
{
|
||||
char path[128], prefix[32];
|
||||
char path[128], pcm_prefix[32], compr_prefix[32];;
|
||||
spa_autoptr(DIR) snd = NULL;
|
||||
struct dirent *entry;
|
||||
bool accessible = false;
|
||||
|
||||
snprintf(path, sizeof(path), "/dev/snd/controlC%u", device->id);
|
||||
snprintf(path, sizeof(path), "/dev/snd/controlC%u", card->card_nr);
|
||||
if (access(path, R_OK|W_OK) >= 0 && (snd = opendir("/dev/snd"))) {
|
||||
/*
|
||||
* It's possible that controlCX is accessible before pcmCX* or
|
||||
|
|
@ -520,10 +667,12 @@ static bool check_access(struct impl *this, struct device *device)
|
|||
*/
|
||||
|
||||
accessible = true;
|
||||
spa_scnprintf(prefix, sizeof(prefix), "pcmC%uD", device->id);
|
||||
spa_scnprintf(pcm_prefix, sizeof(pcm_prefix), "pcmC%uD", card->card_nr);
|
||||
spa_scnprintf(compr_prefix, sizeof(compr_prefix), "comprC%uD", card->card_nr);
|
||||
while ((entry = readdir(snd)) != NULL) {
|
||||
if (!(entry->d_type == DT_CHR &&
|
||||
spa_strstartswith(entry->d_name, prefix)))
|
||||
(spa_strstartswith(entry->d_name, pcm_prefix) ||
|
||||
spa_strstartswith(entry->d_name, compr_prefix))))
|
||||
continue;
|
||||
|
||||
snprintf(path, sizeof(path), "/dev/snd/%.32s", entry->d_name);
|
||||
|
|
@ -534,70 +683,90 @@ static bool check_access(struct impl *this, struct device *device)
|
|||
}
|
||||
}
|
||||
|
||||
if (accessible != device->accessible)
|
||||
if (accessible != card->accessible)
|
||||
spa_log_debug(this->log, "%s accessible:%u", path, accessible);
|
||||
device->accessible = accessible;
|
||||
card->accessible = accessible;
|
||||
|
||||
return device->accessible;
|
||||
return card->accessible;
|
||||
}
|
||||
|
||||
static void process_device(struct impl *this, uint32_t action, struct udev_device *dev)
|
||||
static void process_card(struct impl *this, uint32_t action, struct udev_device *udev_device)
|
||||
{
|
||||
uint32_t id;
|
||||
struct device *device;
|
||||
unsigned int card_nr;
|
||||
struct card *card;
|
||||
bool emitted;
|
||||
int res;
|
||||
|
||||
if ((id = get_card_id(this, dev)) == SPA_ID_INVALID)
|
||||
if ((card_nr = get_card_nr(this, udev_device)) == SPA_ID_INVALID)
|
||||
return;
|
||||
|
||||
device = find_device(this, id);
|
||||
if (device && device->ignored)
|
||||
card = find_card(this, card_nr);
|
||||
if (card && card->ignored)
|
||||
return;
|
||||
|
||||
switch (action) {
|
||||
case ACTION_ADD:
|
||||
if (device == NULL)
|
||||
device = add_device(this, id, dev);
|
||||
if (device == NULL)
|
||||
if (card == NULL)
|
||||
card = add_card(this, card_nr, udev_device);
|
||||
if (card == NULL)
|
||||
return;
|
||||
if (!check_access(this, device))
|
||||
if (!check_access(this, card))
|
||||
return;
|
||||
res = emit_object_info(this, device);
|
||||
res = emit_added_object_info(this, card);
|
||||
if (res < 0) {
|
||||
if (device->ignored)
|
||||
if (card->ignored)
|
||||
spa_log_info(this->log, "ALSA card %u unavailable (%s): it is ignored",
|
||||
device->id, spa_strerror(res));
|
||||
else if (!device->unavailable)
|
||||
card->card_nr, spa_strerror(res));
|
||||
else if (!card->unavailable)
|
||||
spa_log_info(this->log, "ALSA card %u unavailable (%s): wait for it",
|
||||
device->id, spa_strerror(res));
|
||||
card->card_nr, spa_strerror(res));
|
||||
else
|
||||
spa_log_debug(this->log, "ALSA card %u still unavailable (%s)",
|
||||
device->id, spa_strerror(res));
|
||||
device->unavailable = true;
|
||||
card->card_nr, spa_strerror(res));
|
||||
card->unavailable = true;
|
||||
} else {
|
||||
if (device->unavailable)
|
||||
if (card->unavailable)
|
||||
spa_log_info(this->log, "ALSA card %u now available",
|
||||
device->id);
|
||||
device->unavailable = false;
|
||||
card->card_nr);
|
||||
card->unavailable = false;
|
||||
}
|
||||
break;
|
||||
|
||||
case ACTION_REMOVE:
|
||||
if (device == NULL)
|
||||
case ACTION_REMOVE: {
|
||||
uint32_t pcm_device_id, compress_offload_device_id;
|
||||
|
||||
if (card == NULL)
|
||||
return;
|
||||
emitted = device->emitted;
|
||||
remove_device(this, device);
|
||||
if (emitted)
|
||||
spa_device_emit_object_info(&this->hooks, id, NULL);
|
||||
|
||||
emitted = card->emitted;
|
||||
pcm_device_id = card->pcm_device_id;
|
||||
compress_offload_device_id = card->compress_offload_device_id;
|
||||
remove_card(this, card);
|
||||
|
||||
if (emitted) {
|
||||
if (pcm_device_id != ID_DEVICE_NOT_SUPPORTED)
|
||||
spa_device_emit_object_info(&this->hooks, pcm_device_id, NULL);
|
||||
if (compress_offload_device_id != ID_DEVICE_NOT_SUPPORTED)
|
||||
spa_device_emit_object_info(&this->hooks, compress_offload_device_id, NULL);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case ACTION_DISABLE:
|
||||
if (device == NULL)
|
||||
if (card == NULL)
|
||||
return;
|
||||
if (device->emitted) {
|
||||
device->emitted = false;
|
||||
spa_device_emit_object_info(&this->hooks, id, NULL);
|
||||
if (card->emitted) {
|
||||
uint32_t pcm_device_id, compress_offload_device_id;
|
||||
|
||||
pcm_device_id = card->pcm_device_id;
|
||||
compress_offload_device_id = card->compress_offload_device_id;
|
||||
|
||||
card->emitted = false;
|
||||
|
||||
if (pcm_device_id != ID_DEVICE_NOT_SUPPORTED)
|
||||
spa_device_emit_object_info(&this->hooks, pcm_device_id, NULL);
|
||||
if (compress_offload_device_id != ID_DEVICE_NOT_SUPPORTED)
|
||||
spa_device_emit_object_info(&this->hooks, compress_offload_device_id, NULL);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
@ -638,28 +807,28 @@ static void impl_on_notify_events(struct spa_source *source)
|
|||
|
||||
for (p = &buf; p < e;
|
||||
p = SPA_PTROFF(p, sizeof(struct inotify_event) + event->len, void)) {
|
||||
unsigned int id;
|
||||
struct device *device;
|
||||
unsigned int card_nr;
|
||||
struct card *card;
|
||||
|
||||
event = (const struct inotify_event *) p;
|
||||
spa_assert_se(SPA_PTRDIFF(e, p) >= (ptrdiff_t)sizeof(struct inotify_event) &&
|
||||
SPA_PTRDIFF(e, p) - sizeof(struct inotify_event) >= event->len &&
|
||||
"bad event from kernel");
|
||||
|
||||
/* Device becomes accessible or not busy */
|
||||
/* card becomes accessible or not busy */
|
||||
if ((event->mask & (IN_ATTRIB | IN_CLOSE_WRITE))) {
|
||||
bool access;
|
||||
if (sscanf(event->name, "controlC%u", &id) != 1 &&
|
||||
sscanf(event->name, "pcmC%uD", &id) != 1)
|
||||
if (sscanf(event->name, "controlC%u", &card_nr) != 1 &&
|
||||
sscanf(event->name, "pcmC%uD", &card_nr) != 1)
|
||||
continue;
|
||||
if ((device = find_device(this, id)) == NULL)
|
||||
if ((card = find_card(this, card_nr)) == NULL)
|
||||
continue;
|
||||
|
||||
access = check_access(this, device);
|
||||
if (access && !device->emitted)
|
||||
process_device(this, ACTION_ADD, device->dev);
|
||||
else if (!access && device->emitted)
|
||||
process_device(this, ACTION_DISABLE, device->dev);
|
||||
access = check_access(this, card);
|
||||
if (access && !card->emitted)
|
||||
process_card(this, ACTION_ADD, card->udev_device);
|
||||
else if (!access && card->emitted)
|
||||
process_card(this, ACTION_DISABLE, card->udev_device);
|
||||
}
|
||||
/* /dev/snd/ might have been removed */
|
||||
if ((event->mask & (IN_DELETE_SELF | IN_MOVE_SELF)))
|
||||
|
|
@ -707,14 +876,14 @@ static int start_inotify(struct impl *this)
|
|||
static void impl_on_fd_events(struct spa_source *source)
|
||||
{
|
||||
struct impl *this = source->data;
|
||||
struct udev_device *dev;
|
||||
struct udev_device *udev_device;
|
||||
const char *action;
|
||||
|
||||
dev = udev_monitor_receive_device(this->umonitor);
|
||||
if (dev == NULL)
|
||||
udev_device = udev_monitor_receive_device(this->umonitor);
|
||||
if (udev_device == NULL)
|
||||
return;
|
||||
|
||||
if ((action = udev_device_get_action(dev)) == NULL)
|
||||
if ((action = udev_device_get_action(udev_device)) == NULL)
|
||||
action = "change";
|
||||
|
||||
spa_log_debug(this->log, "action %s", action);
|
||||
|
|
@ -722,11 +891,11 @@ static void impl_on_fd_events(struct spa_source *source)
|
|||
start_inotify(this);
|
||||
|
||||
if (spa_streq(action, "change")) {
|
||||
process_device(this, ACTION_ADD, dev);
|
||||
process_card(this, ACTION_ADD, udev_device);
|
||||
} else if (spa_streq(action, "remove")) {
|
||||
process_device(this, ACTION_REMOVE, dev);
|
||||
process_card(this, ACTION_REMOVE, udev_device);
|
||||
}
|
||||
udev_device_unref(dev);
|
||||
udev_device_unref(udev_device);
|
||||
}
|
||||
|
||||
static int start_monitor(struct impl *this)
|
||||
|
|
@ -763,7 +932,7 @@ static int stop_monitor(struct impl *this)
|
|||
if (this->umonitor == NULL)
|
||||
return 0;
|
||||
|
||||
clear_devices (this);
|
||||
clear_cards (this);
|
||||
|
||||
spa_loop_remove_source(this->main_loop, &this->source);
|
||||
udev_monitor_unref(this->umonitor);
|
||||
|
|
@ -774,10 +943,10 @@ static int stop_monitor(struct impl *this)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int enum_devices(struct impl *this)
|
||||
static int enum_cards(struct impl *this)
|
||||
{
|
||||
struct udev_enumerate *enumerate;
|
||||
struct udev_list_entry *devices;
|
||||
struct udev_list_entry *udev_devices;
|
||||
|
||||
enumerate = udev_enumerate_new(this->udev);
|
||||
if (enumerate == NULL)
|
||||
|
|
@ -786,17 +955,18 @@ static int enum_devices(struct impl *this)
|
|||
udev_enumerate_add_match_subsystem(enumerate, "sound");
|
||||
udev_enumerate_scan_devices(enumerate);
|
||||
|
||||
for (devices = udev_enumerate_get_list_entry(enumerate); devices;
|
||||
devices = udev_list_entry_get_next(devices)) {
|
||||
struct udev_device *dev;
|
||||
for (udev_devices = udev_enumerate_get_list_entry(enumerate); udev_devices;
|
||||
udev_devices = udev_list_entry_get_next(udev_devices)) {
|
||||
struct udev_device *udev_device;
|
||||
|
||||
dev = udev_device_new_from_syspath(this->udev, udev_list_entry_get_name(devices));
|
||||
if (dev == NULL)
|
||||
udev_device = udev_device_new_from_syspath(this->udev,
|
||||
udev_list_entry_get_name(udev_devices));
|
||||
if (udev_device == NULL)
|
||||
continue;
|
||||
|
||||
process_device(this, ACTION_ADD, dev);
|
||||
process_card(this, ACTION_ADD, udev_device);
|
||||
|
||||
udev_device_unref(dev);
|
||||
udev_device_unref(udev_device);
|
||||
}
|
||||
udev_enumerate_unref(enumerate);
|
||||
|
||||
|
|
@ -851,7 +1021,7 @@ impl_device_add_listener(void *object, struct spa_hook *listener,
|
|||
if ((res = start_monitor(this)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = enum_devices(this)) < 0)
|
||||
if ((res = enum_cards(this)) < 0)
|
||||
return res;
|
||||
|
||||
spa_hook_list_join(&this->hooks, &save);
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ extern const struct spa_handle_factory spa_alsa_seq_bridge_factory;
|
|||
extern const struct spa_handle_factory spa_alsa_acp_device_factory;
|
||||
#ifdef HAVE_ALSA_COMPRESS_OFFLOAD
|
||||
extern const struct spa_handle_factory spa_alsa_compress_offload_sink_factory;
|
||||
extern const struct spa_handle_factory spa_alsa_compress_offload_device_factory;
|
||||
#endif
|
||||
|
||||
struct spa_log_topic log_topic = SPA_LOG_TOPIC(0, "spa.alsa");
|
||||
|
|
@ -51,6 +52,9 @@ int spa_handle_factory_enum(const struct spa_handle_factory **factory, uint32_t
|
|||
case 6:
|
||||
*factory = &spa_alsa_compress_offload_sink_factory;
|
||||
break;
|
||||
case 7:
|
||||
*factory = &spa_alsa_compress_offload_device_factory;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
return 0;
|
||||
|
|
|
|||
36
spa/plugins/alsa/compress-offload-api-util.c
Normal file
36
spa/plugins/alsa/compress-offload-api-util.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include "compress-offload-api.h"
|
||||
#include "compress-offload-api-util.h"
|
||||
|
||||
int get_compress_offload_device_direction(int card_nr, int device_nr,
|
||||
struct spa_log *log,
|
||||
enum spa_compress_offload_direction *direction)
|
||||
{
|
||||
int ret = 0;
|
||||
struct compress_offload_api_context *device_context;
|
||||
const struct snd_compr_caps *compr_caps;
|
||||
|
||||
device_context = compress_offload_api_open(card_nr, device_nr, log);
|
||||
if (device_context == NULL)
|
||||
return -errno;
|
||||
|
||||
compr_caps = compress_offload_api_get_caps(device_context);
|
||||
|
||||
switch (compr_caps->direction) {
|
||||
case SND_COMPRESS_PLAYBACK:
|
||||
*direction = SPA_COMPRESS_OFFLOAD_DIRECTION_PLAYBACK;
|
||||
break;
|
||||
case SND_COMPRESS_CAPTURE:
|
||||
*direction = SPA_COMPRESS_OFFLOAD_DIRECTION_CAPTURE;
|
||||
break;
|
||||
default:
|
||||
spa_log_error(log, "card nr %d device nr %d: unknown direction %#" PRIx32,
|
||||
card_nr, device_nr, (uint32_t)(compr_caps->direction));
|
||||
ret = -EINVAL;
|
||||
}
|
||||
|
||||
compress_offload_api_close(device_context);
|
||||
|
||||
return ret;
|
||||
}
|
||||
30
spa/plugins/alsa/compress-offload-api-util.h
Normal file
30
spa/plugins/alsa/compress-offload-api-util.h
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#ifndef SPA_ALSA_COMPRESS_OFFLOAD_DEVICE_UTIL_H
|
||||
#define SPA_ALSA_COMPRESS_OFFLOAD_DEVICE_UTIL_H
|
||||
|
||||
#include <spa/support/log.h>
|
||||
|
||||
#if defined(__GNUC__) && __GNUC__ >= 4
|
||||
#define COMPR_API_PRIVATE __attribute__((visibility("hidden")))
|
||||
#elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590)
|
||||
#define COMPR_API_PRIVATE __attribute__((visibility("hidden")))
|
||||
#elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
|
||||
#define COMPR_API_PRIVATE __hidden
|
||||
#else
|
||||
#define COMPR_API_PRIVATE
|
||||
#endif
|
||||
|
||||
enum spa_compress_offload_direction {
|
||||
SPA_COMPRESS_OFFLOAD_DIRECTION_PLAYBACK,
|
||||
SPA_COMPRESS_OFFLOAD_DIRECTION_CAPTURE
|
||||
};
|
||||
|
||||
/* This exists for situations where both the direction of the compress-offload
|
||||
* device and the functions from asoundlib.h are needed. It is not possible to
|
||||
* include asoundlib.h and the compress-offload headers in the same C file,
|
||||
* since these headers contain conflicting declarations. Provide this direction
|
||||
* check function to keep the compress-offload headers encapsulated. */
|
||||
COMPR_API_PRIVATE int get_compress_offload_device_direction(int card_nr, int device_nr,
|
||||
struct spa_log *log,
|
||||
enum spa_compress_offload_direction *direction);
|
||||
|
||||
#endif /* SPA_ALSA_COMPRESS_OFFLOAD_DEVICE_UTIL_H */
|
||||
|
|
@ -6,22 +6,12 @@
|
|||
#include <sound/compress_offload.h>
|
||||
#include <sound/compress_params.h>
|
||||
#include <spa/support/log.h>
|
||||
#include "compress-offload-api-util.h"
|
||||
|
||||
|
||||
struct compress_offload_api_context;
|
||||
|
||||
|
||||
#if defined(__GNUC__) && __GNUC__ >= 4
|
||||
#define COMPR_API_PRIVATE __attribute__((visibility("hidden")))
|
||||
#elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590)
|
||||
#define COMPR_API_PRIVATE __attribute__((visibility("hidden")))
|
||||
#elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
|
||||
#define COMPR_API_PRIVATE __hidden
|
||||
#else
|
||||
#define COMPR_API_PRIVATE
|
||||
#endif
|
||||
|
||||
|
||||
/* This is a simple encapsulation of the ALSA Compress-Offload API
|
||||
* and its ioctl calls. It is intentionally not using any PipeWire
|
||||
* or SPA headers to allow for porting it or extracting it as its
|
||||
|
|
|
|||
|
|
@ -15,7 +15,10 @@ spa_alsa_sources = ['alsa.c',
|
|||
'alsa-seq.c']
|
||||
|
||||
if compress_offload_option.allowed()
|
||||
spa_alsa_sources += [ 'alsa-compress-offload-sink.c', 'compress-offload-api.c' ]
|
||||
spa_alsa_sources += ['alsa-compress-offload-sink.c',
|
||||
'alsa-compress-offload-device.c',
|
||||
'compress-offload-api-util.c',
|
||||
'compress-offload-api.c']
|
||||
endif
|
||||
|
||||
spa_alsa = shared_library(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue