]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/live/live-socket-messages.ts
shared/ typescript types dir server-commands
[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 { VideoPrivacy, VideoState } from '@shared/models'
6 import {
7 cleanupTests,
8 createMultipleServers,
9 doubleFollow,
10 PeerTubeServer,
11 setAccessTokensToServers,
12 setDefaultVideoChannel,
13 stopFfmpeg,
14 wait,
15 waitJobs,
16 waitUntilLivePublishedOnAllServers
17 } from '../../../../shared/server-commands'
18
19 const expect = chai.expect
20
21 describe('Test live', function () {
22 let servers: PeerTubeServer[] = []
23
24 before(async function () {
25 this.timeout(120000)
26
27 servers = await createMultipleServers(2)
28
29 // Get the access tokens
30 await setAccessTokensToServers(servers)
31 await setDefaultVideoChannel(servers)
32
33 await servers[0].config.updateCustomSubConfig({
34 newConfig: {
35 live: {
36 enabled: true,
37 allowReplay: true,
38 transcoding: {
39 enabled: false
40 }
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',
54 channelId: servers[0].store.channel.id,
55 privacy: VideoPrivacy.PUBLIC
56 }
57
58 const { uuid } = await servers[0].live.create({ fields: liveAttributes })
59 return uuid
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 {
72 const videoId = await servers[0].videos.getId({ uuid: liveVideoUUID })
73
74 const localSocket = servers[0].socketIO.getLiveNotificationSocket()
75 localSocket.on('state-change', data => localStateChanges.push(data.state))
76 localSocket.emit('subscribe', { videoId })
77 }
78
79 {
80 const videoId = await servers[1].videos.getId({ uuid: liveVideoUUID })
81
82 const remoteSocket = servers[1].socketIO.getLiveNotificationSocket()
83 remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
84 remoteSocket.emit('subscribe', { videoId })
85 }
86
87 const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
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
97 await stopFfmpeg(ffmpegCommand)
98
99 for (const server of servers) {
100 await server.live.waitUntilEnded({ videoId: liveVideoUUID })
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 {
120 const videoId = await servers[0].videos.getId({ uuid: liveVideoUUID })
121
122 const localSocket = servers[0].socketIO.getLiveNotificationSocket()
123 localSocket.on('views-change', data => { localLastVideoViews = data.views })
124 localSocket.emit('subscribe', { videoId })
125 }
126
127 {
128 const videoId = await servers[1].videos.getId({ uuid: liveVideoUUID })
129
130 const remoteSocket = servers[1].socketIO.getLiveNotificationSocket()
131 remoteSocket.on('views-change', data => { remoteLastVideoViews = data.views })
132 remoteSocket.emit('subscribe', { videoId })
133 }
134
135 const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
136
137 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
138 await waitJobs(servers)
139
140 expect(localLastVideoViews).to.equal(0)
141 expect(remoteLastVideoViews).to.equal(0)
142
143 await servers[0].videos.view({ id: liveVideoUUID })
144 await servers[1].videos.view({ id: liveVideoUUID })
145
146 await waitJobs(servers)
147
148 expect(localLastVideoViews).to.equal(2)
149 expect(remoteLastVideoViews).to.equal(2)
150
151 await stopFfmpeg(ffmpegCommand)
152 })
153
154 it('Should not receive a notification after unsubscribe', async function () {
155 this.timeout(120000)
156
157 const stateChanges: VideoState[] = []
158
159 const liveVideoUUID = await createLiveWrapper()
160 await waitJobs(servers)
161
162 const videoId = await servers[0].videos.getId({ uuid: liveVideoUUID })
163
164 const socket = servers[0].socketIO.getLiveNotificationSocket()
165 socket.on('state-change', data => stateChanges.push(data.state))
166 socket.emit('subscribe', { videoId })
167
168 const command = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
169
170 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
171 await waitJobs(servers)
172
173 // Notifier waits before sending a notification
174 await wait(10000)
175
176 expect(stateChanges).to.have.lengthOf(1)
177 socket.emit('unsubscribe', { videoId })
178
179 await stopFfmpeg(command)
180 await waitJobs(servers)
181
182 expect(stateChanges).to.have.lengthOf(1)
183 })
184 })
185
186 after(async function () {
187 await cleanupTests(servers)
188 })
189 })