video-streaming-playlist.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, HasMany, Is, Model, Table, UpdatedAt, DataType } from 'sequelize-typescript'
  2. import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos'
  3. import { throwIfNotValid } from '../utils'
  4. import { VideoModel } from './video'
  5. import { VideoRedundancyModel } from '../redundancy/video-redundancy'
  6. import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
  7. import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
  8. import { CONSTRAINTS_FIELDS, STATIC_PATHS, P2P_MEDIA_LOADER_PEER_VERSION } from '../../initializers/constants'
  9. import { VideoFileModel } from './video-file'
  10. import { join } from 'path'
  11. import { sha1 } from '../../helpers/core-utils'
  12. import { isArrayOf } from '../../helpers/custom-validators/misc'
  13. import { QueryTypes, Op } from 'sequelize'
  14. @Table({
  15. tableName: 'videoStreamingPlaylist',
  16. indexes: [
  17. {
  18. fields: [ 'videoId' ]
  19. },
  20. {
  21. fields: [ 'videoId', 'type' ],
  22. unique: true
  23. },
  24. {
  25. fields: [ 'p2pMediaLoaderInfohashes' ],
  26. using: 'gin'
  27. }
  28. ]
  29. })
  30. export class VideoStreamingPlaylistModel extends Model<VideoStreamingPlaylistModel> {
  31. @CreatedAt
  32. createdAt: Date
  33. @UpdatedAt
  34. updatedAt: Date
  35. @AllowNull(false)
  36. @Column
  37. type: VideoStreamingPlaylistType
  38. @AllowNull(false)
  39. @Is('PlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'playlist url'))
  40. @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
  41. playlistUrl: string
  42. @AllowNull(false)
  43. @Is('VideoStreamingPlaylistInfoHashes', value => throwIfNotValid(value, v => isArrayOf(v, isVideoFileInfoHashValid), 'info hashes'))
  44. @Column(DataType.ARRAY(DataType.STRING))
  45. p2pMediaLoaderInfohashes: string[]
  46. @AllowNull(false)
  47. @Column
  48. p2pMediaLoaderPeerVersion: number
  49. @AllowNull(false)
  50. @Is('VideoStreamingSegmentsSha256Url', value => throwIfNotValid(value, isActivityPubUrlValid, 'segments sha256 url'))
  51. @Column
  52. segmentsSha256Url: string
  53. @ForeignKey(() => VideoModel)
  54. @Column
  55. videoId: number
  56. @BelongsTo(() => VideoModel, {
  57. foreignKey: {
  58. allowNull: false
  59. },
  60. onDelete: 'CASCADE'
  61. })
  62. Video: VideoModel
  63. @HasMany(() => VideoRedundancyModel, {
  64. foreignKey: {
  65. allowNull: false
  66. },
  67. onDelete: 'CASCADE',
  68. hooks: true
  69. })
  70. RedundancyVideos: VideoRedundancyModel[]
  71. static doesInfohashExist (infoHash: string) {
  72. const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE $infoHash = ANY("p2pMediaLoaderInfohashes") LIMIT 1'
  73. const options = {
  74. type: QueryTypes.SELECT as QueryTypes.SELECT,
  75. bind: { infoHash },
  76. raw: true
  77. }
  78. return VideoModel.sequelize.query<object>(query, options)
  79. .then(results => results.length === 1)
  80. }
  81. static buildP2PMediaLoaderInfoHashes (playlistUrl: string, videoFiles: VideoFileModel[]) {
  82. const hashes: string[] = []
  83. // https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115
  84. for (let i = 0; i < videoFiles.length; i++) {
  85. hashes.push(sha1(`${P2P_MEDIA_LOADER_PEER_VERSION}${playlistUrl}+V${i}`))
  86. }
  87. return hashes
  88. }
  89. static listByIncorrectPeerVersion () {
  90. const query = {
  91. where: {
  92. p2pMediaLoaderPeerVersion: {
  93. [Op.ne]: P2P_MEDIA_LOADER_PEER_VERSION
  94. }
  95. }
  96. }
  97. return VideoStreamingPlaylistModel.findAll(query)
  98. }
  99. static loadWithVideo (id: number) {
  100. const options = {
  101. include: [
  102. {
  103. model: VideoModel.unscoped(),
  104. required: true
  105. }
  106. ]
  107. }
  108. return VideoStreamingPlaylistModel.findByPk(id, options)
  109. }
  110. static getHlsPlaylistFilename (resolution: number) {
  111. return resolution + '.m3u8'
  112. }
  113. static getMasterHlsPlaylistFilename () {
  114. return 'master.m3u8'
  115. }
  116. static getHlsSha256SegmentsFilename () {
  117. return 'segments-sha256.json'
  118. }
  119. static getHlsVideoName (uuid: string, resolution: number) {
  120. return `${uuid}-${resolution}-fragmented.mp4`
  121. }
  122. static getHlsMasterPlaylistStaticPath (videoUUID: string) {
  123. return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getMasterHlsPlaylistFilename())
  124. }
  125. static getHlsPlaylistStaticPath (videoUUID: string, resolution: number) {
  126. return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
  127. }
  128. static getHlsSha256SegmentsStaticPath (videoUUID: string) {
  129. return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsSha256SegmentsFilename())
  130. }
  131. getStringType () {
  132. if (this.type === VideoStreamingPlaylistType.HLS) return 'hls'
  133. return 'unknown'
  134. }
  135. getVideoRedundancyUrl (baseUrlHttp: string) {
  136. return baseUrlHttp + STATIC_PATHS.REDUNDANCY + this.getStringType() + '/' + this.Video.uuid
  137. }
  138. hasSameUniqueKeysThan (other: VideoStreamingPlaylistModel) {
  139. return this.type === other.type &&
  140. this.videoId === other.videoId
  141. }
  142. }