]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { exists } from '@server/helpers/custom-validators/misc' | |
3 | import { createReqFiles } from '@server/helpers/express-utils' | |
4 | import { getFormattedObjects } from '@server/helpers/utils' | |
5 | import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants' | |
6 | import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' | |
7 | import { federateVideoIfNeeded } from '@server/lib/activitypub/videos' | |
8 | import { Hooks } from '@server/lib/plugins/hooks' | |
9 | import { buildLocalVideoFromReq, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' | |
10 | import { | |
11 | videoLiveAddValidator, | |
12 | videoLiveFindReplaySessionValidator, | |
13 | videoLiveGetValidator, | |
14 | videoLiveListSessionsValidator, | |
15 | videoLiveUpdateValidator | |
16 | } from '@server/middlewares/validators/videos/video-live' | |
17 | import { VideoLiveModel } from '@server/models/video/video-live' | |
18 | import { VideoLiveSessionModel } from '@server/models/video/video-live-session' | |
19 | import { MVideoDetails, MVideoFullLight } from '@server/types/models' | |
20 | import { buildUUID, uuidToShort } from '@shared/extra-utils' | |
21 | import { HttpStatusCode, LiveVideoCreate, LiveVideoLatencyMode, LiveVideoUpdate, UserRight, VideoState } from '@shared/models' | |
22 | import { logger } from '../../../helpers/logger' | |
23 | import { sequelizeTypescript } from '../../../initializers/database' | |
24 | import { updateVideoMiniatureFromExisting } from '../../../lib/thumbnail' | |
25 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, optionalAuthenticate } from '../../../middlewares' | |
26 | import { VideoModel } from '../../../models/video/video' | |
27 | ||
28 | const liveRouter = express.Router() | |
29 | ||
30 | const reqVideoFileLive = createReqFiles([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT) | |
31 | ||
32 | liveRouter.post('/live', | |
33 | authenticate, | |
34 | reqVideoFileLive, | |
35 | asyncMiddleware(videoLiveAddValidator), | |
36 | asyncRetryTransactionMiddleware(addLiveVideo) | |
37 | ) | |
38 | ||
39 | liveRouter.get('/live/:videoId/sessions', | |
40 | authenticate, | |
41 | asyncMiddleware(videoLiveGetValidator), | |
42 | videoLiveListSessionsValidator, | |
43 | asyncMiddleware(getLiveVideoSessions) | |
44 | ) | |
45 | ||
46 | liveRouter.get('/live/:videoId', | |
47 | optionalAuthenticate, | |
48 | asyncMiddleware(videoLiveGetValidator), | |
49 | getLiveVideo | |
50 | ) | |
51 | ||
52 | liveRouter.put('/live/:videoId', | |
53 | authenticate, | |
54 | asyncMiddleware(videoLiveGetValidator), | |
55 | videoLiveUpdateValidator, | |
56 | asyncRetryTransactionMiddleware(updateLiveVideo) | |
57 | ) | |
58 | ||
59 | liveRouter.get('/:videoId/live-session', | |
60 | asyncMiddleware(videoLiveFindReplaySessionValidator), | |
61 | getLiveReplaySession | |
62 | ) | |
63 | ||
64 | // --------------------------------------------------------------------------- | |
65 | ||
66 | export { | |
67 | liveRouter | |
68 | } | |
69 | ||
70 | // --------------------------------------------------------------------------- | |
71 | ||
72 | function getLiveVideo (req: express.Request, res: express.Response) { | |
73 | const videoLive = res.locals.videoLive | |
74 | ||
75 | return res.json(videoLive.toFormattedJSON(canSeePrivateLiveInformation(res))) | |
76 | } | |
77 | ||
78 | function getLiveReplaySession (req: express.Request, res: express.Response) { | |
79 | const session = res.locals.videoLiveSession | |
80 | ||
81 | return res.json(session.toFormattedJSON()) | |
82 | } | |
83 | ||
84 | async function getLiveVideoSessions (req: express.Request, res: express.Response) { | |
85 | const videoLive = res.locals.videoLive | |
86 | ||
87 | const data = await VideoLiveSessionModel.listSessionsOfLiveForAPI({ videoId: videoLive.videoId }) | |
88 | ||
89 | return res.json(getFormattedObjects(data, data.length)) | |
90 | } | |
91 | ||
92 | function canSeePrivateLiveInformation (res: express.Response) { | |
93 | const user = res.locals.oauth?.token.User | |
94 | if (!user) return false | |
95 | ||
96 | if (user.hasRight(UserRight.GET_ANY_LIVE)) return true | |
97 | ||
98 | const video = res.locals.videoAll | |
99 | return video.VideoChannel.Account.userId === user.id | |
100 | } | |
101 | ||
102 | async function updateLiveVideo (req: express.Request, res: express.Response) { | |
103 | const body: LiveVideoUpdate = req.body | |
104 | ||
105 | const video = res.locals.videoAll | |
106 | const videoLive = res.locals.videoLive | |
107 | ||
108 | if (exists(body.saveReplay)) videoLive.saveReplay = body.saveReplay | |
109 | if (exists(body.permanentLive)) videoLive.permanentLive = body.permanentLive | |
110 | if (exists(body.latencyMode)) videoLive.latencyMode = body.latencyMode | |
111 | ||
112 | video.VideoLive = await videoLive.save() | |
113 | ||
114 | await federateVideoIfNeeded(video, false) | |
115 | ||
116 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
117 | } | |
118 | ||
119 | async function addLiveVideo (req: express.Request, res: express.Response) { | |
120 | const videoInfo: LiveVideoCreate = req.body | |
121 | ||
122 | // Prepare data so we don't block the transaction | |
123 | let videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id) | |
124 | videoData = await Hooks.wrapObject(videoData, 'filter:api.video.live.video-attribute.result') | |
125 | ||
126 | videoData.isLive = true | |
127 | videoData.state = VideoState.WAITING_FOR_LIVE | |
128 | videoData.duration = 0 | |
129 | ||
130 | const video = new VideoModel(videoData) as MVideoDetails | |
131 | video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object | |
132 | ||
133 | const videoLive = new VideoLiveModel() | |
134 | videoLive.saveReplay = videoInfo.saveReplay || false | |
135 | videoLive.permanentLive = videoInfo.permanentLive || false | |
136 | videoLive.latencyMode = videoInfo.latencyMode || LiveVideoLatencyMode.DEFAULT | |
137 | videoLive.streamKey = buildUUID() | |
138 | ||
139 | const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ | |
140 | video, | |
141 | files: req.files, | |
142 | fallback: type => { | |
143 | return updateVideoMiniatureFromExisting({ | |
144 | inputPath: ASSETS_PATH.DEFAULT_LIVE_BACKGROUND, | |
145 | video, | |
146 | type, | |
147 | automaticallyGenerated: true, | |
148 | keepOriginal: true | |
149 | }) | |
150 | } | |
151 | }) | |
152 | ||
153 | const { videoCreated } = await sequelizeTypescript.transaction(async t => { | |
154 | const sequelizeOptions = { transaction: t } | |
155 | ||
156 | const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight | |
157 | ||
158 | if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t) | |
159 | if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t) | |
160 | ||
161 | // Do not forget to add video channel information to the created video | |
162 | videoCreated.VideoChannel = res.locals.videoChannel | |
163 | ||
164 | videoLive.videoId = videoCreated.id | |
165 | videoCreated.VideoLive = await videoLive.save(sequelizeOptions) | |
166 | ||
167 | await setVideoTags({ video, tags: videoInfo.tags, transaction: t }) | |
168 | ||
169 | await federateVideoIfNeeded(videoCreated, true, t) | |
170 | ||
171 | logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid) | |
172 | ||
173 | return { videoCreated } | |
174 | }) | |
175 | ||
176 | Hooks.runAction('action:api.live-video.created', { video: videoCreated, req, res }) | |
177 | ||
178 | return res.json({ | |
179 | video: { | |
180 | id: videoCreated.id, | |
181 | shortUUID: uuidToShort(videoCreated.uuid), | |
182 | uuid: videoCreated.uuid | |
183 | } | |
184 | }) | |
185 | } |