aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos
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/videos
parent110d463fece85e87a26aca48a6048ae0017a27b3 (diff)
downloadPeerTube-c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e.tar.gz
PeerTube-c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e.tar.zst
PeerTube-c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e.zip
Live streaming implementation first step
Diffstat (limited to 'server/controllers/api/videos')
-rw-r--r--server/controllers/api/videos/index.ts4
-rw-r--r--server/controllers/api/videos/live.ts116
2 files changed, 119 insertions, 1 deletions
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}