+ const base = '#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution
+
+ if (resolution === VideoResolution.H_NOVIDEO) {
+ expect(masterPlaylist).to.match(new RegExp(`${base},CODECS="mp4a.40.2"`))
+ } else if (transcoded) {
+ expect(masterPlaylist).to.match(new RegExp(`${base},(FRAME-RATE=\\d+,)?CODECS="avc1.64001f,mp4a.40.2"`))
+ } else {
+ expect(masterPlaylist).to.match(new RegExp(`${base}`))
+ }
+ }
+
+ const playlistsLength = masterPlaylist.split('\n').filter(line => line.startsWith('#EXT-X-STREAM-INF:BANDWIDTH='))
+ expect(playlistsLength).to.have.lengthOf(resolutions.length)
+}
+
+async function completeCheckHlsPlaylist (options: {
+ servers: PeerTubeServer[]
+ videoUUID: string
+ hlsOnly: boolean
+
+ resolutions?: number[]
+ objectStorageBaseUrl?: string
+}) {
+ const { videoUUID, hlsOnly, objectStorageBaseUrl } = options
+
+ const resolutions = options.resolutions ?? [ 240, 360, 480, 720 ]
+
+ for (const server of options.servers) {
+ const videoDetails = await server.videos.getWithToken({ id: videoUUID })
+ const requiresAuth = videoDetails.privacy.id === VideoPrivacy.PRIVATE || videoDetails.privacy.id === VideoPrivacy.INTERNAL
+
+ const privatePath = requiresAuth
+ ? 'private/'
+ : ''
+ const token = requiresAuth
+ ? server.accessToken
+ : undefined
+
+ const baseUrl = `http://${videoDetails.account.host}`
+
+ expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
+
+ const hlsPlaylist = videoDetails.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
+ expect(hlsPlaylist).to.not.be.undefined
+
+ const hlsFiles = hlsPlaylist.files
+ expect(hlsFiles).to.have.lengthOf(resolutions.length)
+
+ if (hlsOnly) expect(videoDetails.files).to.have.lengthOf(0)
+ else expect(videoDetails.files).to.have.lengthOf(resolutions.length)
+
+ // Check JSON files
+ for (const resolution of resolutions) {
+ const file = hlsFiles.find(f => f.resolution.id === resolution)
+ expect(file).to.not.be.undefined
+
+ if (file.resolution.id === VideoResolution.H_NOVIDEO) {
+ expect(file.resolution.label).to.equal('Audio')
+ } else {
+ expect(file.resolution.label).to.equal(resolution + 'p')
+ }
+
+ expect(file.magnetUri).to.have.lengthOf.above(2)
+ await checkWebTorrentWorks(file.magnetUri)
+
+ {
+ const nameReg = `${uuidRegex}-${file.resolution.id}`
+
+ expect(file.torrentUrl).to.match(new RegExp(`${server.url}/lazy-static/torrents/${nameReg}-hls.torrent`))
+
+ if (objectStorageBaseUrl && requiresAuth) {
+ // eslint-disable-next-line max-len
+ expect(file.fileUrl).to.match(new RegExp(`${server.url}/object-storage-proxy/streaming-playlists/hls/${privatePath}${videoDetails.uuid}/${nameReg}-fragmented.mp4`))
+ } else if (objectStorageBaseUrl) {
+ expectStartWith(file.fileUrl, objectStorageBaseUrl)
+ } else {
+ expect(file.fileUrl).to.match(
+ new RegExp(`${baseUrl}/static/streaming-playlists/hls/${privatePath}${videoDetails.uuid}/${nameReg}-fragmented.mp4`)
+ )
+ }
+ }
+
+ {
+ await Promise.all([
+ makeRawRequest({ url: file.torrentUrl, token, expectedStatus: HttpStatusCode.OK_200 }),
+ makeRawRequest({ url: file.torrentDownloadUrl, token, expectedStatus: HttpStatusCode.OK_200 }),
+ makeRawRequest({ url: file.metadataUrl, token, expectedStatus: HttpStatusCode.OK_200 }),
+ makeRawRequest({ url: file.fileUrl, token, expectedStatus: HttpStatusCode.OK_200 }),
+
+ makeRawRequest({
+ url: file.fileDownloadUrl,
+ token,
+ expectedStatus: objectStorageBaseUrl
+ ? HttpStatusCode.FOUND_302
+ : HttpStatusCode.OK_200
+ })
+ ])
+ }
+ }
+
+ // Check master playlist
+ {
+ await checkResolutionsInMasterPlaylist({ server, token, playlistUrl: hlsPlaylist.playlistUrl, resolutions })