]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-live.ts
Add latency setting support
[github/Chocobozzz/PeerTube.git] / server / models / video / video-live.ts
1 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, DefaultScope, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
2 import { CONFIG } from '@server/initializers/config'
3 import { WEBSERVER } from '@server/initializers/constants'
4 import { MVideoLive, MVideoLiveVideo } from '@server/types/models'
5 import { LiveVideo, LiveVideoLatencyMode, VideoState } from '@shared/models'
6 import { AttributesOnly } from '@shared/typescript-utils'
7 import { VideoModel } from './video'
8 import { VideoBlacklistModel } from './video-blacklist'
9
10 @DefaultScope(() => ({
11 include: [
12 {
13 model: VideoModel,
14 required: true,
15 include: [
16 {
17 model: VideoBlacklistModel,
18 required: false
19 }
20 ]
21 }
22 ]
23 }))
24 @Table({
25 tableName: 'videoLive',
26 indexes: [
27 {
28 fields: [ 'videoId' ],
29 unique: true
30 }
31 ]
32 })
33 export class VideoLiveModel extends Model<Partial<AttributesOnly<VideoLiveModel>>> {
34
35 @AllowNull(true)
36 @Column(DataType.STRING)
37 streamKey: string
38
39 @AllowNull(false)
40 @Column
41 saveReplay: boolean
42
43 @AllowNull(false)
44 @Column
45 permanentLive: boolean
46
47 @AllowNull(false)
48 @Column
49 latencyMode: LiveVideoLatencyMode
50
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
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 ]
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
104 toFormattedJSON (): LiveVideo {
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
114 return {
115 rtmpUrl,
116 rtmpsUrl,
117
118 streamKey: this.streamKey,
119 permanentLive: this.permanentLive,
120 saveReplay: this.saveReplay,
121 latencyMode: this.latencyMode
122 }
123 }
124 }