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