UNPKG

lavva.exalushome.webcams

Version:

Library implementing webcams and abstraction layers for webcams configuration API in ExalusHome system

142 lines 6.52 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 { Guid } from "lavva.exalushome/build/js/Guid"; import { ExalusConnectionService } from 'lavva.exalushome/build/js/Services/ExalusConnectionService'; import { Api } from 'lavva.exalushome'; import { LoggerService } from 'lavva.exalushome/build/js/Services/Logging/LoggerService'; export class WebCam { constructor() { this._domain = "https://camproxy.lavva.cloud"; this._baseServerUrl = `${this._domain}/api/webrtc/`; this._pc = new RTCPeerConnection(); this._log = Api.Get(LoggerService.ServiceName); this._channel = null; } /// here should be something like this: /// this.videoElement = React.createRef(); //private videoElement: any = {}; SetDeviceChannel(channel) { this._channel = channel; } _GetRTCStream(channel, onStreamReady) { return __awaiter(this, void 0, void 0, function* () { this._log.Info(`Getting RTC stream for channel: ${channel.Name}.`); let connection = Api.Get(ExalusConnectionService.ServiceName); let serial = connection.GetControllerSerialNumber(); let key = channel.Configurations["camera_key"]; let data = channel.Configurations["camera_data"]; this._log.Info(`Getting RTC stream for channel: ${channel.Name} with serial: ${serial} and key: ${key} and data: ${data}.`); this._pc = new RTCPeerConnection(); let tmpPc = this._pc; this._log.Info(`Created new peer connection.`); let streamId = Guid.NewGuid(); /// change URL to send encrypted data let getOfferUrl = `${this._baseServerUrl}getcam`; let req = new RequestData(); req.guid = streamId; req.controllerSerial = serial; req.key = key; req.data = data; this._log.Info(`Getting data from: ${getOfferUrl}.`); /// is this needed? let setIceCandidateUrl = `${this._baseServerUrl}addicecandidate?id=${streamId}`; /// is this needed? let setAnswerUrl = `${this._baseServerUrl}setanswer?id=${streamId}`; let log = this._log; this._pc.ontrack = ({ track, streams: [stream] }) => { track.onunmute = () => { log.Info("Adding track to video control."); onStreamReady(stream); //this.videoElement.current.srcObject = stream; }; }; this._pc.onicecandidate = function (event) { return __awaiter(this, void 0, void 0, function* () { if (event.candidate) { log.Info('new ice candidate:'); log.Info(event.candidate.candidate); log.Info("JSON: " + JSON.stringify(event.candidate.toJSON())); yield fetch(setIceCandidateUrl, { method: 'POST', body: JSON.stringify(event.candidate), headers: { 'Content-Type': 'application/json' } }); } }); }; this._pc.onicegatheringstatechange = function () { log.Info("onicegatheringstatechange: " + tmpPc.iceGatheringState); }; this._pc.oniceconnectionstatechange = function () { log.Info("oniceconnectionstatechange: " + tmpPc.iceConnectionState); }; this._log.Info(`Getting offer from: ${getOfferUrl}.`); let offerResponse = yield fetch(getOfferUrl, { method: "post", headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify(req) }); let offer = yield offerResponse.json(); log.Info("got stream offer: " + offer.type + " " + offer.sdp + "."); yield this._pc.setRemoteDescription(offer); this._pc.createAnswer().then(function (answer) { return tmpPc.setLocalDescription(answer); }).then(function () { return __awaiter(this, void 0, void 0, function* () { var _a; log.Info("Sending answer SDP."); log.Info("SDP: " + ((_a = tmpPc.localDescription) === null || _a === void 0 ? void 0 : _a.sdp)); yield fetch(setAnswerUrl, { method: 'POST', body: JSON.stringify(tmpPc.localDescription), headers: { 'Content-Type': 'application/json' } }); }); }); }); } StopPeerAsync() { let log = this._log; return new Promise((resolve, reject) => { let pc = this._pc; if (pc != null) { log.Info("Closing peer connection"); pc.close(); } }); } GetRTCMediaStreamAsync() { return __awaiter(this, void 0, void 0, function* () { let ch = this._channel; let log = this._log; return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { try { yield this._GetRTCStream(ch, (stream) => resolve(stream)); } catch (e) { log.Error(`Failed to get RTC stream. Error: ${e}.`); reject(e); } })); }); } } class RequestData { constructor() { this.guid = ""; this.controllerSerial = ""; this.key = null; this.data = null; } } //# sourceMappingURL=WebCam.js.map