1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { FfmpegCommand } from 'fluent-ffmpeg'
6 import { join } from 'path'
7 import { ffprobePromise, getVideoStreamFromFile } from '@server/helpers/ffprobe-utils'
8 import { getLiveNotificationSocket } from '@shared/extra-utils/socket/socket-io'
9 import { LiveVideo, LiveVideoCreate, Video, VideoDetails, VideoPrivacy, VideoState, VideoStreamingPlaylistType } from '@shared/models'
10 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
16 checkResolutionsInMasterPlaylist,
20 flushAndRunMultipleServers,
31 sendRTMPStreamInVideo,
33 setAccessTokensToServers,
34 setDefaultVideoChannel,
36 testFfmpegStreamError,
38 updateCustomSubConfig,
44 waitUntilLivePublished,
45 waitUntilLiveSegmentGeneration
46 } from '../../../../shared/extra-utils'
48 const expect = chai.expect
50 describe('Test live', function () {
51 let servers: ServerInfo[] = []
53 before(async function () {
56 servers = await flushAndRunMultipleServers(2)
58 // Get the access tokens
59 await setAccessTokensToServers(servers)
60 await setDefaultVideoChannel(servers)
62 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
72 // Server 1 and server 2 follow each other
73 await doubleFollow(servers[0], servers[1])
76 describe('Live creation, update and delete', function () {
77 let liveVideoUUID: string
79 it('Should create a live with the appropriate parameters', async function () {
82 const attributes: LiveVideoCreate = {
86 description: 'super live description',
87 support: 'support field',
88 channelId: servers[0].videoChannel.id,
90 waitTranscoding: false,
91 name: 'my super live',
92 tags: [ 'tag1', 'tag2' ],
93 commentsEnabled: false,
94 downloadEnabled: false,
96 privacy: VideoPrivacy.PUBLIC,
97 previewfile: 'video_short1-preview.webm.jpg',
98 thumbnailfile: 'video_short1.webm.jpg'
101 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
102 liveVideoUUID = res.body.video.uuid
104 await waitJobs(servers)
106 for (const server of servers) {
107 const resVideo = await getVideo(server.url, liveVideoUUID)
108 const video: VideoDetails = resVideo.body
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')
116 expect(video.channel.name).to.equal(servers[0].videoChannel.name)
117 expect(video.channel.host).to.equal(servers[0].videoChannel.host)
119 expect(video.isLive).to.be.true
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)
129 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
130 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
132 const resLive = await getLive(server.url, server.accessToken, liveVideoUUID)
133 const live: LiveVideo = resLive.body
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
139 expect(live.rtmpUrl).to.be.null
140 expect(live.streamKey).to.be.null
143 expect(live.saveReplay).to.be.true
147 it('Should have a default preview and thumbnail', async function () {
150 const attributes: LiveVideoCreate = {
151 name: 'default live thumbnail',
152 channelId: servers[0].videoChannel.id,
153 privacy: VideoPrivacy.UNLISTED,
157 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
158 const videoId = res.body.video.uuid
160 await waitJobs(servers)
162 for (const server of servers) {
163 const resVideo = await getVideo(server.url, videoId)
164 const video: VideoDetails = resVideo.body
166 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
167 expect(video.nsfw).to.be.true
169 await makeRawRequest(server.url + video.thumbnailPath, HttpStatusCode.OK_200)
170 await makeRawRequest(server.url + video.previewPath, HttpStatusCode.OK_200)
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)
178 expect(res.body.total).to.equal(0)
179 expect(res.body.data).to.have.lengthOf(0)
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)
187 it('Should update the live', async function () {
190 await updateLive(servers[0].url, servers[0].accessToken, liveVideoUUID, { saveReplay: false })
191 await waitJobs(servers)
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
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
203 expect(live.rtmpUrl).to.be.null
204 expect(live.streamKey).to.be.null
207 expect(live.saveReplay).to.be.false
211 it('Delete the live', async function () {
214 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
215 await waitJobs(servers)
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)
226 describe('Stream checks', function () {
227 let liveVideo: LiveVideo & VideoDetails
231 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
234 async function createLiveWrapper () {
235 const liveAttributes = {
237 channelId: servers[0].videoChannel.id,
238 privacy: VideoPrivacy.PUBLIC,
242 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
243 const uuid = res.body.video.uuid
245 const resLive = await getLive(servers[0].url, servers[0].accessToken, uuid)
246 const resVideo = await getVideo(servers[0].url, uuid)
248 return Object.assign(resVideo.body, resLive.body) as LiveVideo & VideoDetails
251 it('Should not allow a stream without the appropriate path', async function () {
254 liveVideo = await createLiveWrapper()
256 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
257 await testFfmpegStreamError(command, true)
260 it('Should not allow a stream without the appropriate stream key', async function () {
263 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
264 await testFfmpegStreamError(command, true)
267 it('Should succeed with the correct params', async function () {
270 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
271 await testFfmpegStreamError(command, false)
274 it('Should list this live now someone stream into it', async function () {
275 for (const server of servers) {
276 const res = await getVideosList(server.url)
278 expect(res.body.total).to.equal(1)
279 expect(res.body.data).to.have.lengthOf(1)
281 const video: Video = res.body.data[0]
283 expect(video.name).to.equal('user live')
284 expect(video.isLive).to.be.true
288 it('Should not allow a stream on a live that was blacklisted', async function () {
291 liveVideo = await createLiveWrapper()
293 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideo.uuid)
295 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
296 await testFfmpegStreamError(command, true)
299 it('Should not allow a stream on a live that was deleted', async function () {
302 liveVideo = await createLiveWrapper()
304 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid)
306 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
307 await testFfmpegStreamError(command, true)
311 describe('Live transcoding', function () {
312 let liveVideoId: string
314 async function createLiveWrapper (saveReplay: boolean) {
315 const liveAttributes = {
317 channelId: servers[0].videoChannel.id,
318 privacy: VideoPrivacy.PUBLIC,
322 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
323 return res.body.video.uuid
326 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
327 for (const server of servers) {
328 const resList = await getVideosList(server.url)
329 const videos: Video[] = resList.body.data
331 expect(videos.find(v => v.uuid === liveVideoId)).to.exist
333 const resVideo = await getVideo(server.url, liveVideoId)
334 const video: VideoDetails = resVideo.body
336 expect(video.streamingPlaylists).to.have.lengthOf(1)
338 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
339 expect(hlsPlaylist).to.exist
341 // Only finite files are displayed
342 expect(hlsPlaylist.files).to.have.lengthOf(0)
344 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions)
346 for (let i = 0; i < resolutions.length; i++) {
348 const segmentName = `${i}-00000${segmentNum}.ts`
349 await waitUntilLiveSegmentGeneration(servers[0], video.uuid, i, segmentNum)
351 const res = await getPlaylist(`${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`)
352 const subPlaylist = res.text
354 expect(subPlaylist).to.contain(segmentName)
356 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
357 await checkLiveSegmentHash(baseUrlAndPath, video.uuid, segmentName, hlsPlaylist)
362 function updateConf (resolutions: number[]) {
363 return updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
371 '240p': resolutions.includes(240),
372 '360p': resolutions.includes(360),
373 '480p': resolutions.includes(480),
374 '720p': resolutions.includes(720),
375 '1080p': resolutions.includes(1080),
376 '2160p': resolutions.includes(2160)
383 before(async function () {
387 it('Should enable transcoding without additional resolutions', async function () {
390 liveVideoId = await createLiveWrapper(false)
392 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
393 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoId)
394 await waitJobs(servers)
396 await testVideoResolutions(liveVideoId, [ 720 ])
398 await stopFfmpeg(command)
401 it('Should enable transcoding with some resolutions', async function () {
404 const resolutions = [ 240, 480 ]
405 await updateConf(resolutions)
406 liveVideoId = await createLiveWrapper(false)
408 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
409 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoId)
410 await waitJobs(servers)
412 await testVideoResolutions(liveVideoId, resolutions)
414 await stopFfmpeg(command)
417 it('Should enable transcoding with some resolutions and correctly save them', async function () {
420 const resolutions = [ 240, 360, 720 ]
422 await updateConf(resolutions)
423 liveVideoId = await createLiveWrapper(true)
425 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId, 'video_short2.webm')
426 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoId)
427 await waitJobs(servers)
429 await testVideoResolutions(liveVideoId, resolutions)
431 await stopFfmpeg(command)
432 await waitUntilLiveEnded(servers[0].url, servers[0].accessToken, liveVideoId)
434 await waitJobs(servers)
436 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoId)
438 const bitrateLimits = {
439 720: 5000 * 1000, // 60FPS
444 for (const server of servers) {
445 const resVideo = await getVideo(server.url, liveVideoId)
446 const video: VideoDetails = resVideo.body
448 expect(video.state.id).to.equal(VideoState.PUBLISHED)
449 expect(video.duration).to.be.greaterThan(1)
450 expect(video.files).to.have.lengthOf(0)
452 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
453 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
454 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
456 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
458 for (const resolution of resolutions) {
459 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
461 expect(file).to.exist
462 expect(file.size).to.be.greaterThan(1)
464 if (resolution >= 720) {
465 expect(file.fps).to.be.approximately(60, 2)
467 expect(file.fps).to.be.approximately(30, 2)
470 const filename = `${video.uuid}-${resolution}-fragmented.mp4`
471 const segmentPath = buildServerDirectory(servers[0], join('streaming-playlists', 'hls', video.uuid, filename))
473 const probe = await ffprobePromise(segmentPath)
474 const videoStream = await getVideoStreamFromFile(segmentPath, probe)
476 expect(probe.format.bit_rate).to.be.below(bitrateLimits[videoStream.height])
478 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
479 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
484 it('Should correctly have cleaned up the live files', async function () {
487 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
491 describe('Live views', function () {
492 let liveVideoId: string
493 let command: FfmpegCommand
495 async function countViews (expected: number) {
496 for (const server of servers) {
497 const res = await getVideo(server.url, liveVideoId)
498 const video: VideoDetails = res.body
500 expect(video.views).to.equal(expected)
504 before(async function () {
507 const liveAttributes = {
509 channelId: servers[0].videoChannel.id,
510 privacy: VideoPrivacy.PUBLIC
513 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
514 liveVideoId = res.body.video.uuid
516 command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
517 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoId)
518 await waitJobs(servers)
521 it('Should display no views for a live', async function () {
525 it('Should view a live twice and display 1 view', async function () {
528 await viewVideo(servers[0].url, liveVideoId)
529 await viewVideo(servers[0].url, liveVideoId)
533 await waitJobs(servers)
538 it('Should wait and display 0 views', async function () {
542 await waitJobs(servers)
547 it('Should view a live on a remote and on local and display 2 views', async function () {
550 await viewVideo(servers[0].url, liveVideoId)
551 await viewVideo(servers[1].url, liveVideoId)
552 await viewVideo(servers[1].url, liveVideoId)
555 await waitJobs(servers)
560 after(async function () {
561 await stopFfmpeg(command)
565 describe('Live socket messages', function () {
567 async function createLiveWrapper () {
568 const liveAttributes = {
570 channelId: servers[0].videoChannel.id,
571 privacy: VideoPrivacy.PUBLIC
574 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
575 return res.body.video.uuid
578 it('Should correctly send a message when the live starts and ends', async function () {
581 const localStateChanges: VideoState[] = []
582 const remoteStateChanges: VideoState[] = []
584 const liveVideoUUID = await createLiveWrapper()
585 await waitJobs(servers)
588 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
590 const localSocket = getLiveNotificationSocket(servers[0].url)
591 localSocket.on('state-change', data => localStateChanges.push(data.state))
592 localSocket.emit('subscribe', { videoId })
596 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
598 const remoteSocket = getLiveNotificationSocket(servers[1].url)
599 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
600 remoteSocket.emit('subscribe', { videoId })
603 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
605 for (const server of servers) {
606 await waitUntilLivePublished(server.url, server.accessToken, liveVideoUUID)
609 await waitJobs(servers)
611 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
612 expect(stateChanges).to.have.length.at.least(1)
613 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.PUBLISHED)
616 await stopFfmpeg(command)
618 for (const server of servers) {
619 await waitUntilLiveEnded(server.url, server.accessToken, liveVideoUUID)
622 await waitJobs(servers)
624 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
625 expect(stateChanges).to.have.length.at.least(2)
626 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.LIVE_ENDED)
630 it('Should correctly send views change notification', async function () {
633 let localLastVideoViews = 0
634 let remoteLastVideoViews = 0
636 const liveVideoUUID = await createLiveWrapper()
637 await waitJobs(servers)
640 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
642 const localSocket = getLiveNotificationSocket(servers[0].url)
643 localSocket.on('views-change', data => { localLastVideoViews = data.views })
644 localSocket.emit('subscribe', { videoId })
648 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
650 const remoteSocket = getLiveNotificationSocket(servers[1].url)
651 remoteSocket.on('views-change', data => { remoteLastVideoViews = data.views })
652 remoteSocket.emit('subscribe', { videoId })
655 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
657 for (const server of servers) {
658 await waitUntilLivePublished(server.url, server.accessToken, liveVideoUUID)
661 await waitJobs(servers)
663 expect(localLastVideoViews).to.equal(0)
664 expect(remoteLastVideoViews).to.equal(0)
666 await viewVideo(servers[0].url, liveVideoUUID)
667 await viewVideo(servers[1].url, liveVideoUUID)
669 await waitJobs(servers)
671 await waitJobs(servers)
673 expect(localLastVideoViews).to.equal(2)
674 expect(remoteLastVideoViews).to.equal(2)
676 await stopFfmpeg(command)
679 it('Should not receive a notification after unsubscribe', async function () {
682 const stateChanges: VideoState[] = []
684 const liveVideoUUID = await createLiveWrapper()
685 await waitJobs(servers)
687 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
689 const socket = getLiveNotificationSocket(servers[0].url)
690 socket.on('state-change', data => stateChanges.push(data.state))
691 socket.emit('subscribe', { videoId })
693 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
694 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoUUID)
695 await waitJobs(servers)
697 expect(stateChanges).to.have.lengthOf(1)
698 socket.emit('unsubscribe', { videoId })
700 await stopFfmpeg(command)
701 await waitJobs(servers)
703 expect(stateChanges).to.have.lengthOf(1)
707 describe('After a server restart', function () {
708 let liveVideoId: string
709 let liveVideoReplayId: string
711 async function createLiveWrapper (saveReplay: boolean) {
712 const liveAttributes = {
714 channelId: servers[0].videoChannel.id,
715 privacy: VideoPrivacy.PUBLIC,
719 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
720 return res.body.video.uuid
723 before(async function () {
726 liveVideoId = await createLiveWrapper(false)
727 liveVideoReplayId = await createLiveWrapper(true)
730 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId),
731 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoReplayId)
735 waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoId),
736 waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoReplayId)
739 await waitUntilLiveSegmentGeneration(servers[0], liveVideoId, 0, 2)
740 await waitUntilLiveSegmentGeneration(servers[0], liveVideoReplayId, 0, 2)
742 await killallServers([ servers[0] ])
743 await reRunServer(servers[0])
748 it('Should cleanup lives', async function () {
751 await waitUntilLiveEnded(servers[0].url, servers[0].accessToken, liveVideoId)
754 it('Should save a live replay', async function () {
757 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoReplayId)
761 after(async function () {
762 await cleanupTests(servers)