PeerTube/server/models/video/video-streaming-playlist.ts

265 lines
7.1 KiB
TypeScript
Raw Normal View History

2021-08-27 14:32:44 +02:00
import memoizee from 'memoizee'
import { join } from 'path'
2021-07-23 11:20:00 +02:00
import { Op } from 'sequelize'
Add support for saving video files to object storage (#4290) * Add support for saving video files to object storage * Add support for custom url generation on s3 stored files Uses two config keys to support url generation that doesn't directly go to (compatible s3). Can be used to generate urls to any cache server or CDN. * Upload files to s3 concurrently and delete originals afterwards * Only publish after move to object storage is complete * Use base url instead of url template * Fix mistyped config field * Add rudenmentary way to download before transcode * Implement Chocobozzz suggestions https://github.com/Chocobozzz/PeerTube/pull/4290#issuecomment-891670478 The remarks in question: Try to use objectStorage prefix instead of s3 prefix for your function/variables/config names Prefer to use a tree for the config: s3.streaming_playlists_bucket -> object_storage.streaming_playlists.bucket Use uppercase for config: S3.STREAMING_PLAYLISTS_BUCKETINFO.bucket -> OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET (maybe BUCKET_NAME instead of BUCKET) I suggest to rename moveJobsRunning to pendingMovingJobs (or better, create a dedicated videoJobInfo table with a pendingMove & videoId columns so we could also use this table to track pending transcoding jobs) https://github.com/Chocobozzz/PeerTube/pull/4290/files#diff-3e26d41ca4bda1de8e1747af70ca2af642abcc1e9e0bfb94239ff2165acfbde5R19 uses a string instead of an integer I think we should store the origin object storage URL in fileUrl, without base_url injection. Instead, inject the base_url at "runtime" so admins can easily change this configuration without running a script to update DB URLs * Import correct function * Support multipart upload * Remove import of node 15.0 module stream/promises * Extend maximum upload job length Using the same value as for redundancy downloading seems logical * Use dynamic part size for really large uploads Also adds very small part size for local testing * Fix decreasePendingMove query * Resolve various PR comments * Move to object storage after optimize * Make upload size configurable and increase default * Prune webtorrent files that are stored in object storage * Move files after transcoding jobs * Fix federation * Add video path manager * Support move to external storage job in client * Fix live object storage tests Co-authored-by: Chocobozzz <me@florianbigard.com>
2021-08-17 08:26:20 +02:00
import {
AllowNull,
BelongsTo,
Column,
CreatedAt,
DataType,
Default,
ForeignKey,
HasMany,
Is,
Model,
Table,
UpdatedAt
} from 'sequelize-typescript'
import { getHLSPublicFileUrl } from '@server/lib/object-storage'
import { VideoFileModel } from '@server/models/video/video-file'
2021-07-23 11:20:00 +02:00
import { MStreamingPlaylist, MVideo } from '@server/types/models'
2021-12-17 13:58:07 +01:00
import { sha1 } from '@shared/extra-utils'
Add support for saving video files to object storage (#4290) * Add support for saving video files to object storage * Add support for custom url generation on s3 stored files Uses two config keys to support url generation that doesn't directly go to (compatible s3). Can be used to generate urls to any cache server or CDN. * Upload files to s3 concurrently and delete originals afterwards * Only publish after move to object storage is complete * Use base url instead of url template * Fix mistyped config field * Add rudenmentary way to download before transcode * Implement Chocobozzz suggestions https://github.com/Chocobozzz/PeerTube/pull/4290#issuecomment-891670478 The remarks in question: Try to use objectStorage prefix instead of s3 prefix for your function/variables/config names Prefer to use a tree for the config: s3.streaming_playlists_bucket -> object_storage.streaming_playlists.bucket Use uppercase for config: S3.STREAMING_PLAYLISTS_BUCKETINFO.bucket -> OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET (maybe BUCKET_NAME instead of BUCKET) I suggest to rename moveJobsRunning to pendingMovingJobs (or better, create a dedicated videoJobInfo table with a pendingMove & videoId columns so we could also use this table to track pending transcoding jobs) https://github.com/Chocobozzz/PeerTube/pull/4290/files#diff-3e26d41ca4bda1de8e1747af70ca2af642abcc1e9e0bfb94239ff2165acfbde5R19 uses a string instead of an integer I think we should store the origin object storage URL in fileUrl, without base_url injection. Instead, inject the base_url at "runtime" so admins can easily change this configuration without running a script to update DB URLs * Import correct function * Support multipart upload * Remove import of node 15.0 module stream/promises * Extend maximum upload job length Using the same value as for redundancy downloading seems logical * Use dynamic part size for really large uploads Also adds very small part size for local testing * Fix decreasePendingMove query * Resolve various PR comments * Move to object storage after optimize * Make upload size configurable and increase default * Prune webtorrent files that are stored in object storage * Move files after transcoding jobs * Fix federation * Add video path manager * Support move to external storage job in client * Fix live object storage tests Co-authored-by: Chocobozzz <me@florianbigard.com>
2021-08-17 08:26:20 +02:00
import { VideoStorage } from '@shared/models'
2021-12-17 13:58:07 +01:00
import { AttributesOnly } from '@shared/typescript-utils'
2019-01-29 08:37:25 +01:00
import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
2019-01-29 08:37:25 +01:00
import { isArrayOf } from '../../helpers/custom-validators/misc'
import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos'
2021-07-23 11:20:00 +02:00
import {
CONSTRAINTS_FIELDS,
MEMOIZE_LENGTH,
MEMOIZE_TTL,
P2P_MEDIA_LOADER_PEER_VERSION,
STATIC_PATHS,
WEBSERVER
} from '../../initializers/constants'
import { VideoRedundancyModel } from '../redundancy/video-redundancy'
import { doesExist } from '../shared'
import { throwIfNotValid } from '../utils'
import { VideoModel } from './video'
2019-01-29 08:37:25 +01:00
@Table({
tableName: 'videoStreamingPlaylist',
indexes: [
{
fields: [ 'videoId' ]
},
{
fields: [ 'videoId', 'type' ],
unique: true
},
{
fields: [ 'p2pMediaLoaderInfohashes' ],
using: 'gin'
}
2019-04-23 09:50:57 +02:00
]
2019-01-29 08:37:25 +01:00
})
2021-05-12 14:09:04 +02:00
export class VideoStreamingPlaylistModel extends Model<Partial<AttributesOnly<VideoStreamingPlaylistModel>>> {
2019-01-29 08:37:25 +01:00
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(false)
@Column
type: VideoStreamingPlaylistType
@AllowNull(false)
2021-07-23 11:20:00 +02:00
@Column
playlistFilename: string
@AllowNull(true)
@Is('PlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'playlist url', true))
2019-01-29 08:37:25 +01:00
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
playlistUrl: string
@AllowNull(false)
@Is('VideoStreamingPlaylistInfoHashes', value => throwIfNotValid(value, v => isArrayOf(v, isVideoFileInfoHashValid), 'info hashes'))
2019-04-23 09:50:57 +02:00
@Column(DataType.ARRAY(DataType.STRING))
2019-01-29 08:37:25 +01:00
p2pMediaLoaderInfohashes: string[]
2019-04-08 11:13:49 +02:00
@AllowNull(false)
@Column
p2pMediaLoaderPeerVersion: number
2019-01-29 08:37:25 +01:00
@AllowNull(false)
2021-07-23 11:20:00 +02:00
@Column
segmentsSha256Filename: string
@AllowNull(true)
@Is('VideoStreamingSegmentsSha256Url', value => throwIfNotValid(value, isActivityPubUrlValid, 'segments sha256 url', true))
2019-01-29 08:37:25 +01:00
@Column
segmentsSha256Url: string
@ForeignKey(() => VideoModel)
@Column
videoId: number
Add support for saving video files to object storage (#4290) * Add support for saving video files to object storage * Add support for custom url generation on s3 stored files Uses two config keys to support url generation that doesn't directly go to (compatible s3). Can be used to generate urls to any cache server or CDN. * Upload files to s3 concurrently and delete originals afterwards * Only publish after move to object storage is complete * Use base url instead of url template * Fix mistyped config field * Add rudenmentary way to download before transcode * Implement Chocobozzz suggestions https://github.com/Chocobozzz/PeerTube/pull/4290#issuecomment-891670478 The remarks in question: Try to use objectStorage prefix instead of s3 prefix for your function/variables/config names Prefer to use a tree for the config: s3.streaming_playlists_bucket -> object_storage.streaming_playlists.bucket Use uppercase for config: S3.STREAMING_PLAYLISTS_BUCKETINFO.bucket -> OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET (maybe BUCKET_NAME instead of BUCKET) I suggest to rename moveJobsRunning to pendingMovingJobs (or better, create a dedicated videoJobInfo table with a pendingMove & videoId columns so we could also use this table to track pending transcoding jobs) https://github.com/Chocobozzz/PeerTube/pull/4290/files#diff-3e26d41ca4bda1de8e1747af70ca2af642abcc1e9e0bfb94239ff2165acfbde5R19 uses a string instead of an integer I think we should store the origin object storage URL in fileUrl, without base_url injection. Instead, inject the base_url at "runtime" so admins can easily change this configuration without running a script to update DB URLs * Import correct function * Support multipart upload * Remove import of node 15.0 module stream/promises * Extend maximum upload job length Using the same value as for redundancy downloading seems logical * Use dynamic part size for really large uploads Also adds very small part size for local testing * Fix decreasePendingMove query * Resolve various PR comments * Move to object storage after optimize * Make upload size configurable and increase default * Prune webtorrent files that are stored in object storage * Move files after transcoding jobs * Fix federation * Add video path manager * Support move to external storage job in client * Fix live object storage tests Co-authored-by: Chocobozzz <me@florianbigard.com>
2021-08-17 08:26:20 +02:00
@AllowNull(false)
@Default(VideoStorage.FILE_SYSTEM)
@Column
storage: VideoStorage
2019-01-29 08:37:25 +01:00
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Video: VideoModel
@HasMany(() => VideoFileModel, {
foreignKey: {
allowNull: true
},
onDelete: 'CASCADE'
})
VideoFiles: VideoFileModel[]
2019-01-29 08:37:25 +01:00
@HasMany(() => VideoRedundancyModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE',
hooks: true
})
RedundancyVideos: VideoRedundancyModel[]
2020-01-03 13:47:45 +01:00
static doesInfohashExistCached = memoizee(VideoStreamingPlaylistModel.doesInfohashExist, {
promise: true,
max: MEMOIZE_LENGTH.INFO_HASH_EXISTS,
maxAge: MEMOIZE_TTL.INFO_HASH_EXISTS
})
2019-01-29 08:37:25 +01:00
static doesInfohashExist (infoHash: string) {
const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE $infoHash = ANY("p2pMediaLoaderInfohashes") LIMIT 1'
2021-07-23 11:20:00 +02:00
return doesExist(query, { infoHash })
2019-01-29 08:37:25 +01:00
}
static buildP2PMediaLoaderInfoHashes (playlistUrl: string, files: unknown[]) {
2019-01-29 08:37:25 +01:00
const hashes: string[] = []
2019-04-08 11:13:49 +02:00
// https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115
for (let i = 0; i < files.length; i++) {
2019-04-08 11:13:49 +02:00
hashes.push(sha1(`${P2P_MEDIA_LOADER_PEER_VERSION}${playlistUrl}+V${i}`))
2019-01-29 08:37:25 +01:00
}
return hashes
}
2019-04-08 11:13:49 +02:00
static listByIncorrectPeerVersion () {
const query = {
where: {
p2pMediaLoaderPeerVersion: {
2019-04-18 11:28:17 +02:00
[Op.ne]: P2P_MEDIA_LOADER_PEER_VERSION
2019-04-08 11:13:49 +02:00
}
2021-07-23 11:20:00 +02:00
},
include: [
{
model: VideoModel.unscoped(),
required: true
}
]
2019-04-08 11:13:49 +02:00
}
return VideoStreamingPlaylistModel.findAll(query)
}
2019-01-29 08:37:25 +01:00
static loadWithVideo (id: number) {
const options = {
include: [
{
model: VideoModel.unscoped(),
required: true
}
]
}
2019-02-21 14:28:06 +01:00
return VideoStreamingPlaylistModel.findByPk(id, options)
2019-01-29 08:37:25 +01:00
}
2021-07-23 11:20:00 +02:00
static loadHLSPlaylistByVideo (videoId: number): Promise<MStreamingPlaylist> {
const options = {
where: {
type: VideoStreamingPlaylistType.HLS,
videoId
}
}
return VideoStreamingPlaylistModel.findOne(options)
}
2021-07-23 11:20:00 +02:00
static async loadOrGenerate (video: MVideo) {
let playlist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
if (!playlist) playlist = new VideoStreamingPlaylistModel()
2019-01-29 08:37:25 +01:00
2021-07-23 11:20:00 +02:00
return Object.assign(playlist, { videoId: video.id, Video: video })
2019-01-29 08:37:25 +01:00
}
2021-12-10 10:28:46 +01:00
static doesOwnedHLSPlaylistExist (videoUUID: string) {
const query = `SELECT 1 FROM "videoStreamingPlaylist" ` +
`INNER JOIN "video" ON "video"."id" = "videoStreamingPlaylist"."videoId" ` +
`AND "video"."remote" IS FALSE AND "video"."uuid" = $videoUUID ` +
`AND "storage" = ${VideoStorage.FILE_SYSTEM} LIMIT 1`
return doesExist(query, { videoUUID })
}
2021-07-23 11:20:00 +02:00
assignP2PMediaLoaderInfoHashes (video: MVideo, files: unknown[]) {
const masterPlaylistUrl = this.getMasterPlaylistUrl(video)
2019-01-29 08:37:25 +01:00
2021-07-23 11:20:00 +02:00
this.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(masterPlaylistUrl, files)
2019-01-29 08:37:25 +01:00
}
2021-07-23 11:20:00 +02:00
getMasterPlaylistUrl (video: MVideo) {
Add support for saving video files to object storage (#4290) * Add support for saving video files to object storage * Add support for custom url generation on s3 stored files Uses two config keys to support url generation that doesn't directly go to (compatible s3). Can be used to generate urls to any cache server or CDN. * Upload files to s3 concurrently and delete originals afterwards * Only publish after move to object storage is complete * Use base url instead of url template * Fix mistyped config field * Add rudenmentary way to download before transcode * Implement Chocobozzz suggestions https://github.com/Chocobozzz/PeerTube/pull/4290#issuecomment-891670478 The remarks in question: Try to use objectStorage prefix instead of s3 prefix for your function/variables/config names Prefer to use a tree for the config: s3.streaming_playlists_bucket -> object_storage.streaming_playlists.bucket Use uppercase for config: S3.STREAMING_PLAYLISTS_BUCKETINFO.bucket -> OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET (maybe BUCKET_NAME instead of BUCKET) I suggest to rename moveJobsRunning to pendingMovingJobs (or better, create a dedicated videoJobInfo table with a pendingMove & videoId columns so we could also use this table to track pending transcoding jobs) https://github.com/Chocobozzz/PeerTube/pull/4290/files#diff-3e26d41ca4bda1de8e1747af70ca2af642abcc1e9e0bfb94239ff2165acfbde5R19 uses a string instead of an integer I think we should store the origin object storage URL in fileUrl, without base_url injection. Instead, inject the base_url at "runtime" so admins can easily change this configuration without running a script to update DB URLs * Import correct function * Support multipart upload * Remove import of node 15.0 module stream/promises * Extend maximum upload job length Using the same value as for redundancy downloading seems logical * Use dynamic part size for really large uploads Also adds very small part size for local testing * Fix decreasePendingMove query * Resolve various PR comments * Move to object storage after optimize * Make upload size configurable and increase default * Prune webtorrent files that are stored in object storage * Move files after transcoding jobs * Fix federation * Add video path manager * Support move to external storage job in client * Fix live object storage tests Co-authored-by: Chocobozzz <me@florianbigard.com>
2021-08-17 08:26:20 +02:00
if (this.storage === VideoStorage.OBJECT_STORAGE) {
return getHLSPublicFileUrl(this.playlistUrl)
}
2021-07-23 11:20:00 +02:00
if (video.isOwned()) return WEBSERVER.URL + this.getMasterPlaylistStaticPath(video.uuid)
return this.playlistUrl
2019-01-29 08:37:25 +01:00
}
2021-07-23 11:20:00 +02:00
getSha256SegmentsUrl (video: MVideo) {
Add support for saving video files to object storage (#4290) * Add support for saving video files to object storage * Add support for custom url generation on s3 stored files Uses two config keys to support url generation that doesn't directly go to (compatible s3). Can be used to generate urls to any cache server or CDN. * Upload files to s3 concurrently and delete originals afterwards * Only publish after move to object storage is complete * Use base url instead of url template * Fix mistyped config field * Add rudenmentary way to download before transcode * Implement Chocobozzz suggestions https://github.com/Chocobozzz/PeerTube/pull/4290#issuecomment-891670478 The remarks in question: Try to use objectStorage prefix instead of s3 prefix for your function/variables/config names Prefer to use a tree for the config: s3.streaming_playlists_bucket -> object_storage.streaming_playlists.bucket Use uppercase for config: S3.STREAMING_PLAYLISTS_BUCKETINFO.bucket -> OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET (maybe BUCKET_NAME instead of BUCKET) I suggest to rename moveJobsRunning to pendingMovingJobs (or better, create a dedicated videoJobInfo table with a pendingMove & videoId columns so we could also use this table to track pending transcoding jobs) https://github.com/Chocobozzz/PeerTube/pull/4290/files#diff-3e26d41ca4bda1de8e1747af70ca2af642abcc1e9e0bfb94239ff2165acfbde5R19 uses a string instead of an integer I think we should store the origin object storage URL in fileUrl, without base_url injection. Instead, inject the base_url at "runtime" so admins can easily change this configuration without running a script to update DB URLs * Import correct function * Support multipart upload * Remove import of node 15.0 module stream/promises * Extend maximum upload job length Using the same value as for redundancy downloading seems logical * Use dynamic part size for really large uploads Also adds very small part size for local testing * Fix decreasePendingMove query * Resolve various PR comments * Move to object storage after optimize * Make upload size configurable and increase default * Prune webtorrent files that are stored in object storage * Move files after transcoding jobs * Fix federation * Add video path manager * Support move to external storage job in client * Fix live object storage tests Co-authored-by: Chocobozzz <me@florianbigard.com>
2021-08-17 08:26:20 +02:00
if (this.storage === VideoStorage.OBJECT_STORAGE) {
return getHLSPublicFileUrl(this.segmentsSha256Url)
}
2021-07-23 11:20:00 +02:00
if (video.isOwned()) return WEBSERVER.URL + this.getSha256SegmentsStaticPath(video.uuid, video.isLive)
2021-07-23 11:20:00 +02:00
return this.segmentsSha256Url
2019-01-29 08:37:25 +01:00
}
getStringType () {
if (this.type === VideoStreamingPlaylistType.HLS) return 'hls'
return 'unknown'
}
getTrackerUrls (baseUrlHttp: string, baseUrlWs: string) {
return [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
}
2019-08-15 11:53:26 +02:00
hasSameUniqueKeysThan (other: MStreamingPlaylist) {
2019-01-29 08:37:25 +01:00
return this.type === other.type &&
this.videoId === other.videoId
}
2021-07-23 11:20:00 +02:00
2021-11-18 14:35:08 +01:00
withVideo (video: MVideo) {
return Object.assign(this, { Video: video })
}
2021-07-23 11:20:00 +02:00
private getMasterPlaylistStaticPath (videoUUID: string) {
return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, this.playlistFilename)
}
private getSha256SegmentsStaticPath (videoUUID: string, isLive: boolean) {
if (isLive) return join('/live', 'segments-sha256', videoUUID)
return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, this.segmentsSha256Filename)
}
2019-01-29 08:37:25 +01:00
}