PeerTube/server/core/controllers/api/videos/live.ts

208 lines
6.4 KiB
TypeScript
Raw Normal View History

2021-08-27 14:32:44 +02:00
import express from 'express'
import {
HttpStatusCode,
LiveVideoCreate,
LiveVideoUpdate,
2024-02-13 14:23:32 +01:00
ThumbnailType,
UserRight,
VideoState
} from '@peertube/peertube-models'
import { exists } from '@server/helpers/custom-validators/misc.js'
import { createReqFiles } from '@server/helpers/express-utils.js'
import { getFormattedObjects } from '@server/helpers/utils.js'
import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants.js'
import { federateVideoIfNeeded } from '@server/lib/activitypub/videos/index.js'
import { Hooks } from '@server/lib/plugins/hooks.js'
2022-05-03 11:38:07 +02:00
import {
videoLiveAddValidator,
videoLiveFindReplaySessionValidator,
videoLiveGetValidator,
videoLiveListSessionsValidator,
videoLiveUpdateValidator
} from '@server/middlewares/validators/videos/video-live.js'
import { VideoLiveReplaySettingModel } from '@server/models/video/video-live-replay-setting.js'
import { VideoLiveSessionModel } from '@server/models/video/video-live-session.js'
2024-02-13 14:23:32 +01:00
import { MVideoLive } from '@server/types/models/index.js'
import { uuidToShort } from '@peertube/peertube-node-utils'
import { logger, loggerTagsFactory } from '../../../helpers/logger.js'
import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, optionalAuthenticate } from '../../../middlewares/index.js'
2024-02-13 14:23:32 +01:00
import { LocalVideoCreator } from '@server/lib/local-video-creator.js'
import { pick } from '@peertube/peertube-core-utils'
const lTags = loggerTagsFactory('api', 'live')
const liveRouter = express.Router()
const reqVideoFileLive = createReqFiles([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
liveRouter.post('/live',
authenticate,
reqVideoFileLive,
asyncMiddleware(videoLiveAddValidator),
asyncRetryTransactionMiddleware(addLiveVideo)
)
2022-05-03 11:38:07 +02:00
liveRouter.get('/live/:videoId/sessions',
authenticate,
asyncMiddleware(videoLiveGetValidator),
videoLiveListSessionsValidator,
asyncMiddleware(getLiveVideoSessions)
)
liveRouter.get('/live/:videoId',
2022-04-22 09:50:20 +02:00
optionalAuthenticate,
asyncMiddleware(videoLiveGetValidator),
2021-08-25 16:14:11 +02:00
getLiveVideo
2020-10-26 16:44:23 +01:00
)
liveRouter.put('/live/:videoId',
authenticate,
asyncMiddleware(videoLiveGetValidator),
videoLiveUpdateValidator,
asyncRetryTransactionMiddleware(updateLiveVideo)
)
2022-05-03 11:38:07 +02:00
liveRouter.get('/:videoId/live-session',
asyncMiddleware(videoLiveFindReplaySessionValidator),
getLiveReplaySession
)
// ---------------------------------------------------------------------------
export {
liveRouter
}
// ---------------------------------------------------------------------------
2021-08-25 16:14:11 +02:00
function getLiveVideo (req: express.Request, res: express.Response) {
const videoLive = res.locals.videoLive
2022-04-22 09:50:20 +02:00
return res.json(videoLive.toFormattedJSON(canSeePrivateLiveInformation(res)))
}
2022-05-03 11:38:07 +02:00
function getLiveReplaySession (req: express.Request, res: express.Response) {
const session = res.locals.videoLiveSession
return res.json(session.toFormattedJSON())
}
async function getLiveVideoSessions (req: express.Request, res: express.Response) {
const videoLive = res.locals.videoLive
const data = await VideoLiveSessionModel.listSessionsOfLiveForAPI({ videoId: videoLive.videoId })
return res.json(getFormattedObjects(data, data.length))
}
2022-04-22 09:50:20 +02:00
function canSeePrivateLiveInformation (res: express.Response) {
const user = res.locals.oauth?.token.User
if (!user) return false
if (user.hasRight(UserRight.GET_ANY_LIVE)) return true
const video = res.locals.videoAll
return video.VideoChannel.Account.userId === user.id
}
2020-10-26 16:44:23 +01:00
async function updateLiveVideo (req: express.Request, res: express.Response) {
const body: LiveVideoUpdate = req.body
2020-11-02 15:43:44 +01:00
const video = res.locals.videoAll
2020-10-26 16:44:23 +01:00
const videoLive = res.locals.videoLive
2020-12-03 14:10:54 +01:00
const newReplaySettingModel = await updateReplaySettings(videoLive, body)
if (newReplaySettingModel) videoLive.replaySettingId = newReplaySettingModel.id
else videoLive.replaySettingId = null
2022-03-04 13:40:02 +01:00
if (exists(body.permanentLive)) videoLive.permanentLive = body.permanentLive
if (exists(body.latencyMode)) videoLive.latencyMode = body.latencyMode
2020-10-26 16:44:23 +01:00
2020-11-02 15:43:44 +01:00
video.VideoLive = await videoLive.save()
await federateVideoIfNeeded(video, false)
2020-10-26 16:44:23 +01:00
return res.status(HttpStatusCode.NO_CONTENT_204).end()
2020-10-26 16:44:23 +01:00
}
async function updateReplaySettings (videoLive: MVideoLive, body: LiveVideoUpdate) {
if (exists(body.saveReplay)) videoLive.saveReplay = body.saveReplay
// The live replay is not saved anymore, destroy the old model if it existed
if (!videoLive.saveReplay) {
if (videoLive.replaySettingId) {
await VideoLiveReplaySettingModel.removeSettings(videoLive.replaySettingId)
}
return undefined
}
const settingModel = videoLive.replaySettingId
? await VideoLiveReplaySettingModel.load(videoLive.replaySettingId)
: new VideoLiveReplaySettingModel()
if (exists(body.replaySettings.privacy)) settingModel.privacy = body.replaySettings.privacy
return settingModel.save()
}
async function addLiveVideo (req: express.Request, res: express.Response) {
2020-10-26 16:44:23 +01:00
const videoInfo: LiveVideoCreate = req.body
2024-02-13 14:23:32 +01:00
const thumbnails = [ { type: ThumbnailType.MINIATURE, field: 'thumbnailfile' }, { type: ThumbnailType.PREVIEW, field: 'previewfile' } ]
.map(({ type, field }) => {
if (req.files?.[field]?.[0]) {
return {
path: req.files[field][0].path,
type,
automaticallyGenerated: false,
keepOriginal: false
}
}
return {
path: ASSETS_PATH.DEFAULT_LIVE_BACKGROUND,
2020-10-26 16:44:23 +01:00
type,
automaticallyGenerated: true,
keepOriginal: true
2024-02-13 14:23:32 +01:00
}
})
const localVideoCreator = new LocalVideoCreator({
channel: res.locals.videoChannel,
chapters: undefined,
fallbackChapters: {
fromDescription: false,
finalFallback: undefined
},
liveAttributes: pick(videoInfo, [ 'saveReplay', 'permanentLive', 'latencyMode', 'replaySettings' ]),
videoAttributeResultHook: 'filter:api.video.live.video-attribute.result',
lTags,
videoAttributes: {
...videoInfo,
duration: 0,
state: VideoState.WAITING_FOR_LIVE,
isLive: true,
inputFilename: null
2024-02-13 14:23:32 +01:00
},
videoFile: undefined,
2024-02-13 14:23:32 +01:00
user: res.locals.oauth.token.User,
thumbnails
2020-09-17 10:00:46 +02:00
})
2024-02-13 14:23:32 +01:00
const { video } = await localVideoCreator.create()
2024-02-13 14:23:32 +01:00
logger.info('Video live %s with uuid %s created.', videoInfo.name, video.uuid, lTags())
2024-02-13 14:23:32 +01:00
Hooks.runAction('action:api.live-video.created', { video, req, res })
2020-11-06 13:59:50 +01:00
return res.json({
video: {
2024-02-13 14:23:32 +01:00
id: video.id,
shortUUID: uuidToShort(video.uuid),
uuid: video.uuid
}
})
}