speaker-calibration 2.2.225 → 2.2.227

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.
@@ -17,7 +17,6 @@ class Listener extends AudioPeer {
17
17
  */
18
18
  constructor(params) {
19
19
  super(params);
20
- console.log('Listener constructor', this.peer);
21
20
  this.microphoneFromAPI = params.microphoneFromAPI ? params.microphoneFromAPI : '';
22
21
  this.microphoneDeviceId = params.microphoneDeviceId ? params.microphoneDeviceId : '';
23
22
  // this.deviceInfoFromUser = params.deviceInfoFromUser
@@ -26,179 +25,14 @@ class Listener extends AudioPeer {
26
25
  this.startTime = Date.now();
27
26
  this.receiverPeerId = null;
28
27
 
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;
28
+ this.calibrateSoundHz = params.hz;
29
+ this.calibrateSoundSamplingDesiredBits = params.bits;
30
+ this.speakerPeerId = params.speakerPeerId;
37
31
  this.lastPeerId = this.speakerPeerId;
38
32
  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: 'turn:global.relay.metered.ca:80',
83
- username: 'de884cfc34189cdf1a5dd616',
84
- credential: 'IcOpouU9/TYBmpHU',
85
- },
86
- {
87
- urls: 'turn:global.relay.metered.ca:80?transport=tcp',
88
- username: 'de884cfc34189cdf1a5dd616',
89
- credential: 'IcOpouU9/TYBmpHU',
90
- },
91
- {
92
- urls: 'turn:global.relay.metered.ca:443',
93
- username: 'de884cfc34189cdf1a5dd616',
94
- credential: 'IcOpouU9/TYBmpHU',
95
- },
96
- {
97
- urls: 'turns:global.relay.metered.ca:443?transport=tcp',
98
- username: 'de884cfc34189cdf1a5dd616',
99
- credential: 'IcOpouU9/TYBmpHU',
100
- },
101
- ],
102
- },
103
- });
104
33
 
105
- this.peer.on('open', this.onPeerOpen);
106
- this.peer.on('connection', this.onPeerConnection);
107
- this.peer.on('disconnected', this.onPeerDisconnected);
108
- this.peer.on('close', this.onPeerClose);
109
- this.peer.on('error', this.onPeerError);
110
-
111
- console.log('Peer object created:', this.peer);
112
- } catch (error) {
113
- console.error('Failed to initialize PeerJS:', error);
114
- }
115
- };
116
-
117
- onPeerOpen = id => {
118
- this.displayUpdate('Listener - onPeerOpen');
119
- console.log('onPeerOpen: ', id);
120
- // Workaround for peer.reconnect deleting previous id
121
- try {
122
- if (id === null) {
123
- this.displayUpdate('Received null id from peer open');
124
- this.peer.id = this.lastPeerId;
125
- } else {
126
- this.lastPeerId = this.peer.id;
127
- }
128
- } catch (error) {
129
- console.error('Error in onPeerOpen: ', error);
130
- }
131
- this.join();
132
- };
133
-
134
- onPeerConnection = connection => {
135
- this.displayUpdate('Listener - onPeerConnection');
136
- console.log('onPeerConnection: ', connection);
137
- // Disallow incoming connections
138
- connection.on('open', () => {
139
- connection.send('Sender does not accept incoming connections');
140
- setTimeout(() => {
141
- connection.close();
142
- }, 500);
143
- });
144
- };
145
-
146
- onConnData = data => {
147
- this.displayUpdate('Listener - onConnData');
148
- const hasSpeakerID = Object.prototype.hasOwnProperty.call(data, 'speakerPeerId');
149
- // if (!hasSpeakerID) {
150
- // this.displayUpdate('Error in parsing data received! Must set "speakerPeerId" property');
151
- // throw new MissingSpeakerIdError('Must set "speakerPeerId" property');
152
- // } else {
153
- // // this.conn.close();
154
- // this.displayUpdate(this.speakerPeerId);
155
- // this.speakerPeerId = data.speakerPeerId;
156
- // const newParams = {
157
- // speakerPeerId: this.speakerPeerId,
158
- // };
159
- // /*
160
- // FUTURE does this limit usable environments?
161
- // ie does this work if internet is lost after initial page load?
162
- // */
163
- // window.location.search = this.queryStringFromObject(newParams); // Redirect to correctly constructed keypad page
164
- // }
165
- };
166
-
167
- join = async () => {
168
- this.displayUpdate('Listener - join');
169
- console.log(' Creating connection to: ', this.speakerPeerId);
170
- /**
171
- * Create the connection between the two Peers.
172
- *
173
- * Sets up callbacks that handle any events related to the
174
- * connection and data received on it.
175
- */
176
- // Close old connection
177
- if (this.conn) {
178
- this.displayUpdate('Closing old connection');
179
- this.conn.close();
180
- }
181
-
182
- // Create connection to destination peer specified by the query param
183
- this.displayUpdate(`Creating connection to: ${this.speakerPeerId}`);
184
- this.conn = this.peer.connect(this.speakerPeerId, {
185
- reliable: true,
186
- });
187
-
188
- this.displayUpdate('Created connection');
189
- this.conn.on('open', async () => {
190
- this.displayUpdate('Listener - conn open');
191
- this.connOpen = true;
192
- // this.sendSamplingRate();
193
- });
194
-
195
- // Handle incoming data (messages only since this is the signal sender)
196
- this.conn.on('data', this.onConnData);
197
- this.conn.on('close', () => {
198
- console.log('Connection closed');
199
- this.connOpen = false;
200
- });
201
- };
34
+ this.connectionManager = null;
35
+ }
202
36
 
203
37
  startCalibration = async () => {
204
38
  await this.getDeviceInfo();
@@ -222,33 +56,45 @@ class Listener extends AudioPeer {
222
56
 
223
57
  sendSamplingRate = sampleRate => {
224
58
  this.displayUpdate('Listener - sendSamplingRate');
225
- this.conn.send({
226
- name: 'samplingRate',
227
- payload: sampleRate,
59
+ this.connectionManager.send({
60
+ name: 'SoundCalibration',
61
+ payload: {
62
+ name: 'samplingRate',
63
+ payload: sampleRate,
64
+ },
228
65
  });
229
66
  };
230
67
 
231
68
  sendSampleSize = sampleSize => {
232
69
  this.displayUpdate('Listener - sendSampleSize');
233
- this.conn.send({
234
- name: 'sampleSize',
235
- payload: sampleSize,
70
+ this.connectionManager.send({
71
+ name: 'SoundCalibration',
72
+ payload: {
73
+ name: 'sampleSize',
74
+ payload: sampleSize,
75
+ },
236
76
  });
237
77
  };
238
78
 
239
79
  sendFlags = flags => {
240
80
  this.displayUpdate('Listener - sendFlags');
241
- this.conn.send({
242
- name: 'flags',
243
- payload: flags,
81
+ this.connectionManager.send({
82
+ name: 'SoundCalibration',
83
+ payload: {
84
+ name: 'flags',
85
+ payload: flags,
86
+ },
244
87
  });
245
88
  };
246
89
 
247
90
  sendPermissionStatus = status => {
248
91
  // this.displayUpdate('Listener - sendPermissionStatus');
249
- this.conn.send({
250
- name: 'permissionStatus',
251
- payload: status,
92
+ this.connectionManager.send({
93
+ name: 'SoundCalibration',
94
+ payload: {
95
+ name: 'permissionStatus',
96
+ payload: status,
97
+ },
252
98
  });
253
99
  };
254
100
 
@@ -277,9 +123,12 @@ class Listener extends AudioPeer {
277
123
  deviceInfo['screenWidth'] = window.screen.width;
278
124
  deviceInfo['screenHeight'] = window.screen.height;
279
125
  console.log('deviceInfo Inside getDeviceInfo: ', deviceInfo);
280
- this.conn.send({
281
- name: 'deviceInfo',
282
- payload: deviceInfo,
126
+ this.connectionManager.send({
127
+ name: 'SoundCalibration',
128
+ payload: {
129
+ name: 'deviceInfo',
130
+ payload: deviceInfo,
131
+ },
283
132
  });
284
133
  return deviceInfo;
285
134
  };
@@ -394,9 +243,12 @@ class Listener extends AudioPeer {
394
243
  const mobileOS = this.getMobileOS();
395
244
  if (process.env.NODE_ENV !== 'development' && mobileOS !== 'iOS') {
396
245
  const err = new UnsupportedDeviceError(`${mobileOS} is not supported`);
397
- this.conn.send({
398
- name: err.name,
399
- payload: err,
246
+ this.connectionManager.send({
247
+ name: 'SoundCalibration',
248
+ payload: {
249
+ name: err.name,
250
+ payload: err,
251
+ },
400
252
  });
401
253
  return;
402
254
  }
@@ -433,7 +285,7 @@ class Listener extends AudioPeer {
433
285
  noiseSuppression: settings.noiseSuppression,
434
286
  echoCancellation: settings.echoCancellation,
435
287
  });
436
- this.peer.call(this.speakerPeerId, stream); // one-way call
288
+ this.connectionManager.peer.call(this.speakerPeerId, stream); // one-way call
437
289
  this.displayUpdate('Listener - openAudioStream');
438
290
  })
439
291
  .catch(err => {