speaker-calibration 2.2.221 → 2.2.222
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +71 -71
- package/.eslintrc.json +40 -40
- package/.github/workflows/update-phrases.yml +37 -0
- package/.prettierignore +69 -69
- package/.prettierrc +14 -14
- package/LICENSE +20 -20
- package/README.md +133 -133
- package/__mocks__/fileMock.js +1 -1
- package/__mocks__/styleMock.js +1 -1
- package/babel.config.js +3 -3
- package/coverage/clover.xml +71 -71
- package/coverage/coverage-final.json +224 -224
- package/coverage/lcov-report/PythonServerInterface.js.html +265 -265
- package/coverage/lcov-report/base.css +354 -354
- package/coverage/lcov-report/block-navigation.js +82 -82
- package/coverage/lcov-report/index.html +123 -123
- package/coverage/lcov-report/prettify.css +101 -101
- package/coverage/lcov-report/prettify.js +937 -937
- package/coverage/lcov-report/sorter.js +189 -189
- package/coverage/lcov-report/src/index.html +121 -121
- package/coverage/lcov-report/src/server/PythonServerInterface.js.html +268 -268
- package/coverage/lcov-report/src/server/index.html +123 -123
- package/coverage/lcov-report/src/tasks/audioCalibrator.js.html +499 -499
- package/coverage/lcov-report/src/tasks/audioRecorder.js.html +412 -412
- package/coverage/lcov-report/src/tasks/index.html +143 -143
- package/coverage/lcov-report/src/tasks/volume/index.html +123 -123
- package/coverage/lcov-report/src/tasks/volume/volume.js.html +409 -409
- package/coverage/lcov-report/src/utils.js.html +172 -172
- package/coverage/lcov.info +91 -91
- package/dist/example/NoSleep.min.js +1 -1
- package/dist/example/fetch-languages-sheets.js +77 -77
- package/dist/example/i18n.js +29082 -29082
- package/dist/example/index.html +47 -47
- package/dist/example/listener.html +81 -81
- package/dist/example/server.js +51 -51
- package/dist/example/speaker.html +145 -145
- package/dist/example/speakerUI.js +273 -273
- package/dist/example/styles.css +152 -152
- package/dist/listener.js +3 -3
- package/dist/main.js +11 -11
- package/dist/mlsGen.js +6814 -6814
- package/dist/mlsGen.wasm +0 -0
- package/dist/package-lock.json +1018 -1018
- package/dist/package.json +18 -18
- package/doc/AudioCalibrator.html +417 -417
- package/doc/AudioPeer.html +251 -251
- package/doc/AudioRecorder.html +195 -195
- package/doc/ImpulseResponse.html +215 -215
- package/doc/Listener.html +308 -308
- package/doc/MlsGenInterface.html +226 -226
- package/doc/MyEventEmitter.html +274 -274
- package/doc/PythonServerAPI.html +109 -109
- package/doc/Speaker.html +276 -276
- package/doc/Takes%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +128 -128
- package/doc/Takes%20the%20url%20of%20the%20current%20site%0Aand%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +138 -138
- package/doc/Takes%20the%20url%20of%20the%20current%20site%20and%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +137 -137
- package/doc/Volume.html +88 -88
- package/doc/audioCalibrator.js.html +179 -179
- package/doc/audioPeer.js.html +175 -175
- package/doc/audioRecorder.js.html +163 -163
- package/doc/creates%20a%20new%20AudioRecorder%20instance.%20%0ASets%20up%20the%20audio%20context%20and%20file%20reader..html +114 -114
- package/doc/fonts/OpenSans-Bold-webfont.svg +1829 -1829
- package/doc/fonts/OpenSans-BoldItalic-webfont.svg +1829 -1829
- package/doc/fonts/OpenSans-Italic-webfont.svg +1829 -1829
- package/doc/fonts/OpenSans-Light-webfont.svg +1830 -1830
- package/doc/fonts/OpenSans-LightItalic-webfont.svg +1834 -1834
- package/doc/fonts/OpenSans-Regular-webfont.svg +1830 -1830
- package/doc/global.html +308 -308
- package/doc/index.html +58 -58
- package/doc/listener.js.html +170 -170
- package/doc/mlsGen_mlsGenInterface.js.html +117 -117
- package/doc/myEventEmitter.js.html +124 -124
- package/doc/peer-connection_audioPeer.js.html +188 -188
- package/doc/peer-connection_listener.js.html +311 -311
- package/doc/peer-connection_speaker.js.html +381 -381
- package/doc/scripts/linenumber.js +25 -25
- package/doc/scripts/prettify/Apache-License-2.0.txt +202 -202
- package/doc/scripts/prettify/lang-css.js +24 -24
- package/doc/scripts/prettify/prettify.js +640 -640
- package/doc/server_PythonServerAPI.js.html +160 -160
- package/doc/speaker.js.html +248 -248
- package/doc/styles/jsdoc-default.css +371 -371
- package/doc/styles/prettify-jsdoc.css +111 -111
- package/doc/styles/prettify-tomorrow.css +163 -163
- package/doc/tasks_audioCalibrator.js.html +207 -207
- package/doc/tasks_audioRecorder.js.html +190 -190
- package/doc/tasks_impulse-response_impulseResponse.js.html +442 -442
- package/doc/tasks_impulse-response_mlsGen_mlsGenInterface.js.html +175 -175
- package/doc/tasks_volume_volume.js.html +185 -185
- package/doc/utils.js.html +105 -105
- package/jest.config.js +173 -173
- package/makefile +74 -0
- package/netlify.toml +26 -26
- package/package.json +78 -78
- package/src/config/firebase.js +26 -26
- package/src/index.html +21 -21
- package/src/listener-app/listener.js +377 -377
- package/src/main.js +22 -22
- package/src/myEventEmitter.js +83 -83
- package/src/peer-connection/audioPeer.js +148 -148
- package/src/peer-connection/listener.js +467 -467
- package/src/peer-connection/peerErrors.js +25 -25
- package/src/peer-connection/speaker.js +818 -812
- package/src/powerCheck.js +98 -98
- package/src/server/PythonServerAPI.js +869 -869
- package/src/tasks/audioCalibrator.js +360 -360
- package/src/tasks/audioRecorder.js +315 -315
- package/src/tasks/combination/combination.js +3171 -3171
- package/src/tasks/combination/mlsGen/mlsGen.cpp +98 -98
- package/src/tasks/combination/mlsGen/mlsGen.hpp +303 -303
- package/src/tasks/combination/mlsGen/mlsGenInterface.js +131 -131
- package/src/tasks/combination/mlsGen/mlsGenTest.cpp +180 -180
- package/src/tasks/impulse-response/impulseResponse.js +610 -610
- package/src/tasks/impulse-response/mlsGen/mlsGen.cpp +98 -98
- package/src/tasks/impulse-response/mlsGen/mlsGen.hpp +303 -303
- package/src/tasks/impulse-response/mlsGen/mlsGenInterface.js +131 -131
- package/src/tasks/impulse-response/mlsGen/mlsGenTest.cpp +180 -180
- package/src/tasks/volume/volume.cpp +2 -2
- package/src/tasks/volume/volume.hpp +22 -22
- package/src/tasks/volume/volume.js +279 -279
- package/src/utils.js +205 -205
- package/webpack.config.js +64 -64
- package/.gitignore +0 -81
|
@@ -1,467 +1,467 @@
|
|
|
1
|
-
import AudioPeer from './audioPeer';
|
|
2
|
-
import {UnsupportedDeviceError, MissingSpeakerIdError} from './peerErrors';
|
|
3
|
-
import axios from 'axios';
|
|
4
|
-
import Peer from 'peerjs';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @class Handles the listener's side of the connection. Responsible for getting access to user's microphone,
|
|
8
|
-
* and initiating a call to the Speaker.
|
|
9
|
-
* @augments AudioPeer
|
|
10
|
-
*/
|
|
11
|
-
class Listener extends AudioPeer {
|
|
12
|
-
/**
|
|
13
|
-
* Takes a target element where html elements will be appended.
|
|
14
|
-
*
|
|
15
|
-
* @param params - See type definition for initParameters.
|
|
16
|
-
* @example
|
|
17
|
-
*/
|
|
18
|
-
constructor(params) {
|
|
19
|
-
super(params);
|
|
20
|
-
console.log('Listener constructor', this.peer);
|
|
21
|
-
this.microphoneFromAPI = params.microphoneFromAPI ? params.microphoneFromAPI : '';
|
|
22
|
-
this.microphoneDeviceId = params.microphoneDeviceId ? params.microphoneDeviceId : '';
|
|
23
|
-
// this.deviceInfoFromUser = params.deviceInfoFromUser
|
|
24
|
-
// ? params.deviceInfoFromUser
|
|
25
|
-
// : {modelNumber: '', modelName: ''};
|
|
26
|
-
this.startTime = Date.now();
|
|
27
|
-
this.receiverPeerId = null;
|
|
28
|
-
|
|
29
|
-
const urlParameters = this.parseURLSearchParams();
|
|
30
|
-
this.calibrateSoundHz =
|
|
31
|
-
// previous calibrateSoundHz
|
|
32
|
-
urlParameters.hz !== null && urlParameters.hz !== undefined ? urlParameters.hz : 48000;
|
|
33
|
-
this.calibrateSoundSamplingDesiredBits =
|
|
34
|
-
// previous calibrateSoundSamplingDesiredBits
|
|
35
|
-
urlParameters.bits !== null && urlParameters.bits !== undefined ? urlParameters.bits : 24;
|
|
36
|
-
this.speakerPeerId = urlParameters.speakerPeerId;
|
|
37
|
-
this.lastPeerId = this.speakerPeerId;
|
|
38
|
-
this.connOpen = false;
|
|
39
|
-
}
|
|
40
|
-
generateTimeBasedPeerID = async () => {
|
|
41
|
-
const now = new Date().getTime();
|
|
42
|
-
const randomBuffer = new Uint8Array(10);
|
|
43
|
-
crypto.getRandomValues(randomBuffer);
|
|
44
|
-
const randomPart = Array.from(randomBuffer)
|
|
45
|
-
.map(b => b.toString(36))
|
|
46
|
-
.join('');
|
|
47
|
-
const toHash = `${now}-${randomPart}`;
|
|
48
|
-
const encoder = new TextEncoder();
|
|
49
|
-
const data = encoder.encode(toHash);
|
|
50
|
-
const hash = await crypto.subtle.digest('SHA-256', data);
|
|
51
|
-
const hashArray = Array.from(new Uint8Array(hash));
|
|
52
|
-
const hashString = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
|
|
53
|
-
const shortHash = hashString.substring(0, 12);
|
|
54
|
-
return this.encodeBase62(parseInt(shortHash, 16));
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
encodeBase62 = num => {
|
|
58
|
-
const base = 26;
|
|
59
|
-
const characters = 'abcdefghijklmnopqrstuvwxyz';
|
|
60
|
-
let result = '';
|
|
61
|
-
while (num > 0) {
|
|
62
|
-
result = characters[num % base] + result;
|
|
63
|
-
num = Math.floor(num / base);
|
|
64
|
-
}
|
|
65
|
-
return result || 'a';
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
initializePeer = async () => {
|
|
69
|
-
console.log('Initializing PeerJS connection...');
|
|
70
|
-
const id = await this.generateTimeBasedPeerID();
|
|
71
|
-
console.log('Generated Peer ID:', id);
|
|
72
|
-
|
|
73
|
-
try {
|
|
74
|
-
this.peer = new Peer(id, {
|
|
75
|
-
debug: 2,
|
|
76
|
-
host: 'easyeyes-peer-server.herokuapp.com',
|
|
77
|
-
port: 443,
|
|
78
|
-
secure: true,
|
|
79
|
-
config: {
|
|
80
|
-
iceServers: [
|
|
81
|
-
{
|
|
82
|
-
urls: 'stun:stun.relay.metered.ca:80',
|
|
83
|
-
},
|
|
84
|
-
{
|
|
85
|
-
urls: 'turn:global.relay.metered.ca:80',
|
|
86
|
-
username: 'de884cfc34189cdf1a5dd616',
|
|
87
|
-
credential: 'IcOpouU9/TYBmpHU',
|
|
88
|
-
},
|
|
89
|
-
{
|
|
90
|
-
urls: 'turn:global.relay.metered.ca:80?transport=tcp',
|
|
91
|
-
username: 'de884cfc34189cdf1a5dd616',
|
|
92
|
-
credential: 'IcOpouU9/TYBmpHU',
|
|
93
|
-
},
|
|
94
|
-
{
|
|
95
|
-
urls: 'turn:global.relay.metered.ca:443',
|
|
96
|
-
username: 'de884cfc34189cdf1a5dd616',
|
|
97
|
-
credential: 'IcOpouU9/TYBmpHU',
|
|
98
|
-
},
|
|
99
|
-
{
|
|
100
|
-
urls: 'turns:global.relay.metered.ca:443?transport=tcp',
|
|
101
|
-
username: 'de884cfc34189cdf1a5dd616',
|
|
102
|
-
credential: 'IcOpouU9/TYBmpHU',
|
|
103
|
-
},
|
|
104
|
-
],
|
|
105
|
-
},
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
this.peer.on('open', this.onPeerOpen);
|
|
109
|
-
this.peer.on('connection', this.onPeerConnection);
|
|
110
|
-
this.peer.on('disconnected', this.onPeerDisconnected);
|
|
111
|
-
this.peer.on('close', this.onPeerClose);
|
|
112
|
-
this.peer.on('error', this.onPeerError);
|
|
113
|
-
|
|
114
|
-
console.log('Peer object created:', this.peer);
|
|
115
|
-
} catch (error) {
|
|
116
|
-
console.error('Failed to initialize PeerJS:', error);
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
onPeerOpen = id => {
|
|
121
|
-
this.displayUpdate('Listener - onPeerOpen');
|
|
122
|
-
console.log('onPeerOpen: ', id);
|
|
123
|
-
// Workaround for peer.reconnect deleting previous id
|
|
124
|
-
try {
|
|
125
|
-
if (id === null) {
|
|
126
|
-
this.displayUpdate('Received null id from peer open');
|
|
127
|
-
this.peer.id = this.lastPeerId;
|
|
128
|
-
} else {
|
|
129
|
-
this.lastPeerId = this.peer.id;
|
|
130
|
-
}
|
|
131
|
-
} catch (error) {
|
|
132
|
-
console.error('Error in onPeerOpen: ', error);
|
|
133
|
-
}
|
|
134
|
-
this.join();
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
onPeerConnection = connection => {
|
|
138
|
-
this.displayUpdate('Listener - onPeerConnection');
|
|
139
|
-
console.log('onPeerConnection: ', connection);
|
|
140
|
-
// Disallow incoming connections
|
|
141
|
-
connection.on('open', () => {
|
|
142
|
-
connection.send('Sender does not accept incoming connections');
|
|
143
|
-
setTimeout(() => {
|
|
144
|
-
connection.close();
|
|
145
|
-
}, 500);
|
|
146
|
-
});
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
onConnData = data => {
|
|
150
|
-
this.displayUpdate('Listener - onConnData');
|
|
151
|
-
const hasSpeakerID = Object.prototype.hasOwnProperty.call(data, 'speakerPeerId');
|
|
152
|
-
// if (!hasSpeakerID) {
|
|
153
|
-
// this.displayUpdate('Error in parsing data received! Must set "speakerPeerId" property');
|
|
154
|
-
// throw new MissingSpeakerIdError('Must set "speakerPeerId" property');
|
|
155
|
-
// } else {
|
|
156
|
-
// // this.conn.close();
|
|
157
|
-
// this.displayUpdate(this.speakerPeerId);
|
|
158
|
-
// this.speakerPeerId = data.speakerPeerId;
|
|
159
|
-
// const newParams = {
|
|
160
|
-
// speakerPeerId: this.speakerPeerId,
|
|
161
|
-
// };
|
|
162
|
-
// /*
|
|
163
|
-
// FUTURE does this limit usable environments?
|
|
164
|
-
// ie does this work if internet is lost after initial page load?
|
|
165
|
-
// */
|
|
166
|
-
// window.location.search = this.queryStringFromObject(newParams); // Redirect to correctly constructed keypad page
|
|
167
|
-
// }
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
join = async () => {
|
|
171
|
-
this.displayUpdate('Listener - join');
|
|
172
|
-
console.log(' Creating connection to: ', this.speakerPeerId);
|
|
173
|
-
/**
|
|
174
|
-
* Create the connection between the two Peers.
|
|
175
|
-
*
|
|
176
|
-
* Sets up callbacks that handle any events related to the
|
|
177
|
-
* connection and data received on it.
|
|
178
|
-
*/
|
|
179
|
-
// Close old connection
|
|
180
|
-
if (this.conn) {
|
|
181
|
-
this.displayUpdate('Closing old connection');
|
|
182
|
-
this.conn.close();
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// Create connection to destination peer specified by the query param
|
|
186
|
-
this.displayUpdate(`Creating connection to: ${this.speakerPeerId}`);
|
|
187
|
-
this.conn = this.peer.connect(this.speakerPeerId, {
|
|
188
|
-
reliable: true,
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
this.displayUpdate('Created connection');
|
|
192
|
-
this.conn.on('open', async () => {
|
|
193
|
-
this.displayUpdate('Listener - conn open');
|
|
194
|
-
this.connOpen = true;
|
|
195
|
-
// this.sendSamplingRate();
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
// Handle incoming data (messages only since this is the signal sender)
|
|
199
|
-
this.conn.on('data', this.onConnData);
|
|
200
|
-
this.conn.on('close', () => {
|
|
201
|
-
console.log('Connection closed');
|
|
202
|
-
this.connOpen = false;
|
|
203
|
-
});
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
startCalibration = async () => {
|
|
207
|
-
await this.getDeviceInfo();
|
|
208
|
-
await this.openAudioStream();
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
getMobileOS = () => {
|
|
212
|
-
const ua = navigator.userAgent;
|
|
213
|
-
if (/android/i.test(ua)) {
|
|
214
|
-
return 'Android';
|
|
215
|
-
}
|
|
216
|
-
if (
|
|
217
|
-
/iPad|iPhone|iPod/.test(ua) ||
|
|
218
|
-
((navigator?.userAgentData?.platform || navigator?.platform) === 'MacIntel' &&
|
|
219
|
-
navigator.maxTouchPoints > 1)
|
|
220
|
-
) {
|
|
221
|
-
return 'iOS';
|
|
222
|
-
}
|
|
223
|
-
return 'Other';
|
|
224
|
-
};
|
|
225
|
-
|
|
226
|
-
sendSamplingRate = sampleRate => {
|
|
227
|
-
this.displayUpdate('Listener - sendSamplingRate');
|
|
228
|
-
this.conn.send({
|
|
229
|
-
name: 'samplingRate',
|
|
230
|
-
payload: sampleRate,
|
|
231
|
-
});
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
sendSampleSize = sampleSize => {
|
|
235
|
-
this.displayUpdate('Listener - sendSampleSize');
|
|
236
|
-
this.conn.send({
|
|
237
|
-
name: 'sampleSize',
|
|
238
|
-
payload: sampleSize,
|
|
239
|
-
});
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
sendFlags = flags => {
|
|
243
|
-
this.displayUpdate('Listener - sendFlags');
|
|
244
|
-
this.conn.send({
|
|
245
|
-
name: 'flags',
|
|
246
|
-
payload: flags,
|
|
247
|
-
});
|
|
248
|
-
};
|
|
249
|
-
|
|
250
|
-
sendPermissionStatus = status => {
|
|
251
|
-
// this.displayUpdate('Listener - sendPermissionStatus');
|
|
252
|
-
this.conn.send({
|
|
253
|
-
name: 'permissionStatus',
|
|
254
|
-
payload: status,
|
|
255
|
-
});
|
|
256
|
-
};
|
|
257
|
-
|
|
258
|
-
getDeviceInfo = async () => {
|
|
259
|
-
const deviceInfo = {};
|
|
260
|
-
try {
|
|
261
|
-
fod.complete(function (data) {
|
|
262
|
-
deviceInfo['IsMobile'] = data.device['ismobile'];
|
|
263
|
-
deviceInfo['HardwareName'] = data.device['hardwarename'];
|
|
264
|
-
deviceInfo['HardwareFamily'] = data.device['hardwarefamily'];
|
|
265
|
-
deviceInfo['HardwareModel'] = data.device['hardwaremodel'];
|
|
266
|
-
deviceInfo['OEM'] = data.device['oem'];
|
|
267
|
-
deviceInfo['HardwareModelVariants'] = data.device['hardwaremodelvariants'];
|
|
268
|
-
deviceInfo['DeviceId'] = data.device['deviceid'];
|
|
269
|
-
deviceInfo['PlatformName'] = data.device['platformname'];
|
|
270
|
-
deviceInfo['PlatformVersion'] = data.device['platformversion'];
|
|
271
|
-
deviceInfo['DeviceType'] = data.device['devicetype'];
|
|
272
|
-
// deviceInfo['deviceInfoFromUser'] = this.deviceInfoFromUser;
|
|
273
|
-
});
|
|
274
|
-
} catch (error) {
|
|
275
|
-
console.error('Error fetching or executing script:', error.message);
|
|
276
|
-
}
|
|
277
|
-
// deviceInfo['deviceInfoFromUser'] = this.deviceInfoFromUser;
|
|
278
|
-
deviceInfo['microphoneFromAPI'] = this.microphoneFromAPI;
|
|
279
|
-
deviceInfo['microphoneDeviceId'] = this.microphoneDeviceId;
|
|
280
|
-
deviceInfo['screenWidth'] = window.screen.width;
|
|
281
|
-
deviceInfo['screenHeight'] = window.screen.height;
|
|
282
|
-
console.log('deviceInfo Inside getDeviceInfo: ', deviceInfo);
|
|
283
|
-
this.conn.send({
|
|
284
|
-
name: 'deviceInfo',
|
|
285
|
-
payload: deviceInfo,
|
|
286
|
-
});
|
|
287
|
-
return deviceInfo;
|
|
288
|
-
};
|
|
289
|
-
|
|
290
|
-
applyHQTrackConstraints = async stream => {
|
|
291
|
-
// Contraint the incoming audio to the sampling rate we want
|
|
292
|
-
stream.getAudioTracks().forEach(track => {
|
|
293
|
-
console.log(track, track.enabled);
|
|
294
|
-
});
|
|
295
|
-
const track = stream.getAudioTracks()[0];
|
|
296
|
-
console.log(track);
|
|
297
|
-
const capabilities = track.getCapabilities();
|
|
298
|
-
|
|
299
|
-
this.displayUpdate(
|
|
300
|
-
`Listener Track Capabilities - ${JSON.stringify(capabilities, undefined, 2)}`
|
|
301
|
-
);
|
|
302
|
-
|
|
303
|
-
const constraints = track.getConstraints();
|
|
304
|
-
|
|
305
|
-
if (capabilities.echoCancellation) {
|
|
306
|
-
constraints.echoCancellation = false;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
if (capabilities.sampleRate) {
|
|
310
|
-
constraints.sampleRate = this.calibrateSoundHz;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
if (capabilities.sampleSize) {
|
|
314
|
-
constraints.sampleSize = this.calibrateSoundSamplingDesiredBits;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
if (capabilities.channelCount) {
|
|
318
|
-
constraints.channelCount = 1;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
this.displayUpdate(`Listener Track Constraints - ${JSON.stringify(constraints, undefined, 2)}`);
|
|
322
|
-
|
|
323
|
-
// await the promise
|
|
324
|
-
try {
|
|
325
|
-
await track.applyConstraints(constraints);
|
|
326
|
-
} catch (err) {
|
|
327
|
-
console.error(err);
|
|
328
|
-
this.displayUpdate(`Error applying constraints to track: ${err}`);
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
const settings = track.getSettings();
|
|
332
|
-
this.displayUpdate(`Listener Track Settings - ${JSON.stringify(settings, undefined, 2)}`);
|
|
333
|
-
return settings;
|
|
334
|
-
};
|
|
335
|
-
|
|
336
|
-
getMediaDevicesAudioContraints = async () => {
|
|
337
|
-
const availableConstraints = navigator.mediaDevices.getSupportedConstraints();
|
|
338
|
-
|
|
339
|
-
const contraints = {
|
|
340
|
-
// ...(availableConstraints.echoCancellation && availableConstraints.echoCancellation == true
|
|
341
|
-
// ? {echoCancellation: {exact: false}}
|
|
342
|
-
// : {}),
|
|
343
|
-
// ...(availableConstraints.sampleRate && availableConstraints.sampleRate == true
|
|
344
|
-
// ? {sampleRate: {ideal: this.calibrateSoundHz}}
|
|
345
|
-
// : {}),
|
|
346
|
-
// ...(availableConstraints.sampleSize && availableConstraints.sampleSize == true
|
|
347
|
-
// ? {sampleSize: {ideal: this.calibrateSoundSamplingDesiredBits}}
|
|
348
|
-
// : {}),
|
|
349
|
-
// ...(availableConstraints.channelCount && availableConstraints.channelCount == true
|
|
350
|
-
// ? {channelCount: {exact: 1}}
|
|
351
|
-
// : {}),
|
|
352
|
-
autoGainControl: false,
|
|
353
|
-
noiseSuppression: false,
|
|
354
|
-
echoCancellation: false,
|
|
355
|
-
channelCount: 1,
|
|
356
|
-
};
|
|
357
|
-
|
|
358
|
-
if (this.microphoneDeviceId !== '') {
|
|
359
|
-
contraints.deviceId = {exact: await this.getDeviceIdByLabel(this.microphoneDeviceId)};
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
console.log(contraints);
|
|
363
|
-
|
|
364
|
-
return contraints;
|
|
365
|
-
};
|
|
366
|
-
setMicrophoneFromAPI = microphoneFromAPI => {
|
|
367
|
-
this.microphoneFromAPI = microphoneFromAPI;
|
|
368
|
-
};
|
|
369
|
-
setMicrophoneDeviceId = microphoneDeviceId => {
|
|
370
|
-
this.microphoneDeviceId = microphoneDeviceId;
|
|
371
|
-
};
|
|
372
|
-
getDeviceIdByLabel = async targetLabel => {
|
|
373
|
-
try {
|
|
374
|
-
//get permission to use audio first. (Returns empty labels on some computers if not done first)
|
|
375
|
-
await navigator.mediaDevices.getUserMedia({audio: true});
|
|
376
|
-
// Enumerate available media devices
|
|
377
|
-
const devices = await navigator.mediaDevices.enumerateDevices();
|
|
378
|
-
|
|
379
|
-
// Find the device with the matching label
|
|
380
|
-
const matchingDevice = devices.find(
|
|
381
|
-
device => device.kind === 'audioinput' && device.label === targetLabel
|
|
382
|
-
);
|
|
383
|
-
|
|
384
|
-
if (matchingDevice) {
|
|
385
|
-
return matchingDevice.deviceId; // Return the deviceId if found
|
|
386
|
-
} else {
|
|
387
|
-
throw new Error(`No audio input device found with label: "${targetLabel}"`);
|
|
388
|
-
}
|
|
389
|
-
} catch (error) {
|
|
390
|
-
console.error('Error finding device ID:', error);
|
|
391
|
-
return null;
|
|
392
|
-
}
|
|
393
|
-
};
|
|
394
|
-
|
|
395
|
-
openAudioStream = async () => {
|
|
396
|
-
this.displayUpdate('Listener - openAudioStream');
|
|
397
|
-
const mobileOS = this.getMobileOS();
|
|
398
|
-
if (process.env.NODE_ENV !== 'development' && mobileOS !== 'iOS') {
|
|
399
|
-
const err = new UnsupportedDeviceError(`${mobileOS} is not supported`);
|
|
400
|
-
this.conn.send({
|
|
401
|
-
name: err.name,
|
|
402
|
-
payload: err,
|
|
403
|
-
});
|
|
404
|
-
return;
|
|
405
|
-
}
|
|
406
|
-
const constraints = await this.getMediaDevicesAudioContraints();
|
|
407
|
-
console.log('Constraints right before getUserMedia:', constraints);
|
|
408
|
-
navigator.mediaDevices
|
|
409
|
-
.getUserMedia({
|
|
410
|
-
audio: constraints,
|
|
411
|
-
video: false,
|
|
412
|
-
//audio: {echoCancellation: false, noiseSuppression: false, autoGainControl: false, deviceId: {exact: await this.getDeviceIdByLabel(this.microphoneDeviceId) }},
|
|
413
|
-
})
|
|
414
|
-
.then(stream => {
|
|
415
|
-
this.displayUpdate(
|
|
416
|
-
`Listener Track settings before applied constraints - ${JSON.stringify(
|
|
417
|
-
stream.getAudioTracks()[0].getSettings(),
|
|
418
|
-
undefined,
|
|
419
|
-
2
|
|
420
|
-
)}`
|
|
421
|
-
);
|
|
422
|
-
this.applyHQTrackConstraints(stream)
|
|
423
|
-
.then(settings => {
|
|
424
|
-
console.log(settings);
|
|
425
|
-
this.sendSamplingRate(settings.sampleRate);
|
|
426
|
-
//let sampleSize = settings.sampleSize;
|
|
427
|
-
let sampleSize = this.calibrateSoundSamplingDesiredBits;
|
|
428
|
-
/*
|
|
429
|
-
if (!sampleSize) {
|
|
430
|
-
sampleSize = this.calibrateSoundSamplingDesiredBits;
|
|
431
|
-
}
|
|
432
|
-
*/
|
|
433
|
-
this.sendSampleSize(sampleSize);
|
|
434
|
-
this.sendFlags({
|
|
435
|
-
autoGainControl: settings.autoGainControl,
|
|
436
|
-
noiseSuppression: settings.noiseSuppression,
|
|
437
|
-
echoCancellation: settings.echoCancellation,
|
|
438
|
-
});
|
|
439
|
-
this.peer.call(this.speakerPeerId, stream); // one-way call
|
|
440
|
-
this.displayUpdate('Listener - openAudioStream');
|
|
441
|
-
})
|
|
442
|
-
.catch(err => {
|
|
443
|
-
console.log(err);
|
|
444
|
-
this.displayUpdate(
|
|
445
|
-
`Listener - Error in applyHQTrackConstraints - ${JSON.stringify(err, undefined, 2)}`
|
|
446
|
-
);
|
|
447
|
-
});
|
|
448
|
-
})
|
|
449
|
-
.catch(err => {
|
|
450
|
-
console.error(err);
|
|
451
|
-
if (err.name === 'OverconstrainedError') {
|
|
452
|
-
const constraint = err.constraint;
|
|
453
|
-
const message = `The constraint "${constraint}" cannot be satisfied by the selected microphone. Please adjust your calibration settings or choose a different microphone.`;
|
|
454
|
-
|
|
455
|
-
this.displayUpdate(`Listener - OverconstrainedError: ${message}`);
|
|
456
|
-
console.error(message);
|
|
457
|
-
|
|
458
|
-
alert(`Overconstrained Error: ${message}`);
|
|
459
|
-
}
|
|
460
|
-
this.displayUpdate(
|
|
461
|
-
`Listener - Error in getUserMedia - ${JSON.stringify(err, undefined, 2)}`
|
|
462
|
-
);
|
|
463
|
-
});
|
|
464
|
-
};
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
export default Listener;
|
|
1
|
+
import AudioPeer from './audioPeer';
|
|
2
|
+
import {UnsupportedDeviceError, MissingSpeakerIdError} from './peerErrors';
|
|
3
|
+
import axios from 'axios';
|
|
4
|
+
import Peer from 'peerjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @class Handles the listener's side of the connection. Responsible for getting access to user's microphone,
|
|
8
|
+
* and initiating a call to the Speaker.
|
|
9
|
+
* @augments AudioPeer
|
|
10
|
+
*/
|
|
11
|
+
class Listener extends AudioPeer {
|
|
12
|
+
/**
|
|
13
|
+
* Takes a target element where html elements will be appended.
|
|
14
|
+
*
|
|
15
|
+
* @param params - See type definition for initParameters.
|
|
16
|
+
* @example
|
|
17
|
+
*/
|
|
18
|
+
constructor(params) {
|
|
19
|
+
super(params);
|
|
20
|
+
console.log('Listener constructor', this.peer);
|
|
21
|
+
this.microphoneFromAPI = params.microphoneFromAPI ? params.microphoneFromAPI : '';
|
|
22
|
+
this.microphoneDeviceId = params.microphoneDeviceId ? params.microphoneDeviceId : '';
|
|
23
|
+
// this.deviceInfoFromUser = params.deviceInfoFromUser
|
|
24
|
+
// ? params.deviceInfoFromUser
|
|
25
|
+
// : {modelNumber: '', modelName: ''};
|
|
26
|
+
this.startTime = Date.now();
|
|
27
|
+
this.receiverPeerId = null;
|
|
28
|
+
|
|
29
|
+
const urlParameters = this.parseURLSearchParams();
|
|
30
|
+
this.calibrateSoundHz =
|
|
31
|
+
// previous calibrateSoundHz
|
|
32
|
+
urlParameters.hz !== null && urlParameters.hz !== undefined ? urlParameters.hz : 48000;
|
|
33
|
+
this.calibrateSoundSamplingDesiredBits =
|
|
34
|
+
// previous calibrateSoundSamplingDesiredBits
|
|
35
|
+
urlParameters.bits !== null && urlParameters.bits !== undefined ? urlParameters.bits : 24;
|
|
36
|
+
this.speakerPeerId = urlParameters.speakerPeerId;
|
|
37
|
+
this.lastPeerId = this.speakerPeerId;
|
|
38
|
+
this.connOpen = false;
|
|
39
|
+
}
|
|
40
|
+
generateTimeBasedPeerID = async () => {
|
|
41
|
+
const now = new Date().getTime();
|
|
42
|
+
const randomBuffer = new Uint8Array(10);
|
|
43
|
+
crypto.getRandomValues(randomBuffer);
|
|
44
|
+
const randomPart = Array.from(randomBuffer)
|
|
45
|
+
.map(b => b.toString(36))
|
|
46
|
+
.join('');
|
|
47
|
+
const toHash = `${now}-${randomPart}`;
|
|
48
|
+
const encoder = new TextEncoder();
|
|
49
|
+
const data = encoder.encode(toHash);
|
|
50
|
+
const hash = await crypto.subtle.digest('SHA-256', data);
|
|
51
|
+
const hashArray = Array.from(new Uint8Array(hash));
|
|
52
|
+
const hashString = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
|
|
53
|
+
const shortHash = hashString.substring(0, 12);
|
|
54
|
+
return this.encodeBase62(parseInt(shortHash, 16));
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
encodeBase62 = num => {
|
|
58
|
+
const base = 26;
|
|
59
|
+
const characters = 'abcdefghijklmnopqrstuvwxyz';
|
|
60
|
+
let result = '';
|
|
61
|
+
while (num > 0) {
|
|
62
|
+
result = characters[num % base] + result;
|
|
63
|
+
num = Math.floor(num / base);
|
|
64
|
+
}
|
|
65
|
+
return result || 'a';
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
initializePeer = async () => {
|
|
69
|
+
console.log('Initializing PeerJS connection...');
|
|
70
|
+
const id = await this.generateTimeBasedPeerID();
|
|
71
|
+
console.log('Generated Peer ID:', id);
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
this.peer = new Peer(id, {
|
|
75
|
+
debug: 2,
|
|
76
|
+
host: 'easyeyes-peer-server.herokuapp.com',
|
|
77
|
+
port: 443,
|
|
78
|
+
secure: true,
|
|
79
|
+
config: {
|
|
80
|
+
iceServers: [
|
|
81
|
+
{
|
|
82
|
+
urls: 'stun:stun.relay.metered.ca:80',
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
urls: 'turn:global.relay.metered.ca:80',
|
|
86
|
+
username: 'de884cfc34189cdf1a5dd616',
|
|
87
|
+
credential: 'IcOpouU9/TYBmpHU',
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
urls: 'turn:global.relay.metered.ca:80?transport=tcp',
|
|
91
|
+
username: 'de884cfc34189cdf1a5dd616',
|
|
92
|
+
credential: 'IcOpouU9/TYBmpHU',
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
urls: 'turn:global.relay.metered.ca:443',
|
|
96
|
+
username: 'de884cfc34189cdf1a5dd616',
|
|
97
|
+
credential: 'IcOpouU9/TYBmpHU',
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
urls: 'turns:global.relay.metered.ca:443?transport=tcp',
|
|
101
|
+
username: 'de884cfc34189cdf1a5dd616',
|
|
102
|
+
credential: 'IcOpouU9/TYBmpHU',
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
this.peer.on('open', this.onPeerOpen);
|
|
109
|
+
this.peer.on('connection', this.onPeerConnection);
|
|
110
|
+
this.peer.on('disconnected', this.onPeerDisconnected);
|
|
111
|
+
this.peer.on('close', this.onPeerClose);
|
|
112
|
+
this.peer.on('error', this.onPeerError);
|
|
113
|
+
|
|
114
|
+
console.log('Peer object created:', this.peer);
|
|
115
|
+
} catch (error) {
|
|
116
|
+
console.error('Failed to initialize PeerJS:', error);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
onPeerOpen = id => {
|
|
121
|
+
this.displayUpdate('Listener - onPeerOpen');
|
|
122
|
+
console.log('onPeerOpen: ', id);
|
|
123
|
+
// Workaround for peer.reconnect deleting previous id
|
|
124
|
+
try {
|
|
125
|
+
if (id === null) {
|
|
126
|
+
this.displayUpdate('Received null id from peer open');
|
|
127
|
+
this.peer.id = this.lastPeerId;
|
|
128
|
+
} else {
|
|
129
|
+
this.lastPeerId = this.peer.id;
|
|
130
|
+
}
|
|
131
|
+
} catch (error) {
|
|
132
|
+
console.error('Error in onPeerOpen: ', error);
|
|
133
|
+
}
|
|
134
|
+
this.join();
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
onPeerConnection = connection => {
|
|
138
|
+
this.displayUpdate('Listener - onPeerConnection');
|
|
139
|
+
console.log('onPeerConnection: ', connection);
|
|
140
|
+
// Disallow incoming connections
|
|
141
|
+
connection.on('open', () => {
|
|
142
|
+
connection.send('Sender does not accept incoming connections');
|
|
143
|
+
setTimeout(() => {
|
|
144
|
+
connection.close();
|
|
145
|
+
}, 500);
|
|
146
|
+
});
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
onConnData = data => {
|
|
150
|
+
this.displayUpdate('Listener - onConnData');
|
|
151
|
+
const hasSpeakerID = Object.prototype.hasOwnProperty.call(data, 'speakerPeerId');
|
|
152
|
+
// if (!hasSpeakerID) {
|
|
153
|
+
// this.displayUpdate('Error in parsing data received! Must set "speakerPeerId" property');
|
|
154
|
+
// throw new MissingSpeakerIdError('Must set "speakerPeerId" property');
|
|
155
|
+
// } else {
|
|
156
|
+
// // this.conn.close();
|
|
157
|
+
// this.displayUpdate(this.speakerPeerId);
|
|
158
|
+
// this.speakerPeerId = data.speakerPeerId;
|
|
159
|
+
// const newParams = {
|
|
160
|
+
// speakerPeerId: this.speakerPeerId,
|
|
161
|
+
// };
|
|
162
|
+
// /*
|
|
163
|
+
// FUTURE does this limit usable environments?
|
|
164
|
+
// ie does this work if internet is lost after initial page load?
|
|
165
|
+
// */
|
|
166
|
+
// window.location.search = this.queryStringFromObject(newParams); // Redirect to correctly constructed keypad page
|
|
167
|
+
// }
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
join = async () => {
|
|
171
|
+
this.displayUpdate('Listener - join');
|
|
172
|
+
console.log(' Creating connection to: ', this.speakerPeerId);
|
|
173
|
+
/**
|
|
174
|
+
* Create the connection between the two Peers.
|
|
175
|
+
*
|
|
176
|
+
* Sets up callbacks that handle any events related to the
|
|
177
|
+
* connection and data received on it.
|
|
178
|
+
*/
|
|
179
|
+
// Close old connection
|
|
180
|
+
if (this.conn) {
|
|
181
|
+
this.displayUpdate('Closing old connection');
|
|
182
|
+
this.conn.close();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Create connection to destination peer specified by the query param
|
|
186
|
+
this.displayUpdate(`Creating connection to: ${this.speakerPeerId}`);
|
|
187
|
+
this.conn = this.peer.connect(this.speakerPeerId, {
|
|
188
|
+
reliable: true,
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
this.displayUpdate('Created connection');
|
|
192
|
+
this.conn.on('open', async () => {
|
|
193
|
+
this.displayUpdate('Listener - conn open');
|
|
194
|
+
this.connOpen = true;
|
|
195
|
+
// this.sendSamplingRate();
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// Handle incoming data (messages only since this is the signal sender)
|
|
199
|
+
this.conn.on('data', this.onConnData);
|
|
200
|
+
this.conn.on('close', () => {
|
|
201
|
+
console.log('Connection closed');
|
|
202
|
+
this.connOpen = false;
|
|
203
|
+
});
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
startCalibration = async () => {
|
|
207
|
+
await this.getDeviceInfo();
|
|
208
|
+
await this.openAudioStream();
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
getMobileOS = () => {
|
|
212
|
+
const ua = navigator.userAgent;
|
|
213
|
+
if (/android/i.test(ua)) {
|
|
214
|
+
return 'Android';
|
|
215
|
+
}
|
|
216
|
+
if (
|
|
217
|
+
/iPad|iPhone|iPod/.test(ua) ||
|
|
218
|
+
((navigator?.userAgentData?.platform || navigator?.platform) === 'MacIntel' &&
|
|
219
|
+
navigator.maxTouchPoints > 1)
|
|
220
|
+
) {
|
|
221
|
+
return 'iOS';
|
|
222
|
+
}
|
|
223
|
+
return 'Other';
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
sendSamplingRate = sampleRate => {
|
|
227
|
+
this.displayUpdate('Listener - sendSamplingRate');
|
|
228
|
+
this.conn.send({
|
|
229
|
+
name: 'samplingRate',
|
|
230
|
+
payload: sampleRate,
|
|
231
|
+
});
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
sendSampleSize = sampleSize => {
|
|
235
|
+
this.displayUpdate('Listener - sendSampleSize');
|
|
236
|
+
this.conn.send({
|
|
237
|
+
name: 'sampleSize',
|
|
238
|
+
payload: sampleSize,
|
|
239
|
+
});
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
sendFlags = flags => {
|
|
243
|
+
this.displayUpdate('Listener - sendFlags');
|
|
244
|
+
this.conn.send({
|
|
245
|
+
name: 'flags',
|
|
246
|
+
payload: flags,
|
|
247
|
+
});
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
sendPermissionStatus = status => {
|
|
251
|
+
// this.displayUpdate('Listener - sendPermissionStatus');
|
|
252
|
+
this.conn.send({
|
|
253
|
+
name: 'permissionStatus',
|
|
254
|
+
payload: status,
|
|
255
|
+
});
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
getDeviceInfo = async () => {
|
|
259
|
+
const deviceInfo = {};
|
|
260
|
+
try {
|
|
261
|
+
fod.complete(function (data) {
|
|
262
|
+
deviceInfo['IsMobile'] = data.device['ismobile'];
|
|
263
|
+
deviceInfo['HardwareName'] = data.device['hardwarename'];
|
|
264
|
+
deviceInfo['HardwareFamily'] = data.device['hardwarefamily'];
|
|
265
|
+
deviceInfo['HardwareModel'] = data.device['hardwaremodel'];
|
|
266
|
+
deviceInfo['OEM'] = data.device['oem'];
|
|
267
|
+
deviceInfo['HardwareModelVariants'] = data.device['hardwaremodelvariants'];
|
|
268
|
+
deviceInfo['DeviceId'] = data.device['deviceid'];
|
|
269
|
+
deviceInfo['PlatformName'] = data.device['platformname'];
|
|
270
|
+
deviceInfo['PlatformVersion'] = data.device['platformversion'];
|
|
271
|
+
deviceInfo['DeviceType'] = data.device['devicetype'];
|
|
272
|
+
// deviceInfo['deviceInfoFromUser'] = this.deviceInfoFromUser;
|
|
273
|
+
});
|
|
274
|
+
} catch (error) {
|
|
275
|
+
console.error('Error fetching or executing script:', error.message);
|
|
276
|
+
}
|
|
277
|
+
// deviceInfo['deviceInfoFromUser'] = this.deviceInfoFromUser;
|
|
278
|
+
deviceInfo['microphoneFromAPI'] = this.microphoneFromAPI;
|
|
279
|
+
deviceInfo['microphoneDeviceId'] = this.microphoneDeviceId;
|
|
280
|
+
deviceInfo['screenWidth'] = window.screen.width;
|
|
281
|
+
deviceInfo['screenHeight'] = window.screen.height;
|
|
282
|
+
console.log('deviceInfo Inside getDeviceInfo: ', deviceInfo);
|
|
283
|
+
this.conn.send({
|
|
284
|
+
name: 'deviceInfo',
|
|
285
|
+
payload: deviceInfo,
|
|
286
|
+
});
|
|
287
|
+
return deviceInfo;
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
applyHQTrackConstraints = async stream => {
|
|
291
|
+
// Contraint the incoming audio to the sampling rate we want
|
|
292
|
+
stream.getAudioTracks().forEach(track => {
|
|
293
|
+
console.log(track, track.enabled);
|
|
294
|
+
});
|
|
295
|
+
const track = stream.getAudioTracks()[0];
|
|
296
|
+
console.log(track);
|
|
297
|
+
const capabilities = track.getCapabilities();
|
|
298
|
+
|
|
299
|
+
this.displayUpdate(
|
|
300
|
+
`Listener Track Capabilities - ${JSON.stringify(capabilities, undefined, 2)}`
|
|
301
|
+
);
|
|
302
|
+
|
|
303
|
+
const constraints = track.getConstraints();
|
|
304
|
+
|
|
305
|
+
if (capabilities.echoCancellation) {
|
|
306
|
+
constraints.echoCancellation = false;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (capabilities.sampleRate) {
|
|
310
|
+
constraints.sampleRate = this.calibrateSoundHz;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (capabilities.sampleSize) {
|
|
314
|
+
constraints.sampleSize = this.calibrateSoundSamplingDesiredBits;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (capabilities.channelCount) {
|
|
318
|
+
constraints.channelCount = 1;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
this.displayUpdate(`Listener Track Constraints - ${JSON.stringify(constraints, undefined, 2)}`);
|
|
322
|
+
|
|
323
|
+
// await the promise
|
|
324
|
+
try {
|
|
325
|
+
await track.applyConstraints(constraints);
|
|
326
|
+
} catch (err) {
|
|
327
|
+
console.error(err);
|
|
328
|
+
this.displayUpdate(`Error applying constraints to track: ${err}`);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const settings = track.getSettings();
|
|
332
|
+
this.displayUpdate(`Listener Track Settings - ${JSON.stringify(settings, undefined, 2)}`);
|
|
333
|
+
return settings;
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
getMediaDevicesAudioContraints = async () => {
|
|
337
|
+
const availableConstraints = navigator.mediaDevices.getSupportedConstraints();
|
|
338
|
+
|
|
339
|
+
const contraints = {
|
|
340
|
+
// ...(availableConstraints.echoCancellation && availableConstraints.echoCancellation == true
|
|
341
|
+
// ? {echoCancellation: {exact: false}}
|
|
342
|
+
// : {}),
|
|
343
|
+
// ...(availableConstraints.sampleRate && availableConstraints.sampleRate == true
|
|
344
|
+
// ? {sampleRate: {ideal: this.calibrateSoundHz}}
|
|
345
|
+
// : {}),
|
|
346
|
+
// ...(availableConstraints.sampleSize && availableConstraints.sampleSize == true
|
|
347
|
+
// ? {sampleSize: {ideal: this.calibrateSoundSamplingDesiredBits}}
|
|
348
|
+
// : {}),
|
|
349
|
+
// ...(availableConstraints.channelCount && availableConstraints.channelCount == true
|
|
350
|
+
// ? {channelCount: {exact: 1}}
|
|
351
|
+
// : {}),
|
|
352
|
+
autoGainControl: false,
|
|
353
|
+
noiseSuppression: false,
|
|
354
|
+
echoCancellation: false,
|
|
355
|
+
channelCount: 1,
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
if (this.microphoneDeviceId !== '') {
|
|
359
|
+
contraints.deviceId = {exact: await this.getDeviceIdByLabel(this.microphoneDeviceId)};
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
console.log(contraints);
|
|
363
|
+
|
|
364
|
+
return contraints;
|
|
365
|
+
};
|
|
366
|
+
setMicrophoneFromAPI = microphoneFromAPI => {
|
|
367
|
+
this.microphoneFromAPI = microphoneFromAPI;
|
|
368
|
+
};
|
|
369
|
+
setMicrophoneDeviceId = microphoneDeviceId => {
|
|
370
|
+
this.microphoneDeviceId = microphoneDeviceId;
|
|
371
|
+
};
|
|
372
|
+
getDeviceIdByLabel = async targetLabel => {
|
|
373
|
+
try {
|
|
374
|
+
//get permission to use audio first. (Returns empty labels on some computers if not done first)
|
|
375
|
+
await navigator.mediaDevices.getUserMedia({audio: true});
|
|
376
|
+
// Enumerate available media devices
|
|
377
|
+
const devices = await navigator.mediaDevices.enumerateDevices();
|
|
378
|
+
|
|
379
|
+
// Find the device with the matching label
|
|
380
|
+
const matchingDevice = devices.find(
|
|
381
|
+
device => device.kind === 'audioinput' && device.label === targetLabel
|
|
382
|
+
);
|
|
383
|
+
|
|
384
|
+
if (matchingDevice) {
|
|
385
|
+
return matchingDevice.deviceId; // Return the deviceId if found
|
|
386
|
+
} else {
|
|
387
|
+
throw new Error(`No audio input device found with label: "${targetLabel}"`);
|
|
388
|
+
}
|
|
389
|
+
} catch (error) {
|
|
390
|
+
console.error('Error finding device ID:', error);
|
|
391
|
+
return null;
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
openAudioStream = async () => {
|
|
396
|
+
this.displayUpdate('Listener - openAudioStream');
|
|
397
|
+
const mobileOS = this.getMobileOS();
|
|
398
|
+
if (process.env.NODE_ENV !== 'development' && mobileOS !== 'iOS') {
|
|
399
|
+
const err = new UnsupportedDeviceError(`${mobileOS} is not supported`);
|
|
400
|
+
this.conn.send({
|
|
401
|
+
name: err.name,
|
|
402
|
+
payload: err,
|
|
403
|
+
});
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
const constraints = await this.getMediaDevicesAudioContraints();
|
|
407
|
+
console.log('Constraints right before getUserMedia:', constraints);
|
|
408
|
+
navigator.mediaDevices
|
|
409
|
+
.getUserMedia({
|
|
410
|
+
audio: constraints,
|
|
411
|
+
video: false,
|
|
412
|
+
//audio: {echoCancellation: false, noiseSuppression: false, autoGainControl: false, deviceId: {exact: await this.getDeviceIdByLabel(this.microphoneDeviceId) }},
|
|
413
|
+
})
|
|
414
|
+
.then(stream => {
|
|
415
|
+
this.displayUpdate(
|
|
416
|
+
`Listener Track settings before applied constraints - ${JSON.stringify(
|
|
417
|
+
stream.getAudioTracks()[0].getSettings(),
|
|
418
|
+
undefined,
|
|
419
|
+
2
|
|
420
|
+
)}`
|
|
421
|
+
);
|
|
422
|
+
this.applyHQTrackConstraints(stream)
|
|
423
|
+
.then(settings => {
|
|
424
|
+
console.log(settings);
|
|
425
|
+
this.sendSamplingRate(settings.sampleRate);
|
|
426
|
+
//let sampleSize = settings.sampleSize;
|
|
427
|
+
let sampleSize = this.calibrateSoundSamplingDesiredBits;
|
|
428
|
+
/*
|
|
429
|
+
if (!sampleSize) {
|
|
430
|
+
sampleSize = this.calibrateSoundSamplingDesiredBits;
|
|
431
|
+
}
|
|
432
|
+
*/
|
|
433
|
+
this.sendSampleSize(sampleSize);
|
|
434
|
+
this.sendFlags({
|
|
435
|
+
autoGainControl: settings.autoGainControl,
|
|
436
|
+
noiseSuppression: settings.noiseSuppression,
|
|
437
|
+
echoCancellation: settings.echoCancellation,
|
|
438
|
+
});
|
|
439
|
+
this.peer.call(this.speakerPeerId, stream); // one-way call
|
|
440
|
+
this.displayUpdate('Listener - openAudioStream');
|
|
441
|
+
})
|
|
442
|
+
.catch(err => {
|
|
443
|
+
console.log(err);
|
|
444
|
+
this.displayUpdate(
|
|
445
|
+
`Listener - Error in applyHQTrackConstraints - ${JSON.stringify(err, undefined, 2)}`
|
|
446
|
+
);
|
|
447
|
+
});
|
|
448
|
+
})
|
|
449
|
+
.catch(err => {
|
|
450
|
+
console.error(err);
|
|
451
|
+
if (err.name === 'OverconstrainedError') {
|
|
452
|
+
const constraint = err.constraint;
|
|
453
|
+
const message = `The constraint "${constraint}" cannot be satisfied by the selected microphone. Please adjust your calibration settings or choose a different microphone.`;
|
|
454
|
+
|
|
455
|
+
this.displayUpdate(`Listener - OverconstrainedError: ${message}`);
|
|
456
|
+
console.error(message);
|
|
457
|
+
|
|
458
|
+
alert(`Overconstrained Error: ${message}`);
|
|
459
|
+
}
|
|
460
|
+
this.displayUpdate(
|
|
461
|
+
`Listener - Error in getUserMedia - ${JSON.stringify(err, undefined, 2)}`
|
|
462
|
+
);
|
|
463
|
+
});
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
export default Listener;
|