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 { getLiveNotificationSocket } from '@shared/extra-utils/socket/socket-io'
7 import { LiveVideo, LiveVideoCreate, Video, VideoDetails, VideoPrivacy, VideoState, VideoStreamingPlaylistType } from '@shared/models'
12 checkResolutionsInMasterPlaylist,
16 flushAndRunMultipleServers,
27 sendRTMPStreamInVideo,
29 setAccessTokensToServers,
30 setDefaultVideoChannel,
32 testFfmpegStreamError,
34 updateCustomSubConfig,
41 } from '../../../../shared/extra-utils'
43 const expect = chai.expect
45 describe('Test live', function () {
46 let servers: ServerInfo[] = []
48 before(async function () {
51 servers = await flushAndRunMultipleServers(2)
53 // Get the access tokens
54 await setAccessTokensToServers(servers)
55 await setDefaultVideoChannel(servers)
57 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
67 // Server 1 and server 2 follow each other
68 await doubleFollow(servers[0], servers[1])
71 describe('Live creation, update and delete', function () {
72 let liveVideoUUID: string
74 it('Should create a live with the appropriate parameters', async function () {
77 const attributes: LiveVideoCreate = {
81 description: 'super live description',
82 support: 'support field',
83 channelId: servers[0].videoChannel.id,
85 waitTranscoding: false,
86 name: 'my super live',
87 tags: [ 'tag1', 'tag2' ],
88 commentsEnabled: false,
89 downloadEnabled: false,
91 privacy: VideoPrivacy.PUBLIC,
92 previewfile: 'video_short1-preview.webm.jpg',
93 thumbnailfile: 'video_short1.webm.jpg'
96 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
97 liveVideoUUID = res.body.video.uuid
99 await waitJobs(servers)
101 for (const server of servers) {
102 const resVideo = await getVideo(server.url, liveVideoUUID)
103 const video: VideoDetails = resVideo.body
105 expect(video.category.id).to.equal(1)
106 expect(video.licence.id).to.equal(2)
107 expect(video.language.id).to.equal('fr')
108 expect(video.description).to.equal('super live description')
109 expect(video.support).to.equal('support field')
111 expect(video.channel.name).to.equal(servers[0].videoChannel.name)
112 expect(video.channel.host).to.equal(servers[0].videoChannel.host)
114 expect(video.nsfw).to.be.false
115 expect(video.waitTranscoding).to.be.false
116 expect(video.name).to.equal('my super live')
117 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
118 expect(video.commentsEnabled).to.be.false
119 expect(video.downloadEnabled).to.be.false
120 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
122 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
123 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
125 const resLive = await getLive(server.url, server.accessToken, liveVideoUUID)
126 const live: LiveVideo = resLive.body
128 if (server.url === servers[0].url) {
129 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
130 expect(live.streamKey).to.not.be.empty
132 expect(live.rtmpUrl).to.be.null
133 expect(live.streamKey).to.be.null
136 expect(live.saveReplay).to.be.true
140 it('Should have a default preview and thumbnail', async function () {
143 const attributes: LiveVideoCreate = {
144 name: 'default live thumbnail',
145 channelId: servers[0].videoChannel.id,
146 privacy: VideoPrivacy.UNLISTED,
150 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
151 const videoId = res.body.video.uuid
153 await waitJobs(servers)
155 for (const server of servers) {
156 const resVideo = await getVideo(server.url, videoId)
157 const video: VideoDetails = resVideo.body
159 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
160 expect(video.nsfw).to.be.true
162 await makeRawRequest(server.url + video.thumbnailPath, 200)
163 await makeRawRequest(server.url + video.previewPath, 200)
167 it('Should not have the live listed since nobody streams into', async function () {
168 for (const server of servers) {
169 const res = await getVideosList(server.url)
171 expect(res.body.total).to.equal(0)
172 expect(res.body.data).to.have.lengthOf(0)
176 it('Should not be able to update a live of another server', async function () {
177 await updateLive(servers[1].url, servers[1].accessToken, liveVideoUUID, { saveReplay: false }, 403)
180 it('Should update the live', async function () {
183 await updateLive(servers[0].url, servers[0].accessToken, liveVideoUUID, { saveReplay: false })
184 await waitJobs(servers)
187 it('Have the live updated', async function () {
188 for (const server of servers) {
189 const res = await getLive(server.url, server.accessToken, liveVideoUUID)
190 const live: LiveVideo = res.body
192 if (server.url === servers[0].url) {
193 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
194 expect(live.streamKey).to.not.be.empty
196 expect(live.rtmpUrl).to.be.null
197 expect(live.streamKey).to.be.null
200 expect(live.saveReplay).to.be.false
204 it('Delete the live', async function () {
207 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
208 await waitJobs(servers)
211 it('Should have the live deleted', async function () {
212 for (const server of servers) {
213 await getVideo(server.url, liveVideoUUID, 404)
214 await getLive(server.url, server.accessToken, liveVideoUUID, 404)
219 describe('Stream checks', function () {
220 let liveVideo: LiveVideo & VideoDetails
224 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
227 async function createLiveWrapper () {
228 const liveAttributes = {
230 channelId: servers[0].videoChannel.id,
231 privacy: VideoPrivacy.PUBLIC,
235 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
236 const uuid = res.body.video.uuid
238 const resLive = await getLive(servers[0].url, servers[0].accessToken, uuid)
239 const resVideo = await getVideo(servers[0].url, uuid)
241 return Object.assign(resVideo.body, resLive.body) as LiveVideo & VideoDetails
244 it('Should not allow a stream without the appropriate path', async function () {
247 liveVideo = await createLiveWrapper()
249 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
250 await testFfmpegStreamError(command, true)
253 it('Should not allow a stream without the appropriate stream key', async function () {
256 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
257 await testFfmpegStreamError(command, true)
260 it('Should succeed with the correct params', async function () {
263 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
264 await testFfmpegStreamError(command, false)
267 it('Should not allow a stream on a live that was blacklisted', async function () {
270 liveVideo = await createLiveWrapper()
272 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideo.uuid)
274 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
275 await testFfmpegStreamError(command, true)
278 it('Should not allow a stream on a live that was deleted', async function () {
281 liveVideo = await createLiveWrapper()
283 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid)
285 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
286 await testFfmpegStreamError(command, true)
290 describe('Live transcoding', function () {
291 let liveVideoId: string
293 async function createLiveWrapper (saveReplay: boolean) {
294 const liveAttributes = {
296 channelId: servers[0].videoChannel.id,
297 privacy: VideoPrivacy.PUBLIC,
301 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
302 return res.body.video.uuid
305 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
306 for (const server of servers) {
307 const resList = await getVideosList(server.url)
308 const videos: Video[] = resList.body.data
310 expect(videos.find(v => v.uuid === liveVideoId)).to.exist
312 const resVideo = await getVideo(server.url, liveVideoId)
313 const video: VideoDetails = resVideo.body
315 expect(video.streamingPlaylists).to.have.lengthOf(1)
317 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
318 expect(hlsPlaylist).to.exist
320 // Only finite files are displayed
321 expect(hlsPlaylist.files).to.have.lengthOf(0)
323 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions)
325 for (let i = 0; i < resolutions.length; i++) {
326 const segmentName = `${i}-000001.ts`
327 await waitUntilLog(servers[0], `${video.uuid}/${segmentName}`, 1, false)
329 const res = await getPlaylist(`${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`)
330 const subPlaylist = res.text
332 expect(subPlaylist).to.contain(segmentName)
334 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
335 await checkLiveSegmentHash(baseUrlAndPath, video.uuid, segmentName, hlsPlaylist)
340 function updateConf (resolutions: number[]) {
341 return updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
349 '240p': resolutions.includes(240),
350 '360p': resolutions.includes(360),
351 '480p': resolutions.includes(480),
352 '720p': resolutions.includes(720),
353 '1080p': resolutions.includes(1080),
354 '2160p': resolutions.includes(2160)
361 before(async function () {
365 it('Should enable transcoding without additional resolutions', async function () {
368 liveVideoId = await createLiveWrapper(false)
370 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
371 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
372 await waitJobs(servers)
374 await testVideoResolutions(liveVideoId, [ 720 ])
376 await stopFfmpeg(command)
379 it('Should enable transcoding with some resolutions', async function () {
382 const resolutions = [ 240, 480 ]
383 await updateConf(resolutions)
384 liveVideoId = await createLiveWrapper(false)
386 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
387 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
388 await waitJobs(servers)
390 await testVideoResolutions(liveVideoId, resolutions)
392 await stopFfmpeg(command)
395 it('Should enable transcoding with some resolutions and correctly save them', async function () {
398 const resolutions = [ 240, 360, 720 ]
399 await updateConf(resolutions)
400 liveVideoId = await createLiveWrapper(true)
402 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
403 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
404 await waitJobs(servers)
406 await testVideoResolutions(liveVideoId, resolutions)
408 await stopFfmpeg(command)
410 await waitJobs(servers)
412 for (const server of servers) {
413 const resVideo = await getVideo(server.url, liveVideoId)
414 const video: VideoDetails = resVideo.body
416 expect(video.duration).to.be.greaterThan(1)
417 expect(video.files).to.have.lengthOf(0)
419 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
421 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
423 for (const resolution of resolutions) {
424 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
426 expect(file).to.exist
427 expect(file.fps).to.equal(25)
428 expect(file.size).to.be.greaterThan(1)
430 await makeRawRequest(file.torrentUrl, 200)
431 await makeRawRequest(file.fileUrl, 200)
436 it('Should correctly have cleaned up the live files', async function () {
439 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
443 describe('Live views', function () {
444 let liveVideoId: string
445 let command: FfmpegCommand
447 async function countViews (expected: number) {
448 for (const server of servers) {
449 const res = await getVideo(server.url, liveVideoId)
450 const video: VideoDetails = res.body
452 expect(video.views).to.equal(expected)
456 before(async function () {
459 const liveAttributes = {
461 channelId: servers[0].videoChannel.id,
462 privacy: VideoPrivacy.PUBLIC
465 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
466 liveVideoId = res.body.video.uuid
468 command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
469 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
470 await waitJobs(servers)
473 it('Should display no views for a live', async function () {
477 it('Should view a live twice and display 1 view', async function () {
480 await viewVideo(servers[0].url, liveVideoId)
481 await viewVideo(servers[0].url, liveVideoId)
485 await waitJobs(servers)
490 it('Should wait and display 0 views', async function () {
494 await waitJobs(servers)
499 it('Should view a live on a remote and on local and display 2 views', async function () {
502 await viewVideo(servers[0].url, liveVideoId)
503 await viewVideo(servers[1].url, liveVideoId)
504 await viewVideo(servers[1].url, liveVideoId)
507 await waitJobs(servers)
512 after(async function () {
513 await stopFfmpeg(command)
517 describe('Live socket messages', function () {
519 async function createLiveWrapper () {
520 const liveAttributes = {
522 channelId: servers[0].videoChannel.id,
523 privacy: VideoPrivacy.PUBLIC
526 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
527 return res.body.video.uuid
530 it('Should correctly send a message when the live starts and ends', async function () {
533 const localStateChanges: VideoState[] = []
534 const remoteStateChanges: VideoState[] = []
536 const liveVideoUUID = await createLiveWrapper()
537 await waitJobs(servers)
540 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
542 const localSocket = getLiveNotificationSocket(servers[0].url)
543 localSocket.on('state-change', data => localStateChanges.push(data.state))
544 localSocket.emit('subscribe', { videoId })
548 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
550 const remoteSocket = getLiveNotificationSocket(servers[1].url)
551 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
552 remoteSocket.emit('subscribe', { videoId })
555 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
556 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoUUID)
557 await waitJobs(servers)
559 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
560 expect(stateChanges).to.have.lengthOf(1)
561 expect(stateChanges[0]).to.equal(VideoState.PUBLISHED)
564 await stopFfmpeg(command)
565 await waitJobs(servers)
567 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
568 expect(stateChanges).to.have.lengthOf(2)
569 expect(stateChanges[1]).to.equal(VideoState.LIVE_ENDED)
573 it('Should not receive a notification after unsubscribe', async function () {
576 const stateChanges: VideoState[] = []
578 const liveVideoUUID = await createLiveWrapper()
579 await waitJobs(servers)
581 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
583 const socket = getLiveNotificationSocket(servers[0].url)
584 socket.on('state-change', data => stateChanges.push(data.state))
585 socket.emit('subscribe', { videoId })
587 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
588 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoUUID)
589 await waitJobs(servers)
591 expect(stateChanges).to.have.lengthOf(1)
592 socket.emit('unsubscribe', { videoId })
594 await stopFfmpeg(command)
595 await waitJobs(servers)
597 expect(stateChanges).to.have.lengthOf(1)
601 describe('After a server restart', function () {
602 let liveVideoId: string
603 let liveVideoReplayId: string
605 async function createLiveWrapper (saveReplay: boolean) {
606 const liveAttributes = {
608 channelId: servers[0].videoChannel.id,
609 privacy: VideoPrivacy.PUBLIC,
613 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
614 return res.body.video.uuid
617 before(async function () {
620 liveVideoId = await createLiveWrapper(false)
621 liveVideoReplayId = await createLiveWrapper(true)
624 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId),
625 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoReplayId)
629 waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId),
630 waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoReplayId)
633 await killallServers([ servers[0] ])
634 await reRunServer(servers[0])
639 it('Should cleanup lives', async function () {
642 const res = await getVideo(servers[0].url, liveVideoId)
643 const video: VideoDetails = res.body
645 expect(video.state.id).to.equal(VideoState.LIVE_ENDED)
648 it('Should save a live replay', async function () {
651 await waitJobs(servers)
653 const res = await getVideo(servers[0].url, liveVideoReplayId)
654 const video: VideoDetails = res.body
656 expect(video.state.id).to.equal(VideoState.PUBLISHED)
660 after(async function () {
661 await cleanupTests(servers)