1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { VideoPrivacy, VideoState } from '@shared/models'
9 flushAndRunMultipleServers,
12 setAccessTokensToServers,
13 setDefaultVideoChannel,
18 waitUntilLivePublishedOnAllServers
19 } from '../../../../shared/extra-utils'
21 const expect = chai.expect
23 describe('Test live', function () {
24 let servers: ServerInfo[] = []
26 before(async function () {
29 servers = await flushAndRunMultipleServers(2)
31 // Get the access tokens
32 await setAccessTokensToServers(servers)
33 await setDefaultVideoChannel(servers)
35 await servers[0].configCommand.updateCustomSubConfig({
47 // Server 1 and server 2 follow each other
48 await doubleFollow(servers[0], servers[1])
51 describe('Live socket messages', function () {
53 async function createLiveWrapper () {
54 const liveAttributes = {
56 channelId: servers[0].videoChannel.id,
57 privacy: VideoPrivacy.PUBLIC
60 const { uuid } = await servers[0].liveCommand.create({ fields: liveAttributes })
64 it('Should correctly send a message when the live starts and ends', async function () {
67 const localStateChanges: VideoState[] = []
68 const remoteStateChanges: VideoState[] = []
70 const liveVideoUUID = await createLiveWrapper()
71 await waitJobs(servers)
74 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
76 const localSocket = servers[0].socketIOCommand.getLiveNotificationSocket()
77 localSocket.on('state-change', data => localStateChanges.push(data.state))
78 localSocket.emit('subscribe', { videoId })
82 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
84 const remoteSocket = servers[1].socketIOCommand.getLiveNotificationSocket()
85 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
86 remoteSocket.emit('subscribe', { videoId })
89 const ffmpegCommand = await servers[0].liveCommand.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
91 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
92 await waitJobs(servers)
94 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
95 expect(stateChanges).to.have.length.at.least(1)
96 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.PUBLISHED)
99 await stopFfmpeg(ffmpegCommand)
101 for (const server of servers) {
102 await server.liveCommand.waitUntilEnded({ videoId: liveVideoUUID })
104 await waitJobs(servers)
106 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
107 expect(stateChanges).to.have.length.at.least(2)
108 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.LIVE_ENDED)
112 it('Should correctly send views change notification', async function () {
115 let localLastVideoViews = 0
116 let remoteLastVideoViews = 0
118 const liveVideoUUID = await createLiveWrapper()
119 await waitJobs(servers)
122 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
124 const localSocket = servers[0].socketIOCommand.getLiveNotificationSocket()
125 localSocket.on('views-change', data => { localLastVideoViews = data.views })
126 localSocket.emit('subscribe', { videoId })
130 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
132 const remoteSocket = servers[1].socketIOCommand.getLiveNotificationSocket()
133 remoteSocket.on('views-change', data => { remoteLastVideoViews = data.views })
134 remoteSocket.emit('subscribe', { videoId })
137 const ffmpegCommand = await servers[0].liveCommand.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
139 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
140 await waitJobs(servers)
142 expect(localLastVideoViews).to.equal(0)
143 expect(remoteLastVideoViews).to.equal(0)
145 await viewVideo(servers[0].url, liveVideoUUID)
146 await viewVideo(servers[1].url, liveVideoUUID)
148 await waitJobs(servers)
150 await waitJobs(servers)
152 expect(localLastVideoViews).to.equal(2)
153 expect(remoteLastVideoViews).to.equal(2)
155 await stopFfmpeg(ffmpegCommand)
158 it('Should not receive a notification after unsubscribe', async function () {
161 const stateChanges: VideoState[] = []
163 const liveVideoUUID = await createLiveWrapper()
164 await waitJobs(servers)
166 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
168 const socket = servers[0].socketIOCommand.getLiveNotificationSocket()
169 socket.on('state-change', data => stateChanges.push(data.state))
170 socket.emit('subscribe', { videoId })
172 const command = await servers[0].liveCommand.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
174 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
175 await waitJobs(servers)
177 // Notifier waits before sending a notification
180 expect(stateChanges).to.have.lengthOf(1)
181 socket.emit('unsubscribe', { videoId })
183 await stopFfmpeg(command)
184 await waitJobs(servers)
186 expect(stateChanges).to.have.lengthOf(1)
190 after(async function () {
191 await cleanupTests(servers)