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 | |
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')
-rw-r--r-- | server/controllers/api/config.ts | 39 | ||||
-rw-r--r-- | server/controllers/api/videos/index.ts | 4 | ||||
-rw-r--r-- | server/controllers/api/videos/live.ts | 116 | ||||
-rw-r--r-- | server/controllers/index.ts | 1 | ||||
-rw-r--r-- | server/controllers/live.ts | 29 | ||||
-rw-r--r-- | server/controllers/static.ts | 9 |
6 files changed, 190 insertions, 8 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 | ||
238 | async function updateCustomConfig (req: express.Request, res: express.Response) { | 246 | async 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 | ||
260 | function getRegisteredThemes () { | 268 | function getRegisteredThemes () { |
@@ -268,9 +276,13 @@ function getRegisteredThemes () { | |||
268 | })) | 276 | })) |
269 | } | 277 | } |
270 | 278 | ||
271 | function getEnabledResolutions () { | 279 | function 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' | |||
63 | import { videoCaptionsRouter } from './captions' | 63 | import { videoCaptionsRouter } from './captions' |
64 | import { videoCommentRouter } from './comment' | 64 | import { videoCommentRouter } from './comment' |
65 | import { videoImportsRouter } from './import' | 65 | import { videoImportsRouter } from './import' |
66 | import { liveRouter } from './live' | ||
66 | import { ownershipVideoRouter } from './ownership' | 67 | import { ownershipVideoRouter } from './ownership' |
67 | import { rateVideoRouter } from './rate' | 68 | import { rateVideoRouter } from './rate' |
68 | import { watchingRouter } from './watching' | 69 | import { watchingRouter } from './watching' |
@@ -96,6 +97,7 @@ videosRouter.use('/', videoCaptionsRouter) | |||
96 | videosRouter.use('/', videoImportsRouter) | 97 | videosRouter.use('/', videoImportsRouter) |
97 | videosRouter.use('/', ownershipVideoRouter) | 98 | videosRouter.use('/', ownershipVideoRouter) |
98 | videosRouter.use('/', watchingRouter) | 99 | videosRouter.use('/', watchingRouter) |
100 | videosRouter.use('/', liveRouter) | ||
99 | 101 | ||
100 | videosRouter.get('/categories', listVideoCategories) | 102 | videosRouter.get('/categories', listVideoCategories) |
101 | videosRouter.get('/licences', listVideoLicences) | 103 | videosRouter.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 | ||
310 | async function updateVideo (req: express.Request, res: express.Response) { | 312 | async 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 @@ | |||
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 | } | ||
diff --git a/server/controllers/index.ts b/server/controllers/index.ts index 0d64b33bb..5a199ae9c 100644 --- a/server/controllers/index.ts +++ b/server/controllers/index.ts | |||
@@ -5,6 +5,7 @@ export * from './feeds' | |||
5 | export * from './services' | 5 | export * from './services' |
6 | export * from './static' | 6 | export * from './static' |
7 | export * from './lazy-static' | 7 | export * from './lazy-static' |
8 | export * from './live' | ||
8 | export * from './webfinger' | 9 | export * from './webfinger' |
9 | export * from './tracker' | 10 | export * from './tracker' |
10 | export * from './bots' | 11 | export * from './bots' |
diff --git a/server/controllers/live.ts b/server/controllers/live.ts new file mode 100644 index 000000000..fa4c2cc1a --- /dev/null +++ b/server/controllers/live.ts | |||
@@ -0,0 +1,29 @@ | |||
1 | import * as express from 'express' | ||
2 | import { mapToJSON } from '@server/helpers/core-utils' | ||
3 | import { LiveManager } from '@server/lib/live-manager' | ||
4 | |||
5 | const liveRouter = express.Router() | ||
6 | |||
7 | liveRouter.use('/segments-sha256/:videoUUID', | ||
8 | getSegmentsSha256 | ||
9 | ) | ||
10 | |||
11 | // --------------------------------------------------------------------------- | ||
12 | |||
13 | export { | ||
14 | liveRouter | ||
15 | } | ||
16 | |||
17 | // --------------------------------------------------------------------------- | ||
18 | |||
19 | function getSegmentsSha256 (req: express.Request, res: express.Response) { | ||
20 | const videoUUID = req.params.videoUUID | ||
21 | |||
22 | const result = LiveManager.Instance.getSegmentsSha256(videoUUID) | ||
23 | |||
24 | if (!result) { | ||
25 | return res.sendStatus(404) | ||
26 | } | ||
27 | |||
28 | return res.json(mapToJSON(result)) | ||
29 | } | ||
diff --git a/server/controllers/static.ts b/server/controllers/static.ts index 3f7bbdbae..e04c27b11 100644 --- a/server/controllers/static.ts +++ b/server/controllers/static.ts | |||
@@ -260,7 +260,14 @@ async function generateNodeinfo (req: express.Request, res: express.Response) { | |||
260 | webtorrent: { | 260 | webtorrent: { |
261 | enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED | 261 | enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED |
262 | }, | 262 | }, |
263 | enabledResolutions: getEnabledResolutions() | 263 | enabledResolutions: getEnabledResolutions('vod') |
264 | }, | ||
265 | live: { | ||
266 | enabled: CONFIG.LIVE.ENABLED, | ||
267 | transcoding: { | ||
268 | enabled: CONFIG.LIVE.TRANSCODING.ENABLED, | ||
269 | enabledResolutions: getEnabledResolutions('live') | ||
270 | } | ||
264 | }, | 271 | }, |
265 | import: { | 272 | import: { |
266 | videos: { | 273 | videos: { |