]>
Commit | Line | Data |
---|---|---|
1 | import { Transaction } from 'sequelize' | |
2 | import { | |
3 | AllowNull, | |
4 | BeforeDestroy, | |
5 | BelongsTo, | |
6 | Column, | |
7 | CreatedAt, | |
8 | DataType, | |
9 | DefaultScope, | |
10 | ForeignKey, | |
11 | Model, | |
12 | Table, | |
13 | UpdatedAt | |
14 | } from 'sequelize-typescript' | |
15 | import { CONFIG } from '@server/initializers/config' | |
16 | import { WEBSERVER } from '@server/initializers/constants' | |
17 | import { MVideoLive, MVideoLiveVideoWithSetting } from '@server/types/models' | |
18 | import { LiveVideo, LiveVideoLatencyMode, VideoState } from '@shared/models' | |
19 | import { AttributesOnly } from '@shared/typescript-utils' | |
20 | import { VideoModel } from './video' | |
21 | import { VideoBlacklistModel } from './video-blacklist' | |
22 | import { VideoLiveReplaySettingModel } from './video-live-replay-setting' | |
23 | ||
24 | @DefaultScope(() => ({ | |
25 | include: [ | |
26 | { | |
27 | model: VideoModel, | |
28 | required: true, | |
29 | include: [ | |
30 | { | |
31 | model: VideoBlacklistModel, | |
32 | required: false | |
33 | } | |
34 | ] | |
35 | }, | |
36 | { | |
37 | model: VideoLiveReplaySettingModel, | |
38 | required: false | |
39 | } | |
40 | ] | |
41 | })) | |
42 | @Table({ | |
43 | tableName: 'videoLive', | |
44 | indexes: [ | |
45 | { | |
46 | fields: [ 'videoId' ], | |
47 | unique: true | |
48 | }, | |
49 | { | |
50 | fields: [ 'replaySettingId' ], | |
51 | unique: true | |
52 | } | |
53 | ] | |
54 | }) | |
55 | export class VideoLiveModel extends Model<Partial<AttributesOnly<VideoLiveModel>>> { | |
56 | ||
57 | @AllowNull(true) | |
58 | @Column(DataType.STRING) | |
59 | streamKey: string | |
60 | ||
61 | @AllowNull(false) | |
62 | @Column | |
63 | saveReplay: boolean | |
64 | ||
65 | @AllowNull(false) | |
66 | @Column | |
67 | permanentLive: boolean | |
68 | ||
69 | @AllowNull(false) | |
70 | @Column | |
71 | latencyMode: LiveVideoLatencyMode | |
72 | ||
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 | ||
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 | |
104 | static deleteReplaySetting (instance: VideoLiveModel, options: { transaction: Transaction }) { | |
105 | return VideoLiveReplaySettingModel.destroy({ | |
106 | where: { | |
107 | id: instance.replaySettingId | |
108 | }, | |
109 | transaction: options.transaction | |
110 | }) | |
111 | } | |
112 | ||
113 | static loadByStreamKey (streamKey: string) { | |
114 | const query = { | |
115 | where: { | |
116 | streamKey | |
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 | ] | |
131 | }, | |
132 | { | |
133 | model: VideoLiveReplaySettingModel.unscoped(), | |
134 | required: false | |
135 | } | |
136 | ] | |
137 | } | |
138 | ||
139 | return VideoLiveModel.findOne<MVideoLiveVideoWithSetting>(query) | |
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 | ||
152 | toFormattedJSON (canSeePrivateInformation: boolean): LiveVideo { | |
153 | let privateInformation: Pick<LiveVideo, 'rtmpUrl' | 'rtmpsUrl' | 'streamKey'> | {} = {} | |
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 | |
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 | } | |
169 | } | |
170 | ||
171 | const replaySettings = this.replaySettingId | |
172 | ? this.ReplaySetting.toFormattedJSON() | |
173 | : undefined | |
174 | ||
175 | return { | |
176 | ...privateInformation, | |
177 | ||
178 | permanentLive: this.permanentLive, | |
179 | saveReplay: this.saveReplay, | |
180 | replaySettings, | |
181 | latencyMode: this.latencyMode | |
182 | } | |
183 | } | |
184 | } |