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