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 { checkLiveSegmentHash, checkResolutionsInMasterPlaylist, testImage } from '@server/tests/shared'
7 import { getAllFiles, 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 describe('Test live', function () {
36 let servers: PeerTubeServer[] = []
37 let commands: LiveCommand[]
39 before(async function () {
42 servers = await createMultipleServers(2)
44 // Get the access tokens
45 await setAccessTokensToServers(servers)
46 await setDefaultVideoChannel(servers)
48 await servers[0].config.updateCustomSubConfig({
63 // Server 1 and server 2 follow each other
64 await doubleFollow(servers[0], servers[1])
66 commands = servers.map(s => s.live)
69 describe('Live creation, update and delete', function () {
70 let liveVideoUUID: string
72 it('Should create a live with the appropriate parameters', async function () {
75 const attributes: LiveVideoCreate = {
79 description: 'super live description',
80 support: 'support field',
81 channelId: servers[0].store.channel.id,
83 waitTranscoding: false,
84 name: 'my super live',
85 tags: [ 'tag1', 'tag2' ],
86 commentsEnabled: false,
87 downloadEnabled: false,
89 latencyMode: LiveVideoLatencyMode.SMALL_LATENCY,
90 privacy: VideoPrivacy.PUBLIC,
91 previewfile: 'video_short1-preview.webm.jpg',
92 thumbnailfile: 'video_short1.webm.jpg'
95 const live = await commands[0].create({ fields: attributes })
96 liveVideoUUID = live.uuid
98 await waitJobs(servers)
100 for (const server of servers) {
101 const video = await server.videos.get({ id: liveVideoUUID })
103 expect(video.category.id).to.equal(1)
104 expect(video.licence.id).to.equal(2)
105 expect(video.language.id).to.equal('fr')
106 expect(video.description).to.equal('super live description')
107 expect(video.support).to.equal('support field')
109 expect(video.channel.name).to.equal(servers[0].store.channel.name)
110 expect(video.channel.host).to.equal(servers[0].store.channel.host)
112 expect(video.isLive).to.be.true
114 expect(video.nsfw).to.be.false
115 expect(video.waitTranscoding).to.be.false
116 expect(video.name).to.equal('my super live')
117 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
118 expect(video.commentsEnabled).to.be.false
119 expect(video.downloadEnabled).to.be.false
120 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
122 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
123 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
125 const live = await server.live.get({ videoId: liveVideoUUID })
127 if (server.url === servers[0].url) {
128 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
129 expect(live.streamKey).to.not.be.empty
131 expect(live.rtmpUrl).to.not.exist
132 expect(live.streamKey).to.not.exist
135 expect(live.saveReplay).to.be.true
136 expect(live.latencyMode).to.equal(LiveVideoLatencyMode.SMALL_LATENCY)
140 it('Should have a default preview and thumbnail', async function () {
143 const attributes: LiveVideoCreate = {
144 name: 'default live thumbnail',
145 channelId: servers[0].store.channel.id,
146 privacy: VideoPrivacy.UNLISTED,
150 const live = await commands[0].create({ fields: attributes })
151 const videoId = live.uuid
153 await waitJobs(servers)
155 for (const server of servers) {
156 const video = await server.videos.get({ id: videoId })
157 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
158 expect(video.nsfw).to.be.true
160 await makeRawRequest(server.url + video.thumbnailPath, HttpStatusCode.OK_200)
161 await makeRawRequest(server.url + video.previewPath, HttpStatusCode.OK_200)
165 it('Should not have the live listed since nobody streams into', async function () {
166 for (const server of servers) {
167 const { total, data } = await server.videos.list()
169 expect(total).to.equal(0)
170 expect(data).to.have.lengthOf(0)
174 it('Should not be able to update a live of another server', async function () {
175 await commands[1].update({ videoId: liveVideoUUID, fields: { saveReplay: false }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
178 it('Should update the live', async function () {
181 await commands[0].update({ videoId: liveVideoUUID, fields: { saveReplay: false, latencyMode: LiveVideoLatencyMode.DEFAULT } })
182 await waitJobs(servers)
185 it('Have the live updated', async function () {
186 for (const server of servers) {
187 const live = await server.live.get({ videoId: liveVideoUUID })
189 if (server.url === servers[0].url) {
190 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
191 expect(live.streamKey).to.not.be.empty
193 expect(live.rtmpUrl).to.not.exist
194 expect(live.streamKey).to.not.exist
197 expect(live.saveReplay).to.be.false
198 expect(live.latencyMode).to.equal(LiveVideoLatencyMode.DEFAULT)
202 it('Delete the live', async function () {
205 await servers[0].videos.remove({ id: liveVideoUUID })
206 await waitJobs(servers)
209 it('Should have the live deleted', async function () {
210 for (const server of servers) {
211 await server.videos.get({ id: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
212 await server.live.get({ videoId: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
217 describe('Live filters', function () {
218 let ffmpegCommand: any
219 let liveVideoId: string
220 let vodVideoId: string
222 before(async function () {
225 vodVideoId = (await servers[0].videos.quickUpload({ name: 'vod video' })).uuid
227 const liveOptions = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: servers[0].store.channel.id }
228 const live = await commands[0].create({ fields: liveOptions })
229 liveVideoId = live.uuid
231 ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoId })
232 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
233 await waitJobs(servers)
236 it('Should only display lives', async function () {
237 const { data, total } = await servers[0].videos.list({ isLive: true })
239 expect(total).to.equal(1)
240 expect(data).to.have.lengthOf(1)
241 expect(data[0].name).to.equal('live')
244 it('Should not display lives', async function () {
245 const { data, total } = await servers[0].videos.list({ isLive: false })
247 expect(total).to.equal(1)
248 expect(data).to.have.lengthOf(1)
249 expect(data[0].name).to.equal('vod video')
252 it('Should display my lives', async function () {
255 await stopFfmpeg(ffmpegCommand)
256 await waitJobs(servers)
258 const { data } = await servers[0].videos.listMyVideos({ isLive: true })
260 const result = data.every(v => v.isLive)
261 expect(result).to.be.true
264 it('Should not display my lives', async function () {
265 const { data } = await servers[0].videos.listMyVideos({ isLive: false })
267 const result = data.every(v => !v.isLive)
268 expect(result).to.be.true
271 after(async function () {
272 await servers[0].videos.remove({ id: vodVideoId })
273 await servers[0].videos.remove({ id: liveVideoId })
277 describe('Stream checks', function () {
278 let liveVideo: LiveVideo & VideoDetails
282 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
285 async function createLiveWrapper () {
286 const liveAttributes = {
288 channelId: servers[0].store.channel.id,
289 privacy: VideoPrivacy.PUBLIC,
293 const { uuid } = await commands[0].create({ fields: liveAttributes })
295 const live = await commands[0].get({ videoId: uuid })
296 const video = await servers[0].videos.get({ id: uuid })
298 return Object.assign(video, live)
301 it('Should not allow a stream without the appropriate path', async function () {
304 liveVideo = await createLiveWrapper()
306 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/bad-live', streamKey: liveVideo.streamKey })
307 await testFfmpegStreamError(command, true)
310 it('Should not allow a stream without the appropriate stream key', async function () {
313 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: 'bad-stream-key' })
314 await testFfmpegStreamError(command, true)
317 it('Should succeed with the correct params', async function () {
320 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
321 await testFfmpegStreamError(command, false)
324 it('Should list this live now someone stream into it', async function () {
325 for (const server of servers) {
326 const { total, data } = await server.videos.list()
328 expect(total).to.equal(1)
329 expect(data).to.have.lengthOf(1)
331 const video = data[0]
332 expect(video.name).to.equal('user live')
333 expect(video.isLive).to.be.true
337 it('Should not allow a stream on a live that was blacklisted', async function () {
340 liveVideo = await createLiveWrapper()
342 await servers[0].blacklist.add({ videoId: liveVideo.uuid })
344 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
345 await testFfmpegStreamError(command, true)
348 it('Should not allow a stream on a live that was deleted', async function () {
351 liveVideo = await createLiveWrapper()
353 await servers[0].videos.remove({ id: liveVideo.uuid })
355 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
356 await testFfmpegStreamError(command, true)
360 describe('Live transcoding', function () {
361 let liveVideoId: string
363 async function createLiveWrapper (saveReplay: boolean) {
364 const liveAttributes = {
366 channelId: servers[0].store.channel.id,
367 privacy: VideoPrivacy.PUBLIC,
371 const { uuid } = await commands[0].create({ fields: liveAttributes })
375 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
376 for (const server of servers) {
377 const { data } = await server.videos.list()
378 expect(data.find(v => v.uuid === liveVideoId)).to.exist
380 const video = await server.videos.get({ id: liveVideoId })
382 expect(video.streamingPlaylists).to.have.lengthOf(1)
384 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
385 expect(hlsPlaylist).to.exist
387 // Only finite files are displayed
388 expect(hlsPlaylist.files).to.have.lengthOf(0)
390 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
392 for (let i = 0; i < resolutions.length; i++) {
394 const segmentName = `${i}-00000${segmentNum}.ts`
395 await commands[0].waitUntilSegmentGeneration({ videoUUID: video.uuid, playlistNumber: i, segment: segmentNum })
397 const subPlaylist = await servers[0].streamingPlaylists.get({
398 url: `${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`
401 expect(subPlaylist).to.contain(segmentName)
403 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
404 await checkLiveSegmentHash({
406 baseUrlSegment: baseUrlAndPath,
407 videoUUID: video.uuid,
415 function updateConf (resolutions: number[]) {
416 return servers[0].config.updateCustomSubConfig({
425 '144p': resolutions.includes(144),
426 '240p': resolutions.includes(240),
427 '360p': resolutions.includes(360),
428 '480p': resolutions.includes(480),
429 '720p': resolutions.includes(720),
430 '1080p': resolutions.includes(1080),
431 '2160p': resolutions.includes(2160)
439 before(async function () {
443 it('Should enable transcoding without additional resolutions', async function () {
446 liveVideoId = await createLiveWrapper(false)
448 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
449 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
450 await waitJobs(servers)
452 await testVideoResolutions(liveVideoId, [ 720 ])
454 await stopFfmpeg(ffmpegCommand)
457 it('Should enable transcoding with some resolutions', async function () {
460 const resolutions = [ 240, 480 ]
461 await updateConf(resolutions)
462 liveVideoId = await createLiveWrapper(false)
464 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
465 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
466 await waitJobs(servers)
468 await testVideoResolutions(liveVideoId, resolutions.concat([ 720 ]))
470 await stopFfmpeg(ffmpegCommand)
473 it('Should correctly set the appropriate bitrate depending on the input', async function () {
476 liveVideoId = await createLiveWrapper(false)
478 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({
479 videoId: liveVideoId,
480 fixtureName: 'video_short.mp4',
483 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
484 await waitJobs(servers)
486 const video = await servers[0].videos.get({ id: liveVideoId })
488 const masterPlaylist = video.streamingPlaylists[0].playlistUrl
489 const probe = await ffprobePromise(masterPlaylist)
491 const bitrates = probe.streams.map(s => parseInt(s.tags.variant_bitrate))
492 for (const bitrate of bitrates) {
493 expect(bitrate).to.exist
494 expect(isNaN(bitrate)).to.be.false
495 expect(bitrate).to.be.below(61_000_000) // video_short.mp4 bitrate
498 await stopFfmpeg(ffmpegCommand)
501 it('Should enable transcoding with some resolutions and correctly save them', async function () {
502 this.timeout(400_000)
504 const resolutions = [ 240, 360, 720 ]
506 await updateConf(resolutions)
507 liveVideoId = await createLiveWrapper(true)
509 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
510 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
511 await waitJobs(servers)
513 await testVideoResolutions(liveVideoId, resolutions)
515 await stopFfmpeg(ffmpegCommand)
516 await commands[0].waitUntilEnded({ videoId: liveVideoId })
518 await waitJobs(servers)
520 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
522 const maxBitrateLimits = {
523 720: 6500 * 1000, // 60FPS
528 const minBitrateLimits = {
534 for (const server of servers) {
535 const video = await server.videos.get({ id: liveVideoId })
537 expect(video.state.id).to.equal(VideoState.PUBLISHED)
538 expect(video.duration).to.be.greaterThan(1)
539 expect(video.files).to.have.lengthOf(0)
541 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
542 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
543 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
545 // We should have generated random filenames
546 expect(basename(hlsPlaylist.playlistUrl)).to.not.equal('master.m3u8')
547 expect(basename(hlsPlaylist.segmentsSha256Url)).to.not.equal('segments-sha256.json')
549 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
551 for (const resolution of resolutions) {
552 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
554 expect(file).to.exist
555 expect(file.size).to.be.greaterThan(1)
557 if (resolution >= 720) {
558 expect(file.fps).to.be.approximately(60, 10)
560 expect(file.fps).to.be.approximately(30, 2)
563 const filename = basename(file.fileUrl)
564 expect(filename).to.not.contain(video.uuid)
566 const segmentPath = servers[0].servers.buildDirectory(join('streaming-playlists', 'hls', video.uuid, filename))
568 const probe = await ffprobePromise(segmentPath)
569 const videoStream = await getVideoStream(segmentPath, probe)
571 expect(probe.format.bit_rate).to.be.below(maxBitrateLimits[videoStream.height])
572 expect(probe.format.bit_rate).to.be.at.least(minBitrateLimits[videoStream.height])
574 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
575 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
580 it('Should not generate an upper resolution than original file', async function () {
581 this.timeout(400_000)
583 const resolutions = [ 240, 480 ]
584 await updateConf(resolutions)
586 await servers[0].config.updateExistingSubConfig({
590 alwaysTranscodeOriginalResolution: false
596 liveVideoId = await createLiveWrapper(true)
598 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
599 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
600 await waitJobs(servers)
602 await testVideoResolutions(liveVideoId, resolutions)
604 await stopFfmpeg(ffmpegCommand)
605 await commands[0].waitUntilEnded({ videoId: liveVideoId })
607 await waitJobs(servers)
609 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
611 const video = await servers[0].videos.get({ id: liveVideoId })
612 const hlsFiles = video.streamingPlaylists[0].files
614 expect(video.files).to.have.lengthOf(0)
615 expect(hlsFiles).to.have.lengthOf(resolutions.length)
617 // eslint-disable-next-line @typescript-eslint/require-array-sort-compare
618 expect(getAllFiles(video).map(f => f.resolution.id).sort()).to.deep.equal(resolutions)
621 it('Should only keep the original resolution if all resolutions are disabled', async function () {
622 this.timeout(600_000)
625 liveVideoId = await createLiveWrapper(true)
627 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
628 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
629 await waitJobs(servers)
631 await testVideoResolutions(liveVideoId, [ 720 ])
633 await stopFfmpeg(ffmpegCommand)
634 await commands[0].waitUntilEnded({ videoId: liveVideoId })
636 await waitJobs(servers)
638 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
640 const video = await servers[0].videos.get({ id: liveVideoId })
641 const hlsFiles = video.streamingPlaylists[0].files
643 expect(video.files).to.have.lengthOf(0)
644 expect(hlsFiles).to.have.lengthOf(1)
646 expect(hlsFiles[0].resolution.id).to.equal(720)
650 describe('After a server restart', function () {
651 let liveVideoId: string
652 let liveVideoReplayId: string
653 let permanentLiveVideoReplayId: string
655 let permanentLiveReplayName: string
657 let beforeServerRestart: Date
659 async function createLiveWrapper (options: { saveReplay: boolean, permanent: boolean }) {
660 const liveAttributes: LiveVideoCreate = {
662 channelId: servers[0].store.channel.id,
663 privacy: VideoPrivacy.PUBLIC,
664 saveReplay: options.saveReplay,
665 permanentLive: options.permanent
668 const { uuid } = await commands[0].create({ fields: liveAttributes })
672 before(async function () {
673 this.timeout(600_000)
675 liveVideoId = await createLiveWrapper({ saveReplay: false, permanent: false })
676 liveVideoReplayId = await createLiveWrapper({ saveReplay: true, permanent: false })
677 permanentLiveVideoReplayId = await createLiveWrapper({ saveReplay: true, permanent: true })
680 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId }),
681 commands[0].sendRTMPStreamInVideo({ videoId: permanentLiveVideoReplayId }),
682 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoReplayId })
686 commands[0].waitUntilPublished({ videoId: liveVideoId }),
687 commands[0].waitUntilPublished({ videoId: permanentLiveVideoReplayId }),
688 commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
691 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoId, playlistNumber: 0, segment: 2 })
692 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoReplayId, playlistNumber: 0, segment: 2 })
693 await commands[0].waitUntilSegmentGeneration({ videoUUID: permanentLiveVideoReplayId, playlistNumber: 0, segment: 2 })
696 const video = await servers[0].videos.get({ id: permanentLiveVideoReplayId })
697 permanentLiveReplayName = video.name + ' - ' + new Date(video.publishedAt).toLocaleString()
700 await killallServers([ servers[0] ])
702 beforeServerRestart = new Date()
703 await servers[0].run()
706 await waitJobs(servers)
709 it('Should cleanup lives', async function () {
712 await commands[0].waitUntilEnded({ videoId: liveVideoId })
713 await commands[0].waitUntilWaiting({ videoId: permanentLiveVideoReplayId })
716 it('Should save a non permanent live replay', async function () {
719 await commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
721 const session = await commands[0].getReplaySession({ videoId: liveVideoReplayId })
722 expect(session.endDate).to.exist
723 expect(new Date(session.endDate)).to.be.above(beforeServerRestart)
726 it('Should have saved a permanent live replay', async function () {
729 const { data } = await servers[0].videos.listMyVideos({ sort: '-publishedAt' })
730 expect(data.find(v => v.name === permanentLiveReplayName)).to.exist
734 after(async function () {
735 await cleanupTests(servers)