X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fhls.ts;h=43043315be6ed42dc752d029ca87ebb30ba67de2;hb=ab623c0e0b4815bd69a94887241a69aaa857ed26;hp=8160e7949571c2938e2fac6120582eb2396398c6;hpb=e1ab52d7ec7370a6f9f5937192d6003206af1ac0;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/hls.ts b/server/lib/hls.ts index 8160e7949..43043315b 100644 --- a/server/lib/hls.ts +++ b/server/lib/hls.ts @@ -2,16 +2,18 @@ import { close, ensureDir, move, open, outputJSON, read, readFile, remove, stat, import { flatten, uniq } from 'lodash' import { basename, dirname, join } from 'path' import { MStreamingPlaylistFilesVideo, MVideo, MVideoUUID } from '@server/types/models' -import { sha256 } from '../helpers/core-utils' -import { getAudioStreamCodec, getVideoStreamCodec, getVideoStreamSize } from '../helpers/ffprobe-utils' +import { sha256 } from '@shared/extra-utils' +import { VideoStorage } from '@shared/models' +import { getAudioStreamCodec, getVideoStreamCodec, getVideoStreamDimensionsInfo } from '../helpers/ffmpeg' import { logger } from '../helpers/logger' import { doRequest, doRequestAndSaveToFile } from '../helpers/requests' import { generateRandomString } from '../helpers/utils' import { CONFIG } from '../initializers/config' -import { P2P_MEDIA_LOADER_PEER_VERSION } from '../initializers/constants' +import { P2P_MEDIA_LOADER_PEER_VERSION, REQUEST_TIMEOUTS } from '../initializers/constants' import { sequelizeTypescript } from '../initializers/database' import { VideoFileModel } from '../models/video/video-file' import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist' +import { storeHLSFile } from './object-storage' import { getHlsResolutionPlaylistFilename } from './paths' import { VideoPathManager } from './video-path-manager' @@ -37,11 +39,11 @@ async function updateMasterHLSPlaylist (video: MVideo, playlist: MStreamingPlayl for (const file of playlist.VideoFiles) { const playlistFilename = getHlsResolutionPlaylistFilename(file.filename) - await VideoPathManager.Instance.makeAvailableVideoFile(playlist, file, async videoFilePath => { - const size = await getVideoStreamSize(videoFilePath) + await VideoPathManager.Instance.makeAvailableVideoFile(file.withVideoOrPlaylist(playlist), async videoFilePath => { + const size = await getVideoStreamDimensionsInfo(videoFilePath) const bandwidth = 'BANDWIDTH=' + video.getBandwidthBits(file) - const resolution = `RESOLUTION=${size.width}x${size.height}` + const resolution = `RESOLUTION=${size?.width || 0}x${size?.height || 0}` let line = `#EXT-X-STREAM-INF:${bandwidth},${resolution}` if (file.fps) line += ',FRAME-RATE=' + file.fps @@ -58,8 +60,12 @@ async function updateMasterHLSPlaylist (video: MVideo, playlist: MStreamingPlayl }) } - await VideoPathManager.Instance.makeAvailablePlaylistFile(playlist, playlist.playlistFilename, masterPlaylistPath => { - return writeFile(masterPlaylistPath, masterPlaylists.join('\n') + '\n') + await VideoPathManager.Instance.makeAvailablePlaylistFile(playlist, playlist.playlistFilename, async masterPlaylistPath => { + await writeFile(masterPlaylistPath, masterPlaylists.join('\n') + '\n') + + if (playlist.storage === VideoStorage.OBJECT_STORAGE) { + await storeHLSFile(playlist, playlist.playlistFilename, masterPlaylistPath) + } }) } @@ -69,10 +75,11 @@ async function updateSha256VODSegments (video: MVideoUUID, playlist: MStreamingP // For all the resolutions available for this video for (const file of playlist.VideoFiles) { const rangeHashes: { [range: string]: string } = {} + const fileWithPlaylist = file.withVideoOrPlaylist(playlist) - await VideoPathManager.Instance.makeAvailableVideoFile(playlist, file, videoPath => { + await VideoPathManager.Instance.makeAvailableVideoFile(fileWithPlaylist, videoPath => { - return VideoPathManager.Instance.makeAvailableResolutionPlaylistFile(playlist, file, async resolutionPlaylistPath => { + return VideoPathManager.Instance.makeAvailableResolutionPlaylistFile(fileWithPlaylist, async resolutionPlaylistPath => { const playlistContent = await readFile(resolutionPlaylistPath) const ranges = getRangesFromPlaylist(playlistContent.toString()) @@ -93,6 +100,11 @@ async function updateSha256VODSegments (video: MVideoUUID, playlist: MStreamingP const outputPath = VideoPathManager.Instance.getFSHLSOutputPath(video, playlist.segmentsSha256Filename) await outputJSON(outputPath, json) + + if (playlist.storage === VideoStorage.OBJECT_STORAGE) { + await storeHLSFile(playlist, playlist.segmentsSha256Filename) + await remove(outputPath) + } } async function buildSha256Segment (segmentPath: string) { @@ -129,7 +141,7 @@ function downloadPlaylistSegments (playlistUrl: string, destinationDir: string, for (const fileUrl of fileUrls) { const destPath = join(tmpDirectory, basename(fileUrl)) - await doRequestAndSaveToFile(fileUrl, destPath, { bodyKBLimit: remainingBodyKBLimit }) + await doRequestAndSaveToFile(fileUrl, destPath, { bodyKBLimit: remainingBodyKBLimit, timeout: REQUEST_TIMEOUTS.REDUNDANCY }) const { size } = await stat(destPath) remainingBodyKBLimit -= (size / 1000)