]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/live/live.ts
Increase timeouts
[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'
5c0904fc 5import { FfmpegCommand } from 'fluent-ffmpeg'
ca5c612b
C
6import { join } from 'path'
7import { ffprobePromise, getVideoStreamFromFile } from '@server/helpers/ffprobe-utils'
bd54ad19 8import { getLiveNotificationSocket } from '@shared/extra-utils/socket/socket-io'
053aed43 9import { LiveVideo, LiveVideoCreate, Video, VideoDetails, VideoPrivacy, VideoState, VideoStreamingPlaylistType } from '@shared/models'
0d8de275 10import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
af4ae64f 11import {
68e70a74 12 addVideoToBlacklist,
ca5c612b 13 buildServerDirectory,
bd54ad19 14 checkLiveCleanup,
5c0904fc 15 checkLiveSegmentHash,
bd54ad19 16 checkResolutionsInMasterPlaylist,
af4ae64f
C
17 cleanupTests,
18 createLive,
19 doubleFollow,
20 flushAndRunMultipleServers,
21 getLive,
5c0904fc 22 getPlaylist,
af4ae64f 23 getVideo,
bd54ad19 24 getVideoIdFromUUID,
af4ae64f 25 getVideosList,
5c0904fc 26 killallServers,
af4ae64f
C
27 makeRawRequest,
28 removeVideo,
5c0904fc 29 reRunServer,
68e70a74 30 sendRTMPStream,
bd54ad19 31 sendRTMPStreamInVideo,
af4ae64f
C
32 ServerInfo,
33 setAccessTokensToServers,
34 setDefaultVideoChannel,
bd54ad19 35 stopFfmpeg,
97969c4e 36 testFfmpegStreamError,
af4ae64f
C
37 testImage,
38 updateCustomSubConfig,
39 updateLive,
e4bf7856
C
40 viewVideo,
41 wait,
bd54ad19 42 waitJobs,
0e856b78 43 waitUntilLiveEnded,
6b67897e 44 waitUntilLivePublished,
5cac83a7 45 waitUntilLiveSegmentGeneration
af4ae64f
C
46} from '../../../../shared/extra-utils'
47
48const expect = chai.expect
49
50describe('Test live', function () {
51 let servers: ServerInfo[] = []
af4ae64f 52
fae6e4da
C
53 async function waitUntilLivePublishedOnAllServers (videoId: string) {
54 for (const server of servers) {
55 await waitUntilLivePublished(server.url, server.accessToken, videoId)
56 }
57 }
58
af4ae64f
C
59 before(async function () {
60 this.timeout(120000)
61
62 servers = await flushAndRunMultipleServers(2)
63
64 // Get the access tokens
65 await setAccessTokensToServers(servers)
66 await setDefaultVideoChannel(servers)
67
68 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
69 live: {
70 enabled: true,
68e70a74
C
71 allowReplay: true,
72 transcoding: {
73 enabled: false
74 }
af4ae64f
C
75 }
76 })
77
78 // Server 1 and server 2 follow each other
79 await doubleFollow(servers[0], servers[1])
80 })
81
82 describe('Live creation, update and delete', function () {
68e70a74 83 let liveVideoUUID: string
af4ae64f
C
84
85 it('Should create a live with the appropriate parameters', async function () {
86 this.timeout(20000)
87
88 const attributes: LiveVideoCreate = {
89 category: 1,
90 licence: 2,
91 language: 'fr',
92 description: 'super live description',
93 support: 'support field',
94 channelId: servers[0].videoChannel.id,
95 nsfw: false,
96 waitTranscoding: false,
97 name: 'my super live',
98 tags: [ 'tag1', 'tag2' ],
99 commentsEnabled: false,
100 downloadEnabled: false,
101 saveReplay: true,
102 privacy: VideoPrivacy.PUBLIC,
103 previewfile: 'video_short1-preview.webm.jpg',
104 thumbnailfile: 'video_short1.webm.jpg'
105 }
106
107 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
108 liveVideoUUID = res.body.video.uuid
109
110 await waitJobs(servers)
111
112 for (const server of servers) {
113 const resVideo = await getVideo(server.url, liveVideoUUID)
114 const video: VideoDetails = resVideo.body
115
116 expect(video.category.id).to.equal(1)
117 expect(video.licence.id).to.equal(2)
118 expect(video.language.id).to.equal('fr')
119 expect(video.description).to.equal('super live description')
120 expect(video.support).to.equal('support field')
121
122 expect(video.channel.name).to.equal(servers[0].videoChannel.name)
123 expect(video.channel.host).to.equal(servers[0].videoChannel.host)
124
1ab60243
C
125 expect(video.isLive).to.be.true
126
af4ae64f
C
127 expect(video.nsfw).to.be.false
128 expect(video.waitTranscoding).to.be.false
129 expect(video.name).to.equal('my super live')
130 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
131 expect(video.commentsEnabled).to.be.false
132 expect(video.downloadEnabled).to.be.false
133 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
134
135 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
136 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
137
138 const resLive = await getLive(server.url, server.accessToken, liveVideoUUID)
139 const live: LiveVideo = resLive.body
140
141 if (server.url === servers[0].url) {
c655c9ef 142 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
af4ae64f
C
143 expect(live.streamKey).to.not.be.empty
144 } else {
145 expect(live.rtmpUrl).to.be.null
146 expect(live.streamKey).to.be.null
147 }
148
149 expect(live.saveReplay).to.be.true
150 }
151 })
152
153 it('Should have a default preview and thumbnail', async function () {
154 this.timeout(20000)
155
156 const attributes: LiveVideoCreate = {
157 name: 'default live thumbnail',
158 channelId: servers[0].videoChannel.id,
159 privacy: VideoPrivacy.UNLISTED,
160 nsfw: true
161 }
162
163 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
164 const videoId = res.body.video.uuid
165
166 await waitJobs(servers)
167
168 for (const server of servers) {
169 const resVideo = await getVideo(server.url, videoId)
170 const video: VideoDetails = resVideo.body
171
172 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
173 expect(video.nsfw).to.be.true
174
f2eb23cd
RK
175 await makeRawRequest(server.url + video.thumbnailPath, HttpStatusCode.OK_200)
176 await makeRawRequest(server.url + video.previewPath, HttpStatusCode.OK_200)
af4ae64f
C
177 }
178 })
179
180 it('Should not have the live listed since nobody streams into', async function () {
181 for (const server of servers) {
182 const res = await getVideosList(server.url)
183
184 expect(res.body.total).to.equal(0)
185 expect(res.body.data).to.have.lengthOf(0)
186 }
187 })
188
189 it('Should not be able to update a live of another server', async function () {
f2eb23cd 190 await updateLive(servers[1].url, servers[1].accessToken, liveVideoUUID, { saveReplay: false }, HttpStatusCode.FORBIDDEN_403)
af4ae64f
C
191 })
192
193 it('Should update the live', async function () {
194 this.timeout(10000)
195
196 await updateLive(servers[0].url, servers[0].accessToken, liveVideoUUID, { saveReplay: false })
197 await waitJobs(servers)
198 })
199
200 it('Have the live updated', async function () {
201 for (const server of servers) {
202 const res = await getLive(server.url, server.accessToken, liveVideoUUID)
203 const live: LiveVideo = res.body
204
205 if (server.url === servers[0].url) {
c655c9ef 206 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
af4ae64f
C
207 expect(live.streamKey).to.not.be.empty
208 } else {
209 expect(live.rtmpUrl).to.be.null
210 expect(live.streamKey).to.be.null
211 }
212
213 expect(live.saveReplay).to.be.false
214 }
215 })
216
217 it('Delete the live', async function () {
218 this.timeout(10000)
219
220 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
221 await waitJobs(servers)
222 })
223
224 it('Should have the live deleted', async function () {
225 for (const server of servers) {
f2eb23cd
RK
226 await getVideo(server.url, liveVideoUUID, HttpStatusCode.NOT_FOUND_404)
227 await getLive(server.url, server.accessToken, liveVideoUUID, HttpStatusCode.NOT_FOUND_404)
af4ae64f
C
228 }
229 })
230 })
231
68e70a74
C
232 describe('Stream checks', function () {
233 let liveVideo: LiveVideo & VideoDetails
234 let rtmpUrl: string
235
236 before(function () {
c655c9ef 237 rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
68e70a74 238 })
af4ae64f 239
68e70a74 240 async function createLiveWrapper () {
97969c4e
C
241 const liveAttributes = {
242 name: 'user live',
bd54ad19 243 channelId: servers[0].videoChannel.id,
97969c4e 244 privacy: VideoPrivacy.PUBLIC,
68e70a74 245 saveReplay: false
97969c4e
C
246 }
247
bd54ad19 248 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
68e70a74 249 const uuid = res.body.video.uuid
97969c4e 250
68e70a74
C
251 const resLive = await getLive(servers[0].url, servers[0].accessToken, uuid)
252 const resVideo = await getVideo(servers[0].url, uuid)
97969c4e 253
68e70a74
C
254 return Object.assign(resVideo.body, resLive.body) as LiveVideo & VideoDetails
255 }
97969c4e 256
68e70a74 257 it('Should not allow a stream without the appropriate path', async function () {
97969c4e
C
258 this.timeout(30000)
259
68e70a74 260 liveVideo = await createLiveWrapper()
97969c4e 261
68e70a74
C
262 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
263 await testFfmpegStreamError(command, true)
af4ae64f
C
264 })
265
68e70a74 266 it('Should not allow a stream without the appropriate stream key', async function () {
97969c4e
C
267 this.timeout(30000)
268
68e70a74
C
269 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
270 await testFfmpegStreamError(command, true)
97969c4e
C
271 })
272
68e70a74 273 it('Should succeed with the correct params', async function () {
97969c4e
C
274 this.timeout(30000)
275
68e70a74
C
276 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
277 await testFfmpegStreamError(command, false)
af4ae64f
C
278 })
279
1ab60243
C
280 it('Should list this live now someone stream into it', async function () {
281 for (const server of servers) {
282 const res = await getVideosList(server.url)
283
284 expect(res.body.total).to.equal(1)
285 expect(res.body.data).to.have.lengthOf(1)
286
287 const video: Video = res.body.data[0]
288
289 expect(video.name).to.equal('user live')
290 expect(video.isLive).to.be.true
291 }
292 })
293
68e70a74 294 it('Should not allow a stream on a live that was blacklisted', async function () {
97969c4e
C
295 this.timeout(30000)
296
68e70a74 297 liveVideo = await createLiveWrapper()
af4ae64f 298
68e70a74 299 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideo.uuid)
af4ae64f 300
68e70a74
C
301 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
302 await testFfmpegStreamError(command, true)
af4ae64f
C
303 })
304
68e70a74
C
305 it('Should not allow a stream on a live that was deleted', async function () {
306 this.timeout(30000)
af4ae64f 307
68e70a74 308 liveVideo = await createLiveWrapper()
af4ae64f 309
68e70a74 310 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid)
af4ae64f 311
68e70a74
C
312 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
313 await testFfmpegStreamError(command, true)
af4ae64f
C
314 })
315 })
316
317 describe('Live transcoding', function () {
bd54ad19
C
318 let liveVideoId: string
319
320 async function createLiveWrapper (saveReplay: boolean) {
321 const liveAttributes = {
322 name: 'live video',
323 channelId: servers[0].videoChannel.id,
324 privacy: VideoPrivacy.PUBLIC,
325 saveReplay
326 }
327
328 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
329 return res.body.video.uuid
330 }
331
332 async function testVideoResolutions (liveVideoId: string, resolutions: number[]) {
333 for (const server of servers) {
334 const resList = await getVideosList(server.url)
335 const videos: Video[] = resList.body.data
336
337 expect(videos.find(v => v.uuid === liveVideoId)).to.exist
338
339 const resVideo = await getVideo(server.url, liveVideoId)
340 const video: VideoDetails = resVideo.body
341
342 expect(video.streamingPlaylists).to.have.lengthOf(1)
343
344 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
345 expect(hlsPlaylist).to.exist
346
347 // Only finite files are displayed
348 expect(hlsPlaylist.files).to.have.lengthOf(0)
349
350 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions)
5c0904fc
C
351
352 for (let i = 0; i < resolutions.length; i++) {
a800dbf3 353 const segmentNum = 3
0d8de275
C
354 const segmentName = `${i}-00000${segmentNum}.ts`
355 await waitUntilLiveSegmentGeneration(servers[0], video.uuid, i, segmentNum)
5c0904fc
C
356
357 const res = await getPlaylist(`${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`)
358 const subPlaylist = res.text
359
360 expect(subPlaylist).to.contain(segmentName)
361
362 const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
363 await checkLiveSegmentHash(baseUrlAndPath, video.uuid, segmentName, hlsPlaylist)
364 }
bd54ad19
C
365 }
366 }
367
368 function updateConf (resolutions: number[]) {
369 return updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
370 live: {
371 enabled: true,
372 allowReplay: true,
c9bc850e 373 maxDuration: -1,
bd54ad19
C
374 transcoding: {
375 enabled: true,
376 resolutions: {
377 '240p': resolutions.includes(240),
378 '360p': resolutions.includes(360),
379 '480p': resolutions.includes(480),
380 '720p': resolutions.includes(720),
381 '1080p': resolutions.includes(1080),
382 '2160p': resolutions.includes(2160)
383 }
384 }
385 }
386 })
387 }
388
389 before(async function () {
390 await updateConf([])
391 })
af4ae64f
C
392
393 it('Should enable transcoding without additional resolutions', async function () {
dbb15e37 394 this.timeout(60000)
bd54ad19
C
395
396 liveVideoId = await createLiveWrapper(false)
397
398 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
fae6e4da 399 await waitUntilLivePublishedOnAllServers(liveVideoId)
bd54ad19 400 await waitJobs(servers)
af4ae64f 401
bd54ad19
C
402 await testVideoResolutions(liveVideoId, [ 720 ])
403
404 await stopFfmpeg(command)
af4ae64f
C
405 })
406
407 it('Should enable transcoding with some resolutions', async function () {
dbb15e37 408 this.timeout(60000)
bd54ad19
C
409
410 const resolutions = [ 240, 480 ]
411 await updateConf(resolutions)
412 liveVideoId = await createLiveWrapper(false)
413
414 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
fae6e4da 415 await waitUntilLivePublishedOnAllServers(liveVideoId)
bd54ad19
C
416 await waitJobs(servers)
417
418 await testVideoResolutions(liveVideoId, resolutions)
419
420 await stopFfmpeg(command)
af4ae64f
C
421 })
422
423 it('Should enable transcoding with some resolutions and correctly save them', async function () {
f42c2152 424 this.timeout(200000)
bd54ad19
C
425
426 const resolutions = [ 240, 360, 720 ]
ca5c612b 427
bd54ad19
C
428 await updateConf(resolutions)
429 liveVideoId = await createLiveWrapper(true)
430
ca5c612b 431 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId, 'video_short2.webm')
fae6e4da 432 await waitUntilLivePublishedOnAllServers(liveVideoId)
bd54ad19
C
433 await waitJobs(servers)
434
435 await testVideoResolutions(liveVideoId, resolutions)
436
437 await stopFfmpeg(command)
34caef7f
C
438 await waitUntilLiveEnded(servers[0].url, servers[0].accessToken, liveVideoId)
439
bd54ad19
C
440 await waitJobs(servers)
441
fae6e4da 442 await waitUntilLivePublishedOnAllServers(liveVideoId)
e0783718 443
ca5c612b 444 const bitrateLimits = {
4ef9ea48 445 720: 5000 * 1000, // 60FPS
6b67897e
C
446 360: 1100 * 1000,
447 240: 600 * 1000
ca5c612b
C
448 }
449
bd54ad19
C
450 for (const server of servers) {
451 const resVideo = await getVideo(server.url, liveVideoId)
452 const video: VideoDetails = resVideo.body
453
e0783718 454 expect(video.state.id).to.equal(VideoState.PUBLISHED)
bd54ad19
C
455 expect(video.duration).to.be.greaterThan(1)
456 expect(video.files).to.have.lengthOf(0)
457
458 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
f2eb23cd
RK
459 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
460 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
bd54ad19
C
461
462 expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length)
463
464 for (const resolution of resolutions) {
465 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
466
467 expect(file).to.exist
bd54ad19
C
468 expect(file.size).to.be.greaterThan(1)
469
884d2c39
C
470 if (resolution >= 720) {
471 expect(file.fps).to.be.approximately(60, 2)
472 } else {
473 expect(file.fps).to.be.approximately(30, 2)
474 }
475
ca5c612b
C
476 const filename = `${video.uuid}-${resolution}-fragmented.mp4`
477 const segmentPath = buildServerDirectory(servers[0], join('streaming-playlists', 'hls', video.uuid, filename))
478
479 const probe = await ffprobePromise(segmentPath)
480 const videoStream = await getVideoStreamFromFile(segmentPath, probe)
6b67897e 481
ca5c612b
C
482 expect(probe.format.bit_rate).to.be.below(bitrateLimits[videoStream.height])
483
f2eb23cd
RK
484 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
485 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
bd54ad19
C
486 }
487 }
af4ae64f
C
488 })
489
490 it('Should correctly have cleaned up the live files', async function () {
bd54ad19
C
491 this.timeout(30000)
492
493 await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ])
af4ae64f
C
494 })
495 })
496
e4bf7856
C
497 describe('Live views', function () {
498 let liveVideoId: string
499 let command: FfmpegCommand
500
501 async function countViews (expected: number) {
502 for (const server of servers) {
503 const res = await getVideo(server.url, liveVideoId)
504 const video: VideoDetails = res.body
505
506 expect(video.views).to.equal(expected)
507 }
508 }
509
510 before(async function () {
511 this.timeout(30000)
512
513 const liveAttributes = {
514 name: 'live video',
515 channelId: servers[0].videoChannel.id,
516 privacy: VideoPrivacy.PUBLIC
517 }
518
519 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
520 liveVideoId = res.body.video.uuid
521
522 command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
fae6e4da 523 await waitUntilLivePublishedOnAllServers(liveVideoId)
e4bf7856
C
524 await waitJobs(servers)
525 })
526
527 it('Should display no views for a live', async function () {
528 await countViews(0)
529 })
530
531 it('Should view a live twice and display 1 view', async function () {
532 this.timeout(30000)
533
534 await viewVideo(servers[0].url, liveVideoId)
535 await viewVideo(servers[0].url, liveVideoId)
536
2a9562fc 537 await wait(7000)
e4bf7856
C
538
539 await waitJobs(servers)
540
541 await countViews(1)
542 })
543
2a9562fc 544 it('Should wait and display 0 views', async function () {
e4bf7856
C
545 this.timeout(30000)
546
2a9562fc 547 await wait(7000)
e4bf7856
C
548 await waitJobs(servers)
549
550 await countViews(0)
551 })
552
553 it('Should view a live on a remote and on local and display 2 views', async function () {
554 this.timeout(30000)
555
556 await viewVideo(servers[0].url, liveVideoId)
557 await viewVideo(servers[1].url, liveVideoId)
558 await viewVideo(servers[1].url, liveVideoId)
559
2a9562fc 560 await wait(7000)
e4bf7856
C
561 await waitJobs(servers)
562
563 await countViews(2)
564 })
565
566 after(async function () {
567 await stopFfmpeg(command)
568 })
569 })
570
af4ae64f
C
571 describe('Live socket messages', function () {
572
bd54ad19
C
573 async function createLiveWrapper () {
574 const liveAttributes = {
575 name: 'live video',
576 channelId: servers[0].videoChannel.id,
577 privacy: VideoPrivacy.PUBLIC
578 }
579
580 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
581 return res.body.video.uuid
582 }
583
584 it('Should correctly send a message when the live starts and ends', async function () {
585 this.timeout(60000)
586
587 const localStateChanges: VideoState[] = []
588 const remoteStateChanges: VideoState[] = []
589
590 const liveVideoUUID = await createLiveWrapper()
591 await waitJobs(servers)
592
593 {
594 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
595
596 const localSocket = getLiveNotificationSocket(servers[0].url)
597 localSocket.on('state-change', data => localStateChanges.push(data.state))
598 localSocket.emit('subscribe', { videoId })
599 }
600
601 {
602 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
603
604 const remoteSocket = getLiveNotificationSocket(servers[1].url)
605 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
606 remoteSocket.emit('subscribe', { videoId })
607 }
608
609 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
0484ec9e 610
fae6e4da 611 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
bd54ad19
C
612 await waitJobs(servers)
613
614 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
e5a516e7
C
615 expect(stateChanges).to.have.length.at.least(1)
616 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.PUBLISHED)
bd54ad19
C
617 }
618
619 await stopFfmpeg(command)
0484ec9e
C
620
621 for (const server of servers) {
622 await waitUntilLiveEnded(server.url, server.accessToken, liveVideoUUID)
623 }
bd54ad19
C
624 await waitJobs(servers)
625
626 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
e5a516e7
C
627 expect(stateChanges).to.have.length.at.least(2)
628 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.LIVE_ENDED)
bd54ad19 629 }
af4ae64f
C
630 })
631
a800dbf3
C
632 it('Should correctly send views change notification', async function () {
633 this.timeout(60000)
634
635 let localLastVideoViews = 0
636 let remoteLastVideoViews = 0
637
638 const liveVideoUUID = await createLiveWrapper()
639 await waitJobs(servers)
640
641 {
642 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
643
644 const localSocket = getLiveNotificationSocket(servers[0].url)
645 localSocket.on('views-change', data => { localLastVideoViews = data.views })
646 localSocket.emit('subscribe', { videoId })
647 }
648
649 {
650 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
651
652 const remoteSocket = getLiveNotificationSocket(servers[1].url)
653 remoteSocket.on('views-change', data => { remoteLastVideoViews = data.views })
654 remoteSocket.emit('subscribe', { videoId })
655 }
656
657 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
658
fae6e4da 659 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
a800dbf3
C
660 await waitJobs(servers)
661
662 expect(localLastVideoViews).to.equal(0)
663 expect(remoteLastVideoViews).to.equal(0)
664
665 await viewVideo(servers[0].url, liveVideoUUID)
666 await viewVideo(servers[1].url, liveVideoUUID)
667
668 await waitJobs(servers)
669 await wait(5000)
670 await waitJobs(servers)
671
672 expect(localLastVideoViews).to.equal(2)
673 expect(remoteLastVideoViews).to.equal(2)
674
675 await stopFfmpeg(command)
676 })
677
bd54ad19
C
678 it('Should not receive a notification after unsubscribe', async function () {
679 this.timeout(60000)
680
681 const stateChanges: VideoState[] = []
682
683 const liveVideoUUID = await createLiveWrapper()
684 await waitJobs(servers)
685
686 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
687
688 const socket = getLiveNotificationSocket(servers[0].url)
689 socket.on('state-change', data => stateChanges.push(data.state))
690 socket.emit('subscribe', { videoId })
691
692 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
fae6e4da
C
693
694 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
bd54ad19
C
695 await waitJobs(servers)
696
697 expect(stateChanges).to.have.lengthOf(1)
698 socket.emit('unsubscribe', { videoId })
699
700 await stopFfmpeg(command)
701 await waitJobs(servers)
702
703 expect(stateChanges).to.have.lengthOf(1)
af4ae64f
C
704 })
705 })
706
5c0904fc
C
707 describe('After a server restart', function () {
708 let liveVideoId: string
709 let liveVideoReplayId: string
710
711 async function createLiveWrapper (saveReplay: boolean) {
712 const liveAttributes = {
713 name: 'live video',
714 channelId: servers[0].videoChannel.id,
715 privacy: VideoPrivacy.PUBLIC,
716 saveReplay
717 }
718
719 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
720 return res.body.video.uuid
721 }
722
723 before(async function () {
0d8de275 724 this.timeout(120000)
5c0904fc
C
725
726 liveVideoId = await createLiveWrapper(false)
727 liveVideoReplayId = await createLiveWrapper(true)
728
729 await Promise.all([
730 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId),
731 sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoReplayId)
732 ])
733
734 await Promise.all([
0d8de275
C
735 waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoId),
736 waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoReplayId)
5c0904fc
C
737 ])
738
0d8de275
C
739 await waitUntilLiveSegmentGeneration(servers[0], liveVideoId, 0, 2)
740 await waitUntilLiveSegmentGeneration(servers[0], liveVideoReplayId, 0, 2)
741
5c0904fc
C
742 await killallServers([ servers[0] ])
743 await reRunServer(servers[0])
744
745 await wait(5000)
746 })
747
748 it('Should cleanup lives', async function () {
749 this.timeout(60000)
750
0d8de275 751 await waitUntilLiveEnded(servers[0].url, servers[0].accessToken, liveVideoId)
5c0904fc
C
752 })
753
754 it('Should save a live replay', async function () {
0d8de275 755 this.timeout(120000)
5c0904fc 756
b49f22d8 757 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoReplayId)
5c0904fc
C
758 })
759 })
760
af4ae64f
C
761 after(async function () {
762 await cleanupTests(servers)
763 })
764})