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, {
58 describe('Test transcoding plugins', function () {
59 let server: ServerInfo
61 before(async function () {
64 server = await flushAndRunServer(1)
65 await setAccessTokensToServers([ server ])
66 await setDefaultVideoChannel([ server ])
68 await updateConf(server, 'default', 'default')
71 describe('When using a plugin adding profiles to existing encoders', function () {
73 async function checkVideoFPS (uuid: string, type: 'above' | 'below', fps: number) {
74 const res = await getVideo(server.url, uuid)
75 const video = res.body as VideoDetails
76 const files = video.files.concat(...video.streamingPlaylists.map(p => p.files))
78 for (const file of files) {
79 if (type === 'above') {
80 expect(file.fps).to.be.above(fps)
82 expect(file.fps).to.be.below(fps)
87 async function checkLiveFPS (uuid: string, type: 'above' | 'below', fps: number) {
88 const playlistUrl = `${server.url}/static/streaming-playlists/hls/${uuid}/0.m3u8`
89 const videoFPS = await getVideoFileFPS(playlistUrl)
91 if (type === 'above') {
92 expect(videoFPS).to.be.above(fps)
94 expect(videoFPS).to.be.below(fps)
98 before(async function () {
101 accessToken: server.accessToken,
102 path: getPluginTestPath('-transcoding-one')
106 it('Should have the appropriate available profiles', async function () {
107 const res = await getConfig(server.url)
108 const config = res.body as ServerConfig
110 expect(config.transcoding.availableProfiles).to.have.members([ 'default', 'low-vod' ])
111 expect(config.live.transcoding.availableProfiles).to.have.members([ 'default', 'low-live' ])
114 it('Should not use the plugin profile if not chosen by the admin', async function () {
117 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
118 await waitJobs([ server ])
120 await checkVideoFPS(videoUUID, 'above', 20)
123 it('Should use the vod profile', async function () {
126 await updateConf(server, 'low-vod', 'default')
128 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
129 await waitJobs([ server ])
131 await checkVideoFPS(videoUUID, 'below', 12)
134 it('Should not use the plugin profile if not chosen by the admin', async function () {
137 const liveVideoId = await createLiveWrapper(server)
139 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
140 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
141 await waitJobs([ server ])
143 await checkLiveFPS(liveVideoId, 'above', 20)
146 it('Should use the live profile', async function () {
149 await updateConf(server, 'low-vod', 'low-live')
151 const liveVideoId = await createLiveWrapper(server)
153 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
154 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
155 await waitJobs([ server ])
157 await checkLiveFPS(liveVideoId, 'below', 12)
160 it('Should default to the default profile if the specified profile does not exist', async function () {
163 await uninstallPlugin({ url: server.url, accessToken: server.accessToken, npmName: 'peertube-plugin-test-transcoding-one' })
165 const res = await getConfig(server.url)
166 const config = res.body as ServerConfig
168 expect(config.transcoding.availableProfiles).to.deep.equal([ 'default' ])
169 expect(config.live.transcoding.availableProfiles).to.deep.equal([ 'default' ])
171 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
172 await waitJobs([ server ])
174 await checkVideoFPS(videoUUID, 'above', 20)
179 describe('When using a plugin adding new encoders', function () {
181 before(async function () {
182 await installPlugin({
184 accessToken: server.accessToken,
185 path: getPluginTestPath('-transcoding-two')
188 await updateConf(server, 'test-vod-profile', 'test-live-profile')
191 it('Should use the new vod encoders', async function () {
194 const videoUUID = (await uploadVideoAndGetId({ server, videoName: 'video' })).uuid
195 await waitJobs([ server ])
197 const path = buildServerDirectory(server, join('videos', videoUUID + '-720.mp4'))
198 const audioProbe = await getAudioStream(path)
199 expect(audioProbe.audioStream.codec_name).to.equal('opus')
201 const videoProbe = await getVideoStreamFromFile(path)
202 expect(videoProbe.codec_name).to.equal('vp9')
205 it('Should use the new live encoders', async function () {
208 const liveVideoId = await createLiveWrapper(server)
210 await sendRTMPStreamInVideo(server.url, server.accessToken, liveVideoId, 'video_short2.webm')
211 await waitUntilLivePublished(server.url, server.accessToken, liveVideoId)
212 await waitJobs([ server ])
214 const playlistUrl = `${server.url}/static/streaming-playlists/hls/${liveVideoId}/0.m3u8`
215 const audioProbe = await getAudioStream(playlistUrl)
216 expect(audioProbe.audioStream.codec_name).to.equal('opus')
218 const videoProbe = await getVideoStreamFromFile(playlistUrl)
219 expect(videoProbe.codec_name).to.equal('h264')
223 after(async function () {
224 await cleanupTests([ server ])