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 sendRTMPStreamInVideo,
16 setAccessTokensToServers,
17 setDefaultVideoChannel,
18 testFfmpegStreamError,
21 waitUntilLivePublished
22 } from '@shared/extra-utils'
23 import { VideoDetails, VideoPrivacy } from '@shared/models'
25 async function createLiveWrapper (server: ServerInfo) {
26 const liveAttributes = {
28 channelId: server.videoChannel.id,
29 privacy: VideoPrivacy.PUBLIC
32 const res = await createLive(server.url, server.accessToken, liveAttributes)
33 return res.body.video.uuid
36 function updateConf (server: ServerInfo, vodProfile: string, liveProfile: string) {
37 return server.configCommand.updateCustomSubConfig({
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 config = await server.configCommand.getConfig()
118 expect(config.transcoding.availableProfiles).to.have.members([ 'default', 'low-vod', 'input-options-vod', 'bad-scale-vod' ])
119 expect(config.live.transcoding.availableProfiles).to.have.members([ 'default', 'low-live', 'input-options-live', 'bad-scale-live' ])
122 it('Should not use the plugin profile if not chosen by the admin', async function () {
125 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
126 await waitJobs([ server ])
128 await checkVideoFPS(videoUUID, 'above', 20)
131 it('Should use the vod profile', async function () {
134 await updateConf(server, 'low-vod', 'default')
136 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
137 await waitJobs([ server ])
139 await checkVideoFPS(videoUUID, 'below', 12)
142 it('Should apply input options in vod profile', async function () {
145 await updateConf(server, 'input-options-vod', 'default')
147 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
148 await waitJobs([ server ])
150 await checkVideoFPS(videoUUID, 'below', 6)
153 it('Should apply the scale filter in vod profile', async function () {
156 await updateConf(server, 'bad-scale-vod', 'default')
158 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
159 await waitJobs([ server ])
161 // Transcoding failed
162 const res = await getVideo(server.url, videoUUID)
163 const video: VideoDetails = res.body
165 expect(video.files).to.have.lengthOf(1)
166 expect(video.streamingPlaylists).to.have.lengthOf(0)
169 it('Should not use the plugin profile if not chosen by the admin', async function () {
172 const liveVideoId = await createLiveWrapper(server)
174 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
175 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
176 await waitJobs([ server ])
178 await checkLiveFPS(liveVideoId, 'above', 20)
181 it('Should use the live profile', async function () {
184 await updateConf(server, 'low-vod', 'low-live')
186 const liveVideoId = await createLiveWrapper(server)
188 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
189 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
190 await waitJobs([ server ])
192 await checkLiveFPS(liveVideoId, 'below', 12)
195 it('Should apply the input options on live profile', async function () {
198 await updateConf(server, 'low-vod', 'input-options-live')
200 const liveVideoId = await createLiveWrapper(server)
202 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
203 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
204 await waitJobs([ server ])
206 await checkLiveFPS(liveVideoId, 'below', 6)
209 it('Should apply the scale filter name on live profile', async function () {
212 await updateConf(server, 'low-vod', 'bad-scale-live')
214 const liveVideoId = await createLiveWrapper(server)
216 const command = await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
217 await testFfmpegStreamError(command, true)
220 it('Should default to the default profile if the specified profile does not exist', async function () {
223 await server.pluginsCommand.uninstall({ npmName: 'peertube-plugin-test-transcoding-one' })
225 const config = await server.configCommand.getConfig()
227 expect(config.transcoding.availableProfiles).to.deep.equal([ 'default' ])
228 expect(config.live.transcoding.availableProfiles).to.deep.equal([ 'default' ])
230 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
231 await waitJobs([ server ])
233 await checkVideoFPS(videoUUID, 'above', 20)
238 describe('When using a plugin adding new encoders', function () {
240 before(async function () {
241 await server.pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-transcoding-two') })
243 await updateConf(server, 'test-vod-profile', 'test-live-profile')
246 it('Should use the new vod encoders', async function () {
249 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video', fixture: 'video_short_240p.mp4' })).uuid
250 await waitJobs([ server ])
252 const path = buildServerDirectory(server, join('videos', videoUUID + '-240.mp4'))
253 const audioProbe = await getAudioStream(path)
254 expect(audioProbe.audioStream.codec_name).to.equal('opus')
256 const videoProbe = await getVideoStreamFromFile(path)
257 expect(videoProbe.codec_name).to.equal('vp9')
260 it('Should use the new live encoders', async function () {
263 const liveVideoId = await createLiveWrapper(server)
265 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
266 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
267 await waitJobs([ server ])
269 const playlistUrl = `${server.url}/static/streaming-playlists/hls/${liveVideoId}/0.m3u8`
270 const audioProbe = await getAudioStream(playlistUrl)
271 expect(audioProbe.audioStream.codec_name).to.equal('opus')
273 const videoProbe = await getVideoStreamFromFile(playlistUrl)
274 expect(videoProbe.codec_name).to.equal('h264')
278 after(async function () {
279 await cleanupTests([ server ])