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