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,
73 channelId: this.firstStepChannelId
76 // Go live in private mode, but correctly fill the update form with the first user choice
77 const toPatch = Object.assign({}, video, { privacy: this.firstStepPrivacyId })
78 this.form.patchValue(toPatch)
80 this.liveVideoService.goLive(video)
83 this.videoId = res.video.id
84 this.videoUUID = res.video.uuid
85 this.videoShortUUID = res.video.shortUUID
86 this.isInUpdateForm = true
88 this.firstStepDone.emit(name)
94 this.firstStepError.emit()
96 let message = err.message
98 const error = err.body as PeerTubeProblemDocument
100 if (error?.code === ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED) {
101 message = $localize`Cannot create live because this instance have too many created lives`
102 } else if (error?.code === ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED) {
103 message = $localize`Cannot create live because you created too many lives`
106 this.notifier.error(message)
111 updateSecondStep () {
112 if (this.checkForm() === false) {
116 const video = new VideoEdit()
117 video.patch(this.form.value)
118 video.id = this.videoId
119 video.uuid = this.videoUUID
120 video.shortUUID = this.videoShortUUID
122 const liveVideoUpdate: LiveVideoUpdate = {
123 saveReplay: this.form.value.saveReplay,
124 permanentLive: this.form.value.permanentLive
129 this.updateVideoAndCaptions(video),
131 this.liveVideoService.updateLive(this.videoId, liveVideoUpdate)
134 this.notifier.success($localize`Live published.`)
136 this.router.navigateByUrl(Video.buildWatchUrl(video))
140 this.error = err.message
147 getMaxLiveDuration () {
148 return this.serverConfig.live.maxDuration / 1000
151 isWaitTranscodingEnabled () {
152 return this.form.value['saveReplay'] === true
155 private fetchVideoLive () {
156 this.liveVideoService.getVideoLive(this.videoId)
159 this.liveVideo = liveVideo
163 this.firstStepError.emit()
164 this.notifier.error(err.message)