]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/live.ts
e67d89612828f93c8f8041eb876f8b27ec17ceda
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / live.ts
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
20 const liveRouter = express.Router()
21
22 const reqVideoFileLive = createReqFiles(
23 [ 'thumbnailfile', 'previewfile' ],
24 MIMETYPES.IMAGE.MIMETYPE_EXT,
25 {
26 thumbnailfile: CONFIG.STORAGE.TMP_DIR,
27 previewfile: CONFIG.STORAGE.TMP_DIR
28 }
29 )
30
31 liveRouter.post('/live',
32 authenticate,
33 reqVideoFileLive,
34 asyncMiddleware(videoLiveAddValidator),
35 asyncRetryTransactionMiddleware(addLiveVideo)
36 )
37
38 liveRouter.get('/live/:videoId',
39 authenticate,
40 asyncMiddleware(videoLiveGetValidator),
41 asyncRetryTransactionMiddleware(getLiveVideo)
42 )
43
44 liveRouter.put('/live/:videoId',
45 authenticate,
46 asyncMiddleware(videoLiveGetValidator),
47 videoLiveUpdateValidator,
48 asyncRetryTransactionMiddleware(updateLiveVideo)
49 )
50
51 // ---------------------------------------------------------------------------
52
53 export {
54 liveRouter
55 }
56
57 // ---------------------------------------------------------------------------
58
59 async function getLiveVideo (req: express.Request, res: express.Response) {
60 const videoLive = res.locals.videoLive
61
62 return res.json(videoLive.toFormattedJSON())
63 }
64
65 async function updateLiveVideo (req: express.Request, res: express.Response) {
66 const body: LiveVideoUpdate = req.body
67
68 const video = res.locals.videoAll
69 const videoLive = res.locals.videoLive
70
71 videoLive.saveReplay = body.saveReplay || false
72 videoLive.permanentLive = body.permanentLive || false
73
74 video.VideoLive = await videoLive.save()
75
76 await federateVideoIfNeeded(video, false)
77
78 return res.sendStatus(204)
79 }
80
81 async function addLiveVideo (req: express.Request, res: express.Response) {
82 const videoInfo: LiveVideoCreate = req.body
83
84 // Prepare data so we don't block the transaction
85 const videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id)
86 videoData.isLive = true
87 videoData.state = VideoState.WAITING_FOR_LIVE
88 videoData.duration = 0
89
90 const video = new VideoModel(videoData) as MVideoDetails
91 video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object
92
93 const videoLive = new VideoLiveModel()
94 videoLive.saveReplay = videoInfo.saveReplay || false
95 videoLive.permanentLive = videoInfo.permanentLive || false
96 videoLive.streamKey = uuidv4()
97
98 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
99 video,
100 files: req.files,
101 fallback: type => {
102 return createVideoMiniatureFromExisting({
103 inputPath: ASSETS_PATH.DEFAULT_LIVE_BACKGROUND,
104 video,
105 type,
106 automaticallyGenerated: true,
107 keepOriginal: true
108 })
109 }
110 })
111
112 const { videoCreated } = await sequelizeTypescript.transaction(async t => {
113 const sequelizeOptions = { transaction: t }
114
115 const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight
116
117 if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
118 if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t)
119
120 // Do not forget to add video channel information to the created video
121 videoCreated.VideoChannel = res.locals.videoChannel
122
123 videoLive.videoId = videoCreated.id
124 videoCreated.VideoLive = await videoLive.save(sequelizeOptions)
125
126 await setVideoTags({ video, tags: videoInfo.tags, transaction: t })
127
128 await federateVideoIfNeeded(videoCreated, true, t)
129
130 logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid)
131
132 return { videoCreated }
133 })
134
135 Hooks.runAction('action:api.live-video.created', { video: videoCreated })
136
137 return res.json({
138 video: {
139 id: videoCreated.id,
140 uuid: videoCreated.uuid
141 }
142 })
143 }