PeerTube/server/tools/test-live.ts

103 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-06-25 17:48:27 +02:00
import { program } from 'commander'
2021-07-09 15:03:44 +02:00
import { LiveVideoCreate, VideoPrivacy } from '@shared/models'
2020-11-24 14:08:23 +01:00
import {
flushAndRunServer,
killallServers,
sendRTMPStream,
ServerInfo,
setAccessTokensToServers,
2021-07-09 15:03:44 +02:00
setDefaultVideoChannel
2020-11-24 14:08:23 +01:00
} from '../../shared/extra-utils'
2021-07-09 15:03:44 +02:00
import { registerTSPaths } from '../helpers/register-ts-paths'
registerTSPaths()
2020-11-24 14:08:23 +01:00
type CommandType = 'live-mux' | 'live-transcoding'
registerTSPaths()
const command = program
2021-07-09 15:03:44 +02:00
.name('test-live')
2020-11-24 14:08:23 +01:00
.option('-t, --type <type>', 'live-muxing|live-transcoding')
.parse(process.argv)
run()
.catch(err => {
console.error(err)
process.exit(-1)
})
async function run () {
const commandType: CommandType = command['type']
if (!commandType) {
console.error('Miss command type')
process.exit(-1)
}
console.log('Starting server.')
2020-11-30 14:47:54 +01:00
const server = await flushAndRunServer(1, {}, [], { hideLogs: false, execArgv: [ '--inspect' ] })
2020-11-24 14:08:23 +01:00
const cleanup = () => {
console.log('Killing server')
killallServers([ server ])
}
process.on('exit', cleanup)
process.on('SIGINT', cleanup)
await setAccessTokensToServers([ server ])
await setDefaultVideoChannel([ server ])
await buildConfig(server, commandType)
const attributes: LiveVideoCreate = {
name: 'live',
saveReplay: true,
channelId: server.videoChannel.id,
privacy: VideoPrivacy.PUBLIC
}
console.log('Creating live.')
2021-07-09 15:03:44 +02:00
const { uuid: liveVideoUUID } = await server.liveCommand.create({ fields: attributes })
2020-11-24 14:08:23 +01:00
2021-07-09 15:03:44 +02:00
const live = await server.liveCommand.get({ videoId: liveVideoUUID })
2020-11-24 14:08:23 +01:00
console.log('Sending RTMP stream.')
const ffmpegCommand = sendRTMPStream(live.rtmpUrl, live.streamKey)
ffmpegCommand.on('error', err => {
console.error(err)
process.exit(-1)
})
ffmpegCommand.on('end', () => {
console.log('ffmpeg ended')
process.exit(0)
})
}
// ----------------------------------------------------------------------------
async function buildConfig (server: ServerInfo, commandType: CommandType) {
2021-07-09 15:03:44 +02:00
await server.configCommand.updateCustomSubConfig({
newConfig: {
instance: {
customizations: {
javascript: '',
css: ''
}
},
live: {
enabled: true,
allowReplay: true,
transcoding: {
enabled: commandType === 'live-transcoding'
}
2020-11-24 14:08:23 +01:00
}
}
})
}