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