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 { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
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 { LiveVideoCreate, LiveVideoUpdate, VideoState } from '../../../../shared'
13 import { logger } from '../../../helpers/logger'
14 import { sequelizeTypescript } from '../../../initializers/database'
15 import { createVideoMiniatureFromExisting } from '../../../lib/thumbnail'
16 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
17 import { VideoModel } from '../../../models/video/video'
19 const liveRouter = express.Router()
21 const reqVideoFileLive = createReqFiles(
22 [ 'thumbnailfile', 'previewfile' ],
23 MIMETYPES.IMAGE.MIMETYPE_EXT,
25 thumbnailfile: CONFIG.STORAGE.TMP_DIR,
26 previewfile: CONFIG.STORAGE.TMP_DIR
30 liveRouter.post('/live',
33 asyncMiddleware(videoLiveAddValidator),
34 asyncRetryTransactionMiddleware(addLiveVideo)
37 liveRouter.get('/live/:videoId',
39 asyncMiddleware(videoLiveGetValidator),
40 asyncRetryTransactionMiddleware(getLiveVideo)
43 liveRouter.put('/live/:videoId',
45 asyncMiddleware(videoLiveGetValidator),
46 videoLiveUpdateValidator,
47 asyncRetryTransactionMiddleware(updateLiveVideo)
50 // ---------------------------------------------------------------------------
56 // ---------------------------------------------------------------------------
58 async function getLiveVideo (req: express.Request, res: express.Response) {
59 const videoLive = res.locals.videoLive
61 return res.json(videoLive.toFormattedJSON())
64 async function updateLiveVideo (req: express.Request, res: express.Response) {
65 const body: LiveVideoUpdate = req.body
67 const video = res.locals.videoAll
68 const videoLive = res.locals.videoLive
69 videoLive.saveReplay = body.saveReplay || false
71 video.VideoLive = await videoLive.save()
73 await federateVideoIfNeeded(video, false)
75 return res.sendStatus(204)
78 async function addLiveVideo (req: express.Request, res: express.Response) {
79 const videoInfo: LiveVideoCreate = req.body
81 // Prepare data so we don't block the transaction
82 const videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id)
83 videoData.isLive = true
84 videoData.state = VideoState.WAITING_FOR_LIVE
85 videoData.duration = 0
87 const video = new VideoModel(videoData) as MVideoDetails
88 video.url = getVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object
90 const videoLive = new VideoLiveModel()
91 videoLive.saveReplay = videoInfo.saveReplay || false
92 videoLive.streamKey = uuidv4()
94 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
98 return createVideoMiniatureFromExisting({
99 inputPath: ASSETS_PATH.DEFAULT_LIVE_BACKGROUND,
102 automaticallyGenerated: true,
108 const { videoCreated } = await sequelizeTypescript.transaction(async t => {
109 const sequelizeOptions = { transaction: t }
111 const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight
113 if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
114 if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t)
116 // Do not forget to add video channel information to the created video
117 videoCreated.VideoChannel = res.locals.videoChannel
119 videoLive.videoId = videoCreated.id
120 videoCreated.VideoLive = await videoLive.save(sequelizeOptions)
122 await setVideoTags({ video, tags: videoInfo.tags, transaction: t })
124 await federateVideoIfNeeded(videoCreated, true, t)
126 logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid)
128 return { videoCreated }
134 uuid: videoCreated.uuid