1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import * as ffmpeg from 'fluent-ffmpeg'
5 import { pathExists, readdir } from 'fs-extra'
6 import { omit } from 'lodash'
7 import { join } from 'path'
8 import { LiveVideo, LiveVideoCreate, LiveVideoUpdate, VideoDetails, VideoState } from '@shared/models'
9 import { buildAbsoluteFixturePath, buildServerDirectory, wait } from '../miscs/miscs'
10 import { makeGetRequest, makePutBodyRequest, makeUploadRequest } from '../requests/requests'
11 import { ServerInfo } from '../server/servers'
12 import { getVideoWithToken } from './videos'
14 function getLive (url: string, token: string, videoId: number | string, statusCodeExpected = 200) {
15 const path = '/api/v1/videos/live'
17 return makeGetRequest({
20 path: path + '/' + videoId,
25 function updateLive (url: string, token: string, videoId: number | string, fields: LiveVideoUpdate, statusCodeExpected = 204) {
26 const path = '/api/v1/videos/live'
28 return makePutBodyRequest({
31 path: path + '/' + videoId,
37 function createLive (url: string, token: string, fields: LiveVideoCreate, statusCodeExpected = 200) {
38 const path = '/api/v1/videos/live'
40 const attaches: any = {}
41 if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile
42 if (fields.previewfile) attaches.previewfile = fields.previewfile
44 const updatedFields = omit(fields, 'thumbnailfile', 'previewfile')
46 return makeUploadRequest({
51 fields: updatedFields,
56 async function sendRTMPStreamInVideo (url: string, token: string, videoId: number | string) {
57 const res = await getLive(url, token, videoId)
58 const videoLive = res.body as LiveVideo
60 return sendRTMPStream(videoLive.rtmpUrl, videoLive.streamKey)
63 function sendRTMPStream (rtmpBaseUrl: string, streamKey: string) {
64 const fixture = buildAbsoluteFixturePath('video_short.mp4')
66 const command = ffmpeg(fixture)
67 command.inputOption('-stream_loop -1')
68 command.inputOption('-re')
69 command.outputOption('-c:v libx264')
70 command.outputOption('-g 50')
71 command.outputOption('-keyint_min 2')
72 command.outputOption('-f flv')
74 const rtmpUrl = rtmpBaseUrl + '/' + streamKey
75 command.output(rtmpUrl)
77 command.on('error', err => {
78 if (err?.message?.includes('Exiting normally')) return
80 if (process.env.DEBUG) console.error(err)
83 if (process.env.DEBUG) {
84 command.on('stderr', data => console.log(data))
92 function waitFfmpegUntilError (command: ffmpeg.FfmpegCommand, successAfterMS = 10000) {
93 return new Promise((res, rej) => {
94 command.on('error', err => {
104 async function runAndTestFfmpegStreamError (url: string, token: string, videoId: number | string, shouldHaveError: boolean) {
105 const command = await sendRTMPStreamInVideo(url, token, videoId)
107 return testFfmpegStreamError(command, shouldHaveError)
110 async function testFfmpegStreamError (command: ffmpeg.FfmpegCommand, shouldHaveError: boolean) {
114 await waitFfmpegUntilError(command, 10000)
119 await stopFfmpeg(command)
121 if (shouldHaveError && !error) throw new Error('Ffmpeg did not have an error')
122 if (!shouldHaveError && error) throw error
125 async function stopFfmpeg (command: ffmpeg.FfmpegCommand) {
126 command.kill('SIGINT')
131 async function waitUntilLiveStarts (url: string, token: string, videoId: number | string) {
132 let video: VideoDetails
135 const res = await getVideoWithToken(url, token, videoId)
139 } while (video.state.id === VideoState.WAITING_FOR_LIVE)
142 async function checkLiveCleanup (server: ServerInfo, videoUUID: string, resolutions: number[] = []) {
143 const basePath = buildServerDirectory(server.internalServerNumber, 'streaming-playlists')
144 const hlsPath = join(basePath, 'hls', videoUUID)
146 if (resolutions.length === 0) {
147 const result = await pathExists(hlsPath)
148 expect(result).to.be.false
153 const files = await readdir(hlsPath)
155 // fragmented file and playlist per resolution + master playlist + segments sha256 json file
156 expect(files).to.have.lengthOf(resolutions.length * 2 + 2)
158 for (const resolution of resolutions) {
159 expect(files).to.contain(`${videoUUID}-${resolution}-fragmented.mp4`)
160 expect(files).to.contain(`${resolution}.m3u8`)
163 expect(files).to.contain('master.m3u8')
164 expect(files).to.contain('segments-sha256.json')
167 // ---------------------------------------------------------------------------
174 runAndTestFfmpegStreamError,
177 sendRTMPStreamInVideo,
178 waitFfmpegUntilError,
180 testFfmpegStreamError