1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { basename, join } from 'path'
6 import { ffprobePromise, getVideoStreamFromFile } from '@server/helpers/ffprobe-utils'
7 import { checkLiveCleanupAfterSave, checkLiveSegmentHash, checkResolutionsInMasterPlaylist, testImage } from '@server/tests/shared'
8 import { wait } from '@shared/core-utils'
16 VideoStreamingPlaylistType
17 } from '@shared/models'
20 createMultipleServers,
27 setAccessTokensToServers,
28 setDefaultVideoChannel,
30 testFfmpegStreamError,
32 waitUntilLivePublishedOnAllServers
33 } from '@shared/server-commands'
35 const expect = chai.expect
37 describe('Test live', function () {
38 let servers: PeerTubeServer[] = []
39 let commands: LiveCommand[]
41 before(async function () {
44 servers = await createMultipleServers(2)
46 // Get the access tokens
47 await setAccessTokensToServers(servers)
48 await setDefaultVideoChannel(servers)
50 await servers[0].config.updateCustomSubConfig({
62 // Server 1 and server 2 follow each other
63 await doubleFollow(servers[0], servers[1])
65 commands = servers.map(s => s.live)
68 describe('Live creation, update and delete', function () {
69 let liveVideoUUID: string
71 it('Should create a live with the appropriate parameters', async function () {
74 const attributes: LiveVideoCreate = {
78 description: 'super live description',
79 support: 'support field',
80 channelId: servers[0].store.channel.id,
82 waitTranscoding: false,
83 name: 'my super live',
84 tags: [ 'tag1', 'tag2' ],
85 commentsEnabled: false,
86 downloadEnabled: false,
88 privacy: VideoPrivacy.PUBLIC,
89 previewfile: 'video_short1-preview.webm.jpg',
90 thumbnailfile: 'video_short1.webm.jpg'
93 const live = await commands[0].create({ fields: attributes })
94 liveVideoUUID = live.uuid
96 await waitJobs(servers)
98 for (const server of servers) {
99 const video = await server.videos.get({ id: liveVideoUUID })
101 expect(video.category.id).to.equal(1)
102 expect(video.licence.id).to.equal(2)
103 expect(video.language.id).to.equal('fr')
104 expect(video.description).to.equal('super live description')
105 expect(video.support).to.equal('support field')
107 expect(video.channel.name).to.equal(servers[0].store.channel.name)
108 expect(video.channel.host).to.equal(servers[0].store.channel.host)
110 expect(video.isLive).to.be.true
112 expect(video.nsfw).to.be.false
113 expect(video.waitTranscoding).to.be.false
114 expect(video.name).to.equal('my super live')
115 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
116 expect(video.commentsEnabled).to.be.false
117 expect(video.downloadEnabled).to.be.false
118 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
120 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
121 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
123 const live = await server.live.get({ videoId: liveVideoUUID })
125 if (server.url === servers[0].url) {
126 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
127 expect(live.streamKey).to.not.be.empty
129 expect(live.rtmpUrl).to.be.null
130 expect(live.streamKey).to.be.null
133 expect(live.saveReplay).to.be.true
137 it('Should have a default preview and thumbnail', async function () {
140 const attributes: LiveVideoCreate = {
141 name: 'default live thumbnail',
142 channelId: servers[0].store.channel.id,
143 privacy: VideoPrivacy.UNLISTED,
147 const live = await commands[0].create({ fields: attributes })
148 const videoId = live.uuid
150 await waitJobs(servers)
152 for (const server of servers) {
153 const video = await server.videos.get({ id: videoId })
154 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
155 expect(video.nsfw).to.be.true
157 await makeRawRequest(server.url + video.thumbnailPath, HttpStatusCode.OK_200)
158 await makeRawRequest(server.url + video.previewPath, HttpStatusCode.OK_200)
162 it('Should not have the live listed since nobody streams into', async function () {
163 for (const server of servers) {
164 const { total, data } = await server.videos.list()
166 expect(total).to.equal(0)
167 expect(data).to.have.lengthOf(0)
171 it('Should not be able to update a live of another server', async function () {
172 await commands[1].update({ videoId: liveVideoUUID, fields: { saveReplay: false }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
175 it('Should update the live', async function () {
178 await commands[0].update({ videoId: liveVideoUUID, fields: { saveReplay: false } })
179 await waitJobs(servers)
182 it('Have the live updated', async function () {
183 for (const server of servers) {
184 const live = await server.live.get({ videoId: liveVideoUUID })
186 if (server.url === servers[0].url) {
187 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
188 expect(live.streamKey).to.not.be.empty
190 expect(live.rtmpUrl).to.be.null
191 expect(live.streamKey).to.be.null
194 expect(live.saveReplay).to.be.false
198 it('Delete the live', async function () {
201 await servers[0].videos.remove({ id: liveVideoUUID })
202 await waitJobs(servers)
205 it('Should have the live deleted', async function () {
206 for (const server of servers) {
207 await server.videos.get({ id: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
208 await server.live.get({ videoId: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
213 describe('Live filters', function () {
214 let ffmpegCommand: any
215 let liveVideoId: string
216 let vodVideoId: string
218 before(async function () {
221 vodVideoId = (await servers[0].videos.quickUpload({ name: 'vod video' })).uuid
223 const liveOptions = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: servers[0].store.channel.id }
224 const live = await commands[0].create({ fields: liveOptions })
225 liveVideoId = live.uuid
227 ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoId })
228 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
229 await waitJobs(servers)
232 it('Should only display lives', async function () {
233 const { data, total } = await servers[0].videos.list({ isLive: true })
235 expect(total).to.equal(1)
236 expect(data).to.have.lengthOf(1)
237 expect(data[0].name).to.equal('live')
240 it('Should not display lives', async function () {
241 const { data, total } = await servers[0].videos.list({ isLive: false })
243 expect(total).to.equal(1)
244 expect(data).to.have.lengthOf(1)
245 expect(data[0].name).to.equal('vod video')
248 it('Should display my lives', async function () {
251 await stopFfmpeg(ffmpegCommand)
252 await waitJobs(servers)
254 const { data } = await servers[0].videos.listMyVideos({ isLive: true })
256 const result = data.every(v => v.isLive)
257 expect(result).to.be.true
260 it('Should not display my lives', async function () {
261 const { data } = await servers[0].videos.listMyVideos({ isLive: false })
263 const result = data.every(v => !v.isLive)
264 expect(result).to.be.true
267 after(async function () {
268 await servers[0].videos.remove({ id: vodVideoId })
269 await servers[0].videos.remove({ id: liveVideoId })
273 describe('Stream checks', function () {
274 let liveVideo: LiveVideo & VideoDetails
278 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
281 async function createLiveWrapper () {
282 const liveAttributes = {
284 channelId: servers[0].store.channel.id,
285 privacy: VideoPrivacy.PUBLIC,
289 const { uuid } = await commands[0].create({ fields: liveAttributes })
291 const live = await commands[0].get({ videoId: uuid })
292 const video = await servers[0].videos.get({ id: uuid })
294 return Object.assign(video, live)
297 it('Should not allow a stream without the appropriate path', async function () {
300 liveVideo = await createLiveWrapper()
302 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/bad-live', streamKey: liveVideo.streamKey })
303 await testFfmpegStreamError(command, true)
306 it('Should not allow a stream without the appropriate stream key', async function () {
309 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: 'bad-stream-key' })
310 await testFfmpegStreamError(command, true)
313 it('Should succeed with the correct params', async function () {
316 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
317 await testFfmpegStreamError(command, false)
320 it('Should list this live now someone stream into it', async function () {
321 for (const server of servers) {
322 const { total, data } = await server.videos.list()
324 expect(total).to.equal(1)
325 expect(data).to.have.lengthOf(1)
327 const video = data[0]
328 expect(video.name).to.equal('user live')
329 expect(video.isLive).to.be.true
333 it('Should not allow a stream on a live that was blacklisted', async function () {
336 liveVideo = await createLiveWrapper()
338 await servers[0].blacklist.add({ videoId: liveVideo.uuid })
340 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
341 await testFfmpegStreamError(command, true)
344 it('Should not allow a stream on a live that was deleted', async function () {
347 liveVideo = await createLiveWrapper()
349 await servers[0].videos.remove({ id: liveVideo.uuid })
351 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
352 await testFfmpegStreamError(command, true)
356 describe('Live transcoding', function () {
357 let liveVideoId: string
359 async function createLiveWrapper (saveReplay: boolean) {
360 const liveAttributes = {
362 channelId: servers[0].store.channel.id,
363 privacy: VideoPrivacy.PUBLIC,
367 const { uuid } = await commands[0].create({ fields: liveAttributes })
371 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
372 for (const server of servers) {
373 const { data } = await server.videos.list()
374 expect(data.find(v => v.uuid === liveVideoId)).to.exist
376 const video = await server.videos.get({ id: liveVideoId })
378 expect(video.streamingPlaylists).to.have.lengthOf(1)
380 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
381 expect(hlsPlaylist).to.exist
383 // Only finite files are displayed
384 expect(hlsPlaylist.files).to.have.lengthOf(0)
386 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
388 for (let i = 0; i < resolutions.length; i++) {
390 const segmentName = `${i}-00000${segmentNum}.ts`
391 await commands[0].waitUntilSegmentGeneration({ videoUUID: video.uuid, resolution: i, segment: segmentNum })
393 const subPlaylist = await servers[0].streamingPlaylists.get({
394 url: `${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`
397 expect(subPlaylist).to.contain(segmentName)
399 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
400 await checkLiveSegmentHash({
402 baseUrlSegment: baseUrlAndPath,
403 videoUUID: video.uuid,
411 function updateConf (resolutions: number[]) {
412 return servers[0].config.updateCustomSubConfig({
421 '144p': resolutions.includes(144),
422 '240p': resolutions.includes(240),
423 '360p': resolutions.includes(360),
424 '480p': resolutions.includes(480),
425 '720p': resolutions.includes(720),
426 '1080p': resolutions.includes(1080),
427 '2160p': resolutions.includes(2160)
435 before(async function () {
439 it('Should enable transcoding without additional resolutions', async function () {
442 liveVideoId = await createLiveWrapper(false)
444 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
445 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
446 await waitJobs(servers)
448 await testVideoResolutions(liveVideoId, [ 720 ])
450 await stopFfmpeg(ffmpegCommand)
453 it('Should enable transcoding with some resolutions', async function () {
456 const resolutions = [ 240, 480 ]
457 await updateConf(resolutions)
458 liveVideoId = await createLiveWrapper(false)
460 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
461 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
462 await waitJobs(servers)
464 await testVideoResolutions(liveVideoId, resolutions)
466 await stopFfmpeg(ffmpegCommand)
469 it('Should correctly set the appropriate bitrate depending on the input', async function () {
472 liveVideoId = await createLiveWrapper(false)
474 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({
475 videoId: liveVideoId,
476 fixtureName: 'video_short.mp4',
479 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
480 await waitJobs(servers)
482 const video = await servers[0].videos.get({ id: liveVideoId })
484 const masterPlaylist = video.streamingPlaylists[0].playlistUrl
485 const probe = await ffprobePromise(masterPlaylist)
487 const bitrates = probe.streams.map(s => parseInt(s.tags.variant_bitrate))
488 for (const bitrate of bitrates) {
489 expect(bitrate).to.exist
490 expect(isNaN(bitrate)).to.be.false
491 expect(bitrate).to.be.below(61_000_000) // video_short.mp4 bitrate
494 await stopFfmpeg(ffmpegCommand)
497 it('Should enable transcoding with some resolutions and correctly save them', async function () {
498 this.timeout(400_000)
500 const resolutions = [ 240, 360, 720 ]
502 await updateConf(resolutions)
503 liveVideoId = await createLiveWrapper(true)
505 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
506 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
507 await waitJobs(servers)
509 await testVideoResolutions(liveVideoId, resolutions)
511 await stopFfmpeg(ffmpegCommand)
512 await commands[0].waitUntilEnded({ videoId: liveVideoId })
514 await waitJobs(servers)
516 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
518 const maxBitrateLimits = {
519 720: 6500 * 1000, // 60FPS
524 const minBitrateLimits = {
530 for (const server of servers) {
531 const video = await server.videos.get({ id: liveVideoId })
533 expect(video.state.id).to.equal(VideoState.PUBLISHED)
534 expect(video.duration).to.be.greaterThan(1)
535 expect(video.files).to.have.lengthOf(0)
537 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
538 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
539 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
541 // We should have generated random filenames
542 expect(basename(hlsPlaylist.playlistUrl)).to.not.equal('master.m3u8')
543 expect(basename(hlsPlaylist.segmentsSha256Url)).to.not.equal('segments-sha256.json')
545 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
547 for (const resolution of resolutions) {
548 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
550 expect(file).to.exist
551 expect(file.size).to.be.greaterThan(1)
553 if (resolution >= 720) {
554 expect(file.fps).to.be.approximately(60, 2)
556 expect(file.fps).to.be.approximately(30, 2)
559 const filename = basename(file.fileUrl)
560 expect(filename).to.not.contain(video.uuid)
562 const segmentPath = servers[0].servers.buildDirectory(join('streaming-playlists', 'hls', video.uuid, filename))
564 const probe = await ffprobePromise(segmentPath)
565 const videoStream = await getVideoStreamFromFile(segmentPath, probe)
567 expect(probe.format.bit_rate).to.be.below(maxBitrateLimits[videoStream.height])
568 expect(probe.format.bit_rate).to.be.at.least(minBitrateLimits[videoStream.height])
570 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
571 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
576 it('Should correctly have cleaned up the live files', async function () {
579 await checkLiveCleanupAfterSave(servers[0], liveVideoId, [ 240, 360, 720 ])
583 describe('After a server restart', function () {
584 let liveVideoId: string
585 let liveVideoReplayId: string
587 async function createLiveWrapper (saveReplay: boolean) {
588 const liveAttributes = {
590 channelId: servers[0].store.channel.id,
591 privacy: VideoPrivacy.PUBLIC,
595 const { uuid } = await commands[0].create({ fields: liveAttributes })
599 before(async function () {
602 liveVideoId = await createLiveWrapper(false)
603 liveVideoReplayId = await createLiveWrapper(true)
606 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId }),
607 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoReplayId })
611 commands[0].waitUntilPublished({ videoId: liveVideoId }),
612 commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
615 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoId, resolution: 0, segment: 2 })
616 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoReplayId, resolution: 0, segment: 2 })
618 await killallServers([ servers[0] ])
619 await servers[0].run()
624 it('Should cleanup lives', async function () {
627 await commands[0].waitUntilEnded({ videoId: liveVideoId })
630 it('Should save a live replay', async function () {
633 await commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
637 after(async function () {
638 await cleanupTests(servers)