milan-avb: streams: seperate legacy-avb and milan-avb

This commit is contained in:
hackerman-kl 2026-04-24 17:29:00 +02:00 committed by Wim Taymans
parent 6cb03f8e04
commit 5bc1eafd3e

View file

@ -85,7 +85,7 @@ set_iovec(struct spa_ringbuffer *rbuf, void *buffer, uint32_t size,
iov[1].iov_base = buffer; iov[1].iov_base = buffer;
} }
static int flush_write(struct stream *stream, uint64_t current_time) static int flush_write_milan_v12(struct stream *stream, uint64_t current_time)
{ {
int32_t avail; int32_t avail;
uint32_t index; uint32_t index;
@ -93,7 +93,7 @@ static int flush_write(struct stream *stream, uint64_t current_time)
int pdu_count; int pdu_count;
ssize_t n; ssize_t n;
struct avb_frame_header *h = (void*)stream->pdu; struct avb_frame_header *h = (void*)stream->pdu;
bool is_milan = stream->server->avb_mode == AVB_MODE_MILAN_V12; struct avb_packet_aaf *p = SPA_PTROFF(h, sizeof(*h), void);
avail = spa_ringbuffer_get_read_index(&stream->ring, &index); avail = spa_ringbuffer_get_read_index(&stream->ring, &index);
@ -102,66 +102,81 @@ static int flush_write(struct stream *stream, uint64_t current_time)
txtime = current_time + stream->t_uncertainty; txtime = current_time + stream->t_uncertainty;
ptime = txtime + stream->mtt; ptime = txtime + stream->mtt;
if (is_milan) { while (pdu_count--) {
struct avb_packet_aaf *p = SPA_PTROFF(h, sizeof(*h), void); *(uint64_t*)CMSG_DATA(stream->cmsg) = txtime;
while (pdu_count--) { set_iovec(&stream->ring,
*(uint64_t*)CMSG_DATA(stream->cmsg) = txtime; stream->buffer_data,
stream->buffer_size,
index % stream->buffer_size,
&stream->iov[1], stream->payload_size);
set_iovec(&stream->ring, p->seq_num = stream->pdu_seq++;
stream->buffer_data, p->tv = 1;
stream->buffer_size, p->timestamp = htonl((uint32_t)ptime);
index % stream->buffer_size,
&stream->iov[1], stream->payload_size);
p->seq_num = stream->pdu_seq++; n = avb_server_stream_send(stream->server, stream,
p->tv = 1; &stream->msg, MSG_NOSIGNAL);
p->timestamp = htonl((uint32_t)ptime); if (n < 0 || n != (ssize_t)stream->pdu_size)
pw_log_error("stream send failed %zd != %zd: %m",
n = avb_server_stream_send(stream->server, stream, n, stream->pdu_size);
&stream->msg, MSG_NOSIGNAL); txtime += stream->pdu_period;
if (n < 0 || n != (ssize_t)stream->pdu_size) ptime += stream->pdu_period;
pw_log_error("stream send failed %zd != %zd: %m", index += stream->payload_size;
n, stream->pdu_size);
txtime += stream->pdu_period;
ptime += stream->pdu_period;
index += stream->payload_size;
}
} else {
struct avb_packet_iec61883 *p = SPA_PTROFF(h, sizeof(*h), void);
uint8_t dbc = stream->dbc;
while (pdu_count--) {
*(uint64_t*)CMSG_DATA(stream->cmsg) = txtime;
set_iovec(&stream->ring,
stream->buffer_data,
stream->buffer_size,
index % stream->buffer_size,
&stream->iov[1], stream->payload_size);
p->seq_num = stream->pdu_seq++;
p->tv = 1;
p->timestamp = ptime;
p->dbc = dbc;
n = avb_server_stream_send(stream->server, stream,
&stream->msg, MSG_NOSIGNAL);
if (n < 0 || n != (ssize_t)stream->pdu_size)
pw_log_error("stream send failed %zd != %zd: %m",
n, stream->pdu_size);
txtime += stream->pdu_period;
ptime += stream->pdu_period;
index += stream->payload_size;
dbc += stream->frames_per_pdu;
}
stream->dbc = dbc;
} }
spa_ringbuffer_read_update(&stream->ring, index); spa_ringbuffer_read_update(&stream->ring, index);
return 0; return 0;
} }
static int flush_write_legacy(struct stream *stream, uint64_t current_time)
{
int32_t avail;
uint32_t index;
uint64_t ptime, txtime;
int pdu_count;
ssize_t n;
struct avb_frame_header *h = (void*)stream->pdu;
struct avb_packet_iec61883 *p = SPA_PTROFF(h, sizeof(*h), void);
uint8_t dbc = stream->dbc;
avail = spa_ringbuffer_get_read_index(&stream->ring, &index);
pdu_count = (avail / stream->stride) / stream->frames_per_pdu;
txtime = current_time + stream->t_uncertainty;
ptime = txtime + stream->mtt;
while (pdu_count--) {
*(uint64_t*)CMSG_DATA(stream->cmsg) = txtime;
set_iovec(&stream->ring,
stream->buffer_data,
stream->buffer_size,
index % stream->buffer_size,
&stream->iov[1], stream->payload_size);
p->seq_num = stream->pdu_seq++;
p->tv = 1;
p->timestamp = ptime;
p->dbc = dbc;
n = avb_server_stream_send(stream->server, stream,
&stream->msg, MSG_NOSIGNAL);
if (n < 0 || n != (ssize_t)stream->pdu_size)
pw_log_error("stream send failed %zd != %zd: %m",
n, stream->pdu_size);
txtime += stream->pdu_period;
ptime += stream->pdu_period;
index += stream->payload_size;
dbc += stream->frames_per_pdu;
}
stream->dbc = dbc;
spa_ringbuffer_read_update(&stream->ring, index);
return 0;
}
static void on_sink_stream_process(void *data) static void on_sink_stream_process(void *data)
{ {
struct stream *stream = data; struct stream *stream = data;
@ -198,68 +213,81 @@ static void on_sink_stream_process(void *data)
pw_stream_queue_buffer(stream->stream, buf); pw_stream_queue_buffer(stream->stream, buf);
clock_gettime(CLOCK_TAI, &now); clock_gettime(CLOCK_TAI, &now);
flush_write(stream, SPA_TIMESPEC_TO_NSEC(&now)); if (stream->server->avb_mode == AVB_MODE_MILAN_V12)
flush_write_milan_v12(stream, SPA_TIMESPEC_TO_NSEC(&now));
else
flush_write_legacy(stream, SPA_TIMESPEC_TO_NSEC(&now));
} }
static void setup_pdu(struct stream *stream) static void setup_pdu_milan_v12(struct stream *stream)
{ {
struct avb_frame_header *h; struct avb_frame_header *h;
struct avb_packet_aaf *p;
ssize_t payload_size, hdr_size, pdu_size; ssize_t payload_size, hdr_size, pdu_size;
bool is_milan = stream->server->avb_mode == AVB_MODE_MILAN_V12;
spa_memzero(stream->pdu, sizeof(stream->pdu)); spa_memzero(stream->pdu, sizeof(stream->pdu));
h = (struct avb_frame_header*)stream->pdu; h = (struct avb_frame_header*)stream->pdu;
p = SPA_PTROFF(h, sizeof(*h), void);
payload_size = stream->stride * stream->frames_per_pdu; payload_size = stream->stride * stream->frames_per_pdu;
hdr_size = sizeof(*h) + sizeof(*p);
pdu_size = hdr_size + payload_size;
if (is_milan) { h->type = htons(0x8100);
struct avb_packet_aaf *p = SPA_PTROFF(h, sizeof(*h), void); h->prio_cfi_id = htons((stream->prio << 13) | stream->vlan_id);
h->etype = htons(0x22f0);
hdr_size = sizeof(*h) + sizeof(*p); if (stream->direction == SPA_DIRECTION_OUTPUT) {
pdu_size = hdr_size + payload_size; p->subtype = AVB_SUBTYPE_AAF;
p->sv = 1;
p->stream_id = htobe64(stream->id);
p->format = AVB_AAF_FORMAT_INT_32BIT;
p->nsr = AVB_AAF_PCM_NSR_48KHZ;
p->bit_depth = 32;
p->chan_per_frame = stream->info.info.raw.channels;
p->sp = AVB_AAF_PCM_SP_NORMAL;
p->event = 0;
p->seq_num = 0;
p->data_len = htons(payload_size);
}
h->type = htons(0x8100); stream->hdr_size = hdr_size;
h->prio_cfi_id = htons((stream->prio << 13) | stream->vlan_id); stream->payload_size = payload_size;
h->etype = htons(0x22f0); stream->pdu_size = pdu_size;
}
if (stream->direction == SPA_DIRECTION_OUTPUT) { static void setup_pdu_legacy(struct stream *stream)
p->subtype = AVB_SUBTYPE_AAF; {
p->sv = 1; struct avb_frame_header *h;
p->stream_id = htobe64(stream->id); struct avb_packet_iec61883 *p;
p->format = AVB_AAF_FORMAT_INT_32BIT; ssize_t payload_size, hdr_size, pdu_size;
p->nsr = AVB_AAF_PCM_NSR_48KHZ;
p->bit_depth = 32;
p->chan_per_frame = stream->info.info.raw.channels;
p->sp = AVB_AAF_PCM_SP_NORMAL;
p->event = 0;
p->seq_num = 0;
p->data_len = htons(payload_size);
}
} else {
struct avb_packet_iec61883 *p = SPA_PTROFF(h, sizeof(*h), void);
hdr_size = sizeof(*h) + sizeof(*p); spa_memzero(stream->pdu, sizeof(stream->pdu));
pdu_size = hdr_size + payload_size; h = (struct avb_frame_header*)stream->pdu;
p = SPA_PTROFF(h, sizeof(*h), void);
h->type = htons(0x8100); payload_size = stream->stride * stream->frames_per_pdu;
h->prio_cfi_id = htons((stream->prio << 13) | stream->vlan_id); hdr_size = sizeof(*h) + sizeof(*p);
h->etype = htons(0x22f0); pdu_size = hdr_size + payload_size;
if (stream->direction == SPA_DIRECTION_OUTPUT) { h->type = htons(0x8100);
p->subtype = AVB_SUBTYPE_61883_IIDC; h->prio_cfi_id = htons((stream->prio << 13) | stream->vlan_id);
p->sv = 1; h->etype = htons(0x22f0);
p->stream_id = htobe64(stream->id);
p->data_len = htons(payload_size + 8); if (stream->direction == SPA_DIRECTION_OUTPUT) {
p->tag = 0x1; p->subtype = AVB_SUBTYPE_61883_IIDC;
p->channel = 0x1f; p->sv = 1;
p->tcode = 0xa; p->stream_id = htobe64(stream->id);
p->sid = 0x3f; p->data_len = htons(payload_size + 8);
p->dbs = stream->info.info.raw.channels; p->tag = 0x1;
p->qi2 = 0x2; p->channel = 0x1f;
p->format_id = 0x10; p->tcode = 0xa;
p->fdf = 0x2; p->sid = 0x3f;
p->syt = htons(0x0008); p->dbs = stream->info.info.raw.channels;
} p->qi2 = 0x2;
p->format_id = 0x10;
p->fdf = 0x2;
p->syt = htons(0x0008);
} }
stream->hdr_size = hdr_size; stream->hdr_size = hdr_size;
@ -373,7 +401,10 @@ struct stream *server_create_stream(struct server *server, struct stream *stream
stream->pdu_period = SPA_NSEC_PER_SEC * stream->frames_per_pdu / stream->pdu_period = SPA_NSEC_PER_SEC * stream->frames_per_pdu /
stream->info.info.raw.rate; stream->info.info.raw.rate;
setup_pdu(stream); if (server->avb_mode == AVB_MODE_MILAN_V12)
setup_pdu_milan_v12(stream);
else
setup_pdu_legacy(stream);
setup_msg(stream); setup_msg(stream);
res = avb_msrp_attribute_new(server->msrp, &common->lstream_attr, res = avb_msrp_attribute_new(server->msrp, &common->lstream_attr,
@ -398,10 +429,12 @@ struct stream *server_create_stream(struct server *server, struct stream *stream
} }
common->tastream_attr.attr.talker.vlan_id = htons(stream->vlan_id); common->tastream_attr.attr.talker.vlan_id = htons(stream->vlan_id);
common->tastream_attr.attr.talker.tspec_max_frame_size = if (server->avb_mode == AVB_MODE_MILAN_V12)
htons(stream->server->avb_mode == AVB_MODE_MILAN_V12 common->tastream_attr.attr.talker.tspec_max_frame_size =
? (uint16_t)stream->pdu_size htons((uint16_t)stream->pdu_size);
: (uint16_t)(32 + stream->frames_per_pdu * stream->stride)); else
common->tastream_attr.attr.talker.tspec_max_frame_size =
htons((uint16_t)(32 + stream->frames_per_pdu * stream->stride));
common->tastream_attr.attr.talker.tspec_max_interval_frames = common->tastream_attr.attr.talker.tspec_max_interval_frames =
htons(AVB_MSRP_TSPEC_MAX_INTERVAL_FRAMES_DEFAULT); htons(AVB_MSRP_TSPEC_MAX_INTERVAL_FRAMES_DEFAULT);
common->tastream_attr.attr.talker.priority = stream->prio; common->tastream_attr.attr.talker.priority = stream->prio;