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'
7 import { ServerConfig, VideoDetails, VideoPrivacy } from '@shared/models'
15 sendRTMPStreamInVideo,
16 setAccessTokensToServers,
17 setDefaultVideoChannel,
18 testFfmpegStreamError,
20 updateCustomSubConfig,
24 waitUntilLivePublished
25 } from '../../../shared/extra-utils'
26 import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../shared/extra-utils/server/servers'
28 async function createLiveWrapper (server: ServerInfo) {
29 const liveAttributes = {
31 channelId: server.videoChannel.id,
32 privacy: VideoPrivacy.PUBLIC
35 const res = await createLive(server.url, server.accessToken, liveAttributes)
36 return res.body.video.uuid
39 function updateConf (server: ServerInfo, vodProfile: string, liveProfile: string) {
40 return updateCustomSubConfig(server.url, server.accessToken, {
72 describe('Test transcoding plugins', function () {
73 let server: ServerInfo
75 before(async function () {
78 server = await flushAndRunServer(1)
79 await setAccessTokensToServers([ server ])
80 await setDefaultVideoChannel([ server ])
82 await updateConf(server, 'default', 'default')
85 describe('When using a plugin adding profiles to existing encoders', function () {
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))
92 for (const file of files) {
93 if (type === 'above') {
94 expect(file.fps).to.be.above(fps)
96 expect(file.fps).to.be.below(fps)
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)
105 if (type === 'above') {
106 expect(videoFPS).to.be.above(fps)
108 expect(videoFPS).to.be.below(fps)
112 before(async function () {
113 await installPlugin({
115 accessToken: server.accessToken,
116 path: getPluginTestPath('-transcoding-one')
120 it('Should have the appropriate available profiles', async function () {
121 const res = await getConfig(server.url)
122 const config = res.body as ServerConfig
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' ])
128 it('Should not use the plugin profile if not chosen by the admin', async function () {
131 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
132 await waitJobs([ server ])
134 await checkVideoFPS(videoUUID, 'above', 20)
137 it('Should use the vod profile', async function () {
140 await updateConf(server, 'low-vod', 'default')
142 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
143 await waitJobs([ server ])
145 await checkVideoFPS(videoUUID, 'below', 12)
148 it('Should apply input options in vod profile', async function () {
151 await updateConf(server, 'input-options-vod', 'default')
153 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
154 await waitJobs([ server ])
156 await checkVideoFPS(videoUUID, 'below', 6)
159 it('Should apply the scale filter in vod profile', async function () {
162 await updateConf(server, 'bad-scale-vod', 'default')
164 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
165 await waitJobs([ server ])
167 // Transcoding failed
168 const res = await getVideo(server.url, videoUUID)
169 const video: VideoDetails = res.body
171 expect(video.files).to.have.lengthOf(1)
172 expect(video.streamingPlaylists).to.have.lengthOf(0)
175 it('Should not use the plugin profile if not chosen by the admin', async function () {
178 const liveVideoId = await createLiveWrapper(server)
180 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
181 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
182 await waitJobs([ server ])
184 await checkLiveFPS(liveVideoId, 'above', 20)
187 it('Should use the live profile', async function () {
190 await updateConf(server, 'low-vod', 'low-live')
192 const liveVideoId = await createLiveWrapper(server)
194 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
195 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
196 await waitJobs([ server ])
198 await checkLiveFPS(liveVideoId, 'below', 12)
201 it('Should apply the input options on live profile', async function () {
204 await updateConf(server, 'low-vod', 'input-options-live')
206 const liveVideoId = await createLiveWrapper(server)
208 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
209 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
210 await waitJobs([ server ])
212 await checkLiveFPS(liveVideoId, 'below', 6)
215 it('Should apply the scale filter name on live profile', async function () {
218 await updateConf(server, 'low-vod', 'bad-scale-live')
220 const liveVideoId = await createLiveWrapper(server)
222 const command = await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
223 await testFfmpegStreamError(command, true)
226 it('Should default to the default profile if the specified profile does not exist', async function () {
229 await uninstallPlugin({ url: server.url, accessToken: server.accessToken, npmName: 'peertube-plugin-test-transcoding-one' })
231 const res = await getConfig(server.url)
232 const config = res.body as ServerConfig
234 expect(config.transcoding.availableProfiles).to.deep.equal([ 'default' ])
235 expect(config.live.transcoding.availableProfiles).to.deep.equal([ 'default' ])
237 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
238 await waitJobs([ server ])
240 await checkVideoFPS(videoUUID, 'above', 20)
245 describe('When using a plugin adding new encoders', function () {
247 before(async function () {
248 await installPlugin({
250 accessToken: server.accessToken,
251 path: getPluginTestPath('-transcoding-two')
254 await updateConf(server, 'test-vod-profile', 'test-live-profile')
257 it('Should use the new vod encoders', async function () {
260 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video', fixture: 'video_short_240p.mp4' })).uuid
261 await waitJobs([ server ])
263 const path = buildServerDirectory(server, join('videos', videoUUID + '-240.mp4'))
264 const audioProbe = await getAudioStream(path)
265 expect(audioProbe.audioStream.codec_name).to.equal('opus')
267 const videoProbe = await getVideoStreamFromFile(path)
268 expect(videoProbe.codec_name).to.equal('vp9')
271 it('Should use the new live encoders', async function () {
274 const liveVideoId = await createLiveWrapper(server)
276 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
277 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
278 await waitJobs([ server ])
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')
284 const videoProbe = await getVideoStreamFromFile(playlistUrl)
285 expect(videoProbe.codec_name).to.equal('h264')
289 after(async function () {
290 await cleanupTests([ server ])