]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-live.ts
Implement remote runner jobs in server
[github/Chocobozzz/PeerTube.git] / server / models / video / video-live.ts
CommitLineData
bb7e5605 1import { Transaction } from 'sequelize'
05a60d85 2import {
05a60d85 3 AllowNull,
bb7e5605 4 BeforeDestroy,
05a60d85
W
5 BelongsTo,
6 Column,
7 CreatedAt,
8 DataType,
9 DefaultScope,
10 ForeignKey,
11 Model,
12 Table,
13 UpdatedAt
14} from 'sequelize-typescript'
f443a746 15import { CONFIG } from '@server/initializers/config'
c6c0fa6c 16import { WEBSERVER } from '@server/initializers/constants'
05a60d85 17import { MVideoLive, MVideoLiveVideoWithSetting } from '@server/types/models'
5d9b867e 18import { LiveVideo, LiveVideoLatencyMode, VideoState } from '@shared/models'
6b5f72be 19import { AttributesOnly } from '@shared/typescript-utils'
c6c0fa6c 20import { VideoModel } from './video'
a5cf76af 21import { VideoBlacklistModel } from './video-blacklist'
05a60d85 22import { VideoLiveReplaySettingModel } from './video-live-replay-setting'
c6c0fa6c
C
23
24@DefaultScope(() => ({
25 include: [
26 {
27 model: VideoModel,
a5cf76af
C
28 required: true,
29 include: [
30 {
31 model: VideoBlacklistModel,
32 required: false
33 }
34 ]
05a60d85
W
35 },
36 {
37 model: VideoLiveReplaySettingModel,
38 required: false
c6c0fa6c
C
39 }
40 ]
41}))
42@Table({
43 tableName: 'videoLive',
44 indexes: [
45 {
46 fields: [ 'videoId' ],
47 unique: true
05a60d85
W
48 },
49 {
50 fields: [ 'replaySettingId' ],
51 unique: true
c6c0fa6c
C
52 }
53 ]
54})
16c016e8 55export class VideoLiveModel extends Model<Partial<AttributesOnly<VideoLiveModel>>> {
c6c0fa6c 56
fb719404 57 @AllowNull(true)
c6c0fa6c
C
58 @Column(DataType.STRING)
59 streamKey: string
60
fb719404
C
61 @AllowNull(false)
62 @Column
63 saveReplay: boolean
64
bb4ba6d9
C
65 @AllowNull(false)
66 @Column
67 permanentLive: boolean
68
f443a746
C
69 @AllowNull(false)
70 @Column
71 latencyMode: LiveVideoLatencyMode
72
c6c0fa6c
C
73 @CreatedAt
74 createdAt: Date
75
76 @UpdatedAt
77 updatedAt: Date
78
79 @ForeignKey(() => VideoModel)
80 @Column
81 videoId: number
82
83 @BelongsTo(() => VideoModel, {
84 foreignKey: {
85 allowNull: false
86 },
87 onDelete: 'cascade'
88 })
89 Video: VideoModel
90
05a60d85
W
91 @ForeignKey(() => VideoLiveReplaySettingModel)
92 @Column
93 replaySettingId: number
94
95 @BelongsTo(() => VideoLiveReplaySettingModel, {
96 foreignKey: {
97 allowNull: true
98 },
99 onDelete: 'set null'
100 })
101 ReplaySetting: VideoLiveReplaySettingModel
102
103 @BeforeDestroy
bb7e5605 104 static deleteReplaySetting (instance: VideoLiveModel, options: { transaction: Transaction }) {
05a60d85
W
105 return VideoLiveReplaySettingModel.destroy({
106 where: {
107 id: instance.replaySettingId
bb7e5605
C
108 },
109 transaction: options.transaction
05a60d85
W
110 })
111 }
112
c6c0fa6c
C
113 static loadByStreamKey (streamKey: string) {
114 const query = {
115 where: {
116 streamKey
a5cf76af
C
117 },
118 include: [
119 {
120 model: VideoModel.unscoped(),
121 required: true,
122 where: {
123 state: VideoState.WAITING_FOR_LIVE
124 },
125 include: [
126 {
127 model: VideoBlacklistModel.unscoped(),
128 required: false
129 }
130 ]
05a60d85
W
131 },
132 {
133 model: VideoLiveReplaySettingModel.unscoped(),
134 required: false
a5cf76af
C
135 }
136 ]
c6c0fa6c
C
137 }
138
05a60d85 139 return VideoLiveModel.findOne<MVideoLiveVideoWithSetting>(query)
c6c0fa6c
C
140 }
141
142 static loadByVideoId (videoId: number) {
143 const query = {
144 where: {
145 videoId
146 }
147 }
148
149 return VideoLiveModel.findOne<MVideoLive>(query)
150 }
151
961cbe42
C
152 toFormattedJSON (canSeePrivateInformation: boolean): LiveVideo {
153 let privateInformation: Pick<LiveVideo, 'rtmpUrl' | 'rtmpsUrl' | 'streamKey'> | {} = {}
df1db951
C
154
155 // If we don't have a stream key, it means this is a remote live so we don't specify the rtmp URL
961cbe42
C
156 // We also display these private information only to the live owne/moderators
157 if (this.streamKey && canSeePrivateInformation === true) {
158 privateInformation = {
159 streamKey: this.streamKey,
160
161 rtmpUrl: CONFIG.LIVE.RTMP.ENABLED
162 ? WEBSERVER.RTMP_URL
163 : null,
164
165 rtmpsUrl: CONFIG.LIVE.RTMPS.ENABLED
166 ? WEBSERVER.RTMPS_URL
167 : null
168 }
df1db951
C
169 }
170
05a60d85
W
171 const replaySettings = this.replaySettingId
172 ? this.ReplaySetting.toFormattedJSON()
173 : undefined
174
c6c0fa6c 175 return {
961cbe42 176 ...privateInformation,
af4ae64f 177
bb4ba6d9 178 permanentLive: this.permanentLive,
f443a746 179 saveReplay: this.saveReplay,
05a60d85 180 replaySettings,
f443a746 181 latencyMode: this.latencyMode
c6c0fa6c
C
182 }
183 }
184}