1 import * as express from 'express'
2 import { v4 as uuidv4 } from 'uuid'
3 import { createReqFiles } from '@server/helpers/express-utils'
4 import { CONFIG } from '@server/initializers/config'
5 import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants'
6 import { getVideoActivityPubUrl } from '@server/lib/activitypub/url'
7 import { buildLocalVideoFromReq, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
8 import { videoLiveAddValidator, videoLiveGetValidator, videoLiveUpdateValidator } from '@server/middlewares/validators/videos/video-live'
9 import { VideoLiveModel } from '@server/models/video/video-live'
10 import { MVideoDetails, MVideoFullLight } from '@server/types/models'
11 import { LiveVideoCreate, LiveVideoUpdate, VideoState } from '../../../../shared'
12 import { logger } from '../../../helpers/logger'
13 import { sequelizeTypescript } from '../../../initializers/database'
14 import { createVideoMiniatureFromExisting } from '../../../lib/thumbnail'
15 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
16 import { VideoModel } from '../../../models/video/video'
18 const liveRouter = express.Router()
20 const reqVideoFileLive = createReqFiles(
21 [ 'thumbnailfile', 'previewfile' ],
22 MIMETYPES.IMAGE.MIMETYPE_EXT,
24 thumbnailfile: CONFIG.STORAGE.TMP_DIR,
25 previewfile: CONFIG.STORAGE.TMP_DIR
29 liveRouter.post('/live',
32 asyncMiddleware(videoLiveAddValidator),
33 asyncRetryTransactionMiddleware(addLiveVideo)
36 liveRouter.get('/live/:videoId',
38 asyncMiddleware(videoLiveGetValidator),
39 asyncRetryTransactionMiddleware(getLiveVideo)
42 liveRouter.put('/live/:videoId',
44 asyncMiddleware(videoLiveGetValidator),
45 videoLiveUpdateValidator,
46 asyncRetryTransactionMiddleware(updateLiveVideo)
49 // ---------------------------------------------------------------------------
55 // ---------------------------------------------------------------------------
57 async function getLiveVideo (req: express.Request, res: express.Response) {
58 const videoLive = res.locals.videoLive
60 return res.json(videoLive.toFormattedJSON())
63 async function updateLiveVideo (req: express.Request, res: express.Response) {
64 const body: LiveVideoUpdate = req.body
66 const videoLive = res.locals.videoLive
67 videoLive.saveReplay = body.saveReplay || false
69 await videoLive.save()
71 return res.sendStatus(204)
74 async function addLiveVideo (req: express.Request, res: express.Response) {
75 const videoInfo: LiveVideoCreate = req.body
77 // Prepare data so we don't block the transaction
78 const videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id)
79 videoData.isLive = true
80 videoData.state = VideoState.WAITING_FOR_LIVE
81 videoData.duration = 0
83 const video = new VideoModel(videoData) as MVideoDetails
84 video.url = getVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object
86 const videoLive = new VideoLiveModel()
87 videoLive.saveReplay = videoInfo.saveReplay || false
88 videoLive.streamKey = uuidv4()
90 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
94 return createVideoMiniatureFromExisting({
95 inputPath: ASSETS_PATH.DEFAULT_LIVE_BACKGROUND,
98 automaticallyGenerated: true,
104 const { videoCreated } = await sequelizeTypescript.transaction(async t => {
105 const sequelizeOptions = { transaction: t }
107 const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight
109 if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
110 if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t)
112 // Do not forget to add video channel information to the created video
113 videoCreated.VideoChannel = res.locals.videoChannel
115 videoLive.videoId = videoCreated.id
116 await videoLive.save(sequelizeOptions)
118 await setVideoTags({ video, tags: videoInfo.tags, transaction: t })
120 logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid)
122 return { videoCreated }
128 uuid: videoCreated.uuid