1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { wait } from '@shared/core-utils'
5 import { VideoPrivacy, VideoState } from '@shared/models'
11 setAccessTokensToServers,
12 setDefaultVideoChannel,
15 waitUntilLivePublishedOnAllServers
16 } from '@shared/server-commands'
18 describe('Test live socket messages', function () {
19 let servers: PeerTubeServer[] = []
21 before(async function () {
24 servers = await createMultipleServers(2)
26 // Get the access tokens
27 await setAccessTokensToServers(servers)
28 await setDefaultVideoChannel(servers)
30 await servers[0].config.updateCustomSubConfig({
42 // Server 1 and server 2 follow each other
43 await doubleFollow(servers[0], servers[1])
46 describe('Live socket messages', function () {
48 async function createLiveWrapper () {
49 const liveAttributes = {
51 channelId: servers[0].store.channel.id,
52 privacy: VideoPrivacy.PUBLIC
55 const { uuid } = await servers[0].live.create({ fields: liveAttributes })
59 it('Should correctly send a message when the live starts and ends', async function () {
62 const localStateChanges: VideoState[] = []
63 const remoteStateChanges: VideoState[] = []
65 const liveVideoUUID = await createLiveWrapper()
66 await waitJobs(servers)
69 const videoId = await servers[0].videos.getId({ uuid: liveVideoUUID })
71 const localSocket = servers[0].socketIO.getLiveNotificationSocket()
72 localSocket.on('state-change', data => localStateChanges.push(data.state))
73 localSocket.emit('subscribe', { videoId })
77 const videoId = await servers[1].videos.getId({ uuid: liveVideoUUID })
79 const remoteSocket = servers[1].socketIO.getLiveNotificationSocket()
80 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
81 remoteSocket.emit('subscribe', { videoId })
84 const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
86 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
87 await waitJobs(servers)
89 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
90 expect(stateChanges).to.have.length.at.least(1)
91 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.PUBLISHED)
94 await stopFfmpeg(ffmpegCommand)
96 for (const server of servers) {
97 await server.live.waitUntilEnded({ videoId: liveVideoUUID })
99 await waitJobs(servers)
101 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
102 expect(stateChanges).to.have.length.at.least(2)
103 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.LIVE_ENDED)
107 it('Should correctly send views change notification', async function () {
110 let localLastVideoViews = 0
111 let remoteLastVideoViews = 0
113 const liveVideoUUID = await createLiveWrapper()
114 await waitJobs(servers)
117 const videoId = await servers[0].videos.getId({ uuid: liveVideoUUID })
119 const localSocket = servers[0].socketIO.getLiveNotificationSocket()
120 localSocket.on('views-change', data => { localLastVideoViews = data.views })
121 localSocket.emit('subscribe', { videoId })
125 const videoId = await servers[1].videos.getId({ uuid: liveVideoUUID })
127 const remoteSocket = servers[1].socketIO.getLiveNotificationSocket()
128 remoteSocket.on('views-change', data => { remoteLastVideoViews = data.views })
129 remoteSocket.emit('subscribe', { videoId })
132 const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
134 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
135 await waitJobs(servers)
137 expect(localLastVideoViews).to.equal(0)
138 expect(remoteLastVideoViews).to.equal(0)
140 await servers[0].views.simulateView({ id: liveVideoUUID })
141 await servers[1].views.simulateView({ id: liveVideoUUID })
143 await waitJobs(servers)
145 expect(localLastVideoViews).to.equal(2)
146 expect(remoteLastVideoViews).to.equal(2)
148 await stopFfmpeg(ffmpegCommand)
151 it('Should not receive a notification after unsubscribe', async function () {
154 const stateChanges: VideoState[] = []
156 const liveVideoUUID = await createLiveWrapper()
157 await waitJobs(servers)
159 const videoId = await servers[0].videos.getId({ uuid: liveVideoUUID })
161 const socket = servers[0].socketIO.getLiveNotificationSocket()
162 socket.on('state-change', data => stateChanges.push(data.state))
163 socket.emit('subscribe', { videoId })
165 const command = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
167 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
168 await waitJobs(servers)
170 // Notifier waits before sending a notification
173 expect(stateChanges).to.have.lengthOf(1)
174 socket.emit('unsubscribe', { videoId })
176 await stopFfmpeg(command)
177 await waitJobs(servers)
179 expect(stateChanges).to.have.lengthOf(1)
183 after(async function () {
184 await cleanupTests(servers)