aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/runners/runner-vod-transcoding.ts
blob: d9da0f40deb725e45d13e4e1a2b1dfefc9d47adc (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import { expect } from 'chai'
import { readFile } from 'fs-extra'
import { completeCheckHlsPlaylist } from '@server/tests/shared'
import { buildAbsoluteFixturePath } from '@shared/core-utils'
import {
  HttpStatusCode,
  RunnerJobSuccessPayload,
  RunnerJobVODAudioMergeTranscodingPayload,
  RunnerJobVODHLSTranscodingPayload,
  RunnerJobVODPayload,
  RunnerJobVODWebVideoTranscodingPayload,
  VideoState,
  VODAudioMergeTranscodingSuccess,
  VODHLSTranscodingSuccess,
  VODWebVideoTranscodingSuccess
} from '@shared/models'
import {
  cleanupTests,
  createMultipleServers,
  doubleFollow,
  makeGetRequest,
  makeRawRequest,
  PeerTubeServer,
  setAccessTokensToServers,
  setDefaultVideoChannel,
  waitJobs
} from '@shared/server-commands'

async function processAllJobs (server: PeerTubeServer, runnerToken: string) {
  do {
    const { availableJobs } = await server.runnerJobs.requestVOD({ runnerToken })
    if (availableJobs.length === 0) break

    const { job } = await server.runnerJobs.accept<RunnerJobVODPayload>({ runnerToken, jobUUID: availableJobs[0].uuid })

    const payload: RunnerJobSuccessPayload = {
      videoFile: `video_short_${job.payload.output.resolution}p.mp4`,
      resolutionPlaylistFile: `video_short_${job.payload.output.resolution}p.m3u8`
    }
    await server.runnerJobs.success({ runnerToken, jobUUID: job.uuid, jobToken: job.jobToken, payload })
  } while (true)

  await waitJobs([ server ])
}

describe('Test runner VOD transcoding', function () {
  let servers: PeerTubeServer[] = []
  let runnerToken: string

  before(async function () {
    this.timeout(120_000)

    servers = await createMultipleServers(2)

    await setAccessTokensToServers(servers)
    await setDefaultVideoChannel(servers)

    await doubleFollow(servers[0], servers[1])

    await servers[0].config.enableRemoteTranscoding()
    runnerToken = await servers[0].runners.autoRegisterRunner()
  })

  describe('Without transcoding', function () {

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

      await servers[0].config.disableTranscoding()
      await servers[0].videos.quickUpload({ name: 'video' })

      await waitJobs(servers)
    })

    it('Should not have available jobs', async function () {
      const { availableJobs } = await servers[0].runnerJobs.requestVOD({ runnerToken })
      expect(availableJobs).to.have.lengthOf(0)
    })
  })

  describe('With classic transcoding enabled', function () {

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

      await servers[0].config.enableTranscoding(true, true)
    })

    it('Should error a transcoding job', async function () {
      this.timeout(60000)

      await servers[0].runnerJobs.cancelAllJobs()
      const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
      await waitJobs(servers)

      const { availableJobs } = await servers[0].runnerJobs.request({ runnerToken })
      const jobUUID = availableJobs[0].uuid

      for (let i = 0; i < 5; i++) {
        const { job } = await servers[0].runnerJobs.accept({ runnerToken, jobUUID })
        const jobToken = job.jobToken

        await servers[0].runnerJobs.error({ runnerToken, jobUUID, jobToken, message: 'Error' })
      }

      const video = await servers[0].videos.get({ id: uuid })
      expect(video.state.id).to.equal(VideoState.TRANSCODING_FAILED)
    })

    it('Should cancel a transcoding job', async function () {
      await servers[0].runnerJobs.cancelAllJobs()
      const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
      await waitJobs(servers)

      const { availableJobs } = await servers[0].runnerJobs.request({ runnerToken })
      const jobUUID = availableJobs[0].uuid

      await servers[0].runnerJobs.cancelByAdmin({ jobUUID })

      const video = await servers[0].videos.get({ id: uuid })
      expect(video.state.id).to.equal(VideoState.PUBLISHED)
    })
  })

  describe('Web video transcoding only', function () {
    let videoUUID: string
    let jobToken: string
    let jobUUID: string

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

      await servers[0].runnerJobs.cancelAllJobs()
      await servers[0].config.enableTranscoding(true, false)

      const { uuid } = await servers[0].videos.quickUpload({ name: 'web video', fixture: 'video_short.webm' })
      videoUUID = uuid

      await waitJobs(servers)
    })

    it('Should have jobs available for remote runners', async function () {
      const { availableJobs } = await servers[0].runnerJobs.requestVOD({ runnerToken })
      expect(availableJobs).to.have.lengthOf(1)

      jobUUID = availableJobs[0].uuid
    })

    it('Should have a valid first transcoding job', async function () {
      const { job } = await servers[0].runnerJobs.accept<RunnerJobVODWebVideoTranscodingPayload>({ runnerToken, jobUUID })
      jobToken = job.jobToken

      expect(job.type === 'vod-web-video-transcoding')
      expect(job.payload.input.videoFileUrl).to.exist
      expect(job.payload.output.resolution).to.equal(720)
      expect(job.payload.output.fps).to.equal(25)

      const { body } = await servers[0].runnerJobs.getJobFile({ url: job.payload.input.videoFileUrl, jobToken, runnerToken })
      const inputFile = await readFile(buildAbsoluteFixturePath('video_short.webm'))

      expect(body).to.deep.equal(inputFile)
    })

    it('Should transcode the max video resolution and send it back to the server', async function () {
      this.timeout(60000)

      const payload: VODWebVideoTranscodingSuccess = {
        videoFile: 'video_short.mp4'
      }
      await servers[0].runnerJobs.success({ runnerToken, jobUUID, jobToken, payload })

      await waitJobs(servers)
    })

    it('Should have the video updated', async function () {
      for (const server of servers) {
        const video = await server.videos.get({ id: videoUUID })
        expect(video.files).to.have.lengthOf(1)
        expect(video.streamingPlaylists).to.have.lengthOf(0)

        const { body } = await makeRawRequest({ url: video.files[0].fileUrl, expectedStatus: HttpStatusCode.OK_200 })
        expect(body).to.deep.equal(await readFile(buildAbsoluteFixturePath('video_short.mp4')))
      }
    })

    it('Should have 4 lower resolution to transcode', async function () {
      const { availableJobs } = await servers[0].runnerJobs.requestVOD({ runnerToken })
      expect(availableJobs).to.have.lengthOf(4)

      for (const resolution of [ 480, 360, 240, 144 ]) {
        const job = availableJobs.find(j => j.payload.output.resolution === resolution)
        expect(job).to.exist
        expect(job.type).to.equal('vod-web-video-transcoding')

        if (resolution === 240) jobUUID = job.uuid
      }
    })

    it('Should process one of these transcoding jobs', async function () {
      const { job } = await servers[0].runnerJobs.accept<RunnerJobVODWebVideoTranscodingPayload>({ runnerToken, jobUUID })
      jobToken = job.jobToken

      const { body } = await servers[0].runnerJobs.getJobFile({ url: job.payload.input.videoFileUrl, jobToken, runnerToken })
      const inputFile = await readFile(buildAbsoluteFixturePath('video_short.mp4'))

      expect(body).to.deep.equal(inputFile)

      const payload: VODWebVideoTranscodingSuccess = { videoFile: `video_short_${job.payload.output.resolution}p.mp4` }
      await servers[0].runnerJobs.success({ runnerToken, jobUUID, jobToken, payload })
    })

    it('Should process all other jobs', async function () {
      const { availableJobs } = await servers[0].runnerJobs.requestVOD({ runnerToken })
      expect(availableJobs).to.have.lengthOf(3)

      for (const resolution of [ 480, 360, 144 ]) {
        const availableJob = availableJobs.find(j => j.payload.output.resolution === resolution)
        expect(availableJob).to.exist
        jobUUID = availableJob.uuid

        const { job } = await servers[0].runnerJobs.accept<RunnerJobVODWebVideoTranscodingPayload>({ runnerToken, jobUUID })
        jobToken = job.jobToken

        const { body } = await servers[0].runnerJobs.getJobFile({ url: job.payload.input.videoFileUrl, jobToken, runnerToken })
        const inputFile = await readFile(buildAbsoluteFixturePath('video_short.mp4'))
        expect(body).to.deep.equal(inputFile)

        const payload: VODWebVideoTranscodingSuccess = { videoFile: `video_short_${resolution}p.mp4` }
        await servers[0].runnerJobs.success({ runnerToken, jobUUID, jobToken, payload })
      }

      await waitJobs(servers)
    })

    it('Should have the video updated', async function () {
      for (const server of servers) {
        const video = await server.videos.get({ id: videoUUID })
        expect(video.files).to.have.lengthOf(5)
        expect(video.streamingPlaylists).to.have.lengthOf(0)

        const { body } = await makeRawRequest({ url: video.files[0].fileUrl, expectedStatus: HttpStatusCode.OK_200 })
        expect(body).to.deep.equal(await readFile(buildAbsoluteFixturePath('video_short.mp4')))

        for (const file of video.files) {
          await makeRawRequest({ url: file.fileUrl, expectedStatus: HttpStatusCode.OK_200 })
          await makeRawRequest({ url: file.torrentUrl, expectedStatus: HttpStatusCode.OK_200 })
        }
      }
    })

    it('Should not have available jobs anymore', async function () {
      const { availableJobs } = await servers[0].runnerJobs.requestVOD({ runnerToken })
      expect(availableJobs).to.have.lengthOf(0)
    })
  })

  describe('HLS transcoding only', function () {
    let videoUUID: string
    let jobToken: string
    let jobUUID: string

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

      await servers[0].config.enableTranscoding(false, true)

      const { uuid } = await servers[0].videos.quickUpload({ name: 'hls video', fixture: 'video_short.webm' })
      videoUUID = uuid

      await waitJobs(servers)
    })

    it('Should run the optimize job', async function () {
      this.timeout(60000)

      await servers[0].runnerJobs.autoProcessWebVideoJob(runnerToken)
    })

    it('Should have 5 HLS resolution to transcode', async function () {
      const { availableJobs } = await servers[0].runnerJobs.requestVOD({ runnerToken })
      expect(availableJobs).to.have.lengthOf(5)

      for (const resolution of [ 720, 480, 360, 240, 144 ]) {
        const job = availableJobs.find(j => j.payload.output.resolution === resolution)
        expect(job).to.exist
        expect(job.type).to.equal('vod-hls-transcoding')

        if (resolution === 480) jobUUID = job.uuid
      }
    })

    it('Should process one of these transcoding jobs', async function () {
      this.timeout(60000)

      const { job } = await servers[0].runnerJobs.accept<RunnerJobVODHLSTranscodingPayload>({ runnerToken, jobUUID })
      jobToken = job.jobToken

      const { body } = await servers[0].runnerJobs.getJobFile({ url: job.payload.input.videoFileUrl, jobToken, runnerToken })
      const inputFile = await readFile(buildAbsoluteFixturePath('video_short.mp4'))

      expect(body).to.deep.equal(inputFile)

      const payload: VODHLSTranscodingSuccess = {
        videoFile: 'video_short_480p.mp4',
        resolutionPlaylistFile: 'video_short_480p.m3u8'
      }
      await servers[0].runnerJobs.success({ runnerToken, jobUUID, jobToken, payload })

      await waitJobs(servers)
    })

    it('Should have the video updated', async function () {
      for (const server of servers) {
        const video = await server.videos.get({ id: videoUUID })

        expect(video.files).to.have.lengthOf(1)
        expect(video.streamingPlaylists).to.have.lengthOf(1)

        const hls = video.streamingPlaylists[0]
        expect(hls.files).to.have.lengthOf(1)

        await completeCheckHlsPlaylist({ videoUUID, hlsOnly: false, servers, resolutions: [ 480 ] })
      }
    })

    it('Should process all other jobs', async function () {
      this.timeout(60000)

      const { availableJobs } = await servers[0].runnerJobs.requestVOD({ runnerToken })
      expect(availableJobs).to.have.lengthOf(4)

      let maxQualityFile = 'video_short.mp4'

      for (const resolution of [ 720, 360, 240, 144 ]) {
        const availableJob = availableJobs.find(j => j.payload.output.resolution === resolution)
        expect(availableJob).to.exist
        jobUUID = availableJob.uuid

        const { job } = await servers[0].runnerJobs.accept<RunnerJobVODHLSTranscodingPayload>({ runnerToken, jobUUID })
        jobToken = job.jobToken

        const { body } = await servers[0].runnerJobs.getJobFile({ url: job.payload.input.videoFileUrl, jobToken, runnerToken })
        const inputFile = await readFile(buildAbsoluteFixturePath(maxQualityFile))
        expect(body).to.deep.equal(inputFile)

        const payload: VODHLSTranscodingSuccess = {
          videoFile: `video_short_${resolution}p.mp4`,
          resolutionPlaylistFile: `video_short_${resolution}p.m3u8`
        }
        await servers[0].runnerJobs.success({ runnerToken, jobUUID, jobToken, payload })

        if (resolution === 720) {
          maxQualityFile = 'video_short_720p.mp4'
        }
      }

      await waitJobs(servers)
    })

    it('Should have the video updated', async function () {
      for (const server of servers) {
        const video = await server.videos.get({ id: videoUUID })

        expect(video.files).to.have.lengthOf(0)
        expect(video.streamingPlaylists).to.have.lengthOf(1)

        const hls = video.streamingPlaylists[0]
        expect(hls.files).to.have.lengthOf(5)

        await completeCheckHlsPlaylist({ videoUUID, hlsOnly: true, servers, resolutions: [ 720, 480, 360, 240, 144 ] })
      }
    })

    it('Should not have available jobs anymore', async function () {
      const { availableJobs } = await servers[0].runnerJobs.requestVOD({ runnerToken })
      expect(availableJobs).to.have.lengthOf(0)
    })
  })

  describe('Web video and HLS transcoding', function () {

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

      await servers[0].config.enableTranscoding(true, true)

      await servers[0].videos.quickUpload({ name: 'web video and hls video', fixture: 'video_short.webm' })

      await waitJobs(servers)
    })

    it('Should process the first optimize job', async function () {
      this.timeout(60000)

      await servers[0].runnerJobs.autoProcessWebVideoJob(runnerToken)
    })

    it('Should have 9 jobs to process', async function () {
      const { availableJobs } = await servers[0].runnerJobs.requestVOD({ runnerToken })

      expect(availableJobs).to.have.lengthOf(9)

      const webVideoJobs = availableJobs.filter(j => j.type === 'vod-web-video-transcoding')
      const hlsJobs = availableJobs.filter(j => j.type === 'vod-hls-transcoding')

      expect(webVideoJobs).to.have.lengthOf(4)
      expect(hlsJobs).to.have.lengthOf(5)
    })

    it('Should process all available jobs', async function () {
      await processAllJobs(servers[0], runnerToken)
    })
  })

  describe('Audio merge transcoding', function () {
    let videoUUID: string
    let jobToken: string
    let jobUUID: string

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

      await servers[0].config.enableTranscoding(true, true)

      const attributes = { name: 'audio_with_preview', previewfile: 'preview.jpg', fixture: 'sample.ogg' }
      const { uuid } = await servers[0].videos.upload({ attributes, mode: 'legacy' })
      videoUUID = uuid

      await waitJobs(servers)
    })

    it('Should have an audio merge transcoding job', async function () {
      const { availableJobs } = await servers[0].runnerJobs.requestVOD({ runnerToken })
      expect(availableJobs).to.have.lengthOf(1)

      expect(availableJobs[0].type).to.equal('vod-audio-merge-transcoding')

      jobUUID = availableJobs[0].uuid
    })

    it('Should have a valid remote audio merge transcoding job', async function () {
      const { job } = await servers[0].runnerJobs.accept<RunnerJobVODAudioMergeTranscodingPayload>({ runnerToken, jobUUID })
      jobToken = job.jobToken

      expect(job.type === 'vod-audio-merge-transcoding')
      expect(job.payload.input.audioFileUrl).to.exist
      expect(job.payload.input.previewFileUrl).to.exist
      expect(job.payload.output.resolution).to.equal(480)

      {
        const { body } = await servers[0].runnerJobs.getJobFile({ url: job.payload.input.audioFileUrl, jobToken, runnerToken })
        const inputFile = await readFile(buildAbsoluteFixturePath('sample.ogg'))
        expect(body).to.deep.equal(inputFile)
      }

      {
        const { body } = await servers[0].runnerJobs.getJobFile({ url: job.payload.input.previewFileUrl, jobToken, runnerToken })

        const video = await servers[0].videos.get({ id: videoUUID })
        const { body: inputFile } = await makeGetRequest({
          url: servers[0].url,
          path: video.previewPath,
          expectedStatus: HttpStatusCode.OK_200
        })

        expect(body).to.deep.equal(inputFile)
      }
    })

    it('Should merge the audio', async function () {
      this.timeout(60000)

      const payload: VODAudioMergeTranscodingSuccess = { videoFile: 'video_short_480p.mp4' }
      await servers[0].runnerJobs.success({ runnerToken, jobUUID, jobToken, payload })

      await waitJobs(servers)
    })

    it('Should have the video updated', async function () {
      for (const server of servers) {
        const video = await server.videos.get({ id: videoUUID })
        expect(video.files).to.have.lengthOf(1)
        expect(video.streamingPlaylists).to.have.lengthOf(0)

        const { body } = await makeRawRequest({ url: video.files[0].fileUrl, expectedStatus: HttpStatusCode.OK_200 })
        expect(body).to.deep.equal(await readFile(buildAbsoluteFixturePath('video_short_480p.mp4')))
      }
    })

    it('Should have 7 lower resolutions to transcode', async function () {
      const { availableJobs } = await servers[0].runnerJobs.requestVOD({ runnerToken })
      expect(availableJobs).to.have.lengthOf(7)

      for (const resolution of [ 360, 240, 144 ]) {
        const jobs = availableJobs.filter(j => j.payload.output.resolution === resolution)
        expect(jobs).to.have.lengthOf(2)
      }

      jobUUID = availableJobs.find(j => j.payload.output.resolution === 480).uuid
    })

    it('Should process one other job', async function () {
      this.timeout(60000)

      const { job } = await servers[0].runnerJobs.accept<RunnerJobVODHLSTranscodingPayload>({ runnerToken, jobUUID })
      jobToken = job.jobToken

      const { body } = await servers[0].runnerJobs.getJobFile({ url: job.payload.input.videoFileUrl, jobToken, runnerToken })
      const inputFile = await readFile(buildAbsoluteFixturePath('video_short_480p.mp4'))
      expect(body).to.deep.equal(inputFile)

      const payload: VODHLSTranscodingSuccess = {
        videoFile: `video_short_480p.mp4`,
        resolutionPlaylistFile: `video_short_480p.m3u8`
      }
      await servers[0].runnerJobs.success({ runnerToken, jobUUID, jobToken, payload })

      await waitJobs(servers)
    })

    it('Should have the video updated', async function () {
      for (const server of servers) {
        const video = await server.videos.get({ id: videoUUID })

        expect(video.files).to.have.lengthOf(1)
        expect(video.streamingPlaylists).to.have.lengthOf(1)

        const hls = video.streamingPlaylists[0]
        expect(hls.files).to.have.lengthOf(1)

        await completeCheckHlsPlaylist({ videoUUID, hlsOnly: false, servers, resolutions: [ 480 ] })
      }
    })

    it('Should process all available jobs', async function () {
      await processAllJobs(servers[0], runnerToken)
    })
  })

  after(async function () {
    await cleanupTests(servers)
  })
})