]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/hls.ts
Add request body limit
[github/Chocobozzz/PeerTube.git] / server / lib / hls.ts
CommitLineData
09209296 1import { VideoModel } from '../models/video/video'
4c280004
C
2import { basename, join, dirname } from 'path'
3import { CONFIG, HLS_PLAYLIST_DIRECTORY } from '../initializers'
4import { close, ensureDir, move, open, outputJSON, pathExists, read, readFile, remove, writeFile } from 'fs-extra'
09209296
C
5import { getVideoFileSize } from '../helpers/ffmpeg-utils'
6import { sha256 } from '../helpers/core-utils'
7import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
09209296 8import { logger } from '../helpers/logger'
4c280004
C
9import { doRequest, doRequestAndSaveToFile } from '../helpers/requests'
10import { generateRandomString } from '../helpers/utils'
11import { flatten, uniq } from 'lodash'
09209296
C
12
13async function updateMasterHLSPlaylist (video: VideoModel) {
14 const directory = join(HLS_PLAYLIST_DIRECTORY, video.uuid)
15 const masterPlaylists: string[] = [ '#EXTM3U', '#EXT-X-VERSION:3' ]
16 const masterPlaylistPath = join(directory, VideoStreamingPlaylistModel.getMasterHlsPlaylistFilename())
17
18 for (const file of video.VideoFiles) {
19 // If we did not generated a playlist for this resolution, skip
20 const filePlaylistPath = join(directory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(file.resolution))
21 if (await pathExists(filePlaylistPath) === false) continue
22
23 const videoFilePath = video.getVideoFilePath(file)
24
25 const size = await getVideoFileSize(videoFilePath)
26
27 const bandwidth = 'BANDWIDTH=' + video.getBandwidthBits(file)
28 const resolution = `RESOLUTION=${size.width}x${size.height}`
29
30 let line = `#EXT-X-STREAM-INF:${bandwidth},${resolution}`
31 if (file.fps) line += ',FRAME-RATE=' + file.fps
32
33 masterPlaylists.push(line)
34 masterPlaylists.push(VideoStreamingPlaylistModel.getHlsPlaylistFilename(file.resolution))
35 }
36
37 await writeFile(masterPlaylistPath, masterPlaylists.join('\n') + '\n')
38}
39
40async function updateSha256Segments (video: VideoModel) {
4c280004
C
41 const json: { [filename: string]: { [range: string]: string } } = {}
42
43 const playlistDirectory = join(HLS_PLAYLIST_DIRECTORY, video.uuid)
44
45 // For all the resolutions available for this video
46 for (const file of video.VideoFiles) {
47 const rangeHashes: { [range: string]: string } = {}
48
49 const videoPath = join(playlistDirectory, VideoStreamingPlaylistModel.getHlsVideoName(video.uuid, file.resolution))
50 const playlistPath = join(playlistDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(file.resolution))
09209296 51
4c280004
C
52 // Maybe the playlist is not generated for this resolution yet
53 if (!await pathExists(playlistPath)) continue
09209296 54
4c280004
C
55 const playlistContent = await readFile(playlistPath)
56 const ranges = getRangesFromPlaylist(playlistContent.toString())
09209296 57
4c280004
C
58 const fd = await open(videoPath, 'r')
59 for (const range of ranges) {
60 const buf = Buffer.alloc(range.length)
61 await read(fd, buf, 0, range.length, range.offset)
62
63 rangeHashes[`${range.offset}-${range.offset + range.length - 1}`] = sha256(buf)
64 }
65 await close(fd)
66
67 const videoFilename = VideoStreamingPlaylistModel.getHlsVideoName(video.uuid, file.resolution)
68 json[videoFilename] = rangeHashes
09209296
C
69 }
70
4c280004 71 const outputPath = join(playlistDirectory, VideoStreamingPlaylistModel.getHlsSha256SegmentsFilename())
09209296
C
72 await outputJSON(outputPath, json)
73}
74
4c280004
C
75function getRangesFromPlaylist (playlistContent: string) {
76 const ranges: { offset: number, length: number }[] = []
77 const lines = playlistContent.split('\n')
78 const regex = /^#EXT-X-BYTERANGE:(\d+)@(\d+)$/
09209296 79
4c280004
C
80 for (const line of lines) {
81 const captured = regex.exec(line)
09209296 82
4c280004
C
83 if (captured) {
84 ranges.push({ length: parseInt(captured[1], 10), offset: parseInt(captured[2], 10) })
85 }
09209296 86 }
09209296 87
4c280004
C
88 return ranges
89}
09209296 90
4c280004
C
91function downloadPlaylistSegments (playlistUrl: string, destinationDir: string, timeout: number) {
92 let timer
09209296 93
4c280004 94 logger.info('Importing HLS playlist %s', playlistUrl)
09209296 95
4c280004
C
96 return new Promise<string>(async (res, rej) => {
97 const tmpDirectory = join(CONFIG.STORAGE.TMP_DIR, await generateRandomString(10))
09209296 98
4c280004 99 await ensureDir(tmpDirectory)
09209296
C
100
101 timer = setTimeout(() => {
4c280004 102 deleteTmpDirectory(tmpDirectory)
09209296
C
103
104 return rej(new Error('HLS download timeout.'))
105 }, timeout)
106
4c280004
C
107 try {
108 // Fetch master playlist
109 const subPlaylistUrls = await fetchUniqUrls(playlistUrl)
110
111 const subRequests = subPlaylistUrls.map(u => fetchUniqUrls(u))
112 const fileUrls = uniq(flatten(await Promise.all(subRequests)))
113
114 logger.debug('Will download %d HLS files.', fileUrls.length, { fileUrls })
115
116 for (const fileUrl of fileUrls) {
117 const destPath = join(tmpDirectory, basename(fileUrl))
118
bfe2ef6b
C
119 const bodyKBLimit = 10 * 1000 * 1000 // 10GB
120 await doRequestAndSaveToFile({ uri: fileUrl }, destPath, bodyKBLimit)
4c280004
C
121 }
122
123 clearTimeout(timer)
124
125 await move(tmpDirectory, destinationDir, { overwrite: true })
126
127 return res()
128 } catch (err) {
129 deleteTmpDirectory(tmpDirectory)
130
131 return rej(err)
09209296
C
132 }
133 })
4c280004
C
134
135 function deleteTmpDirectory (directory: string) {
136 remove(directory)
137 .catch(err => logger.error('Cannot delete path on HLS download error.', { err }))
138 }
139
140 async function fetchUniqUrls (playlistUrl: string) {
141 const { body } = await doRequest<string>({ uri: playlistUrl })
142
143 if (!body) return []
144
145 const urls = body.split('\n')
146 .filter(line => line.endsWith('.m3u8') || line.endsWith('.mp4'))
147 .map(url => {
148 if (url.startsWith('http://') || url.startsWith('https://')) return url
149
150 return `${dirname(playlistUrl)}/${url}`
151 })
152
153 return uniq(urls)
154 }
09209296
C
155}
156
157// ---------------------------------------------------------------------------
158
159export {
160 updateMasterHLSPlaylist,
161 updateSha256Segments,
162 downloadPlaylistSegments
163}
164
165// ---------------------------------------------------------------------------