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'
15 checkResolutionsInMasterPlaylist,
19 flushAndRunMultipleServers,
30 sendRTMPStreamInVideo,
32 setAccessTokensToServers,
33 setDefaultVideoChannel,
35 testFfmpegStreamError,
37 updateCustomSubConfig,
42 waitUntilLivePublished,
45 } from '../../../../shared/extra-utils'
47 const expect = chai.expect
49 describe('Test live', function () {
50 let servers: ServerInfo[] = []
52 before(async function () {
55 servers = await flushAndRunMultipleServers(2)
57 // Get the access tokens
58 await setAccessTokensToServers(servers)
59 await setDefaultVideoChannel(servers)
61 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
71 // Server 1 and server 2 follow each other
72 await doubleFollow(servers[0], servers[1])
75 describe('Live creation, update and delete', function () {
76 let liveVideoUUID: string
78 it('Should create a live with the appropriate parameters', async function () {
81 const attributes: LiveVideoCreate = {
85 description: 'super live description',
86 support: 'support field',
87 channelId: servers[0].videoChannel.id,
89 waitTranscoding: false,
90 name: 'my super live',
91 tags: [ 'tag1', 'tag2' ],
92 commentsEnabled: false,
93 downloadEnabled: false,
95 privacy: VideoPrivacy.PUBLIC,
96 previewfile: 'video_short1-preview.webm.jpg',
97 thumbnailfile: 'video_short1.webm.jpg'
100 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
101 liveVideoUUID = res.body.video.uuid
103 await waitJobs(servers)
105 for (const server of servers) {
106 const resVideo = await getVideo(server.url, liveVideoUUID)
107 const video: VideoDetails = resVideo.body
109 expect(video.category.id).to.equal(1)
110 expect(video.licence.id).to.equal(2)
111 expect(video.language.id).to.equal('fr')
112 expect(video.description).to.equal('super live description')
113 expect(video.support).to.equal('support field')
115 expect(video.channel.name).to.equal(servers[0].videoChannel.name)
116 expect(video.channel.host).to.equal(servers[0].videoChannel.host)
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)
126 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
127 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
129 const resLive = await getLive(server.url, server.accessToken, liveVideoUUID)
130 const live: LiveVideo = resLive.body
132 if (server.url === servers[0].url) {
133 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
134 expect(live.streamKey).to.not.be.empty
136 expect(live.rtmpUrl).to.be.null
137 expect(live.streamKey).to.be.null
140 expect(live.saveReplay).to.be.true
144 it('Should have a default preview and thumbnail', async function () {
147 const attributes: LiveVideoCreate = {
148 name: 'default live thumbnail',
149 channelId: servers[0].videoChannel.id,
150 privacy: VideoPrivacy.UNLISTED,
154 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
155 const videoId = res.body.video.uuid
157 await waitJobs(servers)
159 for (const server of servers) {
160 const resVideo = await getVideo(server.url, videoId)
161 const video: VideoDetails = resVideo.body
163 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
164 expect(video.nsfw).to.be.true
166 await makeRawRequest(server.url + video.thumbnailPath, 200)
167 await makeRawRequest(server.url + video.previewPath, 200)
171 it('Should not have the live listed since nobody streams into', async function () {
172 for (const server of servers) {
173 const res = await getVideosList(server.url)
175 expect(res.body.total).to.equal(0)
176 expect(res.body.data).to.have.lengthOf(0)
180 it('Should not be able to update a live of another server', async function () {
181 await updateLive(servers[1].url, servers[1].accessToken, liveVideoUUID, { saveReplay: false }, 403)
184 it('Should update the live', async function () {
187 await updateLive(servers[0].url, servers[0].accessToken, liveVideoUUID, { saveReplay: false })
188 await waitJobs(servers)
191 it('Have the live updated', async function () {
192 for (const server of servers) {
193 const res = await getLive(server.url, server.accessToken, liveVideoUUID)
194 const live: LiveVideo = res.body
196 if (server.url === servers[0].url) {
197 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
198 expect(live.streamKey).to.not.be.empty
200 expect(live.rtmpUrl).to.be.null
201 expect(live.streamKey).to.be.null
204 expect(live.saveReplay).to.be.false
208 it('Delete the live', async function () {
211 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
212 await waitJobs(servers)
215 it('Should have the live deleted', async function () {
216 for (const server of servers) {
217 await getVideo(server.url, liveVideoUUID, 404)
218 await getLive(server.url, server.accessToken, liveVideoUUID, 404)
223 describe('Stream checks', function () {
224 let liveVideo: LiveVideo & VideoDetails
228 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
231 async function createLiveWrapper () {
232 const liveAttributes = {
234 channelId: servers[0].videoChannel.id,
235 privacy: VideoPrivacy.PUBLIC,
239 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
240 const uuid = res.body.video.uuid
242 const resLive = await getLive(servers[0].url, servers[0].accessToken, uuid)
243 const resVideo = await getVideo(servers[0].url, uuid)
245 return Object.assign(resVideo.body, resLive.body) as LiveVideo & VideoDetails
248 it('Should not allow a stream without the appropriate path', async function () {
251 liveVideo = await createLiveWrapper()
253 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
254 await testFfmpegStreamError(command, true)
257 it('Should not allow a stream without the appropriate stream key', async function () {
260 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
261 await testFfmpegStreamError(command, true)
264 it('Should succeed with the correct params', async function () {
267 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
268 await testFfmpegStreamError(command, false)
271 it('Should not allow a stream on a live that was blacklisted', async function () {
274 liveVideo = await createLiveWrapper()
276 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideo.uuid)
278 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
279 await testFfmpegStreamError(command, true)
282 it('Should not allow a stream on a live that was deleted', async function () {
285 liveVideo = await createLiveWrapper()
287 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid)
289 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
290 await testFfmpegStreamError(command, true)
294 describe('Live transcoding', function () {
295 let liveVideoId: string
297 async function createLiveWrapper (saveReplay: boolean) {
298 const liveAttributes = {
300 channelId: servers[0].videoChannel.id,
301 privacy: VideoPrivacy.PUBLIC,
305 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
306 return res.body.video.uuid
309 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
310 for (const server of servers) {
311 const resList = await getVideosList(server.url)
312 const videos: Video[] = resList.body.data
314 expect(videos.find(v => v.uuid === liveVideoId)).to.exist
316 const resVideo = await getVideo(server.url, liveVideoId)
317 const video: VideoDetails = resVideo.body
319 expect(video.streamingPlaylists).to.have.lengthOf(1)
321 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
322 expect(hlsPlaylist).to.exist
324 // Only finite files are displayed
325 expect(hlsPlaylist.files).to.have.lengthOf(0)
327 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions)
329 for (let i = 0; i < resolutions.length; i++) {
330 const segmentName = `${i}-000001.ts`
331 await waitUntilLog(servers[0], `${video.uuid}/${segmentName}`, 2, false)
333 const res = await getPlaylist(`${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`)
334 const subPlaylist = res.text
336 expect(subPlaylist).to.contain(segmentName)
338 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
339 await checkLiveSegmentHash(baseUrlAndPath, video.uuid, segmentName, hlsPlaylist)
344 function updateConf (resolutions: number[]) {
345 return updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
353 '240p': resolutions.includes(240),
354 '360p': resolutions.includes(360),
355 '480p': resolutions.includes(480),
356 '720p': resolutions.includes(720),
357 '1080p': resolutions.includes(1080),
358 '2160p': resolutions.includes(2160)
365 before(async function () {
369 it('Should enable transcoding without additional resolutions', async function () {
372 liveVideoId = await createLiveWrapper(false)
374 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
375 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
376 await waitJobs(servers)
378 await testVideoResolutions(liveVideoId, [ 720 ])
380 await stopFfmpeg(command)
383 it('Should enable transcoding with some resolutions', async function () {
386 const resolutions = [ 240, 480 ]
387 await updateConf(resolutions)
388 liveVideoId = await createLiveWrapper(false)
390 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
391 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
392 await waitJobs(servers)
394 await testVideoResolutions(liveVideoId, resolutions)
396 await stopFfmpeg(command)
399 it('Should enable transcoding with some resolutions and correctly save them', async function () {
402 const resolutions = [ 240, 360, 720 ]
404 await updateConf(resolutions)
405 liveVideoId = await createLiveWrapper(true)
407 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId, 'video_short2.webm')
408 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
409 await waitJobs(servers)
411 await testVideoResolutions(liveVideoId, resolutions)
413 await stopFfmpeg(command)
414 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoId)
416 await waitJobs(servers)
418 const bitrateLimits = {
424 for (const server of servers) {
425 const resVideo = await getVideo(server.url, liveVideoId)
426 const video: VideoDetails = resVideo.body
428 expect(video.duration).to.be.greaterThan(1)
429 expect(video.files).to.have.lengthOf(0)
431 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
433 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
435 for (const resolution of resolutions) {
436 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
438 expect(file).to.exist
439 expect(file.fps).to.be.approximately(30, 5)
440 expect(file.size).to.be.greaterThan(1)
442 const filename = `${video.uuid}-${resolution}-fragmented.mp4`
443 const segmentPath = buildServerDirectory(servers[0], join('streaming-playlists', 'hls', video.uuid, filename))
445 const probe = await ffprobePromise(segmentPath)
446 const videoStream = await getVideoStreamFromFile(segmentPath, probe)
448 expect(probe.format.bit_rate).to.be.below(bitrateLimits[videoStream.height])
450 await makeRawRequest(file.torrentUrl, 200)
451 await makeRawRequest(file.fileUrl, 200)
456 it('Should correctly have cleaned up the live files', async function () {
459 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
463 describe('Live views', function () {
464 let liveVideoId: string
465 let command: FfmpegCommand
467 async function countViews (expected: number) {
468 for (const server of servers) {
469 const res = await getVideo(server.url, liveVideoId)
470 const video: VideoDetails = res.body
472 expect(video.views).to.equal(expected)
476 before(async function () {
479 const liveAttributes = {
481 channelId: servers[0].videoChannel.id,
482 privacy: VideoPrivacy.PUBLIC
485 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
486 liveVideoId = res.body.video.uuid
488 command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
489 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
490 await waitJobs(servers)
493 it('Should display no views for a live', async function () {
497 it('Should view a live twice and display 1 view', async function () {
500 await viewVideo(servers[0].url, liveVideoId)
501 await viewVideo(servers[0].url, liveVideoId)
505 await waitJobs(servers)
510 it('Should wait and display 0 views', async function () {
514 await waitJobs(servers)
519 it('Should view a live on a remote and on local and display 2 views', async function () {
522 await viewVideo(servers[0].url, liveVideoId)
523 await viewVideo(servers[1].url, liveVideoId)
524 await viewVideo(servers[1].url, liveVideoId)
527 await waitJobs(servers)
532 after(async function () {
533 await stopFfmpeg(command)
537 describe('Live socket messages', function () {
539 async function createLiveWrapper () {
540 const liveAttributes = {
542 channelId: servers[0].videoChannel.id,
543 privacy: VideoPrivacy.PUBLIC
546 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
547 return res.body.video.uuid
550 it('Should correctly send a message when the live starts and ends', async function () {
553 const localStateChanges: VideoState[] = []
554 const remoteStateChanges: VideoState[] = []
556 const liveVideoUUID = await createLiveWrapper()
557 await waitJobs(servers)
560 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
562 const localSocket = getLiveNotificationSocket(servers[0].url)
563 localSocket.on('state-change', data => localStateChanges.push(data.state))
564 localSocket.emit('subscribe', { videoId })
568 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
570 const remoteSocket = getLiveNotificationSocket(servers[1].url)
571 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
572 remoteSocket.emit('subscribe', { videoId })
575 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
576 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoUUID)
577 await waitJobs(servers)
579 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
580 expect(stateChanges).to.have.lengthOf(1)
581 expect(stateChanges[0]).to.equal(VideoState.PUBLISHED)
584 await stopFfmpeg(command)
585 await waitJobs(servers)
587 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
588 expect(stateChanges).to.have.lengthOf(2)
589 expect(stateChanges[1]).to.equal(VideoState.LIVE_ENDED)
593 it('Should not receive a notification after unsubscribe', async function () {
596 const stateChanges: VideoState[] = []
598 const liveVideoUUID = await createLiveWrapper()
599 await waitJobs(servers)
601 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
603 const socket = getLiveNotificationSocket(servers[0].url)
604 socket.on('state-change', data => stateChanges.push(data.state))
605 socket.emit('subscribe', { videoId })
607 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
608 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoUUID)
609 await waitJobs(servers)
611 expect(stateChanges).to.have.lengthOf(1)
612 socket.emit('unsubscribe', { videoId })
614 await stopFfmpeg(command)
615 await waitJobs(servers)
617 expect(stateChanges).to.have.lengthOf(1)
621 describe('After a server restart', function () {
622 let liveVideoId: string
623 let liveVideoReplayId: string
625 async function createLiveWrapper (saveReplay: boolean) {
626 const liveAttributes = {
628 channelId: servers[0].videoChannel.id,
629 privacy: VideoPrivacy.PUBLIC,
633 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
634 return res.body.video.uuid
637 before(async function () {
640 liveVideoId = await createLiveWrapper(false)
641 liveVideoReplayId = await createLiveWrapper(true)
644 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId),
645 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoReplayId)
649 waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId),
650 waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoReplayId)
653 await killallServers([ servers[0] ])
654 await reRunServer(servers[0])
659 it('Should cleanup lives', async function () {
662 const res = await getVideo(servers[0].url, liveVideoId)
663 const video: VideoDetails = res.body
665 expect(video.state.id).to.equal(VideoState.LIVE_ENDED)
668 it('Should save a live replay', async function () {
671 await waitJobs(servers)
673 const res = await getVideo(servers[0].url, liveVideoReplayId)
674 const video: VideoDetails = res.body
676 expect(video.state.id).to.equal(VideoState.PUBLISHED)
680 after(async function () {
681 await cleanupTests(servers)