@@ -8,21 +8,15 @@ export class Audio {
8
8
#kind = "audioinput" ;
9
9
#label = "Audio Only" ;
10
10
#videoElement = null ;
11
- #canvasElement = null ;
12
- #canvasContext = null ;
13
11
#audioSelector = null ;
14
12
#recordCheckbox = null ;
15
- #videoCheckbox = null ;
16
13
#audioCheckbox = null ;
17
14
#audioMonitorUI = null ;
18
15
#audioContext = null ;
19
16
#pluginDiv = null ;
20
17
#constraints = { audio : false , video : false }
21
18
#stream = null ;
22
19
23
- #videoResolutionWidth = 1280 ; //Default to 1280 - for best FOV
24
- #videoResolutionHeight = 720 ; //Default to 720 - for best FOV
25
-
26
20
#isVisible = false ;
27
21
#isRecording = false ;
28
22
#fileNameInputElement = null ;
@@ -77,92 +71,6 @@ export class Audio {
77
71
return this . #kind;
78
72
}
79
73
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
-
166
74
monitorAudio ( stream ) {
167
75
var max_level_L = 0 ;
168
76
var old_level_L = 0 ;
@@ -201,19 +109,10 @@ export class Audio {
201
109
*/
202
110
203
111
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
-
213
112
/* Second check that this object has a deviceId set */
214
113
if ( this . #deviceId === null ) {
215
114
console . log (
216
- "[camera .js:startStream()] - ERROR: deviceId NOT set"
115
+ "[audio .js:startStream()] - ERROR: deviceId NOT set"
217
116
) ;
218
117
219
118
return false ;
@@ -222,47 +121,20 @@ export class Audio {
222
121
/* Try to open and start the stream */
223
122
try {
224
123
this . #stream = await navigator . mediaDevices . getUserMedia ( this . #constraints) ;
225
- if ( this . #videoCheckbox. checked ) {
226
- this . #videoElement. srcObject = this . #stream;
227
- }
228
124
if ( this . #audioCheckbox. checked ) {
229
125
this . monitorAudio ( this . #stream) ;
230
126
}
231
127
232
128
} catch ( err ) {
233
129
console . log (
234
- `camera .js:startStream()] - ERROR: ${ err . message } `
130
+ `audio .js:startStream()] - ERROR: ${ err . message } `
235
131
) ;
236
132
return false ;
237
133
}
238
134
239
135
return true ;
240
136
}
241
137
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
-
266
138
async closeAudioContext ( ) {
267
139
try {
268
140
await this . #audioContext. close ( ) ;
@@ -279,27 +151,15 @@ export class Audio {
279
151
*
280
152
*/
281
153
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
- }
291
154
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
+ }
298
158
}
299
159
catch {
300
- console . log ( "camera not open" ) ;
160
+ console . log ( "audio not open" ) ;
301
161
}
302
- //console.log("[camera .js:stopStream()] - Camera has been stopped");
162
+ //console.log("[audio .js:stopStream()] - Camera has been stopped");
303
163
304
164
}
305
165
@@ -409,7 +269,6 @@ export class Audio {
409
269
let videoElement = document . createElement ( "video" ) ;
410
270
let canvasElement = document . createElement ( "canvas" ) ;
411
271
let avCheckContainer = document . createElement ( "div" ) ;
412
- let videoCheckContainer = document . createElement ( "div" ) ;
413
272
let audioCheckContainer = document . createElement ( "div" ) ;
414
273
let fileNameContainer = document . createElement ( "div" ) ;
415
274
let recordCheckContainer = document . createElement ( "div" ) ;
@@ -428,8 +287,6 @@ export class Audio {
428
287
videoElement . autoplay = false ;
429
288
videoElement . muted = false ;
430
289
//videoElement.classList.add("camera-canvas");
431
-
432
- this . setInputAndOutput ( videoElement , canvasElement ) ; //Connect elements to class variables.
433
290
434
291
// Build Close Button
435
292
closeButton . classList . add ( "close-button" ) ;
@@ -477,14 +334,6 @@ export class Audio {
477
334
avCheckContainer . style . height = "2em" ;
478
335
479
336
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: ' ) ) ;
488
337
489
338
this . #audioCheckbox = document . createElement ( "input" ) ;
490
339
this . #audioCheckbox. type = 'checkbox' ;
@@ -494,29 +343,19 @@ export class Audio {
494
343
audioLabel . appendChild ( document . createTextNode ( 'Audio: ' ) ) ;
495
344
496
345
this . #recordCheckbox. classList . add ( 'flipswitch' ) ;
497
- this . #videoCheckbox. classList . add ( 'flipswitch' ) ;
498
346
this . #audioCheckbox. classList . add ( 'flipswitch' ) ;
499
347
500
348
this . #recordCheckbox. classList . add ( 'checkbox-disabled' ) ;
501
- this . #videoCheckbox. classList . add ( 'checkbox-disabled' ) ;
502
349
this . #audioCheckbox. classList . add ( 'checkbox-disabled' ) ;
503
350
//this.#audioSelector.classList.add('checkbox-disabled')
504
351
505
352
recordCheckContainer . addEventListener ( "click" , ( ) => {
506
353
this . updateRecordStatus ( ) ;
507
354
} ) ;
508
-
509
- videoCheckContainer . addEventListener ( "click" , ( ) => {
510
- this . updateConstraints ( ) ;
511
- } ) ;
512
355
513
356
audioCheckContainer . addEventListener ( "click" , ( ) => {
514
357
this . updateConstraints ( ) ;
515
358
} ) ;
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) ;
520
359
521
360
audioCheckContainer . append ( audioLabel ) ;
522
361
audioCheckContainer . appendChild ( this . #audioCheckbox) ;
@@ -549,7 +388,6 @@ export class Audio {
549
388
cameraContainer . appendChild ( closeButton ) ;
550
389
551
390
cameraContainer . appendChild ( videoContainer ) ;
552
- videoContainer . appendChild ( videoElement ) ;
553
391
videoContainer . appendChild ( audioContainer ) ;
554
392
audioContainer . appendChild ( this . #audioSelector) ;
555
393
audioContainer . appendChild ( audioMonitorContainer ) ;
@@ -579,19 +417,6 @@ export class Audio {
579
417
* @param {String } elementOption - String value of option that needs to was checked
580
418
*/
581
419
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
-
595
420
if ( this . #audioCheckbox. checked ) {
596
421
// Preview is off and audio isn't checked, so check
597
422
const audioSource = this . #audioSelector. value ;
0 commit comments