1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { readdir } from 'fs-extra'
4 import { join } from 'path'
5 import { omit, wait } from '@shared/core-utils'
16 } from '@shared/models'
17 import { unwrapBody } from '../requests'
18 import { AbstractCommand, OverrideCommandOptions } from '../shared'
19 import { sendRTMPStream, testFfmpegStreamError } from './live'
21 export class LiveCommand extends AbstractCommand {
23 get (options: OverrideCommandOptions & {
24 videoId: number | string
26 const path = '/api/v1/videos/live'
28 return this.getRequestBody<LiveVideo>({
31 path: path + '/' + options.videoId,
33 defaultExpectedStatus: HttpStatusCode.OK_200
37 listSessions (options: OverrideCommandOptions & {
38 videoId: number | string
40 const path = `/api/v1/videos/live/${options.videoId}/sessions`
42 return this.getRequestBody<ResultList<LiveVideoSession>>({
47 defaultExpectedStatus: HttpStatusCode.OK_200
51 async findLatestSession (options: OverrideCommandOptions & {
52 videoId: number | string
54 const { data: sessions } = await this.listSessions(options)
56 return sessions[sessions.length - 1]
59 getReplaySession (options: OverrideCommandOptions & {
60 videoId: number | string
62 const path = `/api/v1/videos/${options.videoId}/live-session`
64 return this.getRequestBody<LiveVideoSession>({
69 defaultExpectedStatus: HttpStatusCode.OK_200
73 update (options: OverrideCommandOptions & {
74 videoId: number | string
75 fields: LiveVideoUpdate
77 const { videoId, fields } = options
78 const path = '/api/v1/videos/live'
80 return this.putBodyRequest({
83 path: path + '/' + videoId,
86 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
90 async create (options: OverrideCommandOptions & {
91 fields: LiveVideoCreate
93 const { fields } = options
94 const path = '/api/v1/videos/live'
96 const attaches: any = {}
97 if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile
98 if (fields.previewfile) attaches.previewfile = fields.previewfile
100 const body = await unwrapBody<{ video: VideoCreateResult }>(this.postUploadRequest({
105 fields: omit(fields, [ 'thumbnailfile', 'previewfile' ]),
107 defaultExpectedStatus: HttpStatusCode.OK_200
113 async sendRTMPStreamInVideo (options: OverrideCommandOptions & {
114 videoId: number | string
118 const { videoId, fixtureName, copyCodecs } = options
119 const videoLive = await this.get({ videoId })
121 return sendRTMPStream({ rtmpBaseUrl: videoLive.rtmpUrl, streamKey: videoLive.streamKey, fixtureName, copyCodecs })
124 async runAndTestStreamError (options: OverrideCommandOptions & {
125 videoId: number | string
126 shouldHaveError: boolean
128 const command = await this.sendRTMPStreamInVideo(options)
130 return testFfmpegStreamError(command, options.shouldHaveError)
133 waitUntilPublished (options: OverrideCommandOptions & {
134 videoId: number | string
136 const { videoId } = options
137 return this.waitUntilState({ videoId, state: VideoState.PUBLISHED })
140 waitUntilWaiting (options: OverrideCommandOptions & {
141 videoId: number | string
143 const { videoId } = options
144 return this.waitUntilState({ videoId, state: VideoState.WAITING_FOR_LIVE })
147 waitUntilEnded (options: OverrideCommandOptions & {
148 videoId: number | string
150 const { videoId } = options
151 return this.waitUntilState({ videoId, state: VideoState.LIVE_ENDED })
154 waitUntilSegmentGeneration (options: OverrideCommandOptions & {
156 playlistNumber: number
158 totalSessions?: number
160 const { playlistNumber, segment, videoUUID, totalSessions = 1 } = options
161 const segmentName = `${playlistNumber}-00000${segment}.ts`
163 return this.server.servers.waitUntilLog(`${videoUUID}/${segmentName}`, totalSessions * 2, false)
166 getSegment (options: OverrideCommandOptions & {
168 playlistNumber: number
171 const { playlistNumber, segment, videoUUID } = options
173 const segmentName = `${playlistNumber}-00000${segment}.ts`
174 const url = `${this.server.url}/static/streaming-playlists/hls/${videoUUID}/${segmentName}`
176 return this.getRawRequest({
180 implicitToken: false,
181 defaultExpectedStatus: HttpStatusCode.OK_200
185 async waitUntilReplacedByReplay (options: OverrideCommandOptions & {
186 videoId: number | string
188 let video: VideoDetails
191 video = await this.server.videos.getWithToken({ token: options.token, id: options.videoId })
194 } while (video.isLive === true || video.state.id !== VideoState.PUBLISHED)
197 async countPlaylists (options: OverrideCommandOptions & {
200 const basePath = this.server.servers.buildDirectory('streaming-playlists')
201 const hlsPath = join(basePath, 'hls', options.videoUUID)
203 const files = await readdir(hlsPath)
205 return files.filter(f => f.endsWith('.m3u8')).length
208 private async waitUntilState (options: OverrideCommandOptions & {
209 videoId: number | string
212 let video: VideoDetails
215 video = await this.server.videos.getWithToken({ token: options.token, id: options.videoId })
218 } while (video.state.id !== options.state)