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,
28 setAccessTokensToServers,
29 setDefaultVideoChannel,
31 testFfmpegStreamError,
36 waitUntilLivePublishedOnAllServers
37 } from '../../../../shared/extra-utils'
39 const expect = chai.expect
41 describe('Test live', function () {
42 let servers: ServerInfo[] = []
43 let commands: LiveCommand[]
45 before(async function () {
48 servers = await flushAndRunMultipleServers(2)
50 // Get the access tokens
51 await setAccessTokensToServers(servers)
52 await setDefaultVideoChannel(servers)
54 await servers[0].configCommand.updateCustomSubConfig({
66 // Server 1 and server 2 follow each other
67 await doubleFollow(servers[0], servers[1])
69 commands = servers.map(s => s.liveCommand)
72 describe('Live creation, update and delete', function () {
73 let liveVideoUUID: string
75 it('Should create a live with the appropriate parameters', async function () {
78 const attributes: LiveVideoCreate = {
82 description: 'super live description',
83 support: 'support field',
84 channelId: servers[0].videoChannel.id,
86 waitTranscoding: false,
87 name: 'my super live',
88 tags: [ 'tag1', 'tag2' ],
89 commentsEnabled: false,
90 downloadEnabled: false,
92 privacy: VideoPrivacy.PUBLIC,
93 previewfile: 'video_short1-preview.webm.jpg',
94 thumbnailfile: 'video_short1.webm.jpg'
97 const live = await commands[0].create({ fields: attributes })
98 liveVideoUUID = live.uuid
100 await waitJobs(servers)
102 for (const server of servers) {
103 const resVideo = await getVideo(server.url, liveVideoUUID)
104 const video: VideoDetails = resVideo.body
106 expect(video.category.id).to.equal(1)
107 expect(video.licence.id).to.equal(2)
108 expect(video.language.id).to.equal('fr')
109 expect(video.description).to.equal('super live description')
110 expect(video.support).to.equal('support field')
112 expect(video.channel.name).to.equal(servers[0].videoChannel.name)
113 expect(video.channel.host).to.equal(servers[0].videoChannel.host)
115 expect(video.isLive).to.be.true
117 expect(video.nsfw).to.be.false
118 expect(video.waitTranscoding).to.be.false
119 expect(video.name).to.equal('my super live')
120 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
121 expect(video.commentsEnabled).to.be.false
122 expect(video.downloadEnabled).to.be.false
123 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
125 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
126 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
128 const live = await server.liveCommand.get({ videoId: liveVideoUUID })
130 if (server.url === servers[0].url) {
131 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
132 expect(live.streamKey).to.not.be.empty
134 expect(live.rtmpUrl).to.be.null
135 expect(live.streamKey).to.be.null
138 expect(live.saveReplay).to.be.true
142 it('Should have a default preview and thumbnail', async function () {
145 const attributes: LiveVideoCreate = {
146 name: 'default live thumbnail',
147 channelId: servers[0].videoChannel.id,
148 privacy: VideoPrivacy.UNLISTED,
152 const live = await commands[0].create({ fields: attributes })
153 const videoId = live.uuid
155 await waitJobs(servers)
157 for (const server of servers) {
158 const resVideo = await getVideo(server.url, videoId)
159 const video: VideoDetails = resVideo.body
161 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
162 expect(video.nsfw).to.be.true
164 await makeRawRequest(server.url + video.thumbnailPath, HttpStatusCode.OK_200)
165 await makeRawRequest(server.url + video.previewPath, HttpStatusCode.OK_200)
169 it('Should not have the live listed since nobody streams into', async function () {
170 for (const server of servers) {
171 const res = await getVideosList(server.url)
173 expect(res.body.total).to.equal(0)
174 expect(res.body.data).to.have.lengthOf(0)
178 it('Should not be able to update a live of another server', async function () {
179 await commands[1].update({ videoId: liveVideoUUID, fields: { saveReplay: false }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
182 it('Should update the live', async function () {
185 await commands[0].update({ videoId: liveVideoUUID, fields: { saveReplay: false } })
186 await waitJobs(servers)
189 it('Have the live updated', async function () {
190 for (const server of servers) {
191 const live = await server.liveCommand.get({ videoId: liveVideoUUID })
193 if (server.url === servers[0].url) {
194 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
195 expect(live.streamKey).to.not.be.empty
197 expect(live.rtmpUrl).to.be.null
198 expect(live.streamKey).to.be.null
201 expect(live.saveReplay).to.be.false
205 it('Delete the live', async function () {
208 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
209 await waitJobs(servers)
212 it('Should have the live deleted', async function () {
213 for (const server of servers) {
214 await getVideo(server.url, liveVideoUUID, HttpStatusCode.NOT_FOUND_404)
215 await server.liveCommand.get({ videoId: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
220 describe('Live filters', function () {
221 let ffmpegCommand: any
222 let liveVideoId: string
223 let vodVideoId: string
225 before(async function () {
228 vodVideoId = (await uploadVideoAndGetId({ server: servers[0], videoName: 'vod video' })).uuid
230 const liveOptions = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: servers[0].videoChannel.id }
231 const live = await commands[0].create({ fields: liveOptions })
232 liveVideoId = live.uuid
234 ffmpegCommand = await servers[0].liveCommand.sendRTMPStreamInVideo({ videoId: liveVideoId })
235 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
236 await waitJobs(servers)
239 it('Should only display lives', async function () {
240 const res = await getVideosWithFilters(servers[0].url, { isLive: true })
242 expect(res.body.total).to.equal(1)
243 expect(res.body.data).to.have.lengthOf(1)
244 expect(res.body.data[0].name).to.equal('live')
247 it('Should not display lives', async function () {
248 const res = await getVideosWithFilters(servers[0].url, { isLive: false })
250 expect(res.body.total).to.equal(1)
251 expect(res.body.data).to.have.lengthOf(1)
252 expect(res.body.data[0].name).to.equal('vod video')
255 it('Should display my lives', async function () {
258 await stopFfmpeg(ffmpegCommand)
259 await waitJobs(servers)
261 const res = await getMyVideosWithFilter(servers[0].url, servers[0].accessToken, { isLive: true })
262 const videos = res.body.data as Video[]
264 const result = videos.every(v => v.isLive)
265 expect(result).to.be.true
268 it('Should not display my lives', async function () {
269 const res = await getMyVideosWithFilter(servers[0].url, servers[0].accessToken, { isLive: false })
270 const videos = res.body.data as Video[]
272 const result = videos.every(v => !v.isLive)
273 expect(result).to.be.true
276 after(async function () {
277 await removeVideo(servers[0].url, servers[0].accessToken, vodVideoId)
278 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoId)
282 describe('Stream checks', function () {
283 let liveVideo: LiveVideo & VideoDetails
287 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
290 async function createLiveWrapper () {
291 const liveAttributes = {
293 channelId: servers[0].videoChannel.id,
294 privacy: VideoPrivacy.PUBLIC,
298 const { uuid } = await commands[0].create({ fields: liveAttributes })
300 const live = await commands[0].get({ videoId: uuid })
301 const resVideo = await getVideo(servers[0].url, uuid)
303 return Object.assign(resVideo.body as VideoDetails, live)
306 it('Should not allow a stream without the appropriate path', async function () {
309 liveVideo = await createLiveWrapper()
311 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
312 await testFfmpegStreamError(command, true)
315 it('Should not allow a stream without the appropriate stream key', async function () {
318 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
319 await testFfmpegStreamError(command, true)
322 it('Should succeed with the correct params', async function () {
325 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
326 await testFfmpegStreamError(command, false)
329 it('Should list this live now someone stream into it', async function () {
330 for (const server of servers) {
331 const res = await getVideosList(server.url)
333 expect(res.body.total).to.equal(1)
334 expect(res.body.data).to.have.lengthOf(1)
336 const video: Video = res.body.data[0]
338 expect(video.name).to.equal('user live')
339 expect(video.isLive).to.be.true
343 it('Should not allow a stream on a live that was blacklisted', async function () {
346 liveVideo = await createLiveWrapper()
348 await servers[0].blacklistCommand.add({ videoId: liveVideo.uuid })
350 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
351 await testFfmpegStreamError(command, true)
354 it('Should not allow a stream on a live that was deleted', async function () {
357 liveVideo = await createLiveWrapper()
359 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid)
361 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
362 await testFfmpegStreamError(command, true)
366 describe('Live transcoding', function () {
367 let liveVideoId: string
369 async function createLiveWrapper (saveReplay: boolean) {
370 const liveAttributes = {
372 channelId: servers[0].videoChannel.id,
373 privacy: VideoPrivacy.PUBLIC,
377 const { uuid } = await commands[0].create({ fields: liveAttributes })
381 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
382 for (const server of servers) {
383 const resList = await getVideosList(server.url)
384 const videos: Video[] = resList.body.data
386 expect(videos.find(v => v.uuid === liveVideoId)).to.exist
388 const resVideo = await getVideo(server.url, liveVideoId)
389 const video: VideoDetails = resVideo.body
391 expect(video.streamingPlaylists).to.have.lengthOf(1)
393 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
394 expect(hlsPlaylist).to.exist
396 // Only finite files are displayed
397 expect(hlsPlaylist.files).to.have.lengthOf(0)
399 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
401 for (let i = 0; i < resolutions.length; i++) {
403 const segmentName = `${i}-00000${segmentNum}.ts`
404 await commands[0].waitUntilSegmentGeneration({ videoUUID: video.uuid, resolution: i, segment: segmentNum })
406 const subPlaylist = await servers[0].streamingPlaylistsCommand.get({
407 url: `${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`
410 expect(subPlaylist).to.contain(segmentName)
412 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
413 await checkLiveSegmentHash({
415 baseUrlSegment: baseUrlAndPath,
416 videoUUID: video.uuid,
424 function updateConf (resolutions: number[]) {
425 return servers[0].configCommand.updateCustomSubConfig({
434 '240p': resolutions.includes(240),
435 '360p': resolutions.includes(360),
436 '480p': resolutions.includes(480),
437 '720p': resolutions.includes(720),
438 '1080p': resolutions.includes(1080),
439 '2160p': resolutions.includes(2160)
447 before(async function () {
451 it('Should enable transcoding without additional resolutions', async function () {
454 liveVideoId = await createLiveWrapper(false)
456 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
457 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
458 await waitJobs(servers)
460 await testVideoResolutions(liveVideoId, [ 720 ])
462 await stopFfmpeg(ffmpegCommand)
465 it('Should enable transcoding with some resolutions', async function () {
468 const resolutions = [ 240, 480 ]
469 await updateConf(resolutions)
470 liveVideoId = await createLiveWrapper(false)
472 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
473 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
474 await waitJobs(servers)
476 await testVideoResolutions(liveVideoId, resolutions)
478 await stopFfmpeg(ffmpegCommand)
481 it('Should enable transcoding with some resolutions and correctly save them', async function () {
484 const resolutions = [ 240, 360, 720 ]
486 await updateConf(resolutions)
487 liveVideoId = await createLiveWrapper(true)
489 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
490 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
491 await waitJobs(servers)
493 await testVideoResolutions(liveVideoId, resolutions)
495 await stopFfmpeg(ffmpegCommand)
496 await commands[0].waitUntilEnded({ videoId: liveVideoId })
498 await waitJobs(servers)
500 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
502 const bitrateLimits = {
503 720: 5000 * 1000, // 60FPS
508 for (const server of servers) {
509 const resVideo = await getVideo(server.url, liveVideoId)
510 const video: VideoDetails = resVideo.body
512 expect(video.state.id).to.equal(VideoState.PUBLISHED)
513 expect(video.duration).to.be.greaterThan(1)
514 expect(video.files).to.have.lengthOf(0)
516 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
517 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
518 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
520 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
522 for (const resolution of resolutions) {
523 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
525 expect(file).to.exist
526 expect(file.size).to.be.greaterThan(1)
528 if (resolution >= 720) {
529 expect(file.fps).to.be.approximately(60, 2)
531 expect(file.fps).to.be.approximately(30, 2)
534 const filename = `${video.uuid}-${resolution}-fragmented.mp4`
535 const segmentPath = buildServerDirectory(servers[0], join('streaming-playlists', 'hls', video.uuid, filename))
537 const probe = await ffprobePromise(segmentPath)
538 const videoStream = await getVideoStreamFromFile(segmentPath, probe)
540 expect(probe.format.bit_rate).to.be.below(bitrateLimits[videoStream.height])
542 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
543 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
548 it('Should correctly have cleaned up the live files', async function () {
551 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
555 describe('After a server restart', function () {
556 let liveVideoId: string
557 let liveVideoReplayId: string
559 async function createLiveWrapper (saveReplay: boolean) {
560 const liveAttributes = {
562 channelId: servers[0].videoChannel.id,
563 privacy: VideoPrivacy.PUBLIC,
567 const { uuid } = await commands[0].create({ fields: liveAttributes })
571 before(async function () {
574 liveVideoId = await createLiveWrapper(false)
575 liveVideoReplayId = await createLiveWrapper(true)
578 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId }),
579 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoReplayId })
583 commands[0].waitUntilPublished({ videoId: liveVideoId }),
584 commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
587 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoId, resolution: 0, segment: 2 })
588 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoReplayId, resolution: 0, segment: 2 })
590 await killallServers([ servers[0] ])
591 await reRunServer(servers[0])
596 it('Should cleanup lives', async function () {
599 await commands[0].waitUntilEnded({ videoId: liveVideoId })
602 it('Should save a live replay', async function () {
605 await commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
609 after(async function () {
610 await cleanupTests(servers)