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 { ObjectStorageCommand } from '../server'
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 // ---------------------------------------------------------------------------
40 listSessions (options: OverrideCommandOptions & {
41 videoId: number | string
43 const path = `/api/v1/videos/live/${options.videoId}/sessions`
45 return this.getRequestBody<ResultList<LiveVideoSession>>({
50 defaultExpectedStatus: HttpStatusCode.OK_200
54 async findLatestSession (options: OverrideCommandOptions & {
55 videoId: number | string
57 const { data: sessions } = await this.listSessions(options)
59 return sessions[sessions.length - 1]
62 getReplaySession (options: OverrideCommandOptions & {
63 videoId: number | string
65 const path = `/api/v1/videos/${options.videoId}/live-session`
67 return this.getRequestBody<LiveVideoSession>({
72 defaultExpectedStatus: HttpStatusCode.OK_200
76 // ---------------------------------------------------------------------------
78 update (options: OverrideCommandOptions & {
79 videoId: number | string
80 fields: LiveVideoUpdate
82 const { videoId, fields } = options
83 const path = '/api/v1/videos/live'
85 return this.putBodyRequest({
88 path: path + '/' + videoId,
91 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
95 async create (options: OverrideCommandOptions & {
96 fields: LiveVideoCreate
98 const { fields } = options
99 const path = '/api/v1/videos/live'
101 const attaches: any = {}
102 if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile
103 if (fields.previewfile) attaches.previewfile = fields.previewfile
105 const body = await unwrapBody<{ video: VideoCreateResult }>(this.postUploadRequest({
110 fields: omit(fields, [ 'thumbnailfile', 'previewfile' ]),
112 defaultExpectedStatus: HttpStatusCode.OK_200
118 // ---------------------------------------------------------------------------
120 async sendRTMPStreamInVideo (options: OverrideCommandOptions & {
121 videoId: number | string
125 const { videoId, fixtureName, copyCodecs } = options
126 const videoLive = await this.get({ videoId })
128 return sendRTMPStream({ rtmpBaseUrl: videoLive.rtmpUrl, streamKey: videoLive.streamKey, fixtureName, copyCodecs })
131 async runAndTestStreamError (options: OverrideCommandOptions & {
132 videoId: number | string
133 shouldHaveError: boolean
135 const command = await this.sendRTMPStreamInVideo(options)
137 return testFfmpegStreamError(command, options.shouldHaveError)
140 // ---------------------------------------------------------------------------
142 waitUntilPublished (options: OverrideCommandOptions & {
143 videoId: number | string
145 const { videoId } = options
146 return this.waitUntilState({ videoId, state: VideoState.PUBLISHED })
149 waitUntilWaiting (options: OverrideCommandOptions & {
150 videoId: number | string
152 const { videoId } = options
153 return this.waitUntilState({ videoId, state: VideoState.WAITING_FOR_LIVE })
156 waitUntilEnded (options: OverrideCommandOptions & {
157 videoId: number | string
159 const { videoId } = options
160 return this.waitUntilState({ videoId, state: VideoState.LIVE_ENDED })
163 waitUntilSegmentGeneration (options: OverrideCommandOptions & {
165 playlistNumber: number
167 totalSessions?: number
169 const { playlistNumber, segment, videoUUID, totalSessions = 1 } = options
170 const segmentName = `${playlistNumber}-00000${segment}.ts`
172 return this.server.servers.waitUntilLog(`${videoUUID}/${segmentName}`, totalSessions * 2, false)
175 waitUntilSegmentUpload (options: OverrideCommandOptions & {
176 playlistNumber: number
178 totalSessions?: number
180 const { playlistNumber, segment, totalSessions = 1 } = options
181 const segmentName = `${playlistNumber}-00000${segment}.ts`
183 return this.server.servers.waitUntilLog(`${segmentName} in bucket `, totalSessions * 2, false)
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 // ---------------------------------------------------------------------------
200 getSegmentFile (options: OverrideCommandOptions & {
202 playlistNumber: number
204 objectStorage?: boolean // default false
206 const { playlistNumber, segment, videoUUID, objectStorage = false } = options
208 const segmentName = `${playlistNumber}-00000${segment}.ts`
209 const baseUrl = objectStorage
210 ? ObjectStorageCommand.getPlaylistBaseUrl()
211 : `${this.server.url}/static/streaming-playlists/hls`
213 const url = `${baseUrl}/${videoUUID}/${segmentName}`
215 return this.getRawRequest({
219 implicitToken: false,
220 defaultExpectedStatus: HttpStatusCode.OK_200
224 getPlaylistFile (options: OverrideCommandOptions & {
227 objectStorage?: boolean // default false
229 const { playlistName, videoUUID, objectStorage = false } = options
231 const baseUrl = objectStorage
232 ? ObjectStorageCommand.getPlaylistBaseUrl()
233 : `${this.server.url}/static/streaming-playlists/hls`
235 const url = `${baseUrl}/${videoUUID}/${playlistName}`
237 return this.getRawRequest({
241 implicitToken: false,
242 defaultExpectedStatus: HttpStatusCode.OK_200
246 // ---------------------------------------------------------------------------
248 async countPlaylists (options: OverrideCommandOptions & {
251 const basePath = this.server.servers.buildDirectory('streaming-playlists')
252 const hlsPath = join(basePath, 'hls', options.videoUUID)
254 const files = await readdir(hlsPath)
256 return files.filter(f => f.endsWith('.m3u8')).length
259 private async waitUntilState (options: OverrideCommandOptions & {
260 videoId: number | string
263 let video: VideoDetails
266 video = await this.server.videos.getWithToken({ token: options.token, id: options.videoId })
269 } while (video.state.id !== options.state)