1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { getLiveNotificationSocket } from '@shared/extra-utils/socket/socket-io'
6 import { LiveVideo, LiveVideoCreate, Video, VideoDetails, VideoPrivacy, VideoState, VideoStreamingPlaylistType } from '@shared/models'
10 checkResolutionsInMasterPlaylist,
14 flushAndRunMultipleServers,
22 sendRTMPStreamInVideo,
24 setAccessTokensToServers,
25 setDefaultVideoChannel,
27 testFfmpegStreamError,
29 updateCustomSubConfig,
33 } from '../../../../shared/extra-utils'
35 const expect = chai.expect
37 describe('Test live', function () {
38 let servers: ServerInfo[] = []
40 before(async function () {
43 servers = await flushAndRunMultipleServers(2)
45 // Get the access tokens
46 await setAccessTokensToServers(servers)
47 await setDefaultVideoChannel(servers)
49 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
59 // Server 1 and server 2 follow each other
60 await doubleFollow(servers[0], servers[1])
63 describe('Live creation, update and delete', function () {
64 let liveVideoUUID: string
66 it('Should create a live with the appropriate parameters', async function () {
69 const attributes: LiveVideoCreate = {
73 description: 'super live description',
74 support: 'support field',
75 channelId: servers[0].videoChannel.id,
77 waitTranscoding: false,
78 name: 'my super live',
79 tags: [ 'tag1', 'tag2' ],
80 commentsEnabled: false,
81 downloadEnabled: false,
83 privacy: VideoPrivacy.PUBLIC,
84 previewfile: 'video_short1-preview.webm.jpg',
85 thumbnailfile: 'video_short1.webm.jpg'
88 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
89 liveVideoUUID = res.body.video.uuid
91 await waitJobs(servers)
93 for (const server of servers) {
94 const resVideo = await getVideo(server.url, liveVideoUUID)
95 const video: VideoDetails = resVideo.body
97 expect(video.category.id).to.equal(1)
98 expect(video.licence.id).to.equal(2)
99 expect(video.language.id).to.equal('fr')
100 expect(video.description).to.equal('super live description')
101 expect(video.support).to.equal('support field')
103 expect(video.channel.name).to.equal(servers[0].videoChannel.name)
104 expect(video.channel.host).to.equal(servers[0].videoChannel.host)
106 expect(video.nsfw).to.be.false
107 expect(video.waitTranscoding).to.be.false
108 expect(video.name).to.equal('my super live')
109 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
110 expect(video.commentsEnabled).to.be.false
111 expect(video.downloadEnabled).to.be.false
112 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
114 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
115 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
117 const resLive = await getLive(server.url, server.accessToken, liveVideoUUID)
118 const live: LiveVideo = resLive.body
120 if (server.url === servers[0].url) {
121 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':1936/live')
122 expect(live.streamKey).to.not.be.empty
124 expect(live.rtmpUrl).to.be.null
125 expect(live.streamKey).to.be.null
128 expect(live.saveReplay).to.be.true
132 it('Should have a default preview and thumbnail', async function () {
135 const attributes: LiveVideoCreate = {
136 name: 'default live thumbnail',
137 channelId: servers[0].videoChannel.id,
138 privacy: VideoPrivacy.UNLISTED,
142 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
143 const videoId = res.body.video.uuid
145 await waitJobs(servers)
147 for (const server of servers) {
148 const resVideo = await getVideo(server.url, videoId)
149 const video: VideoDetails = resVideo.body
151 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
152 expect(video.nsfw).to.be.true
154 await makeRawRequest(server.url + video.thumbnailPath, 200)
155 await makeRawRequest(server.url + video.previewPath, 200)
159 it('Should not have the live listed since nobody streams into', async function () {
160 for (const server of servers) {
161 const res = await getVideosList(server.url)
163 expect(res.body.total).to.equal(0)
164 expect(res.body.data).to.have.lengthOf(0)
168 it('Should not be able to update a live of another server', async function () {
169 await updateLive(servers[1].url, servers[1].accessToken, liveVideoUUID, { saveReplay: false }, 403)
172 it('Should update the live', async function () {
175 await updateLive(servers[0].url, servers[0].accessToken, liveVideoUUID, { saveReplay: false })
176 await waitJobs(servers)
179 it('Have the live updated', async function () {
180 for (const server of servers) {
181 const res = await getLive(server.url, server.accessToken, liveVideoUUID)
182 const live: LiveVideo = res.body
184 if (server.url === servers[0].url) {
185 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':1936/live')
186 expect(live.streamKey).to.not.be.empty
188 expect(live.rtmpUrl).to.be.null
189 expect(live.streamKey).to.be.null
192 expect(live.saveReplay).to.be.false
196 it('Delete the live', async function () {
199 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
200 await waitJobs(servers)
203 it('Should have the live deleted', async function () {
204 for (const server of servers) {
205 await getVideo(server.url, liveVideoUUID, 404)
206 await getLive(server.url, server.accessToken, liveVideoUUID, 404)
211 describe('Stream checks', function () {
212 let liveVideo: LiveVideo & VideoDetails
216 rtmpUrl = 'rtmp://' + servers[0].hostname + ':1936'
219 async function createLiveWrapper () {
220 const liveAttributes = {
222 channelId: servers[0].videoChannel.id,
223 privacy: VideoPrivacy.PUBLIC,
227 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
228 const uuid = res.body.video.uuid
230 const resLive = await getLive(servers[0].url, servers[0].accessToken, uuid)
231 const resVideo = await getVideo(servers[0].url, uuid)
233 return Object.assign(resVideo.body, resLive.body) as LiveVideo & VideoDetails
236 it('Should not allow a stream without the appropriate path', async function () {
239 liveVideo = await createLiveWrapper()
241 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
242 await testFfmpegStreamError(command, true)
245 it('Should not allow a stream without the appropriate stream key', async function () {
248 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
249 await testFfmpegStreamError(command, true)
252 it('Should succeed with the correct params', async function () {
255 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
256 await testFfmpegStreamError(command, false)
259 it('Should not allow a stream on a live that was blacklisted', async function () {
262 liveVideo = await createLiveWrapper()
264 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideo.uuid)
266 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
267 await testFfmpegStreamError(command, true)
270 it('Should not allow a stream on a live that was deleted', async function () {
273 liveVideo = await createLiveWrapper()
275 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid)
277 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
278 await testFfmpegStreamError(command, true)
282 describe('Live transcoding', function () {
283 let liveVideoId: string
285 async function createLiveWrapper (saveReplay: boolean) {
286 const liveAttributes = {
288 channelId: servers[0].videoChannel.id,
289 privacy: VideoPrivacy.PUBLIC,
293 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
294 return res.body.video.uuid
297 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
298 for (const server of servers) {
299 const resList = await getVideosList(server.url)
300 const videos: Video[] = resList.body.data
302 expect(videos.find(v => v.uuid === liveVideoId)).to.exist
304 const resVideo = await getVideo(server.url, liveVideoId)
305 const video: VideoDetails = resVideo.body
307 expect(video.streamingPlaylists).to.have.lengthOf(1)
309 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
310 expect(hlsPlaylist).to.exist
312 // Only finite files are displayed
313 expect(hlsPlaylist.files).to.have.lengthOf(0)
315 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions)
319 function updateConf (resolutions: number[]) {
320 return updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
328 '240p': resolutions.includes(240),
329 '360p': resolutions.includes(360),
330 '480p': resolutions.includes(480),
331 '720p': resolutions.includes(720),
332 '1080p': resolutions.includes(1080),
333 '2160p': resolutions.includes(2160)
340 before(async function () {
344 it('Should enable transcoding without additional resolutions', async function () {
347 liveVideoId = await createLiveWrapper(false)
349 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
350 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
351 await waitJobs(servers)
353 await testVideoResolutions(liveVideoId, [ 720 ])
355 await stopFfmpeg(command)
358 it('Should enable transcoding with some resolutions', async function () {
361 const resolutions = [ 240, 480 ]
362 await updateConf(resolutions)
363 liveVideoId = await createLiveWrapper(false)
365 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
366 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
367 await waitJobs(servers)
369 await testVideoResolutions(liveVideoId, resolutions)
371 await stopFfmpeg(command)
374 it('Should enable transcoding with some resolutions and correctly save them', async function () {
377 const resolutions = [ 240, 360, 720 ]
378 await updateConf(resolutions)
379 liveVideoId = await createLiveWrapper(true)
381 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
382 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
383 await waitJobs(servers)
385 await testVideoResolutions(liveVideoId, resolutions)
387 await stopFfmpeg(command)
389 await waitJobs(servers)
391 for (const server of servers) {
392 const resVideo = await getVideo(server.url, liveVideoId)
393 const video: VideoDetails = resVideo.body
395 expect(video.duration).to.be.greaterThan(1)
396 expect(video.files).to.have.lengthOf(0)
398 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
400 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
402 for (const resolution of resolutions) {
403 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
405 expect(file).to.exist
406 expect(file.fps).to.equal(25)
407 expect(file.size).to.be.greaterThan(1)
409 await makeRawRequest(file.torrentUrl, 200)
410 await makeRawRequest(file.fileUrl, 200)
415 it('Should correctly have cleaned up the live files', async function () {
418 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
422 describe('Live socket messages', function () {
424 async function createLiveWrapper () {
425 const liveAttributes = {
427 channelId: servers[0].videoChannel.id,
428 privacy: VideoPrivacy.PUBLIC
431 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
432 return res.body.video.uuid
435 it('Should correctly send a message when the live starts and ends', async function () {
438 const localStateChanges: VideoState[] = []
439 const remoteStateChanges: VideoState[] = []
441 const liveVideoUUID = await createLiveWrapper()
442 await waitJobs(servers)
445 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
447 const localSocket = getLiveNotificationSocket(servers[0].url)
448 localSocket.on('state-change', data => localStateChanges.push(data.state))
449 localSocket.emit('subscribe', { videoId })
453 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
455 const remoteSocket = getLiveNotificationSocket(servers[1].url)
456 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
457 remoteSocket.emit('subscribe', { videoId })
460 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
461 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoUUID)
462 await waitJobs(servers)
464 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
465 expect(stateChanges).to.have.lengthOf(1)
466 expect(stateChanges[0]).to.equal(VideoState.PUBLISHED)
469 await stopFfmpeg(command)
470 await waitJobs(servers)
472 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
473 expect(stateChanges).to.have.lengthOf(2)
474 expect(stateChanges[1]).to.equal(VideoState.LIVE_ENDED)
478 it('Should not receive a notification after unsubscribe', async function () {
481 const stateChanges: VideoState[] = []
483 const liveVideoUUID = await createLiveWrapper()
484 await waitJobs(servers)
486 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
488 const socket = getLiveNotificationSocket(servers[0].url)
489 socket.on('state-change', data => stateChanges.push(data.state))
490 socket.emit('subscribe', { videoId })
492 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
493 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoUUID)
494 await waitJobs(servers)
496 expect(stateChanges).to.have.lengthOf(1)
497 socket.emit('unsubscribe', { videoId })
499 await stopFfmpeg(command)
500 await waitJobs(servers)
502 expect(stateChanges).to.have.lengthOf(1)
506 after(async function () {
507 await cleanupTests(servers)