Hi Team,
As I am trying to send the rtp back to the asterisk with the help of dgram package of node js
and i have used
channel.externalMedia({
channelId : uniqueIdGeneratorV4(),
app : ``,
external_host : `${rtpDetails.rtpIp}:${rtpDetails.rtpPort}`,
format : "slin16",
transport : "udp",
encapsulation : "rtp",
connection_type: "client",
direction : "both",
});
and on my script i am using dummy script in which i passes the external media port
and that script is streaming mp3 file to the slin16 port here is my current script
import fs from 'fs';
import dgram from 'dgram';
import prism from 'prism-media';
// === CONFIG ===
const DEST_IP = 'ASTERISK IP'; // Asterisk IP
const DEST_PORT = parseInt(process.argv[2], 10) || 10320;
const INPUT_FILE = 'input.mp3';
const SAMPLE_RATE = 16000;
const FRAME_DURATION_MS = 20;
const FRAME_SIZE = SAMPLE_RATE * FRAME_DURATION_MS / 1000; // 320 samples
const CHUNK_SIZE = FRAME_SIZE * 2; // 640 bytes
const PAYLOAD_TYPE = 118;
const SSRC = 0x12345678;
let sequenceNumber = 0;
let timestamp = 0;
const socket = dgram.createSocket('udp4');
let buffer = Buffer.alloc(0);
let interval = null;
// RTP header generator
function createRTPHeader(seq, ts, ssrc, marker = false) {
const header = Buffer.alloc(12);
header[0] = 0x80;
header[1] = (marker ? 0x80 : 0x00) | (PAYLOAD_TYPE & 0x7F);
header.writeUInt16BE(seq, 2);
header.writeUInt32BE(ts, 4);
header.writeUInt32BE(ssrc, 8);
return header;
}
// FFmpeg decoder: MP3 → s16be (PCM big-endian, mono, 16kHz)
const decoder = new prism.FFmpeg({
args: [
'-i', INPUT_FILE,
'-f', 's16be', // Big-endian PCM
'-acodec', 'pcm_s16be', // Explicit codec
'-ac', '1', // Mono
'-ar', SAMPLE_RATE.toString(),
'-loglevel', 'quiet'
]
});
// Handle decoded data
decoder.on('data', (chunk) => {
buffer = Buffer.concat([buffer, chunk]);
if (!interval) {
interval = setInterval(() => {
if (buffer.length >= CHUNK_SIZE) {
const audioChunk = buffer.slice(0, CHUNK_SIZE);
buffer = buffer.slice(CHUNK_SIZE);
const rtpHeader = createRTPHeader(sequenceNumber, timestamp, SSRC, sequenceNumber === 0);
const packet = Buffer.concat([rtpHeader, audioChunk]);
socket.send(packet, 0, packet.length, DEST_PORT, DEST_IP, (err) => {
if (err) console.error('Send error:', err);
});
sequenceNumber = (sequenceNumber + 1) & 0xFFFF;
timestamp += FRAME_SIZE;
} else if (decoder.readableEnded && buffer.length === 0) {
clearInterval(interval);
socket.close();
console.log('✅ Finished streaming.');
}
}, FRAME_DURATION_MS);
}
});
decoder.on('end', () => decoder.destroy());
decoder.on('error', (err) => {
console.error('Decoder error:', err.message);
process.exit(1);
});
console.log(`🎵 Streaming '${INPUT_FILE}' to ${DEST_IP}:${DEST_PORT} as L16/16000/1 (PT=${PAYLOAD_TYPE})...`);
fs.createReadStream(INPUT_FILE).pipe(decoder);
Can you help me am i doing any thing wrong when i run this script i play onyl the silence no audio
i am dicy about the payload type how can i fix this please help me though it
when i use
format: "ulaw"
in external media and
in my current script when used the
const DEST_IP = 'ASTERISK IP'; // Asterisk IP
const DEST_PORT = parseInt(process.argv[2], 10) || 10320;
const INPUT_FILE = 'input.mp3';
const SAMPLE_RATE = 8000;
const FRAME_DURATION_MS = 20;
const FRAME_SIZE = SAMPLE_RATE * FRAME_DURATION_MS / 1000; // 320 samples
const CHUNK_SIZE = FRAME_SIZE * 2; // 640 bytes
const PAYLOAD_TYPE = 0;
const SSRC = 0x12345678;
it works but my aim to support the slin16 and other formats to play the sound please help me out through this