]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/live/live.ts
Introduce live command
[github/Chocobozzz/PeerTube.git] / server / tests / api / live / live.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { join } from 'path'
6 import { ffprobePromise, getVideoStreamFromFile } from '@server/helpers/ffprobe-utils'
7 import { LiveVideo, LiveVideoCreate, Video, VideoDetails, VideoPrivacy, VideoState, VideoStreamingPlaylistType } from '@shared/models'
8 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
9 import {
10 addVideoToBlacklist,
11 buildServerDirectory,
12 checkLiveCleanup,
13 checkLiveSegmentHash,
14 checkResolutionsInMasterPlaylist,
15 cleanupTests,
16 doubleFollow,
17 flushAndRunMultipleServers,
18 getMyVideosWithFilter,
19 getPlaylist,
20 getVideo,
21 getVideosList,
22 getVideosWithFilters,
23 killallServers,
24 LiveCommand,
25 makeRawRequest,
26 removeVideo,
27 reRunServer,
28 sendRTMPStream,
29 ServerInfo,
30 setAccessTokensToServers,
31 setDefaultVideoChannel,
32 stopFfmpeg,
33 testFfmpegStreamError,
34 testImage,
35 uploadVideoAndGetId,
36 wait,
37 waitJobs,
38 waitUntilLivePublishedOnAllServers
39 } from '../../../../shared/extra-utils'
40
41 const expect = chai.expect
42
43 describe('Test live', function () {
44 let servers: ServerInfo[] = []
45 let commands: LiveCommand[]
46
47 before(async function () {
48 this.timeout(120000)
49
50 servers = await flushAndRunMultipleServers(2)
51
52 // Get the access tokens
53 await setAccessTokensToServers(servers)
54 await setDefaultVideoChannel(servers)
55
56 await servers[0].configCommand.updateCustomSubConfig({
57 newConfig: {
58 live: {
59 enabled: true,
60 allowReplay: true,
61 transcoding: {
62 enabled: false
63 }
64 }
65 }
66 })
67
68 // Server 1 and server 2 follow each other
69 await doubleFollow(servers[0], servers[1])
70
71 commands = servers.map(s => s.liveCommand)
72 })
73
74 describe('Live creation, update and delete', function () {
75 let liveVideoUUID: string
76
77 it('Should create a live with the appropriate parameters', async function () {
78 this.timeout(20000)
79
80 const attributes: LiveVideoCreate = {
81 category: 1,
82 licence: 2,
83 language: 'fr',
84 description: 'super live description',
85 support: 'support field',
86 channelId: servers[0].videoChannel.id,
87 nsfw: false,
88 waitTranscoding: false,
89 name: 'my super live',
90 tags: [ 'tag1', 'tag2' ],
91 commentsEnabled: false,
92 downloadEnabled: false,
93 saveReplay: true,
94 privacy: VideoPrivacy.PUBLIC,
95 previewfile: 'video_short1-preview.webm.jpg',
96 thumbnailfile: 'video_short1.webm.jpg'
97 }
98
99 const live = await commands[0].createLive({ fields: attributes })
100 liveVideoUUID = live.uuid
101
102 await waitJobs(servers)
103
104 for (const server of servers) {
105 const resVideo = await getVideo(server.url, liveVideoUUID)
106 const video: VideoDetails = resVideo.body
107
108 expect(video.category.id).to.equal(1)
109 expect(video.licence.id).to.equal(2)
110 expect(video.language.id).to.equal('fr')
111 expect(video.description).to.equal('super live description')
112 expect(video.support).to.equal('support field')
113
114 expect(video.channel.name).to.equal(servers[0].videoChannel.name)
115 expect(video.channel.host).to.equal(servers[0].videoChannel.host)
116
117 expect(video.isLive).to.be.true
118
119 expect(video.nsfw).to.be.false
120 expect(video.waitTranscoding).to.be.false
121 expect(video.name).to.equal('my super live')
122 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
123 expect(video.commentsEnabled).to.be.false
124 expect(video.downloadEnabled).to.be.false
125 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
126
127 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
128 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
129
130 const live = await server.liveCommand.getLive({ videoId: liveVideoUUID })
131
132 if (server.url === servers[0].url) {
133 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
134 expect(live.streamKey).to.not.be.empty
135 } else {
136 expect(live.rtmpUrl).to.be.null
137 expect(live.streamKey).to.be.null
138 }
139
140 expect(live.saveReplay).to.be.true
141 }
142 })
143
144 it('Should have a default preview and thumbnail', async function () {
145 this.timeout(20000)
146
147 const attributes: LiveVideoCreate = {
148 name: 'default live thumbnail',
149 channelId: servers[0].videoChannel.id,
150 privacy: VideoPrivacy.UNLISTED,
151 nsfw: true
152 }
153
154 const live = await commands[0].createLive({ fields: attributes })
155 const videoId = live.uuid
156
157 await waitJobs(servers)
158
159 for (const server of servers) {
160 const resVideo = await getVideo(server.url, videoId)
161 const video: VideoDetails = resVideo.body
162
163 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
164 expect(video.nsfw).to.be.true
165
166 await makeRawRequest(server.url + video.thumbnailPath, HttpStatusCode.OK_200)
167 await makeRawRequest(server.url + video.previewPath, HttpStatusCode.OK_200)
168 }
169 })
170
171 it('Should not have the live listed since nobody streams into', async function () {
172 for (const server of servers) {
173 const res = await getVideosList(server.url)
174
175 expect(res.body.total).to.equal(0)
176 expect(res.body.data).to.have.lengthOf(0)
177 }
178 })
179
180 it('Should not be able to update a live of another server', async function () {
181 await commands[1].updateLive({ videoId: liveVideoUUID, fields: { saveReplay: false }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
182 })
183
184 it('Should update the live', async function () {
185 this.timeout(10000)
186
187 await commands[0].updateLive({ videoId: liveVideoUUID, fields: { saveReplay: false } })
188 await waitJobs(servers)
189 })
190
191 it('Have the live updated', async function () {
192 for (const server of servers) {
193 const live = await server.liveCommand.getLive({ videoId: liveVideoUUID })
194
195 if (server.url === servers[0].url) {
196 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
197 expect(live.streamKey).to.not.be.empty
198 } else {
199 expect(live.rtmpUrl).to.be.null
200 expect(live.streamKey).to.be.null
201 }
202
203 expect(live.saveReplay).to.be.false
204 }
205 })
206
207 it('Delete the live', async function () {
208 this.timeout(10000)
209
210 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
211 await waitJobs(servers)
212 })
213
214 it('Should have the live deleted', async function () {
215 for (const server of servers) {
216 await getVideo(server.url, liveVideoUUID, HttpStatusCode.NOT_FOUND_404)
217 await server.liveCommand.getLive({ videoId: liveVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
218 }
219 })
220 })
221
222 describe('Live filters', function () {
223 let ffmpegCommand: any
224 let liveVideoId: string
225 let vodVideoId: string
226
227 before(async function () {
228 this.timeout(120000)
229
230 vodVideoId = (await uploadVideoAndGetId({ server: servers[0], videoName: 'vod video' })).uuid
231
232 const liveOptions = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: servers[0].videoChannel.id }
233 const live = await commands[0].createLive({ fields: liveOptions })
234 liveVideoId = live.uuid
235
236 ffmpegCommand = await servers[0].liveCommand.sendRTMPStreamInVideo({ videoId: liveVideoId })
237 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
238 await waitJobs(servers)
239 })
240
241 it('Should only display lives', async function () {
242 const res = await getVideosWithFilters(servers[0].url, { isLive: true })
243
244 expect(res.body.total).to.equal(1)
245 expect(res.body.data).to.have.lengthOf(1)
246 expect(res.body.data[0].name).to.equal('live')
247 })
248
249 it('Should not display lives', async function () {
250 const res = await getVideosWithFilters(servers[0].url, { isLive: false })
251
252 expect(res.body.total).to.equal(1)
253 expect(res.body.data).to.have.lengthOf(1)
254 expect(res.body.data[0].name).to.equal('vod video')
255 })
256
257 it('Should display my lives', async function () {
258 this.timeout(60000)
259
260 await stopFfmpeg(ffmpegCommand)
261 await waitJobs(servers)
262
263 const res = await getMyVideosWithFilter(servers[0].url, servers[0].accessToken, { isLive: true })
264 const videos = res.body.data as Video[]
265
266 const result = videos.every(v => v.isLive)
267 expect(result).to.be.true
268 })
269
270 it('Should not display my lives', async function () {
271 const res = await getMyVideosWithFilter(servers[0].url, servers[0].accessToken, { isLive: false })
272 const videos = res.body.data as Video[]
273
274 const result = videos.every(v => !v.isLive)
275 expect(result).to.be.true
276 })
277
278 after(async function () {
279 await removeVideo(servers[0].url, servers[0].accessToken, vodVideoId)
280 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoId)
281 })
282 })
283
284 describe('Stream checks', function () {
285 let liveVideo: LiveVideo & VideoDetails
286 let rtmpUrl: string
287
288 before(function () {
289 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
290 })
291
292 async function createLiveWrapper () {
293 const liveAttributes = {
294 name: 'user live',
295 channelId: servers[0].videoChannel.id,
296 privacy: VideoPrivacy.PUBLIC,
297 saveReplay: false
298 }
299
300 const { uuid } = await commands[0].createLive({ fields: liveAttributes })
301
302 const live = await commands[0].getLive({ videoId: uuid })
303 const resVideo = await getVideo(servers[0].url, uuid)
304
305 return Object.assign(resVideo.body as VideoDetails, live)
306 }
307
308 it('Should not allow a stream without the appropriate path', async function () {
309 this.timeout(60000)
310
311 liveVideo = await createLiveWrapper()
312
313 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
314 await testFfmpegStreamError(command, true)
315 })
316
317 it('Should not allow a stream without the appropriate stream key', async function () {
318 this.timeout(60000)
319
320 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
321 await testFfmpegStreamError(command, true)
322 })
323
324 it('Should succeed with the correct params', async function () {
325 this.timeout(60000)
326
327 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
328 await testFfmpegStreamError(command, false)
329 })
330
331 it('Should list this live now someone stream into it', async function () {
332 for (const server of servers) {
333 const res = await getVideosList(server.url)
334
335 expect(res.body.total).to.equal(1)
336 expect(res.body.data).to.have.lengthOf(1)
337
338 const video: Video = res.body.data[0]
339
340 expect(video.name).to.equal('user live')
341 expect(video.isLive).to.be.true
342 }
343 })
344
345 it('Should not allow a stream on a live that was blacklisted', async function () {
346 this.timeout(60000)
347
348 liveVideo = await createLiveWrapper()
349
350 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideo.uuid)
351
352 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
353 await testFfmpegStreamError(command, true)
354 })
355
356 it('Should not allow a stream on a live that was deleted', async function () {
357 this.timeout(60000)
358
359 liveVideo = await createLiveWrapper()
360
361 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid)
362
363 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
364 await testFfmpegStreamError(command, true)
365 })
366 })
367
368 describe('Live transcoding', function () {
369 let liveVideoId: string
370
371 async function createLiveWrapper (saveReplay: boolean) {
372 const liveAttributes = {
373 name: 'live video',
374 channelId: servers[0].videoChannel.id,
375 privacy: VideoPrivacy.PUBLIC,
376 saveReplay
377 }
378
379 const { uuid } = await commands[0].createLive({ fields: liveAttributes })
380 return uuid
381 }
382
383 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
384 for (const server of servers) {
385 const resList = await getVideosList(server.url)
386 const videos: Video[] = resList.body.data
387
388 expect(videos.find(v => v.uuid === liveVideoId)).to.exist
389
390 const resVideo = await getVideo(server.url, liveVideoId)
391 const video: VideoDetails = resVideo.body
392
393 expect(video.streamingPlaylists).to.have.lengthOf(1)
394
395 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
396 expect(hlsPlaylist).to.exist
397
398 // Only finite files are displayed
399 expect(hlsPlaylist.files).to.have.lengthOf(0)
400
401 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions)
402
403 for (let i = 0; i < resolutions.length; i++) {
404 const segmentNum = 3
405 const segmentName = `${i}-00000${segmentNum}.ts`
406 await commands[0].waitUntilLiveSegmentGeneration({ videoUUID: video.uuid, resolution: i, segment: segmentNum })
407
408 const res = await getPlaylist(`${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`)
409 const subPlaylist = res.text
410
411 expect(subPlaylist).to.contain(segmentName)
412
413 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
414 await checkLiveSegmentHash(baseUrlAndPath, video.uuid, segmentName, hlsPlaylist)
415 }
416 }
417 }
418
419 function updateConf (resolutions: number[]) {
420 return servers[0].configCommand.updateCustomSubConfig({
421 newConfig: {
422 live: {
423 enabled: true,
424 allowReplay: true,
425 maxDuration: -1,
426 transcoding: {
427 enabled: true,
428 resolutions: {
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)
435 }
436 }
437 }
438 }
439 })
440 }
441
442 before(async function () {
443 await updateConf([])
444 })
445
446 it('Should enable transcoding without additional resolutions', async function () {
447 this.timeout(60000)
448
449 liveVideoId = await createLiveWrapper(false)
450
451 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
452 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
453 await waitJobs(servers)
454
455 await testVideoResolutions(liveVideoId, [ 720 ])
456
457 await stopFfmpeg(ffmpegCommand)
458 })
459
460 it('Should enable transcoding with some resolutions', async function () {
461 this.timeout(60000)
462
463 const resolutions = [ 240, 480 ]
464 await updateConf(resolutions)
465 liveVideoId = await createLiveWrapper(false)
466
467 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId })
468 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
469 await waitJobs(servers)
470
471 await testVideoResolutions(liveVideoId, resolutions)
472
473 await stopFfmpeg(ffmpegCommand)
474 })
475
476 it('Should enable transcoding with some resolutions and correctly save them', async function () {
477 this.timeout(200000)
478
479 const resolutions = [ 240, 360, 720 ]
480
481 await updateConf(resolutions)
482 liveVideoId = await createLiveWrapper(true)
483
484 const ffmpegCommand = await commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
485 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
486 await waitJobs(servers)
487
488 await testVideoResolutions(liveVideoId, resolutions)
489
490 await stopFfmpeg(ffmpegCommand)
491 await commands[0].waitUntilLiveEnded({ videoId: liveVideoId })
492
493 await waitJobs(servers)
494
495 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
496
497 const bitrateLimits = {
498 720: 5000 * 1000, // 60FPS
499 360: 1100 * 1000,
500 240: 600 * 1000
501 }
502
503 for (const server of servers) {
504 const resVideo = await getVideo(server.url, liveVideoId)
505 const video: VideoDetails = resVideo.body
506
507 expect(video.state.id).to.equal(VideoState.PUBLISHED)
508 expect(video.duration).to.be.greaterThan(1)
509 expect(video.files).to.have.lengthOf(0)
510
511 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
512 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
513 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
514
515 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
516
517 for (const resolution of resolutions) {
518 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
519
520 expect(file).to.exist
521 expect(file.size).to.be.greaterThan(1)
522
523 if (resolution >= 720) {
524 expect(file.fps).to.be.approximately(60, 2)
525 } else {
526 expect(file.fps).to.be.approximately(30, 2)
527 }
528
529 const filename = `${video.uuid}-${resolution}-fragmented.mp4`
530 const segmentPath = buildServerDirectory(servers[0], join('streaming-playlists', 'hls', video.uuid, filename))
531
532 const probe = await ffprobePromise(segmentPath)
533 const videoStream = await getVideoStreamFromFile(segmentPath, probe)
534
535 expect(probe.format.bit_rate).to.be.below(bitrateLimits[videoStream.height])
536
537 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
538 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
539 }
540 }
541 })
542
543 it('Should correctly have cleaned up the live files', async function () {
544 this.timeout(30000)
545
546 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
547 })
548 })
549
550 describe('After a server restart', function () {
551 let liveVideoId: string
552 let liveVideoReplayId: string
553
554 async function createLiveWrapper (saveReplay: boolean) {
555 const liveAttributes = {
556 name: 'live video',
557 channelId: servers[0].videoChannel.id,
558 privacy: VideoPrivacy.PUBLIC,
559 saveReplay
560 }
561
562 const { uuid } = await commands[0].createLive({ fields: liveAttributes })
563 return uuid
564 }
565
566 before(async function () {
567 this.timeout(120000)
568
569 liveVideoId = await createLiveWrapper(false)
570 liveVideoReplayId = await createLiveWrapper(true)
571
572 await Promise.all([
573 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoId }),
574 commands[0].sendRTMPStreamInVideo({ videoId: liveVideoReplayId })
575 ])
576
577 await Promise.all([
578 commands[0].waitUntilLivePublished({ videoId: liveVideoId }),
579 commands[0].waitUntilLivePublished({ videoId: liveVideoReplayId })
580 ])
581
582 await commands[0].waitUntilLiveSegmentGeneration({ videoUUID: liveVideoId, resolution: 0, segment: 2 })
583 await commands[0].waitUntilLiveSegmentGeneration({ videoUUID: liveVideoReplayId, resolution: 0, segment: 2 })
584
585 await killallServers([ servers[0] ])
586 await reRunServer(servers[0])
587
588 await wait(5000)
589 })
590
591 it('Should cleanup lives', async function () {
592 this.timeout(60000)
593
594 await commands[0].waitUntilLiveEnded({ videoId: liveVideoId })
595 })
596
597 it('Should save a live replay', async function () {
598 this.timeout(120000)
599
600 await commands[0].waitUntilLivePublished({ videoId: liveVideoReplayId })
601 })
602 })
603
604 after(async function () {
605 await cleanupTests(servers)
606 })
607 })