UNPKG

robbie-sdk

Version:

Robbie Visio SDK to send events for analaysis

71 lines (64 loc) 2.04 kB
export const FRAME_RATE = 2000; export default class Session { constructor(id, robbieSdk) { this._id = id; this._isOpened = true; this._intervalCallback = undefined; this._robbieSdk = robbieSdk; this._video = robbieSdk._video; this._canvas = robbieSdk._canvas; this._robbieApi = robbieSdk.robbieApi; } containsId() { return this._id != null && this._id !== ''; } startRecording(onEmotionsReceived) { return new Promise((resolve, reject) => { if (!this.containsId() || !this.isOpened) { reject(new Error('Session does not contains an id')); } this._robbieSdk.validateCameraPermissions() .then((stream) => { this._video.src = window.URL.createObjectURL(stream); }) .then(() => { this._intervalCallback = setInterval(() => { const frame = this._getVideoFrameString(); this._robbieApi.requestEmotions(this._id, frame) .then((_response) => { resolve(_response); if (onEmotionsReceived != null) { onEmotionsReceived(_response); } }); }, FRAME_RATE); const frame = this._getVideoFrameString(); return this._robbieApi.requestEmotions(this._id, frame) .then((response) => { resolve(response); if (onEmotionsReceived != null) { onEmotionsReceived(response); } }); }); }); } get id() { return this._id; } get isOpened() { return this._isOpened; } close() { this._isOpened = false; clearInterval(this._intervalCallback); } _getVideoFrameString() { const width = this._canvas.width; const height = this._canvas.height; const ctx = this._canvas.getContext('2d'); ctx.drawImage(this._video, 0, 0, width, height); const dataURL = this._canvas.toDataURL('image/jpeg', 0.90); return dataURL.replace(/^data:image\/jpeg;base64,/, ''); } }