]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/live/live-socket-messages.ts
Use an object to represent a server
[github/Chocobozzz/PeerTube.git] / server / tests / api / live / live-socket-messages.ts
CommitLineData
8ebf2a5d
C
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import 'mocha'
4import * as chai from 'chai'
8ebf2a5d
C
5import { VideoPrivacy, VideoState } from '@shared/models'
6import {
7 cleanupTests,
8ebf2a5d 8 doubleFollow,
254d3579
C
9 createMultipleServers,
10 PeerTubeServer,
8ebf2a5d
C
11 setAccessTokensToServers,
12 setDefaultVideoChannel,
13 stopFfmpeg,
8ebf2a5d
C
14 wait,
15 waitJobs,
8ebf2a5d
C
16 waitUntilLivePublishedOnAllServers
17} from '../../../../shared/extra-utils'
18
19const expect = chai.expect
20
21describe('Test live', function () {
254d3579 22 let servers: PeerTubeServer[] = []
8ebf2a5d
C
23
24 before(async function () {
25 this.timeout(120000)
26
254d3579 27 servers = await createMultipleServers(2)
8ebf2a5d
C
28
29 // Get the access tokens
30 await setAccessTokensToServers(servers)
31 await setDefaultVideoChannel(servers)
32
89d241a7 33 await servers[0].config.updateCustomSubConfig({
65e6e260
C
34 newConfig: {
35 live: {
36 enabled: true,
37 allowReplay: true,
38 transcoding: {
39 enabled: false
40 }
8ebf2a5d
C
41 }
42 }
43 })
44
45 // Server 1 and server 2 follow each other
46 await doubleFollow(servers[0], servers[1])
47 })
48
49 describe('Live socket messages', function () {
50
51 async function createLiveWrapper () {
52 const liveAttributes = {
53 name: 'live video',
89d241a7 54 channelId: servers[0].store.channel.id,
8ebf2a5d
C
55 privacy: VideoPrivacy.PUBLIC
56 }
57
89d241a7 58 const { uuid } = await servers[0].live.create({ fields: liveAttributes })
4f219914 59 return uuid
8ebf2a5d
C
60 }
61
62 it('Should correctly send a message when the live starts and ends', async function () {
63 this.timeout(60000)
64
65 const localStateChanges: VideoState[] = []
66 const remoteStateChanges: VideoState[] = []
67
68 const liveVideoUUID = await createLiveWrapper()
69 await waitJobs(servers)
70
71 {
89d241a7 72 const videoId = await servers[0].videos.getId({ uuid: liveVideoUUID })
8ebf2a5d 73
89d241a7 74 const localSocket = servers[0].socketIO.getLiveNotificationSocket()
8ebf2a5d
C
75 localSocket.on('state-change', data => localStateChanges.push(data.state))
76 localSocket.emit('subscribe', { videoId })
77 }
78
79 {
89d241a7 80 const videoId = await servers[1].videos.getId({ uuid: liveVideoUUID })
8ebf2a5d 81
89d241a7 82 const remoteSocket = servers[1].socketIO.getLiveNotificationSocket()
8ebf2a5d
C
83 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
84 remoteSocket.emit('subscribe', { videoId })
85 }
86
89d241a7 87 const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
8ebf2a5d
C
88
89 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
90 await waitJobs(servers)
91
92 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
93 expect(stateChanges).to.have.length.at.least(1)
94 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.PUBLISHED)
95 }
96
4f219914 97 await stopFfmpeg(ffmpegCommand)
8ebf2a5d
C
98
99 for (const server of servers) {
89d241a7 100 await server.live.waitUntilEnded({ videoId: liveVideoUUID })
8ebf2a5d
C
101 }
102 await waitJobs(servers)
103
104 for (const stateChanges of [ localStateChanges, remoteStateChanges ]) {
105 expect(stateChanges).to.have.length.at.least(2)
106 expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.LIVE_ENDED)
107 }
108 })
109
110 it('Should correctly send views change notification', async function () {
111 this.timeout(60000)
112
113 let localLastVideoViews = 0
114 let remoteLastVideoViews = 0
115
116 const liveVideoUUID = await createLiveWrapper()
117 await waitJobs(servers)
118
119 {
89d241a7 120 const videoId = await servers[0].videos.getId({ uuid: liveVideoUUID })
8ebf2a5d 121
89d241a7 122 const localSocket = servers[0].socketIO.getLiveNotificationSocket()
8ebf2a5d
C
123 localSocket.on('views-change', data => { localLastVideoViews = data.views })
124 localSocket.emit('subscribe', { videoId })
125 }
126
127 {
89d241a7 128 const videoId = await servers[1].videos.getId({ uuid: liveVideoUUID })
8ebf2a5d 129
89d241a7 130 const remoteSocket = servers[1].socketIO.getLiveNotificationSocket()
8ebf2a5d
C
131 remoteSocket.on('views-change', data => { remoteLastVideoViews = data.views })
132 remoteSocket.emit('subscribe', { videoId })
133 }
134
89d241a7 135 const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
8ebf2a5d
C
136
137 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
138 await waitJobs(servers)
139
140 expect(localLastVideoViews).to.equal(0)
141 expect(remoteLastVideoViews).to.equal(0)
142
89d241a7
C
143 await servers[0].videos.view({ id: liveVideoUUID })
144 await servers[1].videos.view({ id: liveVideoUUID })
8ebf2a5d
C
145
146 await waitJobs(servers)
147 await wait(5000)
148 await waitJobs(servers)
149
150 expect(localLastVideoViews).to.equal(2)
151 expect(remoteLastVideoViews).to.equal(2)
152
4f219914 153 await stopFfmpeg(ffmpegCommand)
8ebf2a5d
C
154 })
155
156 it('Should not receive a notification after unsubscribe', async function () {
157 this.timeout(120000)
158
159 const stateChanges: VideoState[] = []
160
161 const liveVideoUUID = await createLiveWrapper()
162 await waitJobs(servers)
163
89d241a7 164 const videoId = await servers[0].videos.getId({ uuid: liveVideoUUID })
8ebf2a5d 165
89d241a7 166 const socket = servers[0].socketIO.getLiveNotificationSocket()
8ebf2a5d
C
167 socket.on('state-change', data => stateChanges.push(data.state))
168 socket.emit('subscribe', { videoId })
169
89d241a7 170 const command = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
8ebf2a5d
C
171
172 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
173 await waitJobs(servers)
174
175 // Notifier waits before sending a notification
176 await wait(10000)
177
178 expect(stateChanges).to.have.lengthOf(1)
179 socket.emit('unsubscribe', { videoId })
180
181 await stopFfmpeg(command)
182 await waitJobs(servers)
183
184 expect(stateChanges).to.have.lengthOf(1)
185 })
186 })
187
188 after(async function () {
189 await cleanupTests(servers)
190 })
191})