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,
18 flushAndRunMultipleServers,
20 getMyVideosWithFilter,
30 sendRTMPStreamInVideo,
32 setAccessTokensToServers,
33 setDefaultVideoChannel,
35 testFfmpegStreamError,
37 updateCustomSubConfig,
43 waitUntilLivePublished,
44 waitUntilLivePublishedOnAllServers,
45 waitUntilLiveSegmentGeneration
46 } from '../../../../shared/extra-utils'
48 const expect = chai.expect
50 describe('Test live', function () {
51 let servers: ServerInfo[] = []
53 before(async function () {
56 servers = await flushAndRunMultipleServers(2)
58 // Get the access tokens
59 await setAccessTokensToServers(servers)
60 await setDefaultVideoChannel(servers)
62 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
72 // Server 1 and server 2 follow each other
73 await doubleFollow(servers[0], servers[1])
76 describe('Live creation, update and delete', function () {
77 let liveVideoUUID: string
79 it('Should create a live with the appropriate parameters', async function () {
82 const attributes: LiveVideoCreate = {
86 description: 'super live description',
87 support: 'support field',
88 channelId: servers[0].videoChannel.id,
90 waitTranscoding: false,
91 name: 'my super live',
92 tags: [ 'tag1', 'tag2' ],
93 commentsEnabled: false,
94 downloadEnabled: false,
96 privacy: VideoPrivacy.PUBLIC,
97 previewfile: 'video_short1-preview.webm.jpg',
98 thumbnailfile: 'video_short1.webm.jpg'
101 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
102 liveVideoUUID = res.body.video.uuid
104 await waitJobs(servers)
106 for (const server of servers) {
107 const resVideo = await getVideo(server.url, liveVideoUUID)
108 const video: VideoDetails = resVideo.body
110 expect(video.category.id).to.equal(1)
111 expect(video.licence.id).to.equal(2)
112 expect(video.language.id).to.equal('fr')
113 expect(video.description).to.equal('super live description')
114 expect(video.support).to.equal('support field')
116 expect(video.channel.name).to.equal(servers[0].videoChannel.name)
117 expect(video.channel.host).to.equal(servers[0].videoChannel.host)
119 expect(video.isLive).to.be.true
121 expect(video.nsfw).to.be.false
122 expect(video.waitTranscoding).to.be.false
123 expect(video.name).to.equal('my super live')
124 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
125 expect(video.commentsEnabled).to.be.false
126 expect(video.downloadEnabled).to.be.false
127 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
129 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
130 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
132 const resLive = await getLive(server.url, server.accessToken, liveVideoUUID)
133 const live: LiveVideo = resLive.body
135 if (server.url === servers[0].url) {
136 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
137 expect(live.streamKey).to.not.be.empty
139 expect(live.rtmpUrl).to.be.null
140 expect(live.streamKey).to.be.null
143 expect(live.saveReplay).to.be.true
147 it('Should have a default preview and thumbnail', async function () {
150 const attributes: LiveVideoCreate = {
151 name: 'default live thumbnail',
152 channelId: servers[0].videoChannel.id,
153 privacy: VideoPrivacy.UNLISTED,
157 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
158 const videoId = res.body.video.uuid
160 await waitJobs(servers)
162 for (const server of servers) {
163 const resVideo = await getVideo(server.url, videoId)
164 const video: VideoDetails = resVideo.body
166 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
167 expect(video.nsfw).to.be.true
169 await makeRawRequest(server.url + video.thumbnailPath, HttpStatusCode.OK_200)
170 await makeRawRequest(server.url + video.previewPath, HttpStatusCode.OK_200)
174 it('Should not have the live listed since nobody streams into', async function () {
175 for (const server of servers) {
176 const res = await getVideosList(server.url)
178 expect(res.body.total).to.equal(0)
179 expect(res.body.data).to.have.lengthOf(0)
183 it('Should not be able to update a live of another server', async function () {
184 await updateLive(servers[1].url, servers[1].accessToken, liveVideoUUID, { saveReplay: false }, HttpStatusCode.FORBIDDEN_403)
187 it('Should update the live', async function () {
190 await updateLive(servers[0].url, servers[0].accessToken, liveVideoUUID, { saveReplay: false })
191 await waitJobs(servers)
194 it('Have the live updated', async function () {
195 for (const server of servers) {
196 const res = await getLive(server.url, server.accessToken, liveVideoUUID)
197 const live: LiveVideo = res.body
199 if (server.url === servers[0].url) {
200 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
201 expect(live.streamKey).to.not.be.empty
203 expect(live.rtmpUrl).to.be.null
204 expect(live.streamKey).to.be.null
207 expect(live.saveReplay).to.be.false
211 it('Delete the live', async function () {
214 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
215 await waitJobs(servers)
218 it('Should have the live deleted', async function () {
219 for (const server of servers) {
220 await getVideo(server.url, liveVideoUUID, HttpStatusCode.NOT_FOUND_404)
221 await getLive(server.url, server.accessToken, liveVideoUUID, HttpStatusCode.NOT_FOUND_404)
226 describe('Live filters', function () {
228 let liveVideoId: string
229 let vodVideoId: string
231 before(async function () {
234 vodVideoId = (await uploadVideoAndGetId({ server: servers[0], videoName: 'vod video' })).uuid
236 const liveOptions = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: servers[0].videoChannel.id }
237 const resLive = await createLive(servers[0].url, servers[0].accessToken, liveOptions)
238 liveVideoId = resLive.body.video.uuid
240 command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
241 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
242 await waitJobs(servers)
245 it('Should only display lives', async function () {
246 const res = await getVideosWithFilters(servers[0].url, { isLive: true })
248 expect(res.body.total).to.equal(1)
249 expect(res.body.data).to.have.lengthOf(1)
250 expect(res.body.data[0].name).to.equal('live')
253 it('Should not display lives', async function () {
254 const res = await getVideosWithFilters(servers[0].url, { isLive: false })
256 expect(res.body.total).to.equal(1)
257 expect(res.body.data).to.have.lengthOf(1)
258 expect(res.body.data[0].name).to.equal('vod video')
261 it('Should display my lives', async function () {
264 await stopFfmpeg(command)
265 await waitJobs(servers)
267 const res = await getMyVideosWithFilter(servers[0].url, servers[0].accessToken, { isLive: true })
268 const videos = res.body.data as Video[]
270 const result = videos.every(v => v.isLive)
271 expect(result).to.be.true
274 it('Should not display my lives', async function () {
275 const res = await getMyVideosWithFilter(servers[0].url, servers[0].accessToken, { isLive: false })
276 const videos = res.body.data as Video[]
278 const result = videos.every(v => !v.isLive)
279 expect(result).to.be.true
282 after(async function () {
283 await removeVideo(servers[0].url, servers[0].accessToken, vodVideoId)
284 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoId)
288 describe('Stream checks', function () {
289 let liveVideo: LiveVideo & VideoDetails
293 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
296 async function createLiveWrapper () {
297 const liveAttributes = {
299 channelId: servers[0].videoChannel.id,
300 privacy: VideoPrivacy.PUBLIC,
304 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
305 const uuid = res.body.video.uuid
307 const resLive = await getLive(servers[0].url, servers[0].accessToken, uuid)
308 const resVideo = await getVideo(servers[0].url, uuid)
310 return Object.assign(resVideo.body, resLive.body) as LiveVideo & VideoDetails
313 it('Should not allow a stream without the appropriate path', async function () {
316 liveVideo = await createLiveWrapper()
318 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
319 await testFfmpegStreamError(command, true)
322 it('Should not allow a stream without the appropriate stream key', async function () {
325 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
326 await testFfmpegStreamError(command, true)
329 it('Should succeed with the correct params', async function () {
332 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
333 await testFfmpegStreamError(command, false)
336 it('Should list this live now someone stream into it', async function () {
337 for (const server of servers) {
338 const res = await getVideosList(server.url)
340 expect(res.body.total).to.equal(1)
341 expect(res.body.data).to.have.lengthOf(1)
343 const video: Video = res.body.data[0]
345 expect(video.name).to.equal('user live')
346 expect(video.isLive).to.be.true
350 it('Should not allow a stream on a live that was blacklisted', async function () {
353 liveVideo = await createLiveWrapper()
355 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideo.uuid)
357 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
358 await testFfmpegStreamError(command, true)
361 it('Should not allow a stream on a live that was deleted', async function () {
364 liveVideo = await createLiveWrapper()
366 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid)
368 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
369 await testFfmpegStreamError(command, true)
373 describe('Live transcoding', function () {
374 let liveVideoId: string
376 async function createLiveWrapper (saveReplay: boolean) {
377 const liveAttributes = {
379 channelId: servers[0].videoChannel.id,
380 privacy: VideoPrivacy.PUBLIC,
384 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
385 return res.body.video.uuid
388 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
389 for (const server of servers) {
390 const resList = await getVideosList(server.url)
391 const videos: Video[] = resList.body.data
393 expect(videos.find(v => v.uuid === liveVideoId)).to.exist
395 const resVideo = await getVideo(server.url, liveVideoId)
396 const video: VideoDetails = resVideo.body
398 expect(video.streamingPlaylists).to.have.lengthOf(1)
400 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
401 expect(hlsPlaylist).to.exist
403 // Only finite files are displayed
404 expect(hlsPlaylist.files).to.have.lengthOf(0)
406 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions)
408 for (let i = 0; i < resolutions.length; i++) {
410 const segmentName = `${i}-00000${segmentNum}.ts`
411 await waitUntilLiveSegmentGeneration(servers[0], video.uuid, i, segmentNum)
413 const res = await getPlaylist(`${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`)
414 const subPlaylist = res.text
416 expect(subPlaylist).to.contain(segmentName)
418 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
419 await checkLiveSegmentHash(baseUrlAndPath, video.uuid, segmentName, hlsPlaylist)
424 function updateConf (resolutions: number[]) {
425 return updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
433 '240p': resolutions.includes(240),
434 '360p': resolutions.includes(360),
435 '480p': resolutions.includes(480),
436 '720p': resolutions.includes(720),
437 '1080p': resolutions.includes(1080),
438 '2160p': resolutions.includes(2160)
445 before(async function () {
449 it('Should enable transcoding without additional resolutions', async function () {
452 liveVideoId = await createLiveWrapper(false)
454 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
455 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
456 await waitJobs(servers)
458 await testVideoResolutions(liveVideoId, [ 720 ])
460 await stopFfmpeg(command)
463 it('Should enable transcoding with some resolutions', async function () {
466 const resolutions = [ 240, 480 ]
467 await updateConf(resolutions)
468 liveVideoId = await createLiveWrapper(false)
470 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
471 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
472 await waitJobs(servers)
474 await testVideoResolutions(liveVideoId, resolutions)
476 await stopFfmpeg(command)
479 it('Should enable transcoding with some resolutions and correctly save them', async function () {
482 const resolutions = [ 240, 360, 720 ]
484 await updateConf(resolutions)
485 liveVideoId = await createLiveWrapper(true)
487 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId, 'video_short2.webm')
488 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
489 await waitJobs(servers)
491 await testVideoResolutions(liveVideoId, resolutions)
493 await stopFfmpeg(command)
494 await waitUntilLiveEnded(servers[0].url, servers[0].accessToken, liveVideoId)
496 await waitJobs(servers)
498 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
500 const bitrateLimits = {
501 720: 5000 * 1000, // 60FPS
506 for (const server of servers) {
507 const resVideo = await getVideo(server.url, liveVideoId)
508 const video: VideoDetails = resVideo.body
510 expect(video.state.id).to.equal(VideoState.PUBLISHED)
511 expect(video.duration).to.be.greaterThan(1)
512 expect(video.files).to.have.lengthOf(0)
514 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
515 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
516 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
518 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
520 for (const resolution of resolutions) {
521 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
523 expect(file).to.exist
524 expect(file.size).to.be.greaterThan(1)
526 if (resolution >= 720) {
527 expect(file.fps).to.be.approximately(60, 2)
529 expect(file.fps).to.be.approximately(30, 2)
532 const filename = `${video.uuid}-${resolution}-fragmented.mp4`
533 const segmentPath = buildServerDirectory(servers[0], join('streaming-playlists', 'hls', video.uuid, filename))
535 const probe = await ffprobePromise(segmentPath)
536 const videoStream = await getVideoStreamFromFile(segmentPath, probe)
538 expect(probe.format.bit_rate).to.be.below(bitrateLimits[videoStream.height])
540 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
541 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
546 it('Should correctly have cleaned up the live files', async function () {
549 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
553 describe('After a server restart', function () {
554 let liveVideoId: string
555 let liveVideoReplayId: string
557 async function createLiveWrapper (saveReplay: boolean) {
558 const liveAttributes = {
560 channelId: servers[0].videoChannel.id,
561 privacy: VideoPrivacy.PUBLIC,
565 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
566 return res.body.video.uuid
569 before(async function () {
572 liveVideoId = await createLiveWrapper(false)
573 liveVideoReplayId = await createLiveWrapper(true)
576 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId),
577 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoReplayId)
581 waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoId),
582 waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoReplayId)
585 await waitUntilLiveSegmentGeneration(servers[0], liveVideoId, 0, 2)
586 await waitUntilLiveSegmentGeneration(servers[0], liveVideoReplayId, 0, 2)
588 await killallServers([ servers[0] ])
589 await reRunServer(servers[0])
594 it('Should cleanup lives', async function () {
597 await waitUntilLiveEnded(servers[0].url, servers[0].accessToken, liveVideoId)
600 it('Should save a live replay', async function () {
603 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoReplayId)
607 after(async function () {
608 await cleanupTests(servers)