]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/live/live.ts
Merge branch 'release/5.0.0' into develop
[github/Chocobozzz/PeerTube.git] / server / tests / api / live / live.ts
CommitLineData
af4ae64f
C
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
86347717 3import { expect } from 'chai'
764b1a14 4import { basename, join } from 'path'
c729caf6 5import { ffprobePromise, getVideoStream } from '@server/helpers/ffmpeg'
cfd57d2c 6import { testImage, testVideoResolutions } from '@server/tests/shared'
367a9dc6 7import { getAllFiles, wait } from '@shared/core-utils'
c55e3d72
C
8import {
9 HttpStatusCode,
10 LiveVideo,
11 LiveVideoCreate,
f443a746 12 LiveVideoLatencyMode,
c55e3d72
C
13 VideoDetails,
14 VideoPrivacy,
15 VideoState,
16 VideoStreamingPlaylistType
17} from '@shared/models'
af4ae64f 18import {
af4ae64f 19 cleanupTests,
254d3579 20 createMultipleServers,
4c7e60bc 21 doubleFollow,
5c0904fc 22 killallServers,
4f219914 23 LiveCommand,
3545e72c 24 makeGetRequest,
af4ae64f 25 makeRawRequest,
254d3579 26 PeerTubeServer,
4c7e60bc 27 sendRTMPStream,
af4ae64f
C
28 setAccessTokensToServers,
29 setDefaultVideoChannel,
bd54ad19 30 stopFfmpeg,
97969c4e 31 testFfmpegStreamError,
bd54ad19 32 waitJobs,
4f219914 33 waitUntilLivePublishedOnAllServers
bf54587a 34} from '@shared/server-commands'
af4ae64f 35
af4ae64f 36describe('Test live', function () {
254d3579 37 let servers: PeerTubeServer[] = []
4f219914 38 let commands: LiveCommand[]
af4ae64f
C
39
40 before(async function () {
41 this.timeout(120000)
42
254d3579 43 servers = await createMultipleServers(2)
af4ae64f
C
44
45 // Get the access tokens
46 await setAccessTokensToServers(servers)
47 await setDefaultVideoChannel(servers)
48
89d241a7 49 await servers[0].config.updateCustomSubConfig({
65e6e260
C
50 newConfig: {
51 live: {
52 enabled: true,
53 allowReplay: true,
f443a746
C
54 latencySetting: {
55 enabled: true
56 },
65e6e260
C
57 transcoding: {
58 enabled: false
59 }
68e70a74 60 }
af4ae64f
C
61 }
62 })
63
64 // Server 1 and server 2 follow each other
65 await doubleFollow(servers[0], servers[1])
4f219914 66
89d241a7 67 commands = servers.map(s => s.live)
af4ae64f
C
68 })
69
70 describe('Live creation, update and delete', function () {
68e70a74 71 let liveVideoUUID: string
af4ae64f
C
72
73 it('Should create a live with the appropriate parameters', async function () {
74 this.timeout(20000)
75
76 const attributes: LiveVideoCreate = {
77 category: 1,
78 licence: 2,
79 language: 'fr',
80 description: 'super live description',
81 support: 'support field',
89d241a7 82 channelId: servers[0].store.channel.id,
af4ae64f
C
83 nsfw: false,
84 waitTranscoding: false,
85 name: 'my super live',
86 tags: [ 'tag1', 'tag2' ],
87 commentsEnabled: false,
88 downloadEnabled: false,
89 saveReplay: true,
f443a746 90 latencyMode: LiveVideoLatencyMode.SMALL_LATENCY,
af4ae64f
C
91 privacy: VideoPrivacy.PUBLIC,
92 previewfile: 'video_short1-preview.webm.jpg',
93 thumbnailfile: 'video_short1.webm.jpg'
94 }
95
04aed767 96 const live = await commands[0].create({ fields: attributes })
4f219914 97 liveVideoUUID = live.uuid
af4ae64f
C
98
99 await waitJobs(servers)
100
101 for (const server of servers) {
89d241a7 102 const video = await server.videos.get({ id: liveVideoUUID })
af4ae64f
C
103
104 expect(video.category.id).to.equal(1)
105 expect(video.licence.id).to.equal(2)
106 expect(video.language.id).to.equal('fr')
107 expect(video.description).to.equal('super live description')
108 expect(video.support).to.equal('support field')
109
89d241a7
C
110 expect(video.channel.name).to.equal(servers[0].store.channel.name)
111 expect(video.channel.host).to.equal(servers[0].store.channel.host)
af4ae64f 112
1ab60243
C
113 expect(video.isLive).to.be.true
114
af4ae64f
C
115 expect(video.nsfw).to.be.false
116 expect(video.waitTranscoding).to.be.false
117 expect(video.name).to.equal('my super live')
118 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
119 expect(video.commentsEnabled).to.be.false
120 expect(video.downloadEnabled).to.be.false
121 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
122
123 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
124 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
125
89d241a7 126 const live = await server.live.get({ videoId: liveVideoUUID })
af4ae64f
C
127
128 if (server.url === servers[0].url) {
c655c9ef 129 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
af4ae64f
C
130 expect(live.streamKey).to.not.be.empty
131 } else {
5d9b867e
C
132 expect(live.rtmpUrl).to.not.exist
133 expect(live.streamKey).to.not.exist
af4ae64f
C
134 }
135
136 expect(live.saveReplay).to.be.true
f443a746 137 expect(live.latencyMode).to.equal(LiveVideoLatencyMode.SMALL_LATENCY)
af4ae64f
C
138 }
139 })
140
141 it('Should have a default preview and thumbnail', async function () {
142 this.timeout(20000)
143
144 const attributes: LiveVideoCreate = {
145 name: 'default live thumbnail',
89d241a7 146 channelId: servers[0].store.channel.id,
af4ae64f
C
147 privacy: VideoPrivacy.UNLISTED,
148 nsfw: true
149 }
150
04aed767 151 const live = await commands[0].create({ fields: attributes })
4f219914 152 const videoId = live.uuid
af4ae64f
C
153
154 await waitJobs(servers)
155
156 for (const server of servers) {
89d241a7 157 const video = await server.videos.get({ id: videoId })
af4ae64f
C
158 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
159 expect(video.nsfw).to.be.true
160
3545e72c
C
161 await makeGetRequest({ url: server.url, path: video.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
162 await makeGetRequest({ url: server.url, path: video.previewPath, expectedStatus: HttpStatusCode.OK_200 })
af4ae64f
C
163 }
164 })
165
166 it('Should not have the live listed since nobody streams into', async function () {
167 for (const server of servers) {
89d241a7 168 const { total, data } = await server.videos.list()
af4ae64f 169
d23dd9fb
C
170 expect(total).to.equal(0)
171 expect(data).to.have.lengthOf(0)
af4ae64f
C
172 }
173 })
174
175 it('Should not be able to update a live of another server', async function () {
04aed767 176 await commands[1].update({ videoId: liveVideoUUID, fields: { saveReplay: false }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
af4ae64f
C
177 })
178
179 it('Should update the live', async function () {
180 this.timeout(10000)
181
f443a746 182 await commands[0].update({ videoId: liveVideoUUID, fields: { saveReplay: false, latencyMode: LiveVideoLatencyMode.DEFAULT } })
af4ae64f
C
183 await waitJobs(servers)
184 })
185
186 it('Have the live updated', async function () {
187 for (const server of servers) {
89d241a7 188 const live = await server.live.get({ videoId: liveVideoUUID })
af4ae64f
C
189
190 if (server.url === servers[0].url) {
c655c9ef 191 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
af4ae64f
C
192 expect(live.streamKey).to.not.be.empty
193 } else {
5d9b867e
C
194 expect(live.rtmpUrl).to.not.exist
195 expect(live.streamKey).to.not.exist
af4ae64f
C
196 }
197
198 expect(live.saveReplay).to.be.false
f443a746 199 expect(live.latencyMode).to.equal(LiveVideoLatencyMode.DEFAULT)
af4ae64f
C
200 }
201 })
202
203 it('Delete the live', async function () {
204 this.timeout(10000)
205
89d241a7 206 await servers[0].videos.remove({ id: liveVideoUUID })
af4ae64f
C
207 await waitJobs(servers)
208 })
209
210 it('Should have the live deleted', async function () {
211 for (const server of servers) {
89d241a7
C
212 await server.videos.get({ id: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
213 await server.live.get({ videoId: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
af4ae64f
C
214 }
215 })
216 })
217
1fd61899 218 describe('Live filters', function () {
4f219914 219 let ffmpegCommand: any
1fd61899
C
220 let liveVideoId: string
221 let vodVideoId: string
222
223 before(async function () {
3b2006bb 224 this.timeout(240000)
1fd61899 225
89d241a7 226 vodVideoId = (await servers[0].videos.quickUpload({ name: 'vod video' })).uuid
1fd61899 227
89d241a7 228 const liveOptions = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: servers[0].store.channel.id }
04aed767 229 const live = await commands[0].create({ fields: liveOptions })
4f219914 230 liveVideoId = live.uuid
1fd61899 231
89d241a7 232 ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoId })
8ebf2a5d 233 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
1fd61899
C
234 await waitJobs(servers)
235 })
236
237 it('Should only display lives', async function () {
89d241a7 238 const { data, total } = await servers[0].videos.list({ isLive: true })
1fd61899 239
d23dd9fb
C
240 expect(total).to.equal(1)
241 expect(data).to.have.lengthOf(1)
242 expect(data[0].name).to.equal('live')
1fd61899
C
243 })
244
245 it('Should not display lives', async function () {
89d241a7 246 const { data, total } = await servers[0].videos.list({ isLive: false })
1fd61899 247
d23dd9fb
C
248 expect(total).to.equal(1)
249 expect(data).to.have.lengthOf(1)
250 expect(data[0].name).to.equal('vod video')
1fd61899
C
251 })
252
253 it('Should display my lives', async function () {
254 this.timeout(60000)
255
4f219914 256 await stopFfmpeg(ffmpegCommand)
1fd61899
C
257 await waitJobs(servers)
258
89d241a7 259 const { data } = await servers[0].videos.listMyVideos({ isLive: true })
1fd61899 260
d23dd9fb 261 const result = data.every(v => v.isLive)
1fd61899
C
262 expect(result).to.be.true
263 })
264
265 it('Should not display my lives', async function () {
89d241a7 266 const { data } = await servers[0].videos.listMyVideos({ isLive: false })
1fd61899 267
d23dd9fb 268 const result = data.every(v => !v.isLive)
1fd61899
C
269 expect(result).to.be.true
270 })
271
272 after(async function () {
89d241a7
C
273 await servers[0].videos.remove({ id: vodVideoId })
274 await servers[0].videos.remove({ id: liveVideoId })
1fd61899
C
275 })
276 })
277
68e70a74
C
278 describe('Stream checks', function () {
279 let liveVideo: LiveVideo & VideoDetails
280 let rtmpUrl: string
281
282 before(function () {
c655c9ef 283 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
68e70a74 284 })
af4ae64f 285
68e70a74 286 async function createLiveWrapper () {
97969c4e
C
287 const liveAttributes = {
288 name: 'user live',
89d241a7 289 channelId: servers[0].store.channel.id,
97969c4e 290 privacy: VideoPrivacy.PUBLIC,
68e70a74 291 saveReplay: false
97969c4e
C
292 }
293
04aed767 294 const { uuid } = await commands[0].create({ fields: liveAttributes })
97969c4e 295
04aed767 296 const live = await commands[0].get({ videoId: uuid })
89d241a7 297 const video = await servers[0].videos.get({ id: uuid })
97969c4e 298
d23dd9fb 299 return Object.assign(video, live)
68e70a74 300 }
97969c4e 301
68e70a74 302 it('Should not allow a stream without the appropriate path', async function () {
2df6f943 303 this.timeout(60000)
97969c4e 304
68e70a74 305 liveVideo = await createLiveWrapper()
97969c4e 306
c826f34a 307 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/bad-live', streamKey: liveVideo.streamKey })
68e70a74 308 await testFfmpegStreamError(command, true)
af4ae64f
C
309 })
310
68e70a74 311 it('Should not allow a stream without the appropriate stream key', async function () {
2df6f943 312 this.timeout(60000)
97969c4e 313
c826f34a 314 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: 'bad-stream-key' })
68e70a74 315 await testFfmpegStreamError(command, true)
97969c4e
C
316 })
317
68e70a74 318 it('Should succeed with the correct params', async function () {
2df6f943 319 this.timeout(60000)
97969c4e 320
c826f34a 321 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
68e70a74 322 await testFfmpegStreamError(command, false)
af4ae64f
C
323 })
324
1ab60243
C
325 it('Should list this live now someone stream into it', async function () {
326 for (const server of servers) {
89d241a7 327 const { total, data } = await server.videos.list()
1ab60243 328
d23dd9fb
C
329 expect(total).to.equal(1)
330 expect(data).to.have.lengthOf(1)
1ab60243 331
d23dd9fb 332 const video = data[0]
1ab60243
C
333 expect(video.name).to.equal('user live')
334 expect(video.isLive).to.be.true
335 }
336 })
337
68e70a74 338 it('Should not allow a stream on a live that was blacklisted', async function () {
2df6f943 339 this.timeout(60000)
97969c4e 340
68e70a74 341 liveVideo = await createLiveWrapper()
af4ae64f 342
89d241a7 343 await servers[0].blacklist.add({ videoId: liveVideo.uuid })
af4ae64f 344
c826f34a 345 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
68e70a74 346 await testFfmpegStreamError(command, true)
af4ae64f
C
347 })
348
68e70a74 349 it('Should not allow a stream on a live that was deleted', async function () {
2df6f943 350 this.timeout(60000)
af4ae64f 351
68e70a74 352 liveVideo = await createLiveWrapper()
af4ae64f 353
89d241a7 354 await servers[0].videos.remove({ id: liveVideo.uuid })
af4ae64f 355
c826f34a 356 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
68e70a74 357 await testFfmpegStreamError(command, true)
af4ae64f
C
358 })
359 })
360
361 describe('Live transcoding', function () {
bd54ad19
C
362 let liveVideoId: string
363
364 async function createLiveWrapper (saveReplay: boolean) {
365 const liveAttributes = {
366 name: 'live video',
89d241a7 367 channelId: servers[0].store.channel.id,
bd54ad19
C
368 privacy: VideoPrivacy.PUBLIC,
369 saveReplay
370 }
371
04aed767 372 const { uuid } = await commands[0].create({ fields: liveAttributes })
4f219914 373 return uuid
bd54ad19
C
374 }
375
bd54ad19 376 function updateConf (resolutions: number[]) {
89d241a7 377 return servers[0].config.updateCustomSubConfig({
65e6e260
C
378 newConfig: {
379 live: {
bd54ad19 380 enabled: true,
65e6e260
C
381 allowReplay: true,
382 maxDuration: -1,
383 transcoding: {
384 enabled: true,
385 resolutions: {
8dd754c7 386 '144p': resolutions.includes(144),
65e6e260
C
387 '240p': resolutions.includes(240),
388 '360p': resolutions.includes(360),
389 '480p': resolutions.includes(480),
390 '720p': resolutions.includes(720),
391 '1080p': resolutions.includes(1080),
392 '2160p': resolutions.includes(2160)
393 }
bd54ad19
C
394 }
395 }
396 }
397 })
398 }
399
400 before(async function () {
401 await updateConf([])
402 })
af4ae64f
C
403
404 it('Should enable transcoding without additional resolutions', async function () {
d8fb3a2f 405 this.timeout(120000)
bd54ad19
C
406
407 liveVideoId = await createLiveWrapper(false)
408
4f219914 409 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
8ebf2a5d 410 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
bd54ad19 411 await waitJobs(servers)
af4ae64f 412
cfd57d2c
C
413 await testVideoResolutions({
414 originServer: servers[0],
415 servers,
416 liveVideoId,
417 resolutions: [ 720 ],
418 objectStorage: false,
419 transcoded: true
420 })
bd54ad19 421
4f219914 422 await stopFfmpeg(ffmpegCommand)
af4ae64f
C
423 })
424
1593e0dd
C
425 it('Should transcode audio only RTMP stream', async function () {
426 this.timeout(120000)
427
428 liveVideoId = await createLiveWrapper(false)
429
430 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short_no_audio.mp4' })
431 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
432 await waitJobs(servers)
433
434 await stopFfmpeg(ffmpegCommand)
435 })
436
af4ae64f 437 it('Should enable transcoding with some resolutions', async function () {
d8fb3a2f 438 this.timeout(120000)
bd54ad19
C
439
440 const resolutions = [ 240, 480 ]
441 await updateConf(resolutions)
442 liveVideoId = await createLiveWrapper(false)
443
4f219914 444 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
8ebf2a5d 445 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
bd54ad19
C
446 await waitJobs(servers)
447
cfd57d2c
C
448 await testVideoResolutions({
449 originServer: servers[0],
450 servers,
451 liveVideoId,
452 resolutions: resolutions.concat([ 720 ]),
453 objectStorage: false,
454 transcoded: true
455 })
bd54ad19 456
4f219914 457 await stopFfmpeg(ffmpegCommand)
af4ae64f
C
458 })
459
c826f34a
C
460 it('Should correctly set the appropriate bitrate depending on the input', async function () {
461 this.timeout(120000)
462
463 liveVideoId = await createLiveWrapper(false)
464
465 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({
466 videoId: liveVideoId,
467 fixtureName: 'video_short.mp4',
468 copyCodecs: true
469 })
470 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
471 await waitJobs(servers)
472
473 const video = await servers[0].videos.get({ id: liveVideoId })
474
475 const masterPlaylist = video.streamingPlaylists[0].playlistUrl
476 const probe = await ffprobePromise(masterPlaylist)
477
478 const bitrates = probe.streams.map(s => parseInt(s.tags.variant_bitrate))
479 for (const bitrate of bitrates) {
480 expect(bitrate).to.exist
481 expect(isNaN(bitrate)).to.be.false
482 expect(bitrate).to.be.below(61_000_000) // video_short.mp4 bitrate
483 }
484
485 await stopFfmpeg(ffmpegCommand)
486 })
487
af4ae64f 488 it('Should enable transcoding with some resolutions and correctly save them', async function () {
5c08e1c2 489 this.timeout(400_000)
bd54ad19
C
490
491 const resolutions = [ 240, 360, 720 ]
ca5c612b 492
bd54ad19
C
493 await updateConf(resolutions)
494 liveVideoId = await createLiveWrapper(true)
495
4f219914 496 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
8ebf2a5d 497 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
bd54ad19
C
498 await waitJobs(servers)
499
cfd57d2c
C
500 await testVideoResolutions({
501 originServer: servers[0],
502 servers,
503 liveVideoId,
504 resolutions,
505 objectStorage: false,
506 transcoded: true
507 })
bd54ad19 508
4f219914 509 await stopFfmpeg(ffmpegCommand)
04aed767 510 await commands[0].waitUntilEnded({ videoId: liveVideoId })
34caef7f 511
bd54ad19
C
512 await waitJobs(servers)
513
8ebf2a5d 514 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
e0783718 515
9f430a53
C
516 const maxBitrateLimits = {
517 720: 6500 * 1000, // 60FPS
518 360: 1250 * 1000,
519 240: 700 * 1000
520 }
521
522 const minBitrateLimits = {
dd84f4f2 523 720: 4800 * 1000,
9f430a53
C
524 360: 1000 * 1000,
525 240: 550 * 1000
ca5c612b
C
526 }
527
bd54ad19 528 for (const server of servers) {
89d241a7 529 const video = await server.videos.get({ id: liveVideoId })
bd54ad19 530
e0783718 531 expect(video.state.id).to.equal(VideoState.PUBLISHED)
bd54ad19
C
532 expect(video.duration).to.be.greaterThan(1)
533 expect(video.files).to.have.lengthOf(0)
534
535 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
3545e72c
C
536 await makeRawRequest({ url: hlsPlaylist.playlistUrl, expectedStatus: HttpStatusCode.OK_200 })
537 await makeRawRequest({ url: hlsPlaylist.segmentsSha256Url, expectedStatus: HttpStatusCode.OK_200 })
bd54ad19 538
764b1a14
C
539 // We should have generated random filenames
540 expect(basename(hlsPlaylist.playlistUrl)).to.not.equal('master.m3u8')
541 expect(basename(hlsPlaylist.segmentsSha256Url)).to.not.equal('segments-sha256.json')
542
bd54ad19
C
543 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
544
545 for (const resolution of resolutions) {
546 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
547
548 expect(file).to.exist
bd54ad19
C
549 expect(file.size).to.be.greaterThan(1)
550
884d2c39 551 if (resolution >= 720) {
f4057afd 552 expect(file.fps).to.be.approximately(60, 10)
884d2c39 553 } else {
43972ee4 554 expect(file.fps).to.be.approximately(30, 3)
884d2c39
C
555 }
556
764b1a14
C
557 const filename = basename(file.fileUrl)
558 expect(filename).to.not.contain(video.uuid)
559
89d241a7 560 const segmentPath = servers[0].servers.buildDirectory(join('streaming-playlists', 'hls', video.uuid, filename))
ca5c612b
C
561
562 const probe = await ffprobePromise(segmentPath)
c729caf6 563 const videoStream = await getVideoStream(segmentPath, probe)
6b67897e 564
9f430a53
C
565 expect(probe.format.bit_rate).to.be.below(maxBitrateLimits[videoStream.height])
566 expect(probe.format.bit_rate).to.be.at.least(minBitrateLimits[videoStream.height])
ca5c612b 567
3545e72c
C
568 await makeRawRequest({ url: file.torrentUrl, expectedStatus: HttpStatusCode.OK_200 })
569 await makeRawRequest({ url: file.fileUrl, expectedStatus: HttpStatusCode.OK_200 })
bd54ad19
C
570 }
571 }
af4ae64f
C
572 })
573
84cae54e
C
574 it('Should not generate an upper resolution than original file', async function () {
575 this.timeout(400_000)
576
577 const resolutions = [ 240, 480 ]
578 await updateConf(resolutions)
579
580 await servers[0].config.updateExistingSubConfig({
581 newConfig: {
582 live: {
583 transcoding: {
584 alwaysTranscodeOriginalResolution: false
585 }
586 }
587 }
588 })
589
590 liveVideoId = await createLiveWrapper(true)
591
592 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
593 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
594 await waitJobs(servers)
595
cfd57d2c
C
596 await testVideoResolutions({
597 originServer: servers[0],
598 servers,
599 liveVideoId,
600 resolutions,
601 objectStorage: false,
602 transcoded: true
603 })
84cae54e
C
604
605 await stopFfmpeg(ffmpegCommand)
606 await commands[0].waitUntilEnded({ videoId: liveVideoId })
607
608 await waitJobs(servers)
609
610 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
611
612 const video = await servers[0].videos.get({ id: liveVideoId })
613 const hlsFiles = video.streamingPlaylists[0].files
614
615 expect(video.files).to.have.lengthOf(0)
616 expect(hlsFiles).to.have.lengthOf(resolutions.length)
617
618 // eslint-disable-next-line @typescript-eslint/require-array-sort-compare
619 expect(getAllFiles(video).map(f => f.resolution.id).sort()).to.deep.equal(resolutions)
620 })
621
622 it('Should only keep the original resolution if all resolutions are disabled', async function () {
70c6a848 623 this.timeout(600_000)
84cae54e
C
624
625 await updateConf([])
626 liveVideoId = await createLiveWrapper(true)
627
628 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
629 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
630 await waitJobs(servers)
631
cfd57d2c
C
632 await testVideoResolutions({
633 originServer: servers[0],
634 servers,
635 liveVideoId,
636 resolutions: [ 720 ],
637 objectStorage: false,
638 transcoded: true
639 })
84cae54e
C
640
641 await stopFfmpeg(ffmpegCommand)
642 await commands[0].waitUntilEnded({ videoId: liveVideoId })
643
644 await waitJobs(servers)
645
646 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
647
648 const video = await servers[0].videos.get({ id: liveVideoId })
649 const hlsFiles = video.streamingPlaylists[0].files
650
651 expect(video.files).to.have.lengthOf(0)
652 expect(hlsFiles).to.have.lengthOf(1)
bd54ad19 653
84cae54e 654 expect(hlsFiles[0].resolution.id).to.equal(720)
af4ae64f
C
655 })
656 })
657
5c0904fc
C
658 describe('After a server restart', function () {
659 let liveVideoId: string
660 let liveVideoReplayId: string
98ebfa39 661 let permanentLiveVideoReplayId: string
5c0904fc 662
98ebfa39
C
663 let permanentLiveReplayName: string
664
26e3e98f
C
665 let beforeServerRestart: Date
666
98ebfa39
C
667 async function createLiveWrapper (options: { saveReplay: boolean, permanent: boolean }) {
668 const liveAttributes: LiveVideoCreate = {
5c0904fc 669 name: 'live video',
89d241a7 670 channelId: servers[0].store.channel.id,
5c0904fc 671 privacy: VideoPrivacy.PUBLIC,
98ebfa39
C
672 saveReplay: options.saveReplay,
673 permanentLive: options.permanent
5c0904fc
C
674 }
675
04aed767 676 const { uuid } = await commands[0].create({ fields: liveAttributes })
4f219914 677 return uuid
5c0904fc
C
678 }
679
680 before(async function () {
70c6a848 681 this.timeout(600_000)
5c0904fc 682
98ebfa39
C
683 liveVideoId = await createLiveWrapper({ saveReplay: false, permanent: false })
684 liveVideoReplayId = await createLiveWrapper({ saveReplay: true, permanent: false })
685 permanentLiveVideoReplayId = await createLiveWrapper({ saveReplay: true, permanent: true })
5c0904fc
C
686
687 await Promise.all([
4f219914 688 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId }),
98ebfa39 689 commands[0].sendRTMPStreamInVideo({ videoId: permanentLiveVideoReplayId }),
4f219914 690 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoReplayId })
5c0904fc
C
691 ])
692
693 await Promise.all([
04aed767 694 commands[0].waitUntilPublished({ videoId: liveVideoId }),
98ebfa39 695 commands[0].waitUntilPublished({ videoId: permanentLiveVideoReplayId }),
04aed767 696 commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
5c0904fc
C
697 ])
698
bbae45c3
C
699 for (const videoUUID of [ liveVideoId, liveVideoReplayId, permanentLiveVideoReplayId ]) {
700 await commands[0].waitUntilSegmentGeneration({
701 server: servers[0],
702 videoUUID,
703 playlistNumber: 0,
704 segment: 2,
705 objectStorage: false
706 })
707 }
98ebfa39
C
708
709 {
710 const video = await servers[0].videos.get({ id: permanentLiveVideoReplayId })
711 permanentLiveReplayName = video.name + ' - ' + new Date(video.publishedAt).toLocaleString()
712 }
0d8de275 713
9293139f 714 await killallServers([ servers[0] ])
26e3e98f
C
715
716 beforeServerRestart = new Date()
254d3579 717 await servers[0].run()
5c0904fc
C
718
719 await wait(5000)
98ebfa39 720 await waitJobs(servers)
5c0904fc
C
721 })
722
723 it('Should cleanup lives', async function () {
724 this.timeout(60000)
725
04aed767 726 await commands[0].waitUntilEnded({ videoId: liveVideoId })
98ebfa39 727 await commands[0].waitUntilWaiting({ videoId: permanentLiveVideoReplayId })
5c0904fc
C
728 })
729
98ebfa39 730 it('Should save a non permanent live replay', async function () {
3cb60ca1 731 this.timeout(240000)
5c0904fc 732
04aed767 733 await commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
26e3e98f
C
734
735 const session = await commands[0].getReplaySession({ videoId: liveVideoReplayId })
736 expect(session.endDate).to.exist
737 expect(new Date(session.endDate)).to.be.above(beforeServerRestart)
5c0904fc 738 })
98ebfa39
C
739
740 it('Should have saved a permanent live replay', async function () {
741 this.timeout(120000)
742
743 const { data } = await servers[0].videos.listMyVideos({ sort: '-publishedAt' })
744 expect(data.find(v => v.name === permanentLiveReplayName)).to.exist
745 })
5c0904fc
C
746 })
747
af4ae64f
C
748 after(async function () {
749 await cleanupTests(servers)
750 })
751})