mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-08 13:30:08 -05:00
avb: avbtp -> avb
This commit is contained in:
parent
d5b4c12684
commit
773bd610aa
39 changed files with 1975 additions and 2108 deletions
|
|
@ -1,380 +0,0 @@
|
|||
/* AVB support
|
||||
*
|
||||
* Copyright © 2022 Wim Taymans
|
||||
*
|
||||
* 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:
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <spa/debug/mem.h>
|
||||
|
||||
#include <pipewire/pipewire.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "msrp.h"
|
||||
|
||||
static const uint8_t mac[6] = AVB_MSRP_MAC;
|
||||
|
||||
struct attr {
|
||||
struct avbtp_msrp_attribute attr;
|
||||
struct spa_list link;
|
||||
};
|
||||
|
||||
struct msrp {
|
||||
struct server *server;
|
||||
struct spa_hook server_listener;
|
||||
struct spa_hook mrp_listener;
|
||||
|
||||
struct spa_list attributes;
|
||||
};
|
||||
|
||||
static void debug_msrp_talker_common(const struct avbtp_packet_msrp_talker *t)
|
||||
{
|
||||
char buf[128];
|
||||
pw_log_info(" stream-id: %s", avbtp_utils_format_id(buf, sizeof(buf), be64toh(t->stream_id)));
|
||||
pw_log_info(" dest-addr: %s", avbtp_utils_format_addr(buf, sizeof(buf), t->dest_addr));
|
||||
pw_log_info(" vlan-id: %d", ntohs(t->vlan_id));
|
||||
pw_log_info(" tspec-max-frame-size: %d", ntohs(t->tspec_max_frame_size));
|
||||
pw_log_info(" tspec-max-interval-frames: %d", ntohs(t->tspec_max_interval_frames));
|
||||
pw_log_info(" priority: %d", t->priority);
|
||||
pw_log_info(" rank: %d", t->rank);
|
||||
pw_log_info(" accumulated-latency: %d", ntohl(t->accumulated_latency));
|
||||
}
|
||||
|
||||
static void debug_msrp_talker(const struct avbtp_packet_msrp_talker *t)
|
||||
{
|
||||
pw_log_info("talker");
|
||||
debug_msrp_talker_common(t);
|
||||
}
|
||||
|
||||
static void notify_talker(struct msrp *msrp, uint64_t now, struct attr *attr, uint8_t notify)
|
||||
{
|
||||
pw_log_info("> notify talker: %d", notify);
|
||||
debug_msrp_talker(&attr->attr.attr.talker);
|
||||
}
|
||||
|
||||
static int process_talker(struct msrp *msrp, uint64_t now, uint8_t attr_type,
|
||||
const void *m, uint8_t event, uint8_t param, int num)
|
||||
{
|
||||
const struct avbtp_packet_msrp_talker *t = m;
|
||||
struct attr *a;
|
||||
spa_list_for_each(a, &msrp->attributes, link)
|
||||
if (a->attr.type == attr_type &&
|
||||
a->attr.attr.talker.stream_id == t->stream_id) {
|
||||
a->attr.attr.talker = *t;
|
||||
avbtp_mrp_rx_event(msrp->server->mrp, now, a->attr.mrp, event);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void debug_msrp_talker_fail(const struct avbtp_packet_msrp_talker_fail *t)
|
||||
{
|
||||
char buf[128];
|
||||
pw_log_info("talker fail");
|
||||
debug_msrp_talker_common(&t->talker);
|
||||
pw_log_info(" bridge-id: %s", avbtp_utils_format_id(buf, sizeof(buf), be64toh(t->bridge_id)));
|
||||
pw_log_info(" failure-code: %d", t->failure_code);
|
||||
}
|
||||
|
||||
static int process_talker_fail(struct msrp *msrp, uint64_t now, uint8_t attr_type,
|
||||
const void *m, uint8_t event, uint8_t param, int num)
|
||||
{
|
||||
const struct avbtp_packet_msrp_talker_fail *t = m;
|
||||
struct attr *a;
|
||||
|
||||
debug_msrp_talker_fail(t);
|
||||
|
||||
spa_list_for_each(a, &msrp->attributes, link)
|
||||
if (a->attr.type == attr_type &&
|
||||
a->attr.attr.talker_fail.talker.stream_id == t->talker.stream_id)
|
||||
avbtp_mrp_rx_event(msrp->server->mrp, now, a->attr.mrp, event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void debug_msrp_listener(const struct avbtp_packet_msrp_listener *l)
|
||||
{
|
||||
char buf[128];
|
||||
pw_log_info("listener");
|
||||
pw_log_info(" %s", avbtp_utils_format_id(buf, sizeof(buf), be64toh(l->stream_id)));
|
||||
}
|
||||
|
||||
static void notify_listener(struct msrp *msrp, uint64_t now, struct attr *attr, uint8_t notify)
|
||||
{
|
||||
pw_log_info("> notify listener: %d", notify);
|
||||
debug_msrp_listener(&attr->attr.attr.listener);
|
||||
}
|
||||
|
||||
static int process_listener(struct msrp *msrp, uint64_t now, uint8_t attr_type,
|
||||
const void *m, uint8_t event, uint8_t param, int num)
|
||||
{
|
||||
const struct avbtp_packet_msrp_listener *l = m;
|
||||
struct attr *a;
|
||||
spa_list_for_each(a, &msrp->attributes, link)
|
||||
if (a->attr.type == attr_type &&
|
||||
a->attr.attr.listener.stream_id == l->stream_id)
|
||||
avbtp_mrp_rx_event(msrp->server->mrp, now, a->attr.mrp, event);
|
||||
return 0;
|
||||
}
|
||||
static int encode_listener(struct msrp *msrp, struct attr *a, void *m)
|
||||
{
|
||||
struct avbtp_packet_msrp_msg *msg = m;
|
||||
struct avbtp_packet_mrp_vector *v;
|
||||
struct avbtp_packet_msrp_listener *l;
|
||||
struct avbtp_packet_mrp_footer *f;
|
||||
uint8_t *ev;
|
||||
size_t attr_list_length = sizeof(*v) + sizeof(*l) + sizeof(*f) + 1 + 1;
|
||||
|
||||
msg->attribute_type = AVBTP_MSRP_ATTRIBUTE_TYPE_LISTENER;
|
||||
msg->attribute_length = sizeof(*l);
|
||||
msg->attribute_list_length = htons(attr_list_length);
|
||||
|
||||
v = (struct avbtp_packet_mrp_vector *)msg->attribute_list;
|
||||
v->lva = 0;
|
||||
AVBTP_MRP_VECTOR_SET_NUM_VALUES(v, 1);
|
||||
|
||||
l = (struct avbtp_packet_msrp_listener *)v->first_value;
|
||||
*l = a->attr.attr.listener;
|
||||
|
||||
ev = SPA_PTROFF(l, sizeof(*l), uint8_t);
|
||||
*ev = a->attr.mrp->pending_send * 6 * 6;
|
||||
|
||||
ev = SPA_PTROFF(ev, sizeof(*ev), uint8_t);
|
||||
*ev = a->attr.param * 4 * 4 * 4;
|
||||
|
||||
f = SPA_PTROFF(ev, sizeof(*ev), struct avbtp_packet_mrp_footer);
|
||||
f->end_mark = 0;
|
||||
|
||||
return attr_list_length + sizeof(*msg);
|
||||
}
|
||||
|
||||
|
||||
static void debug_msrp_domain(const struct avbtp_packet_msrp_domain *d)
|
||||
{
|
||||
pw_log_info("domain");
|
||||
pw_log_info(" %d", d->sr_class_id);
|
||||
pw_log_info(" %d", d->sr_class_priority);
|
||||
pw_log_info(" %d", ntohs(d->sr_class_vid));
|
||||
}
|
||||
|
||||
static void notify_domain(struct msrp *msrp, uint64_t now, struct attr *attr, uint8_t notify)
|
||||
{
|
||||
pw_log_info("> notify domain: %d", notify);
|
||||
debug_msrp_domain(&attr->attr.attr.domain);
|
||||
}
|
||||
|
||||
static int process_domain(struct msrp *msrp, uint64_t now, uint8_t attr_type,
|
||||
const void *m, uint8_t event, uint8_t param, int num)
|
||||
{
|
||||
struct attr *a;
|
||||
spa_list_for_each(a, &msrp->attributes, link)
|
||||
if (a->attr.type == attr_type)
|
||||
avbtp_mrp_rx_event(msrp->server->mrp, now, a->attr.mrp, event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int encode_domain(struct msrp *msrp, struct attr *a, void *m)
|
||||
{
|
||||
struct avbtp_packet_msrp_msg *msg = m;
|
||||
struct avbtp_packet_mrp_vector *v;
|
||||
struct avbtp_packet_msrp_domain *d;
|
||||
struct avbtp_packet_mrp_footer *f;
|
||||
uint8_t *ev;
|
||||
size_t attr_list_length = sizeof(*v) + sizeof(*d) + sizeof(*f) + 1;
|
||||
|
||||
msg->attribute_type = AVBTP_MSRP_ATTRIBUTE_TYPE_DOMAIN;
|
||||
msg->attribute_length = sizeof(*d);
|
||||
msg->attribute_list_length = htons(attr_list_length);
|
||||
|
||||
v = (struct avbtp_packet_mrp_vector *)msg->attribute_list;
|
||||
v->lva = 0;
|
||||
AVBTP_MRP_VECTOR_SET_NUM_VALUES(v, 1);
|
||||
|
||||
d = (struct avbtp_packet_msrp_domain *)v->first_value;
|
||||
*d = a->attr.attr.domain;
|
||||
|
||||
ev = SPA_PTROFF(d, sizeof(*d), uint8_t);
|
||||
*ev = a->attr.mrp->pending_send * 36;
|
||||
|
||||
f = SPA_PTROFF(ev, sizeof(*ev), struct avbtp_packet_mrp_footer);
|
||||
f->end_mark = 0;
|
||||
|
||||
return attr_list_length + sizeof(*msg);
|
||||
}
|
||||
|
||||
static const struct {
|
||||
int (*dispatch) (struct msrp *msrp, uint64_t now, uint8_t attr_type,
|
||||
const void *m, uint8_t event, uint8_t param, int num);
|
||||
int (*encode) (struct msrp *msrp, struct attr *attr, void *m);
|
||||
void (*notify) (struct msrp *msrp, uint64_t now, struct attr *attr, uint8_t notify);
|
||||
} dispatch[] = {
|
||||
[AVBTP_MSRP_ATTRIBUTE_TYPE_TALKER_ADVERTISE] = { process_talker, NULL, notify_talker, },
|
||||
[AVBTP_MSRP_ATTRIBUTE_TYPE_TALKER_FAILED] = { process_talker_fail, NULL, NULL },
|
||||
[AVBTP_MSRP_ATTRIBUTE_TYPE_LISTENER] = { process_listener, encode_listener, notify_listener },
|
||||
[AVBTP_MSRP_ATTRIBUTE_TYPE_DOMAIN] = { process_domain, encode_domain, notify_domain, },
|
||||
};
|
||||
|
||||
static bool msrp_check_header(void *data, const void *hdr, size_t *hdr_size, bool *has_params)
|
||||
{
|
||||
const struct avbtp_packet_msrp_msg *msg = hdr;
|
||||
uint8_t attr_type = msg->attribute_type;
|
||||
|
||||
if (!AVBTP_MSRP_ATTRIBUTE_TYPE_VALID(attr_type))
|
||||
return false;
|
||||
|
||||
*hdr_size = sizeof(*msg);
|
||||
*has_params = attr_type == AVBTP_MSRP_ATTRIBUTE_TYPE_LISTENER;
|
||||
return true;
|
||||
}
|
||||
|
||||
static int msrp_attr_event(void *data, uint64_t now, uint8_t attribute_type, uint8_t event)
|
||||
{
|
||||
struct msrp *msrp = data;
|
||||
struct attr *a;
|
||||
spa_list_for_each(a, &msrp->attributes, link)
|
||||
if (a->attr.type == attribute_type)
|
||||
avbtp_mrp_update_state(msrp->server->mrp, now, a->attr.mrp, event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int msrp_process(void *data, uint64_t now, uint8_t attribute_type, const void *value,
|
||||
uint8_t event, uint8_t param, int index)
|
||||
{
|
||||
struct msrp *msrp = data;
|
||||
return dispatch[attribute_type].dispatch(msrp, now,
|
||||
attribute_type, value, event, param, index);
|
||||
}
|
||||
|
||||
static const struct avbtp_mrp_parse_info info = {
|
||||
AVBTP_VERSION_MRP_PARSE_INFO,
|
||||
.check_header = msrp_check_header,
|
||||
.attr_event = msrp_attr_event,
|
||||
.process = msrp_process,
|
||||
};
|
||||
|
||||
|
||||
static int msrp_message(void *data, uint64_t now, const void *message, int len)
|
||||
{
|
||||
struct msrp *msrp = data;
|
||||
const struct avbtp_packet_mrp *p = message;
|
||||
|
||||
if (ntohs(p->eth.type) != AVB_MSRP_ETH)
|
||||
return 0;
|
||||
if (memcmp(p->eth.dest, mac, 6) != 0)
|
||||
return 0;
|
||||
|
||||
pw_log_debug("MSRP");
|
||||
return avbtp_mrp_parse_packet(msrp->server->mrp,
|
||||
now, message, len, &info, msrp);
|
||||
}
|
||||
|
||||
static void msrp_destroy(void *data)
|
||||
{
|
||||
struct msrp *msrp = data;
|
||||
spa_hook_remove(&msrp->server_listener);
|
||||
free(msrp);
|
||||
}
|
||||
|
||||
static const struct server_events server_events = {
|
||||
AVBTP_VERSION_SERVER_EVENTS,
|
||||
.destroy = msrp_destroy,
|
||||
.message = msrp_message
|
||||
};
|
||||
|
||||
struct avbtp_msrp_attribute *avbtp_msrp_attribute_new(struct avbtp_msrp *m,
|
||||
uint8_t type)
|
||||
{
|
||||
struct msrp *msrp = (struct msrp*)m;
|
||||
struct avbtp_mrp_attribute *attr;
|
||||
struct attr *a;
|
||||
|
||||
attr = avbtp_mrp_attribute_new(msrp->server->mrp, sizeof(struct attr));
|
||||
|
||||
a = attr->user_data;
|
||||
a->attr.mrp = attr;
|
||||
a->attr.type = type;
|
||||
spa_list_append(&msrp->attributes, &a->link);
|
||||
|
||||
return &a->attr;
|
||||
}
|
||||
|
||||
static void msrp_event(void *data, uint64_t now, uint8_t event)
|
||||
{
|
||||
struct msrp *msrp = data;
|
||||
uint8_t buffer[2048];
|
||||
struct avbtp_packet_mrp *p = (struct avbtp_packet_mrp*)buffer;
|
||||
struct avbtp_packet_mrp_footer *f;
|
||||
void *msg = SPA_PTROFF(buffer, sizeof(*p), void);
|
||||
struct attr *a;
|
||||
int len, count = 0;
|
||||
size_t total = sizeof(*p) + 2;
|
||||
|
||||
p->version = AVBTP_MRP_PROTOCOL_VERSION;
|
||||
|
||||
spa_list_for_each(a, &msrp->attributes, link) {
|
||||
if (!a->attr.mrp->pending_send)
|
||||
continue;
|
||||
if (dispatch[a->attr.type].encode == NULL)
|
||||
continue;
|
||||
|
||||
len = dispatch[a->attr.type].encode(msrp, a, msg);
|
||||
if (len < 0)
|
||||
break;
|
||||
|
||||
count++;
|
||||
msg = SPA_PTROFF(msg, len, void);
|
||||
total += len;
|
||||
}
|
||||
f = (struct avbtp_packet_mrp_footer *)msg;
|
||||
f->end_mark = 0;
|
||||
|
||||
if (count > 0)
|
||||
avbtp_server_send_packet(msrp->server, mac, AVB_MSRP_ETH,
|
||||
buffer, total);
|
||||
}
|
||||
|
||||
static void msrp_notify(void *data, uint64_t now, struct avbtp_mrp_attribute *attr, uint8_t notify)
|
||||
{
|
||||
struct msrp *msrp = data;
|
||||
struct attr *a = attr->user_data;
|
||||
if (dispatch[a->attr.type].notify != NULL)
|
||||
dispatch[a->attr.type].notify(msrp, now, a, notify);
|
||||
}
|
||||
|
||||
static const struct avbtp_mrp_events mrp_events = {
|
||||
AVBTP_VERSION_MRP_ATTRIBUTE_CALLBACKS,
|
||||
.event = msrp_event,
|
||||
.notify = msrp_notify
|
||||
};
|
||||
|
||||
struct avbtp_msrp *avbtp_msrp_register(struct server *server)
|
||||
{
|
||||
struct msrp *msrp;
|
||||
|
||||
msrp = calloc(1, sizeof(*msrp));
|
||||
if (msrp == NULL)
|
||||
return NULL;
|
||||
|
||||
msrp->server = server;
|
||||
spa_list_init(&msrp->attributes);
|
||||
|
||||
avdecc_server_add_listener(server, &msrp->server_listener, &server_events, msrp);
|
||||
avbtp_mrp_add_listener(server->mrp, &msrp->mrp_listener, &mrp_events, msrp);
|
||||
|
||||
return (struct avbtp_msrp*)msrp;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue