Skip to content

Instantly share code, notes, and snippets.

@philhartung
Last active February 13, 2022 06:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philhartung/dc5799e8a81520f958089d8c33fa8b20 to your computer and use it in GitHub Desktop.
Save philhartung/dc5799e8a81520f958089d8c33fa8b20 to your computer and use it in GitHub Desktop.
First draft of AES67 RTP timestamp analyzer tool. Prints RTP to PTP timestamp difference min/avg/max in console
const dgram = require('dgram');
const chalk = require('chalk');
const ptpv2 = require('ptpv2');
let interface = '10.0.0.12';
//let multicastAddr = '239.69.161.58';
let multicastAddr = '239.69.80.114';
let samplerate = 48000;
let client = dgram.createSocket({ type: 'udp4', reuseAddr: true });
let total = 0;
let count = 0;
let min = 100000;
let max = -100000;
let seqErrors = 0;
let tsErrors = 0;
let prevTimestamp = 0;
let prevSeqNum = 0;
client.on('listening', function() {
client.addMembership(multicastAddr, interface);
});
client.on('message', function(buffer, remote) {
let ptpTime = ptpv2.ptp_time();
let timestampCalc = ((ptpTime[0] * samplerate) + Math.round((ptpTime[1] * samplerate) / 1000000000)) % 0x100000000;
let timestampBuffer = buffer.readUInt32BE(4);
let seqNum = buffer.readUInt16BE(2);
let diff = timestampCalc - timestampBuffer;
total += diff;
if(diff < min){
min = diff;
}
if(diff > max){
max = diff;
}
if(seqNum != ((prevSeqNum + 1) % 0x10000)){
seqErrors++;
}
if(timestampBuffer != prevTimestamp + 48){
tsErrors++;
}
prevSeqNum = seqNum;
prevTimestamp = timestampBuffer;
count++;
});
ptpv2.init(interface, 0, function(){
client.bind(5004);
});
setInterval(function(){
if(count != 0){
var avg = Math.round(total / count * 100) / 100;
var diff = max - min;
var diffMS = Math.round(diff / 48 * 1000) / 1000;
var minStr = (Math.round(min / 48 * 1000) / 1000)+'ms';
var maxStr = (Math.round(max / 48 * 1000) / 1000)+'ms';
var avgStr = (Math.round(avg / 48 * 1000) / 1000)+'ms';
if(diff > 2*48){
console.log(chalk.red('min/avg/max (diff) =', minStr+'/'+avgStr+'/'+maxStr+' ('+diffMS+'ms)'));
}else if(max > 2*48 || min < 0){
console.log(chalk.yellow('min/avg/max (diff) =', minStr+'/'+avgStr+'/'+maxStr+' ('+diffMS+'ms)'));
}else{
console.log(chalk.green('min/avg/max (diff) =', minStr+'/'+avgStr+'/'+maxStr+' ('+diffMS+'ms)'));
}
}else{
console.log('0 packets received, RTP stream not running');
}
if(seqErrors + tsErrors > 0){
console.log(chalk.red('seq/ts errors = '+seqErrors+' / '+tsErrors));
}
total = 0;
count = 0;
min = 100000;
max = -100000;
seqErrors = 0;
tsErrors = 0;
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment