Hello @jcolp
I want trying to do with multiple external channels I can see the package is going properly but I can’t listen it on mobile side but when I play from file it works fine, can you please check the code and help me if I am getting anything wrong, I worked whole night yesterday I am really tired now and sorry to bother you.
await this.bridge.addChannel({
channel: this.channel.id,
role: 'participant'
});
// Create external media channel
const externalChannelId = `inbound_external_${this.channelId}`;
this.externalMediaChannel = await this.client.channels.externalMedia({
app: 'voicebot',
external_host: '127.0.0.1:3001',
format: 'ulaw',
channelId: externalChannelId
});
// Add external media channel to bridge
await this.bridge.addChannel({
channel: this.externalMediaChannel.id,
role: 'participant'
});
// Create external media channel
const externalVChannelId = `outbound_external_${this.channelId}`;
this.externalVMediaChannel = await this.client.channels.externalMedia({
app: 'voicebot',
external_host: '127.0.0.1:10001',
format: 'ulaw',
channelId: externalVChannelId
});
await this.externalVMediaChannel.setChannelVar({
variable: 'CHANNEL(rtp_use_dynamic_payload)',
value: '0'
});
await this.bridge.addChannel({
channel: this.externalVMediaChannel.id,
role: 'participant'
});
in this code 3001 is working but 10001 is not working my rtp.conf is
GNU nano 7.2 /etc/asterisk/rtp.conf
[general]
rtpstart=10000
rtpend=20000
rtpchecksums=no
strictrtp=yes ; Changed to yes for security
icesupport=no
stunaddr=
dtls_learning=no
rtptimeout=60 ; Add timeout values
rtpholdtimeout=300
rtpkeepalive=15
here is the full code of RTP Handler
const dgram = require('dgram');
const WebSocket = require('ws');
const MULAW_DECODE_TABLE = new Int16Array([
-32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
-23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
-15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
-11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316,
-7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
-5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
-3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
-2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
-1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
-1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
-876, -844, -812, -780, -748, -716, -684, -652,
-620, -588, -556, -524, -492, -460, -428, -396,
-372, -356, -340, -324, -308, -292, -276, -260,
-244, -228, -212, -196, -180, -164, -148, -132,
-120, -112, -104, -96, -88, -80, -72, -64,
-56, -48, -40, -32, -24, -16, -8, 0,
32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
876, 844, 812, 780, 748, 716, 684, 652,
620, 588, 556, 524, 492, 460, 428, 396,
372, 356, 340, 324, 308, 292, 276, 260,
244, 228, 212, 196, 180, 164, 148, 132,
120, 112, 104, 96, 88, 80, 72, 64,
56, 48, 40, 32, 24, 16, 8, 0
]);
class RTPHandler {
constructor() {
this.server = dgram.createSocket('udp4');
this.server1 = dgram.createSocket('udp4');
this.activeChannels = new Map(); // Map to store channel-websocket pairs
this.streamSid = null;
this.sequenceNumber = 0;
this.startTime = null; // Add this
this.setupRTPServer();
}
setStreamSid(streamSid) {
this.streamSid = streamSid;
}
setupRTPServer() {
this.server.on('listening', () => {
const address = this.server.address();
console.log(`RTP server listening on ${address.address}:${address.port}`);
});
this.server.on('message', (msg, rinfo) => {
// console.log(`Received RTP packet from ${rinfo.address}:${rinfo.port}`);
this.handleRTPPacket(msg);
});
this.server.bind(3001);
this.server1.on('listening', () => {
const address = this.server1.address();
console.log(`RTP server listening on ${address.address}:${address.port}`);
});
this.server1.on('message', (msg, rinfo) => {
// console.log(`Received RTP packet from ${rinfo.address}:${rinfo.port}`);
// this.handleRTPPacket(msg);
});
this.server1.bind(10001);
}
handleRTPPacket(rtpPacket) {
try {
// Extract RTP header (first 12 bytes)
const rtpVersion = (rtpPacket[0] >> 6) & 0x03;
const payloadType = rtpPacket[1] & 0x7f;
const sequenceNumber = (rtpPacket[2] << 8) | rtpPacket[3];
const timestamp = (rtpPacket[4] << 24) | (rtpPacket[5] << 16) |
(rtpPacket[6] << 8) | rtpPacket[7];
// Extract payload (audio data) - skip RTP header
const payload = rtpPacket.slice(12);
// Send to all active WebSocket connections
this.activeChannels.forEach((ws, channelId) => {
if (ws.readyState === WebSocket.OPEN) {
const message = {
event: 'media',
channelId: channelId,
streamSid: this.streamSid,
media: {
format: 'ulaw',
timestamp: timestamp,
sequenceNumber: sequenceNumber,
payload: payload.toString('base64')
}
};
ws.send(JSON.stringify(message));
}
});
} catch (error) {
console.error('Error processing RTP packet:', error);
}
}
async sendAudio(channelId, audioPayload) {
const channelInfo = this.activeChannels.get(channelId);
if (!channelInfo) {
console.error(`No channel info found for ${channelId}`);
return;
}
// Get RTP port from channel info
const rtpPort = channelInfo.rtpPort || 10001; // Default to start of Asterisk RTP range
const rtpHost = channelInfo.rtpHost || '127.0.0.1';
try {
let audioBuffer;
audioBuffer = Buffer.from(audioPayload, 'base64');
const CHUNK_SIZE = 160;
let packetsReceived = 0;
console.log('Starting RTP transmission:', {
totalSize: audioBuffer.length,
numberOfChunks: Math.ceil(audioBuffer.length / CHUNK_SIZE),
rtpPort,
rtpHost
});
for (let offset = 0; offset < audioBuffer.length; offset += CHUNK_SIZE) {
let chunk = audioBuffer.slice(offset, offset + CHUNK_SIZE);
if (chunk.length < CHUNK_SIZE) {
const padding = Buffer.alloc(CHUNK_SIZE - chunk.length, 0x7F);
chunk = Buffer.concat([chunk, padding]);
}
const rtpHeader = Buffer.alloc(12);
rtpHeader[0] = 0x80;
rtpHeader[1] = 0x00;
rtpHeader.writeUInt16BE(channelInfo.sequenceNumber, 2);
rtpHeader.writeUInt32BE(channelInfo.timestamp, 4);
rtpHeader.writeUInt32BE(channelInfo.ssrc, 8);
const rtpPacket = Buffer.concat([rtpHeader, chunk]);
const sendPromise = new Promise((resolve, reject) => {
this.server1.send(rtpPacket, 0, rtpPacket.length, rtpPort, rtpHost, (err) => {
if (err) {
console.error('Failed to send RTP packet:', err);
reject(err);
} else {
packetsReceived++;
if (channelInfo.sequenceNumber % 5 === 0) {
console.log('RTP packet sent:', {
sequenceNumber: channelInfo.sequenceNumber,
timestamp: channelInfo.timestamp,
packetSize: rtpPacket.length,
rtpPort,
rtpHost,
firstPayloadByte: chunk[0],
lastPayloadByte: chunk[chunk.length - 1]
});
}
resolve();
}
});
});
await Promise.all([
sendPromise,
new Promise(resolve => setTimeout(resolve, 20))
]);
channelInfo.sequenceNumber = (channelInfo.sequenceNumber + 1) & 0xFFFF;
channelInfo.timestamp = (channelInfo.timestamp + CHUNK_SIZE) & 0xFFFFFFFF;
}
console.log('RTP transmission complete:', {
packetsReceived,
expectedPackets: Math.ceil(audioBuffer.length / CHUNK_SIZE),
finalSequenceNumber: channelInfo.sequenceNumber,
finalTimestamp: channelInfo.timestamp,
rtpPort,
rtpHost
});
} catch (error) {
console.error(`Error in sendAudio:`, error);
}
}
// Method to register a new channel and its WebSocket
registerChannel(channelId, webSocket) {
console.log(`Registering channel ${channelId} for RTP handling`);
this.activeChannels.set(channelId, webSocket);
}
// Method to unregister a channel
unregisterChannel(channelId) {
console.log(`Unregistering channel ${channelId} from RTP handling`);
this.activeChannels.delete(channelId);
// this.cleanup();
}
// Cleanup method
cleanup() {
try{
this.server.close();
this.server1.close();
} catch(err) {
console.log("RTP Cleanup error", err);
}
this.activeChannels.clear();
}
}
module.exports = RTPHandler;
I am generating audio like this
const response = await fetch(
`https://api.deepgram.com/v1/speak?model=${voice_name.toLowerCase()}&encoding=mulaw&sample_rate=8000&container=none`,
{
method: "POST",
headers: {
Authorization: `Token ${process.env.DEEPGRAM_API_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
text: cleanString(partialResponse)
}),
encoding: "mulaw"
}
);
const audioArrayBuffer = await response.arrayBuffer();
this.emit(
"speech",
partialResponseIndex,
Buffer.from(audioArrayBuffer).toString("base64"),
partialResponse,
interactionCount
);
the same audio is working fine with Twilio but not with asterisk external media.