]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
f443a746 | 2 | import { exists } from '@server/helpers/custom-validators/misc' |
c6c0fa6c | 3 | import { createReqFiles } from '@server/helpers/express-utils' |
c6c0fa6c | 4 | import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants' |
de94ac86 | 5 | import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' |
af4ae64f | 6 | import { federateVideoIfNeeded } from '@server/lib/activitypub/videos' |
3cabf353 | 7 | import { Hooks } from '@server/lib/plugins/hooks' |
1ef65f4c | 8 | import { buildLocalVideoFromReq, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' |
b5b68755 | 9 | import { videoLiveAddValidator, videoLiveGetValidator, videoLiveUpdateValidator } from '@server/middlewares/validators/videos/video-live' |
c6c0fa6c C |
10 | import { VideoLiveModel } from '@server/models/video/video-live' |
11 | import { MVideoDetails, MVideoFullLight } from '@server/types/models' | |
0628157f | 12 | import { buildUUID, uuidToShort } from '@shared/extra-utils' |
f443a746 | 13 | import { HttpStatusCode, LiveVideoCreate, LiveVideoLatencyMode, LiveVideoUpdate, VideoState } from '@shared/models' |
c6c0fa6c C |
14 | import { logger } from '../../../helpers/logger' |
15 | import { sequelizeTypescript } from '../../../initializers/database' | |
91f8f8db | 16 | import { updateVideoMiniatureFromExisting } from '../../../lib/thumbnail' |
c6c0fa6c | 17 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares' |
c6c0fa6c | 18 | import { VideoModel } from '../../../models/video/video' |
c6c0fa6c C |
19 | |
20 | const liveRouter = express.Router() | |
21 | ||
d3d3deaa | 22 | const reqVideoFileLive = createReqFiles([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT) |
c6c0fa6c C |
23 | |
24 | liveRouter.post('/live', | |
25 | authenticate, | |
26 | reqVideoFileLive, | |
27 | asyncMiddleware(videoLiveAddValidator), | |
28 | asyncRetryTransactionMiddleware(addLiveVideo) | |
29 | ) | |
30 | ||
31 | liveRouter.get('/live/:videoId', | |
32 | authenticate, | |
33 | asyncMiddleware(videoLiveGetValidator), | |
98ab5dc8 | 34 | getLiveVideo |
b5b68755 C |
35 | ) |
36 | ||
37 | liveRouter.put('/live/:videoId', | |
38 | authenticate, | |
39 | asyncMiddleware(videoLiveGetValidator), | |
40 | videoLiveUpdateValidator, | |
41 | asyncRetryTransactionMiddleware(updateLiveVideo) | |
c6c0fa6c C |
42 | ) |
43 | ||
44 | // --------------------------------------------------------------------------- | |
45 | ||
46 | export { | |
47 | liveRouter | |
48 | } | |
49 | ||
50 | // --------------------------------------------------------------------------- | |
51 | ||
98ab5dc8 | 52 | function getLiveVideo (req: express.Request, res: express.Response) { |
c6c0fa6c C |
53 | const videoLive = res.locals.videoLive |
54 | ||
55 | return res.json(videoLive.toFormattedJSON()) | |
56 | } | |
57 | ||
b5b68755 C |
58 | async function updateLiveVideo (req: express.Request, res: express.Response) { |
59 | const body: LiveVideoUpdate = req.body | |
60 | ||
af4ae64f | 61 | const video = res.locals.videoAll |
b5b68755 | 62 | const videoLive = res.locals.videoLive |
bb4ba6d9 | 63 | |
f443a746 C |
64 | if (exists(body.saveReplay)) videoLive.saveReplay = body.saveReplay |
65 | if (exists(body.permanentLive)) videoLive.permanentLive = body.permanentLive | |
66 | if (exists(body.latencyMode)) videoLive.latencyMode = body.latencyMode | |
b5b68755 | 67 | |
af4ae64f C |
68 | video.VideoLive = await videoLive.save() |
69 | ||
70 | await federateVideoIfNeeded(video, false) | |
b5b68755 | 71 | |
76148b27 | 72 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
b5b68755 C |
73 | } |
74 | ||
c6c0fa6c | 75 | async function addLiveVideo (req: express.Request, res: express.Response) { |
b5b68755 | 76 | const videoInfo: LiveVideoCreate = req.body |
c6c0fa6c C |
77 | |
78 | // Prepare data so we don't block the transaction | |
d17d7430 C |
79 | let videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id) |
80 | videoData = await Hooks.wrapObject(videoData, 'filter:api.video.live.video-attribute.result') | |
81 | ||
c6c0fa6c | 82 | videoData.isLive = true |
1ef65f4c C |
83 | videoData.state = VideoState.WAITING_FOR_LIVE |
84 | videoData.duration = 0 | |
c6c0fa6c C |
85 | |
86 | const video = new VideoModel(videoData) as MVideoDetails | |
de94ac86 | 87 | video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object |
c6c0fa6c | 88 | |
1ef65f4c | 89 | const videoLive = new VideoLiveModel() |
b5b68755 | 90 | videoLive.saveReplay = videoInfo.saveReplay || false |
bb4ba6d9 | 91 | videoLive.permanentLive = videoInfo.permanentLive || false |
f443a746 | 92 | videoLive.latencyMode = videoInfo.latencyMode || LiveVideoLatencyMode.DEFAULT |
d4a8e7a6 | 93 | videoLive.streamKey = buildUUID() |
c6c0fa6c | 94 | |
1ef65f4c C |
95 | const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ |
96 | video, | |
97 | files: req.files, | |
98 | fallback: type => { | |
91f8f8db | 99 | return updateVideoMiniatureFromExisting({ |
b5b68755 C |
100 | inputPath: ASSETS_PATH.DEFAULT_LIVE_BACKGROUND, |
101 | video, | |
102 | type, | |
103 | automaticallyGenerated: true, | |
104 | keepOriginal: true | |
105 | }) | |
1ef65f4c C |
106 | } |
107 | }) | |
c6c0fa6c C |
108 | |
109 | const { videoCreated } = await sequelizeTypescript.transaction(async t => { | |
110 | const sequelizeOptions = { transaction: t } | |
111 | ||
112 | const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight | |
113 | ||
114 | if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t) | |
115 | if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t) | |
116 | ||
117 | // Do not forget to add video channel information to the created video | |
118 | videoCreated.VideoChannel = res.locals.videoChannel | |
119 | ||
120 | videoLive.videoId = videoCreated.id | |
af4ae64f | 121 | videoCreated.VideoLive = await videoLive.save(sequelizeOptions) |
c6c0fa6c | 122 | |
1ef65f4c | 123 | await setVideoTags({ video, tags: videoInfo.tags, transaction: t }) |
c6c0fa6c | 124 | |
af4ae64f C |
125 | await federateVideoIfNeeded(videoCreated, true, t) |
126 | ||
c6c0fa6c C |
127 | logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid) |
128 | ||
129 | return { videoCreated } | |
130 | }) | |
131 | ||
7226e90f | 132 | Hooks.runAction('action:api.live-video.created', { video: videoCreated, req, res }) |
3cabf353 | 133 | |
c6c0fa6c C |
134 | return res.json({ |
135 | video: { | |
136 | id: videoCreated.id, | |
d4a8e7a6 | 137 | shortUUID: uuidToShort(videoCreated.uuid), |
c6c0fa6c C |
138 | uuid: videoCreated.uuid |
139 | } | |
140 | }) | |
141 | } |