2023-03-25 16:58:27 +02:00
|
|
|
/* Spa ISO I/O */
|
|
|
|
|
/* SPDX-FileCopyrightText: Copyright © 2023 Pauli Virtanen. */
|
|
|
|
|
/* SPDX-License-Identifier: MIT */
|
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
|
|
#include <spa/support/loop.h>
|
|
|
|
|
#include <spa/support/log.h>
|
|
|
|
|
#include <spa/utils/list.h>
|
|
|
|
|
#include <spa/utils/string.h>
|
|
|
|
|
#include <spa/utils/result.h>
|
|
|
|
|
#include <spa/node/io.h>
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#include "iso-io.h"
|
|
|
|
|
|
2023-04-09 21:28:40 +03:00
|
|
|
#include "media-codecs.h"
|
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
2023-12-23 21:07:45 +02:00
|
|
|
SPA_LOG_TOPIC_DEFINE_STATIC(log_topic, "spa.bluez5.iso");
|
2023-03-25 16:58:27 +02:00
|
|
|
#undef SPA_LOG_TOPIC_DEFAULT
|
|
|
|
|
#define SPA_LOG_TOPIC_DEFAULT &log_topic
|
|
|
|
|
|
2024-02-20 23:01:07 +02:00
|
|
|
#include "bt-latency.h"
|
|
|
|
|
|
2023-04-09 21:28:40 +03:00
|
|
|
#define IDLE_TIME (500 * SPA_NSEC_PER_MSEC)
|
|
|
|
|
#define EMPTY_BUF_SIZE 65536
|
2023-03-25 16:58:27 +02:00
|
|
|
|
2024-05-31 20:31:59 +03:00
|
|
|
#define LATENCY_PERIOD (1000 * SPA_NSEC_PER_MSEC)
|
|
|
|
|
#define MAX_LATENCY (50 * SPA_NSEC_PER_MSEC)
|
2024-02-20 23:01:07 +02:00
|
|
|
|
2023-03-25 16:58:27 +02:00
|
|
|
struct group {
|
|
|
|
|
struct spa_log *log;
|
|
|
|
|
struct spa_loop *data_loop;
|
|
|
|
|
struct spa_system *data_system;
|
|
|
|
|
struct spa_source source;
|
|
|
|
|
struct spa_list streams;
|
|
|
|
|
int timerfd;
|
2023-11-12 18:24:45 +02:00
|
|
|
uint8_t id;
|
2023-03-25 16:58:27 +02:00
|
|
|
uint64_t next;
|
|
|
|
|
uint64_t duration;
|
2024-05-31 20:31:59 +03:00
|
|
|
bool flush;
|
2023-04-09 21:28:40 +03:00
|
|
|
bool started;
|
2023-03-25 16:58:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct stream {
|
|
|
|
|
struct spa_bt_iso_io this;
|
|
|
|
|
struct spa_list link;
|
|
|
|
|
struct group *group;
|
|
|
|
|
int fd;
|
|
|
|
|
bool sink;
|
|
|
|
|
bool idle;
|
|
|
|
|
|
|
|
|
|
spa_bt_iso_io_pull_t pull;
|
2023-04-09 21:28:40 +03:00
|
|
|
|
|
|
|
|
const struct media_codec *codec;
|
|
|
|
|
uint32_t block_size;
|
2024-02-20 23:01:07 +02:00
|
|
|
|
|
|
|
|
struct spa_bt_latency tx_latency;
|
2023-03-25 16:58:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct modify_info
|
|
|
|
|
{
|
|
|
|
|
struct stream *stream;
|
|
|
|
|
struct spa_list *streams;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int do_modify(struct spa_loop *loop, bool async, uint32_t seq, const void *data, size_t size, void *user_data)
|
|
|
|
|
{
|
|
|
|
|
struct modify_info *info = user_data;
|
|
|
|
|
|
|
|
|
|
if (info->streams)
|
|
|
|
|
spa_list_append(info->streams, &info->stream->link);
|
|
|
|
|
else
|
|
|
|
|
spa_list_remove(&info->stream->link);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void stream_link(struct group *group, struct stream *stream)
|
|
|
|
|
{
|
|
|
|
|
struct modify_info info = { .stream = stream, .streams = &group->streams };
|
|
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
res = spa_loop_invoke(group->data_loop, do_modify, 0, NULL, 0, true, &info);
|
|
|
|
|
spa_assert_se(res == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void stream_unlink(struct stream *stream)
|
|
|
|
|
{
|
|
|
|
|
struct modify_info info = { .stream = stream, .streams = NULL };
|
|
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
res = spa_loop_invoke(stream->group->data_loop, do_modify, 0, NULL, 0, true, &info);
|
|
|
|
|
spa_assert_se(res == 0);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 21:28:40 +03:00
|
|
|
static int stream_silence(struct stream *stream)
|
|
|
|
|
{
|
|
|
|
|
static uint8_t empty[EMPTY_BUF_SIZE] = {0};
|
|
|
|
|
const size_t max_size = sizeof(stream->this.buf);
|
|
|
|
|
int res, used, need_flush;
|
|
|
|
|
size_t encoded;
|
|
|
|
|
|
|
|
|
|
stream->idle = true;
|
|
|
|
|
|
|
|
|
|
res = used = stream->codec->start_encode(stream->this.codec_data, stream->this.buf, max_size, 0, 0);
|
|
|
|
|
if (res < 0)
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
|
|
res = stream->codec->encode(stream->this.codec_data, empty, stream->block_size,
|
|
|
|
|
SPA_PTROFF(stream->this.buf, used, void), max_size - used, &encoded, &need_flush);
|
|
|
|
|
if (res < 0)
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
|
|
used += encoded;
|
|
|
|
|
|
|
|
|
|
if (!need_flush)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
stream->this.size = used;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-25 16:58:27 +02:00
|
|
|
static int set_timeout(struct group *group, uint64_t time)
|
|
|
|
|
{
|
|
|
|
|
struct itimerspec ts;
|
|
|
|
|
ts.it_value.tv_sec = time / SPA_NSEC_PER_SEC;
|
|
|
|
|
ts.it_value.tv_nsec = time % SPA_NSEC_PER_SEC;
|
|
|
|
|
ts.it_interval.tv_sec = 0;
|
|
|
|
|
ts.it_interval.tv_nsec = 0;
|
|
|
|
|
return spa_system_timerfd_settime(group->data_system,
|
|
|
|
|
group->timerfd, SPA_FD_TIMER_ABSTIME, &ts, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-20 23:01:07 +02:00
|
|
|
static uint64_t get_time_ns(struct spa_system *system, clockid_t clockid)
|
2023-03-25 16:58:27 +02:00
|
|
|
{
|
|
|
|
|
struct timespec now;
|
|
|
|
|
|
2024-02-20 23:01:07 +02:00
|
|
|
spa_system_clock_gettime(system, clockid, &now);
|
|
|
|
|
return SPA_TIMESPEC_TO_NSEC(&now);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int set_timers(struct group *group)
|
|
|
|
|
{
|
2024-04-01 17:26:35 +03:00
|
|
|
if (group->duration == 0)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
2024-02-20 23:01:07 +02:00
|
|
|
group->next = SPA_ROUND_UP(get_time_ns(group->data_system, CLOCK_MONOTONIC) + group->duration,
|
2023-03-25 16:58:27 +02:00
|
|
|
group->duration);
|
|
|
|
|
|
|
|
|
|
return set_timeout(group, group->next);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-02 00:37:02 +02:00
|
|
|
static void drop_rx(int fd)
|
|
|
|
|
{
|
|
|
|
|
ssize_t res;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
res = recv(fd, NULL, 0, MSG_TRUNC | MSG_DONTWAIT);
|
|
|
|
|
} while (res >= 0);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-31 20:31:59 +03:00
|
|
|
static bool group_latency_check(struct group *group)
|
|
|
|
|
{
|
|
|
|
|
struct stream *stream;
|
|
|
|
|
int32_t min_latency = INT32_MAX, max_latency = INT32_MIN;
|
|
|
|
|
unsigned int kernel_queue = UINT_MAX;
|
|
|
|
|
|
|
|
|
|
spa_list_for_each(stream, &group->streams, link) {
|
|
|
|
|
if (!stream->sink)
|
|
|
|
|
continue;
|
|
|
|
|
if (!stream->tx_latency.enabled)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (kernel_queue == UINT_MAX)
|
|
|
|
|
kernel_queue = stream->tx_latency.kernel_queue;
|
|
|
|
|
|
|
|
|
|
if (group->flush && stream->tx_latency.queue) {
|
|
|
|
|
spa_log_debug(group->log, "%p: ISO group:%d latency skip: flushing",
|
|
|
|
|
group, group->id);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (stream->tx_latency.kernel_queue != kernel_queue) {
|
|
|
|
|
/* Streams out of sync, try to correct if it persists */
|
|
|
|
|
spa_log_debug(group->log, "%p: ISO group:%d latency skip: imbalance",
|
|
|
|
|
group, group->id);
|
|
|
|
|
group->flush = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group->flush = false;
|
|
|
|
|
|
|
|
|
|
spa_list_for_each(stream, &group->streams, link) {
|
|
|
|
|
if (!stream->sink)
|
|
|
|
|
continue;
|
|
|
|
|
if (!stream->tx_latency.valid)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
min_latency = SPA_MIN(min_latency, stream->tx_latency.ptp.min);
|
|
|
|
|
max_latency = SPA_MAX(max_latency, stream->tx_latency.ptp.max);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (max_latency > MAX_LATENCY) {
|
|
|
|
|
spa_log_debug(group->log, "%p: ISO group:%d latency skip: latency %d ms",
|
|
|
|
|
group, group->id, (int)(max_latency / SPA_NSEC_PER_MSEC));
|
|
|
|
|
group->flush = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-25 16:58:27 +02:00
|
|
|
static void group_on_timeout(struct spa_source *source)
|
|
|
|
|
{
|
|
|
|
|
struct group *group = source->data;
|
|
|
|
|
struct stream *stream;
|
2023-04-09 21:28:40 +03:00
|
|
|
bool resync = false;
|
|
|
|
|
bool fail = false;
|
2023-03-25 16:58:27 +02:00
|
|
|
uint64_t exp;
|
|
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
if ((res = spa_system_timerfd_read(group->data_system, group->timerfd, &exp)) < 0) {
|
|
|
|
|
if (res != -EAGAIN)
|
|
|
|
|
spa_log_warn(group->log, "%p: ISO group:%u error reading timerfd: %s",
|
2023-11-12 18:24:45 +02:00
|
|
|
group, group->id, spa_strerror(res));
|
2023-03-25 16:58:27 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2024-05-31 20:31:59 +03:00
|
|
|
if (!exp)
|
|
|
|
|
return;
|
2023-03-25 16:58:27 +02:00
|
|
|
|
|
|
|
|
spa_list_for_each(stream, &group->streams, link) {
|
2024-03-02 00:37:02 +02:00
|
|
|
if (!stream->sink) {
|
|
|
|
|
if (!stream->pull) {
|
|
|
|
|
/* Source not running: drop any incoming data */
|
|
|
|
|
drop_rx(stream->fd);
|
|
|
|
|
}
|
2023-03-25 16:58:27 +02:00
|
|
|
continue;
|
2024-03-02 00:37:02 +02:00
|
|
|
}
|
2023-03-25 16:58:27 +02:00
|
|
|
|
2024-02-20 23:01:07 +02:00
|
|
|
spa_bt_latency_recv_errqueue(&stream->tx_latency, stream->fd, group->log);
|
|
|
|
|
|
2023-04-09 21:28:40 +03:00
|
|
|
if (stream->this.need_resync) {
|
|
|
|
|
resync = true;
|
|
|
|
|
stream->this.need_resync = false;
|
|
|
|
|
}
|
2023-03-25 16:58:27 +02:00
|
|
|
|
2023-04-09 21:28:40 +03:00
|
|
|
if (!group->started && !stream->idle && stream->this.size > 0)
|
|
|
|
|
group->started = true;
|
2023-03-25 16:58:27 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-31 20:31:59 +03:00
|
|
|
if (group_latency_check(group)) {
|
|
|
|
|
spa_list_for_each(stream, &group->streams, link)
|
|
|
|
|
spa_bt_latency_reset(&stream->tx_latency);
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-25 16:58:27 +02:00
|
|
|
/* Produce output */
|
|
|
|
|
spa_list_for_each(stream, &group->streams, link) {
|
2024-04-12 18:14:54 +02:00
|
|
|
int res = 0;
|
2024-02-20 23:01:07 +02:00
|
|
|
uint64_t now;
|
2023-03-25 16:58:27 +02:00
|
|
|
|
|
|
|
|
if (!stream->sink)
|
|
|
|
|
continue;
|
2024-02-20 23:01:07 +02:00
|
|
|
if (!group->started) {
|
2023-04-09 19:53:42 +03:00
|
|
|
stream->this.resync = true;
|
2023-03-25 16:58:27 +02:00
|
|
|
stream->this.size = 0;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-04-09 21:28:40 +03:00
|
|
|
if (stream->this.size == 0) {
|
|
|
|
|
spa_log_debug(group->log, "%p: ISO group:%u miss fd:%d",
|
2023-11-12 18:24:45 +02:00
|
|
|
group, group->id, stream->fd);
|
2023-04-09 21:28:40 +03:00
|
|
|
if (stream_silence(stream) < 0) {
|
|
|
|
|
fail = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-25 16:58:27 +02:00
|
|
|
|
2024-02-20 23:01:07 +02:00
|
|
|
now = get_time_ns(group->data_system, CLOCK_REALTIME);
|
2024-05-31 20:31:59 +03:00
|
|
|
res = spa_bt_send(stream->fd, stream->this.buf, stream->this.size,
|
|
|
|
|
&stream->tx_latency, now);
|
2023-04-09 21:28:40 +03:00
|
|
|
if (res < 0) {
|
2023-03-25 16:58:27 +02:00
|
|
|
res = -errno;
|
2023-04-09 21:28:40 +03:00
|
|
|
fail = true;
|
2024-05-31 20:31:59 +03:00
|
|
|
group->flush = true;
|
2023-04-09 21:28:40 +03:00
|
|
|
}
|
2023-03-25 16:58:27 +02:00
|
|
|
|
2024-05-31 20:31:59 +03:00
|
|
|
spa_log_trace(group->log, "%p: ISO group:%u sent fd:%d size:%u ts:%u idle:%d res:%d latency:%d..%d%sus queue:%u",
|
2023-11-12 18:24:45 +02:00
|
|
|
group, group->id, stream->fd, (unsigned)stream->this.size,
|
2024-02-20 23:01:07 +02:00
|
|
|
(unsigned)stream->this.timestamp, stream->idle, res,
|
2024-05-31 20:31:59 +03:00
|
|
|
stream->tx_latency.ptp.min/1000, stream->tx_latency.ptp.max/1000,
|
|
|
|
|
stream->tx_latency.valid ? " " : "* ",
|
|
|
|
|
stream->tx_latency.queue);
|
2023-03-25 16:58:27 +02:00
|
|
|
|
|
|
|
|
stream->this.size = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 21:28:40 +03:00
|
|
|
if (fail)
|
2024-02-20 23:01:07 +02:00
|
|
|
spa_log_debug(group->log, "%p: ISO group:%d send failure", group, group->id);
|
2023-04-09 21:28:40 +03:00
|
|
|
|
2024-05-31 20:31:59 +03:00
|
|
|
done:
|
2023-03-25 16:58:27 +02:00
|
|
|
/* Pull data for the next interval */
|
|
|
|
|
group->next += exp * group->duration;
|
|
|
|
|
|
|
|
|
|
spa_list_for_each(stream, &group->streams, link) {
|
|
|
|
|
if (!stream->sink)
|
|
|
|
|
continue;
|
|
|
|
|
|
2023-04-09 21:28:40 +03:00
|
|
|
if (resync)
|
|
|
|
|
stream->this.resync = true;
|
|
|
|
|
|
2023-03-25 16:58:27 +02:00
|
|
|
if (stream->pull) {
|
2023-04-09 21:28:40 +03:00
|
|
|
stream->idle = false;
|
2023-03-25 16:58:27 +02:00
|
|
|
stream->this.now = group->next;
|
|
|
|
|
stream->pull(&stream->this);
|
|
|
|
|
} else {
|
2023-04-09 21:28:40 +03:00
|
|
|
stream_silence(stream);
|
2023-03-25 16:58:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set_timeout(group, group->next);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 21:28:40 +03:00
|
|
|
static struct group *group_create(struct spa_bt_transport *t,
|
2023-04-06 23:02:44 +03:00
|
|
|
struct spa_log *log, struct spa_loop *data_loop, struct spa_system *data_system)
|
2023-03-25 16:58:27 +02:00
|
|
|
{
|
|
|
|
|
struct group *group;
|
2023-11-12 18:24:45 +02:00
|
|
|
uint8_t id;
|
2023-03-25 16:58:27 +02:00
|
|
|
|
2023-11-12 18:24:45 +02:00
|
|
|
if (t->profile & (SPA_BT_PROFILE_BAP_SINK | SPA_BT_PROFILE_BAP_SOURCE)) {
|
|
|
|
|
id = t->bap_cig;
|
|
|
|
|
} else if (t->profile & (SPA_BT_PROFILE_BAP_BROADCAST_SINK | SPA_BT_PROFILE_BAP_BROADCAST_SOURCE)) {
|
|
|
|
|
id = t->bap_big;
|
|
|
|
|
} else {
|
|
|
|
|
errno = EINVAL;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-25 16:58:27 +02:00
|
|
|
group = calloc(1, sizeof(struct group));
|
|
|
|
|
if (group == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
spa_log_topic_init(log, &log_topic);
|
|
|
|
|
|
2023-11-12 18:24:45 +02:00
|
|
|
group->id = id;
|
2023-03-25 16:58:27 +02:00
|
|
|
group->log = log;
|
|
|
|
|
group->data_loop = data_loop;
|
|
|
|
|
group->data_system = data_system;
|
2024-04-01 17:26:35 +03:00
|
|
|
group->duration = 0;
|
2023-03-25 16:58:27 +02:00
|
|
|
|
|
|
|
|
spa_list_init(&group->streams);
|
|
|
|
|
|
|
|
|
|
group->timerfd = spa_system_timerfd_create(group->data_system,
|
|
|
|
|
CLOCK_MONOTONIC, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
|
|
|
|
|
if (group->timerfd < 0) {
|
|
|
|
|
int err = errno;
|
|
|
|
|
free(group);
|
|
|
|
|
errno = err;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group->source.data = group;
|
|
|
|
|
group->source.fd = group->timerfd;
|
|
|
|
|
group->source.func = group_on_timeout;
|
|
|
|
|
group->source.mask = SPA_IO_IN;
|
|
|
|
|
group->source.rmask = 0;
|
|
|
|
|
spa_loop_add_source(group->data_loop, &group->source);
|
|
|
|
|
|
|
|
|
|
return group;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int do_remove_source(struct spa_loop *loop, bool async, uint32_t seq,
|
|
|
|
|
const void *data, size_t size, void *user_data)
|
|
|
|
|
{
|
|
|
|
|
struct group *group = user_data;
|
|
|
|
|
|
|
|
|
|
if (group->source.loop)
|
|
|
|
|
spa_loop_remove_source(group->data_loop, &group->source);
|
|
|
|
|
|
|
|
|
|
set_timeout(group, 0);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void group_destroy(struct group *group)
|
|
|
|
|
{
|
|
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
spa_assert(spa_list_is_empty(&group->streams));
|
|
|
|
|
|
|
|
|
|
res = spa_loop_invoke(group->data_loop, do_remove_source, 0, NULL, 0, true, group);
|
|
|
|
|
spa_assert_se(res == 0);
|
|
|
|
|
|
|
|
|
|
close(group->timerfd);
|
|
|
|
|
free(group);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 04:34:59 +02:00
|
|
|
static struct stream *stream_create(struct spa_bt_transport *t, struct group *group)
|
2023-03-25 16:58:27 +02:00
|
|
|
{
|
|
|
|
|
struct stream *stream;
|
2023-04-09 21:28:40 +03:00
|
|
|
void *codec_data = NULL;
|
|
|
|
|
int block_size = 0;
|
|
|
|
|
struct spa_audio_info format = { 0 };
|
|
|
|
|
int res;
|
2023-07-23 22:16:17 +03:00
|
|
|
bool sink;
|
2024-04-01 17:26:35 +03:00
|
|
|
|
|
|
|
|
if (t->profile == SPA_BT_PROFILE_BAP_SINK ||
|
|
|
|
|
t->profile == SPA_BT_PROFILE_BAP_BROADCAST_SINK) {
|
2023-07-23 22:16:17 +03:00
|
|
|
sink = true;
|
2023-08-22 14:53:51 +03:00
|
|
|
} else {
|
2023-07-23 22:16:17 +03:00
|
|
|
sink = false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 17:26:35 +03:00
|
|
|
if (!t->media_codec->bap || !t->media_codec->get_interval) {
|
2023-04-09 21:28:40 +03:00
|
|
|
res = -EINVAL;
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sink) {
|
2024-04-01 17:26:35 +03:00
|
|
|
uint64_t interval;
|
|
|
|
|
|
2023-04-09 21:28:40 +03:00
|
|
|
res = t->media_codec->validate_config(t->media_codec, 0, t->configuration, t->configuration_len, &format);
|
|
|
|
|
if (res < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
codec_data = t->media_codec->init(t->media_codec, 0, t->configuration, t->configuration_len,
|
|
|
|
|
&format, NULL, t->write_mtu);
|
|
|
|
|
if (!codec_data) {
|
|
|
|
|
res = -EINVAL;
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
block_size = t->media_codec->get_block_size(codec_data);
|
|
|
|
|
if (block_size < 0 || block_size > EMPTY_BUF_SIZE) {
|
|
|
|
|
res = -EINVAL;
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2024-04-01 17:26:35 +03:00
|
|
|
|
|
|
|
|
interval = t->media_codec->get_interval(codec_data);
|
|
|
|
|
if (interval <= 5000) {
|
|
|
|
|
res = -EINVAL;
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (group->duration == 0) {
|
|
|
|
|
group->duration = interval;
|
|
|
|
|
} else if (interval != group->duration) {
|
|
|
|
|
/* SDU_Interval in ISO group must be same for each direction */
|
|
|
|
|
res = -EINVAL;
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2023-04-09 21:28:40 +03:00
|
|
|
}
|
2023-03-25 16:58:27 +02:00
|
|
|
|
|
|
|
|
stream = calloc(1, sizeof(struct stream));
|
|
|
|
|
if (stream == NULL)
|
2023-04-09 21:28:40 +03:00
|
|
|
goto fail_errno;
|
2023-03-25 16:58:27 +02:00
|
|
|
|
2023-04-09 21:28:40 +03:00
|
|
|
stream->fd = t->fd;
|
2023-03-25 16:58:27 +02:00
|
|
|
stream->sink = sink;
|
|
|
|
|
stream->group = group;
|
2024-04-01 17:26:35 +03:00
|
|
|
stream->this.duration = sink ? group->duration : 0;
|
2023-03-25 16:58:27 +02:00
|
|
|
|
2023-04-09 21:28:40 +03:00
|
|
|
stream->codec = t->media_codec;
|
|
|
|
|
stream->this.codec_data = codec_data;
|
|
|
|
|
stream->this.format = format;
|
|
|
|
|
stream->block_size = block_size;
|
|
|
|
|
|
2024-05-26 12:46:31 +03:00
|
|
|
spa_bt_latency_init(&stream->tx_latency, t, LATENCY_PERIOD, group->log);
|
2024-02-20 23:01:07 +02:00
|
|
|
|
2023-04-09 21:28:40 +03:00
|
|
|
if (sink)
|
|
|
|
|
stream_silence(stream);
|
|
|
|
|
|
2023-03-25 16:58:27 +02:00
|
|
|
stream_link(group, stream);
|
|
|
|
|
|
|
|
|
|
return stream;
|
2023-04-09 21:28:40 +03:00
|
|
|
|
|
|
|
|
fail_errno:
|
|
|
|
|
res = -errno;
|
|
|
|
|
fail:
|
|
|
|
|
if (codec_data)
|
|
|
|
|
t->media_codec->deinit(codec_data);
|
|
|
|
|
errno = -res;
|
|
|
|
|
return NULL;
|
2023-03-25 16:58:27 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-09 21:28:40 +03:00
|
|
|
struct spa_bt_iso_io *spa_bt_iso_io_create(struct spa_bt_transport *t,
|
2023-04-06 23:02:44 +03:00
|
|
|
struct spa_log *log, struct spa_loop *data_loop, struct spa_system *data_system)
|
2023-03-25 16:58:27 +02:00
|
|
|
{
|
|
|
|
|
struct stream *stream;
|
|
|
|
|
struct group *group;
|
|
|
|
|
|
2023-04-09 21:28:40 +03:00
|
|
|
group = group_create(t, log, data_loop, data_system);
|
2023-03-25 16:58:27 +02:00
|
|
|
if (group == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2023-04-09 21:28:40 +03:00
|
|
|
stream = stream_create(t, group);
|
2023-03-25 16:58:27 +02:00
|
|
|
if (stream == NULL) {
|
|
|
|
|
int err = errno;
|
|
|
|
|
group_destroy(group);
|
|
|
|
|
errno = err;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &stream->this;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 21:28:40 +03:00
|
|
|
struct spa_bt_iso_io *spa_bt_iso_io_attach(struct spa_bt_iso_io *this, struct spa_bt_transport *t)
|
2023-03-25 16:58:27 +02:00
|
|
|
{
|
|
|
|
|
struct stream *stream = SPA_CONTAINER_OF(this, struct stream, this);
|
|
|
|
|
|
2023-04-09 21:28:40 +03:00
|
|
|
stream = stream_create(t, stream->group);
|
2023-03-25 16:58:27 +02:00
|
|
|
if (stream == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
return &stream->this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void spa_bt_iso_io_destroy(struct spa_bt_iso_io *this)
|
|
|
|
|
{
|
|
|
|
|
struct stream *stream = SPA_CONTAINER_OF(this, struct stream, this);
|
|
|
|
|
|
|
|
|
|
stream_unlink(stream);
|
|
|
|
|
|
2024-02-20 23:01:07 +02:00
|
|
|
spa_bt_latency_flush(&stream->tx_latency, stream->fd, stream->group->log);
|
|
|
|
|
|
2023-03-25 16:58:27 +02:00
|
|
|
if (spa_list_is_empty(&stream->group->streams))
|
|
|
|
|
group_destroy(stream->group);
|
|
|
|
|
|
2023-04-09 21:28:40 +03:00
|
|
|
if (stream->this.codec_data)
|
|
|
|
|
stream->codec->deinit(stream->this.codec_data);
|
|
|
|
|
stream->this.codec_data = NULL;
|
|
|
|
|
|
2023-03-25 16:58:27 +02:00
|
|
|
free(stream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool group_is_enabled(struct group *group)
|
|
|
|
|
{
|
|
|
|
|
struct stream *stream;
|
|
|
|
|
|
2024-03-02 00:37:02 +02:00
|
|
|
spa_list_for_each(stream, &group->streams, link) {
|
|
|
|
|
if (!stream->sink)
|
|
|
|
|
continue;
|
2023-03-25 16:58:27 +02:00
|
|
|
if (stream->pull)
|
|
|
|
|
return true;
|
2024-03-02 00:37:02 +02:00
|
|
|
}
|
2023-03-25 16:58:27 +02:00
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Must be called from data thread */
|
|
|
|
|
void spa_bt_iso_io_set_cb(struct spa_bt_iso_io *this, spa_bt_iso_io_pull_t pull, void *user_data)
|
|
|
|
|
{
|
|
|
|
|
struct stream *stream = SPA_CONTAINER_OF(this, struct stream, this);
|
|
|
|
|
bool was_enabled, enabled;
|
|
|
|
|
|
2024-04-01 17:26:35 +03:00
|
|
|
if (!stream->sink)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-03-25 16:58:27 +02:00
|
|
|
was_enabled = group_is_enabled(stream->group);
|
|
|
|
|
|
|
|
|
|
stream->pull = pull;
|
|
|
|
|
stream->this.user_data = user_data;
|
|
|
|
|
|
|
|
|
|
enabled = group_is_enabled(stream->group);
|
|
|
|
|
|
2023-04-09 21:28:40 +03:00
|
|
|
if (!enabled && was_enabled) {
|
|
|
|
|
stream->group->started = false;
|
2023-03-25 16:58:27 +02:00
|
|
|
set_timeout(stream->group, 0);
|
2023-04-09 21:28:40 +03:00
|
|
|
} else if (enabled && !was_enabled) {
|
2023-03-25 16:58:27 +02:00
|
|
|
set_timers(stream->group);
|
2023-04-09 21:28:40 +03:00
|
|
|
}
|
2023-03-25 16:58:27 +02:00
|
|
|
|
2023-04-06 18:06:29 +03:00
|
|
|
stream->idle = true;
|
2023-04-09 19:53:42 +03:00
|
|
|
stream->this.resync = true;
|
2023-04-06 18:06:29 +03:00
|
|
|
|
2023-03-25 16:58:27 +02:00
|
|
|
if (pull == NULL) {
|
|
|
|
|
stream->this.size = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-20 23:01:07 +02:00
|
|
|
|
|
|
|
|
/** Must be called from data thread */
|
|
|
|
|
int spa_bt_iso_io_recv_errqueue(struct spa_bt_iso_io *this)
|
|
|
|
|
{
|
|
|
|
|
struct stream *stream = SPA_CONTAINER_OF(this, struct stream, this);
|
|
|
|
|
struct group *group = stream->group;
|
|
|
|
|
|
|
|
|
|
return spa_bt_latency_recv_errqueue(&stream->tx_latency, stream->fd, group->log);
|
|
|
|
|
}
|