2 import { forkJoin } from 'rxjs'
3 import { Component, EventEmitter, OnInit, Output } from '@angular/core'
4 import { Router } from '@angular/router'
5 import { AuthService, CanComponentDeactivate, Notifier, ServerService } from '@app/core'
6 import { scrollToTop } from '@app/helpers'
7 import { FormValidatorService } from '@app/shared/shared-forms'
8 import { 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, ServerErrorCode, VideoPrivacy } 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, CanComponentDeactivate {
23 @Output() firstStepDone = new EventEmitter<string>()
24 @Output() firstStepError = new EventEmitter<void>()
26 isInUpdateForm = false
33 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
36 protected formValidatorService: FormValidatorService,
37 protected loadingBar: LoadingBarService,
38 protected notifier: Notifier,
39 protected authService: AuthService,
40 protected serverService: ServerService,
41 protected videoService: VideoService,
42 protected videoCaptionService: VideoCaptionService,
43 private liveVideoService: LiveVideoService,
44 private router: Router
54 return { canDeactivate: true }
58 const video: LiveVideoCreate = {
60 privacy: VideoPrivacy.PRIVATE,
61 nsfw: this.serverConfig.instance.isNSFW,
62 waitTranscoding: true,
63 commentsEnabled: true,
64 downloadEnabled: true,
65 channelId: this.firstStepChannelId
68 // Go live in private mode, but correctly fill the update form with the first user choice
69 const toPatch = Object.assign({}, video, { privacy: this.firstStepPrivacyId })
70 this.form.patchValue(toPatch)
72 this.liveVideoService.goLive(video).subscribe(
74 this.videoId = res.video.id
75 this.videoUUID = res.video.uuid
76 this.isInUpdateForm = true
78 this.firstStepDone.emit(name)
84 this.firstStepError.emit()
86 let message = err.message
88 if (err.body?.code === ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED) {
89 message = $localize`Cannot create live because this instance have too many created lives`
90 } else if (err.body?.code) {
91 message = $localize`Cannot create live because you created too many lives`
94 this.notifier.error(message)
100 if (this.checkForm() === false) {
104 const video = new VideoEdit()
105 video.patch(this.form.value)
106 video.id = this.videoId
107 video.uuid = this.videoUUID
109 const liveVideoUpdate: LiveVideoUpdate = {
110 saveReplay: this.form.value.saveReplay,
111 permanentLive: this.form.value.permanentLive
116 this.updateVideoAndCaptions(video),
118 this.liveVideoService.updateLive(this.videoId, liveVideoUpdate)
121 this.notifier.success($localize`Live published.`)
123 this.router.navigate(['/videos/watch', video.uuid])
127 this.error = err.message
134 getMaxLiveDuration () {
135 return this.serverConfig.live.maxDuration / 1000
138 private fetchVideoLive () {
139 this.liveVideoService.getVideoLive(this.videoId)
142 this.liveVideo = liveVideo
146 this.firstStepError.emit()
147 this.notifier.error(err.message)