]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-streaming-playlist.ts
Merge branch 'release/4.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / models / video / video-streaming-playlist.ts
1 import memoizee from 'memoizee'
2 import { join } from 'path'
3 import { Op, Transaction } from 'sequelize'
4 import {
5 AllowNull,
6 BelongsTo,
7 Column,
8 CreatedAt,
9 DataType,
10 Default,
11 ForeignKey,
12 HasMany,
13 Is,
14 Model,
15 Table,
16 UpdatedAt
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'
31 import {
32 CONSTRAINTS_FIELDS,
33 MEMOIZE_LENGTH,
34 MEMOIZE_TTL,
35 P2P_MEDIA_LOADER_PEER_VERSION,
36 STATIC_PATHS,
37 WEBSERVER
38 } from '../../initializers/constants'
39 import { VideoRedundancyModel } from '../redundancy/video-redundancy'
40 import { doesExist } from '../shared'
41 import { throwIfNotValid } from '../utils'
42 import { VideoModel } from './video'
43
44 @Table({
45 tableName: 'videoStreamingPlaylist',
46 indexes: [
47 {
48 fields: [ 'videoId' ]
49 },
50 {
51 fields: [ 'videoId', 'type' ],
52 unique: true
53 },
54 {
55 fields: [ 'p2pMediaLoaderInfohashes' ],
56 using: 'gin'
57 }
58 ]
59 })
60 export class VideoStreamingPlaylistModel extends Model<Partial<AttributesOnly<VideoStreamingPlaylistModel>>> {
61 @CreatedAt
62 createdAt: Date
63
64 @UpdatedAt
65 updatedAt: Date
66
67 @AllowNull(false)
68 @Column
69 type: VideoStreamingPlaylistType
70
71 @AllowNull(false)
72 @Column
73 playlistFilename: string
74
75 @AllowNull(true)
76 @Is('PlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'playlist url', true))
77 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
78 playlistUrl: string
79
80 @AllowNull(false)
81 @Is('VideoStreamingPlaylistInfoHashes', value => throwIfNotValid(value, v => isArrayOf(v, isVideoFileInfoHashValid), 'info hashes'))
82 @Column(DataType.ARRAY(DataType.STRING))
83 p2pMediaLoaderInfohashes: string[]
84
85 @AllowNull(false)
86 @Column
87 p2pMediaLoaderPeerVersion: number
88
89 @AllowNull(false)
90 @Column
91 segmentsSha256Filename: string
92
93 @AllowNull(true)
94 @Is('VideoStreamingSegmentsSha256Url', value => throwIfNotValid(value, isActivityPubUrlValid, 'segments sha256 url', true))
95 @Column
96 segmentsSha256Url: string
97
98 @ForeignKey(() => VideoModel)
99 @Column
100 videoId: number
101
102 @AllowNull(false)
103 @Default(VideoStorage.FILE_SYSTEM)
104 @Column
105 storage: VideoStorage
106
107 @BelongsTo(() => VideoModel, {
108 foreignKey: {
109 allowNull: false
110 },
111 onDelete: 'CASCADE'
112 })
113 Video: VideoModel
114
115 @HasMany(() => VideoFileModel, {
116 foreignKey: {
117 allowNull: true
118 },
119 onDelete: 'CASCADE'
120 })
121 VideoFiles: VideoFileModel[]
122
123 @HasMany(() => VideoRedundancyModel, {
124 foreignKey: {
125 allowNull: false
126 },
127 onDelete: 'CASCADE',
128 hooks: true
129 })
130 RedundancyVideos: VideoRedundancyModel[]
131
132 static doesInfohashExistCached = memoizee(VideoStreamingPlaylistModel.doesInfohashExist, {
133 promise: true,
134 max: MEMOIZE_LENGTH.INFO_HASH_EXISTS,
135 maxAge: MEMOIZE_TTL.INFO_HASH_EXISTS
136 })
137
138 static doesInfohashExist (infoHash: string) {
139 const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE $infoHash = ANY("p2pMediaLoaderInfohashes") LIMIT 1'
140
141 return doesExist(query, { infoHash })
142 }
143
144 static buildP2PMediaLoaderInfoHashes (playlistUrl: string, files: unknown[]) {
145 const hashes: string[] = []
146
147 // https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115
148 for (let i = 0; i < files.length; i++) {
149 hashes.push(sha1(`${P2P_MEDIA_LOADER_PEER_VERSION}${playlistUrl}+V${i}`))
150 }
151
152 return hashes
153 }
154
155 static listByIncorrectPeerVersion () {
156 const query = {
157 where: {
158 p2pMediaLoaderPeerVersion: {
159 [Op.ne]: P2P_MEDIA_LOADER_PEER_VERSION
160 }
161 },
162 include: [
163 {
164 model: VideoModel.unscoped(),
165 required: true
166 }
167 ]
168 }
169
170 return VideoStreamingPlaylistModel.findAll(query)
171 }
172
173 static loadWithVideoAndFiles (id: number) {
174 const options = {
175 include: [
176 {
177 model: VideoModel.unscoped(),
178 required: true
179 },
180 {
181 model: VideoFileModel.unscoped()
182 }
183 ]
184 }
185
186 return VideoStreamingPlaylistModel.findByPk<MStreamingPlaylistFilesVideo>(id, options)
187 }
188
189 static loadWithVideo (id: number) {
190 const options = {
191 include: [
192 {
193 model: VideoModel.unscoped(),
194 required: true
195 }
196 ]
197 }
198
199 return VideoStreamingPlaylistModel.findByPk(id, options)
200 }
201
202 static loadHLSPlaylistByVideo (videoId: number, transaction?: Transaction): Promise<MStreamingPlaylist> {
203 const options = {
204 where: {
205 type: VideoStreamingPlaylistType.HLS,
206 videoId
207 },
208 transaction
209 }
210
211 return VideoStreamingPlaylistModel.findOne(options)
212 }
213
214 static async loadOrGenerate (video: MVideo, transaction?: Transaction) {
215 let playlist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id, transaction)
216
217 if (!playlist) {
218 playlist = new VideoStreamingPlaylistModel({
219 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
220 type: VideoStreamingPlaylistType.HLS,
221 storage: VideoStorage.FILE_SYSTEM,
222 p2pMediaLoaderInfohashes: [],
223 playlistFilename: generateHLSMasterPlaylistFilename(video.isLive),
224 segmentsSha256Filename: generateHlsSha256SegmentsFilename(video.isLive),
225 videoId: video.id
226 })
227
228 await playlist.save({ transaction })
229 }
230
231 return Object.assign(playlist, { Video: video })
232 }
233
234 static doesOwnedHLSPlaylistExist (videoUUID: string) {
235 const query = `SELECT 1 FROM "videoStreamingPlaylist" ` +
236 `INNER JOIN "video" ON "video"."id" = "videoStreamingPlaylist"."videoId" ` +
237 `AND "video"."remote" IS FALSE AND "video"."uuid" = $videoUUID ` +
238 `AND "storage" = ${VideoStorage.FILE_SYSTEM} LIMIT 1`
239
240 return doesExist(query, { videoUUID })
241 }
242
243 assignP2PMediaLoaderInfoHashes (video: MVideo, files: unknown[]) {
244 const masterPlaylistUrl = this.getMasterPlaylistUrl(video)
245
246 this.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(masterPlaylistUrl, files)
247 }
248
249 // ---------------------------------------------------------------------------
250
251 getMasterPlaylistUrl (video: MVideo) {
252 if (video.isOwned()) {
253 if (this.storage === VideoStorage.OBJECT_STORAGE) {
254 return this.getMasterPlaylistObjectStorageUrl(video)
255 }
256
257 return WEBSERVER.URL + this.getMasterPlaylistStaticPath(video)
258 }
259
260 return this.playlistUrl
261 }
262
263 private getMasterPlaylistObjectStorageUrl (video: MVideo) {
264 if (video.hasPrivateStaticPath() && CONFIG.OBJECT_STORAGE.PROXY.PROXIFY_PRIVATE_FILES === true) {
265 return getHLSPrivateFileUrl(video, this.playlistFilename)
266 }
267
268 return getHLSPublicFileUrl(this.playlistUrl)
269 }
270
271 // ---------------------------------------------------------------------------
272
273 getSha256SegmentsUrl (video: MVideo) {
274 if (video.isOwned()) {
275 if (this.storage === VideoStorage.OBJECT_STORAGE) {
276 return this.getSha256SegmentsObjectStorageUrl(video)
277 }
278
279 return WEBSERVER.URL + this.getSha256SegmentsStaticPath(video)
280 }
281
282 return this.segmentsSha256Url
283 }
284
285 private getSha256SegmentsObjectStorageUrl (video: MVideo) {
286 if (video.hasPrivateStaticPath() && CONFIG.OBJECT_STORAGE.PROXY.PROXIFY_PRIVATE_FILES === true) {
287 return getHLSPrivateFileUrl(video, this.segmentsSha256Filename)
288 }
289
290 return getHLSPublicFileUrl(this.segmentsSha256Url)
291 }
292
293 // ---------------------------------------------------------------------------
294
295 getStringType () {
296 if (this.type === VideoStreamingPlaylistType.HLS) return 'hls'
297
298 return 'unknown'
299 }
300
301 getTrackerUrls (baseUrlHttp: string, baseUrlWs: string) {
302 return [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
303 }
304
305 hasSameUniqueKeysThan (other: MStreamingPlaylist) {
306 return this.type === other.type &&
307 this.videoId === other.videoId
308 }
309
310 withVideo (video: MVideo) {
311 return Object.assign(this, { Video: video })
312 }
313
314 private getMasterPlaylistStaticPath (video: MVideo) {
315 if (isVideoInPrivateDirectory(video.privacy)) {
316 return join(STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS, video.uuid, this.playlistFilename)
317 }
318
319 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.playlistFilename)
320 }
321
322 private getSha256SegmentsStaticPath (video: MVideo) {
323 if (isVideoInPrivateDirectory(video.privacy)) {
324 return join(STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS, video.uuid, this.segmentsSha256Filename)
325 }
326
327 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.segmentsSha256Filename)
328 }
329 }