1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { join } from 'path'
6 import { ffprobePromise, getVideoStreamFromFile } from '@server/helpers/ffprobe-utils'
7 import { LiveVideo, LiveVideoCreate, Video, VideoDetails, VideoPrivacy, VideoState, VideoStreamingPlaylistType } from '@shared/models'
8 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
13 checkResolutionsInMasterPlaylist,
16 flushAndRunMultipleServers,
17 getMyVideosWithFilter,
29 setAccessTokensToServers,
30 setDefaultVideoChannel,
32 testFfmpegStreamError,
37 waitUntilLivePublishedOnAllServers
38 } from '../../../../shared/extra-utils'
40 const expect = chai.expect
42 describe('Test live', function () {
43 let servers: ServerInfo[] = []
44 let commands: LiveCommand[]
46 before(async function () {
49 servers = await flushAndRunMultipleServers(2)
51 // Get the access tokens
52 await setAccessTokensToServers(servers)
53 await setDefaultVideoChannel(servers)
55 await servers[0].configCommand.updateCustomSubConfig({
67 // Server 1 and server 2 follow each other
68 await doubleFollow(servers[0], servers[1])
70 commands = servers.map(s => s.liveCommand)
73 describe('Live creation, update and delete', function () {
74 let liveVideoUUID: string
76 it('Should create a live with the appropriate parameters', async function () {
79 const attributes: LiveVideoCreate = {
83 description: 'super live description',
84 support: 'support field',
85 channelId: servers[0].videoChannel.id,
87 waitTranscoding: false,
88 name: 'my super live',
89 tags: [ 'tag1', 'tag2' ],
90 commentsEnabled: false,
91 downloadEnabled: false,
93 privacy: VideoPrivacy.PUBLIC,
94 previewfile: 'video_short1-preview.webm.jpg',
95 thumbnailfile: 'video_short1.webm.jpg'
98 const live = await commands[0].create({ fields: attributes })
99 liveVideoUUID = live.uuid
101 await waitJobs(servers)
103 for (const server of servers) {
104 const resVideo = await getVideo(server.url, liveVideoUUID)
105 const video: VideoDetails = resVideo.body
107 expect(video.category.id).to.equal(1)
108 expect(video.licence.id).to.equal(2)
109 expect(video.language.id).to.equal('fr')
110 expect(video.description).to.equal('super live description')
111 expect(video.support).to.equal('support field')
113 expect(video.channel.name).to.equal(servers[0].videoChannel.name)
114 expect(video.channel.host).to.equal(servers[0].videoChannel.host)
116 expect(video.isLive).to.be.true
118 expect(video.nsfw).to.be.false
119 expect(video.waitTranscoding).to.be.false
120 expect(video.name).to.equal('my super live')
121 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
122 expect(video.commentsEnabled).to.be.false
123 expect(video.downloadEnabled).to.be.false
124 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
126 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
127 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
129 const live = await server.liveCommand.get({ videoId: liveVideoUUID })
131 if (server.url === servers[0].url) {
132 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
133 expect(live.streamKey).to.not.be.empty
135 expect(live.rtmpUrl).to.be.null
136 expect(live.streamKey).to.be.null
139 expect(live.saveReplay).to.be.true
143 it('Should have a default preview and thumbnail', async function () {
146 const attributes: LiveVideoCreate = {
147 name: 'default live thumbnail',
148 channelId: servers[0].videoChannel.id,
149 privacy: VideoPrivacy.UNLISTED,
153 const live = await commands[0].create({ fields: attributes })
154 const videoId = live.uuid
156 await waitJobs(servers)
158 for (const server of servers) {
159 const resVideo = await getVideo(server.url, videoId)
160 const video: VideoDetails = resVideo.body
162 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
163 expect(video.nsfw).to.be.true
165 await makeRawRequest(server.url + video.thumbnailPath, HttpStatusCode.OK_200)
166 await makeRawRequest(server.url + video.previewPath, HttpStatusCode.OK_200)
170 it('Should not have the live listed since nobody streams into', async function () {
171 for (const server of servers) {
172 const res = await getVideosList(server.url)
174 expect(res.body.total).to.equal(0)
175 expect(res.body.data).to.have.lengthOf(0)
179 it('Should not be able to update a live of another server', async function () {
180 await commands[1].update({ videoId: liveVideoUUID, fields: { saveReplay: false }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
183 it('Should update the live', async function () {
186 await commands[0].update({ videoId: liveVideoUUID, fields: { saveReplay: false } })
187 await waitJobs(servers)
190 it('Have the live updated', async function () {
191 for (const server of servers) {
192 const live = await server.liveCommand.get({ videoId: liveVideoUUID })
194 if (server.url === servers[0].url) {
195 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
196 expect(live.streamKey).to.not.be.empty
198 expect(live.rtmpUrl).to.be.null
199 expect(live.streamKey).to.be.null
202 expect(live.saveReplay).to.be.false
206 it('Delete the live', async function () {
209 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
210 await waitJobs(servers)
213 it('Should have the live deleted', async function () {
214 for (const server of servers) {
215 await getVideo(server.url, liveVideoUUID, HttpStatusCode.NOT_FOUND_404)
216 await server.liveCommand.get({ videoId: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
221 describe('Live filters', function () {
222 let ffmpegCommand: any
223 let liveVideoId: string
224 let vodVideoId: string
226 before(async function () {
229 vodVideoId = (await uploadVideoAndGetId({ server: servers[0], videoName: 'vod video' })).uuid
231 const liveOptions = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: servers[0].videoChannel.id }
232 const live = await commands[0].create({ fields: liveOptions })
233 liveVideoId = live.uuid
235 ffmpegCommand = await servers[0].liveCommand.sendRTMPStreamInVideo({ videoId: liveVideoId })
236 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
237 await waitJobs(servers)
240 it('Should only display lives', async function () {
241 const res = await getVideosWithFilters(servers[0].url, { isLive: true })
243 expect(res.body.total).to.equal(1)
244 expect(res.body.data).to.have.lengthOf(1)
245 expect(res.body.data[0].name).to.equal('live')
248 it('Should not display lives', async function () {
249 const res = await getVideosWithFilters(servers[0].url, { isLive: false })
251 expect(res.body.total).to.equal(1)
252 expect(res.body.data).to.have.lengthOf(1)
253 expect(res.body.data[0].name).to.equal('vod video')
256 it('Should display my lives', async function () {
259 await stopFfmpeg(ffmpegCommand)
260 await waitJobs(servers)
262 const res = await getMyVideosWithFilter(servers[0].url, servers[0].accessToken, { isLive: true })
263 const videos = res.body.data as Video[]
265 const result = videos.every(v => v.isLive)
266 expect(result).to.be.true
269 it('Should not display my lives', async function () {
270 const res = await getMyVideosWithFilter(servers[0].url, servers[0].accessToken, { isLive: false })
271 const videos = res.body.data as Video[]
273 const result = videos.every(v => !v.isLive)
274 expect(result).to.be.true
277 after(async function () {
278 await removeVideo(servers[0].url, servers[0].accessToken, vodVideoId)
279 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoId)
283 describe('Stream checks', function () {
284 let liveVideo: LiveVideo & VideoDetails
288 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
291 async function createLiveWrapper () {
292 const liveAttributes = {
294 channelId: servers[0].videoChannel.id,
295 privacy: VideoPrivacy.PUBLIC,
299 const { uuid } = await commands[0].create({ fields: liveAttributes })
301 const live = await commands[0].get({ videoId: uuid })
302 const resVideo = await getVideo(servers[0].url, uuid)
304 return Object.assign(resVideo.body as VideoDetails, live)
307 it('Should not allow a stream without the appropriate path', async function () {
310 liveVideo = await createLiveWrapper()
312 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
313 await testFfmpegStreamError(command, true)
316 it('Should not allow a stream without the appropriate stream key', async function () {
319 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
320 await testFfmpegStreamError(command, true)
323 it('Should succeed with the correct params', async function () {
326 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
327 await testFfmpegStreamError(command, false)
330 it('Should list this live now someone stream into it', async function () {
331 for (const server of servers) {
332 const res = await getVideosList(server.url)
334 expect(res.body.total).to.equal(1)
335 expect(res.body.data).to.have.lengthOf(1)
337 const video: Video = res.body.data[0]
339 expect(video.name).to.equal('user live')
340 expect(video.isLive).to.be.true
344 it('Should not allow a stream on a live that was blacklisted', async function () {
347 liveVideo = await createLiveWrapper()
349 await servers[0].blacklistCommand.add({ videoId: liveVideo.uuid })
351 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
352 await testFfmpegStreamError(command, true)
355 it('Should not allow a stream on a live that was deleted', async function () {
358 liveVideo = await createLiveWrapper()
360 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid)
362 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
363 await testFfmpegStreamError(command, true)
367 describe('Live transcoding', function () {
368 let liveVideoId: string
370 async function createLiveWrapper (saveReplay: boolean) {
371 const liveAttributes = {
373 channelId: servers[0].videoChannel.id,
374 privacy: VideoPrivacy.PUBLIC,
378 const { uuid } = await commands[0].create({ fields: liveAttributes })
382 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
383 for (const server of servers) {
384 const resList = await getVideosList(server.url)
385 const videos: Video[] = resList.body.data
387 expect(videos.find(v => v.uuid === liveVideoId)).to.exist
389 const resVideo = await getVideo(server.url, liveVideoId)
390 const video: VideoDetails = resVideo.body
392 expect(video.streamingPlaylists).to.have.lengthOf(1)
394 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
395 expect(hlsPlaylist).to.exist
397 // Only finite files are displayed
398 expect(hlsPlaylist.files).to.have.lengthOf(0)
400 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions)
402 for (let i = 0; i < resolutions.length; i++) {
404 const segmentName = `${i}-00000${segmentNum}.ts`
405 await commands[0].waitUntilSegmentGeneration({ videoUUID: video.uuid, resolution: i, segment: segmentNum })
407 const res = await getPlaylist(`${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`)
408 const subPlaylist = res.text
410 expect(subPlaylist).to.contain(segmentName)
412 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
413 await checkLiveSegmentHash(baseUrlAndPath, video.uuid, segmentName, hlsPlaylist)
418 function updateConf (resolutions: number[]) {
419 return servers[0].configCommand.updateCustomSubConfig({
428 '240p': resolutions.includes(240),
429 '360p': resolutions.includes(360),
430 '480p': resolutions.includes(480),
431 '720p': resolutions.includes(720),
432 '1080p': resolutions.includes(1080),
433 '2160p': resolutions.includes(2160)
441 before(async function () {
445 it('Should enable transcoding without additional resolutions', async function () {
448 liveVideoId = await createLiveWrapper(false)
450 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
451 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
452 await waitJobs(servers)
454 await testVideoResolutions(liveVideoId, [ 720 ])
456 await stopFfmpeg(ffmpegCommand)
459 it('Should enable transcoding with some resolutions', async function () {
462 const resolutions = [ 240, 480 ]
463 await updateConf(resolutions)
464 liveVideoId = await createLiveWrapper(false)
466 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
467 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
468 await waitJobs(servers)
470 await testVideoResolutions(liveVideoId, resolutions)
472 await stopFfmpeg(ffmpegCommand)
475 it('Should enable transcoding with some resolutions and correctly save them', async function () {
478 const resolutions = [ 240, 360, 720 ]
480 await updateConf(resolutions)
481 liveVideoId = await createLiveWrapper(true)
483 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
484 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
485 await waitJobs(servers)
487 await testVideoResolutions(liveVideoId, resolutions)
489 await stopFfmpeg(ffmpegCommand)
490 await commands[0].waitUntilEnded({ videoId: liveVideoId })
492 await waitJobs(servers)
494 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
496 const bitrateLimits = {
497 720: 5000 * 1000, // 60FPS
502 for (const server of servers) {
503 const resVideo = await getVideo(server.url, liveVideoId)
504 const video: VideoDetails = resVideo.body
506 expect(video.state.id).to.equal(VideoState.PUBLISHED)
507 expect(video.duration).to.be.greaterThan(1)
508 expect(video.files).to.have.lengthOf(0)
510 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
511 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
512 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
514 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
516 for (const resolution of resolutions) {
517 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
519 expect(file).to.exist
520 expect(file.size).to.be.greaterThan(1)
522 if (resolution >= 720) {
523 expect(file.fps).to.be.approximately(60, 2)
525 expect(file.fps).to.be.approximately(30, 2)
528 const filename = `${video.uuid}-${resolution}-fragmented.mp4`
529 const segmentPath = buildServerDirectory(servers[0], join('streaming-playlists', 'hls', video.uuid, filename))
531 const probe = await ffprobePromise(segmentPath)
532 const videoStream = await getVideoStreamFromFile(segmentPath, probe)
534 expect(probe.format.bit_rate).to.be.below(bitrateLimits[videoStream.height])
536 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
537 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
542 it('Should correctly have cleaned up the live files', async function () {
545 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
549 describe('After a server restart', function () {
550 let liveVideoId: string
551 let liveVideoReplayId: string
553 async function createLiveWrapper (saveReplay: boolean) {
554 const liveAttributes = {
556 channelId: servers[0].videoChannel.id,
557 privacy: VideoPrivacy.PUBLIC,
561 const { uuid } = await commands[0].create({ fields: liveAttributes })
565 before(async function () {
568 liveVideoId = await createLiveWrapper(false)
569 liveVideoReplayId = await createLiveWrapper(true)
572 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId }),
573 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoReplayId })
577 commands[0].waitUntilPublished({ videoId: liveVideoId }),
578 commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
581 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoId, resolution: 0, segment: 2 })
582 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoReplayId, resolution: 0, segment: 2 })
584 killallServers([ servers[0] ])
585 await reRunServer(servers[0])
590 it('Should cleanup lives', async function () {
593 await commands[0].waitUntilEnded({ videoId: liveVideoId })
596 it('Should save a live replay', async function () {
599 await commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
603 after(async function () {
604 await cleanupTests(servers)