1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { basename, join } from 'path'
5 import { ffprobePromise, getVideoStream } from '@server/helpers/ffmpeg'
6 import { testImage, testVideoResolutions } from '@server/tests/shared'
7 import { getAllFiles, wait } from '@shared/core-utils'
16 VideoStreamingPlaylistType
17 } from '@shared/models'
20 createMultipleServers,
28 setAccessTokensToServers,
29 setDefaultVideoChannel,
31 testFfmpegStreamError,
33 waitUntilLivePublishedOnAllServers
34 } from '@shared/server-commands'
36 describe('Test live', function () {
37 let servers: PeerTubeServer[] = []
38 let commands: LiveCommand[]
40 before(async function () {
43 servers = await createMultipleServers(2)
45 // Get the access tokens
46 await setAccessTokensToServers(servers)
47 await setDefaultVideoChannel(servers)
49 await servers[0].config.updateCustomSubConfig({
64 // Server 1 and server 2 follow each other
65 await doubleFollow(servers[0], servers[1])
67 commands = servers.map(s => s.live)
70 describe('Live creation, update and delete', function () {
71 let liveVideoUUID: string
73 it('Should create a live with the appropriate parameters', async function () {
76 const attributes: LiveVideoCreate = {
80 description: 'super live description',
81 support: 'support field',
82 channelId: servers[0].store.channel.id,
84 waitTranscoding: false,
85 name: 'my super live',
86 tags: [ 'tag1', 'tag2' ],
87 commentsEnabled: false,
88 downloadEnabled: false,
90 latencyMode: LiveVideoLatencyMode.SMALL_LATENCY,
91 privacy: VideoPrivacy.PUBLIC,
92 previewfile: 'video_short1-preview.webm.jpg',
93 thumbnailfile: 'video_short1.webm.jpg'
96 const live = await commands[0].create({ fields: attributes })
97 liveVideoUUID = live.uuid
99 await waitJobs(servers)
101 for (const server of servers) {
102 const video = await server.videos.get({ id: liveVideoUUID })
104 expect(video.category.id).to.equal(1)
105 expect(video.licence.id).to.equal(2)
106 expect(video.language.id).to.equal('fr')
107 expect(video.description).to.equal('super live description')
108 expect(video.support).to.equal('support field')
110 expect(video.channel.name).to.equal(servers[0].store.channel.name)
111 expect(video.channel.host).to.equal(servers[0].store.channel.host)
113 expect(video.isLive).to.be.true
115 expect(video.nsfw).to.be.false
116 expect(video.waitTranscoding).to.be.false
117 expect(video.name).to.equal('my super live')
118 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
119 expect(video.commentsEnabled).to.be.false
120 expect(video.downloadEnabled).to.be.false
121 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
123 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
124 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
126 const live = await server.live.get({ videoId: liveVideoUUID })
128 if (server.url === servers[0].url) {
129 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
130 expect(live.streamKey).to.not.be.empty
132 expect(live.rtmpUrl).to.not.exist
133 expect(live.streamKey).to.not.exist
136 expect(live.saveReplay).to.be.true
137 expect(live.latencyMode).to.equal(LiveVideoLatencyMode.SMALL_LATENCY)
141 it('Should have a default preview and thumbnail', async function () {
144 const attributes: LiveVideoCreate = {
145 name: 'default live thumbnail',
146 channelId: servers[0].store.channel.id,
147 privacy: VideoPrivacy.UNLISTED,
151 const live = await commands[0].create({ fields: attributes })
152 const videoId = live.uuid
154 await waitJobs(servers)
156 for (const server of servers) {
157 const video = await server.videos.get({ id: videoId })
158 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
159 expect(video.nsfw).to.be.true
161 await makeGetRequest({ url: server.url, path: video.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
162 await makeGetRequest({ url: server.url, path: video.previewPath, expectedStatus: HttpStatusCode.OK_200 })
166 it('Should not have the live listed since nobody streams into', async function () {
167 for (const server of servers) {
168 const { total, data } = await server.videos.list()
170 expect(total).to.equal(0)
171 expect(data).to.have.lengthOf(0)
175 it('Should not be able to update a live of another server', async function () {
176 await commands[1].update({ videoId: liveVideoUUID, fields: { saveReplay: false }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
179 it('Should update the live', async function () {
182 await commands[0].update({ videoId: liveVideoUUID, fields: { saveReplay: false, latencyMode: LiveVideoLatencyMode.DEFAULT } })
183 await waitJobs(servers)
186 it('Have the live updated', async function () {
187 for (const server of servers) {
188 const live = await server.live.get({ videoId: liveVideoUUID })
190 if (server.url === servers[0].url) {
191 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
192 expect(live.streamKey).to.not.be.empty
194 expect(live.rtmpUrl).to.not.exist
195 expect(live.streamKey).to.not.exist
198 expect(live.saveReplay).to.be.false
199 expect(live.latencyMode).to.equal(LiveVideoLatencyMode.DEFAULT)
203 it('Delete the live', async function () {
206 await servers[0].videos.remove({ id: liveVideoUUID })
207 await waitJobs(servers)
210 it('Should have the live deleted', async function () {
211 for (const server of servers) {
212 await server.videos.get({ id: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
213 await server.live.get({ videoId: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
218 describe('Live filters', function () {
219 let ffmpegCommand: any
220 let liveVideoId: string
221 let vodVideoId: string
223 before(async function () {
226 vodVideoId = (await servers[0].videos.quickUpload({ name: 'vod video' })).uuid
228 const liveOptions = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: servers[0].store.channel.id }
229 const live = await commands[0].create({ fields: liveOptions })
230 liveVideoId = live.uuid
232 ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoId })
233 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
234 await waitJobs(servers)
237 it('Should only display lives', async function () {
238 const { data, total } = await servers[0].videos.list({ isLive: true })
240 expect(total).to.equal(1)
241 expect(data).to.have.lengthOf(1)
242 expect(data[0].name).to.equal('live')
245 it('Should not display lives', async function () {
246 const { data, total } = await servers[0].videos.list({ isLive: false })
248 expect(total).to.equal(1)
249 expect(data).to.have.lengthOf(1)
250 expect(data[0].name).to.equal('vod video')
253 it('Should display my lives', async function () {
256 await stopFfmpeg(ffmpegCommand)
257 await waitJobs(servers)
259 const { data } = await servers[0].videos.listMyVideos({ isLive: true })
261 const result = data.every(v => v.isLive)
262 expect(result).to.be.true
265 it('Should not display my lives', async function () {
266 const { data } = await servers[0].videos.listMyVideos({ isLive: false })
268 const result = data.every(v => !v.isLive)
269 expect(result).to.be.true
272 after(async function () {
273 await servers[0].videos.remove({ id: vodVideoId })
274 await servers[0].videos.remove({ id: liveVideoId })
278 describe('Stream checks', function () {
279 let liveVideo: LiveVideo & VideoDetails
283 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
286 async function createLiveWrapper () {
287 const liveAttributes = {
289 channelId: servers[0].store.channel.id,
290 privacy: VideoPrivacy.PUBLIC,
294 const { uuid } = await commands[0].create({ fields: liveAttributes })
296 const live = await commands[0].get({ videoId: uuid })
297 const video = await servers[0].videos.get({ id: uuid })
299 return Object.assign(video, live)
302 it('Should not allow a stream without the appropriate path', async function () {
305 liveVideo = await createLiveWrapper()
307 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/bad-live', streamKey: liveVideo.streamKey })
308 await testFfmpegStreamError(command, true)
311 it('Should not allow a stream without the appropriate stream key', async function () {
314 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: 'bad-stream-key' })
315 await testFfmpegStreamError(command, true)
318 it('Should succeed with the correct params', async function () {
321 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
322 await testFfmpegStreamError(command, false)
325 it('Should list this live now someone stream into it', async function () {
326 for (const server of servers) {
327 const { total, data } = await server.videos.list()
329 expect(total).to.equal(1)
330 expect(data).to.have.lengthOf(1)
332 const video = data[0]
333 expect(video.name).to.equal('user live')
334 expect(video.isLive).to.be.true
338 it('Should not allow a stream on a live that was blacklisted', async function () {
341 liveVideo = await createLiveWrapper()
343 await servers[0].blacklist.add({ videoId: liveVideo.uuid })
345 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
346 await testFfmpegStreamError(command, true)
349 it('Should not allow a stream on a live that was deleted', async function () {
352 liveVideo = await createLiveWrapper()
354 await servers[0].videos.remove({ id: liveVideo.uuid })
356 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
357 await testFfmpegStreamError(command, true)
361 describe('Live transcoding', function () {
362 let liveVideoId: string
364 async function createLiveWrapper (saveReplay: boolean) {
365 const liveAttributes = {
367 channelId: servers[0].store.channel.id,
368 privacy: VideoPrivacy.PUBLIC,
372 const { uuid } = await commands[0].create({ fields: liveAttributes })
376 function updateConf (resolutions: number[]) {
377 return servers[0].config.updateCustomSubConfig({
386 '144p': resolutions.includes(144),
387 '240p': resolutions.includes(240),
388 '360p': resolutions.includes(360),
389 '480p': resolutions.includes(480),
390 '720p': resolutions.includes(720),
391 '1080p': resolutions.includes(1080),
392 '2160p': resolutions.includes(2160)
400 before(async function () {
404 it('Should enable transcoding without additional resolutions', async function () {
407 liveVideoId = await createLiveWrapper(false)
409 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
410 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
411 await waitJobs(servers)
413 await testVideoResolutions({
414 originServer: servers[0],
417 resolutions: [ 720 ],
418 objectStorage: false,
422 await stopFfmpeg(ffmpegCommand)
425 it('Should transcode audio only RTMP stream', async function () {
428 liveVideoId = await createLiveWrapper(false)
430 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short_no_audio.mp4' })
431 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
432 await waitJobs(servers)
434 await stopFfmpeg(ffmpegCommand)
437 it('Should enable transcoding with some resolutions', async function () {
440 const resolutions = [ 240, 480 ]
441 await updateConf(resolutions)
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({
449 originServer: servers[0],
452 resolutions: resolutions.concat([ 720 ]),
453 objectStorage: false,
457 await stopFfmpeg(ffmpegCommand)
460 it('Should correctly set the appropriate bitrate depending on the input', async function () {
463 liveVideoId = await createLiveWrapper(false)
465 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({
466 videoId: liveVideoId,
467 fixtureName: 'video_short.mp4',
470 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
471 await waitJobs(servers)
473 const video = await servers[0].videos.get({ id: liveVideoId })
475 const masterPlaylist = video.streamingPlaylists[0].playlistUrl
476 const probe = await ffprobePromise(masterPlaylist)
478 const bitrates = probe.streams.map(s => parseInt(s.tags.variant_bitrate))
479 for (const bitrate of bitrates) {
480 expect(bitrate).to.exist
481 expect(isNaN(bitrate)).to.be.false
482 expect(bitrate).to.be.below(61_000_000) // video_short.mp4 bitrate
485 await stopFfmpeg(ffmpegCommand)
488 it('Should enable transcoding with some resolutions and correctly save them', async function () {
489 this.timeout(400_000)
491 const resolutions = [ 240, 360, 720 ]
493 await updateConf(resolutions)
494 liveVideoId = await createLiveWrapper(true)
496 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
497 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
498 await waitJobs(servers)
500 await testVideoResolutions({
501 originServer: servers[0],
505 objectStorage: false,
509 await stopFfmpeg(ffmpegCommand)
510 await commands[0].waitUntilEnded({ videoId: liveVideoId })
512 await waitJobs(servers)
514 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
516 const maxBitrateLimits = {
517 720: 6500 * 1000, // 60FPS
522 const minBitrateLimits = {
528 for (const server of servers) {
529 const video = await server.videos.get({ id: liveVideoId })
531 expect(video.state.id).to.equal(VideoState.PUBLISHED)
532 expect(video.duration).to.be.greaterThan(1)
533 expect(video.files).to.have.lengthOf(0)
535 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
536 await makeRawRequest({ url: hlsPlaylist.playlistUrl, expectedStatus: HttpStatusCode.OK_200 })
537 await makeRawRequest({ url: hlsPlaylist.segmentsSha256Url, expectedStatus: HttpStatusCode.OK_200 })
539 // We should have generated random filenames
540 expect(basename(hlsPlaylist.playlistUrl)).to.not.equal('master.m3u8')
541 expect(basename(hlsPlaylist.segmentsSha256Url)).to.not.equal('segments-sha256.json')
543 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
545 for (const resolution of resolutions) {
546 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
548 expect(file).to.exist
549 expect(file.size).to.be.greaterThan(1)
551 if (resolution >= 720) {
552 expect(file.fps).to.be.approximately(60, 10)
554 expect(file.fps).to.be.approximately(30, 3)
557 const filename = basename(file.fileUrl)
558 expect(filename).to.not.contain(video.uuid)
560 const segmentPath = servers[0].servers.buildDirectory(join('streaming-playlists', 'hls', video.uuid, filename))
562 const probe = await ffprobePromise(segmentPath)
563 const videoStream = await getVideoStream(segmentPath, probe)
565 expect(probe.format.bit_rate).to.be.below(maxBitrateLimits[videoStream.height])
566 expect(probe.format.bit_rate).to.be.at.least(minBitrateLimits[videoStream.height])
568 await makeRawRequest({ url: file.torrentUrl, expectedStatus: HttpStatusCode.OK_200 })
569 await makeRawRequest({ url: file.fileUrl, expectedStatus: HttpStatusCode.OK_200 })
574 it('Should not generate an upper resolution than original file', async function () {
575 this.timeout(400_000)
577 const resolutions = [ 240, 480 ]
578 await updateConf(resolutions)
580 await servers[0].config.updateExistingSubConfig({
584 alwaysTranscodeOriginalResolution: false
590 liveVideoId = await createLiveWrapper(true)
592 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
593 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
594 await waitJobs(servers)
596 await testVideoResolutions({
597 originServer: servers[0],
601 objectStorage: false,
605 await stopFfmpeg(ffmpegCommand)
606 await commands[0].waitUntilEnded({ videoId: liveVideoId })
608 await waitJobs(servers)
610 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
612 const video = await servers[0].videos.get({ id: liveVideoId })
613 const hlsFiles = video.streamingPlaylists[0].files
615 expect(video.files).to.have.lengthOf(0)
616 expect(hlsFiles).to.have.lengthOf(resolutions.length)
618 // eslint-disable-next-line @typescript-eslint/require-array-sort-compare
619 expect(getAllFiles(video).map(f => f.resolution.id).sort()).to.deep.equal(resolutions)
622 it('Should only keep the original resolution if all resolutions are disabled', async function () {
623 this.timeout(600_000)
626 liveVideoId = await createLiveWrapper(true)
628 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
629 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
630 await waitJobs(servers)
632 await testVideoResolutions({
633 originServer: servers[0],
636 resolutions: [ 720 ],
637 objectStorage: false,
641 await stopFfmpeg(ffmpegCommand)
642 await commands[0].waitUntilEnded({ videoId: liveVideoId })
644 await waitJobs(servers)
646 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
648 const video = await servers[0].videos.get({ id: liveVideoId })
649 const hlsFiles = video.streamingPlaylists[0].files
651 expect(video.files).to.have.lengthOf(0)
652 expect(hlsFiles).to.have.lengthOf(1)
654 expect(hlsFiles[0].resolution.id).to.equal(720)
658 describe('After a server restart', function () {
659 let liveVideoId: string
660 let liveVideoReplayId: string
661 let permanentLiveVideoReplayId: string
663 let permanentLiveReplayName: string
665 let beforeServerRestart: Date
667 async function createLiveWrapper (options: { saveReplay: boolean, permanent: boolean }) {
668 const liveAttributes: LiveVideoCreate = {
670 channelId: servers[0].store.channel.id,
671 privacy: VideoPrivacy.PUBLIC,
672 saveReplay: options.saveReplay,
673 permanentLive: options.permanent
676 const { uuid } = await commands[0].create({ fields: liveAttributes })
680 before(async function () {
681 this.timeout(600_000)
683 liveVideoId = await createLiveWrapper({ saveReplay: false, permanent: false })
684 liveVideoReplayId = await createLiveWrapper({ saveReplay: true, permanent: false })
685 permanentLiveVideoReplayId = await createLiveWrapper({ saveReplay: true, permanent: true })
688 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId }),
689 commands[0].sendRTMPStreamInVideo({ videoId: permanentLiveVideoReplayId }),
690 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoReplayId })
694 commands[0].waitUntilPublished({ videoId: liveVideoId }),
695 commands[0].waitUntilPublished({ videoId: permanentLiveVideoReplayId }),
696 commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
699 for (const videoUUID of [ liveVideoId, liveVideoReplayId, permanentLiveVideoReplayId ]) {
700 await commands[0].waitUntilSegmentGeneration({
710 const video = await servers[0].videos.get({ id: permanentLiveVideoReplayId })
711 permanentLiveReplayName = video.name + ' - ' + new Date(video.publishedAt).toLocaleString()
714 await killallServers([ servers[0] ])
716 beforeServerRestart = new Date()
717 await servers[0].run()
720 await waitJobs(servers)
723 it('Should cleanup lives', async function () {
726 await commands[0].waitUntilEnded({ videoId: liveVideoId })
727 await commands[0].waitUntilWaiting({ videoId: permanentLiveVideoReplayId })
730 it('Should save a non permanent live replay', async function () {
733 await commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
735 const session = await commands[0].getReplaySession({ videoId: liveVideoReplayId })
736 expect(session.endDate).to.exist
737 expect(new Date(session.endDate)).to.be.above(beforeServerRestart)
740 it('Should have saved a permanent live replay', async function () {
743 const { data } = await servers[0].videos.listMyVideos({ sort: '-publishedAt' })
744 expect(data.find(v => v.name === permanentLiveReplayName)).to.exist
748 after(async function () {
749 await cleanupTests(servers)