2 import { forkJoin } from 'rxjs'
3 import { AfterViewInit, Component, EventEmitter, OnInit, Output } from '@angular/core'
4 import { Router } from '@angular/router'
5 import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core'
6 import { scrollToTop } from '@app/helpers'
7 import { FormValidatorService } from '@app/shared/shared-forms'
8 import { Video, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main'
9 import { LiveVideoService } from '@app/shared/shared-video-live'
10 import { LoadingBarService } from '@ngx-loading-bar/core'
11 import { LiveVideo, LiveVideoCreate, LiveVideoUpdate, PeerTubeProblemDocument, ServerErrorCode } from '@shared/models'
12 import { VideoSend } from './video-send'
15 selector: 'my-video-go-live',
16 templateUrl: './video-go-live.component.html',
18 '../shared/video-edit.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 isInUpdateForm = false
32 videoShortUUID: string
37 protected formValidatorService: FormValidatorService,
38 protected loadingBar: LoadingBarService,
39 protected notifier: Notifier,
40 protected authService: AuthService,
41 protected serverService: ServerService,
42 protected videoService: VideoService,
43 protected videoCaptionService: VideoCaptionService,
44 private liveVideoService: LiveVideoService,
45 private router: Router,
46 private hooks: HooksService
56 this.hooks.runAction('action:go-live.init', 'video-edit')
60 return { canDeactivate: true }
66 const video: LiveVideoCreate = {
68 privacy: this.highestPrivacy,
69 nsfw: this.serverConfig.instance.isNSFW,
70 waitTranscoding: true,
71 commentsEnabled: true,
72 downloadEnabled: true,
75 channelId: this.firstStepChannelId
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)
82 this.liveVideoService.goLive(video)
85 this.videoId = res.video.id
86 this.videoUUID = res.video.uuid
87 this.videoShortUUID = res.video.shortUUID
88 this.isInUpdateForm = true
90 this.firstStepDone.emit(name)
96 this.firstStepError.emit()
98 let message = err.message
100 const error = err.body as PeerTubeProblemDocument
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`
108 this.notifier.error(message)
113 updateSecondStep () {
114 if (this.checkForm() === false) {
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
124 const liveVideoUpdate: LiveVideoUpdate = {
125 saveReplay: this.form.value.saveReplay,
126 permanentLive: this.form.value.permanentLive
131 this.updateVideoAndCaptions(video),
133 this.liveVideoService.updateLive(this.videoId, liveVideoUpdate)
136 this.notifier.success($localize`Live published.`)
138 this.router.navigateByUrl(Video.buildWatchUrl(video))
142 this.error = err.message
149 getMaxLiveDuration () {
150 return this.serverConfig.live.maxDuration / 1000
153 isWaitTranscodingEnabled () {
154 return this.form.value['saveReplay'] === true
157 private fetchVideoLive () {
158 this.liveVideoService.getVideoLive(this.videoId)
161 this.liveVideo = liveVideo
165 this.firstStepError.emit()
166 this.notifier.error(err.message)