@koush/ring-client-api
Version:
Unofficial API for Ring doorbells, cameras, security alarm system and smart lighting
94 lines (93 loc) • 3.9 kB
JavaScript
"use strict";
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LiveCallNegotiation = void 0;
const rtp_utils_1 = require("./rtp-utils");
const ws_1 = require("ws");
const rxjs_1 = require("rxjs");
const util_1 = require("./util");
const subscribed_1 = require("./subscribed");
class LiveCallNegotiation extends subscribed_1.Subscribed {
constructor(sessionId, camera) {
super();
this.sessionId = sessionId;
this.camera = camera;
this.onCallAnswered = new rxjs_1.ReplaySubject(1);
this.onCallEnded = new rxjs_1.ReplaySubject(1);
this.activated = false;
const liveCallSession = (0, rtp_utils_1.parseLiveCallSession)(sessionId);
this.ws = new ws_1.WebSocket(`wss://${liveCallSession.rms_fqdn}:${liveCallSession.webrtc_port}/`, {
headers: {
API_VERSION: '3.1',
API_TOKEN: sessionId,
CLIENT_INFO: 'Ring/3.48.0;Platform/Android;OS/7.0;Density/2.0;Device/samsung-SM-T710;Locale/en-US;TimeZone/GMT-07:00',
},
});
this.onMessage = (0, rxjs_1.fromEvent)(this.ws, 'message')
.pipe((0, rxjs_1.concatMap)((message) => __awaiter(this, void 0, void 0, function* () { return JSON.parse(message.data); })));
this.onWsOpen = (0, rxjs_1.fromEvent)(this.ws, 'open');
const onError = (0, rxjs_1.fromEvent)(this.ws, 'error'), onClose = (0, rxjs_1.fromEvent)(this.ws, 'close');
this.addSubscriptions(this.onWsOpen.subscribe(() => {
(0, util_1.logDebug)(`WebSocket connected for ${this.camera.name}`);
}), onError.subscribe((e) => {
(0, util_1.logError)(e);
this.callEnded();
}), onClose.subscribe(() => {
this.callEnded();
}));
}
sendMessage(message) {
this.ws.send(JSON.stringify(message));
}
sendAnswer(answer) {
this.sendMessage(Object.assign({ method: 'sdp' }, answer));
this.onCallAnswered.next();
}
activate() {
return __awaiter(this, void 0, void 0, function* () {
if (this.activated) {
return;
}
this.activated = true;
yield (0, rxjs_1.firstValueFrom)(this.onCallAnswered);
this.sendMessage({ method: 'activate_session' });
this.sendMessage({
video_enabled: true,
audio_enabled: true,
method: 'stream_options',
});
});
}
activateCameraSpeaker() {
return __awaiter(this, void 0, void 0, function* () {
yield (0, rxjs_1.firstValueFrom)(this.onCallAnswered);
this.sendMessage({
stealth_mode: false,
method: 'camera_options',
});
});
}
callEnded() {
try {
this.sendMessage({
reason: { code: 0, text: '' },
method: 'close',
});
this.ws.close();
}
catch (_) {
// ignore any errors since we are stopping the call
}
this.unsubscribe();
this.onCallEnded.next();
}
}
exports.LiveCallNegotiation = LiveCallNegotiation;