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,
43 waitUntilLivePublished,
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.nsfw).to.be.false
120 expect(video.waitTranscoding).to.be.false
121 expect(video.name).to.equal('my super live')
122 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
123 expect(video.commentsEnabled).to.be.false
124 expect(video.downloadEnabled).to.be.false
125 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
127 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
128 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
130 const resLive = await getLive(server.url, server.accessToken, liveVideoUUID)
131 const live: LiveVideo = resLive.body
133 if (server.url === servers[0].url) {
134 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
135 expect(live.streamKey).to.not.be.empty
137 expect(live.rtmpUrl).to.be.null
138 expect(live.streamKey).to.be.null
141 expect(live.saveReplay).to.be.true
145 it('Should have a default preview and thumbnail', async function () {
148 const attributes: LiveVideoCreate = {
149 name: 'default live thumbnail',
150 channelId: servers[0].videoChannel.id,
151 privacy: VideoPrivacy.UNLISTED,
155 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
156 const videoId = res.body.video.uuid
158 await waitJobs(servers)
160 for (const server of servers) {
161 const resVideo = await getVideo(server.url, videoId)
162 const video: VideoDetails = resVideo.body
164 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
165 expect(video.nsfw).to.be.true
167 await makeRawRequest(server.url + video.thumbnailPath, 200)
168 await makeRawRequest(server.url + video.previewPath, 200)
172 it('Should not have the live listed since nobody streams into', async function () {
173 for (const server of servers) {
174 const res = await getVideosList(server.url)
176 expect(res.body.total).to.equal(0)
177 expect(res.body.data).to.have.lengthOf(0)
181 it('Should not be able to update a live of another server', async function () {
182 await updateLive(servers[1].url, servers[1].accessToken, liveVideoUUID, { saveReplay: false }, 403)
185 it('Should update the live', async function () {
188 await updateLive(servers[0].url, servers[0].accessToken, liveVideoUUID, { saveReplay: false })
189 await waitJobs(servers)
192 it('Have the live updated', async function () {
193 for (const server of servers) {
194 const res = await getLive(server.url, server.accessToken, liveVideoUUID)
195 const live: LiveVideo = res.body
197 if (server.url === servers[0].url) {
198 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
199 expect(live.streamKey).to.not.be.empty
201 expect(live.rtmpUrl).to.be.null
202 expect(live.streamKey).to.be.null
205 expect(live.saveReplay).to.be.false
209 it('Delete the live', async function () {
212 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
213 await waitJobs(servers)
216 it('Should have the live deleted', async function () {
217 for (const server of servers) {
218 await getVideo(server.url, liveVideoUUID, 404)
219 await getLive(server.url, server.accessToken, liveVideoUUID, 404)
224 describe('Stream checks', function () {
225 let liveVideo: LiveVideo & VideoDetails
229 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
232 async function createLiveWrapper () {
233 const liveAttributes = {
235 channelId: servers[0].videoChannel.id,
236 privacy: VideoPrivacy.PUBLIC,
240 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
241 const uuid = res.body.video.uuid
243 const resLive = await getLive(servers[0].url, servers[0].accessToken, uuid)
244 const resVideo = await getVideo(servers[0].url, uuid)
246 return Object.assign(resVideo.body, resLive.body) as LiveVideo & VideoDetails
249 it('Should not allow a stream without the appropriate path', async function () {
252 liveVideo = await createLiveWrapper()
254 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
255 await testFfmpegStreamError(command, true)
258 it('Should not allow a stream without the appropriate stream key', async function () {
261 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
262 await testFfmpegStreamError(command, true)
265 it('Should succeed with the correct params', async function () {
268 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
269 await testFfmpegStreamError(command, false)
272 it('Should not allow a stream on a live that was blacklisted', async function () {
275 liveVideo = await createLiveWrapper()
277 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideo.uuid)
279 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
280 await testFfmpegStreamError(command, true)
283 it('Should not allow a stream on a live that was deleted', async function () {
286 liveVideo = await createLiveWrapper()
288 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid)
290 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
291 await testFfmpegStreamError(command, true)
295 describe('Live transcoding', function () {
296 let liveVideoId: string
298 async function createLiveWrapper (saveReplay: boolean) {
299 const liveAttributes = {
301 channelId: servers[0].videoChannel.id,
302 privacy: VideoPrivacy.PUBLIC,
306 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
307 return res.body.video.uuid
310 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
311 for (const server of servers) {
312 const resList = await getVideosList(server.url)
313 const videos: Video[] = resList.body.data
315 expect(videos.find(v => v.uuid === liveVideoId)).to.exist
317 const resVideo = await getVideo(server.url, liveVideoId)
318 const video: VideoDetails = resVideo.body
320 expect(video.streamingPlaylists).to.have.lengthOf(1)
322 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
323 expect(hlsPlaylist).to.exist
325 // Only finite files are displayed
326 expect(hlsPlaylist.files).to.have.lengthOf(0)
328 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions)
330 for (let i = 0; i < resolutions.length; i++) {
331 const segmentName = `${i}-000001.ts`
332 await waitUntilLog(servers[0], `${video.uuid}/${segmentName}`, 2, false)
334 const res = await getPlaylist(`${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`)
335 const subPlaylist = res.text
337 expect(subPlaylist).to.contain(segmentName)
339 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
340 await checkLiveSegmentHash(baseUrlAndPath, video.uuid, segmentName, hlsPlaylist)
345 function updateConf (resolutions: number[]) {
346 return updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
354 '240p': resolutions.includes(240),
355 '360p': resolutions.includes(360),
356 '480p': resolutions.includes(480),
357 '720p': resolutions.includes(720),
358 '1080p': resolutions.includes(1080),
359 '2160p': resolutions.includes(2160)
366 before(async function () {
370 it('Should enable transcoding without additional resolutions', async function () {
373 liveVideoId = await createLiveWrapper(false)
375 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
376 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
377 await waitJobs(servers)
379 await testVideoResolutions(liveVideoId, [ 720 ])
381 await stopFfmpeg(command)
384 it('Should enable transcoding with some resolutions', async function () {
387 const resolutions = [ 240, 480 ]
388 await updateConf(resolutions)
389 liveVideoId = await createLiveWrapper(false)
391 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
392 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
393 await waitJobs(servers)
395 await testVideoResolutions(liveVideoId, resolutions)
397 await stopFfmpeg(command)
400 it('Should enable transcoding with some resolutions and correctly save them', async function () {
403 const resolutions = [ 240, 360, 720 ]
405 await updateConf(resolutions)
406 liveVideoId = await createLiveWrapper(true)
408 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId, 'video_short2.webm')
409 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
410 await waitJobs(servers)
412 await testVideoResolutions(liveVideoId, resolutions)
414 await stopFfmpeg(command)
415 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoId)
417 await waitJobs(servers)
419 const bitrateLimits = {
420 720: 4000 * 1000, // 60FPS
425 for (const server of servers) {
426 const resVideo = await getVideo(server.url, liveVideoId)
427 const video: VideoDetails = resVideo.body
429 expect(video.duration).to.be.greaterThan(1)
430 expect(video.files).to.have.lengthOf(0)
432 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
434 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
436 for (const resolution of resolutions) {
437 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
439 expect(file).to.exist
440 expect(file.size).to.be.greaterThan(1)
442 if (resolution >= 720) {
443 expect(file.fps).to.be.approximately(60, 2)
445 expect(file.fps).to.be.approximately(30, 2)
448 const filename = `${video.uuid}-${resolution}-fragmented.mp4`
449 const segmentPath = buildServerDirectory(servers[0], join('streaming-playlists', 'hls', video.uuid, filename))
451 const probe = await ffprobePromise(segmentPath)
452 const videoStream = await getVideoStreamFromFile(segmentPath, probe)
454 expect(probe.format.bit_rate).to.be.below(bitrateLimits[videoStream.height])
456 await makeRawRequest(file.torrentUrl, 200)
457 await makeRawRequest(file.fileUrl, 200)
462 it('Should correctly have cleaned up the live files', async function () {
465 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
469 describe('Live views', function () {
470 let liveVideoId: string
471 let command: FfmpegCommand
473 async function countViews (expected: number) {
474 for (const server of servers) {
475 const res = await getVideo(server.url, liveVideoId)
476 const video: VideoDetails = res.body
478 expect(video.views).to.equal(expected)
482 before(async function () {
485 const liveAttributes = {
487 channelId: servers[0].videoChannel.id,
488 privacy: VideoPrivacy.PUBLIC
491 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
492 liveVideoId = res.body.video.uuid
494 command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
495 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
496 await waitJobs(servers)
499 it('Should display no views for a live', async function () {
503 it('Should view a live twice and display 1 view', async function () {
506 await viewVideo(servers[0].url, liveVideoId)
507 await viewVideo(servers[0].url, liveVideoId)
511 await waitJobs(servers)
516 it('Should wait and display 0 views', async function () {
520 await waitJobs(servers)
525 it('Should view a live on a remote and on local and display 2 views', async function () {
528 await viewVideo(servers[0].url, liveVideoId)
529 await viewVideo(servers[1].url, liveVideoId)
530 await viewVideo(servers[1].url, liveVideoId)
533 await waitJobs(servers)
538 after(async function () {
539 await stopFfmpeg(command)
543 describe('Live socket messages', function () {
545 async function createLiveWrapper () {
546 const liveAttributes = {
548 channelId: servers[0].videoChannel.id,
549 privacy: VideoPrivacy.PUBLIC
552 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
553 return res.body.video.uuid
556 it('Should correctly send a message when the live starts and ends', async function () {
559 const localStateChanges: VideoState[] = []
560 const remoteStateChanges: VideoState[] = []
562 const liveVideoUUID = await createLiveWrapper()
563 await waitJobs(servers)
566 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
568 const localSocket = getLiveNotificationSocket(servers[0].url)
569 localSocket.on('state-change', data => localStateChanges.push(data.state))
570 localSocket.emit('subscribe', { videoId })
574 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
576 const remoteSocket = getLiveNotificationSocket(servers[1].url)
577 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
578 remoteSocket.emit('subscribe', { videoId })
581 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
582 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoUUID)
583 await waitJobs(servers)
585 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
586 expect(stateChanges).to.have.length.at.least(1)
587 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.PUBLISHED)
590 await stopFfmpeg(command)
591 await waitUntilLiveEnded(servers[0].url, servers[0].accessToken, liveVideoUUID)
593 await waitJobs(servers)
595 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
596 expect(stateChanges).to.have.length.at.least(2)
597 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.LIVE_ENDED)
601 it('Should not receive a notification after unsubscribe', async function () {
604 const stateChanges: VideoState[] = []
606 const liveVideoUUID = await createLiveWrapper()
607 await waitJobs(servers)
609 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
611 const socket = getLiveNotificationSocket(servers[0].url)
612 socket.on('state-change', data => stateChanges.push(data.state))
613 socket.emit('subscribe', { videoId })
615 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
616 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoUUID)
617 await waitJobs(servers)
619 expect(stateChanges).to.have.lengthOf(1)
620 socket.emit('unsubscribe', { videoId })
622 await stopFfmpeg(command)
623 await waitJobs(servers)
625 expect(stateChanges).to.have.lengthOf(1)
629 describe('After a server restart', function () {
630 let liveVideoId: string
631 let liveVideoReplayId: string
633 async function createLiveWrapper (saveReplay: boolean) {
634 const liveAttributes = {
636 channelId: servers[0].videoChannel.id,
637 privacy: VideoPrivacy.PUBLIC,
641 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
642 return res.body.video.uuid
645 before(async function () {
648 liveVideoId = await createLiveWrapper(false)
649 liveVideoReplayId = await createLiveWrapper(true)
652 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId),
653 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoReplayId)
657 waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId),
658 waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoReplayId)
661 await killallServers([ servers[0] ])
662 await reRunServer(servers[0])
667 it('Should cleanup lives', async function () {
670 const res = await getVideo(servers[0].url, liveVideoId)
671 const video: VideoDetails = res.body
673 expect(video.state.id).to.equal(VideoState.LIVE_ENDED)
676 it('Should save a live replay', async function () {
679 await waitJobs(servers)
681 const res = await getVideo(servers[0].url, liveVideoReplayId)
682 const video: VideoDetails = res.body
684 expect(video.state.id).to.equal(VideoState.PUBLISHED)
688 after(async function () {
689 await cleanupTests(servers)