Skip to content

Instantly share code, notes, and snippets.

@philhartung
Last active August 23, 2022 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philhartung/e927e0c480f5e2f7e4ff4e07d0361506 to your computer and use it in GitHub Desktop.
Save philhartung/e927e0c480f5e2f7e4ff4e07d0361506 to your computer and use it in GitHub Desktop.
Easily multiply an AES67 Stream for load testing. SDP has to be adjusted accordingly
const dgram = require('dgram');
const process = require('process');
const sdp = require('./sdp');
let client = dgram.createSocket({ type: 'udp4', reuseAddr: true });
let clients = [];
if(!process.argv[2] || !(parseInt(process.argv[2]) > 0)){
console.log('$ node multiply 12');
process.exit();
}
for(var i = 0; i < parseInt(process.argv[2]); i++){
let mcast = '239.69.'+(Math.floor(Math.random() * 256))+'.'+(Math.floor(Math.random() * 256));
clients[i] = {
mcast: mcast,
dgram: dgram.createSocket('udp4')
};
clients[i].dgram.on('listening', function() {
this.setMulticastInterface('192.168.1.100');
});
clients[i].dgram.bind();
sdp.add(mcast);
}
client.on('listening', function() {
client.setMulticastInterface('192.168.1.100');
client.addMembership('239.69.161.174', '192.168.1.100');
});
client.on('message', function(buffer, remote) {
for(var i = 0; i < clients.length; i++){
clients[i].dgram.send(buffer, 5004, clients[i].mcast);
}
});
setTimeout(function(){
client.bind(5004);
}, 1000);
var dgram = require('dgram');
var socket = dgram.createSocket({ type: 'udp4', reuseAddr: true });
let ipAddress = '192.168.1.100';
var constructSDPMsg = function(multicastAddr, number){
let hash = Math.floor(Math.random() * 65536);
let id = Math.floor(Math.random() * 2*65536);
let sapHeader = Buffer.alloc(8);
let sapContentType = Buffer.from('application/sdp\0');
let ip = ipAddress.split('.');
//write version/options
sapHeader.writeUInt8(0x20);
//write hash
sapHeader.writeUInt16LE(hash, 2);
//write ip
sapHeader.writeUInt8(parseInt(ip[0]), 4);
sapHeader.writeUInt8(parseInt(ip[1]), 5);
sapHeader.writeUInt8(parseInt(ip[2]), 6);
sapHeader.writeUInt8(parseInt(ip[3]), 7);
var sdpConfig = [
'v=0',
'o=- '+id+' '+id+' IN IP4 '+ipAddress,
's= Worker #'+number,
'c=IN IP4 '+multicastAddr+'/32',
't=0 0',
'm=audio 5004 RTP/AVP 97',
'i=2 channels: Left, Right',
'a=recvonly',
'a=rtpmap:97 L24/48000/2',
'a=ptime:1',
'a=ts-refclk:ptp=IEEE1588-2008:00-1D-C1-FF-FE-51-D7-EB:0',
'a=mediaclk:direct=2578788582',
''
];
var sdpBody = Buffer.from(sdpConfig.join('\r\n'));
return Buffer.concat([sapHeader, sapContentType, sdpBody]);
}
let broadcast = function(){
for(let i = 0; i < sdps.length; i++){
socket.send(sdps[i], 9875, '239.255.255.255', function(err){});
}
}
exports.start = function(){
socket.bind(9875, function(){
socket.setMulticastInterface(ipAddress);
});
}
let sdps = [];
exports.add = function(mcast){
console.log(mcast);
let sdpBuffer = constructSDPMsg(mcast, sdps.length + 1);
sdps.push(sdpBuffer);
socket.send(sdpBuffer, 9875, '239.255.255.255', function(err){});
}
process.on('SIGINT', function(code){
for(let i = 0; i < sdps.length; i++){
sdps[i].writeUInt8(0x24, 0);
socket.send(sdps[i], 9875, '239.255.255.255', function(err){});
}
setTimeout(function(){
process.exit();
}, 100);
});
setInterval(broadcast, 5*1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment