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