1 import * as memoizee from 'memoizee'
2 import { join } from 'path'
3 import { Op } from 'sequelize'
4 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, HasMany, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
5 import { VideoFileModel } from '@server/models/video/video-file'
6 import { MStreamingPlaylist, MVideo } from '@server/types/models'
7 import { AttributesOnly } from '@shared/core-utils'
8 import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
9 import { sha1 } from '../../helpers/core-utils'
10 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
11 import { isArrayOf } from '../../helpers/custom-validators/misc'
12 import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos'
17 P2P_MEDIA_LOADER_PEER_VERSION,
20 } from '../../initializers/constants'
21 import { VideoRedundancyModel } from '../redundancy/video-redundancy'
22 import { doesExist } from '../shared'
23 import { throwIfNotValid } from '../utils'
24 import { VideoModel } from './video'
27 tableName: 'videoStreamingPlaylist',
33 fields: [ 'videoId', 'type' ],
37 fields: [ 'p2pMediaLoaderInfohashes' ],
42 export class VideoStreamingPlaylistModel extends Model<Partial<AttributesOnly<VideoStreamingPlaylistModel>>> {
51 type: VideoStreamingPlaylistType
55 playlistFilename: string
58 @Is('PlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'playlist url', true))
59 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
63 @Is('VideoStreamingPlaylistInfoHashes', value => throwIfNotValid(value, v => isArrayOf(v, isVideoFileInfoHashValid), 'info hashes'))
64 @Column(DataType.ARRAY(DataType.STRING))
65 p2pMediaLoaderInfohashes: string[]
69 p2pMediaLoaderPeerVersion: number
73 segmentsSha256Filename: string
76 @Is('VideoStreamingSegmentsSha256Url', value => throwIfNotValid(value, isActivityPubUrlValid, 'segments sha256 url', true))
78 segmentsSha256Url: string
80 @ForeignKey(() => VideoModel)
84 @BelongsTo(() => VideoModel, {
92 @HasMany(() => VideoFileModel, {
98 VideoFiles: VideoFileModel[]
100 @HasMany(() => VideoRedundancyModel, {
107 RedundancyVideos: VideoRedundancyModel[]
109 static doesInfohashExistCached = memoizee(VideoStreamingPlaylistModel.doesInfohashExist, {
111 max: MEMOIZE_LENGTH.INFO_HASH_EXISTS,
112 maxAge: MEMOIZE_TTL.INFO_HASH_EXISTS
115 static doesInfohashExist (infoHash: string) {
116 const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE $infoHash = ANY("p2pMediaLoaderInfohashes") LIMIT 1'
118 return doesExist(query, { infoHash })
121 static buildP2PMediaLoaderInfoHashes (playlistUrl: string, files: unknown[]) {
122 const hashes: string[] = []
124 // https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115
125 for (let i = 0; i < files.length; i++) {
126 hashes.push(sha1(`${P2P_MEDIA_LOADER_PEER_VERSION}${playlistUrl}+V${i}`))
132 static listByIncorrectPeerVersion () {
135 p2pMediaLoaderPeerVersion: {
136 [Op.ne]: P2P_MEDIA_LOADER_PEER_VERSION
141 model: VideoModel.unscoped(),
147 return VideoStreamingPlaylistModel.findAll(query)
150 static loadWithVideo (id: number) {
154 model: VideoModel.unscoped(),
160 return VideoStreamingPlaylistModel.findByPk(id, options)
163 static loadHLSPlaylistByVideo (videoId: number): Promise<MStreamingPlaylist> {
166 type: VideoStreamingPlaylistType.HLS,
171 return VideoStreamingPlaylistModel.findOne(options)
174 static async loadOrGenerate (video: MVideo) {
175 let playlist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
176 if (!playlist) playlist = new VideoStreamingPlaylistModel()
178 return Object.assign(playlist, { videoId: video.id, Video: video })
181 assignP2PMediaLoaderInfoHashes (video: MVideo, files: unknown[]) {
182 const masterPlaylistUrl = this.getMasterPlaylistUrl(video)
184 this.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(masterPlaylistUrl, files)
187 getMasterPlaylistUrl (video: MVideo) {
188 if (video.isOwned()) return WEBSERVER.URL + this.getMasterPlaylistStaticPath(video.uuid)
190 return this.playlistUrl
193 getSha256SegmentsUrl (video: MVideo) {
194 if (video.isOwned()) return WEBSERVER.URL + this.getSha256SegmentsStaticPath(video.uuid, video.isLive)
196 return this.segmentsSha256Url
200 if (this.type === VideoStreamingPlaylistType.HLS) return 'hls'
205 getTrackerUrls (baseUrlHttp: string, baseUrlWs: string) {
206 return [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
209 hasSameUniqueKeysThan (other: MStreamingPlaylist) {
210 return this.type === other.type &&
211 this.videoId === other.videoId
214 private getMasterPlaylistStaticPath (videoUUID: string) {
215 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, this.playlistFilename)
218 private getSha256SegmentsStaticPath (videoUUID: string, isLive: boolean) {
219 if (isLive) return join('/live', 'segments-sha256', videoUUID)
221 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, this.segmentsSha256Filename)