1 import memoizee from 'memoizee'
2 import { join } from 'path'
3 import { Op, Transaction } from 'sequelize'
17 } from 'sequelize-typescript'
18 import { CONFIG } from '@server/initializers/config'
19 import { getHLSPrivateFileUrl, getHLSPublicFileUrl } from '@server/lib/object-storage'
20 import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename } from '@server/lib/paths'
21 import { isVideoInPrivateDirectory } from '@server/lib/video-privacy'
22 import { VideoFileModel } from '@server/models/video/video-file'
23 import { MStreamingPlaylist, MStreamingPlaylistFilesVideo, MVideo } from '@server/types/models'
24 import { sha1 } from '@shared/extra-utils'
25 import { VideoStorage } from '@shared/models'
26 import { AttributesOnly } from '@shared/typescript-utils'
27 import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
28 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
29 import { isArrayOf } from '../../helpers/custom-validators/misc'
30 import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos'
35 P2P_MEDIA_LOADER_PEER_VERSION,
38 } from '../../initializers/constants'
39 import { VideoRedundancyModel } from '../redundancy/video-redundancy'
40 import { doesExist, throwIfNotValid } from '../shared'
41 import { VideoModel } from './video'
44 tableName: 'videoStreamingPlaylist',
50 fields: [ 'videoId', 'type' ],
54 fields: [ 'p2pMediaLoaderInfohashes' ],
59 export class VideoStreamingPlaylistModel extends Model<Partial<AttributesOnly<VideoStreamingPlaylistModel>>> {
68 type: VideoStreamingPlaylistType
72 playlistFilename: string
75 @Is('PlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'playlist url', true))
76 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
80 @Is('VideoStreamingPlaylistInfoHashes', value => throwIfNotValid(value, v => isArrayOf(v, isVideoFileInfoHashValid), 'info hashes'))
81 @Column(DataType.ARRAY(DataType.STRING))
82 p2pMediaLoaderInfohashes: string[]
86 p2pMediaLoaderPeerVersion: number
90 segmentsSha256Filename: string
93 @Is('VideoStreamingSegmentsSha256Url', value => throwIfNotValid(value, isActivityPubUrlValid, 'segments sha256 url', true))
95 segmentsSha256Url: string
97 @ForeignKey(() => VideoModel)
102 @Default(VideoStorage.FILE_SYSTEM)
104 storage: VideoStorage
106 @BelongsTo(() => VideoModel, {
114 @HasMany(() => VideoFileModel, {
120 VideoFiles: VideoFileModel[]
122 @HasMany(() => VideoRedundancyModel, {
129 RedundancyVideos: VideoRedundancyModel[]
131 static doesInfohashExistCached = memoizee(VideoStreamingPlaylistModel.doesInfohashExist, {
133 max: MEMOIZE_LENGTH.INFO_HASH_EXISTS,
134 maxAge: MEMOIZE_TTL.INFO_HASH_EXISTS
137 static doesInfohashExist (infoHash: string) {
138 const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE $infoHash = ANY("p2pMediaLoaderInfohashes") LIMIT 1'
140 return doesExist(this.sequelize, query, { infoHash })
143 static buildP2PMediaLoaderInfoHashes (playlistUrl: string, files: unknown[]) {
144 const hashes: string[] = []
146 // https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115
147 for (let i = 0; i < files.length; i++) {
148 hashes.push(sha1(`${P2P_MEDIA_LOADER_PEER_VERSION}${playlistUrl}+V${i}`))
154 static listByIncorrectPeerVersion () {
157 p2pMediaLoaderPeerVersion: {
158 [Op.ne]: P2P_MEDIA_LOADER_PEER_VERSION
163 model: VideoModel.unscoped(),
169 return VideoStreamingPlaylistModel.findAll(query)
172 static loadWithVideoAndFiles (id: number) {
176 model: VideoModel.unscoped(),
180 model: VideoFileModel.unscoped()
185 return VideoStreamingPlaylistModel.findByPk<MStreamingPlaylistFilesVideo>(id, options)
188 static loadWithVideo (id: number) {
192 model: VideoModel.unscoped(),
198 return VideoStreamingPlaylistModel.findByPk(id, options)
201 static loadHLSPlaylistByVideo (videoId: number, transaction?: Transaction): Promise<MStreamingPlaylist> {
204 type: VideoStreamingPlaylistType.HLS,
210 return VideoStreamingPlaylistModel.findOne(options)
213 static async loadOrGenerate (video: MVideo, transaction?: Transaction) {
214 let playlist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id, transaction)
217 playlist = new VideoStreamingPlaylistModel({
218 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
219 type: VideoStreamingPlaylistType.HLS,
220 storage: VideoStorage.FILE_SYSTEM,
221 p2pMediaLoaderInfohashes: [],
222 playlistFilename: generateHLSMasterPlaylistFilename(video.isLive),
223 segmentsSha256Filename: generateHlsSha256SegmentsFilename(video.isLive),
227 await playlist.save({ transaction })
230 return Object.assign(playlist, { Video: video })
233 static doesOwnedHLSPlaylistExist (videoUUID: string) {
234 const query = `SELECT 1 FROM "videoStreamingPlaylist" ` +
235 `INNER JOIN "video" ON "video"."id" = "videoStreamingPlaylist"."videoId" ` +
236 `AND "video"."remote" IS FALSE AND "video"."uuid" = $videoUUID ` +
237 `AND "storage" = ${VideoStorage.FILE_SYSTEM} LIMIT 1`
239 return doesExist(this.sequelize, query, { videoUUID })
242 assignP2PMediaLoaderInfoHashes (video: MVideo, files: unknown[]) {
243 const masterPlaylistUrl = this.getMasterPlaylistUrl(video)
245 this.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(masterPlaylistUrl, files)
248 // ---------------------------------------------------------------------------
250 getMasterPlaylistUrl (video: MVideo) {
251 if (video.isOwned()) {
252 if (this.storage === VideoStorage.OBJECT_STORAGE) {
253 return this.getMasterPlaylistObjectStorageUrl(video)
256 return WEBSERVER.URL + this.getMasterPlaylistStaticPath(video)
259 return this.playlistUrl
262 private getMasterPlaylistObjectStorageUrl (video: MVideo) {
263 if (video.hasPrivateStaticPath() && CONFIG.OBJECT_STORAGE.PROXY.PROXIFY_PRIVATE_FILES === true) {
264 return getHLSPrivateFileUrl(video, this.playlistFilename)
267 return getHLSPublicFileUrl(this.playlistUrl)
270 // ---------------------------------------------------------------------------
272 getSha256SegmentsUrl (video: MVideo) {
273 if (video.isOwned()) {
274 if (this.storage === VideoStorage.OBJECT_STORAGE) {
275 return this.getSha256SegmentsObjectStorageUrl(video)
278 return WEBSERVER.URL + this.getSha256SegmentsStaticPath(video)
281 return this.segmentsSha256Url
284 private getSha256SegmentsObjectStorageUrl (video: MVideo) {
285 if (video.hasPrivateStaticPath() && CONFIG.OBJECT_STORAGE.PROXY.PROXIFY_PRIVATE_FILES === true) {
286 return getHLSPrivateFileUrl(video, this.segmentsSha256Filename)
289 return getHLSPublicFileUrl(this.segmentsSha256Url)
292 // ---------------------------------------------------------------------------
295 if (this.type === VideoStreamingPlaylistType.HLS) return 'hls'
300 getTrackerUrls (baseUrlHttp: string, baseUrlWs: string) {
301 return [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
304 hasSameUniqueKeysThan (other: MStreamingPlaylist) {
305 return this.type === other.type &&
306 this.videoId === other.videoId
309 withVideo (video: MVideo) {
310 return Object.assign(this, { Video: video })
313 private getMasterPlaylistStaticPath (video: MVideo) {
314 if (isVideoInPrivateDirectory(video.privacy)) {
315 return join(STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS, video.uuid, this.playlistFilename)
318 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.playlistFilename)
321 private getSha256SegmentsStaticPath (video: MVideo) {
322 if (isVideoInPrivateDirectory(video.privacy)) {
323 return join(STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS, video.uuid, this.segmentsSha256Filename)
326 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.segmentsSha256Filename)