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 { 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 { videoLiveAddValidator, videoLiveGetValidator, videoLiveUpdateValidator } from '@server/middlewares/validators/videos/video-live'
11 import { VideoLiveModel } from '@server/models/video/video-live'
12 import { MVideoDetails, MVideoFullLight } from '@server/types/models'
13 import { LiveVideoCreate, LiveVideoUpdate, VideoState } from '../../../../shared'
14 import { logger } from '../../../helpers/logger'
15 import { sequelizeTypescript } from '../../../initializers/database'
16 import { createVideoMiniatureFromExisting } from '../../../lib/thumbnail'
17 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
18 import { VideoModel } from '../../../models/video/video'
19 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
21 const liveRouter = express.Router()
23 const reqVideoFileLive = createReqFiles(
24 [ 'thumbnailfile', 'previewfile' ],
25 MIMETYPES.IMAGE.MIMETYPE_EXT,
27 thumbnailfile: CONFIG.STORAGE.TMP_DIR,
28 previewfile: CONFIG.STORAGE.TMP_DIR
32 liveRouter.post('/live',
35 asyncMiddleware(videoLiveAddValidator),
36 asyncRetryTransactionMiddleware(addLiveVideo)
39 liveRouter.get('/live/:videoId',
41 asyncMiddleware(videoLiveGetValidator),
42 asyncRetryTransactionMiddleware(getLiveVideo)
45 liveRouter.put('/live/:videoId',
47 asyncMiddleware(videoLiveGetValidator),
48 videoLiveUpdateValidator,
49 asyncRetryTransactionMiddleware(updateLiveVideo)
52 // ---------------------------------------------------------------------------
58 // ---------------------------------------------------------------------------
60 async function getLiveVideo (req: express.Request, res: express.Response) {
61 const videoLive = res.locals.videoLive
63 return res.json(videoLive.toFormattedJSON())
66 async function updateLiveVideo (req: express.Request, res: express.Response) {
67 const body: LiveVideoUpdate = req.body
69 const video = res.locals.videoAll
70 const videoLive = res.locals.videoLive
72 videoLive.saveReplay = body.saveReplay || false
73 videoLive.permanentLive = body.permanentLive || false
75 video.VideoLive = await videoLive.save()
77 await federateVideoIfNeeded(video, false)
79 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
82 async function addLiveVideo (req: express.Request, res: express.Response) {
83 const videoInfo: LiveVideoCreate = req.body
85 // Prepare data so we don't block the transaction
86 const videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id)
87 videoData.isLive = true
88 videoData.state = VideoState.WAITING_FOR_LIVE
89 videoData.duration = 0
91 const video = new VideoModel(videoData) as MVideoDetails
92 video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object
94 const videoLive = new VideoLiveModel()
95 videoLive.saveReplay = videoInfo.saveReplay || false
96 videoLive.permanentLive = videoInfo.permanentLive || false
97 videoLive.streamKey = uuidv4()
99 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
103 return createVideoMiniatureFromExisting({
104 inputPath: ASSETS_PATH.DEFAULT_LIVE_BACKGROUND,
107 automaticallyGenerated: true,
113 const { videoCreated } = await sequelizeTypescript.transaction(async t => {
114 const sequelizeOptions = { transaction: t }
116 const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight
118 if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
119 if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t)
121 // Do not forget to add video channel information to the created video
122 videoCreated.VideoChannel = res.locals.videoChannel
124 videoLive.videoId = videoCreated.id
125 videoCreated.VideoLive = await videoLive.save(sequelizeOptions)
127 await setVideoTags({ video, tags: videoInfo.tags, transaction: t })
129 await federateVideoIfNeeded(videoCreated, true, t)
131 logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid)
133 return { videoCreated }
136 Hooks.runAction('action:api.live-video.created', { video: videoCreated })
141 uuid: videoCreated.uuid