]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-create-transcoding.ts
Fix HLS re transcoding with object storage enabled
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-create-transcoding.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { checkResolutionsInMasterPlaylist, expectStartWith } from '@server/tests/shared'
6 import { areObjectStorageTestsDisabled } from '@shared/core-utils'
7 import { HttpStatusCode, VideoDetails } from '@shared/models'
8 import {
9 cleanupTests,
10 ConfigCommand,
11 createMultipleServers,
12 doubleFollow,
13 expectNoFailedTranscodingJob,
14 makeRawRequest,
15 ObjectStorageCommand,
16 PeerTubeServer,
17 setAccessTokensToServers,
18 waitJobs
19 } from '@shared/server-commands'
20
21 const expect = chai.expect
22
23 async function checkFilesInObjectStorage (video: VideoDetails) {
24 for (const file of video.files) {
25 expectStartWith(file.fileUrl, ObjectStorageCommand.getWebTorrentBaseUrl())
26 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
27 }
28
29 if (video.streamingPlaylists.length === 0) return
30
31 const hlsPlaylist = video.streamingPlaylists[0]
32 for (const file of hlsPlaylist.files) {
33 expectStartWith(file.fileUrl, ObjectStorageCommand.getPlaylistBaseUrl())
34 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
35 }
36
37 expectStartWith(hlsPlaylist.playlistUrl, ObjectStorageCommand.getPlaylistBaseUrl())
38 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
39
40 expectStartWith(hlsPlaylist.segmentsSha256Url, ObjectStorageCommand.getPlaylistBaseUrl())
41 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
42 }
43
44 function runTests (objectStorage: boolean) {
45 let servers: PeerTubeServer[] = []
46 let videoUUID: string
47 let publishedAt: string
48
49 before(async function () {
50 this.timeout(120000)
51
52 const config = objectStorage
53 ? ObjectStorageCommand.getDefaultConfig()
54 : {}
55
56 // Run server 2 to have transcoding enabled
57 servers = await createMultipleServers(2, config)
58 await setAccessTokensToServers(servers)
59
60 await servers[0].config.disableTranscoding()
61
62 await doubleFollow(servers[0], servers[1])
63
64 if (objectStorage) await ObjectStorageCommand.prepareDefaultBuckets()
65
66 const { shortUUID } = await servers[0].videos.quickUpload({ name: 'video' })
67 videoUUID = shortUUID
68
69 await waitJobs(servers)
70
71 const video = await servers[0].videos.get({ id: videoUUID })
72 publishedAt = video.publishedAt as string
73
74 await servers[0].config.enableTranscoding()
75 })
76
77 it('Should generate HLS', async function () {
78 this.timeout(60000)
79
80 await servers[0].videos.runTranscoding({
81 videoId: videoUUID,
82 transcodingType: 'hls'
83 })
84
85 await waitJobs(servers)
86 await expectNoFailedTranscodingJob(servers[0])
87
88 for (const server of servers) {
89 const videoDetails = await server.videos.get({ id: videoUUID })
90
91 expect(videoDetails.files).to.have.lengthOf(1)
92 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
93 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
94
95 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
96 }
97 })
98
99 it('Should generate WebTorrent', async function () {
100 this.timeout(60000)
101
102 await servers[0].videos.runTranscoding({
103 videoId: videoUUID,
104 transcodingType: 'webtorrent'
105 })
106
107 await waitJobs(servers)
108
109 for (const server of servers) {
110 const videoDetails = await server.videos.get({ id: videoUUID })
111
112 expect(videoDetails.files).to.have.lengthOf(5)
113 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
114 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
115
116 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
117 }
118 })
119
120 it('Should generate WebTorrent from HLS only video', async function () {
121 this.timeout(60000)
122
123 await servers[0].videos.removeWebTorrentFiles({ videoId: videoUUID })
124 await waitJobs(servers)
125
126 await servers[0].videos.runTranscoding({ videoId: videoUUID, transcodingType: 'webtorrent' })
127 await waitJobs(servers)
128
129 for (const server of servers) {
130 const videoDetails = await server.videos.get({ id: videoUUID })
131
132 expect(videoDetails.files).to.have.lengthOf(5)
133 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
134 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
135
136 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
137 }
138 })
139
140 it('Should only generate WebTorrent', async function () {
141 this.timeout(60000)
142
143 await servers[0].videos.removeHLSFiles({ videoId: videoUUID })
144 await waitJobs(servers)
145
146 await servers[0].videos.runTranscoding({ videoId: videoUUID, transcodingType: 'webtorrent' })
147 await waitJobs(servers)
148
149 for (const server of servers) {
150 const videoDetails = await server.videos.get({ id: videoUUID })
151
152 expect(videoDetails.files).to.have.lengthOf(5)
153 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
154
155 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
156 }
157 })
158
159 it('Should correctly update HLS playlist on resolution change', async function () {
160 await servers[0].config.updateExistingSubConfig({
161 newConfig: {
162 transcoding: {
163 enabled: true,
164 resolutions: ConfigCommand.getCustomConfigResolutions(false),
165
166 webtorrent: {
167 enabled: true
168 },
169 hls: {
170 enabled: true
171 }
172 }
173 }
174 })
175
176 const { uuid } = await servers[0].videos.quickUpload({ name: 'quick' })
177
178 await waitJobs(servers)
179
180 for (const server of servers) {
181 const videoDetails = await server.videos.get({ id: uuid })
182
183 expect(videoDetails.files).to.have.lengthOf(1)
184 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
185 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(1)
186
187 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
188 }
189
190 await servers[0].config.updateExistingSubConfig({
191 newConfig: {
192 transcoding: {
193 enabled: true,
194 resolutions: ConfigCommand.getCustomConfigResolutions(true),
195
196 webtorrent: {
197 enabled: true
198 },
199 hls: {
200 enabled: true
201 }
202 }
203 }
204 })
205
206 await servers[0].videos.runTranscoding({ videoId: uuid, transcodingType: 'hls' })
207 await waitJobs(servers)
208
209 for (const server of servers) {
210 const videoDetails = await server.videos.get({ id: uuid })
211
212 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
213 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
214
215 if (objectStorage) {
216 await checkFilesInObjectStorage(videoDetails)
217
218 const hlsPlaylist = videoDetails.streamingPlaylists[0]
219 const resolutions = hlsPlaylist.files.map(f => f.resolution.id)
220 await checkResolutionsInMasterPlaylist({ server: servers[0], playlistUrl: hlsPlaylist.playlistUrl, resolutions })
221
222 const shaBody = await servers[0].streamingPlaylists.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
223 expect(Object.keys(shaBody)).to.have.lengthOf(5)
224 }
225 }
226 })
227
228 it('Should not have updated published at attributes', async function () {
229 const video = await servers[0].videos.get({ id: videoUUID })
230
231 expect(video.publishedAt).to.equal(publishedAt)
232 })
233
234 after(async function () {
235 await cleanupTests(servers)
236 })
237 }
238
239 describe('Test create transcoding jobs from API', function () {
240
241 describe('On filesystem', function () {
242 runTests(false)
243 })
244
245 describe('On object storage', function () {
246 if (areObjectStorageTestsDisabled()) return
247
248 runTests(true)
249 })
250 })