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'
17 } from '@shared/models'
18 import { unwrapBody } from '../requests'
19 import { ObjectStorageCommand, PeerTubeServer } from '../server'
20 import { AbstractCommand, OverrideCommandOptions } from '../shared'
21 import { sendRTMPStream, testFfmpegStreamError } from './live'
23 export class LiveCommand extends AbstractCommand {
25 get (options: OverrideCommandOptions & {
26 videoId: number | string
28 const path = '/api/v1/videos/live'
30 return this.getRequestBody<LiveVideo>({
33 path: path + '/' + options.videoId,
35 defaultExpectedStatus: HttpStatusCode.OK_200
39 // ---------------------------------------------------------------------------
41 listSessions (options: OverrideCommandOptions & {
42 videoId: number | string
44 const path = `/api/v1/videos/live/${options.videoId}/sessions`
46 return this.getRequestBody<ResultList<LiveVideoSession>>({
51 defaultExpectedStatus: HttpStatusCode.OK_200
55 async findLatestSession (options: OverrideCommandOptions & {
56 videoId: number | string
58 const { data: sessions } = await this.listSessions(options)
60 return sessions[sessions.length - 1]
63 getReplaySession (options: OverrideCommandOptions & {
64 videoId: number | string
66 const path = `/api/v1/videos/${options.videoId}/live-session`
68 return this.getRequestBody<LiveVideoSession>({
73 defaultExpectedStatus: HttpStatusCode.OK_200
77 // ---------------------------------------------------------------------------
79 update (options: OverrideCommandOptions & {
80 videoId: number | string
81 fields: LiveVideoUpdate
83 const { videoId, fields } = options
84 const path = '/api/v1/videos/live'
86 return this.putBodyRequest({
89 path: path + '/' + videoId,
92 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
96 async create (options: OverrideCommandOptions & {
97 fields: LiveVideoCreate
99 const { fields } = options
100 const path = '/api/v1/videos/live'
102 const attaches: any = {}
103 if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile
104 if (fields.previewfile) attaches.previewfile = fields.previewfile
106 const body = await unwrapBody<{ video: VideoCreateResult }>(this.postUploadRequest({
111 fields: omit(fields, [ 'thumbnailfile', 'previewfile' ]),
113 defaultExpectedStatus: HttpStatusCode.OK_200
119 async quickCreate (options: OverrideCommandOptions & {
121 permanentLive: boolean
122 privacy?: VideoPrivacy
124 const { saveReplay, permanentLive, privacy } = options
126 const { uuid } = await this.create({
133 channelId: this.server.store.channel.id,
138 const video = await this.server.videos.getWithToken({ id: uuid })
139 const live = await this.get({ videoId: uuid })
141 return { video, live }
144 // ---------------------------------------------------------------------------
146 async sendRTMPStreamInVideo (options: OverrideCommandOptions & {
147 videoId: number | string
151 const { videoId, fixtureName, copyCodecs } = options
152 const videoLive = await this.get({ videoId })
154 return sendRTMPStream({ rtmpBaseUrl: videoLive.rtmpUrl, streamKey: videoLive.streamKey, fixtureName, copyCodecs })
157 async runAndTestStreamError (options: OverrideCommandOptions & {
158 videoId: number | string
159 shouldHaveError: boolean
161 const command = await this.sendRTMPStreamInVideo(options)
163 return testFfmpegStreamError(command, options.shouldHaveError)
166 // ---------------------------------------------------------------------------
168 waitUntilPublished (options: OverrideCommandOptions & {
169 videoId: number | string
171 const { videoId } = options
172 return this.waitUntilState({ videoId, state: VideoState.PUBLISHED })
175 waitUntilWaiting (options: OverrideCommandOptions & {
176 videoId: number | string
178 const { videoId } = options
179 return this.waitUntilState({ videoId, state: VideoState.WAITING_FOR_LIVE })
182 waitUntilEnded (options: OverrideCommandOptions & {
183 videoId: number | string
185 const { videoId } = options
186 return this.waitUntilState({ videoId, state: VideoState.LIVE_ENDED })
189 async waitUntilSegmentGeneration (options: OverrideCommandOptions & {
190 server: PeerTubeServer
192 playlistNumber: number
194 objectStorage: boolean
195 objectStorageBaseUrl?: string
203 objectStorageBaseUrl = ObjectStorageCommand.getMockPlaylistBaseUrl()
206 const segmentName = `${playlistNumber}-00000${segment}.ts`
207 const baseUrl = objectStorage
208 ? join(objectStorageBaseUrl, 'hls')
209 : server.url + '/static/streaming-playlists/hls'
215 await this.getRawRequest({
218 url: `${baseUrl}/${videoUUID}/${segmentName}`,
219 implicitToken: false,
220 defaultExpectedStatus: HttpStatusCode.OK_200
223 const video = await server.videos.get({ id: videoUUID })
224 const hlsPlaylist = video.streamingPlaylists[0]
226 const shaBody = await server.streamingPlaylists.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
228 if (!shaBody[segmentName]) {
229 throw new Error('Segment SHA does not exist')
240 async waitUntilReplacedByReplay (options: OverrideCommandOptions & {
241 videoId: number | string
243 let video: VideoDetails
246 video = await this.server.videos.getWithToken({ token: options.token, id: options.videoId })
249 } while (video.isLive === true || video.state.id !== VideoState.PUBLISHED)
252 // ---------------------------------------------------------------------------
254 getSegmentFile (options: OverrideCommandOptions & {
256 playlistNumber: number
258 objectStorage?: boolean // default false
260 const { playlistNumber, segment, videoUUID, objectStorage = false } = options
262 const segmentName = `${playlistNumber}-00000${segment}.ts`
263 const baseUrl = objectStorage
264 ? ObjectStorageCommand.getMockPlaylistBaseUrl()
265 : `${this.server.url}/static/streaming-playlists/hls`
267 const url = `${baseUrl}/${videoUUID}/${segmentName}`
269 return this.getRawRequest({
273 implicitToken: false,
274 defaultExpectedStatus: HttpStatusCode.OK_200
278 getPlaylistFile (options: OverrideCommandOptions & {
281 objectStorage?: boolean // default false
283 const { playlistName, videoUUID, objectStorage = false } = options
285 const baseUrl = objectStorage
286 ? ObjectStorageCommand.getMockPlaylistBaseUrl()
287 : `${this.server.url}/static/streaming-playlists/hls`
289 const url = `${baseUrl}/${videoUUID}/${playlistName}`
291 return this.getRawRequest({
295 implicitToken: false,
296 defaultExpectedStatus: HttpStatusCode.OK_200
300 // ---------------------------------------------------------------------------
302 async countPlaylists (options: OverrideCommandOptions & {
305 const basePath = this.server.servers.buildDirectory('streaming-playlists')
306 const hlsPath = join(basePath, 'hls', options.videoUUID)
308 const files = await readdir(hlsPath)
310 return files.filter(f => f.endsWith('.m3u8')).length
313 private async waitUntilState (options: OverrideCommandOptions & {
314 videoId: number | string
317 let video: VideoDetails
320 video = await this.server.videos.getWithToken({ token: options.token, id: options.videoId })
323 } while (video.state.id !== options.state)