Skip to content

Commit 15cf68e

Browse files
authored
Merge pull request #56 from WeibelLab/audioUI_bugfix
stream logic cleanup for audio.js and matching to camera.js
2 parents c6d1a7f + 79d4dfd commit 15cf68e

File tree

3 files changed

+12
-191
lines changed

3 files changed

+12
-191
lines changed

JS_Files/audio.js

Lines changed: 7 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,15 @@ export class Audio {
88
#kind = "audioinput";
99
#label = "Audio Only";
1010
#videoElement = null;
11-
#canvasElement = null;
12-
#canvasContext = null;
1311
#audioSelector = null;
1412
#recordCheckbox = null;
15-
#videoCheckbox = null;
1613
#audioCheckbox = null;
1714
#audioMonitorUI = null;
1815
#audioContext = null;
1916
#pluginDiv = null;
2017
#constraints = {audio: false, video: false}
2118
#stream = null;
2219

23-
#videoResolutionWidth = 1280; //Default to 1280 - for best FOV
24-
#videoResolutionHeight = 720; //Default to 720 - for best FOV
25-
2620
#isVisible = false;
2721
#isRecording = false;
2822
#fileNameInputElement = null;
@@ -77,92 +71,6 @@ export class Audio {
7771
return this.#kind;
7872
}
7973

80-
81-
/**
82-
* Set width and height for the input of the camera feed.
83-
*
84-
* @param {number} width of camera resolution
85-
* @param {number} height
86-
* @return {bool} true if success, false if failure
87-
*/
88-
setWidthAndHeight(width, height) {
89-
if (this.setWidth(width) && this.setHeight(height)) {
90-
return true;
91-
}
92-
return false;
93-
}
94-
95-
/**
96-
* Set width for the input of the camera feed.
97-
*
98-
* @param {number} width of camera feed resolution
99-
* @return {bool} true if success, false if failure
100-
*/
101-
setWidth(width) {
102-
if (width === null || isNaN(width)) {
103-
/*
104-
console.log(
105-
"[camera.js:setWidth()] - ERROR: Width value is invalid"
106-
);
107-
*/
108-
return false;
109-
}
110-
111-
this.#videoResolutionWidth = width;
112-
return true;
113-
}
114-
115-
/**
116-
* Set height for the input of the camera feed.
117-
*
118-
* @param {number} height of camera feed resolution
119-
* @return {bool} true if success, false if failure
120-
*/
121-
setHeight(height) {
122-
if (height === null || isNaN(height)) {
123-
/*
124-
console.log(
125-
"[camera.js:setHeight()] - ERROR: Height value is invalid"
126-
);
127-
*/
128-
return false;
129-
}
130-
131-
this.#videoResolutionHeight = height;
132-
return true;
133-
}
134-
135-
/**
136-
* Function used for setting the <Video> element that will interpret the video content
137-
* and the <Canvas> element that will display the video content.
138-
*
139-
*/
140-
setInputAndOutput(video, canvas) {
141-
//Set input Video HTML element
142-
if (video instanceof HTMLVideoElement && video !== null) {
143-
this.#videoElement = video;
144-
} else {
145-
146-
console.log(
147-
"[camera.js:setInputAndOutput()] - ERROR: Video element argument passed is INVALID"
148-
);
149-
150-
return;
151-
}
152-
//Set output Canvas HTML element
153-
if (canvas instanceof HTMLCanvasElement && canvas !== null) {
154-
this.#canvasElement = canvas;
155-
this.#canvasContext = canvas.getContext("2d");
156-
} else {
157-
158-
console.log(
159-
"[camera.js:setInputAndOutput()] - ERROR: Canvas element argument passed is INVALID"
160-
);
161-
162-
return;
163-
}
164-
}
165-
16674
monitorAudio(stream) {
16775
var max_level_L = 0;
16876
var old_level_L = 0;
@@ -201,19 +109,10 @@ export class Audio {
201109
*/
202110

203111
async startStream() {
204-
/* First check that the canvas and video elements are valid */
205-
if (this.#videoElement == null || this.#canvasElement == null) {
206-
console.log(
207-
"[camera.js:startStream()] - ERROR: Video and canvas elements not set"
208-
);
209-
210-
return false;
211-
}
212-
213112
/* Second check that this object has a deviceId set */
214113
if (this.#deviceId === null) {
215114
console.log(
216-
"[camera.js:startStream()] - ERROR: deviceId NOT set"
115+
"[audio.js:startStream()] - ERROR: deviceId NOT set"
217116
);
218117

219118
return false;
@@ -222,47 +121,20 @@ export class Audio {
222121
/* Try to open and start the stream */
223122
try {
224123
this.#stream = await navigator.mediaDevices.getUserMedia(this.#constraints);
225-
if (this.#videoCheckbox.checked){
226-
this.#videoElement.srcObject = this.#stream;
227-
}
228124
if (this.#audioCheckbox.checked){
229125
this.monitorAudio(this.#stream);
230126
}
231127

232128
} catch (err) {
233129
console.log(
234-
`camera.js:startStream()] - ERROR: ${err.message}`
130+
`audio.js:startStream()] - ERROR: ${err.message}`
235131
);
236132
return false;
237133
}
238134

239135
return true;
240136
}
241137

242-
/**
243-
* Draws the Video frames to the Canvas element and continuously calls itself
244-
* until stopped.
245-
*
246-
* @param {HTML Video Element} video - Video element camera originally streams to
247-
* @param {HTML Canvas Element 2D Context} canvasContext - 2D Canvas context taken from the desired output Canvas
248-
* @param {number} canvasWidth - Width of the desired output Canvas
249-
* @param {number} canvasHeight - Height of the desired output Canvas
250-
*
251-
*/
252-
drawToCanvas(video, canvasContext, canvasWidth, canvasHeight) {
253-
//canvasContext.clearRect(0, 0, canvasWidth, canvasHeight);
254-
if (video.readyState === video.HAVE_ENOUGH_DATA) {
255-
canvasContext.drawImage(video, 0, 0, canvasWidth, canvasHeight);
256-
this.recordFrame(); // Checks for recording status in function
257-
}
258-
var frameId = requestAnimationFrame(() => {
259-
this.drawToCanvas(video, canvasContext, canvasWidth, canvasHeight);
260-
});
261-
if (!this.#videoCheckbox.checked) {
262-
cancelAnimationFrame(frameId);
263-
}
264-
}
265-
266138
async closeAudioContext(){
267139
try{
268140
await this.#audioContext.close();
@@ -279,27 +151,15 @@ export class Audio {
279151
*
280152
*/
281153
async stopStream() {
282-
if (
283-
this.#videoElement !== null &&
284-
this.#videoElement.srcObject !== null
285-
) {
286-
this.stopRecording();
287-
this.#videoElement.srcObject.getTracks().forEach((track) => {
288-
track.stop();
289-
})
290-
}
291154
try {
292-
// if (this.#isVisible){
293-
if (!this.#audioCheckbox.checked){
294-
// console.log("closing audio");
295-
await this.closeAudioContext();
296-
}
297-
// }
155+
if (this.#audioContext.state != "closed"){
156+
await this.closeAudioContext();
157+
}
298158
}
299159
catch {
300-
console.log("camera not open");
160+
console.log("audio not open");
301161
}
302-
//console.log("[camera.js:stopStream()] - Camera has been stopped");
162+
//console.log("[audio.js:stopStream()] - Camera has been stopped");
303163

304164
}
305165

@@ -409,7 +269,6 @@ export class Audio {
409269
let videoElement = document.createElement("video");
410270
let canvasElement = document.createElement("canvas");
411271
let avCheckContainer = document.createElement("div");
412-
let videoCheckContainer = document.createElement("div");
413272
let audioCheckContainer = document.createElement("div");
414273
let fileNameContainer = document.createElement("div");
415274
let recordCheckContainer = document.createElement("div");
@@ -428,8 +287,6 @@ export class Audio {
428287
videoElement.autoplay = false;
429288
videoElement.muted = false;
430289
//videoElement.classList.add("camera-canvas");
431-
432-
this.setInputAndOutput(videoElement, canvasElement); //Connect elements to class variables.
433290

434291
// Build Close Button
435292
closeButton.classList.add("close-button");
@@ -477,14 +334,6 @@ export class Audio {
477334
avCheckContainer.style.height = "2em";
478335

479336
audioCheckContainer.classList.add("av-inner-container");
480-
videoCheckContainer.classList.add("av-inner-container");
481-
482-
this.#videoCheckbox = document.createElement("input");
483-
this.#videoCheckbox.type = 'checkbox';
484-
this.#videoCheckbox.checked = false;
485-
var videoLabel = document.createElement('label');
486-
videoLabel.htmlFor = this.#videoCheckbox;
487-
videoLabel.appendChild(document.createTextNode('Video: '));
488337

489338
this.#audioCheckbox = document.createElement("input");
490339
this.#audioCheckbox.type = 'checkbox';
@@ -494,29 +343,19 @@ export class Audio {
494343
audioLabel.appendChild(document.createTextNode('Audio: '));
495344

496345
this.#recordCheckbox.classList.add('flipswitch');
497-
this.#videoCheckbox.classList.add('flipswitch');
498346
this.#audioCheckbox.classList.add('flipswitch');
499347

500348
this.#recordCheckbox.classList.add('checkbox-disabled');
501-
this.#videoCheckbox.classList.add('checkbox-disabled');
502349
this.#audioCheckbox.classList.add('checkbox-disabled');
503350
//this.#audioSelector.classList.add('checkbox-disabled')
504351

505352
recordCheckContainer.addEventListener("click", () => {
506353
this.updateRecordStatus();
507354
});
508-
509-
videoCheckContainer.addEventListener("click", () => {
510-
this.updateConstraints();
511-
});
512355

513356
audioCheckContainer.addEventListener("click", () => {
514357
this.updateConstraints();
515358
});
516-
517-
// ! Final step for avCheckContainer - add a decibel meter below audio option for live monitoring.
518-
videoCheckContainer.append(videoLabel);
519-
videoCheckContainer.appendChild(this.#videoCheckbox);
520359

521360
audioCheckContainer.append(audioLabel);
522361
audioCheckContainer.appendChild(this.#audioCheckbox);
@@ -549,7 +388,6 @@ export class Audio {
549388
cameraContainer.appendChild(closeButton);
550389

551390
cameraContainer.appendChild(videoContainer);
552-
videoContainer.appendChild(videoElement);
553391
videoContainer.appendChild(audioContainer);
554392
audioContainer.appendChild(this.#audioSelector);
555393
audioContainer.appendChild(audioMonitorContainer);
@@ -579,19 +417,6 @@ export class Audio {
579417
* @param {String} elementOption - String value of option that needs to was checked
580418
*/
581419
async checkboxConstraintHelper() {
582-
if (this.#videoCheckbox.checked) {
583-
// Turning on Video and setting constraints
584-
this.#constraints.video = {
585-
deviceId: this.#deviceId,
586-
width: { ideal: this.#videoResolutionWidth },
587-
height: { ideal: this.#videoResolutionHeight }
588-
};
589-
} else if (!this.#videoCheckbox.checked) {
590-
// Unchecks video and turns off constraints
591-
this.#constraints.video = false;
592-
593-
}
594-
595420
if (this.#audioCheckbox.checked) {
596421
// Preview is off and audio isn't checked, so check
597422
const audioSource = this.#audioSelector.value;

JS_Files/camera.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,16 +286,12 @@ export class Camera {
286286
})
287287
}
288288
try {
289-
//if (this.#isVisible){
290-
// Checking for false b/c true value is updated to false on click
291-
if (!this.#audioCheckbox.checked){
292-
// console.log("closing audio");
293-
await this.closeAudioContext();
294-
}
295-
//}
289+
if (this.#audioContext.state != "closed"){
290+
await this.closeAudioContext();
291+
}
296292
}
297293
catch {
298-
console.log("camera not open");
294+
console.log("audio not open");
299295
}
300296
//console.log("[camera.js:stopStream()] - Camera has been stopped");
301297

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ChronoSense",
3-
"version": "1.6.0",
3+
"version": "1.6.1",
44
"description": "Chronosense Electron Application",
55
"main": "main.js",
66
"scripts": {

0 commit comments

Comments
 (0)