aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-12-09 15:00:02 +0100
committerChocobozzz <me@florianbigard.com>2020-12-09 16:31:42 +0100
commita800dbf345e856ab790e7b3ab9a97e8c5dfa0a32 (patch)
treeb7f814ee0dae543a6f2937b9ab5154cfeff3e986 /server
parent5cac83a78d6ba5212755fafce2ed25f7c74b04e5 (diff)
downloadPeerTube-a800dbf345e856ab790e7b3ab9a97e8c5dfa0a32.tar.gz
PeerTube-a800dbf345e856ab790e7b3ab9a97e8c5dfa0a32.tar.zst
PeerTube-a800dbf345e856ab790e7b3ab9a97e8c5dfa0a32.zip
Live views update
Diffstat (limited to 'server')
-rw-r--r--server/lib/activitypub/videos.ts9
-rw-r--r--server/lib/job-queue/handlers/video-live-ending.ts2
-rw-r--r--server/lib/live-manager.ts2
-rw-r--r--server/lib/peertube-socket.ts13
-rw-r--r--server/tests/api/live/live.ts51
5 files changed, 72 insertions, 5 deletions
diff --git a/server/lib/activitypub/videos.ts b/server/lib/activitypub/videos.ts
index cb462e258..8545e5bad 100644
--- a/server/lib/activitypub/videos.ts
+++ b/server/lib/activitypub/videos.ts
@@ -461,8 +461,13 @@ async function updateVideoFromAP (options: {
461 transaction: undefined 461 transaction: undefined
462 }) 462 })
463 463
464 if (wasPrivateVideo || wasUnlistedVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoUpdated) // Notify our users? 464 // Notify our users?
465 if (videoUpdated.isLive) PeerTubeSocket.Instance.sendVideoLiveNewState(videoUpdated) 465 if (wasPrivateVideo || wasUnlistedVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoUpdated)
466
467 if (videoUpdated.isLive) {
468 PeerTubeSocket.Instance.sendVideoLiveNewState(videoUpdated)
469 PeerTubeSocket.Instance.sendVideoViewsUpdate(videoUpdated)
470 }
466 471
467 logger.info('Remote video with uuid %s updated', videoObject.uuid) 472 logger.info('Remote video with uuid %s updated', videoObject.uuid)
468 473
diff --git a/server/lib/job-queue/handlers/video-live-ending.ts b/server/lib/job-queue/handlers/video-live-ending.ts
index 93d925830..8018e2277 100644
--- a/server/lib/job-queue/handlers/video-live-ending.ts
+++ b/server/lib/job-queue/handlers/video-live-ending.ts
@@ -91,7 +91,7 @@ async function saveLive (video: MVideo, live: MVideoLive) {
91 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id) 91 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
92 hlsPlaylist.VideoFiles = [] 92 hlsPlaylist.VideoFiles = []
93 93
94 let durationDone: boolean 94 let durationDone = false
95 95
96 for (const playlistFile of playlistFiles) { 96 for (const playlistFile of playlistFiles) {
97 const concatenatedTsFile = LiveManager.Instance.buildConcatenatedName(playlistFile) 97 const concatenatedTsFile = LiveManager.Instance.buildConcatenatedName(playlistFile)
diff --git a/server/lib/live-manager.ts b/server/lib/live-manager.ts
index 5d9b68756..2fb4b774c 100644
--- a/server/lib/live-manager.ts
+++ b/server/lib/live-manager.ts
@@ -537,6 +537,8 @@ class LiveManager {
537 537
538 await federateVideoIfNeeded(video, false) 538 await federateVideoIfNeeded(video, false)
539 539
540 PeerTubeSocket.Instance.sendVideoViewsUpdate(video)
541
540 // Only keep not expired watchers 542 // Only keep not expired watchers
541 const newWatchers = watchers.filter(w => w > notBefore) 543 const newWatchers = watchers.filter(w => w > notBefore)
542 this.watchersPerVideo.set(videoId, newWatchers) 544 this.watchersPerVideo.set(videoId, newWatchers)
diff --git a/server/lib/peertube-socket.ts b/server/lib/peertube-socket.ts
index 5fc5bc20b..e27963e60 100644
--- a/server/lib/peertube-socket.ts
+++ b/server/lib/peertube-socket.ts
@@ -69,7 +69,18 @@ class PeerTubeSocket {
69 const data: LiveVideoEventPayload = { state: video.state } 69 const data: LiveVideoEventPayload = { state: video.state }
70 const type: LiveVideoEventType = 'state-change' 70 const type: LiveVideoEventType = 'state-change'
71 71
72 logger.debug('Sending video live new state notification of %s.', video.url) 72 logger.debug('Sending video live new state notification of %s.', video.url, { state: video.state })
73
74 this.liveVideosNamespace
75 .in(video.id)
76 .emit(type, data)
77 }
78
79 sendVideoViewsUpdate (video: MVideo) {
80 const data: LiveVideoEventPayload = { views: video.views }
81 const type: LiveVideoEventType = 'views-change'
82
83 logger.debug('Sending video live views update notification of %s.', video.url, { views: video.views })
73 84
74 this.liveVideosNamespace 85 this.liveVideosNamespace
75 .in(video.id) 86 .in(video.id)
diff --git a/server/tests/api/live/live.ts b/server/tests/api/live/live.ts
index e728fcce0..6d504f742 100644
--- a/server/tests/api/live/live.ts
+++ b/server/tests/api/live/live.ts
@@ -328,7 +328,7 @@ describe('Test live', function () {
328 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions) 328 await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions)
329 329
330 for (let i = 0; i < resolutions.length; i++) { 330 for (let i = 0; i < resolutions.length; i++) {
331 const segmentNum = 2 331 const segmentNum = 3
332 const segmentName = `${i}-00000${segmentNum}.ts` 332 const segmentName = `${i}-00000${segmentNum}.ts`
333 await waitUntilLiveSegmentGeneration(servers[0], video.uuid, i, segmentNum) 333 await waitUntilLiveSegmentGeneration(servers[0], video.uuid, i, segmentNum)
334 334
@@ -608,6 +608,55 @@ describe('Test live', function () {
608 } 608 }
609 }) 609 })
610 610
611 it('Should correctly send views change notification', async function () {
612 this.timeout(60000)
613
614 let localLastVideoViews = 0
615 let remoteLastVideoViews = 0
616
617 const liveVideoUUID = await createLiveWrapper()
618 await waitJobs(servers)
619
620 {
621 const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
622
623 const localSocket = getLiveNotificationSocket(servers[0].url)
624 localSocket.on('views-change', data => { localLastVideoViews = data.views })
625 localSocket.emit('subscribe', { videoId })
626 }
627
628 {
629 const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
630
631 const remoteSocket = getLiveNotificationSocket(servers[1].url)
632 remoteSocket.on('views-change', data => { remoteLastVideoViews = data.views })
633 remoteSocket.emit('subscribe', { videoId })
634 }
635
636 const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
637
638 for (const server of servers) {
639 await waitUntilLivePublished(server.url, server.accessToken, liveVideoUUID)
640 }
641
642 await waitJobs(servers)
643
644 expect(localLastVideoViews).to.equal(0)
645 expect(remoteLastVideoViews).to.equal(0)
646
647 await viewVideo(servers[0].url, liveVideoUUID)
648 await viewVideo(servers[1].url, liveVideoUUID)
649
650 await waitJobs(servers)
651 await wait(5000)
652 await waitJobs(servers)
653
654 expect(localLastVideoViews).to.equal(2)
655 expect(remoteLastVideoViews).to.equal(2)
656
657 await stopFfmpeg(command)
658 })
659
611 it('Should not receive a notification after unsubscribe', async function () { 660 it('Should not receive a notification after unsubscribe', async function () {
612 this.timeout(60000) 661 this.timeout(60000)
613 662