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 { wait } from '@shared/core-utils'
17 } from '@shared/models'
18 import { unwrapBody } from '../requests'
19 import { AbstractCommand, OverrideCommandOptions } from '../shared'
20 import { sendRTMPStream, testFfmpegStreamError } from './live'
22 export class LiveCommand extends AbstractCommand {
24 get (options: OverrideCommandOptions & {
25 videoId: number | string
27 const path = '/api/v1/videos/live'
29 return this.getRequestBody<LiveVideo>({
32 path: path + '/' + options.videoId,
34 defaultExpectedStatus: HttpStatusCode.OK_200
38 listSessions (options: OverrideCommandOptions & {
39 videoId: number | string
41 const path = `/api/v1/videos/live/${options.videoId}/sessions`
43 return this.getRequestBody<ResultList<LiveVideoSession>>({
48 defaultExpectedStatus: HttpStatusCode.OK_200
52 async findLatestSession (options: OverrideCommandOptions & {
53 videoId: number | string
55 const { data: sessions } = await this.listSessions(options)
57 return sessions[sessions.length - 1]
60 getReplaySession (options: OverrideCommandOptions & {
61 videoId: number | string
63 const path = `/api/v1/videos/${options.videoId}/live-session`
65 return this.getRequestBody<LiveVideoSession>({
70 defaultExpectedStatus: HttpStatusCode.OK_200
74 update (options: OverrideCommandOptions & {
75 videoId: number | string
76 fields: LiveVideoUpdate
78 const { videoId, fields } = options
79 const path = '/api/v1/videos/live'
81 return this.putBodyRequest({
84 path: path + '/' + videoId,
87 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
91 async create (options: OverrideCommandOptions & {
92 fields: LiveVideoCreate
94 const { fields } = options
95 const path = '/api/v1/videos/live'
97 const attaches: any = {}
98 if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile
99 if (fields.previewfile) attaches.previewfile = fields.previewfile
101 const body = await unwrapBody<{ video: VideoCreateResult }>(this.postUploadRequest({
106 fields: omit(fields, 'thumbnailfile', 'previewfile'),
108 defaultExpectedStatus: HttpStatusCode.OK_200
114 async sendRTMPStreamInVideo (options: OverrideCommandOptions & {
115 videoId: number | string
119 const { videoId, fixtureName, copyCodecs } = options
120 const videoLive = await this.get({ videoId })
122 return sendRTMPStream({ rtmpBaseUrl: videoLive.rtmpUrl, streamKey: videoLive.streamKey, fixtureName, copyCodecs })
125 async runAndTestStreamError (options: OverrideCommandOptions & {
126 videoId: number | string
127 shouldHaveError: boolean
129 const command = await this.sendRTMPStreamInVideo(options)
131 return testFfmpegStreamError(command, options.shouldHaveError)
134 waitUntilPublished (options: OverrideCommandOptions & {
135 videoId: number | string
137 const { videoId } = options
138 return this.waitUntilState({ videoId, state: VideoState.PUBLISHED })
141 waitUntilWaiting (options: OverrideCommandOptions & {
142 videoId: number | string
144 const { videoId } = options
145 return this.waitUntilState({ videoId, state: VideoState.WAITING_FOR_LIVE })
148 waitUntilEnded (options: OverrideCommandOptions & {
149 videoId: number | string
151 const { videoId } = options
152 return this.waitUntilState({ videoId, state: VideoState.LIVE_ENDED })
155 waitUntilSegmentGeneration (options: OverrideCommandOptions & {
157 playlistNumber: number
159 totalSessions?: number
161 const { playlistNumber, segment, videoUUID, totalSessions = 1 } = options
162 const segmentName = `${playlistNumber}-00000${segment}.ts`
164 return this.server.servers.waitUntilLog(`${videoUUID}/${segmentName}`, totalSessions * 2, false)
167 getSegment (options: OverrideCommandOptions & {
169 playlistNumber: number
172 const { playlistNumber, segment, videoUUID } = options
174 const segmentName = `${playlistNumber}-00000${segment}.ts`
175 const url = `${this.server.url}/static/streaming-playlists/hls/${videoUUID}/${segmentName}`
177 return this.getRawRequest({
181 implicitToken: false,
182 defaultExpectedStatus: HttpStatusCode.OK_200
186 async waitUntilReplacedByReplay (options: OverrideCommandOptions & {
187 videoId: number | string
189 let video: VideoDetails
192 video = await this.server.videos.getWithToken({ token: options.token, id: options.videoId })
195 } while (video.isLive === true || video.state.id !== VideoState.PUBLISHED)
198 async countPlaylists (options: OverrideCommandOptions & {
201 const basePath = this.server.servers.buildDirectory('streaming-playlists')
202 const hlsPath = join(basePath, 'hls', options.videoUUID)
204 const files = await readdir(hlsPath)
206 return files.filter(f => f.endsWith('.m3u8')).length
209 private async waitUntilState (options: OverrideCommandOptions & {
210 videoId: number | string
213 let video: VideoDetails
216 video = await this.server.videos.getWithToken({ token: options.token, id: options.videoId })
219 } while (video.state.id !== options.state)