]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/server-commands/videos/live-command.ts
Add runner server tests
[github/Chocobozzz/PeerTube.git] / shared / server-commands / videos / live-command.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import { readdir } from 'fs-extra'
4 import { join } from 'path'
5 import { omit, wait } from '@shared/core-utils'
6 import {
7 HttpStatusCode,
8 LiveVideo,
9 LiveVideoCreate,
10 LiveVideoSession,
11 LiveVideoUpdate,
12 ResultList,
13 VideoCreateResult,
14 VideoDetails,
15 VideoPrivacy,
16 VideoState
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'
22
23 export class LiveCommand extends AbstractCommand {
24
25 get (options: OverrideCommandOptions & {
26 videoId: number | string
27 }) {
28 const path = '/api/v1/videos/live'
29
30 return this.getRequestBody<LiveVideo>({
31 ...options,
32
33 path: path + '/' + options.videoId,
34 implicitToken: true,
35 defaultExpectedStatus: HttpStatusCode.OK_200
36 })
37 }
38
39 // ---------------------------------------------------------------------------
40
41 listSessions (options: OverrideCommandOptions & {
42 videoId: number | string
43 }) {
44 const path = `/api/v1/videos/live/${options.videoId}/sessions`
45
46 return this.getRequestBody<ResultList<LiveVideoSession>>({
47 ...options,
48
49 path,
50 implicitToken: true,
51 defaultExpectedStatus: HttpStatusCode.OK_200
52 })
53 }
54
55 async findLatestSession (options: OverrideCommandOptions & {
56 videoId: number | string
57 }) {
58 const { data: sessions } = await this.listSessions(options)
59
60 return sessions[sessions.length - 1]
61 }
62
63 getReplaySession (options: OverrideCommandOptions & {
64 videoId: number | string
65 }) {
66 const path = `/api/v1/videos/${options.videoId}/live-session`
67
68 return this.getRequestBody<LiveVideoSession>({
69 ...options,
70
71 path,
72 implicitToken: true,
73 defaultExpectedStatus: HttpStatusCode.OK_200
74 })
75 }
76
77 // ---------------------------------------------------------------------------
78
79 update (options: OverrideCommandOptions & {
80 videoId: number | string
81 fields: LiveVideoUpdate
82 }) {
83 const { videoId, fields } = options
84 const path = '/api/v1/videos/live'
85
86 return this.putBodyRequest({
87 ...options,
88
89 path: path + '/' + videoId,
90 fields,
91 implicitToken: true,
92 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
93 })
94 }
95
96 async create (options: OverrideCommandOptions & {
97 fields: LiveVideoCreate
98 }) {
99 const { fields } = options
100 const path = '/api/v1/videos/live'
101
102 const attaches: any = {}
103 if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile
104 if (fields.previewfile) attaches.previewfile = fields.previewfile
105
106 const body = await unwrapBody<{ video: VideoCreateResult }>(this.postUploadRequest({
107 ...options,
108
109 path,
110 attaches,
111 fields: omit(fields, [ 'thumbnailfile', 'previewfile' ]),
112 implicitToken: true,
113 defaultExpectedStatus: HttpStatusCode.OK_200
114 }))
115
116 return body.video
117 }
118
119 async quickCreate (options: OverrideCommandOptions & {
120 saveReplay: boolean
121 permanentLive: boolean
122 privacy?: VideoPrivacy
123 }) {
124 const { saveReplay, permanentLive, privacy = VideoPrivacy.PUBLIC } = options
125
126 const { uuid } = await this.create({
127 ...options,
128
129 fields: {
130 name: 'live',
131 permanentLive,
132 saveReplay,
133 replaySettings: { privacy },
134 channelId: this.server.store.channel.id,
135 privacy
136 }
137 })
138
139 const video = await this.server.videos.getWithToken({ id: uuid })
140 const live = await this.get({ videoId: uuid })
141
142 return { video, live }
143 }
144
145 // ---------------------------------------------------------------------------
146
147 async sendRTMPStreamInVideo (options: OverrideCommandOptions & {
148 videoId: number | string
149 fixtureName?: string
150 copyCodecs?: boolean
151 }) {
152 const { videoId, fixtureName, copyCodecs } = options
153 const videoLive = await this.get({ videoId })
154
155 return sendRTMPStream({ rtmpBaseUrl: videoLive.rtmpUrl, streamKey: videoLive.streamKey, fixtureName, copyCodecs })
156 }
157
158 async runAndTestStreamError (options: OverrideCommandOptions & {
159 videoId: number | string
160 shouldHaveError: boolean
161 }) {
162 const command = await this.sendRTMPStreamInVideo(options)
163
164 return testFfmpegStreamError(command, options.shouldHaveError)
165 }
166
167 // ---------------------------------------------------------------------------
168
169 waitUntilPublished (options: OverrideCommandOptions & {
170 videoId: number | string
171 }) {
172 const { videoId } = options
173 return this.waitUntilState({ videoId, state: VideoState.PUBLISHED })
174 }
175
176 waitUntilWaiting (options: OverrideCommandOptions & {
177 videoId: number | string
178 }) {
179 const { videoId } = options
180 return this.waitUntilState({ videoId, state: VideoState.WAITING_FOR_LIVE })
181 }
182
183 waitUntilEnded (options: OverrideCommandOptions & {
184 videoId: number | string
185 }) {
186 const { videoId } = options
187 return this.waitUntilState({ videoId, state: VideoState.LIVE_ENDED })
188 }
189
190 async waitUntilSegmentGeneration (options: OverrideCommandOptions & {
191 server: PeerTubeServer
192 videoUUID: string
193 playlistNumber: number
194 segment: number
195 objectStorage: boolean
196 objectStorageBaseUrl?: string
197 }) {
198 const {
199 server,
200 objectStorage,
201 playlistNumber,
202 segment,
203 videoUUID,
204 objectStorageBaseUrl = ObjectStorageCommand.getMockPlaylistBaseUrl()
205 } = options
206
207 const segmentName = `${playlistNumber}-00000${segment}.ts`
208 const baseUrl = objectStorage
209 ? join(objectStorageBaseUrl, 'hls')
210 : server.url + '/static/streaming-playlists/hls'
211
212 let error = true
213
214 while (error) {
215 try {
216 await this.getRawRequest({
217 ...options,
218
219 url: `${baseUrl}/${videoUUID}/${segmentName}`,
220 implicitToken: false,
221 defaultExpectedStatus: HttpStatusCode.OK_200
222 })
223
224 const video = await server.videos.get({ id: videoUUID })
225 const hlsPlaylist = video.streamingPlaylists[0]
226
227 const shaBody = await server.streamingPlaylists.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
228
229 if (!shaBody[segmentName]) {
230 throw new Error('Segment SHA does not exist')
231 }
232
233 error = false
234 } catch {
235 error = true
236 await wait(100)
237 }
238 }
239 }
240
241 async waitUntilReplacedByReplay (options: OverrideCommandOptions & {
242 videoId: number | string
243 }) {
244 let video: VideoDetails
245
246 do {
247 video = await this.server.videos.getWithToken({ token: options.token, id: options.videoId })
248
249 await wait(500)
250 } while (video.isLive === true || video.state.id !== VideoState.PUBLISHED)
251 }
252
253 // ---------------------------------------------------------------------------
254
255 getSegmentFile (options: OverrideCommandOptions & {
256 videoUUID: string
257 playlistNumber: number
258 segment: number
259 objectStorage?: boolean // default false
260 }) {
261 const { playlistNumber, segment, videoUUID, objectStorage = false } = options
262
263 const segmentName = `${playlistNumber}-00000${segment}.ts`
264 const baseUrl = objectStorage
265 ? ObjectStorageCommand.getMockPlaylistBaseUrl()
266 : `${this.server.url}/static/streaming-playlists/hls`
267
268 const url = `${baseUrl}/${videoUUID}/${segmentName}`
269
270 return this.getRawRequest({
271 ...options,
272
273 url,
274 implicitToken: false,
275 defaultExpectedStatus: HttpStatusCode.OK_200
276 })
277 }
278
279 getPlaylistFile (options: OverrideCommandOptions & {
280 videoUUID: string
281 playlistName: string
282 objectStorage?: boolean // default false
283 }) {
284 const { playlistName, videoUUID, objectStorage = false } = options
285
286 const baseUrl = objectStorage
287 ? ObjectStorageCommand.getMockPlaylistBaseUrl()
288 : `${this.server.url}/static/streaming-playlists/hls`
289
290 const url = `${baseUrl}/${videoUUID}/${playlistName}`
291
292 return this.getRawRequest({
293 ...options,
294
295 url,
296 implicitToken: false,
297 defaultExpectedStatus: HttpStatusCode.OK_200
298 })
299 }
300
301 // ---------------------------------------------------------------------------
302
303 async countPlaylists (options: OverrideCommandOptions & {
304 videoUUID: string
305 }) {
306 const basePath = this.server.servers.buildDirectory('streaming-playlists')
307 const hlsPath = join(basePath, 'hls', options.videoUUID)
308
309 const files = await readdir(hlsPath)
310
311 return files.filter(f => f.endsWith('.m3u8')).length
312 }
313
314 private async waitUntilState (options: OverrideCommandOptions & {
315 videoId: number | string
316 state: VideoState
317 }) {
318 let video: VideoDetails
319
320 do {
321 video = await this.server.videos.getWithToken({ token: options.token, id: options.videoId })
322
323 await wait(500)
324 } while (video.state.id !== options.state)
325 }
326 }