]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/live/live.ts
Cleanup lives on server restart
[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 { FfmpegCommand } from 'fluent-ffmpeg'
6 import { getLiveNotificationSocket } from '@shared/extra-utils/socket/socket-io'
7 import { LiveVideo, LiveVideoCreate, Video, VideoDetails, VideoPrivacy, VideoState, VideoStreamingPlaylistType } from '@shared/models'
8 import {
9 addVideoToBlacklist,
10 checkLiveCleanup,
11 checkLiveSegmentHash,
12 checkResolutionsInMasterPlaylist,
13 checkSegmentHash,
14 cleanupTests,
15 createLive,
16 doubleFollow,
17 flushAndRunMultipleServers,
18 getLive,
19 getPlaylist,
20 getVideo,
21 getVideoIdFromUUID,
22 getVideosList,
23 killallServers,
24 makeRawRequest,
25 removeVideo,
26 reRunServer,
27 sendRTMPStream,
28 sendRTMPStreamInVideo,
29 ServerInfo,
30 setAccessTokensToServers,
31 setDefaultVideoChannel,
32 stopFfmpeg,
33 testFfmpegStreamError,
34 testImage,
35 updateCustomSubConfig,
36 updateLive,
37 viewVideo,
38 wait,
39 waitJobs,
40 waitUntilLiveStarts,
41 waitUntilLog
42 } from '../../../../shared/extra-utils'
43
44 const expect = chai.expect
45
46 describe('Test live', function () {
47 let servers: ServerInfo[] = []
48
49 before(async function () {
50 this.timeout(120000)
51
52 servers = await flushAndRunMultipleServers(2)
53
54 // Get the access tokens
55 await setAccessTokensToServers(servers)
56 await setDefaultVideoChannel(servers)
57
58 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
59 live: {
60 enabled: true,
61 allowReplay: true,
62 transcoding: {
63 enabled: false
64 }
65 }
66 })
67
68 // Server 1 and server 2 follow each other
69 await doubleFollow(servers[0], servers[1])
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 res = await createLive(servers[0].url, servers[0].accessToken, attributes)
98 liveVideoUUID = res.body.video.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.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 resLive = await getLive(server.url, server.accessToken, liveVideoUUID)
127 const live: LiveVideo = resLive.body
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 res = await createLive(servers[0].url, servers[0].accessToken, attributes)
152 const videoId = res.body.video.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, 200)
164 await makeRawRequest(server.url + video.previewPath, 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 updateLive(servers[1].url, servers[1].accessToken, liveVideoUUID, { saveReplay: false }, 403)
179 })
180
181 it('Should update the live', async function () {
182 this.timeout(10000)
183
184 await updateLive(servers[0].url, servers[0].accessToken, liveVideoUUID, { saveReplay: false })
185 await waitJobs(servers)
186 })
187
188 it('Have the live updated', async function () {
189 for (const server of servers) {
190 const res = await getLive(server.url, server.accessToken, liveVideoUUID)
191 const live: LiveVideo = res.body
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, 404)
215 await getLive(server.url, server.accessToken, liveVideoUUID, 404)
216 }
217 })
218 })
219
220 describe('Stream checks', function () {
221 let liveVideo: LiveVideo & VideoDetails
222 let rtmpUrl: string
223
224 before(function () {
225 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
226 })
227
228 async function createLiveWrapper () {
229 const liveAttributes = {
230 name: 'user live',
231 channelId: servers[0].videoChannel.id,
232 privacy: VideoPrivacy.PUBLIC,
233 saveReplay: false
234 }
235
236 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
237 const uuid = res.body.video.uuid
238
239 const resLive = await getLive(servers[0].url, servers[0].accessToken, uuid)
240 const resVideo = await getVideo(servers[0].url, uuid)
241
242 return Object.assign(resVideo.body, resLive.body) as LiveVideo & VideoDetails
243 }
244
245 it('Should not allow a stream without the appropriate path', async function () {
246 this.timeout(30000)
247
248 liveVideo = await createLiveWrapper()
249
250 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
251 await testFfmpegStreamError(command, true)
252 })
253
254 it('Should not allow a stream without the appropriate stream key', async function () {
255 this.timeout(30000)
256
257 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
258 await testFfmpegStreamError(command, true)
259 })
260
261 it('Should succeed with the correct params', async function () {
262 this.timeout(30000)
263
264 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
265 await testFfmpegStreamError(command, false)
266 })
267
268 it('Should not allow a stream on a live that was blacklisted', async function () {
269 this.timeout(30000)
270
271 liveVideo = await createLiveWrapper()
272
273 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideo.uuid)
274
275 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
276 await testFfmpegStreamError(command, true)
277 })
278
279 it('Should not allow a stream on a live that was deleted', async function () {
280 this.timeout(30000)
281
282 liveVideo = await createLiveWrapper()
283
284 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid)
285
286 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
287 await testFfmpegStreamError(command, true)
288 })
289 })
290
291 describe('Live transcoding', function () {
292 let liveVideoId: string
293
294 async function createLiveWrapper (saveReplay: boolean) {
295 const liveAttributes = {
296 name: 'live video',
297 channelId: servers[0].videoChannel.id,
298 privacy: VideoPrivacy.PUBLIC,
299 saveReplay
300 }
301
302 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
303 return res.body.video.uuid
304 }
305
306 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
307 for (const server of servers) {
308 const resList = await getVideosList(server.url)
309 const videos: Video[] = resList.body.data
310
311 expect(videos.find(v => v.uuid === liveVideoId)).to.exist
312
313 const resVideo = await getVideo(server.url, liveVideoId)
314 const video: VideoDetails = resVideo.body
315
316 expect(video.streamingPlaylists).to.have.lengthOf(1)
317
318 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
319 expect(hlsPlaylist).to.exist
320
321 // Only finite files are displayed
322 expect(hlsPlaylist.files).to.have.lengthOf(0)
323
324 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions)
325
326 for (let i = 0; i < resolutions.length; i++) {
327 const segmentName = `${i}-000001.ts`
328 await waitUntilLog(servers[0], `${video.uuid}/${segmentName}`, 1, false)
329
330 const res = await getPlaylist(`${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`)
331 const subPlaylist = res.text
332
333 expect(subPlaylist).to.contain(segmentName)
334
335 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
336 await checkLiveSegmentHash(baseUrlAndPath, video.uuid, segmentName, hlsPlaylist)
337 }
338 }
339 }
340
341 function updateConf (resolutions: number[]) {
342 return updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
343 live: {
344 enabled: true,
345 allowReplay: true,
346 maxDuration: null,
347 transcoding: {
348 enabled: true,
349 resolutions: {
350 '240p': resolutions.includes(240),
351 '360p': resolutions.includes(360),
352 '480p': resolutions.includes(480),
353 '720p': resolutions.includes(720),
354 '1080p': resolutions.includes(1080),
355 '2160p': resolutions.includes(2160)
356 }
357 }
358 }
359 })
360 }
361
362 before(async function () {
363 await updateConf([])
364 })
365
366 it('Should enable transcoding without additional resolutions', async function () {
367 this.timeout(30000)
368
369 liveVideoId = await createLiveWrapper(false)
370
371 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
372 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
373 await waitJobs(servers)
374
375 await testVideoResolutions(liveVideoId, [ 720 ])
376
377 await stopFfmpeg(command)
378 })
379
380 it('Should enable transcoding with some resolutions', async function () {
381 this.timeout(30000)
382
383 const resolutions = [ 240, 480 ]
384 await updateConf(resolutions)
385 liveVideoId = await createLiveWrapper(false)
386
387 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
388 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
389 await waitJobs(servers)
390
391 await testVideoResolutions(liveVideoId, resolutions)
392
393 await stopFfmpeg(command)
394 })
395
396 it('Should enable transcoding with some resolutions and correctly save them', async function () {
397 this.timeout(60000)
398
399 const resolutions = [ 240, 360, 720 ]
400 await updateConf(resolutions)
401 liveVideoId = await createLiveWrapper(true)
402
403 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
404 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
405 await waitJobs(servers)
406
407 await testVideoResolutions(liveVideoId, resolutions)
408
409 await stopFfmpeg(command)
410
411 await waitJobs(servers)
412
413 for (const server of servers) {
414 const resVideo = await getVideo(server.url, liveVideoId)
415 const video: VideoDetails = resVideo.body
416
417 expect(video.duration).to.be.greaterThan(1)
418 expect(video.files).to.have.lengthOf(0)
419
420 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
421
422 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
423
424 for (const resolution of resolutions) {
425 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
426
427 expect(file).to.exist
428 expect(file.fps).to.equal(25)
429 expect(file.size).to.be.greaterThan(1)
430
431 await makeRawRequest(file.torrentUrl, 200)
432 await makeRawRequest(file.fileUrl, 200)
433 }
434 }
435 })
436
437 it('Should correctly have cleaned up the live files', async function () {
438 this.timeout(30000)
439
440 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
441 })
442 })
443
444 describe('Live views', function () {
445 let liveVideoId: string
446 let command: FfmpegCommand
447
448 async function countViews (expected: number) {
449 for (const server of servers) {
450 const res = await getVideo(server.url, liveVideoId)
451 const video: VideoDetails = res.body
452
453 expect(video.views).to.equal(expected)
454 }
455 }
456
457 before(async function () {
458 this.timeout(30000)
459
460 const liveAttributes = {
461 name: 'live video',
462 channelId: servers[0].videoChannel.id,
463 privacy: VideoPrivacy.PUBLIC
464 }
465
466 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
467 liveVideoId = res.body.video.uuid
468
469 command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
470 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
471 await waitJobs(servers)
472 })
473
474 it('Should display no views for a live', async function () {
475 await countViews(0)
476 })
477
478 it('Should view a live twice and display 1 view', async function () {
479 this.timeout(30000)
480
481 await viewVideo(servers[0].url, liveVideoId)
482 await viewVideo(servers[0].url, liveVideoId)
483
484 await wait(7000)
485
486 await waitJobs(servers)
487
488 await countViews(1)
489 })
490
491 it('Should wait and display 0 views', async function () {
492 this.timeout(30000)
493
494 await wait(7000)
495 await waitJobs(servers)
496
497 await countViews(0)
498 })
499
500 it('Should view a live on a remote and on local and display 2 views', async function () {
501 this.timeout(30000)
502
503 await viewVideo(servers[0].url, liveVideoId)
504 await viewVideo(servers[1].url, liveVideoId)
505 await viewVideo(servers[1].url, liveVideoId)
506
507 await wait(7000)
508 await waitJobs(servers)
509
510 await countViews(2)
511 })
512
513 after(async function () {
514 await stopFfmpeg(command)
515 })
516 })
517
518 describe('Live socket messages', function () {
519
520 async function createLiveWrapper () {
521 const liveAttributes = {
522 name: 'live video',
523 channelId: servers[0].videoChannel.id,
524 privacy: VideoPrivacy.PUBLIC
525 }
526
527 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
528 return res.body.video.uuid
529 }
530
531 it('Should correctly send a message when the live starts and ends', async function () {
532 this.timeout(60000)
533
534 const localStateChanges: VideoState[] = []
535 const remoteStateChanges: VideoState[] = []
536
537 const liveVideoUUID = await createLiveWrapper()
538 await waitJobs(servers)
539
540 {
541 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
542
543 const localSocket = getLiveNotificationSocket(servers[0].url)
544 localSocket.on('state-change', data => localStateChanges.push(data.state))
545 localSocket.emit('subscribe', { videoId })
546 }
547
548 {
549 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
550
551 const remoteSocket = getLiveNotificationSocket(servers[1].url)
552 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
553 remoteSocket.emit('subscribe', { videoId })
554 }
555
556 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
557 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoUUID)
558 await waitJobs(servers)
559
560 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
561 expect(stateChanges).to.have.lengthOf(1)
562 expect(stateChanges[0]).to.equal(VideoState.PUBLISHED)
563 }
564
565 await stopFfmpeg(command)
566 await waitJobs(servers)
567
568 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
569 expect(stateChanges).to.have.lengthOf(2)
570 expect(stateChanges[1]).to.equal(VideoState.LIVE_ENDED)
571 }
572 })
573
574 it('Should not receive a notification after unsubscribe', async function () {
575 this.timeout(60000)
576
577 const stateChanges: VideoState[] = []
578
579 const liveVideoUUID = await createLiveWrapper()
580 await waitJobs(servers)
581
582 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
583
584 const socket = getLiveNotificationSocket(servers[0].url)
585 socket.on('state-change', data => stateChanges.push(data.state))
586 socket.emit('subscribe', { videoId })
587
588 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
589 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoUUID)
590 await waitJobs(servers)
591
592 expect(stateChanges).to.have.lengthOf(1)
593 socket.emit('unsubscribe', { videoId })
594
595 await stopFfmpeg(command)
596 await waitJobs(servers)
597
598 expect(stateChanges).to.have.lengthOf(1)
599 })
600 })
601
602 describe('After a server restart', function () {
603 let liveVideoId: string
604 let liveVideoReplayId: string
605
606 async function createLiveWrapper (saveReplay: boolean) {
607 const liveAttributes = {
608 name: 'live video',
609 channelId: servers[0].videoChannel.id,
610 privacy: VideoPrivacy.PUBLIC,
611 saveReplay
612 }
613
614 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
615 return res.body.video.uuid
616 }
617
618 before(async function () {
619 this.timeout(60000)
620
621 liveVideoId = await createLiveWrapper(false)
622 liveVideoReplayId = await createLiveWrapper(true)
623
624 await Promise.all([
625 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId),
626 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoReplayId)
627 ])
628
629 await Promise.all([
630 waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId),
631 waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoReplayId)
632 ])
633
634 await killallServers([ servers[0] ])
635 await reRunServer(servers[0])
636
637 await wait(5000)
638 })
639
640 it('Should cleanup lives', async function () {
641 this.timeout(60000)
642
643 const res = await getVideo(servers[0].url, liveVideoId)
644 const video: VideoDetails = res.body
645
646 expect(video.state.id).to.equal(VideoState.LIVE_ENDED)
647 })
648
649 it('Should save a live replay', async function () {
650 this.timeout(60000)
651
652 await waitJobs(servers)
653
654 const res = await getVideo(servers[0].url, liveVideoReplayId)
655 const video: VideoDetails = res.body
656
657 expect(video.state.id).to.equal(VideoState.PUBLISHED)
658 })
659 })
660
661 after(async function () {
662 await cleanupTests(servers)
663 })
664 })