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