1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
4 import { basename, join } from 'path'
5 import { ffprobePromise, getVideoStream } from '@server/helpers/ffmpeg'
6 import { checkLiveSegmentHash, checkResolutionsInMasterPlaylist, getAllFiles, testImage } from '@server/tests/shared'
7 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({
65 // Server 1 and server 2 follow each other
66 await doubleFollow(servers[0], servers[1])
68 commands = servers.map(s => s.live)
71 describe('Live creation, update and delete', function () {
72 let liveVideoUUID: string
74 it('Should create a live with the appropriate parameters', async function () {
77 const attributes: LiveVideoCreate = {
81 description: 'super live description',
82 support: 'support field',
83 channelId: servers[0].store.channel.id,
85 waitTranscoding: false,
86 name: 'my super live',
87 tags: [ 'tag1', 'tag2' ],
88 commentsEnabled: false,
89 downloadEnabled: false,
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.rtmpUrl).to.not.exist
134 expect(live.streamKey).to.not.exist
137 expect(live.saveReplay).to.be.true
138 expect(live.latencyMode).to.equal(LiveVideoLatencyMode.SMALL_LATENCY)
142 it('Should have a default preview and thumbnail', async function () {
145 const attributes: LiveVideoCreate = {
146 name: 'default live thumbnail',
147 channelId: servers[0].store.channel.id,
148 privacy: VideoPrivacy.UNLISTED,
152 const live = await commands[0].create({ fields: attributes })
153 const videoId = live.uuid
155 await waitJobs(servers)
157 for (const server of servers) {
158 const video = await server.videos.get({ id: videoId })
159 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
160 expect(video.nsfw).to.be.true
162 await makeRawRequest(server.url + video.thumbnailPath, HttpStatusCode.OK_200)
163 await makeRawRequest(server.url + video.previewPath, HttpStatusCode.OK_200)
167 it('Should not have the live listed since nobody streams into', async function () {
168 for (const server of servers) {
169 const { total, data } = await server.videos.list()
171 expect(total).to.equal(0)
172 expect(data).to.have.lengthOf(0)
176 it('Should not be able to update a live of another server', async function () {
177 await commands[1].update({ videoId: liveVideoUUID, fields: { saveReplay: false }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
180 it('Should update the live', async function () {
183 await commands[0].update({ videoId: liveVideoUUID, fields: { saveReplay: false, latencyMode: LiveVideoLatencyMode.DEFAULT } })
184 await waitJobs(servers)
187 it('Have the live updated', async function () {
188 for (const server of servers) {
189 const live = await server.live.get({ videoId: liveVideoUUID })
191 if (server.url === servers[0].url) {
192 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
193 expect(live.streamKey).to.not.be.empty
195 expect(live.rtmpUrl).to.not.exist
196 expect(live.streamKey).to.not.exist
199 expect(live.saveReplay).to.be.false
200 expect(live.latencyMode).to.equal(LiveVideoLatencyMode.DEFAULT)
204 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
365 async function createLiveWrapper (saveReplay: boolean) {
366 const liveAttributes = {
368 channelId: servers[0].store.channel.id,
369 privacy: VideoPrivacy.PUBLIC,
373 const { uuid } = await commands[0].create({ fields: liveAttributes })
377 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
378 for (const server of servers) {
379 const { data } = await server.videos.list()
380 expect(data.find(v => v.uuid === liveVideoId)).to.exist
382 const video = await server.videos.get({ id: liveVideoId })
384 expect(video.streamingPlaylists).to.have.lengthOf(1)
386 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
387 expect(hlsPlaylist).to.exist
389 // Only finite files are displayed
390 expect(hlsPlaylist.files).to.have.lengthOf(0)
392 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
394 for (let i = 0; i < resolutions.length; i++) {
396 const segmentName = `${i}-00000${segmentNum}.ts`
397 await commands[0].waitUntilSegmentGeneration({ videoUUID: video.uuid, playlistNumber: i, segment: segmentNum })
399 const subPlaylist = await servers[0].streamingPlaylists.get({
400 url: `${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`
403 expect(subPlaylist).to.contain(segmentName)
405 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
406 await checkLiveSegmentHash({
408 baseUrlSegment: baseUrlAndPath,
409 videoUUID: video.uuid,
417 function updateConf (resolutions: number[]) {
418 return servers[0].config.updateCustomSubConfig({
427 '144p': resolutions.includes(144),
428 '240p': resolutions.includes(240),
429 '360p': resolutions.includes(360),
430 '480p': resolutions.includes(480),
431 '720p': resolutions.includes(720),
432 '1080p': resolutions.includes(1080),
433 '2160p': resolutions.includes(2160)
441 before(async function () {
445 it('Should enable transcoding without additional resolutions', async function () {
448 liveVideoId = await createLiveWrapper(false)
450 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
451 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
452 await waitJobs(servers)
454 await testVideoResolutions(liveVideoId, [ 720 ])
456 await stopFfmpeg(ffmpegCommand)
459 it('Should enable transcoding with some resolutions', async function () {
462 const resolutions = [ 240, 480 ]
463 await updateConf(resolutions)
464 liveVideoId = await createLiveWrapper(false)
466 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
467 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
468 await waitJobs(servers)
470 await testVideoResolutions(liveVideoId, resolutions.concat([ 720 ]))
472 await stopFfmpeg(ffmpegCommand)
475 it('Should correctly set the appropriate bitrate depending on the input', async function () {
478 liveVideoId = await createLiveWrapper(false)
480 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({
481 videoId: liveVideoId,
482 fixtureName: 'video_short.mp4',
485 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
486 await waitJobs(servers)
488 const video = await servers[0].videos.get({ id: liveVideoId })
490 const masterPlaylist = video.streamingPlaylists[0].playlistUrl
491 const probe = await ffprobePromise(masterPlaylist)
493 const bitrates = probe.streams.map(s => parseInt(s.tags.variant_bitrate))
494 for (const bitrate of bitrates) {
495 expect(bitrate).to.exist
496 expect(isNaN(bitrate)).to.be.false
497 expect(bitrate).to.be.below(61_000_000) // video_short.mp4 bitrate
500 await stopFfmpeg(ffmpegCommand)
503 it('Should enable transcoding with some resolutions and correctly save them', async function () {
504 this.timeout(400_000)
506 const resolutions = [ 240, 360, 720 ]
508 await updateConf(resolutions)
509 liveVideoId = await createLiveWrapper(true)
511 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
512 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
513 await waitJobs(servers)
515 await testVideoResolutions(liveVideoId, resolutions)
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(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
545 await makeRawRequest(hlsPlaylist.segmentsSha256Url, 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, 2)
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(file.torrentUrl, HttpStatusCode.OK_200)
577 await makeRawRequest(file.fileUrl, 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(liveVideoId, resolutions)
606 await stopFfmpeg(ffmpegCommand)
607 await commands[0].waitUntilEnded({ videoId: liveVideoId })
609 await waitJobs(servers)
611 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
613 const video = await servers[0].videos.get({ id: liveVideoId })
614 const hlsFiles = video.streamingPlaylists[0].files
616 expect(video.files).to.have.lengthOf(0)
617 expect(hlsFiles).to.have.lengthOf(resolutions.length)
619 // eslint-disable-next-line @typescript-eslint/require-array-sort-compare
620 expect(getAllFiles(video).map(f => f.resolution.id).sort()).to.deep.equal(resolutions)
623 it('Should only keep the original resolution if all resolutions are disabled', async function () {
624 this.timeout(600_000)
627 liveVideoId = await createLiveWrapper(true)
629 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
630 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
631 await waitJobs(servers)
633 await testVideoResolutions(liveVideoId, [ 720 ])
635 await stopFfmpeg(ffmpegCommand)
636 await commands[0].waitUntilEnded({ videoId: liveVideoId })
638 await waitJobs(servers)
640 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
642 const video = await servers[0].videos.get({ id: liveVideoId })
643 const hlsFiles = video.streamingPlaylists[0].files
645 expect(video.files).to.have.lengthOf(0)
646 expect(hlsFiles).to.have.lengthOf(1)
648 expect(hlsFiles[0].resolution.id).to.equal(720)
652 describe('After a server restart', function () {
653 let liveVideoId: string
654 let liveVideoReplayId: string
655 let permanentLiveVideoReplayId: string
657 let permanentLiveReplayName: string
659 let beforeServerRestart: Date
661 async function createLiveWrapper (options: { saveReplay: boolean, permanent: boolean }) {
662 const liveAttributes: LiveVideoCreate = {
664 channelId: servers[0].store.channel.id,
665 privacy: VideoPrivacy.PUBLIC,
666 saveReplay: options.saveReplay,
667 permanentLive: options.permanent
670 const { uuid } = await commands[0].create({ fields: liveAttributes })
674 before(async function () {
675 this.timeout(600_000)
677 liveVideoId = await createLiveWrapper({ saveReplay: false, permanent: false })
678 liveVideoReplayId = await createLiveWrapper({ saveReplay: true, permanent: false })
679 permanentLiveVideoReplayId = await createLiveWrapper({ saveReplay: true, permanent: true })
682 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId }),
683 commands[0].sendRTMPStreamInVideo({ videoId: permanentLiveVideoReplayId }),
684 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoReplayId })
688 commands[0].waitUntilPublished({ videoId: liveVideoId }),
689 commands[0].waitUntilPublished({ videoId: permanentLiveVideoReplayId }),
690 commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
693 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoId, playlistNumber: 0, segment: 2 })
694 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoReplayId, playlistNumber: 0, segment: 2 })
695 await commands[0].waitUntilSegmentGeneration({ videoUUID: permanentLiveVideoReplayId, playlistNumber: 0, segment: 2 })
698 const video = await servers[0].videos.get({ id: permanentLiveVideoReplayId })
699 permanentLiveReplayName = video.name + ' - ' + new Date(video.publishedAt).toLocaleString()
702 await killallServers([ servers[0] ])
704 beforeServerRestart = new Date()
705 await servers[0].run()
708 await waitJobs(servers)
711 it('Should cleanup lives', async function () {
714 await commands[0].waitUntilEnded({ videoId: liveVideoId })
715 await commands[0].waitUntilWaiting({ videoId: permanentLiveVideoReplayId })
718 it('Should save a non permanent live replay', async function () {
721 await commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
723 const session = await commands[0].getReplaySession({ videoId: liveVideoReplayId })
724 expect(session.endDate).to.exist
725 expect(new Date(session.endDate)).to.be.above(beforeServerRestart)
728 it('Should have saved a permanent live replay', async function () {
731 const { data } = await servers[0].videos.listMyVideos({ sort: '-publishedAt' })
732 expect(data.find(v => v.name === permanentLiveReplayName)).to.exist
736 after(async function () {
737 await cleanupTests(servers)