1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { basename, join } from 'path'
6 import { ffprobePromise, getVideoStream } from '@server/helpers/ffmpeg'
7 import { checkLiveCleanup, checkLiveSegmentHash, checkResolutionsInMasterPlaylist, testImage } from '@server/tests/shared'
8 import { wait } from '@shared/core-utils'
17 VideoStreamingPlaylistType
18 } from '@shared/models'
21 createMultipleServers,
28 setAccessTokensToServers,
29 setDefaultVideoChannel,
31 testFfmpegStreamError,
33 waitUntilLivePublishedOnAllServers
34 } from '@shared/server-commands'
36 const expect = chai.expect
38 describe('Test live', function () {
39 let servers: PeerTubeServer[] = []
40 let commands: LiveCommand[]
42 before(async function () {
45 servers = await createMultipleServers(2)
47 // Get the access tokens
48 await setAccessTokensToServers(servers)
49 await setDefaultVideoChannel(servers)
51 await servers[0].config.updateCustomSubConfig({
66 // Server 1 and server 2 follow each other
67 await doubleFollow(servers[0], servers[1])
69 commands = servers.map(s => s.live)
72 describe('Live creation, update and delete', function () {
73 let liveVideoUUID: string
75 it('Should create a live with the appropriate parameters', async function () {
78 const attributes: LiveVideoCreate = {
82 description: 'super live description',
83 support: 'support field',
84 channelId: servers[0].store.channel.id,
86 waitTranscoding: false,
87 name: 'my super live',
88 tags: [ 'tag1', 'tag2' ],
89 commentsEnabled: false,
90 downloadEnabled: false,
92 latencyMode: LiveVideoLatencyMode.SMALL_LATENCY,
93 privacy: VideoPrivacy.PUBLIC,
94 previewfile: 'video_short1-preview.webm.jpg',
95 thumbnailfile: 'video_short1.webm.jpg'
98 const live = await commands[0].create({ fields: attributes })
99 liveVideoUUID = live.uuid
101 await waitJobs(servers)
103 for (const server of servers) {
104 const video = await server.videos.get({ id: liveVideoUUID })
106 expect(video.category.id).to.equal(1)
107 expect(video.licence.id).to.equal(2)
108 expect(video.language.id).to.equal('fr')
109 expect(video.description).to.equal('super live description')
110 expect(video.support).to.equal('support field')
112 expect(video.channel.name).to.equal(servers[0].store.channel.name)
113 expect(video.channel.host).to.equal(servers[0].store.channel.host)
115 expect(video.isLive).to.be.true
117 expect(video.nsfw).to.be.false
118 expect(video.waitTranscoding).to.be.false
119 expect(video.name).to.equal('my super live')
120 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
121 expect(video.commentsEnabled).to.be.false
122 expect(video.downloadEnabled).to.be.false
123 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
125 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
126 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
128 const live = await server.live.get({ videoId: liveVideoUUID })
130 if (server.url === servers[0].url) {
131 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
132 expect(live.streamKey).to.not.be.empty
134 expect(live.rtmpUrl).to.not.exist
135 expect(live.streamKey).to.not.exist
138 expect(live.saveReplay).to.be.true
139 expect(live.latencyMode).to.equal(LiveVideoLatencyMode.SMALL_LATENCY)
143 it('Should have a default preview and thumbnail', async function () {
146 const attributes: LiveVideoCreate = {
147 name: 'default live thumbnail',
148 channelId: servers[0].store.channel.id,
149 privacy: VideoPrivacy.UNLISTED,
153 const live = await commands[0].create({ fields: attributes })
154 const videoId = live.uuid
156 await waitJobs(servers)
158 for (const server of servers) {
159 const video = await server.videos.get({ id: videoId })
160 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
161 expect(video.nsfw).to.be.true
163 await makeRawRequest(server.url + video.thumbnailPath, HttpStatusCode.OK_200)
164 await makeRawRequest(server.url + video.previewPath, HttpStatusCode.OK_200)
168 it('Should not have the live listed since nobody streams into', async function () {
169 for (const server of servers) {
170 const { total, data } = await server.videos.list()
172 expect(total).to.equal(0)
173 expect(data).to.have.lengthOf(0)
177 it('Should not be able to update a live of another server', async function () {
178 await commands[1].update({ videoId: liveVideoUUID, fields: { saveReplay: false }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
181 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.latencyMode).to.equal(LiveVideoLatencyMode.DEFAULT)
205 it('Delete the live', async function () {
208 await servers[0].videos.remove({ id: liveVideoUUID })
209 await waitJobs(servers)
212 it('Should have the live deleted', async function () {
213 for (const server of servers) {
214 await server.videos.get({ id: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
215 await server.live.get({ videoId: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
220 describe('Live filters', function () {
221 let ffmpegCommand: any
222 let liveVideoId: string
223 let vodVideoId: string
225 before(async function () {
228 vodVideoId = (await servers[0].videos.quickUpload({ name: 'vod video' })).uuid
230 const liveOptions = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: servers[0].store.channel.id }
231 const live = await commands[0].create({ fields: liveOptions })
232 liveVideoId = live.uuid
234 ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoId })
235 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
236 await waitJobs(servers)
239 it('Should only display lives', async function () {
240 const { data, total } = await servers[0].videos.list({ isLive: true })
242 expect(total).to.equal(1)
243 expect(data).to.have.lengthOf(1)
244 expect(data[0].name).to.equal('live')
247 it('Should not display lives', async function () {
248 const { data, total } = await servers[0].videos.list({ isLive: false })
250 expect(total).to.equal(1)
251 expect(data).to.have.lengthOf(1)
252 expect(data[0].name).to.equal('vod video')
255 it('Should display my lives', async function () {
258 await stopFfmpeg(ffmpegCommand)
259 await waitJobs(servers)
261 const { data } = await servers[0].videos.listMyVideos({ isLive: true })
263 const result = data.every(v => v.isLive)
264 expect(result).to.be.true
267 it('Should not display my lives', async function () {
268 const { data } = await servers[0].videos.listMyVideos({ isLive: false })
270 const result = data.every(v => !v.isLive)
271 expect(result).to.be.true
274 after(async function () {
275 await servers[0].videos.remove({ id: vodVideoId })
276 await servers[0].videos.remove({ id: liveVideoId })
280 describe('Stream checks', function () {
281 let liveVideo: LiveVideo & VideoDetails
285 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
288 async function createLiveWrapper () {
289 const liveAttributes = {
291 channelId: servers[0].store.channel.id,
292 privacy: VideoPrivacy.PUBLIC,
296 const { uuid } = await commands[0].create({ fields: liveAttributes })
298 const live = await commands[0].get({ videoId: uuid })
299 const video = await servers[0].videos.get({ id: uuid })
301 return Object.assign(video, live)
304 it('Should not allow a stream without the appropriate path', async function () {
307 liveVideo = await createLiveWrapper()
309 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/bad-live', streamKey: liveVideo.streamKey })
310 await testFfmpegStreamError(command, true)
313 it('Should not allow a stream without the appropriate stream key', async function () {
316 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: 'bad-stream-key' })
317 await testFfmpegStreamError(command, true)
320 it('Should succeed with the correct params', async function () {
323 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
324 await testFfmpegStreamError(command, false)
327 it('Should list this live now someone stream into it', async function () {
328 for (const server of servers) {
329 const { total, data } = await server.videos.list()
331 expect(total).to.equal(1)
332 expect(data).to.have.lengthOf(1)
334 const video = data[0]
335 expect(video.name).to.equal('user live')
336 expect(video.isLive).to.be.true
340 it('Should not allow a stream on a live that was blacklisted', async function () {
343 liveVideo = await createLiveWrapper()
345 await servers[0].blacklist.add({ videoId: liveVideo.uuid })
347 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
348 await testFfmpegStreamError(command, true)
351 it('Should not allow a stream on a live that was deleted', async function () {
354 liveVideo = await createLiveWrapper()
356 await servers[0].videos.remove({ id: liveVideo.uuid })
358 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
359 await testFfmpegStreamError(command, true)
363 describe('Live transcoding', function () {
364 let liveVideoId: string
366 async function createLiveWrapper (saveReplay: boolean) {
367 const liveAttributes = {
369 channelId: servers[0].store.channel.id,
370 privacy: VideoPrivacy.PUBLIC,
374 const { uuid } = await commands[0].create({ fields: liveAttributes })
378 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
379 for (const server of servers) {
380 const { data } = await server.videos.list()
381 expect(data.find(v => v.uuid === liveVideoId)).to.exist
383 const video = await server.videos.get({ id: liveVideoId })
385 expect(video.streamingPlaylists).to.have.lengthOf(1)
387 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
388 expect(hlsPlaylist).to.exist
390 // Only finite files are displayed
391 expect(hlsPlaylist.files).to.have.lengthOf(0)
393 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
395 for (let i = 0; i < resolutions.length; i++) {
397 const segmentName = `${i}-00000${segmentNum}.ts`
398 await commands[0].waitUntilSegmentGeneration({ videoUUID: video.uuid, resolution: i, segment: segmentNum })
400 const subPlaylist = await servers[0].streamingPlaylists.get({
401 url: `${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`
404 expect(subPlaylist).to.contain(segmentName)
406 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
407 await checkLiveSegmentHash({
409 baseUrlSegment: baseUrlAndPath,
410 videoUUID: video.uuid,
418 function updateConf (resolutions: number[]) {
419 return servers[0].config.updateCustomSubConfig({
428 '144p': resolutions.includes(144),
429 '240p': resolutions.includes(240),
430 '360p': resolutions.includes(360),
431 '480p': resolutions.includes(480),
432 '720p': resolutions.includes(720),
433 '1080p': resolutions.includes(1080),
434 '2160p': resolutions.includes(2160)
442 before(async function () {
446 it('Should enable transcoding without additional resolutions', async function () {
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 testVideoResolutions(liveVideoId, [ 720 ])
457 await stopFfmpeg(ffmpegCommand)
460 it('Should enable transcoding with some resolutions', async function () {
463 const resolutions = [ 240, 480 ]
464 await updateConf(resolutions)
465 liveVideoId = await createLiveWrapper(false)
467 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
468 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
469 await waitJobs(servers)
471 await testVideoResolutions(liveVideoId, resolutions)
473 await stopFfmpeg(ffmpegCommand)
476 it('Should correctly set the appropriate bitrate depending on the input', async function () {
479 liveVideoId = await createLiveWrapper(false)
481 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({
482 videoId: liveVideoId,
483 fixtureName: 'video_short.mp4',
486 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
487 await waitJobs(servers)
489 const video = await servers[0].videos.get({ id: liveVideoId })
491 const masterPlaylist = video.streamingPlaylists[0].playlistUrl
492 const probe = await ffprobePromise(masterPlaylist)
494 const bitrates = probe.streams.map(s => parseInt(s.tags.variant_bitrate))
495 for (const bitrate of bitrates) {
496 expect(bitrate).to.exist
497 expect(isNaN(bitrate)).to.be.false
498 expect(bitrate).to.be.below(61_000_000) // video_short.mp4 bitrate
501 await stopFfmpeg(ffmpegCommand)
504 it('Should enable transcoding with some resolutions and correctly save them', async function () {
505 this.timeout(400_000)
507 const resolutions = [ 240, 360, 720 ]
509 await updateConf(resolutions)
510 liveVideoId = await createLiveWrapper(true)
512 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
513 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
514 await waitJobs(servers)
516 await testVideoResolutions(liveVideoId, resolutions)
518 await stopFfmpeg(ffmpegCommand)
519 await commands[0].waitUntilEnded({ videoId: liveVideoId })
521 await waitJobs(servers)
523 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
525 const maxBitrateLimits = {
526 720: 6500 * 1000, // 60FPS
531 const minBitrateLimits = {
537 for (const server of servers) {
538 const video = await server.videos.get({ id: liveVideoId })
540 expect(video.state.id).to.equal(VideoState.PUBLISHED)
541 expect(video.duration).to.be.greaterThan(1)
542 expect(video.files).to.have.lengthOf(0)
544 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
545 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
546 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
548 // We should have generated random filenames
549 expect(basename(hlsPlaylist.playlistUrl)).to.not.equal('master.m3u8')
550 expect(basename(hlsPlaylist.segmentsSha256Url)).to.not.equal('segments-sha256.json')
552 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
554 for (const resolution of resolutions) {
555 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
557 expect(file).to.exist
558 expect(file.size).to.be.greaterThan(1)
560 if (resolution >= 720) {
561 expect(file.fps).to.be.approximately(60, 2)
563 expect(file.fps).to.be.approximately(30, 2)
566 const filename = basename(file.fileUrl)
567 expect(filename).to.not.contain(video.uuid)
569 const segmentPath = servers[0].servers.buildDirectory(join('streaming-playlists', 'hls', video.uuid, filename))
571 const probe = await ffprobePromise(segmentPath)
572 const videoStream = await getVideoStream(segmentPath, probe)
574 expect(probe.format.bit_rate).to.be.below(maxBitrateLimits[videoStream.height])
575 expect(probe.format.bit_rate).to.be.at.least(minBitrateLimits[videoStream.height])
577 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
578 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
583 it('Should correctly have cleaned up the live files', async function () {
586 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
590 describe('After a server restart', function () {
591 let liveVideoId: string
592 let liveVideoReplayId: string
593 let permanentLiveVideoReplayId: string
595 let permanentLiveReplayName: string
597 let beforeServerRestart: Date
599 async function createLiveWrapper (options: { saveReplay: boolean, permanent: boolean }) {
600 const liveAttributes: LiveVideoCreate = {
602 channelId: servers[0].store.channel.id,
603 privacy: VideoPrivacy.PUBLIC,
604 saveReplay: options.saveReplay,
605 permanentLive: options.permanent
608 const { uuid } = await commands[0].create({ fields: liveAttributes })
612 before(async function () {
615 liveVideoId = await createLiveWrapper({ saveReplay: false, permanent: false })
616 liveVideoReplayId = await createLiveWrapper({ saveReplay: true, permanent: false })
617 permanentLiveVideoReplayId = await createLiveWrapper({ saveReplay: true, permanent: true })
620 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId }),
621 commands[0].sendRTMPStreamInVideo({ videoId: permanentLiveVideoReplayId }),
622 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoReplayId })
626 commands[0].waitUntilPublished({ videoId: liveVideoId }),
627 commands[0].waitUntilPublished({ videoId: permanentLiveVideoReplayId }),
628 commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
631 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoId, resolution: 0, segment: 2 })
632 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoReplayId, resolution: 0, segment: 2 })
633 await commands[0].waitUntilSegmentGeneration({ videoUUID: permanentLiveVideoReplayId, resolution: 0, segment: 2 })
636 const video = await servers[0].videos.get({ id: permanentLiveVideoReplayId })
637 permanentLiveReplayName = video.name + ' - ' + new Date(video.publishedAt).toLocaleString()
640 await killallServers([ servers[0] ])
642 beforeServerRestart = new Date()
643 await servers[0].run()
646 await waitJobs(servers)
649 it('Should cleanup lives', async function () {
652 await commands[0].waitUntilEnded({ videoId: liveVideoId })
653 await commands[0].waitUntilWaiting({ videoId: permanentLiveVideoReplayId })
656 it('Should save a non permanent live replay', async function () {
659 await commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
661 const session = await commands[0].getReplaySession({ videoId: liveVideoReplayId })
662 expect(session.endDate).to.exist
663 expect(new Date(session.endDate)).to.be.above(beforeServerRestart)
666 it('Should have saved a permanent live replay', async function () {
669 const { data } = await servers[0].videos.listMyVideos({ sort: '-publishedAt' })
670 expect(data.find(v => v.name === permanentLiveReplayName)).to.exist
674 after(async function () {
675 await cleanupTests(servers)