aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-09-17 09:20:52 +0200
committerChocobozzz <chocobozzz@cpy.re>2020-11-09 15:33:04 +0100
commitc6c0fa6cd8fe8f752463d8982c3dbcd448739c4e (patch)
tree79304b0152b0a38d33b26e65d4acdad0da4032a7 /server/controllers/api
parent110d463fece85e87a26aca48a6048ae0017a27b3 (diff)
downloadPeerTube-c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e.tar.gz
PeerTube-c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e.tar.zst
PeerTube-c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e.zip
Live streaming implementation first step
Diffstat (limited to 'server/controllers/api')
-rw-r--r--server/controllers/api/config.ts39
-rw-r--r--server/controllers/api/videos/index.ts4
-rw-r--r--server/controllers/api/videos/live.ts116
3 files changed, 152 insertions, 7 deletions
diff --git a/server/controllers/api/config.ts b/server/controllers/api/config.ts
index b80ea4902..bd100ef9c 100644
--- a/server/controllers/api/config.ts
+++ b/server/controllers/api/config.ts
@@ -113,7 +113,15 @@ async function getConfig (req: express.Request, res: express.Response) {
113 webtorrent: { 113 webtorrent: {
114 enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED 114 enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED
115 }, 115 },
116 enabledResolutions: getEnabledResolutions() 116 enabledResolutions: getEnabledResolutions('vod')
117 },
118 live: {
119 enabled: CONFIG.LIVE.ENABLED,
120
121 transcoding: {
122 enabled: CONFIG.LIVE.TRANSCODING.ENABLED,
123 enabledResolutions: getEnabledResolutions('live')
124 }
117 }, 125 },
118 import: { 126 import: {
119 videos: { 127 videos: {
@@ -232,7 +240,7 @@ async function deleteCustomConfig (req: express.Request, res: express.Response)
232 240
233 const data = customConfig() 241 const data = customConfig()
234 242
235 return res.json(data).end() 243 return res.json(data)
236} 244}
237 245
238async function updateCustomConfig (req: express.Request, res: express.Response) { 246async function updateCustomConfig (req: express.Request, res: express.Response) {
@@ -254,7 +262,7 @@ async function updateCustomConfig (req: express.Request, res: express.Response)
254 oldCustomConfigAuditKeys 262 oldCustomConfigAuditKeys
255 ) 263 )
256 264
257 return res.json(data).end() 265 return res.json(data)
258} 266}
259 267
260function getRegisteredThemes () { 268function getRegisteredThemes () {
@@ -268,9 +276,13 @@ function getRegisteredThemes () {
268 })) 276 }))
269} 277}
270 278
271function getEnabledResolutions () { 279function getEnabledResolutions (type: 'vod' | 'live') {
272 return Object.keys(CONFIG.TRANSCODING.RESOLUTIONS) 280 const transcoding = type === 'vod'
273 .filter(key => CONFIG.TRANSCODING.ENABLED && CONFIG.TRANSCODING.RESOLUTIONS[key] === true) 281 ? CONFIG.TRANSCODING
282 : CONFIG.LIVE.TRANSCODING
283
284 return Object.keys(transcoding.RESOLUTIONS)
285 .filter(key => transcoding.ENABLED && transcoding.RESOLUTIONS[key] === true)
274 .map(r => parseInt(r, 10)) 286 .map(r => parseInt(r, 10))
275} 287}
276 288
@@ -411,6 +423,21 @@ function customConfig (): CustomConfig {
411 enabled: CONFIG.TRANSCODING.HLS.ENABLED 423 enabled: CONFIG.TRANSCODING.HLS.ENABLED
412 } 424 }
413 }, 425 },
426 live: {
427 enabled: CONFIG.LIVE.ENABLED,
428 transcoding: {
429 enabled: CONFIG.LIVE.TRANSCODING.ENABLED,
430 threads: CONFIG.LIVE.TRANSCODING.THREADS,
431 resolutions: {
432 '240p': CONFIG.LIVE.TRANSCODING.RESOLUTIONS['240p'],
433 '360p': CONFIG.LIVE.TRANSCODING.RESOLUTIONS['360p'],
434 '480p': CONFIG.LIVE.TRANSCODING.RESOLUTIONS['480p'],
435 '720p': CONFIG.LIVE.TRANSCODING.RESOLUTIONS['720p'],
436 '1080p': CONFIG.LIVE.TRANSCODING.RESOLUTIONS['1080p'],
437 '2160p': CONFIG.LIVE.TRANSCODING.RESOLUTIONS['2160p']
438 }
439 }
440 },
414 import: { 441 import: {
415 videos: { 442 videos: {
416 http: { 443 http: {
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts
index 15b6f214f..94f0361ee 100644
--- a/server/controllers/api/videos/index.ts
+++ b/server/controllers/api/videos/index.ts
@@ -63,6 +63,7 @@ import { blacklistRouter } from './blacklist'
63import { videoCaptionsRouter } from './captions' 63import { videoCaptionsRouter } from './captions'
64import { videoCommentRouter } from './comment' 64import { videoCommentRouter } from './comment'
65import { videoImportsRouter } from './import' 65import { videoImportsRouter } from './import'
66import { liveRouter } from './live'
66import { ownershipVideoRouter } from './ownership' 67import { ownershipVideoRouter } from './ownership'
67import { rateVideoRouter } from './rate' 68import { rateVideoRouter } from './rate'
68import { watchingRouter } from './watching' 69import { watchingRouter } from './watching'
@@ -96,6 +97,7 @@ videosRouter.use('/', videoCaptionsRouter)
96videosRouter.use('/', videoImportsRouter) 97videosRouter.use('/', videoImportsRouter)
97videosRouter.use('/', ownershipVideoRouter) 98videosRouter.use('/', ownershipVideoRouter)
98videosRouter.use('/', watchingRouter) 99videosRouter.use('/', watchingRouter)
100videosRouter.use('/', liveRouter)
99 101
100videosRouter.get('/categories', listVideoCategories) 102videosRouter.get('/categories', listVideoCategories)
101videosRouter.get('/licences', listVideoLicences) 103videosRouter.get('/licences', listVideoLicences)
@@ -304,7 +306,7 @@ async function addVideo (req: express.Request, res: express.Response) {
304 id: videoCreated.id, 306 id: videoCreated.id,
305 uuid: videoCreated.uuid 307 uuid: videoCreated.uuid
306 } 308 }
307 }).end() 309 })
308} 310}
309 311
310async function updateVideo (req: express.Request, res: express.Response) { 312async function updateVideo (req: express.Request, res: express.Response) {
diff --git a/server/controllers/api/videos/live.ts b/server/controllers/api/videos/live.ts
new file mode 100644
index 000000000..d08ef9869
--- /dev/null
+++ b/server/controllers/api/videos/live.ts
@@ -0,0 +1,116 @@
1import * as express from 'express'
2import { v4 as uuidv4 } from 'uuid'
3import { createReqFiles } from '@server/helpers/express-utils'
4import { CONFIG } from '@server/initializers/config'
5import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants'
6import { getVideoActivityPubUrl } from '@server/lib/activitypub/url'
7import { videoLiveAddValidator, videoLiveGetValidator } from '@server/middlewares/validators/videos/video-live'
8import { VideoLiveModel } from '@server/models/video/video-live'
9import { MVideoDetails, MVideoFullLight } from '@server/types/models'
10import { VideoCreate, VideoPrivacy, VideoState } from '../../../../shared'
11import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type'
12import { logger } from '../../../helpers/logger'
13import { sequelizeTypescript } from '../../../initializers/database'
14import { createVideoMiniatureFromExisting } from '../../../lib/thumbnail'
15import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
16import { TagModel } from '../../../models/video/tag'
17import { VideoModel } from '../../../models/video/video'
18import { buildLocalVideoFromCreate } from '@server/lib/video'
19
20const liveRouter = express.Router()
21
22const 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
31liveRouter.post('/live',
32 authenticate,
33 reqVideoFileLive,
34 asyncMiddleware(videoLiveAddValidator),
35 asyncRetryTransactionMiddleware(addLiveVideo)
36)
37
38liveRouter.get('/live/:videoId',
39 authenticate,
40 asyncMiddleware(videoLiveGetValidator),
41 asyncRetryTransactionMiddleware(getVideoLive)
42)
43
44// ---------------------------------------------------------------------------
45
46export {
47 liveRouter
48}
49
50// ---------------------------------------------------------------------------
51
52async function getVideoLive (req: express.Request, res: express.Response) {
53 const videoLive = res.locals.videoLive
54
55 return res.json(videoLive.toFormattedJSON())
56}
57
58async function addLiveVideo (req: express.Request, res: express.Response) {
59 const videoInfo: VideoCreate = req.body
60
61 // Prepare data so we don't block the transaction
62 const videoData = buildLocalVideoFromCreate(videoInfo, res.locals.videoChannel.id)
63 videoData.isLive = true
64
65 const videoLive = new VideoLiveModel()
66 videoLive.streamKey = uuidv4()
67
68 const video = new VideoModel(videoData) as MVideoDetails
69 video.url = getVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object
70
71 // Process thumbnail or create it from the video
72 const thumbnailField = req.files ? req.files['thumbnailfile'] : null
73 const thumbnailModel = thumbnailField
74 ? await createVideoMiniatureFromExisting(thumbnailField[0].path, video, ThumbnailType.MINIATURE, false)
75 : await createVideoMiniatureFromExisting(ASSETS_PATH.DEFAULT_LIVE_BACKGROUND, video, ThumbnailType.MINIATURE, true)
76
77 // Process preview or create it from the video
78 const previewField = req.files ? req.files['previewfile'] : null
79 const previewModel = previewField
80 ? await createVideoMiniatureFromExisting(previewField[0].path, video, ThumbnailType.PREVIEW, false)
81 : await createVideoMiniatureFromExisting(ASSETS_PATH.DEFAULT_LIVE_BACKGROUND, video, ThumbnailType.PREVIEW, true)
82
83 const { videoCreated } = await sequelizeTypescript.transaction(async t => {
84 const sequelizeOptions = { transaction: t }
85
86 const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight
87
88 if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
89 if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t)
90
91 // Do not forget to add video channel information to the created video
92 videoCreated.VideoChannel = res.locals.videoChannel
93
94 videoLive.videoId = videoCreated.id
95 await videoLive.save(sequelizeOptions)
96
97 // Create tags
98 if (videoInfo.tags !== undefined) {
99 const tagInstances = await TagModel.findOrCreateTags(videoInfo.tags, t)
100
101 await video.$set('Tags', tagInstances, sequelizeOptions)
102 video.Tags = tagInstances
103 }
104
105 logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid)
106
107 return { videoCreated }
108 })
109
110 return res.json({
111 video: {
112 id: videoCreated.id,
113 uuid: videoCreated.uuid
114 }
115 })
116}