1 import * as ffmpeg from 'fluent-ffmpeg'
2 import { LiveVideoCreate, LiveVideoUpdate, VideoDetails, VideoState } from '@shared/models'
3 import { buildAbsoluteFixturePath, wait } from '../miscs/miscs'
4 import { makeGetRequest, makePutBodyRequest, makeUploadRequest } from '../requests/requests'
5 import { ServerInfo } from '../server/servers'
6 import { getVideo, getVideoWithToken } from './videos'
8 function getLive (url: string, token: string, videoId: number | string, statusCodeExpected = 200) {
9 const path = '/api/v1/videos/live'
11 return makeGetRequest({
14 path: path + '/' + videoId,
19 function updateLive (url: string, token: string, videoId: number | string, fields: LiveVideoUpdate, statusCodeExpected = 204) {
20 const path = '/api/v1/videos/live'
22 return makePutBodyRequest({
25 path: path + '/' + videoId,
31 function createLive (url: string, token: string, fields: LiveVideoCreate, statusCodeExpected = 200) {
32 const path = '/api/v1/videos/live'
34 let attaches: any = {}
35 if (fields.thumbnailfile) attaches = { thumbnailfile: fields.thumbnailfile }
36 if (fields.previewfile) attaches = { previewfile: fields.previewfile }
38 return makeUploadRequest({
48 function sendRTMPStream (rtmpBaseUrl: string, streamKey: string) {
49 const fixture = buildAbsoluteFixturePath('video_short.mp4')
51 const command = ffmpeg(fixture)
52 command.inputOption('-stream_loop -1')
53 command.inputOption('-re')
55 command.outputOption('-c copy')
56 command.outputOption('-f flv')
58 const rtmpUrl = rtmpBaseUrl + '/' + streamKey
59 command.output(rtmpUrl)
61 command.on('error', err => {
62 if (err?.message?.includes('Exiting normally')) return
64 console.error('Cannot send RTMP stream.', { err })
67 if (process.env.DEBUG) {
68 command.on('stderr', data => console.log(data))
76 async function stopFfmpeg (command: ffmpeg.FfmpegCommand) {
77 command.kill('SIGINT')
82 async function waitUntilLiveStarts (url: string, token: string, videoId: number | string) {
83 let video: VideoDetails
86 const res = await getVideoWithToken(url, token, videoId)
90 } while (video.state.id === VideoState.WAITING_FOR_LIVE)
93 // ---------------------------------------------------------------------------