UNPKG

infobip-rtc

Version:

Infobip RTC JavaScript SDK - Infobip WebRTC API Implementation

202 lines 9.11 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 "webrtc-adapter"; import { CameraOrientation } from "../call/options/CameraOrientation"; import _ from "lodash"; import { VideoOptionsUtil } from "../util/VideoOptionsUtil"; export class DefaultDevice { constructor(logger) { this.logger = logger; this.deviceKindNames = { "audioinput": "Microphone", "audiooutput": "Speaker", "videoinput": "Camera" }; this.getMicrophoneDefaults = () => ({ noiseSuppression: true, echoCancellation: true }); this.getCameraDefaults = (hdResolution = false, cameraVideoFrameRate) => ({ width: hdResolution ? 1280 : 640, height: hdResolution ? 720 : 480, frameRate: VideoOptionsUtil.limitFps(cameraVideoFrameRate) || DefaultDevice.CAMERA_VIDEO_FPS_DEFAULT }); this.getScreenShareDefaults = (screenShareFrameRate) => ({ width: { max: 1920 }, height: { max: 1080 }, frameRate: VideoOptionsUtil.limitFps(screenShareFrameRate) || DefaultDevice.SCREEN_SHARE_FPS_DEFAULT, cursor: "always" }); } getAudioInputDevices() { return this.getDevices("audioinput") .then(devices => this.updateDevices(devices)); } getAudioOutputDevices() { return this.getDevices("audiooutput") .then(devices => this.updateDevices(devices)); } getVideoInputDevices() { return this.getDevices("videoinput") .then(devices => this.updateDevices(devices)); } setAudioInputDevice(id) { this.audioInputDevice = { deviceId: id, kind: "audioinput" }; } unsetAudioInputDevice() { this.audioInputDevice = null; } getAudioInputDevice() { var _a; return (_a = this.audioInputDevice) === null || _a === void 0 ? void 0 : _a.deviceId; } audioInputDeviceShouldChange() { return this.getAudioInputDevices() .then(devices => { return devices.findIndex(device => { var _a; return device.deviceId === ((_a = this.audioInputDevice) === null || _a === void 0 ? void 0 : _a.deviceId); }) === -1 && devices.length > 0; }) .catch(() => { return false; }); } setVideoInputDevice(id) { this.videoInputDevice = { deviceId: id, kind: "videoinput" }; } unsetVideoInputDevice() { this.videoInputDevice = null; } getVideoInputDevice() { var _a; return (_a = this.videoInputDevice) === null || _a === void 0 ? void 0 : _a.deviceId; } getCameraOrientation() { return this.cameraOrientation; } getLocalStream(audio = true, video = false, cameraOrientation = CameraOrientation.FRONT, hdResolution = false, useExactDevice = true, cameraVideoFrameRate) { return __awaiter(this, void 0, void 0, function* () { let mediaConstraints; let devices; try { this.cameraOrientation = cameraOrientation; if (this.audioInputDevice || this.videoInputDevice) { devices = yield this.getDevices(); const audioDeviceConstraints = this.getDeviceConstraint(devices, this.audioInputDevice); const videoDeviceConstraints = this.getDeviceConstraint(devices, this.videoInputDevice, useExactDevice); const audioConstraints = audio && this.getAudioConstraints(audioDeviceConstraints); const videoConstraints = video && this.getVideoConstraints(videoDeviceConstraints, cameraOrientation, hdResolution, cameraVideoFrameRate); mediaConstraints = { audio: audioConstraints, video: videoConstraints }; } else { let audioConstraints = audio && this.getAudioConstraints(null); let videoConstraints = video && this.getVideoConstraints(null, cameraOrientation, hdResolution, cameraVideoFrameRate); mediaConstraints = { audio: audioConstraints, video: videoConstraints }; } return yield navigator.mediaDevices.getUserMedia(mediaConstraints); } catch (error) { if (error) { this.logger.error(`Failed to request device audio=${audio} video=${video} with constraints ${mediaConstraints && JSON.stringify(mediaConstraints)} (${devices && JSON.stringify(devices)}) - ${error}`); } throw error; } }); } getDisplayMedia(displayOptions, screenShareFrameRate) { var _a, _b, _c; const constraints = Object.assign({ video: this.getScreenShareDefaults(screenShareFrameRate) }, (displayOptions && { monitorTypeSurfaces: "include" })); if (displayOptions) { constraints.video.displaySurface = displayOptions.allowedDisplayOptions[0]; } if (displayOptions && !displayOptions.allowedDisplayOptions.includes("monitor")) { constraints.monitorTypeSurfaces = "exclude"; } return (_c = (_b = (_a = navigator.mediaDevices).getDisplayMedia) === null || _b === void 0 ? void 0 : _b.call(_a, constraints)) !== null && _c !== void 0 ? _c : Promise.reject("Screen share is not supported."); } getAudioConstraints(deviceId = null) { return Object.assign(Object.assign({}, this.getMicrophoneDefaults()), { deviceId }); } getVideoConstraints(deviceId = null, cameraOrientation = CameraOrientation.FRONT, hdResolution = false, cameraVideoFrameRate) { return Object.assign(Object.assign({}, this.getCameraDefaults(hdResolution, cameraVideoFrameRate)), { facingMode: cameraOrientation, deviceId }); } getDeviceConstraint(devices, inputDevice, useExactDevice = true) { if (!inputDevice) return null; let videoInputDeviceExists = _.findIndex(devices, { kind: inputDevice.kind, deviceId: inputDevice.deviceId }) !== -1; return videoInputDeviceExists && useExactDevice ? { exact: inputDevice.deviceId } : null; } getDevices(deviceKind = null) { return this.enumerateDevices(deviceKind) .then(mediaDevices => { if (mediaDevices.length === 0) { return []; } if (mediaDevices[0].label) { return this.filterDevices(mediaDevices, deviceKind); } else { return this.getUserMediaDevices(deviceKind); } }); } filterDevices(mediaDeviceInfos, deviceKind) { if (deviceKind) { let devices = mediaDeviceInfos.filter(device => device.kind === deviceKind); return _.uniqBy(devices, 'deviceId'); } return _.uniqBy(mediaDeviceInfos, 'deviceId'); } getUserMediaDevices(deviceKind) { let constraints = deviceKind === 'videoinput' ? { video: true } : { audio: true }; return navigator.mediaDevices.getUserMedia(constraints).then(this.stopTracks) .then(() => this.enumerateDevices(deviceKind)); } enumerateDevices(deviceKind) { return navigator.mediaDevices.enumerateDevices() .then(mediaDevices => this.filterDevices(mediaDevices, deviceKind)); } stopTracks(stream) { stream.getTracks().forEach((track) => track.stop()); } updateDevices(devices) { return devices.map((device, index) => { return this.generateCustomMediaDeviceInfo(device, index + 1); }); } generateCustomMediaDeviceInfo(mediaDeviceInfo, index) { let deviceKindName = this.deviceKindNames[mediaDeviceInfo.kind]; let customMediaDeviceInfo = mediaDeviceInfo.toJSON(); if (!mediaDeviceInfo.label) { customMediaDeviceInfo.label = mediaDeviceInfo.deviceId === "default" ? `Default ${deviceKindName}` : `${deviceKindName} ${index}`; } return customMediaDeviceInfo; } } DefaultDevice.CAMERA_VIDEO_FPS_DEFAULT = 24; DefaultDevice.SCREEN_SHARE_FPS_DEFAULT = 8; //# sourceMappingURL=DefaultDevice.js.map