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