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 { SQLCommand, testImage, testLiveVideoResolutions } from '@server/tests/shared'
6 import { getAllFiles, wait } from '@shared/core-utils'
7 import { ffprobePromise, getVideoStream } from '@shared/ffmpeg'
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 replaySettings: { privacy: VideoPrivacy.PUBLIC },
91 latencyMode: LiveVideoLatencyMode.SMALL_LATENCY,
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 video = await server.videos.get({ id: liveVideoUUID })
105 expect(video.category.id).to.equal(1)
106 expect(video.licence.id).to.equal(2)
107 expect(video.language.id).to.equal('fr')
108 expect(video.description).to.equal('super live description')
109 expect(video.support).to.equal('support field')
111 expect(video.channel.name).to.equal(servers[0].store.channel.name)
112 expect(video.channel.host).to.equal(servers[0].store.channel.host)
114 expect(video.isLive).to.be.true
116 expect(video.nsfw).to.be.false
117 expect(video.waitTranscoding).to.be.false
118 expect(video.name).to.equal('my super live')
119 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
120 expect(video.commentsEnabled).to.be.false
121 expect(video.downloadEnabled).to.be.false
122 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
124 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
125 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
127 const live = await server.live.get({ videoId: liveVideoUUID })
129 if (server.url === servers[0].url) {
130 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
131 expect(live.streamKey).to.not.be.empty
133 expect(live.replaySettings).to.exist
134 expect(live.replaySettings.privacy).to.equal(VideoPrivacy.PUBLIC)
136 expect(live.rtmpUrl).to.not.exist
137 expect(live.streamKey).to.not.exist
140 expect(live.saveReplay).to.be.true
141 expect(live.latencyMode).to.equal(LiveVideoLatencyMode.SMALL_LATENCY)
145 it('Should have a default preview and thumbnail', async function () {
148 const attributes: LiveVideoCreate = {
149 name: 'default live thumbnail',
150 channelId: servers[0].store.channel.id,
151 privacy: VideoPrivacy.UNLISTED,
155 const live = await commands[0].create({ fields: attributes })
156 const videoId = live.uuid
158 await waitJobs(servers)
160 for (const server of servers) {
161 const video = await server.videos.get({ id: videoId })
162 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
163 expect(video.nsfw).to.be.true
165 await makeGetRequest({ url: server.url, path: video.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
166 await makeGetRequest({ url: server.url, path: video.previewPath, expectedStatus: HttpStatusCode.OK_200 })
170 it('Should not have the live listed since nobody streams into', async function () {
171 for (const server of servers) {
172 const { total, data } = await server.videos.list()
174 expect(total).to.equal(0)
175 expect(data).to.have.lengthOf(0)
179 it('Should not be able to update a live of another server', async function () {
180 await commands[1].update({ videoId: liveVideoUUID, fields: { saveReplay: false }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
183 it('Should update the live', async function () {
184 await commands[0].update({ videoId: liveVideoUUID, fields: { saveReplay: false, latencyMode: LiveVideoLatencyMode.DEFAULT } })
185 await waitJobs(servers)
188 it('Have the live updated', async function () {
189 for (const server of servers) {
190 const live = await server.live.get({ videoId: liveVideoUUID })
192 if (server.url === servers[0].url) {
193 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
194 expect(live.streamKey).to.not.be.empty
196 expect(live.rtmpUrl).to.not.exist
197 expect(live.streamKey).to.not.exist
200 expect(live.saveReplay).to.be.false
201 expect(live.replaySettings).to.not.exist
202 expect(live.latencyMode).to.equal(LiveVideoLatencyMode.DEFAULT)
206 it('Delete the live', async function () {
207 await servers[0].videos.remove({ id: liveVideoUUID })
208 await waitJobs(servers)
211 it('Should have the live deleted', async function () {
212 for (const server of servers) {
213 await server.videos.get({ id: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
214 await server.live.get({ videoId: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
219 describe('Live filters', function () {
220 let ffmpegCommand: any
221 let liveVideoId: string
222 let vodVideoId: string
224 before(async function () {
227 vodVideoId = (await servers[0].videos.quickUpload({ name: 'vod video' })).uuid
229 const liveOptions = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: servers[0].store.channel.id }
230 const live = await commands[0].create({ fields: liveOptions })
231 liveVideoId = live.uuid
233 ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoId })
234 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
235 await waitJobs(servers)
238 it('Should only display lives', async function () {
239 const { data, total } = await servers[0].videos.list({ isLive: true })
241 expect(total).to.equal(1)
242 expect(data).to.have.lengthOf(1)
243 expect(data[0].name).to.equal('live')
246 it('Should not display lives', async function () {
247 const { data, total } = await servers[0].videos.list({ isLive: false })
249 expect(total).to.equal(1)
250 expect(data).to.have.lengthOf(1)
251 expect(data[0].name).to.equal('vod video')
254 it('Should display my lives', async function () {
257 await stopFfmpeg(ffmpegCommand)
258 await waitJobs(servers)
260 const { data } = await servers[0].videos.listMyVideos({ isLive: true })
262 const result = data.every(v => v.isLive)
263 expect(result).to.be.true
266 it('Should not display my lives', async function () {
267 const { data } = await servers[0].videos.listMyVideos({ isLive: false })
269 const result = data.every(v => !v.isLive)
270 expect(result).to.be.true
273 after(async function () {
274 await servers[0].videos.remove({ id: vodVideoId })
275 await servers[0].videos.remove({ id: liveVideoId })
279 describe('Stream checks', function () {
280 let liveVideo: LiveVideo & VideoDetails
284 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
287 async function createLiveWrapper () {
288 const liveAttributes = {
290 channelId: servers[0].store.channel.id,
291 privacy: VideoPrivacy.PUBLIC,
295 const { uuid } = await commands[0].create({ fields: liveAttributes })
297 const live = await commands[0].get({ videoId: uuid })
298 const video = await servers[0].videos.get({ id: uuid })
300 return Object.assign(video, live)
303 it('Should not allow a stream without the appropriate path', async function () {
306 liveVideo = await createLiveWrapper()
308 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/bad-live', streamKey: liveVideo.streamKey })
309 await testFfmpegStreamError(command, true)
312 it('Should not allow a stream without the appropriate stream key', async function () {
315 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: 'bad-stream-key' })
316 await testFfmpegStreamError(command, true)
319 it('Should succeed with the correct params', async function () {
322 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
323 await testFfmpegStreamError(command, false)
326 it('Should list this live now someone stream into it', async function () {
327 for (const server of servers) {
328 const { total, data } = await server.videos.list()
330 expect(total).to.equal(1)
331 expect(data).to.have.lengthOf(1)
333 const video = data[0]
334 expect(video.name).to.equal('user live')
335 expect(video.isLive).to.be.true
339 it('Should not allow a stream on a live that was blacklisted', async function () {
342 liveVideo = await createLiveWrapper()
344 await servers[0].blacklist.add({ videoId: liveVideo.uuid })
346 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
347 await testFfmpegStreamError(command, true)
350 it('Should not allow a stream on a live that was deleted', async function () {
353 liveVideo = await createLiveWrapper()
355 await servers[0].videos.remove({ id: liveVideo.uuid })
357 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
358 await testFfmpegStreamError(command, true)
362 describe('Live transcoding', function () {
363 let liveVideoId: string
364 let sqlCommandServer1: SQLCommand
366 async function createLiveWrapper (saveReplay: boolean) {
367 const liveAttributes = {
369 channelId: servers[0].store.channel.id,
370 privacy: VideoPrivacy.PUBLIC,
372 replaySettings: saveReplay
373 ? { privacy: VideoPrivacy.PUBLIC }
377 const { uuid } = await commands[0].create({ fields: liveAttributes })
381 function updateConf (resolutions: number[]) {
382 return servers[0].config.updateCustomSubConfig({
391 '144p': resolutions.includes(144),
392 '240p': resolutions.includes(240),
393 '360p': resolutions.includes(360),
394 '480p': resolutions.includes(480),
395 '720p': resolutions.includes(720),
396 '1080p': resolutions.includes(1080),
397 '2160p': resolutions.includes(2160)
405 before(async function () {
408 sqlCommandServer1 = new SQLCommand(servers[0])
411 it('Should enable transcoding without additional resolutions', async function () {
414 liveVideoId = await createLiveWrapper(false)
416 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
417 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
418 await waitJobs(servers)
420 await testLiveVideoResolutions({
421 originServer: servers[0],
422 sqlCommand: sqlCommandServer1,
425 resolutions: [ 720 ],
429 await stopFfmpeg(ffmpegCommand)
432 it('Should transcode audio only RTMP stream', async function () {
435 liveVideoId = await createLiveWrapper(false)
437 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short_no_audio.mp4' })
438 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
439 await waitJobs(servers)
441 await stopFfmpeg(ffmpegCommand)
444 it('Should enable transcoding with some resolutions', async function () {
447 const resolutions = [ 240, 480 ]
448 await updateConf(resolutions)
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 testLiveVideoResolutions({
456 originServer: servers[0],
457 sqlCommand: sqlCommandServer1,
460 resolutions: resolutions.concat([ 720 ]),
464 await stopFfmpeg(ffmpegCommand)
467 it('Should correctly set the appropriate bitrate depending on the input', async function () {
470 liveVideoId = await createLiveWrapper(false)
472 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({
473 videoId: liveVideoId,
474 fixtureName: 'video_short.mp4',
477 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
478 await waitJobs(servers)
480 const video = await servers[0].videos.get({ id: liveVideoId })
482 const masterPlaylist = video.streamingPlaylists[0].playlistUrl
483 const probe = await ffprobePromise(masterPlaylist)
485 const bitrates = probe.streams.map(s => parseInt(s.tags.variant_bitrate))
486 for (const bitrate of bitrates) {
487 expect(bitrate).to.exist
488 expect(isNaN(bitrate)).to.be.false
489 expect(bitrate).to.be.below(61_000_000) // video_short.mp4 bitrate
492 await stopFfmpeg(ffmpegCommand)
495 it('Should enable transcoding with some resolutions and correctly save them', async function () {
496 this.timeout(500_000)
498 const resolutions = [ 240, 360, 720 ]
500 await updateConf(resolutions)
501 liveVideoId = await createLiveWrapper(true)
503 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
504 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
505 await waitJobs(servers)
507 await testLiveVideoResolutions({
508 originServer: servers[0],
509 sqlCommand: sqlCommandServer1,
516 await stopFfmpeg(ffmpegCommand)
517 await commands[0].waitUntilEnded({ videoId: liveVideoId })
519 await waitJobs(servers)
521 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
523 const maxBitrateLimits = {
524 720: 6500 * 1000, // 60FPS
529 const minBitrateLimits = {
535 for (const server of servers) {
536 const video = await server.videos.get({ id: liveVideoId })
538 expect(video.state.id).to.equal(VideoState.PUBLISHED)
539 expect(video.duration).to.be.greaterThan(1)
540 expect(video.files).to.have.lengthOf(0)
542 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
543 await makeRawRequest({ url: hlsPlaylist.playlistUrl, expectedStatus: HttpStatusCode.OK_200 })
544 await makeRawRequest({ url: hlsPlaylist.segmentsSha256Url, expectedStatus: HttpStatusCode.OK_200 })
546 // We should have generated random filenames
547 expect(basename(hlsPlaylist.playlistUrl)).to.not.equal('master.m3u8')
548 expect(basename(hlsPlaylist.segmentsSha256Url)).to.not.equal('segments-sha256.json')
550 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
552 for (const resolution of resolutions) {
553 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
555 expect(file).to.exist
556 expect(file.size).to.be.greaterThan(1)
558 if (resolution >= 720) {
559 expect(file.fps).to.be.approximately(60, 10)
561 expect(file.fps).to.be.approximately(30, 3)
564 const filename = basename(file.fileUrl)
565 expect(filename).to.not.contain(video.uuid)
567 const segmentPath = servers[0].servers.buildDirectory(join('streaming-playlists', 'hls', video.uuid, filename))
569 const probe = await ffprobePromise(segmentPath)
570 const videoStream = await getVideoStream(segmentPath, probe)
572 expect(probe.format.bit_rate).to.be.below(maxBitrateLimits[videoStream.height])
573 expect(probe.format.bit_rate).to.be.at.least(minBitrateLimits[videoStream.height])
575 await makeRawRequest({ url: file.torrentUrl, expectedStatus: HttpStatusCode.OK_200 })
576 await makeRawRequest({ url: file.fileUrl, expectedStatus: HttpStatusCode.OK_200 })
581 it('Should not generate an upper resolution than original file', async function () {
582 this.timeout(500_000)
584 const resolutions = [ 240, 480 ]
585 await updateConf(resolutions)
587 await servers[0].config.updateExistingSubConfig({
591 alwaysTranscodeOriginalResolution: false
597 liveVideoId = await createLiveWrapper(true)
599 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
600 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
601 await waitJobs(servers)
603 await testLiveVideoResolutions({
604 originServer: servers[0],
605 sqlCommand: sqlCommandServer1,
612 await stopFfmpeg(ffmpegCommand)
613 await commands[0].waitUntilEnded({ videoId: liveVideoId })
615 await waitJobs(servers)
617 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
619 const video = await servers[0].videos.get({ id: liveVideoId })
620 const hlsFiles = video.streamingPlaylists[0].files
622 expect(video.files).to.have.lengthOf(0)
623 expect(hlsFiles).to.have.lengthOf(resolutions.length)
625 // eslint-disable-next-line @typescript-eslint/require-array-sort-compare
626 expect(getAllFiles(video).map(f => f.resolution.id).sort()).to.deep.equal(resolutions)
629 it('Should only keep the original resolution if all resolutions are disabled', async function () {
630 this.timeout(600_000)
633 liveVideoId = await createLiveWrapper(true)
635 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
636 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
637 await waitJobs(servers)
639 await testLiveVideoResolutions({
640 originServer: servers[0],
641 sqlCommand: sqlCommandServer1,
644 resolutions: [ 720 ],
648 await stopFfmpeg(ffmpegCommand)
649 await commands[0].waitUntilEnded({ videoId: liveVideoId })
651 await waitJobs(servers)
653 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
655 const video = await servers[0].videos.get({ id: liveVideoId })
656 const hlsFiles = video.streamingPlaylists[0].files
658 expect(video.files).to.have.lengthOf(0)
659 expect(hlsFiles).to.have.lengthOf(1)
661 expect(hlsFiles[0].resolution.id).to.equal(720)
664 after(async function () {
665 await sqlCommandServer1.cleanup()
669 describe('After a server restart', function () {
670 let liveVideoId: string
671 let liveVideoReplayId: string
672 let permanentLiveVideoReplayId: string
674 let permanentLiveReplayName: string
676 let beforeServerRestart: Date
678 async function createLiveWrapper (options: { saveReplay: boolean, permanent: boolean }) {
679 const liveAttributes: LiveVideoCreate = {
681 channelId: servers[0].store.channel.id,
682 privacy: VideoPrivacy.PUBLIC,
683 saveReplay: options.saveReplay,
684 replaySettings: options.saveReplay
685 ? { privacy: VideoPrivacy.PUBLIC }
687 permanentLive: options.permanent
690 const { uuid } = await commands[0].create({ fields: liveAttributes })
694 before(async function () {
695 this.timeout(600_000)
697 liveVideoId = await createLiveWrapper({ saveReplay: false, permanent: false })
698 liveVideoReplayId = await createLiveWrapper({ saveReplay: true, permanent: false })
699 permanentLiveVideoReplayId = await createLiveWrapper({ saveReplay: true, permanent: true })
702 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId }),
703 commands[0].sendRTMPStreamInVideo({ videoId: permanentLiveVideoReplayId }),
704 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoReplayId })
708 commands[0].waitUntilPublished({ videoId: liveVideoId }),
709 commands[0].waitUntilPublished({ videoId: permanentLiveVideoReplayId }),
710 commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
713 for (const videoUUID of [ liveVideoId, liveVideoReplayId, permanentLiveVideoReplayId ]) {
714 await commands[0].waitUntilSegmentGeneration({
723 const video = await servers[0].videos.get({ id: permanentLiveVideoReplayId })
724 permanentLiveReplayName = video.name + ' - ' + new Date(video.publishedAt).toLocaleString()
727 await killallServers([ servers[0] ])
729 beforeServerRestart = new Date()
730 await servers[0].run()
733 await waitJobs(servers)
736 it('Should cleanup lives', async function () {
739 await commands[0].waitUntilEnded({ videoId: liveVideoId })
740 await commands[0].waitUntilWaiting({ videoId: permanentLiveVideoReplayId })
743 it('Should save a non permanent live replay', async function () {
746 await commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
748 const session = await commands[0].getReplaySession({ videoId: liveVideoReplayId })
749 expect(session.endDate).to.exist
750 expect(new Date(session.endDate)).to.be.above(beforeServerRestart)
753 it('Should have saved a permanent live replay', async function () {
756 const { data } = await servers[0].videos.listMyVideos({ sort: '-publishedAt' })
757 expect(data.find(v => v.name === permanentLiveReplayName)).to.exist
761 after(async function () {
762 await cleanupTests(servers)