1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { join } from 'path'
6 import { ffprobePromise, getVideoStreamFromFile } from '@server/helpers/ffprobe-utils'
7 import { HttpStatusCode } from '@shared/core-utils'
11 checkResolutionsInMasterPlaylist,
14 createMultipleServers,
20 setAccessTokensToServers,
21 setDefaultVideoChannel,
23 testFfmpegStreamError,
27 waitUntilLivePublishedOnAllServers
28 } from '@shared/extra-utils'
29 import { LiveVideo, LiveVideoCreate, VideoDetails, VideoPrivacy, VideoState, VideoStreamingPlaylistType } from '@shared/models'
31 const expect = chai.expect
33 describe('Test live', function () {
34 let servers: PeerTubeServer[] = []
35 let commands: LiveCommand[]
37 before(async function () {
40 servers = await createMultipleServers(2)
42 // Get the access tokens
43 await setAccessTokensToServers(servers)
44 await setDefaultVideoChannel(servers)
46 await servers[0].config.updateCustomSubConfig({
58 // Server 1 and server 2 follow each other
59 await doubleFollow(servers[0], servers[1])
61 commands = servers.map(s => s.live)
64 describe('Live creation, update and delete', function () {
65 let liveVideoUUID: string
67 it('Should create a live with the appropriate parameters', async function () {
70 const attributes: LiveVideoCreate = {
74 description: 'super live description',
75 support: 'support field',
76 channelId: servers[0].store.channel.id,
78 waitTranscoding: false,
79 name: 'my super live',
80 tags: [ 'tag1', 'tag2' ],
81 commentsEnabled: false,
82 downloadEnabled: false,
84 privacy: VideoPrivacy.PUBLIC,
85 previewfile: 'video_short1-preview.webm.jpg',
86 thumbnailfile: 'video_short1.webm.jpg'
89 const live = await commands[0].create({ fields: attributes })
90 liveVideoUUID = live.uuid
92 await waitJobs(servers)
94 for (const server of servers) {
95 const video = await server.videos.get({ id: liveVideoUUID })
97 expect(video.category.id).to.equal(1)
98 expect(video.licence.id).to.equal(2)
99 expect(video.language.id).to.equal('fr')
100 expect(video.description).to.equal('super live description')
101 expect(video.support).to.equal('support field')
103 expect(video.channel.name).to.equal(servers[0].store.channel.name)
104 expect(video.channel.host).to.equal(servers[0].store.channel.host)
106 expect(video.isLive).to.be.true
108 expect(video.nsfw).to.be.false
109 expect(video.waitTranscoding).to.be.false
110 expect(video.name).to.equal('my super live')
111 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
112 expect(video.commentsEnabled).to.be.false
113 expect(video.downloadEnabled).to.be.false
114 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
116 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
117 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
119 const live = await server.live.get({ videoId: liveVideoUUID })
121 if (server.url === servers[0].url) {
122 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
123 expect(live.streamKey).to.not.be.empty
125 expect(live.rtmpUrl).to.be.null
126 expect(live.streamKey).to.be.null
129 expect(live.saveReplay).to.be.true
133 it('Should have a default preview and thumbnail', async function () {
136 const attributes: LiveVideoCreate = {
137 name: 'default live thumbnail',
138 channelId: servers[0].store.channel.id,
139 privacy: VideoPrivacy.UNLISTED,
143 const live = await commands[0].create({ fields: attributes })
144 const videoId = live.uuid
146 await waitJobs(servers)
148 for (const server of servers) {
149 const video = await server.videos.get({ id: videoId })
150 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
151 expect(video.nsfw).to.be.true
153 await makeRawRequest(server.url + video.thumbnailPath, HttpStatusCode.OK_200)
154 await makeRawRequest(server.url + video.previewPath, HttpStatusCode.OK_200)
158 it('Should not have the live listed since nobody streams into', async function () {
159 for (const server of servers) {
160 const { total, data } = await server.videos.list()
162 expect(total).to.equal(0)
163 expect(data).to.have.lengthOf(0)
167 it('Should not be able to update a live of another server', async function () {
168 await commands[1].update({ videoId: liveVideoUUID, fields: { saveReplay: false }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
171 it('Should update the live', async function () {
174 await commands[0].update({ videoId: liveVideoUUID, fields: { saveReplay: false } })
175 await waitJobs(servers)
178 it('Have the live updated', async function () {
179 for (const server of servers) {
180 const live = await server.live.get({ videoId: liveVideoUUID })
182 if (server.url === servers[0].url) {
183 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
184 expect(live.streamKey).to.not.be.empty
186 expect(live.rtmpUrl).to.be.null
187 expect(live.streamKey).to.be.null
190 expect(live.saveReplay).to.be.false
194 it('Delete the live', async function () {
197 await servers[0].videos.remove({ id: liveVideoUUID })
198 await waitJobs(servers)
201 it('Should have the live deleted', async function () {
202 for (const server of servers) {
203 await server.videos.get({ id: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
204 await server.live.get({ videoId: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
209 describe('Live filters', function () {
210 let ffmpegCommand: any
211 let liveVideoId: string
212 let vodVideoId: string
214 before(async function () {
217 vodVideoId = (await servers[0].videos.quickUpload({ name: 'vod video' })).uuid
219 const liveOptions = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: servers[0].store.channel.id }
220 const live = await commands[0].create({ fields: liveOptions })
221 liveVideoId = live.uuid
223 ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoId })
224 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
225 await waitJobs(servers)
228 it('Should only display lives', async function () {
229 const { data, total } = await servers[0].videos.list({ isLive: true })
231 expect(total).to.equal(1)
232 expect(data).to.have.lengthOf(1)
233 expect(data[0].name).to.equal('live')
236 it('Should not display lives', async function () {
237 const { data, total } = await servers[0].videos.list({ isLive: false })
239 expect(total).to.equal(1)
240 expect(data).to.have.lengthOf(1)
241 expect(data[0].name).to.equal('vod video')
244 it('Should display my lives', async function () {
247 await stopFfmpeg(ffmpegCommand)
248 await waitJobs(servers)
250 const { data } = await servers[0].videos.listMyVideos({ isLive: true })
252 const result = data.every(v => v.isLive)
253 expect(result).to.be.true
256 it('Should not display my lives', async function () {
257 const { data } = await servers[0].videos.listMyVideos({ isLive: false })
259 const result = data.every(v => !v.isLive)
260 expect(result).to.be.true
263 after(async function () {
264 await servers[0].videos.remove({ id: vodVideoId })
265 await servers[0].videos.remove({ id: liveVideoId })
269 describe('Stream checks', function () {
270 let liveVideo: LiveVideo & VideoDetails
274 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
277 async function createLiveWrapper () {
278 const liveAttributes = {
280 channelId: servers[0].store.channel.id,
281 privacy: VideoPrivacy.PUBLIC,
285 const { uuid } = await commands[0].create({ fields: liveAttributes })
287 const live = await commands[0].get({ videoId: uuid })
288 const video = await servers[0].videos.get({ id: uuid })
290 return Object.assign(video, live)
293 it('Should not allow a stream without the appropriate path', async function () {
296 liveVideo = await createLiveWrapper()
298 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
299 await testFfmpegStreamError(command, true)
302 it('Should not allow a stream without the appropriate stream key', async function () {
305 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
306 await testFfmpegStreamError(command, true)
309 it('Should succeed with the correct params', async function () {
312 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
313 await testFfmpegStreamError(command, false)
316 it('Should list this live now someone stream into it', async function () {
317 for (const server of servers) {
318 const { total, data } = await server.videos.list()
320 expect(total).to.equal(1)
321 expect(data).to.have.lengthOf(1)
323 const video = data[0]
324 expect(video.name).to.equal('user live')
325 expect(video.isLive).to.be.true
329 it('Should not allow a stream on a live that was blacklisted', async function () {
332 liveVideo = await createLiveWrapper()
334 await servers[0].blacklist.add({ videoId: liveVideo.uuid })
336 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
337 await testFfmpegStreamError(command, true)
340 it('Should not allow a stream on a live that was deleted', async function () {
343 liveVideo = await createLiveWrapper()
345 await servers[0].videos.remove({ id: liveVideo.uuid })
347 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
348 await testFfmpegStreamError(command, true)
352 describe('Live transcoding', function () {
353 let liveVideoId: string
355 async function createLiveWrapper (saveReplay: boolean) {
356 const liveAttributes = {
358 channelId: servers[0].store.channel.id,
359 privacy: VideoPrivacy.PUBLIC,
363 const { uuid } = await commands[0].create({ fields: liveAttributes })
367 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
368 for (const server of servers) {
369 const { data } = await server.videos.list()
370 expect(data.find(v => v.uuid === liveVideoId)).to.exist
372 const video = await server.videos.get({ id: liveVideoId })
374 expect(video.streamingPlaylists).to.have.lengthOf(1)
376 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
377 expect(hlsPlaylist).to.exist
379 // Only finite files are displayed
380 expect(hlsPlaylist.files).to.have.lengthOf(0)
382 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
384 for (let i = 0; i < resolutions.length; i++) {
386 const segmentName = `${i}-00000${segmentNum}.ts`
387 await commands[0].waitUntilSegmentGeneration({ videoUUID: video.uuid, resolution: i, segment: segmentNum })
389 const subPlaylist = await servers[0].streamingPlaylists.get({
390 url: `${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`
393 expect(subPlaylist).to.contain(segmentName)
395 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
396 await checkLiveSegmentHash({
398 baseUrlSegment: baseUrlAndPath,
399 videoUUID: video.uuid,
407 function updateConf (resolutions: number[]) {
408 return servers[0].config.updateCustomSubConfig({
417 '240p': resolutions.includes(240),
418 '360p': resolutions.includes(360),
419 '480p': resolutions.includes(480),
420 '720p': resolutions.includes(720),
421 '1080p': resolutions.includes(1080),
422 '2160p': resolutions.includes(2160)
430 before(async function () {
434 it('Should enable transcoding without additional resolutions', async function () {
437 liveVideoId = await createLiveWrapper(false)
439 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
440 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
441 await waitJobs(servers)
443 await testVideoResolutions(liveVideoId, [ 720 ])
445 await stopFfmpeg(ffmpegCommand)
448 it('Should enable transcoding with some resolutions', async function () {
451 const resolutions = [ 240, 480 ]
452 await updateConf(resolutions)
453 liveVideoId = await createLiveWrapper(false)
455 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
456 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
457 await waitJobs(servers)
459 await testVideoResolutions(liveVideoId, resolutions)
461 await stopFfmpeg(ffmpegCommand)
464 it('Should enable transcoding with some resolutions and correctly save them', async function () {
467 const resolutions = [ 240, 360, 720 ]
469 await updateConf(resolutions)
470 liveVideoId = await createLiveWrapper(true)
472 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
473 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
474 await waitJobs(servers)
476 await testVideoResolutions(liveVideoId, resolutions)
478 await stopFfmpeg(ffmpegCommand)
479 await commands[0].waitUntilEnded({ videoId: liveVideoId })
481 await waitJobs(servers)
483 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
485 const bitrateLimits = {
486 720: 5000 * 1000, // 60FPS
491 for (const server of servers) {
492 const video = await server.videos.get({ id: liveVideoId })
494 expect(video.state.id).to.equal(VideoState.PUBLISHED)
495 expect(video.duration).to.be.greaterThan(1)
496 expect(video.files).to.have.lengthOf(0)
498 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
499 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
500 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
502 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
504 for (const resolution of resolutions) {
505 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
507 expect(file).to.exist
508 expect(file.size).to.be.greaterThan(1)
510 if (resolution >= 720) {
511 expect(file.fps).to.be.approximately(60, 2)
513 expect(file.fps).to.be.approximately(30, 2)
516 const filename = `${video.uuid}-${resolution}-fragmented.mp4`
517 const segmentPath = servers[0].servers.buildDirectory(join('streaming-playlists', 'hls', video.uuid, filename))
519 const probe = await ffprobePromise(segmentPath)
520 const videoStream = await getVideoStreamFromFile(segmentPath, probe)
522 expect(probe.format.bit_rate).to.be.below(bitrateLimits[videoStream.height])
524 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
525 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
530 it('Should correctly have cleaned up the live files', async function () {
533 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
537 describe('After a server restart', function () {
538 let liveVideoId: string
539 let liveVideoReplayId: string
541 async function createLiveWrapper (saveReplay: boolean) {
542 const liveAttributes = {
544 channelId: servers[0].store.channel.id,
545 privacy: VideoPrivacy.PUBLIC,
549 const { uuid } = await commands[0].create({ fields: liveAttributes })
553 before(async function () {
556 liveVideoId = await createLiveWrapper(false)
557 liveVideoReplayId = await createLiveWrapper(true)
560 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId }),
561 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoReplayId })
565 commands[0].waitUntilPublished({ videoId: liveVideoId }),
566 commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
569 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoId, resolution: 0, segment: 2 })
570 await commands[0].waitUntilSegmentGeneration({ videoUUID: liveVideoReplayId, resolution: 0, segment: 2 })
572 await killallServers([ servers[0] ])
573 await servers[0].run()
578 it('Should cleanup lives', async function () {
581 await commands[0].waitUntilEnded({ videoId: liveVideoId })
584 it('Should save a live replay', async function () {
587 await commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
591 after(async function () {
592 await cleanupTests(servers)