create-transcoding-job.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import 'mocha'
  3. import * as chai from 'chai'
  4. import { areObjectStorageTestsDisabled } from '@shared/core-utils'
  5. import { HttpStatusCode, VideoFile } from '@shared/models'
  6. import {
  7. cleanupTests,
  8. createMultipleServers,
  9. doubleFollow,
  10. makeRawRequest,
  11. ObjectStorageCommand,
  12. PeerTubeServer,
  13. setAccessTokensToServers,
  14. waitJobs
  15. } from '@shared/server-commands'
  16. import { checkResolutionsInMasterPlaylist, expectStartWith } from '../shared'
  17. const expect = chai.expect
  18. async function checkFilesInObjectStorage (files: VideoFile[], type: 'webtorrent' | 'playlist') {
  19. for (const file of files) {
  20. const shouldStartWith = type === 'webtorrent'
  21. ? ObjectStorageCommand.getWebTorrentBaseUrl()
  22. : ObjectStorageCommand.getPlaylistBaseUrl()
  23. expectStartWith(file.fileUrl, shouldStartWith)
  24. await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
  25. }
  26. }
  27. function runTests (objectStorage: boolean) {
  28. let servers: PeerTubeServer[] = []
  29. const videosUUID: string[] = []
  30. const publishedAt: string[] = []
  31. before(async function () {
  32. this.timeout(120000)
  33. const config = objectStorage
  34. ? ObjectStorageCommand.getDefaultConfig()
  35. : {}
  36. // Run server 2 to have transcoding enabled
  37. servers = await createMultipleServers(2, config)
  38. await setAccessTokensToServers(servers)
  39. await servers[0].config.disableTranscoding()
  40. await doubleFollow(servers[0], servers[1])
  41. if (objectStorage) await ObjectStorageCommand.prepareDefaultBuckets()
  42. for (let i = 1; i <= 5; i++) {
  43. const { uuid, shortUUID } = await servers[0].videos.upload({ attributes: { name: 'video' + i } })
  44. await waitJobs(servers)
  45. const video = await servers[0].videos.get({ id: uuid })
  46. publishedAt.push(video.publishedAt as string)
  47. if (i > 2) {
  48. videosUUID.push(uuid)
  49. } else {
  50. videosUUID.push(shortUUID)
  51. }
  52. }
  53. await waitJobs(servers)
  54. })
  55. it('Should have two video files on each server', async function () {
  56. this.timeout(30000)
  57. for (const server of servers) {
  58. const { data } = await server.videos.list()
  59. expect(data).to.have.lengthOf(videosUUID.length)
  60. for (const video of data) {
  61. const videoDetail = await server.videos.get({ id: video.uuid })
  62. expect(videoDetail.files).to.have.lengthOf(1)
  63. expect(videoDetail.streamingPlaylists).to.have.lengthOf(0)
  64. }
  65. }
  66. })
  67. it('Should run a transcoding job on video 2', async function () {
  68. this.timeout(60000)
  69. await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[1]}`)
  70. await waitJobs(servers)
  71. for (const server of servers) {
  72. const { data } = await server.videos.list()
  73. let infoHashes: { [id: number]: string }
  74. for (const video of data) {
  75. const videoDetails = await server.videos.get({ id: video.uuid })
  76. if (video.shortUUID === videosUUID[1] || video.uuid === videosUUID[1]) {
  77. expect(videoDetails.files).to.have.lengthOf(4)
  78. expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
  79. if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
  80. if (!infoHashes) {
  81. infoHashes = {}
  82. for (const file of videoDetails.files) {
  83. infoHashes[file.resolution.id.toString()] = file.magnetUri
  84. }
  85. } else {
  86. for (const resolution of Object.keys(infoHashes)) {
  87. const file = videoDetails.files.find(f => f.resolution.id.toString() === resolution)
  88. expect(file.magnetUri).to.equal(infoHashes[resolution])
  89. }
  90. }
  91. } else {
  92. expect(videoDetails.files).to.have.lengthOf(1)
  93. expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
  94. }
  95. }
  96. }
  97. })
  98. it('Should run a transcoding job on video 1 with resolution', async function () {
  99. this.timeout(60000)
  100. await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[0]} -r 480`)
  101. await waitJobs(servers)
  102. for (const server of servers) {
  103. const { data } = await server.videos.list()
  104. expect(data).to.have.lengthOf(videosUUID.length)
  105. const videoDetails = await server.videos.get({ id: videosUUID[0] })
  106. expect(videoDetails.files).to.have.lengthOf(2)
  107. expect(videoDetails.files[0].resolution.id).to.equal(720)
  108. expect(videoDetails.files[1].resolution.id).to.equal(480)
  109. expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
  110. if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
  111. }
  112. })
  113. it('Should generate an HLS resolution', async function () {
  114. this.timeout(120000)
  115. await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[2]} --generate-hls -r 480`)
  116. await waitJobs(servers)
  117. for (const server of servers) {
  118. const videoDetails = await server.videos.get({ id: videosUUID[2] })
  119. expect(videoDetails.files).to.have.lengthOf(1)
  120. if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
  121. expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
  122. const hlsPlaylist = videoDetails.streamingPlaylists[0]
  123. const files = hlsPlaylist.files
  124. expect(files).to.have.lengthOf(1)
  125. expect(files[0].resolution.id).to.equal(480)
  126. if (objectStorage) {
  127. await checkFilesInObjectStorage(files, 'playlist')
  128. const resolutions = files.map(f => f.resolution.id)
  129. await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
  130. }
  131. }
  132. })
  133. it('Should not duplicate an HLS resolution', async function () {
  134. this.timeout(120000)
  135. await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[2]} --generate-hls -r 480`)
  136. await waitJobs(servers)
  137. for (const server of servers) {
  138. const videoDetails = await server.videos.get({ id: videosUUID[2] })
  139. const files = videoDetails.streamingPlaylists[0].files
  140. expect(files).to.have.lengthOf(1)
  141. expect(files[0].resolution.id).to.equal(480)
  142. if (objectStorage) await checkFilesInObjectStorage(files, 'playlist')
  143. }
  144. })
  145. it('Should generate all HLS resolutions', async function () {
  146. this.timeout(120000)
  147. await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[3]} --generate-hls`)
  148. await waitJobs(servers)
  149. for (const server of servers) {
  150. const videoDetails = await server.videos.get({ id: videosUUID[3] })
  151. expect(videoDetails.files).to.have.lengthOf(1)
  152. expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
  153. const files = videoDetails.streamingPlaylists[0].files
  154. expect(files).to.have.lengthOf(4)
  155. if (objectStorage) await checkFilesInObjectStorage(files, 'playlist')
  156. }
  157. })
  158. it('Should optimize the video file and generate HLS videos if enabled in config', async function () {
  159. this.timeout(120000)
  160. await servers[0].config.enableTranscoding()
  161. await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[4]}`)
  162. await waitJobs(servers)
  163. for (const server of servers) {
  164. const videoDetails = await server.videos.get({ id: videosUUID[4] })
  165. expect(videoDetails.files).to.have.lengthOf(5)
  166. expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
  167. expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
  168. if (objectStorage) {
  169. await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
  170. await checkFilesInObjectStorage(videoDetails.streamingPlaylists[0].files, 'playlist')
  171. }
  172. }
  173. })
  174. it('Should not have updated published at attributes', async function () {
  175. for (const id of videosUUID) {
  176. const video = await servers[0].videos.get({ id })
  177. expect(publishedAt.some(p => video.publishedAt === p)).to.be.true
  178. }
  179. })
  180. after(async function () {
  181. await cleanupTests(servers)
  182. })
  183. }
  184. describe('Test create transcoding jobs', function () {
  185. describe('On filesystem', function () {
  186. runTests(false)
  187. })
  188. describe('On object storage', function () {
  189. if (areObjectStorageTestsDisabled()) return
  190. runTests(true)
  191. })
  192. })