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,
17 flushAndRunMultipleServers,
28 sendRTMPStreamInVideo,
30 setAccessTokensToServers,
31 setDefaultVideoChannel,
33 testFfmpegStreamError,
35 updateCustomSubConfig,
42 } from '../../../../shared/extra-utils'
44 const expect = chai.expect
46 describe('Test live', function () {
47 let servers: ServerInfo[] = []
49 before(async function () {
52 servers = await flushAndRunMultipleServers(2)
54 // Get the access tokens
55 await setAccessTokensToServers(servers)
56 await setDefaultVideoChannel(servers)
58 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
68 // Server 1 and server 2 follow each other
69 await doubleFollow(servers[0], servers[1])
72 describe('Live creation, update and delete', function () {
73 let liveVideoUUID: string
75 it('Should create a live with the appropriate parameters', async function () {
78 const attributes: LiveVideoCreate = {
82 description: 'super live description',
83 support: 'support field',
84 channelId: servers[0].videoChannel.id,
86 waitTranscoding: false,
87 name: 'my super live',
88 tags: [ 'tag1', 'tag2' ],
89 commentsEnabled: false,
90 downloadEnabled: false,
92 privacy: VideoPrivacy.PUBLIC,
93 previewfile: 'video_short1-preview.webm.jpg',
94 thumbnailfile: 'video_short1.webm.jpg'
97 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
98 liveVideoUUID = res.body.video.uuid
100 await waitJobs(servers)
102 for (const server of servers) {
103 const resVideo = await getVideo(server.url, liveVideoUUID)
104 const video: VideoDetails = resVideo.body
106 expect(video.category.id).to.equal(1)
107 expect(video.licence.id).to.equal(2)
108 expect(video.language.id).to.equal('fr')
109 expect(video.description).to.equal('super live description')
110 expect(video.support).to.equal('support field')
112 expect(video.channel.name).to.equal(servers[0].videoChannel.name)
113 expect(video.channel.host).to.equal(servers[0].videoChannel.host)
115 expect(video.nsfw).to.be.false
116 expect(video.waitTranscoding).to.be.false
117 expect(video.name).to.equal('my super live')
118 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
119 expect(video.commentsEnabled).to.be.false
120 expect(video.downloadEnabled).to.be.false
121 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
123 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
124 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
126 const resLive = await getLive(server.url, server.accessToken, liveVideoUUID)
127 const live: LiveVideo = resLive.body
129 if (server.url === servers[0].url) {
130 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
131 expect(live.streamKey).to.not.be.empty
133 expect(live.rtmpUrl).to.be.null
134 expect(live.streamKey).to.be.null
137 expect(live.saveReplay).to.be.true
141 it('Should have a default preview and thumbnail', async function () {
144 const attributes: LiveVideoCreate = {
145 name: 'default live thumbnail',
146 channelId: servers[0].videoChannel.id,
147 privacy: VideoPrivacy.UNLISTED,
151 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
152 const videoId = res.body.video.uuid
154 await waitJobs(servers)
156 for (const server of servers) {
157 const resVideo = await getVideo(server.url, videoId)
158 const video: VideoDetails = resVideo.body
160 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
161 expect(video.nsfw).to.be.true
163 await makeRawRequest(server.url + video.thumbnailPath, 200)
164 await makeRawRequest(server.url + video.previewPath, 200)
168 it('Should not have the live listed since nobody streams into', async function () {
169 for (const server of servers) {
170 const res = await getVideosList(server.url)
172 expect(res.body.total).to.equal(0)
173 expect(res.body.data).to.have.lengthOf(0)
177 it('Should not be able to update a live of another server', async function () {
178 await updateLive(servers[1].url, servers[1].accessToken, liveVideoUUID, { saveReplay: false }, 403)
181 it('Should update the live', async function () {
184 await updateLive(servers[0].url, servers[0].accessToken, liveVideoUUID, { saveReplay: false })
185 await waitJobs(servers)
188 it('Have the live updated', async function () {
189 for (const server of servers) {
190 const res = await getLive(server.url, server.accessToken, liveVideoUUID)
191 const live: LiveVideo = res.body
193 if (server.url === servers[0].url) {
194 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
195 expect(live.streamKey).to.not.be.empty
197 expect(live.rtmpUrl).to.be.null
198 expect(live.streamKey).to.be.null
201 expect(live.saveReplay).to.be.false
205 it('Delete the live', async function () {
208 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
209 await waitJobs(servers)
212 it('Should have the live deleted', async function () {
213 for (const server of servers) {
214 await getVideo(server.url, liveVideoUUID, 404)
215 await getLive(server.url, server.accessToken, liveVideoUUID, 404)
220 describe('Stream checks', function () {
221 let liveVideo: LiveVideo & VideoDetails
225 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
228 async function createLiveWrapper () {
229 const liveAttributes = {
231 channelId: servers[0].videoChannel.id,
232 privacy: VideoPrivacy.PUBLIC,
236 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
237 const uuid = res.body.video.uuid
239 const resLive = await getLive(servers[0].url, servers[0].accessToken, uuid)
240 const resVideo = await getVideo(servers[0].url, uuid)
242 return Object.assign(resVideo.body, resLive.body) as LiveVideo & VideoDetails
245 it('Should not allow a stream without the appropriate path', async function () {
248 liveVideo = await createLiveWrapper()
250 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
251 await testFfmpegStreamError(command, true)
254 it('Should not allow a stream without the appropriate stream key', async function () {
257 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
258 await testFfmpegStreamError(command, true)
261 it('Should succeed with the correct params', async function () {
264 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
265 await testFfmpegStreamError(command, false)
268 it('Should not allow a stream on a live that was blacklisted', async function () {
271 liveVideo = await createLiveWrapper()
273 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideo.uuid)
275 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
276 await testFfmpegStreamError(command, true)
279 it('Should not allow a stream on a live that was deleted', async function () {
282 liveVideo = await createLiveWrapper()
284 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid)
286 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
287 await testFfmpegStreamError(command, true)
291 describe('Live transcoding', function () {
292 let liveVideoId: string
294 async function createLiveWrapper (saveReplay: boolean) {
295 const liveAttributes = {
297 channelId: servers[0].videoChannel.id,
298 privacy: VideoPrivacy.PUBLIC,
302 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
303 return res.body.video.uuid
306 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
307 for (const server of servers) {
308 const resList = await getVideosList(server.url)
309 const videos: Video[] = resList.body.data
311 expect(videos.find(v => v.uuid === liveVideoId)).to.exist
313 const resVideo = await getVideo(server.url, liveVideoId)
314 const video: VideoDetails = resVideo.body
316 expect(video.streamingPlaylists).to.have.lengthOf(1)
318 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
319 expect(hlsPlaylist).to.exist
321 // Only finite files are displayed
322 expect(hlsPlaylist.files).to.have.lengthOf(0)
324 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions)
326 for (let i = 0; i < resolutions.length; i++) {
327 const segmentName = `${i}-000001.ts`
328 await waitUntilLog(servers[0], `${video.uuid}/${segmentName}`, 1, false)
330 const res = await getPlaylist(`${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`)
331 const subPlaylist = res.text
333 expect(subPlaylist).to.contain(segmentName)
335 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
336 await checkLiveSegmentHash(baseUrlAndPath, video.uuid, segmentName, hlsPlaylist)
341 function updateConf (resolutions: number[]) {
342 return updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
350 '240p': resolutions.includes(240),
351 '360p': resolutions.includes(360),
352 '480p': resolutions.includes(480),
353 '720p': resolutions.includes(720),
354 '1080p': resolutions.includes(1080),
355 '2160p': resolutions.includes(2160)
362 before(async function () {
366 it('Should enable transcoding without additional resolutions', async function () {
369 liveVideoId = await createLiveWrapper(false)
371 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
372 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
373 await waitJobs(servers)
375 await testVideoResolutions(liveVideoId, [ 720 ])
377 await stopFfmpeg(command)
380 it('Should enable transcoding with some resolutions', async function () {
383 const resolutions = [ 240, 480 ]
384 await updateConf(resolutions)
385 liveVideoId = await createLiveWrapper(false)
387 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
388 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
389 await waitJobs(servers)
391 await testVideoResolutions(liveVideoId, resolutions)
393 await stopFfmpeg(command)
396 it('Should enable transcoding with some resolutions and correctly save them', async function () {
399 const resolutions = [ 240, 360, 720 ]
400 await updateConf(resolutions)
401 liveVideoId = await createLiveWrapper(true)
403 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
404 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
405 await waitJobs(servers)
407 await testVideoResolutions(liveVideoId, resolutions)
409 await stopFfmpeg(command)
411 await waitJobs(servers)
413 for (const server of servers) {
414 const resVideo = await getVideo(server.url, liveVideoId)
415 const video: VideoDetails = resVideo.body
417 expect(video.duration).to.be.greaterThan(1)
418 expect(video.files).to.have.lengthOf(0)
420 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
422 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
424 for (const resolution of resolutions) {
425 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
427 expect(file).to.exist
428 expect(file.fps).to.equal(25)
429 expect(file.size).to.be.greaterThan(1)
431 await makeRawRequest(file.torrentUrl, 200)
432 await makeRawRequest(file.fileUrl, 200)
437 it('Should correctly have cleaned up the live files', async function () {
440 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
444 describe('Live views', function () {
445 let liveVideoId: string
446 let command: FfmpegCommand
448 async function countViews (expected: number) {
449 for (const server of servers) {
450 const res = await getVideo(server.url, liveVideoId)
451 const video: VideoDetails = res.body
453 expect(video.views).to.equal(expected)
457 before(async function () {
460 const liveAttributes = {
462 channelId: servers[0].videoChannel.id,
463 privacy: VideoPrivacy.PUBLIC
466 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
467 liveVideoId = res.body.video.uuid
469 command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
470 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
471 await waitJobs(servers)
474 it('Should display no views for a live', async function () {
478 it('Should view a live twice and display 1 view', async function () {
481 await viewVideo(servers[0].url, liveVideoId)
482 await viewVideo(servers[0].url, liveVideoId)
486 await waitJobs(servers)
491 it('Should wait and display 0 views', async function () {
495 await waitJobs(servers)
500 it('Should view a live on a remote and on local and display 2 views', async function () {
503 await viewVideo(servers[0].url, liveVideoId)
504 await viewVideo(servers[1].url, liveVideoId)
505 await viewVideo(servers[1].url, liveVideoId)
508 await waitJobs(servers)
513 after(async function () {
514 await stopFfmpeg(command)
518 describe('Live socket messages', function () {
520 async function createLiveWrapper () {
521 const liveAttributes = {
523 channelId: servers[0].videoChannel.id,
524 privacy: VideoPrivacy.PUBLIC
527 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
528 return res.body.video.uuid
531 it('Should correctly send a message when the live starts and ends', async function () {
534 const localStateChanges: VideoState[] = []
535 const remoteStateChanges: VideoState[] = []
537 const liveVideoUUID = await createLiveWrapper()
538 await waitJobs(servers)
541 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
543 const localSocket = getLiveNotificationSocket(servers[0].url)
544 localSocket.on('state-change', data => localStateChanges.push(data.state))
545 localSocket.emit('subscribe', { videoId })
549 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
551 const remoteSocket = getLiveNotificationSocket(servers[1].url)
552 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
553 remoteSocket.emit('subscribe', { videoId })
556 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
557 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoUUID)
558 await waitJobs(servers)
560 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
561 expect(stateChanges).to.have.lengthOf(1)
562 expect(stateChanges[0]).to.equal(VideoState.PUBLISHED)
565 await stopFfmpeg(command)
566 await waitJobs(servers)
568 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
569 expect(stateChanges).to.have.lengthOf(2)
570 expect(stateChanges[1]).to.equal(VideoState.LIVE_ENDED)
574 it('Should not receive a notification after unsubscribe', async function () {
577 const stateChanges: VideoState[] = []
579 const liveVideoUUID = await createLiveWrapper()
580 await waitJobs(servers)
582 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
584 const socket = getLiveNotificationSocket(servers[0].url)
585 socket.on('state-change', data => stateChanges.push(data.state))
586 socket.emit('subscribe', { videoId })
588 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
589 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoUUID)
590 await waitJobs(servers)
592 expect(stateChanges).to.have.lengthOf(1)
593 socket.emit('unsubscribe', { videoId })
595 await stopFfmpeg(command)
596 await waitJobs(servers)
598 expect(stateChanges).to.have.lengthOf(1)
602 describe('After a server restart', function () {
603 let liveVideoId: string
604 let liveVideoReplayId: string
606 async function createLiveWrapper (saveReplay: boolean) {
607 const liveAttributes = {
609 channelId: servers[0].videoChannel.id,
610 privacy: VideoPrivacy.PUBLIC,
614 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
615 return res.body.video.uuid
618 before(async function () {
621 liveVideoId = await createLiveWrapper(false)
622 liveVideoReplayId = await createLiveWrapper(true)
625 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId),
626 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoReplayId)
630 waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId),
631 waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoReplayId)
634 await killallServers([ servers[0] ])
635 await reRunServer(servers[0])
640 it('Should cleanup lives', async function () {
643 const res = await getVideo(servers[0].url, liveVideoId)
644 const video: VideoDetails = res.body
646 expect(video.state.id).to.equal(VideoState.LIVE_ENDED)
649 it('Should save a live replay', async function () {
652 await waitJobs(servers)
654 const res = await getVideo(servers[0].url, liveVideoReplayId)
655 const video: VideoDetails = res.body
657 expect(video.state.id).to.equal(VideoState.PUBLISHED)
661 after(async function () {
662 await cleanupTests(servers)