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 { HttpStatusCode } from '@shared/models'
7 import { LiveVideo, LiveVideoCreate, LiveVideoUpdate, VideoCreateResult, VideoDetails, VideoState } from '@shared/models'
8 import { wait } from '../miscs'
9 import { unwrapBody } from '../requests'
10 import { AbstractCommand, OverrideCommandOptions } from '../shared'
11 import { sendRTMPStream, testFfmpegStreamError } from './live'
13 export class LiveCommand extends AbstractCommand {
15 get (options: OverrideCommandOptions & {
16 videoId: number | string
18 const path = '/api/v1/videos/live'
20 return this.getRequestBody<LiveVideo>({
23 path: path + '/' + options.videoId,
25 defaultExpectedStatus: HttpStatusCode.OK_200
29 update (options: OverrideCommandOptions & {
30 videoId: number | string
31 fields: LiveVideoUpdate
33 const { videoId, fields } = options
34 const path = '/api/v1/videos/live'
36 return this.putBodyRequest({
39 path: path + '/' + videoId,
42 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
46 async create (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'),
63 defaultExpectedStatus: HttpStatusCode.OK_200
69 async sendRTMPStreamInVideo (options: OverrideCommandOptions & {
70 videoId: number | string
73 const { videoId, fixtureName } = options
74 const videoLive = await this.get({ videoId })
76 return sendRTMPStream(videoLive.rtmpUrl, videoLive.streamKey, fixtureName)
79 async runAndTestStreamError (options: OverrideCommandOptions & {
80 videoId: number | string
81 shouldHaveError: boolean
83 const command = await this.sendRTMPStreamInVideo(options)
85 return testFfmpegStreamError(command, options.shouldHaveError)
88 waitUntilPublished (options: OverrideCommandOptions & {
89 videoId: number | string
91 const { videoId } = options
92 return this.waitUntilState({ videoId, state: VideoState.PUBLISHED })
95 waitUntilWaiting (options: OverrideCommandOptions & {
96 videoId: number | string
98 const { videoId } = options
99 return this.waitUntilState({ videoId, state: VideoState.WAITING_FOR_LIVE })
102 waitUntilEnded (options: OverrideCommandOptions & {
103 videoId: number | string
105 const { videoId } = options
106 return this.waitUntilState({ videoId, state: VideoState.LIVE_ENDED })
109 waitUntilSegmentGeneration (options: OverrideCommandOptions & {
114 const { resolution, segment, videoUUID } = options
115 const segmentName = `${resolution}-00000${segment}.ts`
117 return this.server.servers.waitUntilLog(`${videoUUID}/${segmentName}`, 2, false)
120 async waitUntilSaved (options: OverrideCommandOptions & {
121 videoId: number | string
123 let video: VideoDetails
126 video = await this.server.videos.getWithToken({ token: options.token, id: options.videoId })
129 } while (video.isLive === true && video.state.id !== VideoState.PUBLISHED)
132 async countPlaylists (options: OverrideCommandOptions & {
135 const basePath = this.server.servers.buildDirectory('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 waitUntilState (options: OverrideCommandOptions & {
144 videoId: number | string
147 let video: VideoDetails
150 video = await this.server.videos.getWithToken({ token: options.token, id: options.videoId })
153 } while (video.state.id !== options.state)