From 77e9f859c6ad75ba179dec74e5410cc651eaa49b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 30 Oct 2020 15:09:00 +0100 Subject: Add check params live tests --- shared/extra-utils/index.ts | 3 +- shared/extra-utils/requests/requests.ts | 4 +- shared/extra-utils/videos/live.ts | 102 ++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 shared/extra-utils/videos/live.ts (limited to 'shared/extra-utils') diff --git a/shared/extra-utils/index.ts b/shared/extra-utils/index.ts index af4d23856..d118b12d2 100644 --- a/shared/extra-utils/index.ts +++ b/shared/extra-utils/index.ts @@ -13,11 +13,12 @@ export * from './requests/requests' export * from './requests/check-api-params' export * from './server/servers' export * from './server/plugins' -export * from './videos/services' export * from './videos/video-playlists' export * from './users/users' export * from './users/accounts' export * from './moderation/abuses' +export * from './videos/services' +export * from './videos/live' export * from './videos/video-abuses' export * from './videos/video-blacklist' export * from './videos/video-captions' diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts index 0e9d67f0b..6b00871e0 100644 --- a/shared/extra-utils/requests/requests.ts +++ b/shared/extra-utils/requests/requests.ts @@ -63,7 +63,7 @@ function makeUploadRequest (options: { path: string token?: string fields: { [ fieldName: string ]: any } - attaches: { [ attachName: string ]: any | any[] } + attaches?: { [ attachName: string ]: any | any[] } statusCodeExpected?: number }) { if (!options.statusCodeExpected) options.statusCodeExpected = 400 @@ -93,7 +93,7 @@ function makeUploadRequest (options: { } }) - Object.keys(options.attaches).forEach(attach => { + Object.keys(options.attaches || {}).forEach(attach => { const value = options.attaches[attach] if (Array.isArray(value)) { req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1]) diff --git a/shared/extra-utils/videos/live.ts b/shared/extra-utils/videos/live.ts new file mode 100644 index 000000000..f500fdc3e --- /dev/null +++ b/shared/extra-utils/videos/live.ts @@ -0,0 +1,102 @@ +import * as ffmpeg from 'fluent-ffmpeg' +import { LiveVideoCreate, LiveVideoUpdate, VideoDetails, VideoState } from '@shared/models' +import { buildAbsoluteFixturePath, wait } from '../miscs/miscs' +import { makeGetRequest, makePutBodyRequest, makeUploadRequest } from '../requests/requests' +import { ServerInfo } from '../server/servers' +import { getVideo, getVideoWithToken } from './videos' + +function getLive (url: string, token: string, videoId: number | string, statusCodeExpected = 200) { + const path = '/api/v1/videos/live' + + return makeGetRequest({ + url, + token, + path: path + '/' + videoId, + statusCodeExpected + }) +} + +function updateLive (url: string, token: string, videoId: number | string, fields: LiveVideoUpdate, statusCodeExpected = 204) { + const path = '/api/v1/videos/live' + + return makePutBodyRequest({ + url, + token, + path: path + '/' + videoId, + fields, + statusCodeExpected + }) +} + +function createLive (url: string, token: string, fields: LiveVideoCreate, statusCodeExpected = 200) { + const path = '/api/v1/videos/live' + + let attaches: any = {} + if (fields.thumbnailfile) attaches = { thumbnailfile: fields.thumbnailfile } + if (fields.previewfile) attaches = { previewfile: fields.previewfile } + + return makeUploadRequest({ + url, + path, + token, + attaches, + fields, + statusCodeExpected + }) +} + +function sendRTMPStream (rtmpBaseUrl: string, streamKey: string) { + const fixture = buildAbsoluteFixturePath('video_short.mp4') + + const command = ffmpeg(fixture) + command.inputOption('-stream_loop -1') + command.inputOption('-re') + + command.outputOption('-c copy') + command.outputOption('-f flv') + + const rtmpUrl = rtmpBaseUrl + '/' + streamKey + command.output(rtmpUrl) + + command.on('error', err => { + if (err?.message?.includes('Exiting normally')) return + + console.error('Cannot send RTMP stream.', { err }) + }) + + if (process.env.DEBUG) { + command.on('stderr', data => console.log(data)) + } + + command.run() + + return command +} + +async function stopFfmpeg (command: ffmpeg.FfmpegCommand) { + command.kill('SIGINT') + + await wait(500) +} + +async function waitUntilLiveStarts (url: string, token: string, videoId: number | string) { + let video: VideoDetails + + do { + const res = await getVideoWithToken(url, token, videoId) + video = res.body + + await wait(500) + } while (video.state.id === VideoState.WAITING_FOR_LIVE) +} + +// --------------------------------------------------------------------------- + +export { + getLive, + updateLive, + waitUntilLiveStarts, + createLive, + stopFfmpeg, + sendRTMPStream +} -- cgit v1.2.3