aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tools/test.ts
blob: fc7f8d769a7483553d77e70d30e34f0c61f7e608 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { registerTSPaths } from '../helpers/register-ts-paths'
registerTSPaths()

import { LiveVideo, LiveVideoCreate, VideoPrivacy } from '@shared/models'
import * as program from 'commander'
import {
  createLive,
  flushAndRunServer,
  getLive,
  killallServers,
  sendRTMPStream,
  ServerInfo,
  setAccessTokensToServers,
  setDefaultVideoChannel,
  updateCustomSubConfig
} from '../../shared/extra-utils'

type CommandType = 'live-mux' | 'live-transcoding'

registerTSPaths()

const command = program
  .name('test')
  .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.')

  const server = await flushAndRunServer(1, {}, [], { hideLogs: false, execArgv: [ '--inspect' ] })

  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.')

  const res = await createLive(server.url, server.accessToken, attributes)
  const liveVideoUUID = res.body.video.uuid

  const resLive = await getLive(server.url, server.accessToken, liveVideoUUID)
  const live: LiveVideo = resLive.body

  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) {
  await updateCustomSubConfig(server.url, server.accessToken, {
    instance: {
      customizations: {
        javascript: '',
        css: ''
      }
    },
    live: {
      enabled: true,
      allowReplay: true,
      transcoding: {
        enabled: commandType === 'live-transcoding'
      }
    }
  })
}