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,
44 } from '../../../../shared/extra-utils'
46 const expect = chai.expect
48 describe('Test live', function () {
49 let servers: ServerInfo[] = []
51 before(async function () {
54 servers = await flushAndRunMultipleServers(2)
56 // Get the access tokens
57 await setAccessTokensToServers(servers)
58 await setDefaultVideoChannel(servers)
60 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
70 // Server 1 and server 2 follow each other
71 await doubleFollow(servers[0], servers[1])
74 describe('Live creation, update and delete', function () {
75 let liveVideoUUID: string
77 it('Should create a live with the appropriate parameters', async function () {
80 const attributes: LiveVideoCreate = {
84 description: 'super live description',
85 support: 'support field',
86 channelId: servers[0].videoChannel.id,
88 waitTranscoding: false,
89 name: 'my super live',
90 tags: [ 'tag1', 'tag2' ],
91 commentsEnabled: false,
92 downloadEnabled: false,
94 privacy: VideoPrivacy.PUBLIC,
95 previewfile: 'video_short1-preview.webm.jpg',
96 thumbnailfile: 'video_short1.webm.jpg'
99 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
100 liveVideoUUID = res.body.video.uuid
102 await waitJobs(servers)
104 for (const server of servers) {
105 const resVideo = await getVideo(server.url, liveVideoUUID)
106 const video: VideoDetails = resVideo.body
108 expect(video.category.id).to.equal(1)
109 expect(video.licence.id).to.equal(2)
110 expect(video.language.id).to.equal('fr')
111 expect(video.description).to.equal('super live description')
112 expect(video.support).to.equal('support field')
114 expect(video.channel.name).to.equal(servers[0].videoChannel.name)
115 expect(video.channel.host).to.equal(servers[0].videoChannel.host)
117 expect(video.nsfw).to.be.false
118 expect(video.waitTranscoding).to.be.false
119 expect(video.name).to.equal('my super live')
120 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
121 expect(video.commentsEnabled).to.be.false
122 expect(video.downloadEnabled).to.be.false
123 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
125 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
126 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
128 const resLive = await getLive(server.url, server.accessToken, liveVideoUUID)
129 const live: LiveVideo = resLive.body
131 if (server.url === servers[0].url) {
132 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
133 expect(live.streamKey).to.not.be.empty
135 expect(live.rtmpUrl).to.be.null
136 expect(live.streamKey).to.be.null
139 expect(live.saveReplay).to.be.true
143 it('Should have a default preview and thumbnail', async function () {
146 const attributes: LiveVideoCreate = {
147 name: 'default live thumbnail',
148 channelId: servers[0].videoChannel.id,
149 privacy: VideoPrivacy.UNLISTED,
153 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
154 const videoId = res.body.video.uuid
156 await waitJobs(servers)
158 for (const server of servers) {
159 const resVideo = await getVideo(server.url, videoId)
160 const video: VideoDetails = resVideo.body
162 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
163 expect(video.nsfw).to.be.true
165 await makeRawRequest(server.url + video.thumbnailPath, 200)
166 await makeRawRequest(server.url + video.previewPath, 200)
170 it('Should not have the live listed since nobody streams into', async function () {
171 for (const server of servers) {
172 const res = await getVideosList(server.url)
174 expect(res.body.total).to.equal(0)
175 expect(res.body.data).to.have.lengthOf(0)
179 it('Should not be able to update a live of another server', async function () {
180 await updateLive(servers[1].url, servers[1].accessToken, liveVideoUUID, { saveReplay: false }, 403)
183 it('Should update the live', async function () {
186 await updateLive(servers[0].url, servers[0].accessToken, liveVideoUUID, { saveReplay: false })
187 await waitJobs(servers)
190 it('Have the live updated', async function () {
191 for (const server of servers) {
192 const res = await getLive(server.url, server.accessToken, liveVideoUUID)
193 const live: LiveVideo = res.body
195 if (server.url === servers[0].url) {
196 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
197 expect(live.streamKey).to.not.be.empty
199 expect(live.rtmpUrl).to.be.null
200 expect(live.streamKey).to.be.null
203 expect(live.saveReplay).to.be.false
207 it('Delete the live', async function () {
210 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
211 await waitJobs(servers)
214 it('Should have the live deleted', async function () {
215 for (const server of servers) {
216 await getVideo(server.url, liveVideoUUID, 404)
217 await getLive(server.url, server.accessToken, liveVideoUUID, 404)
222 describe('Stream checks', function () {
223 let liveVideo: LiveVideo & VideoDetails
227 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
230 async function createLiveWrapper () {
231 const liveAttributes = {
233 channelId: servers[0].videoChannel.id,
234 privacy: VideoPrivacy.PUBLIC,
238 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
239 const uuid = res.body.video.uuid
241 const resLive = await getLive(servers[0].url, servers[0].accessToken, uuid)
242 const resVideo = await getVideo(servers[0].url, uuid)
244 return Object.assign(resVideo.body, resLive.body) as LiveVideo & VideoDetails
247 it('Should not allow a stream without the appropriate path', async function () {
250 liveVideo = await createLiveWrapper()
252 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
253 await testFfmpegStreamError(command, true)
256 it('Should not allow a stream without the appropriate stream key', async function () {
259 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
260 await testFfmpegStreamError(command, true)
263 it('Should succeed with the correct params', async function () {
266 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
267 await testFfmpegStreamError(command, false)
270 it('Should not allow a stream on a live that was blacklisted', async function () {
273 liveVideo = await createLiveWrapper()
275 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideo.uuid)
277 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
278 await testFfmpegStreamError(command, true)
281 it('Should not allow a stream on a live that was deleted', async function () {
284 liveVideo = await createLiveWrapper()
286 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid)
288 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
289 await testFfmpegStreamError(command, true)
293 describe('Live transcoding', function () {
294 let liveVideoId: string
296 async function createLiveWrapper (saveReplay: boolean) {
297 const liveAttributes = {
299 channelId: servers[0].videoChannel.id,
300 privacy: VideoPrivacy.PUBLIC,
304 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
305 return res.body.video.uuid
308 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
309 for (const server of servers) {
310 const resList = await getVideosList(server.url)
311 const videos: Video[] = resList.body.data
313 expect(videos.find(v => v.uuid === liveVideoId)).to.exist
315 const resVideo = await getVideo(server.url, liveVideoId)
316 const video: VideoDetails = resVideo.body
318 expect(video.streamingPlaylists).to.have.lengthOf(1)
320 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
321 expect(hlsPlaylist).to.exist
323 // Only finite files are displayed
324 expect(hlsPlaylist.files).to.have.lengthOf(0)
326 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions)
328 for (let i = 0; i < resolutions.length; i++) {
329 const segmentName = `${i}-000001.ts`
330 await waitUntilLog(servers[0], `${video.uuid}/${segmentName}`, 2, false)
332 const res = await getPlaylist(`${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`)
333 const subPlaylist = res.text
335 expect(subPlaylist).to.contain(segmentName)
337 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
338 await checkLiveSegmentHash(baseUrlAndPath, video.uuid, segmentName, hlsPlaylist)
343 function updateConf (resolutions: number[]) {
344 return updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
352 '240p': resolutions.includes(240),
353 '360p': resolutions.includes(360),
354 '480p': resolutions.includes(480),
355 '720p': resolutions.includes(720),
356 '1080p': resolutions.includes(1080),
357 '2160p': resolutions.includes(2160)
364 before(async function () {
368 it('Should enable transcoding without additional resolutions', async function () {
371 liveVideoId = await createLiveWrapper(false)
373 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
374 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
375 await waitJobs(servers)
377 await testVideoResolutions(liveVideoId, [ 720 ])
379 await stopFfmpeg(command)
382 it('Should enable transcoding with some resolutions', async function () {
385 const resolutions = [ 240, 480 ]
386 await updateConf(resolutions)
387 liveVideoId = await createLiveWrapper(false)
389 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
390 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
391 await waitJobs(servers)
393 await testVideoResolutions(liveVideoId, resolutions)
395 await stopFfmpeg(command)
398 it('Should enable transcoding with some resolutions and correctly save them', async function () {
401 const resolutions = [ 240, 360, 720 ]
403 await updateConf(resolutions)
404 liveVideoId = await createLiveWrapper(true)
406 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId, 'video_short2.webm')
407 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
408 await waitJobs(servers)
410 await testVideoResolutions(liveVideoId, resolutions)
412 await stopFfmpeg(command)
414 await waitJobs(servers)
416 const bitrateLimits = {
422 for (const server of servers) {
423 const resVideo = await getVideo(server.url, liveVideoId)
424 const video: VideoDetails = resVideo.body
426 expect(video.duration).to.be.greaterThan(1)
427 expect(video.files).to.have.lengthOf(0)
429 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
431 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
433 for (const resolution of resolutions) {
434 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
436 expect(file).to.exist
437 expect(file.fps).to.be.approximately(30, 5)
438 expect(file.size).to.be.greaterThan(1)
440 const filename = `${video.uuid}-${resolution}-fragmented.mp4`
441 const segmentPath = buildServerDirectory(servers[0], join('streaming-playlists', 'hls', video.uuid, filename))
443 const probe = await ffprobePromise(segmentPath)
444 const videoStream = await getVideoStreamFromFile(segmentPath, probe)
445 console.log(videoStream)
446 expect(probe.format.bit_rate).to.be.below(bitrateLimits[videoStream.height])
448 await makeRawRequest(file.torrentUrl, 200)
449 await makeRawRequest(file.fileUrl, 200)
454 it('Should correctly have cleaned up the live files', async function () {
457 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
461 describe('Live views', function () {
462 let liveVideoId: string
463 let command: FfmpegCommand
465 async function countViews (expected: number) {
466 for (const server of servers) {
467 const res = await getVideo(server.url, liveVideoId)
468 const video: VideoDetails = res.body
470 expect(video.views).to.equal(expected)
474 before(async function () {
477 const liveAttributes = {
479 channelId: servers[0].videoChannel.id,
480 privacy: VideoPrivacy.PUBLIC
483 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
484 liveVideoId = res.body.video.uuid
486 command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
487 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
488 await waitJobs(servers)
491 it('Should display no views for a live', async function () {
495 it('Should view a live twice and display 1 view', async function () {
498 await viewVideo(servers[0].url, liveVideoId)
499 await viewVideo(servers[0].url, liveVideoId)
503 await waitJobs(servers)
508 it('Should wait and display 0 views', async function () {
512 await waitJobs(servers)
517 it('Should view a live on a remote and on local and display 2 views', async function () {
520 await viewVideo(servers[0].url, liveVideoId)
521 await viewVideo(servers[1].url, liveVideoId)
522 await viewVideo(servers[1].url, liveVideoId)
525 await waitJobs(servers)
530 after(async function () {
531 await stopFfmpeg(command)
535 describe('Live socket messages', function () {
537 async function createLiveWrapper () {
538 const liveAttributes = {
540 channelId: servers[0].videoChannel.id,
541 privacy: VideoPrivacy.PUBLIC
544 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
545 return res.body.video.uuid
548 it('Should correctly send a message when the live starts and ends', async function () {
551 const localStateChanges: VideoState[] = []
552 const remoteStateChanges: VideoState[] = []
554 const liveVideoUUID = await createLiveWrapper()
555 await waitJobs(servers)
558 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
560 const localSocket = getLiveNotificationSocket(servers[0].url)
561 localSocket.on('state-change', data => localStateChanges.push(data.state))
562 localSocket.emit('subscribe', { videoId })
566 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
568 const remoteSocket = getLiveNotificationSocket(servers[1].url)
569 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
570 remoteSocket.emit('subscribe', { videoId })
573 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
574 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoUUID)
575 await waitJobs(servers)
577 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
578 expect(stateChanges).to.have.lengthOf(1)
579 expect(stateChanges[0]).to.equal(VideoState.PUBLISHED)
582 await stopFfmpeg(command)
583 await waitJobs(servers)
585 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
586 expect(stateChanges).to.have.lengthOf(2)
587 expect(stateChanges[1]).to.equal(VideoState.LIVE_ENDED)
591 it('Should not receive a notification after unsubscribe', async function () {
594 const stateChanges: VideoState[] = []
596 const liveVideoUUID = await createLiveWrapper()
597 await waitJobs(servers)
599 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
601 const socket = getLiveNotificationSocket(servers[0].url)
602 socket.on('state-change', data => stateChanges.push(data.state))
603 socket.emit('subscribe', { videoId })
605 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
606 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoUUID)
607 await waitJobs(servers)
609 expect(stateChanges).to.have.lengthOf(1)
610 socket.emit('unsubscribe', { videoId })
612 await stopFfmpeg(command)
613 await waitJobs(servers)
615 expect(stateChanges).to.have.lengthOf(1)
619 describe('After a server restart', function () {
620 let liveVideoId: string
621 let liveVideoReplayId: string
623 async function createLiveWrapper (saveReplay: boolean) {
624 const liveAttributes = {
626 channelId: servers[0].videoChannel.id,
627 privacy: VideoPrivacy.PUBLIC,
631 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
632 return res.body.video.uuid
635 before(async function () {
638 liveVideoId = await createLiveWrapper(false)
639 liveVideoReplayId = await createLiveWrapper(true)
642 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId),
643 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoReplayId)
647 waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId),
648 waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoReplayId)
651 await killallServers([ servers[0] ])
652 await reRunServer(servers[0])
657 it('Should cleanup lives', async function () {
660 const res = await getVideo(servers[0].url, liveVideoId)
661 const video: VideoDetails = res.body
663 expect(video.state.id).to.equal(VideoState.LIVE_ENDED)
666 it('Should save a live replay', async function () {
669 await waitJobs(servers)
671 const res = await getVideo(servers[0].url, liveVideoReplayId)
672 const video: VideoDetails = res.body
674 expect(video.state.id).to.equal(VideoState.PUBLISHED)
678 after(async function () {
679 await cleanupTests(servers)