]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-streaming-playlist.ts
Add support for saving video files to object storage (#4290)
[github/Chocobozzz/PeerTube.git] / server / models / video / video-streaming-playlist.ts
1 import * as memoizee from 'memoizee'
2 import { join } from 'path'
3 import { Op } 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 { getHLSPublicFileUrl } from '@server/lib/object-storage'
19 import { VideoFileModel } from '@server/models/video/video-file'
20 import { MStreamingPlaylist, MVideo } from '@server/types/models'
21 import { AttributesOnly } from '@shared/core-utils'
22 import { VideoStorage } from '@shared/models'
23 import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
24 import { sha1 } from '../../helpers/core-utils'
25 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
26 import { isArrayOf } from '../../helpers/custom-validators/misc'
27 import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos'
28 import {
29 CONSTRAINTS_FIELDS,
30 MEMOIZE_LENGTH,
31 MEMOIZE_TTL,
32 P2P_MEDIA_LOADER_PEER_VERSION,
33 STATIC_PATHS,
34 WEBSERVER
35 } from '../../initializers/constants'
36 import { VideoRedundancyModel } from '../redundancy/video-redundancy'
37 import { doesExist } from '../shared'
38 import { throwIfNotValid } from '../utils'
39 import { VideoModel } from './video'
40
41 @Table({
42 tableName: 'videoStreamingPlaylist',
43 indexes: [
44 {
45 fields: [ 'videoId' ]
46 },
47 {
48 fields: [ 'videoId', 'type' ],
49 unique: true
50 },
51 {
52 fields: [ 'p2pMediaLoaderInfohashes' ],
53 using: 'gin'
54 }
55 ]
56 })
57 export class VideoStreamingPlaylistModel extends Model<Partial<AttributesOnly<VideoStreamingPlaylistModel>>> {
58 @CreatedAt
59 createdAt: Date
60
61 @UpdatedAt
62 updatedAt: Date
63
64 @AllowNull(false)
65 @Column
66 type: VideoStreamingPlaylistType
67
68 @AllowNull(false)
69 @Column
70 playlistFilename: string
71
72 @AllowNull(true)
73 @Is('PlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'playlist url', true))
74 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
75 playlistUrl: string
76
77 @AllowNull(false)
78 @Is('VideoStreamingPlaylistInfoHashes', value => throwIfNotValid(value, v => isArrayOf(v, isVideoFileInfoHashValid), 'info hashes'))
79 @Column(DataType.ARRAY(DataType.STRING))
80 p2pMediaLoaderInfohashes: string[]
81
82 @AllowNull(false)
83 @Column
84 p2pMediaLoaderPeerVersion: number
85
86 @AllowNull(false)
87 @Column
88 segmentsSha256Filename: string
89
90 @AllowNull(true)
91 @Is('VideoStreamingSegmentsSha256Url', value => throwIfNotValid(value, isActivityPubUrlValid, 'segments sha256 url', true))
92 @Column
93 segmentsSha256Url: string
94
95 @ForeignKey(() => VideoModel)
96 @Column
97 videoId: number
98
99 @AllowNull(false)
100 @Default(VideoStorage.FILE_SYSTEM)
101 @Column
102 storage: VideoStorage
103
104 @BelongsTo(() => VideoModel, {
105 foreignKey: {
106 allowNull: false
107 },
108 onDelete: 'CASCADE'
109 })
110 Video: VideoModel
111
112 @HasMany(() => VideoFileModel, {
113 foreignKey: {
114 allowNull: true
115 },
116 onDelete: 'CASCADE'
117 })
118 VideoFiles: VideoFileModel[]
119
120 @HasMany(() => VideoRedundancyModel, {
121 foreignKey: {
122 allowNull: false
123 },
124 onDelete: 'CASCADE',
125 hooks: true
126 })
127 RedundancyVideos: VideoRedundancyModel[]
128
129 static doesInfohashExistCached = memoizee(VideoStreamingPlaylistModel.doesInfohashExist, {
130 promise: true,
131 max: MEMOIZE_LENGTH.INFO_HASH_EXISTS,
132 maxAge: MEMOIZE_TTL.INFO_HASH_EXISTS
133 })
134
135 static doesInfohashExist (infoHash: string) {
136 const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE $infoHash = ANY("p2pMediaLoaderInfohashes") LIMIT 1'
137
138 return doesExist(query, { infoHash })
139 }
140
141 static buildP2PMediaLoaderInfoHashes (playlistUrl: string, files: unknown[]) {
142 const hashes: string[] = []
143
144 // https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115
145 for (let i = 0; i < files.length; i++) {
146 hashes.push(sha1(`${P2P_MEDIA_LOADER_PEER_VERSION}${playlistUrl}+V${i}`))
147 }
148
149 return hashes
150 }
151
152 static listByIncorrectPeerVersion () {
153 const query = {
154 where: {
155 p2pMediaLoaderPeerVersion: {
156 [Op.ne]: P2P_MEDIA_LOADER_PEER_VERSION
157 }
158 },
159 include: [
160 {
161 model: VideoModel.unscoped(),
162 required: true
163 }
164 ]
165 }
166
167 return VideoStreamingPlaylistModel.findAll(query)
168 }
169
170 static loadWithVideo (id: number) {
171 const options = {
172 include: [
173 {
174 model: VideoModel.unscoped(),
175 required: true
176 }
177 ]
178 }
179
180 return VideoStreamingPlaylistModel.findByPk(id, options)
181 }
182
183 static loadHLSPlaylistByVideo (videoId: number): Promise<MStreamingPlaylist> {
184 const options = {
185 where: {
186 type: VideoStreamingPlaylistType.HLS,
187 videoId
188 }
189 }
190
191 return VideoStreamingPlaylistModel.findOne(options)
192 }
193
194 static async loadOrGenerate (video: MVideo) {
195 let playlist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
196 if (!playlist) playlist = new VideoStreamingPlaylistModel()
197
198 return Object.assign(playlist, { videoId: video.id, Video: video })
199 }
200
201 assignP2PMediaLoaderInfoHashes (video: MVideo, files: unknown[]) {
202 const masterPlaylistUrl = this.getMasterPlaylistUrl(video)
203
204 this.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(masterPlaylistUrl, files)
205 }
206
207 getMasterPlaylistUrl (video: MVideo) {
208 if (this.storage === VideoStorage.OBJECT_STORAGE) {
209 return getHLSPublicFileUrl(this.playlistUrl)
210 }
211
212 if (video.isOwned()) return WEBSERVER.URL + this.getMasterPlaylistStaticPath(video.uuid)
213
214 return this.playlistUrl
215 }
216
217 getSha256SegmentsUrl (video: MVideo) {
218 if (this.storage === VideoStorage.OBJECT_STORAGE) {
219 return getHLSPublicFileUrl(this.segmentsSha256Url)
220 }
221
222 if (video.isOwned()) return WEBSERVER.URL + this.getSha256SegmentsStaticPath(video.uuid, video.isLive)
223
224 return this.segmentsSha256Url
225 }
226
227 getStringType () {
228 if (this.type === VideoStreamingPlaylistType.HLS) return 'hls'
229
230 return 'unknown'
231 }
232
233 getTrackerUrls (baseUrlHttp: string, baseUrlWs: string) {
234 return [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
235 }
236
237 hasSameUniqueKeysThan (other: MStreamingPlaylist) {
238 return this.type === other.type &&
239 this.videoId === other.videoId
240 }
241
242 private getMasterPlaylistStaticPath (videoUUID: string) {
243 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, this.playlistFilename)
244 }
245
246 private getSha256SegmentsStaticPath (videoUUID: string, isLive: boolean) {
247 if (isLive) return join('/live', 'segments-sha256', videoUUID)
248
249 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, this.segmentsSha256Filename)
250 }
251 }