]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/live/live.ts
Regenerate miniature on live save
[github/Chocobozzz/PeerTube.git] / server / tests / api / live / live.ts
CommitLineData
af4ae64f
C
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import 'mocha'
4import * as chai from 'chai'
bd54ad19 5import { getLiveNotificationSocket } from '@shared/extra-utils/socket/socket-io'
053aed43 6import { LiveVideo, LiveVideoCreate, Video, VideoDetails, VideoPrivacy, VideoState, VideoStreamingPlaylistType } from '@shared/models'
af4ae64f 7import {
68e70a74 8 addVideoToBlacklist,
bd54ad19
C
9 checkLiveCleanup,
10 checkResolutionsInMasterPlaylist,
af4ae64f
C
11 cleanupTests,
12 createLive,
13 doubleFollow,
14 flushAndRunMultipleServers,
15 getLive,
16 getVideo,
bd54ad19 17 getVideoIdFromUUID,
af4ae64f
C
18 getVideosList,
19 makeRawRequest,
20 removeVideo,
68e70a74 21 sendRTMPStream,
bd54ad19 22 sendRTMPStreamInVideo,
af4ae64f
C
23 ServerInfo,
24 setAccessTokensToServers,
25 setDefaultVideoChannel,
bd54ad19 26 stopFfmpeg,
97969c4e 27 testFfmpegStreamError,
af4ae64f
C
28 testImage,
29 updateCustomSubConfig,
30 updateLive,
bd54ad19
C
31 waitJobs,
32 waitUntilLiveStarts
af4ae64f
C
33} from '../../../../shared/extra-utils'
34
35const expect = chai.expect
36
37describe('Test live', function () {
38 let servers: ServerInfo[] = []
af4ae64f
C
39
40 before(async function () {
41 this.timeout(120000)
42
43 servers = await flushAndRunMultipleServers(2)
44
45 // Get the access tokens
46 await setAccessTokensToServers(servers)
47 await setDefaultVideoChannel(servers)
48
49 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
50 live: {
51 enabled: true,
68e70a74
C
52 allowReplay: true,
53 transcoding: {
54 enabled: false
55 }
af4ae64f
C
56 }
57 })
58
59 // Server 1 and server 2 follow each other
60 await doubleFollow(servers[0], servers[1])
61 })
62
63 describe('Live creation, update and delete', function () {
68e70a74 64 let liveVideoUUID: string
af4ae64f
C
65
66 it('Should create a live with the appropriate parameters', async function () {
67 this.timeout(20000)
68
69 const attributes: LiveVideoCreate = {
70 category: 1,
71 licence: 2,
72 language: 'fr',
73 description: 'super live description',
74 support: 'support field',
75 channelId: servers[0].videoChannel.id,
76 nsfw: false,
77 waitTranscoding: false,
78 name: 'my super live',
79 tags: [ 'tag1', 'tag2' ],
80 commentsEnabled: false,
81 downloadEnabled: false,
82 saveReplay: true,
83 privacy: VideoPrivacy.PUBLIC,
84 previewfile: 'video_short1-preview.webm.jpg',
85 thumbnailfile: 'video_short1.webm.jpg'
86 }
87
88 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
89 liveVideoUUID = res.body.video.uuid
90
91 await waitJobs(servers)
92
93 for (const server of servers) {
94 const resVideo = await getVideo(server.url, liveVideoUUID)
95 const video: VideoDetails = resVideo.body
96
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')
102
103 expect(video.channel.name).to.equal(servers[0].videoChannel.name)
104 expect(video.channel.host).to.equal(servers[0].videoChannel.host)
105
106 expect(video.nsfw).to.be.false
107 expect(video.waitTranscoding).to.be.false
108 expect(video.name).to.equal('my super live')
109 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
110 expect(video.commentsEnabled).to.be.false
111 expect(video.downloadEnabled).to.be.false
112 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
113
114 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
115 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
116
117 const resLive = await getLive(server.url, server.accessToken, liveVideoUUID)
118 const live: LiveVideo = resLive.body
119
120 if (server.url === servers[0].url) {
121 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':1936/live')
122 expect(live.streamKey).to.not.be.empty
123 } else {
124 expect(live.rtmpUrl).to.be.null
125 expect(live.streamKey).to.be.null
126 }
127
128 expect(live.saveReplay).to.be.true
129 }
130 })
131
132 it('Should have a default preview and thumbnail', async function () {
133 this.timeout(20000)
134
135 const attributes: LiveVideoCreate = {
136 name: 'default live thumbnail',
137 channelId: servers[0].videoChannel.id,
138 privacy: VideoPrivacy.UNLISTED,
139 nsfw: true
140 }
141
142 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
143 const videoId = res.body.video.uuid
144
145 await waitJobs(servers)
146
147 for (const server of servers) {
148 const resVideo = await getVideo(server.url, videoId)
149 const video: VideoDetails = resVideo.body
150
151 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
152 expect(video.nsfw).to.be.true
153
154 await makeRawRequest(server.url + video.thumbnailPath, 200)
155 await makeRawRequest(server.url + video.previewPath, 200)
156 }
157 })
158
159 it('Should not have the live listed since nobody streams into', async function () {
160 for (const server of servers) {
161 const res = await getVideosList(server.url)
162
163 expect(res.body.total).to.equal(0)
164 expect(res.body.data).to.have.lengthOf(0)
165 }
166 })
167
168 it('Should not be able to update a live of another server', async function () {
169 await updateLive(servers[1].url, servers[1].accessToken, liveVideoUUID, { saveReplay: false }, 403)
170 })
171
172 it('Should update the live', async function () {
173 this.timeout(10000)
174
175 await updateLive(servers[0].url, servers[0].accessToken, liveVideoUUID, { saveReplay: false })
176 await waitJobs(servers)
177 })
178
179 it('Have the live updated', async function () {
180 for (const server of servers) {
181 const res = await getLive(server.url, server.accessToken, liveVideoUUID)
182 const live: LiveVideo = res.body
183
184 if (server.url === servers[0].url) {
185 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':1936/live')
186 expect(live.streamKey).to.not.be.empty
187 } else {
188 expect(live.rtmpUrl).to.be.null
189 expect(live.streamKey).to.be.null
190 }
191
192 expect(live.saveReplay).to.be.false
193 }
194 })
195
196 it('Delete the live', async function () {
197 this.timeout(10000)
198
199 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
200 await waitJobs(servers)
201 })
202
203 it('Should have the live deleted', async function () {
204 for (const server of servers) {
205 await getVideo(server.url, liveVideoUUID, 404)
206 await getLive(server.url, server.accessToken, liveVideoUUID, 404)
207 }
208 })
209 })
210
68e70a74
C
211 describe('Stream checks', function () {
212 let liveVideo: LiveVideo & VideoDetails
213 let rtmpUrl: string
214
215 before(function () {
216 rtmpUrl = 'rtmp://' + servers[0].hostname + ':1936'
217 })
af4ae64f 218
68e70a74 219 async function createLiveWrapper () {
97969c4e
C
220 const liveAttributes = {
221 name: 'user live',
bd54ad19 222 channelId: servers[0].videoChannel.id,
97969c4e 223 privacy: VideoPrivacy.PUBLIC,
68e70a74 224 saveReplay: false
97969c4e
C
225 }
226
bd54ad19 227 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
68e70a74 228 const uuid = res.body.video.uuid
97969c4e 229
68e70a74
C
230 const resLive = await getLive(servers[0].url, servers[0].accessToken, uuid)
231 const resVideo = await getVideo(servers[0].url, uuid)
97969c4e 232
68e70a74
C
233 return Object.assign(resVideo.body, resLive.body) as LiveVideo & VideoDetails
234 }
97969c4e 235
68e70a74 236 it('Should not allow a stream without the appropriate path', async function () {
97969c4e
C
237 this.timeout(30000)
238
68e70a74 239 liveVideo = await createLiveWrapper()
97969c4e 240
68e70a74
C
241 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
242 await testFfmpegStreamError(command, true)
af4ae64f
C
243 })
244
68e70a74 245 it('Should not allow a stream without the appropriate stream key', async function () {
97969c4e
C
246 this.timeout(30000)
247
68e70a74
C
248 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
249 await testFfmpegStreamError(command, true)
97969c4e
C
250 })
251
68e70a74 252 it('Should succeed with the correct params', async function () {
97969c4e
C
253 this.timeout(30000)
254
68e70a74
C
255 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
256 await testFfmpegStreamError(command, false)
af4ae64f
C
257 })
258
68e70a74 259 it('Should not allow a stream on a live that was blacklisted', async function () {
97969c4e
C
260 this.timeout(30000)
261
68e70a74 262 liveVideo = await createLiveWrapper()
af4ae64f 263
68e70a74 264 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideo.uuid)
af4ae64f 265
68e70a74
C
266 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
267 await testFfmpegStreamError(command, true)
af4ae64f
C
268 })
269
68e70a74
C
270 it('Should not allow a stream on a live that was deleted', async function () {
271 this.timeout(30000)
af4ae64f 272
68e70a74 273 liveVideo = await createLiveWrapper()
af4ae64f 274
68e70a74 275 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid)
af4ae64f 276
68e70a74
C
277 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
278 await testFfmpegStreamError(command, true)
af4ae64f
C
279 })
280 })
281
282 describe('Live transcoding', function () {
bd54ad19
C
283 let liveVideoId: string
284
285 async function createLiveWrapper (saveReplay: boolean) {
286 const liveAttributes = {
287 name: 'live video',
288 channelId: servers[0].videoChannel.id,
289 privacy: VideoPrivacy.PUBLIC,
290 saveReplay
291 }
292
293 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
294 return res.body.video.uuid
295 }
296
297 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
298 for (const server of servers) {
299 const resList = await getVideosList(server.url)
300 const videos: Video[] = resList.body.data
301
302 expect(videos.find(v => v.uuid === liveVideoId)).to.exist
303
304 const resVideo = await getVideo(server.url, liveVideoId)
305 const video: VideoDetails = resVideo.body
306
307 expect(video.streamingPlaylists).to.have.lengthOf(1)
308
309 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
310 expect(hlsPlaylist).to.exist
311
312 // Only finite files are displayed
313 expect(hlsPlaylist.files).to.have.lengthOf(0)
314
315 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions)
316 }
317 }
318
319 function updateConf (resolutions: number[]) {
320 return updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
321 live: {
322 enabled: true,
323 allowReplay: true,
324 maxDuration: null,
325 transcoding: {
326 enabled: true,
327 resolutions: {
328 '240p': resolutions.includes(240),
329 '360p': resolutions.includes(360),
330 '480p': resolutions.includes(480),
331 '720p': resolutions.includes(720),
332 '1080p': resolutions.includes(1080),
333 '2160p': resolutions.includes(2160)
334 }
335 }
336 }
337 })
338 }
339
340 before(async function () {
341 await updateConf([])
342 })
af4ae64f
C
343
344 it('Should enable transcoding without additional resolutions', async function () {
bd54ad19
C
345 this.timeout(30000)
346
347 liveVideoId = await createLiveWrapper(false)
348
349 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
350 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
351 await waitJobs(servers)
af4ae64f 352
bd54ad19
C
353 await testVideoResolutions(liveVideoId, [ 720 ])
354
355 await stopFfmpeg(command)
af4ae64f
C
356 })
357
358 it('Should enable transcoding with some resolutions', async function () {
bd54ad19
C
359 this.timeout(30000)
360
361 const resolutions = [ 240, 480 ]
362 await updateConf(resolutions)
363 liveVideoId = await createLiveWrapper(false)
364
365 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
366 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
367 await waitJobs(servers)
368
369 await testVideoResolutions(liveVideoId, resolutions)
370
371 await stopFfmpeg(command)
af4ae64f
C
372 })
373
374 it('Should enable transcoding with some resolutions and correctly save them', async function () {
bd54ad19
C
375 this.timeout(60000)
376
377 const resolutions = [ 240, 360, 720 ]
378 await updateConf(resolutions)
379 liveVideoId = await createLiveWrapper(true)
380
381 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
382 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
383 await waitJobs(servers)
384
385 await testVideoResolutions(liveVideoId, resolutions)
386
387 await stopFfmpeg(command)
388
389 await waitJobs(servers)
390
391 for (const server of servers) {
392 const resVideo = await getVideo(server.url, liveVideoId)
393 const video: VideoDetails = resVideo.body
394
395 expect(video.duration).to.be.greaterThan(1)
396 expect(video.files).to.have.lengthOf(0)
397
398 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
399
400 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
401
402 for (const resolution of resolutions) {
403 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
404
405 expect(file).to.exist
406 expect(file.fps).to.equal(25)
407 expect(file.size).to.be.greaterThan(1)
408
409 await makeRawRequest(file.torrentUrl, 200)
410 await makeRawRequest(file.fileUrl, 200)
411 }
412 }
af4ae64f
C
413 })
414
415 it('Should correctly have cleaned up the live files', async function () {
bd54ad19
C
416 this.timeout(30000)
417
418 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
af4ae64f
C
419 })
420 })
421
422 describe('Live socket messages', function () {
423
bd54ad19
C
424 async function createLiveWrapper () {
425 const liveAttributes = {
426 name: 'live video',
427 channelId: servers[0].videoChannel.id,
428 privacy: VideoPrivacy.PUBLIC
429 }
430
431 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
432 return res.body.video.uuid
433 }
434
435 it('Should correctly send a message when the live starts and ends', async function () {
436 this.timeout(60000)
437
438 const localStateChanges: VideoState[] = []
439 const remoteStateChanges: VideoState[] = []
440
441 const liveVideoUUID = await createLiveWrapper()
442 await waitJobs(servers)
443
444 {
445 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
446
447 const localSocket = getLiveNotificationSocket(servers[0].url)
448 localSocket.on('state-change', data => localStateChanges.push(data.state))
449 localSocket.emit('subscribe', { videoId })
450 }
451
452 {
453 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
454
455 const remoteSocket = getLiveNotificationSocket(servers[1].url)
456 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
457 remoteSocket.emit('subscribe', { videoId })
458 }
459
460 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
461 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoUUID)
462 await waitJobs(servers)
463
464 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
465 expect(stateChanges).to.have.lengthOf(1)
466 expect(stateChanges[0]).to.equal(VideoState.PUBLISHED)
467 }
468
469 await stopFfmpeg(command)
470 await waitJobs(servers)
471
472 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
473 expect(stateChanges).to.have.lengthOf(2)
474 expect(stateChanges[1]).to.equal(VideoState.LIVE_ENDED)
475 }
af4ae64f
C
476 })
477
bd54ad19
C
478 it('Should not receive a notification after unsubscribe', async function () {
479 this.timeout(60000)
480
481 const stateChanges: VideoState[] = []
482
483 const liveVideoUUID = await createLiveWrapper()
484 await waitJobs(servers)
485
486 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
487
488 const socket = getLiveNotificationSocket(servers[0].url)
489 socket.on('state-change', data => stateChanges.push(data.state))
490 socket.emit('subscribe', { videoId })
491
492 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
493 await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoUUID)
494 await waitJobs(servers)
495
496 expect(stateChanges).to.have.lengthOf(1)
497 socket.emit('unsubscribe', { videoId })
498
499 await stopFfmpeg(command)
500 await waitJobs(servers)
501
502 expect(stateChanges).to.have.lengthOf(1)
af4ae64f
C
503 })
504 })
505
506 after(async function () {
507 await cleanupTests(servers)
508 })
509})