1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { expect } from 'chai'
5 import { getAudioStream, getVideoFileFPS, getVideoStreamFromFile } from '@server/helpers/ffprobe-utils'
11 setAccessTokensToServers,
12 setDefaultVideoChannel,
13 testFfmpegStreamError,
15 } from '@shared/extra-utils'
16 import { VideoPrivacy } from '@shared/models'
18 async function createLiveWrapper (server: PeerTubeServer) {
19 const liveAttributes = {
21 channelId: server.store.channel.id,
22 privacy: VideoPrivacy.PUBLIC
25 const { uuid } = await server.live.create({ fields: liveAttributes })
30 function updateConf (server: PeerTubeServer, vodProfile: string, liveProfile: string) {
31 return server.config.updateCustomSubConfig({
65 describe('Test transcoding plugins', function () {
66 let server: PeerTubeServer
68 before(async function () {
71 server = await createSingleServer(1)
72 await setAccessTokensToServers([ server ])
73 await setDefaultVideoChannel([ server ])
75 await updateConf(server, 'default', 'default')
78 describe('When using a plugin adding profiles to existing encoders', function () {
80 async function checkVideoFPS (uuid: string, type: 'above' | 'below', fps: number) {
81 const video = await server.videos.get({ id: uuid })
82 const files = video.files.concat(...video.streamingPlaylists.map(p => p.files))
84 for (const file of files) {
85 if (type === 'above') {
86 expect(file.fps).to.be.above(fps)
88 expect(file.fps).to.be.below(fps)
93 async function checkLiveFPS (uuid: string, type: 'above' | 'below', fps: number) {
94 const playlistUrl = `${server.url}/static/streaming-playlists/hls/${uuid}/0.m3u8`
95 const videoFPS = await getVideoFileFPS(playlistUrl)
97 if (type === 'above') {
98 expect(videoFPS).to.be.above(fps)
100 expect(videoFPS).to.be.below(fps)
104 before(async function () {
105 await server.plugins.install({ path: PluginsCommand.getPluginTestPath('-transcoding-one') })
108 it('Should have the appropriate available profiles', async function () {
109 const config = await server.config.getConfig()
111 expect(config.transcoding.availableProfiles).to.have.members([ 'default', 'low-vod', 'input-options-vod', 'bad-scale-vod' ])
112 expect(config.live.transcoding.availableProfiles).to.have.members([ 'default', 'high-live', 'input-options-live', 'bad-scale-live' ])
115 describe('VOD', function () {
117 it('Should not use the plugin profile if not chosen by the admin', async function () {
120 const videoUUID = (await server.videos.quickUpload({ name: 'video' })).uuid
121 await waitJobs([ server ])
123 await checkVideoFPS(videoUUID, 'above', 20)
126 it('Should use the vod profile', async function () {
129 await updateConf(server, 'low-vod', 'default')
131 const videoUUID = (await server.videos.quickUpload({ name: 'video' })).uuid
132 await waitJobs([ server ])
134 await checkVideoFPS(videoUUID, 'below', 12)
137 it('Should apply input options in vod profile', async function () {
140 await updateConf(server, 'input-options-vod', 'default')
142 const videoUUID = (await server.videos.quickUpload({ name: 'video' })).uuid
143 await waitJobs([ server ])
145 await checkVideoFPS(videoUUID, 'below', 6)
148 it('Should apply the scale filter in vod profile', async function () {
151 await updateConf(server, 'bad-scale-vod', 'default')
153 const videoUUID = (await server.videos.quickUpload({ name: 'video' })).uuid
154 await waitJobs([ server ])
156 // Transcoding failed
157 const video = await server.videos.get({ id: videoUUID })
158 expect(video.files).to.have.lengthOf(1)
159 expect(video.streamingPlaylists).to.have.lengthOf(0)
163 describe('Live', function () {
165 it('Should not use the plugin profile if not chosen by the admin', async function () {
168 const liveVideoId = await createLiveWrapper(server)
170 await server.live.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_very_short_240p.mp4' })
171 await server.live.waitUntilPublished({ videoId: liveVideoId })
172 await waitJobs([ server ])
174 await checkLiveFPS(liveVideoId, 'above', 20)
177 it('Should use the live profile', async function () {
180 await updateConf(server, 'low-vod', 'high-live')
182 const liveVideoId = await createLiveWrapper(server)
184 await server.live.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_very_short_240p.mp4' })
185 await server.live.waitUntilPublished({ videoId: liveVideoId })
186 await waitJobs([ server ])
188 await checkLiveFPS(liveVideoId, 'above', 45)
191 it('Should apply the input options on live profile', async function () {
194 await updateConf(server, 'low-vod', 'input-options-live')
196 const liveVideoId = await createLiveWrapper(server)
198 await server.live.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_very_short_240p.mp4' })
199 await server.live.waitUntilPublished({ videoId: liveVideoId })
200 await waitJobs([ server ])
202 await checkLiveFPS(liveVideoId, 'above', 45)
205 it('Should apply the scale filter name on live profile', async function () {
208 await updateConf(server, 'low-vod', 'bad-scale-live')
210 const liveVideoId = await createLiveWrapper(server)
212 const command = await server.live.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_very_short_240p.mp4' })
213 await testFfmpegStreamError(command, true)
216 it('Should default to the default profile if the specified profile does not exist', async function () {
219 await server.plugins.uninstall({ npmName: 'peertube-plugin-test-transcoding-one' })
221 const config = await server.config.getConfig()
223 expect(config.transcoding.availableProfiles).to.deep.equal([ 'default' ])
224 expect(config.live.transcoding.availableProfiles).to.deep.equal([ 'default' ])
226 const videoUUID = (await server.videos.quickUpload({ name: 'video', fixture: 'video_very_short_240p.mp4' })).uuid
227 await waitJobs([ server ])
229 await checkVideoFPS(videoUUID, 'above', 20)
235 describe('When using a plugin adding new encoders', function () {
237 before(async function () {
238 await server.plugins.install({ path: PluginsCommand.getPluginTestPath('-transcoding-two') })
240 await updateConf(server, 'test-vod-profile', 'test-live-profile')
243 it('Should use the new vod encoders', async function () {
246 const videoUUID = (await server.videos.quickUpload({ name: 'video', fixture: 'video_very_short_240p.mp4' })).uuid
247 await waitJobs([ server ])
249 const video = await server.videos.get({ id: videoUUID })
251 const path = server.servers.buildWebTorrentFilePath(video.files[0].fileUrl)
252 const audioProbe = await getAudioStream(path)
253 expect(audioProbe.audioStream.codec_name).to.equal('opus')
255 const videoProbe = await getVideoStreamFromFile(path)
256 expect(videoProbe.codec_name).to.equal('vp9')
259 it('Should use the new live encoders', async function () {
262 const liveVideoId = await createLiveWrapper(server)
264 await server.live.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
265 await server.live.waitUntilPublished({ videoId: liveVideoId })
266 await waitJobs([ server ])
268 const playlistUrl = `${server.url}/static/streaming-playlists/hls/${liveVideoId}/0.m3u8`
269 const audioProbe = await getAudioStream(playlistUrl)
270 expect(audioProbe.audioStream.codec_name).to.equal('opus')
272 const videoProbe = await getVideoStreamFromFile(playlistUrl)
273 expect(videoProbe.codec_name).to.equal('h264')
277 after(async function () {
278 await cleanupTests([ server ])