1 import * as ffmpeg from 'fluent-ffmpeg'
2 import { omit } from 'lodash'
3 import { LiveVideo, LiveVideoCreate, LiveVideoUpdate, VideoDetails, VideoState } from '@shared/models'
4 import { buildAbsoluteFixturePath, wait } from '../miscs/miscs'
5 import { makeGetRequest, makePutBodyRequest, makeUploadRequest } from '../requests/requests'
6 import { 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 const attaches: any = {}
35 if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile
36 if (fields.previewfile) attaches.previewfile = fields.previewfile
38 const updatedFields = omit(fields, 'thumbnailfile', 'previewfile')
40 return makeUploadRequest({
45 fields: updatedFields,
50 async function sendRTMPStreamInVideo (url: string, token: string, videoId: number | string, onErrorCb?: Function) {
51 const res = await getLive(url, token, videoId)
52 const videoLive = res.body as LiveVideo
54 return sendRTMPStream(videoLive.rtmpUrl, videoLive.streamKey, onErrorCb)
57 function sendRTMPStream (rtmpBaseUrl: string, streamKey: string, onErrorCb?: Function) {
58 const fixture = buildAbsoluteFixturePath('video_short.mp4')
60 const command = ffmpeg(fixture)
61 command.inputOption('-stream_loop -1')
62 command.inputOption('-re')
64 command.outputOption('-c copy')
65 command.outputOption('-f flv')
67 const rtmpUrl = rtmpBaseUrl + '/' + streamKey
68 command.output(rtmpUrl)
70 command.on('error', err => {
71 if (err?.message?.includes('Exiting normally')) return
73 if (onErrorCb) onErrorCb(err)
76 if (process.env.DEBUG) {
77 command.on('stderr', data => console.log(data))
85 function waitFfmpegUntilError (command: ffmpeg.FfmpegCommand, successAfterMS = 10000) {
86 return new Promise((res, rej) => {
87 command.on('error', err => {
97 async function testFfmpegStreamError (url: string, token: string, videoId: number | string, shouldHaveError: boolean) {
98 const command = await sendRTMPStreamInVideo(url, token, videoId)
102 await waitFfmpegUntilError(command, 10000)
107 await stopFfmpeg(command)
109 if (shouldHaveError && !error) throw new Error('Ffmpeg did not have an error')
110 if (!shouldHaveError && error) throw error
113 async function stopFfmpeg (command: ffmpeg.FfmpegCommand) {
114 command.kill('SIGINT')
119 async function waitUntilLiveStarts (url: string, token: string, videoId: number | string) {
120 let video: VideoDetails
123 const res = await getVideoWithToken(url, token, videoId)
127 } while (video.state.id === VideoState.WAITING_FOR_LIVE)
130 // ---------------------------------------------------------------------------
137 testFfmpegStreamError,
139 sendRTMPStreamInVideo,
140 waitFfmpegUntilError,