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