2021-07-08 10:18:40 +02:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
|
|
|
|
|
|
|
import { readdir } from 'fs-extra'
|
|
|
|
import { omit } from 'lodash'
|
|
|
|
import { join } from 'path'
|
|
|
|
import { LiveVideo, LiveVideoCreate, LiveVideoUpdate, VideoCreateResult, VideoDetails, VideoState } from '@shared/models'
|
|
|
|
import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes'
|
|
|
|
import { buildServerDirectory, wait } from '../miscs/miscs'
|
|
|
|
import { unwrapBody } from '../requests'
|
|
|
|
import { waitUntilLog } from '../server/servers'
|
|
|
|
import { AbstractCommand, OverrideCommandOptions } from '../shared'
|
|
|
|
import { sendRTMPStream, testFfmpegStreamError } from './live'
|
|
|
|
import { getVideoWithToken } from './videos'
|
|
|
|
|
|
|
|
export class LiveCommand extends AbstractCommand {
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
get (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
videoId: number | string
|
|
|
|
}) {
|
|
|
|
const path = '/api/v1/videos/live'
|
|
|
|
|
|
|
|
return this.getRequestBody<LiveVideo>({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path: path + '/' + options.videoId,
|
2021-07-08 10:55:16 +02:00
|
|
|
implicitToken: true,
|
2021-07-08 10:18:40 +02:00
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
update (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
videoId: number | string
|
|
|
|
fields: LiveVideoUpdate
|
|
|
|
}) {
|
|
|
|
const { videoId, fields } = options
|
|
|
|
const path = '/api/v1/videos/live'
|
|
|
|
|
|
|
|
return this.putBodyRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path: path + '/' + videoId,
|
|
|
|
fields,
|
2021-07-08 10:55:16 +02:00
|
|
|
implicitToken: true,
|
2021-07-08 10:18:40 +02:00
|
|
|
defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
async create (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
fields: LiveVideoCreate
|
|
|
|
}) {
|
|
|
|
const { fields } = options
|
|
|
|
const path = '/api/v1/videos/live'
|
|
|
|
|
|
|
|
const attaches: any = {}
|
|
|
|
if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile
|
|
|
|
if (fields.previewfile) attaches.previewfile = fields.previewfile
|
|
|
|
|
|
|
|
const body = await unwrapBody<{ video: VideoCreateResult }>(this.postUploadRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
attaches,
|
|
|
|
fields: omit(fields, 'thumbnailfile', 'previewfile'),
|
2021-07-08 10:55:16 +02:00
|
|
|
implicitToken: true,
|
2021-07-08 10:18:40 +02:00
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
}))
|
|
|
|
|
|
|
|
return body.video
|
|
|
|
}
|
|
|
|
|
|
|
|
async sendRTMPStreamInVideo (options: OverrideCommandOptions & {
|
|
|
|
videoId: number | string
|
|
|
|
fixtureName?: string
|
|
|
|
}) {
|
|
|
|
const { videoId, fixtureName } = options
|
2021-07-08 10:25:50 +02:00
|
|
|
const videoLive = await this.get({ videoId })
|
2021-07-08 10:18:40 +02:00
|
|
|
|
|
|
|
return sendRTMPStream(videoLive.rtmpUrl, videoLive.streamKey, fixtureName)
|
|
|
|
}
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
async runAndTestStreamError (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
videoId: number | string
|
|
|
|
shouldHaveError: boolean
|
|
|
|
}) {
|
|
|
|
const command = await this.sendRTMPStreamInVideo(options)
|
|
|
|
|
|
|
|
return testFfmpegStreamError(command, options.shouldHaveError)
|
|
|
|
}
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
waitUntilPublished (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
videoId: number | string
|
|
|
|
}) {
|
|
|
|
const { videoId } = options
|
2021-07-08 10:25:50 +02:00
|
|
|
return this.waitUntilState({ videoId, state: VideoState.PUBLISHED })
|
2021-07-08 10:18:40 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
waitUntilWaiting (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
videoId: number | string
|
|
|
|
}) {
|
|
|
|
const { videoId } = options
|
2021-07-08 10:25:50 +02:00
|
|
|
return this.waitUntilState({ videoId, state: VideoState.WAITING_FOR_LIVE })
|
2021-07-08 10:18:40 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
waitUntilEnded (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
videoId: number | string
|
|
|
|
}) {
|
|
|
|
const { videoId } = options
|
2021-07-08 10:25:50 +02:00
|
|
|
return this.waitUntilState({ videoId, state: VideoState.LIVE_ENDED })
|
2021-07-08 10:18:40 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
waitUntilSegmentGeneration (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
videoUUID: string
|
|
|
|
resolution: number
|
|
|
|
segment: number
|
|
|
|
}) {
|
|
|
|
const { resolution, segment, videoUUID } = options
|
|
|
|
const segmentName = `${resolution}-00000${segment}.ts`
|
|
|
|
|
|
|
|
return waitUntilLog(this.server, `${videoUUID}/${segmentName}`, 2, false)
|
|
|
|
}
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
async waitUntilSaved (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
videoId: number | string
|
|
|
|
}) {
|
|
|
|
let video: VideoDetails
|
|
|
|
|
|
|
|
do {
|
|
|
|
const res = await getVideoWithToken(this.server.url, options.token ?? this.server.accessToken, options.videoId)
|
|
|
|
video = res.body
|
|
|
|
|
|
|
|
await wait(500)
|
|
|
|
} while (video.isLive === true && video.state.id !== VideoState.PUBLISHED)
|
|
|
|
}
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
async countPlaylists (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
videoUUID: string
|
|
|
|
}) {
|
|
|
|
const basePath = buildServerDirectory(this.server, 'streaming-playlists')
|
|
|
|
const hlsPath = join(basePath, 'hls', options.videoUUID)
|
|
|
|
|
|
|
|
const files = await readdir(hlsPath)
|
|
|
|
|
|
|
|
return files.filter(f => f.endsWith('.m3u8')).length
|
|
|
|
}
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
private async waitUntilState (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
videoId: number | string
|
|
|
|
state: VideoState
|
|
|
|
}) {
|
|
|
|
let video: VideoDetails
|
|
|
|
|
|
|
|
do {
|
|
|
|
const res = await getVideoWithToken(this.server.url, options.token ?? this.server.accessToken, options.videoId)
|
|
|
|
video = res.body
|
|
|
|
|
|
|
|
await wait(500)
|
|
|
|
} while (video.state.id !== options.state)
|
|
|
|
}
|
|
|
|
}
|