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