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