2
1

video-live.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { AllowNull, BelongsTo, Column, CreatedAt, DataType, DefaultScope, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
  2. import { WEBSERVER } from '@server/initializers/constants'
  3. import { MVideoLive, MVideoLiveVideo } from '@server/types/models'
  4. import { LiveVideo, VideoState } from '@shared/models'
  5. import { VideoModel } from './video'
  6. import { VideoBlacklistModel } from './video-blacklist'
  7. @DefaultScope(() => ({
  8. include: [
  9. {
  10. model: VideoModel,
  11. required: true,
  12. include: [
  13. {
  14. model: VideoBlacklistModel,
  15. required: false
  16. }
  17. ]
  18. }
  19. ]
  20. }))
  21. @Table({
  22. tableName: 'videoLive',
  23. indexes: [
  24. {
  25. fields: [ 'videoId' ],
  26. unique: true
  27. }
  28. ]
  29. })
  30. export class VideoLiveModel extends Model {
  31. @AllowNull(true)
  32. @Column(DataType.STRING)
  33. streamKey: string
  34. @AllowNull(false)
  35. @Column
  36. saveReplay: boolean
  37. @AllowNull(false)
  38. @Column
  39. permanentLive: boolean
  40. @CreatedAt
  41. createdAt: Date
  42. @UpdatedAt
  43. updatedAt: Date
  44. @ForeignKey(() => VideoModel)
  45. @Column
  46. videoId: number
  47. @BelongsTo(() => VideoModel, {
  48. foreignKey: {
  49. allowNull: false
  50. },
  51. onDelete: 'cascade'
  52. })
  53. Video: VideoModel
  54. static loadByStreamKey (streamKey: string) {
  55. const query = {
  56. where: {
  57. streamKey
  58. },
  59. include: [
  60. {
  61. model: VideoModel.unscoped(),
  62. required: true,
  63. where: {
  64. state: VideoState.WAITING_FOR_LIVE
  65. },
  66. include: [
  67. {
  68. model: VideoBlacklistModel.unscoped(),
  69. required: false
  70. }
  71. ]
  72. }
  73. ]
  74. }
  75. return VideoLiveModel.findOne<MVideoLiveVideo>(query)
  76. }
  77. static loadByVideoId (videoId: number) {
  78. const query = {
  79. where: {
  80. videoId
  81. }
  82. }
  83. return VideoLiveModel.findOne<MVideoLive>(query)
  84. }
  85. toFormattedJSON (): LiveVideo {
  86. return {
  87. // If we don't have a stream key, it means this is a remote live so we don't specify the rtmp URL
  88. rtmpUrl: this.streamKey
  89. ? WEBSERVER.RTMP_URL
  90. : null,
  91. streamKey: this.streamKey,
  92. permanentLive: this.permanentLive,
  93. saveReplay: this.saveReplay
  94. }
  95. }
  96. }