1 import express from 'express'
2 import { createReqFiles } from '@server/helpers/express-utils'
3 import { CONFIG } from '@server/initializers/config'
4 import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants'
5 import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
6 import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
7 import { Hooks } from '@server/lib/plugins/hooks'
8 import { buildLocalVideoFromReq, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
9 import { videoLiveAddValidator, videoLiveGetValidator, videoLiveUpdateValidator } from '@server/middlewares/validators/videos/video-live'
10 import { VideoLiveModel } from '@server/models/video/video-live'
11 import { MVideoDetails, MVideoFullLight } from '@server/types/models'
12 import { buildUUID, uuidToShort } from '@shared/core-utils'
13 import { HttpStatusCode, LiveVideoCreate, LiveVideoUpdate, VideoState } from '@shared/models'
14 import { logger } from '../../../helpers/logger'
15 import { sequelizeTypescript } from '../../../initializers/database'
16 import { updateVideoMiniatureFromExisting } from '../../../lib/thumbnail'
17 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
18 import { VideoModel } from '../../../models/video/video'
20 const liveRouter = express.Router()
22 const reqVideoFileLive = createReqFiles(
23 [ 'thumbnailfile', 'previewfile' ],
24 MIMETYPES.IMAGE.MIMETYPE_EXT,
26 thumbnailfile: CONFIG.STORAGE.TMP_DIR,
27 previewfile: CONFIG.STORAGE.TMP_DIR
31 liveRouter.post('/live',
34 asyncMiddleware(videoLiveAddValidator),
35 asyncRetryTransactionMiddleware(addLiveVideo)
38 liveRouter.get('/live/:videoId',
40 asyncMiddleware(videoLiveGetValidator),
44 liveRouter.put('/live/:videoId',
46 asyncMiddleware(videoLiveGetValidator),
47 videoLiveUpdateValidator,
48 asyncRetryTransactionMiddleware(updateLiveVideo)
51 // ---------------------------------------------------------------------------
57 // ---------------------------------------------------------------------------
59 function getLiveVideo (req: express.Request, res: express.Response) {
60 const videoLive = res.locals.videoLive
62 return res.json(videoLive.toFormattedJSON())
65 async function updateLiveVideo (req: express.Request, res: express.Response) {
66 const body: LiveVideoUpdate = req.body
68 const video = res.locals.videoAll
69 const videoLive = res.locals.videoLive
71 videoLive.saveReplay = body.saveReplay || false
72 videoLive.permanentLive = body.permanentLive || false
74 video.VideoLive = await videoLive.save()
76 await federateVideoIfNeeded(video, false)
78 return res.status(HttpStatusCode.NO_CONTENT_204).end()
81 async function addLiveVideo (req: express.Request, res: express.Response) {
82 const videoInfo: LiveVideoCreate = req.body
84 // Prepare data so we don't block the transaction
85 let videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id)
86 videoData = await Hooks.wrapObject(videoData, 'filter:api.video.live.video-attribute.result')
88 videoData.isLive = true
89 videoData.state = VideoState.WAITING_FOR_LIVE
90 videoData.duration = 0
92 const video = new VideoModel(videoData) as MVideoDetails
93 video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object
95 const videoLive = new VideoLiveModel()
96 videoLive.saveReplay = videoInfo.saveReplay || false
97 videoLive.permanentLive = videoInfo.permanentLive || false
98 videoLive.streamKey = buildUUID()
100 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
104 return updateVideoMiniatureFromExisting({
105 inputPath: ASSETS_PATH.DEFAULT_LIVE_BACKGROUND,
108 automaticallyGenerated: true,
114 const { videoCreated } = await sequelizeTypescript.transaction(async t => {
115 const sequelizeOptions = { transaction: t }
117 const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight
119 if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
120 if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t)
122 // Do not forget to add video channel information to the created video
123 videoCreated.VideoChannel = res.locals.videoChannel
125 videoLive.videoId = videoCreated.id
126 videoCreated.VideoLive = await videoLive.save(sequelizeOptions)
128 await setVideoTags({ video, tags: videoInfo.tags, transaction: t })
130 await federateVideoIfNeeded(videoCreated, true, t)
132 logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid)
134 return { videoCreated }
137 Hooks.runAction('action:api.live-video.created', { video: videoCreated, req, res })
142 shortUUID: uuidToShort(videoCreated.uuid),
143 uuid: videoCreated.uuid