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, getVideoStreamFromFile } from '@server/helpers/ffprobe-utils'
8 checkLiveCleanupAfterSave,
10 checkResolutionsInMasterPlaylist,
12 createMultipleServers,
19 setAccessTokensToServers,
20 setDefaultVideoChannel,
22 testFfmpegStreamError,
26 waitUntilLivePublishedOnAllServers
27 } from '@shared/extra-utils'
35 VideoStreamingPlaylistType
36 } from '@shared/models'
38 const expect = chai.expect
40 describe('Test live', function () {
41 let servers: PeerTubeServer[] = []
42 let commands: LiveCommand[]
44 before(async function () {
47 servers = await createMultipleServers(2)
49 // Get the access tokens
50 await setAccessTokensToServers(servers)
51 await setDefaultVideoChannel(servers)
53 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 privacy: VideoPrivacy.PUBLIC,
92 previewfile: 'video_short1-preview.webm.jpg',
93 thumbnailfile: 'video_short1.webm.jpg'
96 const live = await commands[0].create({ fields: attributes })
97 liveVideoUUID = live.uuid
99 await waitJobs(servers)
101 for (const server of servers) {
102 const video = await server.videos.get({ id: liveVideoUUID })
104 expect(video.category.id).to.equal(1)
105 expect(video.licence.id).to.equal(2)
106 expect(video.language.id).to.equal('fr')
107 expect(video.description).to.equal('super live description')
108 expect(video.support).to.equal('support field')
110 expect(video.channel.name).to.equal(servers[0].store.channel.name)
111 expect(video.channel.host).to.equal(servers[0].store.channel.host)
113 expect(video.isLive).to.be.true
115 expect(video.nsfw).to.be.false
116 expect(video.waitTranscoding).to.be.false
117 expect(video.name).to.equal('my super live')
118 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
119 expect(video.commentsEnabled).to.be.false
120 expect(video.downloadEnabled).to.be.false
121 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
123 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
124 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
126 const live = await server.live.get({ videoId: liveVideoUUID })
128 if (server.url === servers[0].url) {
129 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
130 expect(live.streamKey).to.not.be.empty
132 expect(live.rtmpUrl).to.be.null
133 expect(live.streamKey).to.be.null
136 expect(live.saveReplay).to.be.true
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 } })
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.be.null
194 expect(live.streamKey).to.be.null
197 expect(live.saveReplay).to.be.false
201 it('Delete the live', async function () {
204 await servers[0].videos.remove({ id: liveVideoUUID })
205 await waitJobs(servers)
208 it('Should have the live deleted', async function () {
209 for (const server of servers) {
210 await server.videos.get({ id: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
211 await server.live.get({ videoId: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
216 describe('Live filters', function () {
217 let ffmpegCommand: any
218 let liveVideoId: string
219 let vodVideoId: string
221 before(async function () {
224 vodVideoId = (await servers[0].videos.quickUpload({ name: 'vod video' })).uuid
226 const liveOptions = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: servers[0].store.channel.id }
227 const live = await commands[0].create({ fields: liveOptions })
228 liveVideoId = live.uuid
230 ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoId })
231 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
232 await waitJobs(servers)
235 it('Should only display lives', async function () {
236 const { data, total } = await servers[0].videos.list({ isLive: true })
238 expect(total).to.equal(1)
239 expect(data).to.have.lengthOf(1)
240 expect(data[0].name).to.equal('live')
243 it('Should not display lives', async function () {
244 const { data, total } = await servers[0].videos.list({ isLive: false })
246 expect(total).to.equal(1)
247 expect(data).to.have.lengthOf(1)
248 expect(data[0].name).to.equal('vod video')
251 it('Should display my lives', async function () {
254 await stopFfmpeg(ffmpegCommand)
255 await waitJobs(servers)
257 const { data } = await servers[0].videos.listMyVideos({ isLive: true })
259 const result = data.every(v => v.isLive)
260 expect(result).to.be.true
263 it('Should not display my lives', async function () {
264 const { data } = await servers[0].videos.listMyVideos({ isLive: false })
266 const result = data.every(v => !v.isLive)
267 expect(result).to.be.true
270 after(async function () {
271 await servers[0].videos.remove({ id: vodVideoId })
272 await servers[0].videos.remove({ id: liveVideoId })
276 describe('Stream checks', function () {
277 let liveVideo: LiveVideo & VideoDetails
281 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
284 async function createLiveWrapper () {
285 const liveAttributes = {
287 channelId: servers[0].store.channel.id,
288 privacy: VideoPrivacy.PUBLIC,
292 const { uuid } = await commands[0].create({ fields: liveAttributes })
294 const live = await commands[0].get({ videoId: uuid })
295 const video = await servers[0].videos.get({ id: uuid })
297 return Object.assign(video, live)
300 it('Should not allow a stream without the appropriate path', async function () {
303 liveVideo = await createLiveWrapper()
305 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/bad-live', streamKey: liveVideo.streamKey })
306 await testFfmpegStreamError(command, true)
309 it('Should not allow a stream without the appropriate stream key', async function () {
312 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: 'bad-stream-key' })
313 await testFfmpegStreamError(command, true)
316 it('Should succeed with the correct params', async function () {
319 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
320 await testFfmpegStreamError(command, false)
323 it('Should list this live now someone stream into it', async function () {
324 for (const server of servers) {
325 const { total, data } = await server.videos.list()
327 expect(total).to.equal(1)
328 expect(data).to.have.lengthOf(1)
330 const video = data[0]
331 expect(video.name).to.equal('user live')
332 expect(video.isLive).to.be.true
336 it('Should not allow a stream on a live that was blacklisted', async function () {
339 liveVideo = await createLiveWrapper()
341 await servers[0].blacklist.add({ videoId: liveVideo.uuid })
343 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
344 await testFfmpegStreamError(command, true)
347 it('Should not allow a stream on a live that was deleted', async function () {
350 liveVideo = await createLiveWrapper()
352 await servers[0].videos.remove({ id: liveVideo.uuid })
354 const command = sendRTMPStream({ rtmpBaseUrl: rtmpUrl + '/live', streamKey: liveVideo.streamKey })
355 await testFfmpegStreamError(command, true)
359 describe('Live transcoding', function () {
360 let liveVideoId: string
362 async function createLiveWrapper (saveReplay: boolean) {
363 const liveAttributes = {
365 channelId: servers[0].store.channel.id,
366 privacy: VideoPrivacy.PUBLIC,
370 const { uuid } = await commands[0].create({ fields: liveAttributes })
374 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
375 for (const server of servers) {
376 const { data } = await server.videos.list()
377 expect(data.find(v => v.uuid === liveVideoId)).to.exist
379 const video = await server.videos.get({ id: liveVideoId })
381 expect(video.streamingPlaylists).to.have.lengthOf(1)
383 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
384 expect(hlsPlaylist).to.exist
386 // Only finite files are displayed
387 expect(hlsPlaylist.files).to.have.lengthOf(0)
389 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
391 for (let i = 0; i < resolutions.length; i++) {
393 const segmentName = `${i}-00000${segmentNum}.ts`
394 await commands[0].waitUntilSegmentGeneration({ videoUUID: video.uuid, resolution: i, segment: segmentNum })
396 const subPlaylist = await servers[0].streamingPlaylists.get({
397 url: `${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`
400 expect(subPlaylist).to.contain(segmentName)
402 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
403 await checkLiveSegmentHash({
405 baseUrlSegment: baseUrlAndPath,
406 videoUUID: video.uuid,
414 function updateConf (resolutions: number[]) {
415 return servers[0].config.updateCustomSubConfig({
424 '240p': resolutions.includes(240),
425 '360p': resolutions.includes(360),
426 '480p': resolutions.includes(480),
427 '720p': resolutions.includes(720),
428 '1080p': resolutions.includes(1080),
429 '2160p': resolutions.includes(2160)
437 before(async function () {
441 it('Should enable transcoding without additional resolutions', async function () {
444 liveVideoId = await createLiveWrapper(false)
446 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
447 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
448 await waitJobs(servers)
450 await testVideoResolutions(liveVideoId, [ 720 ])
452 await stopFfmpeg(ffmpegCommand)
455 it('Should enable transcoding with some resolutions', async function () {
458 const resolutions = [ 240, 480 ]
459 await updateConf(resolutions)
460 liveVideoId = await createLiveWrapper(false)
462 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
463 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
464 await waitJobs(servers)
466 await testVideoResolutions(liveVideoId, resolutions)
468 await stopFfmpeg(ffmpegCommand)
471 it('Should correctly set the appropriate bitrate depending on the input', async function () {
474 liveVideoId = await createLiveWrapper(false)
476 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({
477 videoId: liveVideoId,
478 fixtureName: 'video_short.mp4',
481 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
482 await waitJobs(servers)
484 const video = await servers[0].videos.get({ id: liveVideoId })
486 const masterPlaylist = video.streamingPlaylists[0].playlistUrl
487 const probe = await ffprobePromise(masterPlaylist)
489 const bitrates = probe.streams.map(s => parseInt(s.tags.variant_bitrate))
490 for (const bitrate of bitrates) {
491 expect(bitrate).to.exist
492 expect(isNaN(bitrate)).to.be.false
493 expect(bitrate).to.be.below(61_000_000) // video_short.mp4 bitrate
496 await stopFfmpeg(ffmpegCommand)
499 it('Should enable transcoding with some resolutions and correctly save them', async function () {
502 const resolutions = [ 240, 360, 720 ]
504 await updateConf(resolutions)
505 liveVideoId = await createLiveWrapper(true)
507 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
508 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
509 await waitJobs(servers)
511 await testVideoResolutions(liveVideoId, resolutions)
513 await stopFfmpeg(ffmpegCommand)
514 await commands[0].waitUntilEnded({ videoId: liveVideoId })
516 await waitJobs(servers)
518 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
520 const bitrateLimits = {
521 720: 5000 * 1000, // 60FPS
526 for (const server of servers) {
527 const video = await server.videos.get({ id: liveVideoId })
529 expect(video.state.id).to.equal(VideoState.PUBLISHED)
530 expect(video.duration).to.be.greaterThan(1)
531 expect(video.files).to.have.lengthOf(0)
533 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
534 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
535 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
537 // We should have generated random filenames
538 expect(basename(hlsPlaylist.playlistUrl)).to.not.equal('master.m3u8')
539 expect(basename(hlsPlaylist.segmentsSha256Url)).to.not.equal('segments-sha256.json')
541 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
543 for (const resolution of resolutions) {
544 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
546 expect(file).to.exist
547 expect(file.size).to.be.greaterThan(1)
549 if (resolution >= 720) {
550 expect(file.fps).to.be.approximately(60, 2)
552 expect(file.fps).to.be.approximately(30, 2)
555 const filename = basename(file.fileUrl)
556 expect(filename).to.not.contain(video.uuid)
558 const segmentPath = servers[0].servers.buildDirectory(join('streaming-playlists', 'hls', video.uuid, filename))
560 const probe = await ffprobePromise(segmentPath)
561 const videoStream = await getVideoStreamFromFile(segmentPath, probe)
563 expect(probe.format.bit_rate).to.be.below(bitrateLimits[videoStream.height])
565 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
566 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
571 it('Should correctly have cleaned up the live files', async function () {
574 await checkLiveCleanupAfterSave(servers[0], liveVideoId, [ 240, 360, 720 ])
578 describe('After a server restart', function () {
579 let liveVideoId: string
580 let liveVideoReplayId: string
582 async function createLiveWrapper (saveReplay: boolean) {
583 const liveAttributes = {
585 channelId: servers[0].store.channel.id,
586 privacy: VideoPrivacy.PUBLIC,
590 const { uuid } = await commands[0].create({ fields: liveAttributes })
594 before(async function () {
597 liveVideoId = await createLiveWrapper(false)
598 liveVideoReplayId = await createLiveWrapper(true)
601 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId }),
602 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoReplayId })
606 commands[0].waitUntilPublished({ videoId: liveVideoId }),
607 commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
610 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoId, resolution: 0, segment: 2 })
611 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoReplayId, resolution: 0, segment: 2 })
613 await killallServers([ servers[0] ])
614 await servers[0].run()
619 it('Should cleanup lives', async function () {
622 await commands[0].waitUntilEnded({ videoId: liveVideoId })
625 it('Should save a live replay', async function () {
628 await commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
632 after(async function () {
633 await cleanupTests(servers)