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