2019-02-27 16:43:01 +01:00
|
|
|
/* Spa A2DP Sink
|
2018-01-11 10:23:37 +01:00
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* Copyright © 2018 Wim Taymans
|
2018-01-11 10:23:37 +01:00
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
2018-01-11 10:23:37 +01:00
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
2018-01-11 10:23:37 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
2019-10-25 15:01:02 +02:00
|
|
|
#include <spa/support/plugin.h>
|
2018-01-11 10:23:37 +01:00
|
|
|
#include <spa/support/loop.h>
|
|
|
|
|
#include <spa/support/log.h>
|
2019-06-06 15:21:40 +02:00
|
|
|
#include <spa/support/system.h>
|
2018-01-11 10:23:37 +01:00
|
|
|
#include <spa/utils/list.h>
|
2019-06-03 16:48:01 +02:00
|
|
|
#include <spa/utils/keys.h>
|
2019-06-21 13:31:34 +02:00
|
|
|
#include <spa/utils/names.h>
|
2019-10-25 15:01:02 +02:00
|
|
|
#include <spa/utils/result.h>
|
2019-06-03 16:48:01 +02:00
|
|
|
#include <spa/monitor/device.h>
|
2018-01-11 10:23:37 +01:00
|
|
|
|
|
|
|
|
#include <spa/node/node.h>
|
2019-05-20 10:14:00 +02:00
|
|
|
#include <spa/node/utils.h>
|
2018-01-11 10:23:37 +01:00
|
|
|
#include <spa/node/io.h>
|
2019-11-18 10:10:58 +01:00
|
|
|
#include <spa/node/keys.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"
|
|
|
|
|
|
2020-10-19 09:33:49 +02:00
|
|
|
struct codec;
|
|
|
|
|
|
2018-01-11 10:23:37 +01:00
|
|
|
struct props {
|
|
|
|
|
uint32_t min_latency;
|
|
|
|
|
uint32_t max_latency;
|
2021-02-13 22:59:04 +02:00
|
|
|
int64_t latency_offset;
|
2018-01-11 10:23:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define FILL_FRAMES 2
|
|
|
|
|
#define MAX_BUFFERS 32
|
|
|
|
|
|
|
|
|
|
struct buffer {
|
2019-01-07 17:57:03 +01:00
|
|
|
uint32_t id;
|
2020-09-02 17:04:19 +02:00
|
|
|
#define BUFFER_FLAG_OUT (1<<0)
|
|
|
|
|
uint32_t flags;
|
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;
|
|
|
|
|
struct spa_list link;
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-27 16:43:01 +01:00
|
|
|
struct port {
|
|
|
|
|
struct spa_audio_info current_format;
|
2020-12-05 08:30:14 +01:00
|
|
|
uint32_t frame_size;
|
2019-05-15 10:20:28 +02:00
|
|
|
unsigned int have_format:1;
|
2019-02-27 16:43:01 +01:00
|
|
|
|
2019-03-01 12:00:42 +01:00
|
|
|
uint64_t info_all;
|
2019-02-27 16:43:01 +01:00
|
|
|
struct spa_port_info info;
|
|
|
|
|
struct spa_io_buffers *io;
|
|
|
|
|
struct spa_param_info params[8];
|
|
|
|
|
|
|
|
|
|
struct buffer buffers[MAX_BUFFERS];
|
2019-05-15 10:20:28 +02:00
|
|
|
uint32_t n_buffers;
|
2019-02-27 16:43:01 +01:00
|
|
|
|
|
|
|
|
struct spa_list free;
|
|
|
|
|
struct spa_list ready;
|
|
|
|
|
|
|
|
|
|
size_t ready_offset;
|
|
|
|
|
};
|
|
|
|
|
|
2018-01-11 10:23:37 +01:00
|
|
|
struct impl {
|
|
|
|
|
struct spa_handle handle;
|
|
|
|
|
struct spa_node node;
|
|
|
|
|
|
|
|
|
|
struct spa_log *log;
|
|
|
|
|
struct spa_loop *data_loop;
|
2019-06-06 15:21:40 +02:00
|
|
|
struct spa_system *data_system;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-03-01 12:00:42 +01:00
|
|
|
struct spa_hook_list hooks;
|
2019-05-15 12:17:52 +02:00
|
|
|
struct spa_callbacks callbacks;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-03-01 12:00:42 +01:00
|
|
|
uint64_t info_all;
|
2019-02-27 16:43:01 +01:00
|
|
|
struct spa_node_info info;
|
|
|
|
|
struct spa_param_info params[8];
|
2018-01-11 10:23:37 +01:00
|
|
|
struct props props;
|
|
|
|
|
|
|
|
|
|
struct spa_bt_transport *transport;
|
2019-05-14 18:10:18 +02:00
|
|
|
struct spa_hook transport_listener;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-02-27 16:43:01 +01:00
|
|
|
struct port port;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-04-24 12:41:16 +02:00
|
|
|
unsigned int started:1;
|
2020-02-21 10:47:32 +01:00
|
|
|
unsigned int following:1;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
|
|
|
|
struct spa_source source;
|
|
|
|
|
int timerfd;
|
|
|
|
|
struct spa_source flush_source;
|
|
|
|
|
|
2019-04-24 12:41:16 +02:00
|
|
|
struct spa_io_clock *clock;
|
|
|
|
|
struct spa_io_position *position;
|
|
|
|
|
|
2020-10-16 13:13:04 +02:00
|
|
|
uint64_t current_time;
|
|
|
|
|
uint64_t next_time;
|
|
|
|
|
uint64_t last_error;
|
|
|
|
|
|
2020-10-19 13:27:11 +02:00
|
|
|
const struct a2dp_codec *codec;
|
2020-10-19 09:33:49 +02:00
|
|
|
void *codec_data;
|
2020-10-19 10:38:22 +02:00
|
|
|
struct spa_audio_info codec_format;
|
2020-10-19 09:33:49 +02:00
|
|
|
|
2020-12-05 08:30:14 +01:00
|
|
|
uint32_t block_size;
|
|
|
|
|
uint32_t num_blocks;
|
2018-01-11 10:23:37 +01:00
|
|
|
uint8_t buffer[4096];
|
2020-12-05 08:30:14 +01:00
|
|
|
uint32_t buffer_used;
|
|
|
|
|
uint32_t frame_count;
|
2018-01-11 10:23:37 +01:00
|
|
|
uint16_t seqnum;
|
|
|
|
|
uint32_t timestamp;
|
2020-10-16 13:13:04 +02:00
|
|
|
uint64_t sample_count;
|
2020-12-11 19:23:30 +01:00
|
|
|
uint8_t tmp_buffer[4096];
|
2020-12-05 08:30:14 +01:00
|
|
|
uint32_t tmp_buffer_used;
|
2020-12-19 08:21:40 +08:00
|
|
|
uint32_t fd_buffer_size;
|
2020-10-19 09:33:49 +02:00
|
|
|
};
|
|
|
|
|
|
2018-01-11 10:23:37 +01:00
|
|
|
#define NAME "a2dp-sink"
|
|
|
|
|
|
|
|
|
|
#define CHECK_PORT(this,d,p) ((d) == SPA_DIRECTION_INPUT && (p) == 0)
|
|
|
|
|
|
2019-10-24 12:51:34 +02:00
|
|
|
static const uint32_t default_min_latency = MIN_LATENCY;
|
|
|
|
|
static const uint32_t default_max_latency = MAX_LATENCY;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
|
|
|
|
static void reset_props(struct props *props)
|
|
|
|
|
{
|
|
|
|
|
props->min_latency = default_min_latency;
|
|
|
|
|
props->max_latency = default_max_latency;
|
2021-02-13 22:59:04 +02:00
|
|
|
props->latency_offset = 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
static int impl_node_enum_params(void *object, int seq,
|
2019-02-20 17:51:05 +01:00
|
|
|
uint32_t id, uint32_t start, uint32_t num,
|
2019-02-25 12:29:57 +01:00
|
|
|
const struct spa_pod *filter)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2019-05-20 16:11:23 +02:00
|
|
|
struct impl *this = object;
|
2018-01-11 10:23:37 +01:00
|
|
|
struct spa_pod *param;
|
|
|
|
|
struct spa_pod_builder b = { 0 };
|
|
|
|
|
uint8_t buffer[1024];
|
2019-02-25 12:29:57 +01:00
|
|
|
struct spa_result_node_params result;
|
2019-02-20 17:51:05 +01:00
|
|
|
uint32_t count = 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
spa_return_val_if_fail(this != NULL, -EINVAL);
|
2019-02-20 17:51:05 +01:00
|
|
|
spa_return_val_if_fail(num != 0, -EINVAL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-02-25 12:29:57 +01:00
|
|
|
result.id = id;
|
2019-02-20 17:51:05 +01:00
|
|
|
result.next = start;
|
2018-01-11 10:23:37 +01:00
|
|
|
next:
|
2019-02-25 12:29:57 +01:00
|
|
|
result.index = result.next++;
|
|
|
|
|
|
2018-01-11 10:23:37 +01:00
|
|
|
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_PropInfo:
|
2018-08-23 17:47:57 +02:00
|
|
|
{
|
2018-01-11 10:23:37 +01:00
|
|
|
struct props *p = &this->props;
|
|
|
|
|
|
2019-02-25 12:29:57 +01:00
|
|
|
switch (result.index) {
|
2018-01-11 10:23:37 +01:00
|
|
|
case 0:
|
2019-01-16 11:05:12 +01:00
|
|
|
param = spa_pod_builder_add_object(&b,
|
2018-08-27 15:03:11 +02:00
|
|
|
SPA_TYPE_OBJECT_PropInfo, id,
|
2019-01-16 11:05:12 +01:00
|
|
|
SPA_PROP_INFO_id, SPA_POD_Id(SPA_PROP_minLatency),
|
|
|
|
|
SPA_PROP_INFO_name, SPA_POD_String("The minimum latency"),
|
|
|
|
|
SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Int(p->min_latency, 1, INT32_MAX));
|
2018-01-11 10:23:37 +01:00
|
|
|
break;
|
|
|
|
|
case 1:
|
2019-01-16 11:05:12 +01:00
|
|
|
param = spa_pod_builder_add_object(&b,
|
2018-08-27 15:03:11 +02:00
|
|
|
SPA_TYPE_OBJECT_PropInfo, id,
|
2019-01-16 11:05:12 +01:00
|
|
|
SPA_PROP_INFO_id, SPA_POD_Id(SPA_PROP_maxLatency),
|
|
|
|
|
SPA_PROP_INFO_name, SPA_POD_String("The maximum latency"),
|
|
|
|
|
SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Int(p->max_latency, 1, INT32_MAX));
|
2018-01-11 10:23:37 +01:00
|
|
|
break;
|
2021-02-13 22:59:04 +02:00
|
|
|
case 2:
|
|
|
|
|
param = spa_pod_builder_add_object(&b,
|
|
|
|
|
SPA_TYPE_OBJECT_PropInfo, id,
|
|
|
|
|
SPA_PROP_INFO_id, SPA_POD_Id(SPA_PROP_latencyOffsetNsec),
|
|
|
|
|
SPA_PROP_INFO_name, SPA_POD_String("Latency offset (ns)"),
|
|
|
|
|
SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Long(0, INT64_MIN, INT64_MAX));
|
|
|
|
|
break;
|
2018-01-11 10:23:37 +01:00
|
|
|
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;
|
|
|
|
|
|
2019-02-25 12:29:57 +01:00
|
|
|
switch (result.index) {
|
2018-01-11 10:23:37 +01:00
|
|
|
case 0:
|
2019-01-16 11:05:12 +01:00
|
|
|
param = spa_pod_builder_add_object(&b,
|
2018-08-27 15:03:11 +02:00
|
|
|
SPA_TYPE_OBJECT_Props, id,
|
2019-01-16 11:05:12 +01:00
|
|
|
SPA_PROP_minLatency, SPA_POD_Int(p->min_latency),
|
2021-02-13 22:59:04 +02:00
|
|
|
SPA_PROP_maxLatency, SPA_POD_Int(p->max_latency),
|
|
|
|
|
SPA_PROP_latencyOffsetNsec, SPA_POD_Long(p->latency_offset));
|
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
|
|
|
|
2019-02-20 17:51:05 +01:00
|
|
|
if (spa_pod_filter(&b, &result.param, param, filter) < 0)
|
|
|
|
|
goto next;
|
|
|
|
|
|
2019-05-28 13:59:48 +02:00
|
|
|
spa_node_emit_result(&this->hooks, seq, 0, SPA_RESULT_TYPE_NODE_PARAMS, &result);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-02-20 17:51:05 +01:00
|
|
|
if (++count != num)
|
2018-01-11 10:23:37 +01:00
|
|
|
goto next;
|
|
|
|
|
|
2019-02-20 17:51:05 +01:00
|
|
|
return 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-16 13:13:04 +02:00
|
|
|
static int set_timeout(struct impl *this, uint64_t time)
|
2019-04-24 12:41:16 +02:00
|
|
|
{
|
|
|
|
|
struct itimerspec ts;
|
2020-10-16 13:13:04 +02:00
|
|
|
ts.it_value.tv_sec = time / SPA_NSEC_PER_SEC;
|
|
|
|
|
ts.it_value.tv_nsec = time % SPA_NSEC_PER_SEC;
|
2019-04-24 12:41:16 +02:00
|
|
|
ts.it_interval.tv_sec = 0;
|
|
|
|
|
ts.it_interval.tv_nsec = 0;
|
2020-10-16 13:13:04 +02:00
|
|
|
return spa_system_timerfd_settime(this->data_system,
|
|
|
|
|
this->timerfd, SPA_FD_TIMER_ABSTIME, &ts, NULL);
|
|
|
|
|
}
|
2019-04-24 12:41:16 +02:00
|
|
|
|
2020-10-16 13:13:04 +02:00
|
|
|
static int set_timers(struct impl *this)
|
|
|
|
|
{
|
|
|
|
|
struct timespec now;
|
|
|
|
|
|
|
|
|
|
spa_system_clock_gettime(this->data_system, CLOCK_MONOTONIC, &now);
|
|
|
|
|
this->next_time = SPA_TIMESPEC_TO_NSEC(&now);
|
|
|
|
|
|
|
|
|
|
return set_timeout(this, this->following ? 0 : this->next_time);
|
2019-04-24 12:41:16 +02:00
|
|
|
}
|
|
|
|
|
|
2020-02-21 10:47:32 +01:00
|
|
|
static int do_reassign_follower(struct spa_loop *loop,
|
2019-04-24 12:41:16 +02:00
|
|
|
bool async,
|
|
|
|
|
uint32_t seq,
|
|
|
|
|
const void *data,
|
|
|
|
|
size_t size,
|
|
|
|
|
void *user_data)
|
|
|
|
|
{
|
|
|
|
|
struct impl *this = user_data;
|
|
|
|
|
set_timers(this);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-21 10:47:32 +01:00
|
|
|
static inline bool is_following(struct impl *this)
|
2019-05-15 10:20:28 +02:00
|
|
|
{
|
|
|
|
|
return this->position && this->clock && this->position->clock.id != this->clock->id;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
static int impl_node_set_io(void *object, uint32_t id, void *data, size_t size)
|
2018-10-23 12:31:41 +02:00
|
|
|
{
|
2019-05-20 16:11:23 +02:00
|
|
|
struct impl *this = object;
|
2020-02-21 10:47:32 +01:00
|
|
|
bool following;
|
2019-04-24 12:41:16 +02:00
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
spa_return_val_if_fail(this != NULL, -EINVAL);
|
2019-04-24 12:41:16 +02:00
|
|
|
|
|
|
|
|
switch (id) {
|
|
|
|
|
case SPA_IO_Clock:
|
|
|
|
|
this->clock = data;
|
|
|
|
|
break;
|
|
|
|
|
case SPA_IO_Position:
|
|
|
|
|
this->position = data;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return -ENOENT;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-21 10:47:32 +01:00
|
|
|
following = is_following(this);
|
|
|
|
|
if (this->started && following != this->following) {
|
|
|
|
|
spa_log_debug(this->log, NAME " %p: reassign follower %d->%d", this, this->following, following);
|
|
|
|
|
this->following = following;
|
|
|
|
|
spa_loop_invoke(this->data_loop, do_reassign_follower, 0, NULL, 0, true, this);
|
2019-04-24 12:41:16 +02:00
|
|
|
}
|
2018-10-30 16:29:39 +00:00
|
|
|
return 0;
|
2018-10-23 12:31:41 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-13 22:59:04 +02:00
|
|
|
static void emit_node_props_changed(struct impl *this);
|
|
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
static int impl_node_set_param(void *object, uint32_t id, uint32_t flags,
|
2018-01-11 10:23:37 +01:00
|
|
|
const struct spa_pod *param)
|
|
|
|
|
{
|
2019-05-20 16:11:23 +02:00
|
|
|
struct impl *this = object;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
spa_return_val_if_fail(this != NULL, -EINVAL);
|
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_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);
|
2021-02-13 22:59:04 +02:00
|
|
|
emit_node_props_changed(this);
|
2018-01-11 10:23:37 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
2021-02-13 22:59:04 +02:00
|
|
|
if (spa_pod_parse_object(param,
|
|
|
|
|
SPA_TYPE_OBJECT_Props, NULL,
|
|
|
|
|
SPA_PROP_minLatency, SPA_POD_OPT_Int(&p->min_latency),
|
|
|
|
|
SPA_PROP_maxLatency, SPA_POD_OPT_Int(&p->max_latency),
|
|
|
|
|
SPA_PROP_latencyOffsetNsec, SPA_POD_OPT_Long(&p->latency_offset)) > 0)
|
|
|
|
|
emit_node_props_changed(this);
|
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
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-19 08:21:40 +08:00
|
|
|
static void update_num_blocks(struct impl *this)
|
|
|
|
|
{
|
|
|
|
|
this->num_blocks = this->codec->get_num_blocks(this->codec_data);
|
|
|
|
|
}
|
|
|
|
|
|
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->frame_count = 0;
|
2020-10-19 09:33:49 +02:00
|
|
|
this->buffer_used = this->codec->start_encode(this->codec_data,
|
|
|
|
|
this->buffer, sizeof(this->buffer),
|
|
|
|
|
this->seqnum++, this->timestamp);
|
|
|
|
|
this->timestamp = this->sample_count;
|
2018-01-18 11:57:23 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2020-12-19 08:21:40 +08:00
|
|
|
static int get_transport_unused_size(struct impl *this)
|
|
|
|
|
{
|
|
|
|
|
int res, value;
|
|
|
|
|
res = ioctl(this->flush_source.fd, TIOCOUTQ, &value);
|
|
|
|
|
if (res < 0) {
|
|
|
|
|
spa_log_debug(this->log, NAME " %p: ioctl fail: %m", this);
|
|
|
|
|
return -errno;
|
|
|
|
|
}
|
|
|
|
|
spa_log_debug(this->log, NAME " %p: fd unused buffer size:%d/%d", this, value, this->fd_buffer_size);
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int send_buffer(struct impl *this)
|
|
|
|
|
{
|
2020-12-19 08:21:40 +08:00
|
|
|
int written, unsent;
|
|
|
|
|
unsent = get_transport_unused_size(this);
|
|
|
|
|
if (unsent >= 0) {
|
|
|
|
|
unsent = this->fd_buffer_size - unsent;
|
|
|
|
|
this->codec->abr_process(this->codec_data, unsent);
|
|
|
|
|
update_num_blocks(this);
|
|
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2021-02-08 10:24:50 +01:00
|
|
|
spa_log_trace(this->log, NAME " %p: send %d %u %u %u %u",
|
|
|
|
|
this, this->frame_count, this->block_size, this->seqnum,
|
|
|
|
|
this->timestamp, this->buffer_used);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2021-02-08 10:24:50 +01:00
|
|
|
written = send(this->flush_source.fd, this->buffer,
|
|
|
|
|
this->buffer_used, MSG_DONTWAIT | MSG_NOSIGNAL);
|
2020-10-19 09:33:49 +02:00
|
|
|
reset_buffer(this);
|
|
|
|
|
|
2021-02-08 10:24:50 +01:00
|
|
|
spa_log_trace(this->log, NAME " %p: send %d", this, written);
|
|
|
|
|
|
2020-12-17 02:29:04 +08:00
|
|
|
if (written < 0) {
|
|
|
|
|
spa_log_debug(this->log, NAME " %p: %m", this);
|
2018-01-18 11:57:23 +01:00
|
|
|
return -errno;
|
2020-12-17 02:29:04 +08:00
|
|
|
}
|
2018-01-18 11:57:23 +01:00
|
|
|
|
|
|
|
|
return written;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-08 10:24:50 +01:00
|
|
|
static bool want_flush(struct impl *this)
|
|
|
|
|
{
|
|
|
|
|
return (this->frame_count * this->block_size / this->port.frame_size >= MIN_LATENCY);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-06 15:55:38 +01:00
|
|
|
static bool need_flush(struct impl *this)
|
|
|
|
|
{
|
2020-12-17 02:29:04 +08:00
|
|
|
return (this->frame_count >= this->num_blocks);
|
2020-12-06 15:55:38 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-05 08:30:14 +01:00
|
|
|
static int encode_buffer(struct impl *this, const void *data, uint32_t size)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
int processed;
|
2020-11-02 09:16:59 +01:00
|
|
|
size_t out_encoded;
|
2019-02-27 16:43:01 +01:00
|
|
|
struct port *port = &this->port;
|
2020-06-18 20:05:25 +02:00
|
|
|
const void *from_data = data;
|
|
|
|
|
int from_size = size;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2020-12-04 11:59:40 +01:00
|
|
|
spa_log_trace(this->log, NAME " %p: encode %d used %d, %d %d %d",
|
2020-10-19 09:33:49 +02:00
|
|
|
this, size, this->buffer_used, port->frame_size, this->block_size,
|
2020-12-04 11:59:40 +01:00
|
|
|
this->frame_count);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2020-12-06 15:55:38 +01:00
|
|
|
if (need_flush(this))
|
|
|
|
|
return 0;
|
|
|
|
|
|
2020-12-05 08:30:14 +01:00
|
|
|
if (this->buffer_used >= sizeof(this->buffer))
|
2018-01-18 11:57:23 +01:00
|
|
|
return -ENOSPC;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2020-10-19 09:33:49 +02:00
|
|
|
if (size < this->block_size - this->tmp_buffer_used) {
|
2020-06-18 20:05:25 +02:00
|
|
|
memcpy(this->tmp_buffer + this->tmp_buffer_used, data, size);
|
|
|
|
|
this->tmp_buffer_used += size;
|
|
|
|
|
return size;
|
|
|
|
|
} else if (this->tmp_buffer_used > 0) {
|
2020-10-19 09:33:49 +02:00
|
|
|
memcpy(this->tmp_buffer + this->tmp_buffer_used, data, this->block_size - this->tmp_buffer_used);
|
2020-06-18 20:05:25 +02:00
|
|
|
from_data = this->tmp_buffer;
|
2020-10-19 09:33:49 +02:00
|
|
|
from_size = this->block_size;
|
|
|
|
|
this->tmp_buffer_used = this->block_size - this->tmp_buffer_used;
|
2020-06-18 20:05:25 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-19 09:33:49 +02:00
|
|
|
processed = this->codec->encode(this->codec_data,
|
|
|
|
|
from_data, from_size,
|
|
|
|
|
this->buffer + this->buffer_used,
|
|
|
|
|
sizeof(this->buffer) - this->buffer_used,
|
|
|
|
|
&out_encoded);
|
2018-01-18 11:57:23 +01:00
|
|
|
if (processed < 0)
|
|
|
|
|
return processed;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-02-27 16:43:01 +01:00
|
|
|
this->sample_count += processed / port->frame_size;
|
2020-10-19 09:33:49 +02:00
|
|
|
this->frame_count += processed / this->block_size;
|
2018-01-18 11:57:23 +01:00
|
|
|
this->buffer_used += out_encoded;
|
|
|
|
|
|
2019-06-07 17:16:02 +02:00
|
|
|
spa_log_trace(this->log, NAME " %p: processed %d %zd used %d",
|
2018-01-18 11:57:23 +01:00
|
|
|
this, processed, out_encoded, this->buffer_used);
|
|
|
|
|
|
2020-06-18 20:05:25 +02:00
|
|
|
if (this->tmp_buffer_used) {
|
|
|
|
|
processed = this->tmp_buffer_used;
|
|
|
|
|
this->tmp_buffer_used = 0;
|
|
|
|
|
}
|
2018-01-18 11:57:23 +01:00
|
|
|
return processed;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-08 10:24:50 +01:00
|
|
|
static int flush_buffer(struct impl *this)
|
2018-01-18 11:57:23 +01:00
|
|
|
{
|
2020-12-17 02:29:04 +08:00
|
|
|
spa_log_trace(this->log, NAME" %p: used:%d num_blocks:%d block_size:%d", this,
|
2020-10-19 09:33:49 +02:00
|
|
|
this->buffer_used, this->num_blocks, this->block_size);
|
2018-01-18 11:57:23 +01:00
|
|
|
|
2021-02-08 10:24:50 +01:00
|
|
|
if (want_flush(this) || need_flush(this))
|
2018-01-18 11:57:23 +01:00
|
|
|
return send_buffer(this);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-05 08:30:14 +01:00
|
|
|
static int add_data(struct impl *this, const void *data, uint32_t 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);
|
|
|
|
|
|
2020-01-10 09:55:02 +01:00
|
|
|
if (processed <= 0)
|
|
|
|
|
return total > 0 ? total : processed;
|
2018-01-18 11:57:23 +01:00
|
|
|
|
2019-01-08 11:53:36 +01:00
|
|
|
data = SPA_MEMBER(data, processed, void);
|
2018-01-18 11:57:23 +01:00
|
|
|
size -= processed;
|
|
|
|
|
total += processed;
|
|
|
|
|
}
|
|
|
|
|
return total;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-16 13:13:04 +02:00
|
|
|
static void enable_flush(struct impl *this, bool enabled)
|
|
|
|
|
{
|
|
|
|
|
if (SPA_FLAG_IS_SET(this->flush_source.mask, SPA_IO_OUT) != enabled) {
|
|
|
|
|
SPA_FLAG_UPDATE(this->flush_source.mask, SPA_IO_OUT, enabled);
|
|
|
|
|
spa_loop_update_source(this->data_loop, &this->flush_source);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int flush_data(struct impl *this, uint64_t now_time)
|
|
|
|
|
{
|
2019-01-07 15:52:42 +01:00
|
|
|
int written;
|
2020-12-17 02:29:04 +08:00
|
|
|
uint32_t total_frames, iter_buffer_used;
|
2019-02-27 16:43:01 +01:00
|
|
|
struct port *port = &this->port;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
total_frames = 0;
|
2020-09-02 17:51:33 +02:00
|
|
|
again:
|
2020-12-17 02:29:04 +08:00
|
|
|
iter_buffer_used = this->buffer_used;
|
2019-02-27 16:43:01 +01:00
|
|
|
while (!spa_list_is_empty(&port->ready)) {
|
2018-01-18 11:57:23 +01:00
|
|
|
uint8_t *src;
|
2019-01-07 15:52:42 +01:00
|
|
|
uint32_t n_bytes, n_frames;
|
2018-01-18 11:57:23 +01:00
|
|
|
struct buffer *b;
|
|
|
|
|
struct spa_data *d;
|
|
|
|
|
uint32_t index, offs, avail, l0, l1;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-02-27 16:43:01 +01:00
|
|
|
b = spa_list_first(&port->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
|
|
|
|
2019-02-27 16:43:01 +01:00
|
|
|
index = d[0].chunk->offset + port->ready_offset;
|
|
|
|
|
avail = d[0].chunk->size - port->ready_offset;
|
|
|
|
|
avail /= port->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;
|
2019-02-27 16:43:01 +01:00
|
|
|
n_bytes = n_frames * port->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
|
|
|
|
2020-01-10 09:55:02 +01:00
|
|
|
written = add_data(this, src + offs, l0);
|
|
|
|
|
if (written > 0 && l1 > 0)
|
|
|
|
|
written += add_data(this, src, l1);
|
|
|
|
|
if (written <= 0) {
|
|
|
|
|
if (written < 0 && written != -ENOSPC) {
|
|
|
|
|
spa_list_remove(&b->link);
|
2020-09-02 17:04:19 +02:00
|
|
|
SPA_FLAG_SET(b->flags, BUFFER_FLAG_OUT);
|
|
|
|
|
this->port.io->buffer_id = b->id;
|
2020-10-16 13:13:04 +02:00
|
|
|
spa_log_warn(this->log, NAME " %p: error %s, reuse buffer %u",
|
2020-01-10 09:55:02 +01:00
|
|
|
this, spa_strerror(written), b->id);
|
|
|
|
|
spa_node_call_reuse_buffer(&this->callbacks, 0, b->id);
|
|
|
|
|
port->ready_offset = 0;
|
|
|
|
|
}
|
2018-01-18 11:57:23 +01:00
|
|
|
break;
|
2019-07-12 09:55:55 +02:00
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2020-01-10 09:55:02 +01:00
|
|
|
n_frames = written / port->frame_size;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2020-01-10 09:55:02 +01:00
|
|
|
port->ready_offset += written;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-02-27 16:43:01 +01:00
|
|
|
if (port->ready_offset >= d[0].chunk->size) {
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_list_remove(&b->link);
|
2020-09-02 17:04:19 +02:00
|
|
|
SPA_FLAG_SET(b->flags, BUFFER_FLAG_OUT);
|
2019-05-31 15:06:44 +02:00
|
|
|
spa_log_trace(this->log, NAME " %p: reuse buffer %u", this, b->id);
|
2020-09-02 17:04:19 +02:00
|
|
|
this->port.io->buffer_id = b->id;
|
2019-05-15 11:19:23 +02:00
|
|
|
|
|
|
|
|
spa_node_call_reuse_buffer(&this->callbacks, 0, b->id);
|
2019-02-27 16:43:01 +01:00
|
|
|
port->ready_offset = 0;
|
2018-01-18 11:57:23 +01:00
|
|
|
}
|
|
|
|
|
total_frames += n_frames;
|
|
|
|
|
|
2019-05-31 15:06:44 +02:00
|
|
|
spa_log_trace(this->log, NAME " %p: written %u frames", this, total_frames);
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-17 02:29:04 +08:00
|
|
|
iter_buffer_used = this->buffer_used - iter_buffer_used;
|
|
|
|
|
if (written > 0 && iter_buffer_used == 0) {
|
|
|
|
|
enable_flush(this, false);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-08 10:24:50 +01:00
|
|
|
written = flush_buffer(this);
|
2018-01-18 11:57:23 +01:00
|
|
|
if (written == -EAGAIN) {
|
2020-10-16 13:13:04 +02:00
|
|
|
spa_log_trace(this->log, NAME" %p: delay flush", this);
|
|
|
|
|
if (now_time - this->last_error > SPA_NSEC_PER_SEC / 2) {
|
2020-10-19 09:33:49 +02:00
|
|
|
this->codec->reduce_bitpool(this->codec_data);
|
2020-12-19 08:21:40 +08:00
|
|
|
update_num_blocks(this);
|
2020-10-16 13:13:04 +02:00
|
|
|
this->last_error = now_time;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
2020-10-16 13:13:04 +02:00
|
|
|
enable_flush(this, true);
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
2018-01-18 11:57:23 +01:00
|
|
|
else if (written < 0) {
|
2019-05-31 15:06:44 +02:00
|
|
|
spa_log_trace(this->log, NAME" %p: error flushing %s", this,
|
|
|
|
|
spa_strerror(written));
|
2018-01-18 11:57:23 +01:00
|
|
|
return written;
|
|
|
|
|
}
|
|
|
|
|
else if (written > 0) {
|
2020-10-16 13:13:04 +02:00
|
|
|
if (now_time - this->last_error > SPA_NSEC_PER_SEC) {
|
2020-10-19 09:33:49 +02:00
|
|
|
this->codec->increase_bitpool(this->codec_data);
|
2020-12-19 08:21:40 +08:00
|
|
|
update_num_blocks(this);
|
2018-01-18 11:57:23 +01:00
|
|
|
this->last_error = now_time;
|
|
|
|
|
}
|
2020-09-02 17:51:33 +02:00
|
|
|
if (!spa_list_is_empty(&port->ready))
|
|
|
|
|
goto again;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2020-10-16 13:13:04 +02:00
|
|
|
enable_flush(this, false);
|
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;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-05-31 15:06:44 +02:00
|
|
|
spa_log_trace(this->log, NAME" %p: flushing", this);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2020-10-16 13:13:04 +02:00
|
|
|
if (!SPA_FLAG_IS_SET(source->rmask, SPA_IO_OUT)) {
|
2019-05-31 15:06:44 +02:00
|
|
|
spa_log_warn(this->log, NAME" %p: error %d", this, 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
|
|
|
return;
|
|
|
|
|
}
|
2020-10-16 13:13:04 +02:00
|
|
|
flush_data(this, this->current_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;
|
2019-02-27 16:43:01 +01:00
|
|
|
struct port *port = &this->port;
|
2020-10-16 13:13:04 +02:00
|
|
|
uint64_t exp, duration;
|
|
|
|
|
uint32_t rate;
|
2019-02-27 16:43:01 +01:00
|
|
|
struct spa_io_buffers *io = port->io;
|
2020-10-16 13:13:04 +02:00
|
|
|
uint64_t prev_time, now_time;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2021-01-02 02:02:58 +02:00
|
|
|
if (this->transport == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-06-06 15:21:40 +02:00
|
|
|
if (this->started && spa_system_timerfd_read(this->data_system, this->timerfd, &exp) < 0)
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_log_warn(this->log, "error reading timerfd: %s", strerror(errno));
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2020-10-16 13:13:04 +02:00
|
|
|
prev_time = this->current_time;
|
|
|
|
|
now_time = this->current_time = this->next_time;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2020-10-16 13:13:04 +02:00
|
|
|
if (SPA_LIKELY(this->position)) {
|
|
|
|
|
duration = this->position->clock.duration;
|
|
|
|
|
rate = this->position->clock.rate.denom;
|
|
|
|
|
} else {
|
|
|
|
|
duration = 1024;
|
|
|
|
|
rate = 48000;
|
|
|
|
|
}
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2020-10-16 13:13:04 +02:00
|
|
|
this->next_time = now_time + duration * SPA_NSEC_PER_SEC / rate;
|
|
|
|
|
|
|
|
|
|
if (SPA_LIKELY(this->clock)) {
|
2021-02-13 22:59:04 +02:00
|
|
|
int64_t delay_nsec;
|
|
|
|
|
|
2020-10-16 13:13:04 +02:00
|
|
|
this->clock->nsec = now_time;
|
|
|
|
|
this->clock->position += duration;
|
2020-10-19 15:35:45 +02:00
|
|
|
this->clock->duration = duration;
|
2020-10-16 13:13:04 +02:00
|
|
|
this->clock->rate_diff = 1.0f;
|
|
|
|
|
this->clock->next_nsec = this->next_time;
|
2020-12-23 19:13:54 +01:00
|
|
|
|
2021-02-13 22:59:04 +02:00
|
|
|
delay_nsec = spa_bt_transport_get_delay_nsec(this->transport);
|
|
|
|
|
|
|
|
|
|
/* Negative delay doesn't work properly, so disallow it */
|
|
|
|
|
delay_nsec += SPA_CLAMP(this->props.latency_offset, -delay_nsec, INT64_MAX / 2);
|
|
|
|
|
|
|
|
|
|
this->clock->delay = (delay_nsec * this->clock->rate.denom) / SPA_NSEC_PER_SEC;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-16 13:13:04 +02:00
|
|
|
spa_log_debug(this->log, NAME" %p: timeout %"PRIu64" %"PRIu64"", this,
|
|
|
|
|
now_time, now_time - prev_time);
|
2018-10-05 11:10:36 +02:00
|
|
|
|
2020-10-16 13:13:04 +02:00
|
|
|
spa_log_trace(this->log, NAME " %p: %d", this, io->status);
|
|
|
|
|
io->status = SPA_STATUS_NEED_DATA;
|
|
|
|
|
spa_node_call_ready(&this->callbacks, SPA_STATUS_NEED_DATA);
|
2019-05-16 16:42:49 +02:00
|
|
|
|
2020-10-16 13:13:04 +02:00
|
|
|
set_timeout(this, this->next_time);
|
2018-01-18 11:57:23 +01:00
|
|
|
}
|
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
|
|
|
{
|
2020-12-06 09:32:12 +01:00
|
|
|
int i, res, val, size;
|
2020-12-03 18:05:57 +01:00
|
|
|
struct port *port;
|
2018-01-18 11:57:23 +01:00
|
|
|
socklen_t len;
|
2020-12-06 09:32:12 +01:00
|
|
|
uint8_t *conf;
|
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
|
|
|
|
2020-03-23 13:49:30 +01:00
|
|
|
spa_return_val_if_fail(this->transport, -EIO);
|
|
|
|
|
|
2020-02-21 10:47:32 +01:00
|
|
|
this->following = is_following(this);
|
2019-04-24 12:41:16 +02:00
|
|
|
|
2020-02-21 10:47:32 +01:00
|
|
|
spa_log_debug(this->log, NAME " %p: start following:%d", this, this->following);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-05-14 18:10:18 +02:00
|
|
|
if ((res = spa_bt_transport_acquire(this->transport, false)) < 0)
|
2018-01-18 11:57:23 +01:00
|
|
|
return res;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2020-12-03 18:05:57 +01:00
|
|
|
port = &this->port;
|
|
|
|
|
|
2020-12-06 09:32:12 +01:00
|
|
|
conf = this->transport->configuration;
|
|
|
|
|
size = this->transport->configuration_len;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
|
spa_log_debug(this->log, " %d: %02x", i, conf[i]);
|
|
|
|
|
|
2020-12-04 11:34:38 +01:00
|
|
|
this->codec_data = this->codec->init(this->codec, 0,
|
2020-12-03 18:05:57 +01:00
|
|
|
this->transport->configuration,
|
|
|
|
|
this->transport->configuration_len,
|
2021-01-07 17:39:39 +01:00
|
|
|
&port->current_format, NULL,
|
2020-12-03 18:05:57 +01:00
|
|
|
this->transport->write_mtu);
|
|
|
|
|
if (this->codec_data == NULL)
|
|
|
|
|
return -EIO;
|
|
|
|
|
|
2021-02-13 22:59:04 +02:00
|
|
|
spa_log_info(this->log, NAME " %p: using A2DP codec %s, delay:%"PRIi64" ms", this, this->codec->description,
|
|
|
|
|
(int64_t)(spa_bt_transport_get_delay_nsec(this->transport) / SPA_NSEC_PER_MSEC));
|
2020-12-04 11:34:38 +01:00
|
|
|
|
2020-10-19 09:33:49 +02:00
|
|
|
this->seqnum = 0;
|
|
|
|
|
|
|
|
|
|
this->block_size = this->codec->get_block_size(this->codec_data);
|
2021-02-08 10:24:27 +01:00
|
|
|
update_num_blocks(this);
|
2020-12-11 19:23:30 +01:00
|
|
|
if (this->block_size > sizeof(this->tmp_buffer)) {
|
|
|
|
|
spa_log_error(this->log, "block-size %d > %zu",
|
|
|
|
|
this->block_size, sizeof(this->tmp_buffer));
|
|
|
|
|
return -EIO;
|
|
|
|
|
}
|
2020-10-19 09:33:49 +02:00
|
|
|
|
|
|
|
|
spa_log_debug(this->log, NAME " %p: block_size %d num_blocks:%d", this,
|
|
|
|
|
this->block_size, this->num_blocks);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2021-02-08 10:24:27 +01:00
|
|
|
val = this->codec->send_buf_size > 0
|
2021-01-31 09:01:40 +08:00
|
|
|
/* The kernel doubles the SO_SNDBUF option value set by setsockopt(). */
|
|
|
|
|
? this->codec->send_buf_size / 2 + this->codec->send_buf_size % 2
|
|
|
|
|
: FILL_FRAMES * this->transport->write_mtu;
|
2018-01-18 11:57:23 +01:00
|
|
|
if (setsockopt(this->transport->fd, SOL_SOCKET, SO_SNDBUF, &val, sizeof(val)) < 0)
|
2019-05-31 15:06:44 +02:00
|
|
|
spa_log_warn(this->log, NAME " %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) {
|
2019-05-31 15:06:44 +02:00
|
|
|
spa_log_warn(this->log, NAME " %p: SO_SNDBUF %m", this);
|
2018-01-18 11:57:23 +01:00
|
|
|
}
|
|
|
|
|
else {
|
2019-05-31 15:06:44 +02:00
|
|
|
spa_log_debug(this->log, NAME " %p: SO_SNDBUF: %d", this, val);
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
2020-12-19 08:21:40 +08:00
|
|
|
this->fd_buffer_size = val;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2021-01-31 09:01:40 +08:00
|
|
|
val = FILL_FRAMES * this->transport->read_mtu;
|
2018-01-18 11:57:23 +01:00
|
|
|
if (setsockopt(this->transport->fd, SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)) < 0)
|
2019-05-31 15:06:44 +02:00
|
|
|
spa_log_warn(this->log, NAME " %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
|
|
|
|
2019-04-24 12:41:16 +02:00
|
|
|
set_timers(this);
|
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;
|
2019-06-06 15:21:40 +02:00
|
|
|
spa_system_timerfd_settime(this->data_system, 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
|
|
|
{
|
2019-07-31 18:30:19 +02:00
|
|
|
int res = 0;
|
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
|
|
|
|
2019-05-31 15:06:44 +02:00
|
|
|
spa_log_trace(this->log, NAME " %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
|
|
|
|
2019-07-31 18:30:19 +02:00
|
|
|
if (this->transport)
|
|
|
|
|
res = spa_bt_transport_release(this->transport);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2020-12-03 18:11:06 +01:00
|
|
|
if (this->codec_data)
|
|
|
|
|
this->codec->deinit(this->codec_data);
|
|
|
|
|
this->codec_data = NULL;
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
return res;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
static int impl_node_send_command(void *object, const struct spa_command *command)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2019-05-20 16:11:23 +02:00
|
|
|
struct impl *this = object;
|
2019-02-27 16:43:01 +01:00
|
|
|
struct port *port;
|
2018-01-18 11:57:23 +01:00
|
|
|
int res;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
spa_return_val_if_fail(this != NULL, -EINVAL);
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_return_val_if_fail(command != NULL, -EINVAL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-02-27 16:43:01 +01:00
|
|
|
port = &this->port;
|
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:
|
2019-02-27 16:43:01 +01:00
|
|
|
if (!port->have_format)
|
2018-01-18 11:57:23 +01:00
|
|
|
return -EIO;
|
2019-02-27 16:43:01 +01:00
|
|
|
if (port->n_buffers == 0)
|
2018-01-18 11:57:23 +01:00
|
|
|
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;
|
2020-01-10 13:25:40 +01:00
|
|
|
case SPA_NODE_COMMAND_Suspend:
|
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-11-26 12:41:40 +01:00
|
|
|
static const struct spa_dict_item node_info_items[] = {
|
2019-06-03 16:48:01 +02:00
|
|
|
{ SPA_KEY_DEVICE_API, "bluez5" },
|
|
|
|
|
{ SPA_KEY_MEDIA_CLASS, "Audio/Sink" },
|
|
|
|
|
{ SPA_KEY_NODE_DRIVER, "true" },
|
2021-02-08 10:24:27 +01:00
|
|
|
{ SPA_KEY_NODE_LATENCY, SPA_STRINGIFY(MIN_LATENCY)"/48000" },
|
2018-11-26 12:41:40 +01:00
|
|
|
};
|
|
|
|
|
|
2019-03-01 12:00:42 +01:00
|
|
|
static void emit_node_info(struct impl *this, bool full)
|
2019-02-14 17:08:46 +01:00
|
|
|
{
|
2019-03-01 12:00:42 +01:00
|
|
|
if (full)
|
|
|
|
|
this->info.change_mask = this->info_all;
|
|
|
|
|
if (this->info.change_mask) {
|
2019-02-27 16:43:01 +01:00
|
|
|
this->info.props = &SPA_DICT_INIT_ARRAY(node_info_items);
|
2019-03-01 12:00:42 +01:00
|
|
|
spa_node_emit_info(&this->hooks, &this->info);
|
2019-02-27 16:43:01 +01:00
|
|
|
this->info.change_mask = 0;
|
2019-02-14 17:08:46 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-01 12:00:42 +01:00
|
|
|
static void emit_port_info(struct impl *this, struct port *port, bool full)
|
2019-02-14 17:08:46 +01:00
|
|
|
{
|
2019-03-01 12:00:42 +01:00
|
|
|
if (full)
|
|
|
|
|
port->info.change_mask = port->info_all;
|
|
|
|
|
if (port->info.change_mask) {
|
|
|
|
|
spa_node_emit_port_info(&this->hooks,
|
|
|
|
|
SPA_DIRECTION_INPUT, 0, &port->info);
|
2019-02-27 16:43:01 +01:00
|
|
|
port->info.change_mask = 0;
|
2019-02-14 17:08:46 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-13 22:59:04 +02:00
|
|
|
static void emit_node_props_changed(struct impl *this)
|
|
|
|
|
{
|
|
|
|
|
this->info.change_mask |= SPA_NODE_CHANGE_MASK_PARAMS;
|
|
|
|
|
this->params[1].flags ^= SPA_PARAM_INFO_SERIAL;
|
|
|
|
|
emit_node_info(this, false);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-01 12:00:42 +01:00
|
|
|
static int
|
2019-05-20 16:11:23 +02:00
|
|
|
impl_node_add_listener(void *object,
|
2019-03-01 12:00:42 +01:00
|
|
|
struct spa_hook *listener,
|
|
|
|
|
const struct spa_node_events *events,
|
|
|
|
|
void *data)
|
|
|
|
|
{
|
2019-05-20 16:11:23 +02:00
|
|
|
struct impl *this = object;
|
2019-03-01 12:00:42 +01:00
|
|
|
struct spa_hook_list save;
|
|
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
spa_return_val_if_fail(this != NULL, -EINVAL);
|
2019-03-01 12:00:42 +01:00
|
|
|
|
|
|
|
|
spa_hook_list_isolate(&this->hooks, &save, listener, events, data);
|
|
|
|
|
|
|
|
|
|
emit_node_info(this, true);
|
|
|
|
|
emit_port_info(this, &this->port, true);
|
|
|
|
|
|
|
|
|
|
spa_hook_list_join(&this->hooks, &save);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int
|
2019-05-20 16:11:23 +02:00
|
|
|
impl_node_set_callbacks(void *object,
|
2018-01-18 11:57:23 +01:00
|
|
|
const struct spa_node_callbacks *callbacks,
|
|
|
|
|
void *data)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2019-05-20 16:11:23 +02:00
|
|
|
struct impl *this = object;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
spa_return_val_if_fail(this != NULL, -EINVAL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-05-15 12:17:52 +02:00
|
|
|
this->callbacks = SPA_CALLBACKS_INIT(callbacks, data);
|
2018-01-18 11:57:23 +01:00
|
|
|
|
|
|
|
|
return 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 14:01:18 +02:00
|
|
|
static int impl_node_sync(void *object, int seq)
|
|
|
|
|
{
|
|
|
|
|
struct impl *this = object;
|
|
|
|
|
|
|
|
|
|
spa_return_val_if_fail(this != NULL, -EINVAL);
|
|
|
|
|
|
|
|
|
|
spa_node_emit_result(&this->hooks, seq, 0, 0, NULL);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
static int impl_node_add_port(void *object, enum spa_direction direction, uint32_t port_id,
|
2019-02-18 13:22:46 +01:00
|
|
|
const struct spa_dict *props)
|
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
|
|
|
}
|
|
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
static int impl_node_remove_port(void *object, 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
|
2019-05-20 16:11:23 +02:00
|
|
|
impl_node_port_enum_params(void *object, int seq,
|
2019-02-20 17:51:05 +01:00
|
|
|
enum spa_direction direction, uint32_t port_id,
|
|
|
|
|
uint32_t id, uint32_t start, uint32_t num,
|
2019-02-25 12:29:57 +01:00
|
|
|
const struct spa_pod *filter)
|
2018-01-18 11:57:23 +01:00
|
|
|
{
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
struct impl *this = object;
|
2019-02-27 16:43:01 +01:00
|
|
|
struct port *port;
|
2018-01-18 11:57:23 +01:00
|
|
|
struct spa_pod *param;
|
|
|
|
|
struct spa_pod_builder b = { 0 };
|
|
|
|
|
uint8_t buffer[1024];
|
2019-02-25 12:29:57 +01:00
|
|
|
struct spa_result_node_params result;
|
2019-02-20 17:51:05 +01:00
|
|
|
uint32_t count = 0;
|
2020-12-03 18:05:57 +01:00
|
|
|
int res;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
spa_return_val_if_fail(this != NULL, -EINVAL);
|
2019-02-20 17:51:05 +01:00
|
|
|
spa_return_val_if_fail(num != 0, -EINVAL);
|
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);
|
2019-02-27 16:43:01 +01:00
|
|
|
port = &this->port;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-02-25 12:29:57 +01:00
|
|
|
result.id = id;
|
2019-02-20 17:51:05 +01:00
|
|
|
result.next = start;
|
2018-01-18 11:57:23 +01:00
|
|
|
next:
|
2019-02-25 12:29:57 +01:00
|
|
|
result.index = result.next++;
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
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_EnumFormat:
|
2020-12-03 18:05:57 +01:00
|
|
|
if (this->codec == NULL)
|
2020-03-23 13:49:30 +01:00
|
|
|
return -EIO;
|
2020-12-30 16:31:55 +02:00
|
|
|
if (this->transport == NULL)
|
|
|
|
|
return -EIO;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2020-12-04 11:34:38 +01:00
|
|
|
if ((res = this->codec->enum_config(this->codec,
|
2020-12-03 18:05:57 +01:00
|
|
|
this->transport->configuration,
|
|
|
|
|
this->transport->configuration_len,
|
|
|
|
|
id, result.index, &b, ¶m)) != 1)
|
|
|
|
|
return res;
|
2018-08-23 17:47:57 +02:00
|
|
|
break;
|
|
|
|
|
|
2018-08-25 12:08:29 +02:00
|
|
|
case SPA_PARAM_Format:
|
2019-02-27 16:43:01 +01:00
|
|
|
if (!port->have_format)
|
2018-01-18 11:57:23 +01:00
|
|
|
return -EIO;
|
2019-02-25 12:29:57 +01:00
|
|
|
if (result.index > 0)
|
2018-01-18 11:57:23 +01:00
|
|
|
return 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-02-27 16:43:01 +01:00
|
|
|
param = spa_format_audio_raw_build(&b, id, &port->current_format.info.raw);
|
2018-08-23 17:47:57 +02:00
|
|
|
break;
|
|
|
|
|
|
2018-08-25 12:08:29 +02:00
|
|
|
case SPA_PARAM_Buffers:
|
2019-02-27 16:43:01 +01:00
|
|
|
if (!port->have_format)
|
2018-01-18 11:57:23 +01:00
|
|
|
return -EIO;
|
2019-02-25 12:29:57 +01:00
|
|
|
if (result.index > 0)
|
2018-01-18 11:57:23 +01:00
|
|
|
return 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-01-16 11:05:12 +01:00
|
|
|
param = spa_pod_builder_add_object(&b,
|
2018-08-27 15:03:11 +02:00
|
|
|
SPA_TYPE_OBJECT_ParamBuffers, id,
|
2019-01-16 11:05:12 +01:00
|
|
|
SPA_PARAM_BUFFERS_buffers, SPA_POD_CHOICE_RANGE_Int(2, 2, MAX_BUFFERS),
|
|
|
|
|
SPA_PARAM_BUFFERS_blocks, SPA_POD_Int(1),
|
|
|
|
|
SPA_PARAM_BUFFERS_size, SPA_POD_CHOICE_RANGE_Int(
|
2019-02-27 16:43:01 +01:00
|
|
|
this->props.min_latency * port->frame_size,
|
|
|
|
|
this->props.min_latency * port->frame_size,
|
|
|
|
|
INT32_MAX),
|
2019-04-24 12:41:16 +02:00
|
|
|
SPA_PARAM_BUFFERS_stride, SPA_POD_Int(port->frame_size),
|
2019-01-16 11:05:12 +01:00
|
|
|
SPA_PARAM_BUFFERS_align, SPA_POD_Int(16));
|
2018-08-23 17:47:57 +02:00
|
|
|
break;
|
|
|
|
|
|
2018-08-25 12:08:29 +02:00
|
|
|
case SPA_PARAM_Meta:
|
2019-02-25 12:29:57 +01:00
|
|
|
switch (result.index) {
|
2018-01-18 11:57:23 +01:00
|
|
|
case 0:
|
2019-01-16 11:05:12 +01:00
|
|
|
param = spa_pod_builder_add_object(&b,
|
2018-08-27 15:03:11 +02:00
|
|
|
SPA_TYPE_OBJECT_ParamMeta, id,
|
2019-01-16 11:05:12 +01:00
|
|
|
SPA_PARAM_META_type, SPA_POD_Id(SPA_META_Header),
|
|
|
|
|
SPA_PARAM_META_size, SPA_POD_Int(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;
|
|
|
|
|
|
2020-12-17 12:25:12 +01:00
|
|
|
case SPA_PARAM_IO:
|
|
|
|
|
switch (result.index) {
|
|
|
|
|
case 0:
|
|
|
|
|
param = spa_pod_builder_add_object(&b,
|
|
|
|
|
SPA_TYPE_OBJECT_ParamIO, id,
|
|
|
|
|
SPA_PARAM_IO_id, SPA_POD_Id(SPA_IO_Buffers),
|
|
|
|
|
SPA_PARAM_IO_size, SPA_POD_Int(sizeof(struct spa_io_buffers)));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2018-08-23 17:47:57 +02:00
|
|
|
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
|
|
|
|
2019-02-20 17:51:05 +01:00
|
|
|
if (spa_pod_filter(&b, &result.param, param, filter) < 0)
|
2018-01-18 11:57:23 +01:00
|
|
|
goto next;
|
|
|
|
|
|
2019-05-28 13:59:48 +02:00
|
|
|
spa_node_emit_result(&this->hooks, seq, 0, SPA_RESULT_TYPE_NODE_PARAMS, &result);
|
2019-02-20 17:51:05 +01:00
|
|
|
|
|
|
|
|
if (++count != num)
|
|
|
|
|
goto next;
|
|
|
|
|
|
|
|
|
|
return 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-27 16:43:01 +01:00
|
|
|
static int clear_buffers(struct impl *this, struct port *port)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2018-01-18 11:57:23 +01:00
|
|
|
do_stop(this);
|
2019-02-27 16:43:01 +01:00
|
|
|
if (port->n_buffers > 0) {
|
|
|
|
|
spa_list_init(&port->ready);
|
|
|
|
|
port->n_buffers = 0;
|
2018-01-18 11:57:23 +01:00
|
|
|
}
|
|
|
|
|
return 0;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-27 16:43:01 +01:00
|
|
|
static int port_set_format(struct impl *this, struct port *port,
|
2018-01-18 11:57:23 +01:00
|
|
|
uint32_t flags,
|
|
|
|
|
const struct spa_pod *format)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
if (format == NULL) {
|
2020-04-22 12:47:18 +02:00
|
|
|
spa_log_debug(this->log, "clear format");
|
2019-02-27 16:43:01 +01:00
|
|
|
clear_buffers(this, port);
|
|
|
|
|
port->have_format = false;
|
2018-01-18 11:57:23 +01:00
|
|
|
} else {
|
|
|
|
|
struct spa_audio_info info = { 0 };
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-08-29 14:50:58 +02:00
|
|
|
if ((err = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
|
2018-01-18 11:57:23 +01:00
|
|
|
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
|
|
|
|
2020-12-04 11:34:38 +01:00
|
|
|
port->frame_size = info.info.raw.channels;
|
|
|
|
|
switch (info.info.raw.format) {
|
|
|
|
|
case SPA_AUDIO_FORMAT_S16:
|
|
|
|
|
port->frame_size *= 2;
|
|
|
|
|
break;
|
|
|
|
|
case SPA_AUDIO_FORMAT_S24:
|
|
|
|
|
port->frame_size *= 3;
|
|
|
|
|
break;
|
|
|
|
|
case SPA_AUDIO_FORMAT_S24_32:
|
|
|
|
|
case SPA_AUDIO_FORMAT_S32:
|
|
|
|
|
case SPA_AUDIO_FORMAT_F32:
|
|
|
|
|
port->frame_size *= 4;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 16:43:01 +01:00
|
|
|
port->current_format = info;
|
|
|
|
|
port->have_format = true;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-01 12:00:42 +01:00
|
|
|
port->info.change_mask |= SPA_PORT_CHANGE_MASK_PARAMS;
|
2019-02-27 16:43:01 +01:00
|
|
|
if (port->have_format) {
|
|
|
|
|
port->info.change_mask |= SPA_PORT_CHANGE_MASK_FLAGS;
|
2019-07-25 13:19:39 +02:00
|
|
|
port->info.flags = SPA_PORT_FLAG_LIVE;
|
2019-02-27 16:43:01 +01:00
|
|
|
port->info.change_mask |= SPA_PORT_CHANGE_MASK_RATE;
|
2019-03-01 12:00:42 +01:00
|
|
|
port->info.rate = SPA_FRACTION(1, port->current_format.info.raw.rate);
|
2019-02-27 16:43:01 +01:00
|
|
|
port->params[3] = SPA_PARAM_INFO(SPA_PARAM_Format, SPA_PARAM_INFO_READWRITE);
|
|
|
|
|
port->params[4] = SPA_PARAM_INFO(SPA_PARAM_Buffers, SPA_PARAM_INFO_READ);
|
|
|
|
|
} else {
|
|
|
|
|
port->params[3] = SPA_PARAM_INFO(SPA_PARAM_Format, SPA_PARAM_INFO_WRITE);
|
|
|
|
|
port->params[4] = SPA_PARAM_INFO(SPA_PARAM_Buffers, 0);
|
2018-01-18 11:57:23 +01:00
|
|
|
}
|
2019-03-01 12:00:42 +01:00
|
|
|
emit_port_info(this, port, false);
|
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
|
2019-05-20 16:11:23 +02:00
|
|
|
impl_node_port_set_param(void *object,
|
2018-01-18 11:57:23 +01:00
|
|
|
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
|
|
|
{
|
2019-05-20 16:11:23 +02:00
|
|
|
struct impl *this = object;
|
2019-02-27 16:43:01 +01:00
|
|
|
struct port *port;
|
2019-05-15 10:20:28 +02:00
|
|
|
int res;
|
2019-02-27 16:43:01 +01:00
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
spa_return_val_if_fail(this != NULL, -EINVAL);
|
2018-08-23 17:47:57 +02:00
|
|
|
spa_return_val_if_fail(CHECK_PORT(node, direction, port_id), -EINVAL);
|
2019-02-27 16:43:01 +01:00
|
|
|
port = &this->port;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-05-15 10:20:28 +02:00
|
|
|
switch (id) {
|
|
|
|
|
case SPA_PARAM_Format:
|
|
|
|
|
res = port_set_format(this, port, flags, param);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
res = -ENOENT;
|
|
|
|
|
break;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
2019-05-15 10:20:28 +02:00
|
|
|
return res;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
static int
|
2019-05-20 16:11:23 +02:00
|
|
|
impl_node_port_use_buffers(void *object,
|
2019-07-25 13:19:39 +02:00
|
|
|
enum spa_direction direction, uint32_t port_id,
|
|
|
|
|
uint32_t flags,
|
|
|
|
|
struct spa_buffer **buffers, uint32_t n_buffers)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2019-05-20 16:11:23 +02:00
|
|
|
struct impl *this = object;
|
2019-02-27 16:43:01 +01:00
|
|
|
struct port *port;
|
2019-01-07 15:52:42 +01:00
|
|
|
uint32_t i;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
spa_return_val_if_fail(this != NULL, -EINVAL);
|
2018-01-18 11:57:23 +01:00
|
|
|
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
|
2019-02-27 16:43:01 +01:00
|
|
|
port = &this->port;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2020-04-22 12:47:18 +02:00
|
|
|
spa_log_debug(this->log, "use buffers %d", n_buffers);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-02-27 16:43:01 +01:00
|
|
|
if (!port->have_format)
|
2018-01-18 11:57:23 +01:00
|
|
|
return -EIO;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-02-27 16:43:01 +01:00
|
|
|
clear_buffers(this, port);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-01-18 11:57:23 +01:00
|
|
|
for (i = 0; i < n_buffers; i++) {
|
2019-02-27 16:43:01 +01:00
|
|
|
struct buffer *b = &port->buffers[i];
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2018-05-17 17:26:09 +02:00
|
|
|
b->buf = buffers[i];
|
2019-01-07 17:57:03 +01:00
|
|
|
b->id = i;
|
2020-09-02 17:04:19 +02:00
|
|
|
SPA_FLAG_SET(b->flags, BUFFER_FLAG_OUT);
|
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
|
|
|
|
2019-10-16 12:23:42 +02:00
|
|
|
if (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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-27 16:43:01 +01:00
|
|
|
port->n_buffers = n_buffers;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2019-05-20 16:11:23 +02:00
|
|
|
impl_node_port_set_io(void *object,
|
2018-01-18 11:57:23 +01:00
|
|
|
enum spa_direction direction,
|
|
|
|
|
uint32_t port_id,
|
|
|
|
|
uint32_t id,
|
|
|
|
|
void *data, size_t size)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2019-05-20 16:11:23 +02:00
|
|
|
struct impl *this = object;
|
2019-02-27 16:43:01 +01:00
|
|
|
struct port *port;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
spa_return_val_if_fail(this != NULL, -EINVAL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
|
|
|
|
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
|
2019-02-27 16:43:01 +01:00
|
|
|
port = &this->port;
|
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_IO_Buffers:
|
2019-02-27 16:43:01 +01:00
|
|
|
port->io = 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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
static int impl_node_port_reuse_buffer(void *object, uint32_t port_id, uint32_t buffer_id)
|
2018-01-18 11:57:23 +01:00
|
|
|
{
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
static int impl_node_process(void *object)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
2019-05-20 16:11:23 +02:00
|
|
|
struct impl *this = object;
|
2019-02-27 16:43:01 +01:00
|
|
|
struct port *port;
|
|
|
|
|
struct spa_io_buffers *io;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
spa_return_val_if_fail(this != NULL, -EINVAL);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-02-27 16:43:01 +01:00
|
|
|
port = &this->port;
|
|
|
|
|
io = port->io;
|
|
|
|
|
spa_return_val_if_fail(io != NULL, -EIO);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-09-16 12:55:23 +02:00
|
|
|
if (io->status == SPA_STATUS_HAVE_DATA && io->buffer_id < port->n_buffers) {
|
2019-02-27 16:43:01 +01:00
|
|
|
struct buffer *b = &port->buffers[io->buffer_id];
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2020-09-02 17:16:16 +02:00
|
|
|
if (!SPA_FLAG_IS_SET(b->flags, BUFFER_FLAG_OUT)) {
|
2019-02-27 16:43:01 +01:00
|
|
|
spa_log_warn(this->log, NAME " %p: buffer %u in use", this, io->buffer_id);
|
|
|
|
|
io->status = -EINVAL;
|
2018-01-11 10:23:37 +01:00
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 16:43:01 +01:00
|
|
|
spa_log_trace(this->log, NAME " %p: queue buffer %u", this, io->buffer_id);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-02-27 16:43:01 +01:00
|
|
|
spa_list_append(&port->ready, &b->link);
|
2020-09-02 17:04:19 +02:00
|
|
|
SPA_FLAG_CLEAR(b->flags, BUFFER_FLAG_OUT);
|
2018-04-24 17:02:19 +02:00
|
|
|
|
2020-10-16 13:13:04 +02:00
|
|
|
io->buffer_id = SPA_ID_INVALID;
|
2019-02-27 16:43:01 +01:00
|
|
|
io->status = SPA_STATUS_OK;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
2020-10-16 13:13:04 +02:00
|
|
|
if (!spa_list_is_empty(&port->ready))
|
|
|
|
|
flush_data(this, this->current_time);
|
|
|
|
|
|
2019-09-16 12:55:23 +02:00
|
|
|
return SPA_STATUS_HAVE_DATA;
|
2018-01-11 10:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
static const struct spa_node_methods impl_node = {
|
|
|
|
|
SPA_VERSION_NODE_METHODS,
|
2019-03-01 12:00:42 +01:00
|
|
|
.add_listener = impl_node_add_listener,
|
|
|
|
|
.set_callbacks = impl_node_set_callbacks,
|
2019-05-28 14:01:18 +02:00
|
|
|
.sync = impl_node_sync,
|
2019-02-14 17:08:46 +01:00
|
|
|
.enum_params = impl_node_enum_params,
|
|
|
|
|
.set_param = impl_node_set_param,
|
|
|
|
|
.set_io = impl_node_set_io,
|
|
|
|
|
.send_command = impl_node_send_command,
|
|
|
|
|
.add_port = impl_node_add_port,
|
|
|
|
|
.remove_port = impl_node_remove_port,
|
|
|
|
|
.port_enum_params = impl_node_port_enum_params,
|
|
|
|
|
.port_set_param = impl_node_port_set_param,
|
|
|
|
|
.port_use_buffers = impl_node_port_use_buffers,
|
|
|
|
|
.port_set_io = impl_node_port_set_io,
|
|
|
|
|
.port_reuse_buffer = impl_node_port_reuse_buffer,
|
|
|
|
|
.process = impl_node_process,
|
2018-01-11 10:23:37 +01:00
|
|
|
};
|
|
|
|
|
|
2021-01-02 02:02:58 +02:00
|
|
|
static int do_transport_destroy(struct spa_loop *loop,
|
|
|
|
|
bool async,
|
|
|
|
|
uint32_t seq,
|
|
|
|
|
const void *data,
|
|
|
|
|
size_t size,
|
|
|
|
|
void *user_data)
|
|
|
|
|
{
|
|
|
|
|
struct impl *this = user_data;
|
|
|
|
|
this->transport = NULL;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 18:10:18 +02:00
|
|
|
static void transport_destroy(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct impl *this = data;
|
|
|
|
|
spa_log_debug(this->log, "transport %p destroy", this->transport);
|
2021-01-02 02:02:58 +02:00
|
|
|
spa_loop_invoke(this->data_loop, do_transport_destroy, 0, NULL, 0, true, this);
|
2019-05-14 18:10:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct spa_bt_transport_events transport_events = {
|
|
|
|
|
SPA_VERSION_BT_TRANSPORT_EVENTS,
|
|
|
|
|
.destroy = transport_destroy,
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-19 13:15:10 +01:00
|
|
|
static int impl_get_interface(struct spa_handle *handle, const char *type, void **interface)
|
2018-01-11 10:23:37 +01:00
|
|
|
{
|
|
|
|
|
struct impl *this;
|
|
|
|
|
|
|
|
|
|
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
|
|
|
|
spa_return_val_if_fail(interface != NULL, -EINVAL);
|
|
|
|
|
|
|
|
|
|
this = (struct impl *) handle;
|
|
|
|
|
|
2019-12-19 13:15:10 +01:00
|
|
|
if (strcmp(type, SPA_TYPE_INTERFACE_Node) == 0)
|
2018-01-11 10:23:37 +01:00
|
|
|
*interface = &this->node;
|
|
|
|
|
else
|
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int impl_clear(struct spa_handle *handle)
|
|
|
|
|
{
|
2019-06-06 15:21:40 +02:00
|
|
|
struct impl *this = (struct impl *) handle;
|
2020-10-19 09:33:49 +02:00
|
|
|
|
2020-10-19 18:25:52 +02:00
|
|
|
if (this->codec_data)
|
|
|
|
|
this->codec->deinit(this->codec_data);
|
2020-07-03 16:08:53 +02:00
|
|
|
if (this->transport)
|
|
|
|
|
spa_hook_remove(&this->transport_listener);
|
2019-06-06 15:21:40 +02:00
|
|
|
spa_system_close(this->data_system, this->timerfd);
|
2018-01-11 10:23:37 +01:00
|
|
|
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;
|
2019-02-27 16:43:01 +01:00
|
|
|
struct port *port;
|
2019-12-19 13:15:10 +01:00
|
|
|
const char *str;
|
2018-01-11 10:23:37 +01:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2019-12-19 13:15:10 +01:00
|
|
|
this->log = spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Log);
|
|
|
|
|
this->data_loop = spa_support_find(support, n_support, SPA_TYPE_INTERFACE_DataLoop);
|
|
|
|
|
this->data_system = spa_support_find(support, n_support, SPA_TYPE_INTERFACE_DataSystem);
|
|
|
|
|
|
2018-01-11 10:23:37 +01:00
|
|
|
if (this->data_loop == NULL) {
|
|
|
|
|
spa_log_error(this->log, "a data loop is needed");
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
2019-06-06 15:21:40 +02:00
|
|
|
if (this->data_system == NULL) {
|
|
|
|
|
spa_log_error(this->log, "a data system is needed");
|
2018-01-11 10:23:37 +01:00
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 16:11:23 +02:00
|
|
|
this->node.iface = SPA_INTERFACE_INIT(
|
|
|
|
|
SPA_TYPE_INTERFACE_Node,
|
|
|
|
|
SPA_VERSION_NODE,
|
|
|
|
|
&impl_node, this);
|
2019-03-01 12:00:42 +01:00
|
|
|
spa_hook_list_init(&this->hooks);
|
|
|
|
|
|
2018-01-11 10:23:37 +01:00
|
|
|
reset_props(&this->props);
|
|
|
|
|
|
2019-03-01 12:00:42 +01:00
|
|
|
this->info_all = SPA_NODE_CHANGE_MASK_FLAGS |
|
2019-04-24 12:41:16 +02:00
|
|
|
SPA_NODE_CHANGE_MASK_PARAMS |
|
|
|
|
|
SPA_NODE_CHANGE_MASK_PROPS;
|
2019-02-27 16:43:01 +01:00
|
|
|
this->info = SPA_NODE_INFO_INIT();
|
2019-07-12 09:55:55 +02:00
|
|
|
this->info.max_input_ports = 1;
|
|
|
|
|
this->info.max_output_ports = 0;
|
2019-02-27 16:43:01 +01:00
|
|
|
this->info.flags = SPA_NODE_FLAG_RT;
|
|
|
|
|
this->params[0] = SPA_PARAM_INFO(SPA_PARAM_PropInfo, SPA_PARAM_INFO_READ);
|
|
|
|
|
this->params[1] = SPA_PARAM_INFO(SPA_PARAM_Props, SPA_PARAM_INFO_READWRITE);
|
|
|
|
|
this->info.params = this->params;
|
|
|
|
|
this->info.n_params = 2;
|
|
|
|
|
|
|
|
|
|
port = &this->port;
|
2019-03-01 12:00:42 +01:00
|
|
|
port->info_all = SPA_PORT_CHANGE_MASK_FLAGS |
|
|
|
|
|
SPA_PORT_CHANGE_MASK_PARAMS;
|
2019-02-27 16:43:01 +01:00
|
|
|
port->info = SPA_PORT_INFO_INIT();
|
2019-07-25 13:19:39 +02:00
|
|
|
port->info.flags = 0;
|
2019-02-27 16:43:01 +01:00
|
|
|
port->params[0] = SPA_PARAM_INFO(SPA_PARAM_EnumFormat, SPA_PARAM_INFO_READ);
|
|
|
|
|
port->params[1] = SPA_PARAM_INFO(SPA_PARAM_Meta, SPA_PARAM_INFO_READ);
|
|
|
|
|
port->params[2] = SPA_PARAM_INFO(SPA_PARAM_IO, SPA_PARAM_INFO_READ);
|
|
|
|
|
port->params[3] = SPA_PARAM_INFO(SPA_PARAM_Format, SPA_PARAM_INFO_WRITE);
|
|
|
|
|
port->params[4] = SPA_PARAM_INFO(SPA_PARAM_Buffers, 0);
|
|
|
|
|
port->info.params = port->params;
|
|
|
|
|
port->info.n_params = 5;
|
|
|
|
|
spa_list_init(&port->ready);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-12-19 13:15:10 +01:00
|
|
|
if (info && (str = spa_dict_lookup(info, SPA_KEY_API_BLUEZ5_TRANSPORT)))
|
|
|
|
|
sscanf(str, "pointer:%p", &this->transport);
|
|
|
|
|
|
2018-01-11 10:23:37 +01:00
|
|
|
if (this->transport == NULL) {
|
|
|
|
|
spa_log_error(this->log, "a transport is needed");
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
2020-10-19 13:27:11 +02:00
|
|
|
if (this->transport->a2dp_codec == NULL) {
|
|
|
|
|
spa_log_error(this->log, "a transport codec is needed");
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
this->codec = this->transport->a2dp_codec;
|
|
|
|
|
|
2019-05-14 18:10:18 +02:00
|
|
|
spa_bt_transport_add_listener(this->transport,
|
|
|
|
|
&this->transport_listener, &transport_events, this);
|
|
|
|
|
|
2019-06-06 15:21:40 +02:00
|
|
|
this->timerfd = spa_system_timerfd_create(this->data_system,
|
|
|
|
|
CLOCK_MONOTONIC, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct spa_interface_info impl_interfaces[] = {
|
2018-08-27 15:03:11 +02:00
|
|
|
{SPA_TYPE_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[] = {
|
2019-06-03 16:48:01 +02:00
|
|
|
{ SPA_KEY_FACTORY_AUTHOR, "Wim Taymans <wim.taymans@gmail.com>" },
|
|
|
|
|
{ SPA_KEY_FACTORY_DESCRIPTION, "Play audio with the a2dp" },
|
|
|
|
|
{ SPA_KEY_FACTORY_USAGE, SPA_KEY_API_BLUEZ5_TRANSPORT"=<transport>" },
|
2018-01-11 10:23:37 +01:00
|
|
|
};
|
|
|
|
|
|
2019-02-08 12:01:50 +01:00
|
|
|
static const struct spa_dict info = SPA_DICT_INIT_ARRAY(info_items);
|
2018-01-11 10:23:37 +01:00
|
|
|
|
2019-06-07 09:19:40 +02:00
|
|
|
const struct spa_handle_factory spa_a2dp_sink_factory = {
|
2018-01-11 10:23:37 +01:00
|
|
|
SPA_VERSION_HANDLE_FACTORY,
|
2019-06-21 13:31:34 +02:00
|
|
|
SPA_NAME_API_BLUEZ5_A2DP_SINK,
|
2018-01-11 10:23:37 +01:00
|
|
|
&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,
|
|
|
|
|
};
|