Step 4.1: /stream.sdp from the live config and PTP GM

- aes67_sdp_sap: aes67_sdp_build() builds the AES67 SDP (RFC 4566 +
  RFC 7273) with the same lines and order as the web UI preview;
  GET /stream.sdp serves it as application/sdp.
- aes67_ptp: public aes67_ptp_gm_id(), aes67_ptp_locked(),
  aes67_ptp_now_ns(). Clock IDs are now uppercase (RFC 7273 style), also
  in status.ptp, which the UI copies into ts-refclk.
- aes67_net: aes67_net_netif() returns the AES67 interface.
- Verified on board: /stream.sdp byte-identical to the UI's sdp() for
  the same config/status; mono, L16, ptime 0.333, ttl 8, clk_offset
  4294967295 and session_ver all follow the config.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-25 06:38:55 +10:00
parent 004cc6e212
commit 3d05768f00
11 changed files with 152 additions and 5 deletions
+2 -1
View File
@@ -1,2 +1,3 @@
idf_component_register(SRCS "aes67_sdp_sap.c"
INCLUDE_DIRS "include")
INCLUDE_DIRS "include"
PRIV_REQUIRES aes67_net aes67_ptp aes67_web esp_netif)
+84 -2
View File
@@ -1,7 +1,89 @@
#include "aes67_sdp_sap.h"
// Stub (build step 0).
#include <stdio.h>
#include <string.h>
#include "aes67_cfg.h"
#include "aes67_net.h"
#include "aes67_ptp.h"
#include "aes67_web.h"
#include "esp_netif.h"
#define SDP_MAX 1024
// Same lines, in the same order, as the SDP preview in web/index.html.
size_t aes67_sdp_build(char *buf, size_t size)
{
cJSON *a = cfg_get("aes67");
cJSON *p = cfg_get("ptp");
if (!a || !p) {
cJSON_Delete(a);
cJSON_Delete(p);
return 0;
}
#define NUM(o, k) cJSON_GetObjectItemCaseSensitive(o, k)->valuedouble
#define STR(o, k) cJSON_GetObjectItemCaseSensitive(o, k)->valuestring
char ip[16] = "0.0.0.0";
esp_netif_ip_info_t info;
esp_netif_t *netif = aes67_net_netif();
if (netif && esp_netif_get_ip_info(netif, &info) == ESP_OK && info.ip.addr) {
snprintf(ip, sizeof(ip), IPSTR, IP2STR(&info.ip));
}
char gm[24];
if (!aes67_ptp_gm_id(gm)) {
strcpy(gm, "00-00-00-00-00-00-00-00");
}
int ch = (int)NUM(a, "channels");
char info_ch[16];
snprintf(info_ch, sizeof(info_ch), ch == 1 ? "mono" : ch == 2 ? "stereo" : "%d ch", ch);
int dom = (int)NUM(p, "domain");
int pt = (int)NUM(a, "pt");
int n = snprintf(buf, size,
"v=0\r\n"
"o=- %.0f %.0f IN IP4 %s\r\n"
"s=%s\r\n"
"c=IN IP4 %s/%d\r\n"
"t=0 0\r\n"
"a=clock-domain:PTPv2 %d\r\n"
"m=audio %d RTP/AVP %d\r\n"
"i=%s\r\n"
"a=rtpmap:%d %s/%d/%d\r\n"
"a=recvonly\r\n"
"a=ptime:%g\r\n"
"a=ts-refclk:ptp=IEEE1588-2008:%s:%d\r\n"
"a=mediaclk:direct=%.0f\r\n",
NUM(a, "session_id"), NUM(a, "session_ver"), ip,
STR(a, "name"),
STR(a, "mcast"), (int)NUM(a, "ttl"),
dom,
(int)NUM(a, "port"), pt,
info_ch,
pt, STR(a, "encoding"), (int)NUM(a, "rate"), ch,
NUM(a, "ptime"),
gm, dom,
NUM(a, "clk_offset"));
#undef NUM
#undef STR
cJSON_Delete(a);
cJSON_Delete(p);
return n > 0 && (size_t)n < size ? (size_t)n : 0;
}
static esp_err_t sdp_get(httpd_req_t *req)
{
char buf[SDP_MAX];
size_t len = aes67_sdp_build(buf, sizeof(buf));
if (!len) {
return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "SDP build failed");
}
httpd_resp_set_type(req, "application/sdp");
httpd_resp_set_hdr(req, "Cache-Control", "no-store");
return httpd_resp_send(req, buf, len);
}
esp_err_t aes67_sdp_sap_init(void)
{
return ESP_OK;
static const httpd_uri_t uri = { .uri = "/stream.sdp", .method = HTTP_GET, .handler = sdp_get };
return web_register_uri(&uri);
}
@@ -2,6 +2,11 @@
// Core component: must not depend on main/ (project code).
#pragma once
#include <stddef.h>
#include "esp_err.h"
// Registers GET /stream.sdp. (SAP: step 5.)
esp_err_t aes67_sdp_sap_init(void);
// Build the current SDP (RFC 4566 + RFC 7273, AES67). Returns the length, or 0 if it didn't fit.
size_t aes67_sdp_build(char *buf, size_t size);