-
Notifications
You must be signed in to change notification settings - Fork 1
/
aes67-sender-enhanced.js
349 lines (311 loc) · 11 KB
/
aes67-sender-enhanced.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// execute with realtime scheduling
// sudo chrt -f 99 node aes67 --api ALSA -d 3
const os = require('os');
const ptpv2 = require('ptpv2');
const dgram = require('dgram');
const sdp = require('./lib/sdp');
const util = require('./lib/util');
const { Command } = require('commander');
const { RtAudio, RtAudioFormat, RtAudioApi } = require('audify');
//init udp client
const client = dgram.createSocket('udp4');
//command line options
const program = new Command();
program.version('1.0');
program.option('-v, --verbose', 'Enable verbosity');
program.option('--devices', 'List audio devices');
program.option('-d, --device <index>', 'Which audio device to use. Use --devices to see a list.');
program.option('-m, --mcast <address>', 'First address to multicast the AES67 stream. Leave space for more addresses if you plan to stream more than 8 channels.');
program.option('-n, --streamname <name>', 'Name of AES67 stream(s)');
program.option('-c, --channels <number>', 'Number of Channels');
program.option('-p, --patch <list>', 'Channel Map from the input device, in the order you would like them, separated by commas (i.e. 1,2,3,5,7,8,12, etc)');
program.option('-a, --api <api>', 'Audio API to use (ALSA, OSS, PULSE, JACK, MACOS, ASIO, DS, WASAPI)');
program.option('--address <address>', 'IPv4 address of network interface to use (should be a wired interface)');
program.parse(process.argv);
let logger = function(){};
if(program.verbose){
logger = console.log;
}
//rtAudio API Connect
let rtAudio;
if(program.api){
switch(program.api.toLowerCase()){
case 'alsa':
rtAudio = new RtAudio(RtAudioApi.LINUX_ALSA);
break;
case 'oss':
rtAudio = new RtAudio(RtAudioApi.LINUX_OSS);
break;
case 'pulse':
rtAudio = new RtAudio(RtAudioApi.LINUX_PULSE);
break;
case 'jack':
rtAudio = new RtAudio(RtAudioApi.UNIX_JACK);
break;
case 'macos':
rtAudio = new RtAudio(RtAudioApi.MACOSX_CORE);
break;
case 'asio':
rtAudio = new RtAudio(RtAudioApi.WINDOWS_ASIO);
break;
case 'ds':
rtAudio = new RtAudio(RtAudioApi.WINDOWS_DS);
break;
case 'wasapi':
rtAudio = new RtAudio(RtAudioApi.WINDOWS_WASAPI);
break;
default:
rtAudio = new RtAudio();
}
}else{
rtAudio = new RtAudio();
}
logger('Selected',rtAudio.getApi(),'as audio API.');
// list audio devices when requested on command line
let audioDevices = rtAudio.getDevices();
if(program.devices){
console.log('Device #, Name, # of Channels');
for(let i = 0; i < audioDevices.length; i++){
let device = audioDevices[i];
if(device.inputChannels > 0){
console.log(i, device.name, device.inputChannels);
}
}
process.exit();
}
// *** ASSIGN NETWORK ADDRESS
let addr;
if(program.address) {
if (util.validateIPAddress(program.address)) {
addr = program.address;
}
else {
console.error('That is an invalid IP address.');
process.exit();
}
}
else {
let interfaces = os.networkInterfaces();
let interfaceNames = Object.keys(interfaces);
let addresses = [];
for(let i = 0; i < interfaceNames.length; i++){
let interface = interfaces[interfaceNames[i]];
for(let j = 0; j < interface.length; j++){
if(interface[j].family == 'IPv4' && interface[j].address != '127.0.0.1'){
addresses.push(interface[j].address);
}
}
}
if(addresses.length == 0) {
console.error('No network interface found!');
process.exit();
}
addr = addresses[0];
}
logger('Selected',addr ,'as network interface');
// *** ASSIGN AUDIO DEVICE
let audioDevice = rtAudio.getDefaultInputDevice();
if (program.device) {
audioDevice = parseInt(program.device);
}
// set up the number of channels to process and a list of those channels, based on different situations
var channelMap = [];
var highChannel = 0;
if (program.channels && program.patch) { // the user specified both a number of channels and a patch list
let userPatchList = program.patch.split(',');
for (let i=0; i < userPatchList.length; i++) {
channelMap.push(userPatchList[i] - 1); // this assumes the user thinks of their first channnel as "1" rather than "0"
}
if (channelMap.length != program.channels) {
console.log('The channel patching list does not match the number of channels specified.');
process.exit();
}
}
else if (!program.channels && program.patch) { // user just specified a patch list, we can count the channels from that
let userPatchList = program.patch.split(',');
for (let i=0; i < userPatchList.length; i++) {
channelMap.push(userPatchList[i] - 1);
}
program.channels = channelMap.length;
}
else if (program.channels && !program.patch) { // the user just wants the channels in order starting from zero
for (let i=0; i < program.channels; i++) {
channelMap.push(i);
}
}
else {
// the user didn't specify a patch list or a number of channels, assume 2
channelMap = [0,1];
program.channels = 2;
}
highChannel = Math.max(...channelMap) + 1; // for the rtAudio constructor
logger('Channel input map from the sound card is', channelMap);
const aesFlowChans = []; // place to store the number of channels per flow
let selectedDevice = audioDevices[audioDevice];
let audioChannels;
if(selectedDevice && selectedDevice.inputChannels > 0) {
logger('Selected device', selectedDevice.name, 'with ', selectedDevice.inputChannels, ' input channels');
logger('\nWe are trying to put ', channelMap.length, ' channels on the network');
if (channelMap.length > selectedDevice.inputChannels) {
console.error('This device doesn\'t have that many input channels!');
process.exit();
}
audioChannels = parseInt(channelMap.length);
let numAES67Flows = Math.floor(audioChannels / 8);
if (audioChannels % 8 > 0) {
numAES67Flows++;
}
logger('This will require ', numAES67Flows, ' AES67 flow(s).\n');
let remainingChannels = audioChannels;
for (let i=0; i < numAES67Flows; i++) {
if (remainingChannels % 8 > 0 && remainingChannels / 8 < 1) {
aesFlowChans[i] = remainingChannels % 8;
}
else {
aesFlowChans[i] = 8;
}
remainingChannels = remainingChannels - aesFlowChans[i];
}
}
else {
console.error('Invalid audio device!');
process.exit();
}
// multicast addresses and flow names
const aes67Multicast = [];
const aes67FlowNames = [];
let ipABCD = [];
if (program.mcast) {
ipABCD = program.mcast.split('.');
}
else {
ipABCD = ['239','69','1','1']; // this is the subnet that AES67 likes
}
let baseMCastAddr = ipABCD[3];
let streamName;
if (program.streamname) {
streamName = program.streamname;
}
else {
streamName = os.hostname();
}
for (let i = 0; i < aesFlowChans.length; i++) {
aes67Multicast[i] = ipABCD[0] + '.' + ipABCD[1] + '.' + ipABCD[2] + "." + baseMCastAddr++;
aes67FlowNames[i] = streamName + '-Bank-' + (i + 1);
}
logger('Selected the following MultiCast Addresses: ', aes67Multicast);
logger('Selected the following names for the AES67 flows: ', aes67FlowNames);
// AES67 params (hardcoded)
const samplerate = 48000;
const ptime = 1;
const fpp = (samplerate / 1000) * ptime;
const encoding = 'L24';
let sessID = Math.round(Date.now() / 1000);
const sessVersion = sessID;
let ptpMaster;
//rtp vars
let seqNum = 0;
let timestampCalc = 0;
let ssrc = sessID % 0x100000000;
//timestamp offset stuff
let offsetSum = 0;
let count = 0;
let correctTimestamp = true;
//open audio stream
logger('Opening audio stream.');
rtAudio.openStream(null, {deviceId: audioDevice, nChannels: highChannel, firstChannel: 0}, RtAudioFormat.RTAUDIO_FLOAT32, samplerate, fpp, streamName, pcm => rtpSend(pcm));
logger('Trying to sync to PTP leader.');
// ptp sync timeout - 10 seconds
setTimeout(function() {
if(!ptpMaster) {
console.error('Could not sync to PTP leader. Aborting.');
process.exit();
}
}, 10000);
logger('Initializing PTP client');
//init PTP client
ptpv2.init(addr, 0, function(){
ptpMaster = ptpv2.ptp_master();
logger('Synced to', ptpMaster, 'successfully');
//start audio and sdp
logger('Starting SAP annoucements and audio stream.');
rtAudio.start();
sdp.start(addr, aes67Multicast, samplerate, aesFlowChans, encoding, aes67FlowNames, sessID, sessVersion, ptpMaster);
});
// *** PROCESS PCM DATA AND SEND TO THE NETWORK
let floatArray = [];
let outputArray = [];
let rtpSend = function(pcm){
floatArray.length = 0;
let chanOffset = 0;
for(j = 0; j < aesFlowChans.length; j++) {
// aesFlowChans[j] is the number of channels we need to deal with in AES67 flow
// the 0th time = channels 0-7
// the 1th time = channels 8-15
// the 2th time = channels 16-23
// the 3th time = channels 24-31 etc.
// fpp is the number of frames per packet
//convert 32f to L24 and populate an interleaved buffer, 3 bytes per sample
let l24 = Buffer.alloc(aesFlowChans[j] * fpp * 3);
for(let i = 0; i < fpp; i++) { // keep track of which frame we're on
let frameOffset = i * (highChannel * 4);
for (let k = 0; k < aesFlowChans[j]; k++) { // keep track of which channel we're on
let channelOffset = channelMap[k + (j * 8)] * 4;
let samp32 = pcm.readFloatLE(frameOffset + channelOffset);
let samp24 = ~~(samp32 * 8388607); // scale to the 24-bit data range for audio, no decimals
// convert the 24-bit sample value to big endian and shove it onto the outgoing PCM data
let outputBuffOffset = (i * aesFlowChans[j] * 3) + (k * 3);
l24[outputBuffOffset] = (samp24 & 0xff0000) >>> 16;
l24[outputBuffOffset + 1] = (samp24 & 0x00ff00) >>> 8;
l24[outputBuffOffset + 2] = samp24 & 0x0000ff;
}
}
//create RTP header and RTP buffer with header and pcm data
let rtpHeader = Buffer.alloc(12);
rtpHeader.writeUInt16BE((1 << 15) + 96, 0);// set version byte and add rtp payload type
rtpHeader.writeUInt16BE(seqNum, 2);
rtpHeader.writeUInt32BE(ssrc, 8);
let rtpBuffer = Buffer.concat([rtpHeader, l24]);
// timestamp correction stuff
if(correctTimestamp){
correctTimestamp = false;
let ptpTime = ptpv2.ptp_time();
let timestampRTP = ((ptpTime[0] * samplerate) + Math.round((ptpTime[1] * samplerate) / 1000000000)) % 0x100000000;
timestampCalc = Math.floor(timestampRTP / fpp)*fpp;
}
//write timestamp
rtpBuffer.writeUInt32BE(timestampCalc, 4);
//send RTP packet
client.send(rtpBuffer, 5004, aes67Multicast[j]);
}
//timestamp average stuff
let ptpTime = ptpv2.ptp_time();
let timestampRTP = ((ptpTime[0] * samplerate) + Math.round((ptpTime[1] * samplerate) / 1000000000)) % 0x100000000;
offsetSum += Math.abs(timestampRTP - timestampCalc);
count++;
// increase timestamp and seqnum
seqNum = (seqNum + 1) % 0x10000;
timestampCalc = (timestampCalc + fpp) % 0x100000000;
}
// Interval for timestamp correction calculation
// the original aes67.js by Phil Hartung did this every 100ms, but that seems too often, changed to 1 sec
setInterval(function(){
let avg = Math.round(offsetSum / count);
if(avg > fpp){
correctTimestamp = true;
let offsetMS = Math.round(avg / fpp * 1000) / 1000;
logger('Resycing PTP and RTP timestamp. Offset was '+offsetMS+'ms.');
}
offsetSum = 0;
count = 0;
}, 1000);
// *** CATCH TERMINATION SIGNALS
['SIGINT', 'SIGTERM', 'SIGQUIT']
.forEach(signal => process.on(signal, () => {
logger('Stopping SDP announcements...');
sdp.stop();
logger('Stopping Audio Streams...');
rtAudio.stop();
rtAudio.closeStream();
process.exit();
}));