diff options
author | Chocobozzz <me@florianbigard.com> | 2020-09-17 09:20:52 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-11-09 15:33:04 +0100 |
commit | c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e (patch) | |
tree | 79304b0152b0a38d33b26e65d4acdad0da4032a7 /server/controllers/api/videos/live.ts | |
parent | 110d463fece85e87a26aca48a6048ae0017a27b3 (diff) | |
download | PeerTube-c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e.tar.gz PeerTube-c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e.tar.zst PeerTube-c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e.zip |
Live streaming implementation first step
Diffstat (limited to 'server/controllers/api/videos/live.ts')
-rw-r--r-- | server/controllers/api/videos/live.ts | 116 |
1 files changed, 116 insertions, 0 deletions
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 @@ | |||
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 { videoLiveAddValidator, videoLiveGetValidator } from '@server/middlewares/validators/videos/video-live' | ||
8 | import { VideoLiveModel } from '@server/models/video/video-live' | ||
9 | import { MVideoDetails, MVideoFullLight } from '@server/types/models' | ||
10 | import { VideoCreate, VideoPrivacy, VideoState } from '../../../../shared' | ||
11 | import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type' | ||
12 | import { logger } from '../../../helpers/logger' | ||
13 | import { sequelizeTypescript } from '../../../initializers/database' | ||
14 | import { createVideoMiniatureFromExisting } from '../../../lib/thumbnail' | ||
15 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares' | ||
16 | import { TagModel } from '../../../models/video/tag' | ||
17 | import { VideoModel } from '../../../models/video/video' | ||
18 | import { buildLocalVideoFromCreate } from '@server/lib/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(getVideoLive) | ||
42 | ) | ||
43 | |||
44 | // --------------------------------------------------------------------------- | ||
45 | |||
46 | export { | ||
47 | liveRouter | ||
48 | } | ||
49 | |||
50 | // --------------------------------------------------------------------------- | ||
51 | |||
52 | async function getVideoLive (req: express.Request, res: express.Response) { | ||
53 | const videoLive = res.locals.videoLive | ||
54 | |||
55 | return res.json(videoLive.toFormattedJSON()) | ||
56 | } | ||
57 | |||
58 | async 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 | } | ||