]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-live.ts
Add logo licence to README
[github/Chocobozzz/PeerTube.git] / server / models / video / video-live.ts
CommitLineData
c6c0fa6c 1import { AllowNull, BelongsTo, Column, CreatedAt, DataType, DefaultScope, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
f443a746 2import { CONFIG } from '@server/initializers/config'
c6c0fa6c
C
3import { WEBSERVER } from '@server/initializers/constants'
4import { MVideoLive, MVideoLiveVideo } from '@server/types/models'
f443a746 5import { LiveVideo, LiveVideoLatencyMode, VideoState } from '@shared/models'
6b5f72be 6import { AttributesOnly } from '@shared/typescript-utils'
c6c0fa6c 7import { VideoModel } from './video'
a5cf76af 8import { VideoBlacklistModel } from './video-blacklist'
c6c0fa6c
C
9
10@DefaultScope(() => ({
11 include: [
12 {
13 model: VideoModel,
a5cf76af
C
14 required: true,
15 include: [
16 {
17 model: VideoBlacklistModel,
18 required: false
19 }
20 ]
c6c0fa6c
C
21 }
22 ]
23}))
24@Table({
25 tableName: 'videoLive',
26 indexes: [
27 {
28 fields: [ 'videoId' ],
29 unique: true
30 }
31 ]
32})
16c016e8 33export class VideoLiveModel extends Model<Partial<AttributesOnly<VideoLiveModel>>> {
c6c0fa6c 34
fb719404 35 @AllowNull(true)
c6c0fa6c
C
36 @Column(DataType.STRING)
37 streamKey: string
38
fb719404
C
39 @AllowNull(false)
40 @Column
41 saveReplay: boolean
42
bb4ba6d9
C
43 @AllowNull(false)
44 @Column
45 permanentLive: boolean
46
f443a746
C
47 @AllowNull(false)
48 @Column
49 latencyMode: LiveVideoLatencyMode
50
c6c0fa6c
C
51 @CreatedAt
52 createdAt: Date
53
54 @UpdatedAt
55 updatedAt: Date
56
57 @ForeignKey(() => VideoModel)
58 @Column
59 videoId: number
60
61 @BelongsTo(() => VideoModel, {
62 foreignKey: {
63 allowNull: false
64 },
65 onDelete: 'cascade'
66 })
67 Video: VideoModel
68
69 static loadByStreamKey (streamKey: string) {
70 const query = {
71 where: {
72 streamKey
a5cf76af
C
73 },
74 include: [
75 {
76 model: VideoModel.unscoped(),
77 required: true,
78 where: {
79 state: VideoState.WAITING_FOR_LIVE
80 },
81 include: [
82 {
83 model: VideoBlacklistModel.unscoped(),
84 required: false
85 }
86 ]
87 }
88 ]
c6c0fa6c
C
89 }
90
91 return VideoLiveModel.findOne<MVideoLiveVideo>(query)
92 }
93
94 static loadByVideoId (videoId: number) {
95 const query = {
96 where: {
97 videoId
98 }
99 }
100
101 return VideoLiveModel.findOne<MVideoLive>(query)
102 }
103
a5cf76af 104 toFormattedJSON (): LiveVideo {
df1db951
C
105 let rtmpUrl: string = null
106 let rtmpsUrl: string = null
107
108 // If we don't have a stream key, it means this is a remote live so we don't specify the rtmp URL
109 if (this.streamKey) {
110 if (CONFIG.LIVE.RTMP.ENABLED) rtmpUrl = WEBSERVER.RTMP_URL
111 if (CONFIG.LIVE.RTMPS.ENABLED) rtmpsUrl = WEBSERVER.RTMPS_URL
112 }
113
c6c0fa6c 114 return {
df1db951
C
115 rtmpUrl,
116 rtmpsUrl,
af4ae64f 117
b5b68755 118 streamKey: this.streamKey,
bb4ba6d9 119 permanentLive: this.permanentLive,
f443a746
C
120 saveReplay: this.saveReplay,
121 latencyMode: this.latencyMode
c6c0fa6c
C
122 }
123 }
124}