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'
14 selector: 'my-video-go-live',
15 templateUrl: './video-go-live.component.html',
17 '../shared/video-edit.component.scss',
18 './video-go-live.component.scss',
22 export class VideoGoLiveComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
23 @Output() firstStepDone = new EventEmitter<string>()
24 @Output() firstStepError = new EventEmitter<void>()
26 firstStepPermanentLive: boolean
28 isInUpdateForm = false
34 videoShortUUID: string
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
58 this.hooks.runAction('action:go-live.init', 'video-edit')
62 return { canDeactivate: true }
68 const video: LiveVideoCreate = {
70 privacy: this.highestPrivacy,
71 nsfw: this.serverConfig.instance.isNSFW,
72 waitTranscoding: true,
73 commentsEnabled: true,
74 downloadEnabled: true,
75 permanentLive: this.firstStepPermanentLive,
76 saveReplay: this.firstStepPermanentLive === false && this.isReplayAllowed(),
77 channelId: this.firstStepChannelId
80 // Go live in private mode, but correctly fill the update form with the first user choice
81 const toPatch = Object.assign({}, video, { privacy: this.firstStepPrivacyId })
82 this.form.patchValue(toPatch)
84 this.liveVideoService.goLive(video)
87 this.videoId = res.video.id
88 this.videoUUID = res.video.uuid
89 this.videoShortUUID = res.video.shortUUID
90 this.isInUpdateForm = true
92 this.firstStepDone.emit(name)
98 this.firstStepError.emit()
100 let message = err.message
102 const error = err.body as PeerTubeProblemDocument
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`
110 this.notifier.error(message)
115 updateSecondStep () {
116 if (this.checkForm() === false) {
120 const video = new VideoEdit()
121 video.patch(this.form.value)
122 video.id = this.videoId
123 video.uuid = this.videoUUID
124 video.shortUUID = this.videoShortUUID
126 const liveVideoUpdate: LiveVideoUpdate = {
127 saveReplay: this.form.value.saveReplay,
128 permanentLive: this.form.value.permanentLive
133 this.updateVideoAndCaptions(video),
135 this.liveVideoService.updateLive(this.videoId, liveVideoUpdate)
138 this.notifier.success($localize`Live published.`)
140 this.router.navigateByUrl(Video.buildWatchUrl(video))
144 this.error = err.message
151 getMaxLiveDuration () {
152 return this.serverConfig.live.maxDuration / 1000
155 isWaitTranscodingEnabled () {
156 return this.form.value['saveReplay'] === true
159 getNormalLiveDescription () {
160 if (this.isReplayAllowed()) {
161 return $localize`Stream only once and save a replay of your live`
164 return $localize`Stream only once`
167 getPermanentLiveDescription () {
168 if (this.isReplayAllowed()) {
169 return $localize`Stream multiple times, replays can't be saved`
172 return $localize`Stream multiple times using the same URL`
175 private isReplayAllowed () {
176 return this.serverConfig.live.allowReplay
179 private fetchVideoLive () {
180 this.liveVideoService.getVideoLive(this.videoId)
183 this.liveVideo = liveVideo
187 this.firstStepError.emit()
188 this.notifier.error(err.message)