UNPKG

@viguza/homebridge-ezviz

Version:

A short description about what your plugin does.

59 lines 1.99 kB
import { createSocket } from 'dgram'; import getPort from 'get-port'; function getPayloadType(message) { return message.readUInt8(1) & 0x7f; } function isRtpMessage(message) { const payloadType = getPayloadType(message); return payloadType > 90 || payloadType === 0; } export class RtpSplitter { socket = createSocket('udp4'); constructor(serverPort, audioRTCPPort, returnAudioPort) { // emits when any error occurs const socket = this.socket; socket.on('error', (error) => { console.log('Error: ' + error); socket.close(); }); // emits on new datagram msg 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'); // Send RTCP to return audio as a heartbeat socket.send(msg, returnAudioPort, 'localhost'); } }); socket.bind(serverPort); } close() { this.socket.close(); } } // Need to reserve ports in sequence because ffmpeg uses the next port up by default. If it's taken, ffmpeg will error export async function reservePorts(count = 1) { const port = await getPort(); const ports = [port]; const tryAgain = () => { return reservePorts(count); }; for (let i = 1; i < count; i++) { const targetConsecutivePort = port + i; const openPort = await getPort({ port: targetConsecutivePort }); if (openPort !== targetConsecutivePort) { // can't reserve next port, bail and get another set return tryAgain(); } ports.push(openPort); } return ports; } //# sourceMappingURL=rtp.js.map