lynxis lazus has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/osmo-mgw/+/44026?usp=email )


Change subject: mgw: add RFC 4733 DTMF telephone-event generation via RQNT
......................................................................

mgw: add RFC 4733 DTMF telephone-event generation via RQNT

Implement DTMF tone injection as RFC 4733 telephone-event RTP packets,
triggered by MGCP RQNT commands with signal requests (S: D/<digit>).

Add mgcp_dtmf.c with timer-based packet generation at 20ms intervals,
proper marker bit, fixed timestamp per event, and 3x redundant
end-of-event packets per RFC 4733 section 2.5.

Enhance handle_noti_req() to parse the I: (connection ID) header so
DTMF can be targeted at a specific connection. When telephone-event is
not negotiated on the connection, the RQNT still succeeds (best-effort)
to maintain backward compatibility with the legacy rqnt_cb path.

Change-Id: I399600535aa709f7d404b1d3e07d889cf46d69d8
---
A include/osmocom/mgcp/mgcp_dtmf.h
M include/osmocom/mgcp/mgcp_endp.h
M src/libosmo-mgcp/Makefile.am
A src/libosmo-mgcp/mgcp_dtmf.c
M src/libosmo-mgcp/mgcp_protocol.c
5 files changed, 379 insertions(+), 9 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-mgw refs/changes/26/44026/1

diff --git a/include/osmocom/mgcp/mgcp_dtmf.h b/include/osmocom/mgcp/mgcp_dtmf.h
new file mode 100644
index 0000000..7f12dce
--- /dev/null
+++ b/include/osmocom/mgcp/mgcp_dtmf.h
@@ -0,0 +1,47 @@
+/*
+ * DTMF tone generation via RFC 4733 telephone-event RTP packets
+ *
+ * (C) 2026 by sysmocom - s.f.m.c. GmbH <[email protected]>
+ * All Rights Reserved
+ *
+ * SPDX-License-Identifier: AGPL-3.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <osmocom/core/timer.h>
+
+struct mgcp_endpoint;
+struct mgcp_conn;
+
+struct mgcp_dtmf_state {
+       bool active;
+       uint8_t event;          /* RFC 4733 event code (0-15) */
+       uint16_t duration;      /* accumulated duration in timestamp units */
+       uint32_t start_ts;      /* RTP timestamp at event start */
+       uint8_t payload_type;   /* negotiated PT for telephone-event */
+       bool first_pkt;         /* marker bit on first packet */
+       struct osmo_timer_list timer;
+       struct mgcp_conn *conn;         /* dst connection (CN side) */
+       struct mgcp_conn *src_conn;     /* src connection (RAN side) */
+       struct mgcp_endpoint *endp;
+};
+
+int mgcp_dtmf_start(struct mgcp_endpoint *endp, struct mgcp_conn *conn,
+                   char digit);
+void mgcp_dtmf_stop(struct mgcp_endpoint *endp);
diff --git a/include/osmocom/mgcp/mgcp_endp.h b/include/osmocom/mgcp/mgcp_endp.h
index 4c1fb20..2a804e5 100644
--- a/include/osmocom/mgcp/mgcp_endp.h
+++ b/include/osmocom/mgcp/mgcp_endp.h
@@ -26,6 +26,7 @@
 #include <osmocom/core/msgb.h>
 #include <osmocom/gsm/i460_mux.h>
 #include <osmocom/mgcp/mgcp_protocol.h>
+#include <osmocom/mgcp/mgcp_dtmf.h>

 struct sockaddr;
 struct mgcp_conn;
@@ -117,6 +118,9 @@
        /*! MGCP_X_OSMO_IGN_* flags from 'X-Osmo-IGN:' header */
        uint32_t x_osmo_ign;

+       /* DTMF generation state (RFC 4733) */
+       struct mgcp_dtmf_state dtmf;
+
        /* E1 specific */
        struct {
                struct osmo_i460_schan_desc scd;
diff --git a/src/libosmo-mgcp/Makefile.am b/src/libosmo-mgcp/Makefile.am
index e51da10..a0ae4b1 100644
--- a/src/libosmo-mgcp/Makefile.am
+++ b/src/libosmo-mgcp/Makefile.am
@@ -44,4 +44,5 @@
        mgcp_rtp_end.c \
        mgcp_e1.c \
        mgcp_iuup.c \
+       mgcp_dtmf.c \
        $(NULL)
diff --git a/src/libosmo-mgcp/mgcp_dtmf.c b/src/libosmo-mgcp/mgcp_dtmf.c
new file mode 100644
index 0000000..e0e2d0e
--- /dev/null
+++ b/src/libosmo-mgcp/mgcp_dtmf.c
@@ -0,0 +1,265 @@
+/*
+ * DTMF tone generation via RFC 4733 telephone-event RTP packets
+ *
+ * (C) 2026 by sysmocom - s.f.m.c. GmbH <[email protected]>
+ * All Rights Reserved
+ *
+ * SPDX-License-Identifier: AGPL-3.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <string.h>
+#include <arpa/inet.h>
+
+#include <osmocom/core/logging.h>
+#include <osmocom/core/timer.h>
+#include <osmocom/core/utils.h>
+
+#include <osmocom/mgcp/mgcp.h>
+#include <osmocom/mgcp/mgcp_endp.h>
+#include <osmocom/mgcp/mgcp_conn.h>
+#include <osmocom/mgcp/mgcp_codec.h>
+#include <osmocom/mgcp/mgcp_network.h>
+#include <osmocom/mgcp/mgcp_dtmf.h>
+
+/* RFC 4733 telephone-event RTP payload (4 bytes) */
+struct rfc4733_payload {
+       uint8_t event;
+#if OSMO_IS_BIG_ENDIAN
+       uint8_t e:1, r:1, volume:6;
+#elif OSMO_IS_LITTLE_ENDIAN
+       uint8_t volume:6, r:1, e:1;
+#endif
+       uint16_t duration;
+} __attribute__((packed));
+
+/* DTMF packet interval: 20ms (160 timestamp units at 8kHz) */
+#define DTMF_PKT_DURATION      160
+#define DTMF_PKT_INTERVAL_US   20000
+#define DTMF_VOLUME            10      /* -10 dBm0 */
+#define DTMF_END_REDUNDANCY    3
+
+/* Map a DTMF digit character to RFC 4733 event code.
+ * Returns -1 if the digit is not a valid DTMF character. */
+static int digit_to_event(char digit)
+{
+       if (digit >= '0' && digit <= '9')
+               return digit - '0';
+       if (digit == '*')
+               return 10;
+       if (digit == '#')
+               return 11;
+       if (digit >= 'A' && digit <= 'D')
+               return 12 + (digit - 'A');
+       if (digit >= 'a' && digit <= 'd')
+               return 12 + (digit - 'a');
+       return -1;
+}
+
+/* Find the payload type for telephone-event in the connection's codec set.
+ * Returns the payload type, or -1 if telephone-event was not negotiated. */
+static int find_telephone_event_pt(struct mgcp_conn_rtp *conn_rtp)
+{
+       struct mgcp_rtp_codecset *cset = &conn_rtp->end.cset;
+       unsigned int i;
+
+       for (i = 0; i < cset->codecs_assigned; i++) {
+               if (strcasecmp(cset->codecs[i].subtype_name,
+                              "telephone-event") == 0)
+                       return cset->codecs[i].payload_type;
+       }
+       return -1;
+}
+
+/* Build and send a single RFC 4733 telephone-event RTP packet.
+ *
+ * DTMF packets share the same SSRC and sequence number space as the
+ * forwarded audio stream.  The source (partner) connection's out_stream
+ * state is the authoritative counter for what the receiver sees, so we
+ * read SSRC from it and atomically claim the next sequence number.
+ * We also bump patch.seq_offset so that the next forwarded audio packet
+ * is numbered correctly after the DTMF packets. */
+static int dtmf_send_pkt(struct mgcp_dtmf_state *st, bool end_bit)
+{
+       struct mgcp_conn_rtp *conn_rtp;
+       struct mgcp_rtp_state *src_state;
+       uint8_t buf[16]; /* 12 byte RTP header + 4 byte payload */
+       struct rfc4733_payload *pl;
+       uint32_t ssrc;
+       uint16_t seq;
+
+       if (!st->conn)
+               return -1;
+
+       conn_rtp = &st->conn->u.rtp;
+
+       /* Get SSRC and next seq from the source connection's out_stream */
+       if (st->src_conn && st->src_conn->type == MGCP_CONN_TYPE_RTP) {
+               src_state = &st->src_conn->u.rtp.state;
+               ssrc = src_state->out_stream.ssrc;
+               seq = src_state->out_stream.last_seq + 1;
+               /* Claim this seq: update out_stream and bump offset */
+               src_state->out_stream.last_seq = seq;
+               src_state->patch.seq_offset++;
+       } else {
+               /* No partner connection — use alt counters */
+               ssrc = conn_rtp->state.alt_rtp_tx_ssrc;
+               seq = conn_rtp->state.alt_rtp_tx_sequence++;
+       }
+
+       /* RTP header */
+       buf[0] = 0x80; /* version 2 */
+       buf[1] = st->payload_type & 0x7f;
+       if (st->first_pkt) {
+               buf[1] |= 0x80; /* marker bit */
+               st->first_pkt = false;
+       }
+       buf[2] = (seq >> 8) & 0xff;
+       buf[3] = seq & 0xff;
+       /* timestamp: fixed at start_ts for entire event (RFC 4733 sec. 
2.5.1.2) */
+       buf[4] = (st->start_ts >> 24) & 0xff;
+       buf[5] = (st->start_ts >> 16) & 0xff;
+       buf[6] = (st->start_ts >> 8) & 0xff;
+       buf[7] = st->start_ts & 0xff;
+       /* SSRC */
+       buf[8] = (ssrc >> 24) & 0xff;
+       buf[9] = (ssrc >> 16) & 0xff;
+       buf[10] = (ssrc >> 8) & 0xff;
+       buf[11] = ssrc & 0xff;
+
+       /* RFC 4733 payload */
+       pl = (struct rfc4733_payload *)&buf[12];
+       pl->event = st->event;
+       pl->e = end_bit ? 1 : 0;
+       pl->r = 0;
+       pl->volume = DTMF_VOLUME;
+       pl->duration = htons(st->duration);
+
+       return mgcp_udp_send(conn_rtp->end.rtp, &conn_rtp->end.addr,
+                            (const char *)buf, sizeof(buf));
+}
+
+/* Timer callback: send periodic DTMF telephone-event packets */
+static void dtmf_timer_cb(void *data)
+{
+       struct mgcp_endpoint *endp = data;
+       struct mgcp_dtmf_state *st = &endp->dtmf;
+
+       if (!st->active)
+               return;
+
+       dtmf_send_pkt(st, false);
+       st->duration += DTMF_PKT_DURATION;
+
+       osmo_timer_schedule(&st->timer, 0, DTMF_PKT_INTERVAL_US);
+}
+
+/*! Start generating RFC 4733 telephone-event RTP packets for a DTMF digit.
+ *  \param[in] endp MGW endpoint.
+ *  \param[in] conn connection on which to inject the DTMF event.
+ *  \param[in] digit DTMF digit character ('0'-'9', '*', '#', 'A'-'D').
+ *  \returns 0 on success, negative on error. */
+int mgcp_dtmf_start(struct mgcp_endpoint *endp, struct mgcp_conn *conn,
+                   char digit)
+{
+       struct mgcp_dtmf_state *st = &endp->dtmf;
+       struct mgcp_conn_rtp *conn_rtp;
+       int event, pt;
+
+       event = digit_to_event(digit);
+       if (event < 0) {
+               LOGPENDP(endp, DLMGCP, LOGL_ERROR,
+                        "DTMF: invalid digit '%c'\n", digit);
+               return -EINVAL;
+       }
+
+       if (conn->type != MGCP_CONN_TYPE_RTP) {
+               LOGPENDP(endp, DLMGCP, LOGL_ERROR,
+                        "DTMF: connection is not RTP\n");
+               return -EINVAL;
+       }
+       conn_rtp = mgcp_conn_get_conn_rtp(conn);
+
+       pt = find_telephone_event_pt(conn_rtp);
+       if (pt < 0) {
+               LOGPENDP(endp, DLMGCP, LOGL_ERROR,
+                        "DTMF: telephone-event not negotiated on conn %s\n",
+                        conn->id);
+               return -ENOTSUP;
+       }
+
+       /* stop any active DTMF first */
+       if (st->active)
+               mgcp_dtmf_stop(endp);
+
+       st->active = true;
+       st->event = event;
+       st->duration = 0;
+       st->payload_type = pt;
+       st->conn = conn;
+       st->endp = endp;
+       st->first_pkt = true;
+
+       /* Find the source (partner) connection whose out_stream state
+        * tracks the SSRC and sequence numbers the receiver sees. */
+       st->src_conn = mgcp_find_dst_conn(conn);
+       if (st->src_conn && st->src_conn->type == MGCP_CONN_TYPE_RTP) {
+               struct mgcp_conn_rtp *src_rtp = &st->src_conn->u.rtp;
+               st->start_ts = src_rtp->state.out_stream.last_timestamp
+                              + DTMF_PKT_DURATION;
+       } else {
+               st->start_ts = mgcp_get_current_ts(8000);
+       }
+
+       LOGPENDP(endp, DLMGCP, LOGL_INFO,
+                "DTMF: start digit '%c' (event %d) on conn %s, PT %d\n",
+                digit, event, conn->id, pt);
+
+       /* Send first packet immediately */
+       dtmf_send_pkt(st, false);
+       st->duration += DTMF_PKT_DURATION;
+
+       /* Schedule periodic 20ms timer */
+       osmo_timer_setup(&st->timer, dtmf_timer_cb, endp);
+       osmo_timer_schedule(&st->timer, 0, DTMF_PKT_INTERVAL_US);
+
+       return 0;
+}
+
+/*! Stop generating DTMF telephone-event RTP packets.
+ *  Sends 3 redundant end-of-event packets and cancels the timer.
+ *  \param[in] endp MGW endpoint. */
+void mgcp_dtmf_stop(struct mgcp_endpoint *endp)
+{
+       struct mgcp_dtmf_state *st = &endp->dtmf;
+       int i;
+
+       if (!st->active)
+               return;
+
+       osmo_timer_del(&st->timer);
+
+       LOGPENDP(endp, DLMGCP, LOGL_INFO,
+                "DTMF: stop event %d on conn %s, duration %u\n",
+                st->event, st->conn ? st->conn->id : "?", st->duration);
+
+       /* Send 3 end-of-event packets (RFC 4733 sec. 2.5.1.4) */
+       for (i = 0; i < DTMF_END_REDUNDANCY; i++)
+               dtmf_send_pkt(st, true);
+
+       st->active = false;
+       st->conn = NULL;
+       st->src_conn = NULL;
+}
diff --git a/src/libosmo-mgcp/mgcp_protocol.c b/src/libosmo-mgcp/mgcp_protocol.c
index e33ce35..3fdce1c 100644
--- a/src/libosmo-mgcp/mgcp_protocol.c
+++ b/src/libosmo-mgcp/mgcp_protocol.c
@@ -1181,19 +1181,22 @@
        return str[2];
 }

-/* This can request like DTMF detection and forward, fax detection... it
- * can also request when the notification should be send and such. We don't
- * do this right now. */
+/* Handle RQNT (Request Notification) for DTMF tone generation.
+ * Parses S: (signal) and I: (connection ID) lines. When a DTMF signal
+ * is requested (S: D/<digit>), inject RFC 4733 telephone-event packets
+ * on the specified connection. An empty S: line stops any active DTMF. */
 static struct msgb *handle_noti_req(struct mgcp_request_data *rq)
 {
        int res = 0;
        char *line;
        char tone = CHAR_MAX;
+       char conn_id[MGCP_CONN_ID_MAXLEN] = { 0 };
+       bool have_signal_line = false;
+       struct mgcp_conn *conn;

        LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification 
...\n");

        if (rq->null_endp) {
-               /* trunk not available so rate_ctr aren't available either. */
                LOGP(DLMGCP, LOGL_ERROR, "RQNT: Not allowed in 'null' 
endpoint!\n");
                return create_err_response(rq->cfg, NULL, 502, "RQNT", 
rq->pdata->trans);
        }
@@ -1201,20 +1204,70 @@
        for_each_line(line, rq->pdata->save) {
                switch (toupper(line[0])) {
                case 'S':
+                       have_signal_line = true;
                        tone = extract_tone(line);
                        break;
+               case 'I':
+                       if (line[1] == ':') {
+                               const char *val = line + 2;
+                               while (*val == ' ')
+                                       val++;
+                               osmo_strlcpy(conn_id, val, sizeof(conn_id));
+                               /* strip trailing whitespace */
+                               while (strlen(conn_id) > 0 &&
+                                      conn_id[strlen(conn_id) - 1] <= ' ')
+                                       conn_id[strlen(conn_id) - 1] = '\0';
+                       }
+                       break;
                }
        }

-       /* we didn't see a signal request with a tone */
-       if (tone == CHAR_MAX)
-               return create_ok_response(rq->endp, rq->endp, 200, "RQNT", 
rq->pdata->trans);
+       /* Empty S: line (no D/ tone) means stop active DTMF */
+       if (have_signal_line && tone == CHAR_MAX) {
+               mgcp_dtmf_stop(rq->endp);
+               return create_ok_response(rq->endp, rq->endp, 200, "RQNT",
+                                        rq->pdata->trans);
+       }

+       /* No signal line at all — nothing to do */
+       if (!have_signal_line)
+               return create_ok_response(rq->endp, rq->endp, 200, "RQNT",
+                                        rq->pdata->trans);
+
+       /* Look up connection by ID if specified */
+       if (conn_id[0]) {
+               conn = mgcp_endp_get_conn(rq->endp, conn_id);
+               if (!conn) {
+                       LOGPENDP(rq->endp, DLMGCP, LOGL_ERROR,
+                                "RQNT: unknown connection ID '%s'\n", conn_id);
+                       return create_err_response(rq->endp, rq->endp, 515,
+                                                  "RQNT", rq->pdata->trans);
+               }
+       } else {
+               /* No I: header — use first connection on the endpoint */
+               conn = llist_first_entry_or_null(&rq->endp->conns,
+                                                struct mgcp_conn, entry);
+               if (!conn) {
+                       LOGPENDP(rq->endp, DLMGCP, LOGL_ERROR,
+                                "RQNT: no connections on endpoint\n");
+                       return create_err_response(rq->endp, rq->endp, 515,
+                                                  "RQNT", rq->pdata->trans);
+               }
+       }
+
+       /* Try RFC 4733 DTMF injection (best-effort, may fail if
+        * telephone-event was not negotiated on this connection) */
+       mgcp_dtmf_start(rq->endp, conn, tone);
+
+       /* Also call legacy callback if configured */
        if (rq->cfg->rqnt_cb)
                res = rq->cfg->rqnt_cb(rq->endp, tone);

-       return res == 0 ? create_ok_response(rq->endp, rq->endp, 200, "RQNT", 
rq->pdata->trans) :
-                               create_err_response(rq->endp, rq->endp, res, 
"RQNT", rq->pdata->trans);
+       return res == 0 ?
+               create_ok_response(rq->endp, rq->endp, 200, "RQNT",
+                                  rq->pdata->trans) :
+               create_err_response(rq->endp, rq->endp, 538, "RQNT",
+                                   rq->pdata->trans);
 }

 /* Connection keepalive timer, will take care that dummy packets are send

--
To view, visit https://gerrit.osmocom.org/c/osmo-mgw/+/44026?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: osmo-mgw
Gerrit-Branch: master
Gerrit-Change-Id: I399600535aa709f7d404b1d3e07d889cf46d69d8
Gerrit-Change-Number: 44026
Gerrit-PatchSet: 1
Gerrit-Owner: lynxis lazus <[email protected]>

Reply via email to