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 async function waitUntilLivePublishedOnAllServers (videoId: string) {
54 for (const server of servers) {
55 await waitUntilLivePublished(server.url, server.accessToken, videoId)
59 before(async function () {
62 servers = await flushAndRunMultipleServers(2)
64 // Get the access tokens
65 await setAccessTokensToServers(servers)
66 await setDefaultVideoChannel(servers)
68 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
78 // Server 1 and server 2 follow each other
79 await doubleFollow(servers[0], servers[1])
82 describe('Live creation, update and delete', function () {
83 let liveVideoUUID: string
85 it('Should create a live with the appropriate parameters', async function () {
88 const attributes: LiveVideoCreate = {
92 description: 'super live description',
93 support: 'support field',
94 channelId: servers[0].videoChannel.id,
96 waitTranscoding: false,
97 name: 'my super live',
98 tags: [ 'tag1', 'tag2' ],
99 commentsEnabled: false,
100 downloadEnabled: false,
102 privacy: VideoPrivacy.PUBLIC,
103 previewfile: 'video_short1-preview.webm.jpg',
104 thumbnailfile: 'video_short1.webm.jpg'
107 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
108 liveVideoUUID = res.body.video.uuid
110 await waitJobs(servers)
112 for (const server of servers) {
113 const resVideo = await getVideo(server.url, liveVideoUUID)
114 const video: VideoDetails = resVideo.body
116 expect(video.category.id).to.equal(1)
117 expect(video.licence.id).to.equal(2)
118 expect(video.language.id).to.equal('fr')
119 expect(video.description).to.equal('super live description')
120 expect(video.support).to.equal('support field')
122 expect(video.channel.name).to.equal(servers[0].videoChannel.name)
123 expect(video.channel.host).to.equal(servers[0].videoChannel.host)
125 expect(video.isLive).to.be.true
127 expect(video.nsfw).to.be.false
128 expect(video.waitTranscoding).to.be.false
129 expect(video.name).to.equal('my super live')
130 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
131 expect(video.commentsEnabled).to.be.false
132 expect(video.downloadEnabled).to.be.false
133 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
135 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
136 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
138 const resLive = await getLive(server.url, server.accessToken, liveVideoUUID)
139 const live: LiveVideo = resLive.body
141 if (server.url === servers[0].url) {
142 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
143 expect(live.streamKey).to.not.be.empty
145 expect(live.rtmpUrl).to.be.null
146 expect(live.streamKey).to.be.null
149 expect(live.saveReplay).to.be.true
153 it('Should have a default preview and thumbnail', async function () {
156 const attributes: LiveVideoCreate = {
157 name: 'default live thumbnail',
158 channelId: servers[0].videoChannel.id,
159 privacy: VideoPrivacy.UNLISTED,
163 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
164 const videoId = res.body.video.uuid
166 await waitJobs(servers)
168 for (const server of servers) {
169 const resVideo = await getVideo(server.url, videoId)
170 const video: VideoDetails = resVideo.body
172 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
173 expect(video.nsfw).to.be.true
175 await makeRawRequest(server.url + video.thumbnailPath, HttpStatusCode.OK_200)
176 await makeRawRequest(server.url + video.previewPath, HttpStatusCode.OK_200)
180 it('Should not have the live listed since nobody streams into', async function () {
181 for (const server of servers) {
182 const res = await getVideosList(server.url)
184 expect(res.body.total).to.equal(0)
185 expect(res.body.data).to.have.lengthOf(0)
189 it('Should not be able to update a live of another server', async function () {
190 await updateLive(servers[1].url, servers[1].accessToken, liveVideoUUID, { saveReplay: false }, HttpStatusCode.FORBIDDEN_403)
193 it('Should update the live', async function () {
196 await updateLive(servers[0].url, servers[0].accessToken, liveVideoUUID, { saveReplay: false })
197 await waitJobs(servers)
200 it('Have the live updated', async function () {
201 for (const server of servers) {
202 const res = await getLive(server.url, server.accessToken, liveVideoUUID)
203 const live: LiveVideo = res.body
205 if (server.url === servers[0].url) {
206 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
207 expect(live.streamKey).to.not.be.empty
209 expect(live.rtmpUrl).to.be.null
210 expect(live.streamKey).to.be.null
213 expect(live.saveReplay).to.be.false
217 it('Delete the live', async function () {
220 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
221 await waitJobs(servers)
224 it('Should have the live deleted', async function () {
225 for (const server of servers) {
226 await getVideo(server.url, liveVideoUUID, HttpStatusCode.NOT_FOUND_404)
227 await getLive(server.url, server.accessToken, liveVideoUUID, HttpStatusCode.NOT_FOUND_404)
232 describe('Stream checks', function () {
233 let liveVideo: LiveVideo & VideoDetails
237 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
240 async function createLiveWrapper () {
241 const liveAttributes = {
243 channelId: servers[0].videoChannel.id,
244 privacy: VideoPrivacy.PUBLIC,
248 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
249 const uuid = res.body.video.uuid
251 const resLive = await getLive(servers[0].url, servers[0].accessToken, uuid)
252 const resVideo = await getVideo(servers[0].url, uuid)
254 return Object.assign(resVideo.body, resLive.body) as LiveVideo & VideoDetails
257 it('Should not allow a stream without the appropriate path', async function () {
260 liveVideo = await createLiveWrapper()
262 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
263 await testFfmpegStreamError(command, true)
266 it('Should not allow a stream without the appropriate stream key', async function () {
269 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
270 await testFfmpegStreamError(command, true)
273 it('Should succeed with the correct params', async function () {
276 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
277 await testFfmpegStreamError(command, false)
280 it('Should list this live now someone stream into it', async function () {
281 for (const server of servers) {
282 const res = await getVideosList(server.url)
284 expect(res.body.total).to.equal(1)
285 expect(res.body.data).to.have.lengthOf(1)
287 const video: Video = res.body.data[0]
289 expect(video.name).to.equal('user live')
290 expect(video.isLive).to.be.true
294 it('Should not allow a stream on a live that was blacklisted', async function () {
297 liveVideo = await createLiveWrapper()
299 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideo.uuid)
301 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
302 await testFfmpegStreamError(command, true)
305 it('Should not allow a stream on a live that was deleted', async function () {
308 liveVideo = await createLiveWrapper()
310 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid)
312 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
313 await testFfmpegStreamError(command, true)
317 describe('Live transcoding', function () {
318 let liveVideoId: string
320 async function createLiveWrapper (saveReplay: boolean) {
321 const liveAttributes = {
323 channelId: servers[0].videoChannel.id,
324 privacy: VideoPrivacy.PUBLIC,
328 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
329 return res.body.video.uuid
332 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
333 for (const server of servers) {
334 const resList = await getVideosList(server.url)
335 const videos: Video[] = resList.body.data
337 expect(videos.find(v => v.uuid === liveVideoId)).to.exist
339 const resVideo = await getVideo(server.url, liveVideoId)
340 const video: VideoDetails = resVideo.body
342 expect(video.streamingPlaylists).to.have.lengthOf(1)
344 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
345 expect(hlsPlaylist).to.exist
347 // Only finite files are displayed
348 expect(hlsPlaylist.files).to.have.lengthOf(0)
350 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions)
352 for (let i = 0; i < resolutions.length; i++) {
354 const segmentName = `${i}-00000${segmentNum}.ts`
355 await waitUntilLiveSegmentGeneration(servers[0], video.uuid, i, segmentNum)
357 const res = await getPlaylist(`${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`)
358 const subPlaylist = res.text
360 expect(subPlaylist).to.contain(segmentName)
362 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
363 await checkLiveSegmentHash(baseUrlAndPath, video.uuid, segmentName, hlsPlaylist)
368 function updateConf (resolutions: number[]) {
369 return updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
377 '240p': resolutions.includes(240),
378 '360p': resolutions.includes(360),
379 '480p': resolutions.includes(480),
380 '720p': resolutions.includes(720),
381 '1080p': resolutions.includes(1080),
382 '2160p': resolutions.includes(2160)
389 before(async function () {
393 it('Should enable transcoding without additional resolutions', async function () {
396 liveVideoId = await createLiveWrapper(false)
398 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
399 await waitUntilLivePublishedOnAllServers(liveVideoId)
400 await waitJobs(servers)
402 await testVideoResolutions(liveVideoId, [ 720 ])
404 await stopFfmpeg(command)
407 it('Should enable transcoding with some resolutions', async function () {
410 const resolutions = [ 240, 480 ]
411 await updateConf(resolutions)
412 liveVideoId = await createLiveWrapper(false)
414 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
415 await waitUntilLivePublishedOnAllServers(liveVideoId)
416 await waitJobs(servers)
418 await testVideoResolutions(liveVideoId, resolutions)
420 await stopFfmpeg(command)
423 it('Should enable transcoding with some resolutions and correctly save them', async function () {
426 const resolutions = [ 240, 360, 720 ]
428 await updateConf(resolutions)
429 liveVideoId = await createLiveWrapper(true)
431 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId, 'video_short2.webm')
432 await waitUntilLivePublishedOnAllServers(liveVideoId)
433 await waitJobs(servers)
435 await testVideoResolutions(liveVideoId, resolutions)
437 await stopFfmpeg(command)
438 await waitUntilLiveEnded(servers[0].url, servers[0].accessToken, liveVideoId)
440 await waitJobs(servers)
442 await waitUntilLivePublishedOnAllServers(liveVideoId)
444 const bitrateLimits = {
445 720: 5000 * 1000, // 60FPS
450 for (const server of servers) {
451 const resVideo = await getVideo(server.url, liveVideoId)
452 const video: VideoDetails = resVideo.body
454 expect(video.state.id).to.equal(VideoState.PUBLISHED)
455 expect(video.duration).to.be.greaterThan(1)
456 expect(video.files).to.have.lengthOf(0)
458 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
459 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
460 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
462 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
464 for (const resolution of resolutions) {
465 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
467 expect(file).to.exist
468 expect(file.size).to.be.greaterThan(1)
470 if (resolution >= 720) {
471 expect(file.fps).to.be.approximately(60, 2)
473 expect(file.fps).to.be.approximately(30, 2)
476 const filename = `${video.uuid}-${resolution}-fragmented.mp4`
477 const segmentPath = buildServerDirectory(servers[0], join('streaming-playlists', 'hls', video.uuid, filename))
479 const probe = await ffprobePromise(segmentPath)
480 const videoStream = await getVideoStreamFromFile(segmentPath, probe)
482 expect(probe.format.bit_rate).to.be.below(bitrateLimits[videoStream.height])
484 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
485 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
490 it('Should correctly have cleaned up the live files', async function () {
493 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
497 describe('Live views', function () {
498 let liveVideoId: string
499 let command: FfmpegCommand
501 async function countViews (expected: number) {
502 for (const server of servers) {
503 const res = await getVideo(server.url, liveVideoId)
504 const video: VideoDetails = res.body
506 expect(video.views).to.equal(expected)
510 before(async function () {
513 const liveAttributes = {
515 channelId: servers[0].videoChannel.id,
516 privacy: VideoPrivacy.PUBLIC
519 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
520 liveVideoId = res.body.video.uuid
522 command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
523 await waitUntilLivePublishedOnAllServers(liveVideoId)
524 await waitJobs(servers)
527 it('Should display no views for a live', async function () {
531 it('Should view a live twice and display 1 view', async function () {
534 await viewVideo(servers[0].url, liveVideoId)
535 await viewVideo(servers[0].url, liveVideoId)
539 await waitJobs(servers)
544 it('Should wait and display 0 views', async function () {
548 await waitJobs(servers)
553 it('Should view a live on a remote and on local and display 2 views', async function () {
556 await viewVideo(servers[0].url, liveVideoId)
557 await viewVideo(servers[1].url, liveVideoId)
558 await viewVideo(servers[1].url, liveVideoId)
561 await waitJobs(servers)
566 after(async function () {
567 await stopFfmpeg(command)
571 describe('Live socket messages', function () {
573 async function createLiveWrapper () {
574 const liveAttributes = {
576 channelId: servers[0].videoChannel.id,
577 privacy: VideoPrivacy.PUBLIC
580 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
581 return res.body.video.uuid
584 it('Should correctly send a message when the live starts and ends', async function () {
587 const localStateChanges: VideoState[] = []
588 const remoteStateChanges: VideoState[] = []
590 const liveVideoUUID = await createLiveWrapper()
591 await waitJobs(servers)
594 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
596 const localSocket = getLiveNotificationSocket(servers[0].url)
597 localSocket.on('state-change', data => localStateChanges.push(data.state))
598 localSocket.emit('subscribe', { videoId })
602 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
604 const remoteSocket = getLiveNotificationSocket(servers[1].url)
605 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
606 remoteSocket.emit('subscribe', { videoId })
609 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
611 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
612 await waitJobs(servers)
614 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
615 expect(stateChanges).to.have.length.at.least(1)
616 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.PUBLISHED)
619 await stopFfmpeg(command)
621 for (const server of servers) {
622 await waitUntilLiveEnded(server.url, server.accessToken, liveVideoUUID)
624 await waitJobs(servers)
626 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
627 expect(stateChanges).to.have.length.at.least(2)
628 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.LIVE_ENDED)
632 it('Should correctly send views change notification', async function () {
635 let localLastVideoViews = 0
636 let remoteLastVideoViews = 0
638 const liveVideoUUID = await createLiveWrapper()
639 await waitJobs(servers)
642 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
644 const localSocket = getLiveNotificationSocket(servers[0].url)
645 localSocket.on('views-change', data => { localLastVideoViews = data.views })
646 localSocket.emit('subscribe', { videoId })
650 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
652 const remoteSocket = getLiveNotificationSocket(servers[1].url)
653 remoteSocket.on('views-change', data => { remoteLastVideoViews = data.views })
654 remoteSocket.emit('subscribe', { videoId })
657 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
659 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
660 await waitJobs(servers)
662 expect(localLastVideoViews).to.equal(0)
663 expect(remoteLastVideoViews).to.equal(0)
665 await viewVideo(servers[0].url, liveVideoUUID)
666 await viewVideo(servers[1].url, liveVideoUUID)
668 await waitJobs(servers)
670 await waitJobs(servers)
672 expect(localLastVideoViews).to.equal(2)
673 expect(remoteLastVideoViews).to.equal(2)
675 await stopFfmpeg(command)
678 it('Should not receive a notification after unsubscribe', async function () {
681 const stateChanges: VideoState[] = []
683 const liveVideoUUID = await createLiveWrapper()
684 await waitJobs(servers)
686 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
688 const socket = getLiveNotificationSocket(servers[0].url)
689 socket.on('state-change', data => stateChanges.push(data.state))
690 socket.emit('subscribe', { videoId })
692 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
694 await waitUntilLivePublishedOnAllServers(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)