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 { logger } from '@root-helpers/logger'
11 import { LiveVideo, LiveVideoCreate, LiveVideoLatencyMode, 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',
19 './video-go-live.component.scss',
23 export class VideoGoLiveComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
24 @Output() firstStepDone = new EventEmitter<string>()
25 @Output() firstStepError = new EventEmitter<void>()
27 firstStepPermanentLive: boolean
29 isInUpdateForm = false
35 videoShortUUID: string
40 protected formValidatorService: FormValidatorService,
41 protected loadingBar: LoadingBarService,
42 protected notifier: Notifier,
43 protected authService: AuthService,
44 protected serverService: ServerService,
45 protected videoService: VideoService,
46 protected videoCaptionService: VideoCaptionService,
47 private liveVideoService: LiveVideoService,
48 private router: Router,
49 private hooks: HooksService
59 this.hooks.runAction('action:go-live.init', 'video-edit')
63 return { canDeactivate: true }
69 const video: LiveVideoCreate = {
71 privacy: this.highestPrivacy,
72 nsfw: this.serverConfig.instance.isNSFW,
73 waitTranscoding: true,
74 permanentLive: this.firstStepPermanentLive,
75 latencyMode: LiveVideoLatencyMode.DEFAULT,
76 saveReplay: 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 = { ...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 async updateSecondStep () {
116 if (!await this.isFormValid()) return
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 latencyMode: this.form.value.latencyMode,
127 permanentLive: this.form.value.permanentLive
132 this.updateVideoAndCaptions(video),
134 this.liveVideoService.updateLive(this.videoId, liveVideoUpdate)
137 this.notifier.success($localize`Live published.`)
139 this.router.navigateByUrl(Video.buildWatchUrl(video))
143 this.error = err.message
150 getMaxLiveDuration () {
151 return this.serverConfig.live.maxDuration / 1000
154 isWaitTranscodingEnabled () {
155 return this.form.value['saveReplay'] === true
158 getNormalLiveDescription () {
159 if (this.isReplayAllowed()) {
160 return $localize`Stream only once, replay will replace your live`
163 return $localize`Stream only once`
166 getPermanentLiveDescription () {
167 if (this.isReplayAllowed()) {
168 return $localize`Stream multiple times, replays will be separate videos`
171 return $localize`Stream multiple times using the same URL`
174 private isReplayAllowed () {
175 return this.serverConfig.live.allowReplay
178 private fetchVideoLive () {
179 this.liveVideoService.getVideoLive(this.videoId)
182 this.liveVideo = liveVideo
186 this.firstStepError.emit()
187 this.notifier.error(err.message)