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