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