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'
14 checkResolutionsInMasterPlaylist,
17 flushAndRunMultipleServers,
18 getMyVideosWithFilter,
30 setAccessTokensToServers,
31 setDefaultVideoChannel,
33 testFfmpegStreamError,
38 waitUntilLivePublishedOnAllServers
39 } from '../../../../shared/extra-utils'
41 const expect = chai.expect
43 describe('Test live', function () {
44 let servers: ServerInfo[] = []
45 let commands: LiveCommand[]
47 before(async function () {
50 servers = await flushAndRunMultipleServers(2)
52 // Get the access tokens
53 await setAccessTokensToServers(servers)
54 await setDefaultVideoChannel(servers)
56 await servers[0].configCommand.updateCustomSubConfig({
68 // Server 1 and server 2 follow each other
69 await doubleFollow(servers[0], servers[1])
71 commands = servers.map(s => s.liveCommand)
74 describe('Live creation, update and delete', function () {
75 let liveVideoUUID: string
77 it('Should create a live with the appropriate parameters', async function () {
80 const attributes: LiveVideoCreate = {
84 description: 'super live description',
85 support: 'support field',
86 channelId: servers[0].videoChannel.id,
88 waitTranscoding: false,
89 name: 'my super live',
90 tags: [ 'tag1', 'tag2' ],
91 commentsEnabled: false,
92 downloadEnabled: false,
94 privacy: VideoPrivacy.PUBLIC,
95 previewfile: 'video_short1-preview.webm.jpg',
96 thumbnailfile: 'video_short1.webm.jpg'
99 const live = await commands[0].createLive({ fields: attributes })
100 liveVideoUUID = live.uuid
102 await waitJobs(servers)
104 for (const server of servers) {
105 const resVideo = await getVideo(server.url, liveVideoUUID)
106 const video: VideoDetails = resVideo.body
108 expect(video.category.id).to.equal(1)
109 expect(video.licence.id).to.equal(2)
110 expect(video.language.id).to.equal('fr')
111 expect(video.description).to.equal('super live description')
112 expect(video.support).to.equal('support field')
114 expect(video.channel.name).to.equal(servers[0].videoChannel.name)
115 expect(video.channel.host).to.equal(servers[0].videoChannel.host)
117 expect(video.isLive).to.be.true
119 expect(video.nsfw).to.be.false
120 expect(video.waitTranscoding).to.be.false
121 expect(video.name).to.equal('my super live')
122 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
123 expect(video.commentsEnabled).to.be.false
124 expect(video.downloadEnabled).to.be.false
125 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
127 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
128 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
130 const live = await server.liveCommand.getLive({ videoId: liveVideoUUID })
132 if (server.url === servers[0].url) {
133 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
134 expect(live.streamKey).to.not.be.empty
136 expect(live.rtmpUrl).to.be.null
137 expect(live.streamKey).to.be.null
140 expect(live.saveReplay).to.be.true
144 it('Should have a default preview and thumbnail', async function () {
147 const attributes: LiveVideoCreate = {
148 name: 'default live thumbnail',
149 channelId: servers[0].videoChannel.id,
150 privacy: VideoPrivacy.UNLISTED,
154 const live = await commands[0].createLive({ fields: attributes })
155 const videoId = live.uuid
157 await waitJobs(servers)
159 for (const server of servers) {
160 const resVideo = await getVideo(server.url, videoId)
161 const video: VideoDetails = resVideo.body
163 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
164 expect(video.nsfw).to.be.true
166 await makeRawRequest(server.url + video.thumbnailPath, HttpStatusCode.OK_200)
167 await makeRawRequest(server.url + video.previewPath, HttpStatusCode.OK_200)
171 it('Should not have the live listed since nobody streams into', async function () {
172 for (const server of servers) {
173 const res = await getVideosList(server.url)
175 expect(res.body.total).to.equal(0)
176 expect(res.body.data).to.have.lengthOf(0)
180 it('Should not be able to update a live of another server', async function () {
181 await commands[1].updateLive({ videoId: liveVideoUUID, fields: { saveReplay: false }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
184 it('Should update the live', async function () {
187 await commands[0].updateLive({ videoId: liveVideoUUID, fields: { saveReplay: false } })
188 await waitJobs(servers)
191 it('Have the live updated', async function () {
192 for (const server of servers) {
193 const live = await server.liveCommand.getLive({ videoId: liveVideoUUID })
195 if (server.url === servers[0].url) {
196 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
197 expect(live.streamKey).to.not.be.empty
199 expect(live.rtmpUrl).to.be.null
200 expect(live.streamKey).to.be.null
203 expect(live.saveReplay).to.be.false
207 it('Delete the live', async function () {
210 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
211 await waitJobs(servers)
214 it('Should have the live deleted', async function () {
215 for (const server of servers) {
216 await getVideo(server.url, liveVideoUUID, HttpStatusCode.NOT_FOUND_404)
217 await server.liveCommand.getLive({ videoId: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
222 describe('Live filters', function () {
223 let ffmpegCommand: any
224 let liveVideoId: string
225 let vodVideoId: string
227 before(async function () {
230 vodVideoId = (await uploadVideoAndGetId({ server: servers[0], videoName: 'vod video' })).uuid
232 const liveOptions = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: servers[0].videoChannel.id }
233 const live = await commands[0].createLive({ fields: liveOptions })
234 liveVideoId = live.uuid
236 ffmpegCommand = await servers[0].liveCommand.sendRTMPStreamInVideo({ videoId: liveVideoId })
237 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
238 await waitJobs(servers)
241 it('Should only display lives', async function () {
242 const res = await getVideosWithFilters(servers[0].url, { isLive: true })
244 expect(res.body.total).to.equal(1)
245 expect(res.body.data).to.have.lengthOf(1)
246 expect(res.body.data[0].name).to.equal('live')
249 it('Should not display lives', async function () {
250 const res = await getVideosWithFilters(servers[0].url, { isLive: false })
252 expect(res.body.total).to.equal(1)
253 expect(res.body.data).to.have.lengthOf(1)
254 expect(res.body.data[0].name).to.equal('vod video')
257 it('Should display my lives', async function () {
260 await stopFfmpeg(ffmpegCommand)
261 await waitJobs(servers)
263 const res = await getMyVideosWithFilter(servers[0].url, servers[0].accessToken, { isLive: true })
264 const videos = res.body.data as Video[]
266 const result = videos.every(v => v.isLive)
267 expect(result).to.be.true
270 it('Should not display my lives', async function () {
271 const res = await getMyVideosWithFilter(servers[0].url, servers[0].accessToken, { isLive: false })
272 const videos = res.body.data as Video[]
274 const result = videos.every(v => !v.isLive)
275 expect(result).to.be.true
278 after(async function () {
279 await removeVideo(servers[0].url, servers[0].accessToken, vodVideoId)
280 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoId)
284 describe('Stream checks', function () {
285 let liveVideo: LiveVideo & VideoDetails
289 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
292 async function createLiveWrapper () {
293 const liveAttributes = {
295 channelId: servers[0].videoChannel.id,
296 privacy: VideoPrivacy.PUBLIC,
300 const { uuid } = await commands[0].createLive({ fields: liveAttributes })
302 const live = await commands[0].getLive({ videoId: uuid })
303 const resVideo = await getVideo(servers[0].url, uuid)
305 return Object.assign(resVideo.body as VideoDetails, live)
308 it('Should not allow a stream without the appropriate path', async function () {
311 liveVideo = await createLiveWrapper()
313 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
314 await testFfmpegStreamError(command, true)
317 it('Should not allow a stream without the appropriate stream key', async function () {
320 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
321 await testFfmpegStreamError(command, true)
324 it('Should succeed with the correct params', async function () {
327 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
328 await testFfmpegStreamError(command, false)
331 it('Should list this live now someone stream into it', async function () {
332 for (const server of servers) {
333 const res = await getVideosList(server.url)
335 expect(res.body.total).to.equal(1)
336 expect(res.body.data).to.have.lengthOf(1)
338 const video: Video = res.body.data[0]
340 expect(video.name).to.equal('user live')
341 expect(video.isLive).to.be.true
345 it('Should not allow a stream on a live that was blacklisted', async function () {
348 liveVideo = await createLiveWrapper()
350 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideo.uuid)
352 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
353 await testFfmpegStreamError(command, true)
356 it('Should not allow a stream on a live that was deleted', async function () {
359 liveVideo = await createLiveWrapper()
361 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid)
363 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
364 await testFfmpegStreamError(command, true)
368 describe('Live transcoding', function () {
369 let liveVideoId: string
371 async function createLiveWrapper (saveReplay: boolean) {
372 const liveAttributes = {
374 channelId: servers[0].videoChannel.id,
375 privacy: VideoPrivacy.PUBLIC,
379 const { uuid } = await commands[0].createLive({ fields: liveAttributes })
383 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
384 for (const server of servers) {
385 const resList = await getVideosList(server.url)
386 const videos: Video[] = resList.body.data
388 expect(videos.find(v => v.uuid === liveVideoId)).to.exist
390 const resVideo = await getVideo(server.url, liveVideoId)
391 const video: VideoDetails = resVideo.body
393 expect(video.streamingPlaylists).to.have.lengthOf(1)
395 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
396 expect(hlsPlaylist).to.exist
398 // Only finite files are displayed
399 expect(hlsPlaylist.files).to.have.lengthOf(0)
401 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions)
403 for (let i = 0; i < resolutions.length; i++) {
405 const segmentName = `${i}-00000${segmentNum}.ts`
406 await commands[0].waitUntilLiveSegmentGeneration({ videoUUID: video.uuid, resolution: i, segment: segmentNum })
408 const res = await getPlaylist(`${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`)
409 const subPlaylist = res.text
411 expect(subPlaylist).to.contain(segmentName)
413 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
414 await checkLiveSegmentHash(baseUrlAndPath, video.uuid, segmentName, hlsPlaylist)
419 function updateConf (resolutions: number[]) {
420 return servers[0].configCommand.updateCustomSubConfig({
429 '240p': resolutions.includes(240),
430 '360p': resolutions.includes(360),
431 '480p': resolutions.includes(480),
432 '720p': resolutions.includes(720),
433 '1080p': resolutions.includes(1080),
434 '2160p': resolutions.includes(2160)
442 before(async function () {
446 it('Should enable transcoding without additional resolutions', async function () {
449 liveVideoId = await createLiveWrapper(false)
451 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
452 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
453 await waitJobs(servers)
455 await testVideoResolutions(liveVideoId, [ 720 ])
457 await stopFfmpeg(ffmpegCommand)
460 it('Should enable transcoding with some resolutions', async function () {
463 const resolutions = [ 240, 480 ]
464 await updateConf(resolutions)
465 liveVideoId = await createLiveWrapper(false)
467 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
468 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
469 await waitJobs(servers)
471 await testVideoResolutions(liveVideoId, resolutions)
473 await stopFfmpeg(ffmpegCommand)
476 it('Should enable transcoding with some resolutions and correctly save them', async function () {
479 const resolutions = [ 240, 360, 720 ]
481 await updateConf(resolutions)
482 liveVideoId = await createLiveWrapper(true)
484 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
485 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
486 await waitJobs(servers)
488 await testVideoResolutions(liveVideoId, resolutions)
490 await stopFfmpeg(ffmpegCommand)
491 await commands[0].waitUntilLiveEnded({ videoId: liveVideoId })
493 await waitJobs(servers)
495 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
497 const bitrateLimits = {
498 720: 5000 * 1000, // 60FPS
503 for (const server of servers) {
504 const resVideo = await getVideo(server.url, liveVideoId)
505 const video: VideoDetails = resVideo.body
507 expect(video.state.id).to.equal(VideoState.PUBLISHED)
508 expect(video.duration).to.be.greaterThan(1)
509 expect(video.files).to.have.lengthOf(0)
511 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
512 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
513 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
515 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
517 for (const resolution of resolutions) {
518 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
520 expect(file).to.exist
521 expect(file.size).to.be.greaterThan(1)
523 if (resolution >= 720) {
524 expect(file.fps).to.be.approximately(60, 2)
526 expect(file.fps).to.be.approximately(30, 2)
529 const filename = `${video.uuid}-${resolution}-fragmented.mp4`
530 const segmentPath = buildServerDirectory(servers[0], join('streaming-playlists', 'hls', video.uuid, filename))
532 const probe = await ffprobePromise(segmentPath)
533 const videoStream = await getVideoStreamFromFile(segmentPath, probe)
535 expect(probe.format.bit_rate).to.be.below(bitrateLimits[videoStream.height])
537 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
538 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
543 it('Should correctly have cleaned up the live files', async function () {
546 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
550 describe('After a server restart', function () {
551 let liveVideoId: string
552 let liveVideoReplayId: string
554 async function createLiveWrapper (saveReplay: boolean) {
555 const liveAttributes = {
557 channelId: servers[0].videoChannel.id,
558 privacy: VideoPrivacy.PUBLIC,
562 const { uuid } = await commands[0].createLive({ fields: liveAttributes })
566 before(async function () {
569 liveVideoId = await createLiveWrapper(false)
570 liveVideoReplayId = await createLiveWrapper(true)
573 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId }),
574 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoReplayId })
578 commands[0].waitUntilLivePublished({ videoId: liveVideoId }),
579 commands[0].waitUntilLivePublished({ videoId: liveVideoReplayId })
582 await commands[0].waitUntilLiveSegmentGeneration({ videoUUID: liveVideoId, resolution: 0, segment: 2 })
583 await commands[0].waitUntilLiveSegmentGeneration({ videoUUID: liveVideoReplayId, resolution: 0, segment: 2 })
585 await killallServers([ servers[0] ])
586 await reRunServer(servers[0])
591 it('Should cleanup lives', async function () {
594 await commands[0].waitUntilLiveEnded({ videoId: liveVideoId })
597 it('Should save a live replay', async function () {
600 await commands[0].waitUntilLivePublished({ videoId: liveVideoReplayId })
604 after(async function () {
605 await cleanupTests(servers)