1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { getLiveNotificationSocket } from '@shared/extra-utils/socket/socket-io'
6 import { VideoPrivacy, VideoState } from '@shared/models'
11 flushAndRunMultipleServers,
13 sendRTMPStreamInVideo,
15 setAccessTokensToServers,
16 setDefaultVideoChannel,
22 waitUntilLivePublishedOnAllServers
23 } from '../../../../shared/extra-utils'
25 const expect = chai.expect
27 describe('Test live', function () {
28 let servers: ServerInfo[] = []
30 before(async function () {
33 servers = await flushAndRunMultipleServers(2)
35 // Get the access tokens
36 await setAccessTokensToServers(servers)
37 await setDefaultVideoChannel(servers)
39 await servers[0].configCommand.updateCustomSubConfig({
51 // Server 1 and server 2 follow each other
52 await doubleFollow(servers[0], servers[1])
55 describe('Live socket messages', function () {
57 async function createLiveWrapper () {
58 const liveAttributes = {
60 channelId: servers[0].videoChannel.id,
61 privacy: VideoPrivacy.PUBLIC
64 const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
65 return res.body.video.uuid
68 it('Should correctly send a message when the live starts and ends', async function () {
71 const localStateChanges: VideoState[] = []
72 const remoteStateChanges: VideoState[] = []
74 const liveVideoUUID = await createLiveWrapper()
75 await waitJobs(servers)
78 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
80 const localSocket = getLiveNotificationSocket(servers[0].url)
81 localSocket.on('state-change', data => localStateChanges.push(data.state))
82 localSocket.emit('subscribe', { videoId })
86 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
88 const remoteSocket = getLiveNotificationSocket(servers[1].url)
89 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
90 remoteSocket.emit('subscribe', { videoId })
93 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
95 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
96 await waitJobs(servers)
98 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
99 expect(stateChanges).to.have.length.at.least(1)
100 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.PUBLISHED)
103 await stopFfmpeg(command)
105 for (const server of servers) {
106 await waitUntilLiveEnded(server.url, server.accessToken, liveVideoUUID)
108 await waitJobs(servers)
110 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
111 expect(stateChanges).to.have.length.at.least(2)
112 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.LIVE_ENDED)
116 it('Should correctly send views change notification', async function () {
119 let localLastVideoViews = 0
120 let remoteLastVideoViews = 0
122 const liveVideoUUID = await createLiveWrapper()
123 await waitJobs(servers)
126 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
128 const localSocket = getLiveNotificationSocket(servers[0].url)
129 localSocket.on('views-change', data => { localLastVideoViews = data.views })
130 localSocket.emit('subscribe', { videoId })
134 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
136 const remoteSocket = getLiveNotificationSocket(servers[1].url)
137 remoteSocket.on('views-change', data => { remoteLastVideoViews = data.views })
138 remoteSocket.emit('subscribe', { videoId })
141 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
143 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
144 await waitJobs(servers)
146 expect(localLastVideoViews).to.equal(0)
147 expect(remoteLastVideoViews).to.equal(0)
149 await viewVideo(servers[0].url, liveVideoUUID)
150 await viewVideo(servers[1].url, liveVideoUUID)
152 await waitJobs(servers)
154 await waitJobs(servers)
156 expect(localLastVideoViews).to.equal(2)
157 expect(remoteLastVideoViews).to.equal(2)
159 await stopFfmpeg(command)
162 it('Should not receive a notification after unsubscribe', async function () {
165 const stateChanges: VideoState[] = []
167 const liveVideoUUID = await createLiveWrapper()
168 await waitJobs(servers)
170 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
172 const socket = getLiveNotificationSocket(servers[0].url)
173 socket.on('state-change', data => stateChanges.push(data.state))
174 socket.emit('subscribe', { videoId })
176 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
178 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
179 await waitJobs(servers)
181 // Notifier waits before sending a notification
184 expect(stateChanges).to.have.lengthOf(1)
185 socket.emit('unsubscribe', { videoId })
187 await stopFfmpeg(command)
188 await waitJobs(servers)
190 expect(stateChanges).to.have.lengthOf(1)
194 after(async function () {
195 await cleanupTests(servers)