-
Notifications
You must be signed in to change notification settings - Fork 161
/
server.js
33 lines (25 loc) · 976 Bytes
/
server.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
'use strict';
const { RTCAudioSink } = require('wrtc').nonstandard;
const PitchDetector = require('../../lib/common/pitchdetector');
function beforeOffer(peerConnection) {
const { track } = peerConnection.addTransceiver('audio').receiver;
const sink = new RTCAudioSink(track);
const pitchDetector = new PitchDetector();
const dataChannel = peerConnection.createDataChannel('frequency');
function onData(data) {
const frequency = pitchDetector.onData(data);
if (frequency && dataChannel.readyState === 'open') {
dataChannel.send(JSON.stringify(frequency));
}
}
sink.ondata = onData;
// NOTE(mroberts): This is a hack so that we can get a callback when the
// RTCPeerConnection is closed. In the future, we can subscribe to
// "connectionstatechange" events.
const { close } = peerConnection;
peerConnection.close = function() {
sink.stop();
return close.apply(this, arguments);
};
}
module.exports = { beforeOffer };