1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { expect } from 'chai'
5 import { join } from 'path'
6 import { getAudioStream, getVideoFileFPS, getVideoStreamFromFile } from '@server/helpers/ffprobe-utils'
15 sendRTMPStreamInVideo,
17 setAccessTokensToServers,
18 setDefaultVideoChannel,
19 testFfmpegStreamError,
20 updateCustomSubConfig,
23 waitUntilLivePublished
24 } from '@shared/extra-utils'
25 import { ServerConfig, VideoDetails, VideoPrivacy } from '@shared/models'
27 async function createLiveWrapper (server: ServerInfo) {
28 const liveAttributes = {
30 channelId: server.videoChannel.id,
31 privacy: VideoPrivacy.PUBLIC
34 const res = await createLive(server.url, server.accessToken, liveAttributes)
35 return res.body.video.uuid
38 function updateConf (server: ServerInfo, vodProfile: string, liveProfile: string) {
39 return updateCustomSubConfig(server.url, server.accessToken, {
71 describe('Test transcoding plugins', function () {
72 let server: ServerInfo
74 before(async function () {
77 server = await flushAndRunServer(1)
78 await setAccessTokensToServers([ server ])
79 await setDefaultVideoChannel([ server ])
81 await updateConf(server, 'default', 'default')
84 describe('When using a plugin adding profiles to existing encoders', function () {
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))
91 for (const file of files) {
92 if (type === 'above') {
93 expect(file.fps).to.be.above(fps)
95 expect(file.fps).to.be.below(fps)
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)
104 if (type === 'above') {
105 expect(videoFPS).to.be.above(fps)
107 expect(videoFPS).to.be.below(fps)
111 before(async function () {
112 await server.pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-transcoding-one') })
115 it('Should have the appropriate available profiles', async function () {
116 const res = await getConfig(server.url)
117 const config = res.body as ServerConfig
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' ])
123 it('Should not use the plugin profile if not chosen by the admin', async function () {
126 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
127 await waitJobs([ server ])
129 await checkVideoFPS(videoUUID, 'above', 20)
132 it('Should use the vod profile', async function () {
135 await updateConf(server, 'low-vod', 'default')
137 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
138 await waitJobs([ server ])
140 await checkVideoFPS(videoUUID, 'below', 12)
143 it('Should apply input options in vod profile', async function () {
146 await updateConf(server, 'input-options-vod', 'default')
148 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
149 await waitJobs([ server ])
151 await checkVideoFPS(videoUUID, 'below', 6)
154 it('Should apply the scale filter in vod profile', async function () {
157 await updateConf(server, 'bad-scale-vod', 'default')
159 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
160 await waitJobs([ server ])
162 // Transcoding failed
163 const res = await getVideo(server.url, videoUUID)
164 const video: VideoDetails = res.body
166 expect(video.files).to.have.lengthOf(1)
167 expect(video.streamingPlaylists).to.have.lengthOf(0)
170 it('Should not use the plugin profile if not chosen by the admin', async function () {
173 const liveVideoId = await createLiveWrapper(server)
175 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
176 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
177 await waitJobs([ server ])
179 await checkLiveFPS(liveVideoId, 'above', 20)
182 it('Should use the live profile', async function () {
185 await updateConf(server, 'low-vod', 'low-live')
187 const liveVideoId = await createLiveWrapper(server)
189 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
190 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
191 await waitJobs([ server ])
193 await checkLiveFPS(liveVideoId, 'below', 12)
196 it('Should apply the input options on live profile', async function () {
199 await updateConf(server, 'low-vod', 'input-options-live')
201 const liveVideoId = await createLiveWrapper(server)
203 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
204 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
205 await waitJobs([ server ])
207 await checkLiveFPS(liveVideoId, 'below', 6)
210 it('Should apply the scale filter name on live profile', async function () {
213 await updateConf(server, 'low-vod', 'bad-scale-live')
215 const liveVideoId = await createLiveWrapper(server)
217 const command = await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
218 await testFfmpegStreamError(command, true)
221 it('Should default to the default profile if the specified profile does not exist', async function () {
224 await server.pluginsCommand.uninstall({ npmName: 'peertube-plugin-test-transcoding-one' })
226 const res = await getConfig(server.url)
227 const config = res.body as ServerConfig
229 expect(config.transcoding.availableProfiles).to.deep.equal([ 'default' ])
230 expect(config.live.transcoding.availableProfiles).to.deep.equal([ 'default' ])
232 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
233 await waitJobs([ server ])
235 await checkVideoFPS(videoUUID, 'above', 20)
240 describe('When using a plugin adding new encoders', function () {
242 before(async function () {
243 await server.pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-transcoding-two') })
245 await updateConf(server, 'test-vod-profile', 'test-live-profile')
248 it('Should use the new vod encoders', async function () {
251 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video', fixture: 'video_short_240p.mp4' })).uuid
252 await waitJobs([ server ])
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')
258 const videoProbe = await getVideoStreamFromFile(path)
259 expect(videoProbe.codec_name).to.equal('vp9')
262 it('Should use the new live encoders', async function () {
265 const liveVideoId = await createLiveWrapper(server)
267 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
268 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
269 await waitJobs([ server ])
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')
275 const videoProbe = await getVideoStreamFromFile(playlistUrl)
276 expect(videoProbe.codec_name).to.equal('h264')
280 after(async function () {
281 await cleanupTests([ server ])