Add Streamdelay integration

This commit is contained in:
Max Goodhart
2020-06-27 23:44:05 -07:00
parent b958f21a7d
commit d85897f68c
5 changed files with 159 additions and 3 deletions

View File

@@ -0,0 +1,48 @@
import EventEmitter from 'events'
import * as url from 'url'
import WebSocket from 'ws'
import ReconnectingWebSocket from 'reconnecting-websocket'
export default class StreamdelayClient extends EventEmitter {
constructor({ endpoint, key }) {
super()
this.endpoint = endpoint
this.key = key
this.ws = null
this.status = null
}
connect() {
const wsURL = url.resolve(this.endpoint, `ws?key=${this.key}`)
const ws = (this.ws = new ReconnectingWebSocket(wsURL, [], {
WebSocket,
maxReconnectionDelay: 5000,
minReconnectionDelay: 1000 + Math.random() * 500,
reconnectionDelayGrowFactor: 1.1,
}))
ws.addEventListener('open', () => this.emitState())
ws.addEventListener('close', () => this.emitState())
ws.addEventListener('message', (ev) => {
let data
try {
data = JSON.parse(ev.data)
} catch (err) {
console.error('invalid JSON from streamdelay:', ev.data)
return
}
this.status = data.status
this.emitState()
})
}
emitState() {
this.emit('state', {
isConnected: this.ws.readyState === WebSocket.OPEN,
...this.status,
})
}
setCensored(isCensored) {
this.ws.send(JSON.stringify({ isCensored }))
}
}

View File

@@ -5,6 +5,7 @@ import { app, shell, session, BrowserWindow } from 'electron'
import { ensureValidURL } from '../util'
import { pollPublicData, StreamIDGenerator } from './data'
import StreamWindow from './StreamWindow'
import StreamdelayClient from './StreamdelayClient'
import initWebServer from './server'
async function main() {
@@ -52,6 +53,13 @@ async function main() {
describe: 'Background color of wall (useful for chroma-keying)',
default: '#000',
})
.option('streamdelay-endpoint', {
describe: 'URL of Streamdelay endpoint',
default: 'http://localhost:8404',
})
.option('streamdelay-key', {
describe: 'Streamdelay API key',
})
.help().argv
// Reject all permission requests from web content.
@@ -69,8 +77,14 @@ async function main() {
streamWindow.init()
let browseWindow = null
let streamdelayClient = null
const clientState = { streams: [], customStreams: [], views: [] }
const clientState = {
streams: [],
customStreams: [],
views: [],
streamdelay: false,
}
const getInitialState = () => clientState
let broadcastState = () => {}
const onMessage = (msg) => {
@@ -113,6 +127,8 @@ async function main() {
} else if (msg.type === 'dev-tools') {
streamWindow.openDevTools(msg.viewIdx, browseWindow.webContents)
}
} else if (msg.type === 'set-stream-censored' && streamdelayClient) {
streamdelayClient.setCensored(msg.isCensored)
}
}
@@ -134,6 +150,18 @@ async function main() {
}
}
if (argv.streamdelayKey) {
streamdelayClient = new StreamdelayClient({
endpoint: argv.streamdelayEndpoint,
key: argv.streamdelayKey,
})
streamdelayClient.on('state', (state) => {
clientState.streamdelay = state
broadcastState(clientState)
})
streamdelayClient.connect()
}
streamWindow.on('state', (viewStates) => {
clientState.views = viewStates
streamWindow.send('state', clientState)