]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/standalone/videos/shared/live-manager.ts
Correctly terminate an ended live
[github/Chocobozzz/PeerTube.git] / client / src / standalone / videos / shared / live-manager.ts
1 import { Socket } from 'socket.io-client'
2 import { LiveVideoEventPayload, VideoDetails, VideoState } from '../../../../../shared/models'
3 import { PlayerHTML } from './player-html'
4 import { Translations } from './translations'
5
6 export class LiveManager {
7 private liveSocket: Socket
8
9 private listeners = new Map<string, (payload: LiveVideoEventPayload) => void>()
10
11 constructor (
12 private readonly playerHTML: PlayerHTML
13 ) {
14
15 }
16
17 async listenForChanges (options: {
18 video: VideoDetails
19 onPublishedVideo: () => any
20 }) {
21 const { video, onPublishedVideo } = options
22
23 if (!this.liveSocket) {
24 const io = (await import('socket.io-client')).io
25 this.liveSocket = io(window.location.origin + '/live-videos')
26 }
27
28 const listener = (payload: LiveVideoEventPayload) => {
29 if (payload.state === VideoState.PUBLISHED) {
30 this.playerHTML.removeInformation()
31 onPublishedVideo()
32 return
33 }
34 }
35
36 this.liveSocket.on('state-change', listener)
37 this.listeners.set(video.uuid, listener)
38
39 this.liveSocket.emit('subscribe', { videoId: video.id })
40 }
41
42 stopListeningForChanges (video: VideoDetails) {
43 const listener = this.listeners.get(video.uuid)
44 if (listener) {
45 this.liveSocket.off('state-change', listener)
46 }
47
48 this.liveSocket.emit('unsubscribe', { videoId: video.id })
49 }
50
51 displayInfo (options: {
52 state: VideoState
53 translations: Translations
54 }) {
55 const { state, translations } = options
56
57 if (state === VideoState.WAITING_FOR_LIVE) {
58 this.displayWaitingForLiveInfo(translations)
59 return
60 }
61
62 if (state === VideoState.LIVE_ENDED) {
63 this.displayEndedLiveInfo(translations)
64 return
65 }
66 }
67
68 private displayWaitingForLiveInfo (translations: Translations) {
69 this.playerHTML.displayInformation('This live has not started yet.', translations)
70 }
71
72 private displayEndedLiveInfo (translations: Translations) {
73 this.playerHTML.displayInformation('This live has ended.', translations)
74
75 }
76 }