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