]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-hls.ts
Add codec information in HLS playlist
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-hls.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6 checkDirectoryIsEmpty,
7 checkSegmentHash,
8 checkTmpIsEmpty,
9 cleanupTests,
10 doubleFollow,
11 flushAndRunMultipleServers,
12 getPlaylist,
13 getVideo, makeGetRequest, makeRawRequest,
14 removeVideo,
15 ServerInfo,
16 setAccessTokensToServers, updateCustomSubConfig,
17 updateVideo,
18 uploadVideo,
19 waitJobs, webtorrentAdd
20 } from '../../../../shared/extra-utils'
21 import { VideoDetails } from '../../../../shared/models/videos'
22 import { VideoStreamingPlaylistType } from '../../../../shared/models/videos/video-streaming-playlist.type'
23 import { join } from 'path'
24 import { DEFAULT_AUDIO_RESOLUTION } from '../../../initializers/constants'
25
26 const expect = chai.expect
27
28 async function checkHlsPlaylist (servers: ServerInfo[], videoUUID: string, hlsOnly: boolean, resolutions = [ 240, 360, 480, 720 ]) {
29 for (const server of servers) {
30 const resVideoDetails = await getVideo(server.url, videoUUID)
31 const videoDetails: VideoDetails = resVideoDetails.body
32 const baseUrl = `http://${videoDetails.account.host}`
33
34 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
35
36 const hlsPlaylist = videoDetails.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
37 expect(hlsPlaylist).to.not.be.undefined
38
39 const hlsFiles = hlsPlaylist.files
40 expect(hlsFiles).to.have.lengthOf(resolutions.length)
41
42 if (hlsOnly) expect(videoDetails.files).to.have.lengthOf(0)
43 else expect(videoDetails.files).to.have.lengthOf(resolutions.length)
44
45 for (const resolution of resolutions) {
46 const file = hlsFiles.find(f => f.resolution.id === resolution)
47 expect(file).to.not.be.undefined
48
49 expect(file.magnetUri).to.have.lengthOf.above(2)
50 expect(file.torrentUrl).to.equal(`${baseUrl}/static/torrents/${videoDetails.uuid}-${file.resolution.id}-hls.torrent`)
51 expect(file.fileUrl).to.equal(`${baseUrl}/static/streaming-playlists/hls/${videoDetails.uuid}/${videoDetails.uuid}-${file.resolution.id}-fragmented.mp4`)
52 expect(file.resolution.label).to.equal(resolution + 'p')
53
54 await makeRawRequest(file.torrentUrl, 200)
55 await makeRawRequest(file.fileUrl, 200)
56
57 const torrent = await webtorrentAdd(file.magnetUri, true)
58 expect(torrent.files).to.be.an('array')
59 expect(torrent.files.length).to.equal(1)
60 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
61 }
62
63 {
64 const res = await getPlaylist(hlsPlaylist.playlistUrl)
65
66 const masterPlaylist = res.text
67
68 for (const resolution of resolutions) {
69 const reg = new RegExp('#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',FRAME-RATE=\\d+,CODECS="avc1.64001f,mp4a.40.2"')
70
71 expect(masterPlaylist).to.match(reg)
72 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
73 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
74 }
75 }
76
77 {
78 for (const resolution of resolutions) {
79 const res = await getPlaylist(`${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${resolution}.m3u8`)
80
81 const subPlaylist = res.text
82 expect(subPlaylist).to.contain(`${videoUUID}-${resolution}-fragmented.mp4`)
83 }
84 }
85
86 {
87 const baseUrlAndPath = baseUrl + '/static/streaming-playlists/hls'
88
89 for (const resolution of resolutions) {
90 await checkSegmentHash(baseUrlAndPath, baseUrlAndPath, videoUUID, resolution, hlsPlaylist)
91 }
92 }
93 }
94 }
95
96 describe('Test HLS videos', function () {
97 let servers: ServerInfo[] = []
98 let videoUUID = ''
99 let videoAudioUUID = ''
100
101 function runTestSuite (hlsOnly: boolean) {
102 it('Should upload a video and transcode it to HLS', async function () {
103 this.timeout(120000)
104
105 const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'video 1', fixture: 'video_short.webm' })
106 videoUUID = res.body.video.uuid
107
108 await waitJobs(servers)
109
110 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
111 })
112
113 it('Should upload an audio file and transcode it to HLS', async function () {
114 this.timeout(120000)
115
116 const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'video audio', fixture: 'sample.ogg' })
117 videoAudioUUID = res.body.video.uuid
118
119 await waitJobs(servers)
120
121 await checkHlsPlaylist(servers, videoAudioUUID, hlsOnly, [ DEFAULT_AUDIO_RESOLUTION ])
122 })
123
124 it('Should update the video', async function () {
125 this.timeout(10000)
126
127 await updateVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID, { name: 'video 1 updated' })
128
129 await waitJobs(servers)
130
131 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
132 })
133
134 it('Should delete videos', async function () {
135 this.timeout(10000)
136
137 await removeVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID)
138 await removeVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoAudioUUID)
139
140 await waitJobs(servers)
141
142 for (const server of servers) {
143 await getVideo(server.url, videoUUID, 404)
144 await getVideo(server.url, videoAudioUUID, 404)
145 }
146 })
147
148 it('Should have the playlists/segment deleted from the disk', async function () {
149 for (const server of servers) {
150 await checkDirectoryIsEmpty(server, 'videos')
151 await checkDirectoryIsEmpty(server, join('streaming-playlists', 'hls'))
152 }
153 })
154
155 it('Should have an empty tmp directory', async function () {
156 for (const server of servers) {
157 await checkTmpIsEmpty(server)
158 }
159 })
160 }
161
162 before(async function () {
163 this.timeout(120000)
164
165 const configOverride = {
166 transcoding: {
167 enabled: true,
168 allow_audio_files: true,
169 hls: {
170 enabled: true
171 }
172 }
173 }
174 servers = await flushAndRunMultipleServers(2, configOverride)
175
176 // Get the access tokens
177 await setAccessTokensToServers(servers)
178
179 // Server 1 and server 2 follow each other
180 await doubleFollow(servers[0], servers[1])
181 })
182
183 describe('With WebTorrent & HLS enabled', function () {
184 runTestSuite(false)
185 })
186
187 describe('With only HLS enabled', function () {
188
189 before(async function () {
190 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
191 transcoding: {
192 enabled: true,
193 allowAudioFiles: true,
194 resolutions: {
195 '240p': true,
196 '360p': true,
197 '480p': true,
198 '720p': true,
199 '1080p': true,
200 '2160p': true
201 },
202 hls: {
203 enabled: true
204 },
205 webtorrent: {
206 enabled: false
207 }
208 }
209 })
210 })
211
212 runTestSuite(true)
213 })
214
215 after(async function () {
216 await cleanupTests(servers)
217 })
218 })