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 getLive (options: OverrideCommandOptions & {
18 videoId: number | string
20 const path = '/api/v1/videos/live'
22 return this.getRequestBody<LiveVideo>({
25 path: path + '/' + options.videoId,
26 defaultExpectedStatus: HttpStatusCode.OK_200
30 updateLive (options: OverrideCommandOptions & {
31 videoId: number | string
32 fields: LiveVideoUpdate
34 const { videoId, fields } = options
35 const path = '/api/v1/videos/live'
37 return this.putBodyRequest({
40 path: path + '/' + videoId,
42 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
46 async createLive (options: OverrideCommandOptions & {
47 fields: LiveVideoCreate
49 const { fields } = options
50 const path = '/api/v1/videos/live'
52 const attaches: any = {}
53 if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile
54 if (fields.previewfile) attaches.previewfile = fields.previewfile
56 const body = await unwrapBody<{ video: VideoCreateResult }>(this.postUploadRequest({
61 fields: omit(fields, 'thumbnailfile', 'previewfile'),
62 defaultExpectedStatus: HttpStatusCode.OK_200
68 async sendRTMPStreamInVideo (options: OverrideCommandOptions & {
69 videoId: number | string
72 const { videoId, fixtureName } = options
73 const videoLive = await this.getLive({ videoId })
75 return sendRTMPStream(videoLive.rtmpUrl, videoLive.streamKey, fixtureName)
78 async runAndTestFfmpegStreamError (options: OverrideCommandOptions & {
79 videoId: number | string
80 shouldHaveError: boolean
82 const command = await this.sendRTMPStreamInVideo(options)
84 return testFfmpegStreamError(command, options.shouldHaveError)
87 waitUntilLivePublished (options: OverrideCommandOptions & {
88 videoId: number | string
90 const { videoId } = options
91 return this.waitUntilLiveState({ videoId, state: VideoState.PUBLISHED })
94 waitUntilLiveWaiting (options: OverrideCommandOptions & {
95 videoId: number | string
97 const { videoId } = options
98 return this.waitUntilLiveState({ videoId, state: VideoState.WAITING_FOR_LIVE })
101 waitUntilLiveEnded (options: OverrideCommandOptions & {
102 videoId: number | string
104 const { videoId } = options
105 return this.waitUntilLiveState({ videoId, state: VideoState.LIVE_ENDED })
108 waitUntilLiveSegmentGeneration (options: OverrideCommandOptions & {
113 const { resolution, segment, videoUUID } = options
114 const segmentName = `${resolution}-00000${segment}.ts`
116 return waitUntilLog(this.server, `${videoUUID}/${segmentName}`, 2, false)
119 async waitUntilLiveSaved (options: OverrideCommandOptions & {
120 videoId: number | string
122 let video: VideoDetails
125 const res = await getVideoWithToken(this.server.url, options.token ?? this.server.accessToken, options.videoId)
129 } while (video.isLive === true && video.state.id !== VideoState.PUBLISHED)
132 async getPlaylistsCount (options: OverrideCommandOptions & {
135 const basePath = buildServerDirectory(this.server, 'streaming-playlists')
136 const hlsPath = join(basePath, 'hls', options.videoUUID)
138 const files = await readdir(hlsPath)
140 return files.filter(f => f.endsWith('.m3u8')).length
143 private async waitUntilLiveState (options: OverrideCommandOptions & {
144 videoId: number | string
147 let video: VideoDetails
150 const res = await getVideoWithToken(this.server.url, options.token ?? this.server.accessToken, options.videoId)
154 } while (video.state.id !== options.state)