PeerTube/server/tools/test-live.ts

103 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-06-25 10:48:27 -05:00
import { program } from 'commander'
2021-07-09 08:03:44 -05:00
import { LiveVideoCreate, VideoPrivacy } from '@shared/models'
2020-11-24 07:08:23 -06:00
import {
2021-07-16 02:47:51 -05:00
createSingleServer,
2020-11-24 07:08:23 -06:00
killallServers,
sendRTMPStream,
2021-07-16 02:47:51 -05:00
PeerTubeServer,
2020-11-24 07:08:23 -06:00
setAccessTokensToServers,
2021-07-09 08:03:44 -05:00
setDefaultVideoChannel
2020-11-24 07:08:23 -06:00
} from '../../shared/extra-utils'
2021-07-09 08:03:44 -05:00
import { registerTSPaths } from '../helpers/register-ts-paths'
registerTSPaths()
2020-11-24 07:08:23 -06:00
type CommandType = 'live-mux' | 'live-transcoding'
registerTSPaths()
const command = program
2021-07-09 08:03:44 -05:00
.name('test-live')
2020-11-24 07:08:23 -06: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 05:07:26 -05:00
const server = await createSingleServer(1, {}, { hideLogs: false, nodeArgs: [ '--inspect' ] })
2020-11-24 07:08:23 -06:00
2021-07-12 09:06:57 -05:00
const cleanup = async () => {
2020-11-24 07:08:23 -06:00
console.log('Killing server')
2021-07-09 08:37:43 -05:00
await killallServers([ server ])
2020-11-24 07:08:23 -06: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 02:04:35 -05:00
channelId: server.store.channel.id,
2020-11-24 07:08:23 -06:00
privacy: VideoPrivacy.PUBLIC
}
console.log('Creating live.')
2021-07-16 02:04:35 -05:00
const { uuid: liveVideoUUID } = await server.live.create({ fields: attributes })
2020-11-24 07:08:23 -06:00
2021-07-16 02:04:35 -05:00
const live = await server.live.get({ videoId: liveVideoUUID })
2020-11-24 07:08:23 -06: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 02:47:51 -05:00
async function buildConfig (server: PeerTubeServer, commandType: CommandType) {
2021-07-16 02:04:35 -05:00
await server.config.updateCustomSubConfig({
2021-07-09 08:03:44 -05:00
newConfig: {
instance: {
customizations: {
javascript: '',
css: ''
}
},
live: {
enabled: true,
allowReplay: true,
transcoding: {
enabled: commandType === 'live-transcoding'
}
2020-11-24 07:08:23 -06:00
}
}
})
}