-
Notifications
You must be signed in to change notification settings - Fork 163
/
server.js
34 lines (27 loc) · 980 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
34
'use strict';
const RTCAudioSourceSineWave = require('../../lib/server/webrtc/rtcaudiosourcesinewave');
function beforeOffer(peerConnection) {
const source = new RTCAudioSourceSineWave({
channelCount: 2,
panning: 50
});
const track = source.createTrack();
peerConnection.addTrack(track);
const dataChannel = peerConnection.createDataChannel('panning');
function onMessage({ data }) {
const panning = Number.parseFloat(data);
source.setPanning(panning);
}
dataChannel.addEventListener('message', onMessage);
// 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() {
dataChannel.removeEventListener('message', onMessage);
track.stop();
source.close();
return close.apply(this, arguments);
};
}
module.exports = { beforeOffer };