2023-05-28 15:49:09 +02:00
|
|
|
/* PipeWire */
|
|
|
|
|
/* SPDX-FileCopyrightText: Copyright © 2021 Wim Taymans */
|
|
|
|
|
/* SPDX-License-Identifier: MIT */
|
|
|
|
|
|
2025-05-30 10:15:49 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
|
|
#include <spa/utils/result.h>
|
|
|
|
|
#include <spa/utils/string.h>
|
|
|
|
|
#include <spa/utils/json.h>
|
|
|
|
|
#include <spa/debug/types.h>
|
|
|
|
|
#include <spa/pod/builder.h>
|
|
|
|
|
#include <spa/param/audio/format-utils.h>
|
|
|
|
|
#include <spa/param/latency-utils.h>
|
|
|
|
|
#include <spa/param/audio/raw.h>
|
2024-09-18 09:54:34 +02:00
|
|
|
#include <spa/param/audio/raw-json.h>
|
2024-07-26 15:20:14 +02:00
|
|
|
#include <spa/control/ump-utils.h>
|
2023-05-28 15:49:09 +02:00
|
|
|
|
|
|
|
|
#include <pipewire/impl.h>
|
|
|
|
|
#include <pipewire/i18n.h>
|
|
|
|
|
#include <pipewire/thread.h>
|
|
|
|
|
|
|
|
|
|
#include <libffado/ffado.h>
|
|
|
|
|
|
2023-11-18 15:11:03 +02:00
|
|
|
/** \page page_module_ffado_driver FFADO firewire audio driver
|
2023-05-28 15:49:09 +02:00
|
|
|
*
|
|
|
|
|
* The ffado-driver module provides a source or sink using the libffado library for
|
|
|
|
|
* reading and writing to firewire audio devices.
|
|
|
|
|
*
|
2023-11-20 00:23:03 +02:00
|
|
|
* ## Module Name
|
|
|
|
|
*
|
|
|
|
|
* `libpipewire-module-ffado-driver`
|
|
|
|
|
*
|
2023-05-28 15:49:09 +02:00
|
|
|
* ## Module Options
|
|
|
|
|
*
|
|
|
|
|
* - `driver.mode`: the driver mode, sink|source|duplex, default duplex
|
2023-09-16 19:38:35 +02:00
|
|
|
* - `ffado.devices`: array of devices to open, default "hw:0"
|
2024-07-31 16:22:11 +02:00
|
|
|
* - `ffado.period-size`: period size,default 1024. A value of 0 will use the graph duration.
|
2023-05-28 19:17:41 +02:00
|
|
|
* - `ffado.period-num`: period number,default 3
|
2024-07-31 16:22:11 +02:00
|
|
|
* - `ffado.sample-rate`: sample-rate, default 48000. A value of 0 will use the graph rate.
|
2023-05-28 19:17:41 +02:00
|
|
|
* - `ffado.slave-mode`: slave mode
|
|
|
|
|
* - `ffado.snoop-mode`: snoop mode
|
|
|
|
|
* - `ffado.verbose`: ffado verbose level
|
2024-04-03 09:30:38 +02:00
|
|
|
* - `ffado.rtprio`: ffado realtime priority, this is by default the PipeWire server
|
|
|
|
|
* priority + 5
|
|
|
|
|
* - `ffado.realtime`: ffado realtime mode. this requires correctly configured rlimits
|
|
|
|
|
* to acquire FIFO scheduling at the ffado.rtprio priority
|
2023-05-28 19:17:41 +02:00
|
|
|
* - `latency.internal.input`: extra input latency in frames
|
|
|
|
|
* - `latency.internal.output`: extra output latency in frames
|
2024-04-03 09:30:38 +02:00
|
|
|
* - `source.props`: Extra properties for the source filter
|
|
|
|
|
* - `sink.props`: Extra properties for the sink filter
|
2023-05-28 15:49:09 +02:00
|
|
|
*
|
|
|
|
|
* ## General options
|
|
|
|
|
*
|
|
|
|
|
* Options with well-known behavior.
|
|
|
|
|
*
|
|
|
|
|
* - \ref PW_KEY_REMOTE_NAME
|
|
|
|
|
* - \ref SPA_KEY_AUDIO_POSITION
|
|
|
|
|
* - \ref PW_KEY_NODE_NAME
|
|
|
|
|
* - \ref PW_KEY_NODE_DESCRIPTION
|
|
|
|
|
* - \ref PW_KEY_NODE_GROUP
|
|
|
|
|
* - \ref PW_KEY_NODE_VIRTUAL
|
|
|
|
|
* - \ref PW_KEY_MEDIA_CLASS
|
|
|
|
|
* - \ref PW_KEY_TARGET_OBJECT to specify the remote node.name or serial.id to link to
|
|
|
|
|
*
|
|
|
|
|
* ## Example configuration of a duplex sink/source
|
|
|
|
|
*
|
|
|
|
|
*\code{.unparsed}
|
2024-09-14 15:43:25 +03:00
|
|
|
* # ~/.config/pipewire/pipewire.conf.d/my-ffado-driver.conf
|
|
|
|
|
*
|
2023-05-28 15:49:09 +02:00
|
|
|
* context.modules = [
|
|
|
|
|
* { name = libpipewire-module-ffado-driver
|
|
|
|
|
* args = {
|
2023-05-28 19:17:41 +02:00
|
|
|
* #driver.mode = duplex
|
2023-09-16 19:38:35 +02:00
|
|
|
* #ffado.devices = [ "hw:0" ]
|
2023-05-28 19:17:41 +02:00
|
|
|
* #ffado.period-size = 1024
|
|
|
|
|
* #ffado.period-num = 3
|
|
|
|
|
* #ffado.sample-rate = 48000
|
|
|
|
|
* #ffado.slave-mode = false
|
|
|
|
|
* #ffado.snoop-mode = false
|
|
|
|
|
* #ffado.verbose = 0
|
2024-04-03 09:30:38 +02:00
|
|
|
* #ffado.rtprio = 65
|
|
|
|
|
* #ffado.realtime = true
|
2023-05-28 19:17:41 +02:00
|
|
|
* #latency.internal.input = 0
|
|
|
|
|
* #latency.internal.output = 0
|
|
|
|
|
* #audio.position = [ FL FR ]
|
2023-05-28 15:49:09 +02:00
|
|
|
* source.props = {
|
|
|
|
|
* # extra sink properties
|
|
|
|
|
* }
|
|
|
|
|
* sink.props = {
|
|
|
|
|
* # extra sink properties
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ]
|
|
|
|
|
*\endcode
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define NAME "ffado-driver"
|
|
|
|
|
|
|
|
|
|
PW_LOG_TOPIC_STATIC(mod_topic, "mod." NAME);
|
|
|
|
|
#define PW_LOG_TOPIC_DEFAULT mod_topic
|
|
|
|
|
|
|
|
|
|
#define MAX_PORTS 128
|
2024-04-03 09:30:38 +02:00
|
|
|
#define FFADO_RT_PRIORITY_PACKETIZER_RELATIVE 5
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2023-09-16 19:38:35 +02:00
|
|
|
#define DEFAULT_DEVICES "[ \"hw:0\" ]"
|
2023-05-28 19:17:41 +02:00
|
|
|
#define DEFAULT_PERIOD_SIZE 1024
|
|
|
|
|
#define DEFAULT_PERIOD_NUM 3
|
|
|
|
|
#define DEFAULT_SAMPLE_RATE 48000
|
|
|
|
|
#define DEFAULT_SLAVE_MODE false
|
|
|
|
|
#define DEFAULT_SNOOP_MODE false
|
|
|
|
|
#define DEFAULT_VERBOSE 0
|
2024-04-03 09:30:38 +02:00
|
|
|
#define DEFAULT_RTPRIO (RTPRIO_SERVER + FFADO_RT_PRIORITY_PACKETIZER_RELATIVE)
|
|
|
|
|
#define DEFAULT_REALTIME true
|
2023-05-28 19:17:41 +02:00
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
#define DEFAULT_POSITION "[ FL FR ]"
|
|
|
|
|
#define DEFAULT_MIDI_PORTS 1
|
|
|
|
|
|
2024-04-03 09:30:38 +02:00
|
|
|
#define MODULE_USAGE "( remote.name=<remote> ) " \
|
|
|
|
|
"( driver.mode=<sink|source|duplex, default duplex> ) " \
|
|
|
|
|
"( ffado.devices=<devices array, default "DEFAULT_DEVICES"> ) " \
|
|
|
|
|
"( ffado.period-size=<period size, default 1024> ) " \
|
|
|
|
|
"( ffado.period-num=<period num, default 3> ) " \
|
|
|
|
|
"( ffado.sample-rate=<sampe rate, default 48000> ) " \
|
|
|
|
|
"( ffado.slave-mode=<slave mode, default false> ) " \
|
|
|
|
|
"( ffado.snoop-mode=<snoop mode, default false> ) " \
|
|
|
|
|
"( ffado.verbose=<verbose level, default 0> ) " \
|
|
|
|
|
"( ffado.rtprio=<realtime priority, default "SPA_STRINGIFY(DEFAULT_RTPRIO)"> ) " \
|
|
|
|
|
"( ffado.realtime=<realtime mode, default true> ) " \
|
|
|
|
|
"( audio.position=<channel map> ) " \
|
|
|
|
|
"( source.props=<properties> ) " \
|
2023-05-28 15:49:09 +02:00
|
|
|
"( sink.props=<properties> ) "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const struct spa_dict_item module_props[] = {
|
|
|
|
|
{ PW_KEY_MODULE_AUTHOR, "Wim Taymans <wim.taymans@gmail.com>" },
|
|
|
|
|
{ PW_KEY_MODULE_DESCRIPTION, "Create an FFADO based driver" },
|
|
|
|
|
{ PW_KEY_MODULE_USAGE, MODULE_USAGE },
|
|
|
|
|
{ PW_KEY_MODULE_VERSION, PACKAGE_VERSION },
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-19 13:20:29 +01:00
|
|
|
struct port_data {
|
|
|
|
|
struct port *port;
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
struct port {
|
|
|
|
|
enum spa_direction direction;
|
2024-03-19 13:20:29 +01:00
|
|
|
ffado_streaming_stream_type stream_type;
|
|
|
|
|
char name[280];
|
|
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
struct spa_latency_info latency[2];
|
|
|
|
|
bool latency_changed[2];
|
|
|
|
|
unsigned int is_midi:1;
|
2024-03-07 18:11:01 +01:00
|
|
|
unsigned int cleared:1;
|
2023-05-28 15:49:09 +02:00
|
|
|
void *buffer;
|
2024-03-08 15:24:33 +01:00
|
|
|
|
|
|
|
|
uint8_t event_byte;
|
|
|
|
|
uint8_t event_type;
|
|
|
|
|
uint32_t event_time;
|
|
|
|
|
uint8_t event_buffer[512];
|
|
|
|
|
uint32_t event_pos;
|
|
|
|
|
int event_pending;
|
|
|
|
|
|
2024-03-19 13:20:29 +01:00
|
|
|
struct port_data *data;
|
2023-05-28 15:49:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct volume {
|
|
|
|
|
bool mute;
|
|
|
|
|
uint32_t n_volumes;
|
|
|
|
|
float volumes[SPA_AUDIO_MAX_CHANNELS];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct stream {
|
|
|
|
|
struct impl *impl;
|
|
|
|
|
|
|
|
|
|
enum spa_direction direction;
|
|
|
|
|
struct pw_properties *props;
|
|
|
|
|
struct pw_filter *filter;
|
|
|
|
|
struct spa_hook listener;
|
|
|
|
|
struct spa_audio_info_raw info;
|
|
|
|
|
uint32_t n_ports;
|
|
|
|
|
struct port *ports[MAX_PORTS];
|
|
|
|
|
struct volume volume;
|
|
|
|
|
|
2024-04-22 17:43:04 +02:00
|
|
|
unsigned int ready:1;
|
2023-05-28 15:49:09 +02:00
|
|
|
unsigned int running:1;
|
2024-08-01 17:16:37 +02:00
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
unsigned int transfered:1;
|
|
|
|
|
} rt;
|
2023-05-28 15:49:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct impl {
|
|
|
|
|
struct pw_context *context;
|
|
|
|
|
struct pw_loop *main_loop;
|
2024-03-06 17:50:42 +01:00
|
|
|
struct pw_loop *data_loop;
|
2023-05-28 15:49:09 +02:00
|
|
|
struct spa_system *system;
|
2024-03-06 17:50:42 +01:00
|
|
|
struct spa_source *ffado_timer;
|
2023-05-28 15:49:09 +02:00
|
|
|
|
|
|
|
|
ffado_device_info_t device_info;
|
|
|
|
|
ffado_options_t device_options;
|
|
|
|
|
ffado_device_t *dev;
|
|
|
|
|
|
|
|
|
|
#define MODE_SINK (1<<0)
|
|
|
|
|
#define MODE_SOURCE (1<<1)
|
|
|
|
|
#define MODE_DUPLEX (MODE_SINK|MODE_SOURCE)
|
|
|
|
|
uint32_t mode;
|
|
|
|
|
struct pw_properties *props;
|
|
|
|
|
|
|
|
|
|
struct pw_impl_module *module;
|
|
|
|
|
|
|
|
|
|
struct spa_hook module_listener;
|
|
|
|
|
|
|
|
|
|
struct pw_core *core;
|
|
|
|
|
struct spa_hook core_proxy_listener;
|
|
|
|
|
struct spa_hook core_listener;
|
|
|
|
|
|
2024-03-08 12:54:12 +01:00
|
|
|
uint32_t reset_work_id;
|
|
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
struct spa_io_position *position;
|
|
|
|
|
|
|
|
|
|
uint32_t latency[2];
|
|
|
|
|
|
|
|
|
|
struct stream source;
|
|
|
|
|
struct stream sink;
|
|
|
|
|
|
2023-05-28 19:17:41 +02:00
|
|
|
char *devices[FFADO_MAX_SPECSTRINGS];
|
|
|
|
|
uint32_t n_devices;
|
2023-05-28 15:49:09 +02:00
|
|
|
int32_t sample_rate;
|
|
|
|
|
int32_t period_size;
|
|
|
|
|
int32_t n_periods;
|
2023-05-28 19:17:41 +02:00
|
|
|
bool slave_mode;
|
|
|
|
|
bool snoop_mode;
|
|
|
|
|
uint32_t verbose;
|
2024-04-03 09:30:38 +02:00
|
|
|
int32_t rtprio;
|
|
|
|
|
bool realtime;
|
2023-05-28 19:17:41 +02:00
|
|
|
|
|
|
|
|
uint32_t input_latency;
|
|
|
|
|
uint32_t output_latency;
|
2023-05-28 15:49:09 +02:00
|
|
|
uint32_t quantum_limit;
|
|
|
|
|
|
2023-05-30 15:57:07 +02:00
|
|
|
uint32_t frame_time;
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2024-03-06 17:50:42 +01:00
|
|
|
unsigned int do_disconnect:1;
|
|
|
|
|
unsigned int fix_midi:1;
|
|
|
|
|
unsigned int started:1;
|
2024-08-01 17:17:05 +02:00
|
|
|
unsigned int freewheel:1;
|
2024-03-06 17:50:42 +01:00
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
pthread_t thread;
|
|
|
|
|
|
2024-08-01 17:16:37 +02:00
|
|
|
struct {
|
|
|
|
|
unsigned int done:1;
|
|
|
|
|
unsigned int triggered:1;
|
|
|
|
|
unsigned int new_xrun:1;
|
|
|
|
|
uint32_t pw_xrun;
|
|
|
|
|
uint32_t ffado_xrun;
|
|
|
|
|
} rt;
|
2023-05-28 15:49:09 +02:00
|
|
|
};
|
|
|
|
|
|
2024-03-19 13:20:29 +01:00
|
|
|
static int stop_ffado_device(struct impl *impl);
|
|
|
|
|
static int start_ffado_device(struct impl *impl);
|
2024-03-08 12:54:12 +01:00
|
|
|
static void schedule_reset_ffado_device(struct impl *impl);
|
|
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
static void reset_volume(struct volume *vol, uint32_t n_volumes)
|
|
|
|
|
{
|
|
|
|
|
uint32_t i;
|
|
|
|
|
vol->mute = false;
|
|
|
|
|
vol->n_volumes = n_volumes;
|
|
|
|
|
for (i = 0; i < n_volumes; i++)
|
|
|
|
|
vol->volumes[i] = 1.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void do_volume(float *dst, const float *src, struct volume *vol, uint32_t ch, uint32_t n_samples)
|
|
|
|
|
{
|
|
|
|
|
float v = vol->mute ? 0.0f : vol->volumes[ch];
|
|
|
|
|
|
|
|
|
|
if (v == 0.0f || src == NULL)
|
|
|
|
|
memset(dst, 0, n_samples * sizeof(float));
|
|
|
|
|
else if (v == 1.0f)
|
|
|
|
|
memcpy(dst, src, n_samples * sizeof(float));
|
|
|
|
|
else {
|
|
|
|
|
uint32_t i;
|
|
|
|
|
for (i = 0; i < n_samples; i++)
|
|
|
|
|
dst[i] = src[i] * v;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-07 18:11:01 +01:00
|
|
|
static void clear_port_buffer(struct port *p, uint32_t n_samples)
|
|
|
|
|
{
|
|
|
|
|
if (!p->cleared) {
|
2024-04-22 17:43:04 +02:00
|
|
|
if (p->buffer)
|
|
|
|
|
memset(p->buffer, 0, n_samples * sizeof(uint32_t));
|
2024-03-07 18:11:01 +01:00
|
|
|
p->cleared = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
static inline void fix_midi_event(uint8_t *data, size_t size)
|
|
|
|
|
{
|
|
|
|
|
/* fixup NoteOn with vel 0 */
|
|
|
|
|
if (size > 2 && (data[0] & 0xF0) == 0x90 && data[2] == 0x00) {
|
|
|
|
|
data[0] = 0x80 + (data[0] & 0x0F);
|
|
|
|
|
data[2] = 0x40;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 15:24:33 +01:00
|
|
|
static void midi_to_ffado(struct port *p, float *src, uint32_t n_samples)
|
2023-05-28 15:49:09 +02:00
|
|
|
{
|
2025-07-31 11:50:11 +02:00
|
|
|
struct spa_pod_parser parser;
|
|
|
|
|
struct spa_pod_frame frame;
|
|
|
|
|
struct spa_pod_sequence seq;
|
|
|
|
|
struct spa_pod_control c;
|
|
|
|
|
const void *seq_body, *c_body;
|
2024-03-08 12:54:12 +01:00
|
|
|
uint32_t i, index = 0, unhandled = 0;
|
2024-03-07 18:11:01 +01:00
|
|
|
uint32_t *dst = p->buffer;
|
2023-05-28 15:49:09 +02:00
|
|
|
|
|
|
|
|
if (src == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
2025-07-31 11:50:11 +02:00
|
|
|
spa_pod_parser_init_from_data(&parser, src, n_samples * sizeof(float),
|
|
|
|
|
0, n_samples * sizeof(float));
|
|
|
|
|
if (spa_pod_parser_push_sequence_body(&parser, &frame, &seq, &seq_body) < 0)
|
2023-05-28 15:49:09 +02:00
|
|
|
return;
|
|
|
|
|
|
2024-03-07 18:11:01 +01:00
|
|
|
clear_port_buffer(p, n_samples);
|
|
|
|
|
|
2024-03-08 12:54:12 +01:00
|
|
|
/* first leftovers from previous cycle, always start at offset 0 */
|
2024-03-08 15:24:33 +01:00
|
|
|
for (i = 0; i < p->event_pos; i++) {
|
|
|
|
|
dst[index] = 0x01000000 | (uint32_t) p->event_buffer[i];
|
2024-03-08 12:54:12 +01:00
|
|
|
index += 8;
|
|
|
|
|
}
|
2024-03-08 15:24:33 +01:00
|
|
|
p->event_pos = 0;
|
2024-03-08 12:54:12 +01:00
|
|
|
|
2025-07-31 11:50:11 +02:00
|
|
|
while (spa_pod_parser_get_control_body(&parser, &c, &c_body) >= 0) {
|
2024-07-26 15:20:14 +02:00
|
|
|
uint8_t data[16];
|
|
|
|
|
int j, size;
|
2025-08-19 17:41:03 +02:00
|
|
|
size_t c_size = c.value.size;
|
|
|
|
|
uint64_t state = 0;
|
2024-07-26 15:20:14 +02:00
|
|
|
|
2025-07-31 11:50:11 +02:00
|
|
|
if (c.type != SPA_CONTROL_UMP)
|
2024-07-26 15:20:14 +02:00
|
|
|
continue;
|
|
|
|
|
|
2025-07-31 11:50:11 +02:00
|
|
|
if (index < c.offset)
|
|
|
|
|
index = SPA_ROUND_UP_N(c.offset, 8);
|
2025-08-19 17:41:03 +02:00
|
|
|
|
|
|
|
|
while (c_size > 0) {
|
|
|
|
|
size = spa_ump_to_midi((const uint32_t**)&c_body, &c_size, data, sizeof(data), &state);
|
|
|
|
|
if (size <= 0)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
for (j = 0; j < size; j++) {
|
|
|
|
|
if (index >= n_samples) {
|
|
|
|
|
/* keep events that don't fit for the next cycle */
|
|
|
|
|
if (p->event_pos < sizeof(p->event_buffer))
|
|
|
|
|
p->event_buffer[p->event_pos++] = data[j];
|
|
|
|
|
else
|
|
|
|
|
unhandled++;
|
|
|
|
|
}
|
2024-03-08 12:54:12 +01:00
|
|
|
else
|
2025-08-19 17:41:03 +02:00
|
|
|
dst[index] = 0x01000000 | (uint32_t) data[j];
|
|
|
|
|
index += 8;
|
2024-03-07 18:11:01 +01:00
|
|
|
}
|
2023-05-28 15:49:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-03-08 12:54:12 +01:00
|
|
|
if (unhandled > 0)
|
|
|
|
|
pw_log_warn("%u MIDI events dropped (index %d)", unhandled, index);
|
2024-03-08 15:24:33 +01:00
|
|
|
else if (p->event_pos > 0)
|
|
|
|
|
pw_log_debug("%u MIDI events saved (index %d)", p->event_pos, index);
|
2023-05-28 15:49:09 +02:00
|
|
|
}
|
|
|
|
|
|
2024-03-08 15:24:33 +01:00
|
|
|
static int take_bytes(struct port *p, uint32_t *frame, uint8_t **bytes, size_t *size)
|
2024-03-07 18:11:01 +01:00
|
|
|
{
|
2024-03-08 15:24:33 +01:00
|
|
|
if (p->event_pos == 0)
|
2024-03-07 18:11:01 +01:00
|
|
|
return 0;
|
2024-03-08 15:24:33 +01:00
|
|
|
*frame = p->event_time;
|
|
|
|
|
*bytes = p->event_buffer;
|
|
|
|
|
*size = p->event_pos;
|
2024-03-07 18:11:01 +01:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const int status_len[] = {
|
|
|
|
|
2, /* noteoff */
|
|
|
|
|
2, /* noteon */
|
|
|
|
|
2, /* keypress */
|
|
|
|
|
2, /* controller */
|
|
|
|
|
1, /* pgmchange */
|
|
|
|
|
1, /* chanpress */
|
|
|
|
|
2, /* pitchbend */
|
|
|
|
|
-1, /* invalid */
|
|
|
|
|
1, /* sysex 0xf0 */
|
|
|
|
|
1, /* qframe 0xf1 */
|
|
|
|
|
2, /* songpos 0xf2 */
|
|
|
|
|
1, /* songsel 0xf3 */
|
|
|
|
|
-1, /* none 0xf4 */
|
|
|
|
|
-1, /* none 0xf5 */
|
|
|
|
|
0, /* tune request 0xf6 */
|
|
|
|
|
-1, /* none 0xf7 */
|
|
|
|
|
0, /* clock 0xf8 */
|
|
|
|
|
-1, /* none 0xf9 */
|
|
|
|
|
0, /* start 0xfa */
|
|
|
|
|
0, /* continue 0xfb */
|
|
|
|
|
0, /* stop 0xfc */
|
|
|
|
|
-1, /* none 0xfd */
|
|
|
|
|
0, /* sensing 0xfe */
|
|
|
|
|
0, /* reset 0xff */
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-08 15:24:33 +01:00
|
|
|
static int process_byte(struct port *p, uint32_t time, uint8_t byte,
|
2024-03-07 18:11:01 +01:00
|
|
|
uint32_t *frame, uint8_t **bytes, size_t *size)
|
|
|
|
|
{
|
|
|
|
|
int res = 0;
|
|
|
|
|
if (byte >= 0xf8) {
|
|
|
|
|
if (byte == 0xfd) {
|
2024-05-20 10:45:04 -03:00
|
|
|
pw_log_warn("dropping invalid MIDI status bytes %08x", byte);
|
2024-03-07 18:11:01 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2024-03-08 15:24:33 +01:00
|
|
|
p->event_byte = byte;
|
2024-03-07 18:11:01 +01:00
|
|
|
*frame = time;
|
2024-03-08 15:24:33 +01:00
|
|
|
*bytes = &p->event_byte;
|
2024-03-07 18:11:01 +01:00
|
|
|
*size = 1;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2024-03-08 15:24:33 +01:00
|
|
|
if ((byte & 0x80) && (byte != 0xf7 || p->event_type != 8)) {
|
|
|
|
|
if (p->event_pending > 0)
|
|
|
|
|
pw_log_warn("incomplete MIDI message %02x dropped %u time:%u",
|
|
|
|
|
p->event_type, p->event_pending, time);
|
2024-03-07 18:11:01 +01:00
|
|
|
/* new command */
|
2024-03-08 15:24:33 +01:00
|
|
|
p->event_buffer[0] = byte;
|
|
|
|
|
p->event_time = time;
|
2024-03-07 18:11:01 +01:00
|
|
|
if ((byte & 0xf0) == 0xf0) /* system message */
|
2024-03-08 15:24:33 +01:00
|
|
|
p->event_type = (byte & 0x0f) + 8;
|
2024-03-07 18:11:01 +01:00
|
|
|
else
|
2024-03-08 15:24:33 +01:00
|
|
|
p->event_type = (byte >> 4) & 0x07;
|
|
|
|
|
p->event_pos = 1;
|
|
|
|
|
p->event_pending = status_len[p->event_type];
|
2024-03-07 18:11:01 +01:00
|
|
|
} else {
|
2024-03-08 15:24:33 +01:00
|
|
|
if (p->event_pending > 0) {
|
2024-03-07 18:11:01 +01:00
|
|
|
/* rest of command */
|
2024-03-08 15:24:33 +01:00
|
|
|
if (p->event_pos < sizeof(p->event_buffer))
|
|
|
|
|
p->event_buffer[p->event_pos++] = byte;
|
|
|
|
|
if (p->event_type != 8)
|
|
|
|
|
p->event_pending--;
|
2024-03-07 18:11:01 +01:00
|
|
|
} else {
|
|
|
|
|
/* running status */
|
2024-03-08 15:24:33 +01:00
|
|
|
p->event_buffer[1] = byte;
|
|
|
|
|
p->event_time = time;
|
|
|
|
|
p->event_pending = status_len[p->event_type] - 1;
|
|
|
|
|
p->event_pos = 2;
|
2024-03-07 18:11:01 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-03-08 15:24:33 +01:00
|
|
|
if (p->event_pending == 0) {
|
|
|
|
|
res = take_bytes(p, frame, bytes, size);
|
|
|
|
|
if (p->event_type >= 8)
|
|
|
|
|
p->event_type = 7;
|
|
|
|
|
} else if (p->event_type == 8) {
|
|
|
|
|
if (byte == 0xf7 || p->event_pos >= sizeof(p->event_buffer)) {
|
|
|
|
|
res = take_bytes(p, frame, bytes, size);
|
|
|
|
|
p->event_pos = 0;
|
2024-03-07 18:11:01 +01:00
|
|
|
if (byte == 0xf7) {
|
2024-03-08 15:24:33 +01:00
|
|
|
p->event_pending = 0;
|
|
|
|
|
p->event_type = 7;
|
2024-03-07 18:11:01 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 15:24:33 +01:00
|
|
|
static void ffado_to_midi(struct port *p, float *dst, uint32_t *src, uint32_t size)
|
2023-05-28 15:49:09 +02:00
|
|
|
{
|
|
|
|
|
struct spa_pod_builder b = { 0, };
|
|
|
|
|
uint32_t i, count;
|
|
|
|
|
struct spa_pod_frame f;
|
|
|
|
|
|
2024-03-07 18:11:01 +01:00
|
|
|
count = src ? size : 0;
|
2023-05-28 15:49:09 +02:00
|
|
|
|
|
|
|
|
spa_pod_builder_init(&b, dst, size);
|
|
|
|
|
spa_pod_builder_push_sequence(&b, &f, 0);
|
|
|
|
|
for (i = 0; i < count; i++) {
|
2024-03-07 18:11:01 +01:00
|
|
|
uint32_t data = src[i], frame;
|
|
|
|
|
uint8_t *bytes;
|
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
|
|
if ((data & 0xff000000) == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
2024-03-08 15:24:33 +01:00
|
|
|
if (process_byte(p, i, data & 0xff, &frame, &bytes, &size)) {
|
2024-07-26 15:20:14 +02:00
|
|
|
uint64_t state = 0;
|
|
|
|
|
while (size > 0) {
|
|
|
|
|
uint32_t ev[4];
|
|
|
|
|
int ev_size = spa_ump_from_midi(&bytes, &size, ev, sizeof(ev), 0, &state);
|
|
|
|
|
if (ev_size <= 0)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
spa_pod_builder_control(&b, frame, SPA_CONTROL_UMP);
|
|
|
|
|
spa_pod_builder_bytes(&b, ev, ev_size);
|
|
|
|
|
}
|
2024-03-07 18:11:01 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-05-28 15:49:09 +02:00
|
|
|
spa_pod_builder_pop(&b, &f);
|
2024-03-08 15:24:33 +01:00
|
|
|
if (p->event_pending > 0)
|
2024-03-08 12:54:12 +01:00
|
|
|
/* make sure the rest of the MIDI message is sent first in the next cycle */
|
2024-03-08 15:24:33 +01:00
|
|
|
p->event_time = 0;
|
2023-05-28 15:49:09 +02:00
|
|
|
}
|
|
|
|
|
|
2024-03-06 17:50:42 +01:00
|
|
|
static inline uint64_t get_time_ns(struct impl *impl)
|
|
|
|
|
{
|
|
|
|
|
uint64_t nsec;
|
|
|
|
|
if (impl->sink.filter)
|
|
|
|
|
nsec = pw_filter_get_nsec(impl->sink.filter);
|
|
|
|
|
else if (impl->source.filter)
|
|
|
|
|
nsec = pw_filter_get_nsec(impl->source.filter);
|
|
|
|
|
else
|
|
|
|
|
nsec = 0;
|
|
|
|
|
return nsec;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int set_timeout(struct impl *impl, uint64_t time)
|
|
|
|
|
{
|
|
|
|
|
struct timespec timeout, interval;
|
|
|
|
|
timeout.tv_sec = time / SPA_NSEC_PER_SEC;
|
|
|
|
|
timeout.tv_nsec = time % SPA_NSEC_PER_SEC;
|
|
|
|
|
interval.tv_sec = 0;
|
|
|
|
|
interval.tv_nsec = 0;
|
|
|
|
|
pw_loop_update_timer(impl->data_loop,
|
|
|
|
|
impl->ffado_timer, &timeout, &interval, true);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
static void stream_destroy(void *d)
|
|
|
|
|
{
|
|
|
|
|
struct stream *s = d;
|
2024-03-08 12:54:12 +01:00
|
|
|
uint32_t i;
|
|
|
|
|
for (i = 0; i < s->n_ports; i++) {
|
|
|
|
|
struct port *p = s->ports[i];
|
2024-03-12 12:21:47 +01:00
|
|
|
if (p != NULL) {
|
|
|
|
|
s->ports[i] = NULL;
|
|
|
|
|
free(p->buffer);
|
2024-03-19 13:20:29 +01:00
|
|
|
free(p);
|
2024-03-12 12:21:47 +01:00
|
|
|
}
|
2024-03-08 12:54:12 +01:00
|
|
|
}
|
|
|
|
|
s->n_ports = 0;
|
2023-05-28 15:49:09 +02:00
|
|
|
spa_hook_remove(&s->listener);
|
|
|
|
|
s->filter = NULL;
|
2024-04-22 17:43:04 +02:00
|
|
|
s->ready = false;
|
2024-03-08 12:54:12 +01:00
|
|
|
s->running = false;
|
2023-05-28 15:49:09 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-22 17:43:04 +02:00
|
|
|
static void check_start(struct impl *impl)
|
|
|
|
|
{
|
|
|
|
|
if ((!(impl->mode & MODE_SINK) || (impl->sink.ready && impl->sink.running)) &&
|
|
|
|
|
(!(impl->mode & MODE_SOURCE) || (impl->source.ready && impl->source.running)))
|
|
|
|
|
start_ffado_device(impl);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
static void stream_state_changed(void *d, enum pw_filter_state old,
|
|
|
|
|
enum pw_filter_state state, const char *error)
|
|
|
|
|
{
|
|
|
|
|
struct stream *s = d;
|
|
|
|
|
struct impl *impl = s->impl;
|
|
|
|
|
switch (state) {
|
|
|
|
|
case PW_FILTER_STATE_ERROR:
|
2024-07-18 14:15:22 +02:00
|
|
|
pw_log_warn("filter state %d error: %s", state, error);
|
|
|
|
|
break;
|
2024-04-03 09:30:38 +02:00
|
|
|
case PW_FILTER_STATE_UNCONNECTED:
|
2023-05-28 15:49:09 +02:00
|
|
|
pw_impl_module_schedule_destroy(impl->module);
|
|
|
|
|
break;
|
|
|
|
|
case PW_FILTER_STATE_PAUSED:
|
|
|
|
|
s->running = false;
|
2024-03-06 17:50:42 +01:00
|
|
|
if (!impl->sink.running && !impl->source.running)
|
|
|
|
|
stop_ffado_device(impl);
|
2023-05-28 15:49:09 +02:00
|
|
|
break;
|
|
|
|
|
case PW_FILTER_STATE_STREAMING:
|
2024-04-22 17:43:04 +02:00
|
|
|
s->running = true;
|
|
|
|
|
check_start(impl);
|
2023-05-28 15:49:09 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void sink_process(void *d, struct spa_io_position *position)
|
|
|
|
|
{
|
|
|
|
|
struct stream *s = d;
|
|
|
|
|
struct impl *impl = s->impl;
|
|
|
|
|
uint32_t i, n_samples = position->clock.duration;
|
|
|
|
|
|
2024-08-01 17:16:37 +02:00
|
|
|
pw_log_trace_fp("process %d", impl->rt.triggered);
|
|
|
|
|
if (impl->mode == MODE_SINK && impl->rt.triggered) {
|
|
|
|
|
impl->rt.triggered = false;
|
2023-06-19 16:43:56 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
for (i = 0; i < s->n_ports; i++) {
|
|
|
|
|
struct port *p = s->ports[i];
|
|
|
|
|
float *src;
|
2024-03-19 13:20:29 +01:00
|
|
|
if (p == NULL || p->data == NULL)
|
2023-05-28 15:49:09 +02:00
|
|
|
continue;
|
|
|
|
|
|
2024-03-19 13:20:29 +01:00
|
|
|
src = pw_filter_get_dsp_buffer(p->data, n_samples);
|
2024-03-06 17:50:42 +01:00
|
|
|
if (src == NULL) {
|
2024-03-07 18:11:01 +01:00
|
|
|
clear_port_buffer(p, n_samples);
|
2023-05-28 15:49:09 +02:00
|
|
|
continue;
|
2024-03-06 17:50:42 +01:00
|
|
|
}
|
2023-05-28 15:49:09 +02:00
|
|
|
|
|
|
|
|
if (SPA_UNLIKELY(p->is_midi))
|
2024-03-08 15:24:33 +01:00
|
|
|
midi_to_ffado(p, src, n_samples);
|
2023-05-28 15:49:09 +02:00
|
|
|
else
|
|
|
|
|
do_volume(p->buffer, src, &s->volume, i, n_samples);
|
2024-03-07 18:11:01 +01:00
|
|
|
|
|
|
|
|
p->cleared = false;
|
2023-05-28 15:49:09 +02:00
|
|
|
}
|
|
|
|
|
ffado_streaming_transfer_playback_buffers(impl->dev);
|
2024-08-01 17:16:37 +02:00
|
|
|
s->rt.transfered = true;
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2024-03-06 17:50:42 +01:00
|
|
|
if (impl->mode == MODE_SINK) {
|
|
|
|
|
pw_log_trace_fp("done %u", impl->frame_time);
|
2024-08-01 17:16:37 +02:00
|
|
|
impl->rt.done = true;
|
2024-03-06 17:50:42 +01:00
|
|
|
set_timeout(impl, position->clock.nsec);
|
2023-05-28 15:49:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-06 17:50:42 +01:00
|
|
|
static void silence_playback(struct impl *impl)
|
|
|
|
|
{
|
|
|
|
|
uint32_t i;
|
|
|
|
|
struct stream *s = &impl->sink;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < s->n_ports; i++) {
|
|
|
|
|
struct port *p = s->ports[i];
|
|
|
|
|
if (p != NULL)
|
2024-07-31 15:59:21 +02:00
|
|
|
clear_port_buffer(p, impl->device_options.period_size);
|
2024-03-06 17:50:42 +01:00
|
|
|
}
|
|
|
|
|
ffado_streaming_transfer_playback_buffers(impl->dev);
|
2024-08-01 17:16:37 +02:00
|
|
|
s->rt.transfered = true;
|
2024-03-06 17:50:42 +01:00
|
|
|
}
|
|
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
static void source_process(void *d, struct spa_io_position *position)
|
|
|
|
|
{
|
|
|
|
|
struct stream *s = d;
|
|
|
|
|
struct impl *impl = s->impl;
|
|
|
|
|
uint32_t i, n_samples = position->clock.duration;
|
|
|
|
|
|
2024-08-01 17:16:37 +02:00
|
|
|
pw_log_trace_fp("process %d", impl->rt.triggered);
|
|
|
|
|
|
|
|
|
|
if (SPA_FLAG_IS_SET(impl->position->clock.flags, SPA_IO_CLOCK_FLAG_XRUN_RECOVER))
|
|
|
|
|
return;
|
2024-03-06 17:50:42 +01:00
|
|
|
|
2024-08-01 17:16:37 +02:00
|
|
|
if (!impl->rt.triggered) {
|
2023-06-19 16:43:56 +02:00
|
|
|
pw_log_trace_fp("done %u", impl->frame_time);
|
2024-08-01 17:16:37 +02:00
|
|
|
impl->rt.done = true;
|
|
|
|
|
if (!impl->sink.rt.transfered)
|
2024-06-03 12:53:02 +02:00
|
|
|
silence_playback(impl);
|
2024-03-06 17:50:42 +01:00
|
|
|
set_timeout(impl, position->clock.nsec);
|
2023-06-19 16:43:56 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2024-03-06 17:50:42 +01:00
|
|
|
|
2024-08-01 17:16:37 +02:00
|
|
|
impl->rt.triggered = false;
|
2023-06-19 16:43:56 +02:00
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
ffado_streaming_transfer_capture_buffers(impl->dev);
|
2024-08-01 17:16:37 +02:00
|
|
|
s->rt.transfered = true;
|
2023-05-28 15:49:09 +02:00
|
|
|
|
|
|
|
|
for (i = 0; i < s->n_ports; i++) {
|
|
|
|
|
struct port *p = s->ports[i];
|
|
|
|
|
float *dst;
|
|
|
|
|
|
2024-03-19 13:20:29 +01:00
|
|
|
if (p == NULL || p->data == NULL || p->buffer == NULL)
|
2023-05-28 15:49:09 +02:00
|
|
|
continue;
|
|
|
|
|
|
2024-03-19 13:20:29 +01:00
|
|
|
dst = pw_filter_get_dsp_buffer(p->data, n_samples);
|
2023-05-28 15:49:09 +02:00
|
|
|
if (dst == NULL)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (SPA_UNLIKELY(p->is_midi))
|
2024-03-08 15:24:33 +01:00
|
|
|
ffado_to_midi(p, dst, p->buffer, n_samples);
|
2023-05-28 15:49:09 +02:00
|
|
|
else
|
|
|
|
|
do_volume(dst, p->buffer, &s->volume, i, n_samples);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void stream_io_changed(void *data, void *port_data, uint32_t id, void *area, uint32_t size)
|
|
|
|
|
{
|
|
|
|
|
struct stream *s = data;
|
|
|
|
|
struct impl *impl = s->impl;
|
2024-08-01 17:17:05 +02:00
|
|
|
bool freewheel;
|
|
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
if (port_data == NULL) {
|
|
|
|
|
switch (id) {
|
|
|
|
|
case SPA_IO_Position:
|
|
|
|
|
impl->position = area;
|
2024-08-01 17:17:05 +02:00
|
|
|
freewheel = impl->position != NULL &&
|
|
|
|
|
SPA_FLAG_IS_SET(impl->position->clock.flags, SPA_IO_CLOCK_FLAG_FREEWHEEL);
|
|
|
|
|
if (impl->freewheel != freewheel) {
|
|
|
|
|
pw_log_info("freewheel: %d -> %d", impl->freewheel, freewheel);
|
|
|
|
|
impl->freewheel = freewheel;
|
|
|
|
|
if (impl->started) {
|
|
|
|
|
if (freewheel) {
|
|
|
|
|
set_timeout(impl, 0);
|
|
|
|
|
ffado_streaming_stop(impl->dev);
|
|
|
|
|
} else {
|
|
|
|
|
ffado_streaming_start(impl->dev);
|
|
|
|
|
impl->rt.done = true;
|
|
|
|
|
set_timeout(impl, get_time_ns(impl));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-28 15:49:09 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void param_latency_changed(struct stream *s, const struct spa_pod *param,
|
2024-03-19 13:20:29 +01:00
|
|
|
struct port_data *data)
|
2023-05-28 15:49:09 +02:00
|
|
|
{
|
2024-03-19 13:20:29 +01:00
|
|
|
struct port *port = data->port;
|
2023-05-28 15:49:09 +02:00
|
|
|
struct spa_latency_info latency;
|
|
|
|
|
bool update = false;
|
|
|
|
|
enum spa_direction direction = port->direction;
|
|
|
|
|
|
2023-09-13 11:13:48 +02:00
|
|
|
if (param == NULL || spa_latency_parse(param, &latency) < 0)
|
2023-05-28 15:49:09 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (spa_latency_info_compare(&port->latency[direction], &latency)) {
|
|
|
|
|
port->latency[direction] = latency;
|
|
|
|
|
port->latency_changed[direction] = update = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 17:43:04 +02:00
|
|
|
static int make_stream_ports(struct stream *s)
|
2023-05-28 15:49:09 +02:00
|
|
|
{
|
|
|
|
|
struct impl *impl = s->impl;
|
|
|
|
|
struct pw_properties *props;
|
|
|
|
|
uint8_t buffer[1024];
|
|
|
|
|
struct spa_pod_builder b;
|
2023-06-05 10:19:27 +02:00
|
|
|
struct spa_latency_info latency;
|
2023-05-28 15:49:09 +02:00
|
|
|
const struct spa_pod *params[2];
|
2024-03-06 17:50:42 +01:00
|
|
|
uint32_t i, n_params = 0, n_channels = 0;
|
2023-05-28 15:49:09 +02:00
|
|
|
bool is_midi;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < s->n_ports; i++) {
|
|
|
|
|
struct port *port = s->ports[i];
|
2024-03-19 13:20:29 +01:00
|
|
|
if (port->data != NULL) {
|
2023-05-28 15:49:09 +02:00
|
|
|
free(port->buffer);
|
2024-03-19 13:20:29 +01:00
|
|
|
pw_filter_remove_port(port->data);
|
|
|
|
|
port->data = NULL;
|
2023-05-28 15:49:09 +02:00
|
|
|
}
|
2024-03-12 12:21:47 +01:00
|
|
|
}
|
|
|
|
|
for (i = 0; i < s->n_ports; i++) {
|
|
|
|
|
struct port *port = s->ports[i];
|
2024-03-19 13:20:29 +01:00
|
|
|
char channel[32];
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2024-03-06 17:50:42 +01:00
|
|
|
snprintf(channel, sizeof(channel), "AUX%u", n_channels % SPA_AUDIO_MAX_CHANNELS);
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2024-03-19 13:20:29 +01:00
|
|
|
switch (port->stream_type) {
|
2023-05-28 15:49:09 +02:00
|
|
|
case ffado_stream_type_audio:
|
|
|
|
|
props = pw_properties_new(
|
|
|
|
|
PW_KEY_FORMAT_DSP, "32 bit float mono audio",
|
|
|
|
|
PW_KEY_PORT_PHYSICAL, "true",
|
2024-03-12 12:21:47 +01:00
|
|
|
PW_KEY_PORT_TERMINAL, "true",
|
2024-03-19 13:20:29 +01:00
|
|
|
PW_KEY_PORT_NAME, port->name,
|
2024-03-06 17:50:42 +01:00
|
|
|
PW_KEY_AUDIO_CHANNEL, channel,
|
2023-05-28 15:49:09 +02:00
|
|
|
NULL);
|
|
|
|
|
is_midi = false;
|
2024-03-06 17:50:42 +01:00
|
|
|
n_channels++;
|
2023-05-28 15:49:09 +02:00
|
|
|
break;
|
|
|
|
|
case ffado_stream_type_midi:
|
|
|
|
|
props = pw_properties_new(
|
2025-05-23 16:46:13 +02:00
|
|
|
PW_KEY_FORMAT_DSP, "8 bit raw midi",
|
2024-03-19 13:20:29 +01:00
|
|
|
PW_KEY_PORT_NAME, port->name,
|
2023-05-28 15:49:09 +02:00
|
|
|
PW_KEY_PORT_PHYSICAL, "true",
|
2024-03-12 12:21:47 +01:00
|
|
|
PW_KEY_PORT_TERMINAL, "true",
|
|
|
|
|
PW_KEY_PORT_CONTROL, "true",
|
2023-05-28 15:49:09 +02:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
|
|
is_midi = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pw_log_info("not registering unknown stream %d %s (type %d)", i,
|
2024-03-19 13:20:29 +01:00
|
|
|
port->name, port->stream_type);
|
2023-05-28 15:49:09 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
}
|
2023-06-05 10:19:27 +02:00
|
|
|
latency = SPA_LATENCY_INFO(s->direction,
|
|
|
|
|
.min_quantum = 1,
|
|
|
|
|
.max_quantum = 1,
|
|
|
|
|
.min_rate = impl->latency[s->direction],
|
|
|
|
|
.max_rate = impl->latency[s->direction]);
|
2023-05-28 15:49:09 +02:00
|
|
|
|
|
|
|
|
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
|
|
|
|
n_params = 0;
|
2023-06-05 10:19:27 +02:00
|
|
|
params[n_params++] = spa_latency_build(&b, SPA_PARAM_Latency, &latency);
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2024-03-19 13:20:29 +01:00
|
|
|
port->data = pw_filter_add_port(s->filter,
|
2023-05-28 15:49:09 +02:00
|
|
|
s->direction,
|
|
|
|
|
PW_FILTER_PORT_FLAG_MAP_BUFFERS,
|
2024-03-19 13:20:29 +01:00
|
|
|
sizeof(struct port_data), props, params, n_params);
|
|
|
|
|
if (port->data == NULL) {
|
2023-05-28 15:49:09 +02:00
|
|
|
pw_log_error("Can't create port: %m");
|
2024-04-22 17:43:04 +02:00
|
|
|
return -errno;
|
2023-05-28 15:49:09 +02:00
|
|
|
}
|
2024-03-19 13:20:29 +01:00
|
|
|
port->data->port = port;
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2023-06-05 10:19:27 +02:00
|
|
|
port->latency[s->direction] = latency;
|
2023-05-28 15:49:09 +02:00
|
|
|
port->is_midi = is_midi;
|
2023-10-13 13:46:20 +02:00
|
|
|
port->buffer = calloc(impl->quantum_limit, sizeof(float));
|
2023-05-28 15:49:09 +02:00
|
|
|
if (port->buffer == NULL) {
|
|
|
|
|
pw_log_error("Can't create port buffer: %m");
|
2024-04-22 17:43:04 +02:00
|
|
|
return -errno;
|
2023-05-28 15:49:09 +02:00
|
|
|
}
|
2024-03-19 13:20:29 +01:00
|
|
|
}
|
2024-04-22 17:43:04 +02:00
|
|
|
return 0;
|
2024-03-19 13:20:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void setup_stream_ports(struct stream *s)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = s->impl;
|
|
|
|
|
uint32_t i;
|
|
|
|
|
for (i = 0; i < s->n_ports; i++) {
|
|
|
|
|
struct port *port = s->ports[i];
|
2023-05-29 09:17:08 +02:00
|
|
|
if (s->direction == PW_DIRECTION_INPUT) {
|
|
|
|
|
if (ffado_streaming_set_playback_stream_buffer(impl->dev, i, port->buffer))
|
2024-03-19 13:20:29 +01:00
|
|
|
pw_log_error("cannot configure port buffer for %s", port->name);
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2023-05-29 09:17:08 +02:00
|
|
|
if (ffado_streaming_playback_stream_onoff(impl->dev, i, 1))
|
2024-03-19 13:20:29 +01:00
|
|
|
pw_log_error("cannot enable port %s", port->name);
|
2023-05-29 09:17:08 +02:00
|
|
|
} else {
|
|
|
|
|
if (ffado_streaming_set_capture_stream_buffer(impl->dev, i, port->buffer))
|
2024-03-19 13:20:29 +01:00
|
|
|
pw_log_error("cannot configure port buffer for %s", port->name);
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2023-05-29 09:17:08 +02:00
|
|
|
if (ffado_streaming_capture_stream_onoff(impl->dev, i, 1))
|
2024-03-19 13:20:29 +01:00
|
|
|
pw_log_error("cannot enable port %s", port->name);
|
2023-05-29 09:17:08 +02:00
|
|
|
}
|
2023-05-28 15:49:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct spa_pod *make_props_param(struct spa_pod_builder *b,
|
|
|
|
|
struct volume *vol)
|
|
|
|
|
{
|
|
|
|
|
return spa_pod_builder_add_object(b, SPA_TYPE_OBJECT_Props, SPA_PARAM_Props,
|
|
|
|
|
SPA_PROP_mute, SPA_POD_Bool(vol->mute),
|
|
|
|
|
SPA_PROP_channelVolumes, SPA_POD_Array(sizeof(float),
|
|
|
|
|
SPA_TYPE_Float, vol->n_volumes, vol->volumes));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void parse_props(struct stream *s, const struct spa_pod *param)
|
|
|
|
|
{
|
|
|
|
|
struct spa_pod_object *obj = (struct spa_pod_object *) param;
|
|
|
|
|
struct spa_pod_prop *prop;
|
|
|
|
|
uint8_t buffer[1024];
|
|
|
|
|
struct spa_pod_builder b;
|
|
|
|
|
const struct spa_pod *params[1];
|
|
|
|
|
|
|
|
|
|
SPA_POD_OBJECT_FOREACH(obj, prop) {
|
|
|
|
|
switch (prop->key) {
|
|
|
|
|
case SPA_PROP_mute:
|
|
|
|
|
{
|
|
|
|
|
bool mute;
|
|
|
|
|
if (spa_pod_get_bool(&prop->value, &mute) == 0)
|
|
|
|
|
s->volume.mute = mute;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case SPA_PROP_channelVolumes:
|
|
|
|
|
{
|
|
|
|
|
uint32_t n;
|
|
|
|
|
float vols[SPA_AUDIO_MAX_CHANNELS];
|
|
|
|
|
if ((n = spa_pod_copy_array(&prop->value, SPA_TYPE_Float,
|
|
|
|
|
vols, SPA_AUDIO_MAX_CHANNELS)) > 0) {
|
|
|
|
|
s->volume.n_volumes = n;
|
|
|
|
|
for (n = 0; n < s->volume.n_volumes; n++)
|
|
|
|
|
s->volume.volumes[n] = vols[n];
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
|
|
|
|
params[0] = make_props_param(&b, &s->volume);
|
|
|
|
|
|
|
|
|
|
pw_filter_update_params(s->filter, NULL, params, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void stream_param_changed(void *data, void *port_data, uint32_t id,
|
|
|
|
|
const struct spa_pod *param)
|
|
|
|
|
{
|
|
|
|
|
struct stream *s = data;
|
2024-04-22 17:43:04 +02:00
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
if (port_data != NULL) {
|
|
|
|
|
switch (id) {
|
|
|
|
|
case SPA_PARAM_Latency:
|
|
|
|
|
param_latency_changed(s, param, port_data);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
switch (id) {
|
|
|
|
|
case SPA_PARAM_PortConfig:
|
|
|
|
|
pw_log_debug("PortConfig");
|
2024-04-22 17:43:04 +02:00
|
|
|
if (make_stream_ports(s) >= 0) {
|
|
|
|
|
s->ready = true;
|
|
|
|
|
check_start(s->impl);
|
|
|
|
|
}
|
2023-05-28 15:49:09 +02:00
|
|
|
break;
|
|
|
|
|
case SPA_PARAM_Props:
|
|
|
|
|
pw_log_debug("Props");
|
|
|
|
|
parse_props(s, param);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_filter_events sink_events = {
|
|
|
|
|
PW_VERSION_FILTER_EVENTS,
|
|
|
|
|
.destroy = stream_destroy,
|
|
|
|
|
.state_changed = stream_state_changed,
|
|
|
|
|
.param_changed = stream_param_changed,
|
|
|
|
|
.io_changed = stream_io_changed,
|
|
|
|
|
.process = sink_process
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const struct pw_filter_events source_events = {
|
|
|
|
|
PW_VERSION_FILTER_EVENTS,
|
|
|
|
|
.destroy = stream_destroy,
|
|
|
|
|
.state_changed = stream_state_changed,
|
|
|
|
|
.param_changed = stream_param_changed,
|
|
|
|
|
.io_changed = stream_io_changed,
|
|
|
|
|
.process = source_process,
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-31 16:20:15 +02:00
|
|
|
static int update_stream_format(struct stream *s, uint32_t samplerate)
|
|
|
|
|
{
|
|
|
|
|
uint8_t buffer[1024];
|
|
|
|
|
struct spa_pod_builder b;
|
|
|
|
|
uint32_t n_params;
|
|
|
|
|
const struct spa_pod *params[2];
|
|
|
|
|
|
|
|
|
|
if (s->info.rate == samplerate)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
s->info.rate = samplerate;
|
|
|
|
|
|
|
|
|
|
if (s->filter == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
n_params = 0;
|
|
|
|
|
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
|
|
|
|
params[n_params++] = spa_format_audio_raw_build(&b,
|
|
|
|
|
SPA_PARAM_EnumFormat, &s->info);
|
|
|
|
|
params[n_params++] = spa_format_audio_raw_build(&b,
|
|
|
|
|
SPA_PARAM_Format, &s->info);
|
|
|
|
|
|
|
|
|
|
return pw_filter_update_params(s->filter, NULL, params, n_params);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
static int make_stream(struct stream *s, const char *name)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = s->impl;
|
|
|
|
|
uint32_t n_params;
|
|
|
|
|
const struct spa_pod *params[4];
|
|
|
|
|
uint8_t buffer[1024];
|
|
|
|
|
struct spa_pod_builder b;
|
|
|
|
|
|
2024-03-08 12:54:12 +01:00
|
|
|
s->filter = pw_filter_new(impl->core, name, pw_properties_copy(s->props));
|
2023-05-28 15:49:09 +02:00
|
|
|
if (s->filter == NULL)
|
|
|
|
|
return -errno;
|
|
|
|
|
|
2024-03-08 12:54:12 +01:00
|
|
|
spa_zero(s->listener);
|
2023-05-28 15:49:09 +02:00
|
|
|
if (s->direction == PW_DIRECTION_INPUT) {
|
|
|
|
|
pw_filter_add_listener(s->filter, &s->listener,
|
|
|
|
|
&sink_events, s);
|
|
|
|
|
} else {
|
|
|
|
|
pw_filter_add_listener(s->filter, &s->listener,
|
|
|
|
|
&source_events, s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reset_volume(&s->volume, s->info.channels);
|
|
|
|
|
|
2024-07-31 16:20:15 +02:00
|
|
|
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
|
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
n_params = 0;
|
|
|
|
|
params[n_params++] = spa_format_audio_raw_build(&b,
|
|
|
|
|
SPA_PARAM_EnumFormat, &s->info);
|
|
|
|
|
params[n_params++] = spa_format_audio_raw_build(&b,
|
|
|
|
|
SPA_PARAM_Format, &s->info);
|
|
|
|
|
params[n_params++] = make_props_param(&b, &s->volume);
|
|
|
|
|
|
|
|
|
|
return pw_filter_connect(s->filter,
|
|
|
|
|
PW_FILTER_FLAG_DRIVER |
|
|
|
|
|
PW_FILTER_FLAG_RT_PROCESS |
|
|
|
|
|
PW_FILTER_FLAG_CUSTOM_LATENCY,
|
|
|
|
|
params, n_params);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 12:54:12 +01:00
|
|
|
static void destroy_stream(struct stream *s)
|
2023-05-28 15:49:09 +02:00
|
|
|
{
|
2024-03-08 12:54:12 +01:00
|
|
|
if (s->filter)
|
|
|
|
|
pw_filter_destroy(s->filter);
|
2023-05-28 15:49:09 +02:00
|
|
|
}
|
|
|
|
|
|
2024-03-06 17:50:42 +01:00
|
|
|
static void on_ffado_timeout(void *data, uint64_t expirations)
|
2023-05-28 15:49:09 +02:00
|
|
|
{
|
2024-03-06 17:50:42 +01:00
|
|
|
struct impl *impl = data;
|
2023-05-28 15:49:09 +02:00
|
|
|
bool source_running, sink_running;
|
2023-05-30 15:57:07 +02:00
|
|
|
uint64_t nsec;
|
2024-03-06 17:50:42 +01:00
|
|
|
ffado_wait_response response;
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2024-08-01 17:16:37 +02:00
|
|
|
pw_log_trace_fp("wakeup %d", impl->rt.done);
|
2024-05-27 16:41:30 +02:00
|
|
|
|
2024-08-01 17:17:05 +02:00
|
|
|
if (impl->freewheel)
|
|
|
|
|
return;
|
|
|
|
|
|
2024-08-01 17:16:37 +02:00
|
|
|
if (!impl->rt.done) {
|
|
|
|
|
impl->rt.pw_xrun++;
|
|
|
|
|
impl->rt.new_xrun = true;
|
2024-03-06 17:50:42 +01:00
|
|
|
ffado_streaming_reset(impl->dev);
|
|
|
|
|
}
|
|
|
|
|
again:
|
2024-05-27 16:41:30 +02:00
|
|
|
pw_log_trace_fp("FFADO wait");
|
2024-03-06 17:50:42 +01:00
|
|
|
response = ffado_streaming_wait(impl->dev);
|
|
|
|
|
nsec = get_time_ns(impl);
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2024-03-06 17:50:42 +01:00
|
|
|
switch (response) {
|
|
|
|
|
case ffado_wait_ok:
|
|
|
|
|
break;
|
|
|
|
|
case ffado_wait_xrun:
|
|
|
|
|
pw_log_debug("FFADO xrun");
|
2024-08-01 17:16:37 +02:00
|
|
|
impl->rt.ffado_xrun++;
|
|
|
|
|
impl->rt.new_xrun = true;
|
2024-03-06 17:50:42 +01:00
|
|
|
goto again;
|
|
|
|
|
case ffado_wait_shutdown:
|
|
|
|
|
pw_log_info("FFADO shutdown");
|
|
|
|
|
return;
|
|
|
|
|
case ffado_wait_error:
|
|
|
|
|
default:
|
|
|
|
|
pw_log_error("FFADO error");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-22 17:43:04 +02:00
|
|
|
source_running = impl->source.running && impl->sink.ready;
|
|
|
|
|
sink_running = impl->sink.running && impl->source.ready;
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2024-08-01 17:16:37 +02:00
|
|
|
impl->source.rt.transfered = false;
|
|
|
|
|
impl->sink.rt.transfered = false;
|
2024-06-03 12:53:02 +02:00
|
|
|
|
|
|
|
|
if (!source_running) {
|
2024-03-06 17:50:42 +01:00
|
|
|
ffado_streaming_transfer_capture_buffers(impl->dev);
|
2024-08-01 17:16:37 +02:00
|
|
|
impl->source.rt.transfered = true;
|
2024-06-03 12:53:02 +02:00
|
|
|
}
|
2024-03-06 17:50:42 +01:00
|
|
|
if (!sink_running)
|
|
|
|
|
silence_playback(impl);
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2024-07-31 15:59:21 +02:00
|
|
|
pw_log_trace_fp("process %d %u %u %p %d %"PRIu64,
|
|
|
|
|
impl->device_options.period_size, source_running,
|
2024-05-27 16:41:30 +02:00
|
|
|
sink_running, impl->position, impl->frame_time, nsec);
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2024-08-01 17:16:37 +02:00
|
|
|
if (impl->rt.new_xrun) {
|
2024-03-06 17:50:42 +01:00
|
|
|
pw_log_warn("Xrun FFADO:%u PipeWire:%u source:%d sink:%d",
|
2024-08-01 17:16:37 +02:00
|
|
|
impl->rt.ffado_xrun, impl->rt.pw_xrun, source_running, sink_running);
|
|
|
|
|
impl->rt.new_xrun = false;
|
2024-03-06 17:50:42 +01:00
|
|
|
}
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2024-03-06 17:50:42 +01:00
|
|
|
if (impl->position) {
|
|
|
|
|
struct spa_io_clock *c = &impl->position->clock;
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2024-03-19 15:46:46 +01:00
|
|
|
#if 0
|
2024-07-31 15:59:21 +02:00
|
|
|
if (c->target_duration != (uint64_t) impl->device_options.period_size) {
|
2024-03-19 15:46:46 +01:00
|
|
|
ffado_streaming_transfer_capture_buffers(impl->dev);
|
|
|
|
|
silence_playback(impl);
|
|
|
|
|
|
|
|
|
|
if (ffado_streaming_set_period_size(impl->dev, c->target_duration) != 0) {
|
|
|
|
|
pw_log_warn("can't change period size");
|
|
|
|
|
} else {
|
|
|
|
|
sleep(1);
|
2024-07-31 15:59:21 +02:00
|
|
|
impl->device_options.period_size = c->target_duration;
|
2024-03-19 15:46:46 +01:00
|
|
|
}
|
|
|
|
|
goto again;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-03-06 17:50:42 +01:00
|
|
|
c->nsec = nsec;
|
2024-07-31 15:59:21 +02:00
|
|
|
c->rate = SPA_FRACTION(1, impl->device_options.sample_rate);
|
|
|
|
|
c->position += impl->device_options.period_size;
|
|
|
|
|
c->duration = impl->device_options.period_size;
|
2024-03-06 17:50:42 +01:00
|
|
|
c->delay = 0;
|
|
|
|
|
c->rate_diff = 1.0;
|
2024-07-31 15:59:21 +02:00
|
|
|
c->next_nsec = nsec + (c->duration * SPA_NSEC_PER_SEC) / impl->device_options.sample_rate;
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2024-03-06 17:50:42 +01:00
|
|
|
c->target_rate = c->rate;
|
|
|
|
|
c->target_duration = c->duration;
|
|
|
|
|
}
|
|
|
|
|
if (impl->mode & MODE_SOURCE && source_running) {
|
2024-08-01 17:16:37 +02:00
|
|
|
impl->rt.done = false;
|
|
|
|
|
impl->rt.triggered = true;
|
2024-03-06 17:50:42 +01:00
|
|
|
set_timeout(impl, nsec + SPA_NSEC_PER_SEC);
|
|
|
|
|
pw_filter_trigger_process(impl->source.filter);
|
|
|
|
|
} else if (impl->mode == MODE_SINK && sink_running) {
|
2024-08-01 17:16:37 +02:00
|
|
|
impl->rt.done = false;
|
|
|
|
|
impl->rt.triggered = true;
|
2024-03-06 17:50:42 +01:00
|
|
|
set_timeout(impl, nsec + SPA_NSEC_PER_SEC);
|
|
|
|
|
pw_filter_trigger_process(impl->sink.filter);
|
|
|
|
|
} else {
|
2024-08-01 17:16:37 +02:00
|
|
|
impl->rt.done = true;
|
2024-03-06 17:50:42 +01:00
|
|
|
set_timeout(impl, nsec);
|
2023-05-28 15:49:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-19 13:20:29 +01:00
|
|
|
static void close_ffado_device(struct impl *impl)
|
|
|
|
|
{
|
|
|
|
|
if (impl->dev == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
stop_ffado_device(impl);
|
|
|
|
|
ffado_streaming_finish(impl->dev);
|
|
|
|
|
impl->dev = NULL;
|
|
|
|
|
|
|
|
|
|
pw_log_info("closed FFADO device %s", impl->devices[0]);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
static int open_ffado_device(struct impl *impl)
|
|
|
|
|
{
|
2024-03-19 13:20:29 +01:00
|
|
|
int32_t target_rate, target_period;
|
|
|
|
|
|
|
|
|
|
if (impl->dev != NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2024-07-31 15:59:21 +02:00
|
|
|
target_rate = impl->sample_rate;
|
|
|
|
|
target_period = impl->period_size;
|
|
|
|
|
|
2024-03-19 13:20:29 +01:00
|
|
|
if (impl->position) {
|
|
|
|
|
struct spa_io_clock *c = &impl->position->clock;
|
2024-07-31 15:59:21 +02:00
|
|
|
if (target_rate == 0)
|
|
|
|
|
target_rate = c->target_rate.denom;
|
|
|
|
|
if (target_period == 0)
|
|
|
|
|
target_period = c->target_duration;
|
2024-03-19 13:20:29 +01:00
|
|
|
}
|
2024-07-31 16:20:15 +02:00
|
|
|
if (target_rate == 0)
|
|
|
|
|
target_rate = DEFAULT_SAMPLE_RATE;
|
|
|
|
|
if (target_period == 0)
|
|
|
|
|
target_period = DEFAULT_PERIOD_SIZE;
|
2023-05-28 15:49:09 +02:00
|
|
|
|
|
|
|
|
spa_zero(impl->device_info);
|
2023-05-28 19:17:41 +02:00
|
|
|
impl->device_info.device_spec_strings = impl->devices;
|
|
|
|
|
impl->device_info.nb_device_spec_strings = impl->n_devices;
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2023-05-28 19:17:41 +02:00
|
|
|
spa_zero(impl->device_options);
|
2024-03-19 13:20:29 +01:00
|
|
|
impl->device_options.sample_rate = target_rate;
|
|
|
|
|
impl->device_options.period_size = target_period;
|
2023-05-28 15:49:09 +02:00
|
|
|
impl->device_options.nb_buffers = impl->n_periods;
|
2024-04-03 09:30:38 +02:00
|
|
|
impl->device_options.realtime = impl->realtime;
|
|
|
|
|
impl->device_options.packetizer_priority = impl->rtprio;
|
2023-05-28 19:17:41 +02:00
|
|
|
impl->device_options.verbose = impl->verbose;
|
|
|
|
|
impl->device_options.slave_mode = impl->slave_mode;
|
|
|
|
|
impl->device_options.snoop_mode = impl->snoop_mode;
|
2023-05-28 15:49:09 +02:00
|
|
|
|
|
|
|
|
impl->dev = ffado_streaming_init(impl->device_info, impl->device_options);
|
|
|
|
|
if (impl->dev == NULL) {
|
2023-05-28 19:17:41 +02:00
|
|
|
pw_log_error("can't open FFADO device %s", impl->devices[0]);
|
2023-05-28 15:49:09 +02:00
|
|
|
return -EIO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (impl->device_options.realtime) {
|
|
|
|
|
pw_log_info("Streaming thread running with Realtime scheduling, priority %d",
|
|
|
|
|
impl->device_options.packetizer_priority);
|
|
|
|
|
} else {
|
|
|
|
|
pw_log_info("Streaming thread running without Realtime scheduling");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ffado_streaming_set_audio_datatype(impl->dev, ffado_audio_datatype_float);
|
|
|
|
|
|
|
|
|
|
impl->source.n_ports = ffado_streaming_get_nb_capture_streams(impl->dev);
|
2024-03-19 13:20:29 +01:00
|
|
|
impl->sink.n_ports = ffado_streaming_get_nb_playback_streams(impl->dev);
|
|
|
|
|
|
|
|
|
|
if (impl->source.n_ports == 0 && impl->sink.n_ports == 0) {
|
|
|
|
|
close_ffado_device(impl);
|
|
|
|
|
return -EIO;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 16:20:15 +02:00
|
|
|
update_stream_format(&impl->source, impl->device_options.sample_rate);
|
|
|
|
|
update_stream_format(&impl->sink, impl->device_options.sample_rate);
|
|
|
|
|
|
2024-03-19 15:46:46 +01:00
|
|
|
pw_log_info("opened FFADO device %s source:%d sink:%d rate:%d period:%d %p",
|
|
|
|
|
impl->devices[0], impl->source.n_ports, impl->sink.n_ports,
|
2024-07-31 15:59:21 +02:00
|
|
|
impl->device_options.sample_rate,
|
|
|
|
|
impl->device_options.period_size, impl->position);
|
2024-03-19 13:20:29 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int probe_ffado_device(struct impl *impl)
|
|
|
|
|
{
|
|
|
|
|
int res;
|
|
|
|
|
uint32_t i, n_channels;
|
|
|
|
|
struct port *port;
|
|
|
|
|
char name[256];
|
|
|
|
|
|
|
|
|
|
if ((res = open_ffado_device(impl)) < 0)
|
|
|
|
|
return res;
|
2024-03-06 17:50:42 +01:00
|
|
|
|
|
|
|
|
n_channels = 0;
|
2023-05-28 15:49:09 +02:00
|
|
|
for (i = 0; i < impl->source.n_ports; i++) {
|
2024-03-19 13:20:29 +01:00
|
|
|
port = calloc(1, sizeof(struct port));
|
|
|
|
|
if (port == NULL)
|
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
|
|
port->direction = impl->source.direction;
|
|
|
|
|
port->stream_type = ffado_streaming_get_capture_stream_type(impl->dev, i);
|
|
|
|
|
ffado_streaming_get_capture_stream_name(impl->dev, i, name, sizeof(name));
|
|
|
|
|
snprintf(port->name, sizeof(port->name), "%s_out", name);
|
|
|
|
|
|
|
|
|
|
switch (port->stream_type) {
|
2023-05-28 15:49:09 +02:00
|
|
|
case ffado_stream_type_audio:
|
2024-03-06 17:50:42 +01:00
|
|
|
n_channels++;
|
2023-05-28 15:49:09 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
2023-05-31 21:41:23 +02:00
|
|
|
break;
|
2023-05-28 15:49:09 +02:00
|
|
|
}
|
2024-03-19 13:20:29 +01:00
|
|
|
impl->source.ports[i] = port;
|
2023-05-28 15:49:09 +02:00
|
|
|
}
|
2024-03-06 17:50:42 +01:00
|
|
|
if (impl->source.info.channels != n_channels) {
|
|
|
|
|
impl->source.info.channels = n_channels;
|
|
|
|
|
for (i = 0; i < SPA_MIN(impl->source.info.channels, SPA_AUDIO_MAX_CHANNELS); i++)
|
|
|
|
|
impl->source.info.position[i] = SPA_AUDIO_CHANNEL_AUX0 + i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
n_channels = 0;
|
2023-05-28 15:49:09 +02:00
|
|
|
for (i = 0; i < impl->sink.n_ports; i++) {
|
2024-03-19 13:20:29 +01:00
|
|
|
port = calloc(1, sizeof(struct port));
|
|
|
|
|
if (port == NULL)
|
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
|
|
port->direction = impl->sink.direction;
|
|
|
|
|
port->stream_type = ffado_streaming_get_playback_stream_type(impl->dev, i);
|
|
|
|
|
ffado_streaming_get_playback_stream_name(impl->dev, i, name, sizeof(name));
|
|
|
|
|
snprintf(port->name, sizeof(port->name), "%s_in", name);
|
|
|
|
|
|
|
|
|
|
switch (port->stream_type) {
|
2023-05-28 15:49:09 +02:00
|
|
|
case ffado_stream_type_audio:
|
2024-03-06 17:50:42 +01:00
|
|
|
n_channels++;
|
2023-05-28 15:49:09 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
2023-05-31 21:41:23 +02:00
|
|
|
break;
|
2023-05-28 15:49:09 +02:00
|
|
|
}
|
2024-03-19 13:20:29 +01:00
|
|
|
impl->sink.ports[i] = port;
|
2023-05-28 15:49:09 +02:00
|
|
|
}
|
2024-03-06 17:50:42 +01:00
|
|
|
if (impl->sink.info.channels != n_channels) {
|
|
|
|
|
impl->sink.info.channels = n_channels;
|
|
|
|
|
for (i = 0; i < SPA_MIN(impl->sink.info.channels, SPA_AUDIO_MAX_CHANNELS); i++)
|
|
|
|
|
impl->sink.info.position[i] = SPA_AUDIO_CHANNEL_AUX0 + i;
|
|
|
|
|
}
|
2024-03-19 13:20:29 +01:00
|
|
|
|
2024-03-08 12:54:12 +01:00
|
|
|
if (impl->mode & MODE_SINK) {
|
|
|
|
|
if ((res = make_stream(&impl->sink, "FFADO Sink")) < 0)
|
2024-03-19 13:20:29 +01:00
|
|
|
goto exit;
|
2024-03-08 12:54:12 +01:00
|
|
|
}
|
|
|
|
|
if (impl->mode & MODE_SOURCE) {
|
|
|
|
|
if ((res = make_stream(&impl->source, "FFADO Source")) < 0)
|
2024-03-19 13:20:29 +01:00
|
|
|
goto exit;
|
2023-05-28 15:49:09 +02:00
|
|
|
}
|
2024-03-19 13:20:29 +01:00
|
|
|
exit:
|
|
|
|
|
close_ffado_device(impl);
|
2024-03-08 12:54:12 +01:00
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-19 13:20:29 +01:00
|
|
|
|
|
|
|
|
static int start_ffado_device(struct impl *impl)
|
2024-03-08 12:54:12 +01:00
|
|
|
{
|
2024-03-19 13:20:29 +01:00
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
if (impl->started)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if ((res = open_ffado_device(impl)) < 0)
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
|
|
setup_stream_ports(&impl->source);
|
|
|
|
|
setup_stream_ports(&impl->sink);
|
|
|
|
|
|
|
|
|
|
if (ffado_streaming_prepare(impl->dev)) {
|
|
|
|
|
pw_log_error("Could not prepare streaming");
|
|
|
|
|
schedule_reset_ffado_device(impl);
|
|
|
|
|
return -EIO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ffado_streaming_start(impl->dev)) {
|
|
|
|
|
pw_log_warn("Could not start FFADO streaming, try reset");
|
|
|
|
|
schedule_reset_ffado_device(impl);
|
|
|
|
|
return -EIO;
|
2024-03-08 12:54:12 +01:00
|
|
|
}
|
2024-03-19 13:20:29 +01:00
|
|
|
pw_log_info("FFADO started streaming");
|
|
|
|
|
|
|
|
|
|
impl->started = true;
|
2024-08-01 17:16:37 +02:00
|
|
|
impl->rt.done = true;
|
2024-03-19 13:20:29 +01:00
|
|
|
set_timeout(impl, get_time_ns(impl));
|
|
|
|
|
return 0;
|
2024-03-08 12:54:12 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-19 13:20:29 +01:00
|
|
|
static int stop_ffado_device(struct impl *impl)
|
|
|
|
|
{
|
|
|
|
|
if (!impl->started)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
impl->started = false;
|
|
|
|
|
set_timeout(impl, 0);
|
|
|
|
|
if (ffado_streaming_stop(impl->dev))
|
|
|
|
|
pw_log_error("Could not stop FFADO streaming");
|
|
|
|
|
else
|
|
|
|
|
pw_log_info("FFADO stopped streaming");
|
|
|
|
|
|
|
|
|
|
close_ffado_device(impl);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-03-08 12:54:12 +01:00
|
|
|
static void do_reset_ffado(void *obj, void *data, int res, uint32_t id)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = obj;
|
|
|
|
|
|
|
|
|
|
impl->reset_work_id = SPA_ID_INVALID;
|
|
|
|
|
close_ffado_device(impl);
|
|
|
|
|
open_ffado_device(impl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void schedule_reset_ffado_device(struct impl *impl)
|
|
|
|
|
{
|
|
|
|
|
if (impl->reset_work_id != SPA_ID_INVALID)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
impl->reset_work_id = pw_work_queue_add(pw_context_get_work_queue(impl->context),
|
|
|
|
|
impl, 0, do_reset_ffado, NULL);
|
2023-05-28 15:49:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void core_error(void *data, uint32_t id, int seq, int res, const char *message)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
|
|
|
|
|
pw_log_error("error id:%u seq:%d res:%d (%s): %s",
|
|
|
|
|
id, seq, res, spa_strerror(res), message);
|
|
|
|
|
|
|
|
|
|
if (id == PW_ID_CORE && res == -EPIPE)
|
|
|
|
|
pw_impl_module_schedule_destroy(impl->module);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_core_events core_events = {
|
|
|
|
|
PW_VERSION_CORE_EVENTS,
|
|
|
|
|
.error = core_error,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void core_destroy(void *d)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = d;
|
|
|
|
|
spa_hook_remove(&impl->core_listener);
|
|
|
|
|
impl->core = NULL;
|
|
|
|
|
pw_impl_module_schedule_destroy(impl->module);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_proxy_events core_proxy_events = {
|
|
|
|
|
.destroy = core_destroy,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void impl_destroy(struct impl *impl)
|
|
|
|
|
{
|
2023-05-28 19:17:41 +02:00
|
|
|
uint32_t i;
|
|
|
|
|
|
2024-03-08 12:54:12 +01:00
|
|
|
if (impl->reset_work_id != SPA_ID_INVALID)
|
|
|
|
|
pw_work_queue_cancel(pw_context_get_work_queue(impl->context),
|
|
|
|
|
impl, SPA_ID_INVALID);
|
|
|
|
|
|
|
|
|
|
close_ffado_device(impl);
|
|
|
|
|
|
2024-03-19 13:20:29 +01:00
|
|
|
destroy_stream(&impl->source);
|
|
|
|
|
destroy_stream(&impl->sink);
|
|
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
if (impl->core && impl->do_disconnect)
|
|
|
|
|
pw_core_disconnect(impl->core);
|
2024-03-06 17:50:42 +01:00
|
|
|
if (impl->ffado_timer)
|
|
|
|
|
pw_loop_destroy_source(impl->data_loop, impl->ffado_timer);
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2024-04-22 16:19:02 +02:00
|
|
|
if (impl->data_loop)
|
|
|
|
|
pw_context_release_loop(impl->context, impl->data_loop);
|
|
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
pw_properties_free(impl->sink.props);
|
|
|
|
|
pw_properties_free(impl->source.props);
|
|
|
|
|
pw_properties_free(impl->props);
|
|
|
|
|
|
2023-05-28 19:17:41 +02:00
|
|
|
for (i = 0; i < impl->n_devices; i++)
|
|
|
|
|
free(impl->devices[i]);
|
2023-05-28 15:49:09 +02:00
|
|
|
free(impl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void module_destroy(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
spa_hook_remove(&impl->module_listener);
|
|
|
|
|
impl_destroy(impl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_impl_module_events module_events = {
|
|
|
|
|
PW_VERSION_IMPL_MODULE_EVENTS,
|
|
|
|
|
.destroy = module_destroy,
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-28 19:17:41 +02:00
|
|
|
static void parse_devices(struct impl *impl, const char *val, size_t len)
|
|
|
|
|
{
|
2024-09-13 13:09:54 +02:00
|
|
|
struct spa_json it[1];
|
2023-05-28 19:17:41 +02:00
|
|
|
char v[FFADO_MAX_SPECSTRING_LENGTH];
|
|
|
|
|
|
2024-09-13 13:09:54 +02:00
|
|
|
if (spa_json_begin_array_relax(&it[0], val, len) <= 0)
|
|
|
|
|
return;
|
2023-05-28 19:17:41 +02:00
|
|
|
|
|
|
|
|
impl->n_devices = 0;
|
2024-09-13 13:09:54 +02:00
|
|
|
while (spa_json_get_string(&it[0], v, sizeof(v)) > 0 &&
|
2023-05-28 19:17:41 +02:00
|
|
|
impl->n_devices < FFADO_MAX_SPECSTRINGS) {
|
|
|
|
|
impl->devices[impl->n_devices++] = strdup(v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
static void parse_audio_info(const struct pw_properties *props, struct spa_audio_info_raw *info)
|
|
|
|
|
{
|
2024-09-18 15:46:49 +02:00
|
|
|
spa_audio_info_raw_init_dict_keys(info,
|
|
|
|
|
&SPA_DICT_ITEMS(
|
|
|
|
|
SPA_DICT_ITEM(SPA_KEY_AUDIO_FORMAT, "F32P"),
|
|
|
|
|
SPA_DICT_ITEM(SPA_KEY_AUDIO_POSITION, DEFAULT_POSITION)),
|
|
|
|
|
&props->dict,
|
|
|
|
|
SPA_KEY_AUDIO_CHANNELS,
|
|
|
|
|
SPA_KEY_AUDIO_POSITION, NULL);
|
2023-05-28 15:49:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void copy_props(struct impl *impl, struct pw_properties *props, const char *key)
|
|
|
|
|
{
|
|
|
|
|
const char *str;
|
|
|
|
|
if ((str = pw_properties_get(props, key)) != NULL) {
|
|
|
|
|
if (pw_properties_get(impl->sink.props, key) == NULL)
|
|
|
|
|
pw_properties_set(impl->sink.props, key, str);
|
|
|
|
|
if (pw_properties_get(impl->source.props, key) == NULL)
|
|
|
|
|
pw_properties_set(impl->source.props, key, str);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SPA_EXPORT
|
|
|
|
|
int pipewire__module_init(struct pw_impl_module *module, const char *args)
|
|
|
|
|
{
|
|
|
|
|
struct pw_context *context = pw_impl_module_get_context(module);
|
|
|
|
|
struct pw_properties *props = NULL;
|
|
|
|
|
struct impl *impl;
|
|
|
|
|
const char *str;
|
|
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
PW_LOG_TOPIC_INIT(mod_topic);
|
|
|
|
|
|
|
|
|
|
impl = calloc(1, sizeof(struct impl));
|
|
|
|
|
if (impl == NULL)
|
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
|
|
pw_log_debug("module %p: new %s", impl, args);
|
|
|
|
|
|
|
|
|
|
if (args == NULL)
|
|
|
|
|
args = "";
|
|
|
|
|
|
|
|
|
|
props = pw_properties_new_string(args);
|
|
|
|
|
if (props == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
pw_log_error( "can't create properties: %m");
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
impl->props = props;
|
2023-05-28 19:17:41 +02:00
|
|
|
str = pw_properties_get(props, "ffado.devices");
|
|
|
|
|
if (str == NULL)
|
|
|
|
|
str = DEFAULT_DEVICES;
|
|
|
|
|
parse_devices(impl, str, strlen(str));
|
|
|
|
|
|
|
|
|
|
impl->period_size = pw_properties_get_int32(props,
|
|
|
|
|
"ffado.period-size", DEFAULT_PERIOD_SIZE);
|
|
|
|
|
impl->n_periods = pw_properties_get_int32(props,
|
|
|
|
|
"ffado.period-num", DEFAULT_PERIOD_NUM);
|
|
|
|
|
impl->sample_rate = pw_properties_get_int32(props,
|
|
|
|
|
"ffado.sample-rate", DEFAULT_SAMPLE_RATE);
|
|
|
|
|
impl->slave_mode = pw_properties_get_bool(props,
|
|
|
|
|
"ffado.slave-mode", DEFAULT_SLAVE_MODE);
|
|
|
|
|
impl->snoop_mode = pw_properties_get_bool(props,
|
|
|
|
|
"ffado.snoop-mode", DEFAULT_SNOOP_MODE);
|
|
|
|
|
impl->verbose = pw_properties_get_uint32(props,
|
|
|
|
|
"ffado.verbose", DEFAULT_VERBOSE);
|
2024-04-03 09:30:38 +02:00
|
|
|
impl->rtprio = pw_properties_get_uint32(props,
|
|
|
|
|
"ffado.rtprio", DEFAULT_RTPRIO);
|
|
|
|
|
impl->realtime = pw_properties_get_bool(props,
|
|
|
|
|
"ffado.realtime", DEFAULT_REALTIME);
|
2023-05-28 19:17:41 +02:00
|
|
|
impl->input_latency = pw_properties_get_uint32(props,
|
|
|
|
|
"latency.internal.input", 0);
|
|
|
|
|
impl->output_latency = pw_properties_get_uint32(props,
|
|
|
|
|
"latency.internal.output", 0);
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2023-10-13 13:46:20 +02:00
|
|
|
impl->quantum_limit = pw_properties_get_uint32(
|
|
|
|
|
pw_context_get_properties(context),
|
|
|
|
|
"default.clock.quantum-limit", 8192u);
|
|
|
|
|
|
2023-05-28 15:49:09 +02:00
|
|
|
impl->sink.props = pw_properties_new(NULL, NULL);
|
|
|
|
|
impl->source.props = pw_properties_new(NULL, NULL);
|
|
|
|
|
if (impl->source.props == NULL || impl->sink.props == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
pw_log_error( "can't create properties: %m");
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl->module = module;
|
|
|
|
|
impl->context = context;
|
|
|
|
|
impl->main_loop = pw_context_get_main_loop(context);
|
2024-04-22 16:19:02 +02:00
|
|
|
impl->data_loop = pw_context_acquire_loop(context, &props->dict);
|
2023-05-28 15:49:09 +02:00
|
|
|
impl->system = impl->main_loop->system;
|
2024-03-08 12:54:12 +01:00
|
|
|
impl->reset_work_id = SPA_ID_INVALID;
|
2023-05-28 15:49:09 +02:00
|
|
|
|
|
|
|
|
impl->source.impl = impl;
|
|
|
|
|
impl->source.direction = PW_DIRECTION_OUTPUT;
|
|
|
|
|
impl->sink.impl = impl;
|
|
|
|
|
impl->sink.direction = PW_DIRECTION_INPUT;
|
|
|
|
|
|
|
|
|
|
impl->mode = MODE_DUPLEX;
|
|
|
|
|
if ((str = pw_properties_get(props, "driver.mode")) != NULL) {
|
|
|
|
|
if (spa_streq(str, "source")) {
|
|
|
|
|
impl->mode = MODE_SOURCE;
|
|
|
|
|
} else if (spa_streq(str, "sink")) {
|
|
|
|
|
impl->mode = MODE_SINK;
|
|
|
|
|
} else if (spa_streq(str, "duplex")) {
|
|
|
|
|
impl->mode = MODE_DUPLEX;
|
|
|
|
|
} else {
|
|
|
|
|
pw_log_error("invalid driver.mode '%s'", str);
|
|
|
|
|
res = -EINVAL;
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-06 17:50:42 +01:00
|
|
|
impl->ffado_timer = pw_loop_add_timer(impl->data_loop, on_ffado_timeout, impl);
|
|
|
|
|
if (impl->ffado_timer == NULL) {
|
|
|
|
|
pw_log_error("can't create ffado timer: %m");
|
|
|
|
|
res = -errno;
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
2023-05-28 15:49:09 +02:00
|
|
|
|
2024-04-22 16:19:02 +02:00
|
|
|
pw_properties_set(props, PW_KEY_NODE_LOOP_NAME, impl->data_loop->name);
|
2023-05-28 15:49:09 +02:00
|
|
|
if (pw_properties_get(props, PW_KEY_NODE_VIRTUAL) == NULL)
|
|
|
|
|
pw_properties_set(props, PW_KEY_NODE_VIRTUAL, "true");
|
|
|
|
|
if (pw_properties_get(props, PW_KEY_NODE_GROUP) == NULL)
|
|
|
|
|
pw_properties_set(props, PW_KEY_NODE_GROUP, "ffado-group");
|
2024-03-06 17:50:42 +01:00
|
|
|
if (pw_properties_get(props, PW_KEY_NODE_LINK_GROUP) == NULL)
|
|
|
|
|
pw_properties_set(props, PW_KEY_NODE_LINK_GROUP, "ffado-group");
|
2024-03-11 16:55:35 +01:00
|
|
|
if (pw_properties_get(props, PW_KEY_NODE_PAUSE_ON_IDLE) == NULL)
|
|
|
|
|
pw_properties_set(props, PW_KEY_NODE_PAUSE_ON_IDLE, "false");
|
2023-05-28 15:49:09 +02:00
|
|
|
|
|
|
|
|
pw_properties_set(impl->sink.props, PW_KEY_MEDIA_CLASS, "Audio/Sink");
|
2024-03-06 17:50:42 +01:00
|
|
|
pw_properties_set(impl->sink.props, PW_KEY_PRIORITY_DRIVER, "35000");
|
2024-05-22 09:37:39 +02:00
|
|
|
pw_properties_set(impl->sink.props, PW_KEY_PRIORITY_SESSION, "2000");
|
2023-05-28 15:49:09 +02:00
|
|
|
pw_properties_set(impl->sink.props, PW_KEY_NODE_NAME, "ffado_sink");
|
|
|
|
|
pw_properties_set(impl->sink.props, PW_KEY_NODE_DESCRIPTION, "FFADO Sink");
|
|
|
|
|
|
|
|
|
|
pw_properties_set(impl->source.props, PW_KEY_MEDIA_CLASS, "Audio/Source");
|
2024-03-06 17:50:42 +01:00
|
|
|
pw_properties_set(impl->source.props, PW_KEY_PRIORITY_DRIVER, "35001");
|
2024-05-22 09:37:39 +02:00
|
|
|
pw_properties_set(impl->source.props, PW_KEY_PRIORITY_SESSION, "2001");
|
2023-05-28 15:49:09 +02:00
|
|
|
pw_properties_set(impl->source.props, PW_KEY_NODE_NAME, "ffado_source");
|
|
|
|
|
pw_properties_set(impl->source.props, PW_KEY_NODE_DESCRIPTION, "FFADO Source");
|
|
|
|
|
|
|
|
|
|
if ((str = pw_properties_get(props, "sink.props")) != NULL)
|
|
|
|
|
pw_properties_update_string(impl->sink.props, str, strlen(str));
|
|
|
|
|
if ((str = pw_properties_get(props, "source.props")) != NULL)
|
|
|
|
|
pw_properties_update_string(impl->source.props, str, strlen(str));
|
|
|
|
|
|
2024-04-22 16:19:02 +02:00
|
|
|
copy_props(impl, props, PW_KEY_NODE_LOOP_NAME);
|
2024-03-11 13:22:20 +01:00
|
|
|
copy_props(impl, props, PW_KEY_NODE_LINK_GROUP);
|
2023-05-28 15:49:09 +02:00
|
|
|
copy_props(impl, props, PW_KEY_NODE_GROUP);
|
|
|
|
|
copy_props(impl, props, PW_KEY_NODE_VIRTUAL);
|
2024-03-11 16:55:35 +01:00
|
|
|
copy_props(impl, props, PW_KEY_NODE_PAUSE_ON_IDLE);
|
2023-05-28 15:49:09 +02:00
|
|
|
|
|
|
|
|
parse_audio_info(impl->source.props, &impl->source.info);
|
|
|
|
|
parse_audio_info(impl->sink.props, &impl->sink.info);
|
|
|
|
|
|
|
|
|
|
impl->core = pw_context_get_object(impl->context, PW_TYPE_INTERFACE_Core);
|
|
|
|
|
if (impl->core == NULL) {
|
|
|
|
|
str = pw_properties_get(props, PW_KEY_REMOTE_NAME);
|
|
|
|
|
impl->core = pw_context_connect(impl->context,
|
|
|
|
|
pw_properties_new(
|
|
|
|
|
PW_KEY_REMOTE_NAME, str,
|
|
|
|
|
NULL),
|
|
|
|
|
0);
|
|
|
|
|
impl->do_disconnect = true;
|
|
|
|
|
}
|
|
|
|
|
if (impl->core == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
pw_log_error("can't connect: %m");
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pw_proxy_add_listener((struct pw_proxy*)impl->core,
|
|
|
|
|
&impl->core_proxy_listener,
|
|
|
|
|
&core_proxy_events, impl);
|
|
|
|
|
pw_core_add_listener(impl->core,
|
|
|
|
|
&impl->core_listener,
|
|
|
|
|
&core_events, impl);
|
|
|
|
|
|
2024-03-19 13:20:29 +01:00
|
|
|
if ((res = probe_ffado_device(impl)) < 0)
|
2023-05-28 15:49:09 +02:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
pw_impl_module_add_listener(module, &impl->module_listener, &module_events, impl);
|
|
|
|
|
|
|
|
|
|
pw_impl_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_props));
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
impl_destroy(impl);
|
|
|
|
|
return res;
|
|
|
|
|
}
|