test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { registerTSPaths } from '../helpers/register-ts-paths'
  2. registerTSPaths()
  3. import { LiveVideo, LiveVideoCreate, VideoPrivacy } from '@shared/models'
  4. import * as program from 'commander'
  5. import {
  6. createLive,
  7. flushAndRunServer,
  8. getLive,
  9. killallServers,
  10. sendRTMPStream,
  11. ServerInfo,
  12. setAccessTokensToServers,
  13. setDefaultVideoChannel,
  14. updateCustomSubConfig
  15. } from '../../shared/extra-utils'
  16. type CommandType = 'live-mux' | 'live-transcoding'
  17. registerTSPaths()
  18. const command = program
  19. .name('test')
  20. .option('-t, --type <type>', 'live-muxing|live-transcoding')
  21. .parse(process.argv)
  22. run()
  23. .catch(err => {
  24. console.error(err)
  25. process.exit(-1)
  26. })
  27. async function run () {
  28. const commandType: CommandType = command['type']
  29. if (!commandType) {
  30. console.error('Miss command type')
  31. process.exit(-1)
  32. }
  33. console.log('Starting server.')
  34. const server = await flushAndRunServer(1, {}, [], { hideLogs: false, execArgv: [ '--inspect' ] })
  35. const cleanup = () => {
  36. console.log('Killing server')
  37. killallServers([ server ])
  38. }
  39. process.on('exit', cleanup)
  40. process.on('SIGINT', cleanup)
  41. await setAccessTokensToServers([ server ])
  42. await setDefaultVideoChannel([ server ])
  43. await buildConfig(server, commandType)
  44. const attributes: LiveVideoCreate = {
  45. name: 'live',
  46. saveReplay: true,
  47. channelId: server.videoChannel.id,
  48. privacy: VideoPrivacy.PUBLIC
  49. }
  50. console.log('Creating live.')
  51. const res = await createLive(server.url, server.accessToken, attributes)
  52. const liveVideoUUID = res.body.video.uuid
  53. const resLive = await getLive(server.url, server.accessToken, liveVideoUUID)
  54. const live: LiveVideo = resLive.body
  55. console.log('Sending RTMP stream.')
  56. const ffmpegCommand = sendRTMPStream(live.rtmpUrl, live.streamKey)
  57. ffmpegCommand.on('error', err => {
  58. console.error(err)
  59. process.exit(-1)
  60. })
  61. ffmpegCommand.on('end', () => {
  62. console.log('ffmpeg ended')
  63. process.exit(0)
  64. })
  65. }
  66. // ----------------------------------------------------------------------------
  67. async function buildConfig (server: ServerInfo, commandType: CommandType) {
  68. await updateCustomSubConfig(server.url, server.accessToken, {
  69. instance: {
  70. customizations: {
  71. javascript: '',
  72. css: ''
  73. }
  74. },
  75. live: {
  76. enabled: true,
  77. allowReplay: true,
  78. transcoding: {
  79. enabled: commandType === 'live-transcoding'
  80. }
  81. }
  82. })
  83. }