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,
35 } from '../../../../shared/extra-utils'
36 import { FfmpegCommand } from 'fluent-ffmpeg'
38 const expect = chai.expect
40 describe('Test live', function () {
41 let servers: ServerInfo[] = []
43 before(async function () {
46 servers = await flushAndRunMultipleServers(2)
48 // Get the access tokens
49 await setAccessTokensToServers(servers)
50 await setDefaultVideoChannel(servers)
52 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
62 // Server 1 and server 2 follow each other
63 await doubleFollow(servers[0], servers[1])
66 describe('Live creation, update and delete', function () {
67 let liveVideoUUID: string
69 it('Should create a live with the appropriate parameters', async function () {
72 const attributes: LiveVideoCreate = {
76 description: 'super live description',
77 support: 'support field',
78 channelId: servers[0].videoChannel.id,
80 waitTranscoding: false,
81 name: 'my super live',
82 tags: [ 'tag1', 'tag2' ],
83 commentsEnabled: false,
84 downloadEnabled: false,
86 privacy: VideoPrivacy.PUBLIC,
87 previewfile: 'video_short1-preview.webm.jpg',
88 thumbnailfile: 'video_short1.webm.jpg'
91 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
92 liveVideoUUID = res.body.video.uuid
94 await waitJobs(servers)
96 for (const server of servers) {
97 const resVideo = await getVideo(server.url, liveVideoUUID)
98 const video: VideoDetails = resVideo.body
100 expect(video.category.id).to.equal(1)
101 expect(video.licence.id).to.equal(2)
102 expect(video.language.id).to.equal('fr')
103 expect(video.description).to.equal('super live description')
104 expect(video.support).to.equal('support field')
106 expect(video.channel.name).to.equal(servers[0].videoChannel.name)
107 expect(video.channel.host).to.equal(servers[0].videoChannel.host)
109 expect(video.nsfw).to.be.false
110 expect(video.waitTranscoding).to.be.false
111 expect(video.name).to.equal('my super live')
112 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
113 expect(video.commentsEnabled).to.be.false
114 expect(video.downloadEnabled).to.be.false
115 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
117 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
118 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
120 const resLive = await getLive(server.url, server.accessToken, liveVideoUUID)
121 const live: LiveVideo = resLive.body
123 if (server.url === servers[0].url) {
124 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':1936/live')
125 expect(live.streamKey).to.not.be.empty
127 expect(live.rtmpUrl).to.be.null
128 expect(live.streamKey).to.be.null
131 expect(live.saveReplay).to.be.true
135 it('Should have a default preview and thumbnail', async function () {
138 const attributes: LiveVideoCreate = {
139 name: 'default live thumbnail',
140 channelId: servers[0].videoChannel.id,
141 privacy: VideoPrivacy.UNLISTED,
145 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
146 const videoId = res.body.video.uuid
148 await waitJobs(servers)
150 for (const server of servers) {
151 const resVideo = await getVideo(server.url, videoId)
152 const video: VideoDetails = resVideo.body
154 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
155 expect(video.nsfw).to.be.true
157 await makeRawRequest(server.url + video.thumbnailPath, 200)
158 await makeRawRequest(server.url + video.previewPath, 200)
162 it('Should not have the live listed since nobody streams into', async function () {
163 for (const server of servers) {
164 const res = await getVideosList(server.url)
166 expect(res.body.total).to.equal(0)
167 expect(res.body.data).to.have.lengthOf(0)
171 it('Should not be able to update a live of another server', async function () {
172 await updateLive(servers[1].url, servers[1].accessToken, liveVideoUUID, { saveReplay: false }, 403)
175 it('Should update the live', async function () {
178 await updateLive(servers[0].url, servers[0].accessToken, liveVideoUUID, { saveReplay: false })
179 await waitJobs(servers)
182 it('Have the live updated', async function () {
183 for (const server of servers) {
184 const res = await getLive(server.url, server.accessToken, liveVideoUUID)
185 const live: LiveVideo = res.body
187 if (server.url === servers[0].url) {
188 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':1936/live')
189 expect(live.streamKey).to.not.be.empty
191 expect(live.rtmpUrl).to.be.null
192 expect(live.streamKey).to.be.null
195 expect(live.saveReplay).to.be.false
199 it('Delete the live', async function () {
202 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
203 await waitJobs(servers)
206 it('Should have the live deleted', async function () {
207 for (const server of servers) {
208 await getVideo(server.url, liveVideoUUID, 404)
209 await getLive(server.url, server.accessToken, liveVideoUUID, 404)
214 describe('Stream checks', function () {
215 let liveVideo: LiveVideo & VideoDetails
219 rtmpUrl = 'rtmp://' + servers[0].hostname + ':1936'
222 async function createLiveWrapper () {
223 const liveAttributes = {
225 channelId: servers[0].videoChannel.id,
226 privacy: VideoPrivacy.PUBLIC,
230 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
231 const uuid = res.body.video.uuid
233 const resLive = await getLive(servers[0].url, servers[0].accessToken, uuid)
234 const resVideo = await getVideo(servers[0].url, uuid)
236 return Object.assign(resVideo.body, resLive.body) as LiveVideo & VideoDetails
239 it('Should not allow a stream without the appropriate path', async function () {
242 liveVideo = await createLiveWrapper()
244 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
245 await testFfmpegStreamError(command, true)
248 it('Should not allow a stream without the appropriate stream key', async function () {
251 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
252 await testFfmpegStreamError(command, true)
255 it('Should succeed with the correct params', async function () {
258 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
259 await testFfmpegStreamError(command, false)
262 it('Should not allow a stream on a live that was blacklisted', async function () {
265 liveVideo = await createLiveWrapper()
267 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideo.uuid)
269 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
270 await testFfmpegStreamError(command, true)
273 it('Should not allow a stream on a live that was deleted', async function () {
276 liveVideo = await createLiveWrapper()
278 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid)
280 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
281 await testFfmpegStreamError(command, true)
285 describe('Live transcoding', function () {
286 let liveVideoId: string
288 async function createLiveWrapper (saveReplay: boolean) {
289 const liveAttributes = {
291 channelId: servers[0].videoChannel.id,
292 privacy: VideoPrivacy.PUBLIC,
296 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
297 return res.body.video.uuid
300 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
301 for (const server of servers) {
302 const resList = await getVideosList(server.url)
303 const videos: Video[] = resList.body.data
305 expect(videos.find(v => v.uuid === liveVideoId)).to.exist
307 const resVideo = await getVideo(server.url, liveVideoId)
308 const video: VideoDetails = resVideo.body
310 expect(video.streamingPlaylists).to.have.lengthOf(1)
312 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
313 expect(hlsPlaylist).to.exist
315 // Only finite files are displayed
316 expect(hlsPlaylist.files).to.have.lengthOf(0)
318 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions)
322 function updateConf (resolutions: number[]) {
323 return updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
331 '240p': resolutions.includes(240),
332 '360p': resolutions.includes(360),
333 '480p': resolutions.includes(480),
334 '720p': resolutions.includes(720),
335 '1080p': resolutions.includes(1080),
336 '2160p': resolutions.includes(2160)
343 before(async function () {
347 it('Should enable transcoding without additional resolutions', async function () {
350 liveVideoId = await createLiveWrapper(false)
352 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
353 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
354 await waitJobs(servers)
356 await testVideoResolutions(liveVideoId, [ 720 ])
358 await stopFfmpeg(command)
361 it('Should enable transcoding with some resolutions', async function () {
364 const resolutions = [ 240, 480 ]
365 await updateConf(resolutions)
366 liveVideoId = await createLiveWrapper(false)
368 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
369 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
370 await waitJobs(servers)
372 await testVideoResolutions(liveVideoId, resolutions)
374 await stopFfmpeg(command)
377 it('Should enable transcoding with some resolutions and correctly save them', async function () {
380 const resolutions = [ 240, 360, 720 ]
381 await updateConf(resolutions)
382 liveVideoId = await createLiveWrapper(true)
384 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
385 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
386 await waitJobs(servers)
388 await testVideoResolutions(liveVideoId, resolutions)
390 await stopFfmpeg(command)
392 await waitJobs(servers)
394 for (const server of servers) {
395 const resVideo = await getVideo(server.url, liveVideoId)
396 const video: VideoDetails = resVideo.body
398 expect(video.duration).to.be.greaterThan(1)
399 expect(video.files).to.have.lengthOf(0)
401 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
403 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
405 for (const resolution of resolutions) {
406 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
408 expect(file).to.exist
409 expect(file.fps).to.equal(25)
410 expect(file.size).to.be.greaterThan(1)
412 await makeRawRequest(file.torrentUrl, 200)
413 await makeRawRequest(file.fileUrl, 200)
418 it('Should correctly have cleaned up the live files', async function () {
421 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
425 describe('Live views', function () {
426 let liveVideoId: string
427 let command: FfmpegCommand
429 async function countViews (expected: number) {
430 for (const server of servers) {
431 const res = await getVideo(server.url, liveVideoId)
432 const video: VideoDetails = res.body
434 expect(video.views).to.equal(expected)
438 before(async function () {
441 const liveAttributes = {
443 channelId: servers[0].videoChannel.id,
444 privacy: VideoPrivacy.PUBLIC
447 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
448 liveVideoId = res.body.video.uuid
450 command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
451 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
452 await waitJobs(servers)
455 it('Should display no views for a live', async function () {
459 it('Should view a live twice and display 1 view', async function () {
462 await viewVideo(servers[0].url, liveVideoId)
463 await viewVideo(servers[0].url, liveVideoId)
467 await waitJobs(servers)
472 it('Should wait 5 seconds and display 0 views', async function () {
476 await waitJobs(servers)
481 it('Should view a live on a remote and on local and display 2 views', async function () {
484 await viewVideo(servers[0].url, liveVideoId)
485 await viewVideo(servers[1].url, liveVideoId)
486 await viewVideo(servers[1].url, liveVideoId)
489 await waitJobs(servers)
494 after(async function () {
495 await stopFfmpeg(command)
499 describe('Live socket messages', function () {
501 async function createLiveWrapper () {
502 const liveAttributes = {
504 channelId: servers[0].videoChannel.id,
505 privacy: VideoPrivacy.PUBLIC
508 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
509 return res.body.video.uuid
512 it('Should correctly send a message when the live starts and ends', async function () {
515 const localStateChanges: VideoState[] = []
516 const remoteStateChanges: VideoState[] = []
518 const liveVideoUUID = await createLiveWrapper()
519 await waitJobs(servers)
522 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
524 const localSocket = getLiveNotificationSocket(servers[0].url)
525 localSocket.on('state-change', data => localStateChanges.push(data.state))
526 localSocket.emit('subscribe', { videoId })
530 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
532 const remoteSocket = getLiveNotificationSocket(servers[1].url)
533 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
534 remoteSocket.emit('subscribe', { videoId })
537 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
538 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoUUID)
539 await waitJobs(servers)
541 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
542 expect(stateChanges).to.have.lengthOf(1)
543 expect(stateChanges[0]).to.equal(VideoState.PUBLISHED)
546 await stopFfmpeg(command)
547 await waitJobs(servers)
549 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
550 expect(stateChanges).to.have.lengthOf(2)
551 expect(stateChanges[1]).to.equal(VideoState.LIVE_ENDED)
555 it('Should not receive a notification after unsubscribe', async function () {
558 const stateChanges: VideoState[] = []
560 const liveVideoUUID = await createLiveWrapper()
561 await waitJobs(servers)
563 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
565 const socket = getLiveNotificationSocket(servers[0].url)
566 socket.on('state-change', data => stateChanges.push(data.state))
567 socket.emit('subscribe', { videoId })
569 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
570 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoUUID)
571 await waitJobs(servers)
573 expect(stateChanges).to.have.lengthOf(1)
574 socket.emit('unsubscribe', { videoId })
576 await stopFfmpeg(command)
577 await waitJobs(servers)
579 expect(stateChanges).to.have.lengthOf(1)
583 after(async function () {
584 await cleanupTests(servers)