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 {
2021-07-16 09:47:51 +02:00
createSingleServer,
2020-11-24 14:08:23 +01:00
killallServers,
sendRTMPStream,
2021-07-16 09:47:51 +02:00
PeerTubeServer,
2020-11-24 14:08:23 +01:00
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.')
2021-07-22 12:07:26 +02:00
const server = await createSingleServer(1, {}, { hideLogs: false, nodeArgs: [ '--inspect' ] })
2020-11-24 14:08:23 +01:00
2021-07-12 16:06:57 +02:00
const cleanup = async () => {
2020-11-24 14:08:23 +01:00
console.log('Killing server')
2021-07-09 15:37:43 +02:00
await killallServers([ server ])
2020-11-24 14:08:23 +01:00
}
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,
2021-07-16 09:04:35 +02:00
channelId: server.store.channel.id,
2020-11-24 14:08:23 +01:00
privacy: VideoPrivacy.PUBLIC
}
console.log('Creating live.')
2021-07-16 09:04:35 +02:00
const { uuid: liveVideoUUID } = await server.live.create({ fields: attributes })
2020-11-24 14:08:23 +01:00
2021-07-16 09:04:35 +02:00
const live = await server.live.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)
})
}
// ----------------------------------------------------------------------------
2021-07-16 09:47:51 +02:00
async function buildConfig (server: PeerTubeServer, commandType: CommandType) {
2021-07-16 09:04:35 +02:00
await server.config.updateCustomSubConfig({
2021-07-09 15:03:44 +02:00
newConfig: {
instance: {
customizations: {
javascript: '',
css: ''
}
},
live: {
enabled: true,
allowReplay: true,
transcoding: {
enabled: commandType === 'live-transcoding'
}
2020-11-24 14:08:23 +01:00
}
}
})
}