aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-11-06 16:42:23 +0100
committerChocobozzz <chocobozzz@cpy.re>2020-11-09 15:33:04 +0100
commite4bf78561763cd84d22ebceb6f371cccf9a356d8 (patch)
tree7d8ea6ed53810d1dfcc2cfa5e3150da8e87e4645 /server/lib
parent529f037294d9917a62235f8162887a8edc04c32f (diff)
downloadPeerTube-e4bf78561763cd84d22ebceb6f371cccf9a356d8.tar.gz
PeerTube-e4bf78561763cd84d22ebceb6f371cccf9a356d8.tar.zst
PeerTube-e4bf78561763cd84d22ebceb6f371cccf9a356d8.zip
Handle views for live videos
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/activitypub/process/process-view.ts19
-rw-r--r--server/lib/job-queue/handlers/video-live-ending.ts2
-rw-r--r--server/lib/live-manager.ts50
-rw-r--r--server/lib/redis.ts10
4 files changed, 72 insertions, 9 deletions
diff --git a/server/lib/activitypub/process/process-view.ts b/server/lib/activitypub/process/process-view.ts
index cc26180af..efceb21a2 100644
--- a/server/lib/activitypub/process/process-view.ts
+++ b/server/lib/activitypub/process/process-view.ts
@@ -4,6 +4,7 @@ import { Redis } from '../../redis'
4import { ActivityCreate, ActivityView, ViewObject } from '../../../../shared/models/activitypub' 4import { ActivityCreate, ActivityView, ViewObject } from '../../../../shared/models/activitypub'
5import { APProcessorOptions } from '../../../types/activitypub-processor.model' 5import { APProcessorOptions } from '../../../types/activitypub-processor.model'
6import { MActorSignature } from '../../../types/models' 6import { MActorSignature } from '../../../types/models'
7import { LiveManager } from '@server/lib/live-manager'
7 8
8async function processViewActivity (options: APProcessorOptions<ActivityCreate | ActivityView>) { 9async function processViewActivity (options: APProcessorOptions<ActivityCreate | ActivityView>) {
9 const { activity, byActor } = options 10 const { activity, byActor } = options
@@ -19,19 +20,27 @@ export {
19// --------------------------------------------------------------------------- 20// ---------------------------------------------------------------------------
20 21
21async function processCreateView (activity: ActivityView | ActivityCreate, byActor: MActorSignature) { 22async function processCreateView (activity: ActivityView | ActivityCreate, byActor: MActorSignature) {
22 const videoObject = activity.type === 'View' ? activity.object : (activity.object as ViewObject).object 23 const videoObject = activity.type === 'View'
24 ? activity.object
25 : (activity.object as ViewObject).object
23 26
24 const options = { 27 const options = {
25 videoObject, 28 videoObject,
26 fetchType: 'only-immutable-attributes' as 'only-immutable-attributes', 29 fetchType: 'only-video' as 'only-video',
27 allowRefresh: false as false 30 allowRefresh: false as false
28 } 31 }
29 const { video } = await getOrCreateVideoAndAccountAndChannel(options) 32 const { video } = await getOrCreateVideoAndAccountAndChannel(options)
30 33
31 await Redis.Instance.addVideoView(video.id)
32
33 if (video.isOwned()) { 34 if (video.isOwned()) {
34 // Don't resend the activity to the sender 35 // Our live manager will increment the counter and send the view to followers
36 if (video.isLive) {
37 LiveManager.Instance.addViewTo(video.id)
38 return
39 }
40
41 await Redis.Instance.addVideoView(video.id)
42
43 // Forward the view but don't resend the activity to the sender
35 const exceptions = [ byActor ] 44 const exceptions = [ byActor ]
36 await forwardVideoRelatedActivity(activity, undefined, exceptions, video) 45 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
37 } 46 }
diff --git a/server/lib/job-queue/handlers/video-live-ending.ts b/server/lib/job-queue/handlers/video-live-ending.ts
index 55c7a4ccb..599aabf80 100644
--- a/server/lib/job-queue/handlers/video-live-ending.ts
+++ b/server/lib/job-queue/handlers/video-live-ending.ts
@@ -83,6 +83,8 @@ async function saveLive (video: MVideo, live: MVideoLive) {
83 await live.destroy() 83 await live.destroy()
84 84
85 video.isLive = false 85 video.isLive = false
86 // Reinit views
87 video.views = 0
86 video.state = VideoState.TO_TRANSCODE 88 video.state = VideoState.TO_TRANSCODE
87 video.duration = duration 89 video.duration = duration
88 90
diff --git a/server/lib/live-manager.ts b/server/lib/live-manager.ts
index ef9377e43..4a1081a4f 100644
--- a/server/lib/live-manager.ts
+++ b/server/lib/live-manager.ts
@@ -13,7 +13,7 @@ import {
13} from '@server/helpers/ffmpeg-utils' 13} from '@server/helpers/ffmpeg-utils'
14import { logger } from '@server/helpers/logger' 14import { logger } from '@server/helpers/logger'
15import { CONFIG, registerConfigChangedHandler } from '@server/initializers/config' 15import { CONFIG, registerConfigChangedHandler } from '@server/initializers/config'
16import { MEMOIZE_TTL, P2P_MEDIA_LOADER_PEER_VERSION, VIDEO_LIVE, WEBSERVER } from '@server/initializers/constants' 16import { MEMOIZE_TTL, P2P_MEDIA_LOADER_PEER_VERSION, VIDEO_LIVE, VIEW_LIFETIME, WEBSERVER } from '@server/initializers/constants'
17import { UserModel } from '@server/models/account/user' 17import { UserModel } from '@server/models/account/user'
18import { VideoModel } from '@server/models/video/video' 18import { VideoModel } from '@server/models/video/video'
19import { VideoFileModel } from '@server/models/video/video-file' 19import { VideoFileModel } from '@server/models/video/video-file'
@@ -61,6 +61,8 @@ class LiveManager {
61 61
62 private readonly transSessions = new Map<string, FfmpegCommand>() 62 private readonly transSessions = new Map<string, FfmpegCommand>()
63 private readonly videoSessions = new Map<number, string>() 63 private readonly videoSessions = new Map<number, string>()
64 // Values are Date().getTime()
65 private readonly watchersPerVideo = new Map<number, number[]>()
64 private readonly segmentsSha256 = new Map<string, Map<string, string>>() 66 private readonly segmentsSha256 = new Map<string, Map<string, string>>()
65 private readonly livesPerUser = new Map<number, { liveId: number, videoId: number, size: number }[]>() 67 private readonly livesPerUser = new Map<number, { liveId: number, videoId: number, size: number }[]>()
66 68
@@ -115,6 +117,8 @@ class LiveManager {
115 this.stop() 117 this.stop()
116 } 118 }
117 }) 119 })
120
121 setInterval(() => this.updateLiveViews(), VIEW_LIFETIME.LIVE)
118 } 122 }
119 123
120 run () { 124 run () {
@@ -131,6 +135,10 @@ class LiveManager {
131 this.rtmpServer = undefined 135 this.rtmpServer = undefined
132 } 136 }
133 137
138 isRunning () {
139 return !!this.rtmpServer
140 }
141
134 getSegmentsSha256 (videoUUID: string) { 142 getSegmentsSha256 (videoUUID: string) {
135 return this.segmentsSha256.get(videoUUID) 143 return this.segmentsSha256.get(videoUUID)
136 } 144 }
@@ -150,6 +158,19 @@ class LiveManager {
150 return currentLives.reduce((sum, obj) => sum + obj.size, 0) 158 return currentLives.reduce((sum, obj) => sum + obj.size, 0)
151 } 159 }
152 160
161 addViewTo (videoId: number) {
162 if (this.videoSessions.has(videoId) === false) return
163
164 let watchers = this.watchersPerVideo.get(videoId)
165
166 if (!watchers) {
167 watchers = []
168 this.watchersPerVideo.set(videoId, watchers)
169 }
170
171 watchers.push(new Date().getTime())
172 }
173
153 private getContext () { 174 private getContext () {
154 return context 175 return context
155 } 176 }
@@ -331,6 +352,7 @@ class LiveManager {
331 logger.info('RTMP transmuxing for video %s ended. Scheduling cleanup', rtmpUrl) 352 logger.info('RTMP transmuxing for video %s ended. Scheduling cleanup', rtmpUrl)
332 353
333 this.transSessions.delete(sessionId) 354 this.transSessions.delete(sessionId)
355 this.watchersPerVideo.delete(videoLive.videoId)
334 356
335 Promise.all([ tsWatcher.close(), masterWatcher.close() ]) 357 Promise.all([ tsWatcher.close(), masterWatcher.close() ])
336 .catch(err => logger.error('Cannot close watchers of %s.', outPath, { err })) 358 .catch(err => logger.error('Cannot close watchers of %s.', outPath, { err }))
@@ -426,6 +448,32 @@ class LiveManager {
426 return this.isAbleToUploadVideoWithCache(user.id) 448 return this.isAbleToUploadVideoWithCache(user.id)
427 } 449 }
428 450
451 private async updateLiveViews () {
452 if (!this.isRunning()) return
453
454 logger.info('Updating live video views.')
455
456 for (const videoId of this.watchersPerVideo.keys()) {
457 const notBefore = new Date().getTime() - VIEW_LIFETIME.LIVE
458
459 const watchers = this.watchersPerVideo.get(videoId)
460
461 const numWatchers = watchers.length
462
463 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
464 video.views = numWatchers
465 await video.save()
466
467 await federateVideoIfNeeded(video, false)
468
469 // Only keep not expired watchers
470 const newWatchers = watchers.filter(w => w > notBefore)
471 this.watchersPerVideo.set(videoId, newWatchers)
472
473 logger.debug('New live video views for %s is %d.', video.url, numWatchers)
474 }
475 }
476
429 static get Instance () { 477 static get Instance () {
430 return this.instance || (this.instance = new this()) 478 return this.instance || (this.instance = new this())
431 } 479 }
diff --git a/server/lib/redis.ts b/server/lib/redis.ts
index a075eee2d..4325598b2 100644
--- a/server/lib/redis.ts
+++ b/server/lib/redis.ts
@@ -7,7 +7,7 @@ import {
7 USER_EMAIL_VERIFY_LIFETIME, 7 USER_EMAIL_VERIFY_LIFETIME,
8 USER_PASSWORD_RESET_LIFETIME, 8 USER_PASSWORD_RESET_LIFETIME,
9 USER_PASSWORD_CREATE_LIFETIME, 9 USER_PASSWORD_CREATE_LIFETIME,
10 VIDEO_VIEW_LIFETIME, 10 VIEW_LIFETIME,
11 WEBSERVER, 11 WEBSERVER,
12 TRACKER_RATE_LIMITS 12 TRACKER_RATE_LIMITS
13} from '../initializers/constants' 13} from '../initializers/constants'
@@ -118,8 +118,12 @@ class Redis {
118 118
119 /* ************ Views per IP ************ */ 119 /* ************ Views per IP ************ */
120 120
121 setIPVideoView (ip: string, videoUUID: string) { 121 setIPVideoView (ip: string, videoUUID: string, isLive: boolean) {
122 return this.setValue(this.generateViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME) 122 const lifetime = isLive
123 ? VIEW_LIFETIME.LIVE
124 : VIEW_LIFETIME.VIDEO
125
126 return this.setValue(this.generateViewKey(ip, videoUUID), '1', lifetime)
123 } 127 }
124 128
125 async doesVideoIPViewExist (ip: string, videoUUID: string) { 129 async doesVideoIPViewExist (ip: string, videoUUID: string) {