homebridge-eufy-security-mikebcbc
Version:
Control Eufy Security from homebridge.
97 lines • 4.67 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocalLivestreamManager = void 0;
const stream_1 = require("stream");
class LocalLivestreamManager extends stream_1.EventEmitter {
constructor(streamingDelegate) {
super();
this.streamingDelegate = streamingDelegate;
this.livestreamIsStarting = false;
this.platform = this.streamingDelegate.platform;
this.device = this.streamingDelegate.device;
this.log = this.platform.log;
this.stationStream = null;
this.livestreamStartedAt = null;
this.initialize();
this.platform.eufyClient.on('station livestream start', (station, device, metadata, videostream, audiostream) => {
this.onStationLivestreamStart(station, device, metadata, videostream, audiostream);
});
this.platform.eufyClient.on('station livestream stop', (station, device) => {
this.onStationLivestreamStop(station, device);
});
}
initialize() {
if (this.stationStream) {
this.stationStream.audiostream.unpipe();
this.stationStream.audiostream.destroy();
this.stationStream.videostream.unpipe();
this.stationStream.videostream.destroy();
}
this.stationStream = null;
this.livestreamStartedAt = null;
}
async getLocalLivestream() {
this.log.debug(this.streamingDelegate.cameraName, 'New instance requests livestream.');
if (this.stationStream) {
const runtime = (Date.now() - this.livestreamStartedAt) / 1000;
this.log.debug(this.streamingDelegate.cameraName, 'Using livestream that was started ' + runtime + ' seconds ago.');
return this.stationStream;
}
else {
return await this.startAndGetLocalLiveStream();
}
}
async startAndGetLocalLiveStream() {
return new Promise((resolve, reject) => {
this.log.debug(this.streamingDelegate.cameraName, 'Start new station livestream...');
if (!this.livestreamIsStarting) { // prevent multiple stream starts from eufy station
this.livestreamIsStarting = true;
this.platform.eufyClient.startStationLivestream(this.device.getSerial());
}
else {
this.log.debug(this.streamingDelegate.cameraName, 'stream is already starting. waiting...');
}
this.once('livestream start', async () => {
if (this.stationStream !== null) {
this.log.debug(this.streamingDelegate.cameraName, 'New livestream started.');
this.livestreamIsStarting = false;
resolve(this.stationStream);
}
else {
reject('no started livestream found');
}
});
});
}
stopLocalLiveStream() {
this.log.debug(this.streamingDelegate.cameraName, 'Stopping station livestream.');
this.platform.eufyClient.stopStationLivestream(this.device.getSerial());
this.initialize();
}
onStationLivestreamStop(station, device) {
if (device.getSerial() === this.device.getSerial()) {
this.log.debug(station.getName() + ' station livestream for ' + device.getName() + ' has stopped.');
this.initialize();
}
}
async onStationLivestreamStart(station, device, metadata, videostream, audiostream) {
if (device.getSerial() === this.device.getSerial()) {
if (this.stationStream) {
const diff = (Date.now() - this.stationStream.createdAt) / 1000;
if (diff < 5) {
this.log.warn(this.streamingDelegate.cameraName, 'Second livestream was started from station. Ignore.');
return;
}
}
this.initialize(); // important to prevent unwanted behaviour when the eufy station emits the 'livestream start' event multiple times
this.log.debug(station.getName() + ' station livestream (P2P session) for ' + device.getName() + ' has started.');
this.livestreamStartedAt = Date.now();
const createdAt = Date.now();
this.stationStream = { station, device, metadata, videostream, audiostream, createdAt };
this.log.debug(this.streamingDelegate.cameraName, 'Stream metadata: ' + JSON.stringify(this.stationStream.metadata));
this.emit('livestream start');
}
}
}
exports.LocalLivestreamManager = LocalLivestreamManager;
//# sourceMappingURL=LocalLivestreamManager.js.map