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, LiveVideoLatencyMode, 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 permanentLive: this.firstStepPermanentLive,
74 latencyMode: LiveVideoLatencyMode.DEFAULT,
75 saveReplay: this.isReplayAllowed(),
76 channelId: this.firstStepChannelId
79 // Go live in private mode, but correctly fill the update form with the first user choice
80 const toPatch = { ...video, privacy: this.firstStepPrivacyId }
81 this.form.patchValue(toPatch)
83 this.liveVideoService.goLive(video)
86 this.videoId = res.video.id
87 this.videoUUID = res.video.uuid
88 this.videoShortUUID = res.video.shortUUID
89 this.isInUpdateForm = true
91 this.firstStepDone.emit(name)
97 this.firstStepError.emit()
99 let message = err.message
101 const error = err.body as PeerTubeProblemDocument
103 if (error?.code === ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED) {
104 message = $localize`Cannot create live because this instance have too many created lives`
105 } else if (error?.code === ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED) {
106 message = $localize`Cannot create live because you created too many lives`
109 this.notifier.error(message)
114 async updateSecondStep () {
115 if (!await this.isFormValid()) return
117 const video = new VideoEdit()
118 video.patch(this.form.value)
119 video.id = this.videoId
120 video.uuid = this.videoUUID
121 video.shortUUID = this.videoShortUUID
123 const liveVideoUpdate: LiveVideoUpdate = {
124 saveReplay: this.form.value.saveReplay,
125 latencyMode: this.form.value.latencyMode,
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 getNormalLiveDescription () {
158 if (this.isReplayAllowed()) {
159 return $localize`Stream only once, replay will replace your live`
162 return $localize`Stream only once`
165 getPermanentLiveDescription () {
166 if (this.isReplayAllowed()) {
167 return $localize`Stream multiple times, replays will be separate videos`
170 return $localize`Stream multiple times using the same URL`
173 private isReplayAllowed () {
174 return this.serverConfig.live.allowReplay
177 private fetchVideoLive () {
178 this.liveVideoService.getVideoLive(this.videoId)
181 this.liveVideo = liveVideo
185 this.firstStepError.emit()
186 this.notifier.error(err.message)