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, LiveVideo, LiveVideoCreate, LiveVideoUpdate, VideoCreateResult, VideoDetails, VideoState } from '@shared/models'
7 import { wait } from '../miscs'
8 import { unwrapBody } from '../requests'
9 import { AbstractCommand, OverrideCommandOptions } from '../shared'
10 import { sendRTMPStream, testFfmpegStreamError } from './live'
12 export class LiveCommand extends AbstractCommand {
14 get (options: OverrideCommandOptions & {
15 videoId: number | string
17 const path = '/api/v1/videos/live'
19 return this.getRequestBody<LiveVideo>({
22 path: path + '/' + options.videoId,
24 defaultExpectedStatus: HttpStatusCode.OK_200
28 update (options: OverrideCommandOptions & {
29 videoId: number | string
30 fields: LiveVideoUpdate
32 const { videoId, fields } = options
33 const path = '/api/v1/videos/live'
35 return this.putBodyRequest({
38 path: path + '/' + videoId,
41 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
45 async create (options: OverrideCommandOptions & {
46 fields: LiveVideoCreate
48 const { fields } = options
49 const path = '/api/v1/videos/live'
51 const attaches: any = {}
52 if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile
53 if (fields.previewfile) attaches.previewfile = fields.previewfile
55 const body = await unwrapBody<{ video: VideoCreateResult }>(this.postUploadRequest({
60 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.get({ videoId })
75 return sendRTMPStream(videoLive.rtmpUrl, videoLive.streamKey, fixtureName)
78 async runAndTestStreamError (options: OverrideCommandOptions & {
79 videoId: number | string
80 shouldHaveError: boolean
82 const command = await this.sendRTMPStreamInVideo(options)
84 return testFfmpegStreamError(command, options.shouldHaveError)
87 waitUntilPublished (options: OverrideCommandOptions & {
88 videoId: number | string
90 const { videoId } = options
91 return this.waitUntilState({ videoId, state: VideoState.PUBLISHED })
94 waitUntilWaiting (options: OverrideCommandOptions & {
95 videoId: number | string
97 const { videoId } = options
98 return this.waitUntilState({ videoId, state: VideoState.WAITING_FOR_LIVE })
101 waitUntilEnded (options: OverrideCommandOptions & {
102 videoId: number | string
104 const { videoId } = options
105 return this.waitUntilState({ videoId, state: VideoState.LIVE_ENDED })
108 waitUntilSegmentGeneration (options: OverrideCommandOptions & {
113 const { resolution, segment, videoUUID } = options
114 const segmentName = `${resolution}-00000${segment}.ts`
116 return this.server.servers.waitUntilLog(`${videoUUID}/${segmentName}`, 2, false)
119 async waitUntilSaved (options: OverrideCommandOptions & {
120 videoId: number | string
122 let video: VideoDetails
125 video = await this.server.videos.getWithToken({ token: options.token, id: options.videoId })
128 } while (video.isLive === true && video.state.id !== VideoState.PUBLISHED)
131 async countPlaylists (options: OverrideCommandOptions & {
134 const basePath = this.server.servers.buildDirectory('streaming-playlists')
135 const hlsPath = join(basePath, 'hls', options.videoUUID)
137 const files = await readdir(hlsPath)
139 return files.filter(f => f.endsWith('.m3u8')).length
142 private async waitUntilState (options: OverrideCommandOptions & {
143 videoId: number | string
146 let video: VideoDetails
149 video = await this.server.videos.getWithToken({ token: options.token, id: options.videoId })
152 } while (video.state.id !== options.state)