1 import express from 'express'
2 import { exists } from '@server/helpers/custom-validators/misc'
3 import { createReqFiles } from '@server/helpers/express-utils'
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/extra-utils'
13 import { HttpStatusCode, LiveVideoCreate, LiveVideoLatencyMode, 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([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
24 liveRouter.post('/live',
27 asyncMiddleware(videoLiveAddValidator),
28 asyncRetryTransactionMiddleware(addLiveVideo)
31 liveRouter.get('/live/:videoId',
33 asyncMiddleware(videoLiveGetValidator),
37 liveRouter.put('/live/:videoId',
39 asyncMiddleware(videoLiveGetValidator),
40 videoLiveUpdateValidator,
41 asyncRetryTransactionMiddleware(updateLiveVideo)
44 // ---------------------------------------------------------------------------
50 // ---------------------------------------------------------------------------
52 function getLiveVideo (req: express.Request, res: express.Response) {
53 const videoLive = res.locals.videoLive
55 return res.json(videoLive.toFormattedJSON())
58 async function updateLiveVideo (req: express.Request, res: express.Response) {
59 const body: LiveVideoUpdate = req.body
61 const video = res.locals.videoAll
62 const videoLive = res.locals.videoLive
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
68 video.VideoLive = await videoLive.save()
70 await federateVideoIfNeeded(video, false)
72 return res.status(HttpStatusCode.NO_CONTENT_204).end()
75 async function addLiveVideo (req: express.Request, res: express.Response) {
76 const videoInfo: LiveVideoCreate = req.body
78 // Prepare data so we don't block the transaction
79 let videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id)
80 videoData = await Hooks.wrapObject(videoData, 'filter:api.video.live.video-attribute.result')
82 videoData.isLive = true
83 videoData.state = VideoState.WAITING_FOR_LIVE
84 videoData.duration = 0
86 const video = new VideoModel(videoData) as MVideoDetails
87 video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object
89 const videoLive = new VideoLiveModel()
90 videoLive.saveReplay = videoInfo.saveReplay || false
91 videoLive.permanentLive = videoInfo.permanentLive || false
92 videoLive.latencyMode = videoInfo.latencyMode || LiveVideoLatencyMode.DEFAULT
93 videoLive.streamKey = buildUUID()
95 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
99 return updateVideoMiniatureFromExisting({
100 inputPath: ASSETS_PATH.DEFAULT_LIVE_BACKGROUND,
103 automaticallyGenerated: true,
109 const { videoCreated } = await sequelizeTypescript.transaction(async t => {
110 const sequelizeOptions = { transaction: t }
112 const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight
114 if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
115 if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t)
117 // Do not forget to add video channel information to the created video
118 videoCreated.VideoChannel = res.locals.videoChannel
120 videoLive.videoId = videoCreated.id
121 videoCreated.VideoLive = await videoLive.save(sequelizeOptions)
123 await setVideoTags({ video, tags: videoInfo.tags, transaction: t })
125 await federateVideoIfNeeded(videoCreated, true, t)
127 logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid)
129 return { videoCreated }
132 Hooks.runAction('action:api.live-video.created', { video: videoCreated, req, res })
137 shortUUID: uuidToShort(videoCreated.uuid),
138 uuid: videoCreated.uuid