aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/plugins/plugin-transcoding.ts
blob: ce1047388383176290e3133bfeaee3ebb898f418 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import { expect } from 'chai'
import { getAudioStream, getVideoStreamFPS, getVideoStream } from '@server/helpers/ffmpeg'
import { VideoPrivacy } from '@shared/models'
import {
  cleanupTests,
  createSingleServer,
  PeerTubeServer,
  PluginsCommand,
  setAccessTokensToServers,
  setDefaultVideoChannel,
  testFfmpegStreamError,
  waitJobs
} from '@shared/server-commands'

async function createLiveWrapper (server: PeerTubeServer) {
  const liveAttributes = {
    name: 'live video',
    channelId: server.store.channel.id,
    privacy: VideoPrivacy.PUBLIC
  }

  const { uuid } = await server.live.create({ fields: liveAttributes })

  return uuid
}

function updateConf (server: PeerTubeServer, vodProfile: string, liveProfile: string) {
  return server.config.updateCustomSubConfig({
    newConfig: {
      transcoding: {
        enabled: true,
        profile: vodProfile,
        hls: {
          enabled: true
        },
        webtorrent: {
          enabled: true
        },
        resolutions: {
          '240p': true,
          '360p': false,
          '480p': false,
          '720p': true
        }
      },
      live: {
        transcoding: {
          profile: liveProfile,
          enabled: true,
          resolutions: {
            '240p': true,
            '360p': false,
            '480p': false,
            '720p': true
          }
        }
      }
    }
  })
}

describe('Test transcoding plugins', function () {
  let server: PeerTubeServer

  before(async function () {
    this.timeout(60000)

    server = await createSingleServer(1)
    await setAccessTokensToServers([ server ])
    await setDefaultVideoChannel([ server ])

    await updateConf(server, 'default', 'default')
  })

  describe('When using a plugin adding profiles to existing encoders', function () {

    async function checkVideoFPS (uuid: string, type: 'above' | 'below', fps: number) {
      const video = await server.videos.get({ id: uuid })
      const files = video.files.concat(...video.streamingPlaylists.map(p => p.files))

      for (const file of files) {
        if (type === 'above') {
          expect(file.fps).to.be.above(fps)
        } else {
          expect(file.fps).to.be.below(fps)
        }
      }
    }

    async function checkLiveFPS (uuid: string, type: 'above' | 'below', fps: number) {
      const playlistUrl = `${server.url}/static/streaming-playlists/hls/${uuid}/0.m3u8`
      const videoFPS = await getVideoStreamFPS(playlistUrl)

      if (type === 'above') {
        expect(videoFPS).to.be.above(fps)
      } else {
        expect(videoFPS).to.be.below(fps)
      }
    }

    before(async function () {
      await server.plugins.install({ path: PluginsCommand.getPluginTestPath('-transcoding-one') })
    })

    it('Should have the appropriate available profiles', async function () {
      const config = await server.config.getConfig()

      expect(config.transcoding.availableProfiles).to.have.members([ 'default', 'low-vod', 'input-options-vod', 'bad-scale-vod' ])
      expect(config.live.transcoding.availableProfiles).to.have.members([ 'default', 'high-live', 'input-options-live', 'bad-scale-live' ])
    })

    describe('VOD', function () {

      it('Should not use the plugin profile if not chosen by the admin', async function () {
        this.timeout(240000)

        const videoUUID = (await server.videos.quickUpload({ name: 'video' })).uuid
        await waitJobs([ server ])

        await checkVideoFPS(videoUUID, 'above', 20)
      })

      it('Should use the vod profile', async function () {
        this.timeout(240000)

        await updateConf(server, 'low-vod', 'default')

        const videoUUID = (await server.videos.quickUpload({ name: 'video' })).uuid
        await waitJobs([ server ])

        await checkVideoFPS(videoUUID, 'below', 12)
      })

      it('Should apply input options in vod profile', async function () {
        this.timeout(240000)

        await updateConf(server, 'input-options-vod', 'default')

        const videoUUID = (await server.videos.quickUpload({ name: 'video' })).uuid
        await waitJobs([ server ])

        await checkVideoFPS(videoUUID, 'below', 6)
      })

      it('Should apply the scale filter in vod profile', async function () {
        this.timeout(240000)

        await updateConf(server, 'bad-scale-vod', 'default')

        const videoUUID = (await server.videos.quickUpload({ name: 'video' })).uuid
        await waitJobs([ server ])

        // Transcoding failed
        const video = await server.videos.get({ id: videoUUID })
        expect(video.files).to.have.lengthOf(1)
        expect(video.streamingPlaylists).to.have.lengthOf(0)
      })
    })

    describe('Live', function () {

      it('Should not use the plugin profile if not chosen by the admin', async function () {
        this.timeout(240000)

        const liveVideoId = await createLiveWrapper(server)

        await server.live.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_very_short_240p.mp4' })
        await server.live.waitUntilPublished({ videoId: liveVideoId })
        await waitJobs([ server ])

        await checkLiveFPS(liveVideoId, 'above', 20)
      })

      it('Should use the live profile', async function () {
        this.timeout(240000)

        await updateConf(server, 'low-vod', 'high-live')

        const liveVideoId = await createLiveWrapper(server)

        await server.live.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_very_short_240p.mp4' })
        await server.live.waitUntilPublished({ videoId: liveVideoId })
        await waitJobs([ server ])

        await checkLiveFPS(liveVideoId, 'above', 45)
      })

      it('Should apply the input options on live profile', async function () {
        this.timeout(240000)

        await updateConf(server, 'low-vod', 'input-options-live')

        const liveVideoId = await createLiveWrapper(server)

        await server.live.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_very_short_240p.mp4' })
        await server.live.waitUntilPublished({ videoId: liveVideoId })
        await waitJobs([ server ])

        await checkLiveFPS(liveVideoId, 'above', 45)
      })

      it('Should apply the scale filter name on live profile', async function () {
        this.timeout(240000)

        await updateConf(server, 'low-vod', 'bad-scale-live')

        const liveVideoId = await createLiveWrapper(server)

        const command = await server.live.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_very_short_240p.mp4' })
        await testFfmpegStreamError(command, true)
      })

      it('Should default to the default profile if the specified profile does not exist', async function () {
        this.timeout(240000)

        await server.plugins.uninstall({ npmName: 'peertube-plugin-test-transcoding-one' })

        const config = await server.config.getConfig()

        expect(config.transcoding.availableProfiles).to.deep.equal([ 'default' ])
        expect(config.live.transcoding.availableProfiles).to.deep.equal([ 'default' ])

        const videoUUID = (await server.videos.quickUpload({ name: 'video', fixture: 'video_very_short_240p.mp4' })).uuid
        await waitJobs([ server ])

        await checkVideoFPS(videoUUID, 'above', 20)
      })
    })

  })

  describe('When using a plugin adding new encoders', function () {

    before(async function () {
      await server.plugins.install({ path: PluginsCommand.getPluginTestPath('-transcoding-two') })

      await updateConf(server, 'test-vod-profile', 'test-live-profile')
    })

    it('Should use the new vod encoders', async function () {
      this.timeout(240000)

      const videoUUID = (await server.videos.quickUpload({ name: 'video', fixture: 'video_very_short_240p.mp4' })).uuid
      await waitJobs([ server ])

      const video = await server.videos.get({ id: videoUUID })

      const path = server.servers.buildWebTorrentFilePath(video.files[0].fileUrl)
      const audioProbe = await getAudioStream(path)
      expect(audioProbe.audioStream.codec_name).to.equal('opus')

      const videoProbe = await getVideoStream(path)
      expect(videoProbe.codec_name).to.equal('vp9')
    })

    it('Should use the new live encoders', async function () {
      this.timeout(240000)

      const liveVideoId = await createLiveWrapper(server)

      await server.live.sendRTMPStreamInVideo({ videoId: liveVideoId, fixtureName: 'video_short2.webm' })
      await server.live.waitUntilPublished({ videoId: liveVideoId })
      await waitJobs([ server ])

      const playlistUrl = `${server.url}/static/streaming-playlists/hls/${liveVideoId}/0.m3u8`
      const audioProbe = await getAudioStream(playlistUrl)
      expect(audioProbe.audioStream.codec_name).to.equal('opus')

      const videoProbe = await getVideoStream(playlistUrl)
      expect(videoProbe.codec_name).to.equal('h264')
    })
  })

  after(async function () {
    await cleanupTests([ server ])
  })
})