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,
22 getMyVideosWithFilter,
33 sendRTMPStreamInVideo,
35 setAccessTokensToServers,
36 setDefaultVideoChannel,
38 testFfmpegStreamError,
40 updateCustomSubConfig,
47 waitUntilLivePublished,
48 waitUntilLiveSegmentGeneration
49 } from '../../../../shared/extra-utils'
51 const expect = chai.expect
53 describe('Test live', function () {
54 let servers: ServerInfo[] = []
56 async function waitUntilLivePublishedOnAllServers (videoId: string) {
57 for (const server of servers) {
58 await waitUntilLivePublished(server.url, server.accessToken, videoId)
62 before(async function () {
65 servers = await flushAndRunMultipleServers(2)
67 // Get the access tokens
68 await setAccessTokensToServers(servers)
69 await setDefaultVideoChannel(servers)
71 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
81 // Server 1 and server 2 follow each other
82 await doubleFollow(servers[0], servers[1])
85 describe('Live creation, update and delete', function () {
86 let liveVideoUUID: string
88 it('Should create a live with the appropriate parameters', async function () {
91 const attributes: LiveVideoCreate = {
95 description: 'super live description',
96 support: 'support field',
97 channelId: servers[0].videoChannel.id,
99 waitTranscoding: false,
100 name: 'my super live',
101 tags: [ 'tag1', 'tag2' ],
102 commentsEnabled: false,
103 downloadEnabled: false,
105 privacy: VideoPrivacy.PUBLIC,
106 previewfile: 'video_short1-preview.webm.jpg',
107 thumbnailfile: 'video_short1.webm.jpg'
110 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
111 liveVideoUUID = res.body.video.uuid
113 await waitJobs(servers)
115 for (const server of servers) {
116 const resVideo = await getVideo(server.url, liveVideoUUID)
117 const video: VideoDetails = resVideo.body
119 expect(video.category.id).to.equal(1)
120 expect(video.licence.id).to.equal(2)
121 expect(video.language.id).to.equal('fr')
122 expect(video.description).to.equal('super live description')
123 expect(video.support).to.equal('support field')
125 expect(video.channel.name).to.equal(servers[0].videoChannel.name)
126 expect(video.channel.host).to.equal(servers[0].videoChannel.host)
128 expect(video.isLive).to.be.true
130 expect(video.nsfw).to.be.false
131 expect(video.waitTranscoding).to.be.false
132 expect(video.name).to.equal('my super live')
133 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
134 expect(video.commentsEnabled).to.be.false
135 expect(video.downloadEnabled).to.be.false
136 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
138 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
139 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
141 const resLive = await getLive(server.url, server.accessToken, liveVideoUUID)
142 const live: LiveVideo = resLive.body
144 if (server.url === servers[0].url) {
145 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
146 expect(live.streamKey).to.not.be.empty
148 expect(live.rtmpUrl).to.be.null
149 expect(live.streamKey).to.be.null
152 expect(live.saveReplay).to.be.true
156 it('Should have a default preview and thumbnail', async function () {
159 const attributes: LiveVideoCreate = {
160 name: 'default live thumbnail',
161 channelId: servers[0].videoChannel.id,
162 privacy: VideoPrivacy.UNLISTED,
166 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
167 const videoId = res.body.video.uuid
169 await waitJobs(servers)
171 for (const server of servers) {
172 const resVideo = await getVideo(server.url, videoId)
173 const video: VideoDetails = resVideo.body
175 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
176 expect(video.nsfw).to.be.true
178 await makeRawRequest(server.url + video.thumbnailPath, HttpStatusCode.OK_200)
179 await makeRawRequest(server.url + video.previewPath, HttpStatusCode.OK_200)
183 it('Should not have the live listed since nobody streams into', async function () {
184 for (const server of servers) {
185 const res = await getVideosList(server.url)
187 expect(res.body.total).to.equal(0)
188 expect(res.body.data).to.have.lengthOf(0)
192 it('Should not be able to update a live of another server', async function () {
193 await updateLive(servers[1].url, servers[1].accessToken, liveVideoUUID, { saveReplay: false }, HttpStatusCode.FORBIDDEN_403)
196 it('Should update the live', async function () {
199 await updateLive(servers[0].url, servers[0].accessToken, liveVideoUUID, { saveReplay: false })
200 await waitJobs(servers)
203 it('Have the live updated', async function () {
204 for (const server of servers) {
205 const res = await getLive(server.url, server.accessToken, liveVideoUUID)
206 const live: LiveVideo = res.body
208 if (server.url === servers[0].url) {
209 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
210 expect(live.streamKey).to.not.be.empty
212 expect(live.rtmpUrl).to.be.null
213 expect(live.streamKey).to.be.null
216 expect(live.saveReplay).to.be.false
220 it('Delete the live', async function () {
223 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
224 await waitJobs(servers)
227 it('Should have the live deleted', async function () {
228 for (const server of servers) {
229 await getVideo(server.url, liveVideoUUID, HttpStatusCode.NOT_FOUND_404)
230 await getLive(server.url, server.accessToken, liveVideoUUID, HttpStatusCode.NOT_FOUND_404)
235 describe('Live filters', function () {
237 let liveVideoId: string
238 let vodVideoId: string
240 before(async function () {
243 vodVideoId = (await uploadVideoAndGetId({ server: servers[0], videoName: 'vod video' })).uuid
245 const liveOptions = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: servers[0].videoChannel.id }
246 const resLive = await createLive(servers[0].url, servers[0].accessToken, liveOptions)
247 liveVideoId = resLive.body.video.uuid
249 command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
250 await waitUntilLivePublishedOnAllServers(liveVideoId)
251 await waitJobs(servers)
254 it('Should only display lives', async function () {
255 const res = await getVideosWithFilters(servers[0].url, { isLive: true })
257 expect(res.body.total).to.equal(1)
258 expect(res.body.data).to.have.lengthOf(1)
259 expect(res.body.data[0].name).to.equal('live')
262 it('Should not display lives', async function () {
263 const res = await getVideosWithFilters(servers[0].url, { isLive: false })
265 expect(res.body.total).to.equal(1)
266 expect(res.body.data).to.have.lengthOf(1)
267 expect(res.body.data[0].name).to.equal('vod video')
270 it('Should display my lives', async function () {
273 await stopFfmpeg(command)
274 await waitJobs(servers)
276 const res = await getMyVideosWithFilter(servers[0].url, servers[0].accessToken, { isLive: true })
277 const videos = res.body.data as Video[]
279 const result = videos.every(v => v.isLive)
280 expect(result).to.be.true
283 it('Should not display my lives', async function () {
284 const res = await getMyVideosWithFilter(servers[0].url, servers[0].accessToken, { isLive: false })
285 const videos = res.body.data as Video[]
287 const result = videos.every(v => !v.isLive)
288 expect(result).to.be.true
291 after(async function () {
292 await removeVideo(servers[0].url, servers[0].accessToken, vodVideoId)
293 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoId)
297 describe('Stream checks', function () {
298 let liveVideo: LiveVideo & VideoDetails
302 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
305 async function createLiveWrapper () {
306 const liveAttributes = {
308 channelId: servers[0].videoChannel.id,
309 privacy: VideoPrivacy.PUBLIC,
313 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
314 const uuid = res.body.video.uuid
316 const resLive = await getLive(servers[0].url, servers[0].accessToken, uuid)
317 const resVideo = await getVideo(servers[0].url, uuid)
319 return Object.assign(resVideo.body, resLive.body) as LiveVideo & VideoDetails
322 it('Should not allow a stream without the appropriate path', async function () {
325 liveVideo = await createLiveWrapper()
327 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
328 await testFfmpegStreamError(command, true)
331 it('Should not allow a stream without the appropriate stream key', async function () {
334 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
335 await testFfmpegStreamError(command, true)
338 it('Should succeed with the correct params', async function () {
341 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
342 await testFfmpegStreamError(command, false)
345 it('Should list this live now someone stream into it', async function () {
346 for (const server of servers) {
347 const res = await getVideosList(server.url)
349 expect(res.body.total).to.equal(1)
350 expect(res.body.data).to.have.lengthOf(1)
352 const video: Video = res.body.data[0]
354 expect(video.name).to.equal('user live')
355 expect(video.isLive).to.be.true
359 it('Should not allow a stream on a live that was blacklisted', async function () {
362 liveVideo = await createLiveWrapper()
364 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideo.uuid)
366 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
367 await testFfmpegStreamError(command, true)
370 it('Should not allow a stream on a live that was deleted', async function () {
373 liveVideo = await createLiveWrapper()
375 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid)
377 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
378 await testFfmpegStreamError(command, true)
382 describe('Live transcoding', function () {
383 let liveVideoId: string
385 async function createLiveWrapper (saveReplay: boolean) {
386 const liveAttributes = {
388 channelId: servers[0].videoChannel.id,
389 privacy: VideoPrivacy.PUBLIC,
393 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
394 return res.body.video.uuid
397 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
398 for (const server of servers) {
399 const resList = await getVideosList(server.url)
400 const videos: Video[] = resList.body.data
402 expect(videos.find(v => v.uuid === liveVideoId)).to.exist
404 const resVideo = await getVideo(server.url, liveVideoId)
405 const video: VideoDetails = resVideo.body
407 expect(video.streamingPlaylists).to.have.lengthOf(1)
409 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
410 expect(hlsPlaylist).to.exist
412 // Only finite files are displayed
413 expect(hlsPlaylist.files).to.have.lengthOf(0)
415 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions)
417 for (let i = 0; i < resolutions.length; i++) {
419 const segmentName = `${i}-00000${segmentNum}.ts`
420 await waitUntilLiveSegmentGeneration(servers[0], video.uuid, i, segmentNum)
422 const res = await getPlaylist(`${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`)
423 const subPlaylist = res.text
425 expect(subPlaylist).to.contain(segmentName)
427 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
428 await checkLiveSegmentHash(baseUrlAndPath, video.uuid, segmentName, hlsPlaylist)
433 function updateConf (resolutions: number[]) {
434 return updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
442 '240p': resolutions.includes(240),
443 '360p': resolutions.includes(360),
444 '480p': resolutions.includes(480),
445 '720p': resolutions.includes(720),
446 '1080p': resolutions.includes(1080),
447 '2160p': resolutions.includes(2160)
454 before(async function () {
458 it('Should enable transcoding without additional resolutions', async function () {
461 liveVideoId = await createLiveWrapper(false)
463 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
464 await waitUntilLivePublishedOnAllServers(liveVideoId)
465 await waitJobs(servers)
467 await testVideoResolutions(liveVideoId, [ 720 ])
469 await stopFfmpeg(command)
472 it('Should enable transcoding with some resolutions', async function () {
475 const resolutions = [ 240, 480 ]
476 await updateConf(resolutions)
477 liveVideoId = await createLiveWrapper(false)
479 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
480 await waitUntilLivePublishedOnAllServers(liveVideoId)
481 await waitJobs(servers)
483 await testVideoResolutions(liveVideoId, resolutions)
485 await stopFfmpeg(command)
488 it('Should enable transcoding with some resolutions and correctly save them', async function () {
491 const resolutions = [ 240, 360, 720 ]
493 await updateConf(resolutions)
494 liveVideoId = await createLiveWrapper(true)
496 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId, 'video_short2.webm')
497 await waitUntilLivePublishedOnAllServers(liveVideoId)
498 await waitJobs(servers)
500 await testVideoResolutions(liveVideoId, resolutions)
502 await stopFfmpeg(command)
503 await waitUntilLiveEnded(servers[0].url, servers[0].accessToken, liveVideoId)
505 await waitJobs(servers)
507 await waitUntilLivePublishedOnAllServers(liveVideoId)
509 const bitrateLimits = {
510 720: 5000 * 1000, // 60FPS
515 for (const server of servers) {
516 const resVideo = await getVideo(server.url, liveVideoId)
517 const video: VideoDetails = resVideo.body
519 expect(video.state.id).to.equal(VideoState.PUBLISHED)
520 expect(video.duration).to.be.greaterThan(1)
521 expect(video.files).to.have.lengthOf(0)
523 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
524 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
525 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
527 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
529 for (const resolution of resolutions) {
530 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
532 expect(file).to.exist
533 expect(file.size).to.be.greaterThan(1)
535 if (resolution >= 720) {
536 expect(file.fps).to.be.approximately(60, 2)
538 expect(file.fps).to.be.approximately(30, 2)
541 const filename = `${video.uuid}-${resolution}-fragmented.mp4`
542 const segmentPath = buildServerDirectory(servers[0], join('streaming-playlists', 'hls', video.uuid, filename))
544 const probe = await ffprobePromise(segmentPath)
545 const videoStream = await getVideoStreamFromFile(segmentPath, probe)
547 expect(probe.format.bit_rate).to.be.below(bitrateLimits[videoStream.height])
549 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
550 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
555 it('Should correctly have cleaned up the live files', async function () {
558 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
562 describe('Live views', function () {
563 let liveVideoId: string
564 let command: FfmpegCommand
566 async function countViews (expected: number) {
567 for (const server of servers) {
568 const res = await getVideo(server.url, liveVideoId)
569 const video: VideoDetails = res.body
571 expect(video.views).to.equal(expected)
575 before(async function () {
578 const liveAttributes = {
580 channelId: servers[0].videoChannel.id,
581 privacy: VideoPrivacy.PUBLIC
584 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
585 liveVideoId = res.body.video.uuid
587 command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
588 await waitUntilLivePublishedOnAllServers(liveVideoId)
589 await waitJobs(servers)
592 it('Should display no views for a live', async function () {
596 it('Should view a live twice and display 1 view', async function () {
599 await viewVideo(servers[0].url, liveVideoId)
600 await viewVideo(servers[0].url, liveVideoId)
604 await waitJobs(servers)
609 it('Should wait and display 0 views', async function () {
613 await waitJobs(servers)
618 it('Should view a live on a remote and on local and display 2 views', async function () {
621 await viewVideo(servers[0].url, liveVideoId)
622 await viewVideo(servers[1].url, liveVideoId)
623 await viewVideo(servers[1].url, liveVideoId)
626 await waitJobs(servers)
631 after(async function () {
632 await stopFfmpeg(command)
636 describe('Live socket messages', function () {
638 async function createLiveWrapper () {
639 const liveAttributes = {
641 channelId: servers[0].videoChannel.id,
642 privacy: VideoPrivacy.PUBLIC
645 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
646 return res.body.video.uuid
649 it('Should correctly send a message when the live starts and ends', async function () {
652 const localStateChanges: VideoState[] = []
653 const remoteStateChanges: VideoState[] = []
655 const liveVideoUUID = await createLiveWrapper()
656 await waitJobs(servers)
659 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
661 const localSocket = getLiveNotificationSocket(servers[0].url)
662 localSocket.on('state-change', data => localStateChanges.push(data.state))
663 localSocket.emit('subscribe', { videoId })
667 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
669 const remoteSocket = getLiveNotificationSocket(servers[1].url)
670 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
671 remoteSocket.emit('subscribe', { videoId })
674 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
676 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
677 await waitJobs(servers)
679 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
680 expect(stateChanges).to.have.length.at.least(1)
681 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.PUBLISHED)
684 await stopFfmpeg(command)
686 for (const server of servers) {
687 await waitUntilLiveEnded(server.url, server.accessToken, liveVideoUUID)
689 await waitJobs(servers)
691 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
692 expect(stateChanges).to.have.length.at.least(2)
693 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.LIVE_ENDED)
697 it('Should correctly send views change notification', async function () {
700 let localLastVideoViews = 0
701 let remoteLastVideoViews = 0
703 const liveVideoUUID = await createLiveWrapper()
704 await waitJobs(servers)
707 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
709 const localSocket = getLiveNotificationSocket(servers[0].url)
710 localSocket.on('views-change', data => { localLastVideoViews = data.views })
711 localSocket.emit('subscribe', { videoId })
715 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
717 const remoteSocket = getLiveNotificationSocket(servers[1].url)
718 remoteSocket.on('views-change', data => { remoteLastVideoViews = data.views })
719 remoteSocket.emit('subscribe', { videoId })
722 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
724 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
725 await waitJobs(servers)
727 expect(localLastVideoViews).to.equal(0)
728 expect(remoteLastVideoViews).to.equal(0)
730 await viewVideo(servers[0].url, liveVideoUUID)
731 await viewVideo(servers[1].url, liveVideoUUID)
733 await waitJobs(servers)
735 await waitJobs(servers)
737 expect(localLastVideoViews).to.equal(2)
738 expect(remoteLastVideoViews).to.equal(2)
740 await stopFfmpeg(command)
743 it('Should not receive a notification after unsubscribe', async function () {
746 const stateChanges: VideoState[] = []
748 const liveVideoUUID = await createLiveWrapper()
749 await waitJobs(servers)
751 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
753 const socket = getLiveNotificationSocket(servers[0].url)
754 socket.on('state-change', data => stateChanges.push(data.state))
755 socket.emit('subscribe', { videoId })
757 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
759 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
760 await waitJobs(servers)
762 expect(stateChanges).to.have.lengthOf(1)
763 socket.emit('unsubscribe', { videoId })
765 await stopFfmpeg(command)
766 await waitJobs(servers)
768 expect(stateChanges).to.have.lengthOf(1)
772 describe('After a server restart', function () {
773 let liveVideoId: string
774 let liveVideoReplayId: string
776 async function createLiveWrapper (saveReplay: boolean) {
777 const liveAttributes = {
779 channelId: servers[0].videoChannel.id,
780 privacy: VideoPrivacy.PUBLIC,
784 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
785 return res.body.video.uuid
788 before(async function () {
791 liveVideoId = await createLiveWrapper(false)
792 liveVideoReplayId = await createLiveWrapper(true)
795 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId),
796 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoReplayId)
800 waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoId),
801 waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoReplayId)
804 await waitUntilLiveSegmentGeneration(servers[0], liveVideoId, 0, 2)
805 await waitUntilLiveSegmentGeneration(servers[0], liveVideoReplayId, 0, 2)
807 await killallServers([ servers[0] ])
808 await reRunServer(servers[0])
813 it('Should cleanup lives', async function () {
816 await waitUntilLiveEnded(servers[0].url, servers[0].accessToken, liveVideoId)
819 it('Should save a live replay', async function () {
822 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoReplayId)
826 after(async function () {
827 await cleanupTests(servers)