]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/plugins/plugin-transcoding.ts
f1ff9107778232ea3ec192264711a884f460369e
[github/Chocobozzz/PeerTube.git] / server / tests / plugins / plugin-transcoding.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import { expect } from 'chai'
5 import { join } from 'path'
6 import { getAudioStream, getVideoFileFPS, getVideoStreamFromFile } from '@server/helpers/ffprobe-utils'
7 import {
8 buildServerDirectory,
9 cleanupTests,
10 createLive,
11 flushAndRunServer,
12 getConfig,
13 getVideo,
14 PluginsCommand,
15 sendRTMPStreamInVideo,
16 ServerInfo,
17 setAccessTokensToServers,
18 setDefaultVideoChannel,
19 testFfmpegStreamError,
20 updateCustomSubConfig,
21 uploadVideoAndGetId,
22 waitJobs,
23 waitUntilLivePublished
24 } from '@shared/extra-utils'
25 import { ServerConfig, VideoDetails, VideoPrivacy } from '@shared/models'
26
27 async function createLiveWrapper (server: ServerInfo) {
28 const liveAttributes = {
29 name: 'live video',
30 channelId: server.videoChannel.id,
31 privacy: VideoPrivacy.PUBLIC
32 }
33
34 const res = await createLive(server.url, server.accessToken, liveAttributes)
35 return res.body.video.uuid
36 }
37
38 function updateConf (server: ServerInfo, vodProfile: string, liveProfile: string) {
39 return updateCustomSubConfig(server.url, server.accessToken, {
40 transcoding: {
41 enabled: true,
42 profile: vodProfile,
43 hls: {
44 enabled: true
45 },
46 webtorrent: {
47 enabled: true
48 },
49 resolutions: {
50 '240p': true,
51 '360p': false,
52 '480p': false,
53 '720p': true
54 }
55 },
56 live: {
57 transcoding: {
58 profile: liveProfile,
59 enabled: true,
60 resolutions: {
61 '240p': true,
62 '360p': false,
63 '480p': false,
64 '720p': true
65 }
66 }
67 }
68 })
69 }
70
71 describe('Test transcoding plugins', function () {
72 let server: ServerInfo
73
74 before(async function () {
75 this.timeout(60000)
76
77 server = await flushAndRunServer(1)
78 await setAccessTokensToServers([ server ])
79 await setDefaultVideoChannel([ server ])
80
81 await updateConf(server, 'default', 'default')
82 })
83
84 describe('When using a plugin adding profiles to existing encoders', function () {
85
86 async function checkVideoFPS (uuid: string, type: 'above' | 'below', fps: number) {
87 const res = await getVideo(server.url, uuid)
88 const video = res.body as VideoDetails
89 const files = video.files.concat(...video.streamingPlaylists.map(p => p.files))
90
91 for (const file of files) {
92 if (type === 'above') {
93 expect(file.fps).to.be.above(fps)
94 } else {
95 expect(file.fps).to.be.below(fps)
96 }
97 }
98 }
99
100 async function checkLiveFPS (uuid: string, type: 'above' | 'below', fps: number) {
101 const playlistUrl = `${server.url}/static/streaming-playlists/hls/${uuid}/0.m3u8`
102 const videoFPS = await getVideoFileFPS(playlistUrl)
103
104 if (type === 'above') {
105 expect(videoFPS).to.be.above(fps)
106 } else {
107 expect(videoFPS).to.be.below(fps)
108 }
109 }
110
111 before(async function () {
112 await server.pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-transcoding-one') })
113 })
114
115 it('Should have the appropriate available profiles', async function () {
116 const res = await getConfig(server.url)
117 const config = res.body as ServerConfig
118
119 expect(config.transcoding.availableProfiles).to.have.members([ 'default', 'low-vod', 'input-options-vod', 'bad-scale-vod' ])
120 expect(config.live.transcoding.availableProfiles).to.have.members([ 'default', 'low-live', 'input-options-live', 'bad-scale-live' ])
121 })
122
123 it('Should not use the plugin profile if not chosen by the admin', async function () {
124 this.timeout(240000)
125
126 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
127 await waitJobs([ server ])
128
129 await checkVideoFPS(videoUUID, 'above', 20)
130 })
131
132 it('Should use the vod profile', async function () {
133 this.timeout(240000)
134
135 await updateConf(server, 'low-vod', 'default')
136
137 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
138 await waitJobs([ server ])
139
140 await checkVideoFPS(videoUUID, 'below', 12)
141 })
142
143 it('Should apply input options in vod profile', async function () {
144 this.timeout(240000)
145
146 await updateConf(server, 'input-options-vod', 'default')
147
148 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
149 await waitJobs([ server ])
150
151 await checkVideoFPS(videoUUID, 'below', 6)
152 })
153
154 it('Should apply the scale filter in vod profile', async function () {
155 this.timeout(240000)
156
157 await updateConf(server, 'bad-scale-vod', 'default')
158
159 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
160 await waitJobs([ server ])
161
162 // Transcoding failed
163 const res = await getVideo(server.url, videoUUID)
164 const video: VideoDetails = res.body
165
166 expect(video.files).to.have.lengthOf(1)
167 expect(video.streamingPlaylists).to.have.lengthOf(0)
168 })
169
170 it('Should not use the plugin profile if not chosen by the admin', async function () {
171 this.timeout(240000)
172
173 const liveVideoId = await createLiveWrapper(server)
174
175 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
176 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
177 await waitJobs([ server ])
178
179 await checkLiveFPS(liveVideoId, 'above', 20)
180 })
181
182 it('Should use the live profile', async function () {
183 this.timeout(240000)
184
185 await updateConf(server, 'low-vod', 'low-live')
186
187 const liveVideoId = await createLiveWrapper(server)
188
189 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
190 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
191 await waitJobs([ server ])
192
193 await checkLiveFPS(liveVideoId, 'below', 12)
194 })
195
196 it('Should apply the input options on live profile', async function () {
197 this.timeout(240000)
198
199 await updateConf(server, 'low-vod', 'input-options-live')
200
201 const liveVideoId = await createLiveWrapper(server)
202
203 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
204 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
205 await waitJobs([ server ])
206
207 await checkLiveFPS(liveVideoId, 'below', 6)
208 })
209
210 it('Should apply the scale filter name on live profile', async function () {
211 this.timeout(240000)
212
213 await updateConf(server, 'low-vod', 'bad-scale-live')
214
215 const liveVideoId = await createLiveWrapper(server)
216
217 const command = await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
218 await testFfmpegStreamError(command, true)
219 })
220
221 it('Should default to the default profile if the specified profile does not exist', async function () {
222 this.timeout(240000)
223
224 await server.pluginsCommand.uninstall({ npmName: 'peertube-plugin-test-transcoding-one' })
225
226 const res = await getConfig(server.url)
227 const config = res.body as ServerConfig
228
229 expect(config.transcoding.availableProfiles).to.deep.equal([ 'default' ])
230 expect(config.live.transcoding.availableProfiles).to.deep.equal([ 'default' ])
231
232 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
233 await waitJobs([ server ])
234
235 await checkVideoFPS(videoUUID, 'above', 20)
236 })
237
238 })
239
240 describe('When using a plugin adding new encoders', function () {
241
242 before(async function () {
243 await server.pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-transcoding-two') })
244
245 await updateConf(server, 'test-vod-profile', 'test-live-profile')
246 })
247
248 it('Should use the new vod encoders', async function () {
249 this.timeout(240000)
250
251 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video', fixture: 'video_short_240p.mp4' })).uuid
252 await waitJobs([ server ])
253
254 const path = buildServerDirectory(server, join('videos', videoUUID + '-240.mp4'))
255 const audioProbe = await getAudioStream(path)
256 expect(audioProbe.audioStream.codec_name).to.equal('opus')
257
258 const videoProbe = await getVideoStreamFromFile(path)
259 expect(videoProbe.codec_name).to.equal('vp9')
260 })
261
262 it('Should use the new live encoders', async function () {
263 this.timeout(240000)
264
265 const liveVideoId = await createLiveWrapper(server)
266
267 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
268 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
269 await waitJobs([ server ])
270
271 const playlistUrl = `${server.url}/static/streaming-playlists/hls/${liveVideoId}/0.m3u8`
272 const audioProbe = await getAudioStream(playlistUrl)
273 expect(audioProbe.audioStream.codec_name).to.equal('opus')
274
275 const videoProbe = await getVideoStreamFromFile(playlistUrl)
276 expect(videoProbe.codec_name).to.equal('h264')
277 })
278 })
279
280 after(async function () {
281 await cleanupTests([ server ])
282 })
283 })