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,
23 waitUntilLivePublished
24 } from '../../../shared/extra-utils'
25 import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../shared/extra-utils/server/servers'
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 installPlugin({
114 accessToken: server.accessToken,
115 path: getPluginTestPath('-transcoding-one')
119 it('Should have the appropriate available profiles', async function () {
120 const res = await getConfig(server.url)
121 const config = res.body as ServerConfig
123 expect(config.transcoding.availableProfiles).to.have.members([ 'default', 'low-vod', 'input-options-vod', 'bad-scale-vod' ])
124 expect(config.live.transcoding.availableProfiles).to.have.members([ 'default', 'low-live', 'input-options-live', 'bad-scale-live' ])
127 it('Should not use the plugin profile if not chosen by the admin', async function () {
130 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
131 await waitJobs([ server ])
133 await checkVideoFPS(videoUUID, 'above', 20)
136 it('Should use the vod profile', async function () {
139 await updateConf(server, 'low-vod', 'default')
141 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
142 await waitJobs([ server ])
144 await checkVideoFPS(videoUUID, 'below', 12)
147 it('Should apply input options in vod profile', async function () {
150 await updateConf(server, 'input-options-vod', 'default')
152 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
153 await waitJobs([ server ])
155 await checkVideoFPS(videoUUID, 'below', 6)
158 it('Should apply the scale filter in vod profile', async function () {
161 await updateConf(server, 'bad-scale-vod', 'default')
163 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
164 await waitJobs([ server ])
166 // Transcoding failed
167 const res = await getVideo(server.url, videoUUID)
168 const video: VideoDetails = res.body
170 expect(video.files).to.have.lengthOf(1)
171 expect(video.streamingPlaylists).to.have.lengthOf(0)
174 it('Should not use the plugin profile if not chosen by the admin', async function () {
177 const liveVideoId = await createLiveWrapper(server)
179 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
180 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
181 await waitJobs([ server ])
183 await checkLiveFPS(liveVideoId, 'above', 20)
186 it('Should use the live profile', async function () {
189 await updateConf(server, 'low-vod', 'low-live')
191 const liveVideoId = await createLiveWrapper(server)
193 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
194 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
195 await waitJobs([ server ])
197 await checkLiveFPS(liveVideoId, 'below', 12)
200 it('Should apply the input options on live profile', async function () {
203 await updateConf(server, 'low-vod', 'input-options-live')
205 const liveVideoId = await createLiveWrapper(server)
207 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
208 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
209 await waitJobs([ server ])
211 await checkLiveFPS(liveVideoId, 'below', 6)
214 it('Should apply the scale filter name on live profile', async function () {
217 await updateConf(server, 'low-vod', 'bad-scale-live')
219 const liveVideoId = await createLiveWrapper(server)
221 const command = await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
222 await testFfmpegStreamError(command, true)
225 it('Should default to the default profile if the specified profile does not exist', async function () {
228 await uninstallPlugin({ url: server.url, accessToken: server.accessToken, npmName: 'peertube-plugin-test-transcoding-one' })
230 const res = await getConfig(server.url)
231 const config = res.body as ServerConfig
233 expect(config.transcoding.availableProfiles).to.deep.equal([ 'default' ])
234 expect(config.live.transcoding.availableProfiles).to.deep.equal([ 'default' ])
236 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
237 await waitJobs([ server ])
239 await checkVideoFPS(videoUUID, 'above', 20)
244 describe('When using a plugin adding new encoders', function () {
246 before(async function () {
247 await installPlugin({
249 accessToken: server.accessToken,
250 path: getPluginTestPath('-transcoding-two')
253 await updateConf(server, 'test-vod-profile', 'test-live-profile')
256 it('Should use the new vod encoders', async function () {
259 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video', fixture: 'video_short_240p.mp4' })).uuid
260 await waitJobs([ server ])
262 const path = buildServerDirectory(server, join('videos', videoUUID + '-240.mp4'))
263 const audioProbe = await getAudioStream(path)
264 expect(audioProbe.audioStream.codec_name).to.equal('opus')
266 const videoProbe = await getVideoStreamFromFile(path)
267 expect(videoProbe.codec_name).to.equal('vp9')
270 it('Should use the new live encoders', async function () {
273 const liveVideoId = await createLiveWrapper(server)
275 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
276 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
277 await waitJobs([ server ])
279 const playlistUrl = `${server.url}/static/streaming-playlists/hls/${liveVideoId}/0.m3u8`
280 const audioProbe = await getAudioStream(playlistUrl)
281 expect(audioProbe.audioStream.codec_name).to.equal('opus')
283 const videoProbe = await getVideoStreamFromFile(playlistUrl)
284 expect(videoProbe.codec_name).to.equal('h264')
288 after(async function () {
289 await cleanupTests([ server ])