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 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 () {
186 await commands[0].update({ videoId: liveVideoUUID, fields: { saveReplay: false, latencyMode: LiveVideoLatencyMode.DEFAULT } })
187 await waitJobs(servers)
190 it('Have the live updated', async function () {
191 for (const server of servers) {
192 const live = await server.live.get({ videoId: liveVideoUUID })
194 if (server.url === servers[0].url) {
195 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
196 expect(live.streamKey).to.not.be.empty
198 expect(live.rtmpUrl).to.not.exist
199 expect(live.streamKey).to.not.exist
202 expect(live.saveReplay).to.be.false
203 expect(live.replaySettings).to.not.exist
204 expect(live.latencyMode).to.equal(LiveVideoLatencyMode.DEFAULT)
208 it('Delete the live', async function () {
211 await servers[0].videos.remove({ id: liveVideoUUID })
212 await waitJobs(servers)
215 it('Should have the live deleted', async function () {
216 for (const server of servers) {
217 await server.videos.get({ id: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
218 await server.live.get({ videoId: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
223 describe('Live filters', function () {
224 let ffmpegCommand: any
225 let liveVideoId: string
226 let vodVideoId: string
228 before(async function () {
231 vodVideoId = (await servers[0].videos.quickUpload({ name: 'vod video' })).uuid
233 const liveOptions = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: servers[0].store.channel.id }
234 const live = await commands[0].create({ fields: liveOptions })
235 liveVideoId = live.uuid
237 ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoId })
238 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
239 await waitJobs(servers)
242 it('Should only display lives', async function () {
243 const { data, total } = await servers[0].videos.list({ isLive: true })
245 expect(total).to.equal(1)
246 expect(data).to.have.lengthOf(1)
247 expect(data[0].name).to.equal('live')
250 it('Should not display lives', async function () {
251 const { data, total } = await servers[0].videos.list({ isLive: false })
253 expect(total).to.equal(1)
254 expect(data).to.have.lengthOf(1)
255 expect(data[0].name).to.equal('vod video')
258 it('Should display my lives', async function () {
261 await stopFfmpeg(ffmpegCommand)
262 await waitJobs(servers)
264 const { data } = await servers[0].videos.listMyVideos({ isLive: true })
266 const result = data.every(v => v.isLive)
267 expect(result).to.be.true
270 it('Should not display my lives', async function () {
271 const { data } = await servers[0].videos.listMyVideos({ isLive: false })
273 const result = data.every(v => !v.isLive)
274 expect(result).to.be.true
277 after(async function () {
278 await servers[0].videos.remove({ id: vodVideoId })
279 await servers[0].videos.remove({ id: liveVideoId })
283 describe('Stream checks', function () {
284 let liveVideo: LiveVideo & VideoDetails
288 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
291 async function createLiveWrapper () {
292 const liveAttributes = {
294 channelId: servers[0].store.channel.id,
295 privacy: VideoPrivacy.PUBLIC,
299 const { uuid } = await commands[0].create({ fields: liveAttributes })
301 const live = await commands[0].get({ videoId: uuid })
302 const video = await servers[0].videos.get({ id: uuid })
304 return Object.assign(video, live)
307 it('Should not allow a stream without the appropriate path', async function () {
310 liveVideo = await createLiveWrapper()
312 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/bad-live', streamKey: liveVideo.streamKey })
313 await testFfmpegStreamError(command, true)
316 it('Should not allow a stream without the appropriate stream key', async function () {
319 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: 'bad-stream-key' })
320 await testFfmpegStreamError(command, true)
323 it('Should succeed with the correct params', async function () {
326 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
327 await testFfmpegStreamError(command, false)
330 it('Should list this live now someone stream into it', async function () {
331 for (const server of servers) {
332 const { total, data } = await server.videos.list()
334 expect(total).to.equal(1)
335 expect(data).to.have.lengthOf(1)
337 const video = 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].blacklist.add({ videoId: liveVideo.uuid })
350 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: 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 servers[0].videos.remove({ id: liveVideo.uuid })
361 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: 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].store.channel.id,
373 privacy: VideoPrivacy.PUBLIC,
375 replaySettings: saveReplay
376 ? { privacy: VideoPrivacy.PUBLIC }
380 const { uuid } = await commands[0].create({ fields: liveAttributes })
384 function updateConf (resolutions: number[]) {
385 return servers[0].config.updateCustomSubConfig({
394 '144p': resolutions.includes(144),
395 '240p': resolutions.includes(240),
396 '360p': resolutions.includes(360),
397 '480p': resolutions.includes(480),
398 '720p': resolutions.includes(720),
399 '1080p': resolutions.includes(1080),
400 '2160p': resolutions.includes(2160)
408 before(async function () {
412 it('Should enable transcoding without additional resolutions', async function () {
415 liveVideoId = await createLiveWrapper(false)
417 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
418 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
419 await waitJobs(servers)
421 await testVideoResolutions({
422 originServer: servers[0],
425 resolutions: [ 720 ],
426 objectStorage: false,
430 await stopFfmpeg(ffmpegCommand)
433 it('Should transcode audio only RTMP stream', async function () {
436 liveVideoId = await createLiveWrapper(false)
438 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short_no_audio.mp4' })
439 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
440 await waitJobs(servers)
442 await stopFfmpeg(ffmpegCommand)
445 it('Should enable transcoding with some resolutions', async function () {
448 const resolutions = [ 240, 480 ]
449 await updateConf(resolutions)
450 liveVideoId = await createLiveWrapper(false)
452 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
453 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
454 await waitJobs(servers)
456 await testVideoResolutions({
457 originServer: servers[0],
460 resolutions: resolutions.concat([ 720 ]),
461 objectStorage: false,
465 await stopFfmpeg(ffmpegCommand)
468 it('Should correctly set the appropriate bitrate depending on the input', async function () {
471 liveVideoId = await createLiveWrapper(false)
473 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({
474 videoId: liveVideoId,
475 fixtureName: 'video_short.mp4',
478 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
479 await waitJobs(servers)
481 const video = await servers[0].videos.get({ id: liveVideoId })
483 const masterPlaylist = video.streamingPlaylists[0].playlistUrl
484 const probe = await ffprobePromise(masterPlaylist)
486 const bitrates = probe.streams.map(s => parseInt(s.tags.variant_bitrate))
487 for (const bitrate of bitrates) {
488 expect(bitrate).to.exist
489 expect(isNaN(bitrate)).to.be.false
490 expect(bitrate).to.be.below(61_000_000) // video_short.mp4 bitrate
493 await stopFfmpeg(ffmpegCommand)
496 it('Should enable transcoding with some resolutions and correctly save them', async function () {
497 this.timeout(400_000)
499 const resolutions = [ 240, 360, 720 ]
501 await updateConf(resolutions)
502 liveVideoId = await createLiveWrapper(true)
504 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
505 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
506 await waitJobs(servers)
508 await testVideoResolutions({
509 originServer: servers[0],
513 objectStorage: false,
517 await stopFfmpeg(ffmpegCommand)
518 await commands[0].waitUntilEnded({ videoId: liveVideoId })
520 await waitJobs(servers)
522 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
524 const maxBitrateLimits = {
525 720: 6500 * 1000, // 60FPS
530 const minBitrateLimits = {
536 for (const server of servers) {
537 const video = await server.videos.get({ id: liveVideoId })
539 expect(video.state.id).to.equal(VideoState.PUBLISHED)
540 expect(video.duration).to.be.greaterThan(1)
541 expect(video.files).to.have.lengthOf(0)
543 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
544 await makeRawRequest({ url: hlsPlaylist.playlistUrl, expectedStatus: HttpStatusCode.OK_200 })
545 await makeRawRequest({ url: hlsPlaylist.segmentsSha256Url, expectedStatus: HttpStatusCode.OK_200 })
547 // We should have generated random filenames
548 expect(basename(hlsPlaylist.playlistUrl)).to.not.equal('master.m3u8')
549 expect(basename(hlsPlaylist.segmentsSha256Url)).to.not.equal('segments-sha256.json')
551 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
553 for (const resolution of resolutions) {
554 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
556 expect(file).to.exist
557 expect(file.size).to.be.greaterThan(1)
559 if (resolution >= 720) {
560 expect(file.fps).to.be.approximately(60, 10)
562 expect(file.fps).to.be.approximately(30, 3)
565 const filename = basename(file.fileUrl)
566 expect(filename).to.not.contain(video.uuid)
568 const segmentPath = servers[0].servers.buildDirectory(join('streaming-playlists', 'hls', video.uuid, filename))
570 const probe = await ffprobePromise(segmentPath)
571 const videoStream = await getVideoStream(segmentPath, probe)
573 expect(probe.format.bit_rate).to.be.below(maxBitrateLimits[videoStream.height])
574 expect(probe.format.bit_rate).to.be.at.least(minBitrateLimits[videoStream.height])
576 await makeRawRequest({ url: file.torrentUrl, expectedStatus: HttpStatusCode.OK_200 })
577 await makeRawRequest({ url: file.fileUrl, expectedStatus: HttpStatusCode.OK_200 })
582 it('Should not generate an upper resolution than original file', async function () {
583 this.timeout(400_000)
585 const resolutions = [ 240, 480 ]
586 await updateConf(resolutions)
588 await servers[0].config.updateExistingSubConfig({
592 alwaysTranscodeOriginalResolution: false
598 liveVideoId = await createLiveWrapper(true)
600 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
601 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
602 await waitJobs(servers)
604 await testVideoResolutions({
605 originServer: servers[0],
609 objectStorage: false,
613 await stopFfmpeg(ffmpegCommand)
614 await commands[0].waitUntilEnded({ videoId: liveVideoId })
616 await waitJobs(servers)
618 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
620 const video = await servers[0].videos.get({ id: liveVideoId })
621 const hlsFiles = video.streamingPlaylists[0].files
623 expect(video.files).to.have.lengthOf(0)
624 expect(hlsFiles).to.have.lengthOf(resolutions.length)
626 // eslint-disable-next-line @typescript-eslint/require-array-sort-compare
627 expect(getAllFiles(video).map(f => f.resolution.id).sort()).to.deep.equal(resolutions)
630 it('Should only keep the original resolution if all resolutions are disabled', async function () {
631 this.timeout(600_000)
634 liveVideoId = await createLiveWrapper(true)
636 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
637 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
638 await waitJobs(servers)
640 await testVideoResolutions({
641 originServer: servers[0],
644 resolutions: [ 720 ],
645 objectStorage: false,
649 await stopFfmpeg(ffmpegCommand)
650 await commands[0].waitUntilEnded({ videoId: liveVideoId })
652 await waitJobs(servers)
654 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
656 const video = await servers[0].videos.get({ id: liveVideoId })
657 const hlsFiles = video.streamingPlaylists[0].files
659 expect(video.files).to.have.lengthOf(0)
660 expect(hlsFiles).to.have.lengthOf(1)
662 expect(hlsFiles[0].resolution.id).to.equal(720)
666 describe('After a server restart', function () {
667 let liveVideoId: string
668 let liveVideoReplayId: string
669 let permanentLiveVideoReplayId: string
671 let permanentLiveReplayName: string
673 let beforeServerRestart: Date
675 async function createLiveWrapper (options: { saveReplay: boolean, permanent: boolean }) {
676 const liveAttributes: LiveVideoCreate = {
678 channelId: servers[0].store.channel.id,
679 privacy: VideoPrivacy.PUBLIC,
680 saveReplay: options.saveReplay,
681 replaySettings: options.saveReplay
682 ? { privacy: VideoPrivacy.PUBLIC }
684 permanentLive: options.permanent
687 const { uuid } = await commands[0].create({ fields: liveAttributes })
691 before(async function () {
692 this.timeout(600_000)
694 liveVideoId = await createLiveWrapper({ saveReplay: false, permanent: false })
695 liveVideoReplayId = await createLiveWrapper({ saveReplay: true, permanent: false })
696 permanentLiveVideoReplayId = await createLiveWrapper({ saveReplay: true, permanent: true })
699 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId }),
700 commands[0].sendRTMPStreamInVideo({ videoId: permanentLiveVideoReplayId }),
701 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoReplayId })
705 commands[0].waitUntilPublished({ videoId: liveVideoId }),
706 commands[0].waitUntilPublished({ videoId: permanentLiveVideoReplayId }),
707 commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
710 for (const videoUUID of [ liveVideoId, liveVideoReplayId, permanentLiveVideoReplayId ]) {
711 await commands[0].waitUntilSegmentGeneration({
721 const video = await servers[0].videos.get({ id: permanentLiveVideoReplayId })
722 permanentLiveReplayName = video.name + ' - ' + new Date(video.publishedAt).toLocaleString()
725 await killallServers([ servers[0] ])
727 beforeServerRestart = new Date()
728 await servers[0].run()
731 await waitJobs(servers)
734 it('Should cleanup lives', async function () {
737 await commands[0].waitUntilEnded({ videoId: liveVideoId })
738 await commands[0].waitUntilWaiting({ videoId: permanentLiveVideoReplayId })
741 it('Should save a non permanent live replay', async function () {
744 await commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
746 const session = await commands[0].getReplaySession({ videoId: liveVideoReplayId })
747 expect(session.endDate).to.exist
748 expect(new Date(session.endDate)).to.be.above(beforeServerRestart)
751 it('Should have saved a permanent live replay', async function () {
754 const { data } = await servers[0].videos.listMyVideos({ sort: '-publishedAt' })
755 expect(data.find(v => v.name === permanentLiveReplayName)).to.exist
759 after(async function () {
760 await cleanupTests(servers)