]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-live.ts
Support RTMPS
[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 { WEBSERVER } from '@server/initializers/constants'
3 import { MVideoLive, MVideoLiveVideo } from '@server/types/models'
4 import { AttributesOnly } from '@shared/core-utils'
5 import { LiveVideo, VideoState } from '@shared/models'
6 import { VideoModel } from './video'
7 import { VideoBlacklistModel } from './video-blacklist'
8 import { CONFIG } from '@server/initializers/config'
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 @CreatedAt
48 createdAt: Date
49
50 @UpdatedAt
51 updatedAt: Date
52
53 @ForeignKey(() => VideoModel)
54 @Column
55 videoId: number
56
57 @BelongsTo(() => VideoModel, {
58 foreignKey: {
59 allowNull: false
60 },
61 onDelete: 'cascade'
62 })
63 Video: VideoModel
64
65 static loadByStreamKey (streamKey: string) {
66 const query = {
67 where: {
68 streamKey
69 },
70 include: [
71 {
72 model: VideoModel.unscoped(),
73 required: true,
74 where: {
75 state: VideoState.WAITING_FOR_LIVE
76 },
77 include: [
78 {
79 model: VideoBlacklistModel.unscoped(),
80 required: false
81 }
82 ]
83 }
84 ]
85 }
86
87 return VideoLiveModel.findOne<MVideoLiveVideo>(query)
88 }
89
90 static loadByVideoId (videoId: number) {
91 const query = {
92 where: {
93 videoId
94 }
95 }
96
97 return VideoLiveModel.findOne<MVideoLive>(query)
98 }
99
100 toFormattedJSON (): LiveVideo {
101 let rtmpUrl: string = null
102 let rtmpsUrl: string = null
103
104 // If we don't have a stream key, it means this is a remote live so we don't specify the rtmp URL
105 if (this.streamKey) {
106 if (CONFIG.LIVE.RTMP.ENABLED) rtmpUrl = WEBSERVER.RTMP_URL
107 if (CONFIG.LIVE.RTMPS.ENABLED) rtmpsUrl = WEBSERVER.RTMPS_URL
108 }
109
110 return {
111 rtmpUrl,
112 rtmpsUrl,
113
114 streamKey: this.streamKey,
115 permanentLive: this.permanentLive,
116 saveReplay: this.saveReplay
117 }
118 }
119 }