]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/live.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / live.ts
CommitLineData
41fb13c3 1import express from 'express'
f443a746 2import { exists } from '@server/helpers/custom-validators/misc'
c6c0fa6c 3import { createReqFiles } from '@server/helpers/express-utils'
26e3e98f 4import { getFormattedObjects } from '@server/helpers/utils'
c6c0fa6c 5import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants'
de94ac86 6import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
af4ae64f 7import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
3cabf353 8import { Hooks } from '@server/lib/plugins/hooks'
1ef65f4c 9import { buildLocalVideoFromReq, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
26e3e98f
C
10import {
11 videoLiveAddValidator,
12 videoLiveFindReplaySessionValidator,
13 videoLiveGetValidator,
14 videoLiveListSessionsValidator,
15 videoLiveUpdateValidator
16} from '@server/middlewares/validators/videos/video-live'
c6c0fa6c 17import { VideoLiveModel } from '@server/models/video/video-live'
26e3e98f 18import { VideoLiveSessionModel } from '@server/models/video/video-live-session'
05a60d85 19import { MVideoDetails, MVideoFullLight, MVideoLive } from '@server/types/models'
0628157f 20import { buildUUID, uuidToShort } from '@shared/extra-utils'
961cbe42 21import { HttpStatusCode, LiveVideoCreate, LiveVideoLatencyMode, LiveVideoUpdate, UserRight, VideoState } from '@shared/models'
c6c0fa6c
C
22import { logger } from '../../../helpers/logger'
23import { sequelizeTypescript } from '../../../initializers/database'
91f8f8db 24import { updateVideoMiniatureFromExisting } from '../../../lib/thumbnail'
961cbe42 25import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, optionalAuthenticate } from '../../../middlewares'
c6c0fa6c 26import { VideoModel } from '../../../models/video/video'
05a60d85 27import { VideoLiveReplaySettingModel } from '@server/models/video/video-live-replay-setting'
c6c0fa6c
C
28
29const liveRouter = express.Router()
30
d3d3deaa 31const reqVideoFileLive = createReqFiles([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
c6c0fa6c
C
32
33liveRouter.post('/live',
34 authenticate,
35 reqVideoFileLive,
36 asyncMiddleware(videoLiveAddValidator),
37 asyncRetryTransactionMiddleware(addLiveVideo)
38)
39
26e3e98f
C
40liveRouter.get('/live/:videoId/sessions',
41 authenticate,
42 asyncMiddleware(videoLiveGetValidator),
43 videoLiveListSessionsValidator,
44 asyncMiddleware(getLiveVideoSessions)
45)
46
c6c0fa6c 47liveRouter.get('/live/:videoId',
961cbe42 48 optionalAuthenticate,
c6c0fa6c 49 asyncMiddleware(videoLiveGetValidator),
98ab5dc8 50 getLiveVideo
b5b68755
C
51)
52
53liveRouter.put('/live/:videoId',
54 authenticate,
55 asyncMiddleware(videoLiveGetValidator),
56 videoLiveUpdateValidator,
57 asyncRetryTransactionMiddleware(updateLiveVideo)
c6c0fa6c
C
58)
59
26e3e98f
C
60liveRouter.get('/:videoId/live-session',
61 asyncMiddleware(videoLiveFindReplaySessionValidator),
62 getLiveReplaySession
63)
64
c6c0fa6c
C
65// ---------------------------------------------------------------------------
66
67export {
68 liveRouter
69}
70
71// ---------------------------------------------------------------------------
72
98ab5dc8 73function getLiveVideo (req: express.Request, res: express.Response) {
c6c0fa6c
C
74 const videoLive = res.locals.videoLive
75
961cbe42
C
76 return res.json(videoLive.toFormattedJSON(canSeePrivateLiveInformation(res)))
77}
78
26e3e98f
C
79function getLiveReplaySession (req: express.Request, res: express.Response) {
80 const session = res.locals.videoLiveSession
81
82 return res.json(session.toFormattedJSON())
83}
84
85async function getLiveVideoSessions (req: express.Request, res: express.Response) {
86 const videoLive = res.locals.videoLive
87
88 const data = await VideoLiveSessionModel.listSessionsOfLiveForAPI({ videoId: videoLive.videoId })
89
90 return res.json(getFormattedObjects(data, data.length))
91}
92
961cbe42
C
93function canSeePrivateLiveInformation (res: express.Response) {
94 const user = res.locals.oauth?.token.User
95 if (!user) return false
96
97 if (user.hasRight(UserRight.GET_ANY_LIVE)) return true
98
99 const video = res.locals.videoAll
100 return video.VideoChannel.Account.userId === user.id
c6c0fa6c
C
101}
102
b5b68755
C
103async function updateLiveVideo (req: express.Request, res: express.Response) {
104 const body: LiveVideoUpdate = req.body
105
af4ae64f 106 const video = res.locals.videoAll
b5b68755 107 const videoLive = res.locals.videoLive
bb4ba6d9 108
05a60d85
W
109 const newReplaySettingModel = await updateReplaySettings(videoLive, body)
110 if (newReplaySettingModel) videoLive.replaySettingId = newReplaySettingModel.id
111 else videoLive.replaySettingId = null
112
f443a746
C
113 if (exists(body.permanentLive)) videoLive.permanentLive = body.permanentLive
114 if (exists(body.latencyMode)) videoLive.latencyMode = body.latencyMode
b5b68755 115
af4ae64f
C
116 video.VideoLive = await videoLive.save()
117
118 await federateVideoIfNeeded(video, false)
b5b68755 119
76148b27 120 return res.status(HttpStatusCode.NO_CONTENT_204).end()
b5b68755
C
121}
122
05a60d85
W
123async function updateReplaySettings (videoLive: MVideoLive, body: LiveVideoUpdate) {
124 if (exists(body.saveReplay)) videoLive.saveReplay = body.saveReplay
125
126 // The live replay is not saved anymore, destroy the old model if it existed
127 if (!videoLive.saveReplay) {
128 if (videoLive.replaySettingId) {
129 await VideoLiveReplaySettingModel.removeSettings(videoLive.replaySettingId)
130 }
131
132 return undefined
133 }
134
135 const settingModel = videoLive.replaySettingId
136 ? await VideoLiveReplaySettingModel.load(videoLive.replaySettingId)
137 : new VideoLiveReplaySettingModel()
138
139 if (exists(body.replaySettings.privacy)) settingModel.privacy = body.replaySettings.privacy
140
141 return settingModel.save()
142}
143
c6c0fa6c 144async function addLiveVideo (req: express.Request, res: express.Response) {
b5b68755 145 const videoInfo: LiveVideoCreate = req.body
c6c0fa6c
C
146
147 // Prepare data so we don't block the transaction
d17d7430
C
148 let videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id)
149 videoData = await Hooks.wrapObject(videoData, 'filter:api.video.live.video-attribute.result')
150
c6c0fa6c 151 videoData.isLive = true
1ef65f4c
C
152 videoData.state = VideoState.WAITING_FOR_LIVE
153 videoData.duration = 0
c6c0fa6c
C
154
155 const video = new VideoModel(videoData) as MVideoDetails
de94ac86 156 video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object
c6c0fa6c 157
1ef65f4c 158 const videoLive = new VideoLiveModel()
b5b68755 159 videoLive.saveReplay = videoInfo.saveReplay || false
bb4ba6d9 160 videoLive.permanentLive = videoInfo.permanentLive || false
f443a746 161 videoLive.latencyMode = videoInfo.latencyMode || LiveVideoLatencyMode.DEFAULT
d4a8e7a6 162 videoLive.streamKey = buildUUID()
c6c0fa6c 163
1ef65f4c
C
164 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
165 video,
166 files: req.files,
167 fallback: type => {
91f8f8db 168 return updateVideoMiniatureFromExisting({
b5b68755
C
169 inputPath: ASSETS_PATH.DEFAULT_LIVE_BACKGROUND,
170 video,
171 type,
172 automaticallyGenerated: true,
173 keepOriginal: true
174 })
1ef65f4c
C
175 }
176 })
c6c0fa6c
C
177
178 const { videoCreated } = await sequelizeTypescript.transaction(async t => {
179 const sequelizeOptions = { transaction: t }
180
181 const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight
182
183 if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
184 if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t)
185
186 // Do not forget to add video channel information to the created video
187 videoCreated.VideoChannel = res.locals.videoChannel
188
05a60d85
W
189 if (videoLive.saveReplay) {
190 const replaySettings = new VideoLiveReplaySettingModel({
191 privacy: videoInfo.replaySettings.privacy
192 })
193 await replaySettings.save(sequelizeOptions)
194
195 videoLive.replaySettingId = replaySettings.id
196 }
197
c6c0fa6c 198 videoLive.videoId = videoCreated.id
af4ae64f 199 videoCreated.VideoLive = await videoLive.save(sequelizeOptions)
c6c0fa6c 200
1ef65f4c 201 await setVideoTags({ video, tags: videoInfo.tags, transaction: t })
c6c0fa6c 202
af4ae64f
C
203 await federateVideoIfNeeded(videoCreated, true, t)
204
c6c0fa6c
C
205 logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid)
206
207 return { videoCreated }
208 })
209
7226e90f 210 Hooks.runAction('action:api.live-video.created', { video: videoCreated, req, res })
3cabf353 211
c6c0fa6c
C
212 return res.json({
213 video: {
214 id: videoCreated.id,
d4a8e7a6 215 shortUUID: uuidToShort(videoCreated.uuid),
c6c0fa6c
C
216 uuid: videoCreated.uuid
217 }
218 })
219}