]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/live/live.ts
d555cff194ff89c533fbf077c6e5e136de41fbda
[github/Chocobozzz/PeerTube.git] / server / tests / api / live / live.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { basename, join } from 'path'
6 import { ffprobePromise, getVideoStreamFromFile } from '@server/helpers/ffprobe-utils'
7 import {
8 checkLiveCleanupAfterSave,
9 checkLiveSegmentHash,
10 checkResolutionsInMasterPlaylist,
11 cleanupTests,
12 createMultipleServers,
13 doubleFollow,
14 killallServers,
15 LiveCommand,
16 makeRawRequest,
17 PeerTubeServer,
18 sendRTMPStream,
19 setAccessTokensToServers,
20 setDefaultVideoChannel,
21 stopFfmpeg,
22 testFfmpegStreamError,
23 testImage,
24 wait,
25 waitJobs,
26 waitUntilLivePublishedOnAllServers
27 } from '@shared/extra-utils'
28 import {
29 HttpStatusCode,
30 LiveVideo,
31 LiveVideoCreate,
32 VideoDetails,
33 VideoPrivacy,
34 VideoState,
35 VideoStreamingPlaylistType
36 } from '@shared/models'
37
38 const expect = chai.expect
39
40 describe('Test live', function () {
41 let servers: PeerTubeServer[] = []
42 let commands: LiveCommand[]
43
44 before(async function () {
45 this.timeout(120000)
46
47 servers = await createMultipleServers(2)
48
49 // Get the access tokens
50 await setAccessTokensToServers(servers)
51 await setDefaultVideoChannel(servers)
52
53 await servers[0].config.updateCustomSubConfig({
54 newConfig: {
55 live: {
56 enabled: true,
57 allowReplay: true,
58 transcoding: {
59 enabled: false
60 }
61 }
62 }
63 })
64
65 // Server 1 and server 2 follow each other
66 await doubleFollow(servers[0], servers[1])
67
68 commands = servers.map(s => s.live)
69 })
70
71 describe('Live creation, update and delete', function () {
72 let liveVideoUUID: string
73
74 it('Should create a live with the appropriate parameters', async function () {
75 this.timeout(20000)
76
77 const attributes: LiveVideoCreate = {
78 category: 1,
79 licence: 2,
80 language: 'fr',
81 description: 'super live description',
82 support: 'support field',
83 channelId: servers[0].store.channel.id,
84 nsfw: false,
85 waitTranscoding: false,
86 name: 'my super live',
87 tags: [ 'tag1', 'tag2' ],
88 commentsEnabled: false,
89 downloadEnabled: false,
90 saveReplay: true,
91 privacy: VideoPrivacy.PUBLIC,
92 previewfile: 'video_short1-preview.webm.jpg',
93 thumbnailfile: 'video_short1.webm.jpg'
94 }
95
96 const live = await commands[0].create({ fields: attributes })
97 liveVideoUUID = live.uuid
98
99 await waitJobs(servers)
100
101 for (const server of servers) {
102 const video = await server.videos.get({ id: liveVideoUUID })
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
110 expect(video.channel.name).to.equal(servers[0].store.channel.name)
111 expect(video.channel.host).to.equal(servers[0].store.channel.host)
112
113 expect(video.isLive).to.be.true
114
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
126 const live = await server.live.get({ videoId: liveVideoUUID })
127
128 if (server.url === servers[0].url) {
129 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
130 expect(live.streamKey).to.not.be.empty
131 } else {
132 expect(live.rtmpUrl).to.be.null
133 expect(live.streamKey).to.be.null
134 }
135
136 expect(live.saveReplay).to.be.true
137 }
138 })
139
140 it('Should have a default preview and thumbnail', async function () {
141 this.timeout(20000)
142
143 const attributes: LiveVideoCreate = {
144 name: 'default live thumbnail',
145 channelId: servers[0].store.channel.id,
146 privacy: VideoPrivacy.UNLISTED,
147 nsfw: true
148 }
149
150 const live = await commands[0].create({ fields: attributes })
151 const videoId = live.uuid
152
153 await waitJobs(servers)
154
155 for (const server of servers) {
156 const video = await server.videos.get({ id: videoId })
157 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
158 expect(video.nsfw).to.be.true
159
160 await makeRawRequest(server.url + video.thumbnailPath, HttpStatusCode.OK_200)
161 await makeRawRequest(server.url + video.previewPath, HttpStatusCode.OK_200)
162 }
163 })
164
165 it('Should not have the live listed since nobody streams into', async function () {
166 for (const server of servers) {
167 const { total, data } = await server.videos.list()
168
169 expect(total).to.equal(0)
170 expect(data).to.have.lengthOf(0)
171 }
172 })
173
174 it('Should not be able to update a live of another server', async function () {
175 await commands[1].update({ videoId: liveVideoUUID, fields: { saveReplay: false }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
176 })
177
178 it('Should update the live', async function () {
179 this.timeout(10000)
180
181 await commands[0].update({ videoId: liveVideoUUID, fields: { saveReplay: false } })
182 await waitJobs(servers)
183 })
184
185 it('Have the live updated', async function () {
186 for (const server of servers) {
187 const live = await server.live.get({ videoId: liveVideoUUID })
188
189 if (server.url === servers[0].url) {
190 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
191 expect(live.streamKey).to.not.be.empty
192 } else {
193 expect(live.rtmpUrl).to.be.null
194 expect(live.streamKey).to.be.null
195 }
196
197 expect(live.saveReplay).to.be.false
198 }
199 })
200
201 it('Delete the live', async function () {
202 this.timeout(10000)
203
204 await servers[0].videos.remove({ id: liveVideoUUID })
205 await waitJobs(servers)
206 })
207
208 it('Should have the live deleted', async function () {
209 for (const server of servers) {
210 await server.videos.get({ id: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
211 await server.live.get({ videoId: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
212 }
213 })
214 })
215
216 describe('Live filters', function () {
217 let ffmpegCommand: any
218 let liveVideoId: string
219 let vodVideoId: string
220
221 before(async function () {
222 this.timeout(120000)
223
224 vodVideoId = (await servers[0].videos.quickUpload({ name: 'vod video' })).uuid
225
226 const liveOptions = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: servers[0].store.channel.id }
227 const live = await commands[0].create({ fields: liveOptions })
228 liveVideoId = live.uuid
229
230 ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoId })
231 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
232 await waitJobs(servers)
233 })
234
235 it('Should only display lives', async function () {
236 const { data, total } = await servers[0].videos.list({ isLive: true })
237
238 expect(total).to.equal(1)
239 expect(data).to.have.lengthOf(1)
240 expect(data[0].name).to.equal('live')
241 })
242
243 it('Should not display lives', async function () {
244 const { data, total } = await servers[0].videos.list({ isLive: false })
245
246 expect(total).to.equal(1)
247 expect(data).to.have.lengthOf(1)
248 expect(data[0].name).to.equal('vod video')
249 })
250
251 it('Should display my lives', async function () {
252 this.timeout(60000)
253
254 await stopFfmpeg(ffmpegCommand)
255 await waitJobs(servers)
256
257 const { data } = await servers[0].videos.listMyVideos({ isLive: true })
258
259 const result = data.every(v => v.isLive)
260 expect(result).to.be.true
261 })
262
263 it('Should not display my lives', async function () {
264 const { data } = await servers[0].videos.listMyVideos({ isLive: false })
265
266 const result = data.every(v => !v.isLive)
267 expect(result).to.be.true
268 })
269
270 after(async function () {
271 await servers[0].videos.remove({ id: vodVideoId })
272 await servers[0].videos.remove({ id: liveVideoId })
273 })
274 })
275
276 describe('Stream checks', function () {
277 let liveVideo: LiveVideo & VideoDetails
278 let rtmpUrl: string
279
280 before(function () {
281 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
282 })
283
284 async function createLiveWrapper () {
285 const liveAttributes = {
286 name: 'user live',
287 channelId: servers[0].store.channel.id,
288 privacy: VideoPrivacy.PUBLIC,
289 saveReplay: false
290 }
291
292 const { uuid } = await commands[0].create({ fields: liveAttributes })
293
294 const live = await commands[0].get({ videoId: uuid })
295 const video = await servers[0].videos.get({ id: uuid })
296
297 return Object.assign(video, live)
298 }
299
300 it('Should not allow a stream without the appropriate path', async function () {
301 this.timeout(60000)
302
303 liveVideo = await createLiveWrapper()
304
305 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
306 await testFfmpegStreamError(command, true)
307 })
308
309 it('Should not allow a stream without the appropriate stream key', async function () {
310 this.timeout(60000)
311
312 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
313 await testFfmpegStreamError(command, true)
314 })
315
316 it('Should succeed with the correct params', async function () {
317 this.timeout(60000)
318
319 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
320 await testFfmpegStreamError(command, false)
321 })
322
323 it('Should list this live now someone stream into it', async function () {
324 for (const server of servers) {
325 const { total, data } = await server.videos.list()
326
327 expect(total).to.equal(1)
328 expect(data).to.have.lengthOf(1)
329
330 const video = data[0]
331 expect(video.name).to.equal('user live')
332 expect(video.isLive).to.be.true
333 }
334 })
335
336 it('Should not allow a stream on a live that was blacklisted', async function () {
337 this.timeout(60000)
338
339 liveVideo = await createLiveWrapper()
340
341 await servers[0].blacklist.add({ videoId: liveVideo.uuid })
342
343 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
344 await testFfmpegStreamError(command, true)
345 })
346
347 it('Should not allow a stream on a live that was deleted', async function () {
348 this.timeout(60000)
349
350 liveVideo = await createLiveWrapper()
351
352 await servers[0].videos.remove({ id: liveVideo.uuid })
353
354 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
355 await testFfmpegStreamError(command, true)
356 })
357 })
358
359 describe('Live transcoding', function () {
360 let liveVideoId: string
361
362 async function createLiveWrapper (saveReplay: boolean) {
363 const liveAttributes = {
364 name: 'live video',
365 channelId: servers[0].store.channel.id,
366 privacy: VideoPrivacy.PUBLIC,
367 saveReplay
368 }
369
370 const { uuid } = await commands[0].create({ fields: liveAttributes })
371 return uuid
372 }
373
374 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
375 for (const server of servers) {
376 const { data } = await server.videos.list()
377 expect(data.find(v => v.uuid === liveVideoId)).to.exist
378
379 const video = await server.videos.get({ id: liveVideoId })
380
381 expect(video.streamingPlaylists).to.have.lengthOf(1)
382
383 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
384 expect(hlsPlaylist).to.exist
385
386 // Only finite files are displayed
387 expect(hlsPlaylist.files).to.have.lengthOf(0)
388
389 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
390
391 for (let i = 0; i < resolutions.length; i++) {
392 const segmentNum = 3
393 const segmentName = `${i}-00000${segmentNum}.ts`
394 await commands[0].waitUntilSegmentGeneration({ videoUUID: video.uuid, resolution: i, segment: segmentNum })
395
396 const subPlaylist = await servers[0].streamingPlaylists.get({
397 url: `${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`
398 })
399
400 expect(subPlaylist).to.contain(segmentName)
401
402 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
403 await checkLiveSegmentHash({
404 server,
405 baseUrlSegment: baseUrlAndPath,
406 videoUUID: video.uuid,
407 segmentName,
408 hlsPlaylist
409 })
410 }
411 }
412 }
413
414 function updateConf (resolutions: number[]) {
415 return servers[0].config.updateCustomSubConfig({
416 newConfig: {
417 live: {
418 enabled: true,
419 allowReplay: true,
420 maxDuration: -1,
421 transcoding: {
422 enabled: true,
423 resolutions: {
424 '240p': resolutions.includes(240),
425 '360p': resolutions.includes(360),
426 '480p': resolutions.includes(480),
427 '720p': resolutions.includes(720),
428 '1080p': resolutions.includes(1080),
429 '2160p': resolutions.includes(2160)
430 }
431 }
432 }
433 }
434 })
435 }
436
437 before(async function () {
438 await updateConf([])
439 })
440
441 it('Should enable transcoding without additional resolutions', async function () {
442 this.timeout(60000)
443
444 liveVideoId = await createLiveWrapper(false)
445
446 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
447 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
448 await waitJobs(servers)
449
450 await testVideoResolutions(liveVideoId, [ 720 ])
451
452 await stopFfmpeg(ffmpegCommand)
453 })
454
455 it('Should enable transcoding with some resolutions', async function () {
456 this.timeout(60000)
457
458 const resolutions = [ 240, 480 ]
459 await updateConf(resolutions)
460 liveVideoId = await createLiveWrapper(false)
461
462 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
463 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
464 await waitJobs(servers)
465
466 await testVideoResolutions(liveVideoId, resolutions)
467
468 await stopFfmpeg(ffmpegCommand)
469 })
470
471 it('Should enable transcoding with some resolutions and correctly save them', async function () {
472 this.timeout(200000)
473
474 const resolutions = [ 240, 360, 720 ]
475
476 await updateConf(resolutions)
477 liveVideoId = await createLiveWrapper(true)
478
479 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
480 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
481 await waitJobs(servers)
482
483 await testVideoResolutions(liveVideoId, resolutions)
484
485 await stopFfmpeg(ffmpegCommand)
486 await commands[0].waitUntilEnded({ videoId: liveVideoId })
487
488 await waitJobs(servers)
489
490 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
491
492 const bitrateLimits = {
493 720: 5000 * 1000, // 60FPS
494 360: 1100 * 1000,
495 240: 600 * 1000
496 }
497
498 for (const server of servers) {
499 const video = await server.videos.get({ id: liveVideoId })
500
501 expect(video.state.id).to.equal(VideoState.PUBLISHED)
502 expect(video.duration).to.be.greaterThan(1)
503 expect(video.files).to.have.lengthOf(0)
504
505 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
506 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
507 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
508
509 // We should have generated random filenames
510 expect(basename(hlsPlaylist.playlistUrl)).to.not.equal('master.m3u8')
511 expect(basename(hlsPlaylist.segmentsSha256Url)).to.not.equal('segments-sha256.json')
512
513 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
514
515 for (const resolution of resolutions) {
516 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
517
518 expect(file).to.exist
519 expect(file.size).to.be.greaterThan(1)
520
521 if (resolution >= 720) {
522 expect(file.fps).to.be.approximately(60, 2)
523 } else {
524 expect(file.fps).to.be.approximately(30, 2)
525 }
526
527 const filename = basename(file.fileUrl)
528 expect(filename).to.not.contain(video.uuid)
529
530 const segmentPath = servers[0].servers.buildDirectory(join('streaming-playlists', 'hls', video.uuid, filename))
531
532 const probe = await ffprobePromise(segmentPath)
533 const videoStream = await getVideoStreamFromFile(segmentPath, probe)
534
535 expect(probe.format.bit_rate).to.be.below(bitrateLimits[videoStream.height])
536
537 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
538 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
539 }
540 }
541 })
542
543 it('Should correctly have cleaned up the live files', async function () {
544 this.timeout(30000)
545
546 await checkLiveCleanupAfterSave(servers[0], liveVideoId, [ 240, 360, 720 ])
547 })
548 })
549
550 describe('After a server restart', function () {
551 let liveVideoId: string
552 let liveVideoReplayId: string
553
554 async function createLiveWrapper (saveReplay: boolean) {
555 const liveAttributes = {
556 name: 'live video',
557 channelId: servers[0].store.channel.id,
558 privacy: VideoPrivacy.PUBLIC,
559 saveReplay
560 }
561
562 const { uuid } = await commands[0].create({ fields: liveAttributes })
563 return uuid
564 }
565
566 before(async function () {
567 this.timeout(120000)
568
569 liveVideoId = await createLiveWrapper(false)
570 liveVideoReplayId = await createLiveWrapper(true)
571
572 await Promise.all([
573 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId }),
574 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoReplayId })
575 ])
576
577 await Promise.all([
578 commands[0].waitUntilPublished({ videoId: liveVideoId }),
579 commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
580 ])
581
582 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoId, resolution: 0, segment: 2 })
583 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoReplayId, resolution: 0, segment: 2 })
584
585 await killallServers([ servers[0] ])
586 await servers[0].run()
587
588 await wait(5000)
589 })
590
591 it('Should cleanup lives', async function () {
592 this.timeout(60000)
593
594 await commands[0].waitUntilEnded({ videoId: liveVideoId })
595 })
596
597 it('Should save a live replay', async function () {
598 this.timeout(120000)
599
600 await commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
601 })
602 })
603
604 after(async function () {
605 await cleanupTests(servers)
606 })
607 })