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,
19 updateCustomSubConfig,
22 waitUntilLivePublished
23 } from '../../../shared/extra-utils'
24 import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../shared/extra-utils/server/servers'
26 async function createLiveWrapper (server: ServerInfo) {
27 const liveAttributes = {
29 channelId: server.videoChannel.id,
30 privacy: VideoPrivacy.PUBLIC
33 const res = await createLive(server.url, server.accessToken, liveAttributes)
34 return res.body.video.uuid
37 function updateConf (server: ServerInfo, vodProfile: string, liveProfile: string) {
38 return updateCustomSubConfig(server.url, server.accessToken, {
70 describe('Test transcoding plugins', function () {
71 let server: ServerInfo
73 before(async function () {
76 server = await flushAndRunServer(1)
77 await setAccessTokensToServers([ server ])
78 await setDefaultVideoChannel([ server ])
80 await updateConf(server, 'default', 'default')
83 describe('When using a plugin adding profiles to existing encoders', function () {
85 async function checkVideoFPS (uuid: string, type: 'above' | 'below', fps: number) {
86 const res = await getVideo(server.url, uuid)
87 const video = res.body as VideoDetails
88 const files = video.files.concat(...video.streamingPlaylists.map(p => p.files))
90 for (const file of files) {
91 if (type === 'above') {
92 expect(file.fps).to.be.above(fps)
94 expect(file.fps).to.be.below(fps)
99 async function checkLiveFPS (uuid: string, type: 'above' | 'below', fps: number) {
100 const playlistUrl = `${server.url}/static/streaming-playlists/hls/${uuid}/0.m3u8`
101 const videoFPS = await getVideoFileFPS(playlistUrl)
103 if (type === 'above') {
104 expect(videoFPS).to.be.above(fps)
106 expect(videoFPS).to.be.below(fps)
110 before(async function () {
111 await installPlugin({
113 accessToken: server.accessToken,
114 path: getPluginTestPath('-transcoding-one')
118 it('Should have the appropriate available profiles', async function () {
119 const res = await getConfig(server.url)
120 const config = res.body as ServerConfig
122 expect(config.transcoding.availableProfiles).to.have.members([ 'default', 'low-vod', 'video-filters-vod', 'input-options-vod' ])
123 expect(config.live.transcoding.availableProfiles).to.have.members([ 'default', 'low-live', 'input-options-live' ])
126 it('Should not use the plugin profile if not chosen by the admin', async function () {
129 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
130 await waitJobs([ server ])
132 await checkVideoFPS(videoUUID, 'above', 20)
135 it('Should use the vod profile', async function () {
138 await updateConf(server, 'low-vod', 'default')
140 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
141 await waitJobs([ server ])
143 await checkVideoFPS(videoUUID, 'below', 12)
146 it('Should apply video filters in vod profile', async function () {
149 await updateConf(server, 'video-filters-vod', 'default')
151 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
152 await waitJobs([ server ])
154 await checkVideoFPS(videoUUID, 'below', 12)
157 it('Should apply input options in vod profile', async function () {
160 await updateConf(server, 'input-options-vod', 'default')
162 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
163 await waitJobs([ server ])
165 await checkVideoFPS(videoUUID, 'below', 6)
168 it('Should not use the plugin profile if not chosen by the admin', async function () {
171 const liveVideoId = await createLiveWrapper(server)
173 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
174 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
175 await waitJobs([ server ])
177 await checkLiveFPS(liveVideoId, 'above', 20)
180 it('Should use the live profile', async function () {
183 await updateConf(server, 'low-vod', 'low-live')
185 const liveVideoId = await createLiveWrapper(server)
187 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
188 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
189 await waitJobs([ server ])
191 await checkLiveFPS(liveVideoId, 'below', 12)
194 it('Should apply the input options on live profile', async function () {
197 await updateConf(server, 'low-vod', 'input-options-live')
199 const liveVideoId = await createLiveWrapper(server)
201 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
202 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
203 await waitJobs([ server ])
205 await checkLiveFPS(liveVideoId, 'below', 6)
208 it('Should default to the default profile if the specified profile does not exist', async function () {
211 await uninstallPlugin({ url: server.url, accessToken: server.accessToken, npmName: 'peertube-plugin-test-transcoding-one' })
213 const res = await getConfig(server.url)
214 const config = res.body as ServerConfig
216 expect(config.transcoding.availableProfiles).to.deep.equal([ 'default' ])
217 expect(config.live.transcoding.availableProfiles).to.deep.equal([ 'default' ])
219 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
220 await waitJobs([ server ])
222 await checkVideoFPS(videoUUID, 'above', 20)
227 describe('When using a plugin adding new encoders', function () {
229 before(async function () {
230 await installPlugin({
232 accessToken: server.accessToken,
233 path: getPluginTestPath('-transcoding-two')
236 await updateConf(server, 'test-vod-profile', 'test-live-profile')
239 it('Should use the new vod encoders', async function () {
242 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video', fixture: 'video_short_240p.mp4' })).uuid
243 await waitJobs([ server ])
245 const path = buildServerDirectory(server, join('videos', videoUUID + '-240.mp4'))
246 const audioProbe = await getAudioStream(path)
247 expect(audioProbe.audioStream.codec_name).to.equal('opus')
249 const videoProbe = await getVideoStreamFromFile(path)
250 expect(videoProbe.codec_name).to.equal('vp9')
253 it('Should use the new live encoders', async function () {
256 const liveVideoId = await createLiveWrapper(server)
258 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
259 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
260 await waitJobs([ server ])
262 const playlistUrl = `${server.url}/static/streaming-playlists/hls/${liveVideoId}/0.m3u8`
263 const audioProbe = await getAudioStream(playlistUrl)
264 expect(audioProbe.audioStream.codec_name).to.equal('opus')
266 const videoProbe = await getVideoStreamFromFile(playlistUrl)
267 expect(videoProbe.codec_name).to.equal('h264')
271 after(async function () {
272 await cleanupTests([ server ])