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'
14 setAccessTokensToServers,
15 setDefaultVideoChannel,
16 testFfmpegStreamError,
19 } from '@shared/extra-utils'
20 import { VideoDetails, VideoPrivacy } from '@shared/models'
22 async function createLiveWrapper (server: ServerInfo) {
23 const liveAttributes = {
25 channelId: server.videoChannel.id,
26 privacy: VideoPrivacy.PUBLIC
29 const { uuid } = await server.liveCommand.createLive({ fields: liveAttributes })
34 function updateConf (server: ServerInfo, vodProfile: string, liveProfile: string) {
35 return server.configCommand.updateCustomSubConfig({
69 describe('Test transcoding plugins', function () {
70 let server: ServerInfo
72 before(async function () {
75 server = await flushAndRunServer(1)
76 await setAccessTokensToServers([ server ])
77 await setDefaultVideoChannel([ server ])
79 await updateConf(server, 'default', 'default')
82 describe('When using a plugin adding profiles to existing encoders', function () {
84 async function checkVideoFPS (uuid: string, type: 'above' | 'below', fps: number) {
85 const res = await getVideo(server.url, uuid)
86 const video = res.body as VideoDetails
87 const files = video.files.concat(...video.streamingPlaylists.map(p => p.files))
89 for (const file of files) {
90 if (type === 'above') {
91 expect(file.fps).to.be.above(fps)
93 expect(file.fps).to.be.below(fps)
98 async function checkLiveFPS (uuid: string, type: 'above' | 'below', fps: number) {
99 const playlistUrl = `${server.url}/static/streaming-playlists/hls/${uuid}/0.m3u8`
100 const videoFPS = await getVideoFileFPS(playlistUrl)
102 if (type === 'above') {
103 expect(videoFPS).to.be.above(fps)
105 expect(videoFPS).to.be.below(fps)
109 before(async function () {
110 await server.pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-transcoding-one') })
113 it('Should have the appropriate available profiles', async function () {
114 const config = await server.configCommand.getConfig()
116 expect(config.transcoding.availableProfiles).to.have.members([ 'default', 'low-vod', 'input-options-vod', 'bad-scale-vod' ])
117 expect(config.live.transcoding.availableProfiles).to.have.members([ 'default', 'low-live', 'input-options-live', 'bad-scale-live' ])
120 it('Should not use the plugin profile if not chosen by the admin', async function () {
123 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
124 await waitJobs([ server ])
126 await checkVideoFPS(videoUUID, 'above', 20)
129 it('Should use the vod profile', async function () {
132 await updateConf(server, 'low-vod', 'default')
134 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
135 await waitJobs([ server ])
137 await checkVideoFPS(videoUUID, 'below', 12)
140 it('Should apply input options in vod profile', async function () {
143 await updateConf(server, 'input-options-vod', 'default')
145 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
146 await waitJobs([ server ])
148 await checkVideoFPS(videoUUID, 'below', 6)
151 it('Should apply the scale filter in vod profile', async function () {
154 await updateConf(server, 'bad-scale-vod', 'default')
156 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
157 await waitJobs([ server ])
159 // Transcoding failed
160 const res = await getVideo(server.url, videoUUID)
161 const video: VideoDetails = res.body
163 expect(video.files).to.have.lengthOf(1)
164 expect(video.streamingPlaylists).to.have.lengthOf(0)
167 it('Should not use the plugin profile if not chosen by the admin', async function () {
170 const liveVideoId = await createLiveWrapper(server)
172 await server.liveCommand.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
173 await server.liveCommand.waitUntilLivePublished({ videoId: liveVideoId })
174 await waitJobs([ server ])
176 await checkLiveFPS(liveVideoId, 'above', 20)
179 it('Should use the live profile', async function () {
182 await updateConf(server, 'low-vod', 'low-live')
184 const liveVideoId = await createLiveWrapper(server)
186 await server.liveCommand.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
187 await server.liveCommand.waitUntilLivePublished({ videoId: liveVideoId })
188 await waitJobs([ server ])
190 await checkLiveFPS(liveVideoId, 'below', 12)
193 it('Should apply the input options on live profile', async function () {
196 await updateConf(server, 'low-vod', 'input-options-live')
198 const liveVideoId = await createLiveWrapper(server)
200 await server.liveCommand.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
201 await server.liveCommand.waitUntilLivePublished({ videoId: liveVideoId })
202 await waitJobs([ server ])
204 await checkLiveFPS(liveVideoId, 'below', 6)
207 it('Should apply the scale filter name on live profile', async function () {
210 await updateConf(server, 'low-vod', 'bad-scale-live')
212 const liveVideoId = await createLiveWrapper(server)
214 const command = await server.liveCommand.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
215 await testFfmpegStreamError(command, true)
218 it('Should default to the default profile if the specified profile does not exist', async function () {
221 await server.pluginsCommand.uninstall({ npmName: 'peertube-plugin-test-transcoding-one' })
223 const config = await server.configCommand.getConfig()
225 expect(config.transcoding.availableProfiles).to.deep.equal([ 'default' ])
226 expect(config.live.transcoding.availableProfiles).to.deep.equal([ 'default' ])
228 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
229 await waitJobs([ server ])
231 await checkVideoFPS(videoUUID, 'above', 20)
236 describe('When using a plugin adding new encoders', function () {
238 before(async function () {
239 await server.pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-transcoding-two') })
241 await updateConf(server, 'test-vod-profile', 'test-live-profile')
244 it('Should use the new vod encoders', async function () {
247 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video', fixture: 'video_short_240p.mp4' })).uuid
248 await waitJobs([ server ])
250 const path = buildServerDirectory(server, join('videos', videoUUID + '-240.mp4'))
251 const audioProbe = await getAudioStream(path)
252 expect(audioProbe.audioStream.codec_name).to.equal('opus')
254 const videoProbe = await getVideoStreamFromFile(path)
255 expect(videoProbe.codec_name).to.equal('vp9')
258 it('Should use the new live encoders', async function () {
261 const liveVideoId = await createLiveWrapper(server)
263 await server.liveCommand.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
264 await server.liveCommand.waitUntilLivePublished({ videoId: liveVideoId })
265 await waitJobs([ server ])
267 const playlistUrl = `${server.url}/static/streaming-playlists/hls/${liveVideoId}/0.m3u8`
268 const audioProbe = await getAudioStream(playlistUrl)
269 expect(audioProbe.audioStream.codec_name).to.equal('opus')
271 const videoProbe = await getVideoStreamFromFile(playlistUrl)
272 expect(videoProbe.codec_name).to.equal('h264')
276 after(async function () {
277 await cleanupTests([ server ])