]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/live/live.ts
Fix bitrate tests
[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({ rtmpBaseUrl: rtmpUrl + '/bad-live', streamKey: 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({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: '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({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: 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({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: 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({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: 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(120000)
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(120000)
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 correctly set the appropriate bitrate depending on the input', async function () {
472 this.timeout(120000)
473
474 liveVideoId = await createLiveWrapper(false)
475
476 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({
477 videoId: liveVideoId,
478 fixtureName: 'video_short.mp4',
479 copyCodecs: true
480 })
481 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
482 await waitJobs(servers)
483
484 const video = await servers[0].videos.get({ id: liveVideoId })
485
486 const masterPlaylist = video.streamingPlaylists[0].playlistUrl
487 const probe = await ffprobePromise(masterPlaylist)
488
489 const bitrates = probe.streams.map(s => parseInt(s.tags.variant_bitrate))
490 for (const bitrate of bitrates) {
491 expect(bitrate).to.exist
492 expect(isNaN(bitrate)).to.be.false
493 expect(bitrate).to.be.below(61_000_000) // video_short.mp4 bitrate
494 }
495
496 await stopFfmpeg(ffmpegCommand)
497 })
498
499 it('Should enable transcoding with some resolutions and correctly save them', async function () {
500 this.timeout(200000)
501
502 const resolutions = [ 240, 360, 720 ]
503
504 await updateConf(resolutions)
505 liveVideoId = await createLiveWrapper(true)
506
507 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
508 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
509 await waitJobs(servers)
510
511 await testVideoResolutions(liveVideoId, resolutions)
512
513 await stopFfmpeg(ffmpegCommand)
514 await commands[0].waitUntilEnded({ videoId: liveVideoId })
515
516 await waitJobs(servers)
517
518 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
519
520 const maxBitrateLimits = {
521 720: 6500 * 1000, // 60FPS
522 360: 1250 * 1000,
523 240: 700 * 1000
524 }
525
526 const minBitrateLimits = {
527 720: 5500 * 1000,
528 360: 1000 * 1000,
529 240: 550 * 1000
530 }
531
532 for (const server of servers) {
533 const video = await server.videos.get({ id: liveVideoId })
534
535 expect(video.state.id).to.equal(VideoState.PUBLISHED)
536 expect(video.duration).to.be.greaterThan(1)
537 expect(video.files).to.have.lengthOf(0)
538
539 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
540 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
541 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
542
543 // We should have generated random filenames
544 expect(basename(hlsPlaylist.playlistUrl)).to.not.equal('master.m3u8')
545 expect(basename(hlsPlaylist.segmentsSha256Url)).to.not.equal('segments-sha256.json')
546
547 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
548
549 for (const resolution of resolutions) {
550 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
551
552 expect(file).to.exist
553 expect(file.size).to.be.greaterThan(1)
554
555 if (resolution >= 720) {
556 expect(file.fps).to.be.approximately(60, 2)
557 } else {
558 expect(file.fps).to.be.approximately(30, 2)
559 }
560
561 const filename = basename(file.fileUrl)
562 expect(filename).to.not.contain(video.uuid)
563
564 const segmentPath = servers[0].servers.buildDirectory(join('streaming-playlists', 'hls', video.uuid, filename))
565
566 const probe = await ffprobePromise(segmentPath)
567 const videoStream = await getVideoStreamFromFile(segmentPath, probe)
568
569 expect(probe.format.bit_rate).to.be.below(maxBitrateLimits[videoStream.height])
570 expect(probe.format.bit_rate).to.be.at.least(minBitrateLimits[videoStream.height])
571
572 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
573 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
574 }
575 }
576 })
577
578 it('Should correctly have cleaned up the live files', async function () {
579 this.timeout(30000)
580
581 await checkLiveCleanupAfterSave(servers[0], liveVideoId, [ 240, 360, 720 ])
582 })
583 })
584
585 describe('After a server restart', function () {
586 let liveVideoId: string
587 let liveVideoReplayId: string
588
589 async function createLiveWrapper (saveReplay: boolean) {
590 const liveAttributes = {
591 name: 'live video',
592 channelId: servers[0].store.channel.id,
593 privacy: VideoPrivacy.PUBLIC,
594 saveReplay
595 }
596
597 const { uuid } = await commands[0].create({ fields: liveAttributes })
598 return uuid
599 }
600
601 before(async function () {
602 this.timeout(120000)
603
604 liveVideoId = await createLiveWrapper(false)
605 liveVideoReplayId = await createLiveWrapper(true)
606
607 await Promise.all([
608 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId }),
609 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoReplayId })
610 ])
611
612 await Promise.all([
613 commands[0].waitUntilPublished({ videoId: liveVideoId }),
614 commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
615 ])
616
617 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoId, resolution: 0, segment: 2 })
618 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoReplayId, resolution: 0, segment: 2 })
619
620 await killallServers([ servers[0] ])
621 await servers[0].run()
622
623 await wait(5000)
624 })
625
626 it('Should cleanup lives', async function () {
627 this.timeout(60000)
628
629 await commands[0].waitUntilEnded({ videoId: liveVideoId })
630 })
631
632 it('Should save a live replay', async function () {
633 this.timeout(120000)
634
635 await commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
636 })
637 })
638
639 after(async function () {
640 await cleanupTests(servers)
641 })
642 })