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