|
| 1 | +var recordSate = { |
| 2 | + STOPPED: 1, |
| 3 | + RECORDING: 2, |
| 4 | + NOT_AVAILABLE: 3, |
| 5 | + UNKNOWN: 4, |
| 6 | +}; |
| 7 | + |
| 8 | +const MIMETYPE = 'video/webm; codecs="opus,vp8"'; |
| 9 | +const RECORDER_TIME_SLICE = 300; |
| 10 | +const CAMERA_OPTIONS = { video: true, audio: true } |
| 11 | + |
| 12 | +class GunRecorder { |
| 13 | + constructor(config) { |
| 14 | + this.video = document.getElementById(config.video_id); |
| 15 | + this.mediaRecorder = null; |
| 16 | + this.onDataAvailable = config.onDataAvailable; |
| 17 | + this.onRecordStateChange = config.onRecordStateChange |
| 18 | + this.recorderOptions = { |
| 19 | + mimeType: MIMETYPE, |
| 20 | + audioBitsPerSecond: config.audioBitsPerSecond, |
| 21 | + videoBitsPerSecond: config.videoBitsPerSecond |
| 22 | + } |
| 23 | + this.debug = config.debug; |
| 24 | + this.setRecordingState(recordSate.UNKNOWN); |
| 25 | + } |
| 26 | + |
| 27 | + record() { |
| 28 | + if (this.recordSate == recordSate.RECORDING) { |
| 29 | + this.mediaRecorder.stop(); |
| 30 | + this.changeRecordState(); |
| 31 | + } else if (this.recordSate == recordSate.STOPPED) { |
| 32 | + this.mediaRecorder = new MediaRecorder(gunRecorder.video.captureStream(), this.recorderOptions); |
| 33 | + this.mediaRecorder.ondataavailable = gunRecorder.onDataAvailable; |
| 34 | + this.mediaRecorder.start(RECORDER_TIME_SLICE); |
| 35 | + this.changeRecordState(); |
| 36 | + } else { |
| 37 | + this.debugLog("The camera has not been initialized yet. First call startCamera()") |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + startCamera() { |
| 42 | + if (this.recordSate == recordSate.RECORDING || this.recordSate == recordSate.STOPPED) { |
| 43 | + this.debugLog("Camera already started no need to do again"); |
| 44 | + return; |
| 45 | + } |
| 46 | + var gunRecorder = this; |
| 47 | + if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { |
| 48 | + navigator.mediaDevices.getUserMedia(CAMERA_OPTIONS).then(function (stream) { |
| 49 | + gunRecorder.video.srcObject = stream; |
| 50 | + gunRecorder.video.play(); |
| 51 | + }); |
| 52 | + this.setRecordingState(recordSate.STOPPED); |
| 53 | + } else { |
| 54 | + this.setRecordingState(recordSate.NOT_AVAILABLE); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + changeRecordState() { |
| 59 | + switch (this.recordSate) { |
| 60 | + case recordSate.STOPPED: |
| 61 | + this.setRecordingState(recordSate.RECORDING); |
| 62 | + break; |
| 63 | + case recordSate.NOT_AVAILABLE: |
| 64 | + this.debugLog("Sorry camera not available") |
| 65 | + break; |
| 66 | + case recordSate.UNKNOWN: |
| 67 | + this.debugLog("State is unknown check if camera is intialized") |
| 68 | + break; |
| 69 | + default: |
| 70 | + this.setRecordingState(recordSate.STOPPED); |
| 71 | + break; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + setRecordingState(recordSate) { |
| 76 | + this.debugLog("STATE BEFORE::" + this.recordSate); |
| 77 | + this.recordSate = recordSate; |
| 78 | + this.onRecordStateChange(this.recordSate); |
| 79 | + this.debugLog("STATE AFTER::" + this.recordSate); |
| 80 | + } |
| 81 | + |
| 82 | + debugLog(logData) { |
| 83 | + if (this.debug) { |
| 84 | + console.log(logData); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments