File size: 4,110 Bytes
1397f77 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
// recorder.js
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const BLOB_TYPE = "video/webm";
class ScreenCastRecorder {
/** True if the current browser likely supports screencasts. */
static isSupportedBrowser() {
return (navigator.mediaDevices != null &&
navigator.mediaDevices.getUserMedia != null &&
navigator.mediaDevices.getDisplayMedia != null &&
MediaRecorder.isTypeSupported(BLOB_TYPE));
}
constructor({ recordAudio, onErrorOrStop }) {
this.recordAudio = recordAudio;
this.onErrorOrStopCallback = onErrorOrStop;
this.inputStream = null;
this.recordedChunks = [];
this.mediaRecorder = null;
}
/**
* This asynchronous method will initialize the screen recording object asking
* for permissions to the user which are needed to start recording.
*/
initialize() {
return __awaiter(this, void 0, void 0, function* () {
const desktopStream = yield navigator.mediaDevices.getDisplayMedia({
video: true,
});
let tracks = desktopStream.getTracks();
if (this.recordAudio) {
const voiceStream = yield navigator.mediaDevices.getUserMedia({
video: false,
audio: true,
});
tracks = tracks.concat(voiceStream.getAudioTracks());
}
this.recordedChunks = [];
this.inputStream = new MediaStream(tracks);
this.mediaRecorder = new MediaRecorder(this.inputStream, {
mimeType: BLOB_TYPE,
});
this.mediaRecorder.ondataavailable = e => this.recordedChunks.push(e.data);
});
}
getState() {
if (this.mediaRecorder) {
return this.mediaRecorder.state;
}
return "inactive";
}
/**
* This method will start the screen recording if the user has granted permissions
* and the mediaRecorder has been initialized
*
* @returns {boolean}
*/
start() {
if (!this.mediaRecorder) {
console.warn(`ScreenCastRecorder.start: mediaRecorder is null`);
return false;
}
const logRecorderError = (e) => {
console.warn(`mediaRecorder.start threw an error: ${e}`);
};
this.mediaRecorder.onerror = (e) => {
logRecorderError(e);
this.onErrorOrStopCallback();
};
this.mediaRecorder.onstop = () => this.onErrorOrStopCallback();
try {
this.mediaRecorder.start();
}
catch (e) {
logRecorderError(e);
return false;
}
return true;
}
/**
* This method will stop recording and then return the generated Blob
*
* @returns {(Promise|undefined)}
* A Promise which will return the generated Blob
* Undefined if the MediaRecorder could not initialize
*/
stop() {
if (!this.mediaRecorder) {
return undefined;
}
let resolver;
const promise = new Promise(r => {
resolver = r;
});
this.mediaRecorder.onstop = () => resolver();
this.mediaRecorder.stop();
if (this.inputStream) {
this.inputStream.getTracks().forEach(s => s.stop());
this.inputStream = null;
}
return promise.then(() => this.buildOutputBlob());
}
buildOutputBlob() {
return new Blob(this.recordedChunks, { type: BLOB_TYPE });
}
} |