]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-edit/video-add-components/video-go-live.component.ts
91eb669311a54f1efff9ed43a771dfb924be51b4
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-add-components / video-go-live.component.ts
1 import { forkJoin } from 'rxjs'
2 import { AfterViewInit, Component, EventEmitter, OnInit, Output } from '@angular/core'
3 import { Router } from '@angular/router'
4 import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core'
5 import { scrollToTop } from '@app/helpers'
6 import { FormValidatorService } from '@app/shared/shared-forms'
7 import { Video, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main'
8 import { LiveVideoService } from '@app/shared/shared-video-live'
9 import { LoadingBarService } from '@ngx-loading-bar/core'
10 import { logger } from '@root-helpers/logger'
11 import { LiveVideo, LiveVideoCreate, LiveVideoLatencyMode, LiveVideoUpdate, PeerTubeProblemDocument, ServerErrorCode } from '@shared/models'
12 import { VideoSend } from './video-send'
13
14 @Component({
15 selector: 'my-video-go-live',
16 templateUrl: './video-go-live.component.html',
17 styleUrls: [
18 '../shared/video-edit.component.scss',
19 './video-go-live.component.scss',
20 './video-send.scss'
21 ]
22 })
23 export class VideoGoLiveComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
24 @Output() firstStepDone = new EventEmitter<string>()
25 @Output() firstStepError = new EventEmitter<void>()
26
27 firstStepPermanentLive: boolean
28
29 isInUpdateForm = false
30
31 liveVideo: LiveVideo
32
33 videoId: number
34 videoUUID: string
35 videoShortUUID: string
36
37 error: string
38
39 constructor (
40 protected formValidatorService: FormValidatorService,
41 protected loadingBar: LoadingBarService,
42 protected notifier: Notifier,
43 protected authService: AuthService,
44 protected serverService: ServerService,
45 protected videoService: VideoService,
46 protected videoCaptionService: VideoCaptionService,
47 private liveVideoService: LiveVideoService,
48 private router: Router,
49 private hooks: HooksService
50 ) {
51 super()
52 }
53
54 ngOnInit () {
55 super.ngOnInit()
56 }
57
58 ngAfterViewInit () {
59 this.hooks.runAction('action:go-live.init', 'video-edit')
60 }
61
62 canDeactivate () {
63 return { canDeactivate: true }
64 }
65
66 goLive () {
67 const name = 'Live'
68
69 const video: LiveVideoCreate = {
70 name,
71 privacy: this.highestPrivacy,
72 nsfw: this.serverConfig.instance.isNSFW,
73 waitTranscoding: true,
74 permanentLive: this.firstStepPermanentLive,
75 latencyMode: LiveVideoLatencyMode.DEFAULT,
76 saveReplay: this.isReplayAllowed(),
77 channelId: this.firstStepChannelId
78 }
79
80 // Go live in private mode, but correctly fill the update form with the first user choice
81 const toPatch = { ...video, privacy: this.firstStepPrivacyId }
82 this.form.patchValue(toPatch)
83
84 this.liveVideoService.goLive(video)
85 .subscribe({
86 next: res => {
87 this.videoId = res.video.id
88 this.videoUUID = res.video.uuid
89 this.videoShortUUID = res.video.shortUUID
90 this.isInUpdateForm = true
91
92 this.firstStepDone.emit(name)
93
94 this.fetchVideoLive()
95 },
96
97 error: err => {
98 this.firstStepError.emit()
99
100 let message = err.message
101
102 const error = err.body as PeerTubeProblemDocument
103
104 if (error?.code === ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED) {
105 message = $localize`Cannot create live because this instance have too many created lives`
106 } else if (error?.code === ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED) {
107 message = $localize`Cannot create live because you created too many lives`
108 }
109
110 this.notifier.error(message)
111 }
112 })
113 }
114
115 async updateSecondStep () {
116 if (!await this.isFormValid()) return
117
118 const video = new VideoEdit()
119 video.patch(this.form.value)
120 video.id = this.videoId
121 video.uuid = this.videoUUID
122 video.shortUUID = this.videoShortUUID
123
124 const liveVideoUpdate: LiveVideoUpdate = {
125 saveReplay: this.form.value.saveReplay,
126 latencyMode: this.form.value.latencyMode,
127 permanentLive: this.form.value.permanentLive
128 }
129
130 // Update the video
131 forkJoin([
132 this.updateVideoAndCaptions(video),
133
134 this.liveVideoService.updateLive(this.videoId, liveVideoUpdate)
135 ]).subscribe({
136 next: () => {
137 this.notifier.success($localize`Live published.`)
138
139 this.router.navigateByUrl(Video.buildWatchUrl(video))
140 },
141
142 error: err => {
143 this.error = err.message
144 scrollToTop()
145 logger.error(err)
146 }
147 })
148 }
149
150 getMaxLiveDuration () {
151 return this.serverConfig.live.maxDuration / 1000
152 }
153
154 isWaitTranscodingEnabled () {
155 return this.form.value['saveReplay'] === true
156 }
157
158 getNormalLiveDescription () {
159 if (this.isReplayAllowed()) {
160 return $localize`Stream only once, replay will replace your live`
161 }
162
163 return $localize`Stream only once`
164 }
165
166 getPermanentLiveDescription () {
167 if (this.isReplayAllowed()) {
168 return $localize`Stream multiple times, replays will be separate videos`
169 }
170
171 return $localize`Stream multiple times using the same URL`
172 }
173
174 private isReplayAllowed () {
175 return this.serverConfig.live.allowReplay
176 }
177
178 private fetchVideoLive () {
179 this.liveVideoService.getVideoLive(this.videoId)
180 .subscribe({
181 next: liveVideo => {
182 this.liveVideo = liveVideo
183 },
184
185 error: err => {
186 this.firstStepError.emit()
187 this.notifier.error(err.message)
188 }
189 })
190 }
191 }