2018-02-15 17:54:08 +01:00
|
|
|
/* PipeWire
|
|
|
|
|
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Library General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <math.h>
|
2018-04-24 17:03:56 +02:00
|
|
|
#include <time.h>
|
2018-02-15 17:54:08 +01:00
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include <spa/node/node.h>
|
|
|
|
|
#include <spa/utils/hook.h>
|
|
|
|
|
#include <spa/param/audio/format-utils.h>
|
|
|
|
|
|
|
|
|
|
#include <spa/lib/pod.h>
|
|
|
|
|
#include <spa/lib/debug.h>
|
|
|
|
|
|
|
|
|
|
#include "pipewire/core.h"
|
|
|
|
|
#include "pipewire/link.h"
|
|
|
|
|
#include "pipewire/log.h"
|
|
|
|
|
#include "pipewire/module.h"
|
|
|
|
|
#include "pipewire/type.h"
|
|
|
|
|
#include "pipewire/private.h"
|
|
|
|
|
|
|
|
|
|
#define NAME "dsp"
|
|
|
|
|
|
2018-02-20 09:32:40 +01:00
|
|
|
#define MAX_PORTS 256
|
|
|
|
|
#define MAX_BUFFERS 8
|
|
|
|
|
|
2018-03-14 11:52:13 +01:00
|
|
|
#define DEFAULT_CHANNELS 2
|
|
|
|
|
#define DEFAULT_SAMPLE_RATE 44100
|
|
|
|
|
#define DEFAULT_BUFFER_SIZE 1024
|
|
|
|
|
|
2018-02-15 17:54:08 +01:00
|
|
|
struct type {
|
|
|
|
|
struct spa_type_media_type media_type;
|
|
|
|
|
struct spa_type_media_subtype media_subtype;
|
|
|
|
|
struct spa_type_format_audio format_audio;
|
|
|
|
|
struct spa_type_audio_format audio_format;
|
|
|
|
|
struct spa_type_media_subtype_audio media_subtype_audio;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static inline void init_type(struct type *type, struct spa_type_map *map)
|
|
|
|
|
{
|
|
|
|
|
spa_type_media_type_map(map, &type->media_type);
|
|
|
|
|
spa_type_media_subtype_map(map, &type->media_subtype);
|
|
|
|
|
spa_type_format_audio_map(map, &type->format_audio);
|
|
|
|
|
spa_type_audio_format_map(map, &type->audio_format);
|
|
|
|
|
spa_type_media_subtype_audio_map(map, &type->media_subtype_audio);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct impl {
|
|
|
|
|
struct type type;
|
|
|
|
|
|
|
|
|
|
struct pw_core *core;
|
|
|
|
|
struct pw_type *t;
|
|
|
|
|
struct pw_module *module;
|
|
|
|
|
struct spa_hook core_listener;
|
|
|
|
|
struct spa_hook module_listener;
|
|
|
|
|
struct pw_properties *properties;
|
|
|
|
|
|
2018-02-16 12:02:39 +01:00
|
|
|
int node_count;
|
2018-02-15 17:54:08 +01:00
|
|
|
|
|
|
|
|
struct spa_list node_list;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct buffer {
|
2018-03-09 12:39:14 +01:00
|
|
|
#define BUFFER_FLAG_OUT (1<<0)
|
2018-03-08 11:08:37 +01:00
|
|
|
uint32_t flags;
|
2018-02-15 17:54:08 +01:00
|
|
|
struct spa_list link;
|
2018-04-19 20:15:30 +02:00
|
|
|
struct spa_buffer *buf;
|
2018-02-15 17:54:08 +01:00
|
|
|
void *ptr;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct port {
|
|
|
|
|
struct pw_port *port;
|
|
|
|
|
struct spa_hook port_listener;
|
|
|
|
|
struct node *node;
|
|
|
|
|
|
|
|
|
|
#define PORT_FLAG_DSP (1<<0)
|
|
|
|
|
#define PORT_FLAG_RAW_F32 (1<<1)
|
|
|
|
|
#define PORT_FLAG_MIDI (1<<2)
|
|
|
|
|
uint32_t flags;
|
|
|
|
|
|
|
|
|
|
struct spa_port_info info;
|
|
|
|
|
|
|
|
|
|
struct spa_io_buffers *io;
|
|
|
|
|
|
2018-02-20 09:32:40 +01:00
|
|
|
struct buffer buffers[MAX_BUFFERS];
|
2018-02-15 17:54:08 +01:00
|
|
|
uint32_t n_buffers;
|
|
|
|
|
struct spa_list queue;
|
|
|
|
|
|
2018-04-27 17:30:45 +02:00
|
|
|
int stride;
|
2018-02-15 17:54:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define GET_IN_PORT(n,p) (n->in_ports[p])
|
|
|
|
|
#define GET_OUT_PORT(n,p) (n->out_ports[p])
|
|
|
|
|
#define GET_PORT(n,d,p) (d == SPA_DIRECTION_INPUT ? GET_IN_PORT(n,p) : GET_OUT_PORT(n,p))
|
|
|
|
|
|
2018-04-27 17:30:45 +02:00
|
|
|
typedef void (*conv_func_t)(void *dst, void *src, int index, int n_samples, int stride);
|
|
|
|
|
typedef void (*fill_func_t)(void *dst, int index, int n_samples, int stride);
|
|
|
|
|
|
2018-02-15 17:54:08 +01:00
|
|
|
struct node {
|
|
|
|
|
struct spa_list link;
|
2018-04-24 17:03:56 +02:00
|
|
|
|
2018-02-15 17:54:08 +01:00
|
|
|
struct pw_node *node;
|
2018-04-24 17:03:56 +02:00
|
|
|
struct spa_hook node_listener;
|
2018-02-15 17:54:08 +01:00
|
|
|
|
|
|
|
|
struct impl *impl;
|
|
|
|
|
|
|
|
|
|
int channels;
|
|
|
|
|
int sample_rate;
|
|
|
|
|
int buffer_size;
|
|
|
|
|
|
2018-04-27 17:30:45 +02:00
|
|
|
conv_func_t conv_func;
|
|
|
|
|
fill_func_t fill_func;
|
|
|
|
|
|
2018-02-15 17:54:08 +01:00
|
|
|
struct spa_node node_impl;
|
|
|
|
|
|
|
|
|
|
struct port *in_ports[MAX_PORTS];
|
|
|
|
|
int n_in_ports;
|
|
|
|
|
struct port *out_ports[MAX_PORTS];
|
|
|
|
|
int n_out_ports;
|
2018-02-16 12:02:39 +01:00
|
|
|
|
|
|
|
|
int port_count[2];
|
2018-02-15 17:54:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** \endcond */
|
|
|
|
|
|
|
|
|
|
static int node_enum_params(struct spa_node *node,
|
|
|
|
|
uint32_t id, uint32_t *index,
|
|
|
|
|
const struct spa_pod *filter,
|
|
|
|
|
struct spa_pod **param,
|
|
|
|
|
struct spa_pod_builder *builder)
|
|
|
|
|
{
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int node_set_param(struct spa_node *node,
|
|
|
|
|
uint32_t id, uint32_t flags,
|
|
|
|
|
const struct spa_pod *param)
|
|
|
|
|
{
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int node_send_command(struct spa_node *node,
|
|
|
|
|
const struct spa_command *command)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int node_set_callbacks(struct spa_node *node,
|
|
|
|
|
const struct spa_node_callbacks *callbacks, void *data)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int node_get_n_ports(struct spa_node *node,
|
|
|
|
|
uint32_t *n_input_ports,
|
|
|
|
|
uint32_t *max_input_ports,
|
|
|
|
|
uint32_t *n_output_ports,
|
|
|
|
|
uint32_t *max_output_ports)
|
|
|
|
|
{
|
|
|
|
|
struct node *n = SPA_CONTAINER_OF(node, struct node, node_impl);
|
|
|
|
|
|
|
|
|
|
if (n_input_ports)
|
|
|
|
|
*n_input_ports = n->n_in_ports;
|
|
|
|
|
if (max_input_ports)
|
|
|
|
|
*max_input_ports = n->n_in_ports;
|
|
|
|
|
if (n_output_ports)
|
|
|
|
|
*n_output_ports = n->n_out_ports;
|
|
|
|
|
if (max_output_ports)
|
|
|
|
|
*max_output_ports = n->n_out_ports;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int node_get_port_ids(struct spa_node *node,
|
|
|
|
|
uint32_t *input_ids,
|
|
|
|
|
uint32_t n_input_ids,
|
|
|
|
|
uint32_t *output_ids,
|
|
|
|
|
uint32_t n_output_ids)
|
|
|
|
|
{
|
|
|
|
|
struct node *n = SPA_CONTAINER_OF(node, struct node, node_impl);
|
2018-02-20 18:19:11 +01:00
|
|
|
uint32_t i, c;
|
2018-02-15 17:54:08 +01:00
|
|
|
|
|
|
|
|
for (c = i = 0; i < n->n_in_ports && c < n_input_ids; i++) {
|
|
|
|
|
if (GET_IN_PORT(n, i))
|
|
|
|
|
input_ids[c++] = i;
|
|
|
|
|
}
|
|
|
|
|
for (c = i = 0; i < n->n_out_ports && c < n_output_ids; i++) {
|
|
|
|
|
if (GET_OUT_PORT(n, i))
|
|
|
|
|
output_ids[c++] = i;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int node_add_port(struct spa_node *node, enum spa_direction direction, uint32_t port_id)
|
|
|
|
|
{
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int node_remove_port(struct spa_node *node, enum spa_direction direction, uint32_t port_id)
|
|
|
|
|
{
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int clear_buffers(struct node *n, struct port *p)
|
|
|
|
|
{
|
|
|
|
|
if (p->n_buffers > 0) {
|
|
|
|
|
pw_log_info(NAME " %p: clear buffers %p", n, p);
|
|
|
|
|
p->n_buffers = 0;
|
|
|
|
|
spa_list_init(&p->queue);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 17:30:45 +02:00
|
|
|
|
|
|
|
|
static void conv_f32_s16(void *dst, void *src, int index, int n_samples, int stride)
|
2018-02-15 17:54:08 +01:00
|
|
|
{
|
2018-04-27 17:30:45 +02:00
|
|
|
int16_t *d = dst;
|
|
|
|
|
float *s = src;
|
2018-02-15 17:54:08 +01:00
|
|
|
int i;
|
2018-04-27 17:30:45 +02:00
|
|
|
d += index;
|
2018-02-15 17:54:08 +01:00
|
|
|
for (i = 0; i < n_samples; i++) {
|
2018-04-27 17:30:45 +02:00
|
|
|
if (s[i] < -1.0f)
|
|
|
|
|
*d = -((1 << 15) - 1);
|
|
|
|
|
else if (s[i] >= 1.0f)
|
|
|
|
|
*d = (1U << 15) - 1;
|
2018-02-15 17:54:08 +01:00
|
|
|
else
|
2018-03-14 11:52:13 +01:00
|
|
|
//*out = lrintf(in[i] * 32767.0f);
|
2018-04-27 17:30:45 +02:00
|
|
|
*d = s[i] * (1.0f * ((1U << 15) - 1));
|
|
|
|
|
d += stride;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void conv_s16_f32(void *dst, void *src, int index, int n_samples, int stride)
|
|
|
|
|
{
|
|
|
|
|
float *d = dst;
|
|
|
|
|
int16_t *s = src;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
s += index;
|
|
|
|
|
for (i = 0; i < n_samples; i++) {
|
|
|
|
|
d[i] = *s * (1.0f / ((1U << 15) - 1));
|
|
|
|
|
s += stride;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void fill_s16(void *dst, int index, int n_samples, int stride)
|
|
|
|
|
{
|
|
|
|
|
int16_t *d = dst;
|
|
|
|
|
int i;
|
|
|
|
|
d += index;
|
|
|
|
|
for (i = 0; i < n_samples; i++) {
|
|
|
|
|
*d = 0;
|
|
|
|
|
d += stride;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void conv_f32_s32(void *dst, void *src, int index, int n_samples, int stride)
|
|
|
|
|
{
|
|
|
|
|
int32_t *d = dst;
|
|
|
|
|
float *s = src;
|
|
|
|
|
int i;
|
|
|
|
|
d += index;
|
|
|
|
|
for (i = 0; i < n_samples; i++) {
|
|
|
|
|
if (s[i] < -1.0f)
|
|
|
|
|
*d = -((1U << 31) - 1);
|
|
|
|
|
else if (s[i] >= 1.0f)
|
|
|
|
|
*d = (1U << 31) - 1;
|
|
|
|
|
else
|
|
|
|
|
*d = s[i] * (1.0f * ((1U << 31) - 1));
|
|
|
|
|
d += stride;
|
2018-02-15 17:54:08 +01:00
|
|
|
}
|
|
|
|
|
}
|
2018-04-26 17:23:27 +02:00
|
|
|
|
2018-04-27 17:30:45 +02:00
|
|
|
static void conv_s32_f32(void *dst, void *src, int index, int n_samples, int stride)
|
2018-04-26 17:23:27 +02:00
|
|
|
{
|
2018-04-27 17:30:45 +02:00
|
|
|
float *d = dst;
|
|
|
|
|
int32_t *s = src;
|
2018-04-26 17:23:27 +02:00
|
|
|
int i;
|
2018-04-27 17:30:45 +02:00
|
|
|
|
|
|
|
|
s += index;
|
2018-04-26 17:23:27 +02:00
|
|
|
for (i = 0; i < n_samples; i++) {
|
2018-04-27 17:30:45 +02:00
|
|
|
d[i] = *s * (1.0f / ((1U << 31) - 1));
|
|
|
|
|
s += stride;
|
2018-04-26 17:23:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 17:30:45 +02:00
|
|
|
static void fill_s32(void *dst, int index, int n_samples, int stride)
|
2018-02-15 17:54:08 +01:00
|
|
|
{
|
2018-04-27 17:30:45 +02:00
|
|
|
int32_t *d = dst;
|
2018-02-15 17:54:08 +01:00
|
|
|
int i;
|
2018-04-27 17:30:45 +02:00
|
|
|
d += index;
|
2018-02-15 17:54:08 +01:00
|
|
|
for (i = 0; i < n_samples; i++) {
|
2018-04-27 17:30:45 +02:00
|
|
|
*d = 0;
|
|
|
|
|
d += stride;
|
2018-02-15 17:54:08 +01:00
|
|
|
}
|
|
|
|
|
}
|
2018-04-27 17:30:45 +02:00
|
|
|
|
2018-02-15 17:54:08 +01:00
|
|
|
static void add_f32(float *out, float *in, int n_samples)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < n_samples; i++)
|
|
|
|
|
out[i] += in[i];
|
|
|
|
|
}
|
2018-03-08 11:08:37 +01:00
|
|
|
|
|
|
|
|
static struct buffer * peek_buffer(struct node *n, struct port *p)
|
|
|
|
|
{
|
|
|
|
|
if (spa_list_is_empty(&p->queue))
|
|
|
|
|
return NULL;
|
|
|
|
|
return spa_list_first(&p->queue, struct buffer, link);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-09 12:39:14 +01:00
|
|
|
static void dequeue_buffer(struct node *n, struct buffer *b)
|
2018-03-08 11:08:37 +01:00
|
|
|
{
|
2018-04-19 20:15:30 +02:00
|
|
|
pw_log_trace("dequeue buffer %d", b->buf->id);
|
2018-03-09 12:39:14 +01:00
|
|
|
spa_list_remove(&b->link);
|
|
|
|
|
SPA_FLAG_SET(b->flags, BUFFER_FLAG_OUT);
|
2018-03-08 11:08:37 +01:00
|
|
|
}
|
2018-02-15 17:54:08 +01:00
|
|
|
|
2018-04-19 20:15:30 +02:00
|
|
|
static void queue_buffer(struct node *n, struct port *p, uint32_t id)
|
|
|
|
|
{
|
|
|
|
|
struct buffer *b = &p->buffers[id];
|
|
|
|
|
if (SPA_FLAG_CHECK(b->flags, BUFFER_FLAG_OUT)) {
|
|
|
|
|
pw_log_trace("queue buffer %d", id);
|
|
|
|
|
spa_list_append(&p->queue, &b->link);
|
|
|
|
|
SPA_FLAG_UNSET(b->flags, BUFFER_FLAG_OUT);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 17:23:27 +02:00
|
|
|
static int node_process_mix(struct spa_node *node)
|
2018-02-15 17:54:08 +01:00
|
|
|
{
|
|
|
|
|
struct node *n = SPA_CONTAINER_OF(node, struct node, node_impl);
|
|
|
|
|
struct pw_node *this = n->node;
|
|
|
|
|
struct port *outp = GET_OUT_PORT(n, 0);
|
|
|
|
|
struct spa_io_buffers *outio = outp->io;
|
|
|
|
|
struct buffer *out;
|
|
|
|
|
|
|
|
|
|
pw_log_trace(NAME " %p: process input", this);
|
|
|
|
|
|
|
|
|
|
if (outio->status == SPA_STATUS_HAVE_BUFFER)
|
|
|
|
|
return SPA_STATUS_HAVE_BUFFER;
|
|
|
|
|
|
2018-04-19 20:15:30 +02:00
|
|
|
if (outio->buffer_id < outp->n_buffers) {
|
|
|
|
|
queue_buffer(n, outp, outio->buffer_id);
|
|
|
|
|
outio->buffer_id = SPA_ID_INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-09 12:39:14 +01:00
|
|
|
out = peek_buffer(n, outp);
|
2018-02-15 17:54:08 +01:00
|
|
|
if (out == NULL) {
|
|
|
|
|
pw_log_warn(NAME " %p: out of buffers", this);
|
|
|
|
|
return -EPIPE;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-14 11:52:13 +01:00
|
|
|
dequeue_buffer(n, out);
|
2018-04-19 20:15:30 +02:00
|
|
|
outio->buffer_id = out->buf->id;
|
2018-03-14 11:52:13 +01:00
|
|
|
outio->status = SPA_STATUS_HAVE_BUFFER;
|
|
|
|
|
|
2018-04-19 20:15:30 +02:00
|
|
|
out->buf->datas[0].chunk->offset = 0;
|
2018-04-27 17:30:45 +02:00
|
|
|
out->buf->datas[0].chunk->size = n->buffer_size * outp->stride;
|
|
|
|
|
out->buf->datas[0].chunk->stride = outp->stride;
|
|
|
|
|
|
|
|
|
|
pw_log_trace(NAME " %p: output buffer %d %d %d", this,
|
|
|
|
|
out->buf->id, out->flags, out->buf->datas[0].chunk->size);
|
2018-02-15 17:54:08 +01:00
|
|
|
|
|
|
|
|
return outio->status;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 17:23:27 +02:00
|
|
|
static int node_process_split(struct spa_node *node)
|
|
|
|
|
{
|
|
|
|
|
struct node *n = SPA_CONTAINER_OF(node, struct node, node_impl);
|
|
|
|
|
struct pw_node *this = n->node;
|
|
|
|
|
struct port *inp = GET_IN_PORT(n, 0);
|
|
|
|
|
struct spa_io_buffers *inio = inp->io;
|
|
|
|
|
struct buffer *in;
|
2018-04-27 17:30:45 +02:00
|
|
|
int i, res, channels = n->channels;
|
2018-04-26 17:23:27 +02:00
|
|
|
size_t buffer_size = n->buffer_size;
|
|
|
|
|
|
|
|
|
|
if (inio->status != SPA_STATUS_HAVE_BUFFER)
|
|
|
|
|
return SPA_STATUS_NEED_BUFFER;
|
|
|
|
|
|
|
|
|
|
if (inio->buffer_id >= inp->n_buffers)
|
|
|
|
|
return inio->status = -EINVAL;
|
|
|
|
|
|
|
|
|
|
in = &inp->buffers[inio->buffer_id];
|
|
|
|
|
res = SPA_STATUS_NEED_BUFFER;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < channels; i++) {
|
|
|
|
|
struct port *outp = GET_OUT_PORT(n, i);
|
2018-04-27 17:30:45 +02:00
|
|
|
struct spa_io_buffers *outio;
|
2018-04-26 17:23:27 +02:00
|
|
|
struct buffer *out;
|
|
|
|
|
|
2018-04-27 17:30:45 +02:00
|
|
|
if (outp == NULL ||
|
|
|
|
|
(outio = outp->io) == NULL ||
|
|
|
|
|
outp->n_buffers == 0 ||
|
2018-04-26 17:23:27 +02:00
|
|
|
outio->status != SPA_STATUS_NEED_BUFFER)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (outio->buffer_id < outp->n_buffers) {
|
|
|
|
|
queue_buffer(n, outp, outio->buffer_id);
|
|
|
|
|
outio->buffer_id = SPA_ID_INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((out = peek_buffer(n, outp)) == NULL) {
|
|
|
|
|
pw_log_warn(NAME " %p: out of buffers on port %d", this, i);
|
|
|
|
|
outp->io->status = -EPIPE;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
dequeue_buffer(n, out);
|
|
|
|
|
outio->status = SPA_STATUS_HAVE_BUFFER;
|
|
|
|
|
outio->buffer_id = out->buf->id;
|
|
|
|
|
|
2018-04-27 17:30:45 +02:00
|
|
|
n->conv_func(out->ptr, in->ptr, i, buffer_size, channels);
|
2018-04-26 17:23:27 +02:00
|
|
|
|
|
|
|
|
out->buf->datas[0].chunk->offset = 0;
|
2018-04-27 17:30:45 +02:00
|
|
|
out->buf->datas[0].chunk->size = buffer_size * outp->stride;
|
|
|
|
|
out->buf->datas[0].chunk->stride = outp->stride;
|
|
|
|
|
|
|
|
|
|
pw_log_trace(NAME " %p: output buffer %d %ld", this,
|
|
|
|
|
outio->buffer_id, buffer_size * outp->stride);
|
2018-04-26 17:23:27 +02:00
|
|
|
|
|
|
|
|
res |= SPA_STATUS_HAVE_BUFFER;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-15 17:54:08 +01:00
|
|
|
static int port_set_io(struct spa_node *node,
|
|
|
|
|
enum spa_direction direction, uint32_t port_id,
|
|
|
|
|
uint32_t id, void *data, size_t size)
|
|
|
|
|
{
|
|
|
|
|
struct node *n = SPA_CONTAINER_OF(node, struct node, node_impl);
|
|
|
|
|
struct pw_type *t = n->impl->t;
|
|
|
|
|
struct port *p = GET_PORT(n, direction, port_id);
|
|
|
|
|
|
|
|
|
|
if (id == t->io.Buffers)
|
|
|
|
|
p->io = data;
|
|
|
|
|
else
|
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int port_get_info(struct spa_node *node, enum spa_direction direction, uint32_t port_id,
|
|
|
|
|
const struct spa_port_info **info)
|
|
|
|
|
{
|
|
|
|
|
struct node *n = SPA_CONTAINER_OF(node, struct node, node_impl);
|
|
|
|
|
struct port *p = GET_PORT(n, direction, port_id);
|
|
|
|
|
|
|
|
|
|
p->info.flags = SPA_PORT_INFO_FLAG_CAN_USE_BUFFERS | SPA_PORT_INFO_FLAG_LIVE;
|
|
|
|
|
// if (direction == SPA_DIRECTION_OUTPUT)
|
|
|
|
|
// p->info.flags |= SPA_PORT_INFO_FLAG_CAN_ALLOC_BUFFERS;
|
|
|
|
|
|
|
|
|
|
if (SPA_FLAG_CHECK(p->flags, PORT_FLAG_DSP))
|
|
|
|
|
p->info.flags |= SPA_PORT_INFO_FLAG_PHYSICAL | SPA_PORT_INFO_FLAG_TERMINAL;
|
|
|
|
|
|
|
|
|
|
p->info.rate = n->sample_rate;
|
|
|
|
|
*info = &p->info;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int port_enum_formats(struct spa_node *node,
|
2018-04-27 17:30:45 +02:00
|
|
|
struct port *p,
|
2018-02-15 17:54:08 +01:00
|
|
|
uint32_t *index,
|
|
|
|
|
const struct spa_pod *filter,
|
|
|
|
|
struct spa_pod **param,
|
|
|
|
|
struct spa_pod_builder *builder)
|
|
|
|
|
{
|
|
|
|
|
struct node *n = SPA_CONTAINER_OF(node, struct node, node_impl);
|
|
|
|
|
struct pw_type *type = n->impl->t;
|
|
|
|
|
struct type *t = &n->impl->type;
|
|
|
|
|
|
|
|
|
|
if (*index > 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (SPA_FLAG_CHECK(p->flags, PORT_FLAG_DSP)) {
|
|
|
|
|
if (SPA_FLAG_CHECK(p->flags, PORT_FLAG_RAW_F32)) {
|
|
|
|
|
*param = spa_pod_builder_object(builder,
|
|
|
|
|
type->param.idEnumFormat, type->spa_format,
|
|
|
|
|
"I", t->media_type.audio,
|
|
|
|
|
"I", t->media_subtype.raw,
|
|
|
|
|
":", t->format_audio.format, "I", t->audio_format.F32,
|
2018-04-13 19:58:55 +02:00
|
|
|
":", t->format_audio.layout, "i", SPA_AUDIO_LAYOUT_NON_INTERLEAVED,
|
2018-02-15 17:54:08 +01:00
|
|
|
":", t->format_audio.rate, "i", n->sample_rate,
|
|
|
|
|
":", t->format_audio.channels, "i", 1);
|
|
|
|
|
}
|
|
|
|
|
else if (SPA_FLAG_CHECK(p->flags, PORT_FLAG_MIDI)) {
|
|
|
|
|
*param = spa_pod_builder_object(builder,
|
|
|
|
|
type->param.idEnumFormat, type->spa_format,
|
|
|
|
|
"I", t->media_type.audio,
|
|
|
|
|
"I", t->media_subtype_audio.midi);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
*param = spa_pod_builder_object(builder,
|
|
|
|
|
type->param.idEnumFormat, type->spa_format,
|
|
|
|
|
"I", t->media_type.audio,
|
|
|
|
|
"I", t->media_subtype.raw,
|
2018-04-27 17:30:45 +02:00
|
|
|
":", t->format_audio.format, "Ieu", t->audio_format.S16,
|
|
|
|
|
SPA_POD_PROP_ENUM(2, t->audio_format.S16,
|
|
|
|
|
t->audio_format.S32),
|
2018-04-19 20:09:10 +02:00
|
|
|
":", t->format_audio.layout, "i", SPA_AUDIO_LAYOUT_INTERLEAVED,
|
2018-04-24 17:03:56 +02:00
|
|
|
":", t->format_audio.rate, "iru", n->sample_rate,
|
|
|
|
|
SPA_POD_PROP_MIN_MAX(1, INT32_MAX),
|
|
|
|
|
":", t->format_audio.channels, "iru", n->channels,
|
|
|
|
|
SPA_POD_PROP_MIN_MAX(1, MAX_PORTS));
|
2018-02-15 17:54:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int port_enum_params(struct spa_node *node,
|
|
|
|
|
enum spa_direction direction, uint32_t port_id,
|
|
|
|
|
uint32_t id, uint32_t *index,
|
|
|
|
|
const struct spa_pod *filter,
|
|
|
|
|
struct spa_pod **result,
|
|
|
|
|
struct spa_pod_builder *builder)
|
|
|
|
|
{
|
|
|
|
|
struct node *n = SPA_CONTAINER_OF(node, struct node, node_impl);
|
2018-04-27 17:30:45 +02:00
|
|
|
struct port *p = GET_PORT(n, direction, port_id);
|
2018-02-16 18:15:37 +01:00
|
|
|
struct pw_type *t = n->impl->t;
|
2018-02-15 17:54:08 +01:00
|
|
|
struct spa_pod *param;
|
|
|
|
|
struct spa_pod_builder b = { 0 };
|
|
|
|
|
uint8_t buffer[1024];
|
|
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
next:
|
|
|
|
|
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
|
|
|
|
|
2018-02-16 18:15:37 +01:00
|
|
|
if (id == t->param.idEnumFormat) {
|
2018-04-27 17:30:45 +02:00
|
|
|
if ((res = port_enum_formats(node, p, index, filter, ¶m, &b)) <= 0)
|
2018-02-15 17:54:08 +01:00
|
|
|
return res;
|
|
|
|
|
}
|
2018-02-16 18:15:37 +01:00
|
|
|
else if (id == t->param.idFormat) {
|
2018-04-27 17:30:45 +02:00
|
|
|
if ((res = port_enum_formats(node, p, index, filter, ¶m, &b)) <= 0)
|
2018-02-15 17:54:08 +01:00
|
|
|
return res;
|
|
|
|
|
}
|
2018-02-16 18:15:37 +01:00
|
|
|
else if (id == t->param.idBuffers) {
|
|
|
|
|
if (*index > 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
param = spa_pod_builder_object(&b,
|
|
|
|
|
id, t->param_buffers.Buffers,
|
2018-04-27 17:30:45 +02:00
|
|
|
":", t->param_buffers.size, "i", n->buffer_size * p->stride,
|
2018-03-14 11:52:13 +01:00
|
|
|
// SPA_POD_PROP_MIN_MAX(24, 4096),
|
2018-04-13 19:58:55 +02:00
|
|
|
":", t->param_buffers.blocks, "i", 1,
|
2018-04-27 17:30:45 +02:00
|
|
|
":", t->param_buffers.stride, "i", p->stride,
|
2018-04-26 17:23:27 +02:00
|
|
|
":", t->param_buffers.buffers, "ir", 2,
|
2018-02-20 09:32:40 +01:00
|
|
|
SPA_POD_PROP_MIN_MAX(1, MAX_BUFFERS),
|
2018-02-16 18:15:37 +01:00
|
|
|
":", t->param_buffers.align, "i", 16);
|
|
|
|
|
}
|
2018-02-15 17:54:08 +01:00
|
|
|
else
|
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
|
|
(*index)++;
|
|
|
|
|
|
|
|
|
|
if ((res = spa_pod_filter(builder, result, param, filter)) < 0)
|
|
|
|
|
goto next;
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int port_set_format(struct spa_node *node, struct port *p,
|
2018-04-27 17:30:45 +02:00
|
|
|
enum spa_direction direction,
|
2018-02-15 17:54:08 +01:00
|
|
|
uint32_t flags, const struct spa_pod *format)
|
|
|
|
|
{
|
|
|
|
|
struct spa_audio_info info = { 0 };
|
|
|
|
|
struct node *n = SPA_CONTAINER_OF(node, struct node, node_impl);
|
|
|
|
|
struct type *t = &n->impl->type;
|
|
|
|
|
|
|
|
|
|
if (format == NULL) {
|
|
|
|
|
clear_buffers(n, p);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spa_pod_object_parse(format,
|
|
|
|
|
"I", &info.media_type,
|
|
|
|
|
"I", &info.media_subtype);
|
|
|
|
|
|
|
|
|
|
if (info.media_type != t->media_type.audio ||
|
|
|
|
|
info.media_subtype != t->media_subtype.raw)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
if (spa_format_audio_raw_parse(format, &info.info.raw, &t->format_audio) < 0)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
pw_log_info(NAME " %p: set format on port %p", n, p);
|
2018-04-24 17:03:56 +02:00
|
|
|
n->sample_rate = info.info.raw.rate;
|
2018-04-27 17:30:45 +02:00
|
|
|
|
|
|
|
|
if (!SPA_FLAG_CHECK(p->flags, PORT_FLAG_DSP)) {
|
|
|
|
|
n->channels = info.info.raw.channels;
|
|
|
|
|
|
|
|
|
|
if (info.info.raw.format == t->audio_format.S16) {
|
|
|
|
|
p->stride = sizeof(int16_t) * n->channels;
|
|
|
|
|
n->fill_func = fill_s16;
|
|
|
|
|
if (direction == SPA_DIRECTION_INPUT)
|
|
|
|
|
n->conv_func = conv_s16_f32;
|
|
|
|
|
else
|
|
|
|
|
n->conv_func = conv_f32_s16;
|
|
|
|
|
}
|
|
|
|
|
else if (info.info.raw.format == t->audio_format.S32) {
|
|
|
|
|
p->stride = sizeof(int32_t) * n->channels;
|
|
|
|
|
n->fill_func = fill_s32;
|
|
|
|
|
if (direction == SPA_DIRECTION_INPUT)
|
|
|
|
|
n->conv_func = conv_s32_f32;
|
|
|
|
|
else
|
|
|
|
|
n->conv_func = conv_f32_s32;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
p->stride = sizeof(float);
|
|
|
|
|
}
|
2018-02-15 17:54:08 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int port_set_param(struct spa_node *node,
|
|
|
|
|
enum spa_direction direction, uint32_t port_id,
|
|
|
|
|
uint32_t id, uint32_t flags,
|
|
|
|
|
const struct spa_pod *param)
|
|
|
|
|
{
|
|
|
|
|
struct node *n = SPA_CONTAINER_OF(node, struct node, node_impl);
|
|
|
|
|
struct port *p = GET_PORT(n, direction, port_id);
|
|
|
|
|
struct pw_type *t = n->impl->t;
|
|
|
|
|
|
|
|
|
|
if (id == t->param.idFormat) {
|
2018-04-27 17:30:45 +02:00
|
|
|
return port_set_format(node, p, direction, flags, param);
|
2018-02-15 17:54:08 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int port_use_buffers(struct spa_node *node, enum spa_direction direction, uint32_t port_id,
|
|
|
|
|
struct spa_buffer **buffers, uint32_t n_buffers)
|
|
|
|
|
{
|
|
|
|
|
struct node *n = SPA_CONTAINER_OF(node, struct node, node_impl);
|
|
|
|
|
struct port *p = GET_PORT(n, direction, port_id);
|
|
|
|
|
struct pw_type *t = n->impl->t;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
pw_log_debug("use_buffers %d", n_buffers);
|
|
|
|
|
|
|
|
|
|
clear_buffers(n, p);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < n_buffers; i++) {
|
|
|
|
|
struct buffer *b;
|
|
|
|
|
struct spa_data *d = buffers[i]->datas;
|
|
|
|
|
|
|
|
|
|
b = &p->buffers[i];
|
2018-03-08 11:08:37 +01:00
|
|
|
b->flags = 0;
|
2018-04-19 20:15:30 +02:00
|
|
|
b->buf = buffers[i];
|
2018-03-09 12:39:14 +01:00
|
|
|
|
2018-02-15 17:54:08 +01:00
|
|
|
if ((d[0].type == t->data.MemPtr ||
|
|
|
|
|
d[0].type == t->data.MemFd ||
|
|
|
|
|
d[0].type == t->data.DmaBuf) && d[0].data != NULL) {
|
|
|
|
|
b->ptr = d[0].data;
|
|
|
|
|
} else {
|
|
|
|
|
pw_log_error(NAME " %p: invalid memory on buffer %p", p, buffers[i]);
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
spa_list_append(&p->queue, &b->link);
|
|
|
|
|
}
|
|
|
|
|
p->n_buffers = n_buffers;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int port_alloc_buffers(struct spa_node *node, enum spa_direction direction, uint32_t port_id,
|
|
|
|
|
struct spa_pod **params, uint32_t n_params,
|
|
|
|
|
struct spa_buffer **buffers, uint32_t *n_buffers)
|
|
|
|
|
{
|
|
|
|
|
#if 0
|
|
|
|
|
struct node *n = SPA_CONTAINER_OF(node, struct node, node_impl);
|
|
|
|
|
struct port *p = GET_PORT(n, direction, port_id);
|
|
|
|
|
struct pw_type *t = n->impl->t;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
pw_log_debug("alloc %d", *n_buffers);
|
|
|
|
|
for (i = 0; i < *n_buffers; i++) {
|
|
|
|
|
struct buffer *b;
|
|
|
|
|
struct spa_data *d = buffers[i]->datas;
|
|
|
|
|
|
|
|
|
|
b = &p->buffers[i];
|
2018-04-19 20:15:30 +02:00
|
|
|
b->buf = buffers[i];
|
2018-02-15 17:54:08 +01:00
|
|
|
d[0].type = t->data.MemPtr;
|
|
|
|
|
d[0].maxsize = n->buffer_size;
|
|
|
|
|
b->ptr = d[0].data = p->buffer;
|
|
|
|
|
spa_list_append(&p->queue, &b->link);
|
|
|
|
|
}
|
|
|
|
|
p->n_buffers = *n_buffers;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
#else
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int port_reuse_buffer(struct spa_node *node, uint32_t port_id, uint32_t buffer_id)
|
|
|
|
|
{
|
|
|
|
|
struct node *n = SPA_CONTAINER_OF(node, struct node, node_impl);
|
|
|
|
|
struct port *p = GET_OUT_PORT(n, port_id);
|
2018-04-19 20:15:30 +02:00
|
|
|
queue_buffer(n, p, buffer_id);
|
2018-02-15 17:54:08 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int port_send_command(struct spa_node *node, enum spa_direction direction, uint32_t port_id,
|
|
|
|
|
const struct spa_command *command)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct spa_node node_impl = {
|
|
|
|
|
SPA_VERSION_NODE,
|
|
|
|
|
NULL,
|
|
|
|
|
.enum_params = node_enum_params,
|
|
|
|
|
.set_param = node_set_param,
|
|
|
|
|
.send_command = node_send_command,
|
|
|
|
|
.set_callbacks = node_set_callbacks,
|
|
|
|
|
.get_n_ports = node_get_n_ports,
|
|
|
|
|
.get_port_ids = node_get_port_ids,
|
|
|
|
|
.add_port = node_add_port,
|
|
|
|
|
.remove_port = node_remove_port,
|
|
|
|
|
.port_get_info = port_get_info,
|
|
|
|
|
.port_enum_params = port_enum_params,
|
|
|
|
|
.port_set_param = port_set_param,
|
|
|
|
|
.port_use_buffers = port_use_buffers,
|
|
|
|
|
.port_alloc_buffers = port_alloc_buffers,
|
|
|
|
|
.port_set_io = port_set_io,
|
|
|
|
|
.port_reuse_buffer = port_reuse_buffer,
|
|
|
|
|
.port_send_command = port_send_command,
|
|
|
|
|
};
|
|
|
|
|
|
2018-03-08 11:08:37 +01:00
|
|
|
|
2018-03-16 16:55:25 +01:00
|
|
|
static int schedule_mix(struct spa_node *_node)
|
2018-02-15 17:54:08 +01:00
|
|
|
{
|
2018-03-08 11:08:37 +01:00
|
|
|
struct pw_port *port = SPA_CONTAINER_OF(_node, struct pw_port, mix_node);
|
|
|
|
|
struct port *p = port->owner_data;
|
|
|
|
|
struct node *n = p->node;
|
|
|
|
|
struct port *outp = GET_OUT_PORT(n, 0);
|
2018-02-15 17:54:08 +01:00
|
|
|
struct spa_graph_node *node = &port->rt.mix_node;
|
|
|
|
|
struct spa_graph_port *gp;
|
|
|
|
|
struct spa_io_buffers *io = port->rt.mix_port.io;
|
2018-03-14 11:52:13 +01:00
|
|
|
size_t buffer_size = n->buffer_size;
|
2018-03-08 11:08:37 +01:00
|
|
|
struct buffer *outb;
|
|
|
|
|
float *out = NULL;
|
2018-02-15 17:54:08 +01:00
|
|
|
int layer = 0;
|
2018-04-27 17:30:45 +02:00
|
|
|
int stride = n->channels;
|
2018-02-15 17:54:08 +01:00
|
|
|
|
2018-03-14 11:52:13 +01:00
|
|
|
pw_log_trace("port %p", port);
|
|
|
|
|
|
2018-02-15 17:54:08 +01:00
|
|
|
spa_list_for_each(gp, &node->ports[SPA_DIRECTION_INPUT], link) {
|
2018-03-08 11:08:37 +01:00
|
|
|
struct pw_port_mix *mix = SPA_CONTAINER_OF(gp, struct pw_port_mix, port);
|
2018-02-15 17:54:08 +01:00
|
|
|
|
2018-03-08 11:08:37 +01:00
|
|
|
pw_log_trace("mix %p: input %d %d/%d", node,
|
|
|
|
|
gp->io->status, gp->io->buffer_id, mix->n_buffers);
|
2018-02-15 17:54:08 +01:00
|
|
|
|
2018-03-08 11:08:37 +01:00
|
|
|
if (!(gp->io->buffer_id < mix->n_buffers && gp->io->status == SPA_STATUS_HAVE_BUFFER))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (layer++ == 0) {
|
2018-03-14 11:52:13 +01:00
|
|
|
out = mix->buffers[gp->io->buffer_id]->datas[0].data;
|
2018-03-08 11:08:37 +01:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
add_f32(out, mix->buffers[gp->io->buffer_id]->datas[0].data, buffer_size);
|
|
|
|
|
}
|
2018-02-15 17:54:08 +01:00
|
|
|
|
2018-03-14 11:52:13 +01:00
|
|
|
pw_log_trace("mix %p: input %p %p->%p %d %d %zd", node,
|
|
|
|
|
gp, gp->io, io, gp->io->status, gp->io->buffer_id, buffer_size);
|
2018-02-15 17:54:08 +01:00
|
|
|
}
|
2018-03-08 11:08:37 +01:00
|
|
|
|
|
|
|
|
outb = peek_buffer(n, outp);
|
2018-03-09 12:39:14 +01:00
|
|
|
if (outb == NULL)
|
|
|
|
|
return -EPIPE;
|
|
|
|
|
|
2018-03-14 11:52:13 +01:00
|
|
|
if (layer > 0)
|
2018-04-27 17:30:45 +02:00
|
|
|
n->conv_func(outb->ptr, out, port->port_id, buffer_size, stride);
|
2018-04-02 20:05:11 +02:00
|
|
|
else
|
2018-04-27 17:30:45 +02:00
|
|
|
n->fill_func(outb->ptr, port->port_id, buffer_size, stride);
|
2018-03-14 11:52:13 +01:00
|
|
|
|
2018-02-15 17:54:08 +01:00
|
|
|
return SPA_STATUS_HAVE_BUFFER;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-08 11:08:37 +01:00
|
|
|
static int schedule_mix_use_buffers(struct spa_node *_node,
|
|
|
|
|
enum spa_direction direction,
|
|
|
|
|
uint32_t port_id,
|
|
|
|
|
struct spa_buffer **buffers,
|
|
|
|
|
uint32_t n_buffers)
|
|
|
|
|
{
|
|
|
|
|
struct pw_port *port = SPA_CONTAINER_OF(_node, struct pw_port, mix_node);
|
|
|
|
|
struct port *p = port->owner_data;
|
2018-03-14 11:52:13 +01:00
|
|
|
struct node *n = p->node;
|
2018-03-08 11:08:37 +01:00
|
|
|
|
|
|
|
|
pw_log_debug("port %p: %d use buffers %d %p", port, port_id, n_buffers, buffers);
|
|
|
|
|
|
2018-03-14 11:52:13 +01:00
|
|
|
if (n_buffers > 0) {
|
2018-04-27 17:30:45 +02:00
|
|
|
n->buffer_size = buffers[0]->datas[0].maxsize / p->stride;
|
2018-03-14 11:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
2018-03-08 11:08:37 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-02-15 17:54:08 +01:00
|
|
|
static const struct spa_node schedule_mix_node = {
|
|
|
|
|
SPA_VERSION_NODE,
|
|
|
|
|
NULL,
|
2018-03-08 11:08:37 +01:00
|
|
|
.port_use_buffers = schedule_mix_use_buffers,
|
2018-03-16 16:55:25 +01:00
|
|
|
.process = schedule_mix,
|
2018-02-15 17:54:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void port_free(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct port *p = data;
|
|
|
|
|
struct node *n = p->node;
|
|
|
|
|
struct pw_port *port = p->port;
|
|
|
|
|
|
|
|
|
|
if (port->direction == PW_DIRECTION_INPUT) {
|
|
|
|
|
n->in_ports[port->port_id] = NULL;
|
|
|
|
|
n->n_in_ports--;
|
|
|
|
|
} else {
|
|
|
|
|
n->out_ports[port->port_id] = NULL;
|
|
|
|
|
n->n_out_ports--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_port_events port_events = {
|
|
|
|
|
PW_VERSION_PORT_EVENTS,
|
|
|
|
|
.free = port_free,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static struct port *make_port(struct node *n, enum pw_direction direction,
|
|
|
|
|
uint32_t id, uint32_t flags, struct pw_properties *props)
|
|
|
|
|
{
|
|
|
|
|
struct pw_port *port;
|
|
|
|
|
struct port *p;
|
|
|
|
|
|
|
|
|
|
port = pw_port_new(direction, id, props, sizeof(struct port));
|
|
|
|
|
if (port == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
p = pw_port_get_user_data(port);
|
|
|
|
|
p->port = port;
|
|
|
|
|
p->node = n;
|
|
|
|
|
p->flags = flags;
|
|
|
|
|
spa_list_init(&p->queue);
|
2018-03-08 11:08:37 +01:00
|
|
|
port->owner_data = p;
|
2018-02-15 17:54:08 +01:00
|
|
|
|
|
|
|
|
if (direction == PW_DIRECTION_INPUT) {
|
|
|
|
|
n->in_ports[id] = p;
|
|
|
|
|
n->n_in_ports++;
|
2018-03-14 11:52:13 +01:00
|
|
|
if (flags & PORT_FLAG_RAW_F32) {
|
|
|
|
|
port->mix_node = schedule_mix_node;
|
|
|
|
|
port->mix = &port->mix_node;
|
|
|
|
|
}
|
2018-02-15 17:54:08 +01:00
|
|
|
} else {
|
|
|
|
|
n->out_ports[id] = p;
|
|
|
|
|
n->n_out_ports++;
|
|
|
|
|
}
|
2018-02-26 16:09:47 +01:00
|
|
|
pw_port_add_listener(port, &p->port_listener, &port_events, p);
|
2018-02-15 17:54:08 +01:00
|
|
|
pw_port_add(port, n->node);
|
|
|
|
|
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-24 17:03:56 +02:00
|
|
|
static struct node *make_node(struct impl *impl, const struct pw_properties *props,
|
2018-02-16 12:02:39 +01:00
|
|
|
enum pw_direction direction)
|
2018-02-15 17:54:08 +01:00
|
|
|
{
|
|
|
|
|
struct pw_node *node;
|
|
|
|
|
struct node *n;
|
|
|
|
|
struct port *p;
|
2018-04-24 17:03:56 +02:00
|
|
|
const char *api, *alias, *plugged;
|
2018-02-16 12:02:39 +01:00
|
|
|
char node_name[128];
|
2018-04-24 17:03:56 +02:00
|
|
|
struct pw_properties *pr;
|
2018-02-15 17:54:08 +01:00
|
|
|
int i;
|
|
|
|
|
|
2018-04-24 17:03:56 +02:00
|
|
|
if ((api = pw_properties_get(props, "device.api")) == NULL)
|
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
if ((alias = pw_properties_get(props, "device.name")) == NULL)
|
2018-02-20 10:31:55 +01:00
|
|
|
goto error;
|
|
|
|
|
|
2018-04-24 17:03:56 +02:00
|
|
|
|
2018-02-20 10:31:55 +01:00
|
|
|
snprintf(node_name, sizeof(node_name), "system_%s", alias);
|
|
|
|
|
for (i = 0; node_name[i]; i++) {
|
2018-03-14 11:52:13 +01:00
|
|
|
if (node_name[i] == ':' || node_name[i] == ',')
|
2018-02-20 10:31:55 +01:00
|
|
|
node_name[i] = '_';
|
|
|
|
|
}
|
2018-02-16 12:02:39 +01:00
|
|
|
|
2018-04-24 17:03:56 +02:00
|
|
|
pr = pw_properties_new(
|
|
|
|
|
"media.class",
|
|
|
|
|
direction == PW_DIRECTION_OUTPUT ?
|
|
|
|
|
"Audio/DSP/Playback" :
|
|
|
|
|
"Audio/DSP/Capture",
|
|
|
|
|
"device.name", alias,
|
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
|
|
if ((plugged = pw_properties_get(props, "node.plugged")) != NULL)
|
|
|
|
|
pw_properties_set(pr, "node.plugged", plugged);
|
|
|
|
|
|
|
|
|
|
node = pw_node_new(impl->core, node_name, pr, sizeof(struct node));
|
2018-02-15 17:54:08 +01:00
|
|
|
if (node == NULL)
|
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
n = pw_node_get_user_data(node);
|
|
|
|
|
n->node = node;
|
|
|
|
|
n->impl = impl;
|
|
|
|
|
n->node_impl = node_impl;
|
2018-04-26 17:23:27 +02:00
|
|
|
if (direction == PW_DIRECTION_OUTPUT)
|
|
|
|
|
n->node_impl.process = node_process_mix;
|
|
|
|
|
else
|
|
|
|
|
n->node_impl.process = node_process_split;
|
2018-03-14 11:52:13 +01:00
|
|
|
n->channels = DEFAULT_CHANNELS;
|
|
|
|
|
n->sample_rate = DEFAULT_SAMPLE_RATE;
|
|
|
|
|
n->buffer_size = DEFAULT_BUFFER_SIZE;
|
2018-02-15 17:54:08 +01:00
|
|
|
pw_node_set_implementation(node, &n->node_impl);
|
|
|
|
|
|
|
|
|
|
p = make_port(n, direction, 0, 0, NULL);
|
|
|
|
|
if (p == NULL)
|
|
|
|
|
goto error_free_node;
|
|
|
|
|
|
|
|
|
|
direction = pw_direction_reverse(direction);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < n->channels; i++) {
|
2018-02-16 12:02:39 +01:00
|
|
|
char port_name[128], alias_name[128];
|
2018-02-15 17:54:08 +01:00
|
|
|
|
2018-02-20 10:01:10 +01:00
|
|
|
n->port_count[direction]++;
|
2018-02-15 17:54:08 +01:00
|
|
|
snprintf(port_name, sizeof(port_name), "%s_%d",
|
|
|
|
|
direction == PW_DIRECTION_INPUT ? "playback" : "capture",
|
2018-02-16 12:02:39 +01:00
|
|
|
n->port_count[direction]);
|
2018-04-24 17:03:56 +02:00
|
|
|
snprintf(alias_name, sizeof(alias_name), "%s_pcm:%s:%s%d",
|
|
|
|
|
api,
|
2018-02-16 12:02:39 +01:00
|
|
|
alias,
|
|
|
|
|
direction == PW_DIRECTION_INPUT ? "in" : "out",
|
|
|
|
|
n->port_count[direction]);
|
|
|
|
|
|
2018-02-15 17:54:08 +01:00
|
|
|
p = make_port(n, direction, i,
|
|
|
|
|
PORT_FLAG_DSP | PORT_FLAG_RAW_F32,
|
|
|
|
|
pw_properties_new(
|
2018-02-20 15:49:10 +01:00
|
|
|
"port.dsp", "32 bit float mono audio",
|
2018-02-15 17:54:08 +01:00
|
|
|
"port.name", port_name,
|
2018-02-16 12:02:39 +01:00
|
|
|
"port.alias1", alias_name,
|
2018-02-15 17:54:08 +01:00
|
|
|
NULL));
|
|
|
|
|
if (p == NULL)
|
|
|
|
|
goto error_free_node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spa_list_append(&impl->node_list, &n->link);
|
|
|
|
|
|
|
|
|
|
pw_node_register(node, NULL, pw_module_get_global(impl->module), NULL);
|
|
|
|
|
pw_node_set_active(node, true);
|
|
|
|
|
|
2018-04-24 17:03:56 +02:00
|
|
|
return n;
|
2018-02-15 17:54:08 +01:00
|
|
|
|
|
|
|
|
error_free_node:
|
|
|
|
|
pw_node_destroy(node);
|
|
|
|
|
error:
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-24 17:03:56 +02:00
|
|
|
static void node_destroy(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct node *node = data;
|
|
|
|
|
|
|
|
|
|
spa_list_remove(&node->link);
|
|
|
|
|
pw_node_destroy(node->node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_node_events node_events = {
|
|
|
|
|
PW_VERSION_NODE_EVENTS,
|
|
|
|
|
.destroy = node_destroy,
|
|
|
|
|
};
|
|
|
|
|
|
2018-02-15 17:54:08 +01:00
|
|
|
static int on_global(void *data, struct pw_global *global)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
2018-04-24 17:03:56 +02:00
|
|
|
struct pw_node *n;
|
|
|
|
|
struct node *node;
|
2018-02-15 17:54:08 +01:00
|
|
|
const struct pw_properties *properties;
|
|
|
|
|
const char *str;
|
|
|
|
|
char *error;
|
|
|
|
|
struct pw_port *ip, *op;
|
|
|
|
|
struct pw_link *link;
|
|
|
|
|
|
2018-04-24 17:03:56 +02:00
|
|
|
|
2018-02-15 17:54:08 +01:00
|
|
|
if (pw_global_get_type(global) != impl->t->node)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
n = pw_global_get_object(global);
|
|
|
|
|
|
|
|
|
|
properties = pw_node_get_properties(n);
|
|
|
|
|
if ((str = pw_properties_get(properties, "media.class")) == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2018-04-24 17:03:56 +02:00
|
|
|
pw_log_debug("global added %s", str);
|
|
|
|
|
|
2018-04-12 09:52:15 +02:00
|
|
|
if (strcmp(str, "Audio/Sink") == 0) {
|
2018-02-16 12:02:39 +01:00
|
|
|
if ((ip = pw_node_get_free_port(n, PW_DIRECTION_INPUT)) == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
if ((node = make_node(impl, properties, PW_DIRECTION_OUTPUT)) == NULL)
|
|
|
|
|
return 0;
|
2018-04-24 17:03:56 +02:00
|
|
|
if ((op = pw_node_get_free_port(node->node, PW_DIRECTION_OUTPUT)) == NULL)
|
2018-02-16 12:02:39 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp(str, "Audio/Source") == 0) {
|
|
|
|
|
if ((op = pw_node_get_free_port(n, PW_DIRECTION_OUTPUT)) == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
if ((node = make_node(impl, properties, PW_DIRECTION_INPUT)) == NULL)
|
|
|
|
|
return 0;
|
2018-04-24 17:03:56 +02:00
|
|
|
if ((ip = pw_node_get_free_port(node->node, PW_DIRECTION_INPUT)) == NULL)
|
2018-02-16 12:02:39 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
2018-02-15 17:54:08 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
link = pw_link_new(impl->core,
|
|
|
|
|
op,
|
|
|
|
|
ip,
|
|
|
|
|
NULL,
|
|
|
|
|
pw_properties_new(PW_LINK_PROP_PASSIVE, "true", NULL),
|
|
|
|
|
&error, 0);
|
2018-02-16 12:02:39 +01:00
|
|
|
if (link == NULL) {
|
|
|
|
|
pw_log_error("can't create link: %s", error);
|
|
|
|
|
free(error);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-02-15 17:54:08 +01:00
|
|
|
pw_link_register(link, NULL, pw_module_get_global(impl->module), NULL);
|
|
|
|
|
|
2018-04-24 17:03:56 +02:00
|
|
|
pw_node_add_listener(n, &node->node_listener, &node_events, node);
|
|
|
|
|
|
2018-02-15 17:54:08 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void module_destroy(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
struct node *n, *t;
|
|
|
|
|
|
|
|
|
|
spa_hook_remove(&impl->module_listener);
|
|
|
|
|
spa_hook_remove(&impl->core_listener);
|
|
|
|
|
|
|
|
|
|
spa_list_for_each_safe(n, t, &impl->node_list, link)
|
|
|
|
|
pw_node_destroy(n->node);
|
|
|
|
|
|
|
|
|
|
if (impl->properties)
|
|
|
|
|
pw_properties_free(impl->properties);
|
|
|
|
|
|
|
|
|
|
free(impl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_module_events module_events = {
|
|
|
|
|
PW_VERSION_MODULE_EVENTS,
|
|
|
|
|
.destroy = module_destroy,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
core_global_added(void *data, struct pw_global *global)
|
|
|
|
|
{
|
|
|
|
|
on_global(data, global);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_core_events core_events = {
|
|
|
|
|
PW_VERSION_CORE_EVENTS,
|
|
|
|
|
.global_added = core_global_added,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int module_init(struct pw_module *module, struct pw_properties *properties)
|
|
|
|
|
{
|
|
|
|
|
struct pw_core *core = pw_module_get_core(module);
|
|
|
|
|
struct impl *impl;
|
|
|
|
|
|
|
|
|
|
impl = calloc(1, sizeof(struct impl));
|
|
|
|
|
if (impl == NULL)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
|
|
pw_log_debug("module %p: new", impl);
|
|
|
|
|
|
|
|
|
|
impl->core = core;
|
|
|
|
|
impl->t = pw_core_get_type(core);
|
|
|
|
|
impl->module = module;
|
|
|
|
|
impl->properties = properties;
|
|
|
|
|
|
|
|
|
|
init_type(&impl->type, core->type.map);
|
|
|
|
|
|
|
|
|
|
spa_list_init(&impl->node_list);
|
|
|
|
|
|
|
|
|
|
pw_core_for_each_global(core, on_global, impl);
|
|
|
|
|
|
|
|
|
|
pw_core_add_listener(core, &impl->core_listener, &core_events, impl);
|
|
|
|
|
pw_module_add_listener(module, &impl->module_listener, &module_events, impl);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pipewire__module_init(struct pw_module *module, const char *args)
|
|
|
|
|
{
|
|
|
|
|
return module_init(module, NULL);
|
|
|
|
|
}
|