UNPKG

infobip-rtc

Version:

Infobip RTC JavaScript SDK - Infobip WebRTC API Implementation

75 lines 3.82 kB
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()); }); }; import { AbstractCapturer } from "./AbstractCapturer"; import { DefaultHttpClient } from "../../util/HttpClient"; export class DefaultServerCapturer extends AbstractCapturer { constructor(localCameraStream, localScreenShareStream, remotePeerConnection, mid, currentUserIdentity, token, apiUrl, logger) { super(localCameraStream, localScreenShareStream, remotePeerConnection, mid, currentUserIdentity); this.token = token; this.apiUrl = apiUrl; this.logger = logger; this.initializeHttpClient(); } takeScreenshot(identity, videoType) { return __awaiter(this, void 0, void 0, function* () { let fileResponse = null; try { const canvas = yield this.getCanvas(identity, videoType); const file = yield this.toFile(canvas, `${identity}_${videoType.toString().toLowerCase()}_${Date.now()}.png`); fileResponse = yield this.uploadImage(file); } catch (e) { throw new Error(DefaultServerCapturer.GENERIC_FILE_UPLOAD_ERROR); } if (fileResponse.ok) { this.logger.info("Successfully uploaded file: " + JSON.stringify(fileResponse.data)); return fileResponse.data; } else { throw new Error(this.getErrorMessage(fileResponse.error)); } }); } initializeHttpClient() { this.httpClient = new DefaultHttpClient(`https://${this.apiUrl}`, { "Authorization": `Bearer ${this.token}` }); } uploadImage(file) { return __awaiter(this, void 0, void 0, function* () { let formData = new FormData(); formData.append("file", file); formData.append("fileFormat", "PNG"); return yield this.httpClient.post("/webrtc/1/files", formData); }); } toFile(canvas, fileName) { return __awaiter(this, void 0, void 0, function* () { return new Promise(((resolve, reject) => { const timeoutId = setTimeout(() => { reject("File generation timed out."); }, DefaultServerCapturer.TIMEOUT); canvas.toBlob(blob => { clearTimeout(timeoutId); resolve(new File([blob], fileName, { type: "image/png" })); }); })); }); } getErrorMessage(error) { var _a, _b; if (((_b = (_a = error === null || error === void 0 ? void 0 : error.requestError) === null || _a === void 0 ? void 0 : _a.serviceException) === null || _b === void 0 ? void 0 : _b.text) === DefaultServerCapturer.FILE_STORAGE_NOT_ENABLED_ERROR) { return DefaultServerCapturer.FILE_STORAGE_NOT_ENABLED_ERROR; } return DefaultServerCapturer.GENERIC_FILE_UPLOAD_ERROR; } } DefaultServerCapturer.TIMEOUT = 3000; DefaultServerCapturer.FILE_STORAGE_NOT_ENABLED_ERROR = "File storage is not enabled for account."; DefaultServerCapturer.GENERIC_FILE_UPLOAD_ERROR = "File upload failed."; //# sourceMappingURL=DefaultServerCapturer.js.map