From d3f4689bded2a6f5b589fe79c3f8b6082d553d9e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 31 May 2022 14:18:41 +0200 Subject: Add live autostart/messages in embed --- client/src/standalone/videos/shared/index.ts | 1 + .../src/standalone/videos/shared/live-manager.ts | 69 ++++++++++++++++++++++ client/src/standalone/videos/shared/player-html.ts | 15 +++++ 3 files changed, 85 insertions(+) create mode 100644 client/src/standalone/videos/shared/live-manager.ts (limited to 'client/src/standalone/videos/shared') diff --git a/client/src/standalone/videos/shared/index.ts b/client/src/standalone/videos/shared/index.ts index 4b4e05b7c..928b8e270 100644 --- a/client/src/standalone/videos/shared/index.ts +++ b/client/src/standalone/videos/shared/index.ts @@ -1,5 +1,6 @@ export * from './auth-http' export * from './peertube-plugin' +export * from './live-manager' export * from './player-html' export * from './player-manager-options' export * from './playlist-fetcher' diff --git a/client/src/standalone/videos/shared/live-manager.ts b/client/src/standalone/videos/shared/live-manager.ts new file mode 100644 index 000000000..422d39793 --- /dev/null +++ b/client/src/standalone/videos/shared/live-manager.ts @@ -0,0 +1,69 @@ +import { Socket } from 'socket.io-client' +import { LiveVideoEventPayload, VideoDetails, VideoState } from '../../../../../shared/models' +import { PlayerHTML } from './player-html' +import { Translations } from './translations' + +export class LiveManager { + private liveSocket: Socket + + constructor ( + private readonly playerHTML: PlayerHTML + ) { + + } + + async displayInfoAndListenForChanges (options: { + video: VideoDetails + translations: Translations + onPublishedVideo: () => any + }) { + const { video, onPublishedVideo } = options + + this.displayAppropriateInfo(options) + + if (!this.liveSocket) { + const io = (await import('socket.io-client')).io + this.liveSocket = io(window.location.origin + '/live-videos') + } + + this.liveSocket.on('state-change', (payload: LiveVideoEventPayload) => { + if (payload.state === VideoState.PUBLISHED) { + this.playerHTML.removeInformation() + onPublishedVideo() + return + } + }) + + this.liveSocket.emit('subscribe', { videoId: video.id }) + } + + stopListeningForChanges (video: VideoDetails) { + this.liveSocket.emit('unsubscribe', { videoId: video.id }) + } + + private displayAppropriateInfo (options: { + video: VideoDetails + translations: Translations + }) { + const { video, translations } = options + + if (video.state.id === VideoState.WAITING_FOR_LIVE) { + this.displayWaitingForLiveInfo(translations) + return + } + + if (video.state.id === VideoState.LIVE_ENDED) { + this.displayEndedLiveInfo(translations) + return + } + } + + private displayWaitingForLiveInfo (translations: Translations) { + this.playerHTML.displayInformation('This live has not started yet.', translations) + } + + private displayEndedLiveInfo (translations: Translations) { + this.playerHTML.displayInformation('This live has ended.', translations) + + } +} diff --git a/client/src/standalone/videos/shared/player-html.ts b/client/src/standalone/videos/shared/player-html.ts index 110124417..eb6324ac7 100644 --- a/client/src/standalone/videos/shared/player-html.ts +++ b/client/src/standalone/videos/shared/player-html.ts @@ -6,6 +6,7 @@ export class PlayerHTML { private readonly wrapperElement: HTMLElement private playerElement: HTMLVideoElement + private informationElement: HTMLDivElement constructor (private readonly videoWrapperId: string) { this.wrapperElement = document.getElementById(this.videoWrapperId) @@ -66,6 +67,20 @@ export class PlayerHTML { placeholder.style.display = 'none' } + displayInformation (text: string, translations: Translations) { + if (this.informationElement) this.removeInformation() + + this.informationElement = document.createElement('div') + this.informationElement.className = 'player-information' + this.informationElement.innerText = peertubeTranslate(text, translations) + + document.body.appendChild(this.informationElement) + } + + removeInformation () { + this.removeElement(this.informationElement) + } + private getPlaceholderElement () { return document.getElementById('placeholder-preview') } -- cgit v1.2.3