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