]>
Commit | Line | Data |
---|---|---|
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/extra-utils' | |
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 | await wait(5000) | |
148 | await waitJobs(servers) | |
149 | ||
150 | expect(localLastVideoViews).to.equal(2) | |
151 | expect(remoteLastVideoViews).to.equal(2) | |
152 | ||
153 | await stopFfmpeg(ffmpegCommand) | |
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 | ||
164 | const videoId = await servers[0].videos.getId({ uuid: liveVideoUUID }) | |
165 | ||
166 | const socket = servers[0].socketIO.getLiveNotificationSocket() | |
167 | socket.on('state-change', data => stateChanges.push(data.state)) | |
168 | socket.emit('subscribe', { videoId }) | |
169 | ||
170 | const command = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID }) | |
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 | }) |