2018-01-11 10:23:37 +01:00
|
|
|
/* Spa ALSA Sink
|
|
|
|
|
* Copyright (C) 2016 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 <unistd.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <sys/timerfd.h>
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
|
|
#include <spa/support/loop.h>
|
|
|
|
|
#include <spa/support/log.h>
|
|
|
|
|
#include <spa/utils/list.h>
|
|
|
|
|
|
|
|
|
|
#include <spa/node/node.h>
|
|
|
|
|
#include <spa/node/io.h>
|
2018-08-25 12:08:29 +02:00
|
|
|
#include <spa/param/param.h>
|
2018-01-11 10:23:37 +01:00
|
|
|
#include <spa/param/audio/format.h>
|
|
|
|
|
#include <spa/param/audio/format-utils.h>
|
2018-08-13 17:17:23 +02:00
|
|
|
#include <spa/pod/filter.h>
|
2018-01-11 10:23:37 +01:00
|
|
|
|
|
|
|
|
#include <sbc/sbc.h>
|
|
|
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
|
#include "rtp.h"
|
|
|
|
|
#include "a2dp-codecs.h"
|
|
|
|
|
|
|
|
|
|
struct props {
|
|
|
|
|
uint32_t min_latency;
|
|
|
|
|
uint32_t max_latency;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define FILL_FRAMES 2
|
|
|
|
|
#define MAX_FRAME_COUNT 32
|
|
|
|
|
#define MAX_BUFFERS 32
|
|
|
|
|
|
|
|
|
|
struct buffer {
|
2018-05-17 17:26:09 +02:00
|
|
|
struct spa_buffer *buf;
|
2018-01-11 10:23:37 +01:00
|
|
|
struct spa_meta_header *h;
|
|
|
|
|
bool outstanding;
|
|
|
|
|
struct spa_list link;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct impl {
|
|
|
|
|
struct spa_handle handle;
|
|
|
|
|
struct spa_node node;
|
|
|
|
|
|
|
|
|
|
uint32_t seq;
|
|
|
|
|
|
|
|
|
|
struct spa_log *log;
|
|
|
|
|
struct spa_loop *main_loop;
|
|
|
|
|
struct spa_loop *data_loop;
|
|
|
|
|
|
|
|
|
|
const struct spa_node_callbacks *callbacks;
|
|
|
|
|
void *callbacks_data;
|
|
|
|
|
|
|
|
|
|
struct props props;
|
|
|
|
|
|
|
|
|
|
struct spa_bt_transport *transport;
|
|
|
|
|
|
|
|
|
|
bool opened;
|
|
|
|
|
|
|
|
|
|
bool have_format;
|
|
|
|
|
struct spa_audio_info current_format;
|
|
|
|
|
int frame_size;
|
|
|
|
|
|
|
|
|
|
struct spa_port_info info;
|
|
|
|
|
struct spa_io_buffers *io;
|
|
|
|
|
struct spa_io_control_range *range;
|
|
|
|
|
|
|
|
|
|
struct buffer buffers[MAX_BUFFERS];
|
|
|
|
|
unsigned int n_buffers;
|
|
|
|
|
|
|
|
|
|
struct spa_list free;
|
|
|
|
|
struct spa_list ready;
|
|
|
|
|
|
|
|
|
|
size_t ready_offset;
|
|
|
|
|
|
|
|
|
|
bool started;
|
|
|
|
|
struct spa_source source;
|
|
|
|
|
int timerfd;
|
|
|
|
|
int threshold;
|
|
|
|
|
struct spa_source flush_source;
|
|
|
|
|
|
|
|
|
|
sbc_t sbc;
|
|
|
|
|
int read_size;
|
|
|
|
|
int write_size;
|
|
|
|
|
int write_samples;
|
|
|
|
|
int frame_length;
|
|
|
|
|
int codesize;
|
|
|
|
|
uint8_t buffer[4096];
|
|
|
|
|
int buffer_used;
|
|
|
|
|
int frame_count;
|
|
|
|
|
uint16_t seqnum;
|
|
|
|
|
uint32_t timestamp;
|
|
|
|
|
|
|
|
|
|
int min_bitpool;
|
|
|
|
|
int max_bitpool;
|
|
|
|
|
|
|
|
|
|
uint64_t last_time;
|
|
|
|
|
uint64_t last_error;
|
|
|
|
|
|
|
|
|
|
struct timespec now;
|
|
|
|
|
int64_t start_time;
|
|
|
|
|
int64_t sample_count;
|
|
|
|
|
int64_t sample_time;
|
|
|
|
|
int64_t last_ticks;
|
|
|
|
|
int64_t last_monotonic;
|
|
|
|
|
|
|
|
|
|
uint64_t underrun;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define NAME "a2dp-sink"
|
|
|
|
|
|
|
|
|
|
#define CHECK_PORT(this,d,p) ((d) == SPA_DIRECTION_INPUT && (p) == 0)
|
|
|
|
|
|
2018-05-17 17:26:09 +02:00
|
|
|
static const uint32_t default_min_latency = 128;
|
2018-01-11 10:23:37 +01:00
|
|
|
static const uint32_t default_max_latency = 1024;
|
|
|
|
|
|
|
|
|
|
static void reset_props(struct props *props)
|
|
|
|
|
{
|
|
|
|
|
props->min_latency = default_min_latency;
|
|
|
|
|
props->max_latency = default_max_latency;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int impl_node_enum_params(struct spa_node *node,
|
|
|
|
|
uint32_t id, uint32_t *index,
|
|
|
|
|
const struct spa_pod *filter,
|
|
|
|
|
struct spa_pod **result,
|
|
|
|
|
struct spa_pod_builder *builder)
|
|
|
|
|
{
|
|
|
|
|
struct impl *this;
|
|
|
|
|
struct spa_pod *param;
|
|
|
|
|
struct spa_pod_builder b = { 0 };
|
|
|
|
|
uint8_t buffer[1024];
|
|
|
|
|
|
|
|
|
|
spa_return_val_if_fail(node != NULL, -EINVAL);
|
|
|
|
|
spa_return_val_if_fail(index != NULL, -EINVAL);
|
|
|
|
|
spa_return_val_if_fail(builder != NULL, -EINVAL);
|
|
|
|
|
|
|
|
|
|
this = SPA_CONTAINER_OF(node, struct impl, node);
|
|
|
|
|
|
|
|
|
|
next:
|
|
|
|
|
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
|
|
|
|
|
2018-08-23 17:47:57 +02:00
|
|
|
switch (id) {
|
2018-08-25 12:08:29 +02:00
|
|
|
case SPA_PARAM_List:
|
2018-08-23 17:47:57 +02:00
|
|
|
{
|
2018-08-25 12:08:29 +02:00
|
|
|
uint32_t list[] = { SPA_PARAM_PropInfo,
|
|
|
|
|
SPA_PARAM_Props };
|
2018-01-11 10:23:37 +01:00
|
|
|
|
|
|
|
|
if (*index < SPA_N_ELEMENTS(list))
|
2018-08-23 17:47:57 +02:00
|
|
|
param = spa_pod_builder_object(&b, id, SPA_ID_OBJECT_ParamList,
|
|
|
|
|
":", SPA_PARAM_LIST_id, "I", list[*index]);
|
2018-01-11 10:23:37 +01:00
|
|
|
else
|
|
|
|
|
return 0;
|
2018-08-23 17:47:57 +02:00
|
|
|
break;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
2018-08-25 12:08:29 +02:00
|
|
|
case SPA_PARAM_PropInfo:
|
2018-08-23 17:47:57 +02:00
|
|
|
{
|
2018-01-11 10:23:37 +01:00
|
|
|
struct props *p = &this->props;
|
|
|
|
|
|
|
|
|
|
switch (*index) {
|
|
|
|
|
case 0:
|
|
|
|
|
param = spa_pod_builder_object(&b,
|
2018-08-23 17:47:57 +02:00
|
|
|
id, SPA_ID_OBJECT_PropInfo,
|
|
|
|
|
":", SPA_PROP_INFO_id, "I", SPA_PROP_minLatency,
|
|
|
|
|
":", SPA_PROP_INFO_name, "s", "The minimum latency",
|
|
|
|
|
":", SPA_PROP_INFO_type, "ir", p->min_latency,
|
2018-02-20 09:32:40 +01:00
|
|
|
SPA_POD_PROP_MIN_MAX(1, INT32_MAX));
|
2018-01-11 10:23:37 +01:00
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
param = spa_pod_builder_object(&b,
|
2018-08-23 17:47:57 +02:00
|
|
|
id, SPA_ID_OBJECT_PropInfo,
|
|
|
|
|
":", SPA_PROP_INFO_id, "I", SPA_PROP_maxLatency,
|
|
|
|
|
":", SPA_PROP_INFO_name, "s", "The maximum latency",
|
|
|
|
|
":", SPA_PROP_INFO_type, "ir", p->max_latency,
|
2018-02-20 09:32:40 +01:00
|
|
|
SPA_POD_PROP_MIN_MAX(1, INT32_MAX));
|
2018-01-11 10:23:37 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-08-23 17:47:57 +02:00
|
|
|
break;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
2018-08-25 12:08:29 +02:00
|
|
|
case SPA_PARAM_Props:
|
2018-08-23 17:47:57 +02:00
|
|
|
{
|
2018-01-11 10:23:37 +01:00
|
|
|
struct props *p = &this->props;
|
|
|
|
|
|
|
|
|
|
switch (*index) {
|
|
|
|
|
case 0:
|
|
|
|
|
param = spa_pod_builder_object(&b,
|
2018-08-23 17:47:57 +02:00
|
|
|
id, SPA_ID_OBJECT_Props,
|
|
|
|
|
":", SPA_PROP_minLatency, "i", p->min_latency,
|
|
|
|
|
":", SPA_PROP_maxLatency, "i", p->max_latency);
|
2018-01-11 10:23:37 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-08-23 17:47:57 +02:00
|
|
|
break;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
2018-08-23 17:47:57 +02:00
|
|
|
default:
|
2018-01-11 10:23:37 +01:00
|
|
|
return -ENOENT;
|
2018-08-23 17:47:57 +02:00
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
|
|
|
|
|
(*index)++;
|
|
|
|
|
|
|
|
|
|
if (spa_pod_filter(builder, result, param, filter) < 0)
|
|
|
|
|
goto next;
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int impl_node_set_param(struct spa_node *node, uint32_t id, uint32_t flags,
|
|
|
|
|
const struct spa_pod *param)
|
|
|
|
|
{
|
|
|
|
|
struct impl *this;
|
|
|
|
|
|
|
|
|
|
spa_return_val_if_fail(node != NULL, -EINVAL);
|
|
|
|
|
|
|
|
|
|
this = SPA_CONTAINER_OF(node, struct impl, node);
|
|
|
|
|
|
2018-08-23 17:47:57 +02:00
|
|
|
switch (id) {
|
2018-08-25 12:08:29 +02:00
|
|
|
case SPA_PARAM_Props:
|
2018-08-23 17:47:57 +02:00
|
|
|
{
|
2018-01-11 10:23:37 +01:00
|
|
|
struct props *p = &this->props;
|
|
|
|
|
|
|
|
|
|
if (param == NULL) {
|
|
|
|
|
reset_props(p);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
spa_pod_object_parse(param,
|
2018-08-23 17:47:57 +02:00
|
|
|
":", SPA_PROP_minLatency, "?i", &p->min_latency,
|
|
|
|
|
":", SPA_PROP_maxLatency, "?i", &p->max_latency, NULL);
|
|
|
|
|
break;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
2018-08-23 17:47:57 +02:00
|
|
|
default:
|
2018-01-11 10:23:37 +01:00
|
|
|
return -ENOENT;
|
2018-08-23 17:47:57 +02:00
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static inline void calc_timeout(size_t target, size_t current,
|
|
|
|
|
size_t rate, struct timespec *now,
|
|
|
|
|
struct timespec *ts)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
ts->tv_sec = now->tv_sec;
|
|
|
|
|
ts->tv_nsec = now->tv_nsec;
|
|
|
|
|
if (target > current)
|
|
|
|
|
ts->tv_nsec += ((target - current) * SPA_NSEC_PER_SEC) / rate;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
while (ts->tv_nsec >= SPA_NSEC_PER_SEC) {
|
|
|
|
|
ts->tv_sec++;
|
|
|
|
|
ts->tv_nsec -= SPA_NSEC_PER_SEC;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int reset_buffer(struct impl *this)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
this->buffer_used = sizeof(struct rtp_header) + sizeof(struct rtp_payload);
|
|
|
|
|
this->frame_count = 0;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int send_buffer(struct impl *this)
|
|
|
|
|
{
|
|
|
|
|
int val, written;
|
|
|
|
|
struct rtp_header *header;
|
|
|
|
|
struct rtp_payload *payload;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
header = (struct rtp_header *)this->buffer;
|
|
|
|
|
payload = (struct rtp_payload *)(this->buffer + sizeof(struct rtp_header));
|
|
|
|
|
memset(this->buffer, 0, sizeof(struct rtp_header)+sizeof(struct rtp_payload));
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
payload->frame_count = this->frame_count;
|
|
|
|
|
header->v = 2;
|
|
|
|
|
header->pt = 1;
|
|
|
|
|
header->sequence_number = htons(this->seqnum);
|
|
|
|
|
header->timestamp = htonl(this->timestamp);
|
|
|
|
|
header->ssrc = htonl(1);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
ioctl(this->transport->fd, TIOCOUTQ, &val);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_log_trace(this->log, "a2dp-sink %p: send %d %u %u %u %lu %d",
|
|
|
|
|
this, this->frame_count, this->seqnum, this->timestamp, this->buffer_used,
|
|
|
|
|
this->sample_time, val);
|
|
|
|
|
|
|
|
|
|
written = write(this->transport->fd, this->buffer, this->buffer_used);
|
|
|
|
|
spa_log_trace(this->log, "a2dp-sink %p: send %d", this, written);
|
|
|
|
|
if (written < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
|
|
this->timestamp = this->sample_count;
|
|
|
|
|
this->seqnum++;
|
|
|
|
|
reset_buffer(this);
|
|
|
|
|
|
|
|
|
|
return written;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int encode_buffer(struct impl *this, const void *data, int size)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
int processed;
|
|
|
|
|
ssize_t out_encoded;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_log_trace(this->log, "a2dp-sink %p: encode %d used %d, %d %d",
|
|
|
|
|
this, size, this->buffer_used, this->frame_size, this->write_size);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (this->frame_count > MAX_FRAME_COUNT)
|
|
|
|
|
return -ENOSPC;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
processed = sbc_encode(&this->sbc, data, size,
|
|
|
|
|
this->buffer + this->buffer_used,
|
|
|
|
|
this->write_size - this->buffer_used,
|
|
|
|
|
&out_encoded);
|
|
|
|
|
if (processed < 0)
|
|
|
|
|
return processed;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
this->sample_count += processed / this->frame_size;
|
|
|
|
|
this->sample_time += processed / this->frame_size;
|
|
|
|
|
this->frame_count += processed / this->codesize;
|
|
|
|
|
this->buffer_used += out_encoded;
|
|
|
|
|
|
|
|
|
|
spa_log_trace(this->log, "a2dp-sink %p: processed %d %ld used %d",
|
|
|
|
|
this, processed, out_encoded, this->buffer_used);
|
|
|
|
|
|
|
|
|
|
return processed;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static bool need_flush(struct impl *this)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
return (this->buffer_used + this->frame_length > this->write_size) ||
|
|
|
|
|
this->frame_count > MAX_FRAME_COUNT;
|
|
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int flush_buffer(struct impl *this, bool force)
|
|
|
|
|
{
|
|
|
|
|
spa_log_trace(this->log, "%d %d %d", this->buffer_used, this->frame_length,
|
|
|
|
|
this->write_size);
|
|
|
|
|
|
|
|
|
|
if (force || need_flush(this))
|
|
|
|
|
return send_buffer(this);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int fill_socket(struct impl *this, uint64_t now_time)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
static const uint8_t zero_buffer[1024 * 4] = { 0, };
|
|
|
|
|
int frames = 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
while (frames < FILL_FRAMES) {
|
|
|
|
|
int processed, written;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
processed = encode_buffer(this, zero_buffer, sizeof(zero_buffer));
|
|
|
|
|
if (processed < 0)
|
|
|
|
|
return processed;
|
|
|
|
|
if (processed == 0)
|
|
|
|
|
break;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
written = flush_buffer(this, false);
|
|
|
|
|
if (written == -EAGAIN)
|
|
|
|
|
break;
|
|
|
|
|
else if (written < 0)
|
|
|
|
|
return written;
|
|
|
|
|
else if (written > 0)
|
|
|
|
|
frames++;
|
|
|
|
|
}
|
|
|
|
|
reset_buffer(this);
|
|
|
|
|
this->sample_count = this->timestamp;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
return 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int add_data(struct impl *this, const void *data, int size)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
int processed, total = 0;
|
|
|
|
|
|
|
|
|
|
while (size > 0) {
|
|
|
|
|
processed = encode_buffer(this, data, size);
|
|
|
|
|
|
|
|
|
|
if (processed == -ENOSPC || processed == 0)
|
|
|
|
|
break;
|
|
|
|
|
if (processed < 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
data += processed;
|
|
|
|
|
size -= processed;
|
|
|
|
|
total += processed;
|
|
|
|
|
}
|
|
|
|
|
return total;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int set_bitpool(struct impl *this, int bitpool)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
if (bitpool < this->min_bitpool)
|
|
|
|
|
bitpool = this->min_bitpool;
|
|
|
|
|
if (bitpool > this->max_bitpool)
|
|
|
|
|
bitpool = this->max_bitpool;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (this->sbc.bitpool == bitpool)
|
|
|
|
|
return 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
this->sbc.bitpool = bitpool;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_log_debug(this->log, "set bitpool %d", this->sbc.bitpool);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
this->codesize = sbc_get_codesize(&this->sbc);
|
|
|
|
|
this->frame_length = sbc_get_frame_length(&this->sbc);
|
|
|
|
|
|
|
|
|
|
this->read_size = this->transport->read_mtu
|
|
|
|
|
- sizeof(struct rtp_header) - sizeof(struct rtp_payload) - 24;
|
|
|
|
|
this->write_size = this->transport->write_mtu
|
|
|
|
|
- sizeof(struct rtp_header) - sizeof(struct rtp_payload) - 24;
|
|
|
|
|
this->write_samples = (this->write_size / this->frame_length) * (this->codesize / this->frame_size);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int reduce_bitpool(struct impl *this)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
return set_bitpool(this, this->sbc.bitpool - 2);
|
|
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int increase_bitpool(struct impl *this)
|
|
|
|
|
{
|
|
|
|
|
return set_bitpool(this, this->sbc.bitpool + 1);
|
|
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int flush_data(struct impl *this, uint64_t now_time)
|
|
|
|
|
{
|
|
|
|
|
uint32_t total_frames, written;
|
|
|
|
|
uint64_t elapsed;
|
|
|
|
|
int64_t queued;
|
|
|
|
|
struct itimerspec ts;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
total_frames = 0;
|
|
|
|
|
while (!spa_list_is_empty(&this->ready)) {
|
|
|
|
|
uint8_t *src;
|
|
|
|
|
int n_bytes, n_frames;
|
|
|
|
|
struct buffer *b;
|
|
|
|
|
struct spa_data *d;
|
|
|
|
|
uint32_t index, offs, avail, l0, l1;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
b = spa_list_first(&this->ready, struct buffer, link);
|
2018-05-17 17:26:09 +02:00
|
|
|
d = b->buf->datas;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
src = d[0].data;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
index = d[0].chunk->offset + this->ready_offset;
|
|
|
|
|
avail = d[0].chunk->size - this->ready_offset;
|
|
|
|
|
avail /= this->frame_size;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
offs = index % d[0].maxsize;
|
|
|
|
|
n_frames = avail;
|
|
|
|
|
n_bytes = n_frames * this->frame_size;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
l0 = SPA_MIN(n_bytes, d[0].maxsize - offs);
|
|
|
|
|
l1 = n_bytes - l0;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
n_bytes = add_data(this, src + offs, l0);
|
|
|
|
|
if (n_bytes > 0 && l1 > 0)
|
|
|
|
|
n_bytes += add_data(this, src, l1);
|
|
|
|
|
if (n_bytes <= 0)
|
|
|
|
|
break;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
n_frames = n_bytes / this->frame_size;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
this->ready_offset += n_bytes;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (this->ready_offset >= d[0].chunk->size) {
|
|
|
|
|
spa_list_remove(&b->link);
|
|
|
|
|
b->outstanding = true;
|
2018-05-17 17:26:09 +02:00
|
|
|
spa_log_trace(this->log, "a2dp-sink %p: reuse buffer %u", this, b->buf->id);
|
|
|
|
|
this->callbacks->reuse_buffer(this->callbacks_data, 0, b->buf->id);
|
2018-01-18 11:57:23 +01:00
|
|
|
this->ready_offset = 0;
|
|
|
|
|
}
|
|
|
|
|
total_frames += n_frames;
|
|
|
|
|
|
|
|
|
|
spa_log_trace(this->log, "a2dp-sink %p: written %u frames", this, total_frames);
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
written = flush_buffer(this, false);
|
|
|
|
|
if (written == -EAGAIN) {
|
|
|
|
|
spa_log_trace(this->log, "delay flush %ld", this->sample_time);
|
|
|
|
|
if ((this->flush_source.mask & SPA_IO_OUT) == 0) {
|
|
|
|
|
this->flush_source.mask = SPA_IO_OUT;
|
|
|
|
|
spa_loop_update_source(this->data_loop, &this->flush_source);
|
|
|
|
|
this->source.mask = 0;
|
|
|
|
|
spa_loop_update_source(this->data_loop, &this->source);
|
2018-01-11 10:23:37 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-18 11:57:23 +01:00
|
|
|
else if (written < 0) {
|
|
|
|
|
spa_log_trace(this->log, "error flushing %s", spa_strerror(written));
|
|
|
|
|
return written;
|
|
|
|
|
}
|
|
|
|
|
else if (written > 0) {
|
|
|
|
|
if (now_time - this->last_error > SPA_NSEC_PER_SEC * 3) {
|
|
|
|
|
increase_bitpool(this);
|
|
|
|
|
this->last_error = now_time;
|
|
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
this->flush_source.mask = 0;
|
|
|
|
|
spa_loop_update_source(this->data_loop, &this->flush_source);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (now_time > this->start_time)
|
|
|
|
|
elapsed = now_time - this->start_time;
|
|
|
|
|
else
|
|
|
|
|
elapsed = 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
elapsed = elapsed * this->current_format.info.raw.rate / SPA_NSEC_PER_SEC;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
queued = this->sample_time - elapsed;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_log_trace(this->log, "%ld %ld %ld %ld %d",
|
|
|
|
|
now_time, queued, this->sample_time, elapsed, this->write_samples);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (queued < FILL_FRAMES * this->write_samples) {
|
|
|
|
|
queued = (FILL_FRAMES + 1) * this->write_samples;
|
|
|
|
|
if (this->sample_time < elapsed) {
|
|
|
|
|
this->sample_time = queued;
|
|
|
|
|
this->start_time = now_time;
|
|
|
|
|
}
|
|
|
|
|
if (!spa_list_is_empty(&this->ready) &&
|
|
|
|
|
now_time - this->last_error > SPA_NSEC_PER_SEC / 2) {
|
|
|
|
|
reduce_bitpool(this);
|
|
|
|
|
this->last_error = now_time;
|
|
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
|
|
|
|
|
}
|
2018-01-18 11:57:23 +01:00
|
|
|
calc_timeout(queued,
|
|
|
|
|
FILL_FRAMES * this->write_samples,
|
|
|
|
|
this->current_format.info.raw.rate,
|
|
|
|
|
&this->now, &ts.it_value);
|
|
|
|
|
ts.it_interval.tv_sec = 0;
|
|
|
|
|
ts.it_interval.tv_nsec = 0;
|
|
|
|
|
timerfd_settime(this->timerfd, TFD_TIMER_ABSTIME, &ts, NULL);
|
|
|
|
|
|
|
|
|
|
this->source.mask = SPA_IO_IN;
|
|
|
|
|
spa_loop_update_source(this->data_loop, &this->source);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static void a2dp_on_flush(struct spa_source *source)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
struct impl *this = source->data;
|
|
|
|
|
uint64_t now_time;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_log_trace(this->log, "flushing");
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if ((source->rmask & SPA_IO_OUT) == 0) {
|
|
|
|
|
spa_log_warn(this->log, "error %d", source->rmask);
|
2018-04-24 17:21:23 +02:00
|
|
|
if (this->flush_source.loop)
|
|
|
|
|
spa_loop_remove_source(this->data_loop, &this->flush_source);
|
2018-01-18 11:57:23 +01:00
|
|
|
this->source.mask = 0;
|
|
|
|
|
spa_loop_update_source(this->data_loop, &this->source);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &this->now);
|
|
|
|
|
now_time = this->now.tv_sec * SPA_NSEC_PER_SEC + this->now.tv_nsec;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
flush_data(this, now_time);
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static void a2dp_on_timeout(struct spa_source *source)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
struct impl *this = source->data;
|
|
|
|
|
int err;
|
|
|
|
|
uint64_t exp, now_time;
|
2018-04-24 17:02:19 +02:00
|
|
|
struct spa_io_buffers *io = this->io;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (this->started && read(this->timerfd, &exp, sizeof(uint64_t)) != sizeof(uint64_t))
|
|
|
|
|
spa_log_warn(this->log, "error reading timerfd: %s", strerror(errno));
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &this->now);
|
|
|
|
|
now_time = this->now.tv_sec * SPA_NSEC_PER_SEC + this->now.tv_nsec;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_log_trace(this->log, "timeout %ld %ld", now_time, now_time - this->last_time);
|
|
|
|
|
this->last_time = now_time;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (this->start_time == 0) {
|
|
|
|
|
if ((err = fill_socket(this, now_time)) < 0)
|
|
|
|
|
spa_log_error(this->log, "error fill socket %s", spa_strerror(err));
|
|
|
|
|
this->start_time = now_time;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-04-24 17:02:19 +02:00
|
|
|
if (spa_list_is_empty(&this->ready)) {
|
|
|
|
|
spa_log_trace(this->log, "a2dp-sink %p: %d", this, io->status);
|
|
|
|
|
io->status = SPA_STATUS_NEED_BUFFER;
|
|
|
|
|
if (this->range) {
|
|
|
|
|
this->range->offset = this->sample_count * this->frame_size;
|
|
|
|
|
this->range->min_size = this->threshold * this->frame_size;
|
|
|
|
|
this->range->max_size = this->write_samples * this->frame_size;
|
|
|
|
|
}
|
|
|
|
|
this->callbacks->process(this->callbacks_data, SPA_STATUS_NEED_BUFFER);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
flush_data(this, now_time);
|
|
|
|
|
}
|
2018-01-18 11:57:23 +01:00
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-04-24 17:02:19 +02:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int init_sbc(struct impl *this)
|
|
|
|
|
{
|
|
|
|
|
struct spa_bt_transport *transport = this->transport;
|
|
|
|
|
a2dp_sbc_t *conf = transport->configuration;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
sbc_init(&this->sbc, 0);
|
|
|
|
|
this->sbc.endian = SBC_LE;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (conf->frequency & SBC_SAMPLING_FREQ_48000)
|
|
|
|
|
this->sbc.frequency = SBC_FREQ_48000;
|
|
|
|
|
else if (conf->frequency & SBC_SAMPLING_FREQ_44100)
|
|
|
|
|
this->sbc.frequency = SBC_FREQ_44100;
|
|
|
|
|
else if (conf->frequency & SBC_SAMPLING_FREQ_32000)
|
|
|
|
|
this->sbc.frequency = SBC_FREQ_32000;
|
|
|
|
|
else if (conf->frequency & SBC_SAMPLING_FREQ_16000)
|
|
|
|
|
this->sbc.frequency = SBC_FREQ_16000;
|
|
|
|
|
else
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
if (conf->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
|
|
|
|
|
this->sbc.mode = SBC_MODE_JOINT_STEREO;
|
|
|
|
|
else if (conf->channel_mode & SBC_CHANNEL_MODE_STEREO)
|
|
|
|
|
this->sbc.mode = SBC_MODE_STEREO;
|
|
|
|
|
else if (conf->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
|
|
|
|
|
this->sbc.mode = SBC_MODE_DUAL_CHANNEL;
|
|
|
|
|
else if (conf->channel_mode & SBC_CHANNEL_MODE_MONO)
|
|
|
|
|
this->sbc.mode = SBC_MODE_MONO;
|
|
|
|
|
else
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
switch (conf->subbands) {
|
|
|
|
|
case SBC_SUBBANDS_4:
|
|
|
|
|
this->sbc.subbands = SBC_SB_4;
|
|
|
|
|
break;
|
|
|
|
|
case SBC_SUBBANDS_8:
|
|
|
|
|
this->sbc.subbands = SBC_SB_8;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return -EINVAL;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (conf->allocation_method & SBC_ALLOCATION_LOUDNESS)
|
|
|
|
|
this->sbc.allocation = SBC_AM_LOUDNESS;
|
|
|
|
|
else
|
|
|
|
|
this->sbc.allocation = SBC_AM_SNR;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
switch (conf->block_length) {
|
|
|
|
|
case SBC_BLOCK_LENGTH_4:
|
|
|
|
|
this->sbc.blocks = SBC_BLK_4;
|
|
|
|
|
break;
|
|
|
|
|
case SBC_BLOCK_LENGTH_8:
|
|
|
|
|
this->sbc.blocks = SBC_BLK_8;
|
|
|
|
|
break;
|
|
|
|
|
case SBC_BLOCK_LENGTH_12:
|
|
|
|
|
this->sbc.blocks = SBC_BLK_12;
|
|
|
|
|
break;
|
|
|
|
|
case SBC_BLOCK_LENGTH_16:
|
|
|
|
|
this->sbc.blocks = SBC_BLK_16;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
this->min_bitpool = SPA_MAX(conf->min_bitpool, 12);
|
|
|
|
|
this->max_bitpool = conf->max_bitpool;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
set_bitpool(this, conf->max_bitpool);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
this->seqnum = 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_log_debug(this->log, "a2dp-sink %p: codesize %d frame_length %d size %d:%d %d",
|
|
|
|
|
this, this->codesize, this->frame_length, this->read_size, this->write_size,
|
|
|
|
|
this->sbc.bitpool);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
return 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int do_start(struct impl *this)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
int res, val;
|
|
|
|
|
socklen_t len;
|
|
|
|
|
struct itimerspec ts;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (this->started)
|
|
|
|
|
return 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_log_trace(this->log, "a2dp-sink %p: start", this);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if ((res = this->transport->acquire(this->transport, false)) < 0)
|
|
|
|
|
return res;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
init_sbc(this);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
val = FILL_FRAMES * this->transport->write_mtu;
|
|
|
|
|
if (setsockopt(this->transport->fd, SOL_SOCKET, SO_SNDBUF, &val, sizeof(val)) < 0)
|
|
|
|
|
spa_log_warn(this->log, "a2dp-sink %p: SO_SNDBUF %m", this);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
len = sizeof(val);
|
|
|
|
|
if (getsockopt(this->transport->fd, SOL_SOCKET, SO_SNDBUF, &val, &len) < 0) {
|
|
|
|
|
spa_log_warn(this->log, "a2dp-sink %p: SO_SNDBUF %m", this);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
spa_log_debug(this->log, "a2dp-sink %p: SO_SNDBUF: %d", this, val);
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
val = FILL_FRAMES * this->transport->read_mtu;
|
|
|
|
|
if (setsockopt(this->transport->fd, SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)) < 0)
|
|
|
|
|
spa_log_warn(this->log, "a2dp-sink %p: SO_RCVBUF %m", this);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
val = 6;
|
|
|
|
|
if (setsockopt(this->transport->fd, SOL_SOCKET, SO_PRIORITY, &val, sizeof(val)) < 0)
|
|
|
|
|
spa_log_warn(this->log, "SO_PRIORITY failed: %m");
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
reset_buffer(this);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
this->source.data = this;
|
|
|
|
|
this->source.fd = this->timerfd;
|
|
|
|
|
this->source.func = a2dp_on_timeout;
|
|
|
|
|
this->source.mask = SPA_IO_IN;
|
|
|
|
|
this->source.rmask = 0;
|
|
|
|
|
spa_loop_add_source(this->data_loop, &this->source);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
this->flush_source.data = this;
|
|
|
|
|
this->flush_source.fd = this->transport->fd;
|
|
|
|
|
this->flush_source.func = a2dp_on_flush;
|
|
|
|
|
this->flush_source.mask = 0;
|
|
|
|
|
this->flush_source.rmask = 0;
|
|
|
|
|
spa_loop_add_source(this->data_loop, &this->flush_source);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
ts.it_value.tv_sec = 0;
|
|
|
|
|
ts.it_value.tv_nsec = 1;
|
|
|
|
|
ts.it_interval.tv_sec = 0;
|
|
|
|
|
ts.it_interval.tv_nsec = 0;
|
|
|
|
|
timerfd_settime(this->timerfd, 0, &ts, NULL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
this->started = true;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int do_remove_source(struct spa_loop *loop,
|
|
|
|
|
bool async,
|
|
|
|
|
uint32_t seq,
|
|
|
|
|
const void *data,
|
|
|
|
|
size_t size,
|
|
|
|
|
void *user_data)
|
|
|
|
|
{
|
|
|
|
|
struct impl *this = user_data;
|
|
|
|
|
struct itimerspec ts;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-04-24 17:21:23 +02:00
|
|
|
if (this->source.loop)
|
|
|
|
|
spa_loop_remove_source(this->data_loop, &this->source);
|
2018-01-18 11:57:23 +01:00
|
|
|
ts.it_value.tv_sec = 0;
|
|
|
|
|
ts.it_value.tv_nsec = 0;
|
|
|
|
|
ts.it_interval.tv_sec = 0;
|
|
|
|
|
ts.it_interval.tv_nsec = 0;
|
|
|
|
|
timerfd_settime(this->timerfd, 0, &ts, NULL);
|
2018-04-24 17:21:23 +02:00
|
|
|
if (this->flush_source.loop)
|
|
|
|
|
spa_loop_remove_source(this->data_loop, &this->flush_source);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
return 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int do_stop(struct impl *this)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
int res;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (!this->started)
|
|
|
|
|
return 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_log_trace(this->log, "a2dp-sink %p: stop", this);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_loop_invoke(this->data_loop, do_remove_source, 0, NULL, 0, true, this);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
this->started = false;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
res = this->transport->release(this->transport);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
return res;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int impl_node_send_command(struct spa_node *node, const struct spa_command *command)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
struct impl *this;
|
|
|
|
|
int res;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_return_val_if_fail(node != NULL, -EINVAL);
|
|
|
|
|
spa_return_val_if_fail(command != NULL, -EINVAL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
this = SPA_CONTAINER_OF(node, struct impl, node);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-08-24 10:53:09 +02:00
|
|
|
switch (SPA_NODE_COMMAND_ID(command)) {
|
|
|
|
|
case SPA_NODE_COMMAND_Start:
|
2018-01-18 11:57:23 +01:00
|
|
|
if (!this->have_format)
|
|
|
|
|
return -EIO;
|
|
|
|
|
if (this->n_buffers == 0)
|
|
|
|
|
return -EIO;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if ((res = do_start(this)) < 0)
|
|
|
|
|
return res;
|
2018-08-23 17:47:57 +02:00
|
|
|
break;
|
2018-08-24 10:53:09 +02:00
|
|
|
case SPA_NODE_COMMAND_Pause:
|
2018-01-18 11:57:23 +01:00
|
|
|
if ((res = do_stop(this)) < 0)
|
|
|
|
|
return res;
|
2018-08-23 17:47:57 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
2018-01-18 11:57:23 +01:00
|
|
|
return -ENOTSUP;
|
2018-08-23 17:47:57 +02:00
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int
|
|
|
|
|
impl_node_set_callbacks(struct spa_node *node,
|
|
|
|
|
const struct spa_node_callbacks *callbacks,
|
|
|
|
|
void *data)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
struct impl *this;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_return_val_if_fail(node != NULL, -EINVAL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
this = SPA_CONTAINER_OF(node, struct impl, node);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
this->callbacks = callbacks;
|
|
|
|
|
this->callbacks_data = data;
|
|
|
|
|
|
|
|
|
|
return 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int
|
|
|
|
|
impl_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)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_return_val_if_fail(node != NULL, -EINVAL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (n_input_ports)
|
|
|
|
|
*n_input_ports = 1;
|
|
|
|
|
if (max_input_ports)
|
|
|
|
|
*max_input_ports = 1;
|
|
|
|
|
if (n_output_ports)
|
|
|
|
|
*n_output_ports = 0;
|
|
|
|
|
if (max_output_ports)
|
|
|
|
|
*max_output_ports = 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int
|
|
|
|
|
impl_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)
|
|
|
|
|
{
|
|
|
|
|
spa_return_val_if_fail(node != NULL, -EINVAL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (n_input_ids > 0 && input_ids != NULL)
|
|
|
|
|
input_ids[0] = 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
|
|
|
|
|
static int impl_node_add_port(struct spa_node *node, enum spa_direction direction, uint32_t port_id)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
return -ENOTSUP;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int impl_node_remove_port(struct spa_node *node, enum spa_direction direction, uint32_t port_id)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
return -ENOTSUP;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int
|
|
|
|
|
impl_node_port_get_info(struct spa_node *node,
|
|
|
|
|
enum spa_direction direction, uint32_t port_id, const struct spa_port_info **info)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
struct impl *this;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_return_val_if_fail(node != NULL, -EINVAL);
|
|
|
|
|
spa_return_val_if_fail(info != NULL, -EINVAL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
this = SPA_CONTAINER_OF(node, struct impl, node);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
*info = &this->info;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int
|
|
|
|
|
impl_node_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)
|
|
|
|
|
{
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
struct impl *this;
|
|
|
|
|
struct spa_pod *param;
|
|
|
|
|
struct spa_pod_builder b = { 0 };
|
|
|
|
|
uint8_t buffer[1024];
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_return_val_if_fail(node != NULL, -EINVAL);
|
|
|
|
|
spa_return_val_if_fail(index != NULL, -EINVAL);
|
|
|
|
|
spa_return_val_if_fail(builder != NULL, -EINVAL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
this = SPA_CONTAINER_OF(node, struct impl, node);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
next:
|
|
|
|
|
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-08-23 17:47:57 +02:00
|
|
|
switch (id) {
|
2018-08-25 12:08:29 +02:00
|
|
|
case SPA_PARAM_List:
|
2018-08-23 17:47:57 +02:00
|
|
|
{
|
2018-08-25 12:08:29 +02:00
|
|
|
uint32_t list[] = { SPA_PARAM_EnumFormat,
|
|
|
|
|
SPA_PARAM_Format,
|
|
|
|
|
SPA_PARAM_Buffers,
|
|
|
|
|
SPA_PARAM_Meta };
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (*index < SPA_N_ELEMENTS(list))
|
2018-08-23 17:47:57 +02:00
|
|
|
param = spa_pod_builder_object(&b, id, SPA_ID_OBJECT_ParamList,
|
|
|
|
|
":", SPA_PARAM_LIST_id, "I", list[*index]);
|
2018-01-18 11:57:23 +01:00
|
|
|
else
|
2018-01-11 10:23:37 +01:00
|
|
|
return 0;
|
2018-08-23 17:47:57 +02:00
|
|
|
break;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
2018-08-25 12:08:29 +02:00
|
|
|
case SPA_PARAM_EnumFormat:
|
2018-01-18 11:57:23 +01:00
|
|
|
if (*index > 0)
|
|
|
|
|
return 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (this->transport->codec == 0) {
|
|
|
|
|
a2dp_sbc_t *config = this->transport->configuration;
|
|
|
|
|
int rate, channels;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if ((rate = a2dp_sbc_get_frequency(config)) < 0)
|
|
|
|
|
return -EIO;
|
|
|
|
|
if ((channels = a2dp_sbc_get_channels(config)) < 0)
|
|
|
|
|
return -EIO;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
param = spa_pod_builder_object(&b,
|
2018-08-23 17:47:57 +02:00
|
|
|
id, SPA_ID_OBJECT_Format,
|
|
|
|
|
"I", SPA_MEDIA_TYPE_audio,
|
|
|
|
|
"I", SPA_MEDIA_SUBTYPE_raw,
|
|
|
|
|
":", SPA_FORMAT_AUDIO_format, "I", SPA_AUDIO_FORMAT_S16,
|
2018-08-25 12:08:29 +02:00
|
|
|
":", SPA_FORMAT_AUDIO_layout, "I", SPA_AUDIO_LAYOUT_INTERLEAVED,
|
2018-08-23 17:47:57 +02:00
|
|
|
":", SPA_FORMAT_AUDIO_rate, "i", rate,
|
|
|
|
|
":", SPA_FORMAT_AUDIO_channels, "i", channels);
|
2018-01-18 11:57:23 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return -EIO;
|
2018-08-23 17:47:57 +02:00
|
|
|
break;
|
|
|
|
|
|
2018-08-25 12:08:29 +02:00
|
|
|
case SPA_PARAM_Format:
|
2018-01-18 11:57:23 +01:00
|
|
|
if (!this->have_format)
|
|
|
|
|
return -EIO;
|
|
|
|
|
if (*index > 0)
|
|
|
|
|
return 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
param = spa_pod_builder_object(&b,
|
2018-08-23 17:47:57 +02:00
|
|
|
id, SPA_ID_OBJECT_Format,
|
|
|
|
|
"I", SPA_MEDIA_TYPE_audio,
|
|
|
|
|
"I", SPA_MEDIA_SUBTYPE_raw,
|
|
|
|
|
":", SPA_FORMAT_AUDIO_format, "I", this->current_format.info.raw.format,
|
2018-08-25 12:08:29 +02:00
|
|
|
":", SPA_FORMAT_AUDIO_layout, "I", this->current_format.info.raw.layout,
|
2018-08-23 17:47:57 +02:00
|
|
|
":", SPA_FORMAT_AUDIO_rate, "i", this->current_format.info.raw.rate,
|
|
|
|
|
":", SPA_FORMAT_AUDIO_channels, "i", this->current_format.info.raw.channels);
|
|
|
|
|
break;
|
|
|
|
|
|
2018-08-25 12:08:29 +02:00
|
|
|
case SPA_PARAM_Buffers:
|
2018-01-18 11:57:23 +01:00
|
|
|
if (!this->have_format)
|
|
|
|
|
return -EIO;
|
|
|
|
|
if (*index > 0)
|
|
|
|
|
return 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
param = spa_pod_builder_object(&b,
|
2018-08-23 17:47:57 +02:00
|
|
|
id, SPA_ID_OBJECT_ParamBuffers,
|
|
|
|
|
":", SPA_PARAM_BUFFERS_buffers, "ir", 2,
|
|
|
|
|
SPA_POD_PROP_MIN_MAX(2, MAX_BUFFERS),
|
|
|
|
|
":", SPA_PARAM_BUFFERS_blocks, "i", 1,
|
|
|
|
|
":", SPA_PARAM_BUFFERS_size, "iru", this->props.min_latency *
|
2018-02-20 09:32:40 +01:00
|
|
|
this->frame_size,
|
|
|
|
|
SPA_POD_PROP_MIN_MAX(this->props.min_latency * this->frame_size,
|
|
|
|
|
INT32_MAX),
|
2018-08-23 17:47:57 +02:00
|
|
|
":", SPA_PARAM_BUFFERS_stride, "i", 0,
|
|
|
|
|
":", SPA_PARAM_BUFFERS_align, "i", 16);
|
|
|
|
|
break;
|
|
|
|
|
|
2018-08-25 12:08:29 +02:00
|
|
|
case SPA_PARAM_Meta:
|
2018-01-18 11:57:23 +01:00
|
|
|
if (!this->have_format)
|
|
|
|
|
return -EIO;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
switch (*index) {
|
|
|
|
|
case 0:
|
|
|
|
|
param = spa_pod_builder_object(&b,
|
2018-08-23 17:47:57 +02:00
|
|
|
id, SPA_ID_OBJECT_ParamMeta,
|
|
|
|
|
":", SPA_PARAM_META_type, "I", SPA_META_Header,
|
|
|
|
|
":", SPA_PARAM_META_size, "i", sizeof(struct spa_meta_header));
|
2018-01-18 11:57:23 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
2018-08-23 17:47:57 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2018-01-18 11:57:23 +01:00
|
|
|
return -ENOENT;
|
2018-08-23 17:47:57 +02:00
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
(*index)++;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (spa_pod_filter(builder, result, param, filter) < 0)
|
|
|
|
|
goto next;
|
|
|
|
|
|
|
|
|
|
return 1;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int clear_buffers(struct impl *this)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
do_stop(this);
|
|
|
|
|
if (this->n_buffers > 0) {
|
|
|
|
|
spa_list_init(&this->ready);
|
|
|
|
|
this->n_buffers = 0;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int port_set_format(struct spa_node *node,
|
|
|
|
|
enum spa_direction direction, uint32_t port_id,
|
|
|
|
|
uint32_t flags,
|
|
|
|
|
const struct spa_pod *format)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
struct impl *this = SPA_CONTAINER_OF(node, struct impl, node);
|
2018-01-11 10:23:37 +01:00
|
|
|
int err;
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (format == NULL) {
|
|
|
|
|
spa_log_info(this->log, "clear format");
|
|
|
|
|
clear_buffers(this);
|
|
|
|
|
this->have_format = false;
|
|
|
|
|
} else {
|
|
|
|
|
struct spa_audio_info info = { 0 };
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if ((err = spa_pod_object_parse(format,
|
|
|
|
|
"I", &info.media_type,
|
|
|
|
|
"I", &info.media_subtype)) < 0)
|
|
|
|
|
return err;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-08-23 17:47:57 +02:00
|
|
|
if (info.media_type != SPA_MEDIA_TYPE_audio ||
|
|
|
|
|
info.media_subtype != SPA_MEDIA_SUBTYPE_raw)
|
2018-01-18 11:57:23 +01:00
|
|
|
return -EINVAL;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-08-23 17:47:57 +02:00
|
|
|
if (spa_format_audio_raw_parse(format, &info.info.raw) < 0)
|
2018-01-18 11:57:23 +01:00
|
|
|
return -EINVAL;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
this->frame_size = info.info.raw.channels * 2;
|
|
|
|
|
this->threshold = this->props.min_latency;
|
|
|
|
|
this->current_format = info;
|
|
|
|
|
this->have_format = true;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (this->have_format) {
|
|
|
|
|
this->info.flags = SPA_PORT_INFO_FLAG_CAN_USE_BUFFERS | SPA_PORT_INFO_FLAG_LIVE;
|
|
|
|
|
this->info.rate = this->current_format.info.raw.rate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int
|
|
|
|
|
impl_node_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)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_return_val_if_fail(node != NULL, -EINVAL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-08-23 17:47:57 +02:00
|
|
|
spa_return_val_if_fail(CHECK_PORT(node, direction, port_id), -EINVAL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-08-25 12:08:29 +02:00
|
|
|
if (id == SPA_PARAM_Format) {
|
2018-01-18 11:57:23 +01:00
|
|
|
return port_set_format(node, direction, port_id, flags, param);
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
else
|
2018-01-18 11:57:23 +01:00
|
|
|
return -ENOENT;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int
|
|
|
|
|
impl_node_port_use_buffers(struct spa_node *node,
|
|
|
|
|
enum spa_direction direction,
|
|
|
|
|
uint32_t port_id, struct spa_buffer **buffers, uint32_t n_buffers)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
struct impl *this;
|
|
|
|
|
int i;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_return_val_if_fail(node != NULL, -EINVAL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
this = SPA_CONTAINER_OF(node, struct impl, node);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_log_info(this->log, "use buffers %d", n_buffers);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (!this->have_format)
|
|
|
|
|
return -EIO;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
clear_buffers(this);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
for (i = 0; i < n_buffers; i++) {
|
|
|
|
|
struct buffer *b = &this->buffers[i];
|
|
|
|
|
uint32_t type;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-05-17 17:26:09 +02:00
|
|
|
b->buf = buffers[i];
|
2018-01-18 11:57:23 +01:00
|
|
|
b->outstanding = true;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-08-23 17:47:57 +02:00
|
|
|
b->h = spa_buffer_find_meta_data(buffers[i], SPA_META_Header, sizeof(*b->h));
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
type = buffers[i]->datas[0].type;
|
2018-08-23 17:47:57 +02:00
|
|
|
if ((type == SPA_DATA_MemFd ||
|
|
|
|
|
type == SPA_DATA_DmaBuf ||
|
|
|
|
|
type == SPA_DATA_MemPtr) && buffers[i]->datas[0].data == NULL) {
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_log_error(this->log, NAME " %p: need mapped memory", this);
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
2018-05-17 17:26:09 +02:00
|
|
|
this->threshold = buffers[i]->datas[0].maxsize / this->frame_size;
|
2018-01-18 11:57:23 +01:00
|
|
|
}
|
|
|
|
|
this->n_buffers = n_buffers;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int
|
|
|
|
|
impl_node_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)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
struct impl *this;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_return_val_if_fail(node != NULL, -EINVAL);
|
|
|
|
|
spa_return_val_if_fail(buffers != NULL, -EINVAL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
this = SPA_CONTAINER_OF(node, struct impl, node);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (!this->have_format)
|
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
|
|
return -ENOTSUP;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2018-01-18 11:57:23 +01:00
|
|
|
impl_node_port_set_io(struct spa_node *node,
|
|
|
|
|
enum spa_direction direction,
|
|
|
|
|
uint32_t port_id,
|
|
|
|
|
uint32_t id,
|
|
|
|
|
void *data, size_t size)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
|
|
|
|
struct impl *this;
|
|
|
|
|
|
|
|
|
|
spa_return_val_if_fail(node != NULL, -EINVAL);
|
|
|
|
|
|
|
|
|
|
this = SPA_CONTAINER_OF(node, struct impl, node);
|
|
|
|
|
|
|
|
|
|
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
|
|
|
|
|
|
2018-08-23 17:47:57 +02:00
|
|
|
switch (id) {
|
2018-08-25 12:08:29 +02:00
|
|
|
case SPA_IO_Buffers:
|
2018-01-18 11:57:23 +01:00
|
|
|
this->io = data;
|
2018-08-23 17:47:57 +02:00
|
|
|
break;
|
2018-08-25 12:08:29 +02:00
|
|
|
case SPA_IO_ControlRange:
|
2018-01-18 11:57:23 +01:00
|
|
|
this->range = data;
|
2018-08-23 17:47:57 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
2018-01-18 11:57:23 +01:00
|
|
|
return -ENOENT;
|
2018-08-23 17:47:57 +02:00
|
|
|
}
|
2018-01-18 11:57:23 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int impl_node_port_reuse_buffer(struct spa_node *node, uint32_t port_id, uint32_t buffer_id)
|
|
|
|
|
{
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
impl_node_port_send_command(struct spa_node *node,
|
|
|
|
|
enum spa_direction direction, uint32_t port_id, const struct spa_command *command)
|
|
|
|
|
{
|
|
|
|
|
return -ENOTSUP;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-03-20 11:37:11 +01:00
|
|
|
static int impl_node_process(struct spa_node *node)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
|
|
|
|
struct impl *this;
|
|
|
|
|
struct spa_io_buffers *input;
|
|
|
|
|
|
|
|
|
|
spa_return_val_if_fail(node != NULL, -EINVAL);
|
|
|
|
|
|
|
|
|
|
this = SPA_CONTAINER_OF(node, struct impl, node);
|
|
|
|
|
input = this->io;
|
|
|
|
|
spa_return_val_if_fail(input != NULL, -EIO);
|
|
|
|
|
|
|
|
|
|
if (input->status == SPA_STATUS_HAVE_BUFFER && input->buffer_id < this->n_buffers) {
|
|
|
|
|
struct buffer *b = &this->buffers[input->buffer_id];
|
2018-04-24 17:02:19 +02:00
|
|
|
uint64_t now_time;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
|
|
|
|
if (!b->outstanding) {
|
|
|
|
|
spa_log_warn(this->log, NAME " %p: buffer %u in use", this, input->buffer_id);
|
|
|
|
|
input->status = -EINVAL;
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spa_log_trace(this->log, NAME " %p: queue buffer %u", this, input->buffer_id);
|
|
|
|
|
|
|
|
|
|
spa_list_append(&this->ready, &b->link);
|
|
|
|
|
b->outstanding = false;
|
2018-04-24 17:02:19 +02:00
|
|
|
|
2018-05-17 17:26:09 +02:00
|
|
|
this->threshold = SPA_MIN(b->buf->datas[0].chunk->size / this->frame_size,
|
|
|
|
|
this->props.max_latency);
|
|
|
|
|
|
2018-04-24 17:02:19 +02:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &this->now);
|
|
|
|
|
now_time = this->now.tv_sec * SPA_NSEC_PER_SEC + this->now.tv_nsec;
|
|
|
|
|
|
|
|
|
|
flush_data(this, now_time);
|
|
|
|
|
|
2018-01-11 10:23:37 +01:00
|
|
|
input->status = SPA_STATUS_OK;
|
|
|
|
|
}
|
|
|
|
|
return SPA_STATUS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct spa_dict_item node_info_items[] = {
|
|
|
|
|
{ "media.class", "Audio/Sink" },
|
2018-04-24 17:02:19 +02:00
|
|
|
{ "node.driver", "true" },
|
2018-01-11 10:23:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const struct spa_dict node_info = {
|
|
|
|
|
node_info_items,
|
|
|
|
|
SPA_N_ELEMENTS(node_info_items)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const struct spa_node impl_node = {
|
|
|
|
|
SPA_VERSION_NODE,
|
|
|
|
|
&node_info,
|
|
|
|
|
impl_node_enum_params,
|
|
|
|
|
impl_node_set_param,
|
|
|
|
|
impl_node_send_command,
|
|
|
|
|
impl_node_set_callbacks,
|
|
|
|
|
impl_node_get_n_ports,
|
|
|
|
|
impl_node_get_port_ids,
|
|
|
|
|
impl_node_add_port,
|
|
|
|
|
impl_node_remove_port,
|
|
|
|
|
impl_node_port_get_info,
|
|
|
|
|
impl_node_port_enum_params,
|
|
|
|
|
impl_node_port_set_param,
|
|
|
|
|
impl_node_port_use_buffers,
|
|
|
|
|
impl_node_port_alloc_buffers,
|
|
|
|
|
impl_node_port_set_io,
|
|
|
|
|
impl_node_port_reuse_buffer,
|
|
|
|
|
impl_node_port_send_command,
|
2018-03-20 11:37:11 +01:00
|
|
|
impl_node_process,
|
2018-01-11 10:23:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int impl_get_interface(struct spa_handle *handle, uint32_t interface_id, void **interface)
|
|
|
|
|
{
|
|
|
|
|
struct impl *this;
|
|
|
|
|
|
|
|
|
|
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
|
|
|
|
spa_return_val_if_fail(interface != NULL, -EINVAL);
|
|
|
|
|
|
|
|
|
|
this = (struct impl *) handle;
|
|
|
|
|
|
2018-08-23 17:47:57 +02:00
|
|
|
if (interface_id == SPA_ID_INTERFACE_Node)
|
2018-01-11 10:23:37 +01:00
|
|
|
*interface = &this->node;
|
|
|
|
|
else
|
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int impl_clear(struct spa_handle *handle)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-09 10:06:17 +02:00
|
|
|
static size_t
|
|
|
|
|
impl_get_size(const struct spa_handle_factory *factory,
|
|
|
|
|
const struct spa_dict *params)
|
|
|
|
|
{
|
|
|
|
|
return sizeof(struct impl);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-11 10:23:37 +01:00
|
|
|
static int
|
|
|
|
|
impl_init(const struct spa_handle_factory *factory,
|
|
|
|
|
struct spa_handle *handle,
|
|
|
|
|
const struct spa_dict *info,
|
|
|
|
|
const struct spa_support *support,
|
|
|
|
|
uint32_t n_support)
|
|
|
|
|
{
|
|
|
|
|
struct impl *this;
|
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
|
|
spa_return_val_if_fail(factory != NULL, -EINVAL);
|
|
|
|
|
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
|
|
|
|
|
|
|
|
|
handle->get_interface = impl_get_interface;
|
|
|
|
|
handle->clear = impl_clear;
|
|
|
|
|
|
|
|
|
|
this = (struct impl *) handle;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < n_support; i++) {
|
2018-08-23 17:47:57 +02:00
|
|
|
if (support[i].type == SPA_ID_INTERFACE_Log)
|
2018-01-11 10:23:37 +01:00
|
|
|
this->log = support[i].data;
|
2018-08-23 17:47:57 +02:00
|
|
|
else if (support[i].type == SPA_ID_INTERFACE_DataLoop)
|
2018-01-11 10:23:37 +01:00
|
|
|
this->data_loop = support[i].data;
|
2018-08-23 17:47:57 +02:00
|
|
|
else if (support[i].type == SPA_ID_INTERFACE_MainLoop)
|
2018-01-11 10:23:37 +01:00
|
|
|
this->main_loop = support[i].data;
|
|
|
|
|
}
|
|
|
|
|
if (this->data_loop == NULL) {
|
|
|
|
|
spa_log_error(this->log, "a data loop is needed");
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
if (this->main_loop == NULL) {
|
|
|
|
|
spa_log_error(this->log, "a main loop is needed");
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->node = impl_node;
|
|
|
|
|
reset_props(&this->props);
|
|
|
|
|
|
|
|
|
|
this->info.flags = SPA_PORT_INFO_FLAG_CAN_USE_BUFFERS;
|
|
|
|
|
|
|
|
|
|
spa_list_init(&this->ready);
|
|
|
|
|
|
|
|
|
|
for (i = 0; info && i < info->n_items; i++) {
|
|
|
|
|
if (strcmp(info->items[i].key, "bluez5.transport") == 0)
|
|
|
|
|
sscanf(info->items[i].value, "%p", &this->transport);
|
|
|
|
|
}
|
|
|
|
|
if (this->transport == NULL) {
|
|
|
|
|
spa_log_error(this->log, "a transport is needed");
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
this->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct spa_interface_info impl_interfaces[] = {
|
2018-08-23 17:47:57 +02:00
|
|
|
{SPA_ID_INTERFACE_Node,},
|
2018-01-11 10:23:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
impl_enum_interface_info(const struct spa_handle_factory *factory,
|
|
|
|
|
const struct spa_interface_info **info, uint32_t *index)
|
|
|
|
|
{
|
|
|
|
|
spa_return_val_if_fail(factory != NULL, -EINVAL);
|
|
|
|
|
spa_return_val_if_fail(info != NULL, -EINVAL);
|
|
|
|
|
spa_return_val_if_fail(index != NULL, -EINVAL);
|
|
|
|
|
|
|
|
|
|
switch (*index) {
|
|
|
|
|
case 0:
|
|
|
|
|
*info = &impl_interfaces[*index];
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
(*index)++;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct spa_dict_item info_items[] = {
|
|
|
|
|
{ "factory.author", "Wim Taymans <wim.taymans@gmail.com>" },
|
|
|
|
|
{ "factory.description", "Play audio with the a2dp" },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const struct spa_dict info = {
|
|
|
|
|
info_items,
|
|
|
|
|
SPA_N_ELEMENTS(info_items),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct spa_handle_factory spa_a2dp_sink_factory = {
|
|
|
|
|
SPA_VERSION_HANDLE_FACTORY,
|
|
|
|
|
NAME,
|
|
|
|
|
&info,
|
2018-04-09 10:06:17 +02:00
|
|
|
impl_get_size,
|
2018-01-11 10:23:37 +01:00
|
|
|
impl_init,
|
|
|
|
|
impl_enum_interface_info,
|
|
|
|
|
};
|