homebridge-ezviz
Version:
EZVIZ plugin for homebridge: https://homebridge.io/
73 lines • 2.85 kB
JavaScript
;
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.reservePorts = exports.RtpSplitter = void 0;
const dgram_1 = require("dgram");
const get_port_1 = __importDefault(require("get-port"));
function getPayloadType(message) {
return message.readUInt8(1) & 0x7f;
}
function isRtpMessage(message) {
const payloadType = getPayloadType(message);
return payloadType > 90 || payloadType === 0;
}
class RtpSplitter {
constructor(serverPort, audioRTCPPort, returnAudioPort) {
this.socket = dgram_1.createSocket('udp4');
const socket = this.socket;
socket.on('error', (error) => {
console.log('Error: ' + error);
socket.close();
});
socket.on('message', (msg) => {
if (isRtpMessage(msg)) {
if (msg.length > 50) {
socket.send(msg, returnAudioPort, 'localhost');
}
else {
socket.send(msg, audioRTCPPort, 'localhost');
}
}
else {
socket.send(msg, audioRTCPPort, 'localhost');
socket.send(msg, returnAudioPort, 'localhost');
}
});
socket.bind(serverPort);
}
close() {
this.socket.close();
}
}
exports.RtpSplitter = RtpSplitter;
function reservePorts(count = 1) {
return __awaiter(this, void 0, void 0, function* () {
const port = yield get_port_1.default();
const ports = [port];
const tryAgain = () => {
return reservePorts(count);
};
for (let i = 1; i < count; i++) {
const targetConsecutivePort = port + i;
const openPort = yield get_port_1.default({ port: targetConsecutivePort });
if (openPort !== targetConsecutivePort) {
return tryAgain();
}
ports.push(openPort);
}
return ports;
});
}
exports.reservePorts = reservePorts;
//# sourceMappingURL=rtp.js.map