]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/live/live.ts
f7ccb453d02c132c46b08d5094563b2d514aa848
[github/Chocobozzz/PeerTube.git] / server / tests / api / live / live.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 { LiveVideo, LiveVideoCreate, User, VideoDetails, VideoPrivacy } from '@shared/models'
6 import {
7 addVideoToBlacklist,
8 cleanupTests,
9 createLive,
10 createUser,
11 doubleFollow,
12 flushAndRunMultipleServers,
13 getLive,
14 getMyUserInformation,
15 getVideo,
16 getVideosList,
17 makeRawRequest,
18 removeVideo,
19 sendRTMPStream,
20 ServerInfo,
21 setAccessTokensToServers,
22 setDefaultVideoChannel,
23 testFfmpegStreamError,
24 testImage,
25 updateCustomSubConfig,
26 updateLive,
27 userLogin,
28 waitJobs
29 } from '../../../../shared/extra-utils'
30
31 const expect = chai.expect
32
33 describe('Test live', function () {
34 let servers: ServerInfo[] = []
35 let userId: number
36 let userAccessToken: string
37 let userChannelId: number
38
39 before(async function () {
40 this.timeout(120000)
41
42 servers = await flushAndRunMultipleServers(2)
43
44 // Get the access tokens
45 await setAccessTokensToServers(servers)
46 await setDefaultVideoChannel(servers)
47
48 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
49 live: {
50 enabled: true,
51 allowReplay: true,
52 transcoding: {
53 enabled: false
54 }
55 }
56 })
57
58 {
59 const user = { username: 'user1', password: 'superpassword' }
60 const res = await createUser({
61 url: servers[0].url,
62 accessToken: servers[0].accessToken,
63 username: user.username,
64 password: user.password
65 })
66 userId = res.body.user.id
67
68 userAccessToken = await userLogin(servers[0], user)
69
70 const resMe = await getMyUserInformation(servers[0].url, userAccessToken)
71 userChannelId = (resMe.body as User).videoChannels[0].id
72 }
73
74 // Server 1 and server 2 follow each other
75 await doubleFollow(servers[0], servers[1])
76 })
77
78 describe('Live creation, update and delete', function () {
79 let liveVideoUUID: string
80
81 it('Should create a live with the appropriate parameters', async function () {
82 this.timeout(20000)
83
84 const attributes: LiveVideoCreate = {
85 category: 1,
86 licence: 2,
87 language: 'fr',
88 description: 'super live description',
89 support: 'support field',
90 channelId: servers[0].videoChannel.id,
91 nsfw: false,
92 waitTranscoding: false,
93 name: 'my super live',
94 tags: [ 'tag1', 'tag2' ],
95 commentsEnabled: false,
96 downloadEnabled: false,
97 saveReplay: true,
98 privacy: VideoPrivacy.PUBLIC,
99 previewfile: 'video_short1-preview.webm.jpg',
100 thumbnailfile: 'video_short1.webm.jpg'
101 }
102
103 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
104 liveVideoUUID = res.body.video.uuid
105
106 await waitJobs(servers)
107
108 for (const server of servers) {
109 const resVideo = await getVideo(server.url, liveVideoUUID)
110 const video: VideoDetails = resVideo.body
111
112 expect(video.category.id).to.equal(1)
113 expect(video.licence.id).to.equal(2)
114 expect(video.language.id).to.equal('fr')
115 expect(video.description).to.equal('super live description')
116 expect(video.support).to.equal('support field')
117
118 expect(video.channel.name).to.equal(servers[0].videoChannel.name)
119 expect(video.channel.host).to.equal(servers[0].videoChannel.host)
120
121 expect(video.nsfw).to.be.false
122 expect(video.waitTranscoding).to.be.false
123 expect(video.name).to.equal('my super live')
124 expect(video.tags).to.deep.equal([ 'tag1', 'tag2' ])
125 expect(video.commentsEnabled).to.be.false
126 expect(video.downloadEnabled).to.be.false
127 expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)
128
129 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
130 await testImage(server.url, 'video_short1.webm', video.thumbnailPath)
131
132 const resLive = await getLive(server.url, server.accessToken, liveVideoUUID)
133 const live: LiveVideo = resLive.body
134
135 if (server.url === servers[0].url) {
136 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':1936/live')
137 expect(live.streamKey).to.not.be.empty
138 } else {
139 expect(live.rtmpUrl).to.be.null
140 expect(live.streamKey).to.be.null
141 }
142
143 expect(live.saveReplay).to.be.true
144 }
145 })
146
147 it('Should have a default preview and thumbnail', async function () {
148 this.timeout(20000)
149
150 const attributes: LiveVideoCreate = {
151 name: 'default live thumbnail',
152 channelId: servers[0].videoChannel.id,
153 privacy: VideoPrivacy.UNLISTED,
154 nsfw: true
155 }
156
157 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
158 const videoId = res.body.video.uuid
159
160 await waitJobs(servers)
161
162 for (const server of servers) {
163 const resVideo = await getVideo(server.url, videoId)
164 const video: VideoDetails = resVideo.body
165
166 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
167 expect(video.nsfw).to.be.true
168
169 await makeRawRequest(server.url + video.thumbnailPath, 200)
170 await makeRawRequest(server.url + video.previewPath, 200)
171 }
172 })
173
174 it('Should not have the live listed since nobody streams into', async function () {
175 for (const server of servers) {
176 const res = await getVideosList(server.url)
177
178 expect(res.body.total).to.equal(0)
179 expect(res.body.data).to.have.lengthOf(0)
180 }
181 })
182
183 it('Should not be able to update a live of another server', async function () {
184 await updateLive(servers[1].url, servers[1].accessToken, liveVideoUUID, { saveReplay: false }, 403)
185 })
186
187 it('Should update the live', async function () {
188 this.timeout(10000)
189
190 await updateLive(servers[0].url, servers[0].accessToken, liveVideoUUID, { saveReplay: false })
191 await waitJobs(servers)
192 })
193
194 it('Have the live updated', async function () {
195 for (const server of servers) {
196 const res = await getLive(server.url, server.accessToken, liveVideoUUID)
197 const live: LiveVideo = res.body
198
199 if (server.url === servers[0].url) {
200 expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':1936/live')
201 expect(live.streamKey).to.not.be.empty
202 } else {
203 expect(live.rtmpUrl).to.be.null
204 expect(live.streamKey).to.be.null
205 }
206
207 expect(live.saveReplay).to.be.false
208 }
209 })
210
211 it('Delete the live', async function () {
212 this.timeout(10000)
213
214 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
215 await waitJobs(servers)
216 })
217
218 it('Should have the live deleted', async function () {
219 for (const server of servers) {
220 await getVideo(server.url, liveVideoUUID, 404)
221 await getLive(server.url, server.accessToken, liveVideoUUID, 404)
222 }
223 })
224 })
225
226 describe('Stream checks', function () {
227 let liveVideo: LiveVideo & VideoDetails
228 let rtmpUrl: string
229
230 before(function () {
231 rtmpUrl = 'rtmp://' + servers[0].hostname + ':1936'
232 })
233
234 async function createLiveWrapper () {
235 const liveAttributes = {
236 name: 'user live',
237 channelId: userChannelId,
238 privacy: VideoPrivacy.PUBLIC,
239 saveReplay: false
240 }
241
242 const res = await createLive(servers[0].url, userAccessToken, liveAttributes)
243 const uuid = res.body.video.uuid
244
245 const resLive = await getLive(servers[0].url, servers[0].accessToken, uuid)
246 const resVideo = await getVideo(servers[0].url, uuid)
247
248 return Object.assign(resVideo.body, resLive.body) as LiveVideo & VideoDetails
249 }
250
251 it('Should not allow a stream without the appropriate path', async function () {
252 this.timeout(30000)
253
254 liveVideo = await createLiveWrapper()
255
256 const command = sendRTMPStream(rtmpUrl + '/bad-live', liveVideo.streamKey)
257 await testFfmpegStreamError(command, true)
258 })
259
260 it('Should not allow a stream without the appropriate stream key', async function () {
261 this.timeout(30000)
262
263 const command = sendRTMPStream(rtmpUrl + '/live', 'bad-stream-key')
264 await testFfmpegStreamError(command, true)
265 })
266
267 it('Should succeed with the correct params', async function () {
268 this.timeout(30000)
269
270 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
271 await testFfmpegStreamError(command, false)
272 })
273
274 it('Should not allow a stream on a live that was blacklisted', async function () {
275 this.timeout(30000)
276
277 liveVideo = await createLiveWrapper()
278
279 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideo.uuid)
280
281 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
282 await testFfmpegStreamError(command, true)
283 })
284
285 it('Should not allow a stream on a live that was deleted', async function () {
286 this.timeout(30000)
287
288 liveVideo = await createLiveWrapper()
289
290 await removeVideo(servers[0].url, servers[0].accessToken, liveVideo.uuid)
291
292 const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey)
293 await testFfmpegStreamError(command, true)
294 })
295 })
296
297 describe('Live transcoding', function () {
298
299 it('Should enable transcoding without additional resolutions', async function () {
300 // enable
301 // stream
302 // wait federation + test
303
304 })
305
306 it('Should enable transcoding with some resolutions', async function () {
307 // enable
308 // stream
309 // wait federation + test
310 })
311
312 it('Should enable transcoding with some resolutions and correctly save them', async function () {
313 // enable
314 // stream
315 // end stream
316 // wait federation + test
317 })
318
319 it('Should correctly have cleaned up the live files', async function () {
320 // check files
321 })
322 })
323
324 describe('Live socket messages', function () {
325
326 it('Should correctly send a message when the live starts', async function () {
327 // local
328 // federation
329 })
330
331 it('Should correctly send a message when the live ends', async function () {
332 // local
333 // federation
334 })
335 })
336
337 after(async function () {
338 await cleanupTests(servers)
339 })
340 })