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'
12 setAccessTokensToServers,
13 setDefaultVideoChannel,
14 testFfmpegStreamError,
16 } from '@shared/extra-utils'
17 import { VideoPrivacy } from '@shared/models'
19 async function createLiveWrapper (server: PeerTubeServer) {
20 const liveAttributes = {
22 channelId: server.store.channel.id,
23 privacy: VideoPrivacy.PUBLIC
26 const { uuid } = await server.live.create({ fields: liveAttributes })
31 function updateConf (server: PeerTubeServer, vodProfile: string, liveProfile: string) {
32 return server.config.updateCustomSubConfig({
66 describe('Test transcoding plugins', function () {
67 let server: PeerTubeServer
69 before(async function () {
72 server = await createSingleServer(1)
73 await setAccessTokensToServers([ server ])
74 await setDefaultVideoChannel([ server ])
76 await updateConf(server, 'default', 'default')
79 describe('When using a plugin adding profiles to existing encoders', function () {
81 async function checkVideoFPS (uuid: string, type: 'above' | 'below', fps: number) {
82 const video = await server.videos.get({ id: uuid })
83 const files = video.files.concat(...video.streamingPlaylists.map(p => p.files))
85 for (const file of files) {
86 if (type === 'above') {
87 expect(file.fps).to.be.above(fps)
89 expect(file.fps).to.be.below(fps)
94 async function checkLiveFPS (uuid: string, type: 'above' | 'below', fps: number) {
95 const playlistUrl = `${server.url}/static/streaming-playlists/hls/${uuid}/0.m3u8`
96 const videoFPS = await getVideoFileFPS(playlistUrl)
98 if (type === 'above') {
99 expect(videoFPS).to.be.above(fps)
101 expect(videoFPS).to.be.below(fps)
105 before(async function () {
106 await server.plugins.install({ path: PluginsCommand.getPluginTestPath('-transcoding-one') })
109 it('Should have the appropriate available profiles', async function () {
110 const config = await server.config.getConfig()
112 expect(config.transcoding.availableProfiles).to.have.members([ 'default', 'low-vod', 'input-options-vod', 'bad-scale-vod' ])
113 expect(config.live.transcoding.availableProfiles).to.have.members([ 'default', 'high-live', 'input-options-live', 'bad-scale-live' ])
116 describe('VOD', function () {
118 it('Should not use the plugin profile if not chosen by the admin', async function () {
121 const videoUUID = (await server.videos.quickUpload({ name: 'video' })).uuid
122 await waitJobs([ server ])
124 await checkVideoFPS(videoUUID, 'above', 20)
127 it('Should use the vod profile', async function () {
130 await updateConf(server, 'low-vod', 'default')
132 const videoUUID = (await server.videos.quickUpload({ name: 'video' })).uuid
133 await waitJobs([ server ])
135 await checkVideoFPS(videoUUID, 'below', 12)
138 it('Should apply input options in vod profile', async function () {
141 await updateConf(server, 'input-options-vod', 'default')
143 const videoUUID = (await server.videos.quickUpload({ name: 'video' })).uuid
144 await waitJobs([ server ])
146 await checkVideoFPS(videoUUID, 'below', 6)
149 it('Should apply the scale filter in vod profile', async function () {
152 await updateConf(server, 'bad-scale-vod', 'default')
154 const videoUUID = (await server.videos.quickUpload({ name: 'video' })).uuid
155 await waitJobs([ server ])
157 // Transcoding failed
158 const video = await server.videos.get({ id: videoUUID })
159 expect(video.files).to.have.lengthOf(1)
160 expect(video.streamingPlaylists).to.have.lengthOf(0)
164 describe('Live', function () {
166 it('Should not use the plugin profile if not chosen by the admin', async function () {
169 const liveVideoId = await createLiveWrapper(server)
171 await server.live.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_very_short_240p.mp4' })
172 await server.live.waitUntilPublished({ videoId: liveVideoId })
173 await waitJobs([ server ])
175 await checkLiveFPS(liveVideoId, 'above', 20)
178 it('Should use the live profile', async function () {
181 await updateConf(server, 'low-vod', 'high-live')
183 const liveVideoId = await createLiveWrapper(server)
185 await server.live.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_very_short_240p.mp4' })
186 await server.live.waitUntilPublished({ videoId: liveVideoId })
187 await waitJobs([ server ])
189 await checkLiveFPS(liveVideoId, 'above', 45)
192 it('Should apply the input options on live profile', async function () {
195 await updateConf(server, 'low-vod', 'input-options-live')
197 const liveVideoId = await createLiveWrapper(server)
199 await server.live.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_very_short_240p.mp4' })
200 await server.live.waitUntilPublished({ videoId: liveVideoId })
201 await waitJobs([ server ])
203 await checkLiveFPS(liveVideoId, 'above', 45)
206 it('Should apply the scale filter name on live profile', async function () {
209 await updateConf(server, 'low-vod', 'bad-scale-live')
211 const liveVideoId = await createLiveWrapper(server)
213 const command = await server.live.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_very_short_240p.mp4' })
214 await testFfmpegStreamError(command, true)
217 it('Should default to the default profile if the specified profile does not exist', async function () {
220 await server.plugins.uninstall({ npmName: 'peertube-plugin-test-transcoding-one' })
222 const config = await server.config.getConfig()
224 expect(config.transcoding.availableProfiles).to.deep.equal([ 'default' ])
225 expect(config.live.transcoding.availableProfiles).to.deep.equal([ 'default' ])
227 const videoUUID = (await server.videos.quickUpload({ name: 'video', fixture: 'video_very_short_240p.mp4' })).uuid
228 await waitJobs([ server ])
230 await checkVideoFPS(videoUUID, 'above', 20)
236 describe('When using a plugin adding new encoders', function () {
238 before(async function () {
239 await server.plugins.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 server.videos.quickUpload({ name: 'video', fixture: 'video_very_short_240p.mp4' })).uuid
248 await waitJobs([ server ])
250 const path = server.servers.buildDirectory(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.live.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
264 await server.live.waitUntilPublished({ 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 ])