1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { readdir } from 'fs-extra'
4 import { omit } from 'lodash'
5 import { join } from 'path'
6 import { LiveVideo, LiveVideoCreate, LiveVideoUpdate, VideoCreateResult, VideoDetails, VideoState } from '@shared/models'
7 import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes'
8 import { buildServerDirectory, wait } from '../miscs/miscs'
9 import { unwrapBody } from '../requests'
10 import { waitUntilLog } from '../server/servers'
11 import { AbstractCommand, OverrideCommandOptions } from '../shared'
12 import { sendRTMPStream, testFfmpegStreamError } from './live'
13 import { getVideoWithToken } from './videos'
15 export class LiveCommand extends AbstractCommand {
17 get (options: OverrideCommandOptions & {
18 videoId: number | string
20 const path = '/api/v1/videos/live'
22 return this.getRequestBody<LiveVideo>({
25 path: path + '/' + options.videoId,
27 defaultExpectedStatus: HttpStatusCode.OK_200
31 update (options: OverrideCommandOptions & {
32 videoId: number | string
33 fields: LiveVideoUpdate
35 const { videoId, fields } = options
36 const path = '/api/v1/videos/live'
38 return this.putBodyRequest({
41 path: path + '/' + videoId,
44 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
48 async create (options: OverrideCommandOptions & {
49 fields: LiveVideoCreate
51 const { fields } = options
52 const path = '/api/v1/videos/live'
54 const attaches: any = {}
55 if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile
56 if (fields.previewfile) attaches.previewfile = fields.previewfile
58 const body = await unwrapBody<{ video: VideoCreateResult }>(this.postUploadRequest({
63 fields: omit(fields, 'thumbnailfile', 'previewfile'),
65 defaultExpectedStatus: HttpStatusCode.OK_200
71 async sendRTMPStreamInVideo (options: OverrideCommandOptions & {
72 videoId: number | string
75 const { videoId, fixtureName } = options
76 const videoLive = await this.get({ videoId })
78 return sendRTMPStream(videoLive.rtmpUrl, videoLive.streamKey, fixtureName)
81 async runAndTestStreamError (options: OverrideCommandOptions & {
82 videoId: number | string
83 shouldHaveError: boolean
85 const command = await this.sendRTMPStreamInVideo(options)
87 return testFfmpegStreamError(command, options.shouldHaveError)
90 waitUntilPublished (options: OverrideCommandOptions & {
91 videoId: number | string
93 const { videoId } = options
94 return this.waitUntilState({ videoId, state: VideoState.PUBLISHED })
97 waitUntilWaiting (options: OverrideCommandOptions & {
98 videoId: number | string
100 const { videoId } = options
101 return this.waitUntilState({ videoId, state: VideoState.WAITING_FOR_LIVE })
104 waitUntilEnded (options: OverrideCommandOptions & {
105 videoId: number | string
107 const { videoId } = options
108 return this.waitUntilState({ videoId, state: VideoState.LIVE_ENDED })
111 waitUntilSegmentGeneration (options: OverrideCommandOptions & {
116 const { resolution, segment, videoUUID } = options
117 const segmentName = `${resolution}-00000${segment}.ts`
119 return waitUntilLog(this.server, `${videoUUID}/${segmentName}`, 2, false)
122 async waitUntilSaved (options: OverrideCommandOptions & {
123 videoId: number | string
125 let video: VideoDetails
128 const res = await getVideoWithToken(this.server.url, options.token ?? this.server.accessToken, options.videoId)
132 } while (video.isLive === true && video.state.id !== VideoState.PUBLISHED)
135 async countPlaylists (options: OverrideCommandOptions & {
138 const basePath = buildServerDirectory(this.server, 'streaming-playlists')
139 const hlsPath = join(basePath, 'hls', options.videoUUID)
141 const files = await readdir(hlsPath)
143 return files.filter(f => f.endsWith('.m3u8')).length
146 private async waitUntilState (options: OverrideCommandOptions & {
147 videoId: number | string
150 let video: VideoDetails
153 const res = await getVideoWithToken(this.server.url, options.token ?? this.server.accessToken, options.videoId)
157 } while (video.state.id !== options.state)