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